예제 #1
0
 private void WriteRecord(int i, BiosPartitionRecord newRecord)
 {
     _diskData.Position = 0;
     byte[] bootSector = Utilities.ReadFully(_diskData, Utilities.SectorSize);
     newRecord.WriteTo(bootSector, 0x01BE + (i * 16));
     _diskData.Position = 0;
     _diskData.Write(bootSector, 0, bootSector.Length);
 }
예제 #2
0
        private static BiosPartitionRecord[] ReadPrimaryRecords(byte[] bootSector)
        {
            BiosPartitionRecord[] records = new BiosPartitionRecord[4];
            for (int i = 0; i < 4; ++i)
            {
                records[i] = new BiosPartitionRecord(bootSector, 0x01BE + (i * 0x10), 0, i);
            }

            return(records);
        }
예제 #3
0
        /// <summary>
        /// Updates the CHS fields in partition records to reflect a new BIOS geometry.
        /// </summary>
        /// <param name="geometry">The disk's new BIOS geometry</param>
        /// <remarks>The partitions are not relocated to a cylinder boundary, just the CHS fields are updated on the
        /// assumption the LBA fields are definitive.</remarks>
        public void UpdateBiosGeometry(Geometry geometry)
        {
            _diskData.Position = 0;
            byte[] bootSector = Utilities.ReadFully(_diskData, Utilities.SectorSize);

            BiosPartitionRecord[] records = ReadPrimaryRecords(bootSector);
            for (int i = 0; i < records.Length; ++i)
            {
                BiosPartitionRecord record = records[i];
                if (record.IsValid)
                {
                    ChsAddress newStartAddress = geometry.ToChsAddress(record.LBAStartAbsolute);
                    if (newStartAddress.Cylinder > 1023)
                    {
                        newStartAddress = new ChsAddress(1023, geometry.HeadsPerCylinder - 1, geometry.SectorsPerTrack);
                    }

                    ChsAddress newEndAddress = geometry.ToChsAddress(record.LBAStartAbsolute + record.LBALength - 1);
                    if (newEndAddress.Cylinder > 1023)
                    {
                        newEndAddress = new ChsAddress(1023, geometry.HeadsPerCylinder - 1, geometry.SectorsPerTrack);
                    }

                    record.StartCylinder = (ushort)newStartAddress.Cylinder;
                    record.StartHead     = (byte)newStartAddress.Head;
                    record.StartSector   = (byte)newStartAddress.Sector;
                    record.EndCylinder   = (ushort)newEndAddress.Cylinder;
                    record.EndHead       = (byte)newEndAddress.Head;
                    record.EndSector     = (byte)newEndAddress.Sector;

                    WriteRecord(i, record);
                }
            }

            _diskGeometry = geometry;
        }
예제 #4
0
 internal BiosPartitionInfo(BiosPartitionTable table, BiosPartitionRecord record)
 {
     _table  = table;
     _record = record;
 }
예제 #5
0
 private BiosPartitionRecord[] GetExtendedRecords(BiosPartitionRecord r)
 {
     return(new BiosExtendedPartitionTable(_diskData, r.LBAStart).GetPartitions());
 }
예제 #6
0
 private static bool IsExtendedPartition(BiosPartitionRecord r)
 {
     return(r.PartitionType == BiosPartitionTypes.Extended || r.PartitionType == BiosPartitionTypes.ExtendedLba);
 }
예제 #7
0
 internal SparseStream Open(BiosPartitionRecord record)
 {
     return(new SubStream(_diskData, Ownership.None, ((long)record.LBAStartAbsolute) * Utilities.SectorSize, ((long)record.LBALength) * Utilities.SectorSize));
 }
예제 #8
0
        /// <summary>
        /// Creates a new Primary Partition, specified by Logical Block Addresses.
        /// </summary>
        /// <param name="first">The LBA address of the first sector (inclusive)</param>
        /// <param name="last">The LBA address of the last sector (inclusive)</param>
        /// <param name="type">The BIOS (MBR) type of the new partition</param>
        /// <param name="markActive">Whether to mark the partition active (bootable)</param>
        /// <returns>The index of the new partition</returns>
        public int CreatePrimaryBySector(long first, long last, byte type, bool markActive)
        {
            if (first >= last)
            {
                throw new ArgumentException("The first sector in a partition must be before the last");
            }

            if ((last + 1) * _diskGeometry.BytesPerSector > _diskData.Length)
            {
                throw new ArgumentOutOfRangeException("last", last, "The last sector extends beyond the end of the disk");
            }

            BiosPartitionRecord[] existing = GetPrimaryRecords();

            BiosPartitionRecord newRecord = new BiosPartitionRecord();
            ChsAddress          startAddr = _diskGeometry.ToChsAddress(first);
            ChsAddress          endAddr   = _diskGeometry.ToChsAddress(last);

            // Because C/H/S addresses can max out at lower values than the LBA values,
            // the special tuple (1023, 254, 63) is used.
            if (startAddr.Cylinder > 1023)
            {
                startAddr = new ChsAddress(1023, 254, 63);
            }

            if (endAddr.Cylinder > 1023)
            {
                endAddr = new ChsAddress(1023, 254, 63);
            }

            newRecord.StartCylinder = (ushort)startAddr.Cylinder;
            newRecord.StartHead     = (byte)startAddr.Head;
            newRecord.StartSector   = (byte)startAddr.Sector;
            newRecord.EndCylinder   = (ushort)endAddr.Cylinder;
            newRecord.EndHead       = (byte)endAddr.Head;
            newRecord.EndSector     = (byte)endAddr.Sector;
            newRecord.LBAStart      = (uint)first;
            newRecord.LBALength     = (uint)(last - first + 1);
            newRecord.PartitionType = type;
            newRecord.Status        = (byte)(markActive ? 0x80 : 0x00);

            // First check for overlap with existing partition...
            foreach (var r in existing)
            {
                if (Utilities.RangesOverlap((uint)first, (uint)last + 1, r.LBAStartAbsolute, r.LBAStartAbsolute + r.LBALength))
                {
                    throw new IOException("New partition overlaps with existing partition");
                }
            }

            // Now look for empty partition
            for (int i = 0; i < 4; ++i)
            {
                if (!existing[i].IsValid)
                {
                    WriteRecord(i, newRecord);
                    return(i);
                }
            }

            throw new IOException("No primary partition slots available");
        }