Hung-Te Lin (hungte@chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/2207
-gerrit
commit 40dee62ddb649497585a1cc2b76329d3d61bf184 Author: Hung-Te Lin hungte@chromium.org Date: Tue Jan 29 02:29:49 2013 +0800
cbfstool: Use cbfs_image API for "extract" command.
Replace the "extract" by cbfs_export_entry API.
To verify, run "cbfstool coreboot.rom extract -f blah -n blah" and check if the raw type file is correctly extracted.
Change-Id: I1ed280d47a2224a9d1213709f6b459b403ce5055 Signed-off-by: Hung-Te Lin hungte@chromium.org --- util/cbfstool/cbfs_image.c | 41 +++++++++++++++++++++++++++++++++++++++++ util/cbfstool/cbfs_image.h | 8 ++++++++ util/cbfstool/cbfstool.c | 18 +++++++++--------- 3 files changed, 58 insertions(+), 9 deletions(-)
diff --git a/util/cbfstool/cbfs_image.c b/util/cbfstool/cbfs_image.c index d16ca0f..4e2c8a9 100644 --- a/util/cbfstool/cbfs_image.c +++ b/util/cbfstool/cbfs_image.c @@ -124,6 +124,47 @@ int cbfs_image_delete(struct cbfs_image *image) { return 0; }
+struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) { + struct cbfs_file *entry; + for (entry = cbfs_find_first_entry(image); + entry && cbfs_is_valid_entry(entry); + entry = cbfs_find_next_entry(image, entry)) { + if (strcasecmp(CBFS_NAME(entry), name) == 0) { + DEBUG("cbfs_get_entry: found %s\n", name); + return entry; + } + } + return NULL; +} + +int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, + const char *filename) { + struct cbfs_file *entry = cbfs_get_entry(image, entry_name); + struct buffer buffer; + if (!entry) { + ERROR("File not found: %s\n", entry_name); + return -1; + } + LOG("Found file %.30s at 0x%x, type %.12s, size %d\n", + entry_name, cbfs_get_entry_addr(image, entry), + get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len)); + + if (ntohl(entry->type) != CBFS_COMPONENT_RAW) { + WARN("Only 'raw' files are safe to extract.\n"); + } + + buffer.data = CBFS_SUBHEADER(entry); + buffer.size = ntohl(entry->len); + buffer.name = "(cbfs_export_entry)"; + if (buffer_write_file(&buffer, filename) != 0) { + ERROR("Failed to write %s into %s.\n", + entry_name, filename); + return -1; + } + INFO("Successfully dumped the file to: %s\n", filename); + return 0; +} + int cbfs_print_header_info(struct cbfs_image *image) { assert(image && image->header); printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n" diff --git a/util/cbfstool/cbfs_image.h b/util/cbfstool/cbfs_image.h index ed8be5a..1be2a66 100644 --- a/util/cbfstool/cbfs_image.h +++ b/util/cbfstool/cbfs_image.h @@ -38,6 +38,14 @@ int cbfs_image_write_file(struct cbfs_image *image, const char *filename); /* Releases the CBFS image. Returns 0 on success, otherwise non-zero. */ int cbfs_image_delete(struct cbfs_image *image);
+/* Returns a pointer to entry by name, or NULL if name is not found. */ +struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name); + +/* Exports an entry to external file. + * Returns 0 on success, otherwise (ex, not found) non-zero. */ +int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, + const char *filename); + /* Callback function used by cbfs_walk. * Returns 0 on success, or non-zero to stop further iteration. */ typedef int (*cbfs_entry_callback)(struct cbfs_image *image, diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c index 5e4f8f1..4a44005 100644 --- a/util/cbfstool/cbfstool.c +++ b/util/cbfstool/cbfstool.c @@ -402,8 +402,8 @@ static int cbfs_print(void)
static int cbfs_extract(void) { - void *rom; - int ret; + int result = 0; + struct cbfs_image image;
if (!param.filename) { ERROR("You need to specify -f/--filename.\n"); @@ -415,17 +415,17 @@ static int cbfs_extract(void) return 1; }
- rom = loadrom(param.cbfs_name); - if (rom == NULL) { + if (cbfs_image_from_file(&image, param.cbfs_name) != 0) { ERROR("Could not load ROM image '%s'.\n", param.cbfs_name); - return 1; + result = 1; + } else if (cbfs_export_entry(&image, param.name, + param.filename) != 0) { + result = 1; }
- ret = extract_file_from_cbfs(param.cbfs_name, param.name, param.filename); - - free(rom); - return ret; + cbfs_image_delete(&image); + return result; }
static const struct command commands[] = {