Example #1
0
        public void CreateWholeDisk()
        {
            long capacity         = 3 * 1024 * 1024;
            SparseMemoryStream ms = new SparseMemoryStream();

            ms.SetLength(capacity);
            Geometry           geom  = Geometry.FromCapacity(capacity);
            BiosPartitionTable table = BiosPartitionTable.Initialize(ms, geom);

            int idx = table.Create(WellKnownPartitionType.WindowsFat, true);

            // Make sure the partition fills all but the first track on the disk.
            Assert.AreEqual(geom.TotalSectors, table[idx].SectorCount + geom.SectorsPerTrack);

            // Make sure FAT16 was selected for a disk of this size
            Assert.AreEqual(BiosPartitionTypes.Fat16, table[idx].BiosType);

            // Make sure partition starts where expected
            Assert.AreEqual(new ChsAddress(0, 1, 1), ((BiosPartitionInfo)table[idx]).Start);

            // Make sure partition ends at end of disk
            Assert.AreEqual(geom.ToLogicalBlockAddress(geom.LastSector), table[idx].LastSector);
            Assert.AreEqual(geom.LastSector, ((BiosPartitionInfo)table[idx]).End);

            // Make sure the 'active' flag made it through...
            Assert.IsTrue(((BiosPartitionInfo)table[idx]).IsActive);

            // Make sure the partition index is Zero
            Assert.AreEqual(0, ((BiosPartitionInfo)table[idx]).PrimaryIndex);
        }
        /// <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>();
        }
        /// <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);

            // Write the primary partition table
            disk.Position = header.PartitionEntriesLba * diskGeometry.BytesPerSector;
            disk.Write(entriesBuffer, 0, entriesBuffer.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);

            // Write the alternate partition table
            disk.Position = header.PartitionEntriesLba * diskGeometry.BytesPerSector;
            disk.Write(entriesBuffer, 0, entriesBuffer.Length);

            return(new GuidPartitionTable(disk, diskGeometry));
        }
Example #4
0
        public void Initialize()
        {
            long capacity         = 3 * 1024 * 1024;
            SparseMemoryStream ms = new SparseMemoryStream();

            ms.SetLength(capacity);
            Geometry           geom  = Geometry.FromCapacity(capacity);
            BiosPartitionTable table = BiosPartitionTable.Initialize(ms, geom);

            Assert.AreEqual(0, table.Count);
        }
Example #5
0
        public void CreateBySizeInGap()
        {
            SparseMemoryStream ms   = new SparseMemoryStream();
            Geometry           geom = new Geometry(15, 30, 63);

            ms.SetLength(geom.Capacity);
            BiosPartitionTable table = BiosPartitionTable.Initialize(ms, geom);

            Assert.AreEqual(0, table.CreatePrimaryByCylinder(0, 4, 33, false));
            Assert.AreEqual(1, table.CreatePrimaryByCylinder(10, 14, 33, false));
            table.Create(geom.ToLogicalBlockAddress(new ChsAddress(4, 0, 1)) * 512, WellKnownPartitionType.WindowsFat, true);
        }
Example #6
0
        public void VeryLargePartition()
        {
            long capacity         = 1300 * 1024L * 1024L * 1024;
            SparseMemoryStream ms = new SparseMemoryStream();

            ms.SetLength(capacity);
            Geometry           geom  = Geometry.LbaAssistedBiosGeometry(capacity);
            BiosPartitionTable table = BiosPartitionTable.Initialize(ms, geom);

            // exception occurs here
            int i = table.CreatePrimaryByCylinder(0, 150000, (byte)WellKnownPartitionType.WindowsNtfs, true);

            Assert.AreEqual(150000, geom.ToChsAddress(table[0].LastSector).Cylinder);
        }
Example #7
0
        public void CreateByCylinder()
        {
            SparseMemoryStream ms   = new SparseMemoryStream();
            Geometry           geom = new Geometry(15, 30, 63);

            ms.SetLength(geom.Capacity);
            BiosPartitionTable table = BiosPartitionTable.Initialize(ms, geom);

            Assert.AreEqual(0, table.CreatePrimaryByCylinder(0, 4, 33, false));
            Assert.AreEqual(1, table.CreatePrimaryByCylinder(10, 14, 33, false));

            Assert.AreEqual(geom.ToLogicalBlockAddress(new ChsAddress(0, 1, 1)), table[0].FirstSector);
            Assert.AreEqual(geom.ToLogicalBlockAddress(new ChsAddress(5, 0, 1)) - 1, table[0].LastSector);
            Assert.AreEqual(geom.ToLogicalBlockAddress(new ChsAddress(10, 0, 1)), table[1].FirstSector);
            Assert.AreEqual(geom.ToLogicalBlockAddress(new ChsAddress(14, 29, 63)), table[1].LastSector);
        }
