summaryrefslogtreecommitdiff
path: root/util/cbfstool
diff options
context:
space:
mode:
authorVadim Bendebury <vbendeb@chromium.org>2014-12-23 19:26:54 -0800
committerPatrick Georgi <pgeorgi@google.com>2015-04-18 08:48:22 +0200
commit5e273a4577d0392156cb217848b14eab9daa31d6 (patch)
treef323dc3b807eca26fcf2b73b9adf33b2ffe14364 /util/cbfstool
parent458a12e52beb9dfcb1884dfad6effa8b3d98e8a4 (diff)
downloadcoreboot-5e273a4577d0392156cb217848b14eab9daa31d6.tar.xz
cbfstool: add a command to duplicate a cbfs instance
The new command allows to create a file where the original CBFS image is duplicated at a different offset. The required options of the new command are -D, the offset where the copy CBFS header is placed, and -s, the size of the new CBFS copy. When a CBFS is copied, the bootblock area of the source CBFS is ignored, as well as empty and deleted files in the source CBFS. The size of the destination CBFS is calculated as the rombase size of the source CBFS less the bootblock size. The copy instance can be created in the image only above the original, which rules out the use of this new command for x86 images. If necessary, this limitation could be addressed later. As with other cbfstool commands, unless explicitly specified the lowest CBFS instance in the image is considered the source. If necessary, the user can specify the source CBFS using the -H option. BRANCH=storm BUG=chrome-os-partner:34161, chromium:445938 TEST=run multiple cbfstool commands on a storm image: $ cd /tmp $ cp /build/storm/firmware/image.serial.bin storm.bin $ cbfstool storm.bin print storm.bin: 8192 kB, bootblocksize 34472, romsize 458752, offset 0x8700 alignment: 64 bytes, architecture: arm Name Offset Type Size cdt.mbn 0x8700 raw 416 ddr.mbn 0x8900 raw 25836 rpm.mbn 0xee40 raw 78576 tz.mbn 0x22180 raw 85360 fallback/verstage 0x36f40 stage 41620 fallback/romstage 0x41240 stage 19556 fallback/ramstage 0x45f00 stage 25579 config 0x4c340 raw 2878 fallback/payload 0x4cec0 payload 64811 u-boot.dtb 0x5cc40 (unknown) 2993 (empty) 0x5d840 null 75608 $ cbfstool storm.bin copy -D 0x420000 E: You need to specify -s/--size. $ cbfstool storm.bin copy -D 0x420000 -s 0x70000 $ cbfstool storm.bin print W: Multiple (2) CBFS headers found, using the first one. storm.bin: 8192 kB, bootblocksize 34472, romsize 458752, offset 0x8700 alignment: 64 bytes, architecture: arm Name Offset Type Size cdt.mbn 0x8700 raw 416 ddr.mbn 0x8900 raw 25836 rpm.mbn 0xee40 raw 78576 tz.mbn 0x22180 raw 85360 fallback/verstage 0x36f40 stage 41620 fallback/romstage 0x41240 stage 19556 fallback/ramstage 0x45f00 stage 25579 config 0x4c340 raw 2878 fallback/payload 0x4cec0 payload 64811 u-boot.dtb 0x5cc40 (unknown) 2993 (empty) 0x5d840 null 75608 cbfstool storm.bin print -H 0x420000 storm.bin: 8192 kB, bootblocksize 0, romsize 4784128, offset 0x420040 alignment: 64 bytes, architecture: arm Name Offset Type Size cdt.mbn 0x420040 raw 416 ddr.mbn 0x420240 raw 25836 rpm.mbn 0x426780 raw 78576 tz.mbn 0x439ac0 raw 85360 fallback/verstage 0x44e880 stage 41620 fallback/romstage 0x458b80 stage 19556 fallback/ramstage 0x45d840 stage 25579 config 0x463c80 raw 2878 fallback/payload 0x464800 payload 64811 u-boot.dtb 0x474580 (unknown) 2993 (empty) 0x475180 null 110168 $ cbfstool storm.bin remove -n config -H 0x420000 $ cbfstool storm.bin copy -H 0x420000 -D 0x620000 -s 0x70000 $ cbfstool storm.bin print -H 0x620000 storm.bin: 8192 kB, bootblocksize 0, romsize 6881280, offset 0x620040 alignment: 64 bytes, architecture: arm Name Offset Type Size cdt.mbn 0x620040 raw 416 ddr.mbn 0x620240 raw 25836 rpm.mbn 0x626780 raw 78576 tz.mbn 0x639ac0 raw 85360 fallback/verstage 0x64e880 stage 41620 fallback/romstage 0x658b80 stage 19556 fallback/ramstage 0x65d840 stage 25579 fallback/payload 0x663c80 payload 64811 u-boot.dtb 0x673a00 (unknown) 2993 (empty) 0x674600 null 113112 $ cbfstool /build/storm/firmware/image.serial.bin extract -n fallback/payload -f payload1 [..] $ cbfstool storm.bin extract -H 0x620000 -n fallback/payload -f payload2 [..] $ diff payload1 payload2 Change-Id: Ieb9205848aec361bb870de0d284dff06c597564f Signed-off-by: Patrick Georgi <pgeorgi@chromium.org> Original-Commit-Id: b8d3c1b09a47ca24d2d2effc6de0e89d1b0a8903 Original-Signed-off-by: Aaron Durbin <adurbin@chromium.org> Original-Signed-off-by: Vadim Bendebury <vbendeb@chromium.org> Original-Change-Id: I227e607ccf7a9a8e2a1f3c6bbc506b8d29a35b1b Original-Reviewed-on: https://chromium-review.googlesource.com/237561 Reviewed-on: http://review.coreboot.org/9742 Tested-by: build bot (Jenkins) Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
Diffstat (limited to 'util/cbfstool')
-rw-r--r--util/cbfstool/cbfs_image.c82
-rw-r--r--util/cbfstool/cbfs_image.h4
-rw-r--r--util/cbfstool/cbfstool.c36
3 files changed, 122 insertions, 0 deletions
diff --git a/util/cbfstool/cbfs_image.c b/util/cbfstool/cbfs_image.c
index 621a35d16f..9df2030679 100644
--- a/util/cbfstool/cbfs_image.c
+++ b/util/cbfstool/cbfs_image.c
@@ -294,6 +294,88 @@ int cbfs_image_from_file(struct cbfs_image *image,
return 0;
}
+int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
+ size_t copy_size)
+{
+ struct cbfs_file *src_entry, *dst_entry;
+ struct cbfs_header *copy_header;
+ size_t align, entry_offset;
+ ssize_t last_entry_size;
+
+ size_t header_offset, header_end;
+ size_t cbfs_offset, cbfs_end;
+ size_t copy_end = copy_offset + copy_size;
+
+ align = htonl(image->header->align);
+
+ header_offset = (char *)image->header - image->buffer.data;
+ header_end = header_offset + sizeof(image->header);
+
+ cbfs_offset = htonl(image->header->offset);
+ cbfs_end = htonl(image->header->romsize);
+
+ if (copy_end > image->buffer.size) {
+ ERROR("Copy offset out of range: [%zx:%zx)\n",
+ copy_offset, copy_end);
+ return 1;
+ }
+
+ /* Range check requested copy region with header and source cbfs. */
+ if ((copy_offset >= header_offset && copy_offset < header_end) ||
+ (copy_end >= header_offset && copy_end <= header_end)) {
+ ERROR("New image would overlap old header.\n");
+ }
+
+ if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) ||
+ (copy_end >= cbfs_offset && copy_end <= cbfs_end)) {
+ ERROR("New image would overlap old one.\n");
+ return 1;
+ }
+
+ /* This will work, let's create a copy. */
+ copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset);
+ *copy_header = *image->header;
+
+ copy_header->bootblocksize = 0;
+ /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/
+ copy_header->romsize = htonl(copy_end);
+ entry_offset = align_up(copy_offset + sizeof(*copy_header), align);
+ copy_header->offset = htonl(entry_offset);
+ dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset);
+
+ /* Copy non-empty files */
+ for (src_entry = cbfs_find_first_entry(image);
+ src_entry && cbfs_is_valid_entry(image, src_entry);
+ src_entry = cbfs_find_next_entry(image, src_entry)) {
+ size_t entry_size;
+
+ if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
+ (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
+ continue;
+
+ entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
+ memcpy(dst_entry, src_entry, entry_size);
+ dst_entry = (struct cbfs_file *)(
+ (uintptr_t)dst_entry + align_up(entry_size, align));
+
+ if (((char *)dst_entry - image->buffer.data) >= copy_end) {
+ ERROR("Ran out of room in copy region.\n");
+ return 1;
+ }
+ }
+
+ /* Last entry size is all the room above it. */
+ last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data)
+ - cbfs_calculate_file_header_size("");
+
+ if (last_entry_size < 0)
+ WARN("No room to create the last entry!\n")
+ else
+ cbfs_create_empty_entry(image, dst_entry, last_entry_size, "");
+
+ return 0;
+}
+
int cbfs_image_write_file(struct cbfs_image *image, const char *filename)
{
assert(image && image->buffer.data);
diff --git a/util/cbfstool/cbfs_image.h b/util/cbfstool/cbfs_image.h
index 2c1be3da1c..1e5a99f16a 100644
--- a/util/cbfstool/cbfs_image.h
+++ b/util/cbfstool/cbfs_image.h
@@ -55,6 +55,10 @@ int cbfs_image_create(struct cbfs_image *image,
int cbfs_image_from_file(struct cbfs_image *image,
const char *filename, uint32_t offset);
+/* Create a duplicate CBFS image. Returns 0 on success, otherwise non-zero. */
+int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
+ size_t copy_size);
+
/* Writes a CBFS image into file. Returns 0 on success, otherwise non-zero. */
int cbfs_image_write_file(struct cbfs_image *image, const char *filename);
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c
index 20e5e0c78a..3ccc78610d 100644
--- a/util/cbfstool/cbfstool.c
+++ b/util/cbfstool/cbfstool.c
@@ -47,6 +47,8 @@ static struct param {
uint32_t baseaddress;
uint32_t baseaddress_assigned;
uint32_t loadaddress;
+ uint32_t copyoffset;
+ uint32_t copyoffset_assigned;
uint32_t headeroffset;
uint32_t headeroffset_assigned;
uint32_t entrypoint;
@@ -550,6 +552,32 @@ static int cbfs_update_fit(void)
return ret;
}
+static int cbfs_copy(void)
+{
+ struct cbfs_image image;
+
+ if (!param.copyoffset_assigned) {
+ ERROR("You need to specify -D/--copy_offset.\n");
+ return 1;
+ }
+
+ if (!param.size) {
+ ERROR("You need to specify -s/--size.\n");
+ return 1;
+ }
+
+ if (cbfs_image_from_file(&image, param.cbfs_name,
+ param.headeroffset) != 0)
+ return 1;
+
+ if (cbfs_copy_instance(&image, param.copyoffset, param.size))
+ return 1;
+
+ /* Save the new image. */
+ return buffer_write_file(&image.buffer, param.cbfs_name);
+
+}
+
static const struct command commands[] = {
{"add", "H;f:n:t:b:vh?", cbfs_add},
{"add-payload", "H:f:n:t:c:b:vh?C:I:", cbfs_add_payload},
@@ -557,6 +585,7 @@ static const struct command commands[] = {
{"add-flat-binary", "H:f:n:l:e:c:b:vh?", cbfs_add_flat_binary},
{"add-int", "H:i:n:b:vh?", cbfs_add_integer},
{"remove", "H:n:vh?", cbfs_remove},
+ {"copy", "H:D:s:", cbfs_copy},
{"create", "s:B:b:H:a:o:m:vh?", cbfs_create},
{"locate", "H:f:n:P:a:Tvh?", cbfs_locate},
{"print", "H:vh?", cbfs_print},
@@ -571,6 +600,7 @@ static struct option long_options[] = {
{"base-address", required_argument, 0, 'b' },
{"load-address", required_argument, 0, 'l' },
{"top-aligned", required_argument, 0, 'T' },
+ {"copy-offset", required_argument, 0, 'D' },
{"entry-point", required_argument, 0, 'e' },
{"size", required_argument, 0, 's' },
{"bootblock", required_argument, 0, 'B' },
@@ -616,6 +646,9 @@ static void usage(char *name)
"Add a raw 64-bit integer value\n"
" remove -n NAME "
"Remove a component\n"
+ " copy -D new_header_offset -s region size \\\n"
+ " [-H source header offset] "
+ "Create a copy (duplicate) cbfs instance\n"
" create -s size -m ARCH [-B bootblock] [-b bootblock offset] \\\n"
" [-o CBFS offset] [-H header offset] [-a align] "
"Create a ROM file\n"
@@ -722,6 +755,9 @@ int main(int argc, char **argv)
optarg, NULL, 0);
param.headeroffset_assigned = 1;
break;
+ case 'D':
+ param.copyoffset = strtoul(optarg, NULL, 0);
+ param.copyoffset_assigned = 1;
case 'a':
param.alignment = strtoul(optarg, NULL, 0);
break;