Author: myles Date: 2009-04-20 23:38:11 +0200 (Mon, 20 Apr 2009) New Revision: 4144
Added: trunk/coreboot-v2/util/cbfstool/extract.c Modified: trunk/coreboot-v2/util/cbfstool/Makefile trunk/coreboot-v2/util/cbfstool/cbfstool.c trunk/coreboot-v2/util/cbfstool/cbfstool.h trunk/coreboot-v2/util/cbfstool/fs.c Log: 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 mylesgw@gmail.com Acked-by: Ronald G. Minnich rminnich@gmail.com
Modified: trunk/coreboot-v2/util/cbfstool/Makefile =================================================================== --- trunk/coreboot-v2/util/cbfstool/Makefile 2009-04-20 15:36:57 UTC (rev 4143) +++ trunk/coreboot-v2/util/cbfstool/Makefile 2009-04-20 21:38:11 UTC (rev 4144) @@ -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
Modified: trunk/coreboot-v2/util/cbfstool/cbfstool.c =================================================================== --- trunk/coreboot-v2/util/cbfstool/cbfstool.c 2009-04-20 15:36:57 UTC (rev 4143) +++ trunk/coreboot-v2/util/cbfstool/cbfstool.c 2009-04-20 21:38:11 UTC (rev 4144) @@ -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},};
Modified: trunk/coreboot-v2/util/cbfstool/cbfstool.h =================================================================== --- trunk/coreboot-v2/util/cbfstool/cbfstool.h 2009-04-20 15:36:57 UTC (rev 4143) +++ trunk/coreboot-v2/util/cbfstool/cbfstool.h 2009-04-20 21:38:11 UTC (rev 4144) @@ -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);
Added: trunk/coreboot-v2/util/cbfstool/extract.c =================================================================== --- trunk/coreboot-v2/util/cbfstool/extract.c (rev 0) +++ trunk/coreboot-v2/util/cbfstool/extract.c 2009-04-20 21:38:11 UTC (rev 4144) @@ -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]); +} +
Modified: trunk/coreboot-v2/util/cbfstool/fs.c =================================================================== --- trunk/coreboot-v2/util/cbfstool/fs.c 2009-04-20 15:36:57 UTC (rev 4143) +++ trunk/coreboot-v2/util/cbfstool/fs.c 2009-04-20 21:38:11 UTC (rev 4144) @@ -110,17 +110,43 @@ 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);
- clear = ROM_OFFSET(rom, n) - ROM_OFFSET(rom, c); + return 0; +}
- /* Zero the new space */ - memset(ROM_PTR(rom, rom->fssize - clear), 0, clear); +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; }
@@ -140,22 +166,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);