Index: fs.c
===================================================================
--- fs.c	(revision 4126)
+++ fs.c	(working copy)
@@ -124,6 +124,23 @@
 	return 0;
 }
 
+int rom_extract(struct rom *rom, const char *name, void** buf, unsigned long *size )
+{
+	struct cbfs_file *c = rom_find_by_name(rom, name);
+	unsigned int csize;
+
+	if (c == NULL) {
+		ERROR("Component %s does not exist\n", name);
+		return -1;
+	}
+
+	*size = ntohl(c->len);
+
+	csize = sizeof(struct cbfs_file) + ALIGN(strlen(name) + 1, 16);
+	*buf = ((unsigned char *)c) + csize;
+	return 0;
+}
+
 int rom_add(struct rom *rom, const char *name, void *buffer, int size, int type)
 {
 	struct cbfs_file *c = rom_find_empty(rom);
@@ -140,22 +157,20 @@
 		return -1;
 	}
 
-	csize = sizeof(struct cbfs_file) + ALIGN(strlen(name), 16) + size;
+	csize = sizeof(struct cbfs_file) + ALIGN(strlen(name) + 1, 16);
 
 	offset = ROM_OFFSET(rom, c);
 
-	if (offset + csize >= rom->fssize) {
+	if (offset + csize + size > rom->fssize) {
 		ERROR("There is not enough room in this ROM for this\n");
 		ERROR("component. I need %d bytes, only have %d bytes avail\n",
-		      csize, rom->fssize - offset);
+		      csize + size, rom->fssize - offset);
 
 		return -1;
 	}
 
 	strcpy(c->magic, COMPONENT_MAGIC);
 
-	csize = sizeof(struct cbfs_file) + ALIGN(strlen(name) + 1, 16);
-
 	c->len = htonl(size);
 	c->offset = htonl(csize);
 	c->type = htonl(type);
Index: extract.c
===================================================================
--- extract.c	(revision 0)
+++ extract.c	(revision 0)
@@ -0,0 +1,74 @@
+/*
+ * cbfstool
+ *
+ * Copyright (C) 2009 Myles Watson <mylesgw@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
+ */
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "cbfstool.h"
+
+static int extract_blob(struct rom *rom, const char *filename, const char *name)
+{
+	void *buf;
+	int fd, ret;
+	unsigned long size;
+
+	ret = rom_extract(rom, name, &buf, &size);
+
+	if (ret == -1) 
+		return -1;
+
+	fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
+
+	if (fd == -1) {
+		ERROR("Could not open %s: %m\n", filename);
+		return -1;
+	}
+
+	if (write(fd, buf, size) != size) {
+		ERROR("Couldn't write %ld bytes!\n", size);
+		ret = -1;
+	}
+
+	close(fd);
+
+	return ret;
+}
+
+void extract_usage(void)
+{
+	printf("extract [FILE] [NAME] \textract a component\n");
+}
+
+int extract_handler(struct rom *rom, int argc, char **argv)
+{
+	if (argc < 2) {
+		extract_usage();
+		return -1;
+	}
+
+	if (!rom_exists(rom)) {
+		ERROR("Can't find the ROM!\n");
+		return -1;
+	}
+
+	return extract_blob(rom, argv[0], argv[1]);
+}
+
Index: cbfstool.c
===================================================================
--- cbfstool.c	(revision 4126)
+++ cbfstool.c	(working copy)
@@ -20,6 +20,7 @@
 /* v2 compat: First, assumes a 64K bootblock. 
  * cbfstool coreboot.rom create 0x80000 coreboot.strip 
  * cbfstool coreboot.rom add-payload /tmp/filo.elf payload 
+ * cbfstool coreboot.rom extract new_filo.elf payload 
  * cbfstool coreboot.rom print
  */
 
@@ -34,6 +35,7 @@
 extern int bootblock_handler(struct rom *, int, char **);
 extern int print_handler(struct rom *, int, char **);
 extern int add_handler(struct rom *, int, char **);
+extern int extract_handler(struct rom *, int, char **);
 extern int delete_handler(struct rom *, int, char **);
 extern int resize_handler(struct rom *, int, char **);
 extern int add_payload_handler(struct rom *, int, char **);
@@ -44,6 +46,7 @@
 extern void print_usage(void);
 extern void add_usage(void);
 extern void delete_usage(void);
+extern void extract_usage(void);
 extern void resize_usage(void);
 extern void add_payload_usage(void);
 extern void add_stage_usage(void);
@@ -60,6 +63,7 @@
 	"bootblock", bootblock_handler, bootblock_usage}, {
 	"create", create_handler, create_usage}, {
 	"delete", delete_handler, delete_usage}, {
+	"extract", extract_handler, extract_usage}, {
 	"print", print_handler, print_usage}, {
 	"resize", resize_handler, resize_usage}, {
 "", NULL},};
Index: cbfstool.h
===================================================================
--- cbfstool.h	(revision 4126)
+++ cbfstool.h	(working copy)
@@ -70,6 +70,7 @@
 struct cbfs_file *rom_find_first(struct rom *);
 struct cbfs_file *rom_find_next(struct rom *, struct cbfs_file *);
 int rom_add(struct rom *rom, const char *name, void *, int size, int type);
+int rom_extract(struct rom *rom, const char *name, void **buf, unsigned long *size);
 int rom_remove(struct rom *rom, const char *name);
 unsigned int rom_used_space(struct rom *rom);
 int rom_exists(struct rom *rom);
Index: Makefile
===================================================================
--- Makefile	(revision 4126)
+++ Makefile	(working copy)
@@ -4,7 +4,7 @@
 
 obj ?= $(shell pwd)
 
-COMMANDS=create.o bootblock.o delete.o add.o print.o resize.o
+COMMANDS=create.o bootblock.o delete.o extract.o add.o print.o resize.o
 OBJ=$(COMMANDS) cbfstool.o util.o fs.o
 INC=cbfstool.h cbfs.h
 
