blob: e828be39700063b42666658a70b996b7399d6250 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Galad0586982008-02-14 20:44:42 -06002/*
3 * Copyright 2008 Freescale Semiconductor, Inc.
Wolfgang Denk855f18e2013-03-23 23:50:34 +00004 * Copyright 2013 Wolfgang Denk <wd@denx.de>
Kumar Galad0586982008-02-14 20:44:42 -06005 */
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 Glassc7694dd2019-08-01 09:46:46 -060014#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glass2c021522020-11-01 14:15:44 -070016#include <malloc.h>
Joe Hershberger2068cea2015-05-11 13:53:13 -050017#include <mapmem.h>
Simon Glass2c021522020-11-01 14:15:44 -070018#include <linux/sizes.h>
Kumar Galad0586982008-02-14 20:44:42 -060019
Simon Glassf66bee42020-11-01 14:15:43 -070020/**
21 * struct expr_arg: Holds an argument to an expression
22 *
23 * @ival: Integer value (if width is not CMD_DATA_SIZE_STR)
Simon Glass2c021522020-11-01 14:15:44 -070024 * @sval: String value (if width is CMD_DATA_SIZE_STR)
Simon Glassf66bee42020-11-01 14:15:43 -070025 */
26struct expr_arg {
Simon Glass2c021522020-11-01 14:15:44 -070027 union {
28 ulong ival;
29 char *sval;
30 };
Simon Glassf66bee42020-11-01 14:15:43 -070031};
32
33static int get_arg(char *s, int w, struct expr_arg *argp)
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010034{
Simon Glassf66bee42020-11-01 14:15:43 -070035 struct expr_arg arg;
36
Wolfgang Denk482126e2010-06-23 20:50:54 +020037 /*
Joe Hershberger2068cea2015-05-11 13:53:13 -050038 * If the parameter starts with a '*' then assume it is a pointer to
39 * the value we want.
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010040 */
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010041 if (s[0] == '*') {
Joe Hershberger2068cea2015-05-11 13:53:13 -050042 ulong *p;
43 ulong addr;
44 ulong val;
Simon Glass2c021522020-11-01 14:15:44 -070045 int len;
46 char *str;
Joe Hershberger2068cea2015-05-11 13:53:13 -050047
48 addr = simple_strtoul(&s[1], NULL, 16);
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010049 switch (w) {
Joe Hershberger2068cea2015-05-11 13:53:13 -050050 case 1:
51 p = map_sysmem(addr, sizeof(uchar));
52 val = (ulong)*(uchar *)p;
53 unmap_sysmem(p);
Simon Glassf66bee42020-11-01 14:15:43 -070054 arg.ival = val;
55 break;
Joe Hershberger2068cea2015-05-11 13:53:13 -050056 case 2:
57 p = map_sysmem(addr, sizeof(ushort));
58 val = (ulong)*(ushort *)p;
59 unmap_sysmem(p);
Simon Glassf66bee42020-11-01 14:15:43 -070060 arg.ival = val;
61 break;
Simon Glass2c021522020-11-01 14:15:44 -070062 case CMD_DATA_SIZE_STR:
63 p = map_sysmem(addr, SZ_64K);
64
65 /* Maximum string length of 64KB plus terminator */
66 len = strnlen((char *)p, SZ_64K) + 1;
67 str = malloc(len);
68 if (!str) {
69 printf("Out of memory\n");
70 return -ENOMEM;
71 }
72 memcpy(str, p, len);
73 str[len - 1] = '\0';
74 unmap_sysmem(p);
75 arg.sval = str;
76 break;
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010077 case 4:
Simon Glass25a43ac2020-11-01 14:15:37 -070078 p = map_sysmem(addr, sizeof(u32));
79 val = *(u32 *)p;
80 unmap_sysmem(p);
Simon Glassf66bee42020-11-01 14:15:43 -070081 arg.ival = val;
82 break;
Joe Hershberger2068cea2015-05-11 13:53:13 -050083 default:
84 p = map_sysmem(addr, sizeof(ulong));
85 val = *p;
86 unmap_sysmem(p);
Simon Glassf66bee42020-11-01 14:15:43 -070087 arg.ival = val;
88 break;
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010089 }
90 } else {
Simon Glass2c021522020-11-01 14:15:44 -070091 if (w == CMD_DATA_SIZE_STR)
92 return -EINVAL;
Simon Glassf66bee42020-11-01 14:15:43 -070093 arg.ival = simple_strtoul(s, NULL, 16);
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010094 }
Simon Glassf66bee42020-11-01 14:15:43 -070095 *argp = arg;
96
97 return 0;
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010098}
99
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000100#ifdef CONFIG_REGEX
101
102#include <slre.h>
103
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000104/*
105 * memstr - Find the first substring in memory
106 * @s1: The string to be searched
107 * @s2: The string to search for
108 *
109 * Similar to and based on strstr(),
110 * but strings do not need to be NUL terminated.
111 */
112static char *memstr(const char *s1, int l1, const char *s2, int l2)
113{
114 if (!l2)
115 return (char *)s1;
116
117 while (l1 >= l2) {
118 l1--;
119 if (!memcmp(s1, s2, l2))
120 return (char *)s1;
121 s1++;
122 }
123 return NULL;
124}
125
Simon Glass56331b22020-11-01 14:15:39 -0700126/**
127 * substitute() - Substitute part of one string with another
128 *
129 * This updates @string so that the first occurrence of @old is replaced with
130 * @new
131 *
132 * @string: String buffer containing string to update at the start
133 * @slen: Pointer to current string length, updated on success
134 * @ssize: Size of string buffer
135 * @old: Old string to find in the buffer (no terminator needed)
136 * @olen: Length of @old excluding terminator
137 * @new: New string to replace @old with
138 * @nlen: Length of @new excluding terminator
139 * @return pointer to immediately after the copied @new in @string, or NULL if
140 * no replacement took place
141 */
142static char *substitute(char *string, int *slen, int ssize,
143 const char *old, int olen, const char *new, int nlen)
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000144{
145 char *p = memstr(string, *slen, old, olen);
146
147 if (p == NULL)
148 return NULL;
149
150 debug("## Match at pos %ld: match len %d, subst len %d\n",
151 (long)(p - string), olen, nlen);
152
153 /* make sure replacement matches */
154 if (*slen + nlen - olen > ssize) {
155 printf("## error: substitution buffer overflow\n");
156 return NULL;
157 }
158
159 /* move tail if needed */
160 if (olen != nlen) {
161 int tail, len;
162
163 len = (olen > nlen) ? olen : nlen;
164
165 tail = ssize - (p + len - string);
166
167 debug("## tail len %d\n", tail);
168
169 memmove(p + nlen, p + olen, tail);
170 }
171
Simon Glass56331b22020-11-01 14:15:39 -0700172 /* insert substitute */
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000173 memcpy(p, new, nlen);
174
175 *slen += nlen - olen;
176
177 return p + nlen;
178}
179
Simon Glassd422c772020-11-01 14:15:40 -0700180int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
181 const char *r, const char *s, bool global)
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000182{
183 struct slre slre;
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000184 char *datap = data;
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000185 int res, len, nlen, loop;
186
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000187 if (slre_compile(&slre, r) == 0) {
188 printf("Error compiling regex: %s\n", slre.err_str);
189 return 1;
190 }
191
Simon Glass56331b22020-11-01 14:15:39 -0700192 len = strlen(data);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000193 for (loop = 0;; loop++) {
194 struct cap caps[slre.num_caps + 2];
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000195 const char *old;
196 char *np;
197 int i, olen;
198
199 (void) memset(caps, 0, sizeof(caps));
200
Simon Glass8f4aa7d2020-11-01 14:15:42 -0700201 res = slre_match(&slre, datap, len - (datap - data), caps);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000202
203 debug("Result: %d\n", res);
204
Simon Glass8f4aa7d2020-11-01 14:15:42 -0700205 for (i = 0; i <= slre.num_caps; i++) {
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000206 if (caps[i].len > 0) {
207 debug("Substring %d: [%.*s]\n", i,
208 caps[i].len, caps[i].ptr);
209 }
210 }
211
212 if (res == 0) {
213 if (loop == 0) {
Simon Glass56331b22020-11-01 14:15:39 -0700214 printf("%s: No match\n", data);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000215 return 1;
216 } else {
217 break;
218 }
219 }
220
221 debug("## MATCH ## %s\n", data);
222
Simon Glass56331b22020-11-01 14:15:39 -0700223 if (!s)
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000224 return 1;
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000225
226 old = caps[0].ptr;
227 olen = caps[0].len;
Simon Glass95282292020-11-01 14:15:41 -0700228 nlen = strlen(s);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000229
Simon Glass56331b22020-11-01 14:15:39 -0700230 if (nlen + 1 >= nbuf_size) {
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000231 printf("## error: pattern buffer overflow: have %d, need %d\n",
Simon Glass56331b22020-11-01 14:15:39 -0700232 nbuf_size, nlen + 1);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000233 return 1;
234 }
235 strcpy(nbuf, s);
236
237 debug("## SUBST(1) ## %s\n", nbuf);
238
239 /*
240 * Handle back references
241 *
242 * Support for \0 ... \9, where \0 is the
243 * whole matched pattern (similar to &).
244 *
245 * Implementation is a bit simpleminded as
246 * backrefs are substituted sequentially, one
247 * by one. This will lead to somewhat
248 * unexpected results if the replacement
249 * strings contain any \N strings then then
250 * may get substitued, too. We accept this
251 * restriction for the sake of simplicity.
252 */
253 for (i = 0; i < 10; ++i) {
254 char backref[2] = {
255 '\\',
256 '0',
257 };
258
259 if (caps[i].len == 0)
260 break;
261
262 backref[1] += i;
263
264 debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n",
265 i,
266 2, backref,
267 caps[i].len, caps[i].ptr,
268 nbuf);
269
270 for (np = nbuf;;) {
271 char *p = memstr(np, nlen, backref, 2);
272
273 if (p == NULL)
274 break;
275
276 np = substitute(np, &nlen,
Simon Glass8f4aa7d2020-11-01 14:15:42 -0700277 nbuf_size - (np - nbuf),
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000278 backref, 2,
279 caps[i].ptr, caps[i].len);
280
281 if (np == NULL)
282 return 1;
283 }
284 }
285 debug("## SUBST(2) ## %s\n", nbuf);
286
Simon Glass8f4aa7d2020-11-01 14:15:42 -0700287 datap = substitute(datap, &len, data_size - (datap - data),
288 old, olen, nbuf, nlen);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000289
290 if (datap == NULL)
291 return 1;
292
293 debug("## REMAINDER: %s\n", datap);
294
295 debug("## RESULT: %s\n", data);
296
297 if (!global)
298 break;
299 }
Simon Glass382bee52017-08-03 12:22:09 -0600300 debug("## FINAL (now env_set()) : %s\n", data);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000301
Simon Glass56331b22020-11-01 14:15:39 -0700302 return 0;
303}
304
305#define SLRE_BUFSZ 16384
306#define SLRE_PATSZ 4096
307
308/*
309 * Perform regex operations on a environment variable
310 *
311 * Returns 0 if OK, 1 in case of errors.
312 */
313static int regex_sub_var(const char *name, const char *r, const char *s,
314 const char *t, int global)
315{
316 struct slre slre;
317 char data[SLRE_BUFSZ];
318 char nbuf[SLRE_PATSZ];
319 const char *value;
320 int len;
321 int ret;
322
323 if (!name)
324 return 1;
325
326 if (slre_compile(&slre, r) == 0) {
327 printf("Error compiling regex: %s\n", slre.err_str);
328 return 1;
329 }
330
331 if (!t) {
332 value = env_get(name);
333 if (!value) {
334 printf("## Error: variable \"%s\" not defined\n", name);
335 return 1;
336 }
337 t = value;
338 }
339
340 debug("REGEX on %s=%s\n", name, t);
341 debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n", r, s ? s : "<NULL>",
342 global);
343
344 len = strlen(t);
345 if (len + 1 > SLRE_BUFSZ) {
346 printf("## error: subst buffer overflow: have %d, need %d\n",
347 SLRE_BUFSZ, len + 1);
348 return 1;
349 }
350
351 strcpy(data, t);
352
Simon Glassd422c772020-11-01 14:15:40 -0700353 ret = setexpr_regex_sub(data, SLRE_BUFSZ, nbuf, SLRE_PATSZ, r, s,
354 global);
Simon Glass56331b22020-11-01 14:15:39 -0700355 if (ret)
356 return 1;
357
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000358 printf("%s=%s\n", name, data);
359
Simon Glass382bee52017-08-03 12:22:09 -0600360 return env_set(name, data);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000361}
362#endif
363
Simon Glass09140112020-05-10 11:40:03 -0600364static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
365 char *const argv[])
Kumar Galad0586982008-02-14 20:44:42 -0600366{
Simon Glassf66bee42020-11-01 14:15:43 -0700367 struct expr_arg aval, bval;
Simon Glass41ef3722013-02-24 17:33:22 +0000368 ulong value;
Simon Glass2c021522020-11-01 14:15:44 -0700369 int ret = 0;
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +0100370 int w;
Kumar Galad0586982008-02-14 20:44:42 -0600371
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000372 /*
373 * We take 3, 5, or 6 arguments:
374 * 3 : setexpr name value
375 * 5 : setexpr name val1 op val2
376 * setexpr name [g]sub r s
377 * 6 : setexpr name [g]sub r s t
378 */
379
380 /* > 6 already tested by max command args */
381 if ((argc < 3) || (argc == 4))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000382 return CMD_RET_USAGE;
Kumar Galad0586982008-02-14 20:44:42 -0600383
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +0100384 w = cmd_get_data_size(argv[0], 4);
385
Simon Glassf66bee42020-11-01 14:15:43 -0700386 if (get_arg(argv[2], w, &aval))
387 return CMD_RET_FAILURE;
Joe Hershberger4823b452012-11-01 16:21:14 +0000388
Wolfgang Denk103c94b2013-03-23 23:50:33 +0000389 /* plain assignment: "setexpr name value" */
Simon Glass2c021522020-11-01 14:15:44 -0700390 if (argc == 3) {
391 if (w == CMD_DATA_SIZE_STR) {
392 ret = env_set(argv[1], aval.sval);
393 free(aval.sval);
394 } else {
395 ret = env_set_hex(argv[1], aval.ival);
396 }
397
398 return ret;
399 }
Joe Hershberger4823b452012-11-01 16:21:14 +0000400
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000401 /* 5 or 6 args (6 args only with [g]sub) */
402#ifdef CONFIG_REGEX
403 /*
404 * rexep handling: "setexpr name [g]sub r s [t]"
405 * with 5 args, "t" will be NULL
406 */
407 if (strcmp(argv[2], "gsub") == 0)
Simon Glass56331b22020-11-01 14:15:39 -0700408 return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 1);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000409
410 if (strcmp(argv[2], "sub") == 0)
Simon Glass56331b22020-11-01 14:15:39 -0700411 return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 0);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000412#endif
413
Wolfgang Denk103c94b2013-03-23 23:50:33 +0000414 /* standard operators: "setexpr name val1 op val2" */
415 if (argc != 5)
416 return CMD_RET_USAGE;
417
418 if (strlen(argv[3]) != 1)
419 return CMD_RET_USAGE;
420
Simon Glass2c021522020-11-01 14:15:44 -0700421 if (get_arg(argv[4], w, &bval)) {
422 if (w == CMD_DATA_SIZE_STR)
423 free(aval.sval);
Simon Glassf66bee42020-11-01 14:15:43 -0700424 return CMD_RET_FAILURE;
Simon Glass2c021522020-11-01 14:15:44 -0700425 }
Kumar Galad0586982008-02-14 20:44:42 -0600426
Simon Glass2c021522020-11-01 14:15:44 -0700427 if (w == CMD_DATA_SIZE_STR) {
428 int len;
429 char *str;
430
431 switch (argv[3][0]) {
432 case '+':
433 len = strlen(aval.sval) + strlen(bval.sval) + 1;
434 str = malloc(len);
435 if (!str) {
436 printf("Out of memory\n");
437 ret = CMD_RET_FAILURE;
438 } else {
439 /* These were copied out and checked earlier */
440 strcpy(str, aval.sval);
441 strcat(str, bval.sval);
442 ret = env_set(argv[1], str);
443 if (ret)
444 printf("Could not set var\n");
445 free(str);
446 }
447 break;
448 default:
449 printf("invalid op\n");
450 ret = 1;
451 }
452 } else {
Simon Glassf66bee42020-11-01 14:15:43 -0700453 ulong a = aval.ival;
454 ulong b = bval.ival;
455
456 switch (argv[3][0]) {
457 case '|':
458 value = a | b;
459 break;
460 case '&':
461 value = a & b;
462 break;
463 case '+':
464 value = a + b;
465 break;
466 case '^':
467 value = a ^ b;
468 break;
469 case '-':
470 value = a - b;
471 break;
472 case '*':
473 value = a * b;
474 break;
475 case '/':
476 value = a / b;
477 break;
478 case '%':
479 value = a % b;
480 break;
481 default:
482 printf("invalid op\n");
483 return 1;
484 }
485
486 env_set_hex(argv[1], value);
Kumar Galad0586982008-02-14 20:44:42 -0600487 }
488
Simon Glass2c021522020-11-01 14:15:44 -0700489 if (w == CMD_DATA_SIZE_STR) {
490 free(aval.sval);
491 free(bval.sval);
492 }
493
494 return ret;
Kumar Galad0586982008-02-14 20:44:42 -0600495}
496
497U_BOOT_CMD(
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000498 setexpr, 6, 0, do_setexpr,
Peter Tyser2fb26042009-01-27 18:03:12 -0600499 "set environment variable as the result of eval expression",
Simon Glass2c021522020-11-01 14:15:44 -0700500 "[.b, .w, .l, .s] name [*]value1 <op> [*]value2\n"
Kumar Galad0586982008-02-14 20:44:42 -0600501 " - set environment variable 'name' to the result of the evaluated\n"
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000502 " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
Simon Glass2c021522020-11-01 14:15:44 -0700503 " (for strings only + is supported)\n"
Joe Hershberger4823b452012-11-01 16:21:14 +0000504 " size argument is only meaningful if value1 and/or value2 are\n"
505 " memory addresses (*)\n"
Wolfgang Denk103c94b2013-03-23 23:50:33 +0000506 "setexpr[.b, .w, .l] name [*]value\n"
507 " - load a value into a variable"
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000508#ifdef CONFIG_REGEX
509 "\n"
510 "setexpr name gsub r s [t]\n"
511 " - For each substring matching the regular expression <r> in the\n"
512 " string <t>, substitute the string <s>. The result is\n"
513 " assigned to <name>. If <t> is not supplied, use the old\n"
514 " value of <name>\n"
515 "setexpr name sub r s [t]\n"
516 " - Just like gsub(), but replace only the first matching substring"
517#endif
Kumar Galad0586982008-02-14 20:44:42 -0600518);