All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mario Six <mario.six@gdsys.cc>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 14/18] tools: kwbimage: Factor out add_binary_header_v1
Date: Wed, 11 Jan 2017 16:00:58 +0100	[thread overview]
Message-ID: <20170111150102.7399-15-mario.six@gdsys.cc> (raw)
In-Reply-To: <20170111150102.7399-1-mario.six@gdsys.cc>

In preparation of adding the creation of secure headers, we factor the
add_binary_header_v1 function out of the image_create_v1 function.

Signed-off-by: Mario Six <mario.six@gdsys.cc>
---
Changes in v2:

None
---
 tools/kwbimage.c | 146 ++++++++++++++++++++++++++++++-------------------------
 1 file changed, 79 insertions(+), 67 deletions(-)

diff --git a/tools/kwbimage.c b/tools/kwbimage.c
index 87587f8..6cd4c34 100644
--- a/tools/kwbimage.c
+++ b/tools/kwbimage.c
@@ -369,6 +369,7 @@ static size_t image_headersz_v1(int *hasext)
 		fprintf(stderr, "Increase CONFIG_SYS_U_BOOT_OFFS!\n");
 		return 0;
 	}
+
 	headersz = CONFIG_SYS_U_BOOT_OFFS;
 #endif

@@ -379,10 +380,85 @@ static size_t image_headersz_v1(int *hasext)
 	return ALIGN_SUP(headersz, 4096);
 }

