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