From feaaedc1cf99c12b8d1ab4bb233a311044d7f8b0 Mon Sep 17 00:00:00 2001 From: Myles Watson Date: Mon, 20 Apr 2009 21:38:11 +0000 Subject: This patch adds 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 Acked-by: Ronald G. Minnich git-svn-id: svn://svn.coreboot.org/coreboot/trunk@4144 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1 --- util/cbfstool/Makefile | 2 +- util/cbfstool/cbfstool.c | 4 +++ util/cbfstool/cbfstool.h | 1 + util/cbfstool/extract.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ util/cbfstool/fs.c | 46 +++++++++++++++++++++++------- 5 files changed, 115 insertions(+), 12 deletions(-) create mode 100644 util/cbfstool/extract.c (limited to 'util/cbfstool') diff --git a/util/cbfstool/Makefile b/util/cbfstool/Makefile index 919021d667..39af38b280 100644 --- a/util/cbfstool/Makefile +++ b/util/cbfstool/Makefile @@ -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 diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c index b9915e3fac..cbf448163f 100644 --- a/util/cbfstool/cbfstool.c +++ b/util/cbfstool/cbfstool.c @@ -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},}; diff --git a/util/cbfstool/cbfstool.h b/util/cbfstool/cbfstool.h index 43abb6cb17..f36b28684c 100644 --- a/util/cbfstool/cbfstool.h +++ b/util/cbfstool/cbfstool.h @@ -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 index 0000000000..d6944763b7 --- /dev/null +++ b/util/cbfstool/extract.c @@ -0,0 +1,74 @@ +/* + * cbfstool + * + * Copyright (C) 2009 Myles Watson + * + * 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 +#include +#include +#include +#include +#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]); +} + diff --git a/util/cbfstool/fs.c b/util/cbfstool/fs.c index c5e9892a8e..ff5741e37e 100644 --- a/util/cbfstool/fs.c +++ b/util/cbfstool/fs.c @@ -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); -- cgit v1.2.3