]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_itest.c
Merge with /home/tur/git/u-boot#cm5200-si
[karo-tx-uboot.git] / common / cmd_itest.c
1 /*
2  * (C) Copyright 2003
3  * Tait Electronics Limited, Christchurch, New Zealand
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 /*
25  * This file provides a shell like 'test' function to return
26  * true/false from an integer or string compare of two memory
27  * locations or a location and a scalar/literal.
28  * A few parts were lifted from bash 'test' command
29  */
30
31 #include <common.h>
32 #include <config.h>
33 #include <command.h>
34
35 #if defined(CONFIG_CMD_ITEST)
36
37 #define EQ      0
38 #define NE      1
39 #define LT      2
40 #define GT      3
41 #define LE      4
42 #define GE      5
43
44 struct op_tbl_s {
45         char    *op;            /* operator string */
46         int     opcode;         /* internal representation of opcode */
47 };
48
49 typedef struct op_tbl_s op_tbl_t;
50
51 op_tbl_t op_table [] = {
52         { "-lt", LT },
53         { "<"  , LT },
54         { "-gt", GT },
55         { ">"  , GT },
56         { "-eq", EQ },
57         { "==" , EQ },
58         { "-ne", NE },
59         { "!=" , NE },
60         { "<>" , NE },
61         { "-ge", GE },
62         { ">=" , GE },
63         { "-le", LE },
64         { "<=" , LE },
65 };
66
67 #define op_tbl_size (sizeof(op_table)/sizeof(op_table[0]))
68
69 extern int cmd_get_data_size(char* arg, int default_size);
70
71 static long evalexp(char *s, int w)
72 {
73         long l, *p;
74
75         /* if the parameter starts with a * then assume is a pointer to the value we want */
76         if (s[0] == '*') {
77                 p = (long *)simple_strtoul(&s[1], NULL, 16);
78                 l = *p;
79         } else {
80                 l = simple_strtoul(s, NULL, 16);
81         }
82
83         return (l & ((1 << (w * 8)) - 1));
84 }
85
86 static 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);
91         } else {
92                 return s;
93         }
94 }
95
96 static int stringcomp(char *s, char *t, int op)
97 {
98         int n, p;
99         char *l, *r;
100
101         l = evalstr(s);
102         r = evalstr(t);
103
104         /* we'll do a compare based on the length of the shortest string */
105         n = min(strlen(l), strlen(r));
106
107         p = strncmp(l, r, n);
108         switch (op) {
109         case EQ: return (p == 0);
110         case NE: return (p != 0);
111         case LT: return (p < 0);
112         case GT: return (p > 0);
113         case LE: return (p <= 0);
114         case GE: return (p >= 0);
115         }
116         return (0);
117 }
118
119 static int arithcomp (char *s, char *t, int op, int w)
120 {
121         long l, r;
122
123         l = evalexp (s, w);
124         r = evalexp (t, w);
125
126         switch (op) {
127         case EQ: return (l == r);
128         case NE: return (l != r);
129         case LT: return (l < r);
130         case GT: return (l > r);
131         case LE: return (l <= r);
132         case GE: return (l >= r);
133         }
134         return (0);
135 }
136
137 int binary_test (char *op, char *arg1, char *arg2, int w)
138 {
139         int len, i;
140         op_tbl_t *optp;
141
142         len = strlen(op);
143
144         for (optp = (op_tbl_t *)&op_table, i = 0;
145              i < op_tbl_size;
146              optp++, i++) {
147
148                 if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
149                         if (w == 0) {
150                                 return (stringcomp(arg1, arg2, optp->opcode));
151                         } else {
152                                 return (arithcomp (arg1, arg2, optp->opcode, w));
153                         }
154                 }
155         }
156
157         printf("Unknown operator '%s'\n", op);
158         return 0;       /* op code not found */
159 }
160
161 /* command line interface to the shell test */
162 int do_itest ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] )
163 {
164         int     value, w;
165
166         /* Validate arguments */
167         if ((argc != 4)){
168                 printf("Usage:\n%s\n", cmdtp->usage);
169                 return 1;
170         }
171
172         /* Check for a data width specification.
173          * Defaults to long (4) if no specification.
174          * Uses -2 as 'width' for .s (string) so as not to upset existing code
175          */
176         switch (w = cmd_get_data_size(argv[0], 4)) {
177         case 1:
178         case 2:
179         case 4:
180                 value = binary_test (argv[2], argv[1], argv[3], w);
181                 break;
182         case -2:
183                 value = binary_test (argv[2], argv[1], argv[3], 0);
184                 break;
185         case -1:
186         default:
187                 puts("Invalid data width specifier\n");
188                 value = 0;
189                 break;
190         }
191
192         return !value;
193 }
194
195 U_BOOT_CMD(
196         itest, 4, 0, do_itest,
197         "itest\t- return true/false on integer compare\n",
198         "[.b, .w, .l, .s] [*]value1 <op> [*]value2\n"
199 );
200 #endif