From 12631a4d1d1c2e06b3a2c53b884cf6c33000b9b9 Mon Sep 17 00:00:00 2001 From: Patrick Georgi Date: Wed, 20 Sep 2017 11:59:18 +0200 Subject: 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 Reviewed-on: https://review.coreboot.org/21608 Tested-by: build bot (Jenkins) Reviewed-by: Aaron Durbin --- 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 92815f5f06..780ab6db84 100644 --- a/util/cbfstool/cbfs_image.c +++ b/util/cbfstool/cbfs_image.c @@ -480,6 +480,44 @@ int cbfs_expand_to_region(struct buffer *region) 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; + } + + 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 that file is empty, remove it and report its header's offset as + * maximum size. + */ + if ((strlen(trailer->filename) != 0) && + (trailer->type != htonl(CBFS_COMPONENT_NULL)) && + (trailer->type != htonl(CBFS_COMPONENT_DELETED))) { + /* nothing to truncate. Return de-facto CBFS size in case it + * was already truncated. */ + *size = (void *)entry - buffer_get(region); + return 0; + } + *size = (void *)trailer - buffer_get(region); + memset(trailer, 0xff, buffer_size(region) - *size); + + return 0; +} + static size_t cbfs_file_entry_metadata_size(const struct cbfs_file *f) { return ntohl(f->offset); -- cgit v1.2.3