blob: 596341c9635ac6c1777c171fbda01186e23e02a3 [file] [log] [blame]
wdenk2ebee312004-02-23 19:30:57 +00001/*
2 * (C) Copyright 2003
3 * Tait Electronics Limited, Christchurch, New Zealand
4 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk2ebee312004-02-23 19:30:57 +00006 */
7
8/*
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
13 */
14
15#include <common.h>
16#include <config.h>
17#include <command.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);
62 if (!buf) {
63 puts("Failed to map physical memory\n");
64 return 0;
65 }
Frans Meulenbroekse67b02f2010-02-22 22:49:06 +010066 switch (w) {
Stephen Warren304ce792015-10-03 13:56:46 -060067 case 1: l = (long)(*(unsigned char *)buf);
68 case 2: l = (long)(*(unsigned short *)buf);
69 case 4: l = (long)(*(unsigned long *)buf);
Frans Meulenbroekse67b02f2010-02-22 22:49:06 +010070 }
Stephen Warren304ce792015-10-03 13:56:46 -060071 unmap_physmem(buf, w);
72 return l;
wdenk2ebee312004-02-23 19:30:57 +000073 } else {
74 l = simple_strtoul(s, NULL, 16);
75 }
76
Simon Glassa5d0a332014-05-30 14:41:49 -060077 return l & ((1UL << (w * 8)) - 1);
wdenk2ebee312004-02-23 19:30:57 +000078}
79
80static char * evalstr(char *s)
81{
82 /* if the parameter starts with a * then assume a string pointer else its a literal */
83 if (s[0] == '*') {
84 return (char *)simple_strtoul(&s[1], NULL, 16);
Heiko Schochere82717d2014-01-25 07:27:12 +010085 } else if (s[0] == '$') {
86 int i = 2;
87
88 if (s[1] != '{')
89 return NULL;
90
91 while (s[i] != '}') {
92 if (s[i] == 0)
93 return NULL;
94 i++;
95 }
96 s[i] = 0;
97 return getenv((const char *)&s[2]);
wdenk2ebee312004-02-23 19:30:57 +000098 } else {
99 return s;
100 }
101}
102
103static int stringcomp(char *s, char *t, int op)
104{
Wolfgang Denkdc4d0a22011-02-08 16:56:05 +0100105 int p;
wdenk2ebee312004-02-23 19:30:57 +0000106 char *l, *r;
107
108 l = evalstr(s);
109 r = evalstr(t);
110
Wolfgang Denkdc4d0a22011-02-08 16:56:05 +0100111 p = strcmp(l, r);
wdenk2ebee312004-02-23 19:30:57 +0000112 switch (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);
119 }
120 return (0);
121}
122
123static int arithcomp (char *s, char *t, int op, int w)
124{
125 long l, r;
126
127 l = evalexp (s, w);
128 r = evalexp (t, w);
129
130 switch (op) {
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);
137 }
138 return (0);
139}
140
Kim Phillipsdc00a682012-10-29 13:34:31 +0000141static int binary_test(char *op, char *arg1, char *arg2, int w)
wdenk2ebee312004-02-23 19:30:57 +0000142{
143 int len, i;
Mike Frysinger17a266a2010-10-20 07:17:33 -0400144 const op_tbl_t *optp;
wdenk2ebee312004-02-23 19:30:57 +0000145
146 len = strlen(op);
147
148 for (optp = (op_tbl_t *)&op_table, i = 0;
Mike Frysinger17a266a2010-10-20 07:17:33 -0400149 i < ARRAY_SIZE(op_table);
wdenk2ebee312004-02-23 19:30:57 +0000150 optp++, i++) {
151
152 if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
153 if (w == 0) {
154 return (stringcomp(arg1, arg2, optp->opcode));
155 } else {
156 return (arithcomp (arg1, arg2, optp->opcode, w));
157 }
158 }
159 }
160
161 printf("Unknown operator '%s'\n", op);
162 return 0; /* op code not found */
163}
164
165/* command line interface to the shell test */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000166static int do_itest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk2ebee312004-02-23 19:30:57 +0000167{
wdenkf70cbb22004-02-23 20:48:38 +0000168 int value, w;
wdenk2ebee312004-02-23 19:30:57 +0000169
wdenkf70cbb22004-02-23 20:48:38 +0000170 /* Validate arguments */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200171 if ((argc != 4))
Simon Glassa06dfc72011-12-10 08:44:01 +0000172 return CMD_RET_USAGE;
wdenk2ebee312004-02-23 19:30:57 +0000173
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
177 */
178 switch (w = cmd_get_data_size(argv[0], 4)) {
179 case 1:
180 case 2:
181 case 4:
182 value = binary_test (argv[2], argv[1], argv[3], w);
183 break;
184 case -2:
185 value = binary_test (argv[2], argv[1], argv[3], 0);
186 break;
187 case -1:
188 default:
189 puts("Invalid data width specifier\n");
190 value = 0;
191 break;
192 }
193
194 return !value;
195}
196
197U_BOOT_CMD(
198 itest, 4, 0, do_itest,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600199 "return true/false on integer compare",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200200 "[.b, .w, .l, .s] [*]value1 <op> [*]value2"
wdenk2ebee312004-02-23 19:30:57 +0000201);