]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
cmd: mem: add command for getting ram size for use in scripts
authorFrank Wunderlich <frank-w@public-files.de>
Wed, 4 Feb 2026 18:40:42 +0000 (19:40 +0100)
committerTom Rini <trini@konsulko.com>
Sat, 7 Feb 2026 17:51:33 +0000 (11:51 -0600)
Add a command for getting detected ram size with possibility to
assign it to an environment variable.

example usage:

BPI-R4> memsize
4096 MiB
BPI-R4> memsize memsz
BPI-R4> printenv memsz
memsz=4096
BPI-R4>

board with 8GB ram:

BPI-R4> memsize
8192 MiB
BPI-R4> memsize memsz
BPI-R4> printenv memsz
memsz=8192
BPI-R4>

Signed-off-by: Frank Wunderlich <frank-w@public-files.de>
cmd/Kconfig
cmd/meminfo.c

index 595ac49da41b141e3d71fc580149ded339d0fa69..f12fcf6631e0ef5234c70ab6160fde39f1fbe96e 100644 (file)
@@ -940,6 +940,13 @@ config CMD_MEMINFO_MAP
 
          See doc/usage/cmd/meminfo.rst for more information.
 
+config CMD_MEMSIZE
+       bool "memsize"
+       depends on CMD_MEMINFO
+       help
+         Get RAM via command for use in scripts. Print or assign decimal value
+         in MiB to environment variable.
+
 config CMD_MEMORY
        bool "md, mm, nm, mw, cp, cmp, base, loop"
        default y
index aa3b5bafe176000da7e91a6eb424321858182832..e7db9d065f5acfc9f40473af2529a207dc64dc4d 100644 (file)
@@ -8,10 +8,12 @@
 #include <bootstage.h>
 #include <command.h>
 #include <display_options.h>
+#include <env.h>
 #include <lmb.h>
 #include <malloc.h>
 #include <mapmem.h>
 #include <asm/global_data.h>
+#include <linux/sizes.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -98,8 +100,31 @@ static int do_meminfo(struct cmd_tbl *cmdtp, int flag, int argc,
        return 0;
 }
 
+#ifdef CONFIG_CMD_MEMSIZE
+static int do_mem_size(struct cmd_tbl *cmdtp, int flag, int argc,
+                      char *const argv[])
+{
+       u64 memsize = gd->ram_size / SZ_1M;
+
+       if (argc > 1)
+               return env_set_ulong(argv[1], memsize);
+       else
+               printf("%lld MiB\n", memsize);
+
+       return 0;
+}
+#endif /* CONFIG_CMD_MEMSIZE */
+
 U_BOOT_CMD(
        meminfo,        1,      1,      do_meminfo,
        "display memory information",
        ""
 );
+
+#ifdef CONFIG_CMD_MEMSIZE
+U_BOOT_CMD(
+       memsize,        2,      1,      do_mem_size,
+       "get detected ram size in MiB, optional set env variable with value",
+       "[envvar]"
+);
+#endif /* CONFIG_CMD_MEMSIZE */