[Concept,07/18] sandbox: Extract init code into sandbox_init()

Message ID 20250904130459.848794-8-sjg@u-boot.org
State New
Headers
Series ulib: Introduce building U-Boot as a shared library |

Commit Message

Simon Glass Sept. 4, 2025, 1:04 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Split the init code from sandbox_main() into a separate sandbox_init()
function that handles all setup up to the call to board_init_f(). This
allows the init to be called independently of the main execution flow.

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

 arch/sandbox/cpu/start.c                  | 25 ++++++++++++++++-------
 arch/sandbox/include/asm/u-boot-sandbox.h | 21 +++++++++++++++++++
 2 files changed, 39 insertions(+), 7 deletions(-)
  

Patch

diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c
index 40a3559d244..59682b039e5 100644
--- a/arch/sandbox/cpu/start.c
+++ b/arch/sandbox/cpu/start.c
@@ -601,18 +601,16 @@  static int last_stage_init(void)
 }
 EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, last_stage_init);
 
-int sandbox_main(int argc, char *argv[])
+int sandbox_init(int argc, char *argv[], struct global_data *data)
 {
 	struct sandbox_state *state;
 	void * text_base;
-	gd_t data;
 	int size;
 	int ret;
 
 	text_base = os_find_text_base();
 
-	memset(&data, '\0', sizeof(data));
-	gd = &data;
+	gd = data;
 
 	/*
 	 * This must be the first invocation of os_malloc() to have
@@ -620,7 +618,7 @@  int sandbox_main(int argc, char *argv[])
 	 */
 	ret = state_init();
 	if (ret)
-		goto err;
+		return ret;
 
 	/*
 	 * Copy argv[] so that we can pass the arguments in the original
@@ -663,13 +661,13 @@  int sandbox_main(int argc, char *argv[])
 	if (state->read_state && state->state_fname) {
 		ret = sandbox_read_state(state, state->state_fname);
 		if (ret)
-			goto err;
+			return ret;
 	}
 
 	if (state->handle_signals) {
 		ret = os_setup_signal_handlers();
 		if (ret)
-			goto err;
+			return ret;
 	}
 
 	if (state->upl)
@@ -692,6 +690,19 @@  int sandbox_main(int argc, char *argv[])
 	/* sandbox test: log functions called before log_init in board_init_f */
 	log_debug("debug: %s\n", __func__);
 
+	return 0;
+}
+
+int sandbox_main(int argc, char *argv[])
+{
+	gd_t data;
+	int ret;
+
+	memset(&data, '\0', sizeof(data));
+	ret = sandbox_init(argc, argv, &data);
+	if (ret)
+		goto err;
+
 	/* Do pre- and post-relocation init */
 	board_init_f(gd->flags);
 
diff --git a/arch/sandbox/include/asm/u-boot-sandbox.h b/arch/sandbox/include/asm/u-boot-sandbox.h
index 001b00fe41e..941f35f9e69 100644
--- a/arch/sandbox/include/asm/u-boot-sandbox.h
+++ b/arch/sandbox/include/asm/u-boot-sandbox.h
@@ -16,6 +16,8 @@ 
 
 #include <linux/compiler_attributes.h>
 
+struct global_data;
+
 /* board/.../... */
 int board_init(void);
 
@@ -41,6 +43,22 @@  void sandbox_reset(void);
 /* Exit sandbox (quit U-Boot) */
 void __noreturn sandbox_exit(void);
 
+/**
+ * sandbox_init() - init sandbox
+ *
+ * This function initialises sandbox state, parses arguments, and sets up the
+ * global data structure, but does not call board_init_f().
+ *
+ * The caller must zero @data before calling this function. This function sets
+ * gd to point to @data so it must remain valid for the life of sandbox.
+ *
+ * @argc:	the number of arguments passed to the program
+ * @argv:	array of argc pointers, plus a NULL terminator
+ * @data:	pointer to global data structure to init
+ * Return: 0 if OK, -ve on error
+ */
+int sandbox_init(int argc, char *argv[], struct global_data *data);
+
 /**
  * sandbox_main() - main entrypoint for sandbox
  *
@@ -50,6 +68,9 @@  void __noreturn sandbox_exit(void);
  * This calls sandbox_init(), then board_init_f/r(). It does not return unless
  * something goes wrong.
  *
+ * @argc:	the number of arguments passed to the program
+ * @argv:	array of argc pointers, plus a NULL terminator
+ *
  * Return: 1 on error
  */
 int sandbox_main(int argc, char *argv[]);