diff --git a/iop/fs/bdm/src/part_driver_mbr.c b/iop/fs/bdm/src/part_driver_mbr.c index 69e8472cf6f..a559047cc82 100644 --- a/iop/fs/bdm/src/part_driver_mbr.c +++ b/iop/fs/bdm/src/part_driver_mbr.c @@ -9,6 +9,29 @@ #include "module_debug.h" +int partitions_sanity_check_mbr(struct block_device *bd, master_boot_record* pMbrBlock) { + //All 4 MBR partitions should be considered valid ones to be mounted, even if some are unused. + //At least one of them must be active. + int valid = 0; + int active = 0; + + for (int i = 0; i < 4; i++) + { + + if (pMbrBlock->primary_partitions[i].partition_type != 0) { + + if((pMbrBlock->primary_partitions[i].first_lba == 0) || (pMbrBlock->primary_partitions[i].first_lba >= bd->sectorCount)) + return 0; //invalid + + active++; + } + + valid++; //Considered at least a valid partition. + } + + return (valid == 4) && (active > 0); +} + int part_connect_mbr(struct block_device *bd) { master_boot_record* pMbrBlock = NULL; @@ -16,6 +39,7 @@ int part_connect_mbr(struct block_device *bd) int ret; int mountCount = 0; int partIndex; + int valid_partitions; M_DEBUG("%s\n", __func__); @@ -59,14 +83,29 @@ int part_connect_mbr(struct block_device *bd) return rval; } - // Loop and parse the primary partition entries in the MBR block. + + valid_partitions = partitions_sanity_check_mbr(bd, pMbrBlock); + + //Most likely a VBR + if(valid_partitions == 0) { + printf("MBR disk valid_partitions=%d \n", valid_partitions); + FreeSysMemory(pMbrBlock); + return -1; + } + printf("Found MBR disk\n"); + + // Loop and parse the primary partition entries in the MBR block. for (int i = 0; i < 4; i++) { // Check if the partition is active, checking the status bit is not reliable so check if the sector_count is greater than zero instead. if (pMbrBlock->primary_partitions[i].sector_count == 0) continue; + //Ignore partitions that are not active. 0 is empty partition_type. + if (pMbrBlock->primary_partitions[i].partition_type == 0) + continue; + printf("Found partition type 0x%02x\n", pMbrBlock->primary_partitions[i].partition_type); // TODO: Filter out unsupported partition types. @@ -96,4 +135,4 @@ int part_connect_mbr(struct block_device *bd) FreeSysMemory(pMbrBlock); return rval; -} \ No newline at end of file +}