Skip to contentSkip to conversation

cmd: mem: add function for getting ram size for use in scripts

PATCH Superseded Frank Wunderlich 3 participants 3 messages Jan 17 – Feb 4

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 cmd: mem: add function for getting ram size for use in scripts

From: Frank Wunderlich <[email protected]>

Add a command for getting detected ram size with possibility to write to environment variable.

example usage:

BPI-R4> msize
4294967296
BPI-R4> msize m
4096m
BPI-R4> msize g
4g
BPI-R4> msize g ramsize
BPI-R4> printenv ramsize
ramsize=4
BPI-R4>

board with 8GB ram:

BPI-R4> msize
8589934592
BPI-R4> msize m
8192m
BPI-R4> msize g
8g
BPI-R4> msize g ramsize
BPI-R4> printenv ramsize
ramsize=8
BPI-R4>

---
v2: add Kconfig entry

---
 cmd/Kconfig |  5 +++++
 cmd/mem.c   | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
cmd/Kconfig +5 -0
index 5c611fb3016e..b82b17195b7e 100644
@@ -974,6 +974,11 @@ config CMD_RANDOM
 	help
 	  random - fill memory with random data
 
+config CMD_MEMSIZE
+	bool "memsize"
+	help
+	  Get RAM via command for use in scripts.
+
 config CMD_MEMTEST
 	bool "memtest"
 	help
cmd/mem.c +32 -0
index d5d7ca2790bd..a067740d81c8 100644
@@ -33,6 +33,7 @@
 #include <linux/compiler.h>
 #include <linux/ctype.h>
 #include <linux/delay.h>
+#include <linux/sizes.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -711,6 +712,29 @@ static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc,
 }
 #endif /* CONFIG_LOOPW */
 
+#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;
+
+	if (argc > 1) {
+		if (!strcmp(argv[1], "m"))
+			memsize = (memsize + SZ_1M - 1) / SZ_1M;
+		else if (!strcmp(argv[1], "g"))
+			memsize = (memsize + SZ_1G - 1) / SZ_1G;
+		if (argc > 2)
+			env_set_ulong(argv[2], memsize);
+		else
+			printf("%lld%s\n", memsize, argv[1]);
+	} else {
+		printf("%lld\n", memsize);
+	}
+
+	return 0;
+}
+#endif /* CONFIG_CMD_MEMSIZE */
+
 #ifdef CONFIG_CMD_MEMTEST
 static ulong mem_test_alt(volatile ulong *buf, ulong start_addr, ulong end_addr,
 			  volatile ulong *dummy)
@@ -1404,6 +1428,14 @@ U_BOOT_CMD(
 );
 #endif /* CONFIG_LOOPW */
 
+#ifdef CONFIG_CMD_MEMSIZE
+U_BOOT_CMD(
+	msize,	3,	1,	do_mem_size,
+	"get detected ram size, optional set env variable with value",
+	"[m, g] [envvar]"
+);
+#endif /* CONFIG_CMD_MEMSIZE */
+
 #ifdef CONFIG_CMD_MEMTEST
 U_BOOT_CMD(
 	mtest,	5,	1,	do_mem_mtest,
Signature · 1 line
2.43.0
#1 lore raw

Hi Frank,

Quoted 32 lines · Frank Wunderlich

From: Frank Wunderlich <[email protected]>

Add a command for getting detected ram size with possibility to write to environment variable.

example usage:

BPI-R4> msize
4294967296
BPI-R4> msize m
4096m
BPI-R4> msize g
4g
BPI-R4> msize g ramsize
BPI-R4> printenv ramsize
ramsize=4
BPI-R4>

board with 8GB ram:

BPI-R4> msize
8589934592
BPI-R4> msize m
8192m
BPI-R4> msize g
8g
BPI-R4> msize g ramsize
BPI-R4> printenv ramsize
ramsize=8
BPI-R4>

Quoted 7 lines

---
v2: add Kconfig entry

---
 cmd/Kconfig |  5 +++++
 cmd/mem.c   | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

Please can you add doc/usage/cmd/ and test/cmd

cmd/Kconfig config CMD_RANDOM · line 980

Quoted 13 lines
cmd/Kconfig +4 -0
index 5c611fb3016e..b82b17195b7e 100644
@@ -974,6 +974,11 @@ config CMD_RANDOM
        help
          random - fill memory with random data
 
+config CMD_MEMSIZE
+       bool "memsize"
+       help
+         Get RAM via command for use in scripts.

I suggest a little more info in your help, so it is clear what the command actually does.

Quoted 21 lines
cmd/Kconfig +1 -0
+
 config CMD_MEMTEST
        bool "memtest"
        help
cmd/mem.c +2 -0
index d5d7ca2790bd..a067740d81c8 100644
@@ -33,6 +33,7 @@
 #include <linux/compiler.h>
 #include <linux/ctype.h>
 #include <linux/delay.h>
+#include <linux/sizes.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -711,6 +712,29 @@ static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc,
 }
 #endif /* CONFIG_LOOPW */
 
