[PATCH 10/10] setexpr: Add support for strings

Simon Glass
2020-11-01T21:15:44Z

Add support for dealing with string operands, including reading a string
from memory into an environment variable and concatenating two strings.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

cmd/setexpr.c | 82 +++++++++++++++++++++++++++++++++++++++----
test/cmd/setexpr.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 161 insertions(+), 7 deletions(-)

diff --git a/cmd/setexpr.c b/cmd/setexpr.c
index 8a3654505da..e828be39700 100644
--- a/cmd/setexpr.c
+++ b/cmd/setexpr.c
@@ -13,15 +13,21 @@
#include <command.h>
#include <env.h>
#include <log.h>
+#include <malloc.h>
#include <mapmem.h>
+#include <linux/sizes.h>

/**
* struct expr_arg: Holds an argument to an expression
*
* @ival: Integer value (if width is not CMD_DATA_SIZE_STR)
+ * @sval: String value (if width is CMD_DATA_SIZE_STR)
*/
struct expr_arg {
- ulong ival;
+ union {
+ ulong ival;
+ char *sval;
+ };
};

static int get_arg(char *s, int w, struct expr_arg *argp)
@@ -36,6 +42,8 @@ static int get_arg(char *s, int w, struct expr_arg *argp)
ulong *p;
ulong addr;
ulong val;
+ int len;
+ char *str;

