Files
Aaru/Aaru.Filesystems/AppleMFS/Super.cs

228 lines
9.2 KiB
C#
Raw Normal View History

2017-05-19 20:28:49 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Super.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Apple Macintosh File System plugin.
//
// --[ Description ] ----------------------------------------------------------
//
2016-08-01 00:43:31 +01:00
// Handles mounting and umounting the Apple Macintosh File System.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2025-08-14 02:49:52 +01:00
// Copyright © 2011-2025 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
2017-12-21 14:30:38 +00:00
using Schemas;
2020-07-20 15:43:52 +01:00
namespace Aaru.Filesystems
{
// Information from Inside Macintosh Volume II
2020-07-22 13:20:25 +01:00
public sealed partial class AppleMFS
{
2020-07-20 21:11:32 +01:00
const int BYTES_BEFORE_BLOCK_MAP = 64;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public Errno Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
Dictionary<string, string> options, string @namespace)
{
2020-07-20 21:11:32 +01:00
_device = imagePlugin;
_partitionStart = partition.Start;
Encoding = encoding ?? Encoding.GetEncoding("macintosh");
2020-07-22 13:20:25 +01:00
options ??= GetDefaultOptions();
if(options.TryGetValue("debug", out string debugString))
2020-07-20 21:11:32 +01:00
bool.TryParse(debugString, out _debug);
2020-07-20 21:11:32 +01:00
_volMdb = new MasterDirectoryBlock();
2020-07-20 21:11:32 +01:00
_mdbBlocks = _device.ReadSector(2 + _partitionStart);
_bootBlocks = _device.ReadSector(0 + _partitionStart);
2020-07-20 21:11:32 +01:00
_volMdb.drSigWord = BigEndianBitConverter.ToUInt16(_mdbBlocks, 0x000);
2020-07-20 21:11:32 +01:00
if(_volMdb.drSigWord != MFS_MAGIC)
return Errno.InvalidArgument;
2020-07-20 21:11:32 +01:00
_volMdb.drCrDate = BigEndianBitConverter.ToUInt32(_mdbBlocks, 0x002);
_volMdb.drLsBkUp = BigEndianBitConverter.ToUInt32(_mdbBlocks, 0x006);
_volMdb.drAtrb = (AppleCommon.VolumeAttributes)BigEndianBitConverter.ToUInt16(_mdbBlocks, 0x00A);
_volMdb.drNmFls = BigEndianBitConverter.ToUInt16(_mdbBlocks, 0x00C);
_volMdb.drDirSt = BigEndianBitConverter.ToUInt16(_mdbBlocks, 0x00E);
_volMdb.drBlLen = BigEndianBitConverter.ToUInt16(_mdbBlocks, 0x010);
_volMdb.drNmAlBlks = BigEndianBitConverter.ToUInt16(_mdbBlocks, 0x012);
_volMdb.drAlBlkSiz = BigEndianBitConverter.ToUInt32(_mdbBlocks, 0x014);
_volMdb.drClpSiz = BigEndianBitConverter.ToUInt32(_mdbBlocks, 0x018);
_volMdb.drAlBlSt = BigEndianBitConverter.ToUInt16(_mdbBlocks, 0x01C);
_volMdb.drNxtFNum = BigEndianBitConverter.ToUInt32(_mdbBlocks, 0x01E);
_volMdb.drFreeBks = BigEndianBitConverter.ToUInt16(_mdbBlocks, 0x022);
_volMdb.drVNSiz = _mdbBlocks[0x024];
byte[] variableSize = new byte[_volMdb.drVNSiz + 1];
Array.Copy(_mdbBlocks, 0x024, variableSize, 0, _volMdb.drVNSiz + 1);
_volMdb.drVN = StringHandlers.PascalToString(variableSize, Encoding);
_directoryBlocks = _device.ReadSectors(_volMdb.drDirSt + _partitionStart, _volMdb.drBlLen);
2021-08-17 14:27:19 +01:00
int bytesInBlockMap = (_volMdb.drNmAlBlks * 12 / 8) + (_volMdb.drNmAlBlks * 12 % 8);
int bytesInWholeMdb = bytesInBlockMap + BYTES_BEFORE_BLOCK_MAP;
2020-07-20 21:11:32 +01:00
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;
2020-07-20 21:11:32 +01:00
_blockMap = new uint[_volMdb.drNmAlBlks + 2 + 1];
2020-07-20 21:11:32 +01:00
for(int i = 2; i < _volMdb.drNmAlBlks + 2; i += 8)
{
uint tmp1 = 0;
uint tmp2 = 0;
uint tmp3 = 0;
2020-07-20 21:11:32 +01:00
if(offset + 4 <= _blockMapBytes.Length)
tmp1 = BigEndianBitConverter.ToUInt32(_blockMapBytes, offset);
2020-07-20 21:11:32 +01:00
if(offset + 4 + 4 <= _blockMapBytes.Length)
tmp2 = BigEndianBitConverter.ToUInt32(_blockMapBytes, offset + 4);
2020-07-20 21:11:32 +01:00
if(offset + 8 + 4 <= _blockMapBytes.Length)
tmp3 = BigEndianBitConverter.ToUInt32(_blockMapBytes, offset + 8);
2020-07-20 21:11:32 +01:00
if(i < _blockMap.Length)
_blockMap[i] = (tmp1 & 0xFFF00000) >> 20;
2020-07-20 21:11:32 +01:00
if(i + 2 < _blockMap.Length)
_blockMap[i + 1] = (tmp1 & 0xFFF00) >> 8;
2020-07-20 21:11:32 +01:00
if(i + 3 < _blockMap.Length)
_blockMap[i + 2] = ((tmp1 & 0xFF) << 4) + ((tmp2 & 0xF0000000) >> 28);
2020-07-20 21:11:32 +01:00
if(i + 4 < _blockMap.Length)
_blockMap[i + 3] = (tmp2 & 0xFFF0000) >> 16;
2020-07-20 21:11:32 +01:00
if(i + 5 < _blockMap.Length)
_blockMap[i + 4] = (tmp2 & 0xFFF0) >> 4;
2020-07-20 21:11:32 +01:00
if(i + 6 < _blockMap.Length)
_blockMap[i + 5] = ((tmp2 & 0xF) << 8) + ((tmp3 & 0xFF000000) >> 24);
2020-07-20 21:11:32 +01:00
if(i + 7 < _blockMap.Length)
_blockMap[i + 6] = (tmp3 & 0xFFF000) >> 12;
2020-07-20 21:11:32 +01:00
if(i + 8 < _blockMap.Length)
_blockMap[i + 7] = tmp3 & 0xFFF;
offset += 12;
}
2020-07-20 21:11:32 +01:00
if(_device.Info.ReadableSectorTags.Contains(SectorTagType.AppleSectorTag))
{
2020-07-20 21:11:32 +01:00
_mdbTags = _device.ReadSectorTag(2 + _partitionStart, SectorTagType.AppleSectorTag);
_bootTags = _device.ReadSectorTag(0 + _partitionStart, SectorTagType.AppleSectorTag);
2020-07-20 21:11:32 +01:00
_directoryTags = _device.ReadSectorsTag(_volMdb.drDirSt + _partitionStart, _volMdb.drBlLen,
SectorTagType.AppleSectorTag);
2020-07-20 21:11:32 +01:00
_bitmapTags = _device.ReadSectorsTag(_partitionStart + 2, (uint)sectorsInWholeMdb,
SectorTagType.AppleSectorTag);
}
2020-07-20 21:11:32 +01:00
_sectorsPerBlock = (int)(_volMdb.drAlBlkSiz / _device.Info.SectorSize);
if(!FillDirectory())
return Errno.InvalidArgument;
2020-07-20 21:11:32 +01:00
_mounted = true;
2020-07-20 21:11:32 +01:00
ushort bbSig = BigEndianBitConverter.ToUInt16(_bootBlocks, 0x000);
if(bbSig != AppleCommon.BB_MAGIC)
2020-07-20 21:11:32 +01:00
_bootBlocks = null;
2017-12-26 08:01:40 +00:00
XmlFsType = new FileSystemType();
2020-07-20 21:11:32 +01:00
if(_volMdb.drLsBkUp > 0)
{
2020-07-20 21:11:32 +01:00
XmlFsType.BackupDate = DateHandlers.MacToDateTime(_volMdb.drLsBkUp);
2017-12-26 08:01:40 +00:00
XmlFsType.BackupDateSpecified = true;
}
XmlFsType.Bootable = bbSig == AppleCommon.BB_MAGIC;
2020-07-20 21:11:32 +01:00
XmlFsType.Clusters = _volMdb.drNmAlBlks;
XmlFsType.ClusterSize = _volMdb.drAlBlkSiz;
2020-07-20 21:11:32 +01:00
if(_volMdb.drCrDate > 0)
{
2020-07-20 21:11:32 +01:00
XmlFsType.CreationDate = DateHandlers.MacToDateTime(_volMdb.drCrDate);
2017-12-26 08:01:40 +00:00
XmlFsType.CreationDateSpecified = true;
}
2020-07-20 21:11:32 +01:00
XmlFsType.Files = _volMdb.drNmFls;
XmlFsType.FilesSpecified = true;
2020-07-20 21:11:32 +01:00
XmlFsType.FreeClusters = _volMdb.drFreeBks;
2017-12-26 08:01:40 +00:00
XmlFsType.FreeClustersSpecified = true;
XmlFsType.Type = "MFS";
2020-07-20 21:11:32 +01:00
XmlFsType.VolumeName = _volMdb.drVN;
return Errno.NoError;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public Errno Unmount()
{
2020-07-20 21:11:32 +01:00
_mounted = false;
_idToFilename = null;
_idToEntry = null;
_filenameToId = null;
_bootBlocks = null;
return Errno.NoError;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public Errno StatFs(out FileSystemInfo stat)
{
stat = new FileSystemInfo
{
2020-07-20 21:11:32 +01:00
Blocks = _volMdb.drNmAlBlks,
2020-07-20 04:34:16 +01:00
FilenameLength = 255,
2020-07-20 21:11:32 +01:00
Files = _volMdb.drNmFls,
FreeBlocks = _volMdb.drFreeBks,
2020-07-20 04:34:16 +01:00
PluginId = Id,
Type = "Apple MFS"
};
stat.FreeFiles = uint.MaxValue - stat.Files;
return Errno.NoError;
}
}
2017-12-19 20:33:03 +00:00
}