mem_search.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Tests for memory commands
  4. *
  5. * Copyright 2020 Google LLC
  6. * Written by Simon Glass <sjg@chromium.org>
  7. */
  8. #include <common.h>
  9. #include <console.h>
  10. #include <mapmem.h>
  11. #include <dm/test.h>
  12. #include <test/ut.h>
  13. #define BUF_SIZE 0x100
  14. /* Test 'ms' command with bytes */
  15. static int dm_test_ms_b(struct unit_test_state *uts)
  16. {
  17. u8 *buf;
  18. buf = map_sysmem(0, BUF_SIZE + 1);
  19. memset(buf, '\0', BUF_SIZE);
  20. buf[0x0] = 0x12;
  21. buf[0x31] = 0x12;
  22. buf[0xff] = 0x12;
  23. buf[0x100] = 0x12;
  24. console_record_reset();
  25. run_command("ms.b 1 ff 12", 0);
  26. ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................");
  27. ut_assert_nextline("--");
  28. ut_assert_nextline("000000f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 ................");
  29. ut_assert_nextline("2 matches");
  30. ut_assert_console_end();
  31. ut_asserteq(2, env_get_hex("memmatches", 0));
  32. ut_asserteq(0xff, env_get_hex("memaddr", 0));
  33. ut_asserteq(0xfe, env_get_hex("mempos", 0));
  34. unmap_sysmem(buf);
  35. return 0;
  36. }
  37. DM_TEST(dm_test_ms_b, 0);
  38. /* Test 'ms' command with 16-bit values */
  39. static int dm_test_ms_w(struct unit_test_state *uts)
  40. {
  41. u16 *buf;
  42. buf = map_sysmem(0, BUF_SIZE + 2);
  43. memset(buf, '\0', BUF_SIZE);
  44. buf[0x34 / 2] = 0x1234;
  45. buf[BUF_SIZE / 2] = 0x1234;
  46. console_record_reset();
  47. run_command("ms.w 0 80 1234", 0);
  48. ut_assert_nextline("00000030: 0000 0000 1234 0000 0000 0000 0000 0000 ....4...........");
  49. ut_assert_nextline("1 match");
  50. ut_assert_console_end();
  51. ut_asserteq(1, env_get_hex("memmatches", 0));
  52. ut_asserteq(0x34, env_get_hex("memaddr", 0));
  53. ut_asserteq(0x34 / 2, env_get_hex("mempos", 0));
  54. unmap_sysmem(buf);
  55. return 0;
  56. }
  57. DM_TEST(dm_test_ms_w, 0);
  58. /* Test 'ms' command with 32-bit values */
  59. static int dm_test_ms_l(struct unit_test_state *uts)
  60. {
  61. u32 *buf;
  62. buf = map_sysmem(0, BUF_SIZE + 4);
  63. memset(buf, '\0', BUF_SIZE);
  64. buf[0x38 / 4] = 0x12345678;
  65. buf[BUF_SIZE / 4] = 0x12345678;
  66. console_record_reset();
  67. run_command("ms 0 40 12345678", 0);
  68. ut_assert_nextline("00000030: 00000000 00000000 12345678 00000000 ........xV4.....");
  69. ut_assert_nextline("1 match");
  70. ut_assert_console_end();
  71. ut_asserteq(1, env_get_hex("memmatches", 0));
  72. ut_asserteq(0x38, env_get_hex("memaddr", 0));
  73. ut_asserteq(0x38 / 4, env_get_hex("mempos", 0));
  74. console_record_reset();
  75. run_command("ms 0 80 12345679", 0);
  76. ut_assert_nextline("0 matches");
  77. ut_assert_console_end();
  78. ut_asserteq(0, env_get_hex("memmatches", 0));
  79. ut_asserteq(0, env_get_hex("memaddr", 0));
  80. ut_asserteq(0 / 4, env_get_hex("mempos", 0));
  81. unmap_sysmem(buf);
  82. return 0;
  83. }
  84. DM_TEST(dm_test_ms_l, 0);
  85. /* Test 'ms' command with continuation */
  86. static int dm_test_ms_cont(struct unit_test_state *uts)
  87. {
  88. char *const args[] = {"ms.b", "0", "100", "34"};
  89. int repeatable;
  90. u8 *buf;
  91. int i;
  92. buf = map_sysmem(0, BUF_SIZE);
  93. memset(buf, '\0', BUF_SIZE);
  94. for (i = 5; i < 0x33; i += 3)
  95. buf[i] = 0x34;
  96. console_record_reset();
  97. run_command("ms.b 0 100 34", 0);
  98. ut_assert_nextlinen("00000000: 00 00 00 00 00 34 00 00 34 00 00 34 00 00 34 00");
  99. ut_assert_nextline("--");
  100. ut_assert_nextlinen("00000010: 00 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00");
  101. ut_assert_nextline("--");
  102. ut_assert_nextlinen("00000020: 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00 34");
  103. ut_assert_nextlinen("10 matches (repeat command to check for more)");
  104. ut_assert_console_end();
  105. ut_asserteq(10, env_get_hex("memmatches", 0));
  106. ut_asserteq(0x20, env_get_hex("memaddr", 0));
  107. ut_asserteq(0x20, env_get_hex("mempos", 0));
  108. /*
  109. * run_command() ignoes the repeatable flag when using hush, so call
  110. * cmd_process() directly
  111. */
  112. console_record_reset();
  113. cmd_process(CMD_FLAG_REPEAT, 4, args, &repeatable, NULL);
  114. ut_assert_nextlinen("00000020: 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00 34");
  115. ut_assert_nextline("--");
  116. ut_assert_nextlinen("00000030: 00 00 34 00 00 00 00 00");
  117. ut_assert_nextlinen("6 matches");
  118. ut_assert_console_end();
  119. ut_asserteq(6, env_get_hex("memmatches", 0));
  120. ut_asserteq(0x32, env_get_hex("memaddr", 0));
  121. /* 0x32 less 0x21, where the second search started */
  122. ut_asserteq(0x11, env_get_hex("mempos", 0));
  123. unmap_sysmem(buf);
  124. return 0;
  125. }
  126. DM_TEST(dm_test_ms_cont, 0);
  127. /* Test 'ms' command with multiple values */
  128. static int dm_test_ms_mult(struct unit_test_state *uts)
  129. {
  130. static const char str[] = "hello";
  131. char *buf;
  132. buf = map_sysmem(0, BUF_SIZE + 5);
  133. memset(buf, '\0', BUF_SIZE);
  134. strcpy(buf + 0x1e, str);
  135. strcpy(buf + 0x63, str);
  136. strcpy(buf + BUF_SIZE - strlen(str) + 1, str);
  137. console_record_reset();
  138. run_command("ms.b 0 100 68 65 6c 6c 6f", 0);
  139. ut_assert_nextline("00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 68 65 ..............he");
  140. ut_assert_nextline("00000020: 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 llo.............");
  141. ut_assert_nextline("--");
  142. ut_assert_nextline("00000060: 00 00 00 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 ...hello........");
  143. ut_assert_nextline("2 matches");
  144. ut_assert_console_end();
  145. unmap_sysmem(buf);
  146. ut_asserteq(2, env_get_hex("memmatches", 0));
  147. ut_asserteq(0x63, env_get_hex("memaddr", 0));
  148. ut_asserteq(0x63, env_get_hex("mempos", 0));
  149. return 0;
  150. }
  151. DM_TEST(dm_test_ms_mult, 0);
  152. /* Test 'ms' command with string */
  153. static int dm_test_ms_s(struct unit_test_state *uts)
  154. {
  155. static const char str[] = "hello";
  156. static const char str2[] = "hellothere";
  157. char *buf;
  158. buf = map_sysmem(0, BUF_SIZE);
  159. memset(buf, '\0', BUF_SIZE);
  160. strcpy(buf + 0x1e, str);
  161. strcpy(buf + 0x63, str);
  162. strcpy(buf + 0xa1, str2);
  163. console_record_reset();
  164. run_command("ms.s 0 100 hello", 0);
  165. ut_assert_nextline("00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 68 65 ..............he");
  166. ut_assert_nextline("00000020: 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 llo.............");
  167. ut_assert_nextline("--");
  168. ut_assert_nextline("00000060: 00 00 00 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 ...hello........");
  169. ut_assert_nextline("--");
  170. ut_assert_nextline("000000a0: 00 68 65 6c 6c 6f 74 68 65 72 65 00 00 00 00 00 .hellothere.....");
  171. ut_assert_nextline("3 matches");
  172. ut_assert_console_end();
  173. ut_asserteq(3, env_get_hex("memmatches", 0));
  174. ut_asserteq(0xa1, env_get_hex("memaddr", 0));
  175. ut_asserteq(0xa1, env_get_hex("mempos", 0));
  176. console_record_reset();
  177. run_command("ms.s 0 100 hello there", 0);
  178. ut_assert_nextline("000000a0: 00 68 65 6c 6c 6f 74 68 65 72 65 00 00 00 00 00 .hellothere.....");
  179. ut_assert_nextline("1 match");
  180. ut_assert_console_end();
  181. ut_asserteq(1, env_get_hex("memmatches", 0));
  182. ut_asserteq(0xa1, env_get_hex("memaddr", 0));
  183. ut_asserteq(0xa1, env_get_hex("mempos", 0));
  184. unmap_sysmem(buf);
  185. return 0;
  186. }
  187. DM_TEST(dm_test_ms_s, 0);
  188. /* Test 'ms' command with limit */
  189. static int dm_test_ms_limit(struct unit_test_state *uts)
  190. {
  191. u8 *buf;
  192. buf = map_sysmem(0, BUF_SIZE + 1);
  193. memset(buf, '\0', BUF_SIZE);
  194. buf[0x0] = 0x12;
  195. buf[0x31] = 0x12;
  196. buf[0x62] = 0x12;
  197. buf[0x76] = 0x12;
  198. console_record_reset();
  199. run_command("ms.b -l2 1 ff 12", 0);
  200. ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................");
  201. ut_assert_nextline("--");
  202. ut_assert_nextlinen("00000060: 00 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00");
  203. ut_assert_nextline("2 matches (repeat command to check for more)");
  204. ut_assert_console_end();
  205. ut_asserteq(2, env_get_hex("memmatches", 0));
  206. ut_asserteq(0x62, env_get_hex("memaddr", 0));
  207. ut_asserteq(0x61, env_get_hex("mempos", 0));
  208. unmap_sysmem(buf);
  209. return 0;
  210. }
  211. DM_TEST(dm_test_ms_limit, 0);
  212. /* Test 'ms' command in quiet mode */
  213. static int dm_test_ms_quiet(struct unit_test_state *uts)
  214. {
  215. u8 *buf;
  216. buf = map_sysmem(0, BUF_SIZE + 1);
  217. memset(buf, '\0', BUF_SIZE);
  218. buf[0x0] = 0x12;
  219. buf[0x31] = 0x12;
  220. buf[0x62] = 0x12;
  221. buf[0x76] = 0x12;
  222. console_record_reset();
  223. run_command("ms.b -l2 1 ff 12", 0);
  224. ut_assert_console_end();
  225. unmap_sysmem(buf);
  226. return 0;
  227. }
  228. DM_TEST(dm_test_ms_quiet, 0);