wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Tait Electronics Limited, Christchurch, New Zealand |
| 4 | * |
Wolfgang Denk | 3765b3e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 2d1a537 | 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 | fc9903f | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 33 | static const op_tbl_t op_table [] = { |
wdenk | 2d1a537 | 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 | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 49 | static long evalexp(char *s, int w) |
| 50 | { |
Frans Meulenbroeks | f365176 | 2010-02-22 22:49:06 +0100 | [diff] [blame] | 51 | long l = 0; |
| 52 | long *p; |
wdenk | 2d1a537 | 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 | f365176 | 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 | 2d1a537 | 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); |
| 74 | } else { |
| 75 | return s; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static int stringcomp(char *s, char *t, int op) |
| 80 | { |
Wolfgang Denk | cc22b79 | 2011-02-08 16:56:05 +0100 | [diff] [blame] | 81 | int p; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 82 | char *l, *r; |
| 83 | |
| 84 | l = evalstr(s); |
| 85 | r = evalstr(t); |
| 86 | |
Wolfgang Denk | cc22b79 | 2011-02-08 16:56:05 +0100 | [diff] [blame] | 87 | p = strcmp(l, r); |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 88 | 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 | |
| 99 | static 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 Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 117 | static int binary_test(char *op, char *arg1, char *arg2, int w) |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 118 | { |
| 119 | int len, i; |
Mike Frysinger | fc9903f | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 120 | const op_tbl_t *optp; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 121 | |
| 122 | len = strlen(op); |
| 123 | |
| 124 | for (optp = (op_tbl_t *)&op_table, i = 0; |
Mike Frysinger | fc9903f | 2010-10-20 07:17:33 -0400 | [diff] [blame] | 125 | i < ARRAY_SIZE(op_table); |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 126 | 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 Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 142 | 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] | 143 | { |
wdenk | cd0a9de | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 144 | int value, w; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 145 | |
wdenk | cd0a9de | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 146 | /* Validate arguments */ |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 147 | if ((argc != 4)) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 148 | return CMD_RET_USAGE; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 149 | |
| 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 | |
| 173 | U_BOOT_CMD( |
| 174 | itest, 4, 0, do_itest, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 175 | "return true/false on integer compare", |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 176 | "[.b, .w, .l, .s] [*]value1 <op> [*]value2" |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 177 | ); |