+#ifdef CONFIG_CMD_MEMSIZE

cmd/mem.c static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc, · line 725

Quoted 10 lines
cmd/mem.c +10 -0
+static int do_mem_size(struct cmd_tbl *cmdtp, int flag, int argc,
+                      char *const argv[])
+{
+       u64 memsize = gd->ram_size;
+
+       if (argc > 1) {
+               if (!strcmp(argv[1], "m"))
+                       memsize = (memsize + SZ_1M - 1) / SZ_1M;
+               else if (!strcmp(argv[1], "g"))
+                       memsize = (memsize + SZ_1G - 1) / SZ_1G;

Can you do something like this?

memsize = ALIGN(memsize, SZ_1G)

cmd/mem.c static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc, · line 727

Quoted 2 lines
cmd/mem.c +2 -0
+               if (argc > 2)
+                       env_set_ulong(argv[2], memsize);

Normally U-Boot uses hex, so it would be worth mentioning that this command doesn't. Or even provide a -d flag to select decimal?

Quoted 29 lines
cmd/mem.c +18 -1
+               else
+                       printf("%lld%s\n", memsize, argv[1]);
+       } else {
+               printf("%lld\n", memsize);
+       }
+
+       return 0;
+}
+#endif /* CONFIG_CMD_MEMSIZE */
+
 #ifdef CONFIG_CMD_MEMTEST
 static ulong mem_test_alt(volatile ulong *buf, ulong start_addr, ulong end_addr,
                          volatile ulong *dummy)
@@ -1404,6 +1428,14 @@ U_BOOT_CMD(
 );
 #endif /* CONFIG_LOOPW */
 
