U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Garrett <mjg59@srcf.ucam.org>
To: u-boot@lists.denx.de
Cc: Matthew Garrett <mgarrett@aurora.tech>,
	AKASHI Takahiro <akashi.tkhro@gmail.com>,
	Dmitry Rokosov <ddrokosov@salutedevices.com>,
	Francis Laniel <francis.laniel@amarulasolutions.com>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Jerome Forissier <jerome.forissier@linaro.org>,
	Mattijs Korpershoek <mkorpershoek@baylibre.com>,
	Simon Glass <sjg@chromium.org>, Tom Rini <trini@konsulko.com>
Subject: [PATCH 03/10] Add a command to find a load address
Date: Sat, 23 Nov 2024 11:55:02 -0800	[thread overview]
Message-ID: <20241123195616.305687-4-mjg59@srcf.ucam.org> (raw)
In-Reply-To: <20241123195616.305687-1-mjg59@srcf.ucam.org>

From: Matthew Garrett <mgarrett@aurora.tech>

For systems with more complicated firmware, the firmware memory map may
vary significantly based on a number of factors. This makes it difficult to
pick a hardcoded load address. Add a command for finding an available
address with sufficient room to load the provided path.

Signed-off-by: Matthew Garrett <mgarrett@aurora.tech>
---

 cmd/Kconfig     |  7 ++++
 cmd/Makefile    |  1 +
 cmd/addr_find.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+)
 create mode 100644 cmd/addr_find.c

diff --git a/cmd/Kconfig b/cmd/Kconfig
index ee85928ca21..5ed6a50121c 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -128,6 +128,13 @@ config CMD_ACPI
 	  between the firmware and OS, and is particularly useful when you
 	  want to make hardware changes without the OS needing to be adjusted.
 
+config CMD_ADDR_FIND
+        bool "addr_find"
+	help
+	  This command searches for an unused region of address space
+	  sufficiently large to hold a file. If successful, it sets the
+	  loadaddr variable to this address.
+
 config CMD_ADDRMAP
 	bool "addrmap"
 	depends on ADDR_MAP
diff --git a/cmd/Makefile b/cmd/Makefile
index 16fd8dcd723..38cb7a4aea5 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -15,6 +15,7 @@ obj-y += version.o
 obj-$(CONFIG_CMD_ARMFFA) += armffa.o
 obj-$(CONFIG_CMD_2048) += 2048.o
 obj-$(CONFIG_CMD_ACPI) += acpi.o
+obj-$(CONFIG_CMD_ADDR_FIND) += addr_find.o
 obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
 obj-$(CONFIG_CMD_AES) += aes.o
 obj-$(CONFIG_CMD_ADC) += adc.o
