3 * Tait Electronics Limited, Christchurch, New Zealand
5 * SPDX-License-Identifier: GPL-2.0+
9 * This file provides a shell like 'test' function to return
10 * true/false from an integer or string compare of two memory
11 * locations or a location and a scalar/literal.
12 * A few parts were lifted from bash 'test' command
30 char *op; /* operator string */
31 int opcode; /* internal representation of opcode */
34 typedef struct op_tbl_s op_tbl_t;
36 static const op_tbl_t op_table [] = {
52 static long evalexp(char *s, int w)
58 /* if the parameter starts with a * then assume is a pointer to the value we want */
60 addr = simple_strtoul(&s[1], NULL, 16);
61 buf = map_physmem(addr, w, MAP_WRBACK);
63 puts("Failed to map physical memory\n");
67 case 1: l = (long)(*(unsigned char *)buf);
68 case 2: l = (long)(*(unsigned short *)buf);
69 case 4: l = (long)(*(unsigned long *)buf);
71 unmap_physmem(buf, w);
74 l = simple_strtoul(s, NULL, 16);
77 return l & ((1UL << (w * 8)) - 1);
80 static char * evalstr(char *s)
82 /* if the parameter starts with a * then assume a string pointer else its a literal */
84 return (char *)simple_strtoul(&s[1], NULL, 16);
85 } else if (s[0] == '$') {
97 return getenv((const char *)&s[2]);
103 static int stringcomp(char *s, char *t, int op)
113 case EQ: return (p == 0);
114 case NE: return (p != 0);
115 case LT: return (p < 0);
116 case GT: return (p > 0);
117 case LE: return (p <= 0);
118 case GE: return (p >= 0);
123 static int arithcomp (char *s, char *t, int op, int w)
131 case EQ: return (l == r);
132 case NE: return (l != r);
133 case LT: return (l < r);
134 case GT: return (l > r);
135 case LE: return (l <= r);
136 case GE: return (l >= r);
141 static int binary_test(char *op, char *arg1, char *arg2, int w)
144 const op_tbl_t *optp;
148 for (optp = (op_tbl_t *)&op_table, i = 0;
149 i < ARRAY_SIZE(op_table);
152 if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
154 return (stringcomp(arg1, arg2, optp->opcode));
156 return (arithcomp (arg1, arg2, optp->opcode, w));
161 printf("Unknown operator '%s'\n", op);
162 return 0; /* op code not found */
165 /* command line interface to the shell test */
166 static int do_itest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
170 /* Validate arguments */
172 return CMD_RET_USAGE;
174 /* Check for a data width specification.
175 * Defaults to long (4) if no specification.
176 * Uses -2 as 'width' for .s (string) so as not to upset existing code
178 switch (w = cmd_get_data_size(argv[0], 4)) {
182 value = binary_test (argv[2], argv[1], argv[3], w);
185 value = binary_test (argv[2], argv[1], argv[3], 0);
189 puts("Invalid data width specifier\n");
198 itest, 4, 0, do_itest,
199 "return true/false on integer compare",
200 "[.b, .w, .l, .s] [*]value1 <op> [*]value2"