+int add_binary_header_v1(uint8_t *cur)
+{
+	struct image_cfg_element *binarye;
+	struct opt_hdr_v1 *hdr = (struct opt_hdr_v1 *)cur;
+	uint32_t *args;
+	size_t binhdrsz;
+	struct stat s;
+	int argi;
+	FILE *bin;
+	int ret;
+
+	binarye = image_find_option(IMAGE_CFG_BINARY);
+
+	if (!binarye)
+		return 0;
+
+	hdr->headertype = OPT_HDR_V1_BINARY_TYPE;
+
+	bin = fopen(binarye->binary.file, "r");
+	if (!bin) {
+		fprintf(stderr, "Cannot open binary file %s\n",
+			binarye->binary.file);
+		return -1;
+	}
+
+	fstat(fileno(bin), &s);
+
+	binhdrsz = sizeof(struct opt_hdr_v1) +
+		(binarye->binary.nargs + 2) * sizeof(uint32_t) +
+		s.st_size;
+
+	/*
+	 * The size includes the binary image size, rounded
+	 * up to a 4-byte boundary. Plus 4 bytes for the
+	 * next-header byte and 3-byte alignment at the end.
+	 */
+	binhdrsz = ALIGN_SUP(binhdrsz, 4) + 4;
+	hdr->headersz_lsb = cpu_to_le16(binhdrsz & 0xFFFF);
+	hdr->headersz_msb = (binhdrsz & 0xFFFF0000) >> 16;
+
+	cur += sizeof(struct opt_hdr_v1);
+
+	args = (uint32_t *)cur;
+	*args = cpu_to_le32(binarye->binary.nargs);
+	args++;
+	for (argi = 0; argi < binarye->binary.nargs; argi++)
+		args[argi] = cpu_to_le32(binarye->binary.args[argi]);
+
+	cur += (binarye->binary.nargs + 1) * sizeof(uint32_t);
+
+	ret = fread(cur, s.st_size, 1, bin);
+	if (ret != 1) {
+		fprintf(stderr,
+			"Could not read binary image %s\n",
+			binarye->binary.file);
+		return -1;
+	}
+
+	fclose(bin);
+
+	cur += ALIGN_SUP(s.st_size, 4);
+
+	/*
+	 * For now, we don't support more than one binary
+	 * header, and no other header types are
+	 * supported. So, the binary header is necessarily the
+	 * last one
+	 */
+	*((uint32_t *)cur) = 0x00000000;
+
+	cur += sizeof(uint32_t);
+
+	return 0;
+}
+
 static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
 			     int payloadsz)
 {
-	struct image_cfg_element *e, *binarye;
+	struct image_cfg_element *e;
 	struct main_hdr_v1 *main_hdr;
 	size_t headersz;
 	uint8_t *image, *cur;
@@ -434,72 +510,8 @@ static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
 	if (e)
 		main_hdr->flags = e->debug ? 0x1 : 0;

-	binarye = image_find_option(IMAGE_CFG_BINARY);
-	if (binarye) {
-		struct opt_hdr_v1 *hdr = (struct opt_hdr_v1 *)cur;
-		uint32_t *args;
-		size_t binhdrsz;
-		struct stat s;
-		int argi;
-		FILE *bin;
-		int ret;
-
-		hdr->headertype = OPT_HDR_V1_BINARY_TYPE;
-
-		bin = fopen(binarye->binary.file, "r");
-		if (!bin) {
-			fprintf(stderr, "Cannot open binary file %s\n",
-				binarye->binary.file);
-			return NULL;
-		}
-
-		fstat(fileno(bin), &s);
-
-		binhdrsz = sizeof(struct opt_hdr_v1) +
-			(binarye->binary.nargs + 2) * sizeof(uint32_t) +
-			s.st_size;
-
-		/*
-		 * The size includes the binary image size, rounded
-		 * up to a 4-byte boundary. Plus 4 bytes for the
-		 * next-header byte and 3-byte alignment at the end.
-		 */
-		binhdrsz = ALIGN_SUP(binhdrsz, 4) + 4;
-		hdr->headersz_lsb = cpu_to_le16(binhdrsz & 0xFFFF);
-		hdr->headersz_msb = (binhdrsz & 0xFFFF0000) >> 16;
-
-		cur += sizeof(struct opt_hdr_v1);
-
-		args = (uint32_t *)cur;
-		*args = cpu_to_le32(binarye->binary.nargs);
-		args++;
-		for (argi = 0; argi < binarye->binary.nargs; argi++)
-			args[argi] = cpu_to_le32(binarye->binary.args[argi]);
-
-		cur += (binarye->binary.nargs + 1) * sizeof(uint32_t);
-
-		ret = fread(cur, s.st_size, 1, bin);
-		if (ret != 1) {
-			fprintf(stderr,
-				"Could not read binary image %s\n",
-				binarye->binary.file);
-			return NULL;
-		}
-
-		fclose(bin);
-
-		cur += ALIGN_SUP(s.st_size, 4);
-
-		/*
-		 * For now, we don't support more than one binary
-		 * header, and no other header types are
-		 * supported. So, the binary header is necessarily the
-		 * last one
-		 */
-		*((uint32_t *)cur) = 0x00000000;
-
-		cur += sizeof(uint32_t);
-	}
+	if (add_binary_header_v1(cur))
+		return NULL;

 	/* Calculate and set the header checksum */
 	main_hdr->checksum = image_checksum8(main_hdr, headersz);
--
2.9.0

  parent reply	other threads:[~2017-01-11 15:00 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-11 15:00 [U-Boot] [PATCH v2 00/18] arm: mvebu: Add gdsys ControlCenter-Compact board Mario Six
2017-01-11 15:00 ` [U-Boot] [PATCH v2 01/18] pci: mvebu: Fix Armada 38x support Mario Six
2017-01-18 15:56   ` Stefan Roese
2017-02-01 11:40   ` Stefan Roese
2017-01-11 15:00 ` [U-Boot] [PATCH v2 02/18] net: phy: Support Marvell 88E1680 Mario Six
2017-01-18 15:57   ` Stefan Roese
2017-01-18 22:45     ` Joe Hershberger
2017-01-18 22:48   ` Joe Hershberger
2017-02-01 11:40   ` Stefan Roese
2017-01-11 15:00 ` [U-Boot] [PATCH v2 03/18] mvebu: Add board_pex_config() Mario Six
2017-01-18 15:58   ` Stefan Roese
2017-02-01 11:40   ` Stefan Roese
2017-01-11 15:00 ` [U-Boot] [PATCH v2 04/18] arm: mvebu: spl.c: Remove useless gd declaration Mario Six
2017-01-18 15:58   ` Stefan Roese
2017-02-01 11:40   ` Stefan Roese
2017-01-11 15:00 ` [U-Boot] [PATCH v2 05/18] dm: Add callback to modify the device tree Mario Six
2017-01-18 16:03   ` Stefan Roese
2017-01-11 15:00 ` [U-Boot] [PATCH v2 06/18] lib: tpm: Add command to flush resources Mario Six
2017-01-18 16:07   ` Stefan Roese
2017-01-19 13:57   ` Simon Glass
2017-02-01 11:41   ` Stefan Roese
2017-01-11 15:00 ` [U-Boot] [PATCH v2 07/18] core: device: Add dev_get_by_ofname function Mario Six
2017-01-18 16:23   ` Stefan Roese
2017-01-19 13:57     ` Simon Glass
2017-01-11 15:00 ` [U-Boot] [PATCH v2 08/18] gpio: Add gpio_request_simple function Mario Six
2017-01-18 16:34   ` Stefan Roese
2017-01-11 15:00 ` [U-Boot] [PATCH v2 09/18] tools: kwbimage: Fix dest addr Mario Six
2017-01-18 16:36   ` Stefan Roese
2017-02-01 11:41   ` Stefan Roese
2017-01-11 15:00 ` [U-Boot] [PATCH v2 10/18] tools: kwbimage: Fix style violations Mario Six
2017-01-18 16:37   ` Stefan Roese
2017-02-01 11:41   ` Stefan Roese
2017-01-11 15:00 ` [U-Boot] [PATCH v2 11/18] tools: kwbimage: Fix arithmetic with void pointers Mario Six
2017-01-18 16:38   ` Stefan Roese
2017-02-01 11:41   ` Stefan Roese
2017-01-11 15:00 ` [U-Boot] [PATCH v2 12/18] tools: kwbimage: Reduce scope of variables Mario Six
2017-01-18 16:39   ` Stefan Roese
2017-02-01 11:41   ` Stefan Roese
2017-01-11 15:00 ` [U-Boot] [PATCH v2 13/18] tools: kwbimage: Remove unused parameter Mario Six
2017-01-18 16:39   ` Stefan Roese
2017-02-01 11:42   ` Stefan Roese
2017-01-11 15:00 ` Mario Six [this message]
2017-01-18 16:40   ` [U-Boot] [PATCH v2 14/18] tools: kwbimage: Factor out add_binary_header_v1 Stefan Roese
2017-02-01 11:42   ` Stefan Roese
2017-01-11 15:00 ` [U-Boot] [PATCH v2 15/18] tools: kwbimage: Refactor line parsing and fix error Mario Six
2017-01-18 16:47   ` Stefan Roese
2017-02-01 11:42   ` Stefan Roese
2017-01-11 15:01 ` [U-Boot] [PATCH v2 16/18] arm: mvebu: Implement secure boot Mario Six
2017-01-18 16:58   ` Stefan Roese
2017-01-19 13:57   ` Simon Glass
2017-02-01 11:42   ` Stefan Roese
2017-01-11 15:01 ` [U-Boot] [PATCH v2 17/18] arm: mvebu: Add gdsys ControlCenter-Compact board Mario Six
2017-01-18 17:09   ` Stefan Roese
2017-01-11 15:01 ` [U-Boot] [PATCH v2 18/18] controlcenterdc: Make secure boot available Mario Six
2017-01-18 17:10   ` Stefan Roese

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=20170111150102.7399-15-mario.six@gdsys.cc \
    --to=mario.six@gdsys.cc \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.