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

219 lines
8.6 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.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2024-05-01 04:17:32 +01:00
// Copyright © 2011-2024 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using Aaru.CommonTypes.AaruMetadata;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
using Partition = Aaru.CommonTypes.Partition;
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
// Information from Inside Macintosh Volume II
public sealed partial class AppleMFS
{
2022-03-06 13:29:38 +00:00
const int BYTES_BEFORE_BLOCK_MAP = 64;
#region IReadOnlyFilesystem Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
Dictionary<string, string> options, string @namespace)
{
2022-03-06 13:29:38 +00:00
_device = imagePlugin;
_partitionStart = partition.Start;
_encoding = encoding ?? Encoding.GetEncoding("macintosh");
2020-07-20 21:11:32 +01:00
2022-03-06 13:29:38 +00:00
options ??= GetDefaultOptions();
2024-05-01 04:05:22 +01:00
if(options.TryGetValue("debug", out string debugString)) bool.TryParse(debugString, out _debug);
2022-03-06 13:29:38 +00:00
_volMdb = new MasterDirectoryBlock();
ErrorNumber errno = _device.ReadSector(2 + _partitionStart, out _mdbBlocks);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
2022-03-06 13:29:38 +00:00
errno = _device.ReadSector(0 + _partitionStart, out _bootBlocks);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
2022-03-06 13:29:38 +00:00
_volMdb.drSigWord = BigEndianBitConverter.ToUInt16(_mdbBlocks, 0x000);
2024-05-01 04:05:22 +01:00
if(_volMdb.drSigWord != MFS_MAGIC) return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00: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];
var variableSize = new byte[_volMdb.drVNSiz + 1];
2022-03-06 13:29:38 +00:00
Array.Copy(_mdbBlocks, 0x024, variableSize, 0, _volMdb.drVNSiz + 1);
_volMdb.drVN = StringHandlers.PascalToString(variableSize, _encoding);
2022-03-06 13:29:38 +00:00
errno = _device.ReadSectors(_volMdb.drDirSt + _partitionStart, _volMdb.drBlLen, out _directoryBlocks);
2020-07-20 21:11:32 +01:00
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
int bytesInBlockMap = _volMdb.drNmAlBlks * 12 / 8 + _volMdb.drNmAlBlks * 12 % 8;
int bytesInWholeMdb = bytesInBlockMap + BYTES_BEFORE_BLOCK_MAP;
int sectorsInWholeMdb = bytesInWholeMdb / (int)_device.Info.SectorSize +
bytesInWholeMdb % (int)_device.Info.SectorSize;
2020-07-20 21:11:32 +01:00
2022-03-06 13:29:38 +00:00
errno = _device.ReadSectors(_partitionStart + 2, (uint)sectorsInWholeMdb, out byte[] wholeMdb);
2020-07-20 21:11:32 +01:00
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
2022-03-06 13:29:38 +00:00
_blockMapBytes = new byte[bytesInBlockMap];
Array.Copy(wholeMdb, BYTES_BEFORE_BLOCK_MAP, _blockMapBytes, 0, _blockMapBytes.Length);
var offset = 0;
2022-03-06 13:29:38 +00:00
_blockMap = new uint[_volMdb.drNmAlBlks + 2 + 1];
for(var i = 2; i < _volMdb.drNmAlBlks + 2; i += 8)
2022-03-06 13:29:38 +00:00
{
uint tmp1 = 0;
uint tmp2 = 0;
uint tmp3 = 0;
2024-05-01 04:05:22 +01:00
if(offset + 4 <= _blockMapBytes.Length) tmp1 = BigEndianBitConverter.ToUInt32(_blockMapBytes, offset);
2022-03-06 13:29:38 +00:00
if(offset + 4 + 4 <= _blockMapBytes.Length)
tmp2 = BigEndianBitConverter.ToUInt32(_blockMapBytes, offset + 4);
2022-03-06 13:29:38 +00:00
if(offset + 8 + 4 <= _blockMapBytes.Length)
tmp3 = BigEndianBitConverter.ToUInt32(_blockMapBytes, offset + 8);
2024-05-01 04:05:22 +01:00
if(i < _blockMap.Length) _blockMap[i] = (tmp1 & 0xFFF00000) >> 20;
2024-05-01 04:05:22 +01:00
if(i + 2 < _blockMap.Length) _blockMap[i + 1] = (tmp1 & 0xFFF00) >> 8;
2024-05-01 04:05:22 +01:00
if(i + 3 < _blockMap.Length) _blockMap[i + 2] = ((tmp1 & 0xFF) << 4) + ((tmp2 & 0xF0000000) >> 28);
2024-05-01 04:05:22 +01:00
if(i + 4 < _blockMap.Length) _blockMap[i + 3] = (tmp2 & 0xFFF0000) >> 16;
2024-05-01 04:05:22 +01:00
if(i + 5 < _blockMap.Length) _blockMap[i + 4] = (tmp2 & 0xFFF0) >> 4;
2024-05-01 04:05:22 +01:00
if(i + 6 < _blockMap.Length) _blockMap[i + 5] = ((tmp2 & 0xF) << 8) + ((tmp3 & 0xFF000000) >> 24);
2024-05-01 04:05:22 +01:00
if(i + 7 < _blockMap.Length) _blockMap[i + 6] = (tmp3 & 0xFFF000) >> 12;
2024-05-01 04:05:22 +01:00
if(i + 8 < _blockMap.Length) _blockMap[i + 7] = tmp3 & 0xFFF;
2022-03-06 13:29:38 +00:00
offset += 12;
}
2022-03-06 13:29:38 +00:00
if(_device.Info.ReadableSectorTags.Contains(SectorTagType.AppleSectorTag))
{
_device.ReadSectorTag(2 + _partitionStart, SectorTagType.AppleSectorTag, out _mdbTags);
_device.ReadSectorTag(0 + _partitionStart, SectorTagType.AppleSectorTag, out _bootTags);
2024-05-01 04:05:22 +01:00
_device.ReadSectorsTag(_volMdb.drDirSt + _partitionStart,
_volMdb.drBlLen,
SectorTagType.AppleSectorTag,
2022-03-06 13:29:38 +00:00
out _directoryTags);
2024-05-01 04:05:22 +01:00
_device.ReadSectorsTag(_partitionStart + 2,
(uint)sectorsInWholeMdb,
SectorTagType.AppleSectorTag,
2022-03-06 13:29:38 +00:00
out _bitmapTags);
}
2022-03-06 13:29:38 +00:00
_sectorsPerBlock = (int)(_volMdb.drAlBlkSiz / _device.Info.SectorSize);
2024-05-01 04:05:22 +01:00
if(!FillDirectory()) return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
_mounted = true;
var bbSig = BigEndianBitConverter.ToUInt16(_bootBlocks, 0x000);
2024-05-01 04:05:22 +01:00
if(bbSig != AppleCommon.BB_MAGIC) _bootBlocks = null;
Metadata = new FileSystem();
2024-05-01 04:05:22 +01:00
if(_volMdb.drLsBkUp > 0) Metadata.BackupDate = DateHandlers.MacToDateTime(_volMdb.drLsBkUp);
Metadata.Bootable = bbSig == AppleCommon.BB_MAGIC;
Metadata.Clusters = _volMdb.drNmAlBlks;
Metadata.ClusterSize = _volMdb.drAlBlkSiz;
2024-05-01 04:05:22 +01:00
if(_volMdb.drCrDate > 0) Metadata.CreationDate = DateHandlers.MacToDateTime(_volMdb.drCrDate);
Metadata.Files = _volMdb.drNmFls;
Metadata.FreeClusters = _volMdb.drFreeBks;
Metadata.Type = FS_TYPE;
Metadata.VolumeName = _volMdb.drVN;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Unmount()
{
_mounted = false;
_idToFilename = null;
_idToEntry = null;
_filenameToId = null;
_bootBlocks = null;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber StatFs(out FileSystemInfo stat)
{
stat = new FileSystemInfo
{
2022-03-06 13:29:38 +00:00
Blocks = _volMdb.drNmAlBlks,
FilenameLength = 255,
Files = _volMdb.drNmFls,
FreeBlocks = _volMdb.drFreeBks,
PluginId = Id,
Type = FS_TYPE
2022-03-06 13:29:38 +00:00
};
2022-03-06 13:29:38 +00:00
stat.FreeFiles = uint.MaxValue - stat.Files;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
#endregion
2017-12-19 20:33:03 +00:00
}