Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | d058698 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2008 Freescale Semiconductor, Inc. |
Wolfgang Denk | 855f18e | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 4 | * Copyright 2013 Wolfgang Denk <wd@denx.de> |
Kumar Gala | d058698 | 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 | |
| 11 | #include <common.h> |
| 12 | #include <config.h> |
| 13 | #include <command.h> |
Simon Glass | c7694dd | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 14 | #include <env.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Joe Hershberger | 2068cea | 2015-05-11 13:53:13 -0500 | [diff] [blame] | 16 | #include <mapmem.h> |
Kumar Gala | d058698 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 17 | |
Frans Meulenbroeks | 47ab5ad | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 18 | static ulong get_arg(char *s, int w) |
| 19 | { |
Wolfgang Denk | 482126e | 2010-06-23 20:50:54 +0200 | [diff] [blame] | 20 | /* |
Joe Hershberger | 2068cea | 2015-05-11 13:53:13 -0500 | [diff] [blame] | 21 | * If the parameter starts with a '*' then assume it is a pointer to |
| 22 | * the value we want. |
Frans Meulenbroeks | 47ab5ad | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 23 | */ |
Frans Meulenbroeks | 47ab5ad | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 24 | if (s[0] == '*') { |
Joe Hershberger | 2068cea | 2015-05-11 13:53:13 -0500 | [diff] [blame] | 25 | ulong *p; |
| 26 | ulong addr; |
| 27 | ulong val; |
| 28 | |
| 29 | addr = simple_strtoul(&s[1], NULL, 16); |
Frans Meulenbroeks | 47ab5ad | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 30 | switch (w) { |
Joe Hershberger | 2068cea | 2015-05-11 13:53:13 -0500 | [diff] [blame] | 31 | case 1: |
| 32 | p = map_sysmem(addr, sizeof(uchar)); |
| 33 | val = (ulong)*(uchar *)p; |
| 34 | unmap_sysmem(p); |
| 35 | return val; |
| 36 | case 2: |
| 37 | p = map_sysmem(addr, sizeof(ushort)); |
| 38 | val = (ulong)*(ushort *)p; |
| 39 | unmap_sysmem(p); |
| 40 | return val; |
Frans Meulenbroeks | 47ab5ad | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 41 | case 4: |
Joe Hershberger | 2068cea | 2015-05-11 13:53:13 -0500 | [diff] [blame] | 42 | default: |
| 43 | p = map_sysmem(addr, sizeof(ulong)); |
| 44 | val = *p; |
| 45 | unmap_sysmem(p); |
| 46 | return val; |
Frans Meulenbroeks | 47ab5ad | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 47 | } |
| 48 | } else { |
| 49 | return simple_strtoul(s, NULL, 16); |
| 50 | } |
| 51 | } |
| 52 | |
Wolfgang Denk | 855f18e | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 53 | #ifdef CONFIG_REGEX |
| 54 | |
| 55 | #include <slre.h> |
| 56 | |
| 57 | #define SLRE_BUFSZ 16384 |
| 58 | #define SLRE_PATSZ 4096 |
| 59 | |
| 60 | /* |
| 61 | * memstr - Find the first substring in memory |
| 62 | * @s1: The string to be searched |
| 63 | * @s2: The string to search for |
| 64 | * |
| 65 | * Similar to and based on strstr(), |
| 66 | * but strings do not need to be NUL terminated. |
| 67 | */ |
| 68 | static char *memstr(const char *s1, int l1, const char *s2, int l2) |
| 69 | { |
| 70 | if (!l2) |
| 71 | return (char *)s1; |
| 72 | |
| 73 | while (l1 >= l2) { |
| 74 | l1--; |
| 75 | if (!memcmp(s1, s2, l2)) |
| 76 | return (char *)s1; |
| 77 | s1++; |
| 78 | } |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | static char *substitute(char *string, /* string buffer */ |
| 83 | int *slen, /* current string length */ |
| 84 | int ssize, /* string bufer size */ |
| 85 | const char *old,/* old (replaced) string */ |
| 86 | int olen, /* length of old string */ |
| 87 | const char *new,/* new (replacement) string */ |
| 88 | int nlen) /* length of new string */ |
| 89 | { |
| 90 | char *p = memstr(string, *slen, old, olen); |
| 91 | |
| 92 | if (p == NULL) |
| 93 | return NULL; |
| 94 | |
| 95 | debug("## Match at pos %ld: match len %d, subst len %d\n", |
| 96 | (long)(p - string), olen, nlen); |
| 97 | |
| 98 | /* make sure replacement matches */ |
| 99 | if (*slen + nlen - olen > ssize) { |
| 100 | printf("## error: substitution buffer overflow\n"); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | /* move tail if needed */ |
| 105 | if (olen != nlen) { |
| 106 | int tail, len; |
| 107 | |
| 108 | len = (olen > nlen) ? olen : nlen; |
| 109 | |
| 110 | tail = ssize - (p + len - string); |
| 111 | |
| 112 | debug("## tail len %d\n", tail); |
| 113 | |
| 114 | memmove(p + nlen, p + olen, tail); |
| 115 | } |
| 116 | |
| 117 | /* insert substitue */ |
| 118 | memcpy(p, new, nlen); |
| 119 | |
| 120 | *slen += nlen - olen; |
| 121 | |
| 122 | return p + nlen; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Perform regex operations on a environment variable |
| 127 | * |
| 128 | * Returns 0 if OK, 1 in case of errors. |
| 129 | */ |
| 130 | static int regex_sub(const char *name, |
| 131 | const char *r, const char *s, const char *t, |
| 132 | int global) |
| 133 | { |
| 134 | struct slre slre; |
| 135 | char data[SLRE_BUFSZ]; |
| 136 | char *datap = data; |
| 137 | const char *value; |
| 138 | int res, len, nlen, loop; |
| 139 | |
| 140 | if (name == NULL) |
| 141 | return 1; |
| 142 | |
| 143 | if (slre_compile(&slre, r) == 0) { |
| 144 | printf("Error compiling regex: %s\n", slre.err_str); |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | if (t == NULL) { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 149 | value = env_get(name); |
Wolfgang Denk | 855f18e | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 150 | |
| 151 | if (value == NULL) { |
| 152 | printf("## Error: variable \"%s\" not defined\n", name); |
| 153 | return 1; |
| 154 | } |
| 155 | t = value; |
| 156 | } |
| 157 | |
| 158 | debug("REGEX on %s=%s\n", name, t); |
| 159 | debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n", |
| 160 | r, s ? s : "<NULL>", global); |
| 161 | |
| 162 | len = strlen(t); |
| 163 | if (len + 1 > SLRE_BUFSZ) { |
| 164 | printf("## error: subst buffer overflow: have %d, need %d\n", |
| 165 | SLRE_BUFSZ, len + 1); |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | strcpy(data, t); |
| 170 | |
| 171 | if (s == NULL) |
| 172 | nlen = 0; |
| 173 | else |
| 174 | nlen = strlen(s); |
| 175 | |
| 176 | for (loop = 0;; loop++) { |
| 177 | struct cap caps[slre.num_caps + 2]; |
| 178 | char nbuf[SLRE_PATSZ]; |
| 179 | const char *old; |
| 180 | char *np; |
| 181 | int i, olen; |
| 182 | |
| 183 | (void) memset(caps, 0, sizeof(caps)); |
| 184 | |
| 185 | res = slre_match(&slre, datap, len, caps); |
| 186 | |
| 187 | debug("Result: %d\n", res); |
| 188 | |
| 189 | for (i = 0; i < slre.num_caps; i++) { |
| 190 | if (caps[i].len > 0) { |
| 191 | debug("Substring %d: [%.*s]\n", i, |
| 192 | caps[i].len, caps[i].ptr); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (res == 0) { |
| 197 | if (loop == 0) { |
| 198 | printf("%s: No match\n", t); |
| 199 | return 1; |
| 200 | } else { |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | debug("## MATCH ## %s\n", data); |
| 206 | |
| 207 | if (s == NULL) { |
| 208 | printf("%s=%s\n", name, t); |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | old = caps[0].ptr; |
| 213 | olen = caps[0].len; |
| 214 | |
| 215 | if (nlen + 1 >= SLRE_PATSZ) { |
| 216 | printf("## error: pattern buffer overflow: have %d, need %d\n", |
| 217 | SLRE_BUFSZ, nlen + 1); |
| 218 | return 1; |
| 219 | } |
| 220 | strcpy(nbuf, s); |
| 221 | |
| 222 | debug("## SUBST(1) ## %s\n", nbuf); |
| 223 | |
| 224 | /* |
| 225 | * Handle back references |
| 226 | * |
| 227 | * Support for \0 ... \9, where \0 is the |
| 228 | * whole matched pattern (similar to &). |
| 229 | * |
| 230 | * Implementation is a bit simpleminded as |
| 231 | * backrefs are substituted sequentially, one |
| 232 | * by one. This will lead to somewhat |
| 233 | * unexpected results if the replacement |
| 234 | * strings contain any \N strings then then |
| 235 | * may get substitued, too. We accept this |
| 236 | * restriction for the sake of simplicity. |
| 237 | */ |
| 238 | for (i = 0; i < 10; ++i) { |
| 239 | char backref[2] = { |
| 240 | '\\', |
| 241 | '0', |
| 242 | }; |
| 243 | |
| 244 | if (caps[i].len == 0) |
| 245 | break; |
| 246 | |
| 247 | backref[1] += i; |
| 248 | |
| 249 | debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n", |
| 250 | i, |
| 251 | 2, backref, |
| 252 | caps[i].len, caps[i].ptr, |
| 253 | nbuf); |
| 254 | |
| 255 | for (np = nbuf;;) { |
| 256 | char *p = memstr(np, nlen, backref, 2); |
| 257 | |
| 258 | if (p == NULL) |
| 259 | break; |
| 260 | |
| 261 | np = substitute(np, &nlen, |
| 262 | SLRE_PATSZ, |
| 263 | backref, 2, |
| 264 | caps[i].ptr, caps[i].len); |
| 265 | |
| 266 | if (np == NULL) |
| 267 | return 1; |
| 268 | } |
| 269 | } |
| 270 | debug("## SUBST(2) ## %s\n", nbuf); |
| 271 | |
| 272 | datap = substitute(datap, &len, SLRE_BUFSZ, |
| 273 | old, olen, |
| 274 | nbuf, nlen); |
| 275 | |
| 276 | if (datap == NULL) |
| 277 | return 1; |
| 278 | |
| 279 | debug("## REMAINDER: %s\n", datap); |
| 280 | |
| 281 | debug("## RESULT: %s\n", data); |
| 282 | |
| 283 | if (!global) |
| 284 | break; |
| 285 | } |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 286 | debug("## FINAL (now env_set()) : %s\n", data); |
Wolfgang Denk | 855f18e | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 287 | |
| 288 | printf("%s=%s\n", name, data); |
| 289 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 290 | return env_set(name, data); |
Wolfgang Denk | 855f18e | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 291 | } |
| 292 | #endif |
| 293 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 294 | static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, |
| 295 | char *const argv[]) |
Kumar Gala | d058698 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 296 | { |
| 297 | ulong a, b; |
Simon Glass | 41ef372 | 2013-02-24 17:33:22 +0000 | [diff] [blame] | 298 | ulong value; |
Frans Meulenbroeks | 47ab5ad | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 299 | int w; |
Kumar Gala | d058698 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 300 | |
Wolfgang Denk | 855f18e | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 301 | /* |
| 302 | * We take 3, 5, or 6 arguments: |
| 303 | * 3 : setexpr name value |
| 304 | * 5 : setexpr name val1 op val2 |
| 305 | * setexpr name [g]sub r s |
| 306 | * 6 : setexpr name [g]sub r s t |
| 307 | */ |
| 308 | |
| 309 | /* > 6 already tested by max command args */ |
| 310 | if ((argc < 3) || (argc == 4)) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 311 | return CMD_RET_USAGE; |
Kumar Gala | d058698 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 312 | |
Frans Meulenbroeks | 47ab5ad | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 313 | w = cmd_get_data_size(argv[0], 4); |
| 314 | |
| 315 | a = get_arg(argv[2], w); |
Joe Hershberger | 4823b45 | 2012-11-01 16:21:14 +0000 | [diff] [blame] | 316 | |
Wolfgang Denk | 103c94b | 2013-03-23 23:50:33 +0000 | [diff] [blame] | 317 | /* plain assignment: "setexpr name value" */ |
Joe Hershberger | 4823b45 | 2012-11-01 16:21:14 +0000 | [diff] [blame] | 318 | if (argc == 3) { |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 319 | env_set_hex(argv[1], a); |
Joe Hershberger | 4823b45 | 2012-11-01 16:21:14 +0000 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
Wolfgang Denk | 855f18e | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 323 | /* 5 or 6 args (6 args only with [g]sub) */ |
| 324 | #ifdef CONFIG_REGEX |
| 325 | /* |
| 326 | * rexep handling: "setexpr name [g]sub r s [t]" |
| 327 | * with 5 args, "t" will be NULL |
| 328 | */ |
| 329 | if (strcmp(argv[2], "gsub") == 0) |
| 330 | return regex_sub(argv[1], argv[3], argv[4], argv[5], 1); |
| 331 | |
| 332 | if (strcmp(argv[2], "sub") == 0) |
| 333 | return regex_sub(argv[1], argv[3], argv[4], argv[5], 0); |
| 334 | #endif |
| 335 | |
Wolfgang Denk | 103c94b | 2013-03-23 23:50:33 +0000 | [diff] [blame] | 336 | /* standard operators: "setexpr name val1 op val2" */ |
| 337 | if (argc != 5) |
| 338 | return CMD_RET_USAGE; |
| 339 | |
| 340 | if (strlen(argv[3]) != 1) |
| 341 | return CMD_RET_USAGE; |
| 342 | |
Frans Meulenbroeks | 47ab5ad | 2010-02-26 14:00:19 +0100 | [diff] [blame] | 343 | b = get_arg(argv[4], w); |
Kumar Gala | d058698 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 344 | |
| 345 | switch (argv[3][0]) { |
Simon Glass | 41ef372 | 2013-02-24 17:33:22 +0000 | [diff] [blame] | 346 | case '|': |
| 347 | value = a | b; |
| 348 | break; |
| 349 | case '&': |
| 350 | value = a & b; |
| 351 | break; |
| 352 | case '+': |
| 353 | value = a + b; |
| 354 | break; |
| 355 | case '^': |
| 356 | value = a ^ b; |
| 357 | break; |
| 358 | case '-': |
| 359 | value = a - b; |
| 360 | break; |
| 361 | case '*': |
| 362 | value = a * b; |
| 363 | break; |
| 364 | case '/': |
| 365 | value = a / b; |
| 366 | break; |
| 367 | case '%': |
| 368 | value = a % b; |
| 369 | break; |
Kumar Gala | d058698 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 370 | default: |
| 371 | printf("invalid op\n"); |
| 372 | return 1; |
| 373 | } |
| 374 | |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 375 | env_set_hex(argv[1], value); |
Kumar Gala | d058698 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | U_BOOT_CMD( |
Wolfgang Denk | 855f18e | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 381 | setexpr, 6, 0, do_setexpr, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 382 | "set environment variable as the result of eval expression", |
Joe Hershberger | 4823b45 | 2012-11-01 16:21:14 +0000 | [diff] [blame] | 383 | "[.b, .w, .l] name [*]value1 <op> [*]value2\n" |
Kumar Gala | d058698 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 384 | " - set environment variable 'name' to the result of the evaluated\n" |
Wolfgang Denk | 855f18e | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 385 | " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n" |
Joe Hershberger | 4823b45 | 2012-11-01 16:21:14 +0000 | [diff] [blame] | 386 | " size argument is only meaningful if value1 and/or value2 are\n" |
| 387 | " memory addresses (*)\n" |
Wolfgang Denk | 103c94b | 2013-03-23 23:50:33 +0000 | [diff] [blame] | 388 | "setexpr[.b, .w, .l] name [*]value\n" |
| 389 | " - load a value into a variable" |
Wolfgang Denk | 855f18e | 2013-03-23 23:50:34 +0000 | [diff] [blame] | 390 | #ifdef CONFIG_REGEX |
| 391 | "\n" |
| 392 | "setexpr name gsub r s [t]\n" |
| 393 | " - For each substring matching the regular expression <r> in the\n" |
| 394 | " string <t>, substitute the string <s>. The result is\n" |
| 395 | " assigned to <name>. If <t> is not supplied, use the old\n" |
| 396 | " value of <name>\n" |
| 397 | "setexpr name sub r s [t]\n" |
| 398 | " - Just like gsub(), but replace only the first matching substring" |
| 399 | #endif |
Kumar Gala | d058698 | 2008-02-14 20:44:42 -0600 | [diff] [blame] | 400 | ); |