cmd_itest.c 3.6 KB

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