Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 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 | |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 9 | #include <console.h> |
| 10 | #include <mapmem.h> |
| 11 | #include <dm/test.h> |
| 12 | #include <test/ut.h> |
| 13 | |
| 14 | #define BUF_SIZE 0x100 |
| 15 | |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 16 | /* Declare a new mem test */ |
| 17 | #define MEM_TEST(_name, _flags) UNIT_TEST(_name, _flags, mem_test) |
| 18 | |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 19 | /* Test 'ms' command with bytes */ |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 20 | static int mem_test_ms_b(struct unit_test_state *uts) |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 21 | { |
| 22 | u8 *buf; |
| 23 | |
| 24 | buf = map_sysmem(0, BUF_SIZE + 1); |
| 25 | memset(buf, '\0', BUF_SIZE); |
| 26 | buf[0x0] = 0x12; |
| 27 | buf[0x31] = 0x12; |
| 28 | buf[0xff] = 0x12; |
| 29 | buf[0x100] = 0x12; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 30 | run_command("ms.b 1 ff 12", 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 31 | ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................"); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 32 | ut_assert_nextline("--"); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 33 | ut_assert_nextline("000000f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 ................"); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 34 | ut_assert_nextline("2 matches"); |
| 35 | ut_assert_console_end(); |
| 36 | |
| 37 | ut_asserteq(2, env_get_hex("memmatches", 0)); |
| 38 | ut_asserteq(0xff, env_get_hex("memaddr", 0)); |
| 39 | ut_asserteq(0xfe, env_get_hex("mempos", 0)); |
| 40 | |
| 41 | unmap_sysmem(buf); |
| 42 | |
| 43 | return 0; |
| 44 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 45 | MEM_TEST(mem_test_ms_b, UTF_CONSOLE); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 46 | |
| 47 | /* Test 'ms' command with 16-bit values */ |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 48 | static int mem_test_ms_w(struct unit_test_state *uts) |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 49 | { |
| 50 | u16 *buf; |
| 51 | |
| 52 | buf = map_sysmem(0, BUF_SIZE + 2); |
| 53 | memset(buf, '\0', BUF_SIZE); |
| 54 | buf[0x34 / 2] = 0x1234; |
| 55 | buf[BUF_SIZE / 2] = 0x1234; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 56 | run_command("ms.w 0 80 1234", 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 57 | ut_assert_nextline("00000030: 0000 0000 1234 0000 0000 0000 0000 0000 ....4..........."); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 58 | ut_assert_nextline("1 match"); |
| 59 | ut_assert_console_end(); |
| 60 | |
| 61 | ut_asserteq(1, env_get_hex("memmatches", 0)); |
| 62 | ut_asserteq(0x34, env_get_hex("memaddr", 0)); |
| 63 | ut_asserteq(0x34 / 2, env_get_hex("mempos", 0)); |
| 64 | |
| 65 | unmap_sysmem(buf); |
| 66 | |
| 67 | return 0; |
| 68 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 69 | MEM_TEST(mem_test_ms_w, UTF_CONSOLE); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 70 | |
| 71 | /* Test 'ms' command with 32-bit values */ |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 72 | static int mem_test_ms_l(struct unit_test_state *uts) |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 73 | { |
| 74 | u32 *buf; |
| 75 | |
| 76 | buf = map_sysmem(0, BUF_SIZE + 4); |
| 77 | memset(buf, '\0', BUF_SIZE); |
| 78 | buf[0x38 / 4] = 0x12345678; |
| 79 | buf[BUF_SIZE / 4] = 0x12345678; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 80 | run_command("ms 0 40 12345678", 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 81 | ut_assert_nextline("00000030: 00000000 00000000 12345678 00000000 ........xV4....."); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 82 | ut_assert_nextline("1 match"); |
| 83 | ut_assert_console_end(); |
| 84 | |
| 85 | ut_asserteq(1, env_get_hex("memmatches", 0)); |
| 86 | ut_asserteq(0x38, env_get_hex("memaddr", 0)); |
| 87 | ut_asserteq(0x38 / 4, env_get_hex("mempos", 0)); |
| 88 | |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 89 | run_command("ms 0 80 12345679", 0); |
| 90 | ut_assert_nextline("0 matches"); |
| 91 | ut_assert_console_end(); |
| 92 | |
| 93 | ut_asserteq(0, env_get_hex("memmatches", 0)); |
| 94 | ut_asserteq(0, env_get_hex("memaddr", 0)); |
| 95 | ut_asserteq(0 / 4, env_get_hex("mempos", 0)); |
| 96 | |
| 97 | unmap_sysmem(buf); |
| 98 | |
| 99 | return 0; |
| 100 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 101 | MEM_TEST(mem_test_ms_l, UTF_CONSOLE); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 102 | |
| 103 | /* Test 'ms' command with continuation */ |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 104 | static int mem_test_ms_cont(struct unit_test_state *uts) |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 105 | { |
| 106 | char *const args[] = {"ms.b", "0", "100", "34"}; |
| 107 | int repeatable; |
| 108 | u8 *buf; |
| 109 | int i; |
| 110 | |
| 111 | buf = map_sysmem(0, BUF_SIZE); |
| 112 | memset(buf, '\0', BUF_SIZE); |
| 113 | for (i = 5; i < 0x33; i += 3) |
| 114 | buf[i] = 0x34; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 115 | run_command("ms.b 0 100 34", 0); |
| 116 | ut_assert_nextlinen("00000000: 00 00 00 00 00 34 00 00 34 00 00 34 00 00 34 00"); |
| 117 | ut_assert_nextline("--"); |
| 118 | ut_assert_nextlinen("00000010: 00 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00"); |
| 119 | ut_assert_nextline("--"); |
| 120 | ut_assert_nextlinen("00000020: 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00 34"); |
| 121 | ut_assert_nextlinen("10 matches (repeat command to check for more)"); |
| 122 | ut_assert_console_end(); |
| 123 | |
| 124 | ut_asserteq(10, env_get_hex("memmatches", 0)); |
| 125 | ut_asserteq(0x20, env_get_hex("memaddr", 0)); |
| 126 | ut_asserteq(0x20, env_get_hex("mempos", 0)); |
| 127 | |
| 128 | /* |
| 129 | * run_command() ignoes the repeatable flag when using hush, so call |
| 130 | * cmd_process() directly |
| 131 | */ |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 132 | cmd_process(CMD_FLAG_REPEAT, 4, args, &repeatable, NULL); |
| 133 | ut_assert_nextlinen("00000020: 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00 34"); |
| 134 | ut_assert_nextline("--"); |
| 135 | ut_assert_nextlinen("00000030: 00 00 34 00 00 00 00 00"); |
| 136 | ut_assert_nextlinen("6 matches"); |
| 137 | ut_assert_console_end(); |
| 138 | |
| 139 | ut_asserteq(6, env_get_hex("memmatches", 0)); |
| 140 | ut_asserteq(0x32, env_get_hex("memaddr", 0)); |
| 141 | |
| 142 | /* 0x32 less 0x21, where the second search started */ |
| 143 | ut_asserteq(0x11, env_get_hex("mempos", 0)); |
| 144 | |
| 145 | unmap_sysmem(buf); |
| 146 | |
| 147 | return 0; |
| 148 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 149 | MEM_TEST(mem_test_ms_cont, UTF_CONSOLE); |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 150 | |
| 151 | /* Test that an 'ms' command with continuation stops at the end of the range */ |
| 152 | static int mem_test_ms_cont_end(struct unit_test_state *uts) |
| 153 | { |
| 154 | char *const args[] = {"ms.b", "1", "ff", "12"}; |
| 155 | int repeatable; |
| 156 | u8 *buf; |
| 157 | |
| 158 | buf = map_sysmem(0, BUF_SIZE); |
| 159 | memset(buf, '\0', BUF_SIZE); |
| 160 | buf[0x0] = 0x12; |
| 161 | buf[0x31] = 0x12; |
| 162 | buf[0xff] = 0x12; |
| 163 | buf[0x100] = 0x12; |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 164 | run_command("ms.b 1 ff 12", 0); |
| 165 | ut_assert_nextlinen("00000030"); |
| 166 | ut_assert_nextlinen("--"); |
| 167 | ut_assert_nextlinen("000000f0"); |
| 168 | ut_assert_nextlinen("2 matches"); |
| 169 | ut_assert_console_end(); |
| 170 | |
| 171 | /* |
| 172 | * run_command() ignoes the repeatable flag when using hush, so call |
| 173 | * cmd_process() directly. |
| 174 | * |
| 175 | * This should produce no matches. |
| 176 | */ |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 177 | cmd_process(CMD_FLAG_REPEAT, 4, args, &repeatable, NULL); |
| 178 | ut_assert_nextlinen("0 matches"); |
| 179 | ut_assert_console_end(); |
| 180 | |
| 181 | /* One more time */ |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 182 | cmd_process(CMD_FLAG_REPEAT, 4, args, &repeatable, NULL); |
| 183 | ut_assert_nextlinen("0 matches"); |
| 184 | ut_assert_console_end(); |
| 185 | |
| 186 | unmap_sysmem(buf); |
| 187 | |
| 188 | return 0; |
| 189 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 190 | MEM_TEST(mem_test_ms_cont_end, UTF_CONSOLE); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 191 | |
| 192 | /* Test 'ms' command with multiple values */ |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 193 | static int mem_test_ms_mult(struct unit_test_state *uts) |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 194 | { |
| 195 | static const char str[] = "hello"; |
| 196 | char *buf; |
| 197 | |
| 198 | buf = map_sysmem(0, BUF_SIZE + 5); |
| 199 | memset(buf, '\0', BUF_SIZE); |
| 200 | strcpy(buf + 0x1e, str); |
| 201 | strcpy(buf + 0x63, str); |
| 202 | strcpy(buf + BUF_SIZE - strlen(str) + 1, str); |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 203 | ut_assertok(console_record_reset_enable()); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 204 | run_command("ms.b 0 100 68 65 6c 6c 6f", 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 205 | ut_assert_nextline("00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 68 65 ..............he"); |
| 206 | ut_assert_nextline("00000020: 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 llo............."); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 207 | ut_assert_nextline("--"); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 208 | ut_assert_nextline("00000060: 00 00 00 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 ...hello........"); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 209 | ut_assert_nextline("2 matches"); |
| 210 | ut_assert_console_end(); |
| 211 | unmap_sysmem(buf); |
| 212 | |
| 213 | ut_asserteq(2, env_get_hex("memmatches", 0)); |
| 214 | ut_asserteq(0x63, env_get_hex("memaddr", 0)); |
| 215 | ut_asserteq(0x63, env_get_hex("mempos", 0)); |
| 216 | |
| 217 | return 0; |
| 218 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 219 | MEM_TEST(mem_test_ms_mult, UTF_CONSOLE); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 220 | |
| 221 | /* Test 'ms' command with string */ |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 222 | static int mem_test_ms_s(struct unit_test_state *uts) |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 223 | { |
| 224 | static const char str[] = "hello"; |
| 225 | static const char str2[] = "hellothere"; |
| 226 | char *buf; |
| 227 | |
| 228 | buf = map_sysmem(0, BUF_SIZE); |
| 229 | memset(buf, '\0', BUF_SIZE); |
| 230 | strcpy(buf + 0x1e, str); |
| 231 | strcpy(buf + 0x63, str); |
| 232 | strcpy(buf + 0xa1, str2); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 233 | run_command("ms.s 0 100 hello", 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 234 | ut_assert_nextline("00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 68 65 ..............he"); |
| 235 | ut_assert_nextline("00000020: 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 llo............."); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 236 | ut_assert_nextline("--"); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 237 | ut_assert_nextline("00000060: 00 00 00 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 ...hello........"); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 238 | ut_assert_nextline("--"); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 239 | ut_assert_nextline("000000a0: 00 68 65 6c 6c 6f 74 68 65 72 65 00 00 00 00 00 .hellothere....."); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 240 | ut_assert_nextline("3 matches"); |
| 241 | ut_assert_console_end(); |
| 242 | |
| 243 | ut_asserteq(3, env_get_hex("memmatches", 0)); |
| 244 | ut_asserteq(0xa1, env_get_hex("memaddr", 0)); |
| 245 | ut_asserteq(0xa1, env_get_hex("mempos", 0)); |
| 246 | |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 247 | run_command("ms.s 0 100 hello there", 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 248 | ut_assert_nextline("000000a0: 00 68 65 6c 6c 6f 74 68 65 72 65 00 00 00 00 00 .hellothere....."); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 249 | ut_assert_nextline("1 match"); |
| 250 | ut_assert_console_end(); |
| 251 | |
| 252 | ut_asserteq(1, env_get_hex("memmatches", 0)); |
| 253 | ut_asserteq(0xa1, env_get_hex("memaddr", 0)); |
| 254 | ut_asserteq(0xa1, env_get_hex("mempos", 0)); |
| 255 | |
| 256 | unmap_sysmem(buf); |
| 257 | |
| 258 | return 0; |
| 259 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 260 | MEM_TEST(mem_test_ms_s, UTF_CONSOLE); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 261 | |
| 262 | /* Test 'ms' command with limit */ |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 263 | static int mem_test_ms_limit(struct unit_test_state *uts) |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 264 | { |
| 265 | u8 *buf; |
| 266 | |
| 267 | buf = map_sysmem(0, BUF_SIZE + 1); |
| 268 | memset(buf, '\0', BUF_SIZE); |
| 269 | buf[0x0] = 0x12; |
| 270 | buf[0x31] = 0x12; |
| 271 | buf[0x62] = 0x12; |
| 272 | buf[0x76] = 0x12; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 273 | run_command("ms.b -l2 1 ff 12", 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 274 | ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................"); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 275 | ut_assert_nextline("--"); |
| 276 | ut_assert_nextlinen("00000060: 00 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00"); |
| 277 | ut_assert_nextline("2 matches (repeat command to check for more)"); |
| 278 | ut_assert_console_end(); |
| 279 | |
| 280 | ut_asserteq(2, env_get_hex("memmatches", 0)); |
| 281 | ut_asserteq(0x62, env_get_hex("memaddr", 0)); |
| 282 | ut_asserteq(0x61, env_get_hex("mempos", 0)); |
| 283 | |
| 284 | unmap_sysmem(buf); |
| 285 | |
| 286 | return 0; |
| 287 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 288 | MEM_TEST(mem_test_ms_limit, UTF_CONSOLE); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 289 | |
| 290 | /* Test 'ms' command in quiet mode */ |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 291 | static int mem_test_ms_quiet(struct unit_test_state *uts) |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 292 | { |
| 293 | u8 *buf; |
| 294 | |
| 295 | buf = map_sysmem(0, BUF_SIZE + 1); |
| 296 | memset(buf, '\0', BUF_SIZE); |
| 297 | buf[0x0] = 0x12; |
| 298 | buf[0x31] = 0x12; |
| 299 | buf[0x62] = 0x12; |
| 300 | buf[0x76] = 0x12; |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 301 | run_command("ms.b -q -l2 1 ff 12", 0); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 302 | ut_assert_console_end(); |
| 303 | unmap_sysmem(buf); |
| 304 | |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 305 | ut_asserteq(2, env_get_hex("memmatches", 0)); |
| 306 | ut_asserteq(0x62, env_get_hex("memaddr", 0)); |
| 307 | ut_asserteq(0x61, env_get_hex("mempos", 0)); |
| 308 | |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 309 | return 0; |
| 310 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 311 | MEM_TEST(mem_test_ms_quiet, UTF_CONSOLE); |