+#ifdef CONFIG_CMD_MEMSIZE
+U_BOOT_CMD(
+       msize,  3,      1,      do_mem_size,
+       "get detected ram size, optional set env variable with value",
+       "[m, g] [envvar]"
+);
+#endif /* CONFIG_CMD_MEMSIZE */
+
 #ifdef CONFIG_CMD_MEMTEST
 U_BOOT_CMD(
        mtest,  5,      1,      do_mem_mtest,
--
Quoted 2 lines

2.43.0

Regards,
Simon

#2 lore raw

Hi Simon

Thanks for your review.

Am 4. Februar 2026 01:23:24 MEZ schrieb Simon Glass <[email protected]>:

Quoted 43 lines

Hi Frank,

Quoted 32 lines · Frank Wunderlich

From: Frank Wunderlich <[email protected]>

Add a command for getting detected ram size with possibility to write to environment variable.

example usage:

BPI-R4> msize
4294967296
BPI-R4> msize m
4096m
BPI-R4> msize g
4g
BPI-R4> msize g ramsize
BPI-R4> printenv ramsize
ramsize=4
BPI-R4>

board with 8GB ram:

BPI-R4> msize
8589934592
BPI-R4> msize m
8192m
BPI-R4> msize g
8g
BPI-R4> msize g ramsize
BPI-R4> printenv ramsize
ramsize=8
BPI-R4>

Quoted 7 lines

---
v2: add Kconfig entry

---
 cmd/Kconfig |  5 +++++
 cmd/mem.c   | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
Quoted 1 line

Please can you add doc/usage/cmd/ and test/cmd

I already did in v6.

https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

Quoted 9 lines
Quoted 9 lines
cmd/Kconfig +0 -0
index 5c611fb3016e..b82b17195b7e 100644
@@ -974,6 +974,11 @@ config CMD_RANDOM
        help
          random - fill memory with random data
 
Quoted 7 lines

cmd/Kconfig config CMD_RANDOM · line 980

Quoted 4 lines
cmd/Kconfig +4 -0
+config CMD_MEMSIZE
+       bool "memsize"
+       help
+         Get RAM via command for use in scripts.

I suggest a little more info in your help, so it is clear what the command actually does.

Ok something like "print ram size in MiB or set environment variable with same value in decimal notation for use in scripts."

Quoted 34 lines
Quoted 21 lines
cmd/Kconfig +1 -0
+
 config CMD_MEMTEST
        bool "memtest"
        help
cmd/mem.c +2 -0
index d5d7ca2790bd..a067740d81c8 100644
@@ -33,6 +33,7 @@
 #include <linux/compiler.h>
 #include <linux/ctype.h>
 #include <linux/delay.h>
+#include <linux/sizes.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -711,6 +712,29 @@ static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc,
 }
 #endif /* CONFIG_LOOPW */
 
+#ifdef CONFIG_CMD_MEMSIZE

cmd/mem.c static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc, · line 725

Quoted 10 lines
cmd/mem.c +10 -0
+static int do_mem_size(struct cmd_tbl *cmdtp, int flag, int argc,
+                      char *const argv[])
+{
+       u64 memsize = gd->ram_size;
+
+       if (argc > 1) {
+               if (!strcmp(argv[1], "m"))
+                       memsize = (memsize + SZ_1M - 1) / SZ_1M;
+               else if (!strcmp(argv[1], "g"))
+                       memsize = (memsize + SZ_1G - 1) / SZ_1G;

Can you do something like this?

Quoted 1 line

memsize = ALIGN(memsize, SZ_1G)

V6 only does MiB to make code more clear, avoid issues on round and also not needing floading point.

Quoted 5 lines

cmd/mem.c static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc, · line 727

Quoted 2 lines
cmd/mem.c +2 -0
+               if (argc > 2)
+                       env_set_ulong(argv[2], memsize);

Normally U-Boot uses hex, so it would be worth mentioning that this command doesn't. Or even provide a -d flag to select decimal?

I can mention that result is in decimal of course similar to help text above...seems i missed this in v6.

Quoted 26 lines
Quoted 26 lines
cmd/mem.c +18 -0
+               else
+                       printf("%lld%s\n", memsize, argv[1]);
+       } else {
+               printf("%lld\n", memsize);
+       }
+
+       return 0;
+}
+#endif /* CONFIG_CMD_MEMSIZE */
+
 #ifdef CONFIG_CMD_MEMTEST
 static ulong mem_test_alt(volatile ulong *buf, ulong start_addr, ulong end_addr,
                          volatile ulong *dummy)
@@ -1404,6 +1428,14 @@ U_BOOT_CMD(
 );
 #endif /* CONFIG_LOOPW */
 
+#ifdef CONFIG_CMD_MEMSIZE
+U_BOOT_CMD(
+       msize,  3,      1,      do_mem_size,
+       "get detected ram size, optional set env variable with value",
+       "[m, g] [envvar]"
+);
+#endif /* CONFIG_CMD_MEMSIZE */
+
 #ifdef CONFIG_CMD_MEMTEST
Quoted 8 lines
Quoted 5 lines
cmd/mem.c +0 -1
 U_BOOT_CMD(
        mtest,  5,      1,      do_mem_mtest,
--

2.43.0

Regards,
Simon

regards Frank