Remove DiscImageChef.CommonTypes dependence on DiscImageChef.Decoders.

This commit is contained in:
2020-01-11 20:55:54 +00:00
parent 53f92aa111
commit 6b1033317a
40 changed files with 1640 additions and 776 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -33,31 +33,33 @@
using System;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.Decoders.ATA;
using DiscImageChef.Decoders.SCSI;
using DiscImageChef.CommonTypes.Structs.Devices.ATA;
using DiscImageChef.CommonTypes.Structs.Devices.SCSI;
using DiscImageChef.Decoders.SecureDigital;
using DiscImageChef.Helpers;
using VendorString = DiscImageChef.Decoders.SecureDigital.VendorString;
namespace DiscImageChef.DiscImages
{
public partial class DiscImageChef
{
/// <summary>
/// Checks for media tags that may contain metadata and sets it up if not already set
/// </summary>
/// <summary>Checks for media tags that may contain metadata and sets it up if not already set</summary>
void SetMetadataFromTags()
{
// Search for SecureDigital CID
if(mediaTags.TryGetValue(MediaTagType.SD_CID, out byte[] sdCid))
{
CID decoded = Decoders.SecureDigital.Decoders.DecodeCID(sdCid);
if(string.IsNullOrWhiteSpace(imageInfo.DriveManufacturer))
imageInfo.DriveManufacturer = VendorString.Prettify(decoded.Manufacturer);
if(string.IsNullOrWhiteSpace(imageInfo.DriveModel)) imageInfo.DriveModel = decoded.ProductName;
if(string.IsNullOrWhiteSpace(imageInfo.DriveModel))
imageInfo.DriveModel = decoded.ProductName;
if(string.IsNullOrWhiteSpace(imageInfo.DriveFirmwareRevision))
imageInfo.DriveFirmwareRevision =
$"{(decoded.ProductRevision & 0xF0) >> 4:X2}.{decoded.ProductRevision & 0x0F:X2}";
if(string.IsNullOrWhiteSpace(imageInfo.DriveSerialNumber))
imageInfo.DriveSerialNumber = $"{decoded.ProductSerialNumber}";
}
@@ -66,12 +68,17 @@ namespace DiscImageChef.DiscImages
if(mediaTags.TryGetValue(MediaTagType.MMC_CID, out byte[] mmcCid))
{
Decoders.MMC.CID decoded = Decoders.MMC.Decoders.DecodeCID(mmcCid);
if(string.IsNullOrWhiteSpace(imageInfo.DriveManufacturer))
imageInfo.DriveManufacturer = Decoders.MMC.VendorString.Prettify(decoded.Manufacturer);
if(string.IsNullOrWhiteSpace(imageInfo.DriveModel)) imageInfo.DriveModel = decoded.ProductName;
if(string.IsNullOrWhiteSpace(imageInfo.DriveModel))
imageInfo.DriveModel = decoded.ProductName;
if(string.IsNullOrWhiteSpace(imageInfo.DriveFirmwareRevision))
imageInfo.DriveFirmwareRevision =
$"{(decoded.ProductRevision & 0xF0) >> 4:X2}.{decoded.ProductRevision & 0x0F:X2}";
if(string.IsNullOrWhiteSpace(imageInfo.DriveSerialNumber))
imageInfo.DriveSerialNumber = $"{decoded.ProductSerialNumber}";
}
@@ -79,15 +86,18 @@ namespace DiscImageChef.DiscImages
// Search for SCSI INQUIRY
if(mediaTags.TryGetValue(MediaTagType.SCSI_INQUIRY, out byte[] scsiInquiry))
{
Inquiry.SCSIInquiry? nullableInquiry = Inquiry.Decode(scsiInquiry);
Inquiry? nullableInquiry = Inquiry.Decode(scsiInquiry);
if(nullableInquiry.HasValue)
{
Inquiry.SCSIInquiry inquiry = nullableInquiry.Value;
Inquiry inquiry = nullableInquiry.Value;
if(string.IsNullOrWhiteSpace(imageInfo.DriveManufacturer))
imageInfo.DriveManufacturer = StringHandlers.CToString(inquiry.VendorIdentification)?.Trim();
if(string.IsNullOrWhiteSpace(imageInfo.DriveModel))
imageInfo.DriveModel = StringHandlers.CToString(inquiry.ProductIdentification)?.Trim();
if(string.IsNullOrWhiteSpace(imageInfo.DriveFirmwareRevision))
imageInfo.DriveFirmwareRevision =
StringHandlers.CToString(inquiry.ProductRevisionLevel)?.Trim();
@@ -95,29 +105,34 @@ namespace DiscImageChef.DiscImages
}
// Search for ATA or ATAPI IDENTIFY
if(!mediaTags.TryGetValue(MediaTagType.ATA_IDENTIFY, out byte[] ataIdentify) &&
!mediaTags.TryGetValue(MediaTagType.ATAPI_IDENTIFY, out ataIdentify)) return;
if(!mediaTags.TryGetValue(MediaTagType.ATA_IDENTIFY, out byte[] ataIdentify) &&
!mediaTags.TryGetValue(MediaTagType.ATAPI_IDENTIFY, out ataIdentify))
return;
Identify.IdentifyDevice? nullableIdentify = Decoders.ATA.Identify.Decode(ataIdentify);
Identify.IdentifyDevice? nullableIdentify = CommonTypes.Structs.Devices.ATA.Identify.Decode(ataIdentify);
if(!nullableIdentify.HasValue) return;
if(!nullableIdentify.HasValue)
return;
Identify.IdentifyDevice identify = nullableIdentify.Value;
string[] separated = identify.Model.Split(' ');
if(separated.Length == 1)
if(string.IsNullOrWhiteSpace(imageInfo.DriveModel)) imageInfo.DriveModel = separated[0];
if(string.IsNullOrWhiteSpace(imageInfo.DriveModel))
imageInfo.DriveModel = separated[0];
else
{
if(string.IsNullOrWhiteSpace(imageInfo.DriveManufacturer))
imageInfo.DriveManufacturer = separated[0];
if(string.IsNullOrWhiteSpace(imageInfo.DriveModel))
imageInfo.DriveModel = separated[separated.Length - 1];
}
if(string.IsNullOrWhiteSpace(imageInfo.DriveFirmwareRevision))
imageInfo.DriveFirmwareRevision = identify.FirmwareRevision;
if(string.IsNullOrWhiteSpace(imageInfo.DriveSerialNumber))
imageInfo.DriveSerialNumber = identify.SerialNumber;
}
@@ -225,9 +240,11 @@ namespace DiscImageChef.DiscImages
// Gets a DDT entry
ulong GetDdtEntry(ulong sectorAddress)
{
if(inMemoryDdt) return userDataDdt[sectorAddress];
if(inMemoryDdt)
return userDataDdt[sectorAddress];
if(ddtEntryCache.TryGetValue(sectorAddress, out ulong entry)) return entry;
if(ddtEntryCache.TryGetValue(sectorAddress, out ulong entry))
return entry;
long oldPosition = imageStream.Position;
imageStream.Position = outMemoryDdtPosition + Marshal.SizeOf<DdtHeader>();
@@ -237,9 +254,11 @@ namespace DiscImageChef.DiscImages
imageStream.Position = oldPosition;
entry = BitConverter.ToUInt64(temp, 0);
if(ddtEntryCache.Count >= MAX_DDT_ENTRY_CACHE) ddtEntryCache.Clear();
if(ddtEntryCache.Count >= MAX_DDT_ENTRY_CACHE)
ddtEntryCache.Clear();
ddtEntryCache.Add(sectorAddress, entry);
return entry;
}
@@ -248,8 +267,11 @@ namespace DiscImageChef.DiscImages
{
if(inMemoryDdt)
{
if(IsTape) tapeDdt[sectorAddress] = pointer;
else userDataDdt[sectorAddress] = pointer;
if(IsTape)
tapeDdt[sectorAddress] = pointer;
else
userDataDdt[sectorAddress] = pointer;
return;
}

View File

@@ -37,7 +37,7 @@ using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Exceptions;
using DiscImageChef.CommonTypes.Interfaces;
using DiscImageChef.Decoders.ATA;
using DiscImageChef.CommonTypes.Structs.Devices.ATA;
using DiscImageChef.Helpers;
namespace DiscImageChef.DiscImages
@@ -54,7 +54,8 @@ namespace DiscImageChef.DiscImages
RsIdeHeader hdr = Marshal.ByteArrayToStructureLittleEndian<RsIdeHeader>(hdrB);
if(!hdr.magic.SequenceEqual(signature)) return false;
if(!hdr.magic.SequenceEqual(signature))
return false;
dataOff = hdr.dataOff;
@@ -72,7 +73,7 @@ namespace DiscImageChef.DiscImages
{
identify = new byte[512];
Array.Copy(hdr.identify, 0, identify, 0, hdr.identify.Length);
Identify.IdentifyDevice? ataId = Decoders.ATA.Identify.Decode(identify);
Identify.IdentifyDevice? ataId = CommonTypes.Structs.Devices.ATA.Identify.Decode(identify);
if(ataId.HasValue)
{
@@ -88,7 +89,9 @@ namespace DiscImageChef.DiscImages
}
}
if(imageInfo.Cylinders == 0 || imageInfo.Heads == 0 || imageInfo.SectorsPerTrack == 0)
if(imageInfo.Cylinders == 0 ||
imageInfo.Heads == 0 ||
imageInfo.SectorsPerTrack == 0)
{
imageInfo.Cylinders = (uint)(imageInfo.Sectors / 16 / 63);
imageInfo.Heads = 16;
@@ -114,7 +117,7 @@ namespace DiscImageChef.DiscImages
Stream stream = rsIdeImageFilter.GetDataForkStream();
stream.Seek((long)(dataOff + sectorAddress * imageInfo.SectorSize), SeekOrigin.Begin);
stream.Seek((long)(dataOff + (sectorAddress * imageInfo.SectorSize)), SeekOrigin.Begin);
stream.Read(buffer, 0, (int)(length * imageInfo.SectorSize));
@@ -123,11 +126,13 @@ namespace DiscImageChef.DiscImages
public byte[] ReadDiskTag(MediaTagType tag)
{
if(!imageInfo.ReadableMediaTags.Contains(tag) || tag != MediaTagType.ATA_IDENTIFY)
if(!imageInfo.ReadableMediaTags.Contains(tag) ||
tag != MediaTagType.ATA_IDENTIFY)
throw new FeatureUnsupportedImageException("Feature not supported by image format");
byte[] buffer = new byte[512];
Array.Copy(identify, 0, buffer, 0, 512);
return buffer;
}
}

View File

@@ -37,7 +37,7 @@ using System.Linq;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Structs;
using DiscImageChef.Decoders.ATA;
using DiscImageChef.CommonTypes.Structs.Devices.ATA;
using DiscImageChef.Helpers;
using Schemas;
using Version = DiscImageChef.CommonTypes.Interop.Version;
@@ -47,37 +47,49 @@ namespace DiscImageChef.DiscImages
public partial class RsIde
{
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
uint sectorSize)
uint sectorSize)
{
if(sectorSize != 256 && sectorSize != 512)
if(sectorSize != 256 &&
sectorSize != 512)
{
ErrorMessage = "Unsupported sector size";
return false;
}
if(sectors > 63 * 16 * 1024)
{
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};
imageInfo = new ImageInfo
{
MediaType = mediaType, SectorSize = sectorSize, Sectors = sectors
};
try { writingStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); }
try
{
writingStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
}
catch(IOException e)
{
ErrorMessage = $"Could not create new image file, exception {e.Message}";
return false;
}
IsWriting = true;
ErrorMessage = null;
return true;
}
@@ -86,17 +98,20 @@ namespace DiscImageChef.DiscImages
if(tag != MediaTagType.ATA_IDENTIFY)
{
ErrorMessage = $"Unsupported media tag {tag}.";
return false;
}
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
return false;
}
identify = new byte[106];
Array.Copy(data, 0, identify, 0, 106);
return true;
}
@@ -105,26 +120,31 @@ namespace DiscImageChef.DiscImages
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)((ulong)Marshal.SizeOf<RsIdeHeader>() + sectorAddress * imageInfo.SectorSize),
writingStream.Seek((long)((ulong)Marshal.SizeOf<RsIdeHeader>() + (sectorAddress * imageInfo.SectorSize)),
SeekOrigin.Begin);
writingStream.Write(data, 0, data.Length);
ErrorMessage = "";
return true;
}
@@ -133,38 +153,45 @@ namespace DiscImageChef.DiscImages
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)((ulong)Marshal.SizeOf<RsIdeHeader>() + sectorAddress * imageInfo.SectorSize),
writingStream.Seek((long)((ulong)Marshal.SizeOf<RsIdeHeader>() + (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;
}
@@ -173,6 +200,7 @@ namespace DiscImageChef.DiscImages
if(!IsWriting)
{
ErrorMessage = "Image is not opened for writing";
return false;
}
@@ -194,52 +222,53 @@ namespace DiscImageChef.DiscImages
imageInfo.Cylinders = (uint)(imageInfo.Sectors / imageInfo.Heads / imageInfo.SectorsPerTrack);
if(imageInfo.Cylinders == 0 && imageInfo.Heads == 0 && imageInfo.SectorsPerTrack == 0) break;
if(imageInfo.Cylinders == 0 &&
imageInfo.Heads == 0 &&
imageInfo.SectorsPerTrack == 0)
break;
}
}
RsIdeHeader header = new RsIdeHeader
var header = new RsIdeHeader
{
magic = signature,
identify = new byte[106],
dataOff = (ushort)Marshal.SizeOf<RsIdeHeader>(),
revision = 1,
reserved = new byte[11]
magic = signature, identify = new byte[106], dataOff = (ushort)Marshal.SizeOf<RsIdeHeader>(),
revision = 1, reserved = new byte[11]
};
if(imageInfo.SectorSize == 256) header.flags = RsIdeFlags.HalfSectors;
if(imageInfo.SectorSize == 256)
header.flags = RsIdeFlags.HalfSectors;
if(identify == null)
{
Identify.IdentifyDevice ataId = new Identify.IdentifyDevice
var ataId = new Identify.IdentifyDevice
{
GeneralConfiguration =
Decoders.ATA.Identify.GeneralConfigurationBit.UltraFastIDE |
Decoders.ATA.Identify.GeneralConfigurationBit.Fixed |
Decoders.ATA.Identify.GeneralConfigurationBit.NotMFM |
Decoders.ATA.Identify.GeneralConfigurationBit.SoftSector,
Cylinders = (ushort)imageInfo.Cylinders,
Heads = (ushort)imageInfo.Heads,
SectorsPerTrack = (ushort)imageInfo.SectorsPerTrack,
VendorWord47 = 0x80,
Capabilities =
Decoders.ATA.Identify.CapabilitiesBit.DMASupport |
Decoders.ATA.Identify.CapabilitiesBit.IORDY |
Decoders.ATA.Identify.CapabilitiesBit.LBASupport,
ExtendedIdentify = Decoders.ATA.Identify.ExtendedIdentifyBit.Words54to58Valid,
CurrentCylinders = (ushort)imageInfo.Cylinders,
CurrentHeads = (ushort)imageInfo.Heads,
CommonTypes.Structs.Devices.ATA.Identify.GeneralConfigurationBit.UltraFastIDE |
CommonTypes.Structs.Devices.ATA.Identify.GeneralConfigurationBit.Fixed |
CommonTypes.Structs.Devices.ATA.Identify.GeneralConfigurationBit.NotMFM |
CommonTypes.Structs.Devices.ATA.Identify.GeneralConfigurationBit.SoftSector,
Cylinders = (ushort)imageInfo.Cylinders, Heads = (ushort)imageInfo.Heads,
SectorsPerTrack = (ushort)imageInfo.SectorsPerTrack, VendorWord47 = 0x80,
Capabilities = CommonTypes.Structs.Devices.ATA.Identify.CapabilitiesBit.DMASupport |
CommonTypes.Structs.Devices.ATA.Identify.CapabilitiesBit.IORDY |
CommonTypes.Structs.Devices.ATA.Identify.CapabilitiesBit.LBASupport,
ExtendedIdentify =
CommonTypes.Structs.Devices.ATA.Identify.ExtendedIdentifyBit.Words54to58Valid,
CurrentCylinders = (ushort)imageInfo.Cylinders, CurrentHeads = (ushort)imageInfo.Heads,
CurrentSectorsPerTrack = (ushort)imageInfo.SectorsPerTrack,
CurrentSectors = (uint)imageInfo.Sectors,
LBASectors = (uint)imageInfo.Sectors,
DMASupported = Decoders.ATA.Identify.TransferMode.Mode0,
DMAActive = Decoders.ATA.Identify.TransferMode.Mode0
CurrentSectors = (uint)imageInfo.Sectors, LBASectors = (uint)imageInfo.Sectors,
DMASupported = CommonTypes.Structs.Devices.ATA.Identify.TransferMode.Mode0,
DMAActive = CommonTypes.Structs.Devices.ATA.Identify.TransferMode.Mode0
};
if(string.IsNullOrEmpty(imageInfo.DriveManufacturer)) imageInfo.DriveManufacturer = "DiscImageChef";
if(string.IsNullOrEmpty(imageInfo.DriveManufacturer))
imageInfo.DriveManufacturer = "DiscImageChef";
if(string.IsNullOrEmpty(imageInfo.DriveModel)) imageInfo.DriveModel = "";
if(string.IsNullOrEmpty(imageInfo.DriveModel))
imageInfo.DriveModel = "";
if(string.IsNullOrEmpty(imageInfo.DriveFirmwareRevision)) Version.GetVersion();
if(string.IsNullOrEmpty(imageInfo.DriveFirmwareRevision))
Version.GetVersion();
if(string.IsNullOrEmpty(imageInfo.DriveSerialNumber))
imageInfo.DriveSerialNumber = $"{new Random().NextDouble():16X}";
@@ -247,17 +276,21 @@ namespace DiscImageChef.DiscImages
byte[] ataIdBytes = new byte[Marshal.SizeOf<Identify.IdentifyDevice>()];
IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocHGlobal(512);
System.Runtime.InteropServices.Marshal.StructureToPtr(ataId, ptr, true);
System.Runtime.InteropServices.Marshal.Copy(ptr, ataIdBytes, 0,
Marshal.SizeOf<Identify.IdentifyDevice>());
System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr);
Array.Copy(ScrambleAtaString(imageInfo.DriveManufacturer + " " + imageInfo.DriveModel, 40), 0,
ataIdBytes, 27 * 2, 40);
Array.Copy(ScrambleAtaString(imageInfo.DriveFirmwareRevision, 8), 0, ataIdBytes, 23 * 2, 8);
Array.Copy(ScrambleAtaString(imageInfo.DriveSerialNumber, 20), 0, ataIdBytes, 10 * 2, 20);
Array.Copy(ataIdBytes, 0, header.identify, 0, 106);
Array.Copy(ScrambleAtaString(imageInfo.DriveFirmwareRevision, 8), 0, ataIdBytes, 23 * 2, 8);
Array.Copy(ScrambleAtaString(imageInfo.DriveSerialNumber, 20), 0, ataIdBytes, 10 * 2, 20);
Array.Copy(ataIdBytes, 0, header.identify, 0, 106);
}
else Array.Copy(identify, 0, header.identify, 0, 106);
else
Array.Copy(identify, 0, header.identify, 0, 106);
byte[] hdr = new byte[Marshal.SizeOf<RsIdeHeader>()];
IntPtr hdrPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(Marshal.SizeOf<RsIdeHeader>());
@@ -273,6 +306,7 @@ namespace DiscImageChef.DiscImages
IsWriting = false;
ErrorMessage = "";
return true;
}
@@ -291,18 +325,21 @@ namespace DiscImageChef.DiscImages
if(cylinders > ushort.MaxValue)
{
ErrorMessage = "Too many cylinders.";
return false;
}
if(heads > ushort.MaxValue)
{
ErrorMessage = "Too many heads.";
return false;
}
if(sectorsPerTrack > ushort.MaxValue)
{
ErrorMessage = "Too many sectors per track.";
return false;
}
@@ -316,12 +353,14 @@ namespace DiscImageChef.DiscImages
public bool WriteSectorTag(byte[] data, ulong sectorAddress, SectorTagType tag)
{
ErrorMessage = "Unsupported feature";
return false;
}
public bool WriteSectorsTag(byte[] data, ulong sectorAddress, uint length, SectorTagType tag)
{
ErrorMessage = "Unsupported feature";
return false;
}

