public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Kever Yang <kever.yang@rock-chips.com>
To: u-boot@lists.denx.de
Subject: [PATCH v3 8/8] mkimage: fit_image: Add option to make fit header align
Date: Thu, 26 Mar 2020 18:09:16 +0800	[thread overview]
Message-ID: <20200326100916.22532-8-kever.yang@rock-chips.com> (raw)
In-Reply-To: <20200326100916.22532-1-kever.yang@rock-chips.com>

The image is usually stored in block device like emmc, SD card, make the
offset of image data aligned to block(512 byte) can avoid data copy
during boot process.
eg. SPL boot from FIT image with external data:
- SPL read the first block of FIT image, and then parse the header;
- SPL read image data separately;
- The first image offset is the base_offset which is the header size;
- The second image offset is just after the first image;
- If the offset of imge does not aligned, SPL will do memcpy;
The header size is a ramdon number, which is very possible not aligned, so
add '-B' to specify the align size in hex for better performance.

example usage:
  ./tools/mkimage -E -f u-boot.its -B 200 u-boot.itb

Signed-off-by: Kever Yang <kever.yang@rock-chips.com>
---

Changes in v3:
- add common ALIGN() at imagetool.h
- migrate to use imagetool.h for all other files under tools/

Changes in v2:
- use '-B' to take a argument as a align block lenth;
- add new variable to indecate align_size
- address commens from Heinrich, Rasmus, Tom, Punit;

 doc/uImage.FIT/source_file_format.txt |  5 ++++
 tools/fit_image.c                     | 33 ++++++++++++++++-----------
 tools/imagetool.h                     |  1 +
 tools/mkimage.c                       | 14 ++++++++++--
 4 files changed, 38 insertions(+), 15 deletions(-)

diff --git a/doc/uImage.FIT/source_file_format.txt b/doc/uImage.FIT/source_file_format.txt
index 18d2aedcb7..3ea84bc3bc 100644
--- a/doc/uImage.FIT/source_file_format.txt
+++ b/doc/uImage.FIT/source_file_format.txt
@@ -304,6 +304,11 @@ Normal kernel FIT image has data embedded within FIT structure. U-Boot image
 for SPL boot has external data. Existence of 'data-offset' can be used to
 identify which format is used.
 
+For FIT image with external data, it would be better to align each blob of data
+to block(512 byte) for block device, so that we don't need to do the copy when
+read the image data in SPL. Pass '-B 200' to mkimage to align the FIT structure
+and data to 512 byte or other value for other align size.
+
 9) Examples
 -----------
 
diff --git a/tools/fit_image.c b/tools/fit_image.c
index a417182d4f..790a4ea959 100644
--- a/tools/fit_image.c
+++ b/tools/fit_image.c
@@ -415,7 +415,7 @@ err_buf:
  */
 static int fit_extract_data(struct image_tool_params *params, const char *fname)
 {
-	void *buf;
+	void *buf = NULL;
 	int buf_ptr;
 	int fit_size, new_size;
 	int fd;
@@ -424,26 +424,33 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname)
 	int ret;
 	int images;
 	int node;
+	int image_number;
+	int align_size;
 
+	align_size = params->bl_len ? params->bl_len : 4;
 	fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false);
 	if (fd < 0)
 		return -EIO;
 	fit_size = fdt_totalsize(fdt);
 
-	/* Allocate space to hold the image data we will extract */
-	buf = malloc(fit_size);
-	if (!buf) {
-		ret = -ENOMEM;
-		goto err_munmap;
-	}
-	buf_ptr = 0;
-
 	images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
 	if (images < 0) {
 		debug("%s: Cannot find /images node: %d\n", __func__, images);
 		ret = -EINVAL;
 		goto err_munmap;
 	}
+	image_number = fdtdec_get_child_count(fdt, images);
+
+	/*
+	 * Allocate space to hold the image data we will extract,
+	 * extral space allocate for image alignment to prevent overflow.
+	 */
+	buf = malloc(fit_size + (align_size * image_number));
+	if (!buf) {
+		ret = -ENOMEM;
+		goto err_munmap;
+	}
+	buf_ptr = 0;
 
 	for (node = fdt_first_subnode(fdt, images);
 	     node >= 0;
@@ -471,17 +478,17 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname)
 					buf_ptr);
 		}
 		fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
-
-		buf_ptr += (len + 3) & ~3;
+		buf_ptr += ALIGN(len, align_size);
 	}
 
 	/* Pack the FDT and place the data after it */
 	fdt_pack(fdt);
 
