blob: 55ad2fac1e340c9a9729078ae927fdaf359949f7 [file] [log] [blame]
Simon Glass19038de2020-06-02 19:26:49 -06001// 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 Glass19038de2020-06-02 19:26:49 -06009#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 Glass573c0fa2020-07-28 19:41:14 -060016/* Declare a new mem test */
17#define MEM_TEST(_name, _flags) UNIT_TEST(_name, _flags, mem_test)
18
Simon Glass19038de2020-06-02 19:26:49 -060019/* Test 'ms' command with bytes */
Simon Glass573c0fa2020-07-28 19:41:14 -060020static int mem_test_ms_b(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -060021{
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 Glass573c0fa2020-07-28 19:41:14 -060030 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -060031 run_command("ms.b 1 ff 12", 0);
Simon Glass85c8fc52021-05-08 07:00:00 -060032 ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................");
Simon Glass19038de2020-06-02 19:26:49 -060033 ut_assert_nextline("--");
Simon Glass85c8fc52021-05-08 07:00:00 -060034 ut_assert_nextline("000000f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 ................");
Simon Glass19038de2020-06-02 19:26:49 -060035 ut_assert_nextline("2 matches");
36 ut_assert_console_end();
37
38 ut_asserteq(2, env_get_hex("memmatches", 0));
39 ut_asserteq(0xff, env_get_hex("memaddr", 0));
40 ut_asserteq(0xfe, env_get_hex("mempos", 0));
41
42 unmap_sysmem(buf);
43
44 return 0;
45}
Simon Glass573c0fa2020-07-28 19:41:14 -060046MEM_TEST(mem_test_ms_b, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -060047
48/* Test 'ms' command with 16-bit values */
Simon Glass573c0fa2020-07-28 19:41:14 -060049static int mem_test_ms_w(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -060050{
51 u16 *buf;
52
53 buf = map_sysmem(0, BUF_SIZE + 2);
54 memset(buf, '\0', BUF_SIZE);
55 buf[0x34 / 2] = 0x1234;
56 buf[BUF_SIZE / 2] = 0x1234;
Simon Glass573c0fa2020-07-28 19:41:14 -060057 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -060058 run_command("ms.w 0 80 1234", 0);
Simon Glass85c8fc52021-05-08 07:00:00 -060059 ut_assert_nextline("00000030: 0000 0000 1234 0000 0000 0000 0000 0000 ....4...........");
Simon Glass19038de2020-06-02 19:26:49 -060060 ut_assert_nextline("1 match");
61 ut_assert_console_end();
62
63 ut_asserteq(1, env_get_hex("memmatches", 0));
64 ut_asserteq(0x34, env_get_hex("memaddr", 0));
65 ut_asserteq(0x34 / 2, env_get_hex("mempos", 0));
66
67 unmap_sysmem(buf);
68
69 return 0;
70}
Simon Glass573c0fa2020-07-28 19:41:14 -060071MEM_TEST(mem_test_ms_w, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -060072
73/* Test 'ms' command with 32-bit values */
Simon Glass573c0fa2020-07-28 19:41:14 -060074static int mem_test_ms_l(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -060075{
76 u32 *buf;
77
78 buf = map_sysmem(0, BUF_SIZE + 4);
79 memset(buf, '\0', BUF_SIZE);
80 buf[0x38 / 4] = 0x12345678;
81 buf[BUF_SIZE / 4] = 0x12345678;
Simon Glass573c0fa2020-07-28 19:41:14 -060082 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -060083 run_command("ms 0 40 12345678", 0);
Simon Glass85c8fc52021-05-08 07:00:00 -060084 ut_assert_nextline("00000030: 00000000 00000000 12345678 00000000 ........xV4.....");
Simon Glass19038de2020-06-02 19:26:49 -060085 ut_assert_nextline("1 match");
86 ut_assert_console_end();
87
88 ut_asserteq(1, env_get_hex("memmatches", 0));
89 ut_asserteq(0x38, env_get_hex("memaddr", 0));
90 ut_asserteq(0x38 / 4, env_get_hex("mempos", 0));
91
Simon Glass573c0fa2020-07-28 19:41:14 -060092 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -060093 run_command("ms 0 80 12345679", 0);
94 ut_assert_nextline("0 matches");
95 ut_assert_console_end();
96
97 ut_asserteq(0, env_get_hex("memmatches", 0));
98 ut_asserteq(0, env_get_hex("memaddr", 0));
99 ut_asserteq(0 / 4, env_get_hex("mempos", 0));
100
101 unmap_sysmem(buf);
102
103 return 0;
104}
Simon Glass573c0fa2020-07-28 19:41:14 -0600105MEM_TEST(mem_test_ms_l, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -0600106
107/* Test 'ms' command with continuation */
Simon Glass573c0fa2020-07-28 19:41:14 -0600108static int mem_test_ms_cont(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -0600109{
110 char *const args[] = {"ms.b", "0", "100", "34"};
111 int repeatable;
112 u8 *buf;
113 int i;
114
115 buf = map_sysmem(0, BUF_SIZE);
116 memset(buf, '\0', BUF_SIZE);
117 for (i = 5; i < 0x33; i += 3)
118 buf[i] = 0x34;
Simon Glass573c0fa2020-07-28 19:41:14 -0600119 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -0600120 run_command("ms.b 0 100 34", 0);
121 ut_assert_nextlinen("00000000: 00 00 00 00 00 34 00 00 34 00 00 34 00 00 34 00");
122 ut_assert_nextline("--");
123 ut_assert_nextlinen("00000010: 00 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00");
124 ut_assert_nextline("--");
125 ut_assert_nextlinen("00000020: 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00 34");
126 ut_assert_nextlinen("10 matches (repeat command to check for more)");
127 ut_assert_console_end();
128
129 ut_asserteq(10, env_get_hex("memmatches", 0));
130 ut_asserteq(0x20, env_get_hex("memaddr", 0));
131 ut_asserteq(0x20, env_get_hex("mempos", 0));
132
133 /*
134 * run_command() ignoes the repeatable flag when using hush, so call
135 * cmd_process() directly
136 */
Simon Glass573c0fa2020-07-28 19:41:14 -0600137 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -0600138 cmd_process(CMD_FLAG_REPEAT, 4, args, &repeatable, NULL);
139 ut_assert_nextlinen("00000020: 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00 34");
140 ut_assert_nextline("--");
141 ut_assert_nextlinen("00000030: 00 00 34 00 00 00 00 00");
142 ut_assert_nextlinen("6 matches");
143 ut_assert_console_end();
144
145 ut_asserteq(6, env_get_hex("memmatches", 0));
146 ut_asserteq(0x32, env_get_hex("memaddr", 0));
147
148 /* 0x32 less 0x21, where the second search started */
149 ut_asserteq(0x11, env_get_hex("mempos", 0));
150
151 unmap_sysmem(buf);
152
153 return 0;
154}
Simon Glass573c0fa2020-07-28 19:41:14 -0600155MEM_TEST(mem_test_ms_cont, UT_TESTF_CONSOLE_REC);
156
157/* Test that an 'ms' command with continuation stops at the end of the range */
158static int mem_test_ms_cont_end(struct unit_test_state *uts)
159{
160 char *const args[] = {"ms.b", "1", "ff", "12"};
161 int repeatable;
162 u8 *buf;
163
164 buf = map_sysmem(0, BUF_SIZE);
165 memset(buf, '\0', BUF_SIZE);
166 buf[0x0] = 0x12;
167 buf[0x31] = 0x12;
168 buf[0xff] = 0x12;
169 buf[0x100] = 0x12;
170 ut_assertok(console_record_reset_enable());
171 run_command("ms.b 1 ff 12", 0);
172 ut_assert_nextlinen("00000030");
173 ut_assert_nextlinen("--");
174 ut_assert_nextlinen("000000f0");
175 ut_assert_nextlinen("2 matches");
176 ut_assert_console_end();
177
178 /*
179 * run_command() ignoes the repeatable flag when using hush, so call
180 * cmd_process() directly.
181 *
182 * This should produce no matches.
183 */
184 ut_assertok(console_record_reset_enable());
185 cmd_process(CMD_FLAG_REPEAT, 4, args, &repeatable, NULL);
186 ut_assert_nextlinen("0 matches");
187 ut_assert_console_end();
188
189 /* One more time */
190 ut_assertok(console_record_reset_enable());
191 cmd_process(CMD_FLAG_REPEAT, 4, args, &repeatable, NULL);
192 ut_assert_nextlinen("0 matches");
193 ut_assert_console_end();
194
195 unmap_sysmem(buf);
196
197 return 0;
198}
199MEM_TEST(mem_test_ms_cont_end, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -0600200
201/* Test 'ms' command with multiple values */
Simon Glass573c0fa2020-07-28 19:41:14 -0600202static int mem_test_ms_mult(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -0600203{
204 static const char str[] = "hello";
205 char *buf;
206
207 buf = map_sysmem(0, BUF_SIZE + 5);
208 memset(buf, '\0', BUF_SIZE);
209 strcpy(buf + 0x1e, str);
210 strcpy(buf + 0x63, str);
211 strcpy(buf + BUF_SIZE - strlen(str) + 1, str);
Simon Glass573c0fa2020-07-28 19:41:14 -0600212 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -0600213 run_command("ms.b 0 100 68 65 6c 6c 6f", 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600214 ut_assert_nextline("00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 68 65 ..............he");
215 ut_assert_nextline("00000020: 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 llo.............");
Simon Glass19038de2020-06-02 19:26:49 -0600216 ut_assert_nextline("--");
Simon Glass85c8fc52021-05-08 07:00:00 -0600217 ut_assert_nextline("00000060: 00 00 00 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 ...hello........");
Simon Glass19038de2020-06-02 19:26:49 -0600218 ut_assert_nextline("2 matches");
219 ut_assert_console_end();
220 unmap_sysmem(buf);
221
222 ut_asserteq(2, env_get_hex("memmatches", 0));
223 ut_asserteq(0x63, env_get_hex("memaddr", 0));
224 ut_asserteq(0x63, env_get_hex("mempos", 0));
225
226 return 0;
227}
Simon Glass573c0fa2020-07-28 19:41:14 -0600228MEM_TEST(mem_test_ms_mult, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -0600229
230/* Test 'ms' command with string */
Simon Glass573c0fa2020-07-28 19:41:14 -0600231static int mem_test_ms_s(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -0600232{
233 static const char str[] = "hello";
234 static const char str2[] = "hellothere";
235 char *buf;
236
237 buf = map_sysmem(0, BUF_SIZE);
238 memset(buf, '\0', BUF_SIZE);
239 strcpy(buf + 0x1e, str);
240 strcpy(buf + 0x63, str);
241 strcpy(buf + 0xa1, str2);
Simon Glass573c0fa2020-07-28 19:41:14 -0600242 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -0600243 run_command("ms.s 0 100 hello", 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600244 ut_assert_nextline("00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 68 65 ..............he");
245 ut_assert_nextline("00000020: 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 llo.............");
Simon Glass19038de2020-06-02 19:26:49 -0600246 ut_assert_nextline("--");
Simon Glass85c8fc52021-05-08 07:00:00 -0600247 ut_assert_nextline("00000060: 00 00 00 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 ...hello........");
Simon Glass19038de2020-06-02 19:26:49 -0600248 ut_assert_nextline("--");
Simon Glass85c8fc52021-05-08 07:00:00 -0600249 ut_assert_nextline("000000a0: 00 68 65 6c 6c 6f 74 68 65 72 65 00 00 00 00 00 .hellothere.....");
Simon Glass19038de2020-06-02 19:26:49 -0600250 ut_assert_nextline("3 matches");
251 ut_assert_console_end();
252
253 ut_asserteq(3, env_get_hex("memmatches", 0));
254 ut_asserteq(0xa1, env_get_hex("memaddr", 0));
255 ut_asserteq(0xa1, env_get_hex("mempos", 0));
256
Simon Glass573c0fa2020-07-28 19:41:14 -0600257 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -0600258 run_command("ms.s 0 100 hello there", 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600259 ut_assert_nextline("000000a0: 00 68 65 6c 6c 6f 74 68 65 72 65 00 00 00 00 00 .hellothere.....");
Simon Glass19038de2020-06-02 19:26:49 -0600260 ut_assert_nextline("1 match");
261 ut_assert_console_end();
262
263 ut_asserteq(1, env_get_hex("memmatches", 0));
264 ut_asserteq(0xa1, env_get_hex("memaddr", 0));
265 ut_asserteq(0xa1, env_get_hex("mempos", 0));
266
267 unmap_sysmem(buf);
268
269 return 0;
270}
Simon Glass573c0fa2020-07-28 19:41:14 -0600271MEM_TEST(mem_test_ms_s, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -0600272
273/* Test 'ms' command with limit */
Simon Glass573c0fa2020-07-28 19:41:14 -0600274static int mem_test_ms_limit(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -0600275{
276 u8 *buf;
277
278 buf = map_sysmem(0, BUF_SIZE + 1);
279 memset(buf, '\0', BUF_SIZE);
280 buf[0x0] = 0x12;
281 buf[0x31] = 0x12;
282 buf[0x62] = 0x12;
283 buf[0x76] = 0x12;
Simon Glass573c0fa2020-07-28 19:41:14 -0600284 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -0600285 run_command("ms.b -l2 1 ff 12", 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600286 ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................");
Simon Glass19038de2020-06-02 19:26:49 -0600287 ut_assert_nextline("--");
288 ut_assert_nextlinen("00000060: 00 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00");
289 ut_assert_nextline("2 matches (repeat command to check for more)");
290 ut_assert_console_end();
291
292 ut_asserteq(2, env_get_hex("memmatches", 0));
293 ut_asserteq(0x62, env_get_hex("memaddr", 0));
294 ut_asserteq(0x61, env_get_hex("mempos", 0));
295
296 unmap_sysmem(buf);
297
298 return 0;
299}
Simon Glass573c0fa2020-07-28 19:41:14 -0600300MEM_TEST(mem_test_ms_limit, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -0600301
302/* Test 'ms' command in quiet mode */
Simon Glass573c0fa2020-07-28 19:41:14 -0600303static int mem_test_ms_quiet(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -0600304{
305 u8 *buf;
306
307 buf = map_sysmem(0, BUF_SIZE + 1);
308 memset(buf, '\0', BUF_SIZE);
309 buf[0x0] = 0x12;
310 buf[0x31] = 0x12;
311 buf[0x62] = 0x12;
312 buf[0x76] = 0x12;
Simon Glass573c0fa2020-07-28 19:41:14 -0600313 ut_assertok(console_record_reset_enable());
314 run_command("ms.b -q -l2 1 ff 12", 0);
Simon Glass19038de2020-06-02 19:26:49 -0600315 ut_assert_console_end();
316 unmap_sysmem(buf);
317
Simon Glass573c0fa2020-07-28 19:41:14 -0600318 ut_asserteq(2, env_get_hex("memmatches", 0));
319 ut_asserteq(0x62, env_get_hex("memaddr", 0));
320 ut_asserteq(0x61, env_get_hex("mempos", 0));
321
Simon Glass19038de2020-06-02 19:26:49 -0600322 return 0;
323}
Simon Glass573c0fa2020-07-28 19:41:14 -0600324MEM_TEST(mem_test_ms_quiet, UT_TESTF_CONSOLE_REC);