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 HD-Copy 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
// Copyright © 2017 Michael Drüing
|
2020-12-31 23:08:23 +00:00
|
|
|
|
// Copyright © 2011-2021 Natalia Portillo
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes;
|
|
|
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
|
using Aaru.Console;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
|
namespace Aaru.DiscImages
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2020-07-22 13:20:25 +01:00
|
|
|
|
public sealed partial class HdCopy
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-16 19:10:39 +01:00
|
|
|
|
public ErrorNumber Open(IFilter imageFilter)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
|
|
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
|
|
|
|
|
|
2021-08-17 21:23:10 +01:00
|
|
|
|
var fheader = new FileHeader();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-02-28 19:06:32 +01:00
|
|
|
|
// the start offset of the track data
|
|
|
|
|
|
long currentOffset = 0;
|
|
|
|
|
|
|
2021-08-17 21:23:10 +01:00
|
|
|
|
if(!TryReadHeader(stream, ref fheader, ref currentOffset))
|
2021-09-16 19:10:39 +01:00
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("HDCP plugin",
|
2020-02-29 18:03:35 +00:00
|
|
|
|
"Detected HD-Copy image with {0} tracks and {1} sectors per track.",
|
|
|
|
|
|
fheader.lastCylinder + 1, fheader.sectorsPerTrack);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.Cylinders = (uint)fheader.lastCylinder + 1;
|
|
|
|
|
|
_imageInfo.SectorsPerTrack = fheader.sectorsPerTrack;
|
|
|
|
|
|
_imageInfo.SectorSize = 512; // only 512 bytes per sector supported
|
|
|
|
|
|
_imageInfo.Heads = 2; // only 2-sided floppies are supported
|
2020-11-11 04:19:18 +00:00
|
|
|
|
_imageInfo.Sectors = 2 * _imageInfo.Cylinders * _imageInfo.SectorsPerTrack;
|
|
|
|
|
|
_imageInfo.ImageSize = _imageInfo.Sectors * _imageInfo.SectorSize;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-15 11:25:26 +01:00
|
|
|
|
_imageInfo.CreationTime = imageFilter.CreationTime;
|
|
|
|
|
|
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
|
|
|
|
|
|
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.MediaType = Geometry.GetMediaType(((ushort)_imageInfo.Cylinders, 2,
|
|
|
|
|
|
(ushort)_imageInfo.SectorsPerTrack, 512, MediaEncoding.MFM,
|
|
|
|
|
|
false));
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
|
|
|
|
|
// build table of track offsets
|
2020-07-20 21:11:32 +01:00
|
|
|
|
for(int i = 0; i < _imageInfo.Cylinders * 2; i++)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
if(fheader.trackMap[i] == 0)
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_trackOffset[i] = -1;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// track is present, read the block header
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(currentOffset + 3 >= stream.Length)
|
2021-09-16 19:10:39 +01:00
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
|
|
|
|
|
byte[] blkHeader = new byte[2];
|
|
|
|
|
|
stream.Read(blkHeader, 0, 2);
|
|
|
|
|
|
short blkLength = BitConverter.ToInt16(blkHeader, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// assume block sizes are positive
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(blkLength < 0)
|
2021-09-16 19:10:39 +01:00
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
AaruConsole.DebugWriteLine("HDCP plugin", "Track {0} offset 0x{1:x8}, size={2:x4}", i,
|
|
|
|
|
|
currentOffset, blkLength);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_trackOffset[i] = currentOffset;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
|
|
|
|
|
currentOffset += 2 + blkLength;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// skip the block data
|
|
|
|
|
|
stream.Seek(blkLength, SeekOrigin.Current);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ensure that the last track is present completely
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(currentOffset > stream.Length)
|
2021-09-16 19:10:39 +01:00
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
|
|
|
|
|
// save some variables for later use
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_fileHeader = fheader;
|
|
|
|
|
|
_hdcpImageFilter = imageFilter;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2021-09-16 19:10:39 +01:00
|
|
|
|
return ErrorNumber.NoError;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-19 21:16:47 +01:00
|
|
|
|
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2021-09-19 21:16:47 +01:00
|
|
|
|
buffer = null;
|
2020-07-20 21:11:32 +01:00
|
|
|
|
int trackNum = (int)(sectorAddress / _imageInfo.SectorsPerTrack);
|
|
|
|
|
|
int sectorOffset = (int)(sectorAddress % _imageInfo.SectorsPerTrack);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1)
|
2021-09-19 21:16:47 +01:00
|
|
|
|
return ErrorNumber.OutOfRange;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(trackNum > 2 * _imageInfo.Cylinders)
|
2021-09-19 21:16:47 +01:00
|
|
|
|
return ErrorNumber.SectorNotFound;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
|
buffer = new byte[_imageInfo.SectorSize];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(_trackOffset[trackNum] == -1)
|
2021-09-19 21:16:47 +01:00
|
|
|
|
Array.Clear(buffer, 0, (int)_imageInfo.SectorSize);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// track is present in file, make sure it has been loaded
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(!_trackCache.ContainsKey(trackNum))
|
2021-09-21 04:55:28 +01:00
|
|
|
|
{
|
|
|
|
|
|
ErrorNumber errno = ReadTrackIntoCache(_hdcpImageFilter.GetDataForkStream(), trackNum);
|
|
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
|
return errno;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
|
Array.Copy(_trackCache[trackNum], sectorOffset * _imageInfo.SectorSize, buffer, 0,
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.SectorSize);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
|
return ErrorNumber.NoError;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-19 21:16:47 +01:00
|
|
|
|
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2021-09-19 21:16:47 +01:00
|
|
|
|
buffer = null;
|
|
|
|
|
|
|
|
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1)
|
|
|
|
|
|
return ErrorNumber.OutOfRange;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(sectorAddress + length > _imageInfo.Sectors)
|
2021-09-19 21:16:47 +01:00
|
|
|
|
return ErrorNumber.OutOfRange;
|
|
|
|
|
|
|
|
|
|
|
|
var ms = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
ErrorNumber errno = ReadSector(sectorAddress + i, out byte[] sector);
|
|
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
|
return errno;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
|
ms.Write(sector, 0, sector.Length);
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
|
buffer = ms.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|