/// <summary> /// Processes a valid GUID partition table to initialize its partitions. /// </summary> /// <param name="aGPT">The GPT to process.</param> /// <param name="aDiskDevice">The disk device from which the GPT was read.</param> private static void ProcessGPT(GPT aGPT, DiskDevice aDiskDevice) { for (int i = 0; i < aGPT.Partitions.Count; i++) { GPT.PartitionInfo aPartInfo = (GPT.PartitionInfo)aGPT.Partitions[i]; Partitions.Add(new Partition(aDiskDevice, aPartInfo.FirstLBA, aPartInfo.LastLBA - aPartInfo.FirstLBA)); } }
/// <summary> /// Deletes all partitions on the disk. /// </summary> public void Clear() { if (GPT.IsGPTPartition(Host)) { throw new Exception("Removing all partitions with GPT style not yet supported!"); } for (int i = 0; i < Partitions.Count; i++) { DeletePartition(i); } }
/// <summary> /// Attempts to initialise a disk treating it as GPT formatted. /// </summary> /// <param name="aDiskDevice">The disk to initialise.</param> /// <returns>True if a valid GPT was detected and the disk was successfully initialised. Otherwise, false.</returns> private static bool InitAsGPT(DiskDevice aDiskDevice) { GPT TheGPT = new GPT(aDiskDevice); if (!TheGPT.IsValid) { return(false); } else { ProcessGPT(TheGPT, aDiskDevice); return(true); } }
/// <summary> /// Deletes a partition /// </summary> /// <param name="index">Partition index starting from 0</param> public void DeletePartition(int index) { if (GPT.IsGPTPartition(Host)) { throw new Exception("Deleting partitions with GPT style not yet supported!"); } var location = 446 + 16 * index; byte[] mbr = Host.NewBlockArray(1); Host.ReadBlock(0, 1, ref mbr); for (int i = location; i < location + 16; i++) { mbr[i] = 0; } Host.WriteBlock(0, 1, ref mbr); }
/// <summary> /// Initializes storage components /// </summary> private static unsafe void initStorage() { Disk.Init(); // Init partition schemes GPT.Register(); MBR.Register(); // Init filesystems Fat16.Register(); AHCI.Init(); ATA.Init(); NVMe.Init(); PacketFS.Init(); }
/// <summary> /// Create Partition. /// </summary> /// <param name="size">Size in MB.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown if start / end is smaller then 0.</exception> /// <exception cref="ArgumentException">Thrown if end is smaller or equal to start.</exception> /// <exception cref="NotImplementedException">Thrown if partition type is GPT.</exception> public void CreatePartition(int size) { if (size == 0 | size < 0) { throw new ArgumentException("size"); } if (GPT.IsGPTPartition(Host)) { throw new Exception("Creating partitions with GPT style not yet supported!"); } int location; int startingSector = 63; uint amountOfSectors = (uint)(size * 1024 * 1024 / 512); //TODO: Check if partition is too big if (Partitions.Count == 0) { location = 446; startingSector = 63; } else if (Partitions.Count == 1) { location = 462; startingSector = (int)(Partitions[0].Host.BlockSize + Partitions[0].Host.BlockCount); } else if (Partitions.Count == 2) { location = 478; } else if (Partitions.Count == 3) { location = 494; } else { throw new NotImplementedException("Extended partitons not yet supported."); } //Create MBR var mbrData = new byte[512]; Host.ReadBlock(0, 1, ref mbrData); mbrData[location + 0] = 0x80; //bootable mbrData[location + 1] = 0x1; //starting head mbrData[location + 2] = 0; //Starting sector mbrData[location + 3] = 0x0; //Starting Cylinder mbrData[location + 4] = 83; //normal partition mbrData[location + 5] = 0xFE; //ending head mbrData[location + 6] = 0x3F; //Ending Sector mbrData[location + 7] = 0x40; //Ending Cylinder //Starting Sector byte[] startingSectorBytes = BitConverter.GetBytes(startingSector); mbrData[location + 8] = startingSectorBytes[0]; mbrData[location + 9] = startingSectorBytes[1]; mbrData[location + 10] = startingSectorBytes[2]; mbrData[location + 11] = startingSectorBytes[3]; //Total Sectors in partition byte[] total = BitConverter.GetBytes(amountOfSectors); mbrData[location + 12] = total[0]; mbrData[location + 13] = total[1]; mbrData[location + 14] = total[2]; mbrData[location + 15] = total[3]; //Boot flag byte[] boot = BitConverter.GetBytes((ushort)0xAA55); mbrData[510] = boot[0]; mbrData[511] = boot[1]; //Save the data Host.WriteBlock(0, 1, ref mbrData); }