blob: 29f8076f825568833cdc4324b9aaf778e67f4180 [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>
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
Mike Frysinger17a266a2010-10-20 07:17:33 -040033static const op_tbl_t op_table [] = {
wdenk2ebee312004-02-23 19:30:57 +000034 { "-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
wdenk2ebee312004-02-23 19:30:57 +000049static long evalexp(char *s, int w)
50{
Frans Meulenbroekse67b02f2010-02-22 22:49:06 +010051 long l = 0;
52 long *p;
wdenk2ebee312004-02-23 19:30:57 +000053
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);
Frans Meulenbroekse67b02f2010-02-22 22:49:06 +010057 switch (w) {
58 case 1: return((long)(*(unsigned char *)p));
59 case 2: return((long)(*(unsigned short *)p));
60 case 4: return(*p);
61 }
wdenk2ebee312004-02-23 19:30:57 +000062 } 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{
Wolfgang Denkdc4d0a22011-02-08 16:56:05 +010081 int p;
wdenk2ebee312004-02-23 19:30:57 +000082 char *l, *r;
83
84 l = evalstr(s);
85 r = evalstr(t);
86
Wolfgang Denkdc4d0a22011-02-08 16:56:05 +010087 p = strcmp(l, r);
wdenk2ebee312004-02-23 19:30:57 +000088 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
Kim Phillipsdc00a682012-10-29 13:34:31 +0000117static int binary_test(char *op, char *arg1, char *arg2, int w)
wdenk2ebee312004-02-23 19:30:57 +0000118{
119 int len, i;
Mike Frysinger17a266a2010-10-20 07:17:33 -0400120 const op_tbl_t *optp;
wdenk2ebee312004-02-23 19:30:57 +0000121
122 len = strlen(op);
123
124 for (optp = (op_tbl_t *)&op_table, i = 0;
Mike Frysinger17a266a2010-10-20 07:17:33 -0400125 i < ARRAY_SIZE(op_table);
wdenk2ebee312004-02-23 19:30:57 +0000126 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 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000142static int do_itest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk2ebee312004-02-23 19:30:57 +0000143{
wdenkf70cbb22004-02-23 20:48:38 +0000144 int value, w;
wdenk2ebee312004-02-23 19:30:57 +0000145
wdenkf70cbb22004-02-23 20:48:38 +0000146 /* Validate arguments */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200147 if ((argc != 4))
Simon Glassa06dfc72011-12-10 08:44:01 +0000148 return CMD_RET_USAGE;
wdenk2ebee312004-02-23 19:30:57 +0000149
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,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600175 "return true/false on integer compare",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200176 "[.b, .w, .l, .s] [*]value1 <op> [*]value2"
wdenk2ebee312004-02-23 19:30:57 +0000177);