Files
Aaru/DiscImageChef.DiscImages/NHDr0.cs

428 lines
15 KiB
C#
Raw Normal View History

2017-08-03 11:10:44 +01:00
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : NHDr0.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
2017-08-03 11:10:44 +01:00
//
// --[ Description ] ----------------------------------------------------------
//
// Manages NHD r0 disk images.
2017-08-03 11:10:44 +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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
2017-08-03 11:10:44 +01:00
// ****************************************************************************/
2017-12-19 19:33:46 +00:00
2017-08-03 11:10:44 +01:00
using System;
using System.Collections.Generic;
2017-12-19 19:33:46 +00:00
using System.IO;
2017-08-03 11:10:44 +01:00
using System.Linq;
2017-12-19 19:33:46 +00:00
using System.Runtime.InteropServices;
2017-08-03 11:10:44 +01:00
using System.Text;
2017-12-19 19:33:46 +00:00
using DiscImageChef.CommonTypes;
using DiscImageChef.Console;
2017-08-03 11:10:44 +01:00
using DiscImageChef.Filters;
namespace DiscImageChef.DiscImages
2017-08-03 11:10:44 +01:00
{
2017-12-19 20:33:03 +00:00
// Info from http://www.geocities.jp/t98next/nhdr0.txt
public class Nhdr0 : ImagePlugin
2017-12-19 20:33:03 +00:00
{
#region Internal structures
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Nhdr0Header
2017-12-19 20:33:03 +00:00
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 15)] public byte[] szFileID;
public byte reserved1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x100)] public byte[] szComment;
public int dwHeadSize;
public int dwCylinder;
public short wHead;
public short wSect;
public short wSectLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public byte[] reserved2;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0xE0)] public byte[] reserved3;
}
#endregion
readonly byte[] signature =
{0x54, 0x39, 0x38, 0x48, 0x44, 0x44, 0x49, 0x4D, 0x41, 0x47, 0x45, 0x2E, 0x52, 0x30, 0x00};
public Nhdr0()
2017-12-19 20:33:03 +00:00
{
Name = "T98-Next NHD r0 Disk Image";
PluginUuid = new Guid("6ECACD0A-8F4D-4465-8815-AEA000D370E3");
ImageInfo = new ImageInfo
2017-12-19 20:33:03 +00:00
{
ReadableSectorTags = new List<SectorTagType>(),
ReadableMediaTags = new List<MediaTagType>(),
ImageHasPartitions = false,
ImageHasSessions = false,
ImageVersion = null,
ImageApplication = null,
ImageApplicationVersion = null,
ImageCreator = null,
ImageComments = null,
MediaManufacturer = null,
MediaModel = null,
MediaSerialNumber = null,
MediaBarcode = null,
MediaPartNumber = null,
MediaSequence = 0,
LastMediaSequence = 0,
DriveManufacturer = null,
DriveModel = null,
DriveSerialNumber = null,
DriveFirmwareRevision = null
2017-12-19 20:33:03 +00:00
};
}
Nhdr0Header nhdhdr;
2017-12-19 20:33:03 +00:00
Filter nhdImageFilter;
public override bool IdentifyImage(Filter imageFilter)
{
Stream stream = imageFilter.GetDataForkStream();
stream.Seek(0, SeekOrigin.Begin);
// Even if comment is supposedly ASCII, I'm pretty sure most emulators allow Shift-JIS to be used :p
Encoding shiftjis = Encoding.GetEncoding("shift_jis");
nhdhdr = new Nhdr0Header();
2017-12-19 20:33:03 +00:00
if(stream.Length < Marshal.SizeOf(nhdhdr)) return false;
byte[] hdrB = new byte[Marshal.SizeOf(nhdhdr)];
stream.Read(hdrB, 0, hdrB.Length);
2017-12-19 20:33:03 +00:00
GCHandle handle = GCHandle.Alloc(hdrB, GCHandleType.Pinned);
nhdhdr = (Nhdr0Header)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Nhdr0Header));
2017-12-19 20:33:03 +00:00
handle.Free();
if(!nhdhdr.szFileID.SequenceEqual(signature)) return false;
DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.szFileID = \"{0}\"",
StringHandlers.CToString(nhdhdr.szFileID, shiftjis));
DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.reserved1 = {0}", nhdhdr.reserved1);
DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.szComment = \"{0}\"",
StringHandlers.CToString(nhdhdr.szComment, shiftjis));
DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.dwHeadSize = {0}", nhdhdr.dwHeadSize);
DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.dwCylinder = {0}", nhdhdr.dwCylinder);
DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.wHead = {0}", nhdhdr.wHead);
DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.wSect = {0}", nhdhdr.wSect);
DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.wSectLen = {0}", nhdhdr.wSectLen);
return true;
}
public override bool OpenImage(Filter imageFilter)
{
Stream stream = imageFilter.GetDataForkStream();
stream.Seek(0, SeekOrigin.Begin);
// Even if comment is supposedly ASCII, I'm pretty sure most emulators allow Shift-JIS to be used :p
Encoding shiftjis = Encoding.GetEncoding("shift_jis");
nhdhdr = new Nhdr0Header();
2017-12-19 20:33:03 +00:00
if(stream.Length < Marshal.SizeOf(nhdhdr)) return false;
byte[] hdrB = new byte[Marshal.SizeOf(nhdhdr)];
stream.Read(hdrB, 0, hdrB.Length);
2017-12-19 20:33:03 +00:00
GCHandle handle = GCHandle.Alloc(hdrB, GCHandleType.Pinned);
nhdhdr = (Nhdr0Header)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Nhdr0Header));
2017-12-19 20:33:03 +00:00
handle.Free();
ImageInfo.MediaType = MediaType.GENERIC_HDD;
2017-12-19 20:33:03 +00:00
ImageInfo.ImageSize = (ulong)(stream.Length - nhdhdr.dwHeadSize);
ImageInfo.ImageCreationTime = imageFilter.GetCreationTime();
ImageInfo.ImageLastModificationTime = imageFilter.GetLastWriteTime();
ImageInfo.ImageName = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
ImageInfo.Sectors = (ulong)(nhdhdr.dwCylinder * nhdhdr.wHead * nhdhdr.wSect);
ImageInfo.XmlMediaType = XmlMediaType.BlockMedia;
ImageInfo.SectorSize = (uint)nhdhdr.wSectLen;
ImageInfo.Cylinders = (uint)nhdhdr.dwCylinder;
ImageInfo.Heads = (uint)nhdhdr.wHead;
ImageInfo.SectorsPerTrack = (uint)nhdhdr.wSect;
ImageInfo.ImageComments = StringHandlers.CToString(nhdhdr.szComment, shiftjis);
2017-12-19 20:33:03 +00:00
nhdImageFilter = imageFilter;
return true;
}
public override bool ImageHasPartitions()
{
return false;
}
public override ulong GetImageSize()
{
return ImageInfo.ImageSize;
2017-12-19 20:33:03 +00:00
}
public override ulong GetSectors()
{
return ImageInfo.Sectors;
2017-12-19 20:33:03 +00:00
}
public override uint GetSectorSize()
{
return ImageInfo.SectorSize;
2017-12-19 20:33:03 +00:00
}
public override string GetImageFormat()
{
return "NHDr0 disk image";
}
public override string GetImageVersion()
{
return ImageInfo.ImageVersion;
2017-12-19 20:33:03 +00:00
}
public override string GetImageApplication()
{
return ImageInfo.ImageApplication;
2017-12-19 20:33:03 +00:00
}
public override string GetImageApplicationVersion()
{
return ImageInfo.ImageApplicationVersion;
2017-12-19 20:33:03 +00:00
}
public override string GetImageCreator()
{
return ImageInfo.ImageCreator;
2017-12-19 20:33:03 +00:00
}
public override DateTime GetImageCreationTime()
{
return ImageInfo.ImageCreationTime;
2017-12-19 20:33:03 +00:00
}
public override DateTime GetImageLastModificationTime()
{
return ImageInfo.ImageLastModificationTime;
2017-12-19 20:33:03 +00:00
}
public override string GetImageName()
{
return ImageInfo.ImageName;
2017-12-19 20:33:03 +00:00
}
public override string GetImageComments()
{
return ImageInfo.ImageComments;
2017-12-19 20:33:03 +00:00
}
public override MediaType GetMediaType()
{
return ImageInfo.MediaType;
2017-12-19 20:33:03 +00:00
}
public override byte[] ReadSector(ulong sectorAddress)
{
return ReadSectors(sectorAddress, 1);
}
public override byte[] ReadSectors(ulong sectorAddress, uint length)
{
if(sectorAddress > ImageInfo.Sectors - 1)
2017-12-19 20:33:03 +00:00
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
if(sectorAddress + length > ImageInfo.Sectors)
2017-12-19 20:33:03 +00:00
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
byte[] buffer = new byte[length * ImageInfo.SectorSize];
2017-12-19 20:33:03 +00:00
Stream stream = nhdImageFilter.GetDataForkStream();
stream.Seek((long)((ulong)nhdhdr.dwHeadSize + sectorAddress * ImageInfo.SectorSize), SeekOrigin.Begin);
2017-12-19 20:33:03 +00:00
stream.Read(buffer, 0, (int)(length * ImageInfo.SectorSize));
2017-12-19 20:33:03 +00:00
return buffer;
}
#region Unsupported features
public override byte[] ReadDiskTag(MediaTagType tag)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override byte[] ReadSectorTag(ulong sectorAddress, SectorTagType tag)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override byte[] ReadSector(ulong sectorAddress, uint track)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override byte[] ReadSectorTag(ulong sectorAddress, uint track, SectorTagType tag)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override byte[] ReadSectorsTag(ulong sectorAddress, uint length, SectorTagType tag)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override byte[] ReadSectors(ulong sectorAddress, uint length, uint track)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override byte[] ReadSectorsTag(ulong sectorAddress, uint length, uint track, SectorTagType tag)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override byte[] ReadSectorLong(ulong sectorAddress)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override byte[] ReadSectorLong(ulong sectorAddress, uint track)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override byte[] ReadSectorsLong(ulong sectorAddress, uint length)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override byte[] ReadSectorsLong(ulong sectorAddress, uint length, uint track)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override string GetMediaManufacturer()
{
return null;
}
public override string GetMediaModel()
{
return null;
}
public override string GetMediaSerialNumber()
{
return null;
}
public override string GetMediaBarcode()
{
return null;
}
public override string GetMediaPartNumber()
{
return null;
}
public override int GetMediaSequence()
{
return 0;
}
public override int GetLastDiskSequence()
{
return 0;
}
public override string GetDriveManufacturer()
{
return null;
}
public override string GetDriveModel()
{
return null;
}
public override string GetDriveSerialNumber()
{
return null;
}
public override List<Partition> GetPartitions()
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override List<Track> GetTracks()
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override List<Track> GetSessionTracks(Session session)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override List<Track> GetSessionTracks(ushort session)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override List<Session> GetSessions()
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override bool? VerifySector(ulong sectorAddress)
{
return null;
}
public override bool? VerifySector(ulong sectorAddress, uint track)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override bool? VerifySectors(ulong sectorAddress, uint length, out List<ulong> failingLbas,
out List<ulong> unknownLbas)
2017-12-19 20:33:03 +00:00
{
failingLbas = new List<ulong>();
unknownLbas = new List<ulong>();
for(ulong i = 0; i < ImageInfo.Sectors; i++) unknownLbas.Add(i);
2017-12-19 20:33:03 +00:00
return null;
}
public override bool? VerifySectors(ulong sectorAddress, uint length, uint track, out List<ulong> failingLbas,
out List<ulong> unknownLbas)
2017-12-19 20:33:03 +00:00
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
public override bool? VerifyMediaImage()
{
return null;
}
#endregion
}
2017-08-03 11:10:44 +01:00
}