Example #1
0
        public static byte[] GetBytes(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))
                {
                    return(NativeMethods.readDrive(streamToRead, GPT_OFFSET, SECTOR_SIZE));
                }
            }
            else
            {
                throw new Exception("No GPT found. Please use Get-MBR cmdlet");
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="devicePath"></param>
        /// <returns></returns>
        public static byte[] GetBytes(string devicePath)
        {
            MasterBootRecord mbr = MasterBootRecord.Get(devicePath);

            List <byte> byteList = new List <byte>();

            if (mbr.PartitionTable[0].SystemId == "EFI_GPT_DISK")
            {
                using (FileStream streamToRead = Helper.getFileStream(devicePath))
                {
                    byte[] headerBytes = Helper.readDrive(streamToRead, GPT_OFFSET, SECTOR_SIZE);

                    long partitionTableOffset = BitConverter.ToInt64(headerBytes, 0x48);
                    uint partitionCount       = BitConverter.ToUInt32(headerBytes, 0x50);
                    uint partitionSize        = BitConverter.ToUInt32(headerBytes, 0x54);

                    long partitionBufferSize = partitionCount * partitionSize;

                    if (!((partitionBufferSize % 512) == 0))
                    {
                        partitionBufferSize = partitionBufferSize + (SECTOR_SIZE - (partitionBufferSize % SECTOR_SIZE));
                    }

                    byte[] partitionBytes = Helper.readDrive(streamToRead, partitionTableOffset * SECTOR_SIZE, partitionBufferSize);

                    byteList.AddRange(headerBytes);
                    byteList.AddRange(partitionBytes);
                }

                return(byteList.ToArray());
            }
            else
            {
                throw new Exception("No GPT found. Please use Get-MBR cmdlet");
            }
        }
Example #3
0
 public static MasterBootRecord Get(string drivePath)
 {
     // Read Master Boot Record (first 512 bytes) from disk
     return(new MasterBootRecord(MasterBootRecord.GetBytes(drivePath)));
 }
Example #4
0
        internal GuidPartitionTable(string devicePath)
        {
            MasterBootRecord mbr = MasterBootRecord.Get(devicePath);

            if (mbr.PartitionTable[0].SystemId == "EFI_GPT_DISK")
            {
                using (FileStream streamToRead = Helper.getFileStream(devicePath))
                {
                    // Get Header
                    GuidPartitionTableHeader GPTHeader = new GuidPartitionTableHeader(Helper.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 = Helper.readDrive(streamToRead, (SECTOR_SIZE * (long)j), SECTOR_SIZE);

                        // Iterate through Partition Entries in Sector
                        // Sectors (512 bytes) / Partitions (128 bytes) = 4 partitions per sector
                        for (int i = 0; i < 512; i += (int)GPTHeader.SizeOfPartitionEntry)
                        {
                            // Instantiate a GuidPartitionTableEntry object
                            GuidPartitionTableEntry entry = new GuidPartitionTableEntry(Helper.GetSubArray(partitionSectorBytes, i, (int)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");
            }
        }