Add MMC/SecureDigital device support. Not yet used because of

a bad implementation of SEND_CSD and SEND_CID commands (TODO).
This commit is contained in:
2016-10-22 22:58:01 +01:00
parent 8b9d678893
commit 0c9dfaa11f
24 changed files with 3607 additions and 3 deletions

View File

@@ -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);
}
/// <summary>
/// Sends a MMC/SD command to this device
/// </summary>
/// <returns>The result of the command.</returns>
/// <param name="command">MMC/SD opcode</param>
/// <param name="buffer">Buffer for MMC/SD command response</param>
/// <param name="timeout">Timeout in seconds</param>
/// <param name="duration">Time it took to execute the command in milliseconds</param>
/// <param name="sense"><c>True</c> if MMC/SD returned non-OK status</param>
/// <param name="write"><c>True</c> if data is sent from host to card</param>
/// <param name="isApplication"><c>True</c> if command should be preceded with CMD55</param>
/// <param name="flags">Flags indicating kind and place of response</param>
/// <param name="blocks">How many blocks to transfer</param>
/// <param name="argument">Command argument</param>
/// <param name="response">Response registers</param>
/// <param name="blockSize">Size of block in bytes</param>
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);
}
}
}

View File

@@ -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);

View File

@@ -0,0 +1,151 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : MMC.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// 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;
}
}
}

View File

@@ -0,0 +1,81 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : SecureDigital.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// 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;
}
}
}