Esempio n. 1
0
        internal Table(byte[] gptBuffer, uint bytesPerSector)
        {
            this.gptBuffer = gptBuffer;
            var tempHeaderOffset = ByteOperations.FindAscii(gptBuffer, "EFI PART");

            if (tempHeaderOffset == null)
            {
                throw new Exception("Bad GPT");
            }

            headerOffset      = (uint)tempHeaderOffset;
            headerSize        = ByteOperations.ReadUInt32(gptBuffer, headerOffset + 0x0C);
            tableOffset       = headerOffset + 0x200;
            FirstUsableSector = ByteOperations.ReadUInt64(gptBuffer, headerOffset + 0x28);
            LastUsableSector  = ByteOperations.ReadUInt64(gptBuffer, headerOffset + 0x30);
            var maxPartitions = ByteOperations.ReadUInt32(gptBuffer, headerOffset + 0x50);

            partitionEntrySize = ByteOperations.ReadUInt32(gptBuffer, headerOffset + 0x54);
            tableSize          = maxPartitions * partitionEntrySize;
            if (tableOffset + tableSize > gptBuffer.Length)
            {
                throw new Exception("Bad GPT");
            }

            var partitionOffset = tableOffset;

            uint number = 1;

            while (partitionOffset < tableOffset + tableSize)
            {
                var name = ByteOperations.ReadUnicodeString(gptBuffer, partitionOffset + 0x38, 0x48)
                           .TrimEnd((char)0, ' ');
                if (name.Length == 0)
                {
                    break;
                }

                var partitionTypeGuid = ByteOperations.ReadGuid(gptBuffer, partitionOffset + 0x00);
                var partitionType     = PartitionType.FromGuid(partitionTypeGuid);

                var currentPartition = new Partition(name, partitionType, bytesPerSector)
                {
                    FirstSector = ByteOperations.ReadUInt64(gptBuffer, partitionOffset + 0x20),
                    LastSector  = ByteOperations.ReadUInt64(gptBuffer, partitionOffset + 0x28),
                    Guid        = ByteOperations.ReadGuid(gptBuffer, partitionOffset + 0x10),
                    Attributes  = ByteOperations.ReadUInt64(gptBuffer, partitionOffset + 0x30)
                };
                Partitions.Add(currentPartition);
                partitionOffset += partitionEntrySize;
                number++;
            }
        }
Esempio n. 2
0
        private static Partition ToPartition(Disk disk, object partition)
        {
            string guid          = (string)partition.GetPropertyValue("GptType");
            var    partitionType = guid != null?PartitionType.FromGuid(Guid.Parse(guid)) : null;

            return(new Partition(disk)
            {
                Number = (uint)partition.GetPropertyValue("PartitionNumber"),
                Id = (string)partition.GetPropertyValue("UniqueId"),
                Letter = (char?)partition.GetPropertyValue("DriveLetter"),
                PartitionType = partitionType,
            });
        }
Esempio n. 3
0
        private static WmiPartition ToWmiPartition(object partition)
        {
            var guid          = (string)partition.GetPropertyValue("GptType");
            var partitionType = guid != null?PartitionType.FromGuid(Guid.Parse(guid)) : null;

            var driveLetter = (char)partition.GetPropertyValue("DriveLetter");

            return(new WmiPartition
            {
                Number = (uint)partition.GetPropertyValue("PartitionNumber"),
                UniqueId = (string)partition.GetPropertyValue("UniqueId"),
                Guid = Guid.Parse((string)partition.GetPropertyValue("Guid")),
                Root = driveLetter != 0 ? PathExtensions.GetRootPath(driveLetter) : null,
                PartitionType = partitionType,
                Size = new ByteSize(Convert.ToUInt64(partition.GetPropertyValue("Size"))),
            });
        }