d57bfad39823966a78bd5b9d6f5ffaa3823fe382
[platform/kernel/u-boot.git] / test / cmd / mem_search.c
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
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
17 /* Test 'ms' command with bytes */
18 static int dm_test_ms_b(struct unit_test_state *uts)
19 {
20         u8 *buf;
21
22         buf = map_sysmem(0, BUF_SIZE + 1);
23         memset(buf, '\0', BUF_SIZE);
24         buf[0x0] = 0x12;
25         buf[0x31] = 0x12;
26         buf[0xff] = 0x12;
27         buf[0x100] = 0x12;
28         console_record_reset();
29         run_command("ms.b 1 ff 12", 0);
30         ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................");
31         ut_assert_nextline("--");
32         ut_assert_nextline("000000f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12    ................");
33         ut_assert_nextline("2 matches");
34         ut_assert_console_end();
35
36         ut_asserteq(2, env_get_hex("memmatches", 0));
37         ut_asserteq(0xff, env_get_hex("memaddr", 0));
38         ut_asserteq(0xfe, env_get_hex("mempos", 0));
39
40         unmap_sysmem(buf);
41
42         return 0;
43 }
44 DM_TEST(dm_test_ms_b, 0);
45
46 /* Test 'ms' command with 16-bit values */
47 static int dm_test_ms_w(struct unit_test_state *uts)
48 {
49         u16 *buf;
50
51         buf = map_sysmem(0, BUF_SIZE + 2);
52         memset(buf, '\0', BUF_SIZE);
53         buf[0x34 / 2] = 0x1234;
54         buf[BUF_SIZE / 2] = 0x1234;
55         console_record_reset();
56         run_command("ms.w 0 80 1234", 0);
57         ut_assert_nextline("00000030: 0000 0000 1234 0000 0000 0000 0000 0000    ....4...........");
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 }
69 DM_TEST(dm_test_ms_w, 0);
70
71 /* Test 'ms' command with 32-bit values */
72 static int dm_test_ms_l(struct unit_test_state *uts)
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;
80         console_record_reset();
81         run_command("ms 0 40 12345678", 0);
82         ut_assert_nextline("00000030: 00000000 00000000 12345678 00000000    ........xV4.....");
83         ut_assert_nextline("1 match");
84         ut_assert_console_end();
85
86         ut_asserteq(1, env_get_hex("memmatches", 0));
87         ut_asserteq(0x38, env_get_hex("memaddr", 0));
88         ut_asserteq(0x38 / 4, env_get_hex("mempos", 0));
89
90         console_record_reset();
91         run_command("ms 0 80 12345679", 0);
92         ut_assert_nextline("0 matches");
93         ut_assert_console_end();
94
95         ut_asserteq(0, env_get_hex("memmatches", 0));
96         ut_asserteq(0, env_get_hex("memaddr", 0));
97         ut_asserteq(0 / 4, env_get_hex("mempos", 0));
98
99         unmap_sysmem(buf);
100
101         return 0;
102 }
103 DM_TEST(dm_test_ms_l, 0);
104
105 /* Test 'ms' command with continuation */
106 static int dm_test_ms_cont(struct unit_test_state *uts)
107 {
108         char *const args[] = {"ms.b", "0", "100", "34"};
109         int repeatable;
110         u8 *buf;
111         int i;
112
113         buf = map_sysmem(0, BUF_SIZE);
114         memset(buf, '\0', BUF_SIZE);
115         for (i = 5; i < 0x33; i += 3)
116                 buf[i] = 0x34;
117         console_record_reset();
118         run_command("ms.b 0 100 34", 0);
119         ut_assert_nextlinen("00000000: 00 00 00 00 00 34 00 00 34 00 00 34 00 00 34 00");
120         ut_assert_nextline("--");
121         ut_assert_nextlinen("00000010: 00 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00");
122         ut_assert_nextline("--");
123         ut_assert_nextlinen("00000020: 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00 34");
124         ut_assert_nextlinen("10 matches (repeat command to check for more)");
125         ut_assert_console_end();
126
127         ut_asserteq(10, env_get_hex("memmatches", 0));
128         ut_asserteq(0x20, env_get_hex("memaddr", 0));
129         ut_asserteq(0x20, env_get_hex("mempos", 0));
130
131         /*
132          * run_command() ignoes the repeatable flag when using hush, so call
133          * cmd_process() directly
134          */
135         console_record_reset();
136         cmd_process(CMD_FLAG_REPEAT, 4, args, &repeatable, NULL);
137         ut_assert_nextlinen("00000020: 34 00 00 34 00 00 34 00 00 34 00 00 34 00 00 34");
138         ut_assert_nextline("--");
139         ut_assert_nextlinen("00000030: 00 00 34 00 00 00 00 00");
140         ut_assert_nextlinen("6 matches");
141         ut_assert_console_end();
142
143         ut_asserteq(6, env_get_hex("memmatches", 0));
144         ut_asserteq(0x32, env_get_hex("memaddr", 0));
145
146         /* 0x32 less 0x21, where the second search started */
147         ut_asserteq(0x11, env_get_hex("mempos", 0));
148
149         unmap_sysmem(buf);
150
151         return 0;
152 }
153 DM_TEST(dm_test_ms_cont, 0);
154
155 /* Test 'ms' command with multiple values */
156 static int dm_test_ms_mult(struct unit_test_state *uts)
157 {
158         static const char str[] = "hello";
159         char *buf;
160
161         buf = map_sysmem(0, BUF_SIZE + 5);
162         memset(buf, '\0', BUF_SIZE);
163         strcpy(buf + 0x1e, str);
164         strcpy(buf + 0x63, str);
165         strcpy(buf + BUF_SIZE - strlen(str) + 1, str);
166         console_record_reset();
167         run_command("ms.b 0 100 68 65 6c 6c 6f", 0);
168         ut_assert_nextline("00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 68 65    ..............he");
169         ut_assert_nextline("00000020: 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 00    llo.............");
170         ut_assert_nextline("--");
171         ut_assert_nextline("00000060: 00 00 00 68 65 6c 6c 6f 00 00 00 00 00 00 00 00    ...hello........");
172         ut_assert_nextline("2 matches");
173         ut_assert_console_end();
174         unmap_sysmem(buf);
175
176         ut_asserteq(2, env_get_hex("memmatches", 0));
177         ut_asserteq(0x63, env_get_hex("memaddr", 0));
178         ut_asserteq(0x63, env_get_hex("mempos", 0));
179
180         return 0;
181 }
182 DM_TEST(dm_test_ms_mult, 0);
183
184 /* Test 'ms' command with string */
185 static int dm_test_ms_s(struct unit_test_state *uts)
186 {
187         static const char str[] = "hello";
188         static const char str2[] = "hellothere";
189         char *buf;
190
191         buf = map_sysmem(0, BUF_SIZE);
192         memset(buf, '\0', BUF_SIZE);
193         strcpy(buf + 0x1e, str);
194         strcpy(buf + 0x63, str);
195         strcpy(buf + 0xa1, str2);
196         console_record_reset();
197         run_command("ms.s 0 100 hello", 0);
198         ut_assert_nextline("00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 68 65    ..............he");
199         ut_assert_nextline("00000020: 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 00 00    llo.............");
200         ut_assert_nextline("--");
201         ut_assert_nextline("00000060: 00 00 00 68 65 6c 6c 6f 00 00 00 00 00 00 00 00    ...hello........");
202         ut_assert_nextline("--");
203         ut_assert_nextline("000000a0: 00 68 65 6c 6c 6f 74 68 65 72 65 00 00 00 00 00    .hellothere.....");
204         ut_assert_nextline("3 matches");
205         ut_assert_console_end();
206
207         ut_asserteq(3, env_get_hex("memmatches", 0));
208         ut_asserteq(0xa1, env_get_hex("memaddr", 0));
209         ut_asserteq(0xa1, env_get_hex("mempos", 0));
210
211         console_record_reset();
212         run_command("ms.s 0 100 hello there", 0);
213         ut_assert_nextline("000000a0: 00 68 65 6c 6c 6f 74 68 65 72 65 00 00 00 00 00    .hellothere.....");
214         ut_assert_nextline("1 match");
215         ut_assert_console_end();
216
217         ut_asserteq(1, env_get_hex("memmatches", 0));
218         ut_asserteq(0xa1, env_get_hex("memaddr", 0));
219         ut_asserteq(0xa1, env_get_hex("mempos", 0));
220
221         unmap_sysmem(buf);
222
223         return 0;
224 }
225 DM_TEST(dm_test_ms_s, 0);
226
227 /* Test 'ms' command with limit */
228 static int dm_test_ms_limit(struct unit_test_state *uts)
229 {
230         u8 *buf;
231
232         buf = map_sysmem(0, BUF_SIZE + 1);
233         memset(buf, '\0', BUF_SIZE);
234         buf[0x0] = 0x12;
235         buf[0x31] = 0x12;
236         buf[0x62] = 0x12;
237         buf[0x76] = 0x12;
238         console_record_reset();
239         run_command("ms.b -l2 1 ff 12", 0);
240         ut_assert_nextline("00000030: 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................");
241         ut_assert_nextline("--");
242         ut_assert_nextlinen("00000060: 00 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00");
243         ut_assert_nextline("2 matches (repeat command to check for more)");
244         ut_assert_console_end();
245
246         ut_asserteq(2, env_get_hex("memmatches", 0));
247         ut_asserteq(0x62, env_get_hex("memaddr", 0));
248         ut_asserteq(0x61, env_get_hex("mempos", 0));
249
250         unmap_sysmem(buf);
251
252         return 0;
253 }
254 DM_TEST(dm_test_ms_limit, 0);
255
256 /* Test 'ms' command in quiet mode */
257 static int dm_test_ms_quiet(struct unit_test_state *uts)
258 {
259         u8 *buf;
260
261         buf = map_sysmem(0, BUF_SIZE + 1);
262         memset(buf, '\0', BUF_SIZE);
263         buf[0x0] = 0x12;
264         buf[0x31] = 0x12;
265         buf[0x62] = 0x12;
266         buf[0x76] = 0x12;
267         console_record_reset();
268         run_command("ms.b -l2 1 ff 12", 0);
269         ut_assert_console_end();
270         unmap_sysmem(buf);
271
272         return 0;
273 }
274 DM_TEST(dm_test_ms_quiet, 0);
275