itest.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 = simple_strtoul(&s[1], NULL, 16);
  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. }
  68. unmap_physmem(buf, w);
  69. return l;
  70. } else {
  71. l = simple_strtoul(s, NULL, 16);
  72. }
  73. /* avoid overflow on mask calculus */
  74. return (w >= sizeof(long)) ? l : (l & ((1UL << (w * 8)) - 1));
  75. }
  76. static char * evalstr(char *s)
  77. {
  78. /* if the parameter starts with a * then assume a string pointer else its a literal */
  79. if (s[0] == '*') {
  80. return (char *)simple_strtoul(&s[1], NULL, 16);
  81. } else if (s[0] == '$') {
  82. int i = 2;
  83. if (s[1] != '{')
  84. return NULL;
  85. while (s[i] != '}') {
  86. if (s[i] == 0)
  87. return NULL;
  88. i++;
  89. }
  90. s[i] = 0;
  91. return env_get((const char *)&s[2]);
  92. } else {
  93. return s;
  94. }
  95. }
  96. static int stringcomp(char *s, char *t, int op)
  97. {
  98. int p;
  99. char *l, *r;
  100. l = evalstr(s);
  101. r = evalstr(t);
  102. p = strcmp(l, r);
  103. switch (op) {
  104. case EQ: return (p == 0);
  105. case NE: return (p != 0);
  106. case LT: return (p < 0);
  107. case GT: return (p > 0);
  108. case LE: return (p <= 0);
  109. case GE: return (p >= 0);
  110. }
  111. return (0);
  112. }
  113. static int arithcomp (char *s, char *t, int op, int w)
  114. {
  115. long l, r;
  116. l = evalexp (s, w);
  117. r = evalexp (t, w);
  118. switch (op) {
  119. case EQ: return (l == r);
  120. case NE: return (l != r);
  121. case LT: return (l < r);
  122. case GT: return (l > r);
  123. case LE: return (l <= r);
  124. case GE: return (l >= r);
  125. }
  126. return (0);
  127. }
  128. static int binary_test(char *op, char *arg1, char *arg2, int w)
  129. {
  130. int len, i;
  131. const op_tbl_t *optp;
  132. len = strlen(op);
  133. for (optp = (op_tbl_t *)&op_table, i = 0;
  134. i < ARRAY_SIZE(op_table);
  135. optp++, i++) {
  136. if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
  137. if (w == 0) {
  138. return (stringcomp(arg1, arg2, optp->opcode));
  139. } else {
  140. return (arithcomp (arg1, arg2, optp->opcode, w));
  141. }
  142. }
  143. }
  144. printf("Unknown operator '%s'\n", op);
  145. return 0; /* op code not found */
  146. }
  147. /* command line interface to the shell test */
  148. static int do_itest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  149. {
  150. int value, w;
  151. /* Validate arguments */
  152. if ((argc != 4))
  153. return CMD_RET_USAGE;
  154. /* Check for a data width specification.
  155. * Defaults to long (4) if no specification.
  156. * Uses -2 as 'width' for .s (string) so as not to upset existing code
  157. */
  158. switch (w = cmd_get_data_size(argv[0], 4)) {
  159. case 1:
  160. case 2:
  161. case 4:
  162. value = binary_test (argv[2], argv[1], argv[3], w);
  163. break;
  164. case -2:
  165. value = binary_test (argv[2], argv[1], argv[3], 0);
  166. break;
  167. case -1:
  168. default:
  169. puts("Invalid data width specifier\n");
  170. value = 0;
  171. break;
  172. }
  173. return !value;
  174. }
  175. U_BOOT_CMD(
  176. itest, 4, 0, do_itest,
  177. "return true/false on integer compare",
  178. "[.b, .w, .l, .s] [*]value1 <op> [*]value2"
  179. );