Skip to contentSkip to conversation

cmd: mbr: Allow 4 MBR partitions without need for extended

PATCH Superseded Alexander Gendin 4 participants 6 messages Oct 7, 2023 – Oct 24, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cmd: mbr: Allow 4 MBR partitions without need for extended

Current code allows up to 3 MBR partitions without extended one. If more than 3 partitions are required, then extended partition(s) must be used.
This commit allows up to 4 primary MBR partitions without the need for extended partition.

Add mbr test unit. In order to use the test, mmc1.img file of size 12 MiB or greater is required in the same directory as u-boot. Running mbr test is only supported in sandbox mode.

---
 disk/part_dos.c       |   2 +-
 include/test/suites.h |   1 +
 test/cmd/Makefile     |   1 +
 test/cmd/mbr.c        | 440 ++++++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c         |   4 +
 5 files changed, 447 insertions(+), 1 deletion(-)
 create mode 100644 test/cmd/mbr.c
disk/part_dos.c +1 -1
index 3337438437..567ead7511 100644
@@ -466,7 +466,7 @@ int layout_mbr_partitions(struct disk_partition *p, int count,
 			ext = &p[i];
 	}
 
-	if (count < 4)
+	if (count <= 4)
 		return 0;
 
 	if (!ext) {
include/test/suites.h +1 -0
index 1c7dc65966..51acbe47b2 100644
@@ -45,6 +45,7 @@ int do_ut_font(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_lib(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_loadm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_log(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
+int do_ut_mbr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_mem(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_optee(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc,
test/cmd/Makefile +1 -0
index 6e3d7e919e..2f251e07b4 100644
@@ -23,6 +23,7 @@ obj-$(CONFIG_CMD_PINMUX) += pinmux.o
 obj-$(CONFIG_CMD_PWM) += pwm.o
 obj-$(CONFIG_CMD_SEAMA) += seama.o
 ifdef CONFIG_SANDBOX
+obj-$(CONFIG_CMD_MBR) += mbr.o
 obj-$(CONFIG_CMD_READ) += rw.o
 obj-$(CONFIG_CMD_SETEXPR) += setexpr.o
 obj-$(CONFIG_ARM_FFA_TRANSPORT) += armffa.o
test/cmd/mbr.c new file +440 -0

440 lines not on this page · raw message

test/cmd_ut.c +4 -0
index 0f56409e80..123db7cc7d 100644
@@ -86,6 +86,10 @@ static struct cmd_tbl cmd_ut_sub[] = {
 #endif
 #ifdef CONFIG_UT_LOG
 	U_BOOT_CMD_MKENT(log, CONFIG_SYS_MAXARGS, 1, do_ut_log, "", ""),
+#endif
+#if defined(CONFIG_SANDBOX) && defined(CONFIG_CMD_MBR) && defined(CONFIG_CMD_MMC) \
+        && defined(CONFIG_MMC_SANDBOX) && defined(CONFIG_MMC_WRITE)
+	U_BOOT_CMD_MKENT(mbr, CONFIG_SYS_MAXARGS, 1, do_ut_mbr, "", ""),
 #endif
 	U_BOOT_CMD_MKENT(mem, CONFIG_SYS_MAXARGS, 1, do_ut_mem, "", ""),
 #if defined(CONFIG_SANDBOX) && defined(CONFIG_CMD_SETEXPR)
Signature · 1 line
2.41.0
#1 lore raw

Hi Alexander,

Quoted 12 lines · Alexander Gendin

Current code allows up to 3 MBR partitions without extended one. If more than 3 partitions are required, then extended partition(s) must be used.
This commit allows up to 4 primary MBR partitions without the need for extended partition.

Add mbr test unit. In order to use the test, mmc1.img file of size 12 MiB or greater is required in the same directory as u-boot. Running mbr test is only supported in sandbox mode.

Quoted 8 lines
---
 disk/part_dos.c       |   2 +-
 include/test/suites.h |   1 +
 test/cmd/Makefile     |   1 +
 test/cmd/mbr.c        | 440 ++++++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c         |   4 +
 5 files changed, 447 insertions(+), 1 deletion(-)
 create mode 100644 test/cmd/mbr.c

This looks OK apart from one thing...also I have a few suggestions / questions below.

Quoted 62 lines
disk/part_dos.c +1 -1
index 3337438437..567ead7511 100644
@@ -466,7 +466,7 @@ int layout_mbr_partitions(struct disk_partition *p, int count,
                        ext = &p[i];
        }
 
-       if (count < 4)
+       if (count <= 4)
                return 0;
 
        if (!ext) {
include/test/suites.h +1 -0
index 1c7dc65966..51acbe47b2 100644
@@ -45,6 +45,7 @@ int do_ut_font(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_lib(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_loadm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_log(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
+int do_ut_mbr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_mem(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_optee(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc,
test/cmd/Makefile +1 -0
index 6e3d7e919e..2f251e07b4 100644
@@ -23,6 +23,7 @@ obj-$(CONFIG_CMD_PINMUX) += pinmux.o
 obj-$(CONFIG_CMD_PWM) += pwm.o
 obj-$(CONFIG_CMD_SEAMA) += seama.o
 ifdef CONFIG_SANDBOX
+obj-$(CONFIG_CMD_MBR) += mbr.o
 obj-$(CONFIG_CMD_READ) += rw.o
 obj-$(CONFIG_CMD_SETEXPR) += setexpr.o
 obj-$(CONFIG_ARM_FFA_TRANSPORT) += armffa.o
test/cmd/mbr.c new file +18 -0
new file mode 100644
index 0000000000..bedcef0638
@@ -0,0 +1,440 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for mbr command
+ *
+ * Copyright 2023 Matrox Video
+ * Written by Alex Gendin <[email protected]>
+ */
+
+#include <common.h>
+#include <console.h>
+#include <mapmem.h>
+#include <part.h>
+#include <asm/global_data.h>
+#include <test/suites.h>
+#include <test/ut.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+/*

test/cmd/mbr.c line 28

Quoted 10 lines
test/cmd/mbr.c new file +10 -0
+ * Test requirements:
+ * mmc1.img - File size needs to be at least 12 MiB
+ *
+ * Command to create mmc1.img:
+ * $ dd if=/dev/zero of=mmc1.img bs=12M count=1
+ *
+ * Place mmc1.img into the same directory as sandboxed u-boot
+ *
+ * To run this test manually:
+ * $ ./u-boot -Tc 'ut mbr'

These requirements need to be dealt with by a test script. You can set up this device in test_ut_dm_init_bootstd(). You may need to create a new mmc as the others are in use.

Quoted 98 lines
test/cmd/mbr.c new file +98 -0
+ */
+
+static char * mbr_parts_header = "setenv mbr_parts '";
+static char * mbr_parts_p1 = "uuid_disk=0x12345678;name=p1,start=8M,bootable,size=1M,id=0x0e";
+static char * mbr_parts_p2 = ";name=p2,size=1M,id=0x0e";
+static char * mbr_parts_p3 = ";name=p3,size=1M,id=0x0e";
+static char * mbr_parts_p4 = ";name=p4,size=1M,id=0x0e";
+static char * mbr_parts_p5 = ";name=[ext],size=2M,id=0x05;name=p5,size=1M,id=0x0e";
+static char * mbr_parts_tail = "'";
+
+/*
+ * One MBR partition
+000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 00  |...%$..@........|
+000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+*/
+static unsigned mbr_cmp_start = 0x1B8;
+static unsigned mbr_cmp_size = 0x48;
+static unsigned char mbr_parts_ref_p1[] = {
+                                                0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
+0x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
+};
+
+/*
+ * Two MBR partitions
+000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 00  |%..F...H........|
+000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+*/
+static unsigned char mbr_parts_ref_p2[] = {
+                                                0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
+0x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
+0x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
+};
+
+/*
+ * Three MBR partitions
+000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 46  |%..F...H.......F|
+000001e0  06 01 0e 66 25 01 00 50  00 00 00 08 00 00 00 00  |...f%..P........|
+000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+*/
+static unsigned char mbr_parts_ref_p3[] = {
+                                                0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
+0x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
+0x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x46,
+0x06, 0x01, 0x0e, 0x66, 0x25, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
+};
+
+/*
+ * Four MBR partitions
+000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 46  |%..F...H.......F|
+000001e0  06 01 0e 66 25 01 00 50  00 00 00 08 00 00 00 66  |...f%..P.......f|
+000001f0  26 01 0e 87 06 01 00 58  00 00 00 08 00 00 55 aa  |&......X......U.|
+*/
+static unsigned char mbr_parts_ref_p4[] = {
+                                                0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
+0x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
+0x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x46,
+0x06, 0x01, 0x0e, 0x66, 0x25, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x66,
+0x26, 0x01, 0x0e, 0x87, 0x06, 0x01, 0x00, 0x58, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x55, 0xaa
+};
+
+/*
+ * Five MBR partitions
+000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 46  |%..F...H.......F|
+000001e0  06 01 0e 66 25 01 00 50  00 00 00 08 00 00 00 66  |...f%..P.......f|
+000001f0  26 01 05 a7 26 01 00 58  00 00 00 10 00 00 55 aa  |&...&..X......U.|
+*/
+static unsigned char mbr_parts_ref_p5[] = {
+                                                0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
+0x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
+0x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x46,
+0x06, 0x01, 0x0e, 0x66, 0x25, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x66,
+0x26, 0x01, 0x05, 0xa7, 0x26, 0x01, 0x00, 0x58, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x55, 0xaa
+};
+static unsigned ebr_cmp_start = 0x1B8;
+static unsigned ebr_cmp_size = 0x48;
+/*
+ * EBR
+00b001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 87  |................|
+00b001c0  07 01 0e a7 26 01 00 08  00 00 00 08 00 00 00 00  |....&...........|
+00b001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

test/cmd/mbr.c line 136

Quoted 10 lines
test/cmd/mbr.c new file +10 -0
+00b001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+00b001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+*/
+static unsigned char ebr_parts_ref_p5[] = {
+                                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87,
+0x07, 0x01, 0x0e, 0xa7, 0x26, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
+};

What do you think about creating these in code? E.g. have a function which outputs the bytes for a partition and then call it 1 to 4 times?

test/cmd/mbr.c line 139

Quoted 3 lines
test/cmd/mbr.c new file +3 -0
+
+static void init_buffers(char *rb, size_t rb_size, char *mbr_wb, size_t mbr_wb_size,
+                       char *ebr_wb, size_t ebr_wb_size, unsigned seed)

Needs a comment

Quoted 11 lines
test/cmd/mbr.c new file +11 -0
+{
+       memset(rb, 0, rb_size);
+       while (mbr_wb_size--) {
+               *mbr_wb++ = seed;
+               seed *= 43;
+               seed += 17 + mbr_wb_size/4;
+       }
+       while (ebr_wb_size--) {
+               *ebr_wb++ = seed;
+               seed *= 43;
+               seed += 17 + ebr_wb_size/4;

test/cmd/mbr.c line 160

Quoted 10 lines
test/cmd/mbr.c new file +10 -0
+       }
+}
+
+static unsigned build_mbr_parts(char *buf, size_t buf_size, unsigned num_parts)
+{
+       size_t bytes_remaining, cur_str_size;
+       char * cur_buf;
+
+       if (0 == num_parts || 5 < num_parts || NULL == buf)
+               return 1;

!num_parts
!buf

(we normally put the var before the value, so numparts >= 5)

test/cmd/mbr.c line 167

Quoted 7 lines
test/cmd/mbr.c new file +7 -0
+
+       cur_buf = buf;
+       *cur_buf = '\0';
+       bytes_remaining = buf_size;
+
+       cur_str_size = sizeof(mbr_parts_header);
+       if (1 + cur_str_size > bytes_remaining)

Again, better to use cur_str_size + 1

Quoted 71 lines
test/cmd/mbr.c new file +71 -0
+               return 1;
+       strcat(cur_buf, mbr_parts_header);
+       bytes_remaining -= cur_str_size;
+
+       if (1 <= num_parts) {
+               cur_str_size = sizeof(mbr_parts_p1);
+               if (1 + cur_str_size > bytes_remaining)
+                       return 1;
+               strcat(cur_buf, mbr_parts_p1);
+               bytes_remaining -= cur_str_size;
+
+               if (2 <= num_parts) {
+                       cur_str_size = sizeof(mbr_parts_p2);
+                       if (1 + cur_str_size > bytes_remaining)
+                               return 1;
+                       strcat(cur_buf, mbr_parts_p2);
+                       bytes_remaining -= cur_str_size;
+
+                       if (3 <= num_parts) {
+                               cur_str_size = sizeof(mbr_parts_p3);
+                               if (1 + cur_str_size > bytes_remaining)
+                                       return 1;
+                               strcat(cur_buf, mbr_parts_p3);
+                               bytes_remaining -= cur_str_size;
+
+                               if (4 == num_parts) {
+                                       cur_str_size = sizeof(mbr_parts_p4);
+                                       if (1 + cur_str_size > bytes_remaining)
+                                               return 1;
+                                       strcat(cur_buf, mbr_parts_p4);
+                                       bytes_remaining -= cur_str_size;
+
+                               }
+                       else if (5 == num_parts) {
+                               cur_str_size = sizeof(mbr_parts_p5);
+                               if (1 + cur_str_size > bytes_remaining)
+                                       return 1;
+                               strcat(cur_buf, mbr_parts_p5);
+                               bytes_remaining -= cur_str_size;
+
+                       }
+                       else if (5 < num_parts)
+                               return 1;
+                       }
+               }
+       }
+
+       cur_str_size = sizeof(mbr_parts_tail);
+       if (1 + cur_str_size > bytes_remaining)
+               return 1;
+       strcat(cur_buf, mbr_parts_tail);
+
+       return 0;
+}
+
+static int mbr_test_run(struct unit_test_state *uts)
+{
+       struct blk_desc *mmc_dev_desc;
+       unsigned char mbr_wbuf[512], ebr_wbuf[512], rbuf[512];
+       char mbr_parts_buf[256];
+       ulong mbr_wa, ebr_wa, ra, ebr_blk, mbr_parts_max;
+
+       mbr_parts_max = 2 + sizeof('\0')
+               + strlen(mbr_parts_header)
+               + strlen(mbr_parts_p1)
+               + strlen(mbr_parts_p2)
+               + strlen(mbr_parts_p3)
+               + strlen(mbr_parts_p4)
+               + strlen(mbr_parts_p5)
+               + strlen(mbr_parts_tail);
+       ut_assertf(sizeof(mbr_parts_buf) >= mbr_parts_max, "Buffer avail: %ld; buffer req: %ld\n",

test/cmd/mbr.c line 248

Quoted 10 lines
test/cmd/mbr.c new file +10 -0
+               sizeof(mbr_parts_buf), mbr_parts_max);
+
+       mbr_wa = map_to_sysmem(mbr_wbuf);
+       ebr_wa = map_to_sysmem(ebr_wbuf);
+       ra = map_to_sysmem(rbuf);
+       ebr_blk = (ulong)0xB00000 / 0x200;
+
+       /* Make sure mmc1 exists */
+       ut_asserteq(1, blk_get_device_by_str("mmc", "1", &mmc_dev_desc));
+       ut_assertok(console_record_reset_enable());

You shouldn't need these

Quoted 208 lines
test/cmd/mbr.c new file +192 -0
+       ut_assertok(run_commandf("mmc dev 1"));
+       ut_assert_nextline("switch to partitions #0, OK");
+       ut_assert_nextline("mmc1 is current device");
+       ut_assertok(ut_check_console_end(uts));
+
+       /* Make sure mmc1 is 12+ MiB in size */
+       ut_assertok(run_commandf("mmc read 0x%lx 0x%lx 1", ra, (ulong)0xBFFE00 / 0x200));
+
+       /* Test one MBR partition */
+       init_buffers(rbuf, sizeof(rbuf), mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
+       ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 1));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0 1", mbr_wa));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       ut_assertok(memcmp(mbr_wbuf, rbuf, 512));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0x%lx 1", ebr_wa, ebr_blk));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(console_record_reset_enable());
+       ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
+       ut_assertok(run_commandf("mbr write mmc 1"));
+       ut_assert_nextline("MBR: write success!");
+       ut_assertok(run_commandf("mbr verify mmc 1"));
+       ut_assert_nextline("MBR: verify success!");
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(ut_check_console_end(uts));
+       /*
+       000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+       000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 00  |...%$..@........|
+       000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+       000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+       000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+       */
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       for (unsigned i = 0; i < mbr_cmp_size; i++) {
+               ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p1[i],
+                       "1P MBR+0x%04X: expected 0x%02X, actual: 0x%02X\n",
+                       mbr_cmp_start + i, mbr_parts_ref_p1[i], rbuf[mbr_cmp_start + i]);
+       }
+
+       /* Test two MBR partitions */
+       init_buffers(rbuf, sizeof(rbuf), mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
+       ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 2));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0 1", mbr_wa));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       ut_assertok(memcmp(mbr_wbuf, rbuf, 512));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0x%lx 1", ebr_wa, ebr_blk));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(console_record_reset_enable());
+       ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
+       ut_assertok(run_commandf("mbr write mmc 1"));
+       ut_assert_nextline("MBR: write success!");
+       ut_assertok(run_commandf("mbr verify mmc 1"));
+       ut_assert_nextline("MBR: verify success!");
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(ut_check_console_end(uts));
+       /*
+       000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+       000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+       000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 00  |%..F...H........|
+       000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+       000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+       */
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       for (unsigned i = 0; i < mbr_cmp_size; i++) {
+               ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p2[i],
+                       "2P MBR+0x%04X: expected 0x%02X, actual: 0x%02X\n",
+                       mbr_cmp_start + i, mbr_parts_ref_p2[i], rbuf[mbr_cmp_start + i]);
+       }
+
+       /* Test three MBR partitions */
+       init_buffers(rbuf, sizeof(rbuf), mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
+       ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 3));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0 1", mbr_wa));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       ut_assertok(memcmp(mbr_wbuf, rbuf, 512));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0x%lx 1", ebr_wa, ebr_blk));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(console_record_reset_enable());
+       ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
+       ut_assertok(run_commandf("mbr write mmc 1"));
+       ut_assert_nextline("MBR: write success!");
+       ut_assertok(run_commandf("mbr verify mmc 1"));
+       ut_assert_nextline("MBR: verify success!");
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(ut_check_console_end(uts));
+       /*
+       000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+       000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+       000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 46  |%..F...H.......F|
+       000001e0  06 01 0e 66 25 01 00 50  00 00 00 08 00 00 00 00  |...f%..P........|
+       000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+       */
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       for (unsigned i = 0; i < mbr_cmp_size; i++) {
+               ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p3[i],
+                       "3P MBR+0x%04X: expected 0x%02X, actual: 0x%02X\n",
+                       mbr_cmp_start + i, mbr_parts_ref_p3[i], rbuf[mbr_cmp_start + i]);
+       }
+
+       /* Test four MBR partitions */
+       init_buffers(rbuf, sizeof(rbuf), mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
+       ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 4));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0 1", mbr_wa));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       ut_assertok(memcmp(mbr_wbuf, rbuf, 512));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0x%lx 1", ebr_wa, ebr_blk));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(console_record_reset_enable());
+       ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
+       ut_assertok(run_commandf("mbr write mmc 1"));
+       ut_assert_nextline("MBR: write success!");
+       ut_assertok(run_commandf("mbr verify mmc 1"));
+       ut_assert_nextline("MBR: verify success!");
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(ut_check_console_end(uts));
+       /*
+       000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+       000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+       000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 46  |%..F...H.......F|
+       000001e0  06 01 0e 66 25 01 00 50  00 00 00 08 00 00 00 66  |...f%..P.......f|
+       000001f0  26 01 0e 87 06 01 00 58  00 00 00 08 00 00 55 aa  |&......X......U.|
+       */
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       for (unsigned i = 0; i < mbr_cmp_size; i++) {
+               ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p4[i],
+                       "4P MBR+0x%04X: expected 0x%02X, actual: 0x%02X\n",
+                       mbr_cmp_start + i, mbr_parts_ref_p4[i], rbuf[mbr_cmp_start + i]);
+       }
+
+       /* Test five MBR partitions */
+       init_buffers(rbuf, sizeof(rbuf), mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
+       ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 5));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0 1", mbr_wa));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       ut_assertok(memcmp(mbr_wbuf, rbuf, 512));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0x%lx 1", ebr_wa, ebr_blk));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(console_record_reset_enable());
+       ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
+       ut_assertf(0 == run_commandf("mbr write mmc 1"), "Invalid partitions string: %s\n", mbr_parts_buf);
+       ut_assert_nextline("MBR: write success!");
+       ut_assertok(run_commandf("mbr verify mmc 1"));
+       ut_assert_nextline("MBR: verify success!");
+       ut_assertok(ut_check_console_end(uts));
+       /*
+       000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+       000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+       000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 46  |%..F...H.......F|
+       000001e0  06 01 0e 66 25 01 00 50  00 00 00 08 00 00 00 66  |...f%..P.......f|
+       000001f0  26 01 05 a7 26 01 00 58  00 00 00 10 00 00 55 aa  |&...&..X......U.|
+       */
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       for (unsigned i = 0; i < mbr_cmp_size; i++) {
+               ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p5[i],
+                       "5P MBR+0x%04X: expected 0x%02X, actual: 0x%02X\n",
+                       mbr_cmp_start + i, mbr_parts_ref_p5[i], rbuf[mbr_cmp_start + i]);
+       }
+       /*
+       00b001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 87  |................|
+       00b001c0  07 01 0e a7 26 01 00 08  00 00 00 08 00 00 00 00  |....&...........|
+       00b001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+       00b001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+       00b001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+       */
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       for (unsigned i = 0; i < ebr_cmp_size; i++) {
+               ut_assertf(rbuf[ebr_cmp_start + i] == ebr_parts_ref_p5[i],
+                       "5P EBR+0x%04X: expected 0x%02X, actual: 0x%02X\n",
+                       ebr_cmp_start + i, ebr_parts_ref_p5[i], rbuf[ebr_cmp_start + i]);
+       }
+
+       return 0;
+}
+
+/* Declare mbr test */
+UNIT_TEST(mbr_test_run, UT_TESTF_CONSOLE_REC, mbr_test);
+
+int do_ut_mbr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+       struct unit_test *tests = UNIT_TEST_SUITE_START(mbr_test);
+       const int n_ents = UNIT_TEST_SUITE_COUNT(mbr_test);
+
+       return cmd_ut_category("mbr", "mbr_test_", tests, n_ents, argc, argv);
+}
test/cmd_ut.c +4 -1
index 0f56409e80..123db7cc7d 100644
@@ -86,6 +86,10 @@ static struct cmd_tbl cmd_ut_sub[] = {
 #endif
 #ifdef CONFIG_UT_LOG
        U_BOOT_CMD_MKENT(log, CONFIG_SYS_MAXARGS, 1, do_ut_log, "", ""),
+#endif
+#if defined(CONFIG_SANDBOX) && defined(CONFIG_CMD_MBR) && defined(CONFIG_CMD_MMC) \
+        && defined(CONFIG_MMC_SANDBOX) && defined(CONFIG_MMC_WRITE)
+       U_BOOT_CMD_MKENT(mbr, CONFIG_SYS_MAXARGS, 1, do_ut_mbr, "", ""),
 #endif
        U_BOOT_CMD_MKENT(mem, CONFIG_SYS_MAXARGS, 1, do_ut_mem, "", ""),
 #if defined(CONFIG_SANDBOX) && defined(CONFIG_CMD_SETEXPR)
--
Quoted 1 line

2.41.0

Regards,
Simon

#2 lore raw

PATCH v2 1/1 cmd: mbr: Allow 4 MBR partitions without need for extended

Current code allows up to 3 MBR partitions without extended one. If more than 3 partitions are required, then extended partition(s) must be used.
This commit allows up to 4 primary MBR partitions without the need for extended partition.

Add mbr test unit. In order to run the test manually, mmc6.img file of size 12 MiB or greater is required in the same directory as u-boot. Test also runs automatically via ./test/py/test.py tool. Running mbr test is only supported in sandbox mode.

---
Changes for v2:
- Cleanup coding style
- Adjust commit message to the changes in v2
- Add mmc6 device to sandbox device tree
- Adjust boot/bootdev.c and dm/blk.c tests to include mmc6 in tests
- Auto-create mmc6.img in test_ut_dm_init()
- Add mbr test to the list of tests run by test.py. Test can be run manually too via 'ut mbr'.
- Change mbr test to use mmc6.img dedicated to the test, instead of mmc1.img.
- Clear read buffer before reading data from test file.

 arch/sandbox/dts/test.dts |   7 +
 disk/part_dos.c           |   2 +-
 include/test/suites.h     |   1 +
 test/boot/bootdev.c       |   2 +-
 test/cmd/Makefile         |   1 +
 test/cmd/mbr.c            | 471 ++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c             |   4 +
 test/dm/blk.c             |  44 ++--
 test/py/tests/test_ut.py  |   7 +-
 9 files changed, 522 insertions(+), 17 deletions(-)
 create mode 100644 test/cmd/mbr.c
arch/sandbox/dts/test.dts +7 -0
index 6abce9e396..59b7f8ef10 100644
@@ -41,6 +41,7 @@
 		mmc3 = "/mmc3";
 		mmc4 = "/mmc4";
 		mmc5 = "/mmc5";
+		mmc6 = "/mmc6";
 		pci0 = &pci0;
 		pci1 = &pci1;
 		pci2 = &pci2;
@@ -1101,6 +1102,12 @@
 		filename = "mmc5.img";
 	};
 
