]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - cmd/itest.c
1 // SPDX-License-Identifier: GPL-2.0+
4 * Tait Electronics Limited, Christchurch, New Zealand
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
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");
68 l
= (long)(*(u8
*)buf
);
71 l
= (long)(*(u16
*)buf
);
74 l
= (long)(*(u32
*)buf
);
76 #ifdef CONFIG_PHYS_64BIT
78 l
= (long)(*(unsigned long *)buf
);
82 unmap_physmem(buf
, w
);
85 l
= simple_strtoul(s
, NULL
, 16);
88 /* avoid overflow on mask calculus */
89 return (w
>= sizeof(long)) ? l
: (l
& ((1UL << (w
* 8)) - 1));
92 static char * evalstr(char *s
)
94 /* if the parameter starts with a * then assume a string pointer else its a literal */
96 return (char *)simple_strtoul(&s
[1], NULL
, 16);
97 } else if (s
[0] == '$') {
103 while (s
[i
] != '}') {
109 return env_get((const char *)&s
[2]);
115 static int stringcomp(char *s
, char *t
, int op
)
125 case EQ
: return (p
== 0);
126 case NE
: return (p
!= 0);
127 case LT
: return (p
< 0);
128 case GT
: return (p
> 0);
129 case LE
: return (p
<= 0);
130 case GE
: return (p
>= 0);
135 static int arithcomp (char *s
, char *t
, int op
, int w
)
143 case EQ
: return (l
== r
);
144 case NE
: return (l
!= r
);
145 case LT
: return (l
< r
);
146 case GT
: return (l
> r
);
147 case LE
: return (l
<= r
);
148 case GE
: return (l
>= r
);
153 static int binary_test(char *op
, char *arg1
, char *arg2
, int w
)
156 const op_tbl_t
*optp
;
160 for (optp
= (op_tbl_t
*)&op_table
, i
= 0;
161 i
< ARRAY_SIZE(op_table
);
164 if ((strncmp (op
, optp
->op
, len
) == 0) && (len
== strlen (optp
->op
))) {
166 return (stringcomp(arg1
, arg2
, optp
->opcode
));
168 return (arithcomp (arg1
, arg2
, optp
->opcode
, w
));
173 printf("Unknown operator '%s'\n", op
);
174 return 0; /* op code not found */
177 /* command line interface to the shell test */
178 static int do_itest(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
183 /* Validate arguments */
185 return CMD_RET_USAGE
;
187 /* Check for a data width specification.
188 * Defaults to long (4) if no specification.
189 * Uses -2 as 'width' for .s (string) so as not to upset existing code
191 switch (w
= cmd_get_data_size(argv
[0], 4)) {
195 #ifdef CONFIG_PHYS_64BIT
198 value
= binary_test (argv
[2], argv
[1], argv
[3], w
);
201 value
= binary_test (argv
[2], argv
[1], argv
[3], 0);
205 puts("Invalid data width specifier\n");
214 itest
, 4, 0, do_itest
,
215 "return true/false on integer compare",
216 #ifdef CONFIG_PHYS_64BIT
217 "[.b, .w, .l, .q, .s] [*]value1 <op> [*]value2"
219 "[.b, .w, .l, .s] [*]value1 <op> [*]value2"