2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:23 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-10-22 22:58:01 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : CID.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Component : Device structures decoders.
|
2016-10-22 22:58:01 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Decodes MultiMediaCard CID.
|
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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-02-18 10:02:39 +00:00
|
|
|
// Copyright © 2011-2022 Natalia Portillo
|
2016-10-22 22:58:01 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-12-22 02:04:18 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2016-10-22 22:58:01 +01:00
|
|
|
using System.Text;
|
2020-07-20 15:43:51 +01:00
|
|
|
using Aaru.Helpers;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
namespace Aaru.Decoders.MMC;
|
|
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
[SuppressMessage("ReSharper", "InconsistentNaming"), SuppressMessage("ReSharper", "MemberCanBeInternal"),
|
|
|
|
|
SuppressMessage("ReSharper", "MemberCanBePrivate.Global"), SuppressMessage("ReSharper", "UnassignedField.Global")]
|
|
|
|
|
public class CID
|
|
|
|
|
{
|
|
|
|
|
public byte ApplicationID;
|
|
|
|
|
public byte CRC;
|
|
|
|
|
public byte DeviceType;
|
|
|
|
|
public byte Manufacturer;
|
|
|
|
|
public byte ManufacturingDate;
|
|
|
|
|
public string ProductName;
|
|
|
|
|
public byte ProductRevision;
|
|
|
|
|
public uint ProductSerialNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[SuppressMessage("ReSharper", "InconsistentNaming"), SuppressMessage("ReSharper", "MemberCanBeInternal"),
|
|
|
|
|
SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
|
|
|
|
|
public static partial class Decoders
|
2016-10-22 22:58:01 +01:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
public static CID DecodeCID(uint[] response)
|
2016-10-22 22:58:01 +01:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
if(response?.Length != 4)
|
|
|
|
|
return null;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
byte[] data = new byte[16];
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
byte[] 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);
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return DecodeCID(data);
|
|
|
|
|
}
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public static CID DecodeCID(byte[] response)
|
|
|
|
|
{
|
|
|
|
|
if(response?.Length != 16)
|
|
|
|
|
return null;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
var cid = new CID
|
2016-10-22 22:58:01 +01:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
Manufacturer = response[0],
|
|
|
|
|
DeviceType = (byte)(response[1] & 0x03),
|
|
|
|
|
ProductRevision = response[9],
|
|
|
|
|
ProductSerialNumber = Swapping.Swap(BitConverter.ToUInt32(response, 10)),
|
|
|
|
|
ManufacturingDate = response[14],
|
|
|
|
|
CRC = (byte)((response[15] & 0xFE) >> 1)
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
byte[] tmp = new byte[6];
|
2022-03-06 13:29:37 +00:00
|
|
|
Array.Copy(response, 3, tmp, 0, 6);
|
|
|
|
|
cid.ProductName = StringHandlers.CToString(tmp);
|
|
|
|
|
|
|
|
|
|
return cid;
|
|
|
|
|
}
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public static string PrettifyCID(CID cid)
|
|
|
|
|
{
|
|
|
|
|
if(cid == null)
|
|
|
|
|
return null;
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
var sb = new StringBuilder();
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendLine("MultiMediaCard Device Identification Register:");
|
|
|
|
|
sb.AppendFormat("\tManufacturer: {0}", VendorString.Prettify(cid.Manufacturer)).AppendLine();
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
switch(cid.DeviceType)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
sb.AppendLine("\tRemovable device");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
sb.AppendLine("\tBGA device");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
sb.AppendLine("\tPOP device");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("\tApplication ID: {0}", cid.ApplicationID).AppendLine();
|
|
|
|
|
sb.AppendFormat("\tProduct name: {0}", cid.ProductName).AppendLine();
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("\tProduct revision: {0:X2}.{1:X2}", (cid.ProductRevision & 0xF0) >> 4,
|
|
|
|
|
cid.ProductRevision & 0x0F).AppendLine();
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("\tProduct serial number: {0}", cid.ProductSerialNumber).AppendLine();
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-11-13 19:59:23 +00:00
|
|
|
string year = (cid.ManufacturingDate & 0x0F) switch
|
2022-11-15 15:58:41 +00:00
|
|
|
{
|
|
|
|
|
0 => "1997 or 2013",
|
|
|
|
|
1 => "1998 or 2014",
|
|
|
|
|
2 => "1999 or 2015",
|
|
|
|
|
3 => "2000 or 2016",
|
|
|
|
|
4 => "2001 or 2017",
|
|
|
|
|
5 => "2002 or 2018",
|
|
|
|
|
6 => "2003 or 2019",
|
|
|
|
|
7 => "2004 or 2020",
|
|
|
|
|
8 => "2005 or 2021",
|
|
|
|
|
9 => "2006 or 2022",
|
|
|
|
|
10 => "2007 or 2023",
|
|
|
|
|
11 => "2008 or 2024",
|
|
|
|
|
12 => "2009 or 2025",
|
|
|
|
|
13 => "2010",
|
|
|
|
|
14 => "2011",
|
|
|
|
|
15 => "2012",
|
|
|
|
|
_ => ""
|
|
|
|
|
};
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("\tDevice manufactured month {0} of {1}", (cid.ManufacturingDate & 0xF0) >> 4, year).
|
|
|
|
|
AppendLine();
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("\tCID CRC: 0x{0:X2}", cid.CRC).AppendLine();
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public static string PrettifyCID(uint[] response) => PrettifyCID(DecodeCID(response));
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public static string PrettifyCID(byte[] response) => PrettifyCID(DecodeCID(response));
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|