blob: 94942793a49decd59075d54094358906030c1a8f [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
9#include <common.h>
10#include <console.h>
11#include <mapmem.h>
12#include <dm/test.h>
13#include <test/ut.h>
14
15#define BUF_SIZE 0x100
16
Simon Glass573c0fa2020-07-28 19:41:14 -060017/* Declare a new mem test */
18#define MEM_TEST(_name, _flags) UNIT_TEST(_name, _flags, mem_test)
19
Simon Glass19038de2020-06-02 19:26:49 -060020/* Test 'ms' command with bytes */
Simon Glass573c0fa2020-07-28 19:41:14 -060021static int mem_test_ms_b(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -060022{
23 u8 *buf;
24
25 buf = map_sysmem(0, BUF_SIZE + 1);
26 memset(buf, '\0', BUF_SIZE);
27 buf[0x0] = 0x12;
28 buf[0x31] = 0x12;
29 buf[0xff] = 0x12;
30 buf[0x100] = 0x12;
Simon Glass573c0fa2020-07-28 19:41:14 -060031 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -060032 run_command("ms.b 1 ff 12", 0);
33 ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................");
34 ut_assert_nextline("--");
35 ut_assert_nextline("000000f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 ................");
36 ut_assert_nextline("2 matches");
37 ut_assert_console_end();
38
39 ut_asserteq(2, env_get_hex("memmatches", 0));
40 ut_asserteq(0xff, env_get_hex("memaddr", 0));
41 ut_asserteq(0xfe, env_get_hex("mempos", 0));
42
43 unmap_sysmem(buf);
44
45 return 0;
46}
Simon Glass573c0fa2020-07-28 19:41:14 -060047MEM_TEST(mem_test_ms_b, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -060048
49/* Test 'ms' command with 16-bit values */
Simon Glass573c0fa2020-07-28 19:41:14 -060050static int mem_test_ms_w(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -060051{
52 u16 *buf;
53
54 buf = map_sysmem(0, BUF_SIZE + 2);
55 memset(buf, '\0', BUF_SIZE);
56 buf[0x34 / 2] = 0x1234;
57 buf[BUF_SIZE / 2] = 0x1234;
Simon Glass573c0fa2020-07-28 19:41:14 -060058 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -060059 run_command("ms.w 0 80 1234", 0);
60 ut_assert_nextline("00000030: 0000 0000 1234 0000 0000 0000 0000 0000 ....4...........");
61 ut_assert_nextline("1 match");
62 ut_assert_console_end();
63
64 ut_asserteq(1, env_get_hex("memmatches", 0));
65 ut_asserteq(0x34, env_get_hex("memaddr", 0));
66 ut_asserteq(0x34 / 2, env_get_hex("mempos", 0));
67
68 unmap_sysmem(buf);
69
70 return 0;
71}
Simon Glass573c0fa2020-07-28 19:41:14 -060072MEM_TEST(mem_test_ms_w, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -060073
74/* Test 'ms' command with 32-bit values */
Simon Glass573c0fa2020-07-28 19:41:14 -060075static int mem_test_ms_l(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -060076{
77 u32 *buf;
78
79 buf = map_sysmem(0, BUF_SIZE + 4);
80 memset(buf, '\0', BUF_SIZE);
81 buf[0x38 / 4] = 0x12345678;
82 buf[BUF_SIZE / 4] = 0x12345678;
Simon Glass573c0fa2020-07-28 19:41:14 -060083 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -060084 run_command("ms 0 40 12345678", 0);
85 ut_assert_nextline("00000030: 00000000 00000000 12345678 00000000 ........xV4.....");
86 ut_assert_nextline("1 match");
87 ut_assert_console_end();
88
89 ut_asserteq(1, env_get_hex("memmatches", 0));
90 ut_asserteq(0x38, env_get_hex("memaddr", 0));
91 ut_asserteq(0x38 / 4, env_get_hex("mempos", 0));
92
Simon Glass573c0fa2020-07-28 19:41:14 -060093 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -060094 run_command("ms 0 80 12345679", 0);
95 ut_assert_nextline("0 matches");
96 ut_assert_console_end();
97
98 ut_asserteq(0, env_get_hex("memmatches", 0));
99 ut_asserteq(0, env_get_hex("memaddr", 0));
100 ut_asserteq(0 / 4, env_get_hex("mempos", 0));
101
102 unmap_sysmem(buf);
103
104 return 0;
105}
Simon Glass573c0fa2020-07-28 19:41:14 -0600106MEM_TEST(mem_test_ms_l, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -0600107
108/* Test 'ms' command with continuation */
Simon Glass573c0fa2020-07-28 19:41:14 -0600109static int mem_test_ms_cont(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -0600110{
111 char *const args[] = {"ms.b", "0", "100", "34"};
112 int repeatable;
113 u8 *buf;
114 int i;
115
116 buf = map_sysmem(0, BUF_SIZE);
117 memset(buf, '\0', BUF_SIZE);
118 for (i = 5; i < 0x33; i += 3)
119 buf[i] = 0x34;
Simon Glass573c0fa2020-07-28 19:41:14 -0600120 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -0600121 run_command("ms.b 0 100 34", 0);
122 ut_assert_nextlinen("00000000: 00 00 00 00 00 34 00 00 34 00 00 34 00 00 34 00");
123 ut_assert_nextline("--");
124 ut_assert_nextlinen("00000010: 00 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00");
125 ut_assert_nextline("--");
126 ut_assert_nextlinen("00000020: 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00 34");
127 ut_assert_nextlinen("10 matches (repeat command to check for more)");
128 ut_assert_console_end();
129
130 ut_asserteq(10, env_get_hex("memmatches", 0));
131 ut_asserteq(0x20, env_get_hex("memaddr", 0));
132 ut_asserteq(0x20, env_get_hex("mempos", 0));
133
134 /*
135 * run_command() ignoes the repeatable flag when using hush, so call
136 * cmd_process() directly
137 */
Simon Glass573c0fa2020-07-28 19:41:14 -0600138 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -0600139 cmd_process(CMD_FLAG_REPEAT, 4, args, &repeatable, NULL);
140 ut_assert_nextlinen("00000020: 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00 34");
141 ut_assert_nextline("--");
142 ut_assert_nextlinen("00000030: 00 00 34 00 00 00 00 00");
143 ut_assert_nextlinen("6 matches");
144 ut_assert_console_end();
145
146 ut_asserteq(6, env_get_hex("memmatches", 0));
147 ut_asserteq(0x32, env_get_hex("memaddr", 0));
148
149 /* 0x32 less 0x21, where the second search started */
150 ut_asserteq(0x11, env_get_hex("mempos", 0));
151
152 unmap_sysmem(buf);
153
154 return 0;
155}
Simon Glass573c0fa2020-07-28 19:41:14 -0600156MEM_TEST(mem_test_ms_cont, UT_TESTF_CONSOLE_REC);
157
158/* Test that an 'ms' command with continuation stops at the end of the range */
159static int mem_test_ms_cont_end(struct unit_test_state *uts)
160{
161 char *const args[] = {"ms.b", "1", "ff", "12"};
162 int repeatable;
163 u8 *buf;
164
165 buf = map_sysmem(0, BUF_SIZE);
166 memset(buf, '\0', BUF_SIZE);
167 buf[0x0] = 0x12;
168 buf[0x31] = 0x12;
169 buf[0xff] = 0x12;
170 buf[0x100] = 0x12;
171 ut_assertok(console_record_reset_enable());
172 run_command("ms.b 1 ff 12", 0);
173 ut_assert_nextlinen("00000030");
174 ut_assert_nextlinen("--");
175 ut_assert_nextlinen("000000f0");
176 ut_assert_nextlinen("2 matches");
177 ut_assert_console_end();
178
179 /*
180 * run_command() ignoes the repeatable flag when using hush, so call
181 * cmd_process() directly.
182 *
183 * This should produce no matches.
184 */
185 ut_assertok(console_record_reset_enable());
186 cmd_process(CMD_FLAG_REPEAT, 4, args, &repeatable, NULL);
187 ut_assert_nextlinen("0 matches");
188 ut_assert_console_end();
189
190 /* One more time */
191 ut_assertok(console_record_reset_enable());
192 cmd_process(CMD_FLAG_REPEAT, 4, args, &repeatable, NULL);
193 ut_assert_nextlinen("0 matches");
194 ut_assert_console_end();
195
196 unmap_sysmem(buf);
197
198 return 0;
199}
200MEM_TEST(mem_test_ms_cont_end, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -0600201
202/* Test 'ms' command with multiple values */
Simon Glass573c0fa2020-07-28 19:41:14 -0600203static int mem_test_ms_mult(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -0600204{
205 static const char str[] = "hello";
206 char *buf;
207
208 buf = map_sysmem(0, BUF_SIZE + 5);
209 memset(buf, '\0', BUF_SIZE);
210 strcpy(buf + 0x1e, str);
211 strcpy(buf + 0x63, str);
212 strcpy(buf + BUF_SIZE - strlen(str) + 1, str);
Simon Glass573c0fa2020-07-28 19:41:14 -0600213 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -0600214 run_command("ms.b 0 100 68 65 6c 6c 6f", 0);
215 ut_assert_nextline("00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 68 65 ..............he");
216 ut_assert_nextline("00000020: 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 llo.............");
217 ut_assert_nextline("--");
218 ut_assert_nextline("00000060: 00 00 00 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 ...hello........");
219 ut_assert_nextline("2 matches");
220 ut_assert_console_end();
221 unmap_sysmem(buf);
222
223 ut_asserteq(2, env_get_hex("memmatches", 0));
224 ut_asserteq(0x63, env_get_hex("memaddr", 0));
225 ut_asserteq(0x63, env_get_hex("mempos", 0));
226
227 return 0;
228}
Simon Glass573c0fa2020-07-28 19:41:14 -0600229MEM_TEST(mem_test_ms_mult, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -0600230
231/* Test 'ms' command with string */
Simon Glass573c0fa2020-07-28 19:41:14 -0600232static int mem_test_ms_s(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -0600233{
234 static const char str[] = "hello";
235 static const char str2[] = "hellothere";
236 char *buf;
237
238 buf = map_sysmem(0, BUF_SIZE);
239 memset(buf, '\0', BUF_SIZE);
240 strcpy(buf + 0x1e, str);
241 strcpy(buf + 0x63, str);
242 strcpy(buf + 0xa1, str2);
Simon Glass573c0fa2020-07-28 19:41:14 -0600243 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -0600244 run_command("ms.s 0 100 hello", 0);
245 ut_assert_nextline("00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 68 65 ..............he");
246 ut_assert_nextline("00000020: 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 llo.............");
247 ut_assert_nextline("--");
248 ut_assert_nextline("00000060: 00 00 00 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 ...hello........");
249 ut_assert_nextline("--");
250 ut_assert_nextline("000000a0: 00 68 65 6c 6c 6f 74 68 65 72 65 00 00 00 00 00 .hellothere.....");
251 ut_assert_nextline("3 matches");
252 ut_assert_console_end();
253
254 ut_asserteq(3, env_get_hex("memmatches", 0));
255 ut_asserteq(0xa1, env_get_hex("memaddr", 0));
256 ut_asserteq(0xa1, env_get_hex("mempos", 0));
257
Simon Glass573c0fa2020-07-28 19:41:14 -0600258 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -0600259 run_command("ms.s 0 100 hello there", 0);
260 ut_assert_nextline("000000a0: 00 68 65 6c 6c 6f 74 68 65 72 65 00 00 00 00 00 .hellothere.....");
261 ut_assert_nextline("1 match");
262 ut_assert_console_end();
263
264 ut_asserteq(1, env_get_hex("memmatches", 0));
265 ut_asserteq(0xa1, env_get_hex("memaddr", 0));
266 ut_asserteq(0xa1, env_get_hex("mempos", 0));
267
268 unmap_sysmem(buf);
269
270 return 0;
271}
Simon Glass573c0fa2020-07-28 19:41:14 -0600272MEM_TEST(mem_test_ms_s, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -0600273
274/* Test 'ms' command with limit */
Simon Glass573c0fa2020-07-28 19:41:14 -0600275static int mem_test_ms_limit(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -0600276{
277 u8 *buf;
278
279 buf = map_sysmem(0, BUF_SIZE + 1);
280 memset(buf, '\0', BUF_SIZE);
281 buf[0x0] = 0x12;
282 buf[0x31] = 0x12;
283 buf[0x62] = 0x12;
284 buf[0x76] = 0x12;
Simon Glass573c0fa2020-07-28 19:41:14 -0600285 ut_assertok(console_record_reset_enable());
Simon Glass19038de2020-06-02 19:26:49 -0600286 run_command("ms.b -l2 1 ff 12", 0);
287 ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................");
288 ut_assert_nextline("--");
289 ut_assert_nextlinen("00000060: 00 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00");
290 ut_assert_nextline("2 matches (repeat command to check for more)");
291 ut_assert_console_end();
292
293 ut_asserteq(2, env_get_hex("memmatches", 0));
294 ut_asserteq(0x62, env_get_hex("memaddr", 0));
295 ut_asserteq(0x61, env_get_hex("mempos", 0));
296
297 unmap_sysmem(buf);
298
299 return 0;
300}
Simon Glass573c0fa2020-07-28 19:41:14 -0600301MEM_TEST(mem_test_ms_limit, UT_TESTF_CONSOLE_REC);
Simon Glass19038de2020-06-02 19:26:49 -0600302
303/* Test 'ms' command in quiet mode */
Simon Glass573c0fa2020-07-28 19:41:14 -0600304static int mem_test_ms_quiet(struct unit_test_state *uts)
Simon Glass19038de2020-06-02 19:26:49 -0600305{
306 u8 *buf;
307
308 buf = map_sysmem(0, BUF_SIZE + 1);
309 memset(buf, '\0', BUF_SIZE);
310 buf[0x0] = 0x12;
311 buf[0x31] = 0x12;
312 buf[0x62] = 0x12;
313 buf[0x76] = 0x12;
Simon Glass573c0fa2020-07-28 19:41:14 -0600314 ut_assertok(console_record_reset_enable());
315 run_command("ms.b -q -l2 1 ff 12", 0);
Simon Glass19038de2020-06-02 19:26:49 -0600316 ut_assert_console_end();
317 unmap_sysmem(buf);
318
Simon Glass573c0fa2020-07-28 19:41:14 -0600319 ut_asserteq(2, env_get_hex("memmatches", 0));
320 ut_asserteq(0x62, env_get_hex("memaddr", 0));
321 ut_asserteq(0x61, env_get_hex("mempos", 0));
322
Simon Glass19038de2020-06-02 19:26:49 -0600323 return 0;
324}
Simon Glass573c0fa2020-07-28 19:41:14 -0600325MEM_TEST(mem_test_ms_quiet, UT_TESTF_CONSOLE_REC);