Esempio n. 1
0
        /// <summary>
        /// Formats the specified using the specified partition informations.
        /// </summary>
        /// <param name="aDisk">The disk to format.</param>
        /// <param name="partitionInfos">The partition informations to use for the format.</param>
        public static void FormatDisk(Hardware.Devices.DiskDevice aDisk, List partitionInfos)
        {
            //Necessary to remove any trace of GPT:
            //  Overwrite first 256 sectors with 0s (should do the trick)
            aDisk.WriteBlock(0UL, 256U, null);

#if MBR_TRACE
            BasicConsole.WriteLine("Creating new MBR data...");
#endif
            byte[] newMBRData = new byte[512];

            newMBRData[0x1FE] = 0x55;
            newMBRData[0x1FF] = 0xAA;

#if MBR_TRACE
            BasicConsole.WriteLine(((FOS_System.String)"Set signature: ") + newMBRData[0x1FE] + " " + newMBRData[0x1FF]);
            BasicConsole.DelayOutput(1);

            BasicConsole.WriteLine(((FOS_System.String)"Num new partitions: ") + partitionInfos.Count);
            BasicConsole.DelayOutput(1);
#endif

            uint part1Offset = 0x1BE;
            for (uint i = 0; i < partitionInfos.Count; i++)
            {
                PartitionInfo partInfo = (PartitionInfo)partitionInfos[(int)i];
                uint partOffset = part1Offset + (0x10 * i);
#if MBR_TRACE
                BasicConsole.WriteLine(((FOS_System.String)"Partition ") + i + " @ " + partOffset);
                BasicConsole.WriteLine(((FOS_System.String)"Bootable : ") + partInfo.Bootable);
                BasicConsole.WriteLine(((FOS_System.String)"SystemID : ") + partInfo.SystemID);
                BasicConsole.WriteLine(((FOS_System.String)"StartSector : ") + partInfo.StartSector);
                BasicConsole.WriteLine(((FOS_System.String)"SectorCount : ") + partInfo.SectorCount);
                BasicConsole.DelayOutput(2);
#endif

                //Bootable / active
                newMBRData[partOffset + 0] = (byte)(partInfo.Bootable ? 0x81 : 0x00);
                //System ID
                newMBRData[partOffset + 4] = partInfo.SystemID;
                //Start sector
                newMBRData[partOffset + 8] = (byte)(partInfo.StartSector);
                newMBRData[partOffset + 9] = (byte)(partInfo.StartSector >> 8);
                newMBRData[partOffset + 10] = (byte)(partInfo.StartSector >> 16);
                newMBRData[partOffset + 11] = (byte)(partInfo.StartSector >> 24);
                //Sector count
                newMBRData[partOffset + 12] = (byte)(partInfo.SectorCount);
                newMBRData[partOffset + 13] = (byte)(partInfo.SectorCount >> 8);
                newMBRData[partOffset + 14] = (byte)(partInfo.SectorCount >> 16);
                newMBRData[partOffset + 15] = (byte)(partInfo.SectorCount >> 24);
   
#if MBR_TRACE
                BasicConsole.WriteLine("Reading back data...");
                byte bootable = newMBRData[partOffset + 0];
                byte systemID = newMBRData[partOffset + 4];
                UInt32 startSector = ByteConverter.ToUInt32(newMBRData, partOffset + 8);
                UInt32 sectorCount = ByteConverter.ToUInt32(newMBRData, partOffset + 12);
                BasicConsole.WriteLine(((FOS_System.String)"Bootable : ") + bootable);
                BasicConsole.WriteLine(((FOS_System.String)"SystemID : ") + systemID);
                BasicConsole.WriteLine(((FOS_System.String)"StartSector : ") + startSector);
                BasicConsole.WriteLine(((FOS_System.String)"SectorCount : ") + sectorCount);
                BasicConsole.DelayOutput(2);
#endif
            }
            
#if MBR_TRACE
            BasicConsole.WriteLine("Writing data...");
#endif
            aDisk.WriteBlock(0UL, 1U, newMBRData);
#if MBR_TRACE
            BasicConsole.WriteLine("Data written.");
            BasicConsole.DelayOutput(1);
#endif
            aDisk.CleanCaches();
        }