--- filters/partition/nbdkit-partition-filter.pod | 5 - filters/partition/partition-mbr.c | 105 +++++++++++++++++- tests/test-partitioning1.sh | 22 +++- 3 files changed, 121 insertions(+), 11 deletions(-) diff --git a/filters/partition/nbdkit-partition-filter.pod b/filters/partition/nbdkit-partition-filter.pod index 4a615b6..ccd1b52 100644 --- a/filters/partition/nbdkit-partition-filter.pod +++ b/filters/partition/nbdkit-partition-filter.pod @@ -19,11 +19,6 @@ This works like the C<qemu-nbd -P> option. The opposite of this filter is L<nbdkit-partitioning-plugin(1)> which adds a virtual partition table to a file or files. -=head1 NOTE - -Only MBR primary partitions and GPT partition tables are supported. -MBR logical partitions are B<not> supported. - =head1 PARAMETERS =over 4 diff --git a/filters/partition/partition-mbr.c b/filters/partition/partition-mbr.c index 41ee350..704410c 100644 --- a/filters/partition/partition-mbr.c +++ b/filters/partition/partition-mbr.c @@ -36,14 +36,20 @@ #include <stdio.h> #include <stdlib.h> #include <stdint.h> +#include <inttypes.h> #include <string.h> +#include <errno.h> #include <nbdkit-filter.h> #include "byte-swapping.h" +#include "isaligned.h" #include "partition.h" +/* See also linux.git/block/partitions/msdos.c:is_extended_partition */ +#define is_extended(byte) ((byte) == 0x5 || (byte) == 0xf || (byte) == 0x85) + struct mbr_partition { uint8_t part_type_byte; uint32_t start_sector; @@ -69,16 +75,16 @@ find_mbr_partition (struct nbdkit_next_ops *next_ops, void *nxdata, { int i; struct mbr_partition partition; + uint32_t ep_start_sector, ep_nr_sectors; + uint64_t ebr, next_ebr; + uint8_t sector[SECTOR_SIZE]; - if (partnum > 4) { - nbdkit_error ("MBR logical partitions are not supported"); - return -1; - } - + /* Primary partition. */ for (i = 0; i < 4; ++i) { get_mbr_partition (mbr, i, &partition); if (partition.nr_sectors > 0 && partition.part_type_byte != 0 && + !is_extended (partition.part_type_byte) && partnum == i+1) { *offset_r = partition.start_sector * SECTOR_SIZE; *range_r = partition.nr_sectors * SECTOR_SIZE; @@ -86,6 +92,95 @@ find_mbr_partition (struct nbdkit_next_ops *next_ops, void *nxdata, } } + /* Logical partition. */ + + /* Find the extended partition in the primary partition table. */ + for (i = 0; i < 4; ++i) { + get_mbr_partition (mbr, i, &partition); + if (partition.nr_sectors > 0 && + is_extended (partition.part_type_byte)) { + goto found_extended; + } + } + nbdkit_error ("MBR logical partition selected, " + "but there is no extended partition in the partition table"); + return -1; + + found_extended: + ep_start_sector = partition.start_sector; + ep_nr_sectors = partition.nr_sectors; + ebr = ep_start_sector * (uint64_t)SECTOR_SIZE; + + /* This loop will terminate eventually because we only accept + * links which strictly increase the EBR pointer. + */ + for (i = 5; ; ++i) { + /* Check that the ebr is aligned and pointing inside the disk + * and doesn't point to the MBR. + */ + if (!IS_ALIGNED (ebr, SECTOR_SIZE) || + ebr < SECTOR_SIZE || ebr >= size-SECTOR_SIZE) { + nbdkit_error ("invalid EBR chain: " + "next EBR boot sector is located outside disk boundary"); + return -1; + } + + /* Read the EBR sector. */ + if (next_ops->pread (nxdata, sector, sizeof sector, ebr, 0, + &errno) == -1) + return -1; + + if (i == partnum) { + uint64_t offset, range; + + /* First entry in EBR points to the logical partition. */ + get_mbr_partition (sector, 0, &partition); + + /* The first entry start sector is relative to the EBR. */ + offset = ebr + partition.start_sector * (uint64_t)SECTOR_SIZE; + range = partition.nr_sectors * (uint64_t)SECTOR_SIZE; + + /* Logical partition cannot be before the corresponding EBR, + * and it cannot extend beyond the enclosing extended + * partition. + */ + if (offset <= ebr || + offset + range > + ((uint64_t)ep_start_sector + ep_nr_sectors) * SECTOR_SIZE) { + nbdkit_error ("logical partition start or size out of range " + "(offset=%" PRIu64 ", range=%" PRIu64 ", " + "ep:startsect=%" PRIu32 ", ep:nrsects=%" PRIu32 ")", + offset, range, ep_start_sector, ep_nr_sectors); + return -1; + } + *offset_r = offset; + *range_r = range; + return 0; + } + + /* Second entry in EBR links to the next EBR. */ + get_mbr_partition (sector, 1, &partition); + + /* All zeroes means the end of the chain. */ + if (partition.start_sector == 0 && partition.nr_sectors == 0) + break; + + /* The second entry start sector is relative to the start to the + * extended partition. + */ + next_ebr = + ((uint64_t)ep_start_sector + partition.start_sector) * SECTOR_SIZE; + + /* Make sure the next EBR > current EBR. */ + if (next_ebr <= ebr) { + nbdkit_error ("invalid EBR chain: " + "next EBR %" PRIu64 " <= current EBR %" PRIu64, + next_ebr, ebr); + return -1; + } + ebr = next_ebr; + } + nbdkit_error ("MBR partition %d not found", partnum); return -1; } diff --git a/tests/test-partitioning1.sh b/tests/test-partitioning1.sh index 76ab43b..8aa45b9 100755 --- a/tests/test-partitioning1.sh +++ b/tests/test-partitioning1.sh @@ -77,7 +77,27 @@ nbdkit -f -v -D partitioning.regions=1 -U - \ # Contents of partitioning1.out should be identical to file-data. cmp file-data partitioning1.out -# Same test with GPT and more partitions. +# Same test with > 4 MBR partitions. +# Note we select partition 6 because partition 4 is the extended partition. +nbdkit -f -v -D partitioning.regions=1 -U - \ + --filter=partition \ + partitioning \ + partitioning1-p1 \ + partitioning1-p2 \ + partitioning1-p3 \ + partitioning1-p4 \ + type-guid=A2A0D0EB-E5B9-3344-87C0-68B6B72699C7 \ + file-data \ + type-guid=AF3DC60F-8384-7247-8E79-3D69D8477DE4 \ + partitioning1-p5 \ + partitioning1-p6 \ + partition-type=mbr \ + partition=6 \ + --run 'qemu-img convert $nbd partitioning1.out' + +cmp file-data partitioning1.out + +# Same test with GPT. nbdkit -f -v -D partitioning.regions=1 -U - \ --filter=partition \ partitioning \ -- 2.20.1