2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-10-16 07:29:19 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : CIS.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Component : Device structures decoders.
|
2016-10-16 07:29:19 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-29 17:34:38 +00:00
|
|
|
// Copyright © 2011-2019 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;
|
2017-12-22 02:04:18 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2017-12-21 07:08:26 +00:00
|
|
|
using System.Linq;
|
2016-10-17 04:41:27 +01:00
|
|
|
using System.Text;
|
|
|
|
|
|
2016-10-16 07:29:19 +01:00
|
|
|
namespace DiscImageChef.Decoders.PCMCIA
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
[SuppressMessage("ReSharper", "InconsistentNaming")]
|
|
|
|
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
|
|
|
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
|
2016-10-17 04:41:27 +01:00
|
|
|
public static class CIS
|
2016-10-16 07:29:19 +01:00
|
|
|
{
|
2016-10-17 04:41:27 +01: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
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
List<Tuple> tuples = new List<Tuple>();
|
|
|
|
|
int position = 0;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
|
|
|
|
while(position < data.Length)
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
Tuple tuple = new Tuple {Code = (TupleCodes)data[position]};
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(tuple.Code == TupleCodes.CISTPL_NULL) continue;
|
|
|
|
|
|
|
|
|
|
if(tuple.Code == TupleCodes.CISTPL_END) break;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
|
|
|
|
tuple.Link = data[position + 1];
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(position + 2 + tuple.Link > data.Length) break;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
|
|
|
|
tuple.Data = new byte[tuple.Link + 2];
|
|
|
|
|
Array.Copy(data, position, tuple.Data, 0, tuple.Link + 2);
|
|
|
|
|
|
|
|
|
|
tuples.Add(tuple);
|
|
|
|
|
position += tuple.Link + 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tuples.ToArray();
|
2016-10-16 07:29:19 +01:00
|
|
|
}
|
2016-10-17 04:41:27 +01:00
|
|
|
|
|
|
|
|
public static DeviceGeometryTuple DecodeDeviceGeometryTuple(Tuple tuple)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(tuple == null) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(tuple.Code != TupleCodes.CISTPL_DEVICEGEO && tuple.Code != TupleCodes.CISTPL_DEVICEGEO_A) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
return tuple.Data == null ? null : DecodeDeviceGeometryTuple(tuple.Data);
|
2016-10-17 04:41:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DeviceGeometryTuple DecodeDeviceGeometryTuple(byte[] data)
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
if((data?.Length - 2) % 6 != 0) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
DeviceGeometryTuple tuple = new DeviceGeometryTuple();
|
2016-10-17 04:41:27 +01:00
|
|
|
List<DeviceGeometry> geometries = new List<DeviceGeometry>();
|
|
|
|
|
|
|
|
|
|
for(int position = 2; position < data.Length; position += 6)
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
DeviceGeometry geometry = new DeviceGeometry
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
CardInterface = data[position],
|
2017-12-22 02:04:18 +00:00
|
|
|
EraseBlockSize = data[position + 1],
|
2018-06-22 08:08:38 +01:00
|
|
|
ReadBlockSize = data[position + 2],
|
2017-12-22 02:04:18 +00:00
|
|
|
WriteBlockSize = data[position + 3],
|
2018-06-22 08:08:38 +01:00
|
|
|
Partitions = data[position + 4],
|
|
|
|
|
Interleaving = data[position + 5]
|
2017-12-22 02:04:18 +00:00
|
|
|
};
|
2016-10-17 04:41:27 +01:00
|
|
|
geometries.Add(geometry);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
tuple.Code = (TupleCodes)data[0];
|
|
|
|
|
tuple.Link = data[1];
|
2016-10-17 04:41:27 +01:00
|
|
|
tuple.Geometries = geometries.ToArray();
|
|
|
|
|
|
|
|
|
|
return tuple;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string PrettifyDeviceGeometryTuple(DeviceGeometryTuple tuple)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(tuple == null) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(tuple.Code != TupleCodes.CISTPL_DEVICEGEO && tuple.Code != TupleCodes.CISTPL_DEVICEGEO_A) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
sb.AppendLine("PCMCIA Device Geometry Tuples:");
|
|
|
|
|
foreach(DeviceGeometry geometry in tuple.Geometries)
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine("\tGeometry:");
|
|
|
|
|
sb.AppendFormat("\t\tDevice width: {0} bits", (1 << (geometry.CardInterface - 1)) * 8).AppendLine();
|
2017-12-19 20:33:03 +00:00
|
|
|
sb.AppendFormat("\t\tErase block = {0} bytes",
|
|
|
|
|
(1 << (geometry.EraseBlockSize - 1)) * (1 << (geometry.Interleaving - 1))).AppendLine();
|
|
|
|
|
sb.AppendFormat("\t\tRead block = {0} bytes",
|
|
|
|
|
(1 << (geometry.ReadBlockSize - 1)) * (1 << (geometry.Interleaving - 1))).AppendLine();
|
|
|
|
|
sb.AppendFormat("\t\tWrite block = {0} bytes",
|
|
|
|
|
(1 << (geometry.WriteBlockSize - 1)) * (1 << (geometry.Interleaving - 1))).AppendLine();
|
|
|
|
|
sb.AppendFormat("\t\tPartition alignment = {0} bytes",
|
|
|
|
|
(1 << (geometry.EraseBlockSize - 1)) * (1 << (geometry.Interleaving - 1)) *
|
2018-06-22 08:08:38 +01:00
|
|
|
(1 << (geometry.Partitions - 1))).AppendLine();
|
2016-10-17 04:41:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string PrettifyDeviceGeometryTuple(Tuple tuple)
|
|
|
|
|
{
|
|
|
|
|
return PrettifyDeviceGeometryTuple(DecodeDeviceGeometryTuple(tuple));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string PrettifyDeviceGeometryTuple(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
return PrettifyDeviceGeometryTuple(DecodeDeviceGeometryTuple(data));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ManufacturerIdentificationTuple DecodeManufacturerIdentificationTuple(Tuple tuple)
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
if(tuple?.Code != TupleCodes.CISTPL_MANFID) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
return tuple.Data == null ? null : DecodeManufacturerIdentificationTuple(tuple.Data);
|
2016-10-17 04:41:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ManufacturerIdentificationTuple DecodeManufacturerIdentificationTuple(byte[] data)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(data == null) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(data.Length < 6) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
return new ManufacturerIdentificationTuple
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
Code = (TupleCodes)data[0],
|
|
|
|
|
Link = data[1],
|
2017-12-22 02:04:18 +00:00
|
|
|
ManufacturerID = BitConverter.ToUInt16(data, 2),
|
2018-06-22 08:08:38 +01:00
|
|
|
CardID = BitConverter.ToUInt16(data, 4)
|
2017-12-22 02:04:18 +00:00
|
|
|
};
|
2016-10-17 04:41:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string PrettifyManufacturerIdentificationTuple(ManufacturerIdentificationTuple tuple)
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
if(tuple?.Code != TupleCodes.CISTPL_MANFID) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
sb.AppendLine("PCMCIA Manufacturer Identification Tuple:");
|
|
|
|
|
sb.AppendFormat("\tManufacturer ID: {0}", VendorCode.Prettify(tuple.ManufacturerID)).AppendLine();
|
|
|
|
|
sb.AppendFormat("\tCard ID:D 0x{0:X4}", tuple.CardID).AppendLine();
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string PrettifyManufacturerIdentificationTuple(Tuple tuple)
|
|
|
|
|
{
|
|
|
|
|
return PrettifyManufacturerIdentificationTuple(DecodeManufacturerIdentificationTuple(tuple));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string PrettifyManufacturerIdentificationTuple(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
return PrettifyManufacturerIdentificationTuple(DecodeManufacturerIdentificationTuple(data));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Level1VersionTuple DecodeLevel1VersionTuple(Tuple tuple)
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
if(tuple?.Code != TupleCodes.CISTPL_VERS_1) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
return tuple.Data == null ? null : DecodeLevel1VersionTuple(tuple.Data);
|
2016-10-17 04:41:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Level1VersionTuple DecodeLevel1VersionTuple(byte[] data)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(data == null) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(data.Length < 4) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
List<byte> buffer = new List<byte>();
|
|
|
|
|
List<string> strings = null;
|
|
|
|
|
bool firstString = false;
|
|
|
|
|
bool secondString = false;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
Level1VersionTuple tuple = new Level1VersionTuple
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
Code = (TupleCodes)data[0],
|
|
|
|
|
Link = data[1],
|
2017-12-22 02:04:18 +00:00
|
|
|
MajorVersion = data[2],
|
|
|
|
|
MinorVersion = data[3]
|
|
|
|
|
};
|
2016-10-17 04:41:27 +01:00
|
|
|
|
|
|
|
|
for(int position = 4; position < data.Length; position++)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(data[position] == 0xFF) break;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
|
|
|
|
buffer.Add(data[position]);
|
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(data[position] != 0x00) continue;
|
|
|
|
|
|
|
|
|
|
if(!firstString)
|
2016-10-17 04:41:27 +01:00
|
|
|
{
|
2017-12-21 06:06:19 +00:00
|
|
|
tuple.Manufacturer = StringHandlers.CToString(buffer.ToArray());
|
2018-06-22 08:08:38 +01:00
|
|
|
buffer = new List<byte>();
|
|
|
|
|
firstString = true;
|
2017-12-21 06:06:19 +00:00
|
|
|
continue;
|
2016-10-17 04:41:27 +01:00
|
|
|
}
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2017-12-21 16:07:20 +00:00
|
|
|
// TODO: Check this
|
2017-12-21 06:06:19 +00:00
|
|
|
if(!secondString)
|
|
|
|
|
{
|
|
|
|
|
tuple.Product = StringHandlers.CToString(buffer.ToArray());
|
2018-06-22 08:08:38 +01:00
|
|
|
buffer = new List<byte>();
|
|
|
|
|
firstString = true;
|
2017-12-21 06:06:19 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(strings == null) strings = new List<string>();
|
|
|
|
|
|
|
|
|
|
strings.Add(StringHandlers.CToString(buffer.ToArray()));
|
|
|
|
|
buffer = new List<byte>();
|
2016-10-17 04:41:27 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(strings != null) tuple.AdditionalInformation = strings.ToArray();
|
2016-10-17 04:41:27 +01:00
|
|
|
|
|
|
|
|
return tuple;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string PrettifyLevel1VersionTuple(Level1VersionTuple tuple)
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
if(tuple?.Code != TupleCodes.CISTPL_VERS_1) return null;
|
2016-10-17 04:41:27 +01:00
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
sb.AppendLine("PCMCIA Level 1 Version / Product Information Tuple:");
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
sb.AppendFormat("\tCard indicates compliance with PC Card Standard Release {0}.{1}", tuple.MajorVersion,
|
|
|
|
|
tuple.MinorVersion).AppendLine();
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(string.IsNullOrEmpty(tuple.Manufacturer)) sb.AppendLine("\tNo manufacturer information string.");
|
|
|
|
|
else sb.AppendFormat("\tManufacturer: {0}", tuple.Manufacturer).AppendLine();
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(string.IsNullOrEmpty(tuple.Product)) sb.AppendLine("\tNo product name string.");
|
|
|
|
|
else sb.AppendFormat("\tProduct name: {0}", tuple.Product).AppendLine();
|
2016-10-17 04:41:27 +01:00
|
|
|
|
|
|
|
|
if(tuple.AdditionalInformation == null || tuple.AdditionalInformation.Length == 0)
|
|
|
|
|
sb.AppendLine("\tNo additional information.");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine("\tAdditional information:");
|
2017-12-23 18:31:38 +00:00
|
|
|
foreach(string info in tuple.AdditionalInformation.Where(info => !string.IsNullOrEmpty(info)))
|
|
|
|
|
sb.AppendFormat("\t\t{0}", info).AppendLine();
|
2016-10-17 04:41:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string PrettifyLevel1VersionTuple(Tuple tuple)
|
|
|
|
|
{
|
|
|
|
|
return PrettifyLevel1VersionTuple(DecodeLevel1VersionTuple(tuple));
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2016-10-17 04:41:27 +01:00
|
|
|
public static string PrettifyLevel1VersionTuple(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
return PrettifyLevel1VersionTuple(DecodeLevel1VersionTuple(data));
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
}
|