@@ -24,9 +24,12 @@
#define DOS_PART_DEFAULT_SECTOR 512
-/* should this be configurable? It looks like it's not very common at all
- * to use large numbers of partitions */
-#define MAX_EXT_PARTS 256
+/*
+ * Number at most 255 partitions, as Linux does (DISK_MAX_PARTS - 1), so
+ * that the UUID made up for each from the disk signature and the partition
+ * number, "%08x-%02x", takes 11 characters
+ */
+#define MAX_EXT_PARTS 255
/*
* A link to the next extended partition table adds no partition, so tables
@@ -214,7 +217,7 @@ static void print_partition_extended(struct blk_desc *desc,
/* Print all primary/logical partitions */
pt = (dos_partition_t *)(buffer + DOS_PART_TBL_OFFSET);
- for (i = 0; i < 4; i++, pt++) {
+ for (i = 0; i < 4 && part_num <= MAX_EXT_PARTS; i++, pt++) {
/*
* fdisk does not show the extended partitions that
* are not in the MBR
@@ -302,7 +305,7 @@ static int part_get_info_extended(struct blk_desc *desc,
/* Print all primary/logical partitions */
pt = (dos_partition_t *)(buffer + DOS_PART_TBL_OFFSET);
- for (i = 0; i < 4; i++, pt++) {
+ for (i = 0; i < 4 && part_num <= MAX_EXT_PARTS; i++, pt++) {
/*
* fdisk does not show the extended partitions that
* are not in the MBR
@@ -423,3 +423,62 @@ static int dm_test_part_dos_ext_empty(struct unit_test_state *uts)
return 0;
}
DM_TEST(dm_test_part_dos_ext_empty, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
+
+/*
+ * As Linux, number at most 255 DOS partitions, so that the UUID made up for
+ * each from the disk signature and the partition number takes 11 characters
+ */
+static int dm_test_part_dos_uuid(struct unit_test_state *uts)
+{
+ struct disk_partition info;
+ dos_partition_t pt[4];
+ struct blk_desc *desc;
+ int part;
+
+ if (!CONFIG_IS_ENABLED(PARTITION_UUIDS))
+ return -EAGAIN;
+
+ ut_asserteq(2, blk_get_device_by_str("mmc", "2", &desc));
+ ut_asserteq(512, desc->blksz);
+
+ /* an extended partition from sector 2 */
+ memset(pt, '\0', sizeof(pt));
+ dos_set_entry(&pt[0], DOS_PART_TYPE_EXTENDED, 2, 512);
+ ut_assertok(dos_write_table(desc, 0, pt));
+
+ /*
+ * holding logical partitions 5 to 256, each of one sector after its
+ * table, with the tables two sectors apart from sector 2 on
+ */
+ for (part = 5; part <= 256; part++) {
+ memset(pt, '\0', sizeof(pt));
+ dos_set_entry(&pt[0], 0x83, 1, 1);
+ if (part < 256)
+ dos_set_entry(&pt[1], DOS_PART_TYPE_EXTENDED,
+ (part - 4) * 2, 2);
+ ut_assertok(dos_write_table(desc, 2 + (part - 5) * 2, pt));
+ }
+
+ ut_assertok(part_get_info_by_type(desc, 255, PART_TYPE_DOS, &info));
+ ut_asserteq(2 + 250 * 2 + 1, info.start);
+ ut_asserteq_str("00000000-ff", disk_partition_uuid(&info));
+ ut_assert_console_end();
+
+ /* there is no partition 256 */
+ ut_asserteq(-ENOENT,
+ part_get_info_by_type(desc, 256, PART_TYPE_DOS, &info));
+ ut_assert_nextline("** Nested DOS partitions detected, stopping **");
+ ut_assert_console_end();
+
+ /* and listing them stops at partition 255 too */
+ part_init(desc);
+ ut_asserteq(PART_TYPE_DOS, desc->part_type);
+ part_print(desc);
+ ut_assert_skip_to_line("%3d\t%-10d\t%-10d\t%08x-%02x\t%02x", 255,
+ 2 + 250 * 2 + 1, 1, 0, 255, 0x83);
+ ut_assert_nextline("** Nested DOS partitions detected, stopping **");
+ ut_assert_console_end();
+
+ return 0;
+}
+DM_TEST(dm_test_part_dos_uuid, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);