public static byte[] GetBytes(string devicePath) { MasterBootRecord mbr = MasterBootRecord.Get(devicePath); if (mbr.PartitionTable[0].SystemID == "EFI_GPT_DISK") { IntPtr hDevice = Win32.NativeMethods.getHandle(devicePath); using (FileStream streamToRead = NativeMethods.getFileStream(hDevice)) { return(Win32.NativeMethods.readDrive(streamToRead, GPT_OFFSET, SECTOR_SIZE)); } } else { throw new Exception("No GPT found. Please use Get-MBR cmdlet"); } }
public GuidPartitionTable(string devicePath) { MasterBootRecord mbr = MasterBootRecord.Get(devicePath); if (mbr.PartitionTable[0].SystemID == "EFI_GPT_DISK") { IntPtr hDevice = NativeMethods.getHandle(devicePath); using (FileStream streamToRead = NativeMethods.getFileStream(hDevice)) { // Get Header GuidPartitionTableHeader GPTHeader = new GuidPartitionTableHeader(NativeMethods.readDrive(streamToRead, GPT_OFFSET, SECTOR_SIZE)); Revision = GPTHeader.Revision; HeaderSize = GPTHeader.HeaderSize; MyLBA = GPTHeader.MyLBA; AlternateLBA = GPTHeader.AlternateLBA; FirstUsableLBA = GPTHeader.FirstUsableLBA; LastUsableLBA = GPTHeader.LastUsableLBA; DiskGUID = GPTHeader.DiskGUID; PartitionEntryLBA = GPTHeader.PartitionEntryLBA; NumberOfPartitionEntries = GPTHeader.NumberOfPartitionEntries; SizeOfPartitionEntry = GPTHeader.SizeOfPartitionEntry; // Get PartitionTable List <GuidPartitionTableEntry> partitionList = new List <GuidPartitionTableEntry>(); bool Continue = true; // Iterate through sectors that contain the GPT Entry Array for (ulong j = GPTHeader.PartitionEntryLBA; (j < GPTHeader.PartitionEntryLBA + (GPTHeader.NumberOfPartitionEntries / (SECTOR_SIZE / GPTHeader.SizeOfPartitionEntry))); j++) { // Read one sector byte[] partitionSectorBytes = NativeMethods.readDrive(streamToRead, (SECTOR_SIZE * j), SECTOR_SIZE); // Iterate through Partition Entries in Sector // Sectors (512 bytes) / Partitions (128 bytes) = 4 partitions per sector for (uint i = 0; i < 512; i += GPTHeader.SizeOfPartitionEntry) { // Instantiate a GuidPartitionTableEntry object GuidPartitionTableEntry entry = new GuidPartitionTableEntry(NativeMethods.GetSubArray(partitionSectorBytes, i, GPTHeader.SizeOfPartitionEntry)); // If entry's PartitionTypeGUID is 00000000-0000-0000-0000-000000000000 then it is not a partition if (entry.PartitionTypeGUID == new Guid("00000000-0000-0000-0000-000000000000")) { Continue = false; break; } partitionList.Add(entry); } if (!Continue) { break; } } PartitionTable = partitionList.ToArray(); } } else { throw new Exception("No GPT found. Please use Get-MBR cmdlet"); } }
public static MasterBootRecord Get(string drivePath) { // Read Master Boot Record (first 512 bytes) from disk return(new MasterBootRecord(MasterBootRecord.GetBytes(drivePath))); }