blob: 8b630d71e62dad67c5297182736a994a7f1ae0d5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk2ebee312004-02-23 19:30:57 +00002/*
3 * (C) Copyright 2003
4 * Tait Electronics Limited, Christchurch, New Zealand
wdenk2ebee312004-02-23 19:30:57 +00005 */
6
7/*
8 * This file provides a shell like 'test' function to return
9 * true/false from an integer or string compare of two memory
10 * locations or a location and a scalar/literal.
11 * A few parts were lifted from bash 'test' command
12 */
13
14#include <common.h>
15#include <config.h>
16#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060017#include <env.h>
Stephen Warren304ce792015-10-03 13:56:46 -060018#include <mapmem.h>
19
20#include <asm/io.h>
wdenk2ebee312004-02-23 19:30:57 +000021
22#define EQ 0
23#define NE 1
24#define LT 2
25#define GT 3
26#define LE 4
27#define GE 5
28
29struct op_tbl_s {
30 char *op; /* operator string */
31 int opcode; /* internal representation of opcode */
32};
33
34typedef struct op_tbl_s op_tbl_t;
35
Mike Frysinger17a266a2010-10-20 07:17:33 -040036static const op_tbl_t op_table [] = {
wdenk2ebee312004-02-23 19:30:57 +000037 { "-lt", LT },
38 { "<" , LT },
39 { "-gt", GT },
40 { ">" , GT },
41 { "-eq", EQ },
42 { "==" , EQ },
43 { "-ne", NE },
44 { "!=" , NE },
45 { "<>" , NE },
46 { "-ge", GE },
47 { ">=" , GE },
48 { "-le", LE },
49 { "<=" , LE },
50};
51
wdenk2ebee312004-02-23 19:30:57 +000052static long evalexp(char *s, int w)
53{
Frans Meulenbroekse67b02f2010-02-22 22:49:06 +010054 long l = 0;
Stephen Warren304ce792015-10-03 13:56:46 -060055 unsigned long addr;
56 void *buf;
wdenk2ebee312004-02-23 19:30:57 +000057
58 /* if the parameter starts with a * then assume is a pointer to the value we want */
59 if (s[0] == '*') {
Stephen Warren304ce792015-10-03 13:56:46 -060060 addr = simple_strtoul(&s[1], NULL, 16);
61 buf = map_physmem(addr, w, MAP_WRBACK);
Stephen Warren35d0a952016-01-29 16:10:04 -070062 if (!buf && addr) {
Stephen Warren304ce792015-10-03 13:56:46 -060063 puts("Failed to map physical memory\n");
64 return 0;
65 }
Frans Meulenbroekse67b02f2010-02-22 22:49:06 +010066 switch (w) {
Stephen Warrenaa632ab2015-11-17 10:29:08 -070067 case 1:
Kunihiko Hayashi7e7c8772016-05-04 14:20:04 +090068 l = (long)(*(u8 *)buf);
Stephen Warrenaa632ab2015-11-17 10:29:08 -070069 break;
70 case 2:
Kunihiko Hayashi7e7c8772016-05-04 14:20:04 +090071 l = (long)(*(u16 *)buf);
Stephen Warrenaa632ab2015-11-17 10:29:08 -070072 break;
73 case 4:
Kunihiko Hayashi7e7c8772016-05-04 14:20:04 +090074 l = (long)(*(u32 *)buf);
Stephen Warrenaa632ab2015-11-17 10:29:08 -070075 break;
Frans Meulenbroekse67b02f2010-02-22 22:49:06 +010076 }
Stephen Warren304ce792015-10-03 13:56:46 -060077 unmap_physmem(buf, w);
78 return l;
wdenk2ebee312004-02-23 19:30:57 +000079 } else {
80 l = simple_strtoul(s, NULL, 16);
81 }
82
Sebastien Colleure3b71f82017-02-10 15:59:15 +030083 /* avoid overflow on mask calculus */
84 return (w >= sizeof(long)) ? l : (l & ((1UL << (w * 8)) - 1));
wdenk2ebee312004-02-23 19:30:57 +000085}
86
87static char * evalstr(char *s)
88{
89 /* if the parameter starts with a * then assume a string pointer else its a literal */
90 if (s[0] == '*') {
91 return (char *)simple_strtoul(&s[1], NULL, 16);
Heiko Schochere82717d2014-01-25 07:27:12 +010092 } else if (s[0] == '$') {
93 int i = 2;
94
95 if (s[1] != '{')
96 return NULL;
97
98 while (s[i] != '}') {
99 if (s[i] == 0)
100 return NULL;
101 i++;
102 }
103 s[i] = 0;
Simon Glass64b723f2017-08-03 12:22:12 -0600104 return env_get((const char *)&s[2]);
wdenk2ebee312004-02-23 19:30:57 +0000105 } else {
106 return s;
107 }
108}
109
110static int stringcomp(char *s, char *t, int op)
111{
Wolfgang Denkdc4d0a22011-02-08 16:56:05 +0100112 int p;
wdenk2ebee312004-02-23 19:30:57 +0000113 char *l, *r;
114
115 l = evalstr(s);
116 r = evalstr(t);
117
Wolfgang Denkdc4d0a22011-02-08 16:56:05 +0100118 p = strcmp(l, r);
wdenk2ebee312004-02-23 19:30:57 +0000119 switch (op) {
120 case EQ: return (p == 0);
121 case NE: return (p != 0);
122 case LT: return (p < 0);
123 case GT: return (p > 0);
124 case LE: return (p <= 0);
125 case GE: return (p >= 0);
126 }
127 return (0);
128}
129
130static int arithcomp (char *s, char *t, int op, int w)
131{
132 long l, r;
133
134 l = evalexp (s, w);
135 r = evalexp (t, w);
136
137 switch (op) {
138 case EQ: return (l == r);
139 case NE: return (l != r);
140 case LT: return (l < r);
141 case GT: return (l > r);
142 case LE: return (l <= r);
143 case GE: return (l >= r);
144 }
145 return (0);
146}
147
Kim Phillipsdc00a682012-10-29 13:34:31 +0000148static int binary_test(char *op, char *arg1, char *arg2, int w)
wdenk2ebee312004-02-23 19:30:57 +0000149{
150 int len, i;
Mike Frysinger17a266a2010-10-20 07:17:33 -0400151 const op_tbl_t *optp;
wdenk2ebee312004-02-23 19:30:57 +0000152
153 len = strlen(op);
154
155 for (optp = (op_tbl_t *)&op_table, i = 0;
Mike Frysinger17a266a2010-10-20 07:17:33 -0400156 i < ARRAY_SIZE(op_table);
wdenk2ebee312004-02-23 19:30:57 +0000157 optp++, i++) {
158
159 if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
160 if (w == 0) {
161 return (stringcomp(arg1, arg2, optp->opcode));
162 } else {
163 return (arithcomp (arg1, arg2, optp->opcode, w));
164 }
165 }
166 }
167
168 printf("Unknown operator '%s'\n", op);
169 return 0; /* op code not found */
170}
171
172/* command line interface to the shell test */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000173static int do_itest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk2ebee312004-02-23 19:30:57 +0000174{
wdenkf70cbb22004-02-23 20:48:38 +0000175 int value, w;
wdenk2ebee312004-02-23 19:30:57 +0000176
wdenkf70cbb22004-02-23 20:48:38 +0000177 /* Validate arguments */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200178 if ((argc != 4))
Simon Glassa06dfc72011-12-10 08:44:01 +0000179 return CMD_RET_USAGE;
wdenk2ebee312004-02-23 19:30:57 +0000180
181 /* Check for a data width specification.
182 * Defaults to long (4) if no specification.
183 * Uses -2 as 'width' for .s (string) so as not to upset existing code
184 */
185 switch (w = cmd_get_data_size(argv[0], 4)) {
186 case 1:
187 case 2:
188 case 4:
189 value = binary_test (argv[2], argv[1], argv[3], w);
190 break;
191 case -2:
192 value = binary_test (argv[2], argv[1], argv[3], 0);
193 break;
194 case -1:
195 default:
196 puts("Invalid data width specifier\n");
197 value = 0;
198 break;
199 }
200
201 return !value;
202}
203
204U_BOOT_CMD(
205 itest, 4, 0, do_itest,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600206 "return true/false on integer compare",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200207 "[.b, .w, .l, .s] [*]value1 <op> [*]value2"
wdenk2ebee312004-02-23 19:30:57 +0000208);