2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-10-22 22:58:01 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : MMC.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Component : SecureDigital and MultiMediaCard commands.
|
2016-10-22 22:58:01 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Contains MultiMediaCard commands.
|
2016-10-22 22:58:01 +01:00
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-01-03 17:51:30 +00:00
|
|
|
// Copyright © 2011-2020 Natalia Portillo
|
2016-10-22 22:58:01 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
2020-12-10 13:50:51 +00:00
|
|
|
using System;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.Console;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
namespace Aaru.Devices
|
2016-10-22 22:58:01 +01:00
|
|
|
{
|
2020-07-22 13:20:25 +01:00
|
|
|
public sealed partial class Device
|
2016-10-22 22:58:01 +01:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
public bool ReadCsd(out byte[] buffer, out uint[] response, uint timeout, out double duration)
|
2016-10-22 22:58:01 +01:00
|
|
|
{
|
|
|
|
|
buffer = new byte[16];
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
LastError = SendMmcCommand(MmcCommands.SendCsd, false, false,
|
|
|
|
|
MmcFlags.ResponseSpiR2 | MmcFlags.ResponseR2 | MmcFlags.CommandAc, 0, 16, 1,
|
|
|
|
|
ref buffer, out response, out duration, out bool sense, timeout);
|
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("MMC Device", "SEND_CSD took {0} ms.", duration);
|
2016-10-22 22:58:01 +01:00
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
public bool ReadCid(out byte[] buffer, out uint[] response, uint timeout, out double duration)
|
2016-10-22 22:58:01 +01:00
|
|
|
{
|
|
|
|
|
buffer = new byte[16];
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
LastError = SendMmcCommand(MmcCommands.SendCid, false, false,
|
|
|
|
|
MmcFlags.ResponseSpiR2 | MmcFlags.ResponseR2 | MmcFlags.CommandAc, 0, 16, 1,
|
|
|
|
|
ref buffer, out response, out duration, out bool sense, timeout);
|
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("MMC Device", "SEND_CID took {0} ms.", duration);
|
2016-10-22 22:58:01 +01:00
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
public bool ReadOcr(out byte[] buffer, out uint[] response, uint timeout, out double duration)
|
2016-10-22 22:58:01 +01:00
|
|
|
{
|
|
|
|
|
buffer = new byte[4];
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
LastError = SendMmcCommand(MmcCommands.SendOpCond, false, true,
|
|
|
|
|
MmcFlags.ResponseSpiR3 | MmcFlags.ResponseR3 | MmcFlags.CommandBcr, 0, 4, 1,
|
|
|
|
|
ref buffer, out response, out duration, out bool sense, timeout);
|
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("SecureDigital Device", "SEND_OP_COND took {0} ms.", duration);
|
2016-10-22 22:58:01 +01:00
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
public bool ReadExtendedCsd(out byte[] buffer, out uint[] response, uint timeout, out double duration)
|
2016-10-22 22:58:01 +01:00
|
|
|
{
|
|
|
|
|
buffer = new byte[512];
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
LastError = SendMmcCommand(MmcCommands.SendExtCsd, false, false,
|
|
|
|
|
MmcFlags.ResponseSpiR1 | MmcFlags.ResponseR1 | MmcFlags.CommandAdtc, 0, 512, 1,
|
|
|
|
|
ref buffer, out response, out duration, out bool sense, timeout);
|
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("MMC Device", "SEND_EXT_CSD took {0} ms.", duration);
|
2016-10-22 22:58:01 +01:00
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SetBlockLength(uint length, out uint[] response, uint timeout, out double duration)
|
|
|
|
|
{
|
|
|
|
|
byte[] buffer = new byte[0];
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
LastError = SendMmcCommand(MmcCommands.SetBlocklen, false, false,
|
|
|
|
|
MmcFlags.ResponseSpiR1 | MmcFlags.ResponseR1 | MmcFlags.CommandAc, length, 0, 0,
|
|
|
|
|
ref buffer, out response, out duration, out bool sense, timeout);
|
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("MMC Device", "SET_BLOCKLEN took {0} ms.", duration);
|
2016-10-22 22:58:01 +01:00
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 13:33:47 +00:00
|
|
|
public bool Read(out byte[] buffer, out uint[] response, uint lba, uint blockSize, ushort transferLength,
|
2020-02-29 18:03:35 +00:00
|
|
|
bool byteAddressed, uint timeout, out double duration)
|
2016-10-22 22:58:01 +01:00
|
|
|
{
|
2020-12-10 13:50:51 +00:00
|
|
|
bool sense = true;
|
|
|
|
|
buffer = null;
|
|
|
|
|
response = null;
|
|
|
|
|
duration = -1;
|
|
|
|
|
|
|
|
|
|
if(transferLength <= 1)
|
|
|
|
|
return ReadSingleBlock(out buffer, out response, lba, blockSize, byteAddressed, timeout, out duration);
|
|
|
|
|
|
|
|
|
|
if(!_readMultipleBlockCannotSetBlockCount)
|
|
|
|
|
sense = ReadMultipleBlock(out buffer, out response, lba, blockSize, transferLength, byteAddressed,
|
|
|
|
|
timeout, out duration);
|
|
|
|
|
|
|
|
|
|
if(_readMultipleBlockCannotSetBlockCount)
|
|
|
|
|
return ReadMultipleUsingSingle(out buffer, out response, lba, blockSize, transferLength, byteAddressed,
|
|
|
|
|
timeout, out duration);
|
|
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ReadSingleBlock(out byte[] buffer, out uint[] response, uint lba, uint blockSize,
|
|
|
|
|
bool byteAddressed, uint timeout, out double duration)
|
|
|
|
|
{
|
2016-10-22 22:58:01 +01:00
|
|
|
uint address;
|
2020-12-10 13:50:51 +00:00
|
|
|
buffer = new byte[blockSize];
|
|
|
|
|
response = null;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
if(byteAddressed)
|
|
|
|
|
address = lba * blockSize;
|
|
|
|
|
else
|
|
|
|
|
address = lba;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2020-12-10 13:50:51 +00:00
|
|
|
LastError = SendMmcCommand(MmcCommands.ReadSingleBlock, false, false,
|
2017-12-20 17:15:26 +00:00
|
|
|
MmcFlags.ResponseSpiR1 | MmcFlags.ResponseR1 | MmcFlags.CommandAdtc, address,
|
2020-12-10 13:50:51 +00:00
|
|
|
blockSize, 1, ref buffer, out response, out duration, out bool sense, timeout);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2020-12-10 13:50:51 +00:00
|
|
|
AaruConsole.DebugWriteLine("MMC Device", "READ_SINGLE_BLOCK took {0} ms.", duration);
|
|
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool _readMultipleBlockCannotSetBlockCount;
|
|
|
|
|
|
|
|
|
|
public bool ReadMultipleBlock(out byte[] buffer, out uint[] response, uint lba, uint blockSize,
|
|
|
|
|
ushort transferLength, bool byteAddressed, uint timeout, out double duration)
|
|
|
|
|
{
|
|
|
|
|
buffer = new byte[transferLength * blockSize];
|
|
|
|
|
double setDuration = 0;
|
|
|
|
|
uint address;
|
|
|
|
|
response = null;
|
|
|
|
|
|
|
|
|
|
if(byteAddressed)
|
|
|
|
|
address = lba * blockSize;
|
|
|
|
|
else
|
|
|
|
|
address = lba;
|
|
|
|
|
|
|
|
|
|
LastError = SendMmcCommand(MmcCommands.ReadMultipleBlock, false, false,
|
|
|
|
|
MmcFlags.ResponseSpiR1 | MmcFlags.ResponseR1 | MmcFlags.CommandAdtc, address,
|
2020-12-12 20:56:56 +00:00
|
|
|
blockSize, transferLength, ref buffer, out response, out duration,
|
|
|
|
|
out bool sense, timeout);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2020-12-10 13:50:51 +00:00
|
|
|
Error = LastError != 0;
|
|
|
|
|
|
|
|
|
|
if(transferLength > 1)
|
|
|
|
|
{
|
|
|
|
|
duration += setDuration;
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("MMC Device", "READ_MULTIPLE_BLOCK took {0} ms.", duration);
|
2017-09-29 13:01:16 +00:00
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
else
|
|
|
|
|
AaruConsole.DebugWriteLine("MMC Device", "READ_SINGLE_BLOCK took {0} ms.", duration);
|
2016-10-22 22:58:01 +01:00
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 20:58:58 +00:00
|
|
|
public bool ReadMultipleUsingSingle(out byte[] buffer, out uint[] response, uint lba, uint blockSize,
|
|
|
|
|
ushort transferLength, bool byteAddressed, uint timeout,
|
|
|
|
|
out double duration)
|
2020-12-10 13:50:51 +00:00
|
|
|
{
|
|
|
|
|
buffer = new byte[transferLength * blockSize];
|
|
|
|
|
byte[] blockBuffer = new byte[blockSize];
|
|
|
|
|
duration = 0;
|
|
|
|
|
bool sense = true;
|
|
|
|
|
response = null;
|
|
|
|
|
|
|
|
|
|
for(uint i = 0; i < transferLength; i++)
|
|
|
|
|
{
|
|
|
|
|
uint address;
|
|
|
|
|
|
|
|
|
|
if(byteAddressed)
|
|
|
|
|
address = (lba + i) * blockSize;
|
|
|
|
|
else
|
|
|
|
|
address = lba + i;
|
|
|
|
|
|
|
|
|
|
LastError = SendMmcCommand(MmcCommands.ReadSingleBlock, false, false,
|
|
|
|
|
MmcFlags.ResponseSpiR1 | MmcFlags.ResponseR1 | MmcFlags.CommandAdtc, address,
|
|
|
|
|
blockSize, 1, ref blockBuffer, out response, out double blockDuration,
|
|
|
|
|
out sense, timeout);
|
|
|
|
|
|
|
|
|
|
Error = LastError != 0;
|
|
|
|
|
|
|
|
|
|
duration += blockDuration;
|
|
|
|
|
|
|
|
|
|
if(Error || sense)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
Array.Copy(blockBuffer, 0, buffer, i * blockSize, blockSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AaruConsole.DebugWriteLine("MMC Device", "Multiple READ_SINGLE_BLOCKs took {0} ms.", duration);
|
|
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-22 22:58:01 +01:00
|
|
|
public bool ReadStatus(out byte[] buffer, out uint[] response, uint timeout, out double duration)
|
|
|
|
|
{
|
|
|
|
|
buffer = new byte[4];
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
LastError = SendMmcCommand(MmcCommands.SendStatus, false, true,
|
|
|
|
|
MmcFlags.ResponseSpiR1 | MmcFlags.ResponseR1 | MmcFlags.CommandAc, 0, 4, 1,
|
|
|
|
|
ref buffer, out response, out duration, out bool sense, timeout);
|
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("SecureDigital Device", "SEND_STATUS took {0} ms.", duration);
|
2016-10-22 22:58:01 +01:00
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|