View File

@@ -40,12 +40,14 @@ using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Exceptions;
using DiscImageChef.CommonTypes.Interfaces;
using DiscImageChef.CommonTypes.Structs;
using DiscImageChef.CommonTypes.Structs.Devices.SCSI;
using DiscImageChef.Console;
using DiscImageChef.Decoders.CD;
using DiscImageChef.Decoders.DVD;
using DiscImageChef.Decoders.SCSI;
using Schemas;
using DMI = DiscImageChef.Decoders.Xbox.DMI;
using Inquiry = DiscImageChef.CommonTypes.Structs.Devices.SCSI.Inquiry;
using Session = DiscImageChef.CommonTypes.Structs.Session;
using TrackType = DiscImageChef.CommonTypes.Enums.TrackType;
@@ -781,7 +783,7 @@ namespace DiscImageChef.DiscImages
if (mediaTags.ContainsKey(MediaTagType.SCSI_INQUIRY))
{
var devType = PeripheralDeviceTypes.DirectAccess;
Inquiry.SCSIInquiry? scsiInq = null;
Inquiry? scsiInq = null;
if (mediaTags.TryGetValue(MediaTagType.SCSI_INQUIRY, out var inq))
{
scsiInq = Inquiry.Decode(inq);
@@ -858,7 +860,7 @@ namespace DiscImageChef.DiscImages
// It's ATA, check tags
if (mediaTags.TryGetValue(MediaTagType.ATA_IDENTIFY, out var identifyBuf))
{
var ataId = Decoders.ATA.Identify.Decode(identifyBuf);
var ataId = CommonTypes.Structs.Devices.ATA.Identify.Decode(identifyBuf);
if (ataId.HasValue)
{
imageInfo.MediaType = (ushort) ataId.Value.GeneralConfiguration == 0x848A