[Concept,v2,02/20] event: Add EVT_BOOTCMD event for custom boot commands

Message ID 20251007170549.541981-3-sjg@u-boot.org
State New
Headers
Series expo: Complete mouse operation in the EFI app |

Commit Message

Simon Glass Oct. 7, 2025, 5:05 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Add a new event that is triggered in main_loop() before
autoboot_command(). This allows platform code to provide a custom
bootcmd string via the event system.

The event uses struct event_bootcmd which provides a buffer for the
bootcmd string and its size. Platform event handlers can write their
custom bootcmd to this buffer.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
---

(no changes since v1)

 common/event.c  |  1 +
 common/main.c   | 32 +++++++++++++++++++++++++++++++-
 include/event.h | 22 ++++++++++++++++++++++
 3 files changed, 54 insertions(+), 1 deletion(-)
  

Patch

diff --git a/common/event.c b/common/event.c
index a48ca6c549d..44f32035775 100644
--- a/common/event.c
+++ b/common/event.c
@@ -49,6 +49,7 @@  const char *const type_name[] = {
 
 	/* main loop events */
 	"main_loop",
+	"bootcmd",
 
 	/* booting */
 	"boot_os_addr",
diff --git a/common/main.c b/common/main.c
index b0b6e74f5d3..06e5193ecb9 100644
--- a/common/main.c
+++ b/common/main.c
@@ -14,6 +14,7 @@ 
 #include <command.h>
 #include <console.h>
 #include <env.h>
+#include <event.h>
 #include <fdtdec.h>
 #include <init.h>
 #include <net.h>
@@ -38,10 +39,35 @@  static void run_preboot_environment_command(void)
 	}
 }
 
+static const char *get_autoboot_cmd(char *buf, int size)
+{
+	const char *s = NULL;
+
+	if (IS_ENABLED(CONFIG_EVENT)) {
+		struct event_bootcmd event_bootcmd;
+		int ret;
+
+		event_bootcmd.bootcmd = buf;
+		event_bootcmd.size = size;
+		buf[0] = '\0';
+
+		ret = event_notify(EVT_BOOTCMD, &event_bootcmd,
+				   sizeof(event_bootcmd));
+		if (ret)
+			return NULL;
+
+		if (buf[0] != '\0')
+			s = buf;
+	}
+
+	return s;
+}
+
 /* We come here after U-Boot is initialised and ready to process commands */
 void main_loop(void)
 {
 	const char *s;
+	char bootcmd_buf[CONFIG_SYS_CBSIZE];
 
 	bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
 
@@ -64,7 +90,11 @@  void main_loop(void)
 
 	process_button_cmds();
 
-	s = bootdelay_process();
+	/* Allow platform code to provide bootcmd via event */
+	s = get_autoboot_cmd(bootcmd_buf, sizeof(bootcmd_buf));
+	if (!s)
+		s = bootdelay_process();
+
 	if (cli_process_fdt(&s))
 		cli_secure_boot_cmd(s);
 
diff --git a/include/event.h b/include/event.h
index 5fecaa66e80..2f0eac61633 100644
--- a/include/event.h
+++ b/include/event.h
@@ -172,6 +172,16 @@  enum event_t {
 	 */
 	EVT_MAIN_LOOP,
 
+	/**
+	 * @EVT_BOOTCMD:
+	 * This event is triggered in main_loop() before autoboot_command().
+	 * It allows platform code to provide a custom bootcmd string.
+	 * Its parameter is of type struct event_bootcmd.
+	 * The event handler can write the bootcmd to the provided buffer.
+	 * A non-zero return value causes the boot to fail.
+	 */
+	EVT_BOOTCMD,
+
 	/**
 	 * @EVT_BOOT_OS_ADDR:
 	 * Triggered immediately before the OS is loaded into its final address
@@ -270,6 +280,18 @@  union event_data {
 	struct event_bootm_final {
 		enum bootm_final_t flags;
 	} bootm_final;
+
+	/**
+	 * struct event_bootcmd - bootcmd override
+	 *
+	 * @bootcmd: Buffer for bootcmd string (provided by caller, must be an
+	 * empty string on entry)
+	 * @size: Size of bootcmd buffer
+	 */
+	struct event_bootcmd {
+		char *bootcmd;
+		int size;
+	} bootcmd;
 };
 
 /**