itest.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * (C) Copyright 2003
  3. * Tait Electronics Limited, Christchurch, New Zealand
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  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. #include <common.h>
  14. #include <config.h>
  15. #include <command.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. return 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 getenv((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. );