| 9 | |
| 10 | namespace GPT{ |
| 11 | int Parse(DiskDevice* disk){ |
| 12 | gpt_header_t* header = (gpt_header_t*)kmalloc(disk->blocksize); |
| 13 | |
| 14 | if(disk->ReadDiskBlock(1,disk->blocksize,(uint8_t*)header)){ |
| 15 | return -1; // Disk Error |
| 16 | } |
| 17 | |
| 18 | if(header->signature != GPT_HEADER_SIGNATURE /*Check Signature*/ || header->partNum == 0 /*Make sure there is at least 1 partition*/){ |
| 19 | return 0; // GPT not present or corrupted |
| 20 | } |
| 21 | |
| 22 | if(debugLevelPartitions >= DebugLevelNormal){ |
| 23 | Log::Info("Found GPT Header Partitions: "); |
| 24 | Log::Write(header->partNum); |
| 25 | Log::Write(" Entry Size:"); |
| 26 | Log::Write(header->partEntrySize); |
| 27 | } |
| 28 | |
| 29 | uint64_t tableLBA = header->partitionTableLBA; |
| 30 | |
| 31 | int partNum = 4;//header->partNum; |
| 32 | |
| 33 | gpt_entry_t* partitionTable = (gpt_entry_t*)kmalloc(partNum * header->partEntrySize); |
| 34 | if(disk->ReadDiskBlock(tableLBA,partNum * header->partEntrySize,(uint8_t*)partitionTable)){ |
| 35 | return -1; // Disk Error |
| 36 | } |
| 37 | |
| 38 | for(int i = 0; i < partNum; i++){ |
| 39 | gpt_entry_t entry = partitionTable[i]; |
| 40 | |
| 41 | if(debugLevelPartitions >= DebugLevelNormal){ |
| 42 | Log::Info("Found GPT Partition of size %d MB", (entry.endLBA - entry.startLBA) * 512 / 1024 / 1024); |
| 43 | } |
| 44 | |
| 45 | if((entry.endLBA - entry.startLBA)){ |
| 46 | PartitionDevice* part = new PartitionDevice(entry.startLBA, entry.endLBA, disk); |
| 47 | disk->partitions.add_back(part); |
| 48 | DeviceManager::RegisterDevice(*part); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | kfree(partitionTable); |
| 53 | kfree(header); |
| 54 | return partNum; |
| 55 | } |
| 56 | } |
no test coverage detected