1#include "part_driver.h"
10#include "module_debug.h"
12int partitions_sanity_check_mbr(
struct block_device *bd, master_boot_record* pMbrBlock) {
18 for (
int i = 0; i < 4; i++)
21 if (pMbrBlock->primary_partitions[i].partition_type != 0) {
23 if((pMbrBlock->primary_partitions[i].first_lba == 0) || (pMbrBlock->primary_partitions[i].first_lba >= bd->sectorCount))
32 return (valid == 4) && (active > 0);
37 master_boot_record* pMbrBlock = NULL;
44 M_DEBUG(
"%s\n", __func__);
48 if (bd->sectorOffset != 0)
52 pMbrBlock = AllocSysMemory(ALLOC_FIRST,
sizeof(master_boot_record), NULL);
53 if (pMbrBlock == NULL)
56 M_DEBUG(
"Failed to allocate memory for MBR block\n");
61 ret = bd->read(bd, 0, pMbrBlock, 1);
65 M_DEBUG(
"Failed to read MBR sector from block device %d\n", ret);
70 if (pMbrBlock->boot_signature != MBR_BOOT_SIGNATURE)
73 M_DEBUG(
"MBR boot signature is invalid, device is not MBR\n");
74 FreeSysMemory(pMbrBlock);
79 if (pMbrBlock->primary_partitions[0].partition_type == MBR_PART_TYPE_GPT_PROTECTIVE_MBR)
82 FreeSysMemory(pMbrBlock);
87 valid_partitions = partitions_sanity_check_mbr(bd, pMbrBlock);
90 if(valid_partitions == 0) {
91 printf(
"MBR disk valid_partitions=%d \n", valid_partitions);
92 FreeSysMemory(pMbrBlock);
96 printf(
"Found MBR disk\n");
99 for (
int i = 0; i < 4; i++)
102 if (pMbrBlock->primary_partitions[i].sector_count == 0)
106 if (pMbrBlock->primary_partitions[i].partition_type == 0)
109 printf(
"Found partition type 0x%02x\n", pMbrBlock->primary_partitions[i].partition_type);
113 if ((partIndex = GetNextFreePartitionIndex()) == -1)
116 printf(
"Can't mount partition, no more free partition slots!\n");
121 g_part[partIndex].bd = bd;
122 g_part_bd[partIndex].name = bd->name;
123 g_part_bd[partIndex].devNr = bd->devNr;
124 g_part_bd[partIndex].parNr = i + 1;
125 g_part_bd[partIndex].parId = pMbrBlock->primary_partitions[i].partition_type;
126 g_part_bd[partIndex].sectorSize = bd->sectorSize;
127 g_part_bd[partIndex].sectorOffset = bd->sectorOffset + (u64)pMbrBlock->primary_partitions[i].first_lba;
128 g_part_bd[partIndex].sectorCount = pMbrBlock->primary_partitions[i].sector_count;
129 bdm_connect_bd(&g_part_bd[partIndex]);
134 rval = mountCount > 0 ? 0 : -1;
136 FreeSysMemory(pMbrBlock);