All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: U-Boot Mailing List <u-boot@lists.denx.de>
Cc: Simon Glass <sjg@chromium.org>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Marek Vasut <marek.vasut+renesas@mailbox.org>,
	Mattijs Korpershoek <mkorpershoek@baylibre.com>,
	Neha Malcom Francis <n-francis@ti.com>,
	Quentin Schulz <quentin.schulz@cherry.de>,
	Tom Rini <trini@konsulko.com>
Subject: [PATCH 63/67] upl: Add a command to execute a UPL payload
Date: Thu,  2 Jan 2025 11:09:49 +1300	[thread overview]
Message-ID: <20250101221003.1944600-64-sjg@chromium.org> (raw)
In-Reply-To: <20250101221003.1944600-1-sjg@chromium.org>

Add a new 'upl exec' command which loads a payload, sets up the handoff
information and jumps to it.

This collects image information as the FIT's images are loaded.

With this, the size of the FIT is not known so is reported through UPL
as zero. Future work will address this.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 boot/Kconfig     |   8 +++
 boot/Makefile    |   1 +
 boot/upl_exec.c  | 130 +++++++++++++++++++++++++++++++++++++++++++++++
 boot/upl_write.c |   2 +-
 cmd/upl.c        |  25 ++++++++-
 include/upl.h    |  29 ++++++++++-
 6 files changed, 191 insertions(+), 4 deletions(-)
 create mode 100644 boot/upl_exec.c

diff --git a/boot/Kconfig b/boot/Kconfig
index f9263513268..983bd9cdbf6 100644
--- a/boot/Kconfig
+++ b/boot/Kconfig
@@ -765,6 +765,7 @@ config BOOTMETH_SCRIPT
 config UPL
 	bool "upl - Universal Payload Specification"
 	imply CMD_UPL
+	imply UPL_EXEC
 	imply UPL_READ
 	imply UPL_WRITE
 	imply SPL_UPL if SPL
@@ -794,6 +795,13 @@ config UPL_WRITE
 	  for how to tell U-Boot SPL to actually write it before jumping to
 	  the next phase.
 
+config UPL_EXEC
+	bool "upl - Support for executing a UPL image"
+	help
+	  Provides support for executing a loaded UPL image, passing it a
+	  suitable handoff block. This can be used to pass control to a UPL from
+	  another firmware project.
+
 config UPL_IN
 	bool "upl - Read the UPL handoff on startup"
 	select UPL_READ
diff --git a/boot/Makefile b/boot/Makefile
index 9446c6b82a9..bc4ff91a934 100644
--- a/boot/Makefile
+++ b/boot/Makefile
@@ -42,6 +42,7 @@ obj-$(CONFIG_$(PHASE_)OF_LIBFDT) += fdt_support.o
 obj-$(CONFIG_$(PHASE_)FDT_SIMPLEFB) += fdt_simplefb.o
 
 obj-$(CONFIG_$(PHASE_)UPL) += upl_common.o
+obj-$(CONFIG_$(PHASE_)UPL_EXEC) += upl_exec.o
 obj-$(CONFIG_$(PHASE_)UPL_READ) += upl_read.o
 obj-$(CONFIG_$(PHASE_)UPL_WRITE) += upl_write.o
 
