itest.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2003
  4. * Tait Electronics Limited, Christchurch, New Zealand
  5. */
  6. /*
  7. * This file provides a shell like 'test' function to return
  8. * true/false from an integer or string compare of two memory
  9. * locations or a location and a scalar/literal.
  10. * A few parts were lifted from bash 'test' command
  11. */
  12. #include <common.h>
  13. #include <config.h>
  14. #include <command.h>
  15. #include <env.h>
  16. #include <mapmem.h>
  17. #include <asm/io.h>
  18. #define EQ 0
  19. #define NE 1
  20. #define LT 2
  21. #define GT 3
  22. #define LE 4
  23. #define GE 5
  24. struct op_tbl_s {
  25. char *op; /* operator string */
  26. int opcode; /* internal representation of opcode */
  27. };
  28. typedef struct op_tbl_s op_tbl_t;
  29. static const op_tbl_t op_table [] = {
  30. { "-lt", LT },
  31. { "<" , LT },
  32. { "-gt", GT },
  33. { ">" , GT },
  34. { "-eq", EQ },
  35. { "==" , EQ },
  36. { "-ne", NE },
  37. { "!=" , NE },
  38. { "<>" , NE },
  39. { "-ge", GE },
  40. { ">=" , GE },
  41. { "-le", LE },
  42. { "<=" , LE },
  43. };
  44. static long evalexp(char *s, int w)
  45. {
  46. long l = 0;
  47. unsigned long addr;
  48. void *buf;
  49. /* if the parameter starts with a * then assume is a pointer to the value we want */
  50. if (s[0] == '*') {
  51. addr = hextoul(&s[1], NULL);
  52. buf = map_physmem(addr, w, MAP_WRBACK);
  53. if (!buf && addr) {
  54. puts("Failed to map physical memory\n");
  55. return 0;
  56. }
  57. switch (w) {
  58. case 1:
  59. l = (long)(*(u8 *)buf);
  60. break;
  61. case 2:
  62. l = (long)(*(u16 *)buf);
  63. break;
  64. case 4:
  65. l = (long)(*(u32 *)buf);
  66. break;
  67. #ifdef CONFIG_PHYS_64BIT
  68. case 8:
  69. l = (long)(*(unsigned long *)buf);
  70. break;
  71. #endif
  72. }
  73. unmap_physmem(buf, w);
  74. return l;
  75. } else {
  76. l = hextoul(s, NULL);
  77. }
  78. /* avoid overflow on mask calculus */
  79. return (w >= sizeof(long)) ? l : (l & ((1UL << (w * 8)) - 1));
  80. }
  81. static char * evalstr(char *s)
  82. {
  83. /* if the parameter starts with a * then assume a string pointer else its a literal */
  84. if (s[0] == '*') {
  85. return (char *)hextoul(&s[1], NULL);
  86. } else if (s[0] == '$') {
  87. int i = 2;
  88. if (s[1] != '{')
  89. return NULL;
  90. while (s[i] != '}') {
  91. if (s[i] == 0)
  92. return NULL;
  93. i++;
  94. }
  95. s[i] = 0;
  96. return env_get((const char *)&s[2]);
  97. } else {
  98. return s;
  99. }
  100. }
  101. static int stringcomp(char *s, char *t, int op)
  102. {
  103. int p;
  104. char *l, *r;
  105. l = evalstr(s);
  106. r = evalstr(t);
  107. p = strcmp(l, r);
  108. switch (op) {
  109. case EQ: return (p == 0);
  110. case NE: return (p != 0);
  111. case LT: return (p < 0);
  112. case GT: return (p > 0);
  113. case LE: return (p <= 0);
  114. case GE: return (p >= 0);
  115. }
  116. return (0);
  117. }
  118. static int arithcomp (char *s, char *t, int op, int w)
  119. {
  120. long l, r;
  121. l = evalexp (s, w);
  122. r = evalexp (t, w);
  123. switch (op) {
  124. case EQ: return (l == r);
  125. case NE: return (l != r);
  126. case LT: return (l < r);
  127. case GT: return (l > r);
  128. case LE: return (l <= r);
  129. case GE: return (l >= r);
  130. }
  131. return (0);
  132. }
  133. static int binary_test(char *op, char *arg1, char *arg2, int w)
  134. {
  135. int len, i;
  136. const op_tbl_t *optp;
  137. len = strlen(op);
  138. for (optp = (op_tbl_t *)&op_table, i = 0;
  139. i < ARRAY_SIZE(op_table);
  140. optp++, i++) {
  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. printf("Unknown operator '%s'\n", op);
  150. return 0; /* op code not found */
  151. }
  152. /* command line interface to the shell test */
  153. static int do_itest(struct cmd_tbl *cmdtp, int flag, int argc,
  154. char *const argv[])
  155. {
  156. int value, w;
  157. /* Validate arguments */
  158. if ((argc != 4))
  159. return CMD_RET_USAGE;
  160. /* Check for a data width specification.
  161. * Defaults to long (4) if no specification.
  162. * Uses -2 as 'width' for .s (string) so as not to upset existing code
  163. */
  164. switch (w = cmd_get_data_size(argv[0], 4)) {
  165. case 1:
  166. case 2:
  167. case 4:
  168. #ifdef CONFIG_PHYS_64BIT
  169. case 8:
  170. #endif
  171. value = binary_test (argv[2], argv[1], argv[3], w);
  172. break;
  173. case CMD_DATA_SIZE_STR:
  174. value = binary_test (argv[2], argv[1], argv[3], 0);
  175. break;
  176. case CMD_DATA_SIZE_ERR:
  177. default:
  178. puts("Invalid data width specifier\n");
  179. value = 0;
  180. break;
  181. }
  182. return !value;
  183. }
  184. U_BOOT_CMD(
  185. itest, 4, 0, do_itest,
  186. "return true/false on integer compare",
  187. #ifdef CONFIG_PHYS_64BIT
  188. "[.b, .w, .l, .q, .s] [*]value1 <op> [*]value2"
  189. #else
  190. "[.b, .w, .l, .s] [*]value1 <op> [*]value2"
  191. #endif
  192. );