From a6944f132bb0b5561040fbe660d6065be1c112e3 Mon Sep 17 00:00:00 2001 From: Christian Melki Date: Tue, 9 May 2023 10:18:11 +0200 Subject: [PATCH] mkimage: Do single-pass FDT data extension with cleanup. When signing, more data might be needed in the FDT. So this was solved with a loop that incremented +1k for each pass. Unfortunately, if you had the need for a large extension and/or a lot of signatures, the signing process would take longer than needed as it would recreate everything for every pass in the loop. It would also nag the user with ENOSPC type errors, which were solved with the next pass in the loop. Do away with this loop. Overextend the data set a bit (more than previously) and just pack and cleanup the fdt after the pass. Speedup is between 0-64 times. For me, this was seriously exacerbated by a PKCS11 HSM which took a very long time to complete everything. Signed-off-by: Christian Melki --- tools/fit_image.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/tools/fit_image.c b/tools/fit_image.c index 9fe69ea0d9..62370e12ee 100644 --- a/tools/fit_image.c +++ b/tools/fit_image.c @@ -83,6 +83,15 @@ static int fit_add_file_data(struct image_tool_params *params, size_t size_inc, ¶ms->summary); } + if (!ret) { + fdt_pack(ptr); + if (ftruncate(tfd, fdt_totalsize(ptr))) { + debug("%s: Failed to truncate file: %s\n", __func__, + strerror(errno)); + ret = -EIO; + } + } + if (dest_blob) { munmap(dest_blob, destfd_size); close(destfd); @@ -729,7 +738,6 @@ static int fit_handle_file(struct image_tool_params *params) char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0}; char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; - size_t size_inc; int ret; /* Flattened Image Tree (FIT) format handling */ @@ -787,23 +795,13 @@ static int fit_handle_file(struct image_tool_params *params) /* * Set hashes for images in the blob. Unfortunately we may need more - * space in either FDT, so keep trying until we succeed. - * - * Note: this is pretty inefficient for signing, since we must - * calculate the signature every time. It would be better to calculate - * all the data and then store it in a separate step. However, this - * would be considerably more complex to implement. Generally a few - * steps of this loop is enough to sign with several keys. + * space, so just bump it and clean up later using fdt_pack. */ - for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) { - if (copyfile(bakfile, tmpfile) < 0) { - printf("Can't copy %s to %s\n", bakfile, tmpfile); - ret = -EIO; - break; - } - ret = fit_add_file_data(params, size_inc, tmpfile); - if (!ret || ret != -ENOSPC) - break; + if (copyfile(bakfile, tmpfile) < 0) { + printf("Can't copy %s to %s\n", bakfile, tmpfile); + ret = -EIO; + } else { + ret = fit_add_file_data(params, 512 * 1024, tmpfile); } if (ret) { -- 2.34.1