blob: e651256a4cbd6a72cc9b1457a5bb2b03edca82d3 [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/ut.h>
19
20DECLARE_GLOBAL_DATA_PTR;
Simon Glassa8c12522024-09-01 16:26:32 -060021
22#define BLKSZ SZ_512 /* block size */
23
Alexander Gendin038cb022023-10-09 01:24:36 +000024/*
25 * Requirements for running test manually:
26 * mmc6.img - File size needs to be at least 12 MiB
27 *
28 * Command to create mmc6.img:
29 * $ dd if=/dev/zero of=mmc6.img bs=12M count=1
30 *
31 * To run this test manually, place mmc6.img into the same directory as u-boot,
32 * then run:
33 * $ ./u-boot -Tc 'ut mbr'
34 *
35 * To run this test as part of U-Boot test:
36 * $ ./test/py/test.py --bd sandbox --build -k ut_dm -v
37 * Note: mmc6.img will be created by the test suit.
38 */
39
40static char * mbr_parts_header = "setenv mbr_parts '";
41static char * mbr_parts_p1 = "uuid_disk=0x12345678;name=p1,start=8M,bootable,size=1M,id=0x0e";
42static char * mbr_parts_p2 = ";name=p2,size=1M,id=0x0e";
43static char * mbr_parts_p3 = ";name=p3,size=1M,id=0x0e";
44static char * mbr_parts_p4 = ";name=p4,size=1M,id=0x0e";
45static char * mbr_parts_p5 = ";name=[ext],size=2M,id=0x05;name=p5,size=1M,id=0x0e";
46static char * mbr_parts_tail = "'";
47
48/*
49 * One MBR partition
50000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
51000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 00 |...%$..@........|
52000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
53000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
54000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
55*/
Simon Glass40d2a6e2024-09-01 16:26:21 -060056static unsigned int mbr_cmp_start = 0x1b8;
Alexander Gendin038cb022023-10-09 01:24:36 +000057static unsigned mbr_cmp_size = 0x48;
58static unsigned char mbr_parts_ref_p1[] = {
59 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
600x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
610x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x55, 0xaa
64};
65
66/*
67 * Two MBR partitions
68000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
69000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
70000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 00 |%..F...H........|
71000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
72000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
73*/
74static unsigned char mbr_parts_ref_p2[] = {
75 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
760x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
770x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
780x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
790x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
80};
81
82/*
83 * Three MBR partitions
84000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
85000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
86000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 46 |%..F...H.......F|
87000001e0 06 01 0e 66 25 01 00 50 00 00 00 08 00 00 00 00 |...f%..P........|
88000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
89*/
90static unsigned char mbr_parts_ref_p3[] = {
91 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
920x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
930x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x46,
940x06, 0x01, 0x0e, 0x66, 0x25, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
950x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
96};
97
98/*
99 * Four MBR partitions
100000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
101000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
102000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 46 |%..F...H.......F|
103000001e0 06 01 0e 66 25 01 00 50 00 00 00 08 00 00 00 66 |...f%..P.......f|
104000001f0 26 01 0e 87 06 01 00 58 00 00 00 08 00 00 55 aa |&......X......U.|
105*/
106static unsigned char mbr_parts_ref_p4[] = {
107 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
1080x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
1090x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x46,
1100x06, 0x01, 0x0e, 0x66, 0x25, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x66,
1110x26, 0x01, 0x0e, 0x87, 0x06, 0x01, 0x00, 0x58, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x55, 0xaa
112};
113
114/*
115 * Five MBR partitions
116000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
117000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
118000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 46 |%..F...H.......F|
119000001e0 06 01 0e 66 25 01 00 50 00 00 00 08 00 00 00 66 |...f%..P.......f|
120000001f0 26 01 05 a7 26 01 00 58 00 00 00 10 00 00 55 aa |&...&..X......U.|
121*/
122static unsigned char mbr_parts_ref_p5[] = {
123 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
1240x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
1250x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x46,
1260x06, 0x01, 0x0e, 0x66, 0x25, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x66,
1270x26, 0x01, 0x05, 0xa7, 0x26, 0x01, 0x00, 0x58, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x55, 0xaa
128};
129static unsigned ebr_cmp_start = 0x1B8;
130static unsigned ebr_cmp_size = 0x48;
131/*
132 * EBR
13300b001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 87 |................|
13400b001c0 07 01 0e a7 26 01 00 08 00 00 00 08 00 00 00 00 |....&...........|
13500b001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
13600b001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
13700b001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
138*/
139static unsigned char ebr_parts_ref_p5[] = {
140 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87,
1410x07, 0x01, 0x0e, 0xa7, 0x26, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
1420x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x55, 0xaa
145};
146
147/* Fill write buffers with pseudo-random data */
148static void init_write_buffers(char *mbr_wb, size_t mbr_wb_size,
149 char *ebr_wb, size_t ebr_wb_size, unsigned seed)
150{
151 while (mbr_wb_size--) {
152 *mbr_wb++ = seed;
153 seed *= 43;
154 seed += 17 + mbr_wb_size/4;
155 }
156 while (ebr_wb_size--) {
157 *ebr_wb++ = seed;
158 seed *= 43;
159 seed += 17 + ebr_wb_size/4;
160 }
161}
162
163/* Build string with MBR partition(s) layout */
164static unsigned build_mbr_parts(char *buf, size_t buf_size, unsigned num_parts)
165{
166 size_t bytes_remaining, cur_str_size;
167 char * cur_buf;
168
169 if (!num_parts || num_parts > 5 || !buf)
170 return 1;
171
172 cur_buf = buf;
173 *cur_buf = '\0';
174 bytes_remaining = buf_size;
175
176 cur_str_size = sizeof(mbr_parts_header);
177 if (cur_str_size + 1 > bytes_remaining)
178 return 1;
179 strcat(cur_buf, mbr_parts_header);
180 bytes_remaining -= cur_str_size;
181
182 if (num_parts >= 1) {
183 cur_str_size = sizeof(mbr_parts_p1);
184 if (cur_str_size + 1 > bytes_remaining)
185 return 1;
186 strcat(cur_buf, mbr_parts_p1);
187 bytes_remaining -= cur_str_size;
188
189 if (num_parts >= 2) {
190 cur_str_size = sizeof(mbr_parts_p2);
191 if (cur_str_size + 1 > bytes_remaining)
192 return 1;
193 strcat(cur_buf, mbr_parts_p2);
194 bytes_remaining -= cur_str_size;
195
196 if (num_parts >= 3) {
197 cur_str_size = sizeof(mbr_parts_p3);
198 if (cur_str_size + 1 > bytes_remaining)
199 return 1;
200 strcat(cur_buf, mbr_parts_p3);
201 bytes_remaining -= cur_str_size;
202
203 if (num_parts == 4) {
204 cur_str_size = sizeof(mbr_parts_p4);
205 if (cur_str_size + 1 > bytes_remaining)
206 return 1;
207 strcat(cur_buf, mbr_parts_p4);
208 bytes_remaining -= cur_str_size;
209
210 }
Alexander Gendin207fc532023-11-08 03:05:19 +0000211 else if (num_parts == 5) {
212 cur_str_size = sizeof(mbr_parts_p5);
213 if (cur_str_size + 1 > bytes_remaining)
214 return 1;
215 strcat(cur_buf, mbr_parts_p5);
216 bytes_remaining -= cur_str_size;
Alexander Gendin038cb022023-10-09 01:24:36 +0000217
Alexander Gendin207fc532023-11-08 03:05:19 +0000218 }
Alexander Gendin038cb022023-10-09 01:24:36 +0000219 }
220 }
221 }
222
223 cur_str_size = sizeof(mbr_parts_tail);
224 if (cur_str_size + 1 > bytes_remaining)
225 return 1;
226 strcat(cur_buf, mbr_parts_tail);
227
228 return 0;
229}
230
231static int mbr_test_run(struct unit_test_state *uts)
232{
233 struct blk_desc *mmc_dev_desc;
Simon Glass2ca519b2024-09-01 16:26:33 -0600234 unsigned char *mbr_wbuf, *ebr_wbuf, *rbuf;
Alexander Gendin038cb022023-10-09 01:24:36 +0000235 char mbr_parts_buf[256];
Simon Glass2ca519b2024-09-01 16:26:33 -0600236 ulong addr = 0x1000; /* start address for buffers */
237 ulong mbr_wa = addr, ebr_wa = addr + BLKSZ, ra = addr + BLKSZ * 2;
238 ulong ebr_blk, mbr_parts_max;
Alexander Gendin038cb022023-10-09 01:24:36 +0000239 struct udevice *dev;
240 ofnode root, node;
241
242 /* Enable the mmc6 node for this test */
243 root = oftree_root(oftree_default());
244 node = ofnode_find_subnode(root, "mmc6");
245 ut_assert(ofnode_valid(node));
246 ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
247
Alexander Gendin2036ad12024-02-03 02:56:19 +0000248 /*
249 * 1 byte for null character
250 * 2 reserved bytes
251 */
252 mbr_parts_max = 1 + 2 +
Alexander Gendin038cb022023-10-09 01:24:36 +0000253 strlen(mbr_parts_header) +
254 strlen(mbr_parts_p1) +
255 strlen(mbr_parts_p2) +
256 strlen(mbr_parts_p3) +
257 max(strlen(mbr_parts_p4), strlen(mbr_parts_p5)) +
258 strlen(mbr_parts_tail);
Heinrich Schuchardtf53329d2024-11-03 23:48:11 +0100259 ut_assertf(sizeof(mbr_parts_buf) >= mbr_parts_max, "Buffer avail: %zd; buffer req: %ld\n",
260 sizeof(mbr_parts_buf), mbr_parts_max);
Alexander Gendin038cb022023-10-09 01:24:36 +0000261
Simon Glass2ca519b2024-09-01 16:26:33 -0600262 mbr_wbuf = map_sysmem(mbr_wa, BLKSZ);
263 ebr_wbuf = map_sysmem(ebr_wa, BLKSZ);
264 rbuf = map_sysmem(ra, BLKSZ);
Simon Glassa8c12522024-09-01 16:26:32 -0600265 ebr_blk = (ulong)0xb00000 / BLKSZ;
Alexander Gendin038cb022023-10-09 01:24:36 +0000266
267 /* Make sure mmc6 exists */
268 ut_asserteq(6, blk_get_device_by_str("mmc", "6", &mmc_dev_desc));
Alexander Gendin038cb022023-10-09 01:24:36 +0000269 ut_assertok(run_commandf("mmc dev 6"));
270 ut_assert_nextline("switch to partitions #0, OK");
271 ut_assert_nextline("mmc6 is current device");
Simon Glassc579bd42024-08-22 07:58:03 -0600272 ut_assert_console_end();
Alexander Gendin038cb022023-10-09 01:24:36 +0000273
274 /* Make sure mmc6 is 12+ MiB in size */
Simon Glass345567c2024-09-01 16:26:22 -0600275 ut_assertok(run_commandf("mmc read %lx %lx 1", ra,
Simon Glassa8c12522024-09-01 16:26:32 -0600276 (ulong)0xbffe00 / BLKSZ));
Alexander Gendin038cb022023-10-09 01:24:36 +0000277
278 /* Test one MBR partition */
Heinrich Schuchardtea27c842024-11-02 16:41:30 +0100279 init_write_buffers(mbr_wbuf, BLKSZ, ebr_wbuf, BLKSZ, __LINE__);
Alexander Gendin038cb022023-10-09 01:24:36 +0000280 ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 1));
Simon Glass345567c2024-09-01 16:26:22 -0600281 ut_assertok(run_commandf("write mmc 6:0 %lx 0 1", mbr_wa));
Simon Glass2ca519b2024-09-01 16:26:33 -0600282 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600283 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Simon Glassa8c12522024-09-01 16:26:32 -0600284 ut_assertok(memcmp(mbr_wbuf, rbuf, BLKSZ));
Simon Glass345567c2024-09-01 16:26:22 -0600285 ut_assertok(run_commandf("write mmc 6:0 %lx %lx 1", ebr_wa, ebr_blk));
Simon Glass2ca519b2024-09-01 16:26:33 -0600286 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600287 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600288 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Alexander Gendin038cb022023-10-09 01:24:36 +0000289 ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
290 ut_assertok(run_commandf("mbr write mmc 6"));
Simon Glass56228252024-08-22 07:57:58 -0600291 ut_assert_nextlinen("MMC read: dev # 6");
Alexander Gendin038cb022023-10-09 01:24:36 +0000292 ut_assert_nextline("MBR: write success!");
293 ut_assertok(run_commandf("mbr verify mmc 6"));
294 ut_assert_nextline("MBR: verify success!");
Simon Glass2ca519b2024-09-01 16:26:33 -0600295 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600296 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600297 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Simon Glassc579bd42024-08-22 07:58:03 -0600298 ut_assert_console_end();
Alexander Gendin038cb022023-10-09 01:24:36 +0000299 /*
300 000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
301 000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 00 |...%$..@........|
302 000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
303 000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
304 000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
305 */
Simon Glass2ca519b2024-09-01 16:26:33 -0600306 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600307 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Alexander Gendin038cb022023-10-09 01:24:36 +0000308 for (unsigned i = 0; i < mbr_cmp_size; i++) {
309 ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p1[i],
Simon Glass345567c2024-09-01 16:26:22 -0600310 "1P MBR+0x%04X: expected %#02X, actual: %#02X\n",
Alexander Gendin038cb022023-10-09 01:24:36 +0000311 mbr_cmp_start + i, mbr_parts_ref_p1[i], rbuf[mbr_cmp_start + i]);
312 }
313
314 /* Test two MBR partitions */
315 init_write_buffers(mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
316 ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 2));
Simon Glass345567c2024-09-01 16:26:22 -0600317 ut_assertok(run_commandf("write mmc 6:0 %lx 0 1", mbr_wa));
Simon Glass2ca519b2024-09-01 16:26:33 -0600318 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600319 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Simon Glassa8c12522024-09-01 16:26:32 -0600320 ut_assertok(memcmp(mbr_wbuf, rbuf, BLKSZ));
Simon Glass345567c2024-09-01 16:26:22 -0600321 ut_assertok(run_commandf("write mmc 6:0 %lx %lx 1", ebr_wa, ebr_blk));
Simon Glass2ca519b2024-09-01 16:26:33 -0600322 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600323 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600324 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Alexander Gendin038cb022023-10-09 01:24:36 +0000325 ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
326 ut_assertok(run_commandf("mbr write mmc 6"));
327 ut_assert_nextline("MBR: write success!");
328 ut_assertok(run_commandf("mbr verify mmc 6"));
329 ut_assert_nextline("MBR: verify success!");
Simon Glass2ca519b2024-09-01 16:26:33 -0600330 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600331 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600332 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Simon Glassc579bd42024-08-22 07:58:03 -0600333 ut_assert_console_end();
Alexander Gendin038cb022023-10-09 01:24:36 +0000334 /*
335 000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
336 000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
337 000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 00 |%..F...H........|
338 000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
339 000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
340 */
Simon Glass2ca519b2024-09-01 16:26:33 -0600341 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600342 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Alexander Gendin038cb022023-10-09 01:24:36 +0000343 for (unsigned i = 0; i < mbr_cmp_size; i++) {
344 ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p2[i],
Simon Glass345567c2024-09-01 16:26:22 -0600345 "2P MBR+0x%04X: expected %#02X, actual: %#02X\n",
Alexander Gendin038cb022023-10-09 01:24:36 +0000346 mbr_cmp_start + i, mbr_parts_ref_p2[i], rbuf[mbr_cmp_start + i]);
347 }
348
349 /* Test three MBR partitions */
350 init_write_buffers(mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
351 ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 3));
Simon Glass345567c2024-09-01 16:26:22 -0600352 ut_assertok(run_commandf("write mmc 6:0 %lx 0 1", mbr_wa));
Simon Glass2ca519b2024-09-01 16:26:33 -0600353 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600354 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Simon Glassa8c12522024-09-01 16:26:32 -0600355 ut_assertok(memcmp(mbr_wbuf, rbuf, BLKSZ));
Simon Glass345567c2024-09-01 16:26:22 -0600356 ut_assertok(run_commandf("write mmc 6:0 %lx %lx 1", ebr_wa, ebr_blk));
Simon Glass2ca519b2024-09-01 16:26:33 -0600357 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600358 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600359 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Alexander Gendin038cb022023-10-09 01:24:36 +0000360 ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
361 ut_assertok(run_commandf("mbr write mmc 6"));
362 ut_assert_nextline("MBR: write success!");
363 ut_assertok(run_commandf("mbr verify mmc 6"));
364 ut_assert_nextline("MBR: verify success!");
Simon Glass2ca519b2024-09-01 16:26:33 -0600365 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600366 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600367 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Simon Glassc579bd42024-08-22 07:58:03 -0600368 ut_assert_console_end();
Alexander Gendin038cb022023-10-09 01:24:36 +0000369 /*
370 000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
371 000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
372 000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 46 |%..F...H.......F|
373 000001e0 06 01 0e 66 25 01 00 50 00 00 00 08 00 00 00 00 |...f%..P........|
374 000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
375 */
Simon Glass2ca519b2024-09-01 16:26:33 -0600376 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600377 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Alexander Gendin038cb022023-10-09 01:24:36 +0000378 for (unsigned i = 0; i < mbr_cmp_size; i++) {
379 ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p3[i],
Simon Glass345567c2024-09-01 16:26:22 -0600380 "3P MBR+0x%04X: expected %#02X, actual: %#02X\n",
Alexander Gendin038cb022023-10-09 01:24:36 +0000381 mbr_cmp_start + i, mbr_parts_ref_p3[i], rbuf[mbr_cmp_start + i]);
382 }
383
384 /* Test four MBR partitions */
385 init_write_buffers(mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
386 ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 4));
Simon Glass345567c2024-09-01 16:26:22 -0600387 ut_assertok(run_commandf("write mmc 6:0 %lx 0 1", mbr_wa));
Simon Glass2ca519b2024-09-01 16:26:33 -0600388 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600389 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Simon Glassa8c12522024-09-01 16:26:32 -0600390 ut_assertok(memcmp(mbr_wbuf, rbuf, BLKSZ));
Simon Glass345567c2024-09-01 16:26:22 -0600391 ut_assertok(run_commandf("write mmc 6:0 %lx %lx 1", ebr_wa, ebr_blk));
Simon Glass2ca519b2024-09-01 16:26:33 -0600392 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600393 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600394 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Alexander Gendin038cb022023-10-09 01:24:36 +0000395 ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
396 ut_assertok(run_commandf("mbr write mmc 6"));
397 ut_assert_nextline("MBR: write success!");
398 ut_assertok(run_commandf("mbr verify mmc 6"));
399 ut_assert_nextline("MBR: verify success!");
Simon Glass2ca519b2024-09-01 16:26:33 -0600400 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600401 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600402 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Simon Glassc579bd42024-08-22 07:58:03 -0600403 ut_assert_console_end();
Alexander Gendin038cb022023-10-09 01:24:36 +0000404 /*
405 000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
406 000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
407 000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 46 |%..F...H.......F|
408 000001e0 06 01 0e 66 25 01 00 50 00 00 00 08 00 00 00 66 |...f%..P.......f|
409 000001f0 26 01 0e 87 06 01 00 58 00 00 00 08 00 00 55 aa |&......X......U.|
410 */
Simon Glass2ca519b2024-09-01 16:26:33 -0600411 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600412 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Alexander Gendin038cb022023-10-09 01:24:36 +0000413 for (unsigned i = 0; i < mbr_cmp_size; i++) {
414 ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p4[i],
Simon Glass345567c2024-09-01 16:26:22 -0600415 "4P MBR+0x%04X: expected %#02X, actual: %#02X\n",
Alexander Gendin038cb022023-10-09 01:24:36 +0000416 mbr_cmp_start + i, mbr_parts_ref_p4[i], rbuf[mbr_cmp_start + i]);
417 }
418
419 /* Test five MBR partitions */
420 init_write_buffers(mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
421 ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 5));
Simon Glass345567c2024-09-01 16:26:22 -0600422 ut_assertok(run_commandf("write mmc 6:0 %lx 0 1", mbr_wa));
Simon Glass2ca519b2024-09-01 16:26:33 -0600423 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600424 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Simon Glassa8c12522024-09-01 16:26:32 -0600425 ut_assertok(memcmp(mbr_wbuf, rbuf, BLKSZ));
Simon Glass345567c2024-09-01 16:26:22 -0600426 ut_assertok(run_commandf("write mmc 6:0 %lx %lx 1", ebr_wa, ebr_blk));
Simon Glass2ca519b2024-09-01 16:26:33 -0600427 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600428 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Simon Glassa8c12522024-09-01 16:26:32 -0600429 ut_assertok(memcmp(ebr_wbuf, rbuf, BLKSZ));
Alexander Gendin038cb022023-10-09 01:24:36 +0000430 ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
431 ut_assertf(0 == run_commandf("mbr write mmc 6"), "Invalid partitions string: %s\n", mbr_parts_buf);
432 ut_assert_nextline("MBR: write success!");
433 ut_assertok(run_commandf("mbr verify mmc 6"));
434 ut_assert_nextline("MBR: verify success!");
Simon Glassc579bd42024-08-22 07:58:03 -0600435 ut_assert_console_end();
Alexander Gendin038cb022023-10-09 01:24:36 +0000436 /*
437 000001b0 00 00 00 00 00 00 00 00 78 56 34 12 00 00 80 05 |........xV4.....|
438 000001c0 05 01 0e 25 24 01 00 40 00 00 00 08 00 00 00 25 |...%$..@.......%|
439 000001d0 25 01 0e 46 05 01 00 48 00 00 00 08 00 00 00 46 |%..F...H.......F|
440 000001e0 06 01 0e 66 25 01 00 50 00 00 00 08 00 00 00 66 |...f%..P.......f|
441 000001f0 26 01 05 a7 26 01 00 58 00 00 00 10 00 00 55 aa |&...&..X......U.|
442 */
Simon Glass2ca519b2024-09-01 16:26:33 -0600443 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600444 ut_assertok(run_commandf("read mmc 6:0 %lx 0 1", ra));
Alexander Gendin038cb022023-10-09 01:24:36 +0000445 for (unsigned i = 0; i < mbr_cmp_size; i++) {
446 ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p5[i],
Simon Glass345567c2024-09-01 16:26:22 -0600447 "5P MBR+0x%04X: expected %#02X, actual: %#02X\n",
Alexander Gendin038cb022023-10-09 01:24:36 +0000448 mbr_cmp_start + i, mbr_parts_ref_p5[i], rbuf[mbr_cmp_start + i]);
449 }
450 /*
451 00b001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 87 |................|
452 00b001c0 07 01 0e a7 26 01 00 08 00 00 00 08 00 00 00 00 |....&...........|
453 00b001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
454 00b001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
455 00b001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
456 */
Simon Glass2ca519b2024-09-01 16:26:33 -0600457 memset(rbuf, '\0', BLKSZ);
Simon Glass345567c2024-09-01 16:26:22 -0600458 ut_assertok(run_commandf("read mmc 6:0 %lx %lx 1", ra, ebr_blk));
Alexander Gendin038cb022023-10-09 01:24:36 +0000459 for (unsigned i = 0; i < ebr_cmp_size; i++) {
460 ut_assertf(rbuf[ebr_cmp_start + i] == ebr_parts_ref_p5[i],
Simon Glass345567c2024-09-01 16:26:22 -0600461 "5P EBR+0x%04X: expected %#02X, actual: %#02X\n",
Alexander Gendin038cb022023-10-09 01:24:36 +0000462 ebr_cmp_start + i, ebr_parts_ref_p5[i], rbuf[ebr_cmp_start + i]);
463 }
Simon Glass2a1f1692024-09-01 16:26:31 -0600464 unmap_sysmem(mbr_wbuf);
465 unmap_sysmem(ebr_wbuf);
466 unmap_sysmem(rbuf);
Alexander Gendin038cb022023-10-09 01:24:36 +0000467
468 return 0;
469}
470
471/* Declare mbr test */
Simon Glassb15512c2025-01-20 14:25:32 -0700472UNIT_TEST(mbr_test_run, UTF_CONSOLE, mbr);