|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel.DataAnnotations; |
| 4 | +using System.IO; |
| 5 | +using System.Text; |
| 6 | +using TotalImage.Containers; |
| 7 | + |
| 8 | +namespace TotalImage.Partitions |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// |
| 12 | + /// </summary> |
| 13 | + public class MbrPartitionTable : PartitionTable |
| 14 | + { |
| 15 | + // this is a hardcoded assumption that holds true for currently supported formats but should probably be reconsidered in the future |
| 16 | + private const int SECTOR_SIZE = 512; |
| 17 | + |
| 18 | + /// <inheritdoc /> |
| 19 | + public MbrPartitionTable(Container container) : base(container) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + /// <inheritdoc /> |
| 24 | + protected override IEnumerable<PartitionEntry> LoadPartitions() |
| 25 | + { |
| 26 | + using BinaryReader br = new BinaryReader(_container.Content, Encoding.ASCII, true); |
| 27 | + br.BaseStream.Seek(0x1FE, SeekOrigin.Begin); |
| 28 | + |
| 29 | + var signature = br.ReadUInt16(); |
| 30 | + if (signature != 0xaa55) |
| 31 | + { |
| 32 | + throw new InvalidDataException(); |
| 33 | + } |
| 34 | + |
| 35 | + br.BaseStream.Seek(0x1BE, SeekOrigin.Begin); |
| 36 | + |
| 37 | + List<PartitionEntry> entries = new List<PartitionEntry>(); |
| 38 | + for (int i = 0; i < 4; i++) |
| 39 | + { |
| 40 | + byte status = br.ReadByte(); |
| 41 | + byte[] chsStart = br.ReadBytes(3); |
| 42 | + MbrPartitionType type = (MbrPartitionType)br.ReadByte(); |
| 43 | + byte[] chsEnd = br.ReadBytes(3); |
| 44 | + uint lbaStart = br.ReadUInt32(); |
| 45 | + uint lbaLength = br.ReadUInt32(); |
| 46 | + |
| 47 | + if (type == MbrPartitionType.Empty) |
| 48 | + { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + uint offset = lbaStart * SECTOR_SIZE; |
| 53 | + uint length = lbaLength * SECTOR_SIZE; |
| 54 | + var entry = new MbrPartitionEntry((status & 0x80) != 0, type, offset, length, new PartialStream(_container.Content, offset, length)); |
| 55 | + entries.Add(entry); |
| 56 | + } |
| 57 | + |
| 58 | + return entries; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// A partition entry within an MBR Partition Table |
| 63 | + /// </summary> |
| 64 | + public class MbrPartitionEntry : PartitionEntry |
| 65 | + { |
| 66 | + private bool _active; |
| 67 | + private MbrPartitionType _type; |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Indicates whether this partition is marked as active |
| 71 | + /// </summary> |
| 72 | + public bool Active |
| 73 | + { |
| 74 | + get => _active; |
| 75 | + set => _active = value; |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Indicates the type of partition |
| 80 | + /// </summary> |
| 81 | + public MbrPartitionType Type |
| 82 | + { |
| 83 | + get => _type; |
| 84 | + set => _type = value; |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Initialises an MBR partition entry |
| 89 | + /// </summary> |
| 90 | + /// <param name="active">Whether the partition is marked as active</param> |
| 91 | + /// <param name="type">The type of the partition</param> |
| 92 | + /// <param name="offset">The offset of the partition in it's container file</param> |
| 93 | + /// <param name="length">The length of the partition</param> |
| 94 | + /// <param name="stream">The stream containing the partition data</param> |
| 95 | + public MbrPartitionEntry(bool active, MbrPartitionType type, uint offset, uint length, Stream stream) |
| 96 | + : base(offset, length, stream) |
| 97 | + { |
| 98 | + _active = active; |
| 99 | + _type = type; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// The type of partition |
| 105 | + /// </summary> |
| 106 | + public enum MbrPartitionType : byte |
| 107 | + { |
| 108 | + /// <summary> |
| 109 | + /// An empty partition entry |
| 110 | + /// </summary> |
| 111 | + [Display(Name = "Empty")] |
| 112 | + Empty = 0, |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// A FAT-12 partition for use with DOS |
| 116 | + /// </summary> |
| 117 | + [Display(Name = "DOS (FAT-12)")] |
| 118 | + DosFat12 = 0x01 |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments