99
1010#include "module_debug.h"
1111
12+ int partitions_sanity_check_mbr (struct block_device * bd , master_boot_record * pMbrBlock ) {
13+ //All 4 MBR partitions should be considered valid ones to be mounted, even if some are unused.
14+ //At least one of them must be active.
15+ int valid = 0 ;
16+ int active = 0 ;
17+
18+ for (int i = 0 ; i < 4 ; i ++ )
19+ {
20+
21+ if (pMbrBlock -> primary_partitions [i ].partition_type != 0 ) {
22+
23+ if ((pMbrBlock -> primary_partitions [i ].first_lba == 0 ) || (pMbrBlock -> primary_partitions [i ].first_lba >= bd -> sectorCount ))
24+ return 0 ; //invalid
25+
26+ active ++ ;
27+ }
28+
29+ valid ++ ; //Considered at least a valid partition.
30+ }
31+
32+ return (valid == 4 ) && (active > 0 );
33+ }
34+
1235int part_connect_mbr (struct block_device * bd )
1336{
1437 master_boot_record * pMbrBlock = NULL ;
1538 int rval = -1 ;
1639 int ret ;
1740 int mountCount = 0 ;
1841 int partIndex ;
42+ int valid_partitions ;
1943
2044 M_DEBUG ("%s\n" , __func__ );
2145
@@ -59,14 +83,29 @@ int part_connect_mbr(struct block_device *bd)
5983 return rval ;
6084 }
6185
62- // Loop and parse the primary partition entries in the MBR block.
86+
87+ valid_partitions = partitions_sanity_check_mbr (bd , pMbrBlock );
88+
89+ //Most likely a VBR
90+ if (valid_partitions == 0 ) {
91+ printf ("MBR disk valid_partitions=%d \n" , valid_partitions );
92+ FreeSysMemory (pMbrBlock );
93+ return -1 ;
94+ }
95+
6396 printf ("Found MBR disk\n" );
97+
98+ // Loop and parse the primary partition entries in the MBR block.
6499 for (int i = 0 ; i < 4 ; i ++ )
65100 {
66101 // Check if the partition is active, checking the status bit is not reliable so check if the sector_count is greater than zero instead.
67102 if (pMbrBlock -> primary_partitions [i ].sector_count == 0 )
68103 continue ;
69104
105+ //Ignore partitions that are not active. 0 is empty partition_type.
106+ if (pMbrBlock -> primary_partitions [i ].partition_type == 0 )
107+ continue ;
108+
70109 printf ("Found partition type 0x%02x\n" , pMbrBlock -> primary_partitions [i ].partition_type );
71110
72111 // TODO: Filter out unsupported partition types.
@@ -96,4 +135,4 @@ int part_connect_mbr(struct block_device *bd)
96135
97136 FreeSysMemory (pMbrBlock );
98137 return rval ;
99- }
138+ }
0 commit comments