public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: "Pali Rohár" <pali@kernel.org>
To: "Stefan Roese" <sr@denx.de>, "Marek Behún" <marek.behun@nic.cz>,
	"Chris Packham" <judge.packham@gmail.com>
Cc: u-boot@lists.denx.de
Subject: [PATCH u-boot-marvell 06/16] tools: kwbimage: Add support for specifying LOAD_ADDRESS for BINARY command
Date: Tue, 21 Dec 2021 16:54:06 +0100	[thread overview]
Message-ID: <20211221155416.8557-7-pali@kernel.org> (raw)
In-Reply-To: <20211221155416.8557-1-pali@kernel.org>

ARM executable code included in kwbimage binary header, which is not
position independent, needs to be loaded and executed by BootROM at the
correct fixed address.

Armada BootROMs load kwbimage header (in which the executable code is also
stored) at fixed address 0x40000000, which seems to be mapped to CESA SRAM.

Thus the only way to specify load and execute address of this executable
code in binary kwbimage header is by filling dummy arguments into the
binary header, using the same mechanism we already have for achieving
128-bit boundary alignment on A370 and AXP SoCs.

Extend kwbimage config file parser to allow to specify load address as
part of BINARY command with syntax:

    BINARY path_to_binary arg1 arg2 ... argN LOAD_ADDRESS address

If the specified load address is invalid or cannot be used, mkimage will
throw fatal error and exit. This will prevent generating kwbimage with
invalid load address for non-position independent binary code.

If no load address is specified, kwbimage will not fill any the dummy
arguments, thus it will behave the same as before this change.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>
---
 tools/kwbimage.c | 92 +++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 76 insertions(+), 16 deletions(-)

diff --git a/tools/kwbimage.c b/tools/kwbimage.c
index f298883b2cb0..c21ed7b23fcf 100644
--- a/tools/kwbimage.c
+++ b/tools/kwbimage.c
@@ -153,6 +153,7 @@ struct image_cfg_element {
 		unsigned int bootfrom;
 		struct {
 			const char *file;
+			unsigned int loadaddr;
 			unsigned int args[BINARY_MAX_ARGS];
 			unsigned int nargs;
 		} binary;
@@ -991,10 +992,12 @@ static void *image_create_v0(size_t *imagesz, struct image_tool_params *params,
 
 static size_t image_headersz_v1(int *hasext)
 {
-	struct image_cfg_element *binarye, *e;
+	struct image_cfg_element *e;
 	unsigned int count;
 	size_t headersz;
+	struct stat s;
 	int cfgi;
+	int ret;
 
 	/*
 	 * Calculate the size of the header and the size of the
@@ -1020,19 +1023,11 @@ static size_t image_headersz_v1(int *hasext)
 			headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4;
 			count = 0;
 		}
-	}
-	if (count > 0)
-		headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4;
 
-	for (cfgi = 0; cfgi < cfgn; cfgi++) {
-		int ret;
-		struct stat s;
-
-		binarye = &image_cfg[cfgi];
-		if (binarye->type != IMAGE_CFG_BINARY)
+		if (e->type != IMAGE_CFG_BINARY)
 			continue;
 
-		ret = stat(binarye->binary.file, &s);
+		ret = stat(e->binary.file, &s);
 		if (ret < 0) {
 			char cwd[PATH_MAX];
 			char *dir = cwd;
@@ -1047,18 +1042,48 @@ static size_t image_headersz_v1(int *hasext)
 				"Didn't find the file '%s' in '%s' which is mandatory to generate the image\n"
 				"This file generally contains the DDR3 training code, and should be extracted from an existing bootable\n"
 				"image for your board. Use 'dumpimage -T kwbimage -p 0' to extract it from an existing image.\n",
-				binarye->binary.file, dir);
+				e->binary.file, dir);
 			return 0;
 		}
 
 		headersz += sizeof(struct opt_hdr_v1) + sizeof(uint32_t) +
-			(binarye->binary.nargs) * sizeof(uint32_t);
-		headersz = ALIGN(headersz, 16);
+			(e->binary.nargs) * sizeof(uint32_t);
+
+		if (e->binary.loadaddr) {
+			/*
+			 * BootROM loads kwbimage header (in which the
+			 * executable code is also stored) to address
+			 * 0x40000000. Thus there is restriction for the
+			 * load address of the N-th BINARY image.
+			 */
+			unsigned int low_addr, high_addr;
+
+			low_addr = 0x40000000 + headersz;
+			high_addr = low_addr +
+				    (BINARY_MAX_ARGS - e->binary.nargs) * sizeof(uint32_t);
+
+			if (e->binary.loadaddr % 4 || e->binary.loadaddr < low_addr ||
+			    e->binary.loadaddr > high_addr) {
+				fprintf(stderr,
+					"Invalid LOAD_ADDRESS 0x%08x for BINARY %s with %d args.\n"
+					"Address must be 4-byte aligned and in range 0x%08x-0x%08x\n.",
+					e->binary.loadaddr, e->binary.file,
+					e->binary.nargs, low_addr, high_addr);
+				return 0;
+			}
+			headersz = e->binary.loadaddr - 0x40000000;
+		} else {
+			headersz = ALIGN(headersz, 16);
+		}
+
 		headersz += ALIGN(s.st_size, 4) + sizeof(uint32_t);
 		if (hasext)
 			*hasext = 1;
 	}
 
+	if (count > 0)
+		headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4;
+
 	return image_headersz_align(headersz, image_get_bootfrom());
 }
 
