public static DiskPartitionInfo GetDiskPartitionInfo(char driveLetter) { int deviceID = NativeDiskWrapper.CheckDriveType(string.Format(@"\\.\{0}:\", driveLetter)); IntPtr deviceHandle = NativeDiskWrapper.GetHandleOnDevice(deviceID, NativeDisk.GENERIC_READ); uint partitionTableSize = 0; DiskPartitionInfo partitionsInfo = new DiskPartitionInfo(); var partitionInfo = NativeDiskWrapper.GetDiskPartitionInfo(deviceHandle); partitionsInfo.DiskTotalSize = GetDeviceLength(deviceID); if (partitionInfo.PartitionStyle == PARTITION_STYLE.MasterBootRecord) { partitionsInfo.PartitionType = PartitionType.MBR; partitionTableSize = 512; unsafe { byte *data = NativeDiskWrapper.ReadSectorDataPointerFromHandle(deviceHandle, 0, 1, 512); for (int i = 0; i < 4; i++) { ulong partitionStartSector = (uint)Marshal.ReadInt32(new IntPtr(data), 0x1BE + 8 + 16 * i); ulong partitionNumSectors = (uint)Marshal.ReadInt32(new IntPtr(data), 0x1BE + 12 + 16 * i); if (partitionStartSector + partitionNumSectors > 0) { partitionsInfo.PartitionSizes.Add(partitionNumSectors * 512); } } } } else if (partitionInfo.PartitionStyle == PARTITION_STYLE.GuidPartitionTable) { partitionsInfo.PartitionType = PartitionType.GPT; partitionTableSize = 17408; unsafe { byte *data = NativeDiskWrapper.ReadSectorDataPointerFromHandle(deviceHandle, 0, 1, 512); uint gptHeaderOffset = (uint)Marshal.ReadInt32(new IntPtr(data), 0x1C6); data = NativeDiskWrapper.ReadSectorDataPointerFromHandle(deviceHandle, gptHeaderOffset, 1, 512); ulong partitionTableSector = (ulong)Marshal.ReadInt64(new IntPtr(data), 0x48); uint noOfPartitionEntries = (uint)Marshal.ReadInt32(new IntPtr(data), 0x50); uint sizeOfPartitionEntry = (uint)Marshal.ReadInt32(new IntPtr(data), 0x54); data = NativeDiskWrapper.ReadSectorDataPointerFromHandle(deviceHandle, partitionTableSector, (512 / sizeOfPartitionEntry) * noOfPartitionEntries, 512); for (int i = 0; i < noOfPartitionEntries; i++) { ulong partitionStartSector = (ulong)Marshal.ReadInt64(new IntPtr(data), (int)(0x20 + sizeOfPartitionEntry * i)); ulong partitionNumSectors = (ulong)Marshal.ReadInt64(new IntPtr(data), (int)(0x28 + sizeOfPartitionEntry * i)); if (partitionStartSector + partitionNumSectors > 0) { partitionsInfo.PartitionSizes.Add(partitionNumSectors * 512); } } } } else { partitionsInfo.PartitionType = PartitionType.RAW; } NativeDisk.CloseHandle(deviceHandle); partitionsInfo.UnallocatedSize = partitionsInfo.DiskTotalSize - (partitionsInfo.PartitionSizes.Sum() + partitionTableSize); return(partitionsInfo); }
public ulong GetLastUsedPartition() { ulong[] numSectorsArr = new ulong[deviceHandles.Length]; for (int x = 0; x < deviceHandles.Length; x++) { var partitionInfo = NativeDiskWrapper.GetDiskPartitionInfo(deviceHandles[x]); if (partitionInfo.PartitionStyle == PARTITION_STYLE.MasterBootRecord) { numSectorsArr[x] = 1; unsafe { byte *data = NativeDiskWrapper.ReadSectorDataPointerFromHandle(deviceHandles[x], 0, 1, sectorSize); for (int i = 0; i < 4; i++) { ulong partitionStartSector = (uint)Marshal.ReadInt32(new IntPtr(data), 0x1BE + 8 + 16 * i); ulong partitionNumSectors = (uint)Marshal.ReadInt32(new IntPtr(data), 0x1BE + 12 + 16 * i); if (partitionStartSector + partitionNumSectors > numSectorsArr[x]) { numSectorsArr[x] = partitionStartSector + partitionNumSectors; } } } } else if (partitionInfo.PartitionStyle == PARTITION_STYLE.GuidPartitionTable) { numSectorsArr[x] = 1; unsafe { byte *data = NativeDiskWrapper.ReadSectorDataPointerFromHandle(deviceHandles[x], 0, 1, sectorSize); uint gptHeaderOffset = (uint)Marshal.ReadInt32(new IntPtr(data), 0x1C6); data = NativeDiskWrapper.ReadSectorDataPointerFromHandle(deviceHandles[x], gptHeaderOffset, 1, sectorSize); ulong partitionTableSector = (ulong)Marshal.ReadInt64(new IntPtr(data), 0x48); uint noOfPartitionEntries = (uint)Marshal.ReadInt32(new IntPtr(data), 0x50); uint sizeOfPartitionEntry = (uint)Marshal.ReadInt32(new IntPtr(data), 0x54); data = NativeDiskWrapper.ReadSectorDataPointerFromHandle(deviceHandles[x], partitionTableSector, (sectorSize / sizeOfPartitionEntry) * noOfPartitionEntries, sectorSize); for (int i = 0; i < noOfPartitionEntries; i++) { ulong partitionStartSector = (ulong)Marshal.ReadInt64(new IntPtr(data), (int)(0x20 + sizeOfPartitionEntry * i)); ulong partitionNumSectors = (ulong)Marshal.ReadInt64(new IntPtr(data), (int)(0x28 + sizeOfPartitionEntry * i)); if (partitionStartSector + partitionNumSectors > numSectorsArr[x]) { numSectorsArr[x] = partitionStartSector + partitionNumSectors; } } } } } for (int i = 1; i < deviceHandles.Length; i++) { if (numSectorsArr[0] != numSectorsArr[i]) { throw new Exception("Devices have different partitions size.\nVerifying will not be started."); } } return(numSectorsArr[0]); }