2018-07-23 23:25:43 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : Read.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
|
|
|
|
|
// Component : Disk image plugins.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Reads MaxiDisk disk images.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ 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
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System.IO;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes;
|
|
|
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
|
using Aaru.Helpers;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2023-10-06 01:16:28 +01:00
|
|
|
|
namespace Aaru.Images;
|
2022-11-15 15:58:43 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
public sealed partial class MaxiDisk
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2023-10-03 23:34:59 +01:00
|
|
|
|
#region IWritableImage Members
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public ErrorNumber Open(IFilter imageFilter)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(stream.Length < 8) return ErrorNumber.InvalidArgument;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
|
var buffer = new byte[8];
|
2022-03-06 13:29:38 +00:00
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2022-11-14 09:43:16 +00:00
|
|
|
|
stream.EnsureRead(buffer, 0, buffer.Length);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
Header tmpHeader = Marshal.ByteArrayToStructureLittleEndian<Header>(buffer);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// This is hardcoded
|
|
|
|
|
|
// But its possible values are unknown...
|
|
|
|
|
|
//if(tmp_header.diskType > 11)
|
|
|
|
|
|
// return false;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// Only floppies supported
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(tmpHeader.heads is 0 or > 2) return ErrorNumber.InvalidArgument;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// No floppies with more than this?
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(tmpHeader.cylinders > 90) return ErrorNumber.InvalidArgument;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// Maximum supported bps is 16384
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(tmpHeader.bytesPerSector > 7) return ErrorNumber.InvalidArgument;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
|
int expectedFileSize = tmpHeader.heads *
|
|
|
|
|
|
tmpHeader.cylinders *
|
|
|
|
|
|
tmpHeader.sectorsPerTrack *
|
|
|
|
|
|
(128 << tmpHeader.bytesPerSector) +
|
|
|
|
|
|
8;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(expectedFileSize != stream.Length) return ErrorNumber.InvalidArgument;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_imageInfo.Cylinders = tmpHeader.cylinders;
|
|
|
|
|
|
_imageInfo.Heads = tmpHeader.heads;
|
|
|
|
|
|
_imageInfo.SectorsPerTrack = tmpHeader.sectorsPerTrack;
|
|
|
|
|
|
_imageInfo.Sectors = (ulong)(tmpHeader.heads * tmpHeader.cylinders * tmpHeader.sectorsPerTrack);
|
|
|
|
|
|
_imageInfo.SectorSize = (uint)(128 << tmpHeader.bytesPerSector);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_hdkImageFilter = imageFilter;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_imageInfo.ImageSize = (ulong)(stream.Length - 8);
|
|
|
|
|
|
_imageInfo.CreationTime = imageFilter.CreationTime;
|
|
|
|
|
|
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_imageInfo.MediaType = Geometry.GetMediaType(((ushort)_imageInfo.Cylinders, (byte)_imageInfo.Heads,
|
|
|
|
|
|
(ushort)_imageInfo.SectorsPerTrack, _imageInfo.SectorSize,
|
|
|
|
|
|
MediaEncoding.MFM, false));
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-12-15 22:21:07 +00:00
|
|
|
|
_imageInfo.MetadataMediaType = MetadataMediaType.BlockMedia;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-03-07 07:36:44 +00:00
|
|
|
|
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer) => ReadSectors(sectorAddress, 1, out buffer);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
buffer = null;
|
2021-09-19 21:16:47 +01:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1) return ErrorNumber.OutOfRange;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(sectorAddress + length > _imageInfo.Sectors) return ErrorNumber.OutOfRange;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
buffer = new byte[length * _imageInfo.SectorSize];
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
Stream stream = _hdkImageFilter.GetDataForkStream();
|
2023-10-03 23:34:59 +01:00
|
|
|
|
stream.Seek((long)(8 + sectorAddress * _imageInfo.SectorSize), SeekOrigin.Begin);
|
2022-11-14 09:43:16 +00:00
|
|
|
|
stream.EnsureRead(buffer, 0, (int)(length * _imageInfo.SectorSize));
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return ErrorNumber.NoError;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
2023-10-03 23:34:59 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|