cmd_itest.c 3.9 KB

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