wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Tait Electronics Limited, Christchurch, New Zealand |
| 4 | * |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 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 | |
| 26 | struct op_tbl_s { |
| 27 | char *op; /* operator string */ |
| 28 | int opcode; /* internal representation of opcode */ |
| 29 | }; |
| 30 | |
| 31 | typedef struct op_tbl_s op_tbl_t; |
| 32 | |
Mike Frysinger | 17a266a | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 33 | static const op_tbl_t op_table [] = { |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 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 | |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 49 | static long evalexp(char *s, int w) |
| 50 | { |
Frans Meulenbroeks | e67b02f | 2010-02-22 22:49:06 +0100 | [diff] [blame] | 51 | long l = 0; |
| 52 | long *p; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 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); |
Frans Meulenbroeks | e67b02f | 2010-02-22 22:49:06 +0100 | [diff] [blame] | 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 | } |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 62 | } else { |
| 63 | l = simple_strtoul(s, NULL, 16); |
| 64 | } |
| 65 | |
| 66 | return (l & ((1 << (w * 8)) - 1)); |
| 67 | } |
| 68 | |
| 69 | static 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); |
Heiko Schocher | e82717d | 2014-01-25 07:27:12 +0100 | [diff] [blame] | 74 | } else if (s[0] == '$') { |
| 75 | int i = 2; |
| 76 | |
| 77 | if (s[1] != '{') |
| 78 | return NULL; |
| 79 | |
| 80 | while (s[i] != '}') { |
| 81 | if (s[i] == 0) |
| 82 | return NULL; |
| 83 | i++; |
| 84 | } |
| 85 | s[i] = 0; |
| 86 | return getenv((const char *)&s[2]); |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 87 | } else { |
| 88 | return s; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static int stringcomp(char *s, char *t, int op) |
| 93 | { |
Wolfgang Denk | dc4d0a2 | 2011-02-08 16:56:05 +0100 | [diff] [blame] | 94 | int p; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 95 | char *l, *r; |
| 96 | |
| 97 | l = evalstr(s); |
| 98 | r = evalstr(t); |
| 99 | |
Wolfgang Denk | dc4d0a2 | 2011-02-08 16:56:05 +0100 | [diff] [blame] | 100 | p = strcmp(l, r); |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 101 | switch (op) { |
| 102 | case EQ: return (p == 0); |
| 103 | case NE: return (p != 0); |
| 104 | case LT: return (p < 0); |
| 105 | case GT: return (p > 0); |
| 106 | case LE: return (p <= 0); |
| 107 | case GE: return (p >= 0); |
| 108 | } |
| 109 | return (0); |
| 110 | } |
| 111 | |
| 112 | static int arithcomp (char *s, char *t, int op, int w) |
| 113 | { |
| 114 | long l, r; |
| 115 | |
| 116 | l = evalexp (s, w); |
| 117 | r = evalexp (t, w); |
| 118 | |
| 119 | switch (op) { |
| 120 | case EQ: return (l == r); |
| 121 | case NE: return (l != r); |
| 122 | case LT: return (l < r); |
| 123 | case GT: return (l > r); |
| 124 | case LE: return (l <= r); |
| 125 | case GE: return (l >= r); |
| 126 | } |
| 127 | return (0); |
| 128 | } |
| 129 | |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 130 | static int binary_test(char *op, char *arg1, char *arg2, int w) |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 131 | { |
| 132 | int len, i; |
Mike Frysinger | 17a266a | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 133 | const op_tbl_t *optp; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 134 | |
| 135 | len = strlen(op); |
| 136 | |
| 137 | for (optp = (op_tbl_t *)&op_table, i = 0; |
Mike Frysinger | 17a266a | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 138 | i < ARRAY_SIZE(op_table); |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 139 | optp++, i++) { |
| 140 | |
| 141 | if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) { |
| 142 | if (w == 0) { |
| 143 | return (stringcomp(arg1, arg2, optp->opcode)); |
| 144 | } else { |
| 145 | return (arithcomp (arg1, arg2, optp->opcode, w)); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | printf("Unknown operator '%s'\n", op); |
| 151 | return 0; /* op code not found */ |
| 152 | } |
| 153 | |
| 154 | /* command line interface to the shell test */ |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 155 | static int do_itest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 156 | { |
wdenk | f70cbb2 | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 157 | int value, w; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 158 | |
wdenk | f70cbb2 | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 159 | /* Validate arguments */ |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 160 | if ((argc != 4)) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 161 | return CMD_RET_USAGE; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 162 | |
| 163 | /* Check for a data width specification. |
| 164 | * Defaults to long (4) if no specification. |
| 165 | * Uses -2 as 'width' for .s (string) so as not to upset existing code |
| 166 | */ |
| 167 | switch (w = cmd_get_data_size(argv[0], 4)) { |
| 168 | case 1: |
| 169 | case 2: |
| 170 | case 4: |
| 171 | value = binary_test (argv[2], argv[1], argv[3], w); |
| 172 | break; |
| 173 | case -2: |
| 174 | value = binary_test (argv[2], argv[1], argv[3], 0); |
| 175 | break; |
| 176 | case -1: |
| 177 | default: |
| 178 | puts("Invalid data width specifier\n"); |
| 179 | value = 0; |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | return !value; |
| 184 | } |
| 185 | |
| 186 | U_BOOT_CMD( |
| 187 | itest, 4, 0, do_itest, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 188 | "return true/false on integer compare", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 189 | "[.b, .w, .l, .s] [*]value1 <op> [*]value2" |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 190 | ); |