Move common Apple boot block information and structs to its own class.

This commit is contained in:
2020-02-19 00:12:15 +00:00
parent 80a38c860d
commit 7ac5e40965
12 changed files with 309 additions and 228 deletions

View File

@@ -36,8 +36,6 @@ namespace DiscImageChef.Filesystems.AppleMFS
public partial class AppleMFS
{
const ushort MFS_MAGIC = 0xD2D7;
// "LK"
const ushort MFSBB_MAGIC = 0x4C4B;
const short DIRID_TRASH = -3;
const short DIRID_DESKTOP = -2;

View File

@@ -64,7 +64,6 @@ namespace DiscImageChef.Filesystems.AppleMFS
var sb = new StringBuilder();
var mdb = new MFS_MasterDirectoryBlock();
var bb = new MFS_BootBlock();
byte[] pString = new byte[16];
@@ -93,40 +92,6 @@ namespace DiscImageChef.Filesystems.AppleMFS
Array.Copy(mdbSector, 0x024, variableSize, 0, mdb.drVNSiz + 1);
mdb.drVN = StringHandlers.PascalToString(variableSize, Encoding);
bb.bbID = BigEndianBitConverter.ToUInt16(bbSector, 0x000);
if(bb.bbID == MFSBB_MAGIC)
{
bb.bbEntry = BigEndianBitConverter.ToUInt32(bbSector, 0x002);
bb.boot_flags = bbSector[0x006];
bb.bbVersion = bbSector[0x007];
bb.bbPageFlags = BigEndianBitConverter.ToInt16(bbSector, 0x008);
Array.Copy(mdbSector, 0x00A, pString, 0, 16);
bb.bbSysName = StringHandlers.PascalToString(pString, Encoding);
Array.Copy(mdbSector, 0x01A, pString, 0, 16);
bb.bbShellName = StringHandlers.PascalToString(pString, Encoding);
Array.Copy(mdbSector, 0x02A, pString, 0, 16);
bb.bbDbg1Name = StringHandlers.PascalToString(pString, Encoding);
Array.Copy(mdbSector, 0x03A, pString, 0, 16);
bb.bbDbg2Name = StringHandlers.PascalToString(pString, Encoding);
Array.Copy(mdbSector, 0x04A, pString, 0, 16);
bb.bbScreenName = StringHandlers.PascalToString(pString, Encoding);
Array.Copy(mdbSector, 0x05A, pString, 0, 16);
bb.bbHelloName = StringHandlers.PascalToString(pString, Encoding);
Array.Copy(mdbSector, 0x06A, pString, 0, 16);
bb.bbScrapName = StringHandlers.PascalToString(pString, Encoding);
bb.bbCntFCBs = BigEndianBitConverter.ToUInt16(bbSector, 0x07A);
bb.bbCntEvts = BigEndianBitConverter.ToUInt16(bbSector, 0x07C);
bb.bb128KSHeap = BigEndianBitConverter.ToUInt32(bbSector, 0x07E);
bb.bb256KSHeap = BigEndianBitConverter.ToUInt32(bbSector, 0x082);
bb.bbSysHeapSize = BigEndianBitConverter.ToUInt32(bbSector, 0x086);
}
else
bb.bbID = 0x0000;
sb.AppendLine("Apple Macintosh File System");
sb.AppendLine();
sb.AppendLine("Master Directory Block:");
@@ -150,37 +115,13 @@ namespace DiscImageChef.Filesystems.AppleMFS
sb.AppendFormat("{0} unused allocation blocks.", mdb.drFreeBks).AppendLine();
sb.AppendFormat("Volume name: {0}", mdb.drVN).AppendLine();
if(bb.bbID == MFSBB_MAGIC)
string bootBlockInfo = AppleCommon.GetBootBlockInformation(bbSector, Encoding);
if(bootBlockInfo != null)
{
sb.AppendLine("Volume is bootable.");
sb.AppendLine();
sb.AppendLine("Boot Block:");
if((bb.boot_flags & 0x40) == 0x40)
sb.AppendLine("Boot block should be executed.");
if((bb.boot_flags & 0x80) == 0x80)
sb.AppendLine("Boot block is in new unknown format.");
else
{
if(bb.bbPageFlags > 0)
sb.AppendLine("Allocate secondary sound buffer at boot.");
else if(bb.bbPageFlags < 0)
sb.AppendLine("Allocate secondary sound and video buffers at boot.");
sb.AppendFormat("System filename: {0}", bb.bbSysName).AppendLine();
sb.AppendFormat("Finder filename: {0}", bb.bbShellName).AppendLine();
sb.AppendFormat("Debugger filename: {0}", bb.bbDbg1Name).AppendLine();
sb.AppendFormat("Disassembler filename: {0}", bb.bbDbg2Name).AppendLine();
sb.AppendFormat("Startup screen filename: {0}", bb.bbScreenName).AppendLine();
sb.AppendFormat("First program to execute at boot: {0}", bb.bbHelloName).AppendLine();
sb.AppendFormat("Clipboard filename: {0}", bb.bbScrapName).AppendLine();
sb.AppendFormat("Maximum opened files: {0}", bb.bbCntFCBs * 4).AppendLine();
sb.AppendFormat("Event queue size: {0}", bb.bbCntEvts).AppendLine();
sb.AppendFormat("Heap size with 128KiB of RAM: {0} bytes", bb.bb128KSHeap).AppendLine();
sb.AppendFormat("Heap size with 256KiB of RAM: {0} bytes", bb.bb256KSHeap).AppendLine();
sb.AppendFormat("Heap size with 512KiB of RAM or more: {0} bytes", bb.bbSysHeapSize).AppendLine();
}
sb.AppendLine(bootBlockInfo);
}
else
sb.AppendLine("Volume is not bootable.");
@@ -195,7 +136,7 @@ namespace DiscImageChef.Filesystems.AppleMFS
XmlFsType.BackupDateSpecified = true;
}
XmlFsType.Bootable = bb.bbID == MFSBB_MAGIC;
XmlFsType.Bootable = bootBlockInfo != null;
XmlFsType.Clusters = mdb.drNmAlBlks;
XmlFsType.ClusterSize = mdb.drAlBlkSiz;

