Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2008 Freescale Semiconductor, Inc. |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 4 | * Copyright 2013 Wolfgang Denk <wd@denx.de> |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This file provides a shell like 'expr' function to return. |
| 9 | */ |
| 10 | |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 11 | #include <config.h> |
| 12 | #include <command.h> |
Roland Gaudig | 2c9e7c2 | 2021-07-23 12:29:21 +0000 | [diff] [blame] | 13 | #include <ctype.h> |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 14 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 16 | #include <malloc.h> |
Joe Hershberger | 9d3f896 | 2015-05-11 13:53:13 -0500 | [diff] [blame] | 17 | #include <mapmem.h> |
Tom Rini | 5c1444f | 2024-04-27 08:10:59 -0600 | [diff] [blame] | 18 | #include <vsprintf.h> |
| 19 | #include <linux/errno.h> |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 20 | #include <linux/sizes.h> |
Roland Gaudig | 2c9e7c2 | 2021-07-23 12:29:21 +0000 | [diff] [blame] | 21 | #include "printf.h" |
| 22 | |
| 23 | #define MAX_STR_LEN 128 |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 24 | |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 25 | /** |
| 26 | * struct expr_arg: Holds an argument to an expression |
| 27 | * |
| 28 | * @ival: Integer value (if width is not CMD_DATA_SIZE_STR) |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 29 | * @sval: String value (if width is CMD_DATA_SIZE_STR) |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 30 | */ |
| 31 | struct expr_arg { |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 32 | union { |
| 33 | ulong ival; |
| 34 | char *sval; |
| 35 | }; |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static int get_arg(char *s, int w, struct expr_arg *argp) |
Frans Meulenbroeks | 2347d4e | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 39 | { |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 40 | struct expr_arg arg; |
| 41 | |
Wolfgang Denk | 9a183d2 | 2010-06-23 20:50:54 +0200 | [diff] [blame] | 42 | /* |
Joe Hershberger | 9d3f896 | 2015-05-11 13:53:13 -0500 | [diff] [blame] | 43 | * If the parameter starts with a '*' then assume it is a pointer to |
| 44 | * the value we want. |
Frans Meulenbroeks | 2347d4e | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 45 | */ |
Frans Meulenbroeks | 2347d4e | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 46 | if (s[0] == '*') { |
Joe Hershberger | 9d3f896 | 2015-05-11 13:53:13 -0500 | [diff] [blame] | 47 | ulong *p; |
| 48 | ulong addr; |
| 49 | ulong val; |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 50 | int len; |
| 51 | char *str; |
Joe Hershberger | 9d3f896 | 2015-05-11 13:53:13 -0500 | [diff] [blame] | 52 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 53 | addr = hextoul(&s[1], NULL); |
Frans Meulenbroeks | 2347d4e | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 54 | switch (w) { |
Joe Hershberger | 9d3f896 | 2015-05-11 13:53:13 -0500 | [diff] [blame] | 55 | case 1: |
| 56 | p = map_sysmem(addr, sizeof(uchar)); |
| 57 | val = (ulong)*(uchar *)p; |
| 58 | unmap_sysmem(p); |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 59 | arg.ival = val; |
| 60 | break; |
Joe Hershberger | 9d3f896 | 2015-05-11 13:53:13 -0500 | [diff] [blame] | 61 | case 2: |
| 62 | p = map_sysmem(addr, sizeof(ushort)); |
| 63 | val = (ulong)*(ushort *)p; |
| 64 | unmap_sysmem(p); |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 65 | arg.ival = val; |
| 66 | break; |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 67 | case CMD_DATA_SIZE_STR: |
| 68 | 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'; |
| 79 | unmap_sysmem(p); |
| 80 | arg.sval = str; |
| 81 | break; |
Frans Meulenbroeks | 2347d4e | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 82 | case 4: |
Simon Glass | 7c19edf | 2020-11-01 14:15:37 -0700 | [diff] [blame] | 83 | p = map_sysmem(addr, sizeof(u32)); |
| 84 | val = *(u32 *)p; |
| 85 | unmap_sysmem(p); |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 86 | arg.ival = val; |
| 87 | break; |
Joe Hershberger | 9d3f896 | 2015-05-11 13:53:13 -0500 | [diff] [blame] | 88 | default: |
| 89 | p = map_sysmem(addr, sizeof(ulong)); |
| 90 | val = *p; |
| 91 | unmap_sysmem(p); |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 92 | arg.ival = val; |
| 93 | break; |
Frans Meulenbroeks | 2347d4e | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 94 | } |
| 95 | } else { |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 96 | if (w == CMD_DATA_SIZE_STR) |
| 97 | return -EINVAL; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 98 | arg.ival = hextoul(s, NULL); |
Frans Meulenbroeks | 2347d4e | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 99 | } |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 100 | *argp = arg; |
| 101 | |
| 102 | return 0; |
Frans Meulenbroeks | 2347d4e | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 105 | #ifdef CONFIG_REGEX |
| 106 | |
| 107 | #include <slre.h> |
| 108 | |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 109 | /* |
| 110 | * memstr - Find the first substring in memory |
| 111 | * @s1: The string to be searched |
| 112 | * @s2: The string to search for |
| 113 | * |
| 114 | * Similar to and based on strstr(), |
| 115 | * but strings do not need to be NUL terminated. |
| 116 | */ |
| 117 | static char *memstr(const char *s1, int l1, const char *s2, int l2) |
| 118 | { |
| 119 | if (!l2) |
| 120 | return (char *)s1; |
| 121 | |
| 122 | while (l1 >= l2) { |
| 123 | l1--; |
| 124 | if (!memcmp(s1, s2, l2)) |
| 125 | return (char *)s1; |
| 126 | s1++; |
| 127 | } |
| 128 | return NULL; |
| 129 | } |
| 130 | |
Simon Glass | b8e9f2d | 2020-11-01 14:15:39 -0700 | [diff] [blame] | 131 | /** |
| 132 | * substitute() - Substitute part of one string with another |
| 133 | * |
| 134 | * This updates @string so that the first occurrence of @old is replaced with |
| 135 | * @new |
| 136 | * |
| 137 | * @string: String buffer containing string to update at the start |
| 138 | * @slen: Pointer to current string length, updated on success |
| 139 | * @ssize: Size of string buffer |
| 140 | * @old: Old string to find in the buffer (no terminator needed) |
| 141 | * @olen: Length of @old excluding terminator |
| 142 | * @new: New string to replace @old with |
| 143 | * @nlen: Length of @new excluding terminator |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 144 | * Return: pointer to immediately after the copied @new in @string, or NULL if |
Simon Glass | b8e9f2d | 2020-11-01 14:15:39 -0700 | [diff] [blame] | 145 | * no replacement took place |
| 146 | */ |
| 147 | static char *substitute(char *string, int *slen, int ssize, |
| 148 | const char *old, int olen, const char *new, int nlen) |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 149 | { |
| 150 | char *p = memstr(string, *slen, old, olen); |
| 151 | |
| 152 | if (p == NULL) |
| 153 | return NULL; |
| 154 | |
| 155 | debug("## Match at pos %ld: match len %d, subst len %d\n", |
| 156 | (long)(p - string), olen, nlen); |
| 157 | |
| 158 | /* make sure replacement matches */ |
| 159 | if (*slen + nlen - olen > ssize) { |
| 160 | printf("## error: substitution buffer overflow\n"); |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | /* move tail if needed */ |
| 165 | if (olen != nlen) { |
| 166 | int tail, len; |
| 167 | |
| 168 | len = (olen > nlen) ? olen : nlen; |
| 169 | |
| 170 | tail = ssize - (p + len - string); |
| 171 | |
| 172 | debug("## tail len %d\n", tail); |
| 173 | |
| 174 | memmove(p + nlen, p + olen, tail); |
| 175 | } |
| 176 | |
Simon Glass | b8e9f2d | 2020-11-01 14:15:39 -0700 | [diff] [blame] | 177 | /* insert substitute */ |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 178 | memcpy(p, new, nlen); |
| 179 | |
| 180 | *slen += nlen - olen; |
| 181 | |
| 182 | return p + nlen; |
| 183 | } |
| 184 | |
Simon Glass | 00e4419 | 2020-11-01 14:15:40 -0700 | [diff] [blame] | 185 | int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size, |
| 186 | const char *r, const char *s, bool global) |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 187 | { |
| 188 | struct slre slre; |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 189 | char *datap = data; |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 190 | int res, len, nlen, loop; |
| 191 | |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 192 | if (slre_compile(&slre, r) == 0) { |
| 193 | printf("Error compiling regex: %s\n", slre.err_str); |
| 194 | return 1; |
| 195 | } |
| 196 | |
Simon Glass | b8e9f2d | 2020-11-01 14:15:39 -0700 | [diff] [blame] | 197 | len = strlen(data); |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 198 | for (loop = 0;; loop++) { |
| 199 | struct cap caps[slre.num_caps + 2]; |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 200 | const char *old; |
| 201 | char *np; |
| 202 | int i, olen; |
| 203 | |
| 204 | (void) memset(caps, 0, sizeof(caps)); |
| 205 | |
Simon Glass | 7a11486 | 2020-11-01 14:15:42 -0700 | [diff] [blame] | 206 | res = slre_match(&slre, datap, len - (datap - data), caps); |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 207 | |
| 208 | debug("Result: %d\n", res); |
| 209 | |
Simon Glass | 7a11486 | 2020-11-01 14:15:42 -0700 | [diff] [blame] | 210 | for (i = 0; i <= slre.num_caps; i++) { |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 211 | if (caps[i].len > 0) { |
| 212 | debug("Substring %d: [%.*s]\n", i, |
| 213 | caps[i].len, caps[i].ptr); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (res == 0) { |
| 218 | if (loop == 0) { |
Łukasz Stelmach | 848d73c | 2023-08-24 08:10:25 +0200 | [diff] [blame] | 219 | debug("%s: No match\n", data); |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 220 | } else { |
Massimiliano Minella | 847f69c | 2024-02-08 15:58:27 +0100 | [diff] [blame] | 221 | debug("## MATCH ## %s\n", data); |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 222 | } |
Massimiliano Minella | 847f69c | 2024-02-08 15:58:27 +0100 | [diff] [blame] | 223 | break; |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Simon Glass | b8e9f2d | 2020-11-01 14:15:39 -0700 | [diff] [blame] | 226 | if (!s) |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 227 | return 1; |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 228 | |
| 229 | old = caps[0].ptr; |
| 230 | olen = caps[0].len; |
Simon Glass | 28ae25a | 2020-11-01 14:15:41 -0700 | [diff] [blame] | 231 | nlen = strlen(s); |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 232 | |
Simon Glass | b8e9f2d | 2020-11-01 14:15:39 -0700 | [diff] [blame] | 233 | if (nlen + 1 >= nbuf_size) { |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 234 | printf("## error: pattern buffer overflow: have %d, need %d\n", |
Simon Glass | b8e9f2d | 2020-11-01 14:15:39 -0700 | [diff] [blame] | 235 | nbuf_size, nlen + 1); |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 236 | return 1; |
| 237 | } |
| 238 | strcpy(nbuf, s); |
| 239 | |
| 240 | debug("## SUBST(1) ## %s\n", nbuf); |
| 241 | |
| 242 | /* |
| 243 | * Handle back references |
| 244 | * |
| 245 | * Support for \0 ... \9, where \0 is the |
| 246 | * whole matched pattern (similar to &). |
| 247 | * |
| 248 | * Implementation is a bit simpleminded as |
| 249 | * backrefs are substituted sequentially, one |
| 250 | * by one. This will lead to somewhat |
| 251 | * unexpected results if the replacement |
| 252 | * strings contain any \N strings then then |
| 253 | * may get substitued, too. We accept this |
| 254 | * restriction for the sake of simplicity. |
| 255 | */ |
| 256 | for (i = 0; i < 10; ++i) { |
| 257 | char backref[2] = { |
| 258 | '\\', |
| 259 | '0', |
| 260 | }; |
| 261 | |
| 262 | if (caps[i].len == 0) |
| 263 | break; |
| 264 | |
| 265 | backref[1] += i; |
| 266 | |
| 267 | debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n", |
| 268 | i, |
| 269 | 2, backref, |
| 270 | caps[i].len, caps[i].ptr, |
| 271 | nbuf); |
| 272 | |
| 273 | for (np = nbuf;;) { |
| 274 | char *p = memstr(np, nlen, backref, 2); |
| 275 | |
| 276 | if (p == NULL) |
| 277 | break; |
| 278 | |
| 279 | np = substitute(np, &nlen, |
Simon Glass | 7a11486 | 2020-11-01 14:15:42 -0700 | [diff] [blame] | 280 | nbuf_size - (np - nbuf), |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 281 | backref, 2, |
| 282 | caps[i].ptr, caps[i].len); |
| 283 | |
| 284 | if (np == NULL) |
| 285 | return 1; |
| 286 | } |
| 287 | } |
| 288 | debug("## SUBST(2) ## %s\n", nbuf); |
| 289 | |
Simon Glass | 7a11486 | 2020-11-01 14:15:42 -0700 | [diff] [blame] | 290 | datap = substitute(datap, &len, data_size - (datap - data), |
| 291 | old, olen, nbuf, nlen); |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 292 | |
| 293 | if (datap == NULL) |
| 294 | return 1; |
| 295 | |
| 296 | debug("## REMAINDER: %s\n", datap); |
| 297 | |
| 298 | debug("## RESULT: %s\n", data); |
| 299 | |
| 300 | if (!global) |
| 301 | break; |
| 302 | } |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 303 | debug("## FINAL (now env_set()) : %s\n", data); |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 304 | |
Simon Glass | b8e9f2d | 2020-11-01 14:15:39 -0700 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | #define SLRE_BUFSZ 16384 |
| 309 | #define SLRE_PATSZ 4096 |
| 310 | |
| 311 | /* |
| 312 | * Perform regex operations on a environment variable |
| 313 | * |
| 314 | * Returns 0 if OK, 1 in case of errors. |
| 315 | */ |
| 316 | static int regex_sub_var(const char *name, const char *r, const char *s, |
| 317 | const char *t, int global) |
| 318 | { |
| 319 | struct slre slre; |
| 320 | char data[SLRE_BUFSZ]; |
| 321 | char nbuf[SLRE_PATSZ]; |
| 322 | const char *value; |
| 323 | int len; |
| 324 | int ret; |
| 325 | |
| 326 | if (!name) |
| 327 | return 1; |
| 328 | |
| 329 | if (slre_compile(&slre, r) == 0) { |
| 330 | printf("Error compiling regex: %s\n", slre.err_str); |
| 331 | return 1; |
| 332 | } |
| 333 | |
| 334 | if (!t) { |
| 335 | value = env_get(name); |
| 336 | if (!value) { |
| 337 | printf("## Error: variable \"%s\" not defined\n", name); |
| 338 | return 1; |
| 339 | } |
| 340 | t = value; |
| 341 | } |
| 342 | |
| 343 | debug("REGEX on %s=%s\n", name, t); |
| 344 | debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n", r, s ? s : "<NULL>", |
| 345 | global); |
| 346 | |
| 347 | len = strlen(t); |
| 348 | if (len + 1 > SLRE_BUFSZ) { |
| 349 | printf("## error: subst buffer overflow: have %d, need %d\n", |
| 350 | SLRE_BUFSZ, len + 1); |
| 351 | return 1; |
| 352 | } |
| 353 | |
| 354 | strcpy(data, t); |
| 355 | |
Simon Glass | 00e4419 | 2020-11-01 14:15:40 -0700 | [diff] [blame] | 356 | ret = setexpr_regex_sub(data, SLRE_BUFSZ, nbuf, SLRE_PATSZ, r, s, |
| 357 | global); |
Simon Glass | b8e9f2d | 2020-11-01 14:15:39 -0700 | [diff] [blame] | 358 | if (ret) |
| 359 | return 1; |
| 360 | |
Łukasz Stelmach | 848d73c | 2023-08-24 08:10:25 +0200 | [diff] [blame] | 361 | debug("%s=%s\n", name, data); |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 362 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 363 | return env_set(name, data); |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 364 | } |
| 365 | #endif |
| 366 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 367 | static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, |
| 368 | char *const argv[]) |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 369 | { |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 370 | struct expr_arg aval, bval; |
Simon Glass | 103789d | 2013-02-24 17:33:22 +0000 | [diff] [blame] | 371 | ulong value; |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 372 | int ret = 0; |
Frans Meulenbroeks | 2347d4e | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 373 | int w; |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 374 | |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 375 | /* |
Roland Gaudig | 2c9e7c2 | 2021-07-23 12:29:21 +0000 | [diff] [blame] | 376 | * We take 3, 5, or 6 arguments, except fmt operation, which |
| 377 | * takes 4 to 8 arguments (limited by _maxargs): |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 378 | * 3 : setexpr name value |
| 379 | * 5 : setexpr name val1 op val2 |
| 380 | * setexpr name [g]sub r s |
| 381 | * 6 : setexpr name [g]sub r s t |
Roland Gaudig | 2c9e7c2 | 2021-07-23 12:29:21 +0000 | [diff] [blame] | 382 | * setexpr name fmt format [val1] [val2] [val3] [val4] |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 383 | */ |
| 384 | |
Roland Gaudig | 2c9e7c2 | 2021-07-23 12:29:21 +0000 | [diff] [blame] | 385 | if (argc < 3) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 386 | return CMD_RET_USAGE; |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 387 | |
Frans Meulenbroeks | 2347d4e | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 388 | w = cmd_get_data_size(argv[0], 4); |
| 389 | |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 390 | if (get_arg(argv[2], w, &aval)) |
| 391 | return CMD_RET_FAILURE; |
Joe Hershberger | 0e640e6 | 2012-11-01 16:21:14 +0000 | [diff] [blame] | 392 | |
Roland Gaudig | 2c9e7c2 | 2021-07-23 12:29:21 +0000 | [diff] [blame] | 393 | /* format string assignment: "setexpr name fmt %d value" */ |
| 394 | if (strcmp(argv[2], "fmt") == 0 && IS_ENABLED(CONFIG_CMD_SETEXPR_FMT)) { |
| 395 | char str[MAX_STR_LEN]; |
| 396 | int result; |
| 397 | |
| 398 | if (argc == 3) |
| 399 | return CMD_RET_USAGE; |
| 400 | |
| 401 | result = printf_setexpr(str, sizeof(str), argc - 3, &argv[3]); |
| 402 | if (result) |
| 403 | return result; |
| 404 | |
| 405 | return env_set(argv[1], str); |
| 406 | } |
| 407 | |
| 408 | if (argc == 4 || argc > 6) |
| 409 | return CMD_RET_USAGE; |
| 410 | |
Wolfgang Denk | b2234d6 | 2013-03-23 23:50:33 +0000 | [diff] [blame] | 411 | /* plain assignment: "setexpr name value" */ |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 412 | if (argc == 3) { |
| 413 | if (w == CMD_DATA_SIZE_STR) { |
| 414 | ret = env_set(argv[1], aval.sval); |
| 415 | free(aval.sval); |
| 416 | } else { |
| 417 | ret = env_set_hex(argv[1], aval.ival); |
| 418 | } |
| 419 | |
| 420 | return ret; |
| 421 | } |
Joe Hershberger | 0e640e6 | 2012-11-01 16:21:14 +0000 | [diff] [blame] | 422 | |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 423 | /* 5 or 6 args (6 args only with [g]sub) */ |
| 424 | #ifdef CONFIG_REGEX |
| 425 | /* |
| 426 | * rexep handling: "setexpr name [g]sub r s [t]" |
| 427 | * with 5 args, "t" will be NULL |
| 428 | */ |
| 429 | if (strcmp(argv[2], "gsub") == 0) |
Simon Glass | b8e9f2d | 2020-11-01 14:15:39 -0700 | [diff] [blame] | 430 | return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 1); |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 431 | |
| 432 | if (strcmp(argv[2], "sub") == 0) |
Simon Glass | b8e9f2d | 2020-11-01 14:15:39 -0700 | [diff] [blame] | 433 | return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 0); |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 434 | #endif |
| 435 | |
Wolfgang Denk | b2234d6 | 2013-03-23 23:50:33 +0000 | [diff] [blame] | 436 | /* standard operators: "setexpr name val1 op val2" */ |
| 437 | if (argc != 5) |
| 438 | return CMD_RET_USAGE; |
| 439 | |
| 440 | if (strlen(argv[3]) != 1) |
| 441 | return CMD_RET_USAGE; |
| 442 | |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 443 | if (get_arg(argv[4], w, &bval)) { |
| 444 | if (w == CMD_DATA_SIZE_STR) |
| 445 | free(aval.sval); |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 446 | return CMD_RET_FAILURE; |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 447 | } |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 448 | |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 449 | if (w == CMD_DATA_SIZE_STR) { |
| 450 | int len; |
| 451 | char *str; |
| 452 | |
| 453 | switch (argv[3][0]) { |
| 454 | case '+': |
| 455 | len = strlen(aval.sval) + strlen(bval.sval) + 1; |
| 456 | str = malloc(len); |
| 457 | if (!str) { |
| 458 | printf("Out of memory\n"); |
| 459 | ret = CMD_RET_FAILURE; |
| 460 | } else { |
| 461 | /* These were copied out and checked earlier */ |
| 462 | strcpy(str, aval.sval); |
| 463 | strcat(str, bval.sval); |
| 464 | ret = env_set(argv[1], str); |
| 465 | if (ret) |
| 466 | printf("Could not set var\n"); |
| 467 | free(str); |
| 468 | } |
| 469 | break; |
| 470 | default: |
| 471 | printf("invalid op\n"); |
| 472 | ret = 1; |
| 473 | } |
| 474 | } else { |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 475 | ulong a = aval.ival; |
| 476 | ulong b = bval.ival; |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 477 | |
Simon Glass | 00547af | 2020-11-01 14:15:43 -0700 | [diff] [blame] | 478 | switch (argv[3][0]) { |
| 479 | case '|': |
| 480 | value = a | b; |
| 481 | break; |
| 482 | case '&': |
| 483 | value = a & b; |
| 484 | break; |
| 485 | case '+': |
| 486 | value = a + b; |
| 487 | break; |
| 488 | case '^': |
| 489 | value = a ^ b; |
| 490 | break; |
| 491 | case '-': |
| 492 | value = a - b; |
| 493 | break; |
| 494 | case '*': |
| 495 | value = a * b; |
| 496 | break; |
| 497 | case '/': |
| 498 | value = a / b; |
| 499 | break; |
| 500 | case '%': |
| 501 | value = a % b; |
| 502 | break; |
| 503 | default: |
| 504 | printf("invalid op\n"); |
| 505 | return 1; |
| 506 | } |
| 507 | |
| 508 | env_set_hex(argv[1], value); |
| 509 | } |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 510 | |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 511 | if (w == CMD_DATA_SIZE_STR) { |
| 512 | free(aval.sval); |
| 513 | free(bval.sval); |
| 514 | } |
| 515 | |
| 516 | return ret; |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | U_BOOT_CMD( |
Roland Gaudig | 2c9e7c2 | 2021-07-23 12:29:21 +0000 | [diff] [blame] | 520 | setexpr, 8, 0, do_setexpr, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 521 | "set environment variable as the result of eval expression", |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 522 | "[.b, .w, .l, .s] name [*]value1 <op> [*]value2\n" |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 523 | " - set environment variable 'name' to the result of the evaluated\n" |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 524 | " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n" |
Simon Glass | 580a7a5 | 2020-11-01 14:15:44 -0700 | [diff] [blame] | 525 | " (for strings only + is supported)\n" |
Joe Hershberger | 0e640e6 | 2012-11-01 16:21:14 +0000 | [diff] [blame] | 526 | " size argument is only meaningful if value1 and/or value2 are\n" |
| 527 | " memory addresses (*)\n" |
Wolfgang Denk | b2234d6 | 2013-03-23 23:50:33 +0000 | [diff] [blame] | 528 | "setexpr[.b, .w, .l] name [*]value\n" |
| 529 | " - load a value into a variable" |
Roland Gaudig | 2c9e7c2 | 2021-07-23 12:29:21 +0000 | [diff] [blame] | 530 | #ifdef CONFIG_CMD_SETEXPR_FMT |
| 531 | "\n" |
| 532 | "setexpr name fmt <format> [value1] [value2] [value3] [value4]\n" |
| 533 | " - set environment variable 'name' to the result of the bash like\n" |
| 534 | " format string evaluation of value." |
| 535 | #endif |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 536 | #ifdef CONFIG_REGEX |
| 537 | "\n" |
| 538 | "setexpr name gsub r s [t]\n" |
| 539 | " - For each substring matching the regular expression <r> in the\n" |
| 540 | " string <t>, substitute the string <s>. The result is\n" |
| 541 | " assigned to <name>. If <t> is not supplied, use the old\n" |
Massimiliano Minella | 847f69c | 2024-02-08 15:58:27 +0100 | [diff] [blame] | 542 | " value of <name>. If no substring matching <r> is found in <t>,\n" |
| 543 | " assign <t> to <name>.\n" |
Wolfgang Denk | b04ae78 | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 544 | "setexpr name sub r s [t]\n" |
| 545 | " - Just like gsub(), but replace only the first matching substring" |
| 546 | #endif |
Kumar Gala | aed8334 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 547 | ); |