public bool Format()
        {
            if (!diskDevice.CanWrite)
            {
                return(false);
            }

            BinaryFormat masterboot = new BinaryFormat(new byte[512]);

            masterboot.SetUInt(MasterBootRecord.DiskSignature, diskSignature);
            masterboot.SetUShort(MasterBootRecord.MBRSignature, MasterBootConstants.MBRSignature);

            if (code != null)
            {
                for (uint index = 0; index < MasterBootConstants.CodeAreaSize; index++)
                {
                    masterboot.SetByte(index, code[index]);
                }
            }

            //TODO: write partitions

            diskDevice.WriteBlock(0, 1, masterboot.Data);

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Writes the master boot block.
        /// </summary>
        /// <returns></returns>
        public bool Write()
        {
            if (!diskDevice.CanWrite)
            {
                return(false);
            }

            BinaryFormat masterboot = new BinaryFormat(new byte[512]);

            masterboot.SetUInt(MBR.DiskSignature, diskSignature);
            masterboot.SetUShort(MBR.MBRSignature, MBRConstant.MBRSignature);

            if (code != null)
            {
                for (uint index = 0; ((index < MBRConstant.CodeAreaSize) && (index < code.Length)); index++)
                {
                    masterboot.SetByte(index, code[index]);
                }
            }

            for (uint index = 0; index < MaxMBRPartitions; index++)
            {
                if (Partitions[index].TotalBlocks != 0)
                {
                    uint offset = MBR.FirstPartition + (index * 16);
                    masterboot.SetByte(offset + PartitionRecord.Status, (byte)(Partitions[index].Bootable ? 0x80 : 0x00));
                    masterboot.SetByte(offset + PartitionRecord.PartitionType, Partitions[index].PartitionType);
                    masterboot.SetUInt(offset + PartitionRecord.LBA, Partitions[index].StartLBA);
                    masterboot.SetUInt(offset + PartitionRecord.Sectors, Partitions[index].TotalBlocks);

                    DiskGeometry diskGeometry = new DiskGeometry();
                    diskGeometry.GuessGeometry(diskDevice.TotalBlocks);

                    CHS chsStart = new CHS();
                    CHS chsEnd   = new CHS();

                    chsStart.SetCHS(diskGeometry, Partitions[index].StartLBA);
                    chsEnd.SetCHS(diskGeometry, Partitions[index].StartLBA + Partitions[index].TotalBlocks - 1);

                    masterboot.SetByte(offset + PartitionRecord.FirstCRS, chsStart.Head);
                    masterboot.SetByte(offset + PartitionRecord.FirstCRS + 1, (byte)((chsStart.Sector & 0x3F) | ((chsStart.Cylinder >> 8) & 0x03)));
                    masterboot.SetByte(offset + PartitionRecord.FirstCRS + 2, (byte)(chsStart.Cylinder & 0xFF));
                    masterboot.SetByte(offset + PartitionRecord.LastCRS, chsEnd.Head);
                    masterboot.SetByte(offset + PartitionRecord.LastCRS + 1, (byte)((chsEnd.Sector & 0x3F) | ((chsEnd.Cylinder >> 8) & 0x03)));
                    masterboot.SetByte(offset + PartitionRecord.LastCRS + 2, (byte)(chsEnd.Cylinder & 0xFF));
                }
            }

            diskDevice.WriteBlock(0, 1, masterboot.Data);

            return(true);
        }
 /// <summary>
 /// Writes the block.
 /// </summary>
 /// <param name="block">The block.</param>
 /// <param name="count">The count.</param>
 /// <param name="data">The data.</param>
 /// <returns></returns>
 public bool WriteBlock(uint block, uint count, byte[] data)
 {
     return(diskDevice.WriteBlock(block + StartBlock, count, data));
 }