blob: 770dc24d2b7a32579a05c8aed36b40bc234252b6 [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>
Joe Hershberger2068cea2015-05-11 13:53:13 -050016#include <mapmem.h>
Kumar Galad0586982008-02-14 20:44:42 -060017
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010018static ulong get_arg(char *s, int w)
19{
Wolfgang Denk482126e2010-06-23 20:50:54 +020020 /*
Joe Hershberger2068cea2015-05-11 13:53:13 -050021 * If the parameter starts with a '*' then assume it is a pointer to
22 * the value we want.
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010023 */
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010024 if (s[0] == '*') {
Joe Hershberger2068cea2015-05-11 13:53:13 -050025 ulong *p;
26 ulong addr;
27 ulong val;
28
29 addr = simple_strtoul(&s[1], NULL, 16);
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010030 switch (w) {
Joe Hershberger2068cea2015-05-11 13:53:13 -050031 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 Meulenbroeks47ab5ad2010-02-26 14:00:19 +010041 case 4:
Joe Hershberger2068cea2015-05-11 13:53:13 -050042 default:
43 p = map_sysmem(addr, sizeof(ulong));
44 val = *p;
45 unmap_sysmem(p);
46 return val;
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +010047 }
48 } else {
49 return simple_strtoul(s, NULL, 16);
50 }
51}
52
Wolfgang Denk855f18e2013-03-23 23:50:34 +000053#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 */
68static 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
82static 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 */
130static 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 Glass00caae62017-08-03 12:22:12 -0600149 value = env_get(name);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000150
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 Glass382bee52017-08-03 12:22:09 -0600286 debug("## FINAL (now env_set()) : %s\n", data);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000287
288 printf("%s=%s\n", name, data);
289
Simon Glass382bee52017-08-03 12:22:09 -0600290 return env_set(name, data);
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000291}
292#endif
293
Simon Glass09140112020-05-10 11:40:03 -0600294static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
295 char *const argv[])
Kumar Galad0586982008-02-14 20:44:42 -0600296{
297 ulong a, b;
Simon Glass41ef3722013-02-24 17:33:22 +0000298 ulong value;
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +0100299 int w;
Kumar Galad0586982008-02-14 20:44:42 -0600300
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000301 /*
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 Glass4c12eeb2011-12-10 08:44:01 +0000311 return CMD_RET_USAGE;
Kumar Galad0586982008-02-14 20:44:42 -0600312
Frans Meulenbroeks47ab5ad2010-02-26 14:00:19 +0100313 w = cmd_get_data_size(argv[0], 4);
314
315 a = get_arg(argv[2], w);
Joe Hershberger4823b452012-11-01 16:21:14 +0000316
Wolfgang Denk103c94b2013-03-23 23:50:33 +0000317 /* plain assignment: "setexpr name value" */
Joe Hershberger4823b452012-11-01 16:21:14 +0000318 if (argc == 3) {
Simon Glass018f5302017-08-03 12:22:10 -0600319 env_set_hex(argv[1], a);
Joe Hershberger4823b452012-11-01 16:21:14 +0000320 return 0;
321 }
322
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000323 /* 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 Denk103c94b2013-03-23 23:50:33 +0000336 /* 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 Meulenbroeks47ab5ad2010-02-26 14:00:19 +0100343 b = get_arg(argv[4], w);
Kumar Galad0586982008-02-14 20:44:42 -0600344
345 switch (argv[3][0]) {
Simon Glass41ef3722013-02-24 17:33:22 +0000346 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 Galad0586982008-02-14 20:44:42 -0600370 default:
371 printf("invalid op\n");
372 return 1;
373 }
374
Simon Glass018f5302017-08-03 12:22:10 -0600375 env_set_hex(argv[1], value);
Kumar Galad0586982008-02-14 20:44:42 -0600376
377 return 0;
378}
379
380U_BOOT_CMD(
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000381 setexpr, 6, 0, do_setexpr,
Peter Tyser2fb26042009-01-27 18:03:12 -0600382 "set environment variable as the result of eval expression",
Joe Hershberger4823b452012-11-01 16:21:14 +0000383 "[.b, .w, .l] name [*]value1 <op> [*]value2\n"
Kumar Galad0586982008-02-14 20:44:42 -0600384 " - set environment variable 'name' to the result of the evaluated\n"
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000385 " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
Joe Hershberger4823b452012-11-01 16:21:14 +0000386 " size argument is only meaningful if value1 and/or value2 are\n"
387 " memory addresses (*)\n"
Wolfgang Denk103c94b2013-03-23 23:50:33 +0000388 "setexpr[.b, .w, .l] name [*]value\n"
389 " - load a value into a variable"
Wolfgang Denk855f18e2013-03-23 23:50:34 +0000390#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 Galad0586982008-02-14 20:44:42 -0600400);