+	/* This is used for mbr tests */
+	mmc6 {
+		compatible = "sandbox,mmc";
+		filename = "mmc6.img";
+	};
+
 	pch {
 		compatible = "sandbox,pch";
 	};
disk/part_dos.c +1 -1
index 3337438437..567ead7511 100644
@@ -466,7 +466,7 @@ int layout_mbr_partitions(struct disk_partition *p, int count,
 			ext = &p[i];
 	}
 
-	if (count < 4)
+	if (count <= 4)
 		return 0;
 
 	if (!ext) {
include/test/suites.h +1 -0
index 1c7dc65966..51acbe47b2 100644
@@ -45,6 +45,7 @@ int do_ut_font(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_lib(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_loadm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_log(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
+int do_ut_mbr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_mem(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_optee(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc,
test/boot/bootdev.c +1 -1
index 6b29213416..4d125104c7 100644
@@ -128,7 +128,7 @@ static int bootdev_test_labels(struct unit_test_state *uts)
 		    bootdev_find_by_label("fred0", &dev, &mflags));
 
 	/* Check unknown sequence number */
-	ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev, &mflags));
+	ut_asserteq(-ENOENT, bootdev_find_by_label("mmc7", &dev, &mflags));
 
 	return 0;
 }
test/cmd/Makefile +1 -0
index 6e3d7e919e..2f251e07b4 100644
@@ -23,6 +23,7 @@ obj-$(CONFIG_CMD_PINMUX) += pinmux.o
 obj-$(CONFIG_CMD_PWM) += pwm.o
 obj-$(CONFIG_CMD_SEAMA) += seama.o
 ifdef CONFIG_SANDBOX
+obj-$(CONFIG_CMD_MBR) += mbr.o
 obj-$(CONFIG_CMD_READ) += rw.o
 obj-$(CONFIG_CMD_SETEXPR) += setexpr.o
 obj-$(CONFIG_ARM_FFA_TRANSPORT) += armffa.o
test/cmd/mbr.c new file +471 -0

471 lines not on this page · raw message

test/cmd_ut.c +4 -0
index 0f56409e80..123db7cc7d 100644
@@ -86,6 +86,10 @@ static struct cmd_tbl cmd_ut_sub[] = {
 #endif
 #ifdef CONFIG_UT_LOG
 	U_BOOT_CMD_MKENT(log, CONFIG_SYS_MAXARGS, 1, do_ut_log, "", ""),
+#endif
+#if defined(CONFIG_SANDBOX) && defined(CONFIG_CMD_MBR) && defined(CONFIG_CMD_MMC) \
+        && defined(CONFIG_MMC_SANDBOX) && defined(CONFIG_MMC_WRITE)
+	U_BOOT_CMD_MKENT(mbr, CONFIG_SYS_MAXARGS, 1, do_ut_mbr, "", ""),
 #endif
 	U_BOOT_CMD_MKENT(mem, CONFIG_SYS_MAXARGS, 1, do_ut_mem, "", ""),
 #if defined(CONFIG_SANDBOX) && defined(CONFIG_CMD_SETEXPR)
test/dm/blk.c +30 -14

117 lines not on this page · raw message

test/py/tests/test_ut.py +6 -1
index 82932a662b..1d9149a3f6 100644
@@ -433,7 +433,6 @@ def setup_cedit_file(cons):
     u_boot_utils.run_and_log(
         cons, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}')
 
-
 @pytest.mark.buildconfigspec('ut_dm')
 def test_ut_dm_init(u_boot_console):
     """Initialize data for ut dm tests."""
@@ -463,6 +462,12 @@ def test_ut_dm_init(u_boot_console):
     fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB')
     fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB')
 
+    mmc_dev = 6
+    fn = os.path.join(u_boot_console.config.source_dir, f'mmc{mmc_dev}.img')
+    data = b'\x00' * (12 * 1024 * 1024)
+    with open(fn, 'wb') as fh:
+        fh.write(data)
+
 @pytest.mark.buildconfigspec('cmd_bootflow')
 def test_ut_dm_init_bootstd(u_boot_console):
     """Initialise data for bootflow tests"""
Signature · 1 line
2.41.0
Quoted 95 lines · Simon Glass

Hi Alexander,

Quoted 12 lines · Alexander Gendin

Current code allows up to 3 MBR partitions without extended one. If more than 3 partitions are required, then extended partition(s) must be used.
This commit allows up to 4 primary MBR partitions without the need for extended partition.

Add mbr test unit. In order to use the test, mmc1.img file of size 12 MiB or greater is required in the same directory as u-boot. Running mbr test is only supported in sandbox mode.

Quoted 8 lines
---
 disk/part_dos.c       |   2 +-
 include/test/suites.h |   1 +
 test/cmd/Makefile     |   1 +
 test/cmd/mbr.c        | 440 ++++++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c         |   4 +
 5 files changed, 447 insertions(+), 1 deletion(-)
 create mode 100644 test/cmd/mbr.c

This looks OK apart from one thing...also I have a few suggestions / questions below.

Quoted 68 lines
disk/part_dos.c +1 -1
index 3337438437..567ead7511 100644
@@ -466,7 +466,7 @@ int layout_mbr_partitions(struct disk_partition *p, int count,
                        ext = &p[i];
        }
 
-       if (count < 4)
+       if (count <= 4)
                return 0;
 
        if (!ext) {
include/test/suites.h +1 -0
index 1c7dc65966..51acbe47b2 100644
@@ -45,6 +45,7 @@ int do_ut_font(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_lib(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_loadm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_log(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
+int do_ut_mbr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_mem(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_optee(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc,
test/cmd/Makefile +1 -0
index 6e3d7e919e..2f251e07b4 100644
@@ -23,6 +23,7 @@ obj-$(CONFIG_CMD_PINMUX) += pinmux.o
 obj-$(CONFIG_CMD_PWM) += pwm.o
 obj-$(CONFIG_CMD_SEAMA) += seama.o
 ifdef CONFIG_SANDBOX
+obj-$(CONFIG_CMD_MBR) += mbr.o
 obj-$(CONFIG_CMD_READ) += rw.o
 obj-$(CONFIG_CMD_SETEXPR) += setexpr.o
 obj-$(CONFIG_ARM_FFA_TRANSPORT) += armffa.o
test/cmd/mbr.c new file +24 -0
new file mode 100644
index 0000000000..bedcef0638
@@ -0,0 +1,440 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for mbr command
+ *
+ * Copyright 2023 Matrox Video
+ * Written by Alex Gendin <[email protected]>
+ */
+
+#include <common.h>
+#include <console.h>
+#include <mapmem.h>
+#include <part.h>
+#include <asm/global_data.h>
+#include <test/suites.h>
+#include <test/ut.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+/*
+ * Test requirements:
+ * mmc1.img - File size needs to be at least 12 MiB
+ *
+ * Command to create mmc1.img:
+ * $ dd if=/dev/zero of=mmc1.img bs=12M count=1
+ *
Quoted 8 lines

test/cmd/mbr.c line 28

Quoted 4 lines
test/cmd/mbr.c new file +4 -0
+ * Place mmc1.img into the same directory as sandboxed u-boot
+ *
+ * To run this test manually:
+ * $ ./u-boot -Tc 'ut mbr'

These requirements need to be dealt with by a test script. You can set up this device in test_ut_dm_init_bootstd(). You may need to create a new mmc as the others are in use.

Done, in test_ut_dm_init(). New mmc6 is added for this test.

Quoted 104 lines
Quoted 104 lines
test/cmd/mbr.c new file +103 -0
+ */
+
+static char * mbr_parts_header = "setenv mbr_parts '";
+static char * mbr_parts_p1 = "uuid_disk=0x12345678;name=p1,start=8M,bootable,size=1M,id=0x0e";
+static char * mbr_parts_p2 = ";name=p2,size=1M,id=0x0e";
+static char * mbr_parts_p3 = ";name=p3,size=1M,id=0x0e";
+static char * mbr_parts_p4 = ";name=p4,size=1M,id=0x0e";
+static char * mbr_parts_p5 = ";name=[ext],size=2M,id=0x05;name=p5,size=1M,id=0x0e";
+static char * mbr_parts_tail = "'";
+
+/*
+ * One MBR partition
+000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 00  |...%$..@........|
+000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+*/
+static unsigned mbr_cmp_start = 0x1B8;
+static unsigned mbr_cmp_size = 0x48;
+static unsigned char mbr_parts_ref_p1[] = {
+                                                0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
+0x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
+};
+
+/*
+ * Two MBR partitions
+000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 00  |%..F...H........|
+000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+*/
+static unsigned char mbr_parts_ref_p2[] = {
+                                                0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
+0x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
+0x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
+};
+
+/*
+ * Three MBR partitions
+000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 46  |%..F...H.......F|
+000001e0  06 01 0e 66 25 01 00 50  00 00 00 08 00 00 00 00  |...f%..P........|
+000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+*/
+static unsigned char mbr_parts_ref_p3[] = {
+                                                0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
+0x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
+0x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x46,
+0x06, 0x01, 0x0e, 0x66, 0x25, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
+};
+
+/*
+ * Four MBR partitions
+000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 46  |%..F...H.......F|
+000001e0  06 01 0e 66 25 01 00 50  00 00 00 08 00 00 00 66  |...f%..P.......f|
+000001f0  26 01 0e 87 06 01 00 58  00 00 00 08 00 00 55 aa  |&......X......U.|
+*/
+static unsigned char mbr_parts_ref_p4[] = {
+                                                0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
+0x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
+0x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x46,
+0x06, 0x01, 0x0e, 0x66, 0x25, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x66,
+0x26, 0x01, 0x0e, 0x87, 0x06, 0x01, 0x00, 0x58, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x55, 0xaa
+};
+
+/*
+ * Five MBR partitions
+000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 46  |%..F...H.......F|
+000001e0  06 01 0e 66 25 01 00 50  00 00 00 08 00 00 00 66  |...f%..P.......f|
+000001f0  26 01 05 a7 26 01 00 58  00 00 00 10 00 00 55 aa  |&...&..X......U.|
+*/
+static unsigned char mbr_parts_ref_p5[] = {
+                                                0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x80, 0x05,
+0x05, 0x01, 0x0e, 0x25, 0x24, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x25,
+0x25, 0x01, 0x0e, 0x46, 0x05, 0x01, 0x00, 0x48, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x46,
+0x06, 0x01, 0x0e, 0x66, 0x25, 0x01, 0x00, 0x50, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x66,
+0x26, 0x01, 0x05, 0xa7, 0x26, 0x01, 0x00, 0x58, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x55, 0xaa
+};
+static unsigned ebr_cmp_start = 0x1B8;
+static unsigned ebr_cmp_size = 0x48;
+/*
+ * EBR
+00b001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 87  |................|
+00b001c0  07 01 0e a7 26 01 00 08  00 00 00 08 00 00 00 00  |....&...........|
+00b001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+00b001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+00b001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+*/
+static unsigned char ebr_parts_ref_p5[] = {
+                                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87,
Quoted 8 lines

test/cmd/mbr.c line 136

Quoted 5 lines
test/cmd/mbr.c new file +5 -0
+0x07, 0x01, 0x0e, 0xa7, 0x26, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
+};

What do you think about creating these in code? E.g. have a function which outputs the bytes for a partition and then call it 1 to 4 times?

I don't follow exact question.
The reason for having these in code was to make sure that all bytes in partition table are properly programmed. The whole MBR sector is erased with random data before partition table is written by U-Boot.
The main objective here is to check how partition entries are processed from a text string. This is where the original bug was found, where it was impossible to have exactly 4 primary partitions in the MBR. So, the idea of a test was to create a partition layout as a text string with 1, 2, 3, then 4 primary partitions, write the layout into test file and then read layout back to make sure a proper result. Original code would fail attempting to create 4 primary partitions.

Quoted 23 lines

test/cmd/mbr.c line 139

Quoted 4 lines
test/cmd/mbr.c new file +3 -0
+
+static void init_buffers(char *rb, size_t rb_size, char *mbr_wb, size_t mbr_wb_size,
+                       char *ebr_wb, size_t ebr_wb_size, unsigned seed)

Needs a comment

Quoted 16 lines
test/cmd/mbr.c new file +16 -0
+{
+       memset(rb, 0, rb_size);
+       while (mbr_wb_size--) {
+               *mbr_wb++ = seed;
+               seed *= 43;
+               seed += 17 + mbr_wb_size/4;
+       }
+       while (ebr_wb_size--) {
+               *ebr_wb++ = seed;
+               seed *= 43;
+               seed += 17 + ebr_wb_size/4;
+       }
+}
+
+static unsigned build_mbr_parts(char *buf, size_t buf_size, unsigned num_parts)
+{
Quoted 8 lines

test/cmd/mbr.c line 160

Quoted 5 lines
test/cmd/mbr.c new file +5 -0
+       size_t bytes_remaining, cur_str_size;
+       char * cur_buf;
+
+       if (0 == num_parts || 5 < num_parts || NULL == buf)
+               return 1;

!num_parts
!buf

Done.

Quoted 12 lines

(we normally put the var before the value, so numparts >= 5)

test/cmd/mbr.c line 167

Quoted 7 lines
test/cmd/mbr.c new file +7 -0
+
+       cur_buf = buf;
+       *cur_buf = '\0';
+       bytes_remaining = buf_size;
+
+       cur_str_size = sizeof(mbr_parts_header);
+       if (1 + cur_str_size > bytes_remaining)

Again, better to use cur_str_size + 1

Changed the style accordingly for the whole patch.

Quoted 76 lines
Quoted 76 lines
test/cmd/mbr.c new file +75 -0
+               return 1;
+       strcat(cur_buf, mbr_parts_header);
+       bytes_remaining -= cur_str_size;
+
+       if (1 <= num_parts) {
+               cur_str_size = sizeof(mbr_parts_p1);
+               if (1 + cur_str_size > bytes_remaining)
+                       return 1;
+               strcat(cur_buf, mbr_parts_p1);
+               bytes_remaining -= cur_str_size;
+
+               if (2 <= num_parts) {
+                       cur_str_size = sizeof(mbr_parts_p2);
+                       if (1 + cur_str_size > bytes_remaining)
+                               return 1;
+                       strcat(cur_buf, mbr_parts_p2);
+                       bytes_remaining -= cur_str_size;
+
+                       if (3 <= num_parts) {
+                               cur_str_size = sizeof(mbr_parts_p3);
+                               if (1 + cur_str_size > bytes_remaining)
+                                       return 1;
+                               strcat(cur_buf, mbr_parts_p3);
+                               bytes_remaining -= cur_str_size;
+
+                               if (4 == num_parts) {
+                                       cur_str_size = sizeof(mbr_parts_p4);
+                                       if (1 + cur_str_size > bytes_remaining)
+                                               return 1;
+                                       strcat(cur_buf, mbr_parts_p4);
+                                       bytes_remaining -= cur_str_size;
+
+                               }
+                       else if (5 == num_parts) {
+                               cur_str_size = sizeof(mbr_parts_p5);
+                               if (1 + cur_str_size > bytes_remaining)
+                                       return 1;
+                               strcat(cur_buf, mbr_parts_p5);
+                               bytes_remaining -= cur_str_size;
+
+                       }
+                       else if (5 < num_parts)
+                               return 1;
+                       }
+               }
+       }
+
+       cur_str_size = sizeof(mbr_parts_tail);
+       if (1 + cur_str_size > bytes_remaining)
+               return 1;
+       strcat(cur_buf, mbr_parts_tail);
+
+       return 0;
+}
+
+static int mbr_test_run(struct unit_test_state *uts)
+{
+       struct blk_desc *mmc_dev_desc;
+       unsigned char mbr_wbuf[512], ebr_wbuf[512], rbuf[512];
+       char mbr_parts_buf[256];
+       ulong mbr_wa, ebr_wa, ra, ebr_blk, mbr_parts_max;
+
+       mbr_parts_max = 2 + sizeof('\0')
+               + strlen(mbr_parts_header)
+               + strlen(mbr_parts_p1)
+               + strlen(mbr_parts_p2)
+               + strlen(mbr_parts_p3)
+               + strlen(mbr_parts_p4)
+               + strlen(mbr_parts_p5)
+               + strlen(mbr_parts_tail);
+       ut_assertf(sizeof(mbr_parts_buf) >= mbr_parts_max, "Buffer avail: %ld; buffer req: %ld\n",
+               sizeof(mbr_parts_buf), mbr_parts_max);
+
+       mbr_wa = map_to_sysmem(mbr_wbuf);
+       ebr_wa = map_to_sysmem(ebr_wbuf);
Quoted 8 lines

test/cmd/mbr.c line 248

Quoted 6 lines
test/cmd/mbr.c new file +6 -0
+       ra = map_to_sysmem(rbuf);
+       ebr_blk = (ulong)0xB00000 / 0x200;
+
+       /* Make sure mmc1 exists */
+       ut_asserteq(1, blk_get_device_by_str("mmc", "1", &mmc_dev_desc));
+       ut_assertok(console_record_reset_enable());

You shouldn't need these

The code doesn't work without this line (changed 1 to 6 according to latest patch):

	ut_asserteq(1, blk_get_device_by_str("mmc", "6", &mmc_dev_desc));

I do not have enough knowledge of U-Boot to figure out why.

Quoted 213 lines
Quoted 209 lines
test/cmd/mbr.c new file +192 -0
+       ut_assertok(run_commandf("mmc dev 1"));
+       ut_assert_nextline("switch to partitions #0, OK");
+       ut_assert_nextline("mmc1 is current device");
+       ut_assertok(ut_check_console_end(uts));
+
+       /* Make sure mmc1 is 12+ MiB in size */
+       ut_assertok(run_commandf("mmc read 0x%lx 0x%lx 1", ra, (ulong)0xBFFE00 / 0x200));
+
+       /* Test one MBR partition */
+       init_buffers(rbuf, sizeof(rbuf), mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
+       ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 1));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0 1", mbr_wa));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       ut_assertok(memcmp(mbr_wbuf, rbuf, 512));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0x%lx 1", ebr_wa, ebr_blk));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(console_record_reset_enable());
+       ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
+       ut_assertok(run_commandf("mbr write mmc 1"));
+       ut_assert_nextline("MBR: write success!");
+       ut_assertok(run_commandf("mbr verify mmc 1"));
+       ut_assert_nextline("MBR: verify success!");
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(ut_check_console_end(uts));
+       /*
+       000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+       000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 00  |...%$..@........|
+       000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+       000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+       000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+       */
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       for (unsigned i = 0; i < mbr_cmp_size; i++) {
+               ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p1[i],
+                       "1P MBR+0x%04X: expected 0x%02X, actual: 0x%02X\n",
+                       mbr_cmp_start + i, mbr_parts_ref_p1[i], rbuf[mbr_cmp_start + i]);
+       }
+
+       /* Test two MBR partitions */
+       init_buffers(rbuf, sizeof(rbuf), mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
+       ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 2));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0 1", mbr_wa));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       ut_assertok(memcmp(mbr_wbuf, rbuf, 512));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0x%lx 1", ebr_wa, ebr_blk));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(console_record_reset_enable());
+       ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
+       ut_assertok(run_commandf("mbr write mmc 1"));
+       ut_assert_nextline("MBR: write success!");
+       ut_assertok(run_commandf("mbr verify mmc 1"));
+       ut_assert_nextline("MBR: verify success!");
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(ut_check_console_end(uts));
+       /*
+       000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+       000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+       000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 00  |%..F...H........|
+       000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+       000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+       */
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       for (unsigned i = 0; i < mbr_cmp_size; i++) {
+               ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p2[i],
+                       "2P MBR+0x%04X: expected 0x%02X, actual: 0x%02X\n",
+                       mbr_cmp_start + i, mbr_parts_ref_p2[i], rbuf[mbr_cmp_start + i]);
+       }
+
+       /* Test three MBR partitions */
+       init_buffers(rbuf, sizeof(rbuf), mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
+       ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 3));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0 1", mbr_wa));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       ut_assertok(memcmp(mbr_wbuf, rbuf, 512));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0x%lx 1", ebr_wa, ebr_blk));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(console_record_reset_enable());
+       ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
+       ut_assertok(run_commandf("mbr write mmc 1"));
+       ut_assert_nextline("MBR: write success!");
+       ut_assertok(run_commandf("mbr verify mmc 1"));
+       ut_assert_nextline("MBR: verify success!");
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(ut_check_console_end(uts));
+       /*
+       000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+       000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+       000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 46  |%..F...H.......F|
+       000001e0  06 01 0e 66 25 01 00 50  00 00 00 08 00 00 00 00  |...f%..P........|
+       000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+       */
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       for (unsigned i = 0; i < mbr_cmp_size; i++) {
+               ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p3[i],
+                       "3P MBR+0x%04X: expected 0x%02X, actual: 0x%02X\n",
+                       mbr_cmp_start + i, mbr_parts_ref_p3[i], rbuf[mbr_cmp_start + i]);
+       }
+
+       /* Test four MBR partitions */
+       init_buffers(rbuf, sizeof(rbuf), mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
+       ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 4));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0 1", mbr_wa));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       ut_assertok(memcmp(mbr_wbuf, rbuf, 512));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0x%lx 1", ebr_wa, ebr_blk));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(console_record_reset_enable());
+       ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
+       ut_assertok(run_commandf("mbr write mmc 1"));
+       ut_assert_nextline("MBR: write success!");
+       ut_assertok(run_commandf("mbr verify mmc 1"));
+       ut_assert_nextline("MBR: verify success!");
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(ut_check_console_end(uts));
+       /*
+       000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+       000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+       000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 46  |%..F...H.......F|
+       000001e0  06 01 0e 66 25 01 00 50  00 00 00 08 00 00 00 66  |...f%..P.......f|
+       000001f0  26 01 0e 87 06 01 00 58  00 00 00 08 00 00 55 aa  |&......X......U.|
+       */
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       for (unsigned i = 0; i < mbr_cmp_size; i++) {
+               ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p4[i],
+                       "4P MBR+0x%04X: expected 0x%02X, actual: 0x%02X\n",
+                       mbr_cmp_start + i, mbr_parts_ref_p4[i], rbuf[mbr_cmp_start + i]);
+       }
+
+       /* Test five MBR partitions */
+       init_buffers(rbuf, sizeof(rbuf), mbr_wbuf, sizeof(mbr_wbuf), ebr_wbuf, sizeof(ebr_wbuf), __LINE__);
+       ut_assertok(build_mbr_parts(mbr_parts_buf, sizeof(mbr_parts_buf), 5));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0 1", mbr_wa));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       ut_assertok(memcmp(mbr_wbuf, rbuf, 512));
+       ut_assertok(run_commandf("write mmc 1:0 0x%lx 0x%lx 1", ebr_wa, ebr_blk));
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       ut_assertok(memcmp(ebr_wbuf, rbuf, 512));
+       ut_assertok(console_record_reset_enable());
+       ut_assertf(0 == run_commandf(mbr_parts_buf), "Invalid partitions string: %s\n", mbr_parts_buf);
+       ut_assertf(0 == run_commandf("mbr write mmc 1"), "Invalid partitions string: %s\n", mbr_parts_buf);
+       ut_assert_nextline("MBR: write success!");
+       ut_assertok(run_commandf("mbr verify mmc 1"));
+       ut_assert_nextline("MBR: verify success!");
+       ut_assertok(ut_check_console_end(uts));
+       /*
+       000001b0  00 00 00 00 00 00 00 00  78 56 34 12 00 00 80 05  |........xV4.....|
+       000001c0  05 01 0e 25 24 01 00 40  00 00 00 08 00 00 00 25  |...%$..@.......%|
+       000001d0  25 01 0e 46 05 01 00 48  00 00 00 08 00 00 00 46  |%..F...H.......F|
+       000001e0  06 01 0e 66 25 01 00 50  00 00 00 08 00 00 00 66  |...f%..P.......f|
+       000001f0  26 01 05 a7 26 01 00 58  00 00 00 10 00 00 55 aa  |&...&..X......U.|
+       */
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0 1", ra));
+       for (unsigned i = 0; i < mbr_cmp_size; i++) {
+               ut_assertf(rbuf[mbr_cmp_start + i] == mbr_parts_ref_p5[i],
+                       "5P MBR+0x%04X: expected 0x%02X, actual: 0x%02X\n",
+                       mbr_cmp_start + i, mbr_parts_ref_p5[i], rbuf[mbr_cmp_start + i]);
+       }
+       /*
+       00b001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 87  |................|
+       00b001c0  07 01 0e a7 26 01 00 08  00 00 00 08 00 00 00 00  |....&...........|
+       00b001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+       00b001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+       00b001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
+       */
+       ut_assertok(run_commandf("read mmc 1:0 0x%lx 0x%lx 1", ra, ebr_blk));
+       for (unsigned i = 0; i < ebr_cmp_size; i++) {
+               ut_assertf(rbuf[ebr_cmp_start + i] == ebr_parts_ref_p5[i],
+                       "5P EBR+0x%04X: expected 0x%02X, actual: 0x%02X\n",
+                       ebr_cmp_start + i, ebr_parts_ref_p5[i], rbuf[ebr_cmp_start + i]);
+       }
+
+       return 0;
+}
+
+/* Declare mbr test */
+UNIT_TEST(mbr_test_run, UT_TESTF_CONSOLE_REC, mbr_test);
+
+int do_ut_mbr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+       struct unit_test *tests = UNIT_TEST_SUITE_START(mbr_test);
+       const int n_ents = UNIT_TEST_SUITE_COUNT(mbr_test);
+
+       return cmd_ut_category("mbr", "mbr_test_", tests, n_ents, argc, argv);
+}
test/cmd_ut.c +4 -1
index 0f56409e80..123db7cc7d 100644
@@ -86,6 +86,10 @@ static struct cmd_tbl cmd_ut_sub[] = {
 #endif
 #ifdef CONFIG_UT_LOG
        U_BOOT_CMD_MKENT(log, CONFIG_SYS_MAXARGS, 1, do_ut_log, "", ""),
+#endif
+#if defined(CONFIG_SANDBOX) && defined(CONFIG_CMD_MBR) && defined(CONFIG_CMD_MMC) \
+        && defined(CONFIG_MMC_SANDBOX) && defined(CONFIG_MMC_WRITE)
+       U_BOOT_CMD_MKENT(mbr, CONFIG_SYS_MAXARGS, 1, do_ut_mbr, "", ""),
 #endif
        U_BOOT_CMD_MKENT(mem, CONFIG_SYS_MAXARGS, 1, do_ut_mem, "", ""),
 #if defined(CONFIG_SANDBOX) && defined(CONFIG_CMD_SETEXPR)
--
Quoted 1 line

2.41.0

Regards,
Simon

#3 lore raw
Quoted 24 lines · Alexander Gendin

Current code allows up to 3 MBR partitions without extended one. If more than 3 partitions are required, then extended partition(s) must be used.
This commit allows up to 4 primary MBR partitions without the need for extended partition.

Add mbr test unit. In order to run the test manually, mmc6.img file of size 12 MiB or greater is required in the same directory as u-boot. Test also runs automatically via ./test/py/test.py tool. Running mbr test is only supported in sandbox mode.

---
Changes for v2:
- Cleanup coding style
- Adjust commit message to the changes in v2
- Add mmc6 device to sandbox device tree
- Adjust boot/bootdev.c and dm/blk.c tests to include mmc6 in tests
- Auto-create mmc6.img in test_ut_dm_init()
- Add mbr test to the list of tests run by test.py. Test can be run manually too via 'ut mbr'.
- Change mbr test to use mmc6.img dedicated to the test, instead of mmc1.img.
- Clear read buffer before reading data from test file.

Quoted 8 lines
 arch/sandbox/dts/test.dts |   7 +
 disk/part_dos.c           |   2 +-
 include/test/suites.h     |   1 +
 test/boot/bootdev.c       |   2 +-
 test/cmd/Makefile         |   1 +
 test/cmd/mbr.c            | 471 ++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c             |   4 +
 test/dm/blk.c             |  44 ++--

So, in addition to the changes to test/dm/blk.c I had gotten as far as:

test/boot/bootdev.c +10 -3
index 66c151131edb..33babdcf699c 100644
@@ -43,8 +43,10 @@ static int bootdev_test_cmd_list(struct unit_test_state *uts)
 				   "mmc", "mmc1.bootdev");
 		ut_assert_nextline("%3x   [ %c ]  %6s  %-8s  %s", 2, probe_ch, "OK",
 				   "mmc", "mmc0.bootdev");
