Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2003 |
| 4 | * Tait Electronics Limited, Christchurch, New Zealand |
wdenk | 2d1a537 | 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> |
Simon Glass | 7b51b57 | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 17 | #include <env.h> |
Stephen Warren | 7861204 | 2015-10-03 13:56:46 -0600 | [diff] [blame] | 18 | #include <mapmem.h> |
| 19 | |
| 20 | #include <asm/io.h> |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 21 | |
| 22 | #define EQ 0 |
| 23 | #define NE 1 |
| 24 | #define LT 2 |
| 25 | #define GT 3 |
| 26 | #define LE 4 |
| 27 | #define GE 5 |
| 28 | |
| 29 | struct op_tbl_s { |
| 30 | char *op; /* operator string */ |
| 31 | int opcode; /* internal representation of opcode */ |
| 32 | }; |
| 33 | |
| 34 | typedef struct op_tbl_s op_tbl_t; |
| 35 | |
Mike Frysinger | fc9903f | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 36 | static const op_tbl_t op_table [] = { |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 37 | { "-lt", LT }, |
| 38 | { "<" , LT }, |
| 39 | { "-gt", GT }, |
| 40 | { ">" , GT }, |
| 41 | { "-eq", EQ }, |
| 42 | { "==" , EQ }, |
| 43 | { "-ne", NE }, |
| 44 | { "!=" , NE }, |
| 45 | { "<>" , NE }, |
| 46 | { "-ge", GE }, |
| 47 | { ">=" , GE }, |
| 48 | { "-le", LE }, |
| 49 | { "<=" , LE }, |
| 50 | }; |
| 51 | |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 52 | static long evalexp(char *s, int w) |
| 53 | { |
Frans Meulenbroeks | f365176 | 2010-02-22 22:49:06 +0100 | [diff] [blame] | 54 | long l = 0; |
Stephen Warren | 7861204 | 2015-10-03 13:56:46 -0600 | [diff] [blame] | 55 | unsigned long addr; |
| 56 | void *buf; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 57 | |
| 58 | /* if the parameter starts with a * then assume is a pointer to the value we want */ |
| 59 | if (s[0] == '*') { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 60 | addr = hextoul(&s[1], NULL); |
Stephen Warren | 7861204 | 2015-10-03 13:56:46 -0600 | [diff] [blame] | 61 | buf = map_physmem(addr, w, MAP_WRBACK); |
Stephen Warren | 986fe37 | 2016-01-29 16:10:04 -0700 | [diff] [blame] | 62 | if (!buf && addr) { |
Stephen Warren | 7861204 | 2015-10-03 13:56:46 -0600 | [diff] [blame] | 63 | puts("Failed to map physical memory\n"); |
| 64 | return 0; |
Frans Meulenbroeks | f365176 | 2010-02-22 22:49:06 +0100 | [diff] [blame] | 65 | } |
Stephen Warren | 7861204 | 2015-10-03 13:56:46 -0600 | [diff] [blame] | 66 | switch (w) { |
Stephen Warren | 736d174 | 2015-11-17 10:29:08 -0700 | [diff] [blame] | 67 | case 1: |
Kunihiko Hayashi | dafd648 | 2016-05-04 14:20:04 +0900 | [diff] [blame] | 68 | l = (long)(*(u8 *)buf); |
Stephen Warren | 736d174 | 2015-11-17 10:29:08 -0700 | [diff] [blame] | 69 | break; |
| 70 | case 2: |
Kunihiko Hayashi | dafd648 | 2016-05-04 14:20:04 +0900 | [diff] [blame] | 71 | l = (long)(*(u16 *)buf); |
Stephen Warren | 736d174 | 2015-11-17 10:29:08 -0700 | [diff] [blame] | 72 | break; |
| 73 | case 4: |
Kunihiko Hayashi | dafd648 | 2016-05-04 14:20:04 +0900 | [diff] [blame] | 74 | l = (long)(*(u32 *)buf); |
Stephen Warren | 736d174 | 2015-11-17 10:29:08 -0700 | [diff] [blame] | 75 | break; |
Marek Szyprowski | 7d2dc6a | 2019-10-02 12:24:46 +0200 | [diff] [blame] | 76 | #ifdef CONFIG_PHYS_64BIT |
| 77 | case 8: |
| 78 | l = (long)(*(unsigned long *)buf); |
| 79 | break; |
| 80 | #endif |
Stephen Warren | 7861204 | 2015-10-03 13:56:46 -0600 | [diff] [blame] | 81 | } |
| 82 | unmap_physmem(buf, w); |
| 83 | return l; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 84 | } else { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 85 | l = hextoul(s, NULL); |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Sebastien Colleur | 2c79fd4 | 2017-02-10 15:59:15 +0300 | [diff] [blame] | 88 | /* avoid overflow on mask calculus */ |
| 89 | return (w >= sizeof(long)) ? l : (l & ((1UL << (w * 8)) - 1)); |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static char * evalstr(char *s) |
| 93 | { |
| 94 | /* if the parameter starts with a * then assume a string pointer else its a literal */ |
| 95 | if (s[0] == '*') { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 96 | return (char *)hextoul(&s[1], NULL); |
Heiko Schocher | 06109f4 | 2014-01-25 07:27:12 +0100 | [diff] [blame] | 97 | } else if (s[0] == '$') { |
| 98 | int i = 2; |
| 99 | |
| 100 | if (s[1] != '{') |
| 101 | return NULL; |
| 102 | |
| 103 | while (s[i] != '}') { |
| 104 | if (s[i] == 0) |
| 105 | return NULL; |
| 106 | i++; |
| 107 | } |
| 108 | s[i] = 0; |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 109 | return env_get((const char *)&s[2]); |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 110 | } else { |
| 111 | return s; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static int stringcomp(char *s, char *t, int op) |
| 116 | { |
Wolfgang Denk | cc22b79 | 2011-02-08 16:56:05 +0100 | [diff] [blame] | 117 | int p; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 118 | char *l, *r; |
| 119 | |
| 120 | l = evalstr(s); |
| 121 | r = evalstr(t); |
| 122 | |
Wolfgang Denk | cc22b79 | 2011-02-08 16:56:05 +0100 | [diff] [blame] | 123 | p = strcmp(l, r); |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 124 | switch (op) { |
| 125 | case EQ: return (p == 0); |
| 126 | case NE: return (p != 0); |
| 127 | case LT: return (p < 0); |
| 128 | case GT: return (p > 0); |
| 129 | case LE: return (p <= 0); |
| 130 | case GE: return (p >= 0); |
| 131 | } |
| 132 | return (0); |
| 133 | } |
| 134 | |
| 135 | static int arithcomp (char *s, char *t, int op, int w) |
| 136 | { |
| 137 | long l, r; |
| 138 | |
| 139 | l = evalexp (s, w); |
| 140 | r = evalexp (t, w); |
| 141 | |
| 142 | switch (op) { |
| 143 | case EQ: return (l == r); |
| 144 | case NE: return (l != r); |
| 145 | case LT: return (l < r); |
| 146 | case GT: return (l > r); |
| 147 | case LE: return (l <= r); |
| 148 | case GE: return (l >= r); |
| 149 | } |
| 150 | return (0); |
| 151 | } |
| 152 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 153 | static int binary_test(char *op, char *arg1, char *arg2, int w) |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 154 | { |
| 155 | int len, i; |
Mike Frysinger | fc9903f | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 156 | const op_tbl_t *optp; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 157 | |
| 158 | len = strlen(op); |
| 159 | |
| 160 | for (optp = (op_tbl_t *)&op_table, i = 0; |
Mike Frysinger | fc9903f | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 161 | i < ARRAY_SIZE(op_table); |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 162 | optp++, i++) { |
| 163 | |
| 164 | if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) { |
| 165 | if (w == 0) { |
| 166 | return (stringcomp(arg1, arg2, optp->opcode)); |
| 167 | } else { |
| 168 | return (arithcomp (arg1, arg2, optp->opcode, w)); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | printf("Unknown operator '%s'\n", op); |
| 174 | return 0; /* op code not found */ |
| 175 | } |
| 176 | |
| 177 | /* command line interface to the shell test */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 178 | static int do_itest(struct cmd_tbl *cmdtp, int flag, int argc, |
| 179 | char *const argv[]) |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 180 | { |
wdenk | cd0a9de | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 181 | int value, w; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 182 | |
wdenk | cd0a9de | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 183 | /* Validate arguments */ |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 184 | if ((argc != 4)) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 185 | return CMD_RET_USAGE; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 186 | |
| 187 | /* Check for a data width specification. |
| 188 | * Defaults to long (4) if no specification. |
| 189 | * Uses -2 as 'width' for .s (string) so as not to upset existing code |
| 190 | */ |
| 191 | switch (w = cmd_get_data_size(argv[0], 4)) { |
| 192 | case 1: |
| 193 | case 2: |
| 194 | case 4: |
Marek Szyprowski | 7d2dc6a | 2019-10-02 12:24:46 +0200 | [diff] [blame] | 195 | #ifdef CONFIG_PHYS_64BIT |
| 196 | case 8: |
| 197 | #endif |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 198 | value = binary_test (argv[2], argv[1], argv[3], w); |
| 199 | break; |
Simon Glass | 7526dee | 2020-11-01 14:15:36 -0700 | [diff] [blame] | 200 | case CMD_DATA_SIZE_STR: |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 201 | value = binary_test (argv[2], argv[1], argv[3], 0); |
| 202 | break; |
Simon Glass | 7526dee | 2020-11-01 14:15:36 -0700 | [diff] [blame] | 203 | case CMD_DATA_SIZE_ERR: |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 204 | default: |
| 205 | puts("Invalid data width specifier\n"); |
| 206 | value = 0; |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | return !value; |
| 211 | } |
| 212 | |
| 213 | U_BOOT_CMD( |
| 214 | itest, 4, 0, do_itest, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 215 | "return true/false on integer compare", |
Marek Szyprowski | 7d2dc6a | 2019-10-02 12:24:46 +0200 | [diff] [blame] | 216 | #ifdef CONFIG_PHYS_64BIT |
| 217 | "[.b, .w, .l, .q, .s] [*]value1 <op> [*]value2" |
| 218 | #else |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 219 | "[.b, .w, .l, .s] [*]value1 <op> [*]value2" |
Marek Szyprowski | 7d2dc6a | 2019-10-02 12:24:46 +0200 | [diff] [blame] | 220 | #endif |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 221 | ); |