Add writing support to Digital Research DISKCOPY disk image.

This commit is contained in:
2018-01-01 14:11:12 +00:00
parent 9e5e7621da
commit c27f381c70

View File

@@ -34,7 +34,9 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes;
using DiscImageChef.Console; using DiscImageChef.Console;
@@ -42,7 +44,7 @@ using DiscImageChef.Filters;
namespace DiscImageChef.DiscImages namespace DiscImageChef.DiscImages
{ {
public class DriDiskCopy : IMediaImage public class DriDiskCopy : IWritableImage
{ {
const string REGEX_DRI = @"DiskImage\s(?<version>\d+.\d+)\s\(C\)\s\d+\,*\d*\s+Digital Research Inc"; const string REGEX_DRI = @"DiskImage\s(?<version>\d+.\d+)\s\(C\)\s\d+\,*\d*\s+Digital Research Inc";
@@ -50,8 +52,9 @@ namespace DiscImageChef.DiscImages
IFilter driImageFilter; IFilter driImageFilter;
/// <summary>Footer of opened image</summary> /// <summary>Footer of opened image</summary>
DriFooter footer; DriFooter footer;
ImageInfo imageInfo; ImageInfo imageInfo;
FileStream writingStream;
public DriDiskCopy() public DriDiskCopy()
{ {
@@ -355,6 +358,192 @@ namespace DiscImageChef.DiscImages
throw new FeatureUnsupportedImageException("Feature not supported by image format"); throw new FeatureUnsupportedImageException("Feature not supported by image format");
} }
public IEnumerable<MediaTagType> SupportedMediaTags => new MediaTagType[] { };
public IEnumerable<SectorTagType> SupportedSectorTags => new SectorTagType[] { };
// TODO: Test with real hardware to see real supported media
public IEnumerable<MediaType> SupportedMediaTypes =>
new[]
{
MediaType.ACORN_35_DS_DD, MediaType.ACORN_35_DS_HD, MediaType.Apricot_35, MediaType.ATARI_35_DS_DD,
MediaType.ATARI_35_DS_DD_11, MediaType.ATARI_35_SS_DD, MediaType.ATARI_35_SS_DD_11, MediaType.DMF,
MediaType.DMF_82, MediaType.DOS_35_DS_DD_8, MediaType.DOS_35_DS_DD_9, MediaType.DOS_35_ED,
MediaType.DOS_35_HD, MediaType.DOS_35_SS_DD_8, MediaType.DOS_35_SS_DD_9, MediaType.DOS_525_DS_DD_8,
MediaType.DOS_525_DS_DD_9, MediaType.DOS_525_HD, MediaType.DOS_525_SS_DD_8, MediaType.DOS_525_SS_DD_9,
MediaType.FDFORMAT_35_DD, MediaType.FDFORMAT_35_HD, MediaType.FDFORMAT_525_DD,
MediaType.FDFORMAT_525_HD, MediaType.RX50, MediaType.XDF_35, MediaType.XDF_525
};
public IEnumerable<(string name, Type type, string description)> SupportedOptions =>
new(string name, Type type, string description)[] { };
public IEnumerable<string> KnownExtensions => new[] {".dsk"};
public bool IsWriting { get; private set; }
public string ErrorMessage { get; private set; }
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
uint sectorSize)
{
if(sectorSize == 0)
{
ErrorMessage = "Unsupported sector size";
return false;
}
if(sectors > ushort.MaxValue)
{
ErrorMessage = $"Too many sectors";
return false;
}
if(!SupportedMediaTypes.Contains(mediaType))
{
ErrorMessage = $"Unsupport media format {mediaType}";
return false;
}
imageInfo = new ImageInfo {MediaType = mediaType, SectorSize = sectorSize, Sectors = sectors};
try { writingStream = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None); }
catch(IOException e)
{
ErrorMessage = $"Could not create new image file, exception {e.Message}";
return false;
}
(ushort cylinders, byte heads, ushort sectorsPerTrack, uint bytesPerSector, MediaEncoding encoding, bool
variableSectorsPerTrack, MediaType type) geometry = Geometry.GetGeometry(mediaType);
footer = new DriFooter
{
signature = new byte[51],
bpb = new DriBpb
{
five = 5,
driveCode = DriDriveCodes.mf2ed,
cylinders = geometry.cylinders,
bps = (ushort)imageInfo.SectorSize,
sectors = (ushort)imageInfo.Sectors,
sptrack = (ushort)imageInfo.SectorsPerTrack,
heads = (ushort)imageInfo.Heads,
sptrack2 = (ushort)imageInfo.SectorsPerTrack,
unknown5 = new byte[144]
}
};
Array.Copy(Encoding.ASCII.GetBytes("DiskImage 2.01 (C) 1990,1991 Digital Research Inc"), 0,
footer.signature, 0, 50);
footer.bpbcopy = footer.bpb;
IsWriting = true;
ErrorMessage = null;
return true;
}
public bool WriteMediaTag(byte[] data, MediaTagType tag)
{
ErrorMessage = "Writing media tags is not supported.";
return false;
}
public bool WriteSector(byte[] data, ulong sectorAddress)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
return false;
}
if(data.Length != imageInfo.SectorSize)
{
ErrorMessage = "Incorrect data size";
return false;
}
if(sectorAddress >= imageInfo.Sectors)
{
ErrorMessage = "Tried to write past image size";
return false;
}
writingStream.Seek((long)(sectorAddress * imageInfo.SectorSize), SeekOrigin.Begin);
writingStream.Write(data, 0, data.Length);
ErrorMessage = "";
return true;
}
public bool WriteSectors(byte[] data, ulong sectorAddress, uint length)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
return false;
}
if(data.Length % imageInfo.SectorSize != 0)
{
ErrorMessage = "Incorrect data size";
return false;
}
if(sectorAddress + length > imageInfo.Sectors)
{
ErrorMessage = "Tried to write past image size";
return false;
}
writingStream.Seek((long)(512 + sectorAddress * imageInfo.SectorSize), SeekOrigin.Begin);
writingStream.Write(data, 0, data.Length);
ErrorMessage = "";
return true;
}
public bool WriteSectorLong(byte[] data, ulong sectorAddress)
{
ErrorMessage = "Writing sectors with tags is not supported.";
return false;
}
public bool WriteSectorsLong(byte[] data, ulong sectorAddress, uint length)
{
ErrorMessage = "Writing sectors with tags is not supported.";
return false;
}
public bool SetTracks(List<Track> tracks)
{
ErrorMessage = "Unsupported feature";
return false;
}
public bool Close()
{
if(!IsWriting)
{
ErrorMessage = "Image is not opened for writing";
return false;
}
byte[] hdr = new byte[Marshal.SizeOf(footer)];
IntPtr hdrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(footer));
Marshal.StructureToPtr(footer, hdrPtr, true);
Marshal.Copy(hdrPtr, hdr, 0, hdr.Length);
Marshal.FreeHGlobal(hdrPtr);
writingStream.Seek(0, SeekOrigin.End);
writingStream.Write(hdr, 0, hdr.Length);
writingStream.Flush();
writingStream.Close();
IsWriting = false;
return true;
}
public bool SetMetadata(ImageInfo metadata)
{
return true;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)] [StructLayout(LayoutKind.Sequential, Pack = 1)]
struct DriFooter struct DriFooter
{ {