+		ut_assert_nextline("%3x   [ %c ]  %6s  %-8s  %s", 3, probe_ch, "OK",
+				   "mmc", "mmc6.bootdev");
 		ut_assert_nextlinen("---");
-		ut_assert_nextline("(3 bootdevs)");
+		ut_assert_nextline("(4 bootdevs)");
 		ut_assert_console_end();
 	}
 
@@ -225,10 +227,11 @@ static int bootdev_test_order(struct unit_test_state *uts)
 	ut_assertok(env_set("boot_targets", "mmc spi"));
 	ut_asserteq(0, bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
 	ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
-	ut_asserteq(3, iter.num_devs);
+	ut_asserteq(4, iter.num_devs);
 	ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
 	ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
 	ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
+	ut_asserteq_str("mmc6.bootdev", iter.dev_used[3]->name);
 	bootflow_iter_uninit(&iter);
 
 	return 0;
@@ -254,7 +257,7 @@ static int bootdev_test_order_default(struct unit_test_state *uts)
 	ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
 
 	ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
-	ut_asserteq(3, iter.num_devs);
+	ut_asserteq(4, iter.num_devs);
 	ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
 	bootflow_iter_uninit(&iter);
 
@@ -693,6 +696,10 @@ static int bootdev_test_next_prio(struct unit_test_state *uts)
 	ut_asserteq_str("mmc0.bootdev", dev->name);
 	ut_assert_console_end();
 
+	ut_assertok(bootdev_next_prio(&iter, &dev));
+	ut_asserteq_str("mmc6.bootdev", dev->name);
+	ut_assert_console_end();
+
 	ut_assertok(bootdev_next_prio(&iter, &dev));
 	ut_asserteq_str("[email protected]", dev->name);
 	ut_assert_skip_to_line("Hunting with: spi_flash");

To make the bootdev test pass (and they still don't with the above) before deciding that it seems like there's a bigger problem here, and maybe Simon can help explain or figure out what to do? Is there no way to drop mmc6 once we've completed the new test here?

Signature · 1 line
Tom
#4 lore raw

Hi,

Quoted 87 lines · Tom Rini
Quoted 24 lines · Alexander Gendin

Current code allows up to 3 MBR partitions without extended one. If more than 3 partitions are required, then extended partition(s) must be used.
This commit allows up to 4 primary MBR partitions without the need for extended partition.

Add mbr test unit. In order to run the test manually, mmc6.img file of size 12 MiB or greater is required in the same directory as u-boot. Test also runs automatically via ./test/py/test.py tool. Running mbr test is only supported in sandbox mode.

---
Changes for v2:
- Cleanup coding style
- Adjust commit message to the changes in v2
- Add mmc6 device to sandbox device tree
- Adjust boot/bootdev.c and dm/blk.c tests to include mmc6 in tests
- Auto-create mmc6.img in test_ut_dm_init()
- Add mbr test to the list of tests run by test.py. Test can be run manually too via 'ut mbr'.
- Change mbr test to use mmc6.img dedicated to the test, instead of mmc1.img.
- Clear read buffer before reading data from test file.

Quoted 8 lines
 arch/sandbox/dts/test.dts |   7 +
 disk/part_dos.c           |   2 +-
 include/test/suites.h     |   1 +
 test/boot/bootdev.c       |   2 +-
 test/cmd/Makefile         |   1 +
 test/cmd/mbr.c            | 471 ++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c             |   4 +
 test/dm/blk.c             |  44 ++--

So, in addition to the changes to test/dm/blk.c I had gotten as far as:

test/boot/bootdev.c +10 -3
index 66c151131edb..33babdcf699c 100644
@@ -43,8 +43,10 @@ static int bootdev_test_cmd_list(struct unit_test_state *uts)
                                   "mmc", "mmc1.bootdev");
                ut_assert_nextline("%3x   [ %c ]  %6s  %-8s  %s", 2, probe_ch, "OK",
                                   "mmc", "mmc0.bootdev");
+               ut_assert_nextline("%3x   [ %c ]  %6s  %-8s  %s", 3, probe_ch, "OK",
+                                  "mmc", "mmc6.bootdev");
                ut_assert_nextlinen("---");
-               ut_assert_nextline("(3 bootdevs)");
+               ut_assert_nextline("(4 bootdevs)");
                ut_assert_console_end();
        }
 
