wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 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 |
wdenk | cd0a9de | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 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 | #define EQ 0 |
| 36 | #define NE 1 |
| 37 | #define LT 2 |
| 38 | #define GT 3 |
| 39 | #define LE 4 |
| 40 | #define GE 5 |
| 41 | |
| 42 | struct op_tbl_s { |
| 43 | char *op; /* operator string */ |
| 44 | int opcode; /* internal representation of opcode */ |
| 45 | }; |
| 46 | |
| 47 | typedef struct op_tbl_s op_tbl_t; |
| 48 | |
Mike Frysinger | fc9903f | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 49 | static const op_tbl_t op_table [] = { |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 50 | { "-lt", LT }, |
| 51 | { "<" , LT }, |
| 52 | { "-gt", GT }, |
| 53 | { ">" , GT }, |
| 54 | { "-eq", EQ }, |
| 55 | { "==" , EQ }, |
| 56 | { "-ne", NE }, |
| 57 | { "!=" , NE }, |
| 58 | { "<>" , NE }, |
| 59 | { "-ge", GE }, |
| 60 | { ">=" , GE }, |
| 61 | { "-le", LE }, |
| 62 | { "<=" , LE }, |
| 63 | }; |
| 64 | |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 65 | static long evalexp(char *s, int w) |
| 66 | { |
Frans Meulenbroeks | f365176 | 2010-02-22 22:49:06 +0100 | [diff] [blame] | 67 | long l = 0; |
| 68 | long *p; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 69 | |
| 70 | /* if the parameter starts with a * then assume is a pointer to the value we want */ |
| 71 | if (s[0] == '*') { |
| 72 | p = (long *)simple_strtoul(&s[1], NULL, 16); |
Frans Meulenbroeks | f365176 | 2010-02-22 22:49:06 +0100 | [diff] [blame] | 73 | switch (w) { |
| 74 | case 1: return((long)(*(unsigned char *)p)); |
| 75 | case 2: return((long)(*(unsigned short *)p)); |
| 76 | case 4: return(*p); |
| 77 | } |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 78 | } else { |
| 79 | l = simple_strtoul(s, NULL, 16); |
| 80 | } |
| 81 | |
| 82 | return (l & ((1 << (w * 8)) - 1)); |
| 83 | } |
| 84 | |
| 85 | static char * evalstr(char *s) |
| 86 | { |
| 87 | /* if the parameter starts with a * then assume a string pointer else its a literal */ |
| 88 | if (s[0] == '*') { |
| 89 | return (char *)simple_strtoul(&s[1], NULL, 16); |
| 90 | } else { |
| 91 | return s; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static int stringcomp(char *s, char *t, int op) |
| 96 | { |
Wolfgang Denk | cc22b79 | 2011-02-08 16:56:05 +0100 | [diff] [blame] | 97 | int p; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 98 | char *l, *r; |
| 99 | |
| 100 | l = evalstr(s); |
| 101 | r = evalstr(t); |
| 102 | |
Wolfgang Denk | cc22b79 | 2011-02-08 16:56:05 +0100 | [diff] [blame] | 103 | p = strcmp(l, r); |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 104 | switch (op) { |
| 105 | case EQ: return (p == 0); |
| 106 | case NE: return (p != 0); |
| 107 | case LT: return (p < 0); |
| 108 | case GT: return (p > 0); |
| 109 | case LE: return (p <= 0); |
| 110 | case GE: return (p >= 0); |
| 111 | } |
| 112 | return (0); |
| 113 | } |
| 114 | |
| 115 | static int arithcomp (char *s, char *t, int op, int w) |
| 116 | { |
| 117 | long l, r; |
| 118 | |
| 119 | l = evalexp (s, w); |
| 120 | r = evalexp (t, w); |
| 121 | |
| 122 | switch (op) { |
| 123 | case EQ: return (l == r); |
| 124 | case NE: return (l != r); |
| 125 | case LT: return (l < r); |
| 126 | case GT: return (l > r); |
| 127 | case LE: return (l <= r); |
| 128 | case GE: return (l >= r); |
| 129 | } |
| 130 | return (0); |
| 131 | } |
| 132 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 133 | static int binary_test(char *op, char *arg1, char *arg2, int w) |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 134 | { |
| 135 | int len, i; |
Mike Frysinger | fc9903f | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 136 | const op_tbl_t *optp; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 137 | |
| 138 | len = strlen(op); |
| 139 | |
| 140 | for (optp = (op_tbl_t *)&op_table, i = 0; |
Mike Frysinger | fc9903f | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 141 | i < ARRAY_SIZE(op_table); |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 142 | optp++, i++) { |
| 143 | |
| 144 | if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) { |
| 145 | if (w == 0) { |
| 146 | return (stringcomp(arg1, arg2, optp->opcode)); |
| 147 | } else { |
| 148 | return (arithcomp (arg1, arg2, optp->opcode, w)); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | printf("Unknown operator '%s'\n", op); |
| 154 | return 0; /* op code not found */ |
| 155 | } |
| 156 | |
| 157 | /* command line interface to the shell test */ |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 158 | static int do_itest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 159 | { |
wdenk | cd0a9de | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 160 | int value, w; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 161 | |
wdenk | cd0a9de | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 162 | /* Validate arguments */ |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 163 | if ((argc != 4)) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 164 | return CMD_RET_USAGE; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 165 | |
| 166 | /* Check for a data width specification. |
| 167 | * Defaults to long (4) if no specification. |
| 168 | * Uses -2 as 'width' for .s (string) so as not to upset existing code |
| 169 | */ |
| 170 | switch (w = cmd_get_data_size(argv[0], 4)) { |
| 171 | case 1: |
| 172 | case 2: |
| 173 | case 4: |
| 174 | value = binary_test (argv[2], argv[1], argv[3], w); |
| 175 | break; |
| 176 | case -2: |
| 177 | value = binary_test (argv[2], argv[1], argv[3], 0); |
| 178 | break; |
| 179 | case -1: |
| 180 | default: |
| 181 | puts("Invalid data width specifier\n"); |
| 182 | value = 0; |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | return !value; |
| 187 | } |
| 188 | |
| 189 | U_BOOT_CMD( |
| 190 | itest, 4, 0, do_itest, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 191 | "return true/false on integer compare", |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 192 | "[.b, .w, .l, .s] [*]value1 <op> [*]value2" |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 193 | ); |