Patrick Georgi has uploaded this change for review. ( https://review.coreboot.org/21608
Change subject: util/cbfstool: Add "truncate" command ......................................................................
util/cbfstool: Add "truncate" command
It does the opposite to "expand", removing a trailing empty file from CBFS. It also returns the size of the CBFS post processing on stdout.
BUG=b:65853903 BRANCH=none TEST=`cbfstool test.bin truncate -r FW_MAIN_A` removes the trailing empty file in FW_MAIN_A. Without a trailing empty file, the region is left alone (tested using COREBOOT which comes with a master header pointer).
Change-Id: I0c747090813898539f3428936afa9d8459adee9c Signed-off-by: Patrick Georgi pgeorgi@google.com --- M util/cbfstool/cbfs_image.c M util/cbfstool/cbfs_image.h M util/cbfstool/cbfstool.c 3 files changed, 71 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/08/21608/1
diff --git a/util/cbfstool/cbfs_image.c b/util/cbfstool/cbfs_image.c index 92815f5..af4affc 100644 --- a/util/cbfstool/cbfs_image.c +++ b/util/cbfstool/cbfs_image.c @@ -480,6 +480,52 @@ return 0; }
+int cbfs_truncate_space(struct buffer *region, uint32_t *size) +{ + if (buffer_get(region) == NULL) + return 1; + + struct cbfs_image image; + memset(&image, 0, sizeof(image)); + if (cbfs_image_from_buffer(&image, region, 0)) { + ERROR("reading CBFS failed!\n"); + return 1; + } + + *size = buffer_size(region); + + struct cbfs_file *entry, *trailer; + for (trailer = entry = buffer_get(region); + cbfs_is_valid_entry(&image, entry); + trailer = entry, + entry = cbfs_find_next_entry(&image, entry)) { + /* just iterate through */ + } + + /* trailer now points to the last valid CBFS entry's header. + * If this equals entry, there's only one file - even if that one is + * empty, we still want to keep it or we remove the entire filesystem. + * + * If that file is empty, remove it and report its header's offset as + * maximum size. + */ + if (trailer == entry) { + WARN("Empty CBFS, not truncating"); + return 1; + } + if ((strlen(trailer->filename) != 0) && + (trailer->type != htonl(CBFS_COMPONENT_NULL)) && + (trailer->type != htonl(CBFS_COMPONENT_DELETED))) { + /* nothing to truncate */ + return 1; + } + uint32_t new_size = (void *)trailer - buffer_get(region); + memset(trailer, 0xff, *size - new_size); + *size = new_size; + + return 0; +} + static size_t cbfs_file_entry_metadata_size(const struct cbfs_file *f) { return ntohl(f->offset); diff --git a/util/cbfstool/cbfs_image.h b/util/cbfstool/cbfs_image.h index 1bd74ce..5772b1b 100644 --- a/util/cbfstool/cbfs_image.h +++ b/util/cbfstool/cbfs_image.h @@ -84,6 +84,11 @@ Returns 0 on success, otherwise non-zero. */ int cbfs_expand_to_region(struct buffer *region);
+/* Truncate a CBFS by removing a trailing "empty" file if it exists. + Returns 0 on success, otherwise non-zero and passes the CBFS' remaining + size in the size argument. */ +int cbfs_truncate_space(struct buffer *region, uint32_t *size); + /* Releases the CBFS image. Returns 0 on success, otherwise non-zero. */ int cbfs_image_delete(struct cbfs_image *image);
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c index 5e7d904..3263a91 100644 --- a/util/cbfstool/cbfstool.c +++ b/util/cbfstool/cbfstool.c @@ -1121,6 +1121,23 @@ return cbfs_expand_to_region(param.image_region); }
+static int cbfs_truncate(void) +{ + struct buffer src_buf; + + /* Obtain the source region. */ + if (!partitioned_file_read_region(&src_buf, param.image_file, + param.region_name)) { + ERROR("Region not found in image: %s\n", param.source_region); + return 1; + } + + uint32_t size; + int result = cbfs_truncate_space(param.image_region, &size); + printf("0x%x\n", size); + return result; +} + static const struct command commands[] = { {"add", "H:r:f:n:t:c:b:a:yvA:gh?", cbfs_add, true, true}, {"add-flat-binary", "H:r:f:n:l:e:c:b:vA:gh?", cbfs_add_flat_binary, @@ -1142,6 +1159,7 @@ {"update-fit", "H:r:n:x:vh?", cbfs_update_fit, true, true}, {"write", "r:f:i:Fudvh?", cbfs_write, true, true}, {"expand", "r:h?", cbfs_expand, true, true}, + {"truncate", "r:h?", cbfs_truncate, true, true}, };
static struct option long_options[] = { @@ -1291,6 +1309,8 @@ "Write file into same-size [or larger] raw region\n" " read [-r fmap-region] -f file " "Extract raw region contents into binary file\n" + " truncate [-r fmap-region] " + "Truncate CBFS and print new size on stdout\n" " expand [-r fmap-region] " "Expand CBFS to span entire region\n" " update-fit [-r image,regions] -n MICROCODE_BLOB_NAME \\n"