itest.c 3.9 KB

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