1#include "part_driver.h"
10#include "module_debug.h"
12void GetGPTPartitionNameAscii(gpt_partition_table_entry* pPartition,
char* pAsciiBuffer)
15 for (
int i = 0; i <
sizeof(pPartition->partition_name) /
sizeof(u16); i++)
16 pAsciiBuffer[i] = (
char)pPartition->partition_name[i];
23 gpt_partition_table_header* pGptHeader;
24 gpt_partition_table_entry* pGptPartitionEntry;
27 char partName[37] = { 0 };
31 M_DEBUG(
"%s\n", __func__);
34 buffer = AllocSysMemory(ALLOC_FIRST, 512 * 2, NULL);
37 M_DEBUG(
"Failed to allocate memory\n");
41 pGptHeader = (gpt_partition_table_header*)buffer;
42 pGptPartitionEntry = (gpt_partition_table_entry*)((u8*)buffer + 512);
45 ret = bd->read(bd, 1, pGptHeader, 1);
49 M_DEBUG(
"Failed to read GPT partition table header %d\n", ret);
50 FreeSysMemory(buffer);
55 if (memcmp(pGptHeader->signature, EFI_PARTITION_SIGNATURE,
sizeof(EFI_PARTITION_SIGNATURE)) != 0)
58 M_DEBUG(
"GPT partition table header signature is invalid: %s\n", pGptHeader->signature);
59 FreeSysMemory(buffer);
67 entriesPerSector = bd->sectorSize /
sizeof(gpt_partition_table_entry);
70 printf(
"Found GPT disk '%08x...'\n", *(u32*)&pGptHeader->disk_guid);
71 for (
int i = 0; i < pGptHeader->partition_count && endOfTable == 0; )
75 if (i % entriesPerSector == 0)
78 ret = bd->read(bd, pGptHeader->partition_table_lba + (i / entriesPerSector), pGptPartitionEntry, 1);
83 u64 lba = pGptHeader->partition_table_lba + (i / entriesPerSector);
85 M_DEBUG(
"Failed to read next partition table entry sector lba=0x%08x%08x\n", lba_u32[1], lba_u32[0]);
87 FreeSysMemory(buffer);
92 for (
int x = 0; x < entriesPerSector; x++, i++)
96 if (memcmp(pGptPartitionEntry[x].partition_type_guid, NULL_GUID,
sizeof(NULL_GUID)) == 0)
104 if (pGptPartitionEntry[x].first_lba < pGptHeader->first_lba || pGptPartitionEntry[x].last_lba > pGptHeader->last_lba)
107 M_DEBUG(
"Partition entry %d appears to be corrupt (lba bounds incorrect)\n", i);
112 GetGPTPartitionNameAscii(&pGptPartitionEntry[x], partName);
113 u64 first_lba = pGptPartitionEntry[x].first_lba;
114 u64 last_lba = pGptPartitionEntry[x].last_lba;
115 u64 attribute_flags = pGptPartitionEntry[x].attribute_flags;
116 U64_2XU32(first_lba);
118 U64_2XU32(attribute_flags);
119 M_PRINTF(
"Found partition '%s' type=%08x unique=%08x start=0x%08x%08x end=0x%08x%08x attr=0x%08x%08x\n", partName, *(u32*)&pGptPartitionEntry[x].partition_type_guid,
120 *(u32*)&pGptPartitionEntry[x].partition_unique_guid, first_lba_u32[1], first_lba_u32[0], last_lba_u32[1], last_lba_u32[0], attribute_flags_u32[1], attribute_flags_u32[0]);
123 if (memcmp(pGptPartitionEntry[x].partition_type_guid, MS_RESERVED_PARTITION_GUID,
sizeof(MS_RESERVED_PARTITION_GUID)) == 0 ||
124 memcmp(pGptPartitionEntry[x].partition_type_guid, EFI_SYSTEM_PARTITION,
sizeof(EFI_SYSTEM_PARTITION)) == 0)
128 if ((pGptPartitionEntry[x].attribute_flags & GPT_PART_ATTR_IGNORE) != 0)
133 if ((partIndex = GetNextFreePartitionIndex()) == -1)
136 printf(
"Can't mount partition, no more free partition slots!\n");
141 g_part[partIndex].bd = bd;
142 g_part_bd[partIndex].name = bd->name;
143 g_part_bd[partIndex].devNr = bd->devNr;
144 g_part_bd[partIndex].parNr = i + 1;
145 g_part_bd[partIndex].parId = 0;
146 g_part_bd[partIndex].sectorSize = bd->sectorSize;
147 g_part_bd[partIndex].sectorOffset = bd->sectorOffset + pGptPartitionEntry[x].first_lba;
148 g_part_bd[partIndex].sectorCount = pGptPartitionEntry[x].last_lba - pGptPartitionEntry[x].first_lba;
149 bdm_connect_bd(&g_part_bd[partIndex]);
156 FreeSysMemory(buffer);
157 return mountCount > 0 ? 0 : -1;