diff --git a/boot/upl_exec.c b/boot/upl_exec.c
new file mode 100644
index 00000000000..8eb72a4cb16
--- /dev/null
+++ b/boot/upl_exec.c
@@ -0,0 +1,130 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * UPL handoff parsing
+ *
+ * Copyright 2024 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#define LOG_CATEGORY UCLASS_BOOTSTD
+
+#include <bootstage.h>
+#include <cpu.h>
+#include <display_options.h>
+#include <dm.h>
+#include <image.h>
+#include <log.h>
+#include <mapmem.h>
+#include <serial.h>
+#include <upl.h>
+#include <video.h>
+#include <asm/global_data.h>
+#include <dm/root.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+struct upl *cur_upl;
+
+static void upl_prepare(const struct upl_image *img, const struct abuf *buf)
+{
+	printf("\nUPL: handoff at %lx size %zx\n", abuf_addr(buf), buf->size);
+	printf("Starting at %lx ...\n\n", img->entry);
+
+	bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "upl_prepare");
+	if (IS_ENABLED(CONFIG_BOOTSTAGE_REPORT))
+		bootstage_report();
+
+	/*
+	 * Call remove function of all devices with a removal flag set.
+	 * This may be useful for last-stage operations, like cancelling
+	 * of DMA operation or releasing device internal buffers.
+	 */
+	dm_remove_devices_active();
+}
+
+int _upl_add_image(int node, ulong load_addr, ulong size, const char *desc)
+{
+	struct upl_image img;
+
+	if (!cur_upl)
+		return 0;
+
+	memset(&img, '\0', sizeof(img));
+	img.reg.base = load_addr;
+	img.reg.size = size;
+	img.offset = node;
+	img.description = desc;
+	if (!alist_add(&cur_upl->image, img))
+		return -ENOMEM;
+	log_debug("upl: add image %s at %lx size %lx\n", desc, load_addr, size);
+
+	return 0;
+}
+
+int upl_exec(ulong addr)
+{
+	struct bootm_headers images;
+	struct upl s_upl, *upl = &s_upl;
+	struct upl_image *img;
+	ulong img_data, img_len;
+	const char *uname;
+	struct abuf buf;
+	int ret;
+
+	upl_init(upl);
+
+	cur_upl = upl;
+	uname = NULL;
+	memset(&images, '\0', sizeof(images));
+	images.fit_uname_cfg = "conf-1";
+
+	/* a side-effect of this function is to call _upl_add_image() above */
+	ret = fit_image_load(&images, addr, &uname, &images.fit_uname_cfg,
+			     IH_ARCH_DEFAULT, IH_TYPE_FIRMWARE,
+			     BOOTSTAGE_ID_FIT_KERNEL_START,
+			     FIT_LOAD_OPTIONAL_NON_ZERO, &img_data, &img_len);
+	if (ret < 0) {
+		cur_upl = NULL;
+		return log_msg_retz("ufi", ret);
+	}
+
+	/* add the FIT */
+	upl->fit.base = addr;
+	upl->fit.size = 0;  /* TODO(sjg@chromium.org): Add size */
+
+	images.fit_hdr_os = map_sysmem(addr, 0);
+	img = alist_getw(&upl->image, 0, struct upl_image);
+	if (!img)
+		return log_msg_ret("uim", ret);
+
+	if (fit_image_get_entry(images.fit_hdr_os, ret, &img->entry))
+		return log_msg_ret("uae", -ENOENT);
+	log_debug("entry %lx\n", img->entry);
+
+	/* this calls _upl_add_image() with each loaded image */
+	ret = boot_get_loadable(&images);
+	cur_upl = NULL;
+	if (ret)
+		return log_msg_ret("ulo", ret);
+
+	/* we now have a full handoff state, including the images */
+	ret = upl_create_handoff(upl, &buf);
+	if (ret)
+		return log_msg_ret("uec", ret);
+
+	if (_DEBUG) {
+		set_working_fdt_addr(abuf_addr(&buf));
+		run_command("fdt print", 0);
+	}
+
+	upl_prepare(img, &buf);
+
+	if (IS_ENABLED(CONFIG_X86)) {
+		ret = arch_upl_jump(img->entry, &buf);
+	} else {
+		printf("UPL is not supported on this architecture\n");
+		ret = -ENOSYS;
+	}
+
+	return ret;
+}
diff --git a/boot/upl_write.c b/boot/upl_write.c
index c43a588dc40..ce9f733b450 100644
--- a/boot/upl_write.c
+++ b/boot/upl_write.c
@@ -685,7 +685,7 @@ int upl_create_handoff_tree(const struct upl *upl, oftree *treep)
 	return 0;
 }
 
