]> git.ipfire.org Git - people/ms/u-boot.git/blame - cmd/cmd_itest.c
Move all command code into its own directory
[people/ms/u-boot.git] / cmd / cmd_itest.c
CommitLineData
2d1a537d
WD
1/*
2 * (C) Copyright 2003
3 * Tait Electronics Limited, Christchurch, New Zealand
4 *
3765b3e7 5 * SPDX-License-Identifier: GPL-2.0+
2d1a537d
WD
6 */
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>
7861204c
SW
18#include <mapmem.h>
19
20#include <asm/io.h>
2d1a537d
WD
21
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
fc9903f3 36static const op_tbl_t op_table [] = {
2d1a537d
WD
37 { "-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
2d1a537d
WD
52static long evalexp(char *s, int w)
53{
f3651764 54 long l = 0;
7861204c
SW
55 unsigned long addr;
56 void *buf;
2d1a537d
WD
57
58 /* if the parameter starts with a * then assume is a pointer to the value we want */
59 if (s[0] == '*') {
7861204c
SW
60 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 }
f3651764 66 switch (w) {
736d1746
SW
67 case 1:
68 l = (long)(*(unsigned char *)buf);
69 break;
70 case 2:
71 l = (long)(*(unsigned short *)buf);
72 break;
73 case 4:
74 l = (long)(*(unsigned long *)buf);
75 break;
f3651764 76 }
7861204c
SW
77 unmap_physmem(buf, w);
78 return l;
2d1a537d
WD
79 } else {
80 l = simple_strtoul(s, NULL, 16);
81 }
82
c9bcb6f1 83 return l & ((1UL << (w * 8)) - 1);
2d1a537d
WD
84}
85
86static char * evalstr(char *s)
87{
88 /* if the parameter starts with a * then assume a string pointer else its a literal */
89 if (s[0] == '*') {
90 return (char *)simple_strtoul(&s[1], NULL, 16);
06109f49
HS
91 } else if (s[0] == '$') {
92 int i = 2;
93
94 if (s[1] != '{')
95 return NULL;
96
97 while (s[i] != '}') {
98 if (s[i] == 0)
99 return NULL;
100 i++;
101 }
102 s[i] = 0;
103 return getenv((const char *)&s[2]);
2d1a537d
WD
104 } else {
105 return s;
106 }
107}
108
109static int stringcomp(char *s, char *t, int op)
110{
cc22b795 111 int p;
2d1a537d
WD
112 char *l, *r;
113
114 l = evalstr(s);
115 r = evalstr(t);
116
cc22b795 117 p = strcmp(l, r);
2d1a537d
WD
118 switch (op) {
119 case EQ: return (p == 0);
120 case NE: return (p != 0);
121 case LT: return (p < 0);
122 case GT: return (p > 0);
123 case LE: return (p <= 0);
124 case GE: return (p >= 0);
125 }
126 return (0);
127}
128
129static int arithcomp (char *s, char *t, int op, int w)
130{
131 long l, r;
132
133 l = evalexp (s, w);
134 r = evalexp (t, w);
135
136 switch (op) {
137 case EQ: return (l == r);
138 case NE: return (l != r);
139 case LT: return (l < r);
140 case GT: return (l > r);
141 case LE: return (l <= r);
142 case GE: return (l >= r);
143 }
144 return (0);
145}
146
088f1b19 147static int binary_test(char *op, char *arg1, char *arg2, int w)
2d1a537d
WD
148{
149 int len, i;
fc9903f3 150 const op_tbl_t *optp;
2d1a537d
WD
151
152 len = strlen(op);
153
154 for (optp = (op_tbl_t *)&op_table, i = 0;
fc9903f3 155 i < ARRAY_SIZE(op_table);
2d1a537d
WD
156 optp++, i++) {
157
158 if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
159 if (w == 0) {
160 return (stringcomp(arg1, arg2, optp->opcode));
161 } else {
162 return (arithcomp (arg1, arg2, optp->opcode, w));
163 }
164 }
165 }
166
167 printf("Unknown operator '%s'\n", op);
168 return 0; /* op code not found */
169}
170
171/* command line interface to the shell test */
088f1b19 172static int do_itest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
2d1a537d 173{
cd0a9de6 174 int value, w;
2d1a537d 175
cd0a9de6 176 /* Validate arguments */
47e26b1b 177 if ((argc != 4))
4c12eeb8 178 return CMD_RET_USAGE;
2d1a537d
WD
179
180 /* Check for a data width specification.
181 * Defaults to long (4) if no specification.
182 * Uses -2 as 'width' for .s (string) so as not to upset existing code
183 */
184 switch (w = cmd_get_data_size(argv[0], 4)) {
185 case 1:
186 case 2:
187 case 4:
188 value = binary_test (argv[2], argv[1], argv[3], w);
189 break;
190 case -2:
191 value = binary_test (argv[2], argv[1], argv[3], 0);
192 break;
193 case -1:
194 default:
195 puts("Invalid data width specifier\n");
196 value = 0;
197 break;
198 }
199
200 return !value;
201}
202
203U_BOOT_CMD(
204 itest, 4, 0, do_itest,
2fb2604d 205 "return true/false on integer compare",
a89c33db 206 "[.b, .w, .l, .s] [*]value1 <op> [*]value2"
2d1a537d 207);