]> git.ipfire.org Git - thirdparty/u-boot.git/blame - test/cmd/mem_search.c
test: Remove <common.h> and add needed includes
[thirdparty/u-boot.git] / test / cmd / mem_search.c
CommitLineData
bdded201
SG
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
bdded201
SG
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
550a9e79
SG
16/* Declare a new mem test */
17#define MEM_TEST(_name, _flags) UNIT_TEST(_name, _flags, mem_test)
18
bdded201 19/* Test 'ms' command with bytes */
550a9e79 20static int mem_test_ms_b(struct unit_test_state *uts)
bdded201
SG
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;
550a9e79 30 ut_assertok(console_record_reset_enable());
bdded201 31 run_command("ms.b 1 ff 12", 0);
c7b16d83 32 ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................");
bdded201 33 ut_assert_nextline("--");
c7b16d83 34 ut_assert_nextline("000000f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 ................");
bdded201
SG
35 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}
550a9e79 46MEM_TEST(mem_test_ms_b, UT_TESTF_CONSOLE_REC);
bdded201
SG
47
48/* Test 'ms' command with 16-bit values */
550a9e79 49static int mem_test_ms_w(struct unit_test_state *uts)
bdded201
SG
50{
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;
550a9e79 57 ut_assertok(console_record_reset_enable());
bdded201 58 run_command("ms.w 0 80 1234", 0);
c7b16d83 59 ut_assert_nextline("00000030: 0000 0000 1234 0000 0000 0000 0000 0000 ....4...........");
bdded201
SG
60 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}
550a9e79 71MEM_TEST(mem_test_ms_w, UT_TESTF_CONSOLE_REC);
bdded201
SG
72
73/* Test 'ms' command with 32-bit values */
550a9e79 74static int mem_test_ms_l(struct unit_test_state *uts)
bdded201
SG
75{
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;
550a9e79 82 ut_assertok(console_record_reset_enable());
bdded201 83 run_command("ms 0 40 12345678", 0);
c7b16d83 84 ut_assert_nextline("00000030: 00000000 00000000 12345678 00000000 ........xV4.....");
bdded201
SG
85 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
550a9e79 92 ut_assertok(console_record_reset_enable());
bdded201
SG
93 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}
550a9e79 105MEM_TEST(mem_test_ms_l, UT_TESTF_CONSOLE_REC);
bdded201
SG
106
107/* Test 'ms' command with continuation */
550a9e79 108static int mem_test_ms_cont(struct unit_test_state *uts)
bdded201
SG
109{
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;
550a9e79 119 ut_assertok(console_record_reset_enable());
bdded201
SG
120 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 */
550a9e79 137 ut_assertok(console_record_reset_enable());
bdded201
SG
138 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}
550a9e79
SG
155MEM_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);
bdded201
SG
200
201/* Test 'ms' command with multiple values */
550a9e79 202static int mem_test_ms_mult(struct unit_test_state *uts)
bdded201
SG
203{
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);
550a9e79 212 ut_assertok(console_record_reset_enable());
bdded201 213 run_command("ms.b 0 100 68 65 6c 6c 6f", 0);
c7b16d83
SG
214 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.............");
bdded201 216 ut_assert_nextline("--");
c7b16d83 217 ut_assert_nextline("00000060: 00 00 00 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 ...hello........");
bdded201
SG
218 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}
550a9e79 228MEM_TEST(mem_test_ms_mult, UT_TESTF_CONSOLE_REC);
bdded201
SG
229
230/* Test 'ms' command with string */
550a9e79 231static int mem_test_ms_s(struct unit_test_state *uts)
bdded201
SG
232{
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);
550a9e79 242 ut_assertok(console_record_reset_enable());
bdded201 243 run_command("ms.s 0 100 hello", 0);
c7b16d83
SG
244 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.............");
bdded201 246 ut_assert_nextline("--");
c7b16d83 247 ut_assert_nextline("00000060: 00 00 00 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 ...hello........");
bdded201 248 ut_assert_nextline("--");
c7b16d83 249 ut_assert_nextline("000000a0: 00 68 65 6c 6c 6f 74 68 65 72 65 00 00 00 00 00 .hellothere.....");
bdded201
SG
250 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
550a9e79 257 ut_assertok(console_record_reset_enable());
bdded201 258 run_command("ms.s 0 100 hello there", 0);
c7b16d83 259 ut_assert_nextline("000000a0: 00 68 65 6c 6c 6f 74 68 65 72 65 00 00 00 00 00 .hellothere.....");
bdded201
SG
260 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}
550a9e79 271MEM_TEST(mem_test_ms_s, UT_TESTF_CONSOLE_REC);
bdded201
SG
272
273/* Test 'ms' command with limit */
550a9e79 274static int mem_test_ms_limit(struct unit_test_state *uts)
bdded201
SG
275{
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;
550a9e79 284 ut_assertok(console_record_reset_enable());
bdded201 285 run_command("ms.b -l2 1 ff 12", 0);
c7b16d83 286 ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................");
bdded201
SG
287 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}
550a9e79 300MEM_TEST(mem_test_ms_limit, UT_TESTF_CONSOLE_REC);
bdded201
SG
301
302/* Test 'ms' command in quiet mode */
550a9e79 303static int mem_test_ms_quiet(struct unit_test_state *uts)
bdded201
SG
304{
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;
550a9e79
SG
313 ut_assertok(console_record_reset_enable());
314 run_command("ms.b -q -l2 1 ff 12", 0);
bdded201
SG
315 ut_assert_console_end();
316 unmap_sysmem(buf);
317
550a9e79
SG
318 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
bdded201
SG
322 return 0;
323}
550a9e79 324MEM_TEST(mem_test_ms_quiet, UT_TESTF_CONSOLE_REC);