-int upl_create_handoff(struct upl *upl, ulong addr, struct abuf *buf)
+int upl_create_handoff(struct upl *upl, struct abuf *buf)
 {
 	oftree tree;
 	int ret;
diff --git a/cmd/upl.c b/cmd/upl.c
index 461f8f4f0aa..e64ac7bbd20 100644
--- a/cmd/upl.c
+++ b/cmd/upl.c
@@ -112,12 +112,33 @@ static int do_upl_read(struct cmd_tbl *cmdtp, int flag, int argc,
 	return 0;
 }
 
+static int do_upl_exec(struct cmd_tbl *cmdtp, int flag, int argc,
+		       char *const argv[])
+{
+	ulong addr;
+	int ret;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	addr = hextoul(argv[1], NULL);
+	ret = upl_exec(addr);
+	if (ret) {
+		printf("Failed (err=%dE)\n", ret);
+		return CMD_RET_FAILURE;
+	}
+
+	return 0;
+}
+
 U_BOOT_LONGHELP(upl,
 	"info [-v]     - Check UPL status\n"
 	"upl read <addr>   - Read handoff information\n"
-	"upl write         - Write handoff information");
+	"upl write         - Write handoff information\n"
+	"upl exec          - Execute a loaded UPL");
 
 U_BOOT_CMD_WITH_SUBCMDS(upl, "Universal Payload support", upl_help_text,
 	U_BOOT_SUBCMD_MKENT(info, 2, 1, do_upl_info),
 	U_BOOT_SUBCMD_MKENT(read, 2, 1, do_upl_read),
-	U_BOOT_SUBCMD_MKENT(write, 1, 1, do_upl_write));
+	U_BOOT_SUBCMD_MKENT(write, 1, 1, do_upl_write),
+	U_BOOT_SUBCMD_MKENT(exec, 2, 1, do_upl_exec));
diff --git a/include/upl.h b/include/upl.h
index b9bcdf60089..f210a39df0c 100644
--- a/include/upl.h
+++ b/include/upl.h
@@ -347,6 +347,14 @@ int upl_create_handoff_tree(const struct upl *upl, oftree *treep);
  */
 int upl_read_handoff(struct upl *upl, oftree tree);
 
+/**
+ * upl_exec() - Execulated a loaded UPL image
+ *
+ * @addr: Address of image
+ * Return: 0 on success, or -ve error
+ */
+int upl_exec(ulong addr);
+
 /**
  * upl_get_test_data() - Fill a UPL with some test data
  *
@@ -404,7 +412,7 @@ int _upl_add_image(int node, ulong load_addr, ulong size, const char *desc);
 static inline int upl_add_image(const void *fit, int node, ulong load_addr,
 				ulong size)
 {
-	if (CONFIG_IS_ENABLED(UPL) && IS_ENABLED(CONFIG_XPL_BUILD)) {
+	if (CONFIG_IS_ENABLED(UPL)) {
 		const char *desc = fdt_getprop(fit, node, FIT_DESC_PROP, NULL);
 
 		return _upl_add_image(node, load_addr, size, desc);
@@ -469,6 +477,25 @@ int upl_write_to_buf(struct upl *upl, ofnode root, struct abuf *buf);
 int upl_add_region(struct alist *lst, u64 base, ulong size);
 #endif
 
+/**
+ * upl_create_handoff() - Create a UPL handoff
+ *
+ * @upl: Returns the handoff structure that was created
+ * @buf: Returns buffer containing the final handoff info
+ * Return: 0 if OK, -ve on error
+ */
+int upl_create_handoff(struct upl *upl, struct abuf *buf);
+
+/**
+ * arch_upl_jump() - Jump to the UPL payload
+ *
+ * Jump to
+ *
+ * @entry: Address to jump to
+ * @buf: Buffer containing UPL-handoff information
+ */
+int arch_upl_jump(ulong entry, const struct abuf *buf);
+
 /** upl_init() - Set up a UPL struct */
 void upl_init(struct upl *upl);
 
