Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2003 |
| 4 | * Tait Electronics Limited, Christchurch, New Zealand |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This file provides a shell like 'test' function to return |
| 9 | * true/false from an integer or string compare of two memory |
| 10 | * locations or a location and a scalar/literal. |
| 11 | * A few parts were lifted from bash 'test' command |
| 12 | */ |
| 13 | |
| 14 | #include <common.h> |
| 15 | #include <config.h> |
| 16 | #include <command.h> |
Stephen Warren | 304ce79 | 2015-10-03 13:56:46 -0600 | [diff] [blame] | 17 | #include <mapmem.h> |
| 18 | |
| 19 | #include <asm/io.h> |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 20 | |
| 21 | #define EQ 0 |
| 22 | #define NE 1 |
| 23 | #define LT 2 |
| 24 | #define GT 3 |
| 25 | #define LE 4 |
| 26 | #define GE 5 |
| 27 | |
| 28 | struct op_tbl_s { |
| 29 | char *op; /* operator string */ |
| 30 | int opcode; /* internal representation of opcode */ |
| 31 | }; |
| 32 | |
| 33 | typedef struct op_tbl_s op_tbl_t; |
| 34 | |
Mike Frysinger | 17a266a | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 35 | static const op_tbl_t op_table [] = { |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 36 | { "-lt", LT }, |
| 37 | { "<" , LT }, |
| 38 | { "-gt", GT }, |
| 39 | { ">" , GT }, |
| 40 | { "-eq", EQ }, |
| 41 | { "==" , EQ }, |
| 42 | { "-ne", NE }, |
| 43 | { "!=" , NE }, |
| 44 | { "<>" , NE }, |
| 45 | { "-ge", GE }, |
| 46 | { ">=" , GE }, |
| 47 | { "-le", LE }, |
| 48 | { "<=" , LE }, |
| 49 | }; |
| 50 | |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 51 | static long evalexp(char *s, int w) |
| 52 | { |
Frans Meulenbroeks | e67b02f | 2010-02-22 22:49:06 +0100 | [diff] [blame] | 53 | long l = 0; |
Stephen Warren | 304ce79 | 2015-10-03 13:56:46 -0600 | [diff] [blame] | 54 | unsigned long addr; |
| 55 | void *buf; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 56 | |
| 57 | /* if the parameter starts with a * then assume is a pointer to the value we want */ |
| 58 | if (s[0] == '*') { |
Stephen Warren | 304ce79 | 2015-10-03 13:56:46 -0600 | [diff] [blame] | 59 | addr = simple_strtoul(&s[1], NULL, 16); |
| 60 | buf = map_physmem(addr, w, MAP_WRBACK); |
Stephen Warren | 35d0a95 | 2016-01-29 16:10:04 -0700 | [diff] [blame] | 61 | if (!buf && addr) { |
Stephen Warren | 304ce79 | 2015-10-03 13:56:46 -0600 | [diff] [blame] | 62 | puts("Failed to map physical memory\n"); |
| 63 | return 0; |
| 64 | } |
Frans Meulenbroeks | e67b02f | 2010-02-22 22:49:06 +0100 | [diff] [blame] | 65 | switch (w) { |
Stephen Warren | aa632ab | 2015-11-17 10:29:08 -0700 | [diff] [blame] | 66 | case 1: |
Kunihiko Hayashi | 7e7c877 | 2016-05-04 14:20:04 +0900 | [diff] [blame] | 67 | l = (long)(*(u8 *)buf); |
Stephen Warren | aa632ab | 2015-11-17 10:29:08 -0700 | [diff] [blame] | 68 | break; |
| 69 | case 2: |
Kunihiko Hayashi | 7e7c877 | 2016-05-04 14:20:04 +0900 | [diff] [blame] | 70 | l = (long)(*(u16 *)buf); |
Stephen Warren | aa632ab | 2015-11-17 10:29:08 -0700 | [diff] [blame] | 71 | break; |
| 72 | case 4: |
Kunihiko Hayashi | 7e7c877 | 2016-05-04 14:20:04 +0900 | [diff] [blame] | 73 | l = (long)(*(u32 *)buf); |
Stephen Warren | aa632ab | 2015-11-17 10:29:08 -0700 | [diff] [blame] | 74 | break; |
Frans Meulenbroeks | e67b02f | 2010-02-22 22:49:06 +0100 | [diff] [blame] | 75 | } |
Stephen Warren | 304ce79 | 2015-10-03 13:56:46 -0600 | [diff] [blame] | 76 | unmap_physmem(buf, w); |
| 77 | return l; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 78 | } else { |
| 79 | l = simple_strtoul(s, NULL, 16); |
| 80 | } |
| 81 | |
Sebastien Colleur | e3b71f8 | 2017-02-10 15:59:15 +0300 | [diff] [blame] | 82 | /* avoid overflow on mask calculus */ |
| 83 | return (w >= sizeof(long)) ? l : (l & ((1UL << (w * 8)) - 1)); |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 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); |
Heiko Schocher | e82717d | 2014-01-25 07:27:12 +0100 | [diff] [blame] | 91 | } else if (s[0] == '$') { |
| 92 | int i = 2; |
| 93 | |
| 94 | if (s[1] != '{') |
| 95 | return NULL; |
| 96 | |
| 97 | while (s[i] != '}') { |
| 98 | if (s[i] == 0) |
| 99 | return NULL; |
| 100 | i++; |
| 101 | } |
| 102 | s[i] = 0; |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 103 | return env_get((const char *)&s[2]); |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 104 | } else { |
| 105 | return s; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static int stringcomp(char *s, char *t, int op) |
| 110 | { |
Wolfgang Denk | dc4d0a2 | 2011-02-08 16:56:05 +0100 | [diff] [blame] | 111 | int p; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 112 | char *l, *r; |
| 113 | |
| 114 | l = evalstr(s); |
| 115 | r = evalstr(t); |
| 116 | |
Wolfgang Denk | dc4d0a2 | 2011-02-08 16:56:05 +0100 | [diff] [blame] | 117 | p = strcmp(l, r); |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 118 | switch (op) { |
| 119 | case EQ: return (p == 0); |
| 120 | case NE: return (p != 0); |
| 121 | case LT: return (p < 0); |
| 122 | case GT: return (p > 0); |
| 123 | case LE: return (p <= 0); |
| 124 | case GE: return (p >= 0); |
| 125 | } |
| 126 | return (0); |
| 127 | } |
| 128 | |
| 129 | static int arithcomp (char *s, char *t, int op, int w) |
| 130 | { |
| 131 | long l, r; |
| 132 | |
| 133 | l = evalexp (s, w); |
| 134 | r = evalexp (t, w); |
| 135 | |
| 136 | switch (op) { |
| 137 | case EQ: return (l == r); |
| 138 | case NE: return (l != r); |
| 139 | case LT: return (l < r); |
| 140 | case GT: return (l > r); |
| 141 | case LE: return (l <= r); |
| 142 | case GE: return (l >= r); |
| 143 | } |
| 144 | return (0); |
| 145 | } |
| 146 | |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 147 | static int binary_test(char *op, char *arg1, char *arg2, int w) |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 148 | { |
| 149 | int len, i; |
Mike Frysinger | 17a266a | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 150 | const op_tbl_t *optp; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 151 | |
| 152 | len = strlen(op); |
| 153 | |
| 154 | for (optp = (op_tbl_t *)&op_table, i = 0; |
Mike Frysinger | 17a266a | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 155 | i < ARRAY_SIZE(op_table); |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 156 | optp++, i++) { |
| 157 | |
| 158 | if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) { |
| 159 | if (w == 0) { |
| 160 | return (stringcomp(arg1, arg2, optp->opcode)); |
| 161 | } else { |
| 162 | return (arithcomp (arg1, arg2, optp->opcode, w)); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | printf("Unknown operator '%s'\n", op); |
| 168 | return 0; /* op code not found */ |
| 169 | } |
| 170 | |
| 171 | /* command line interface to the shell test */ |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 172 | 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] | 173 | { |
wdenk | f70cbb2 | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 174 | int value, w; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 175 | |
wdenk | f70cbb2 | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 176 | /* Validate arguments */ |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 177 | if ((argc != 4)) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 178 | return CMD_RET_USAGE; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 179 | |
| 180 | /* Check for a data width specification. |
| 181 | * Defaults to long (4) if no specification. |
| 182 | * Uses -2 as 'width' for .s (string) so as not to upset existing code |
| 183 | */ |
| 184 | switch (w = cmd_get_data_size(argv[0], 4)) { |
| 185 | case 1: |
| 186 | case 2: |
| 187 | case 4: |
| 188 | value = binary_test (argv[2], argv[1], argv[3], w); |
| 189 | break; |
| 190 | case -2: |
| 191 | value = binary_test (argv[2], argv[1], argv[3], 0); |
| 192 | break; |
| 193 | case -1: |
| 194 | default: |
| 195 | puts("Invalid data width specifier\n"); |
| 196 | value = 0; |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | return !value; |
| 201 | } |
| 202 | |
| 203 | U_BOOT_CMD( |
| 204 | itest, 4, 0, do_itest, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 205 | "return true/false on integer compare", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 206 | "[.b, .w, .l, .s] [*]value1 <op> [*]value2" |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 207 | ); |