addr = simple_strtoul(&s[1], NULL, 16);
switch (w) {
@@ -51,6 +59,21 @@ static int get_arg(char *s, int w, struct expr_arg *argp)
unmap_sysmem(p);
arg.ival = val;
break;
+ case CMD_DATA_SIZE_STR:
+ p = map_sysmem(addr, SZ_64K);
+
+ /* Maximum string length of 64KB plus terminator */
+ len = strnlen((char *)p, SZ_64K) + 1;
+ str = malloc(len);
+ if (!str) {
+ printf("Out of memory\n");
+ return -ENOMEM;
+ }
+ memcpy(str, p, len);
+ str[len - 1] = '\0';
+ unmap_sysmem(p);
+ arg.sval = str;
+ break;
case 4:
p = map_sysmem(addr, sizeof(u32));
val = *(u32 *)p;
@@ -65,6 +88,8 @@ static int get_arg(char *s, int w, struct expr_arg *argp)
break;
}
} else {
+ if (w == CMD_DATA_SIZE_STR)
+ return -EINVAL;
arg.ival = simple_strtoul(s, NULL, 16);
}
*argp = arg;
@@ -341,6 +366,7 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
{
struct expr_arg aval, bval;
ulong value;
+ int ret = 0;
int w;

/*
@@ -361,8 +387,16 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
return CMD_RET_FAILURE;

/* plain assignment: "setexpr name value" */
- if (argc == 3)
- return env_set_hex(argv[1], aval.ival);
+ if (argc == 3) {
+ if (w == CMD_DATA_SIZE_STR) {
+ ret = env_set(argv[1], aval.sval);
+ free(aval.sval);
+ } else {
+ ret = env_set_hex(argv[1], aval.ival);
+ }
+
+ return ret;
+ }

/* 5 or 6 args (6 args only with [g]sub) */
#ifdef CONFIG_REGEX
@@ -384,10 +418,38 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
if (strlen(argv[3]) != 1)
return CMD_RET_USAGE;

- if (get_arg(argv[4], w, &bval))
+ if (get_arg(argv[4], w, &bval)) {
+ if (w == CMD_DATA_SIZE_STR)
+ free(aval.sval);
return CMD_RET_FAILURE;
+ }
+
+ if (w == CMD_DATA_SIZE_STR) {
+ int len;
+ char *str;

- if (w != CMD_DATA_SIZE_STR) {
+ switch (argv[3][0]) {
+ case '+':
+ len = strlen(aval.sval) + strlen(bval.sval) + 1;
+ str = malloc(len);
+ if (!str) {
+ printf("Out of memory\n");
+ ret = CMD_RET_FAILURE;
+ } else {
+ /* These were copied out and checked earlier */
+ strcpy(str, aval.sval);
+ strcat(str, bval.sval);
+ ret = env_set(argv[1], str);
+ if (ret)
+ printf("Could not set var\n");
+ free(str);
+ }
+ break;
+ default:
+ printf("invalid op\n");
+ ret = 1;
+ }
+ } else {
ulong a = aval.ival;
ulong b = bval.ival;

@@ -424,15 +486,21 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
env_set_hex(argv[1], value);
}

- return 0;
+ if (w == CMD_DATA_SIZE_STR) {
+ free(aval.sval);
+ free(bval.sval);
+ }
+
+ return ret;
}

U_BOOT_CMD(
setexpr, 6, 0, do_setexpr,
"set environment variable as the result of eval expression",
- "[.b, .w, .l] name [*]value1 <op> [*]value2\n"
+ "[.b, .w, .l, .s] name [*]value1 <op> [*]value2\n"
" - set environment variable 'name' to the result of the evaluated\n"
" expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
+ " (for strings only + is supported)\n"
" size argument is only meaningful if value1 and/or value2 are\n"
" memory addresses (*)\n"
"setexpr[.b, .w, .l] name [*]value\n"
diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c
index 2a897efd9bd..fd6d869c0ed 100644
--- a/test/cmd/setexpr.c
+++ b/test/cmd/setexpr.c
@@ -287,6 +287,92 @@ static int setexpr_test_backref(struct unit_test_state *uts)
}
SETEXPR_TEST(setexpr_test_backref, UT_TESTF_CONSOLE_REC);

+/* Test 'setexpr' command with setting strings */
+static int setexpr_test_str(struct unit_test_state *uts)
+{
+ ulong start_mem;
+ char *buf;
+
+ buf = map_sysmem(0, BUF_SIZE);
+ memset(buf, '\xff', BUF_SIZE);
+
+ /*
+ * Set 'fred' to the same length as we expect to get below, to avoid a
+ * new allocation in 'setexpr'. That way we can check for memory leaks.
+ */
+ ut_assertok(env_set("fred", "x"));
+ start_mem = ut_check_free();
+ strcpy(buf, "hello");
+ ut_asserteq(1, run_command("setexpr.s fred 0", 0));
+ ut_assertok(ut_check_delta(start_mem));
+
+ start_mem = ut_check_free();
+ ut_assertok(env_set("fred", "12345"));
+ ut_assertok(run_command("setexpr.s fred *0", 0));
+ ut_asserteq_str("hello", env_get("fred"));
+ ut_assertok(ut_check_delta(start_mem));
+
+ unmap_sysmem(buf);
+
+ return 0;
+}
+SETEXPR_TEST(setexpr_test_str, UT_TESTF_CONSOLE_REC);
+
+
+/* Test 'setexpr' command with concatenating strings */
+static int setexpr_test_str_oper(struct unit_test_state *uts)
+{
+ ulong start_mem;
+ char *buf;
+
+ buf = map_sysmem(0, BUF_SIZE);
+ memset(buf, '\xff', BUF_SIZE);
+ strcpy(buf, "hello");
+ strcpy(buf + 0x10, " there");
+
+ ut_assertok(console_record_reset_enable());
+ start_mem = ut_check_free();
+ ut_asserteq(1, run_command("setexpr.s fred *0 * *10", 0));
+ ut_assertok(ut_check_delta(start_mem));
+ ut_assert_nextline("invalid op");
+ ut_assert_console_end();
+
+ /*
+ * Set 'fred' to the same length as we expect to get below, to avoid a
+ * new allocation in 'setexpr'. That way we can check for memory leaks.
+ */
+ ut_assertok(env_set("fred", "12345012345"));
+ start_mem = ut_check_free();
+ ut_assertok(run_command("setexpr.s fred *0 + *10", 0));
+ ut_asserteq_str("hello there", env_get("fred"));
+ ut_assertok(ut_check_delta(start_mem));
+
+ unmap_sysmem(buf);
+
+ return 0;
+}
+SETEXPR_TEST(setexpr_test_str_oper, UT_TESTF_CONSOLE_REC);
+
+/* Test 'setexpr' command with a string that is too long */
+static int setexpr_test_str_long(struct unit_test_state *uts)
+{
+ const int size = 128 << 10; /* setexpr strings are a max of 64KB */
+ char *buf, *val;
+
+ buf = map_sysmem(0, size);
+ memset(buf, 'a', size);
+
+ /* String should be truncated to 64KB */
+ ut_assertok(run_command("setexpr.s fred *0", 0));
+ val = env_get("fred");
+ ut_asserteq(64 << 10, strlen(val));
+
+ unmap_sysmem(buf);
+
+ return 0;
+}
+SETEXPR_TEST(setexpr_test_str_long, UT_TESTF_CONSOLE_REC);
+
int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
{
struct unit_test *tests = ll_entry_start(struct unit_test,
--
2.29.1.341.ge80a0c044ae-goog