Files
Aaru/PCMCIA/CIS.cs

323 lines
11 KiB
C#
Raw Normal View History

2017-05-19 20:28:49 +01:00
// /***************************************************************************
2020-02-27 12:31:23 +00:00
// Aaru Data Preservation Suite
2016-10-16 07:29:19 +01:00
// ----------------------------------------------------------------------------
//
// Filename : CIS.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Device structures decoders.
2016-10-16 07:29:19 +01:00
//
// --[ Description ] ----------------------------------------------------------
//
// Decodes PCMCIA Card Information Structure.
2016-10-16 07:29:19 +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-12-03 16:07:08 +00:00
// Copyright © 2011-2023 Natalia Portillo
2016-10-16 07:29:19 +01:00
// ****************************************************************************/
2016-10-17 04:41:27 +01:00
2016-10-16 07:29:19 +01:00
using System;
2016-10-17 04:41:27 +01:00
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
2016-10-17 04:41:27 +01:00
using System.Text;
2020-07-20 15:43:51 +01:00
using Aaru.Helpers;
2016-10-17 04:41:27 +01:00
namespace Aaru.Decoders.PCMCIA;
2023-10-03 23:09:28 +01:00
[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
2022-03-06 13:29:37 +00:00
public static class CIS
2016-10-16 07:29:19 +01:00
{
2022-03-06 13:29:37 +00:00
// TODO: Handle links? Or are they removed in lower layers of the operating system drivers?
public static Tuple[] GetTuples(byte[] data)
2016-10-16 07:29:19 +01:00
{
List<Tuple> tuples = new();
2023-10-03 23:09:28 +01:00
var position = 0;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
while(position < data.Length)
{
var tuple = new Tuple
2016-10-17 04:41:27 +01:00
{
2022-03-06 13:29:37 +00:00
Code = (TupleCodes)data[position]
};
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:37 +00:00
if(tuple.Code == TupleCodes.CISTPL_NULL)
continue;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
if(tuple.Code == TupleCodes.CISTPL_END)
break;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
tuple.Link = data[position + 1];
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
if(position + 2 + tuple.Link > data.Length)
break;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
tuple.Data = new byte[tuple.Link + 2];
Array.Copy(data, position, tuple.Data, 0, tuple.Link + 2);
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
tuples.Add(tuple);
position += tuple.Link + 2;
2016-10-16 07:29:19 +01:00
}
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
return tuples.ToArray();
}
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static DeviceGeometryTuple DecodeDeviceGeometryTuple(Tuple tuple)
{
if(tuple == null)
return null;
2016-10-17 04:41:27 +01:00
if(tuple.Code != TupleCodes.CISTPL_DEVICEGEO && tuple.Code != TupleCodes.CISTPL_DEVICEGEO_A)
2022-03-06 13:29:37 +00:00
return null;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
return tuple.Data == null ? null : DecodeDeviceGeometryTuple(tuple.Data);
}
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static DeviceGeometryTuple DecodeDeviceGeometryTuple(byte[] data)
{
if((data?.Length - 2) % 6 != 0)
return null;
2016-10-17 04:41:27 +01:00
var tuple = new DeviceGeometryTuple();
List<DeviceGeometry> geometries = new();
2016-10-17 04:41:27 +01:00
2023-10-03 23:09:28 +01:00
for(var position = 2; position < data.Length; position += 6)
2022-03-06 13:29:37 +00:00
{
var geometry = new DeviceGeometry
{
CardInterface = data[position],
EraseBlockSize = data[position + 1],
ReadBlockSize = data[position + 2],
WriteBlockSize = data[position + 3],
Partitions = data[position + 4],
Interleaving = data[position + 5]
};
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
geometries.Add(geometry);
2016-10-17 04:41:27 +01:00
}
2022-03-06 13:29:37 +00:00
tuple.Code = (TupleCodes)data[0];
tuple.Link = data[1];
tuple.Geometries = geometries.ToArray();
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
return tuple;
}
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static string PrettifyDeviceGeometryTuple(DeviceGeometryTuple tuple)
{
if(tuple == null)
return null;
2019-11-25 00:54:38 +00:00
if(tuple.Code != TupleCodes.CISTPL_DEVICEGEO && tuple.Code != TupleCodes.CISTPL_DEVICEGEO_A)
2022-03-06 13:29:37 +00:00
return null;
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
var sb = new StringBuilder();
sb.AppendLine(Localization.PCMCIA_Device_Geometry_Tuples);
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
foreach(DeviceGeometry geometry in tuple.Geometries)
{
sb.AppendLine("\t" + Localization.Geometry);
2019-11-25 00:54:38 +00:00
2023-10-03 23:09:28 +01:00
sb.AppendFormat("\t\t" + Localization.Device_width_0_bits, (1 << geometry.CardInterface - 1) * 8).
AppendLine();
sb.AppendFormat("\t\t" + Localization.Erase_block_0_bytes,
(1 << geometry.EraseBlockSize - 1) * (1 << geometry.Interleaving - 1)).
AppendLine();
2019-11-25 00:54:38 +00:00
sb.AppendFormat("\t\t" + Localization.Read_block_0_bytes,
(1 << geometry.ReadBlockSize - 1) * (1 << geometry.Interleaving - 1)).
AppendLine();
2016-10-17 04:41:27 +01:00
sb.AppendFormat("\t\t" + Localization.Write_block_0_bytes,
(1 << geometry.WriteBlockSize - 1) * (1 << geometry.Interleaving - 1)).
AppendLine();
2022-03-06 13:29:37 +00:00
sb.AppendFormat("\t\t" + Localization.Partition_alignment_0_bytes,
(1 << geometry.EraseBlockSize - 1) *
(1 << geometry.Interleaving - 1) *
(1 << geometry.Partitions - 1)).
AppendLine();
2016-10-17 04:41:27 +01:00
}
2022-03-06 13:29:37 +00:00
return sb.ToString();
}
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static string PrettifyDeviceGeometryTuple(Tuple tuple) =>
PrettifyDeviceGeometryTuple(DecodeDeviceGeometryTuple(tuple));
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static string PrettifyDeviceGeometryTuple(byte[] data) =>
PrettifyDeviceGeometryTuple(DecodeDeviceGeometryTuple(data));
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static ManufacturerIdentificationTuple DecodeManufacturerIdentificationTuple(Tuple tuple)
{
if(tuple?.Code != TupleCodes.CISTPL_MANFID)
return null;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
return tuple.Data == null ? null : DecodeManufacturerIdentificationTuple(tuple.Data);
}
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static ManufacturerIdentificationTuple DecodeManufacturerIdentificationTuple(byte[] data)
{
if(data == null)
return null;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
if(data.Length < 6)
return null;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
return new ManufacturerIdentificationTuple
2016-10-17 04:41:27 +01:00
{
2022-03-06 13:29:37 +00:00
Code = (TupleCodes)data[0],
Link = data[1],
ManufacturerID = BitConverter.ToUInt16(data, 2),
CardID = BitConverter.ToUInt16(data, 4)
};
}
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static string PrettifyManufacturerIdentificationTuple(ManufacturerIdentificationTuple tuple)
{
if(tuple?.Code != TupleCodes.CISTPL_MANFID)
return null;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
var sb = new StringBuilder();
sb.AppendLine(Localization.Manufacturer_Identification_Tuple);
sb.AppendFormat("\t" + Localization.Manufacturer_ID_0, VendorCode.Prettify(tuple.ManufacturerID)).AppendLine();
2023-10-03 23:09:28 +01:00
sb.AppendFormat("\t" + Localization.Card_ID_0, tuple.CardID).AppendLine();
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
return sb.ToString();
}
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static string PrettifyManufacturerIdentificationTuple(Tuple tuple) =>
PrettifyManufacturerIdentificationTuple(DecodeManufacturerIdentificationTuple(tuple));
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static string PrettifyManufacturerIdentificationTuple(byte[] data) =>
PrettifyManufacturerIdentificationTuple(DecodeManufacturerIdentificationTuple(data));
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static Level1VersionTuple DecodeLevel1VersionTuple(Tuple tuple)
{
if(tuple?.Code != TupleCodes.CISTPL_VERS_1)
return null;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
return tuple.Data == null ? null : DecodeLevel1VersionTuple(tuple.Data);
}
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static Level1VersionTuple DecodeLevel1VersionTuple(byte[] data)
{
if(data == null)
return null;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
if(data.Length < 4)
return null;
2016-10-17 04:41:27 +01:00
List<byte> buffer = new();
2022-03-06 13:29:37 +00:00
List<string> strings = null;
2023-10-03 23:09:28 +01:00
var firstString = false;
const bool secondString = false;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
var tuple = new Level1VersionTuple
{
Code = (TupleCodes)data[0],
Link = data[1],
MajorVersion = data[2],
MinorVersion = data[3]
};
2016-10-17 04:41:27 +01:00
2023-10-03 23:09:28 +01:00
for(var position = 4; position < data.Length; position++)
2022-03-06 13:29:37 +00:00
{
if(data[position] == 0xFF)
break;
2022-03-06 13:29:37 +00:00
buffer.Add(data[position]);
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
if(data[position] != 0x00)
continue;
2022-03-06 13:29:37 +00:00
if(!firstString)
{
tuple.Manufacturer = StringHandlers.CToString(buffer.ToArray());
buffer = new List<byte>();
firstString = true;
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
continue;
}
2022-03-06 13:29:37 +00:00
// TODO: Check this
if(!secondString)
{
tuple.Product = StringHandlers.CToString(buffer.ToArray());
buffer = new List<byte>();
firstString = true;
2022-03-06 13:29:37 +00:00
continue;
2016-10-17 04:41:27 +01:00
}
2022-03-06 13:29:37 +00:00
if(strings == null)
strings = new List<string>();
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
strings.Add(StringHandlers.CToString(buffer.ToArray()));
buffer = new List<byte>();
2016-10-17 04:41:27 +01:00
}
2022-03-06 13:29:37 +00:00
if(strings != null)
tuple.AdditionalInformation = strings.ToArray();
return tuple;
}
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
public static string PrettifyLevel1VersionTuple(Level1VersionTuple tuple)
{
if(tuple?.Code != TupleCodes.CISTPL_VERS_1)
return null;
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
var sb = new StringBuilder();
sb.AppendLine(Localization.PCMCIA_Level_1_Version_Product_Information_Tuple);
2016-10-17 04:41:27 +01:00
sb.AppendFormat("\t" + Localization.Card_indicates_compliance_with_PC_Card_Standard_Release_0_1,
tuple.MajorVersion, tuple.MinorVersion).
AppendLine();
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
if(string.IsNullOrEmpty(tuple.Manufacturer))
sb.AppendLine("\t" + Localization.No_manufacturer_information_string);
2022-03-06 13:29:37 +00:00
else
sb.AppendFormat(Localization.Manufacturer_0, tuple.Manufacturer).AppendLine();
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
if(string.IsNullOrEmpty(tuple.Product))
sb.AppendLine("\t" + Localization.No_product_name_string);
2022-03-06 13:29:37 +00:00
else
sb.AppendFormat(Localization.Product_name_0, tuple.Product).AppendLine();
2019-11-25 00:54:38 +00:00
if(tuple.AdditionalInformation == null || tuple.AdditionalInformation.Length == 0)
sb.AppendLine("\t" + Localization.No_additional_information);
2022-03-06 13:29:37 +00:00
else
{
sb.AppendLine("\t" + Localization.Additional_information);
2016-10-17 04:41:27 +01:00
2022-03-06 13:29:37 +00:00
foreach(string info in tuple.AdditionalInformation.Where(info => !string.IsNullOrEmpty(info)))
2023-10-04 08:16:20 +01:00
sb.Append($"\t\t{info}").AppendLine();
2016-10-17 04:41:27 +01:00
}
2022-03-06 13:29:37 +00:00
return sb.ToString();
2017-12-19 20:33:03 +00:00
}
2022-03-06 13:29:37 +00:00
public static string PrettifyLevel1VersionTuple(Tuple tuple) =>
PrettifyLevel1VersionTuple(DecodeLevel1VersionTuple(tuple));
public static string PrettifyLevel1VersionTuple(byte[] data) =>
PrettifyLevel1VersionTuple(DecodeLevel1VersionTuple(data));
2017-12-19 20:33:03 +00:00
}