/// <summary>
        /// Initializes a new instance of the BiosPartitionedDiskBuilder class.
        /// </summary>
        /// <param name="capacity">The capacity of the disk (in bytes)</param>
        /// <param name="biosGeometry">The BIOS geometry of the disk</param>
        public BiosPartitionedDiskBuilder(long capacity, Geometry biosGeometry)
        {
            _capacity     = capacity;
            _biosGeometry = biosGeometry;

            _bootSectors = new SparseMemoryStream();
            _bootSectors.SetLength(capacity);
            _partitionTable = BiosPartitionTable.Initialize(_bootSectors, _biosGeometry);

            _partitionContents = new Dictionary <int, BuilderExtent>();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new partition table on a disk.
        /// </summary>
        /// <param name="disk">The stream containing the disk data</param>
        /// <param name="diskGeometry">The geometry of the disk</param>
        /// <returns>An object to access the newly created partition table</returns>
        public static GuidPartitionTable Initialize(Stream disk, Geometry diskGeometry)
        {
            // Create the protective MBR partition record.
            BiosPartitionTable pt = BiosPartitionTable.Initialize(disk, diskGeometry);

            pt.CreatePrimaryByCylinder(0, diskGeometry.Cylinders - 1, BiosPartitionTypes.GptProtective, false);

            // Create the GPT headers, and blank-out the entry areas
            const int EntryCount = 128;
            const int EntrySize  = 128;

            int entrySectors = ((EntryCount * EntrySize) + diskGeometry.BytesPerSector - 1) / diskGeometry.BytesPerSector;

            byte[] entriesBuffer = new byte[EntryCount * EntrySize];

            // Prepare primary header
            GptHeader header = new GptHeader(diskGeometry.BytesPerSector);

            header.HeaderLba           = 1;
            header.AlternateHeaderLba  = (disk.Length / diskGeometry.BytesPerSector) - 1;
            header.FirstUsable         = header.HeaderLba + entrySectors + 1;
            header.LastUsable          = header.AlternateHeaderLba - entrySectors - 1;
            header.DiskGuid            = Guid.NewGuid();
            header.PartitionEntriesLba = 2;
            header.PartitionEntryCount = EntryCount;
            header.PartitionEntrySize  = EntrySize;
            header.EntriesCrc          = CalcEntriesCrc(entriesBuffer);

            // Write the primary header
            byte[] headerBuffer = new byte[diskGeometry.BytesPerSector];
            header.WriteTo(headerBuffer, 0);
            disk.Position = header.HeaderLba * diskGeometry.BytesPerSector;
            disk.Write(headerBuffer, 0, headerBuffer.Length);

            // Calc alternate header
            header.HeaderLba           = header.AlternateHeaderLba;
            header.AlternateHeaderLba  = 1;
            header.PartitionEntriesLba = header.HeaderLba - entrySectors;

            // Write the alternate header
            header.WriteTo(headerBuffer, 0);
            disk.Position = header.HeaderLba * diskGeometry.BytesPerSector;
            disk.Write(headerBuffer, 0, headerBuffer.Length);

            return(new GuidPartitionTable(disk, diskGeometry));
        }