"Das U-Boot" Source Tree

cmd/setexpr: support concatenation of direct strings

The setexpr.s command allows to concatenate two strings.

According to the description in doc/usage/cmd/setexpr.rst the parameters
value1 and value2 can be either direct values or pointers to a
memory location holding the values.

Unfortunately `setexpr.s <value1> + <value2>` fails if any of the values
is a direct value. $? is set to false.

* Add support for direct values in setexpr.s.
* Correct the unit test for "setexpr.s fred 0".
* Add a new unit test for "setexpr.s fred '1' + '3'" giving '13'.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

authored by

Heinrich Schuchardt and committed by
Tom Rini
af7eca24 d4265cdc

+44 -17
+38 -16
cmd/setexpr.c
··· 35 35 }; 36 36 }; 37 37 38 + /** 39 + * arg_set_str() - copy string to expression argument 40 + * 41 + * The string is truncated to 64 KiB plus NUL terminator. 42 + * 43 + * @p: pointer to string 44 + * @argp: pointer to expression argument 45 + * Return: 0 on success, -ENOMEM if out of memory 46 + */ 47 + static int arg_set_str(void *p, struct expr_arg *argp) 48 + { 49 + int len; 50 + char *str; 51 + 52 + /* Maximum string length of 64 KiB plus NUL terminator */ 53 + len = strnlen((char *)p, SZ_64K) + 1; 54 + str = malloc(len); 55 + if (!str) { 56 + printf("Out of memory\n"); 57 + return -ENOMEM; 58 + } 59 + memcpy(str, p, len); 60 + str[len - 1] = '\0'; 61 + argp->sval = str; 62 + return 0; 63 + } 64 + 38 65 static int get_arg(char *s, int w, struct expr_arg *argp) 39 66 { 40 67 struct expr_arg arg; 68 + int ret; 41 69 42 70 /* 43 71 * If the parameter starts with a '*' then assume it is a pointer to ··· 47 75 ulong *p; 48 76 ulong addr; 49 77 ulong val; 50 - int len; 51 - char *str; 52 78 53 79 addr = hextoul(&s[1], NULL); 54 80 switch (w) { ··· 66 92 break; 67 93 case CMD_DATA_SIZE_STR: 68 94 p = map_sysmem(addr, SZ_64K); 69 - 70 - /* Maximum string length of 64KB plus terminator */ 71 - len = strnlen((char *)p, SZ_64K) + 1; 72 - str = malloc(len); 73 - if (!str) { 74 - printf("Out of memory\n"); 75 - return -ENOMEM; 76 - } 77 - memcpy(str, p, len); 78 - str[len - 1] = '\0'; 95 + ret = arg_set_str(p, &arg); 79 96 unmap_sysmem(p); 80 - arg.sval = str; 97 + if (ret) 98 + return ret; 81 99 break; 82 100 case 4: 83 101 p = map_sysmem(addr, sizeof(u32)); ··· 93 111 break; 94 112 } 95 113 } else { 96 - if (w == CMD_DATA_SIZE_STR) 97 - return -EINVAL; 98 - arg.ival = hextoul(s, NULL); 114 + if (w == CMD_DATA_SIZE_STR) { 115 + ret = arg_set_str(s, &arg); 116 + if (ret) 117 + return ret; 118 + } else { 119 + arg.ival = hextoul(s, NULL); 120 + } 99 121 } 100 122 *argp = arg; 101 123
+6 -1
test/cmd/setexpr.c
··· 303 303 memset(buf, '\xff', BUF_SIZE); 304 304 305 305 ut_assertok(env_set("fred", "x")); 306 - ut_asserteq(1, run_command("setexpr.s fred 0", 0)); 306 + ut_asserteq(0, run_command("setexpr.s fred 0", 0)); 307 + ut_asserteq_str("0", env_get("fred")); 307 308 308 309 strcpy(buf, "hello"); 309 310 ut_assertok(env_set("fred", "12345")); ··· 320 321 static int setexpr_test_str_oper(struct unit_test_state *uts) 321 322 { 322 323 char *buf; 324 + 325 + /* Test concatenation of strings */ 326 + ut_assertok(run_command("setexpr.s fred '1' + '3'", 0)); 327 + ut_asserteq_str("13", env_get("fred")); 323 328 324 329 buf = map_sysmem(0, BUF_SIZE); 325 330 memset(buf, '\xff', BUF_SIZE);