-- 
2.43.0


  parent reply	other threads:[~2025-01-01 22:25 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-01 22:08 [PATCH 00/67] upl: Align with the updated spec Simon Glass
2025-01-01 22:08 ` [PATCH 01/67] bloblist: Make BLOBLIST_ALLOC the default Simon Glass
2025-01-01 22:08 ` [PATCH 02/67] abuf: Provide a way to get the buffer address Simon Glass
2025-01-01 22:08 ` [PATCH 03/67] abuf: Allow use in host tools Simon Glass
2025-01-01 22:08 ` [PATCH 04/67] abuf: Provide a constant buffer Simon Glass
2025-01-01 22:08 ` [PATCH 05/67] cpu: Provide a way to get the physical-address size Simon Glass
2025-01-01 22:08 ` [PATCH 06/67] serial: Support info() method in ns16550 xPL with UPL Simon Glass
2025-01-01 22:08 ` [PATCH 07/67] mkimage: Update map_to_sysmem() to match its prototype Simon Glass
2025-01-01 22:08 ` [PATCH 08/67] x86: Emable meminfo command Simon Glass
2025-01-03 14:51   ` Heinrich Schuchardt
2025-01-04 19:30     ` Simon Glass
2025-01-01 22:08 ` [PATCH 09/67] x86: Show the timestamp counter with bdinfo Simon Glass
2025-01-01 22:08 ` [PATCH 10/67] ofnode: Use 4K for a default tree-size Simon Glass
2025-01-01 22:08 ` [PATCH 11/67] ofnode: Indicate when out of space in a few places Simon Glass
2025-01-01 22:08 ` [PATCH 12/67] ofnode: Update of_add_subnode() to indicate name is alloced Simon Glass
2025-01-01 22:08 ` [PATCH 13/67] boot: Rename fit_image_get_data() Simon Glass
2025-01-03 14:55   ` Heinrich Schuchardt
2025-01-01 22:09 ` [PATCH 14/67] boot: Rename fit_image_get_data_and_size() Simon Glass
2025-01-01 22:09 ` [PATCH 15/67] boot: Update fit_image_get_emb_data to use abuf Simon Glass
2025-01-01 22:09 ` [PATCH 16/67] boot: Use fit_image_get_data() to get data Simon Glass
2025-01-01 22:09 ` [PATCH 17/67] boot: Update fit_image_get_data() to use abuf Simon Glass
2025-01-01 22:09 ` [PATCH 18/67] test: Fix inpected typo in upl test Simon Glass
2025-01-01 22:09 ` [PATCH 19/67] emulation: fdt: Relax condition for OF_HAS_PRIOR_STAGE Simon Glass
2025-01-01 22:09 ` [PATCH 20/67] emulation: Use bloblist to hold tables Simon Glass
2025-01-01 22:09 ` [PATCH 21/67] x86: Create more space for SPL with qemu-x86_64 Simon Glass
2025-01-01 22:09 ` [PATCH 22/67] upl: Create a function to create a memory node Simon Glass
2025-01-01 22:09 ` [PATCH 23/67] upl: Update add_upl_memmap() to use write_mem_node() Simon Glass
2025-01-01 22:09 ` [PATCH 24/67] upl: Update add_upl_memres() " Simon Glass
2025-01-01 22:09 ` [PATCH 25/67] upl: Support writing a UPL only for testing Simon Glass
2025-01-01 22:09 ` [PATCH 26/67] upl: Drop enum upl_serial_access_type Simon Glass
2025-01-01 22:09 ` [PATCH 27/67] upl: Init serial and graphics alists in upl_init() Simon Glass
2025-01-01 22:09 ` [PATCH 28/67] upl: Move serial and graphics addition-code to upl_common Simon Glass
2025-01-01 22:09 ` [PATCH 29/67] pci: video: Set up the pixel-format field Simon Glass
2025-01-01 22:09 ` [PATCH 30/67] x86: Show an error if video fails Simon Glass
2025-01-01 22:09 ` [PATCH 31/67] x86: Support jumping to a UPL image Simon Glass
2025-01-01 22:09 ` [PATCH 32/67] x86: Enable UPL handoff for SPL Simon Glass
2025-01-01 22:09 ` [PATCH 33/67] x86: Align the SMBIOS table to a 4K boundary Simon Glass
2025-01-03 15:10   ` Heinrich Schuchardt
2025-01-01 22:09 ` [PATCH 34/67] upl: Require OFNODE_MULTI_TREE when writing a UPL handoff Simon Glass
2025-01-01 22:09 ` [PATCH 35/67] upl: Drop the argument to spl_write_upl_handoff() Simon Glass
2025-01-01 22:09 ` [PATCH 36/67] x86: emulation: Enable UPL Simon Glass
2025-01-01 22:09 ` [PATCH 37/67] upl: Correct comment and condition in add_upl_memres() Simon Glass
2025-01-01 22:09 ` [PATCH 38/67] upl: Add addr/size tags in the reserved-memory node Simon Glass
2025-01-01 22:09 ` [PATCH 39/67] upl: Update upl_add_graphics() to return framebuffer Simon Glass
2025-01-01 22:09 ` [PATCH 40/67] upl: Add a function to write a UPL handoff to an abuf Simon Glass
2025-01-01 22:09 ` [PATCH 41/67] upl: Use a 64-bit value for a memregion base-address Simon Glass
2025-01-01 22:09 ` [PATCH 42/67] upl: Set bit 32 of the address when using ISA Simon Glass
2025-01-01 22:09 ` [PATCH 43/67] upl: Add RAM to the memory region Simon Glass
2025-01-01 22:09 ` [PATCH 44/67] upl: Add a compatible string for the upl-params node Simon Glass
2025-01-01 22:09 ` [PATCH 45/67] upl: Move buffer_addr_size() higher and rename Simon Glass
2025-01-01 22:09 ` [PATCH 46/67] upl: Factor out part of encode_reg() to new function Simon Glass
2025-01-01 22:09 ` [PATCH 47/67] upl: Move decode_addr_size() higher and rename Simon Glass
2025-01-01 22:09 ` [PATCH 48/67] upl: Factor out part of decode_reg() to new function Simon Glass
2025-01-01 22:09 ` [PATCH 49/67] upl: Correct name of upl-images Simon Glass
2025-01-01 22:09 ` [PATCH 50/67] dm: core: Clarify behaviour of ofnode_name_eq() Simon Glass
2025-01-01 22:09 ` [PATCH 51/67] dm: core: Allow ofnode_name_eq() to accept a unit address Simon Glass
2025-01-01 22:09 ` [PATCH 52/67] dm: core: Rewrite ofnode_find_subnode() to use ofnode API Simon Glass
2025-01-01 22:09 ` [PATCH 53/67] upl: test: Show the handoff FDT when debugging Simon Glass
2025-01-01 22:09 ` [PATCH 54/67] upl: Use a reg property to specify the FIT address and size Simon Glass
2025-01-01 22:09 ` [PATCH 55/67] upl: Update and tidy writing of images Simon Glass
2025-01-01 22:09 ` [PATCH 56/67] upl: Make use of alist_for_each() Simon Glass
2025-01-01 22:09 ` [PATCH 57/67] upl: Correct use of sizeof() Simon Glass
2025-01-01 22:09 ` [PATCH 58/67] upl: Drop acpi_nvs_size Simon Glass
2025-01-01 22:09 ` [PATCH 59/67] upl: Improve uniqueness of log_msg_ret() strings Simon Glass
2025-01-01 22:09 ` [PATCH 60/67] upl: Add PCI information Simon Glass
2025-01-01 22:09 ` [PATCH 61/67] upl: Handle serial on an ISA bus with /chosen node Simon Glass
2025-01-01 22:09 ` [PATCH 62/67] upl: Add missing word in comment for upl_write_handoff() Simon Glass
2025-01-01 22:09 ` Simon Glass [this message]
2025-01-01 22:09 ` [PATCH 64/67] scripts: Update qemu script to use 64-bit on x86 always Simon Glass
2025-01-01 22:09 ` [PATCH 65/67] scripts: Update qemu script to support specifying a UPL Simon Glass
2025-01-01 22:09 ` [PATCH 66/67] upl: Add reserved memory for the ACPI and SMBIOS tables Simon Glass
2025-01-01 22:09 ` [PATCH 67/67] upl: Add an 'addr' property for Tianocore Simon Glass
2025-01-02 14:44 ` [PATCH 00/67] upl: Align with the updated spec Tom Rini
2025-01-02 23:45   ` Simon Glass
2025-01-03 14:26     ` Tom Rini
2025-01-04 19:32       ` 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=20250101221003.1944600-64-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=ilias.apalodimas@linaro.org \
    --cc=marek.vasut+renesas@mailbox.org \
    --cc=mkorpershoek@baylibre.com \
    --cc=n-francis@ti.com \
    --cc=quentin.schulz@cherry.de \
    --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 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.