cmd_itest.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 & ((1 << (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 {
  65. return s;
  66. }
  67. }
  68. static int stringcomp(char *s, char *t, int op)
  69. {
  70. int p;
  71. char *l, *r;
  72. l = evalstr(s);
  73. r = evalstr(t);
  74. p = strcmp(l, r);
  75. switch (op) {
  76. case EQ: return (p == 0);
  77. case NE: return (p != 0);
  78. case LT: return (p < 0);
  79. case GT: return (p > 0);
  80. case LE: return (p <= 0);
  81. case GE: return (p >= 0);
  82. }
  83. return (0);
  84. }
  85. static int arithcomp (char *s, char *t, int op, int w)
  86. {
  87. long l, r;
  88. l = evalexp (s, w);
  89. r = evalexp (t, w);
  90. switch (op) {
  91. case EQ: return (l == r);
  92. case NE: return (l != r);
  93. case LT: return (l < r);
  94. case GT: return (l > r);
  95. case LE: return (l <= r);
  96. case GE: return (l >= r);
  97. }
  98. return (0);
  99. }
  100. static int binary_test(char *op, char *arg1, char *arg2, int w)
  101. {
  102. int len, i;
  103. const op_tbl_t *optp;
  104. len = strlen(op);
  105. for (optp = (op_tbl_t *)&op_table, i = 0;
  106. i < ARRAY_SIZE(op_table);
  107. optp++, i++) {
  108. if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
  109. if (w == 0) {
  110. return (stringcomp(arg1, arg2, optp->opcode));
  111. } else {
  112. return (arithcomp (arg1, arg2, optp->opcode, w));
  113. }
  114. }
  115. }
  116. printf("Unknown operator '%s'\n", op);
  117. return 0; /* op code not found */
  118. }
  119. /* command line interface to the shell test */
  120. static int do_itest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  121. {
  122. int value, w;
  123. /* Validate arguments */
  124. if ((argc != 4))
  125. return CMD_RET_USAGE;
  126. /* Check for a data width specification.
  127. * Defaults to long (4) if no specification.
  128. * Uses -2 as 'width' for .s (string) so as not to upset existing code
  129. */
  130. switch (w = cmd_get_data_size(argv[0], 4)) {
  131. case 1:
  132. case 2:
  133. case 4:
  134. value = binary_test (argv[2], argv[1], argv[3], w);
  135. break;
  136. case -2:
  137. value = binary_test (argv[2], argv[1], argv[3], 0);
  138. break;
  139. case -1:
  140. default:
  141. puts("Invalid data width specifier\n");
  142. value = 0;
  143. break;
  144. }
  145. return !value;
  146. }
  147. U_BOOT_CMD(
  148. itest, 4, 0, do_itest,
  149. "return true/false on integer compare",
  150. "[.b, .w, .l, .s] [*]value1 <op> [*]value2"
  151. );