diff --git a/DiscImageChef.Decoders/ChangeLog b/DiscImageChef.Decoders/ChangeLog index 92476c16e..543533d01 100644 --- a/DiscImageChef.Decoders/ChangeLog +++ b/DiscImageChef.Decoders/ChangeLog @@ -1,3 +1,19 @@ +2016-10-22 Natalia Portillo + + * OCR.cs: + * CID.cs: + * CSD.cs: + * ExtendedCSD.cs: + * VendorString.cs: + * SCR.cs: + * CID.cs: + * CSD.cs: + * OCR.cs: + * DiscImageChef.Decoders.csproj: + * VendorString.cs: Add MMC/SecureDigital device support. Not + yet used because of a bad implementation of SEND_CSD and + SEND_CID commands (TODO). + 2016-10-17 Natalia Portillo * CIS.cs: diff --git a/DiscImageChef.Decoders/DiscImageChef.Decoders.csproj b/DiscImageChef.Decoders/DiscImageChef.Decoders.csproj index 1d4c624fd..dbf25acbe 100644 --- a/DiscImageChef.Decoders/DiscImageChef.Decoders.csproj +++ b/DiscImageChef.Decoders/DiscImageChef.Decoders.csproj @@ -97,6 +97,16 @@ + + + + + + + + + + @@ -120,6 +130,8 @@ + + diff --git a/DiscImageChef.Decoders/MMC/CID.cs b/DiscImageChef.Decoders/MMC/CID.cs new file mode 100644 index 000000000..39144f77f --- /dev/null +++ b/DiscImageChef.Decoders/MMC/CID.cs @@ -0,0 +1,193 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : CID.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Description +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ + +using System; +using System.Text; + +namespace DiscImageChef.Decoders.MMC +{ + public class CID + { + public byte Manufacturer; + public byte DeviceType; + public byte ApplicationID; + public string ProductName; + public byte ProductRevision; + public uint ProductSerialNumber; + public byte ManufacturingDate; + public byte CRC; + } + + public partial class Decoders + { + public static CID DecodeCID(uint[] response) + { + if(response == null) + return null; + + if(response.Length != 4) + return null; + + byte[] data = new byte[16]; + byte[] tmp = new byte[4]; + + tmp = BitConverter.GetBytes(response[0]); + Array.Copy(tmp, 0, data, 0, 4); + tmp = BitConverter.GetBytes(response[1]); + Array.Copy(tmp, 0, data, 4, 4); + tmp = BitConverter.GetBytes(response[2]); + Array.Copy(tmp, 0, data, 8, 4); + tmp = BitConverter.GetBytes(response[3]); + Array.Copy(tmp, 0, data, 12, 4); + + return DecodeCID(data); + } + + public static CID DecodeCID(byte[] response) + { + if(response == null) + return null; + + if(response.Length != 16) + return null; + + byte[] tmp; + + CID cid = new CID(); + cid.Manufacturer = response[0]; + cid.DeviceType = (byte)(response[1] & 0x03); + tmp = new byte[6]; + Array.Copy(response, 3, tmp, 0, 6); + cid.ProductName = StringHandlers.CToString(tmp); + cid.ProductRevision = response[9]; + tmp = new byte[4]; + cid.ProductSerialNumber = BitConverter.ToUInt32(response, 10); + cid.ManufacturingDate = response[14]; + cid.CRC = (byte)((response[15] & 0xFE) >> 1); + + return cid; + } + + public static string PrettifyCID(CID cid) + { + if(cid == null) + return null; + + StringBuilder sb = new StringBuilder(); + + sb.AppendLine("MultiMediaCard Device Identification Register:"); + sb.AppendFormat("\tManufacturer: {0}", VendorString.Prettify(cid.Manufacturer)).AppendLine(); + switch(cid.DeviceType) + { + case 0: + sb.AppendLine("\tRemovable device"); + break; + case 1: + sb.AppendLine("\tBGA device"); + break; + case 2: + sb.AppendLine("\tPOP device"); + break; + } + sb.AppendFormat("\tApplication ID: {0}", cid.ApplicationID).AppendLine(); + sb.AppendFormat("\tProduct name: {0}", cid.ProductName).AppendLine(); + sb.AppendFormat("\tProduct revision: {0:X2}.{1:X2}", (cid.ProductRevision & 0xF0) >> 4, cid.ProductRevision & 0x0F).AppendLine(); + sb.AppendFormat("\tProduct serial number: {0}", cid.ProductSerialNumber).AppendLine(); + string year = ""; + switch(cid.ManufacturingDate & 0x0F) + { + case 0: + year = "1997 or 2013"; + break; + case 1: + year = "1998 or 2014"; + break; + case 2: + year = "1999 or 2015"; + break; + case 3: + year = "2000 or 2016"; + break; + case 4: + year = "2001 or 2017"; + break; + case 5: + year = "2002 or 2018"; + break; + case 6: + year = "2003 or 2019"; + break; + case 7: + year = "2004 or 2020"; + break; + case 8: + year = "2005 or 2021"; + break; + case 9: + year = "2006 or 2022"; + break; + case 10: + year = "2007 or 2023"; + break; + case 11: + year = "2008 or 2024"; + break; + case 12: + year = "2009 or 2025"; + break; + case 13: + year = "2010"; + break; + case 14: + year = "2011"; + break; + case 15: + year = "2012"; + break; + } + sb.AppendFormat("\tDevice manufactured month {0} of {1}", (cid.ManufacturingDate & 0xF0) >> 4, year).AppendLine(); + sb.AppendFormat("\tCID CRC: 0x{0:X2}", cid.CRC).AppendLine(); + + return sb.ToString(); + } + + public static string PrettifyCID(uint[] response) + { + return PrettifyCID(DecodeCID(response)); + } + + public static string PrettifyCID(byte[] response) + { + return PrettifyCID(DecodeCID(response)); + } + } +} diff --git a/DiscImageChef.Decoders/MMC/CSD.cs b/DiscImageChef.Decoders/MMC/CSD.cs new file mode 100644 index 000000000..80346c6cd --- /dev/null +++ b/DiscImageChef.Decoders/MMC/CSD.cs @@ -0,0 +1,592 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : CSD.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Description +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ + +using System; +using System.Text; + +namespace DiscImageChef.Decoders.MMC +{ + public class CSD + { + public byte Structure; + public byte Version; + public byte TAAC; + public byte NSAC; + public byte Speed; + public ushort Classes; + public byte ReadBlockLength; + public bool ReadsPartialBlocks; + public bool WriteMisalignment; + public bool ReadMisalignment; + public bool DSRImplemented; + public ushort Size; + public byte ReadCurrentAtVddMin; + public byte ReadCurrentAtVddMax; + public byte WriteCurrentAtVddMin; + public byte WriteCurrentAtVddMax; + public byte SizeMultiplier; + public byte EraseGroupSize; + public byte EraseGroupSizeMultiplier; + public byte WriteProtectGroupSize; + public bool WriteProtectGroupEnable; + public byte DefaultECC; + public byte WriteSpeedFactor; + public byte WriteBlockLength; + public bool WritesPartialBlocks; + public bool ContentProtection; + public bool FileFormatGroup; + public bool Copy; + public bool PermanentWriteProtect; + public bool TemporaryWriteProtect; + public byte FileFormat; + public byte ECC; + public byte CRC; + } + + public partial class Decoders + { + public static CSD DecodeCSD(uint[] response) + { + if(response == null) + return null; + + if(response.Length != 4) + return null; + + byte[] data = new byte[16]; + byte[] tmp = new byte[4]; + + tmp = BitConverter.GetBytes(response[0]); + Array.Copy(tmp, 0, data, 0, 4); + tmp = BitConverter.GetBytes(response[1]); + Array.Copy(tmp, 0, data, 4, 4); + tmp = BitConverter.GetBytes(response[2]); + Array.Copy(tmp, 0, data, 8, 4); + tmp = BitConverter.GetBytes(response[3]); + Array.Copy(tmp, 0, data, 12, 4); + + return DecodeCSD(data); + } + + public static CSD DecodeCSD(byte[] response) + { + if(response == null) + return null; + + if(response.Length != 16) + return null; + + CSD csd = new CSD(); + + csd.Structure = (byte)((response[0] & 0xC0) >> 6); + csd.Version = (byte)((response[0] & 0x3C) >> 2); + csd.TAAC = response[1]; + csd.NSAC = response[2]; + csd.Speed = response[3]; + csd.Classes = (ushort)((response[4] << 4) + ((response[5] & 0xF0) >> 4)); + csd.ReadBlockLength = (byte)(response[5] & 0x0F); + csd.ReadsPartialBlocks = (response[6] & 0x80) == 0x80; + csd.WriteMisalignment = (response[6] & 0x40) == 0x40; + csd.ReadMisalignment = (response[6] & 0x20) == 0x20; + csd.DSRImplemented = (response[6] & 0x10) == 0x10; + csd.Size = (ushort)(((response[6] & 0x03) << 10) + (response[7] << 2) + ((response[8] & 0xC0) >> 6)); + csd.ReadCurrentAtVddMin = (byte)((response[8] & 0x38) >> 3); + csd.ReadCurrentAtVddMax = (byte)(response[8] & 0x07); + csd.WriteCurrentAtVddMin = (byte)((response[9] & 0xE0) >> 5); + csd.WriteCurrentAtVddMax = (byte)((response[9] & 0x1C) >> 2); + csd.SizeMultiplier = (byte)(((response[9] & 0x03) << 1) + ((response[10] & 0x80) >> 7)); + csd.EraseGroupSize = (byte)((response[10] & 0x7C) >> 2); + csd.EraseGroupSizeMultiplier = (byte)(((response[10] & 0x03) << 3) + ((response[11] & 0xE0) >> 5)); + csd.WriteProtectGroupSize = (byte)(response[11] & 0x1F); + csd.WriteProtectGroupEnable = (response[12] & 0x80) == 0x80; + csd.DefaultECC = (byte)((response[12] & 0x60) >> 5); + csd.WriteSpeedFactor = (byte)((response[12] & 0x1C) >> 2); + csd.WriteBlockLength = (byte)(((response[12] & 0x03) << 2) + ((response[13] & 0xC0) >> 6)); + csd.WritesPartialBlocks = (response[13] & 0x20) == 0x20; + csd.ContentProtection = (response[13] & 0x01) == 0x01; + csd.FileFormatGroup = (response[14] & 0x80) == 0x80; + csd.Copy = (response[14] & 0x40) == 0x40; + csd.PermanentWriteProtect = (response[14] & 0x20) == 0x20; + csd.TemporaryWriteProtect = (response[14] & 0x10) == 0x10; + csd.FileFormat = (byte)((response[14] & 0x0C) >> 2); + csd.ECC = (byte)(response[14] & 0x03); + csd.CRC = (byte)((response[15] & 0xFE) >> 1); + + return csd; + } + + public static string PrettifyCSD(CSD csd) + { + if(csd == null) + return null; + + double unitFactor = 0; + double multiplier = 0; + double result = 0; + string unit = ""; + + StringBuilder sb = new StringBuilder(); + sb.AppendLine("MultiMediaCard Device Specific Data Register:"); + switch(csd.Structure) + { + case 0: + sb.AppendLine("\tRegister version 1.0"); + break; + case 1: + sb.AppendLine("\tRegister version 1.1"); + break; + case 2: + sb.AppendLine("\tRegister version 1.2"); + break; + case 3: + sb.AppendLine("\tRegister version is defined in Extended Device Specific Data Register"); + break; + } + + switch(csd.TAAC & 0x07) + { + case 0: + unit = "ns"; + unitFactor = 1; + break; + case 1: + unit = "ns"; + unitFactor = 10; + break; + case 2: + unit = "ns"; + unitFactor = 100; + break; + case 3: + unit = "μs"; + unitFactor = 1; + break; + case 4: + unit = "μs"; + unitFactor = 10; + break; + case 5: + unit = "μs"; + unitFactor = 100; + break; + case 6: + unit = "ms"; + unitFactor = 1; + break; + case 7: + unit = "ms"; + unitFactor = 10; + break; + } + + switch((csd.TAAC & 0x78) >> 3) + { + case 0: + multiplier = 0; + break; + case 1: + multiplier = 1; + break; + case 2: + multiplier = 1.2; + break; + case 3: + multiplier = 1.3; + break; + case 4: + multiplier = 1.5; + break; + case 5: + multiplier = 2; + break; + case 6: + multiplier = 2.5; + break; + case 7: + multiplier = 3; + break; + case 8: + multiplier = 3.5; + break; + case 9: + multiplier = 4; + break; + case 10: + multiplier = 4.5; + break; + case 11: + multiplier = 5; + break; + case 12: + multiplier = 5.5; + break; + case 13: + multiplier = 6; + break; + case 14: + multiplier = 7; + break; + case 15: + multiplier = 8; + break; + } + result = unitFactor * multiplier; + sb.AppendFormat("\tAsynchronous data access time is {0}{1}", result, unit).AppendLine(); + + sb.AppendFormat("\tClock dependent part of data access is {0} clock cycles", csd.NSAC * 100).AppendLine(); + + unit = "MHz"; + switch(csd.Speed & 0x07) + { + case 0: + unitFactor = 0.1; + break; + case 1: + unitFactor = 1; + break; + case 2: + unitFactor = 10; + break; + case 3: + unitFactor = 100; + break; + default: + unit = "unknown"; + unitFactor = 0; + break; + } + + switch((csd.Speed & 0x78) >> 3) + { + case 0: + multiplier = 0; + break; + case 1: + multiplier = 1; + break; + case 2: + multiplier = 1.2; + break; + case 3: + multiplier = 1.3; + break; + case 4: + multiplier = 1.5; + break; + case 5: + multiplier = 2; + break; + case 6: + multiplier = 2.6; + break; + case 7: + multiplier = 3; + break; + case 8: + multiplier = 3.5; + break; + case 9: + multiplier = 4; + break; + case 10: + multiplier = 4.5; + break; + case 11: + multiplier = 5.2; + break; + case 12: + multiplier = 5.5; + break; + case 13: + multiplier = 6; + break; + case 14: + multiplier = 7; + break; + case 15: + multiplier = 8; + break; + } + result = unitFactor * multiplier; + sb.AppendFormat("\tDevice's clock frequency: {0}{1}", result, unit).AppendLine(); + + unit = ""; + for(int cl = 0, mask = 1; cl <= 11; cl++, mask <<= 1) + { + if((csd.Classes & mask) == mask) + unit += string.Format(" {0}", cl); + } + + sb.AppendFormat("\tDevice support command classes {0}", unit).AppendLine(); + if(csd.ReadBlockLength == 15) + sb.AppendLine("\tRead block length size is defined in extended CSD"); + else + sb.AppendFormat("\tRead block length is {0} bytes", Math.Pow(2, csd.ReadBlockLength)).AppendLine(); + + if(csd.ReadsPartialBlocks) + sb.AppendLine("\tDevice allows reading partial blocks"); + + if(csd.WriteMisalignment) + sb.AppendLine("\tWrite commands can cross physical block boundaries"); + if(csd.ReadMisalignment) + sb.AppendLine("\tRead commands can cross physical block boundaries"); + + if(csd.DSRImplemented) + sb.AppendLine("\tDevice implements configurable driver stage"); + + if(csd.Size == 0xFFF) + sb.AppendLine("\tDevice may be bigger than 2GiB and have its real size defined in the extended CSD"); + + result = (csd.Size + 1) * Math.Pow(2, csd.SizeMultiplier + 2); + sb.AppendFormat("\tDevice has {0} blocks", (int)result).AppendLine(); + + result = (csd.Size + 1) * Math.Pow(2, csd.SizeMultiplier + 2) * Math.Pow(2, csd.ReadBlockLength); + if(result > 1073741824) + sb.AppendFormat("\tDevice has {0} GiB", result/1073741824.0); + else if(result > 1048576) + sb.AppendFormat("\tDevice has {0} MiB", result / 1048576.0); + else if(result > 1024) + sb.AppendFormat("\tDevice has {0} KiB", result / 1024.0); + else + sb.AppendFormat("\tDevice has {0} bytes", result); + + switch(csd.ReadCurrentAtVddMin & 0x07) + { + case 0: + sb.AppendLine("\tDevice uses a maximum of 0.5mA for reading at minimum voltage"); + break; + case 1: + sb.AppendLine("\tDevice uses a maximum of 1mA for reading at minimum voltage"); + break; + case 2: + sb.AppendLine("\tDevice uses a maximum of 5mA for reading at minimum voltage"); + break; + case 3: + sb.AppendLine("\tDevice uses a maximum of 10mA for reading at minimum voltage"); + break; + case 4: + sb.AppendLine("\tDevice uses a maximum of 25mA for reading at minimum voltage"); + break; + case 5: + sb.AppendLine("\tDevice uses a maximum of 35mA for reading at minimum voltage"); + break; + case 6: + sb.AppendLine("\tDevice uses a maximum of 60mA for reading at minimum voltage"); + break; + case 7: + sb.AppendLine("\tDevice uses a maximum of 100mA for reading at minimum voltage"); + break; + } + + switch(csd.ReadCurrentAtVddMax & 0x07) + { + case 0: + sb.AppendLine("\tDevice uses a maximum of 1mA for reading at maximum voltage"); + break; + case 1: + sb.AppendLine("\tDevice uses a maximum of 5mA for reading at maximum voltage"); + break; + case 2: + sb.AppendLine("\tDevice uses a maximum of 10mA for reading at maximum voltage"); + break; + case 3: + sb.AppendLine("\tDevice uses a maximum of 25mA for reading at maximum voltage"); + break; + case 4: + sb.AppendLine("\tDevice uses a maximum of 35mA for reading at maximum voltage"); + break; + case 5: + sb.AppendLine("\tDevice uses a maximum of 45mA for reading at maximum voltage"); + break; + case 6: + sb.AppendLine("\tDevice uses a maximum of 80mA for reading at maximum voltage"); + break; + case 7: + sb.AppendLine("\tDevice uses a maximum of 200mA for reading at maximum voltage"); + break; + } + + switch(csd.WriteCurrentAtVddMin & 0x07) + { + case 0: + sb.AppendLine("\tDevice uses a maximum of 0.5mA for writing at minimum voltage"); + break; + case 1: + sb.AppendLine("\tDevice uses a maximum of 1mA for writing at minimum voltage"); + break; + case 2: + sb.AppendLine("\tDevice uses a maximum of 5mA for writing at minimum voltage"); + break; + case 3: + sb.AppendLine("\tDevice uses a maximum of 10mA for writing at minimum voltage"); + break; + case 4: + sb.AppendLine("\tDevice uses a maximum of 25mA for writing at minimum voltage"); + break; + case 5: + sb.AppendLine("\tDevice uses a maximum of 35mA for writing at minimum voltage"); + break; + case 6: + sb.AppendLine("\tDevice uses a maximum of 60mA for writing at minimum voltage"); + break; + case 7: + sb.AppendLine("\tDevice uses a maximum of 100mA for writing at minimum voltage"); + break; + } + + switch(csd.WriteCurrentAtVddMax & 0x07) + { + case 0: + sb.AppendLine("\tDevice uses a maximum of 1mA for writing at maximum voltage"); + break; + case 1: + sb.AppendLine("\tDevice uses a maximum of 5mA for writing at maximum voltage"); + break; + case 2: + sb.AppendLine("\tDevice uses a maximum of 10mA for writing at maximum voltage"); + break; + case 3: + sb.AppendLine("\tDevice uses a maximum of 25mA for writing at maximum voltage"); + break; + case 4: + sb.AppendLine("\tDevice uses a maximum of 35mA for writing at maximum voltage"); + break; + case 5: + sb.AppendLine("\tDevice uses a maximum of 45mA for writing at maximum voltage"); + break; + case 6: + sb.AppendLine("\tDevice uses a maximum of 80mA for writing at maximum voltage"); + break; + case 7: + sb.AppendLine("\tDevice uses a maximum of 200mA for writing at maximum voltage"); + break; + } + + unitFactor = Convert.ToDouble(string.Format("{0:X}", csd.EraseGroupSize)); + multiplier = Convert.ToDouble(string.Format("{0:X}", csd.EraseGroupSizeMultiplier)); + result = (unitFactor + 1) * (multiplier + 1); + sb.AppendFormat("\tDevice can erase a minimum of {0} blocks at a time", (int)result).AppendLine(); + + if(csd.WriteProtectGroupEnable) + { + sb.AppendLine("\tDevice can write protect regions"); + unitFactor = Convert.ToDouble(string.Format("{0:X}", csd.WriteProtectGroupSize)); + sb.AppendFormat("\tDevice can write protect a minimum of {0} blocks at a time", (int)(result + 1)).AppendLine(); + } + else + sb.AppendLine("\tDevice can't write protect regions"); + + switch(csd.DefaultECC) + { + case 0: + sb.AppendLine("\tDevice uses no ECC by default"); + break; + case 1: + sb.AppendLine("\tDevice uses BCH(542, 512) ECC by default"); + break; + case 2: + sb.AppendFormat("\tDevice uses unknown ECC code {0} by default", csd.DefaultECC).AppendLine(); + break; + } + + sb.AppendFormat("\tWriting is {0} times slower than reading", Math.Pow(2, csd.WriteSpeedFactor)).AppendLine(); + + if(csd.WriteBlockLength == 15) + sb.AppendLine("\tWrite block length size is defined in extended CSD"); + else + sb.AppendFormat("\tWrite block length is {0} bytes", Math.Pow(2, csd.WriteBlockLength)).AppendLine(); + + if(csd.WritesPartialBlocks) + sb.AppendLine("\tDevice allows writing partial blocks"); + + if(csd.ContentProtection) + sb.AppendLine("\tDevice supports content protection"); + + if(!csd.Copy) + sb.AppendLine("\tDevice contents are original"); + + if(csd.PermanentWriteProtect) + sb.AppendLine("\tDevice is permanently write protected"); + + if(csd.TemporaryWriteProtect) + sb.AppendLine("\tDevice is temporarily write protected"); + + if(!csd.FileFormatGroup) + { + switch(csd.FileFormat) + { + case 0: + sb.AppendLine("\tDevice is formatted like a hard disk"); + break; + case 1: + sb.AppendLine("\tDevice is formatted like a floppy disk using Microsoft FAT"); + break; + case 2: + sb.AppendLine("\tDevice uses Universal File Format"); + break; + default: + sb.AppendFormat("\tDevice uses unknown file format code {0}", csd.FileFormat).AppendLine(); + break; + } + } + else + sb.AppendFormat("\tDevice uses unknown file format code {0} and file format group 1", csd.FileFormat).AppendLine(); + + switch(csd.ECC) + { + case 0: + sb.AppendLine("\tDevice currently uses no ECC"); + break; + case 1: + sb.AppendLine("\tDevice currently uses BCH(542, 512) ECC by default"); + break; + case 2: + sb.AppendFormat("\tDevice currently uses unknown ECC code {0}", csd.DefaultECC).AppendLine(); + break; + } + + sb.AppendFormat("\tCSD CRC: 0x{0:X2}", csd.CRC).AppendLine(); + + return sb.ToString(); + } + + public static string PrettifyCSD(uint[] response) + { + return PrettifyCSD(DecodeCSD(response)); + } + + public static string PrettifyCSD(byte[] response) + { + return PrettifyCSD(DecodeCSD(response)); + } + } +} diff --git a/DiscImageChef.Decoders/MMC/ExtendedCSD.cs b/DiscImageChef.Decoders/MMC/ExtendedCSD.cs new file mode 100644 index 000000000..b0e880ca2 --- /dev/null +++ b/DiscImageChef.Decoders/MMC/ExtendedCSD.cs @@ -0,0 +1,704 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : ExtendedCSD.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Description +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ + +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace DiscImageChef.Decoders.MMC +{ + [StructLayout(LayoutKind.Sequential)] + public class ExtendedCSD + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] + public byte[] Reserved0; + public byte ExtendedSecurityCommandsError; + public byte SupportedCommandSets; + public byte HPIFeatures; + public byte BackgroundOperationsSupport; + public byte MaxPackedReadCommands; + public byte MaxPackedWriteCommands; + public byte DataTagSupport; + public byte TagUnitSize; + public byte TagResourcesSize; + public byte ContextManagementCaps; + public byte LargeUnitSize; + public byte ExtendedPartitionsSupport; + public byte SupportedModes; + public byte FFUFeatures; + public byte OperationCodesTimeout; + public uint FFUArgument; + public byte BarrierSupport; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 177)] + public byte[] Reserved1; + public byte CMDQueuingSupport; + public byte CMDQueuingDepth; + public uint NumberofFWSectorsCorrectlyProgrammed; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] + public byte[] VendorHealthReport; + public byte DeviceLifeEstimationTypeB; + public byte DeviceLifeEstimationTypeA; + public byte PreEOLInformation; + public byte OptimalReadSize; + public byte OptimalWriteSize; + public byte OptimalTrimUnitSize; + public ushort DeviceVersion; + public ulong FirmwareVersion; + public byte PowerClassDDR200; + public uint CacheSize; + public byte GenericCMD6Timeout; + public byte PowerOffNotificationTimeout; + public byte BackgroundOperationsStatus; + public uint CorrectlyProgrammedSectors; + public byte InitializationTimeAfterPartition; + public byte CacheFlushingPolicy; + public byte PowerClassDDR52; + public byte PowerClassDDR52_195; + public byte PowerClassDDR200_195; + public byte PowerClassDDR200_130; + public byte MinimumWritePerformanceDDR52; + public byte MinimumReadPerformanceDDR52; + public byte Reserved2; + public byte TRIMMultiplier; + public byte SecureFeatureSupport; + public byte SecureEraseMultiplier; + public byte SecureTRIMMultiplier; + public byte BootInformation; + public byte Reserved3; + public byte BootPartitionSize; + public byte AccessSize; + public byte HighCapacityEraseUnitSize; + public byte HighCapacityEraseTimeout; + public byte ReliableWriteSectorCount; + public byte HighCapacityWriteProtectGroupSize; + public byte SleepCurrentVcc; + public byte SleepCurrentVccq; + public byte ProductionStateAwarenessTimeout; + public byte SleepAwakeTimeout; + public byte SleepNotificationTimeout; + public uint SectorCount; + public byte SecureWriteProtectInformation; + public byte MinimumWritePerformance52; + public byte MinimumReadPerformance52; + public byte MinimumWritePerformance26; + public byte MinimumReadPerformance26; + public byte MinimumWritePerformance26_4; + public byte MinimumReadPerformance26_4; + public byte Reserved4; + public byte PowerClass26; + public byte PowerClass52; + public byte PowerClass26_195; + public byte PowerClass52_195; + public byte PartitionSwitchingTime; + public byte OutOfInterruptBusyTiming; + public byte DriverStrength; + public byte DeviceType; + public byte Reserved5; + public byte Structure; + public byte Reserved6; + public byte Revision; + public byte CommandSet; + public byte Reserved7; + public byte CommandSetRevision; + public byte Reserved8; + public byte PowerClass; + public byte Reserved9; + public byte HighSpeedInterfaceTiming; + public byte StrobeSupport; + public byte BusWidth; + public byte Reserved10; + public byte ErasedMemoryContent; + public byte Reserved11; + public byte PartitionConfiguration; + public byte BootConfigProtection; + public byte BootBusConditions; + public byte Reserved12; + public byte HighDensityEraseGroupDefinition; + public byte BootWriteProtectionStatus; + public byte BootAreaWriteProtectionRegister; + public byte Reserved13; + public byte UserAreaWriteProtectionRegister; + public byte Reserved14; + public byte FirmwareConfiguration; + public byte RPMBSize; + public byte WriteReliabilitySettingRegister; + public byte WriteReliabilityParameterRegister; + public byte StartSanitizeOperation; + public byte ManuallyStartBackgroundOperations; + public byte EnableBackgroundOperationsHandshake; + public byte HWResetFunction; + public byte HPIManagement; + public byte PartitioningSupport; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] + public byte[] MaxEnhancedAreaSize; + public byte PartitionsAttribute; + public byte PartitioningSetting; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)] + public byte[] GeneralPurposePartitionSize; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] + public byte[] EnhancedUserDataAreaSize; + public uint EnhancedUserDataStartAddress; + public byte Reserved15; + public byte BadBlockManagementMode; + public byte ProductionStateAwareness; + public byte PackageCaseTemperatureControl; + public byte PeriodicWakeUp; + public byte SupportsProgramCxDInDDR; + public ushort Reserved16; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)] + public byte[] VendorSpecific; + public byte NativeSectorSize; + public byte SectorSizeEmulation; + public byte SectorSize; + public byte InitializationTimeout; + public byte Class6CommandsControl; + public byte AddressedGroupToBeReleased; + public ushort ExceptionEventsControl; + public ushort ExceptionEventsStatus; + public ushort ExtendedPartitionsAttribute; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 15)] + public byte[] ContextConfiguration; + public byte PackedCommandStatus; + public byte PackedCommandFailureIndex; + public byte PowerOffNotification; + public byte CacheControl; + public byte CacheFlushing; + public byte BarrierControl; + public byte ModeConfig; + public byte ModeOperationCodes; + public ushort Reserved17; + public byte FFUStatus; + public uint PreLoadingDataSize; + public uint MaxPreLoadingDataSize; + public byte ProductStateAwarenessEnablement; + public byte SecureRemovalType; + public byte CommandQueueModeEnable; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 15)] + public byte[] Reserved18; + } + + public partial class Decoders + { + public static ExtendedCSD DecodeExtendedCSD(byte[] response) + { + if(response == null) + return null; + + if(response.Length != 512) + return null; + + ExtendedCSD csd = new ExtendedCSD(); + GCHandle handle = GCHandle.Alloc(response, GCHandleType.Pinned); + csd = (ExtendedCSD)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(ExtendedCSD)); + handle.Free(); + + return csd; + } + + public static string PrettifyExtendedCSD(ExtendedCSD csd) + { + if(csd == null) + return null; + + StringBuilder sb = new StringBuilder(); + sb.AppendLine("MultiMediaCard Extended Device Specific Data Register:"); + + double unit; + + if((csd.HPIFeatures & 0x01) == 0x01) + { + if((csd.HPIFeatures & 0x02) == 0x02) + sb.AppendLine("\tDevice implements HPI using CMD12"); + else + sb.AppendLine("\tDevice implements HPI using CMD13"); + } + + if((csd.BackgroundOperationsSupport & 0x01) == 0x01) + sb.AppendLine("\tDevice supports background operations"); + + sb.AppendFormat("\tDevice supports a maximum of {0} packed reads and {1} packed writes", csd.MaxPackedReadCommands, csd.MaxPackedWriteCommands).AppendLine(); + + if((csd.DataTagSupport & 0x01) == 0x01) + { + sb.AppendLine("\tDevice supports Data Tag"); + sb.AppendFormat("\tTags must be in units of {0} sectors", Math.Pow(2, csd.TagUnitSize)).AppendLine(); + } + + if((csd.ExtendedPartitionsSupport & 0x01) == 0x01) + sb.AppendLine("\tDevice supports non-persistent extended partitions"); + if((csd.ExtendedPartitionsSupport & 0x02) == 0x02) + sb.AppendLine("\tDevice supports system code extended partitions"); + + if((csd.SupportedModes & 0x01) == 0x01) + sb.AppendLine("\tDevice supports FFU"); + if((csd.SupportedModes & 0x02) == 0x02) + sb.AppendLine("\tDevice supports Vendor Specific Mode"); + + if((csd.CMDQueuingSupport & 0x01) == 0x01) + sb.AppendFormat("\tDevice supports command queuing with a depth of {0}", csd.CMDQueuingDepth + 1).AppendLine(); + + sb.AppendFormat("\t{0} firmware sectors correctly programmed", csd.NumberofFWSectorsCorrectlyProgrammed).AppendLine(); + + switch(csd.DeviceLifeEstimationTypeB) + { + case 1: + sb.AppendLine("\tDevice used between 0% and 10% of its estimated life time"); + break; + case 2: + sb.AppendLine("\tDevice used between 10% and 20% of its estimated life time"); + break; + case 3: + sb.AppendLine("\tDevice used between 20% and 30% of its estimated life time"); + break; + case 4: + sb.AppendLine("\tDevice used between 30% and 40% of its estimated life time"); + break; + case 5: + sb.AppendLine("\tDevice used between 40% and 50% of its estimated life time"); + break; + case 6: + sb.AppendLine("\tDevice used between 50% and 60% of its estimated life time"); + break; + case 7: + sb.AppendLine("\tDevice used between 60% and 70% of its estimated life time"); + break; + case 8: + sb.AppendLine("\tDevice used between 70% and 80% of its estimated life time"); + break; + case 9: + sb.AppendLine("\tDevice used between 80% and 90% of its estimated life time"); + break; + case 10: + sb.AppendLine("\tDevice used between 90% and 100% of its estimated life time"); + break; + case 11: + sb.AppendLine("\tDevice exceeded its maximum estimated life time"); + break; + } + + switch(csd.DeviceLifeEstimationTypeA) + { + case 1: + sb.AppendLine("\tDevice used between 0% and 10% of its estimated life time"); + break; + case 2: + sb.AppendLine("\tDevice used between 10% and 20% of its estimated life time"); + break; + case 3: + sb.AppendLine("\tDevice used between 20% and 30% of its estimated life time"); + break; + case 4: + sb.AppendLine("\tDevice used between 30% and 40% of its estimated life time"); + break; + case 5: + sb.AppendLine("\tDevice used between 40% and 50% of its estimated life time"); + break; + case 6: + sb.AppendLine("\tDevice used between 50% and 60% of its estimated life time"); + break; + case 7: + sb.AppendLine("\tDevice used between 60% and 70% of its estimated life time"); + break; + case 8: + sb.AppendLine("\tDevice used between 70% and 80% of its estimated life time"); + break; + case 9: + sb.AppendLine("\tDevice used between 80% and 90% of its estimated life time"); + break; + case 10: + sb.AppendLine("\tDevice used between 90% and 100% of its estimated life time"); + break; + case 11: + sb.AppendLine("\tDevice exceeded its maximum estimated life time"); + break; + } + + switch(csd.PreEOLInformation) + { + case 1: + sb.AppendLine("\tDevice informs it's in good health"); + break; + case 2: + sb.AppendLine("\tDevice informs it should be replaced soon"); + break; + case 3: + sb.AppendLine("\tDevice informs it should be replace immediately"); + break; + } + + sb.AppendFormat("\tOptimal read size is {0} logical sectors", csd.OptimalReadSize).AppendLine(); + sb.AppendFormat("\tOptimal write size is {0} logical sectors", csd.OptimalWriteSize).AppendLine(); + sb.AppendFormat("\tOptimal trim size is {0} logical sectors", csd.OptimalTrimUnitSize).AppendLine(); + + sb.AppendFormat("\tDevice version: {0}", csd.DeviceVersion).AppendLine(); + sb.AppendFormat("\tFirmware version: {0}", csd.FirmwareVersion).AppendLine(); + + if(csd.CacheSize == 0) + sb.AppendLine("\tDevice has no cache"); + else + sb.AppendFormat("\tDevice has {0} KiB of cache", csd.CacheSize).AppendLine(); + + if(csd.GenericCMD6Timeout > 0) + sb.AppendFormat("\tDevice takes a maximum of {0} ms by default for a SWITCH command", csd.GenericCMD6Timeout * 10).AppendLine(); + + if(csd.PowerOffNotificationTimeout > 0) + sb.AppendFormat("\tDevice takes a maximum of {0} by default to power off from a SWITCH command notification", csd.PowerOffNotificationTimeout * 10).AppendLine(); + + switch(csd.BackgroundOperationsStatus & 0x03) + { + case 0: + sb.AppendLine("\tDevice has no pending background operations"); + break; + case 1: + sb.AppendLine("\tDevice has non critical operations outstanding"); + break; + case 2: + sb.AppendLine("\tDevice has performance impacted operations outstanding"); + break; + case 3: + sb.AppendLine("\tDevice has critical operations outstanding"); + break; + } + + sb.AppendFormat("\tLast WRITE MULTIPLE command correctly programmed {0} sectors", csd.CorrectlyProgrammedSectors).AppendLine(); + + if(csd.InitializationTimeAfterPartition > 0) + sb.AppendFormat("\tDevice takes a maximum of {0} ms for initialization after partition", csd.InitializationTimeAfterPartition * 100).AppendLine(); + + if((csd.CacheFlushingPolicy & 0x01) == 0x01) + sb.AppendLine("\tDevice uses a FIFO policy for cache flushing"); + + if(csd.TRIMMultiplier > 0) + sb.AppendFormat("\tDevice takes a maximum of {0} ms for trimming a single erase group", csd.TRIMMultiplier * 300).AppendLine(); + + if((csd.SecureFeatureSupport & 0x40) == 0x40) + sb.AppendLine("\tDevice supports the sanitize operation"); + if((csd.SecureFeatureSupport & 0x10) == 0x10) + sb.AppendLine("\tDevice supports supports the secure and insecure trim operations"); + if((csd.SecureFeatureSupport & 0x04) == 0x04) + sb.AppendLine("\tDevice supports automatic erase on retired defective blocks"); + if((csd.SecureFeatureSupport & 0x01) == 0x01) + sb.AppendLine("\tDevice supports secure purge operations"); + + if(csd.SecureEraseMultiplier > 0) + sb.AppendFormat("\tDevice takes a maximum of {0} ms for securely erasing a single erase group", csd.SecureEraseMultiplier * 300).AppendLine(); + if(csd.SecureTRIMMultiplier > 0) + sb.AppendFormat("\tDevice takes a maximum of {0} ms for securely trimming a single erase group", csd.SecureTRIMMultiplier * 300).AppendLine(); + + if((csd.BootInformation & 0x04) == 0x04) + sb.AppendLine("\tDevice supports high speed timing on boot"); + if((csd.BootInformation & 0x02) == 0x02) + sb.AppendLine("\tDevice supports dual data rate on boot"); + if((csd.BootInformation & 0x01) == 0x01) + sb.AppendLine("\tDevice supports alternative boot method"); + + if(csd.BootPartitionSize > 0) + sb.AppendFormat("\tDevice has a {0} KiB boot partition", csd.BootPartitionSize * 128).AppendLine(); + + if((csd.AccessSize & 0x0F) > 0) + sb.AppendFormat("\tDevice has a page size of {0} KiB", (csd.AccessSize & 0x0F) * 512.0 / 1024.0).AppendLine(); + + if(csd.HighCapacityEraseUnitSize > 0) + sb.AppendFormat("\tDevice erase groups are {0} KiB", csd.HighCapacityEraseUnitSize * 512).AppendLine(); + + + if(csd.HighCapacityEraseTimeout > 0) + sb.AppendFormat("\tDevice takes a maximum of {0} ms for erasing a single erase group", csd.HighCapacityEraseTimeout * 300).AppendLine(); + + if(csd.HighCapacityWriteProtectGroupSize > 0) + sb.AppendFormat("\tDevice smallest write protect group is made of {0} erase groups", csd.HighCapacityWriteProtectGroupSize).AppendLine(); + + if(csd.SleepCurrentVcc > 0) + { + unit = Math.Pow(2, csd.SleepCurrentVcc); + if(unit > 1000) + sb.AppendFormat("\tDevice uses {0} mA on Vcc when sleeping", unit / 1000).AppendLine(); + else + sb.AppendFormat("\tDevice uses {0} μA on Vcc when sleeping", unit).AppendLine(); + } + + if(csd.SleepCurrentVccq > 0) + { + unit = Math.Pow(2, csd.SleepCurrentVccq); + if(unit > 1000) + sb.AppendFormat("\tDevice uses {0} mA on Vccq when sleeping", unit / 1000).AppendLine(); + else + sb.AppendFormat("\tDevice uses {0} μA on Vccq when sleeping", unit).AppendLine(); + } + + if(csd.ProductionStateAwarenessTimeout > 0) + { + unit = Math.Pow(2, csd.ProductionStateAwareness) * 100; + if(unit > 1000000) + sb.AppendFormat("\tDevice takes a maximum of {0} s to switch production state awareness", unit / 1000000).AppendLine(); + else if(unit > 1000) + sb.AppendFormat("\tDevice takes a maximum of {0} ms to switch production state awareness", unit / 1000).AppendLine(); + else + sb.AppendFormat("\tDevice takes a maximum of {0} μs to switch production state awareness", unit).AppendLine(); + } + + if(csd.SleepAwakeTimeout > 0) + { + unit = Math.Pow(2, csd.SleepAwakeTimeout) * 100; + if(unit > 1000000) + sb.AppendFormat("\tDevice takes a maximum of {0} ms to transition between sleep and standby states", unit / 1000000).AppendLine(); + else if(unit > 1000) + sb.AppendFormat("\tDevice takes a maximum of {0} μs to transition between sleep and standby states", unit / 1000).AppendLine(); + else + sb.AppendFormat("\tDevice takes a maximum of {0} ns to transition between sleep and standby states", unit).AppendLine(); + } + + if(csd.SleepNotificationTimeout > 0) + { + unit = Math.Pow(2, csd.SleepNotificationTimeout) * 10; + if(unit > 1000000) + sb.AppendFormat("\tDevice takes a maximum of {0} s to move to sleep state", unit / 1000000).AppendLine(); + else if(unit > 1000) + sb.AppendFormat("\tDevice takes a maximum of {0} ms to move to sleep state", unit / 1000).AppendLine(); + else + sb.AppendFormat("\tDevice takes a maximum of {0} μs to move to sleep state", unit).AppendLine(); + } + + sb.AppendFormat("\tDevice has {0} sectors", csd.SectorCount).AppendLine(); + + if((csd.SecureWriteProtectInformation & 0x01) == 0x01) + { + sb.AppendLine("\tDevice supports secure write protection"); + if((csd.SecureWriteProtectInformation & 0x02) == 0x02) + sb.AppendLine("\tDevice has secure write protection enabled"); + } + + unit = csd.MinimumReadPerformance26 * 150; + if(csd.MinimumReadPerformance26 == 0) + sb.AppendLine("\tDevice cannot achieve 2.4MB/s reading in SDR 26Mhz mode"); + else + sb.AppendFormat("\tDevice can achieve a minimum of {0}MB/s reading in SDR 26Mhz mode", unit / 1000).AppendLine(); + + unit = csd.MinimumReadPerformance26_4 * 150; + if(csd.MinimumReadPerformance26_4 == 0) + sb.AppendLine("\tDevice cannot achieve 2.4MB/s reading in SDR 26Mhz 4-bit mode"); + else + sb.AppendFormat("\tDevice can achieve a minimum of {0}MB/s reading in SDR 26Mhz 4-bit mode", unit / 1000).AppendLine(); + + unit = csd.MinimumReadPerformance52 * 150; + if(csd.MinimumReadPerformance52 == 0) + sb.AppendLine("\tDevice cannot achieve 2.4MB/s reading in SDR 52Mhz mode"); + else + sb.AppendFormat("\tDevice can achieve a minimum of {0}MB/s reading in SDR 52Mhz mode", unit / 1000).AppendLine(); + + unit = csd.MinimumReadPerformanceDDR52 * 300; + if(csd.MinimumReadPerformanceDDR52 == 0) + sb.AppendLine("\tDevice cannot achieve 4.8MB/s reading in DDR 52Mhz mode"); + else + sb.AppendFormat("\tDevice can achieve a minimum of {0}MB/s reading in DDR 52Mhz mode", unit / 1000).AppendLine(); + + unit = csd.MinimumWritePerformance26 * 150; + if(csd.MinimumWritePerformance26 == 0) + sb.AppendLine("\tDevice cannot achieve 2.4MB/s writing in SDR 26Mhz mode"); + else + sb.AppendFormat("\tDevice can achieve a minimum of {0}MB/s writing in SDR 26Mhz mode", unit / 1000).AppendLine(); + + unit = csd.MinimumWritePerformance26_4 * 150; + if(csd.MinimumWritePerformance26_4 == 0) + sb.AppendLine("\tDevice cannot achieve 2.4MB/s writing in SDR 26Mhz 4-bit mode"); + else + sb.AppendFormat("\tDevice can achieve a minimum of {0}MB/s writing in SDR 26Mhz 4-bit mode", unit / 1000).AppendLine(); + + unit = csd.MinimumWritePerformance52 * 150; + if(csd.MinimumWritePerformance52 == 0) + sb.AppendLine("\tDevice cannot achieve 2.4MB/s writing in SDR 52Mhz mode"); + else + sb.AppendFormat("\tDevice can achieve a minimum of {0}MB/s writing in SDR 52Mhz mode", unit / 1000).AppendLine(); + + unit = csd.MinimumWritePerformanceDDR52 * 300; + if(csd.MinimumWritePerformanceDDR52 == 0) + sb.AppendLine("\tDevice cannot achieve 4.8MB/s writing in DDR 52Mhz mode"); + else + sb.AppendFormat("\tDevice can achieve a minimum of {0}MB/s writing in DDR 52Mhz mode", unit / 1000).AppendLine(); + + if(csd.PartitionSwitchingTime > 0) + sb.AppendFormat("\tDevice can take a maximum of {0} ms when switching partitions", csd.PartitionSwitchingTime * 10).AppendLine(); + + if(csd.OutOfInterruptBusyTiming > 0) + sb.AppendFormat("\tDevice can take a maximum of {0} ms when releasing from an interrupt", csd.OutOfInterruptBusyTiming * 10).AppendLine(); + + if((csd.DeviceType & 0x01) == 0x01) + sb.AppendLine("\tDevice supports 26 Mhz mode"); + if((csd.DeviceType & 0x02) == 0x02) + sb.AppendLine("\tDevice supports 52 Mhz mode"); + if((csd.DeviceType & 0x04) == 0x04) + sb.AppendLine("\tDevice supports DDR 52 Mhz mode at 1.8V or 3V"); + if((csd.DeviceType & 0x08) == 0x08) + sb.AppendLine("\tDevice supports DDR 52 Mhz mode 1.2V"); + if((csd.DeviceType & 0x10) == 0x10) + sb.AppendLine("\tDevice supports HS-200 mode (SDR 200Mhz) at 1.8V"); + if((csd.DeviceType & 0x20) == 0x20) + sb.AppendLine("\tDevice supports HS-200 mode (SDR 200Mhz) at 1.2V"); + if((csd.DeviceType & 0x40) == 0x40) + sb.AppendLine("\tDevice supports HS-400 mode (DDR 200Mhz) at 1.8V"); + if((csd.DeviceType & 0x80) == 0x80) + sb.AppendLine("\tDevice supports HS-400 mode (DDR 200Mhz) at 1.2V"); + + sb.AppendFormat("\tCSD version 1.{0} revision 1.{1}", csd.Structure, csd.Revision).AppendLine(); + + if((csd.StrobeSupport & 0x01) == 0x01) + { + sb.AppendLine("\tDevice supports enhanced strobe mode"); + if((csd.BusWidth & 0x80) == 0x80) + sb.AppendLine("\tDevice uses strobe during Data Out, CRC and CMD responses"); + else + sb.AppendLine("\tDevice uses strobe during Data Out and CRC responses"); + } + + switch(csd.BusWidth & 0x0F) + { + case 0: + sb.AppendLine("\tDevice is using 1-bit data bus"); + break; + case 1: + sb.AppendLine("\tDevice is using 4-bit data bus"); + break; + case 2: + sb.AppendLine("\tDevice is using 8-bit data bus"); + break; + case 5: + sb.AppendLine("\tDevice is using 4-bit DDR data bus"); + break; + case 6: + sb.AppendLine("\tDevice is using 8-bit DDR data bus"); + break; + default: + sb.AppendFormat("\tDevice is using unknown data bus code {0}", csd.BusWidth & 0x0F).AppendLine(); + break; + } + + if((csd.PartitionConfiguration & 0x80) == 0x80) + sb.AppendLine("\tDevice sends boot acknowledge"); + + switch((csd.PartitionConfiguration & 0x38) >> 3) + { + case 0: + sb.AppendLine("\tDevice is not boot enabled"); + break; + case 1: + sb.AppendLine("\tDevice boot partition 1 is enabled"); + break; + case 2: + sb.AppendLine("\tDevice boot partition 2 is enabled"); + break; + case 7: + sb.AppendLine("\tDevice user area is enable for boot"); + break; + default: + sb.AppendFormat("\tUnknown enabled boot partition code {0}", (csd.PartitionConfiguration & 0x38) >> 3).AppendLine(); + break; + } + + switch(csd.PartitionConfiguration & 0x07) + { + case 0: + sb.AppendLine("\tThere is no access to boot partition"); + break; + case 1: + sb.AppendLine("\tThere is read/write access to boot partition 1"); + break; + case 2: + sb.AppendLine("\tThere is read/write access to boot partition 2"); + break; + case 3: + sb.AppendLine("\tThere is read/write access to replay protected memory block"); + break; + default: + sb.AppendFormat("\tThere is access to general purpose partition {0}", (csd.PartitionConfiguration & 0x07) - 3).AppendLine(); + break; + } + + if((csd.FirmwareConfiguration & 0x01) == 0x01) + sb.AppendLine("\tFirmware updates are permanently disabled"); + + if(csd.RPMBSize > 0) + sb.AppendFormat("\tDevice has a {0} KiB replay protected memory block", csd.RPMBSize * 128).AppendLine(); + + switch(csd.NativeSectorSize) + { + case 0: + sb.AppendLine("\tDevice natively uses 512 byte sectors"); + break; + case 1: + sb.AppendLine("\tDevice natively uses 4096 byte sectors"); + break; + default: + sb.AppendFormat("\tDevice natively uses unknown sector size indicated by code {0}", csd.NativeSectorSize).AppendLine(); + break; + } + + switch(csd.SectorSizeEmulation) + { + case 0: + sb.AppendLine("\tDevice is emulating 512 byte sectors"); + break; + case 1: + sb.AppendLine("\tDevice is using natively sized sectors"); + break; + default: + sb.AppendFormat("\tDevice emulates unknown sector size indicated by code {0}", csd.NativeSectorSize).AppendLine(); + break; + } + + switch(csd.SectorSize) + { + case 0: + sb.AppendLine("\tDevice currently addresses 512 byte sectors"); + break; + case 1: + sb.AppendLine("\tDevice currently addresses 4096 byte sectors"); + break; + default: + sb.AppendFormat("\tDevice currently addresses unknown sector size indicated by code {0}", csd.NativeSectorSize).AppendLine(); + break; + } + + if((csd.CacheControl & 0x01) == 0x01) + sb.AppendLine("\tDevice's cache is enabled"); + + if((csd.CommandQueueModeEnable & 0x01) == 0x01) + sb.AppendLine("\tDevice has enabled command queuing"); + + return sb.ToString(); + } + + public static string PrettifyExtendedCSD(byte[] response) + { + return PrettifyExtendedCSD(DecodeExtendedCSD(response)); + } + } +} diff --git a/DiscImageChef.Decoders/MMC/OCR.cs b/DiscImageChef.Decoders/MMC/OCR.cs new file mode 100644 index 000000000..3141b45c8 --- /dev/null +++ b/DiscImageChef.Decoders/MMC/OCR.cs @@ -0,0 +1,168 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : OCR.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Description +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ + +using System; +using System.Text; + +namespace DiscImageChef.Decoders.MMC +{ + public class OCR + { + public bool PowerUp; + public byte AccessMode; + public bool ThreeFive; + public bool ThreeFour; + public bool ThreeThree; + public bool ThreeTwo; + public bool ThreeOne; + public bool ThreeZero; + public bool TwoNine; + public bool TwoEight; + public bool TwoSeven; + public bool TwoSix; + public bool TwoFive; + public bool TwoFour; + public bool TwoThree; + public bool TwoTwo; + public bool TwoOne; + public bool TwoZero; + public bool OneSix; + } + + public partial class Decoders + { + public static OCR DecodeOCR(uint response) + { + OCR ocr = new OCR(); + + ocr.PowerUp = (response & 0x80000000) == 0x80000000; + ocr.AccessMode = (byte)((response & 0x60000000) >> 29); + ocr.ThreeFive = (response & 0x00800000) == 0x00800000; + ocr.ThreeFour = (response & 0x00400000) == 0x00400000; + ocr.ThreeThree = (response & 0x00200000) == 0x00200000; + ocr.ThreeTwo = (response & 0x00100000) == 0x00100000; + ocr.ThreeOne = (response & 0x00080000) == 0x00080000; + ocr.ThreeZero = (response & 0x00040000) == 0x00040000; + ocr.TwoNine = (response & 0x00020000) == 0x00020000; + ocr.TwoEight = (response & 0x00010000) == 0x00010000; + ocr.TwoSeven = (response & 0x00008000) == 0x00008000; + ocr.TwoSix = (response & 0x00004000) == 0x00004000; + ocr.TwoFive = (response & 0x00002000) == 0x00002000; + ocr.TwoFour = (response & 0x00001000) == 0x00001000; + ocr.TwoThree = (response & 0x00000800) == 0x00000800; + ocr.TwoTwo = (response & 0x00000400) == 0x00000400; + ocr.TwoOne = (response & 0x00000200) == 0x00000200; + ocr.TwoZero = (response & 0x00000100) == 0x00000100; + ocr.OneSix = (response & 0x00000080) == 0x00000080; + + return ocr; + } + + public static OCR DecodeOCR(byte[] response) + { + if(response == null) + return null; + + if(response.Length != 4) + return null; + + return DecodeOCR(BitConverter.ToUInt32(response, 0)); + } + + public static string PrettifyOCR(OCR ocr) + { + if(ocr == null) + return null; + + StringBuilder sb = new StringBuilder(); + sb.AppendLine("MultiMediaCard Operation Conditions Register:"); + if(!ocr.PowerUp) + sb.AppendLine("\tDevice is powering up"); + switch(ocr.AccessMode) + { + case 0: + sb.AppendLine("\tDevice is byte addressed"); + break; + case 2: + sb.AppendLine("\tDevice is sector addressed"); + break; + default: + sb.AppendFormat("\tUnknown device access mode {0}", ocr.AccessMode).AppendLine(); + break; + } + if(ocr.ThreeFive) + sb.AppendLine("\tDevice can work with supply 3.5~3.6V"); + if(ocr.ThreeFour) + sb.AppendLine("\tDevice can work with supply 3.4~3.5V"); + if(ocr.ThreeThree) + sb.AppendLine("\tDevice can work with supply 3.3~3.4V"); + if(ocr.ThreeTwo) + sb.AppendLine("\tDevice can work with supply 3.2~3.3V"); + if(ocr.ThreeOne) + sb.AppendLine("\tDevice can work with supply 3.1~3.2V"); + if(ocr.TwoNine) + sb.AppendLine("\tDevice can work with supply 2.9~3.0V"); + if(ocr.TwoEight) + sb.AppendLine("\tDevice can work with supply 2.8~2.9V"); + if(ocr.TwoSeven) + sb.AppendLine("\tDevice can work with supply 2.7~2.8V"); + if(ocr.TwoSix) + sb.AppendLine("\tDevice can work with supply 2.6~2.7V"); + if(ocr.TwoFive) + sb.AppendLine("\tDevice can work with supply 2.5~2.6V"); + if(ocr.TwoFour) + sb.AppendLine("\tDevice can work with supply 2.4~2.5V"); + if(ocr.TwoThree) + sb.AppendLine("\tDevice can work with supply 2.3~2.4V"); + if(ocr.TwoTwo) + sb.AppendLine("\tDevice can work with supply 2.2~2.3V"); + if(ocr.TwoOne) + sb.AppendLine("\tDevice can work with supply 2.1~2.2V"); + if(ocr.TwoZero) + sb.AppendLine("\tDevice can work with supply 2.0~2.1V"); + if(ocr.OneSix) + sb.AppendLine("\tDevice can work with supply 1.65~1.95V"); + + return sb.ToString(); + } + + public static string PrettifyOCR(byte[] response) + { + return PrettifyOCR(DecodeOCR(response)); + } + + public static string PrettifyOCR(uint response) + { + return PrettifyOCR(DecodeOCR(response)); + } + } +} diff --git a/DiscImageChef.Decoders/MMC/VendorString.cs b/DiscImageChef.Decoders/MMC/VendorString.cs new file mode 100644 index 000000000..6ea840129 --- /dev/null +++ b/DiscImageChef.Decoders/MMC/VendorString.cs @@ -0,0 +1,48 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : VendorString.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Description +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ + +namespace DiscImageChef.Decoders.MMC +{ + public static class VendorString + { + public static string Prettify(byte MMCVendorID) + { + switch(MMCVendorID) + { + case 0x15: + return "Samsung"; + default: + return string.Format("Unknown manufacturer ID 0x{0:X2}", MMCVendorID); + } + } + } +} diff --git a/DiscImageChef.Decoders/SecureDigital/CID.cs b/DiscImageChef.Decoders/SecureDigital/CID.cs new file mode 100644 index 000000000..bb79503ea --- /dev/null +++ b/DiscImageChef.Decoders/SecureDigital/CID.cs @@ -0,0 +1,129 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : CID.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Description +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ + +using System; +using System.Text; + +namespace DiscImageChef.Decoders.SecureDigital +{ + public class CID + { + public byte Manufacturer; + public string ApplicationID; + public string ProductName; + public byte ProductRevision; + public uint ProductSerialNumber; + public ushort ManufacturingDate; + public byte CRC; + } + + public partial class Decoders + { + public static CID DecodeCID(uint[] response) + { + if(response == null) + return null; + + if(response.Length != 4) + return null; + + byte[] data = new byte[16]; + byte[] tmp = new byte[4]; + + tmp = BitConverter.GetBytes(response[0]); + Array.Copy(tmp, 0, data, 0, 4); + tmp = BitConverter.GetBytes(response[1]); + Array.Copy(tmp, 0, data, 4, 4); + tmp = BitConverter.GetBytes(response[2]); + Array.Copy(tmp, 0, data, 8, 4); + tmp = BitConverter.GetBytes(response[3]); + Array.Copy(tmp, 0, data, 12, 4); + + return DecodeCID(data); + } + + public static CID DecodeCID(byte[] response) + { + if(response == null) + return null; + + if(response.Length != 16) + return null; + + byte[] tmp; + + CID cid = new CID(); + cid.Manufacturer = response[0]; + tmp = new byte[2]; + Array.Copy(response, 1, tmp, 0, 2); + cid.ApplicationID = StringHandlers.CToString(tmp); + tmp = new byte[5]; + Array.Copy(response, 3, tmp, 0, 5); + cid.ProductName = StringHandlers.CToString(tmp); + cid.ProductRevision = response[8]; + cid.ProductSerialNumber = BitConverter.ToUInt32(response, 9); + cid.ManufacturingDate = (ushort)(((response[13] & 0x0F) << 4) + response[14]); + cid.CRC = (byte)((response[15] & 0xFE) >> 1); + + return cid; + } + + public static string PrettifyCID(CID cid) + { + if(cid == null) + return null; + + StringBuilder sb = new StringBuilder(); + + sb.AppendLine("SecureDigital Device Identification Register:"); + sb.AppendFormat("\tManufacturer: {0}", VendorString.Prettify(cid.Manufacturer)).AppendLine(); + sb.AppendFormat("\tApplication ID: {0}", cid.ApplicationID).AppendLine(); + sb.AppendFormat("\tProduct name: {0}", cid.ProductName).AppendLine(); + sb.AppendFormat("\tProduct revision: {0:X2}.{1:X2}", (cid.ProductRevision & 0xF0) >> 4, cid.ProductRevision & 0x0F).AppendLine(); + sb.AppendFormat("\tProduct serial number: {0}", cid.ProductSerialNumber).AppendLine(); + sb.AppendFormat("\tDevice manufactured month {0} of {1}", (cid.ManufacturingDate & 0xF00) >> 8, (cid.ManufacturingDate & 0xFF) + 2000).AppendLine(); + sb.AppendFormat("\tCID CRC: 0x{0:X2}", cid.CRC).AppendLine(); + + return sb.ToString(); + } + + public static string PrettifyCID(uint[] response) + { + return PrettifyCID(DecodeCID(response)); + } + + public static string PrettifyCID(byte[] response) + { + return PrettifyCID(DecodeCID(response)); + } + } +} diff --git a/DiscImageChef.Decoders/SecureDigital/CSD.cs b/DiscImageChef.Decoders/SecureDigital/CSD.cs new file mode 100644 index 000000000..c870b9353 --- /dev/null +++ b/DiscImageChef.Decoders/SecureDigital/CSD.cs @@ -0,0 +1,568 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : CSD.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Description +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ + +using System; +using System.Text; + +namespace DiscImageChef.Decoders.SecureDigital +{ + public class CSD + { + public byte Structure; + public byte TAAC; + public byte NSAC; + public byte Speed; + public ushort Classes; + public byte ReadBlockLength; + public bool ReadsPartialBlocks; + public bool WriteMisalignment; + public bool ReadMisalignment; + public bool DSRImplemented; + public uint Size; + public byte ReadCurrentAtVddMin; + public byte ReadCurrentAtVddMax; + public byte WriteCurrentAtVddMin; + public byte WriteCurrentAtVddMax; + public byte SizeMultiplier; + public bool EraseBlockEnable; + public byte EraseSectorSize; + public byte WriteProtectGroupSize; + public bool WriteProtectGroupEnable; + public byte WriteSpeedFactor; + public byte WriteBlockLength; + public bool WritesPartialBlocks; + public bool FileFormatGroup; + public bool Copy; + public bool PermanentWriteProtect; + public bool TemporaryWriteProtect; + public byte FileFormat; + public byte CRC; + } + + public partial class Decoders + { + public static CSD DecodeCSD(uint[] response) + { + if(response == null) + return null; + + if(response.Length != 4) + return null; + + byte[] data = new byte[16]; + byte[] tmp = new byte[4]; + + tmp = BitConverter.GetBytes(response[0]); + Array.Copy(tmp, 0, data, 0, 4); + tmp = BitConverter.GetBytes(response[1]); + Array.Copy(tmp, 0, data, 4, 4); + tmp = BitConverter.GetBytes(response[2]); + Array.Copy(tmp, 0, data, 8, 4); + tmp = BitConverter.GetBytes(response[3]); + Array.Copy(tmp, 0, data, 12, 4); + + return DecodeCSD(data); + } + + public static CSD DecodeCSD(byte[] response) + { + if(response == null) + return null; + + if(response.Length != 16) + return null; + + CSD csd = new CSD(); + + csd.Structure = (byte)((response[0] & 0xC0) >> 6); + csd.TAAC = response[1]; + csd.NSAC = response[2]; + csd.Speed = response[3]; + csd.Classes = (ushort)((response[4] << 4) + ((response[5] & 0xF0) >> 4)); + csd.ReadBlockLength = (byte)(response[5] & 0x0F); + csd.ReadsPartialBlocks = (response[6] & 0x80) == 0x80; + csd.WriteMisalignment = (response[6] & 0x40) == 0x40; + csd.ReadMisalignment = (response[6] & 0x20) == 0x20; + csd.DSRImplemented = (response[6] & 0x10) == 0x10; + if(csd.Structure == 0) + { + csd.Size = (ushort)(((response[6] & 0x03) << 10) + (response[7] << 2) + ((response[8] & 0xC0) >> 6)); + csd.ReadCurrentAtVddMin = (byte)((response[8] & 0x38) >> 3); + csd.ReadCurrentAtVddMax = (byte)(response[8] & 0x07); + csd.WriteCurrentAtVddMin = (byte)((response[9] & 0xE0) >> 5); + csd.WriteCurrentAtVddMax = (byte)((response[9] & 0x1C) >> 2); + csd.SizeMultiplier = (byte)(((response[9] & 0x03) << 1) + ((response[10] & 0x80) >> 7)); + } + else + csd.Size = (uint)(((response[7] & 0x3F) << 16) + (response[8] << 8) + response[9]); + csd.EraseBlockEnable = (response[10] & 0x40) == 0x40; + csd.EraseSectorSize = (byte)(((response[10] & 0x3F) << 1) + ((response[11] & 0x80) >> 7)); + csd.WriteProtectGroupSize = ((byte)(response[11] & 0x7F)); + csd.WriteProtectGroupEnable = (response[12] & 0x80) == 0x80; + csd.WriteSpeedFactor = (byte)((response[12] & 0x1C) >> 2); + csd.WriteBlockLength = (byte)(((response[12] & 0x03) << 2) + ((response[13] & 0xC0) >> 6)); + csd.WritesPartialBlocks = (response[13] & 0x20) == 0x20; + csd.FileFormatGroup = (response[14] & 0x80) == 0x80; + csd.Copy = (response[14] & 0x40) == 0x40; + csd.PermanentWriteProtect = (response[14] & 0x20) == 0x20; + csd.TemporaryWriteProtect = (response[14] & 0x10) == 0x10; + csd.FileFormat = (byte)((response[14] & 0x0C) >> 2); + csd.CRC = (byte)((response[15] & 0xFE) >> 1); + + return csd; + } + + public static string PrettifyCSD(CSD csd) + { + if(csd == null) + return null; + + double unitFactor = 0; + double multiplier = 0; + double result = 0; + string unit = ""; + + StringBuilder sb = new StringBuilder(); + sb.AppendLine("SecureDigital Device Specific Data Register:"); + switch(csd.Structure) + { + case 0: + sb.AppendLine("\tRegister version 1.0"); + break; + case 1: + sb.AppendLine("\tRegister version 2.0"); + break; + } + + switch(csd.TAAC & 0x07) + { + case 0: + unit = "ns"; + unitFactor = 1; + break; + case 1: + unit = "ns"; + unitFactor = 10; + break; + case 2: + unit = "ns"; + unitFactor = 100; + break; + case 3: + unit = "μs"; + unitFactor = 1; + break; + case 4: + unit = "μs"; + unitFactor = 10; + break; + case 5: + unit = "μs"; + unitFactor = 100; + break; + case 6: + unit = "ms"; + unitFactor = 1; + break; + case 7: + unit = "ms"; + unitFactor = 10; + break; + } + + switch((csd.TAAC & 0x78) >> 3) + { + case 0: + multiplier = 0; + break; + case 1: + multiplier = 1; + break; + case 2: + multiplier = 1.2; + break; + case 3: + multiplier = 1.3; + break; + case 4: + multiplier = 1.5; + break; + case 5: + multiplier = 2; + break; + case 6: + multiplier = 2.5; + break; + case 7: + multiplier = 3; + break; + case 8: + multiplier = 3.5; + break; + case 9: + multiplier = 4; + break; + case 10: + multiplier = 4.5; + break; + case 11: + multiplier = 5; + break; + case 12: + multiplier = 5.5; + break; + case 13: + multiplier = 6; + break; + case 14: + multiplier = 7; + break; + case 15: + multiplier = 8; + break; + } + result = unitFactor * multiplier; + sb.AppendFormat("\tAsynchronous data access time is {0}{1}", result, unit).AppendLine(); + + sb.AppendFormat("\tClock dependent part of data access is {0} clock cycles", csd.NSAC * 100).AppendLine(); + + unit = "MBit/s"; + switch(csd.Speed & 0x07) + { + case 0: + unitFactor = 0.1; + break; + case 1: + unitFactor = 1; + break; + case 2: + unitFactor = 10; + break; + case 3: + unitFactor = 100; + break; + default: + unit = "unknown"; + unitFactor = 0; + break; + } + + switch((csd.Speed & 0x78) >> 3) + { + case 0: + multiplier = 0; + break; + case 1: + multiplier = 1; + break; + case 2: + multiplier = 1.2; + break; + case 3: + multiplier = 1.3; + break; + case 4: + multiplier = 1.5; + break; + case 5: + multiplier = 2; + break; + case 6: + multiplier = 2.6; + break; + case 7: + multiplier = 3; + break; + case 8: + multiplier = 3.5; + break; + case 9: + multiplier = 4; + break; + case 10: + multiplier = 4.5; + break; + case 11: + multiplier = 5.2; + break; + case 12: + multiplier = 5.5; + break; + case 13: + multiplier = 6; + break; + case 14: + multiplier = 7; + break; + case 15: + multiplier = 8; + break; + } + result = unitFactor * multiplier; + sb.AppendFormat("\tDevice's transfer speed: {0}{1}", result, unit).AppendLine(); + + unit = ""; + for(int cl = 0, mask = 1; cl <= 11; cl++, mask <<= 1) + { + if((csd.Classes & mask) == mask) + unit += string.Format(" {0}", cl); + } + + sb.AppendFormat("\tDevice support command classes {0}", unit).AppendLine(); + sb.AppendFormat("\tRead block length is {0} bytes", Math.Pow(2, csd.ReadBlockLength)).AppendLine(); + + if(csd.ReadsPartialBlocks) + sb.AppendLine("\tDevice allows reading partial blocks"); + + if(csd.WriteMisalignment) + sb.AppendLine("\tWrite commands can cross physical block boundaries"); + if(csd.ReadMisalignment) + sb.AppendLine("\tRead commands can cross physical block boundaries"); + + if(csd.DSRImplemented) + sb.AppendLine("\tDevice implements configurable driver stage"); + + if(csd.Structure == 0) + { + result = (csd.Size + 1) * Math.Pow(2, csd.SizeMultiplier + 2); + sb.AppendFormat("\tDevice has {0} blocks", (int)result).AppendLine(); + + result = (csd.Size + 1) * Math.Pow(2, csd.SizeMultiplier + 2) * Math.Pow(2, csd.ReadBlockLength); + if(result > 1073741824) + sb.AppendFormat("\tDevice has {0} GiB", result / 1073741824.0); + else if(result > 1048576) + sb.AppendFormat("\tDevice has {0} MiB", result / 1048576.0); + else if(result > 1024) + sb.AppendFormat("\tDevice has {0} KiB", result / 1024.0); + else + sb.AppendFormat("\tDevice has {0} bytes", result); + } + else + { + sb.AppendFormat("\tDevice has {0} blocks", (csd.Size + 1) * 1024); + result = (csd.Size + 1) * 1024 * 512; + if(result > 1099511627776) + sb.AppendFormat("\tDevice has {0} TiB", result / 1099511627776.0); + else if(result > 1073741824) + sb.AppendFormat("\tDevice has {0} GiB", result / 1073741824.0); + else if(result > 1048576) + sb.AppendFormat("\tDevice has {0} MiB", result / 1048576.0); + else if(result > 1024) + sb.AppendFormat("\tDevice has {0} KiB", result / 1024.0); + else + sb.AppendFormat("\tDevice has {0} bytes", result); + } + + if(csd.Structure == 0) + { + switch(csd.ReadCurrentAtVddMin & 0x07) + { + case 0: + sb.AppendLine("\tDevice uses a maximum of 0.5mA for reading at minimum voltage"); + break; + case 1: + sb.AppendLine("\tDevice uses a maximum of 1mA for reading at minimum voltage"); + break; + case 2: + sb.AppendLine("\tDevice uses a maximum of 5mA for reading at minimum voltage"); + break; + case 3: + sb.AppendLine("\tDevice uses a maximum of 10mA for reading at minimum voltage"); + break; + case 4: + sb.AppendLine("\tDevice uses a maximum of 25mA for reading at minimum voltage"); + break; + case 5: + sb.AppendLine("\tDevice uses a maximum of 35mA for reading at minimum voltage"); + break; + case 6: + sb.AppendLine("\tDevice uses a maximum of 60mA for reading at minimum voltage"); + break; + case 7: + sb.AppendLine("\tDevice uses a maximum of 100mA for reading at minimum voltage"); + break; + } + + switch(csd.ReadCurrentAtVddMax & 0x07) + { + case 0: + sb.AppendLine("\tDevice uses a maximum of 1mA for reading at maximum voltage"); + break; + case 1: + sb.AppendLine("\tDevice uses a maximum of 5mA for reading at maximum voltage"); + break; + case 2: + sb.AppendLine("\tDevice uses a maximum of 10mA for reading at maximum voltage"); + break; + case 3: + sb.AppendLine("\tDevice uses a maximum of 25mA for reading at maximum voltage"); + break; + case 4: + sb.AppendLine("\tDevice uses a maximum of 35mA for reading at maximum voltage"); + break; + case 5: + sb.AppendLine("\tDevice uses a maximum of 45mA for reading at maximum voltage"); + break; + case 6: + sb.AppendLine("\tDevice uses a maximum of 80mA for reading at maximum voltage"); + break; + case 7: + sb.AppendLine("\tDevice uses a maximum of 200mA for reading at maximum voltage"); + break; + } + + switch(csd.WriteCurrentAtVddMin & 0x07) + { + case 0: + sb.AppendLine("\tDevice uses a maximum of 0.5mA for writing at minimum voltage"); + break; + case 1: + sb.AppendLine("\tDevice uses a maximum of 1mA for writing at minimum voltage"); + break; + case 2: + sb.AppendLine("\tDevice uses a maximum of 5mA for writing at minimum voltage"); + break; + case 3: + sb.AppendLine("\tDevice uses a maximum of 10mA for writing at minimum voltage"); + break; + case 4: + sb.AppendLine("\tDevice uses a maximum of 25mA for writing at minimum voltage"); + break; + case 5: + sb.AppendLine("\tDevice uses a maximum of 35mA for writing at minimum voltage"); + break; + case 6: + sb.AppendLine("\tDevice uses a maximum of 60mA for writing at minimum voltage"); + break; + case 7: + sb.AppendLine("\tDevice uses a maximum of 100mA for writing at minimum voltage"); + break; + } + + switch(csd.WriteCurrentAtVddMax & 0x07) + { + case 0: + sb.AppendLine("\tDevice uses a maximum of 1mA for writing at maximum voltage"); + break; + case 1: + sb.AppendLine("\tDevice uses a maximum of 5mA for writing at maximum voltage"); + break; + case 2: + sb.AppendLine("\tDevice uses a maximum of 10mA for writing at maximum voltage"); + break; + case 3: + sb.AppendLine("\tDevice uses a maximum of 25mA for writing at maximum voltage"); + break; + case 4: + sb.AppendLine("\tDevice uses a maximum of 35mA for writing at maximum voltage"); + break; + case 5: + sb.AppendLine("\tDevice uses a maximum of 45mA for writing at maximum voltage"); + break; + case 6: + sb.AppendLine("\tDevice uses a maximum of 80mA for writing at maximum voltage"); + break; + case 7: + sb.AppendLine("\tDevice uses a maximum of 200mA for writing at maximum voltage"); + break; + } + + + if(csd.EraseBlockEnable) + sb.AppendLine("\tDevice can erase multiple blocks"); + + sb.AppendFormat("\tDevice must erase a minimum of {0} blocks at a time", Convert.ToUInt32(string.Format("{0:X}", csd.EraseSectorSize)) + 1).AppendLine(); + + + if(csd.WriteProtectGroupEnable) + { + sb.AppendLine("\tDevice can write protect regions"); + unitFactor = Convert.ToDouble(string.Format("{0:X}", csd.WriteProtectGroupSize)); + sb.AppendFormat("\tDevice can write protect a minimum of {0} blocks at a time", (int)(result + 1)).AppendLine(); + } + else + sb.AppendLine("\tDevice can't write protect regions"); + } + + sb.AppendFormat("\tWriting is {0} times slower than reading", Math.Pow(2, csd.WriteSpeedFactor)).AppendLine(); + + sb.AppendFormat("\tWrite block length is {0} bytes", Math.Pow(2, csd.WriteBlockLength)).AppendLine(); + + if(csd.WritesPartialBlocks) + sb.AppendLine("\tDevice allows writing partial blocks"); + + if(!csd.Copy) + sb.AppendLine("\tDevice contents are original"); + + if(csd.PermanentWriteProtect) + sb.AppendLine("\tDevice is permanently write protected"); + + if(csd.TemporaryWriteProtect) + sb.AppendLine("\tDevice is temporarily write protected"); + + if(!csd.FileFormatGroup) + { + switch(csd.FileFormat) + { + case 0: + sb.AppendLine("\tDevice is formatted like a hard disk"); + break; + case 1: + sb.AppendLine("\tDevice is formatted like a floppy disk using Microsoft FAT"); + break; + case 2: + sb.AppendLine("\tDevice uses Universal File Format"); + break; + default: + sb.AppendFormat("\tDevice uses unknown file format code {0}", csd.FileFormat).AppendLine(); + break; + } + } + else + sb.AppendFormat("\tDevice uses unknown file format code {0} and file format group 1", csd.FileFormat).AppendLine(); + + sb.AppendFormat("\tCSD CRC: 0x{0:X2}", csd.CRC).AppendLine(); + + return sb.ToString(); + } + + public static string PrettifyCSD(uint[] response) + { + return PrettifyCSD(DecodeCSD(response)); + } + + public static string PrettifyCSD(byte[] response) + { + return PrettifyCSD(DecodeCSD(response)); + } + } +} diff --git a/DiscImageChef.Decoders/SecureDigital/OCR.cs b/DiscImageChef.Decoders/SecureDigital/OCR.cs new file mode 100644 index 000000000..c24cb5b66 --- /dev/null +++ b/DiscImageChef.Decoders/SecureDigital/OCR.cs @@ -0,0 +1,138 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : OCR.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Description +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ + +using System; +using System.Text; + +namespace DiscImageChef.Decoders.SecureDigital +{ + public class OCR + { + public bool PowerUp; + public bool CCS; + public bool UHS; + public bool OneEight; + public bool ThreeFive; + public bool ThreeFour; + public bool ThreeThree; + public bool ThreeTwo; + public bool ThreeOne; + public bool ThreeZero; + public bool TwoNine; + public bool TwoEight; + public bool TwoSeven; + public bool LowPower; + } + + public partial class Decoders + { + public static OCR DecodeOCR(uint response) + { + OCR ocr = new OCR(); + + ocr.PowerUp = (response & 0x80000000) == 0x80000000; + ocr.CCS = (response & 0x40000000) == 0x40000000; + ocr.PowerUp = (response & 0x20000000) == 0x20000000; + ocr.OneEight = (response & 0x01000000) == 0x01000000; + ocr.ThreeFive = (response & 0x00800000) == 0x00800000; + ocr.ThreeFour = (response & 0x00400000) == 0x00400000; + ocr.ThreeThree = (response & 0x00200000) == 0x00200000; + ocr.ThreeTwo = (response & 0x00100000) == 0x00100000; + ocr.ThreeOne = (response & 0x00080000) == 0x00080000; + ocr.ThreeZero = (response & 0x00040000) == 0x00040000; + ocr.TwoNine = (response & 0x00020000) == 0x00020000; + ocr.TwoEight = (response & 0x00010000) == 0x00010000; + ocr.TwoSeven = (response & 0x00008000) == 0x00008000; + ocr.LowPower = (response & 0x00000080) == 0x00000080; + + return ocr; + } + + public static OCR DecodeOCR(byte[] response) + { + if(response == null) + return null; + + if(response.Length != 4) + return null; + + return DecodeOCR(BitConverter.ToUInt32(response, 0)); + } + + public static string PrettifyOCR(OCR ocr) + { + if(ocr == null) + return null; + + StringBuilder sb = new StringBuilder(); + sb.AppendLine("SecureDigital Operation Conditions Register:"); + if(!ocr.PowerUp) + sb.AppendLine("\tDevice is powering up"); + if(ocr.CCS) + sb.AppendLine("\tDevice is SDHC, SDXC or higher"); + if(ocr.UHS) + sb.AppendLine("\tDevice is UHS-II or higher"); + if(ocr.ThreeFive) + sb.AppendLine("\tDevice can work with supply 3.5~3.6V"); + if(ocr.ThreeFour) + sb.AppendLine("\tDevice can work with supply 3.4~3.5V"); + if(ocr.ThreeThree) + sb.AppendLine("\tDevice can work with supply 3.3~3.4V"); + if(ocr.ThreeTwo) + sb.AppendLine("\tDevice can work with supply 3.2~3.3V"); + if(ocr.ThreeOne) + sb.AppendLine("\tDevice can work with supply 3.1~3.2V"); + if(ocr.TwoNine) + sb.AppendLine("\tDevice can work with supply 2.9~3.0V"); + if(ocr.TwoEight) + sb.AppendLine("\tDevice can work with supply 2.8~2.9V"); + if(ocr.TwoSeven) + sb.AppendLine("\tDevice can work with supply 2.7~2.8V"); + if(ocr.OneEight) + sb.AppendLine("\tDevice can switch to work with 1.8V supply"); + if(ocr.LowPower) + sb.AppendLine("\tDevice is in low power mode"); + + return sb.ToString(); + } + + public static string PrettifyOCR(byte[] response) + { + return PrettifyOCR(DecodeOCR(response)); + } + + public static string PrettifyOCR(uint response) + { + return PrettifyOCR(DecodeOCR(response)); + } + } +} diff --git a/DiscImageChef.Decoders/SecureDigital/SCR.cs b/DiscImageChef.Decoders/SecureDigital/SCR.cs new file mode 100644 index 000000000..f5e33a0d2 --- /dev/null +++ b/DiscImageChef.Decoders/SecureDigital/SCR.cs @@ -0,0 +1,176 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : SCR.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Description +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ +using System; +using System.Text; + +namespace DiscImageChef.Decoders.SecureDigital +{ + public class SCR + { + public byte Structure; + public byte Spec; + public bool DataStatusAfterErase; + public byte Security; + public byte BusWidth; + public bool Spec3; + public byte ExtendedSecurity; + public bool Spec4; + public byte SpecX; + public byte CommandSupport; + public byte[] ManufacturerReserved; + } + + public partial class Decoders + { + public static SCR DecodeSCR(uint[] response) + { + if(response == null) + return null; + + if(response.Length != 2) + return null; + + byte[] data = new byte[8]; + byte[] tmp = new byte[4]; + + tmp = BitConverter.GetBytes(response[0]); + Array.Copy(tmp, 0, data, 0, 4); + tmp = BitConverter.GetBytes(response[1]); + Array.Copy(tmp, 0, data, 4, 4); + + return DecodeSCR(data); + } + + public static SCR DecodeSCR(byte[] response) + { + if(response == null) + return null; + + if(response.Length != 8) + return null; + + SCR scr = new SCR(); + scr.Structure = (byte)((response[0] & 0xF0) >> 4); + scr.Spec = (byte)(response[0] & 0x0F); + scr.DataStatusAfterErase = (response[1] & 0x80) == 0x80; + scr.Security = (byte)((response[1] & 0x70) >> 4); + scr.BusWidth = (byte)(response[1] & 0x0F); + scr.Spec3 = (response[2] & 0x80) == 0x80; + scr.ExtendedSecurity = (byte)((response[2] & 0x78) >> 3); + scr.Spec4 = (response[2] & 0x04) == 0x04; + scr.SpecX = (byte)(((response[2] & 0x03) << 2) + ((response[3] & 0xC0) >> 6)); + scr.CommandSupport = (byte)(response[3] & 0x0F); + scr.ManufacturerReserved = new byte[4]; + Array.Copy(response, 4, scr.ManufacturerReserved, 0, 4); + + return scr; + } + + public static string PrettifySCR(SCR scr) + { + if(scr == null) + return null; + + StringBuilder sb = new StringBuilder(); + sb.AppendLine("SecureDigital Device Configuration Register:"); + + if(scr.Structure != 0) + sb.AppendFormat("\tUnknown register version {0}", scr.Structure).AppendLine(); + + if(scr.Spec == 0 && scr.Spec3 == false && scr.Spec4 == false && scr.SpecX == 0) + sb.AppendLine("\tDevice follows SecureDigital Physical Layer Specification version 1.0x"); + else if(scr.Spec == 1 && scr.Spec3 == false && scr.Spec4 == false && scr.SpecX == 0) + sb.AppendLine("\tDevice follows SecureDigital Physical Layer Specification version 1.10"); + else if(scr.Spec == 2 && scr.Spec3 == false && scr.Spec4 == false && scr.SpecX == 0) + sb.AppendLine("\tDevice follows SecureDigital Physical Layer Specification version 2.00"); + else if(scr.Spec == 2 && scr.Spec3 == true && scr.Spec4 == false && scr.SpecX == 0) + sb.AppendLine("\tDevice follows SecureDigital Physical Layer Specification version 3.0x"); + else if(scr.Spec == 2 && scr.Spec3 == true && scr.Spec4 == true && scr.SpecX == 0) + sb.AppendLine("\tDevice follows SecureDigital Physical Layer Specification version 4.xx"); + else if(scr.Spec == 2 && scr.Spec3 == true && scr.SpecX == 1) + sb.AppendLine("\tDevice follows SecureDigital Physical Layer Specification version 5.xx"); + else + sb.AppendFormat("\tDevice follows SecureDigital Physical Layer Specification with unknown version {0}.{1}.{2}.{3}", + scr.Spec, scr.Spec3, scr.Spec4, scr.SpecX).AppendLine(); + switch(scr.Security) + { + case 0: + sb.AppendLine("\tDevice does not support CPRM"); + break; + case 1: + sb.AppendLine("\tDevice does not use CPRM"); + break; + case 2: + sb.AppendLine("\tDevice uses CPRM according to specification version 1.01"); + break; + case 3: + sb.AppendLine("\tDevice uses CPRM according to specification version 2.00"); + break; + case 4: + sb.AppendLine("\tDevice uses CPRM according to specification version 3.xx"); + break; + default: + sb.AppendFormat("\tDevice uses unknown CPRM specification with code {0}", scr.Security).AppendLine(); + break; + } + + if((scr.BusWidth & 0x01) == 0x01) + sb.AppendLine("\tDevice supports 1-bit data bus"); + if((scr.BusWidth & 0x04) == 0x04) + sb.AppendLine("\tDevice supports 4-bit data bus"); + + if(scr.ExtendedSecurity != 0) + sb.AppendLine("\tDevice supports extended security"); + + if((scr.CommandSupport & 0x08) == 0x08) + sb.AppendLine("\tDevice supports extension register multi-block commands"); + if((scr.CommandSupport & 0x04) == 0x04) + sb.AppendLine("\tDevice supports extension register single-block commands"); + if((scr.CommandSupport & 0x02) == 0x02) + sb.AppendLine("\tDevice supports set block count command"); + if((scr.CommandSupport & 0x01) == 0x01) + sb.AppendLine("\tDevice supports speed class control command"); + + return sb.ToString(); + } + + public static string PrettifySCR(uint[] response) + { + return PrettifySCR(DecodeSCR(response)); + } + + public static string PrettifySCR(byte[] response) + { + return PrettifySCR(DecodeSCR(response)); + } + } +} diff --git a/DiscImageChef.Decoders/SecureDigital/VendorString.cs b/DiscImageChef.Decoders/SecureDigital/VendorString.cs new file mode 100644 index 000000000..2897895b4 --- /dev/null +++ b/DiscImageChef.Decoders/SecureDigital/VendorString.cs @@ -0,0 +1,46 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : VendorString.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Description +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ + +namespace DiscImageChef.Decoders.SecureDigital +{ + public static class VendorString + { + public static string Prettify(byte SDVendorID) + { + switch(SDVendorID) + { + default: + return string.Format("Unknown manufacturer ID 0x{0:X2}", SDVendorID); + } + } + } +} diff --git a/DiscImageChef.Devices/ChangeLog b/DiscImageChef.Devices/ChangeLog index 8e8bf0a6f..fa1225d66 100644 --- a/DiscImageChef.Devices/ChangeLog +++ b/DiscImageChef.Devices/ChangeLog @@ -1,3 +1,19 @@ +2016-10-22 Natalia Portillo + + * Enums.cs: + * Command.cs: + * Enums.cs: + * Extern.cs: + * Command.cs: + * Structs.cs: + * Commands.cs: + * Constructor.cs: + * MMC.cs: + * DiscImageChef.Devices.csproj: + * SecureDigital.cs: Add MMC/SecureDigital device support. Not + yet used because of a bad implementation of SEND_CSD and + SEND_CID commands (TODO). + 2016-10-17 Natalia Portillo * Variables.cs: diff --git a/DiscImageChef.Devices/Command.cs b/DiscImageChef.Devices/Command.cs index c5e7138d9..dc9a1f880 100644 --- a/DiscImageChef.Devices/Command.cs +++ b/DiscImageChef.Devices/Command.cs @@ -219,6 +219,34 @@ namespace DiscImageChef.Devices throw new InvalidOperationException(string.Format("Platform {0} not yet supported.", ptID)); } } + + public static int SendMmcCommand(object fd, MmcCommands command, bool write, bool isApplication, + MmcFlags flags, uint argument, uint blockSize, uint blocks, + ref byte[] buffer, out uint[] response, out double duration, out bool sense, uint timeout = 0) + { + Interop.PlatformID ptID = DetectOS.GetRealPlatformID(); + + return SendMmcCommand(ptID, (int)fd, command, write, isApplication, flags, argument, blockSize, blocks, ref buffer, out response, out duration, out sense, timeout); + } + + public static int SendMmcCommand(Interop.PlatformID ptID, object fd, MmcCommands command, bool write, bool isApplication, + MmcFlags flags, uint argument, uint blockSize, uint blocks, ref byte[] buffer, + out uint[] response, out double duration, out bool sense, uint timeout = 0) + { + switch(ptID) + { + case Interop.PlatformID.Win32NT: + { + throw new NotImplementedException(); + } + case Interop.PlatformID.Linux: + { + return Linux.Command.SendMmcCommand((int)fd, command, write, isApplication, flags, argument, blockSize, blocks, ref buffer, out response, out duration, out sense, timeout); + } + default: + throw new InvalidOperationException(string.Format("Platform {0} not yet supported.", ptID)); + } + } } } diff --git a/DiscImageChef.Devices/Device/Commands.cs b/DiscImageChef.Devices/Device/Commands.cs index d01f617fd..775483177 100644 --- a/DiscImageChef.Devices/Device/Commands.cs +++ b/DiscImageChef.Devices/Device/Commands.cs @@ -114,6 +114,30 @@ namespace DiscImageChef.Devices return Command.SendAtaCommand(platformID, fd, registers, out errorRegisters, protocol, transferRegister, ref buffer, timeout, transferBlocks, out duration, out sense); } + + /// + /// Sends a MMC/SD command to this device + /// + /// The result of the command. + /// MMC/SD opcode + /// Buffer for MMC/SD command response + /// Timeout in seconds + /// Time it took to execute the command in milliseconds + /// True if MMC/SD returned non-OK status + /// True if data is sent from host to card + /// True if command should be preceded with CMD55 + /// Flags indicating kind and place of response + /// How many blocks to transfer + /// Command argument + /// Response registers + /// Size of block in bytes + public int SendMmcCommand(MmcCommands command, bool write, bool isApplication, MmcFlags flags, + uint argument, uint blockSize, uint blocks, ref byte[] buffer, out uint[] response, + out double duration, out bool sense, uint timeout = 0) + { + return Command.SendMmcCommand(platformID, fd, command, write, isApplication, flags, argument, blockSize, blocks, + ref buffer, out response, out duration, out sense, timeout); + } } } diff --git a/DiscImageChef.Devices/Device/Constructor.cs b/DiscImageChef.Devices/Device/Constructor.cs index b1d3a77bb..22d2400a2 100644 --- a/DiscImageChef.Devices/Device/Constructor.cs +++ b/DiscImageChef.Devices/Device/Constructor.cs @@ -90,6 +90,41 @@ namespace DiscImageChef.Devices type = DeviceType.Unknown; scsiType = Decoders.SCSI.PeripheralDeviceTypes.UnknownDevice; + // TODO: This is getting error -110 in Linux. Apparently I should set device to standby, request CID/CSD, put device to transition. However I can't get it right now. + /* + try + { + byte[] csdBuf; + byte[] scrBuf; + uint[] mmcResponse; + double mmcDuration; + + bool mmcSense = ReadCID(out csdBuf, out mmcResponse, 0, out mmcDuration); + + if(!mmcSense) + { + mmcSense = ReadSCR(out scrBuf, out mmcResponse, 0, out mmcDuration); + + if(!mmcSense) + type = DeviceType.SecureDigital; + else + type = DeviceType.MMC; + + manufacturer = "To be filled manufacturer"; + model = "To be filled model"; + revision = "To be filled revision"; + serial = "To be filled serial"; + scsiType = Decoders.SCSI.PeripheralDeviceTypes.DirectAccess; + removable = false; + return; + } + else + System.Console.WriteLine("Error {0}: {1}", error, lastError); + } + catch(NotImplementedException) { } + catch(InvalidOperationException) { } + */ + AtaErrorRegistersCHS errorRegisters; byte[] ataBuf; @@ -288,7 +323,6 @@ namespace DiscImageChef.Devices pcmcia = false; #endregion PCMCIA - if(!scsiSense) { Decoders.SCSI.Inquiry.SCSIInquiry? Inquiry = Decoders.SCSI.Inquiry.Decode(inqBuf); diff --git a/DiscImageChef.Devices/Device/MmcCommands/MMC.cs b/DiscImageChef.Devices/Device/MmcCommands/MMC.cs new file mode 100644 index 000000000..cdbb08b80 --- /dev/null +++ b/DiscImageChef.Devices/Device/MmcCommands/MMC.cs @@ -0,0 +1,151 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : MMC.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Description +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ + +using DiscImageChef.Console; + +namespace DiscImageChef.Devices +{ + public partial class Device + { + public bool ReadCSD(out byte[] buffer, out uint[] response, uint timeout, out double duration) + { + buffer = new byte[16]; + bool sense = false; + + lastError = SendMmcCommand(MmcCommands.SendCSD, false, false, MmcFlags.ResponseSPI_R1 | MmcFlags.Response_R1 | MmcFlags.CommandADTC, + 0, 16, 1, ref buffer, out response, out duration, out sense, timeout); + error = lastError != 0; + + DicConsole.DebugWriteLine("MMC Device", "SEND_CSD took {0} ms.", duration); + + return sense; + } + + public bool ReadCID(out byte[] buffer, out uint[] response, uint timeout, out double duration) + { + buffer = new byte[16]; + bool sense = false; + + lastError = SendMmcCommand(MmcCommands.SendCID, false, false, MmcFlags.ResponseSPI_R1 | MmcFlags.Response_R1 | MmcFlags.CommandADTC, + 0, 16, 1, ref buffer, out response, out duration, out sense, timeout); + error = lastError != 0; + + DicConsole.DebugWriteLine("MMC Device", "SEND_CID took {0} ms.", duration); + + return sense; + } + + public bool ReadOCR(out byte[] buffer, out uint[] response, uint timeout, out double duration) + { + buffer = new byte[4]; + bool sense = false; + + lastError = SendMmcCommand(MmcCommands.SendOpCond, false, true, MmcFlags.ResponseSPI_R3 | MmcFlags.Response_R3 | MmcFlags.CommandBCR, + 0, 4, 1, ref buffer, out response, out duration, out sense, timeout); + error = lastError != 0; + + DicConsole.DebugWriteLine("SecureDigital Device", "SEND_OP_COND took {0} ms.", duration); + + return sense; + } + + public bool ReadExtendedCSD(out byte[] buffer, out uint[] response, uint timeout, out double duration) + { + buffer = new byte[512]; + bool sense = false; + + lastError = SendMmcCommand(MmcCommands.SendExtCSD, false, false, MmcFlags.ResponseSPI_R1 | MmcFlags.Response_R1 | MmcFlags.CommandADTC, + 0, 512, 1, ref buffer, out response, out duration, out sense, timeout); + error = lastError != 0; + + DicConsole.DebugWriteLine("MMC Device", "SEND_EXT_CSD took {0} ms.", duration); + + return sense; + } + + public bool SetBlockLength(uint length, out uint[] response, uint timeout, out double duration) + { + byte[] buffer = new byte[0]; + bool sense = false; + + lastError = SendMmcCommand(MmcCommands.SetBlocklen, false, false, MmcFlags.ResponseSPI_R1 | MmcFlags.Response_R1 | MmcFlags.CommandAC, + length, 0, 0, ref buffer, out response, out duration, out sense, timeout); + error = lastError != 0; + + DicConsole.DebugWriteLine("MMC Device", "SET_BLOCKLEN took {0} ms.", duration); + + return sense; + } + + public bool Read(out byte[] buffer, out uint[] response, uint lba, uint blockSize, uint transferLength, bool byteAddressed, uint timeout, out double duration) + { + buffer = new byte[transferLength * blockSize]; + bool sense = false; + uint address; + if(byteAddressed) + address = lba * blockSize; + else + address = lba; + + MmcCommands command; + if(transferLength > 1) + command = MmcCommands.ReadMultipleBlock; + else + command = MmcCommands.ReadSingleBlock; + + lastError = SendMmcCommand(command, false, false, MmcFlags.ResponseSPI_R1 | MmcFlags.Response_R1 | MmcFlags.CommandADTC, + address, blockSize, transferLength, ref buffer, out response, out duration, out sense, timeout); + error = lastError != 0; + + if(transferLength > 1) + DicConsole.DebugWriteLine("MMC Device", "READ_MULTIPLE_BLOCK took {0} ms.", duration); + else + DicConsole.DebugWriteLine("MMC Device", "READ_SINGLE_BLOCK took {0} ms.", duration); + + return sense; + } + + public bool ReadStatus(out byte[] buffer, out uint[] response, uint timeout, out double duration) + { + buffer = new byte[4]; + bool sense = false; + + lastError = SendMmcCommand(MmcCommands.SendStatus, false, true, MmcFlags.ResponseSPI_R1 | MmcFlags.Response_R1 | MmcFlags.CommandADTC, + 0, 4, 1, ref buffer, out response, out duration, out sense, timeout); + error = lastError != 0; + + DicConsole.DebugWriteLine("SecureDigital Device", "SEND_STATUS took {0} ms.", duration); + + return sense; + } + } +} diff --git a/DiscImageChef.Devices/Device/MmcCommands/SecureDigital.cs b/DiscImageChef.Devices/Device/MmcCommands/SecureDigital.cs new file mode 100644 index 000000000..b28cf7c0b --- /dev/null +++ b/DiscImageChef.Devices/Device/MmcCommands/SecureDigital.cs @@ -0,0 +1,81 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : SecureDigital.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Description +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ + +using DiscImageChef.Console; + +namespace DiscImageChef.Devices +{ + public partial class Device + { + public bool ReadSDStatus(out byte[] buffer, out uint[] response, uint timeout, out double duration) + { + buffer = new byte[64]; + bool sense = false; + + lastError = SendMmcCommand((MmcCommands)SecureDigitalCommands.SendStatus, false, true, MmcFlags.ResponseSPI_R1 | MmcFlags.Response_R1 | MmcFlags.CommandADTC, + 0, 64, 1, ref buffer, out response, out duration, out sense, timeout); + error = lastError != 0; + + DicConsole.DebugWriteLine("SecureDigital Device", "SD_STATUS took {0} ms.", duration); + + return sense; + } + + public bool ReadSDOCR(out byte[] buffer, out uint[] response, uint timeout, out double duration) + { + buffer = new byte[4]; + bool sense = false; + + lastError = SendMmcCommand((MmcCommands)SecureDigitalCommands.SendOperatingCondition, false, true, MmcFlags.ResponseSPI_R3 | MmcFlags.Response_R3 | MmcFlags.CommandBCR, + 0, 4, 1, ref buffer, out response, out duration, out sense, timeout); + error = lastError != 0; + + DicConsole.DebugWriteLine("SecureDigital Device", "SD_SEND_OP_COND took {0} ms.", duration); + + return sense; + } + + public bool ReadSCR(out byte[] buffer, out uint[] response, uint timeout, out double duration) + { + buffer = new byte[8]; + bool sense = false; + + lastError = SendMmcCommand((MmcCommands)SecureDigitalCommands.SendSCR, false, true, MmcFlags.ResponseSPI_R1 | MmcFlags.Response_R1 | MmcFlags.CommandADTC, + 0, 8, 1, ref buffer, out response, out duration, out sense, timeout); + error = lastError != 0; + + DicConsole.DebugWriteLine("SecureDigital Device", "SEND_SCR took {0} ms.", duration); + + return sense; + } + } +} diff --git a/DiscImageChef.Devices/DiscImageChef.Devices.csproj b/DiscImageChef.Devices/DiscImageChef.Devices.csproj index efe67f99a..81cdf6beb 100644 --- a/DiscImageChef.Devices/DiscImageChef.Devices.csproj +++ b/DiscImageChef.Devices/DiscImageChef.Devices.csproj @@ -76,6 +76,8 @@ + + @@ -85,6 +87,7 @@ + @@ -92,8 +95,8 @@ - - + + diff --git a/DiscImageChef.Devices/Enums.cs b/DiscImageChef.Devices/Enums.cs index 76565b865..0c6af8897 100644 --- a/DiscImageChef.Devices/Enums.cs +++ b/DiscImageChef.Devices/Enums.cs @@ -3455,5 +3455,355 @@ namespace DiscImageChef.Devices Obsolete1 = 4, Obsolete2 = 5 } + + /// + /// MMC / SecureDigital commands + /// + public enum MmcCommands : byte + { + #region Class 1 MMC Commands (Basic and read-stream) + /// + /// Resets device to idle (BC) + /// + GoIdle = 0, + /// + /// Resets the device to pre-idle (BC) + /// + GoPreIdleState = 0, + /// + /// Initiate alternative boot operation + /// + BootInitiation = 0, + /// + /// Asks device in idle state to send their operation conditions in response (BCR, R3) + /// + SendOpCond = 1, + /// + /// Asks device to send their CID numbers (BCR, R2) + /// + AllSendCID = 2, + /// + /// Assigns a relative address to the device (AC, R1) + /// + SetRelativeAddress = 3, + /// + /// Programs the DSR of the device (BC) + /// + SetDSR = 4, + /// + /// Toggles the device between sleep and standby (AC, R1b) + /// + SleepAwake = 5, + /// + /// Switches device mode of operation (AC, R1b) + /// + Switch = 6, + /// + /// Toggles a device between the stand-by and transfer stats or between the programming and disconnect states (AC, R1b) + /// + SelectCard = 7, + /// + /// Asks device to send its extended card-specific data (ExtCSD) (ADTC, R1) + /// + SendExtCSD = 8, + /// + /// Asks device to send its card-specific data (CSD) (AC, R2) + /// + SendCSD = 9, + /// + /// Asks device to send its card identification (CID) (AC, R2) + /// + SendCID = 10, + /// + /// Reads data stream from device, starting at given address, until a follows (ADTC, R1) + /// + [Obsolete] + ReadDatUntilStop = 11, + /// + /// Terminates a read/write stream/multiple block operation (AC, R1 / R1b) + /// + StopTransmission = 12, + /// + /// Asks device to send its status register (AC, R1) + /// + SendStatus = 13, + /// + /// The host reads the reversed bus testing data pattern from a device (ADTC, R1) + /// + BusTestRead = 14, + /// + /// Sets the card to inactive state (AC) + /// + GoInactiveState = 15, + /// + /// The host sends the bus testing data pattern to a device (ADTC, R1) + /// + BusTestWrite = 19, + SPIReadOCR = 58, + SPICRCOnOff = 59, + #endregion Class 1 MMC Commands (Basic and read-stream) + + #region Class 2 MMC Commands (Block-oriented read) + /// + /// Sets the block length in bytes (AC, R1) + /// + SetBlocklen = 16, + /// + /// Reads a block (ADTC, R1) + /// + ReadSingleBlock = 17, + /// + /// Transfers data blocks from card to host until interrupted (ADTC, R1) + /// + ReadMultipleBlock = 18, + /// + /// 128 blocks of tuning pattern is sent for HS200 optimal sampling point detection (ADTC, R1) + /// + SendTuningBlockHS200 = 21, + #endregion Class 2 MMC Commands (Block-oriented read) + + #region Class 3 MMC Commands (Stream write) + /// + /// Writes data stream from host until a follows (ADTC, R1) + /// + [Obsolete] + WriteDatUntilStop = 20, + #endregion Class 3 MMC Commands (Stream write) + + #region Class 4 MMC Commands (Block-oriented write) + /// + /// Defines the number of blocks which are going to be transferred in the immediately succeeding multiple block command (AC, R1) + /// + SetBlockCount = 23, + /// + /// Writes a block (ADTC, R1) + /// + WriteBlock = 24, + /// + /// Continuosly writes blocks until interrupted (ADTC, R1) + /// + WriteMultipleBlock = 25, + /// + /// Programs the Card Information register (ADTC, R1) + /// + ProgramCID = 26, + /// + /// Programs the programmable bits of the CSD (ADTC, R1) + /// + ProgramCSD = 27, + /// + /// Sets the real time clock according to information in block (ADTC, R1) + /// + SetTime = 49, + #endregion Class 4 MMC Commands (Block-oriented write) + + #region Class 5 MMC Commands (Erase) + /// + /// Sets the address of the first erase group (AC, R1) + /// + EraseGroupStart = 35, + /// + /// Sets the address of the last erase group (AC, R1) + /// + EraseGroupEnd = 36, + /// + /// Erases previously selected write blocks (AC, R1b) + /// + Erase = 38, + #endregion Class 5 MMC Commands (Erase) + + #region Class 6 MMC Commands (Block-oriented write protection) + /// + /// Sets the write protection bit (AC, R1b) + /// + SetWriteProtect = 28, + /// + /// Clears the write protection bit (AC, R1b) + /// + ClearWriteProtect = 29, + /// + /// Asks the device to send the status of the write protection bit (ADTC, R1) + /// + SendWriteProtect = 30, + /// + /// Sends the type of write protection that is set for the different write protection groups (ADTC, R1) + /// + SentWriteProtectType = 31, + #endregion Class 6 MMC Commands (Block-oriented write protection) + + #region Class 7 MMC Commands (Lock) + /// + /// Used to set/reset the password or lock/unlock the card (ADTC, R1b) + /// + LockUnlock = 42, + #endregion Class 7 MMC Commands (Lock) + + #region Class 8 MMC Commands (Application-specific) + /// + /// Indicates the card that the next command is an application specific command (AC, R1) + /// + ApplicationCommand = 55, + /// + /// Transfers a data block to/from the card for general purpose / application specific commands (ADTC, R1b) + /// + GenericCommand = 56, + #endregion Class 8 MMC Commands (Application-specific) + + #region Class 9 MMC Commands (I/O mode) + /// + /// Used to write and read 8 bit data field, used to access application dependent registers not defined in MMC standard (AC, R4) + /// + FastIO = 39, + /// + /// Sets the system into interrupt mode (BCR, R5) + /// + GoIRQState = 40, + #endregion Class 9 MMC Commands (I/O mode) + + #region Class 10 MMC Commands (Security Protocols) + /// + /// Reads data blocks (ADTC, R1) + /// + ProtocolRead = 53, + /// + /// Writes data blocks (ADTC, R1) + /// + ProtocolWrite = 54, + #endregion Class 10 MMC Commands (Security Protocols) + + #region Class 11 MMC Commands (Command Queue) + /// + /// Defines data direction, priority, task ID and block count of queued task (AC, R1) + /// + QueuedTaskParameters = 44, + /// + /// Defines the block address of queued task (AC, R1) + /// + QueuedTaskAddress = 45, + /// + /// Executes the task queue for reading (ADTC, R1) + /// + ExecuteTaskRead = 46, + /// + /// Executes the task queue for writing (ADTC, R1) + /// + ExecuteTaskWrite = 47, + /// + /// Manages queues and tasks (AC, R1b) + /// + CmdQTaskManagement = 48, + #endregion Class 11 MMC Commands (Command Queue) + + #region Class 1 SecureDigital Commands (Basic) + /// + /// Sends SD interface condition (BCR, R7) + /// + SendInterfaceCondition = 8, + /// + /// Switch to 1.8V bus signaling level (AC, R1) + /// + VoltageSwitch = 11, + #endregion Class 1 SecureDigital Commands (Basic) + + #region Class 2 SecureDigital Commands (Block-oriented read) + /// + /// 64 bytes of tuning pattern is sent for SDR50 and SDR104 optinal sampling point detection (ADTC, R1) + /// + SendTuningBlock = 19, + /// + /// Speed class control command (AC, R1b) + /// + SpeedClassControl = 20, + #endregion Class 2 SecureDigital Commands (Block-oriented read) + + #region Class 11 SecureDigital Commands (Function Extension) + /// + /// Single block read type (ADTC, R1) + /// + ReadExtraSingle = 48, + /// + /// Single block write type (ADTC, R1) + /// + WriteExtraSingle = 49, + /// + /// Multiple block read type (ADTC, R1) + /// + ReadExtraMulti = 58, + /// + /// Multiple block write type (ADTC, R1) + /// + WriteExtraMulti = 59, + #endregion Class 11 SecureDigital Commands (Function Extension) + } + + /// + /// SecureDigital application-specific commands + /// + public enum SecureDigitalCommands : byte + { + /// + /// Defines the data bus width to be used for data transfer (AC, R1) + /// + SetBusWidth = 6, + /// + /// Sends the SD status register (ADTC, R1) + /// + SendStatus = 13, + /// + /// Send the number of the written write blocks (ADTC, R1) + /// + SendNumWriteBlocks = 22, + /// + /// Set the number of write blocks to be pre-erased before writing (AC, R1) + /// + SetWriteBlockEraseCount = 23, + /// + /// Sends host capacity support information and asks the card to send its operating condition register (BCR, R3) + /// + SendOperatingCondition = 41, + /// + /// Connects/Disconnects the 50 kOhm pull-up resistor on CD/DAT3 pin of card (AC, R1) + /// + SetClearCardDetect = 42, + /// + /// Reads the SD Configuration Register SCR (ADTC, R1) + /// + SendSCR = 51, + } + + [Flags] + public enum MmcFlags : uint + { + ResponsePresent = 1 << 0, + Response136 = 1 << 1, + ResponseCrc = 1 << 2, + ResponseBusy = 1 << 3, + ResponseOpcode = 1 << 4, + CommandMask = 3 << 5, + CommandAC = 0 << 5, + CommandADTC = 1 << 5, + CommandBC = 2 << 5, + CommandBCR = 3 << 5, + ResponseSPI_S1 = 1 << 7, + ResponseSPI_S2 = 1 << 8, + ResponseSPI_B4 = 1 << 9, + ResponseSPI_Busy = 1 << 10, + ResponseNone = 0, + Response_R1 = ResponsePresent | ResponseCrc | ResponseOpcode, + Response_R1b = ResponsePresent | ResponseCrc | ResponseOpcode | ResponseBusy, + Response_R2 = ResponsePresent | Response136 | ResponseCrc, + Response_R3 = ResponsePresent, + Response_R4 = ResponsePresent, + Response_R5 = ResponsePresent | ResponseCrc | ResponseOpcode, + Response_R6 = ResponsePresent | ResponseCrc | ResponseOpcode, + Response_R7 = ResponsePresent | ResponseCrc | ResponseOpcode, + ResponseSPI_R1 = ResponseSPI_S1, + ResponseSPI_R1b = ResponseSPI_S1 | ResponseSPI_Busy, + ResponseSPI_R2 = ResponseSPI_S1 | ResponseSPI_S2, + ResponseSPI_R3 = ResponseSPI_S1 | ResponseSPI_B4, + ResponseSPI_R4 = ResponseSPI_S1 | ResponseSPI_B4, + ResponseSPI_R5 = ResponseSPI_S1 | ResponseSPI_S2, + ResponseSPI_R7 = ResponseSPI_S1 | ResponseSPI_B4 + } } diff --git a/DiscImageChef.Devices/Linux/Command.cs b/DiscImageChef.Devices/Linux/Command.cs index 0daf42e17..a63e381cf 100644 --- a/DiscImageChef.Devices/Linux/Command.cs +++ b/DiscImageChef.Devices/Linux/Command.cs @@ -328,6 +328,71 @@ namespace DiscImageChef.Devices.Linux return error; } + /// + /// Sends a MMC/SD command + /// + /// The result of the command. + /// File handle + /// MMC/SD opcode + /// Buffer for MMC/SD command response + /// Timeout in seconds + /// Time it took to execute the command in milliseconds + /// True if MMC/SD returned non-OK status + /// True if data is sent from host to card + /// True if command should be preceded with CMD55 + /// Flags indicating kind and place of response + /// How many blocks to transfer + /// Command argument + /// Response registers + /// Size of block in bytes + internal static int SendMmcCommand(int fd, MmcCommands command, bool write, bool isApplication, MmcFlags flags, uint argument, uint blockSize, uint blocks, ref byte[] buffer, out uint[] response, out double duration, out bool sense, uint timeout = 0) + { + response = null; + duration = 0; + sense = false; + + if(buffer == null) + return -1; + + mmc_ioc_cmd io_cmd = new mmc_ioc_cmd(); + + IntPtr bufPtr = Marshal.AllocHGlobal(buffer.Length); + + io_cmd.write_flag = write; + io_cmd.is_ascmd = isApplication; + io_cmd.opcode = (uint)command; + io_cmd.arg = argument; + io_cmd.flags = flags; + io_cmd.blksz = blockSize; + io_cmd.blksz = blocks; + if(timeout > 0) + { + io_cmd.data_timeout_ns = timeout * 1000000000; + io_cmd.cmd_timeout_ms = timeout * 1000; + } + io_cmd.data_ptr = (ulong)bufPtr; + + Marshal.Copy(buffer, 0, bufPtr, buffer.Length); + + DateTime start = DateTime.UtcNow; + int error = Extern.ioctlMmc(fd, LinuxIoctl.MMC_IOC_CMD, ref io_cmd); + DateTime end = DateTime.UtcNow; + + sense |= error < 0; + + if(error < 0) + error = Marshal.GetLastWin32Error(); + + Marshal.Copy(bufPtr, buffer, 0, buffer.Length); + + response = io_cmd.response; + duration = (end - start).TotalMilliseconds; + + Marshal.FreeHGlobal(bufPtr); + + return error; + } + public static string ReadLink(string path) { IntPtr buf = Marshal.AllocHGlobal(4096); diff --git a/DiscImageChef.Devices/Linux/Enums.cs b/DiscImageChef.Devices/Linux/Enums.cs index a71e8d7d1..bf6debe20 100644 --- a/DiscImageChef.Devices/Linux/Enums.cs +++ b/DiscImageChef.Devices/Linux/Enums.cs @@ -145,6 +145,8 @@ namespace DiscImageChef.Devices.Linux // SCSI IOCtls SG_GET_VERSION_NUM = 0x2282, SG_IO = 0x2285, + // MMC IOCtl + MMC_IOC_CMD = 0xC048B300 } [Flags] diff --git a/DiscImageChef.Devices/Linux/Extern.cs b/DiscImageChef.Devices/Linux/Extern.cs index ff3af77ad..3bc39de37 100644 --- a/DiscImageChef.Devices/Linux/Extern.cs +++ b/DiscImageChef.Devices/Linux/Extern.cs @@ -52,6 +52,9 @@ namespace DiscImageChef.Devices.Linux [DllImport("libc", EntryPoint = "ioctl", SetLastError = true)] internal static extern int ioctlSg(int fd, LinuxIoctl request, ref sg_io_hdr_t value); + [DllImport("libc", EntryPoint = "ioctl", SetLastError = true)] + internal static extern int ioctlMmc(int fd, LinuxIoctl request, ref mmc_ioc_cmd value); + [DllImport("libc", CharSet = CharSet.Ansi, SetLastError = true)] internal static extern int readlink(string path, System.IntPtr buf, int bufsize); diff --git a/DiscImageChef.Devices/Linux/Structs.cs b/DiscImageChef.Devices/Linux/Structs.cs index efec704c9..e2039a1c4 100644 --- a/DiscImageChef.Devices/Linux/Structs.cs +++ b/DiscImageChef.Devices/Linux/Structs.cs @@ -65,5 +65,62 @@ namespace DiscImageChef.Devices.Linux public uint duration; /* [o] */ public SgInfo info; /* [o] */ } + + [StructLayout(LayoutKind.Sequential)] + struct mmc_ioc_cmd + { + /// + /// Implies direction of data. true = write, false = read + /// + public bool write_flag; + /// + /// Application-specific command. true = precede with CMD55 + /// + public bool is_ascmd; + public uint opcode; + public uint arg; + /// + /// CMD response + /// + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] + public uint[] response; + public MmcFlags flags; + public uint blksz; + public uint blocks; + /// + /// Sleep at least useconds, and at most + /// useconds *after* issuing command.Needed for + /// some read commands for which cards have no other way of indicating + /// they're ready for the next command (i.e. there is no equivalent of + /// a "busy" indicator for read operations). + /// + public uint postsleep_min_us; + /// + /// Sleep at least useconds, and at most + /// useconds *after* issuing command.Needed for + /// some read commands for which cards have no other way of indicating + /// they're ready for the next command (i.e. there is no equivalent of + /// a "busy" indicator for read operations). + /// + public uint postsleep_max_us; + /// + /// Override driver-computed timeouts. + /// + public uint data_timeout_ns; + /// + /// Override driver-computed timeouts. + /// + public uint cmd_timeout_ms; + /// + /// For 64-bit machines , wants to + /// be 8-byte aligned.Make sure this struct is the same size when + /// built for 32-bit. + /// + public uint __pad; + /// + /// DAT buffer + /// + public ulong data_ptr; + } }