+	new_size = fdt_totalsize(fdt);
+	new_size = ALIGN(new_size, align_size);
+	fdt_set_totalsize(fdt, new_size);
 	debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
 	debug("External data size %x\n", buf_ptr);
-	new_size = fdt_totalsize(fdt);
-	new_size = (new_size + 3) & ~3;
 	munmap(fdt, sbuf.st_size);
 
 	if (ftruncate(fd, new_size)) {
diff --git a/tools/imagetool.h b/tools/imagetool.h
index 81e5cd0c5c..f54809cd57 100644
--- a/tools/imagetool.h
+++ b/tools/imagetool.h
@@ -79,6 +79,7 @@ struct image_tool_params {
 	bool external_data;	/* Store data outside the FIT */
 	bool quiet;		/* Don't output text in normal operation */
 	unsigned int external_offset;	/* Add padding to external data */
+	int bl_len;		/* Block length in byte for external data */
 	const char *engine_id;	/* Engine to use for signing */
 };
 
diff --git a/tools/mkimage.c b/tools/mkimage.c
index 0279f6867e..eccaf1d2aa 100644
--- a/tools/mkimage.c
+++ b/tools/mkimage.c
@@ -98,8 +98,9 @@ static void usage(const char *msg)
 		"          -i => input filename for ramdisk file\n");
 #ifdef CONFIG_FIT_SIGNATURE
 	fprintf(stderr,
-		"Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
+		"Signing / verified boot options: [-E] [-B] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
 		"          -E => place data outside of the FIT structure\n"
+		"          -B => align FIT structure and header to block\n"
 		"          -k => set directory containing private keys\n"
 		"          -K => write public keys to this .dtb file\n"
 		"          -c => add comment in signature node\n"
@@ -144,7 +145,7 @@ static void process_args(int argc, char **argv)
 	int opt;
 
 	while ((opt = getopt(argc, argv,
-			     "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVx")) != -1) {
+			     "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVx")) != -1) {
 		switch (opt) {
 		case 'a':
 			params.addr = strtoull(optarg, &ptr, 16);
@@ -168,6 +169,15 @@ static void process_args(int argc, char **argv)
 					params.cmdname, optarg);
 				exit(EXIT_FAILURE);
 			}
+			break;
+		case 'B':
+			params.bl_len = strtoull(optarg, &ptr, 16);
+			if (*ptr) {
+				fprintf(stderr, "%s: invalid block length %s\n",
+					params.cmdname, optarg);
+				exit(EXIT_FAILURE);
+			}
+
 			break;
 		case 'c':
 			params.comment = optarg;
-- 
2.17.1

  parent reply	other threads:[~2020-03-26 10:09 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-26 10:09 [PATCH v3 1/8] tool: Move ALIGN_MASK to header as common MACRO Kever Yang
2020-03-26 10:09 ` [PATCH v3 2/8] tool: aisimage: use ALIGN instead of self defiend macro Kever Yang
2020-03-27 18:16   ` Tom Rini
2020-03-26 10:09 ` [PATCH v3 3/8] tools: mkimage: use common ALIGN to do the size align Kever Yang
2020-03-27 18:16   ` Tom Rini
2020-03-26 10:09 ` [PATCH v3 4/8] tools: kwbimage: " Kever Yang
2020-03-26 10:12   ` Stefan Roese
2020-03-27 18:16   ` Tom Rini
2020-03-26 10:09 ` [PATCH v3 5/8] tools: imx8mimage: remove redundant code Kever Yang
2020-03-27 18:16   ` Tom Rini
2020-03-26 10:09 ` [PATCH v3 6/8] tool: use ALIGN() to align the size Kever Yang
2020-03-26 11:39   ` Heinrich Schuchardt
2020-03-27 18:16   ` Tom Rini
2020-03-26 10:09 ` [PATCH v3 7/8] libfdt: Make fdtdec_get_child_count() available for HOST Kever Yang
2020-03-28 20:04   ` Simon Glass
2020-03-26 10:09 ` Kever Yang [this message]
2020-03-27 18:16   ` [PATCH v3 8/8] mkimage: fit_image: Add option to make fit header align Tom Rini
2020-03-30  2:35     ` Kever Yang
2020-03-27  3:27 ` [PATCH v3 1/8] tool: Move ALIGN_MASK to header as common MACRO Punit Agrawal
2020-03-27 18:16 ` Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200326100916.22532-8-kever.yang@rock-chips.com \
    --to=kever.yang@rock-chips.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox