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); }
private bool ReadMasterBootBlock() { valid = false; if (diskDevice.BlockSize != 512) // only going to work with 512 sector sizes { return(false); } BinaryFormat masterboot = new BinaryFormat(diskDevice.ReadBlock(0, 1)); ushort mbrsignature = masterboot.GetUShort(MasterBootRecord.MBRSignature); diskSignature = masterboot.GetUInt(MasterBootRecord.DiskSignature); valid = (mbrsignature == MasterBootConstants.MBRSignature); if (valid) { for (uint index = 0; index < MaxMBRPartitions; index++) { uint offset = MasterBootRecord.PrimaryPartitions + (index * 16); GenericPartition partition = new GenericPartition(index); partition.Bootable = masterboot.GetByte(offset + PartitionRecord.Status) == 0x80; partition.PartitionType = masterboot.GetByte(offset + PartitionRecord.PartitionType); partition.StartLBA = masterboot.GetUInt(offset + PartitionRecord.LBA); partition.TotalBlocks = masterboot.GetUInt(offset + PartitionRecord.Sectors); partitions[index] = partition; } //TODO: Extended Partitions } code = new byte[MasterBootConstants.CodeAreaSize]; for (uint index = 0; index < MasterBootConstants.CodeAreaSize; index++) { code[index] = masterboot.GetByte(index); } return(valid); }
// doesn't work since uint should be uint64 //protected bool ReadLBA48 (SectorOperation operation, uint drive, uint lba, MemoryBlock memory) //{ // FeaturePort.Write8 (0); // FeaturePort.Write8 (0); // SectorCountPort.Write8 (0); // SectorCountPort.Write8 (1); // LBALowPort.Write8 ((byte)((lba >> 24) & 0xFF)); // LBALowPort.Write8 ((byte)(lba & 0xFF)); // LBAMidPort.Write8 ((byte)((lba >> 32) & 0xFF)); // LBAMidPort.Write8 ((byte)((lba >> 8) & 0xFF)); // LBAHighPort.Write8 ((byte)((lba >> 40) & 0xFF)); // LBAHighPort.Write8 ((byte)((lba >> 16) & 0xFF)); // DeviceHeadPort.Write8 ((byte)(0x40 | (drive << 4))); // if (operation == SectorOperation.Write) // CommandPort.Write8 (0x34); // else // CommandPort.Write8 (0x24); // if (!WaitForReqisterReady ()) // return false; // //TODO: Don't use PIO // if (operation == SectorOperation.Read) { // for (uint index = 0; index < 256; index++) // memory.SetUShort (index * 2, DataPort.Read16 ()); // } // else { // for (uint index = 0; index < 256; index++) // DataPort.WriteUShort (memory.GetUShort (index * 2)); // } // return true; //} public bool Open (uint driveNbr) { if (driveNbr == 0) DeviceHeadPort.Write8 (0xA0); else if (driveNbr == 1) DeviceHeadPort.Write8 (0xB0); else return false; CommandPort.Write8 (IDECommand.IdentifyDrive); if (!WaitForReqisterReady ()) return false; BinaryFormat info = new BinaryFormat (new byte[512]); for (uint index = 0; index < 256; index++) info.SetUShort (index * 2, DataPort.Read16 ()); driveInfo[driveNbr].MaxLBA = info.GetUInt (IdentifyDrive.MaxLBA28); // legacy info driveInfo[driveNbr].Heads = info.GetUShort (IdentifyDrive.LogicalHeads); driveInfo[driveNbr].Cylinders = info.GetUShort (IdentifyDrive.LogicalCylinders); driveInfo[driveNbr].SectorsPerTrack = info.GetUShort (IdentifyDrive.LogicalSectors); return true; }
protected bool PerformLBA28 (SectorOperation operation, uint driveNbr, uint lba, byte[] data, uint offset) { FeaturePort.Write8 (0); SectorCountPort.Write8 (1); LBALowPort.Write8 ((byte)(lba & 0xFF)); LBAMidPort.Write8 ((byte)((lba >> 8) & 0xFF)); LBAHighPort.Write8 ((byte)((lba >> 16) & 0xFF)); DeviceHeadPort.Write8 ((byte)(0xE0 | (driveNbr << 4) | ((lba >> 24) & 0x0F))); if (operation == SectorOperation.Write) CommandPort.Write8 (IDECommand.WriteSectorsWithRetry); else CommandPort.Write8 (IDECommand.ReadSectorsWithRetry); if (!WaitForReqisterReady ()) return false; BinaryFormat sector = new BinaryFormat (data); //TODO: Don't use PIO if (operation == SectorOperation.Read) { for (uint index = 0; index < 256; index++) sector.SetUShort (offset + (index * 2), DataPort.Read16 ()); } else { for (uint index = 0; index < 256; index++) DataPort.Write16 (sector.GetUShort (offset + (index * 2))); } return true; }
public bool Compare (byte[] data, uint offset, FATType type) { BinaryFormat entry = new BinaryFormat (data); byte first = entry.GetByte (offset + Entry.DOSName); if (first == FileNameAttribute.LastEntry) return false; if ((first == FileNameAttribute.Deleted) | (first == FileNameAttribute.Dot)) return false; if (first == FileNameAttribute.Escape) return false; uint startcluster = FAT.GetClusterEntry (data, offset, type); if (startcluster == cluster) return true; return false; }
public void Delete (uint childBlock, uint parentBlock, uint parentBlockIndex) { BinaryFormat entry = new BinaryFormat (partition.ReadBlock (parentBlock, 1)); entry.SetByte ((parentBlockIndex * Entry.EntrySize) + Entry.DOSName, (byte)FileNameAttribute.Deleted); partition.WriteBlock (parentBlock, 1, entry.Data); if (!FreeClusterChain (childBlock)) throw new System.ArgumentException (); //throw new IOException ("Unable to free all cluster allocations in fat"); }
public uint GetFileSize (uint directoryBlock, uint index) { BinaryFormat directory = new BinaryFormat (partition.ReadBlock (directoryBlock, 1)); return directory.GetUInt ((index * Entry.EntrySize) + Entry.FileSize); }
public DirectoryEntryLocation FindEntry (FAT.ICompare compare, uint startCluster) { uint activeSector = (startCluster == 0) ? firstRootDirectorySector : (startCluster * this.sectorsPerCluster); uint increment = 0; for (; ; ) { BinaryFormat directory = new BinaryFormat (partition.ReadBlock (activeSector, 1)); for (uint index = 0; index < entriesPerSector; index++) { if (directory.GetByte ((index * Entry.EntrySize) + Entry.DOSName) == FileNameAttribute.LastEntry) return new DirectoryEntryLocation (); FileAttributes attribute = (FileAttributes)directory.GetByte ((index * Entry.EntrySize) + Entry.FileAttributes); if (compare.Compare (directory.Data, index * 32, fatType)) return new DirectoryEntryLocation (GetClusterEntry (directory.Data, index, fatType), activeSector, index, (attribute & FileAttributes.SubDirectory) != 0); } ++increment; if (startCluster == 0) { // root directory if (increment >= rootDirSectors) return new DirectoryEntryLocation (); activeSector = startCluster + increment; continue; } else { // subdirectory if (increment < sectorsPerCluster) { // still within cluster activeSector = startCluster + increment; continue; } // exiting cluster // goto next cluster (if any) uint cluster = GetClusterBySector (startCluster); if (cluster == 0) return new DirectoryEntryLocation (); uint nextCluster = GetClusterEntryValue (cluster); if ((IsClusterLast (nextCluster)) || (IsClusterBad (nextCluster)) || (IsClusterFree (nextCluster)) || (IsClusterReserved (nextCluster))) return new DirectoryEntryLocation (); activeSector = (uint)(dataAreaStart + (nextCluster - 1 * sectorsPerCluster)); continue; } } }
public static string ExtractFileName (byte[] directory, uint index) { BinaryFormat entry = new BinaryFormat (directory); char[] name = new char[12]; for (uint i = 0; i < 8; i++) name[i] = (char)entry.GetByte (index + i + Entry.DOSName); int len = 8; for (int i = 7; i > 0; i--) if (name[i] == ' ') len--; else break; // special case where real character is same as the delete if ((len >= 1) && (name[0] == (char)FileNameAttribute.Escape)) name[0] = (char)FileNameAttribute.Deleted; name[len] = '.'; len++; for (uint i = 0; i < 3; i++) name[len + i] = (char)entry.GetByte (index + i + Entry.DOSExtension); len = len + 3; int spaces = 0; for (int i = len - 1; i >= 0; i--) if (name[i] == ' ') spaces++; else break; if (spaces == 3) spaces = 4; len = len - spaces; string result = SharpOS.Kernel.Foundation.Convert.ToString (name, 0, len); //Runtime.Free (name); return result; }
protected bool SetClusterEntryValue (uint cluster, uint nextcluster) { uint fatOffset = 0; if (fatType == FATType.FAT12) fatOffset = (cluster + (cluster / 2)); else if (fatType == FATType.FAT16) fatOffset = cluster * 2; else //if (type == FatType.FAT32) fatOffset = cluster * 4; uint sector = fatStart + (fatOffset / bytesPerSector); uint sectorOffset = fatOffset % bytesPerSector; uint nbrSectors = 1; if ((fatType == FATType.FAT12) && (sectorOffset == bytesPerSector - 1)) nbrSectors = 2; BinaryFormat fat = new BinaryFormat (partition.ReadBlock (sector, nbrSectors)); if (fatType == FATType.FAT12) { uint clustervalue = fat.GetUShort (sectorOffset); if (cluster % 2 == 1) clustervalue = ((clustervalue & 0xF) | (nextcluster << 4)); else clustervalue = ((clustervalue & 0xf000) | (nextcluster)); fat.SetUShort (sectorOffset, (ushort)clustervalue); } else if (fatType == FATType.FAT16) fat.SetUShort (sectorOffset, (ushort)nextcluster); else //if (type == FatType.FAT32) fat.SetUInt (sectorOffset, nextcluster); partition.WriteBlock (sector, nbrSectors, fat.Data); return true; }
//protected uint ClusterToFirstSector (uint cluster) //{ // return ((cluster - 2) * sectorspercluster) + firstdatasector; //} protected uint GetClusterEntryValue (uint cluster) { uint fatoffset = 0; if (fatType == FATType.FAT12) fatoffset = (cluster + (cluster / 2)); else if (fatType == FATType.FAT16) fatoffset = cluster * 2; else //if (type == FatType.FAT32) fatoffset = cluster * 4; uint sector = fatStart + (fatoffset / bytesPerSector); uint sectorOffset = fatoffset % bytesPerSector; uint nbrSectors = 1; if ((fatType == FATType.FAT12) && (sectorOffset == bytesPerSector - 1)) nbrSectors = 2; BinaryFormat fat = new BinaryFormat (partition.ReadBlock (sector, nbrSectors)); uint clusterValue; if (fatType == FATType.FAT12) { clusterValue = fat.GetUShort (sectorOffset); if (cluster % 2 == 1) clusterValue = clusterValue >> 4; else clusterValue = clusterValue & 0x0fff; } else if (fatType == FATType.FAT16) clusterValue = fat.GetUShort (sectorOffset); else //if (type == FatType.FAT32) clusterValue = fat.GetUInt (sectorOffset) & 0x0fffffff; return clusterValue; }
public bool Format (FATSettings fatSettings) { if (!partition.CanWrite) return false; this.fatType = fatSettings.FatType; bytesPerSector = 512; totalSectors = partition.BlockCount; sectorsPerCluster = GetSectorsPerClusterByTotalSectors (fatType, totalSectors); nbrFats = 2; if (fatType == FATType.FAT32) { reservedSectors = 32; rootEntries = 0; } else { reservedSectors = 1; rootEntries = 512; } rootDirSectors = (((rootEntries * 32) + (bytesPerSector - 1)) / bytesPerSector); fatStart = reservedSectors; uint val1 = totalSectors - (reservedSectors + rootDirSectors); uint val2 = (uint)((256 * sectorsPerCluster) + nbrFats); if (fatType == FATType.FAT32) val2 = val2 / 2; uint sectorsperfat = (val1 + (val2 - 1)) / val2; BinaryFormat bootSector = new BinaryFormat (512); bootSector.SetUInt (BootSector.JumpInstruction, 0); bootSector.SetString (BootSector.EOMName, "MSWIN4.1"); bootSector.SetUShort (BootSector.BytesPerSector, (ushort)bytesPerSector); bootSector.SetByte (BootSector.SectorsPerCluster, (byte)sectorsPerCluster); bootSector.SetUShort (BootSector.ReservedSectors, (ushort)reservedSectors); bootSector.SetByte (BootSector.FatAllocationTables, nbrFats); bootSector.SetUShort (BootSector.MaxRootDirEntries, (ushort)rootEntries); if (totalSectors > 0xFFFF) { bootSector.SetUShort (BootSector.TotalSectors, 0); bootSector.SetUInt (BootSector.FAT32_TotalSectors, totalClusters); } else { bootSector.SetUShort (BootSector.TotalSectors, (ushort)totalSectors); bootSector.SetUInt (BootSector.FAT32_TotalSectors, 0); } bootSector.SetByte (BootSector.MediaDescriptor, 0xF8); if (fatType == FATType.FAT32) bootSector.SetUShort (BootSector.SectorsPerFAT, 0); else bootSector.SetUShort (BootSector.SectorsPerFAT, (ushort)sectorsperfat); bootSector.SetUShort (BootSector.SectorsPerTrack, 0); ////FIXME bootSector.SetUInt (BootSector.HiddenSectors, 0); if (fatType != FATType.FAT32) { bootSector.SetByte (BootSector.PhysicalDriveNbr, 0x80); bootSector.SetByte (BootSector.ReservedCurrentHead, 0); bootSector.SetByte (BootSector.ExtendedBootSignature, 0x29); bootSector.SetBytes (BootSector.IDSerialNumber, fatSettings.SerialID, 0, (uint)(fatSettings.SerialID.Length <= 4 ? fatSettings.SerialID.Length : 4)); bootSector.SetString (BootSector.VolumeLabel, " "); // 12 blank spaces bootSector.SetString (BootSector.VolumeLabel, fatSettings.VolumeLabel, (uint)(fatSettings.VolumeLabel.Length <= 12 ? fatSettings.VolumeLabel.Length : 12)); bootSector.SetUShort (BootSector.BootSectorSignature, 0x55AA); //BootSector.OSBootCode // TODO } if (fatType == FATType.FAT12) bootSector.SetString (BootSector.FATType, "FAT12 "); else if (fatType == FATType.FAT16) bootSector.SetString (BootSector.FATType, "FAT16 "); else // if (type == FatType.FAT32) bootSector.SetString (BootSector.FATType, "FAT32 "); if (fatType == FATType.FAT32) { bootSector.SetUInt (BootSector.FAT32_SectorPerFAT, sectorsperfat); bootSector.SetByte (BootSector.FAT32_Flags, 0); bootSector.SetUShort (BootSector.FAT32_Version, 0); bootSector.SetUInt (BootSector.FAT32_ClusterNumberOfRoot, 2); bootSector.SetUShort (BootSector.FAT32_SectorFSInformation, 1); bootSector.SetUShort (BootSector.FAT32_SecondBootSector, 6); //FAT32_Reserved1 bootSector.SetByte (BootSector.FAT32_PhysicalDriveNbr, 0x80); bootSector.SetByte (BootSector.FAT32_Reserved2, 0); bootSector.SetByte (BootSector.FAT32_ExtendedBootSignature, 0x29); bootSector.SetBytes (BootSector.FAT32_IDSerialNumber, fatSettings.SerialID, 0, (uint)(fatSettings.SerialID.Length <= 4 ? fatSettings.SerialID.Length : 4)); bootSector.SetString (BootSector.FAT32_VolumeLabel, " "); // 12 blank spaces bootSector.SetString (BootSector.FAT32_VolumeLabel, fatSettings.VolumeLabel, (uint)(fatSettings.VolumeLabel.Length <= 12 ? fatSettings.VolumeLabel.Length : 12)); bootSector.SetString (BootSector.FAT32_FATType, "FAT32 "); //BootSector.OSBootCode // TODO } // Write Boot Sector partition.WriteBlock (0, 1, bootSector.Data); // Write backup Boot Sector if (fatType == FATType.FAT32) { partition.WriteBlock (0, 1, bootSector.Data); // FIXME: wrong block # } // create FSInfo Structure if (fatType == FATType.FAT32) { BinaryFormat infoSector = new BinaryFormat (512); infoSector.SetUInt (FSInfo.FSI_LeadSignature, 0x41615252); //FSInfo.FSI_Reserved1 infoSector.SetUInt (FSInfo.FSI_StructureSigature, 0x61417272); infoSector.SetUInt (FSInfo.FSI_FreeCount, 0xFFFFFFFF); infoSector.SetUInt (FSInfo.FSI_NextFree, 0xFFFFFFFF); //FSInfo.FSI_Reserved2 bootSector.SetUInt (FSInfo.FSI_TrailSignature, 0xAA550000); partition.WriteBlock (1, 1, infoSector.Data); partition.WriteBlock (7, 1, infoSector.Data); // create 2nd sector BinaryFormat secondSector = new BinaryFormat (512); secondSector.SetUInt ((ushort)FSInfo.FSI_TrailSignature2, 0xAA55); partition.WriteBlock (2, 1, secondSector.Data); partition.WriteBlock (8, 1, secondSector.Data); } // create fats /// TODO: incomplete BinaryFormat emptyFat = new BinaryFormat (512); // clear primary & secondary fats entries for (uint i = fatStart; i < fatStart + 1; i++) { //FIXME partition.WriteBlock (i, 1, emptyFat.Data); } // first block is special BinaryFormat firstFat = new BinaryFormat (512); // TODO: incomplete partition.WriteBlock (reservedSectors, 1, emptyFat.Data); return ReadBootSector (); }
protected bool ReadBootSector () { valid = false; if (blockSize != 512) // only going to work with 512 sector sizes (for now) return false; BinaryFormat bootSector = new BinaryFormat (partition.ReadBlock (0, 1)); byte bootSignature = bootSector.GetByte (BootSector.ExtendedBootSignature); if ((bootSignature != 0x29) && (bootSignature != 0x28)) return false; //TextMode.Write ("EOM NAME: "); //for (uint i = 0; i < 8; i++) // TextMode.WriteChar (bootsector.GetByte (BootSector.EOMName + i)); //TextMode.WriteLine (); bytesPerSector = bootSector.GetUShort (BootSector.BytesPerSector); sectorsPerCluster = bootSector.GetByte (BootSector.SectorsPerCluster); reservedSectors = bootSector.GetByte (BootSector.ReservedSectors); nbrFats = bootSector.GetByte (BootSector.FatAllocationTables); rootEntries = bootSector.GetUShort (BootSector.MaxRootDirEntries); uint sectorsPerFat16 = bootSector.GetUShort (BootSector.SectorsPerFAT); uint sectorsPerFat32 = bootSector.GetUInt (BootSector.FAT32_SectorPerFAT); uint totalSectors16 = bootSector.GetUShort (BootSector.TotalSectors); uint totalSectors32 = bootSector.GetUInt (BootSector.FAT32_TotalSectors); uint sectorsPerFat = (sectorsPerFat16 != 0) ? sectorsPerFat16 : sectorsPerFat32; uint fatSectors = nbrFats * sectorsPerFat; clusterSizeInBytes = sectorsPerCluster * blockSize; rootDirSectors = (((rootEntries * 32) + (bytesPerSector - 1)) / bytesPerSector); firstDataSector = reservedSectors + (nbrFats * sectorsPerFat) + rootDirSectors; if (totalSectors16 != 0) totalSectors = totalSectors16; else totalSectors = totalSectors32; dataSectors = totalSectors - (reservedSectors + (nbrFats * sectorsPerFat) + rootDirSectors); totalClusters = dataSectors / sectorsPerCluster; entriesPerSector = (bytesPerSector / 32); firstRootDirectorySector = reservedSectors + fatSectors; dataAreaStart = firstRootDirectorySector + rootDirSectors; fatStart = reservedSectors; if (totalClusters < 4085) fatType = FATType.FAT12; else if (totalClusters < 65525) fatType = FATType.FAT16; else fatType = FATType.FAT32; if (fatType == FATType.FAT12) { reserved = 0xFF0; last = 0x0FF8; bad = 0x0FF7; fatMask = 0xFFFFFFFF; fatEntries = sectorsPerFat * 3 * blockSize / 2; } else if (fatType == FATType.FAT16) { reserved = 0xFFF0; last = 0xFFF8; bad = 0xFFF7; fatMask = 0xFFFFFFFF; fatEntries = sectorsPerFat * blockSize / 2; } else { // if (type == FatType.FAT32) { reserved = 0xFFF0; last = 0x0FFFFFF8; bad = 0x0FFFFFF7; fatMask = 0x0FFFFFFF; fatEntries = sectorsPerFat * blockSize / 4; } // some basic checks if ((nbrFats == 0) || (nbrFats > 2)) valid = false; else if (totalSectors == 0) valid = false; else if (sectorsPerFat == 0) valid = false; else if (!((fatType == FATType.FAT12) || (fatType == FATType.FAT16))) // no support for Fat32 yet valid = false; else valid = true; if (valid) { base.volumeLabel = bootSector.GetString (fatType != FATType.FAT32 ? BootSector.VolumeLabel : BootSector.FAT32_VolumeLabel, 11); base.serialNbr = bootSector.GetBytes (fatType != FATType.FAT32 ? BootSector.IDSerialNumber : BootSector.FAT32_IDSerialNumber, 4); } return valid; }
public bool Compare (byte[] data, uint offset, FATType type) { BinaryFormat entry = new BinaryFormat (data); byte first = entry.GetByte (offset + Entry.DOSName); if (first == FileNameAttribute.LastEntry) return false; if ((first == FileNameAttribute.Deleted) | (first == FileNameAttribute.Dot)) return false; if (first == FileNameAttribute.Escape) return false; string entryname = FAT.ExtractFileName (data, offset); if (entryname == name) return true; return false; }
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; }
private bool ReadMasterBootBlock () { valid = false; if (diskDevice.BlockSize != 512) // only going to work with 512 sector sizes return false; BinaryFormat masterboot = new BinaryFormat (diskDevice.ReadBlock (0, 1)); ushort mbrsignature = masterboot.GetUShort (MasterBootRecord.MBRSignature); diskSignature = masterboot.GetUInt (MasterBootRecord.DiskSignature); valid = (mbrsignature == MasterBootConstants.MBRSignature); if (valid) { for (uint index = 0; index < MaxMBRPartitions; index++) { uint offset = MasterBootRecord.PrimaryPartitions + (index * 16); GenericPartition partition = new GenericPartition (index); partition.Bootable = masterboot.GetByte (offset + PartitionRecord.Status) == 0x80; partition.PartitionType = masterboot.GetByte (offset + PartitionRecord.PartitionType); partition.StartLBA = masterboot.GetUInt (offset + PartitionRecord.LBA); partition.TotalBlocks = masterboot.GetUInt (offset + PartitionRecord.Sectors); partitions[index] = partition; } //TODO: Extended Partitions } code = new byte[MasterBootConstants.CodeAreaSize]; for (uint index = 0; index < MasterBootConstants.CodeAreaSize; index++) code[index] = masterboot.GetByte (index); return valid; }
static public uint GetClusterEntry (byte[] data, uint index, FATType type) { BinaryFormat entry = new BinaryFormat (data); uint cluster = entry.GetUShort ((index * Entry.EntrySize) + Entry.FirstCluster); if (type == FATType.FAT32) { uint clusterhi = ((uint)entry.GetUShort ((index * Entry.EntrySize) + Entry.EAIndex)) << 16; cluster = cluster | clusterhi; } return cluster; }