コード例 #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     = GptType.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++;
            }
        }
コード例 #2
0
        private static WmiPartition ToWmiPartition(object partition)
        {
            var gptType       = (string)partition.GetPropertyValue("GptType");
            var partitionType = gptType != null?GptType.FromGuid(Guid.Parse(gptType)) : null;

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

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