This patch adds
authorMyles Watson <mylesgw@gmail.com>
Mon, 20 Apr 2009 21:38:11 +0000 (21:38 +0000)
committerMyles Watson <mylesgw@gmail.com>
Mon, 20 Apr 2009 21:38:11 +0000 (21:38 +0000)
cbfstool extract [FILE] [NAME]

It also factors out the csize calculation in rom_add, and fixes rom_delete so
that it can handle deleting the last entry.

Signed-off-by: Myles Watson <mylesgw@gmail.com>
Acked-by: Ronald G. Minnich <rminnich@gmail.com>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@4144 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1

util/cbfstool/Makefile
util/cbfstool/cbfstool.c
util/cbfstool/cbfstool.h
util/cbfstool/extract.c [new file with mode: 0644]
util/cbfstool/fs.c

index 919021d667de75634cd7be10dcb84bd99e9c9313..39af38b2805266eaf97d649b66e03dcb87f9c3ec 100644 (file)
@@ -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
 
index b9915e3fac7134e8289839b08b8472a878afe3bf..cbf448163f3273dd0256792b479a63ad91b33af9 100644 (file)
@@ -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 create_handler(struct rom *, int, char **);
 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 bootblock_usage(void);
 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 @@ struct {
        "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 43abb6cb17177ade675a2ae31b8bdddd8afd583f..f36b28684c5019e2d2d82509af4f272a410e028a 100644 (file)
@@ -70,6 +70,7 @@ struct cbfs_file *rom_find(struct rom *rom, unsigned int offset);
 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);
diff --git a/util/cbfstool/extract.c b/util/cbfstool/extract.c
new file mode 100644 (file)
index 0000000..d694476
--- /dev/null
@@ -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 c5e9892a8e1cb369a6471f14a93902a605cea07c..ff5741e37e6737aca183e6e46f772aa008dcf665 100644 (file)
@@ -110,17 +110,43 @@ int rom_remove(struct rom *rom, const char *name)
                return -1;
        }
 
-       /* Get the next component - and copy it into the current
-          space */
+       /* Get the next component - and copy it into the current space if it
+        * exists.  If there is no next component, just delete c. */
 
        n = rom_find_next(rom, c);
+       
+       if (n != NULL) {
+               memcpy(c, n, rom->fssize - ROM_OFFSET(rom, n));
+               clear = ROM_OFFSET(rom, n) - ROM_OFFSET(rom, c);
+       }
+       else { /* No component after this one. */
+               unsigned int csize;
+               csize = sizeof(struct cbfs_file) + ALIGN(strlen(name) + 1, 16);
+               clear = ntohl(c->len) + csize;
+               memcpy(c, ((void*)c) + clear, 
+                      rom->fssize - (ROM_OFFSET(rom, c)+clear));
+       }
 
-       memcpy(c, n, rom->fssize - ROM_OFFSET(rom, n));
+       /* Zero the new space, which is always at the end. */
+       memset(ROM_PTR(rom, rom->fssize - clear), 0, clear);
+
+       return 0;
+}
 
-       clear = ROM_OFFSET(rom, n) - ROM_OFFSET(rom, c);
+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;
 
-       /* Zero the new space */
-       memset(ROM_PTR(rom, rom->fssize - clear), 0, clear);
+       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;
 }
 
@@ -140,22 +166,20 @@ int rom_add(struct rom *rom, const char *name, void *buffer, int size, int type)
                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);