Re: [PATCH 14/18] x86: Add a function to boot a zimage

2023-05-15 Thread Bin Meng
On Sat, Apr 29, 2023 at 3:18 AM Simon Glass  wrote:
>
> Add a direct interface to booting a zimage, so that bootstd can call it
> without going through the command-line interface.
>
> Signed-off-by: Simon Glass 
> ---
>
>  arch/x86/include/asm/zimage.h | 17 
>  arch/x86/lib/zimage.c | 82 ++-
>  2 files changed, 88 insertions(+), 11 deletions(-)
>

Reviewed-by: Bin Meng 


[PATCH 14/18] x86: Add a function to boot a zimage

2023-04-28 Thread Simon Glass
Add a direct interface to booting a zimage, so that bootstd can call it
without going through the command-line interface.

Signed-off-by: Simon Glass 
---

 arch/x86/include/asm/zimage.h | 17 
 arch/x86/lib/zimage.c | 82 ++-
 2 files changed, 88 insertions(+), 11 deletions(-)

diff --git a/arch/x86/include/asm/zimage.h b/arch/x86/include/asm/zimage.h
index 000b38ea899..966d7224eb1 100644
--- a/arch/x86/include/asm/zimage.h
+++ b/arch/x86/include/asm/zimage.h
@@ -72,4 +72,21 @@ int setup_zimage(struct boot_params *setup_base, char 
*cmd_line, int auto_boot,
  */
 void zimage_dump(struct boot_params *base_ptr);
 
+/**
+ * zboot_start() - Boot a zimage
+ *
+ * Boot a zimage, given the component parts
+ *
+ * @addr: Address where the bzImage is moved before booting, either
+ * BZIMAGE_LOAD_ADDR or ZIMAGE_LOAD_ADDR
+ * @base: Pointer to the boot parameters, typically at address
+ * DEFAULT_SETUP_BASE
+ * @initrd: Address of the initial ramdisk, or 0 if none
+ * @initrd_size: Size of the initial ramdisk, or 0 if none
+ * @cmdline: Command line to use for booting
+ * Return: -EFAULT on error (normally it does not return)
+ */
+int zboot_start(ulong addr, ulong size, ulong initrd, ulong initrd_size,
+   ulong base, char *cmdline);
+
 #endif
diff --git a/arch/x86/lib/zimage.c b/arch/x86/lib/zimage.c
index e5ea5129c1e..540d4d888bc 100644
--- a/arch/x86/lib/zimage.c
+++ b/arch/x86/lib/zimage.c
@@ -22,6 +22,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -442,8 +443,7 @@ static int do_zboot_start(struct cmd_tbl *cmdtp, int flag, 
int argc,
return 0;
 }
 
-static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, int argc,
-char *const argv[])
+static int zboot_load(void)
 {
struct boot_params *base_ptr;
 
@@ -460,31 +460,51 @@ static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, 
int argc,
   _address);
if (!base_ptr) {
puts("## Kernel loading failed ...\n");
-   return CMD_RET_FAILURE;
+   return -EINVAL;
}
}
state.base_ptr = base_ptr;
-   if (env_set_hex("zbootbase", (ulong)base_ptr) ||
+
+   return 0;
+}
+
+static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, int argc,
+char *const argv[])
+{
+   if (zboot_load())
+   return CMD_RET_FAILURE;
+
+   if (env_set_hex("zbootbase", map_to_sysmem(state.base_ptr)) ||
env_set_hex("zbootaddr", state.load_address))
return CMD_RET_FAILURE;
 
return 0;
 }
 
+static int zboot_setup(void)
+{
+   struct boot_params *base_ptr = state.base_ptr;
+   int ret;
+
+   ret = setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
+  0, state.initrd_addr, state.initrd_size,
+  (ulong)state.cmdline);
+   if (ret)
+   return -EINVAL;
+
+   return 0;
+}
+
 static int do_zboot_setup(struct cmd_tbl *cmdtp, int flag, int argc,
  char *const argv[])
 {
struct boot_params *base_ptr = state.base_ptr;
-   int ret;
 
if (!base_ptr) {
printf("base is not set: use 'zboot load' first\n");
return CMD_RET_FAILURE;
}
-   ret = setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
-  0, state.initrd_addr, state.initrd_size,
-  (ulong)state.cmdline);
-   if (ret) {
+   if (zboot_setup()) {
puts("Setting up boot parameters failed ...\n");
return CMD_RET_FAILURE;
}
@@ -501,8 +521,7 @@ static int do_zboot_info(struct cmd_tbl *cmdtp, int flag, 
int argc,
return 0;
 }
 
-static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, int argc,
-  char *const argv[])
+static int zboot_go(void)
 {
struct boot_params *params = state.base_ptr;
struct setup_header *hdr = >hdr;
@@ -522,11 +541,52 @@ static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, 
int argc,
 
/* we assume that the kernel is in place */
ret = boot_linux_kernel((ulong)state.base_ptr, entry, image_64bit);
+
+   return ret;
+}
+
+static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, int argc,
+  char *const argv[])
+{
+   int ret;
+
+   ret = zboot_go();
printf("Kernel returned! (err=%d)\n", ret);
 
return CMD_RET_FAILURE;
 }
 
+int zboot_start(ulong addr, ulong size, ulong initrd, ulong initrd_size,
+   ulong base, char *cmdline)
+{
+   int ret;
+
+   memset(, '\0', sizeof(state));
+
+   if (base) {
+   state.base_ptr = map_sysmem(base, 0);
+   state.load_address = addr;
+   } else {
+   state.bzimage_addr = addr;
+   }
+