@@ -1104,11 +1129,16 @@ static int add_binary_header_v1(uint8_t **cur, uint8_t **next_ext,
 	/*
 	 * ARM executable code inside the BIN header on some mvebu platforms
 	 * (e.g. A370, AXP) must always be aligned with the 128-bit boundary.
+	 * In the case when this code is not position independent (e.g. ARM
+	 * SPL), it must be placed at fixed load and execute address.
 	 * This requirement can be met by inserting dummy arguments into
 	 * BIN header, if needed.
 	 */
 	offset = *cur - (uint8_t *)main_hdr;
-	add_args = ((16 - offset % 16) % 16) / sizeof(uint32_t);
+	if (binarye->binary.loadaddr)
+		add_args = (binarye->binary.loadaddr - 0x40000000 - offset) / sizeof(uint32_t);
+	else
+		add_args = ((16 - offset % 16) % 16) / sizeof(uint32_t);
 	if (add_args) {
 		*(args - 1) = cpu_to_le32(binarye->binary.nargs + add_args);
 		*cur += add_args * sizeof(uint32_t);
@@ -1520,10 +1550,40 @@ static int image_create_config_parse_oneline(char *line,
 		el->binary.file = strdup(value1);
 		while (1) {
 			char *value = strtok_r(NULL, delimiters, &saveptr);
+			char *endptr;
 
 			if (!value)
 				break;
-			el->binary.args[argi] = strtoul(value, NULL, 16);
+
+			if (!strcmp(value, "LOAD_ADDRESS")) {
+				value = strtok_r(NULL, delimiters, &saveptr);
+				if (!value) {
+					fprintf(stderr,
+						"Missing address argument for BINARY LOAD_ADDRESS\n");
+					return -1;
+				}
+				el->binary.loadaddr = strtoul(value, &endptr, 16);
+				if (*endptr) {
+					fprintf(stderr,
+						"Invalid argument '%s' for BINARY LOAD_ADDRESS\n",
+						value);
+					return -1;
+				}
+				value = strtok_r(NULL, delimiters, &saveptr);
+				if (value) {
+					fprintf(stderr,
+						"Unexpected argument '%s' after BINARY LOAD_ADDRESS\n",
+						value);
+					return -1;
+				}
+				break;
+			}
+
+			el->binary.args[argi] = strtoul(value, &endptr, 16);
+			if (*endptr) {
+				fprintf(stderr, "Invalid argument '%s' for BINARY\n", value);
+				return -1;
+			}
 			argi++;
 			if (argi >= BINARY_MAX_ARGS) {
 				fprintf(stderr,
-- 
2.20.1


  parent reply	other threads:[~2021-12-21 15:58 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-21 15:54 [PATCH u-boot-marvell 00/16] tools: kwbimage: Load address fixes Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 01/16] tools: kwbimage: Mark all local functions as static Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 02/16] tools: kwbimage: Deduplicate v1 regtype header finishing Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 03/16] tools: kwbimage: Fix generating image with multiple DATA_DELAY commands Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 04/16] tools: kwbimage: Preserve order of BINARY, DATA and " Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 05/16] arm: mvebu: Generate kwbimage.cfg with $(call cmd, ...) Pali Rohár
2021-12-21 15:54 ` Pali Rohár [this message]
2021-12-21 15:54 ` [PATCH u-boot-marvell 07/16] tools: kwbimage: Check the return value of image_headersz_v1() Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 08/16] arm: mvebu: Correctly set LOAD_ADDRESS for U-Boot SPL binary in kwbimage Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 09/16] arm: mvebu: Enable BootROM output on A38x Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 10/16] tools: kwbimage: Add missing check for maximal value for DATA_DELAY Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 11/16] tools: kwbimage: Show binary image address in mkimage -l, in addition to size Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 12/16] tools: kwbimage: Dump kwbimage config file on '-p -1' option Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 13/16] tools: kwbimage: Do not cast const pointers to non-const pointers Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 14/16] tools: kwbimage/kwboot: Check ext field for non-zero value Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 15/16] tools: kwbimage: Extract main data image without -p arg for dumpimage Pali Rohár
2021-12-21 15:54 ` [PATCH u-boot-marvell 16/16] tools: kwbimage: Fix mkimage/dumpimage -l argument Pali Rohár
2022-01-12  7:26 ` [PATCH u-boot-marvell 00/16] tools: kwbimage: Load address fixes Stefan Roese
2022-01-12 10:41   ` Pali Rohár
2022-01-12 10:55     ` Stefan Roese
2022-01-12 11:06       ` Stefan Roese
2022-01-12 11:34         ` Pali Rohár
2022-01-12 13:53           ` Stefan Roese
2022-01-12 14:16             ` Pali Rohár
2022-01-12 15:06               ` Stefan Roese
2022-01-12 15:09                 ` Pali Rohár
2022-01-12 17:20 ` [PATCH u-boot-marvell v2 00/20] " Pali Rohár
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 01/20] tools: kwbimage: Mark all local functions as static Pali Rohár
2022-01-13  6:30     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 02/20] tools: kwbimage: Deduplicate v1 regtype header finishing Pali Rohár
2022-01-13  6:31     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 03/20] tools: kwbimage: Fix generating image with multiple DATA_DELAY commands Pali Rohár
2022-01-13  6:31     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 04/20] tools: kwbimage: Preserve order of BINARY, DATA and " Pali Rohár
2022-01-13  6:32     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 05/20] arm: mvebu: Generate kwbimage.cfg with $(call cmd, ...) Pali Rohár
2022-01-13  6:32     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 06/20] tools: kwbimage: Add support for specifying CPU core Pali Rohár
2022-01-13  6:33     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 07/20] tools: kwbimage: Add support for specifying LOAD_ADDRESS for BINARY command Pali Rohár
2022-01-13  6:35     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 08/20] tools: kwbimage: Check the return value of image_headersz_v1() Pali Rohár
2022-01-13  6:35     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 09/20] tools: kwbimage: Check for maximal kwbimage header size Pali Rohár
2022-01-13  6:38     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 10/20] arm: mvebu: Set CPU for U-Boot SPL binary in kwbimage Pali Rohár
2022-01-13  6:39     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 11/20] arm: mvebu: Correctly set LOAD_ADDRESS " Pali Rohár
2022-01-13  6:40     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 12/20] tools: kwbimage: Enforce 128-bit boundary alignment only for Sheeva CPU Pali Rohár
2022-01-13  6:40     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 13/20] arm: mvebu: Enable BootROM output on A38x Pali Rohár
2022-01-13  6:42     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 14/20] tools: kwbimage: Add missing check for maximal value for DATA_DELAY Pali Rohár
2022-01-13  6:43     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 15/20] tools: kwbimage: Show binary image offset in mkimage -l, in addition to size Pali Rohár
2022-01-13  6:43     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 16/20] tools: kwbimage: Dump kwbimage config file on '-p -1' option Pali Rohár
2022-01-13  6:44     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 17/20] tools: kwbimage: Do not cast const pointers to non-const pointers Pali Rohár
2022-01-13  6:45     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 18/20] tools: kwbimage/kwboot: Check ext field for non-zero value Pali Rohár
2022-01-13  6:46     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 19/20] tools: kwbimage: Extract main data image without -p arg for dumpimage Pali Rohár
2022-01-13  6:46     ` Stefan Roese
2022-01-12 17:20   ` [PATCH u-boot-marvell v2 20/20] tools: kwbimage: Fix mkimage/dumpimage -l argument Pali Rohár
2022-01-13  6:46     ` Stefan Roese
2022-01-14 15:40   ` [PATCH u-boot-marvell v2 00/20] tools: kwbimage: Load address fixes 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=20211221155416.8557-7-pali@kernel.org \
    --to=pali@kernel.org \
    --cc=judge.packham@gmail.com \
    --cc=marek.behun@nic.cz \
    --cc=sr@denx.de \
    --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