Example #1
0
        internal GuidPartitionTable(byte[] bytes)
        {
            #region Header

            GuidPartitionTableHeader GPTHeader = new GuidPartitionTableHeader(NativeMethods.GetSubArray(bytes, 0x00, (uint)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;

            #endregion Header

            // Get PartitionTable
            List <GuidPartitionTableEntry> partitionList = new List <GuidPartitionTableEntry>();

            for (uint i = 0; i < (bytes.Length - (int)SECTOR_SIZE); i += GPTHeader.SizeOfPartitionEntry)
            {
                // Instantiate a GuidPartitionTableEntry object
                GuidPartitionTableEntry entry = new GuidPartitionTableEntry(NativeMethods.GetSubArray(bytes, (i + (uint)SECTOR_SIZE), 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"))
                {
                    break;
                }
                partitionList.Add(entry);
            }

            PartitionTable = partitionList.ToArray();
        }
Example #2
0
        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");
            }
        }
        internal GuidPartitionTable(byte[] bytes)
        {
            #region Header

            GuidPartitionTableHeader GPTHeader = new GuidPartitionTableHeader(NativeMethods.GetSubArray(bytes, 0x00, (uint)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;

            #endregion Header

            // Get PartitionTable
            List<GuidPartitionTableEntry> partitionList = new List<GuidPartitionTableEntry>();

            for (uint i = 0; i < (bytes.Length - (int)SECTOR_SIZE); i += GPTHeader.SizeOfPartitionEntry)
            {
                // Instantiate a GuidPartitionTableEntry object
                GuidPartitionTableEntry entry = new GuidPartitionTableEntry(NativeMethods.GetSubArray(bytes, (i + (uint)SECTOR_SIZE), 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"))
                {
                    break;
                }
                partitionList.Add(entry);
            }

            PartitionTable = partitionList.ToArray();
        }
        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");
            }
        }