blob: e67e777c9958c18dbe088d65abcd348a45937de8 [file] [log] [blame]
Alexander Gendin038cb022023-10-09 01:24:36 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for mbr command
4 *
5 * Copyright 2023 Matrox Video
6 * Written by Alex Gendin <agendin@matrox.com>
7 */
8
9#include <dm.h>
10#include <console.h>
11#include <dm/test.h>
12#include <mapmem.h>
13#include <part.h>
14#include <asm/global_data.h>
15#include <dm/device-internal.h>
16#include <dm/lists.h>
Simon Glassa8c12522024-09-01 16:26:32 -060017#include <linux/sizes.h>
Alexander Gendin038cb022023-10-09 01:24:36 +000018#include <test/suites.h>
19#include <test/ut.h>
20
21DECLARE_GLOBAL_DATA_PTR;
Simon Glassa8c12522024-09-01 16:26:32 -060022
23#define BLKSZ SZ_512 /* block size */
24
Alexander Gendin038cb022023-10-09 01:24:36 +000025/*
26 * Requirements for running test manually:
27 * mmc6.img - File size needs to be at least 12 MiB
28 *
29 * Command to create mmc6.img:
30 * $ dd if=/dev/zero of=mmc6.img bs=12M count=1
31 *
32 * To run this test manually, place mmc6.img into the same directory as u-boot,
33 * then run:
34 * $ ./u-boot -Tc 'ut mbr'
35 *
36 * To run this test as part of U-Boot test:
37 * $ ./test/py/test.py --bd sandbox --build -k ut_dm -v
38 * Note: mmc6.img will be created by the test suit.
39 */
40
41static char * mbr_parts_header = "setenv mbr_parts '";
42static char * mbr_parts_p1 = "uuid_disk=0x12345678;name=p1,start=8M,bootable,size=1M,id=0x0e";
43static char * mbr_parts_p2 = ";name=p2,size=1M,id=0x0e";
44static char * mbr_parts_p3 = ";name=p3,size=1M,id=0x0e";
45static char * mbr_parts_p4 = ";name=p4,size=1M,id=0x0e";
46static char * mbr_parts_p5 = ";name=[ext],size=2M,id=0x05;name=p5,size=1M,id=0x0e";
47static char * mbr_parts_tail = "'";
48
49/*
50 * One MBR partition
51000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
52000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 00 |...%$..@........|
53000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
54000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
55000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
56*/
Simon Glass40d2a6e2024-09-01 16:26:21 -060057static unsigned int mbr_cmp_start = 0x1b8;
Alexander Gendin038cb022023-10-09 01:24:36 +000058static unsigned mbr_cmp_size = 0x48;
59static unsigned char mbr_parts_ref_p1[] = {
60 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
610x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
620x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
630x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
640x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
65};
66
67/*
68 * Two MBR partitions
69000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
70000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
71000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 00 |%..F...H........|
72000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
73000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
74*/
75static unsigned char mbr_parts_ref_p2[] = {
76 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
770x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
780x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
790x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
800x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
81};
82
83/*
84 * Three MBR partitions
85000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
86000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
87000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 46 |%..F...H.......F|
88000001e0 06 01 0e 66 25 01 00 50 00 00 00 08 00 00 00 00 |...f%..P........|
89000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
90*/
91static unsigned char mbr_parts_ref_p3[] = {
92 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
930x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
940x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x46,
950x06, 0x01, 0x0e, 0x66, 0x25, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
960x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
97};
98
99/*
100 * Four MBR partitions
101000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
102000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
103000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 46 |%..F...H.......F|
104000001e0 06 01 0e 66 25 01 00 50 00 00 00 08 00 00 00 66 |...f%..P.......f|
105000001f0 26 01 0e 87 06 01 00 58 00 00 00 08 00 00 55 aa |&......X......U.|
106*/
107static unsigned char mbr_parts_ref_p4[] = {
108 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
1090x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
1100x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x46,
1110x06, 0x01, 0x0e, 0x66, 0x25, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x66,
1120x26, 0x01, 0x0e, 0x87, 0x06, 0x01, 0x00, 0x58, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x55, 0xaa
113};
114
115/*
116 * Five MBR partitions
117000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
118000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
119000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 46 |%..F...H.......F|
120000001e0 06 01 0e 66 25 01 00 50 00 00 00 08 00 00 00 66 |...f%..P.......f|
121000001f0 26 01 05 a7 26 01 00 58 00 00 00 10 00 00 55 aa |&...&..X......U.|
122*/
123static unsigned char mbr_parts_ref_p5[] = {
124 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
1250x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
1260x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x46,
1270x06, 0x01, 0x0e, 0x66, 0x25, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x66,
1280x26, 0x01, 0x05, 0xa7, 0x26, 0x01, 0x00, 0x58, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x55, 0xaa
129};
130static unsigned ebr_cmp_start = 0x1B8;
131static unsigned ebr_cmp_size = 0x48;
132/*
133 * EBR
13400b001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 87 |................|
13500b001c0 07 01 0e a7 26 01 00 08 00 00 00 08 00 00 00 00 |....&...........|
13600b001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
13700b001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
13800b001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
139*/
140static unsigned char ebr_parts_ref_p5[] = {
141 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87,
1420x07, 0x01, 0x0e, 0xa7, 0x26, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
1430x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1440x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1450x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
146};
147
148/* Fill write buffers with pseudo-random data */
149static void init_write_buffers(char *mbr_wb, size_t mbr_wb_size,
150 char *ebr_wb, size_t ebr_wb_size, unsigned seed)
151{
152 while (mbr_wb_size--) {
153 *mbr_wb++ = seed;
154 seed *= 43;
155 seed += 17 + mbr_wb_size/4;
156 }
157 while (ebr_wb_size--) {
158 *ebr_wb++ = seed;
159 seed *= 43;
160 seed += 17 + ebr_wb_size/4;
161 }
162}
163
164/* Build string with MBR partition(s) layout */
165static unsigned build_mbr_parts(char *buf, size_t buf_size, unsigned num_parts)
166{
167 size_t bytes_remaining, cur_str_size;
168 char * cur_buf;
169
170 if (!num_parts || num_parts > 5 || !buf)
171 return 1;
172
173 cur_buf = buf;
174 *cur_buf = '\0';
175 bytes_remaining = buf_size;
176
177 cur_str_size = sizeof(mbr_parts_header);
178 if (cur_str_size + 1 > bytes_remaining)
179 return 1;
180 strcat(cur_buf, mbr_parts_header);
181 bytes_remaining -= cur_str_size;
182
183 if (num_parts >= 1) {
184 cur_str_size = sizeof(mbr_parts_p1);
185 if (cur_str_size + 1 > bytes_remaining)
186 return 1;
187 strcat(cur_buf, mbr_parts_p1);
188 bytes_remaining -= cur_str_size;
189
190 if (num_parts >= 2) {
191 cur_str_size = sizeof(mbr_parts_p2);
192 if (cur_str_size + 1 > bytes_remaining)
193 return 1;
194 strcat(cur_buf, mbr_parts_p2);
195 bytes_remaining -= cur_str_size;
196
197 if (num_parts >= 3) {
198 cur_str_size = sizeof(mbr_parts_p3);
199 if (cur_str_size + 1 > bytes_remaining)
200 return 1;
201 strcat(cur_buf, mbr_parts_p3);
202 bytes_remaining -= cur_str_size;
203
204 if (num_parts == 4) {
205 cur_str_size = sizeof(mbr_parts_p4);
206 if (cur_str_size + 1 > bytes_remaining)
207 return 1;
208 strcat(cur_buf, mbr_parts_p4);
209 bytes_remaining -= cur_str_size;
210
211 }
Alexander Gendin207fc532023-11-08 03:05:19 +0000212 else if (num_parts == 5) {
213 cur_str_size = sizeof(mbr_parts_p5);
214 if (cur_str_size + 1 > bytes_remaining)
215 return 1;
216 strcat(cur_buf, mbr_parts_p5);
217 bytes_remaining -= cur_str_size;
Alexander Gendin038cb022023-10-09 01:24:36 +0000218
Alexander Gendin207fc532023-11-08 03:05:19 +0000219 }
Alexander Gendin038cb022023-10-09 01:24:36 +0000220 }
221 }
222 }
223
224 cur_str_size = sizeof(mbr_parts_tail);
225 if (cur_str_size + 1 > bytes_remaining)
226 return 1;
227 strcat(cur_buf, mbr_parts_tail);
228
229 return 0;
230}
231
232static int mbr_test_run(struct unit_test_state *uts)
233{
234 struct blk_desc *mmc_dev_desc;
Simon Glassa8c12522024-09-01 16:26:32 -0600235 unsigned char mbr_wbuf[BLKSZ], ebr_wbuf[BLKSZ], rbuf[BLKSZ];
Alexander Gendin038cb022023-10-09 01:24:36 +0000236 char mbr_parts_buf[256];
237 ulong mbr_wa, ebr_wa, ra, ebr_blk, mbr_parts_max;
238 struct udevice *dev;
239 ofnode root, node;
240
241 /* Enable the mmc6 node for this test */
242 root = oftree_root(oftree_default());
243 node = ofnode_find_subnode(root, "mmc6");
244 ut_assert(ofnode_valid(node));
245 ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
246
Alexander Gendin2036ad12024-02-03 02:56:19 +0000247 /*
248 * 1 byte for null character
249 * 2 reserved bytes
250 */
251 mbr_parts_max = 1 + 2 +
Alexander Gendin038cb022023-10-09 01:24:36 +0000252 strlen(mbr_parts_header) +
253 strlen(mbr_parts_p1) +
254 strlen(mbr_parts_p2) +
255 strlen(mbr_parts_p3) +
256 max(strlen(mbr_parts_p4), strlen(mbr_parts_p5)) +
257 strlen(mbr_parts_tail);
258 ut_assertf(sizeof(mbr_parts_buf) >= mbr_parts_max, "Buffer avail: %ld; buffer req: %ld\n",
259 sizeof(mbr_parts_buf), mbr_parts_max);
260
261 mbr_wa = map_to_sysmem(mbr_wbuf);
262 ebr_wa = map_to_sysmem(ebr_wbuf);
263 ra = map_to_sysmem(rbuf);
Simon Glassa8c12522024-09-01 16:26:32 -0600264 ebr_blk = (ulong)0xb00000 / BLKSZ;
Alexander Gendin038cb022023-10-09 01:24:36 +0000265
266 /* Make sure mmc6 exists */
267 ut_asserteq(6, blk_get_device_by_str("mmc", "6", &mmc_dev_desc));
Alexander Gendin038cb022023-10-09 01:24:36 +0000268 ut_assertok(run_commandf("mmc dev 6"));
269 ut_assert_nextline("switch to partitions #0, OK");
270 ut_assert_nextline("mmc6 is current device");
Simon Glassc579bd42024-08-22 07:58:03 -0600271 ut_assert_console_end();
Alexander Gendin038cb022023-10-09 01:24:36 +0000272
273 /* Make sure mmc6 is 12+ MiB in size */
Simon Glass345567c2024-09-01 16:26:22 -0600274 ut_assertok(run_commandf("mmc read %lx %lx 1", ra,
Simon Glassa8c12522024-09-01 16:26:32 -0600275 (ulong)0xbffe00 / BLKSZ));
Alexander Gendin038cb022023-10-09 01:24:36 +0000276
277 /* Test one MBR partition */
278 init_write_buffers(mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
279 ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 1));
Simon Glass345567c2024-09-01 16:26:22 -0600280 ut_assertok(run_commandf("write mmc 6:0 %lx 0 1", mbr_wa));
Alexander Gendin038cb022023-10-09 01:24:36 +0000281 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600282 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Simon Glassa8c12522024-09-01 16:26:32 -0600283 ut_assertok(memcmp(mbr_wbuf, rbuf, BLKSZ));
Simon Glass345567c2024-09-01 16:26:22 -0600284 ut_assertok(run_commandf("write mmc 6:0 %lx %lx 1", ebr_wa, ebr_blk));
Alexander Gendin038cb022023-10-09 01:24:36 +0000285 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600286 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600287 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Alexander Gendin038cb022023-10-09 01:24:36 +0000288 ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
289 ut_assertok(run_commandf("mbr write mmc 6"));
Simon Glass56228252024-08-22 07:57:58 -0600290 ut_assert_nextlinen("MMC read: dev # 6");
Alexander Gendin038cb022023-10-09 01:24:36 +0000291 ut_assert_nextline("MBR: write success!");
292 ut_assertok(run_commandf("mbr verify mmc 6"));
293 ut_assert_nextline("MBR: verify success!");
294 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600295 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600296 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Simon Glassc579bd42024-08-22 07:58:03 -0600297 ut_assert_console_end();
Alexander Gendin038cb022023-10-09 01:24:36 +0000298 /*
299 000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
300 000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 00 |...%$..@........|
301 000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
302 000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
303 000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
304 */
305 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600306 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Alexander Gendin038cb022023-10-09 01:24:36 +0000307 for (unsigned i = 0; i < mbr_cmp_size; i++) {
308 ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p1[i],
Simon Glass345567c2024-09-01 16:26:22 -0600309 "1P MBR+0x%04X: expected %#02X, actual: %#02X\n",
Alexander Gendin038cb022023-10-09 01:24:36 +0000310 mbr_cmp_start + i, mbr_parts_ref_p1[i], rbuf[mbr_cmp_start + i]);
311 }
312
313 /* Test two MBR partitions */
314 init_write_buffers(mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
315 ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 2));
Simon Glass345567c2024-09-01 16:26:22 -0600316 ut_assertok(run_commandf("write mmc 6:0 %lx 0 1", mbr_wa));
Alexander Gendin038cb022023-10-09 01:24:36 +0000317 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600318 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Simon Glassa8c12522024-09-01 16:26:32 -0600319 ut_assertok(memcmp(mbr_wbuf, rbuf, BLKSZ));
Simon Glass345567c2024-09-01 16:26:22 -0600320 ut_assertok(run_commandf("write mmc 6:0 %lx %lx 1", ebr_wa, ebr_blk));
Alexander Gendin038cb022023-10-09 01:24:36 +0000321 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600322 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600323 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Alexander Gendin038cb022023-10-09 01:24:36 +0000324 ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
325 ut_assertok(run_commandf("mbr write mmc 6"));
326 ut_assert_nextline("MBR: write success!");
327 ut_assertok(run_commandf("mbr verify mmc 6"));
328 ut_assert_nextline("MBR: verify success!");
329 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600330 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600331 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Simon Glassc579bd42024-08-22 07:58:03 -0600332 ut_assert_console_end();
Alexander Gendin038cb022023-10-09 01:24:36 +0000333 /*
334 000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
335 000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
336 000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 00 |%..F...H........|
337 000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
338 000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
339 */
340 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600341 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Alexander Gendin038cb022023-10-09 01:24:36 +0000342 for (unsigned i = 0; i < mbr_cmp_size; i++) {
343 ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p2[i],
Simon Glass345567c2024-09-01 16:26:22 -0600344 "2P MBR+0x%04X: expected %#02X, actual: %#02X\n",
Alexander Gendin038cb022023-10-09 01:24:36 +0000345 mbr_cmp_start + i, mbr_parts_ref_p2[i], rbuf[mbr_cmp_start + i]);
346 }
347
348 /* Test three MBR partitions */
349 init_write_buffers(mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
350 ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 3));
Simon Glass345567c2024-09-01 16:26:22 -0600351 ut_assertok(run_commandf("write mmc 6:0 %lx 0 1", mbr_wa));
Alexander Gendin038cb022023-10-09 01:24:36 +0000352 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600353 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Simon Glassa8c12522024-09-01 16:26:32 -0600354 ut_assertok(memcmp(mbr_wbuf, rbuf, BLKSZ));
Simon Glass345567c2024-09-01 16:26:22 -0600355 ut_assertok(run_commandf("write mmc 6:0 %lx %lx 1", ebr_wa, ebr_blk));
Alexander Gendin038cb022023-10-09 01:24:36 +0000356 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600357 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600358 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Alexander Gendin038cb022023-10-09 01:24:36 +0000359 ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
360 ut_assertok(run_commandf("mbr write mmc 6"));
361 ut_assert_nextline("MBR: write success!");
362 ut_assertok(run_commandf("mbr verify mmc 6"));
363 ut_assert_nextline("MBR: verify success!");
364 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600365 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600366 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Simon Glassc579bd42024-08-22 07:58:03 -0600367 ut_assert_console_end();
Alexander Gendin038cb022023-10-09 01:24:36 +0000368 /*
369 000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
370 000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
371 000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 46 |%..F...H.......F|
372 000001e0 06 01 0e 66 25 01 00 50 00 00 00 08 00 00 00 00 |...f%..P........|
373 000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
374 */
375 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600376 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Alexander Gendin038cb022023-10-09 01:24:36 +0000377 for (unsigned i = 0; i < mbr_cmp_size; i++) {
378 ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p3[i],
Simon Glass345567c2024-09-01 16:26:22 -0600379 "3P MBR+0x%04X: expected %#02X, actual: %#02X\n",
Alexander Gendin038cb022023-10-09 01:24:36 +0000380 mbr_cmp_start + i, mbr_parts_ref_p3[i], rbuf[mbr_cmp_start + i]);
381 }
382
383 /* Test four MBR partitions */
384 init_write_buffers(mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
385 ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 4));
Simon Glass345567c2024-09-01 16:26:22 -0600386 ut_assertok(run_commandf("write mmc 6:0 %lx 0 1", mbr_wa));
Alexander Gendin038cb022023-10-09 01:24:36 +0000387 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600388 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Simon Glassa8c12522024-09-01 16:26:32 -0600389 ut_assertok(memcmp(mbr_wbuf, rbuf, BLKSZ));
Simon Glass345567c2024-09-01 16:26:22 -0600390 ut_assertok(run_commandf("write mmc 6:0 %lx %lx 1", ebr_wa, ebr_blk));
Alexander Gendin038cb022023-10-09 01:24:36 +0000391 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600392 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600393 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Alexander Gendin038cb022023-10-09 01:24:36 +0000394 ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
395 ut_assertok(run_commandf("mbr write mmc 6"));
396 ut_assert_nextline("MBR: write success!");
397 ut_assertok(run_commandf("mbr verify mmc 6"));
398 ut_assert_nextline("MBR: verify success!");
399 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600400 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600401 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Simon Glassc579bd42024-08-22 07:58:03 -0600402 ut_assert_console_end();
Alexander Gendin038cb022023-10-09 01:24:36 +0000403 /*
404 000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
405 000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
406 000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 46 |%..F...H.......F|
407 000001e0 06 01 0e 66 25 01 00 50 00 00 00 08 00 00 00 66 |...f%..P.......f|
408 000001f0 26 01 0e 87 06 01 00 58 00 00 00 08 00 00 55 aa |&......X......U.|
409 */
410 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600411 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Alexander Gendin038cb022023-10-09 01:24:36 +0000412 for (unsigned i = 0; i < mbr_cmp_size; i++) {
413 ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p4[i],
Simon Glass345567c2024-09-01 16:26:22 -0600414 "4P MBR+0x%04X: expected %#02X, actual: %#02X\n",
Alexander Gendin038cb022023-10-09 01:24:36 +0000415 mbr_cmp_start + i, mbr_parts_ref_p4[i], rbuf[mbr_cmp_start + i]);
416 }
417
418 /* Test five MBR partitions */
419 init_write_buffers(mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
420 ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 5));
Simon Glass345567c2024-09-01 16:26:22 -0600421 ut_assertok(run_commandf("write mmc 6:0 %lx 0 1", mbr_wa));
Alexander Gendin038cb022023-10-09 01:24:36 +0000422 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600423 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Simon Glassa8c12522024-09-01 16:26:32 -0600424 ut_assertok(memcmp(mbr_wbuf, rbuf, BLKSZ));
Simon Glass345567c2024-09-01 16:26:22 -0600425 ut_assertok(run_commandf("write mmc 6:0 %lx %lx 1", ebr_wa, ebr_blk));
Alexander Gendin038cb022023-10-09 01:24:36 +0000426 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600427 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600428 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Alexander Gendin038cb022023-10-09 01:24:36 +0000429 ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
430 ut_assertf(0 == run_commandf("mbr write mmc 6"), "Invalid partitions string: %s\n", mbr_parts_buf);
431 ut_assert_nextline("MBR: write success!");
432 ut_assertok(run_commandf("mbr verify mmc 6"));
433 ut_assert_nextline("MBR: verify success!");
Simon Glassc579bd42024-08-22 07:58:03 -0600434 ut_assert_console_end();
Alexander Gendin038cb022023-10-09 01:24:36 +0000435 /*
436 000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
437 000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
438 000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 46 |%..F...H.......F|
439 000001e0 06 01 0e 66 25 01 00 50 00 00 00 08 00 00 00 66 |...f%..P.......f|
440 000001f0 26 01 05 a7 26 01 00 58 00 00 00 10 00 00 55 aa |&...&..X......U.|
441 */
442 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600443 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Alexander Gendin038cb022023-10-09 01:24:36 +0000444 for (unsigned i = 0; i < mbr_cmp_size; i++) {
445 ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p5[i],
Simon Glass345567c2024-09-01 16:26:22 -0600446 "5P MBR+0x%04X: expected %#02X, actual: %#02X\n",
Alexander Gendin038cb022023-10-09 01:24:36 +0000447 mbr_cmp_start + i, mbr_parts_ref_p5[i], rbuf[mbr_cmp_start + i]);
448 }
449 /*
450 00b001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 87 |................|
451 00b001c0 07 01 0e a7 26 01 00 08 00 00 00 08 00 00 00 00 |....&...........|
452 00b001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
453 00b001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
454 00b001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
455 */
456 memset(rbuf, 0, sizeof(rbuf));
Simon Glass345567c2024-09-01 16:26:22 -0600457 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Alexander Gendin038cb022023-10-09 01:24:36 +0000458 for (unsigned i = 0; i < ebr_cmp_size; i++) {
459 ut_assertf(rbuf[ebr_cmp_start + i] == ebr_parts_ref_p5[i],
Simon Glass345567c2024-09-01 16:26:22 -0600460 "5P EBR+0x%04X: expected %#02X, actual: %#02X\n",
Alexander Gendin038cb022023-10-09 01:24:36 +0000461 ebr_cmp_start + i, ebr_parts_ref_p5[i], rbuf[ebr_cmp_start + i]);
462 }
Simon Glass2a1f1692024-09-01 16:26:31 -0600463 unmap_sysmem(mbr_wbuf);
464 unmap_sysmem(ebr_wbuf);
465 unmap_sysmem(rbuf);
Alexander Gendin038cb022023-10-09 01:24:36 +0000466
467 return 0;
468}
469
470/* Declare mbr test */
Simon Glass11fcfa32024-08-22 07:57:50 -0600471UNIT_TEST(mbr_test_run, UTF_CONSOLE, mbr_test);
Alexander Gendin038cb022023-10-09 01:24:36 +0000472
473int do_ut_mbr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
474{
475 struct unit_test *tests = UNIT_TEST_SUITE_START(mbr_test);
476 const int n_ents = UNIT_TEST_SUITE_COUNT(mbr_test);
477
478 return cmd_ut_category("mbr", "mbr_test_", tests, n_ents, argc, argv);
479}
480
481static int dm_test_cmd_mbr(struct unit_test_state *uts)
482{
483 return mbr_test_run(uts);
484}
Simon Glass11fcfa32024-08-22 07:57:50 -0600485DM_TEST(dm_test_cmd_mbr, UTF_SCAN_FDT | UTF_CONSOLE);