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 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 FATFormatInfo(FATType type, int size_mbytes, string volumeLabel, int spt, int heads) { Type = type; VolumeLabel = volumeLabel; SectorsPerTrack = (ushort)spt; Heads = (ushort)heads; Cylinders = (uint)((ulong)size_mbytes * 0x100000 / (ulong)(spt * heads * 512)); TotalSectors = (uint)(Cylinders * heads * spt); }
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); }
public static bool ValidFatType(FATType type, uint totalsectors) { if (totalsectors <= 8400) { return(type == FATType.FAT12); // Only FAT12 can go this low } if (totalsectors <= 66600) { return(type == FATType.FAT16); // between 8400 and 66600 is exclusive to FAT16 } if (totalsectors > 4194304) { return(type == FATType.FAT32); // above this is exclusive to fat32 } return(type != FATType.FAT12); // between 66600 and 4194304 is common to FAT16 or FAT32. }
public static byte GetSectorsPerClusterByTotalSectors (FATType type, uint sectors) { switch (type) { case FATType.FAT12: { if (sectors < 512) return 1; else if (sectors == 720) return 2; else if (sectors == 1440) return 2; else if (sectors <= 2880) return 1; else if (sectors <= 5760) return 2; else if (sectors <= 16384) return 4; else if (sectors <= 32768) return 8; else return 0; } case FATType.FAT16: { if (sectors < 8400) return 0; else if (sectors < 32680) return 2; else if (sectors < 262144) return 4; else if (sectors < 524288) return 8; else if (sectors < 1048576) return 16; else if (sectors < 2097152) return 32; else if (sectors < 4194304) return 64; else return 0; } case FATType.FAT32: { if (sectors < 66600) return 0; else if (sectors < 532480) return 1; else if (sectors < 16777216) return 8; else if (sectors < 33554432) return 16; else if (sectors < 67108864) return 32; else return 64; } default: return 0; } }
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 static void CreateDiskImage(string filename, FATType type, int size_mbytes, string volumeLabel, int spt, int heads) { FATFormatInfo fi = new FATFormatInfo(type, size_mbytes, volumeLabel, spt, heads); CreateDiskImage(filename, fi); }
public FATSettings() { this.FatType = FATType.FAT16; // default this.VolumeLabel = string.Empty; this.SerialID = new byte[0]; }
public static byte GetSectorsPerClusterByTotalSectors(FATType type, uint sectors) { switch (type) { case FATType.FAT12: { if (sectors < 512) { return(1); } else if (sectors == 720) { return(2); } else if (sectors == 1440) { return(2); } else if (sectors <= 2880) { return(1); } else if (sectors <= 5760) { return(2); } else if (sectors <= 16384) { return(4); } else if (sectors <= 32768) { return(8); } else { return(0); } } case FATType.FAT16: { if (sectors < 8400) { return(0); } else if (sectors < 32680) { return(2); } else if (sectors < 262144) { return(4); } else if (sectors < 524288) { return(8); } else if (sectors < 1048576) { return(16); } else if (sectors < 2097152) { return(32); } else if (sectors < 4194304) { return(64); } else { return(0); } } case FATType.FAT32: { if (sectors < 66600) { return(0); } else if (sectors < 532480) { return(1); } else if (sectors < 16777216) { return(8); } else if (sectors < 33554432) { return(16); } else if (sectors < 67108864) { return(32); } else { return(64); } } default: return(0); } }
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); }
static int Main(string[] args) { if (args[0] == "--help") { ShowHelp(); #if DEBUG Console.Write("Press any key to continue..."); Console.ReadKey(true); Console.WriteLine(); #endif return(0); } string last, image, boot, device, spt, heads, media, type, lab, megs; last = image = boot = device = spt = heads = media = type = lab = megs = null; for (int i = 0; i < args.Length; i++) { if (!isopt(args[i])) { if (last == "-i") { image = args[i]; } else if (last == "-b") { boot = args[i]; } else if (last == "-d") { device = args[i]; } else if (last == "-s") { spt = args[i]; } else if (last == "-h") { heads = args[i]; } else if (last == "-m") { media = args[i]; } else if (last == "-t") { type = args[i]; } else if (last == "-v") { lab = args[i]; } else if (last == "-M") { megs = args[i]; } else { System.Console.Error.WriteLine("Invalid option"); return(1); } } last = args[i]; } if (isopt(last)) { Console.Error.WriteLine("Missing parameter after {0}", last); return(1); } if (string.IsNullOrEmpty(image) || string.IsNullOrEmpty(megs) || (!string.IsNullOrEmpty(boot) && string.IsNullOrEmpty(type)) || (!string.IsNullOrEmpty(spt) ^ !string.IsNullOrEmpty(heads)) ) { Console.Error.WriteLine("Missing parameter"); return(1); } try { FATType t = FATType.None; if (type != null) { if (type.ToUpper() == "FAT32") { t = FATType.FAT32; } else if (type.ToUpper() == "FAT16") { t = FATType.FAT16; } else { throw new ArgumentOutOfRangeException(); } } int m = int.Parse(megs); if (m == 0) { throw new Exception("Invalid disk size"); } FATFormatInfo fi = new FATFormatInfo( t, m, lab, string.IsNullOrEmpty(spt) ? 63 : int.Parse(spt), string.IsNullOrEmpty(heads) ? 16 : int.Parse(heads) ); if (!string.IsNullOrEmpty(boot)) { fi.BootSector = System.IO.File.ReadAllBytes(boot); } if (!string.IsNullOrEmpty(device)) { fi.DriveNumber = byte.Parse(device, System.Globalization.NumberStyles.AllowHexSpecifier); } if (!string.IsNullOrEmpty(media)) { fi.MediaType = byte.Parse(media, System.Globalization.NumberStyles.AllowHexSpecifier); } if (FATFormatter.DefaultFatType(fi.TotalSectors) == FATType.FAT12) { throw new Exception("Too few sectors"); } if (t == FATType.None) { t = FATFormatter.DefaultFatType(fi.TotalSectors); } if (!FATFormatter.ValidFatType(t, fi.TotalSectors)) { throw new Exception(string.Format("Cannot create a {0} image that is {1} megabytes", t, m)); } Console.WriteLine("{0}\tHeads", fi.Heads); Console.WriteLine("{0}\tSectors per track", fi.SectorsPerTrack); Console.WriteLine("{0}\tCylinders", fi.Cylinders); Console.WriteLine("{0} Total sectors", fi.TotalSectors); Console.WriteLine("Formatting as {0}...", t); FATFormatter.CreateDiskImage(image, fi); Console.WriteLine("Format complete."); } catch (Exception e) { System.Console.Error.WriteLine("vmkimg: error: {0}", e.Message); return(1); } return(0); }
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; }
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 FATSettings () { this.FatType = FATType.FAT16; // default this.VolumeLabel = string.Empty; this.SerialID = new byte[0]; }