2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:23 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2015-12-03 08:02:12 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : DMI.cs
|
2016-07-28 18:13:49 +01:00
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2015-12-03 08:02:12 +00:00
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Component : Device structures decoders.
|
2015-12-03 08:02:12 +00:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Decodes Xbox discs DMI structure.
|
2015-12-03 08:02:12 +00:00
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// 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
|
2015-12-03 08:02:12 +00:00
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// 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.
|
2015-12-03 08:02:12 +00:00
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// 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/>.
|
2015-12-03 08:02:12 +00:00
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-02-18 10:02:39 +00:00
|
|
|
// Copyright © 2011-2022 Natalia Portillo
|
2015-12-03 08:02:12 +00:00
|
|
|
// ****************************************************************************/
|
2016-07-28 18:13:49 +01:00
|
|
|
|
2015-12-03 08:02:12 +00:00
|
|
|
using System;
|
2017-12-22 02:04:18 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2015-12-03 08:02:12 +00:00
|
|
|
using System.Text;
|
2020-07-20 15:43:51 +01:00
|
|
|
using Aaru.Helpers;
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
namespace Aaru.Decoders.Xbox;
|
|
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
[SuppressMessage("ReSharper", "InconsistentNaming"), SuppressMessage("ReSharper", "MemberCanBeInternal"),
|
|
|
|
|
SuppressMessage("ReSharper", "MemberCanBePrivate.Global"), SuppressMessage("ReSharper", "NotAccessedField.Global")]
|
|
|
|
|
public static class DMI
|
2015-12-03 08:02:12 +00:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
public static bool IsXbox(byte[] dmi)
|
2015-12-03 08:02:12 +00:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
if(dmi?.Length != 2052)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Version is 1
|
|
|
|
|
if(BitConverter.ToUInt32(dmi, 4) != 1)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Catalogue number is two letters, five numbers, one letter
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 12; i < 14; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
if(dmi[i] < 0x41 ||
|
|
|
|
|
dmi[i] > 0x5A)
|
2019-11-25 00:54:38 +00:00
|
|
|
return false;
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 14; i < 19; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
if(dmi[i] < 0x30 ||
|
|
|
|
|
dmi[i] > 0x39)
|
2019-11-25 00:54:38 +00:00
|
|
|
return false;
|
2017-05-23 01:30:57 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
if(dmi[19] < 0x41 ||
|
|
|
|
|
dmi[19] > 0x5A)
|
|
|
|
|
return false;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
long timestamp = BitConverter.ToInt64(dmi, 20);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
// Game cannot exist before the Xbox
|
|
|
|
|
return timestamp >= 0x1BD164833DFC000;
|
|
|
|
|
}
|
2017-05-23 01:30:57 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public static bool IsXbox360(byte[] dmi)
|
|
|
|
|
{
|
|
|
|
|
if(dmi?.Length != 2052)
|
|
|
|
|
return false;
|
2017-05-23 01:30:57 +01:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
uint signature = BitConverter.ToUInt32(dmi, 0x7EC);
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
// "XBOX" swapped as .NET is little endian
|
|
|
|
|
return signature == 0x584F4258;
|
|
|
|
|
}
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public static XboxDMI? DecodeXbox(byte[] response)
|
|
|
|
|
{
|
|
|
|
|
bool isXbox = IsXbox(response);
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
if(!isXbox)
|
|
|
|
|
return null;
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
var dmi = new XboxDMI
|
2017-05-23 01:30:57 +01:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
DataLength = (ushort)((response[0] << 8) + response[1]),
|
|
|
|
|
Reserved1 = response[2],
|
|
|
|
|
Reserved2 = response[3],
|
|
|
|
|
Version = BitConverter.ToUInt32(response, 4),
|
|
|
|
|
Timestamp = BitConverter.ToInt64(response, 20)
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
byte[] tmp = new byte[8];
|
2022-03-06 13:29:37 +00:00
|
|
|
Array.Copy(response, 12, tmp, 0, 8);
|
|
|
|
|
dmi.CatalogNumber = StringHandlers.CToString(tmp);
|
|
|
|
|
|
|
|
|
|
return dmi;
|
|
|
|
|
}
|
2017-05-23 01:30:57 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public static Xbox360DMI? DecodeXbox360(byte[] response)
|
|
|
|
|
{
|
|
|
|
|
bool isX360 = IsXbox360(response);
|
2017-05-23 01:30:57 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
if(!isX360)
|
|
|
|
|
return null;
|
2017-05-23 01:30:57 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
var dmi = new Xbox360DMI
|
2015-12-03 08:02:12 +00:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
DataLength = (ushort)((response[0] << 8) + response[1]),
|
|
|
|
|
Reserved1 = response[2],
|
|
|
|
|
Reserved2 = response[3],
|
|
|
|
|
Version = BitConverter.ToUInt32(response, 4),
|
|
|
|
|
Timestamp = BitConverter.ToInt64(response, 20),
|
|
|
|
|
MediaID = new byte[16]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Array.Copy(response, 36, dmi.MediaID, 0, 16);
|
2022-11-15 15:58:41 +00:00
|
|
|
byte[] tmp = new byte[16];
|
2022-03-06 13:29:37 +00:00
|
|
|
Array.Copy(response, 68, tmp, 0, 16);
|
|
|
|
|
dmi.CatalogNumber = StringHandlers.CToString(tmp);
|
|
|
|
|
|
2022-03-07 07:36:42 +00:00
|
|
|
return dmi.CatalogNumber == null || dmi.CatalogNumber.Length < 13 ? null : dmi;
|
2022-03-06 13:29:37 +00:00
|
|
|
}
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public static string PrettifyXbox(XboxDMI? dmi)
|
|
|
|
|
{
|
|
|
|
|
if(dmi == null)
|
|
|
|
|
return null;
|
2017-05-23 01:30:57 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
XboxDMI decoded = dmi.Value;
|
|
|
|
|
var sb = new StringBuilder();
|
2017-05-23 01:30:57 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append("Catalogue number: ");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 0; i < 2; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("{0}", decoded.CatalogNumber[i]);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append("-");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 2; i < 7; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("{0}", decoded.CatalogNumber[i]);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append("-");
|
|
|
|
|
sb.AppendFormat("{0}", decoded.CatalogNumber[7]);
|
|
|
|
|
sb.AppendLine();
|
2017-05-23 01:30:57 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("Timestamp: {0}", DateTime.FromFileTimeUtc(decoded.Timestamp)).AppendLine();
|
2017-05-23 01:30:57 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2017-05-23 01:30:57 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public static string PrettifyXbox360(Xbox360DMI? dmi)
|
|
|
|
|
{
|
|
|
|
|
if(dmi == null)
|
|
|
|
|
return null;
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
Xbox360DMI decoded = dmi.Value;
|
|
|
|
|
var sb = new StringBuilder();
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append("Catalogue number: ");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 0; i < 2; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("{0}", decoded.CatalogNumber[i]);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append("-");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 2; i < 6; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("{0}", decoded.CatalogNumber[i]);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append("-");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 6; i < 8; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("{0}", decoded.CatalogNumber[i]);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append("-");
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
switch(decoded.CatalogNumber.Length)
|
|
|
|
|
{
|
|
|
|
|
case 13:
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 8; i < 10; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("{0}", decoded.CatalogNumber[i]);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append("-");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 10; i < 13; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("{0}", decoded.CatalogNumber[i]);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
break;
|
|
|
|
|
case 14:
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 8; i < 11; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("{0}", decoded.CatalogNumber[i]);
|
2017-12-21 04:43:29 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append("-");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 11; i < 14; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("{0}", decoded.CatalogNumber[i]);
|
2017-12-21 04:43:29 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 8; i < decoded.CatalogNumber.Length - 3; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("{0}", decoded.CatalogNumber[i]);
|
2017-12-21 04:43:29 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append("-");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
for(int i = decoded.CatalogNumber.Length - 3; i < decoded.CatalogNumber.Length; i++)
|
|
|
|
|
sb.AppendFormat("{0}", decoded.CatalogNumber[i]);
|
2017-12-21 04:43:29 +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.AppendLine();
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append("Media ID: ");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 0; i < 12; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("{0:X2}", decoded.MediaID[i]);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append("-");
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
for(int i = 12; i < 16; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("{0:X2}", decoded.MediaID[i]);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendLine();
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat("Timestamp: {0}", DateTime.FromFileTimeUtc(decoded.Timestamp)).AppendLine();
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2015-12-03 08:02:12 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public static string PrettifyXbox(byte[] response) => PrettifyXbox(DecodeXbox(response));
|
2017-05-23 01:30:57 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public static string PrettifyXbox360(byte[] response) => PrettifyXbox360(DecodeXbox360(response));
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public struct XboxDMI
|
|
|
|
|
{
|
|
|
|
|
/// <summary>Bytes 0 to 1 Data length</summary>
|
|
|
|
|
public ushort DataLength;
|
|
|
|
|
/// <summary>Byte 2 Reserved</summary>
|
|
|
|
|
public byte Reserved1;
|
|
|
|
|
/// <summary>Byte 3 Reserved</summary>
|
|
|
|
|
public byte Reserved2;
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Bytes 4 to 7 0x01 in XGD</summary>
|
|
|
|
|
public uint Version;
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Bytes 12 to 16 Catalogue number in XX-XXXXX-X</summary>
|
|
|
|
|
public string CatalogNumber;
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Bytes 20 to 27 DMI timestamp</summary>
|
|
|
|
|
public long Timestamp;
|
|
|
|
|
}
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
public struct Xbox360DMI
|
|
|
|
|
{
|
|
|
|
|
/// <summary>Bytes 0 to 1 Data length</summary>
|
|
|
|
|
public ushort DataLength;
|
|
|
|
|
/// <summary>Byte 2 Reserved</summary>
|
|
|
|
|
public byte Reserved1;
|
|
|
|
|
/// <summary>Byte 3 Reserved</summary>
|
|
|
|
|
public byte Reserved2;
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Bytes 4 to 7 0x02 in XGD2 and XGD3</summary>
|
|
|
|
|
public uint Version;
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Bytes 20 to 27 DMI timestamp</summary>
|
|
|
|
|
public long Timestamp;
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Bytes 36 to 51 Media ID in hex XXXXXXXXXXXX-XXXXXXXX</summary>
|
|
|
|
|
public byte[] MediaID;
|
2019-11-25 00:54:38 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Bytes 68 to 83 Catalogue number in XX-XXXX-XX-XXY-XXX, Y not always exists</summary>
|
|
|
|
|
public string CatalogNumber;
|
2015-12-03 08:02:12 +00:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|