diff --git a/cmd/Kconfig b/cmd/Kconfig index cc4dd5a3163..322ebe600c5 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -940,6 +940,14 @@ config CMD_MEMINFO_MAP See doc/usage/cmd/meminfo.rst for more information. +config CMD_MEMSIZE + bool "memsize" + default y if SANDBOX + 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 diff --git a/cmd/meminfo.c b/cmd/meminfo.c index b731280d106..69a5b1b51a2 100644 --- a/cmd/meminfo.c +++ b/cmd/meminfo.c @@ -8,10 +8,12 @@ #include #include #include +#include #include #include #include #include +#include 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 */ diff --git a/doc/usage/cmd/memsize.rst b/doc/usage/cmd/memsize.rst new file mode 100644 index 00000000000..6e99d7c1d33 --- /dev/null +++ b/doc/usage/cmd/memsize.rst @@ -0,0 +1,43 @@ +.. SPDX-License-Identifier: GPL-2.0+: + +.. index:: + single: memsize (command) + +memsize command +=============== + +Synopsis +-------- + +:: + + memsize [name] + +Description +----------- + +The memsize command shows the amount of RAM in MiB in decimal notation. +Optionally same value can be assigned to an environment variable. + +Examples +-------- + +This first example shows printing of ram size: + +:: + + => memsize + 8192 MiB + +This second example shows assign ram size to environment variable: + +:: + + => memsize memsz + => printenv memsz + memsz=8192 + +Return value +------------ + +The return value is always 0 except error happens on setting environment variable. diff --git a/test/cmd/meminfo.c b/test/cmd/meminfo.c index d91fa6fcead..c9aae33f7c1 100644 --- a/test/cmd/meminfo.c +++ b/test/cmd/meminfo.c @@ -7,6 +7,7 @@ */ #include +#include #include #include @@ -49,4 +50,23 @@ static int cmd_test_meminfo(struct unit_test_state *uts) return 0; } + CMD_TEST(cmd_test_meminfo, UTF_CONSOLE); + +/* Test 'memsize' command */ +#ifdef CONFIG_CMD_MEMSIZE +static int cmd_test_memsize(struct unit_test_state *uts) +{ + ut_assertok(run_command("memsize", 0)); + ut_assert_nextline("256 MiB"); + ut_assert_console_end(); + + ut_assertok(run_command("memsize memsz", 0)); + ut_asserteq_str("256", env_get("memsz")); + ut_assert_console_end(); + + return 0; +} + +CMD_TEST(cmd_test_memsize, UTF_CONSOLE); +#endif