View File

@@ -76,45 +76,6 @@ namespace DiscImageChef.Filesystems.AppleMFS
public string drVN;
}
/// <summary>Should be at offset 0x0000 in volume, followed by boot code</summary>
struct MFS_BootBlock
{
/// <summary>0x000, Signature, 0x4C4B if bootable</summary>
public ushort bbID;
/// <summary>0x002, Branch</summary>
public uint bbEntry;
/// <summary>0x006, Boot block flags</summary>
public byte boot_flags;
/// <summary>0x007, Boot block version</summary>
public byte bbVersion;
/// <summary>0x008, Allocate secondary buffers</summary>
public short bbPageFlags;
/// <summary>0x00A, System file name (16 bytes)</summary>
public string bbSysName;
/// <summary>0x01A, Finder file name (16 bytes)</summary>
public string bbShellName;
/// <summary>0x02A, Debugger file name (16 bytes)</summary>
public string bbDbg1Name;
/// <summary>0x03A, Disassembler file name (16 bytes)</summary>
public string bbDbg2Name;
/// <summary>0x04A, Startup screen file name (16 bytes)</summary>
public string bbScreenName;
/// <summary>0x05A, First program to execute on boot (16 bytes)</summary>
public string bbHelloName;
/// <summary>0x06A, Clipboard file name (16 bytes)</summary>
public string bbScrapName;
/// <summary>0x07A, 1/4 of maximum opened at a time files</summary>
public ushort bbCntFCBs;
/// <summary>0x07C, Event queue size</summary>
public ushort bbCntEvts;
/// <summary>0x07E, Heap size on a Mac with 128KiB of RAM</summary>
public uint bb128KSHeap;
/// <summary>0x082, Heap size on a Mac with 256KiB of RAM</summary>
public uint bb256KSHeap;
/// <summary>0x086, Heap size on a Mac with 512KiB of RAM or more</summary>
public uint bbSysHeapSize;
}
[Flags]
enum MFS_FileFlags : byte
{

View File

@@ -44,21 +44,28 @@ namespace DiscImageChef.Filesystems.AppleMFS
// Information from Inside Macintosh Volume II
public partial class AppleMFS
{
public Errno Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
Dictionary<string, string> options, string @namespace)
public Errno Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
Dictionary<string, string> options, string @namespace)
{
device = imagePlugin;
partitionStart = partition.Start;
Encoding = encoding ?? Encoding.GetEncoding("macintosh");
if(options == null) options = GetDefaultOptions();
if(options.TryGetValue("debug", out string debugString)) bool.TryParse(debugString, out debug);
if(options == null)
options = GetDefaultOptions();
if(options.TryGetValue("debug", out string debugString))
bool.TryParse(debugString, out debug);
volMDB = new MFS_MasterDirectoryBlock();
mdbBlocks = device.ReadSector(2 + partitionStart);
bootBlocks = device.ReadSector(0 + partitionStart);
volMDB.drSigWord = BigEndianBitConverter.ToUInt16(mdbBlocks, 0x000);
if(volMDB.drSigWord != MFS_MAGIC) return Errno.InvalidArgument;
if(volMDB.drSigWord != MFS_MAGIC)
return Errno.InvalidArgument;
volMDB.drCrDate = BigEndianBitConverter.ToUInt32(mdbBlocks, 0x002);
volMDB.drLsBkUp = BigEndianBitConverter.ToUInt32(mdbBlocks, 0x006);
@@ -78,37 +85,58 @@ namespace DiscImageChef.Filesystems.AppleMFS
volMDB.drVN = StringHandlers.PascalToString(variableSize, Encoding);
directoryBlocks = device.ReadSectors(volMDB.drDirSt + partitionStart, volMDB.drBlLen);
int bytesInBlockMap = volMDB.drNmAlBlks * 12 / 8 + volMDB.drNmAlBlks * 12 % 8;
int bytesInBlockMap = ((volMDB.drNmAlBlks * 12) / 8) + ((volMDB.drNmAlBlks * 12) % 8);
const int BYTES_BEFORE_BLOCK_MAP = 64;
int bytesInWholeMdb = bytesInBlockMap + BYTES_BEFORE_BLOCK_MAP;
int sectorsInWholeMdb = bytesInWholeMdb / (int)device.Info.SectorSize +
bytesInWholeMdb % (int)device.Info.SectorSize;
int sectorsInWholeMdb = (bytesInWholeMdb / (int)device.Info.SectorSize) +
(bytesInWholeMdb % (int)device.Info.SectorSize);
byte[] wholeMdb = device.ReadSectors(partitionStart + 2, (uint)sectorsInWholeMdb);
blockMapBytes = new byte[bytesInBlockMap];
Array.Copy(wholeMdb, BYTES_BEFORE_BLOCK_MAP, blockMapBytes, 0, blockMapBytes.Length);
int offset = 0;
blockMap = new uint[volMDB.drNmAlBlks + 2 + 1];
for(int i = 2; i < volMDB.drNmAlBlks + 2; i += 8)
{
uint tmp1 = 0;
uint tmp2 = 0;
uint tmp3 = 0;
if(offset + 4 <= blockMapBytes.Length) tmp1 = BigEndianBitConverter.ToUInt32(blockMapBytes, offset);
if(offset + 4 <= blockMapBytes.Length)
tmp1 = BigEndianBitConverter.ToUInt32(blockMapBytes, offset);
if(offset + 4 + 4 <= blockMapBytes.Length)
tmp2 = BigEndianBitConverter.ToUInt32(blockMapBytes, offset + 4);
if(offset + 8 + 4 <= blockMapBytes.Length)
tmp3 = BigEndianBitConverter.ToUInt32(blockMapBytes, offset + 8);
if(i < blockMap.Length) blockMap[i] = (tmp1 & 0xFFF00000) >> 20;
if(i + 2 < blockMap.Length) blockMap[i + 1] = (tmp1 & 0xFFF00) >> 8;
if(i + 3 < blockMap.Length) blockMap[i + 2] = ((tmp1 & 0xFF) << 4) + ((tmp2 & 0xF0000000) >> 28);
if(i + 4 < blockMap.Length) blockMap[i + 3] = (tmp2 & 0xFFF0000) >> 16;
if(i + 5 < blockMap.Length) blockMap[i + 4] = (tmp2 & 0xFFF0) >> 4;
if(i + 6 < blockMap.Length) blockMap[i + 5] = ((tmp2 & 0xF) << 8) + ((tmp3 & 0xFF000000) >> 24);
if(i + 7 < blockMap.Length) blockMap[i + 6] = (tmp3 & 0xFFF000) >> 12;
if(i + 8 < blockMap.Length) blockMap[i + 7] = tmp3 & 0xFFF;
if(i < blockMap.Length)
blockMap[i] = (tmp1 & 0xFFF00000) >> 20;
if(i + 2 < blockMap.Length)
blockMap[i + 1] = (tmp1 & 0xFFF00) >> 8;
if(i + 3 < blockMap.Length)
blockMap[i + 2] = ((tmp1 & 0xFF) << 4) + ((tmp2 & 0xF0000000) >> 28);
if(i + 4 < blockMap.Length)
blockMap[i + 3] = (tmp2 & 0xFFF0000) >> 16;
if(i + 5 < blockMap.Length)
blockMap[i + 4] = (tmp2 & 0xFFF0) >> 4;
if(i + 6 < blockMap.Length)
blockMap[i + 5] = ((tmp2 & 0xF) << 8) + ((tmp3 & 0xFF000000) >> 24);
if(i + 7 < blockMap.Length)
blockMap[i + 6] = (tmp3 & 0xFFF000) >> 12;
if(i + 8 < blockMap.Length)
blockMap[i + 7] = tmp3 & 0xFFF;
offset += 12;
}
@@ -117,32 +145,38 @@ namespace DiscImageChef.Filesystems.AppleMFS
{
mdbTags = device.ReadSectorTag(2 + partitionStart, SectorTagType.AppleSectorTag);
bootTags = device.ReadSectorTag(0 + partitionStart, SectorTagType.AppleSectorTag);
directoryTags = device.ReadSectorsTag(volMDB.drDirSt + partitionStart, volMDB.drBlLen,
SectorTagType.AppleSectorTag);
bitmapTags = device.ReadSectorsTag(partitionStart + 2, (uint)sectorsInWholeMdb,
SectorTagType.AppleSectorTag);
}
sectorsPerBlock = (int)(volMDB.drAlBlkSiz / device.Info.SectorSize);
if(!FillDirectory()) return Errno.InvalidArgument;
if(!FillDirectory())
return Errno.InvalidArgument;
mounted = true;
ushort bbSig = BigEndianBitConverter.ToUInt16(bootBlocks, 0x000);
if(bbSig != MFSBB_MAGIC) bootBlocks = null;
if(bbSig != AppleCommon.BB_MAGIC)
bootBlocks = null;
XmlFsType = new FileSystemType();
if(volMDB.drLsBkUp > 0)
{
XmlFsType.BackupDate = DateHandlers.MacToDateTime(volMDB.drLsBkUp);
XmlFsType.BackupDateSpecified = true;
}
XmlFsType.Bootable = bbSig == MFSBB_MAGIC;
XmlFsType.Bootable = bbSig == AppleCommon.BB_MAGIC;
XmlFsType.Clusters = volMDB.drNmAlBlks;
XmlFsType.ClusterSize = volMDB.drAlBlkSiz;
if(volMDB.drCrDate > 0)
{
XmlFsType.CreationDate = DateHandlers.MacToDateTime(volMDB.drCrDate);
@@ -174,13 +208,11 @@ namespace DiscImageChef.Filesystems.AppleMFS
{
stat = new FileSystemInfo
{
Blocks = volMDB.drNmAlBlks,
FilenameLength = 255,
Files = volMDB.drNmFls,
FreeBlocks = volMDB.drFreeBks,
PluginId = Id,
Type = "Apple MFS"
Blocks = volMDB.drNmAlBlks, FilenameLength = 255, Files = volMDB.drNmFls,
FreeBlocks = volMDB.drFreeBks,
PluginId = Id, Type = "Apple MFS"
};
stat.FreeFiles = uint.MaxValue - stat.Files;
return Errno.NoError;