diff --git a/cmd/addr_find.c b/cmd/addr_find.c
new file mode 100644
index 00000000000..b187337d885
--- /dev/null
+++ b/cmd/addr_find.c
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Aurora Innovation, Inc. Copyright 2022.
+ *
+ */
+
+#include <blk.h>
+#include <config.h>
+#include <command.h>
+#include <env.h>
+#include <fs.h>
+#include <lmb.h>
+#include <asm/global_data.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+int do_addr_find(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	struct lmb_region *mem, *reserved;
+	const char *filename;
+	struct lmb lmb;
+	loff_t size;
+	int ret;
+	int i, j;
+
+	if (!gd->fdt_blob) {
+		log_err("No FDT setup\n");
+		return CMD_RET_FAILURE;
+	}
+
+	if (fs_set_blk_dev(argv[1], argc >= 3 ? argv[2] : NULL, FS_TYPE_FAT)) {
+		log_err("Can't set block device\n");
+		return CMD_RET_FAILURE;
+	}
+
+	if (argc >= 4) {
+		filename = argv[3];
+	} else {
+		filename = env_get("bootfile");
+		if (!filename) {
+			log_err("No boot file defined\n");
+			return CMD_RET_FAILURE;
+		}
+	}
+
+	ret = fs_size(filename, &size);
+	if (ret != 0) {
+		log_err("Failed to get file size\n");
+		return CMD_RET_FAILURE;
+	}
+
+	lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
+	mem = &lmb.memory;
+	reserved = &lmb.reserved;
+
+	for (i = 0; i < mem->cnt; i++) {
+		unsigned long long start, end;
+
+		start = mem->region[i].base;
+		end = mem->region[i].base + mem->region[i].size - 1;
+		if ((start + size) > end)
+			continue;
+		for (j = 0; j < reserved->cnt; j++) {
+			if ((reserved->region[j].base + reserved->region[j].size) < start)
+				continue;
+			if ((start + size) > reserved->region[j].base)
+				start = reserved->region[j].base + reserved->region[j].size;
+		}
+		if ((start + size) <= end) {
+			env_set_hex("loadaddr", start);
+			debug("Set loadaddr to 0x%llx\n", start);
+			return CMD_RET_SUCCESS;
+		}
+	}
+
+	log_err("Failed to find enough RAM for 0x%llx bytes\n", size);
+	return CMD_RET_FAILURE;
+}
+
+U_BOOT_CMD(
+	addr_find, 7, 1, do_addr_find,
+	"find a load address suitable for a file",
+	"<interface> [<dev[:part]>] <filename>\n"
+	"- find a consecutive region of memory sufficiently large to hold\n"
+	"  the file called 'filename' from 'dev' on 'interface'. If\n"
+	"  successful, 'loadaddr' will be set to the located address."
+);
-- 
2.47.0


  parent reply	other threads:[~2024-11-23 21:49 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-23 19:54 [PATCH 00/10] Improve UEFI app support Matthew Garrett
2024-11-23 19:55 ` [PATCH 01/10] Add EFI handover support to bootm Matthew Garrett
2024-11-24 14:43   ` Heinrich Schuchardt
2024-11-24 19:29     ` Matthew Garrett
2024-11-24 19:51       ` Heinrich Schuchardt
2024-12-08 23:06         ` Simon Glass
2024-11-25 13:46       ` Ilias Apalodimas
2024-12-01 16:12   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 02/10] Add part_find command Matthew Garrett
2024-12-01 16:12   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-12-10  8:21   ` Heinrich Schuchardt
2024-12-10 16:16     ` Simon Glass
2024-11-23 19:55 ` Matthew Garrett [this message]
2024-11-24 15:56   ` [PATCH 03/10] Add a command to find a load address Tom Rini
2024-12-01 16:12   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 04/10] Hook up EFI env variable support in the EFI app Matthew Garrett
2024-12-01 16:12   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 05/10] Add EFI network driver Matthew Garrett
2024-12-01 16:12   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 06/10] Add UEFI TPM2 driver Matthew Garrett
2024-12-01 16:12   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 07/10] Support separate DTB files with the UEFI app Matthew Garrett
2024-11-25 13:55   ` Ilias Apalodimas
2024-12-01 16:14   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 08/10] Use the correct ramdisk address Matthew Garrett
2024-12-01 16:14   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 09/10] Fix efi_bind_block Matthew Garrett
2024-11-25 13:40   ` Ilias Apalodimas
2024-12-09  2:28     ` Simon Glass
2024-12-01 16:14   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-12-10  8:45   ` Heinrich Schuchardt
2024-12-10 16:17     ` Simon Glass
2024-12-10 17:11       ` Tom Rini
2024-12-11 17:50         ` Janis Danisevskis
2024-12-11 17:59           ` Ilias Apalodimas
2024-12-11 20:23           ` Simon Glass
2024-12-11 22:28             ` Janis Danisevskis
2024-11-23 19:55 ` [PATCH 10/10] Add command to set an environment variable to an EFI variable Matthew Garrett
2024-11-24 14:58   ` Heinrich Schuchardt
2024-12-01 16:14     ` Simon Glass
2024-12-08 15:29       ` Simon Glass
2024-12-09  2:28     ` Simon Glass
2024-11-24 14:40 ` [PATCH 00/10] Improve UEFI app support Heinrich Schuchardt
2024-12-01 16:15 ` Simon Glass
2024-12-08 15:28   ` Simon Glass
2024-12-08 15:51     ` Tom Rini
2024-12-08 18:50       ` Tom Rini
2024-12-08 23:07       ` Simon Glass

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=20241123195616.305687-4-mjg59@srcf.ucam.org \
    --to=mjg59@srcf.ucam.org \
    --cc=akashi.tkhro@gmail.com \
    --cc=ddrokosov@salutedevices.com \
    --cc=francis.laniel@amarulasolutions.com \
    --cc=ilias.apalodimas@linaro.org \
    --cc=jerome.forissier@linaro.org \
    --cc=mgarrett@aurora.tech \
    --cc=mkorpershoek@baylibre.com \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.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