Example #8
0
        public void CreateBySize()
        {
            long capacity         = 3 * 1024 * 1024;
            SparseMemoryStream ms = new SparseMemoryStream();

            ms.SetLength(capacity);
            Geometry           geom  = Geometry.FromCapacity(capacity);
            BiosPartitionTable table = BiosPartitionTable.Initialize(ms, geom);

            int idx = table.Create(2 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false);

            // Make sure the partition is within 10% of the size requested.
            Assert.That((2 * 1024 * 2) * 0.9 < table[idx].SectorCount);

            Assert.AreEqual(geom.ToLogicalBlockAddress(new ChsAddress(0, 1, 1)), table[idx].FirstSector);
            Assert.AreEqual(geom.HeadsPerCylinder - 1, geom.ToChsAddress((int)table[idx].LastSector).Head);
            Assert.AreEqual(geom.SectorsPerTrack, geom.ToChsAddress((int)table[idx].LastSector).Sector);
        }
Example #9
0
        public void CreateWholeDiskAligned()
        {
            long capacity         = 3 * 1024 * 1024;
            SparseMemoryStream ms = new SparseMemoryStream();

            ms.SetLength(capacity);
            Geometry           geom  = Geometry.FromCapacity(capacity);
            BiosPartitionTable table = BiosPartitionTable.Initialize(ms, geom);

            int idx = table.CreateAligned(WellKnownPartitionType.WindowsFat, true, 64 * 1024);

            Assert.AreEqual(0, table[idx].FirstSector % 128);
            Assert.AreEqual(0, (table[idx].LastSector + 1) % 128);
            Assert.Greater(table[idx].SectorCount * 512, capacity * 0.9);

            // Make sure the partition index is Zero
            Assert.AreEqual(0, ((BiosPartitionInfo)table[idx]).PrimaryIndex);
        }
Example #10
0
        public void SetActive()
        {
            long capacity         = 10 * 1024 * 1024;
            SparseMemoryStream ms = new SparseMemoryStream();

            ms.SetLength(capacity);
            Geometry           geom  = Geometry.FromCapacity(capacity);
            BiosPartitionTable table = BiosPartitionTable.Initialize(ms, geom);

            table.Create(1 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false);
            table.Create(2 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false);
            table.Create(3 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false);

            table.SetActivePartition(1);
            table.SetActivePartition(2);
            Assert.IsFalse(((BiosPartitionInfo)table.Partitions[1]).IsActive);
            Assert.IsTrue(((BiosPartitionInfo)table.Partitions[2]).IsActive);
        }
Example #11
0
        public void CreateBySizeInGapAligned()
        {
            SparseMemoryStream ms   = new SparseMemoryStream();
            Geometry           geom = new Geometry(15, 30, 63);

            ms.SetLength(geom.Capacity);
            BiosPartitionTable table = BiosPartitionTable.Initialize(ms, geom);

            Assert.AreEqual(0, table.CreatePrimaryByCylinder(0, 4, 33, false));
            Assert.AreEqual(1, table.CreatePrimaryByCylinder(10, 14, 33, false));

            int idx = table.CreateAligned(3 * 1024 * 1024, WellKnownPartitionType.WindowsFat, true, 64 * 1024);

            Assert.AreEqual(2, idx);

            Assert.AreEqual(0, table[idx].FirstSector % 128);
            Assert.AreEqual(0, (table[idx].LastSector + 1) % 128);
        }
Example #12
0
        public void Delete()
        {
            long capacity         = 10 * 1024 * 1024;
            SparseMemoryStream ms = new SparseMemoryStream();

            ms.SetLength(capacity);
            Geometry           geom  = Geometry.FromCapacity(capacity);
            BiosPartitionTable table = BiosPartitionTable.Initialize(ms, geom);

            Assert.AreEqual(0, table.Create(1 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false));
            Assert.AreEqual(1, table.Create(2 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false));
            Assert.AreEqual(2, table.Create(3 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false));

            long[] sectorCount = new long[] { table[0].SectorCount, table[1].SectorCount, table[2].SectorCount };

            table.Delete(1);

            Assert.AreEqual(2, table.Count);
            Assert.AreEqual(sectorCount[2], table[1].SectorCount);
        }
Example #13
0
        public void LargeDisk()
        {
            long capacity         = 300 * 1024L * 1024L * 1024;
            SparseMemoryStream ms = new SparseMemoryStream();

            ms.SetLength(capacity);
            Geometry           geom  = Geometry.LbaAssistedBiosGeometry(capacity);
            BiosPartitionTable table = BiosPartitionTable.Initialize(ms, geom);

            table.Create(150 * 1024L * 1024L * 1024, WellKnownPartitionType.WindowsNtfs, false);
            table.Create(20 * 1024L * 1024L * 1024, WellKnownPartitionType.WindowsNtfs, false);
            table.Create(20 * 1024L * 1024L * 1024, WellKnownPartitionType.WindowsNtfs, false);

            Assert.AreEqual(3, table.Partitions.Count);
            Assert.Greater(table[0].SectorCount * 512L, 140 * 1024L * 1024L * 1024);
            Assert.Greater(table[1].SectorCount * 512L, 19 * 1024L * 1024L * 1024);
            Assert.Greater(table[2].SectorCount * 512L, 19 * 1024L * 1024L * 1024);

            Assert.Greater(table[0].FirstSector, 0);
            Assert.Greater(table[1].FirstSector, table[0].LastSector);
            Assert.Greater(table[2].FirstSector, table[1].LastSector);
        }