[PATCH] cmd: add FDT setup for bootelf by flag

2024-03-07 Thread Maxim Moskalets
From: Maxim Moskalets 

Added the ability to use FDT for ELF applications, required to run some
OS. To make FDT setup, you need to set the -d fdt_addr_r cmd option for
bootelf command. Enable by selecting CMD_ELF_FDT_SETUP.

Signed-off-by: Maxim Moskalets 
---

 cmd/Kconfig | 11 +++
 cmd/elf.c   | 39 ---
 2 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index a86b570517..8821e66f76 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -479,6 +479,17 @@ config CMD_ELF
help
  Boot an ELF/vxWorks image from the memory.
 
+config CMD_ELF_FDT_SETUP
+   bool "Flattened Device Tree setup in bootelf cmd"
+   default n
+   depends on CMD_ELF
+   select LIB_LIBFDT
+   select LMB
+   help
+ Do FDT setup in bootelf command optionally by param -d, which
+ allows to bring additional system info (e.g. /memory node) to
+ the Operating System or application.
+
 config CMD_FDT
bool "Flattened Device Tree utility commands"
default y
diff --git a/cmd/elf.c b/cmd/elf.c
index b7b9f506a5..df4354d374 100644
--- a/cmd/elf.c
+++ b/cmd/elf.c
@@ -38,6 +38,10 @@ static unsigned long do_bootelf_exec(ulong (*entry)(int, 
char * const[]),
 /* Interpreter command to boot an arbitrary ELF image from memory */
 int do_bootelf(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
+#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
+   struct bootm_headers img = {0};
+   unsigned long fdt_addr = 0; /* Address of the FDT */
+#endif
unsigned long addr; /* Address of the ELF image */
unsigned long rc; /* Return value from user code */
char *sload = NULL;
@@ -46,13 +50,25 @@ int do_bootelf(struct cmd_tbl *cmdtp, int flag, int argc, 
char *const argv[])
/* Consume 'bootelf' */
argc--; argv++;
 
-   /* Check for flag. */
+   /* Check for [-p|-s] flag. */
if (argc >= 1 && (argv[0][0] == '-' && \
(argv[0][1] == 'p' || argv[0][1] == 's'))) {
sload = argv[0];
/* Consume flag. */
argc--; argv++;
}
+
+#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
+   /* Check for [-d fdt_addr_r] option. */
+   if ((argc >= 2) && (argv[0][0] == '-') && (argv[0][1] == 'd')) {
+   if (strict_strtoul(argv[1], 16, _addr) != 0)
+   return CMD_RET_USAGE;
+   /* Consume option. */
+   argc -= 2;
+   argv += 2;
+   }
+#endif
+
/* Check for address. */
if (argc >= 1 && strict_strtoul(argv[0], 16, ) != -EINVAL) {
/* Consume address */
@@ -68,6 +84,16 @@ int do_bootelf(struct cmd_tbl *cmdtp, int flag, int argc, 
char *const argv[])
else
addr = load_elf_image_shdr(addr);
 
+#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
+   if (fdt_addr) {
+   printf("## Setting up FDT at 0x%08lx ...\n", fdt_addr);
+   flush();
+
+   if (image_setup_libfdt(, (void *)fdt_addr, NULL))
+   return 1;
+   }
+#endif
+
if (!env_get_autostart())
return rcode;
 
@@ -298,9 +324,16 @@ int do_bootvx(struct cmd_tbl *cmdtp, int flag, int argc, 
char *const argv[])
 U_BOOT_CMD(
bootelf, CONFIG_SYS_MAXARGS, 0, do_bootelf,
"Boot from an ELF image in memory",
-   "[-p|-s] [address]\n"
+   "[-p|-s] "
+#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
+   "[-d fdt_addr_r] "
+#endif
+   "[address]\n"
"\t- load ELF image at [address] via program headers (-p)\n"
-   "\t  or via section headers (-s)"
+   "\t  or via section headers (-s)\n"
+#if CONFIG_IS_ENABLED(CMD_ELF_FDT_SETUP)
+   "\t- setup FDT image at [fdt_addr_r] (-d)"
+#endif
 );
 
 U_BOOT_CMD(
-- 
2.39.2



Re: [PATCH] cmd: add FDT setup for bootelf by flag

2024-02-12 Thread Tom Rini
On Mon, Feb 12, 2024 at 09:41:17PM +0300, Maxim Moskalets wrote:

> Added the ability to use FDT for ELF applications, required to run some OS. 
> To make FDT setup, you need to set the elf_needed_fdt environment variable to 
> a value like y or yes.
> 
> Signed-off-by: Maxim Moskalets 
> 
> Cc: Tom Rini 
> ---
> 
>  cmd/elf.c | 14 ++
>  env/common.c  |  5 +
>  include/env.h |  7 +++
>  3 files changed, 26 insertions(+)

Is there some change compared with the last version of this patch? And,
I was thinking of how to reply to the previous one and I think my
question is why don't we just use a flag being passed to bootelf instead
of this? Doing bootelf addr -d fdt_addr seems clearer than bootelf addr1
addr2 and checking the environment.

-- 
Tom


signature.asc
Description: PGP signature


[PATCH] cmd: add FDT setup for bootelf by flag

2024-02-12 Thread Maxim Moskalets
Added the ability to use FDT for ELF applications, required to run some OS. To 
make FDT setup, you need to set the elf_needed_fdt environment variable to a 
value like y or yes.

Signed-off-by: Maxim Moskalets 

Cc: Tom Rini 
---

 cmd/elf.c | 14 ++
 env/common.c  |  5 +
 include/env.h |  7 +++
 3 files changed, 26 insertions(+)

diff --git a/cmd/elf.c b/cmd/elf.c
index b7b9f506a5..1ee86c03de 100644
--- a/cmd/elf.c
+++ b/cmd/elf.c
@@ -38,6 +38,8 @@ static unsigned long do_bootelf_exec(ulong (*entry)(int, char 
* const[]),
 /* Interpreter command to boot an arbitrary ELF image from memory */
 int do_bootelf(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
+   struct bootm_headers img = {0};
+   unsigned long fdt_addr; /* Address of the FDT */
unsigned long addr; /* Address of the ELF image */
unsigned long rc; /* Return value from user code */
char *sload = NULL;
@@ -68,6 +70,18 @@ int do_bootelf(struct cmd_tbl *cmdtp, int flag, int argc, 
char *const argv[])
else
addr = load_elf_image_shdr(addr);
 
+   if (env_get_elf_need_fdt()) {
+   if (argc >= 1 && strict_strtoul(argv[0], 16, _addr) != 
-EINVAL) {
+   printf("Got FDT at 0x%08lx ...\n", fdt_addr);
+
+   if (image_setup_libfdt(, (void *)fdt_addr, NULL)) {
+   printf("ERROR: Failed to process device 
tree\n");
+   return 1;
+   }
+   }
+   }
+
+
if (!env_get_autostart())
return rcode;
 
diff --git a/env/common.c b/env/common.c
index 48a565107c..8cd8558c3f 100644
--- a/env/common.c
+++ b/env/common.c
@@ -346,6 +346,11 @@ bool env_get_autostart(void)
return env_get_yesno("autostart") == 1;
 }
 
+bool env_get_elf_need_fdt(void)
+{
+   return env_get_yesno("elf_need_fdt") == 1;
+}
+
 /*
  * Look up the variable from the default environment
  */
diff --git a/include/env.h b/include/env.h
index d2a5954ded..384c312d2e 100644
--- a/include/env.h
+++ b/include/env.h
@@ -148,6 +148,13 @@ int env_get_yesno(const char *var);
  */
 bool env_get_autostart(void);
 
+/**
+ * env_get_elf_need_fdt() - Check if FDT is needed for ELF image
+ *
+ * Return: true if the "elf_need_fdt" env var exists and is set to "yes"
+ */
+bool env_get_elf_need_fdt(void);
+
 /**
  * env_set() - set an environment variable
  *
-- 
2.39.2