2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-08-01 00:41:02 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
2016-08-01 00:41:02 +01:00
|
|
|
//
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-12-31 23:08:23 +00:00
|
|
|
// Copyright © 2011-2021 Natalia Portillo
|
2016-08-01 00:41:02 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
2016-08-01 17:59:22 +01:00
|
|
|
using System;
|
2017-12-27 23:55:59 +00:00
|
|
|
using System.Collections.Generic;
|
2017-12-26 06:05:12 +00:00
|
|
|
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;
|
2016-08-01 17:59:22 +01:00
|
|
|
|
2020-07-20 15:43:52 +01:00
|
|
|
namespace Aaru.Filesystems
|
2016-08-01 00:41:02 +01:00
|
|
|
{
|
|
|
|
|
// Information from Inside Macintosh Volume II
|
2020-07-22 13:20:25 +01:00
|
|
|
public sealed partial class AppleMFS
|
2016-08-01 00:41:02 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
const int BYTES_BEFORE_BLOCK_MAP = 64;
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2021-09-16 04:42:14 +01:00
|
|
|
public ErrorNumber Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
|
|
|
|
|
Dictionary<string, string> options, string @namespace)
|
2016-08-01 00:41:02 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
_device = imagePlugin;
|
|
|
|
|
_partitionStart = partition.Start;
|
|
|
|
|
Encoding = encoding ?? Encoding.GetEncoding("macintosh");
|
2021-09-19 21:16:47 +01:00
|
|
|
ErrorNumber errno;
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-22 13:20:25 +01:00
|
|
|
options ??= GetDefaultOptions();
|
2020-02-19 00:12:15 +00:00
|
|
|
|
|
|
|
|
if(options.TryGetValue("debug", out string debugString))
|
2020-07-20 21:11:32 +01:00
|
|
|
bool.TryParse(debugString, out _debug);
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
_volMdb = new MasterDirectoryBlock();
|
2016-08-01 17:59:22 +01:00
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
errno = _device.ReadSector(2 + _partitionStart, out _mdbBlocks);
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
|
|
errno = _device.ReadSector(0 + _partitionStart, out _bootBlocks);
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
return errno;
|
2016-08-01 17:59:22 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
_volMdb.drSigWord = BigEndianBitConverter.ToUInt16(_mdbBlocks, 0x000);
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_volMdb.drSigWord != MFS_MAGIC)
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.InvalidArgument;
|
2016-08-01 17:59:22 +01:00
|
|
|
|
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);
|
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
errno = _device.ReadSectors(_volMdb.drDirSt + _partitionStart, _volMdb.drBlLen, out _directoryBlocks);
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
return errno;
|
|
|
|
|
|
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);
|
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
errno = _device.ReadSectors(_partitionStart + 2, (uint)sectorsInWholeMdb, out byte[] wholeMdb);
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
return errno;
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
_blockMapBytes = new byte[bytesInBlockMap];
|
|
|
|
|
Array.Copy(wholeMdb, BYTES_BEFORE_BLOCK_MAP, _blockMapBytes, 0, _blockMapBytes.Length);
|
2016-08-01 17:59:22 +01:00
|
|
|
|
|
|
|
|
int offset = 0;
|
2020-07-20 21:11:32 +01:00
|
|
|
_blockMap = new uint[_volMdb.drNmAlBlks + 2 + 1];
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
for(int i = 2; i < _volMdb.drNmAlBlks + 2; i += 8)
|
2016-08-01 17:59:22 +01:00
|
|
|
{
|
2016-08-01 18:52:34 +01:00
|
|
|
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-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(offset + 4 + 4 <= _blockMapBytes.Length)
|
|
|
|
|
tmp2 = BigEndianBitConverter.ToUInt32(_blockMapBytes, offset + 4);
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(offset + 8 + 4 <= _blockMapBytes.Length)
|
|
|
|
|
tmp3 = BigEndianBitConverter.ToUInt32(_blockMapBytes, offset + 8);
|
2016-08-01 17:59:22 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(i < _blockMap.Length)
|
|
|
|
|
_blockMap[i] = (tmp1 & 0xFFF00000) >> 20;
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(i + 2 < _blockMap.Length)
|
|
|
|
|
_blockMap[i + 1] = (tmp1 & 0xFFF00) >> 8;
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(i + 3 < _blockMap.Length)
|
|
|
|
|
_blockMap[i + 2] = ((tmp1 & 0xFF) << 4) + ((tmp2 & 0xF0000000) >> 28);
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(i + 4 < _blockMap.Length)
|
|
|
|
|
_blockMap[i + 3] = (tmp2 & 0xFFF0000) >> 16;
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(i + 5 < _blockMap.Length)
|
|
|
|
|
_blockMap[i + 4] = (tmp2 & 0xFFF0) >> 4;
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(i + 6 < _blockMap.Length)
|
|
|
|
|
_blockMap[i + 5] = ((tmp2 & 0xF) << 8) + ((tmp3 & 0xFF000000) >> 24);
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(i + 7 < _blockMap.Length)
|
|
|
|
|
_blockMap[i + 6] = (tmp3 & 0xFFF000) >> 12;
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(i + 8 < _blockMap.Length)
|
|
|
|
|
_blockMap[i + 7] = tmp3 & 0xFFF;
|
2016-08-01 17:59:22 +01:00
|
|
|
|
|
|
|
|
offset += 12;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_device.Info.ReadableSectorTags.Contains(SectorTagType.AppleSectorTag))
|
2016-08-01 17:59:22 +01:00
|
|
|
{
|
2021-09-20 20:52:18 +01:00
|
|
|
_device.ReadSectorTag(2 + _partitionStart, SectorTagType.AppleSectorTag, out _mdbTags);
|
|
|
|
|
_device.ReadSectorTag(0 + _partitionStart, SectorTagType.AppleSectorTag, out _bootTags);
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2021-09-20 20:52:18 +01:00
|
|
|
_device.ReadSectorsTag(_volMdb.drDirSt + _partitionStart, _volMdb.drBlLen, SectorTagType.AppleSectorTag,
|
|
|
|
|
out _directoryTags);
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2021-09-20 20:52:18 +01:00
|
|
|
_device.ReadSectorsTag(_partitionStart + 2, (uint)sectorsInWholeMdb, SectorTagType.AppleSectorTag,
|
|
|
|
|
out _bitmapTags);
|
2016-08-01 17:59:22 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
_sectorsPerBlock = (int)(_volMdb.drAlBlkSiz / _device.Info.SectorSize);
|
2016-08-01 17:59:22 +01:00
|
|
|
|
2020-02-19 00:12:15 +00:00
|
|
|
if(!FillDirectory())
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.InvalidArgument;
|
2016-08-01 17:59:22 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
_mounted = true;
|
2016-08-01 17:59:22 +01:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
ushort bbSig = BigEndianBitConverter.ToUInt16(_bootBlocks, 0x000);
|
2016-08-01 17:59:22 +01:00
|
|
|
|
2020-02-19 00:12:15 +00:00
|
|
|
if(bbSig != AppleCommon.BB_MAGIC)
|
2020-07-20 21:11:32 +01:00
|
|
|
_bootBlocks = null;
|
2016-08-01 17:59:22 +01:00
|
|
|
|
2017-12-26 08:01:40 +00:00
|
|
|
XmlFsType = new FileSystemType();
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_volMdb.drLsBkUp > 0)
|
2016-08-01 17:59:22 +01:00
|
|
|
{
|
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;
|
2016-08-01 17:59:22 +01:00
|
|
|
}
|
2017-12-27 23:55:59 +00:00
|
|
|
|
2020-02-19 00:12:15 +00:00
|
|
|
XmlFsType.Bootable = bbSig == AppleCommon.BB_MAGIC;
|
2020-07-20 21:11:32 +01:00
|
|
|
XmlFsType.Clusters = _volMdb.drNmAlBlks;
|
|
|
|
|
XmlFsType.ClusterSize = _volMdb.drAlBlkSiz;
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_volMdb.drCrDate > 0)
|
2016-08-01 17:59:22 +01:00
|
|
|
{
|
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;
|
2016-08-01 17:59:22 +01:00
|
|
|
}
|
2017-12-27 23:55:59 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
XmlFsType.Files = _volMdb.drNmFls;
|
2017-12-27 23:55:59 +00:00
|
|
|
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;
|
2017-12-27 23:55:59 +00:00
|
|
|
XmlFsType.Type = "MFS";
|
2020-07-20 21:11:32 +01:00
|
|
|
XmlFsType.VolumeName = _volMdb.drVN;
|
2016-08-01 17:59:22 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NoError;
|
2016-08-01 00:41:02 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2021-09-16 04:42:14 +01:00
|
|
|
public ErrorNumber Unmount()
|
2016-08-01 00:41:02 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
_mounted = false;
|
|
|
|
|
_idToFilename = null;
|
|
|
|
|
_idToEntry = null;
|
|
|
|
|
_filenameToId = null;
|
|
|
|
|
_bootBlocks = null;
|
2016-08-01 17:59:22 +01:00
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NoError;
|
2016-08-01 00:41:02 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2021-09-16 04:42:14 +01:00
|
|
|
public ErrorNumber StatFs(out FileSystemInfo stat)
|
2016-08-01 00:41:02 +01:00
|
|
|
{
|
2017-12-22 08:43:22 +00:00
|
|
|
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"
|
2017-12-22 08:43:22 +00:00
|
|
|
};
|
2020-02-19 00:12:15 +00:00
|
|
|
|
2016-08-01 17:59:22 +01:00
|
|
|
stat.FreeFiles = uint.MaxValue - stat.Files;
|
|
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
return ErrorNumber.NoError;
|
2016-08-01 00:41:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|