@@ -225,10 +227,11 @@ static int bootdev_test_order(struct unit_test_state *uts)
        ut_assertok(env_set("boot_targets", "mmc spi"));
        ut_asserteq(0, bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
        ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
-       ut_asserteq(3, iter.num_devs);
+       ut_asserteq(4, iter.num_devs);
        ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
        ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
        ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
+       ut_asserteq_str("mmc6.bootdev", iter.dev_used[3]->name);
        bootflow_iter_uninit(&iter);
 
        return 0;
@@ -254,7 +257,7 @@ static int bootdev_test_order_default(struct unit_test_state *uts)
        ut_asserteq_str("mmc1.bootdev", iter.dev_used[1]->name);
 
        ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
-       ut_asserteq(3, iter.num_devs);
+       ut_asserteq(4, iter.num_devs);
        ut_asserteq_str("mmc0.bootdev", iter.dev_used[2]->name);
        bootflow_iter_uninit(&iter);
 
@@ -693,6 +696,10 @@ static int bootdev_test_next_prio(struct unit_test_state *uts)
        ut_asserteq_str("mmc0.bootdev", dev->name);
        ut_assert_console_end();
 
+       ut_assertok(bootdev_next_prio(&iter, &dev));
+       ut_asserteq_str("mmc6.bootdev", dev->name);
+       ut_assert_console_end();
+
        ut_assertok(bootdev_next_prio(&iter, &dev));
        ut_asserteq_str("[email protected]", dev->name);
        ut_assert_skip_to_line("Hunting with: spi_flash");
Quoted 4 lines

To make the bootdev test pass (and they still don't with the above) before deciding that it seems like there's a bigger problem here, and maybe Simon can help explain or figure out what to do? Is there no way to drop mmc6 once we've completed the new test here?

I did start looking at this. See u-boot-dm/mbr-working

In short you set status = "disabled" for mmc6 and then manually bind it in the test.

But it still has one failure which I was trying to get to.

Regards,
Simon

#5 lore raw
In reply to Alexander Gendin
Quoted 6 lines · Alexander Gendin

Current code allows up to 3 MBR partitions without extended one. If more than 3 partitions are required, then extended partition(s) must be used.
This commit allows up to 4 primary MBR partitions without the need for extended partition.

Quoted 6 lines

Add mbr test unit. In order to run the test manually, mmc6.img file of size 12 MiB or greater is required in the same directory as u-boot. Test also runs automatically via ./test/py/test.py tool. Running mbr test is only supported in sandbox mode.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom