From 5d982d72be849a15f98bb0583b25bbcd6eb7d315 Mon Sep 17 00:00:00 2001 From: Patrick Georgi Date: Tue, 19 Sep 2017 14:39:58 +0200 Subject: util/cbfstool: Add "expand" command to make CBFS span an fmap region vboot images come with multiple regions carrying CBFS file systems. To expedite hashing (from slow flash memory), the FW_MAIN_* regions are truncated since they typically have pretty large unused space at the end that is of no interest. For test purposes it can be useful to re-engage that space, so add a command that creates a new empty file entry covering that area (except for the last 4 bytes for the master header pointer, as usual). BUG=b:65853903 BRANCH=none TEST=`cbfstool test.bin expand -r FW_MAIN_A` creates a new empty file of the expected size on a Chrome OS firmware image. Change-Id: I160c8529ce4bfcc28685166b6d9035ade4f6f1d1 Signed-off-by: Patrick Georgi Reviewed-on: https://review.coreboot.org/21598 Reviewed-by: Aaron Durbin Tested-by: build bot (Jenkins) --- util/cbfstool/cbfs_image.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'util/cbfstool/cbfs_image.c') diff --git a/util/cbfstool/cbfs_image.c b/util/cbfstool/cbfs_image.c index 1f4b49a48d..92815f5f06 100644 --- a/util/cbfstool/cbfs_image.c +++ b/util/cbfstool/cbfs_image.c @@ -442,6 +442,44 @@ int cbfs_copy_instance(struct cbfs_image *image, struct buffer *dst) return 0; } +int cbfs_expand_to_region(struct buffer *region) +{ + 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; + } + + uint32_t region_sz = buffer_size(region); + + struct cbfs_file *entry; + for (entry = buffer_get(region); + cbfs_is_valid_entry(&image, entry); + entry = cbfs_find_next_entry(&image, entry)) { + /* just iterate through */ + } + + /* entry now points to the first aligned address after the last valid + * file header. That's either outside the image or exactly the place + * where we need to create a new file. + */ + int last_entry_size = region_sz - ((void *)entry - buffer_get(region)) + - cbfs_calculate_file_header_size("") - sizeof(int32_t); + + if (last_entry_size > 0) { + cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, + last_entry_size, ""); + /* If the last entry was an empty file, merge them. */ + cbfs_walk(&image, cbfs_merge_empty_entry, NULL); + } + + return 0; +} + static size_t cbfs_file_entry_metadata_size(const struct cbfs_file *f) { return ntohl(f->offset); -- cgit v1.2.3