]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_itest.c
net: designware: Respect "bus mode" register contents on SW reset
[people/ms/u-boot.git] / common / 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>
18
19#define EQ 0
20#define NE 1
21#define LT 2
22#define GT 3
23#define LE 4
24#define GE 5
25
26struct op_tbl_s {
27 char *op; /* operator string */
28 int opcode; /* internal representation of opcode */
29};
30
31typedef struct op_tbl_s op_tbl_t;
32
fc9903f3 33static const op_tbl_t op_table [] = {
2d1a537d
WD
34 { "-lt", LT },
35 { "<" , LT },
36 { "-gt", GT },
37 { ">" , GT },
38 { "-eq", EQ },
39 { "==" , EQ },
40 { "-ne", NE },
41 { "!=" , NE },
42 { "<>" , NE },
43 { "-ge", GE },
44 { ">=" , GE },
45 { "-le", LE },
46 { "<=" , LE },
47};
48
2d1a537d
WD
49static long evalexp(char *s, int w)
50{
f3651764
FM
51 long l = 0;
52 long *p;
2d1a537d
WD
53
54 /* if the parameter starts with a * then assume is a pointer to the value we want */
55 if (s[0] == '*') {
56 p = (long *)simple_strtoul(&s[1], NULL, 16);
f3651764
FM
57 switch (w) {
58 case 1: return((long)(*(unsigned char *)p));
59 case 2: return((long)(*(unsigned short *)p));
60 case 4: return(*p);
61 }
2d1a537d
WD
62 } else {
63 l = simple_strtoul(s, NULL, 16);
64 }
65
66 return (l & ((1 << (w * 8)) - 1));
67}
68
69static char * evalstr(char *s)
70{
71 /* if the parameter starts with a * then assume a string pointer else its a literal */
72 if (s[0] == '*') {
73 return (char *)simple_strtoul(&s[1], NULL, 16);
74 } else {
75 return s;
76 }
77}
78
79static int stringcomp(char *s, char *t, int op)
80{
cc22b795 81 int p;
2d1a537d
WD
82 char *l, *r;
83
84 l = evalstr(s);
85 r = evalstr(t);
86
cc22b795 87 p = strcmp(l, r);
2d1a537d
WD
88 switch (op) {
89 case EQ: return (p == 0);
90 case NE: return (p != 0);
91 case LT: return (p < 0);
92 case GT: return (p > 0);
93 case LE: return (p <= 0);
94 case GE: return (p >= 0);
95 }
96 return (0);
97}
98
99static int arithcomp (char *s, char *t, int op, int w)
100{
101 long l, r;
102
103 l = evalexp (s, w);
104 r = evalexp (t, w);
105
106 switch (op) {
107 case EQ: return (l == r);
108 case NE: return (l != r);
109 case LT: return (l < r);
110 case GT: return (l > r);
111 case LE: return (l <= r);
112 case GE: return (l >= r);
113 }
114 return (0);
115}
116
088f1b19 117static int binary_test(char *op, char *arg1, char *arg2, int w)
2d1a537d
WD
118{
119 int len, i;
fc9903f3 120 const op_tbl_t *optp;
2d1a537d
WD
121
122 len = strlen(op);
123
124 for (optp = (op_tbl_t *)&op_table, i = 0;
fc9903f3 125 i < ARRAY_SIZE(op_table);
2d1a537d
WD
126 optp++, i++) {
127
128 if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
129 if (w == 0) {
130 return (stringcomp(arg1, arg2, optp->opcode));
131 } else {
132 return (arithcomp (arg1, arg2, optp->opcode, w));
133 }
134 }
135 }
136
137 printf("Unknown operator '%s'\n", op);
138 return 0; /* op code not found */
139}
140
141/* command line interface to the shell test */
088f1b19 142static int do_itest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
2d1a537d 143{
cd0a9de6 144 int value, w;
2d1a537d 145
cd0a9de6 146 /* Validate arguments */
47e26b1b 147 if ((argc != 4))
4c12eeb8 148 return CMD_RET_USAGE;
2d1a537d
WD
149
150 /* Check for a data width specification.
151 * Defaults to long (4) if no specification.
152 * Uses -2 as 'width' for .s (string) so as not to upset existing code
153 */
154 switch (w = cmd_get_data_size(argv[0], 4)) {
155 case 1:
156 case 2:
157 case 4:
158 value = binary_test (argv[2], argv[1], argv[3], w);
159 break;
160 case -2:
161 value = binary_test (argv[2], argv[1], argv[3], 0);
162 break;
163 case -1:
164 default:
165 puts("Invalid data width specifier\n");
166 value = 0;
167 break;
168 }
169
170 return !value;
171}
172
173U_BOOT_CMD(
174 itest, 4, 0, do_itest,
2fb2604d 175 "return true/false on integer compare",
a89c33db 176 "[.b, .w, .l, .s] [*]value1 <op> [*]value2"
2d1a537d 177);