2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2015-10-19 02:28:40 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : DDS.cs
|
2016-07-28 18:13:49 +01:00
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2015-10-19 02:28:40 +01:00
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Component : Device structures decoders.
|
2015-10-19 02:28:40 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Decodes DVD Disc Definition Structure.
|
2015-10-19 02:28:40 +01: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-10-19 02:28:40 +01: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-10-19 02:28:40 +01: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-10-19 02:28:40 +01:00
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-29 17:34:38 +00:00
|
|
|
// Copyright © 2011-2019 Natalia Portillo
|
2015-10-19 02:28:40 +01:00
|
|
|
// ****************************************************************************/
|
2016-07-28 18:13:49 +01:00
|
|
|
|
2015-10-19 02:28:40 +01:00
|
|
|
using System;
|
2017-12-22 02:04:18 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2015-12-02 05:56:50 +00:00
|
|
|
using System.Text;
|
2015-10-19 02:28:40 +01:00
|
|
|
|
|
|
|
|
namespace DiscImageChef.Decoders.DVD
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Information from the following standards:
|
|
|
|
|
/// ANSI X3.304-1997
|
|
|
|
|
/// T10/1048-D revision 9.0
|
|
|
|
|
/// T10/1048-D revision 10a
|
|
|
|
|
/// T10/1228-D revision 7.0c
|
|
|
|
|
/// T10/1228-D revision 11a
|
|
|
|
|
/// T10/1363-D revision 10g
|
|
|
|
|
/// T10/1545-D revision 1d
|
|
|
|
|
/// T10/1545-D revision 5
|
|
|
|
|
/// T10/1545-D revision 5a
|
|
|
|
|
/// T10/1675-D revision 2c
|
|
|
|
|
/// T10/1675-D revision 4
|
|
|
|
|
/// T10/1836-D revision 2g
|
|
|
|
|
/// ECMA 272: 120 mm DVD Rewritable Disk (DVD-RAM)
|
|
|
|
|
/// ECMA 330: 120 mm (4,7 Gbytes per side) and 80 mm (1,46 Gbytes per side) DVD Rewritable Disk (DVD-RAM)
|
2015-10-19 02:28:40 +01:00
|
|
|
/// </summary>
|
2017-12-22 02:04:18 +00:00
|
|
|
[SuppressMessage("ReSharper", "InconsistentNaming")]
|
|
|
|
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
|
|
|
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
|
|
|
|
|
[SuppressMessage("ReSharper", "NotAccessedField.Global")]
|
2015-10-19 02:28:40 +01:00
|
|
|
public static class DDS
|
|
|
|
|
{
|
|
|
|
|
public struct DiscDefinitionStructure
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bytes 0 to 1
|
|
|
|
|
/// Data length
|
2015-10-19 02:28:40 +01:00
|
|
|
/// </summary>
|
2016-07-28 22:25:26 +01:00
|
|
|
public ushort DataLength;
|
2015-10-19 02:28:40 +01:00
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Byte 2
|
|
|
|
|
/// Reserved
|
2015-10-19 02:28:40 +01:00
|
|
|
/// </summary>
|
|
|
|
|
public byte Reserved1;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Byte 3
|
|
|
|
|
/// Reserved
|
2015-10-19 02:28:40 +01:00
|
|
|
/// </summary>
|
|
|
|
|
public byte Reserved2;
|
2015-12-02 05:56:50 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bytes 4 to 5
|
|
|
|
|
/// DDS Identifier = 0x0A0A
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public ushort Identifier;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Byte 6
|
|
|
|
|
/// Reserved
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public byte Reserved3;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Byte 7, bit 7
|
|
|
|
|
/// If set, formatting is in process
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public bool InProcess;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Byte 7, bit 6
|
|
|
|
|
/// If set, formatting is using partial certification
|
|
|
|
|
/// Only in ECMA-272
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public bool PartialCertification;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Byte 7, bit 5
|
|
|
|
|
/// If set, only a group is being formatted
|
|
|
|
|
/// Only in ECMA-272
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public bool FormattingOnlyAGroup;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Byte 7, bits 4 to 2
|
|
|
|
|
/// Reserved
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public byte Reserved4;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Byte 7, bit 1
|
|
|
|
|
/// If set, disk has been certified by a user
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public bool UserCertification;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Byte 7, bit 0
|
|
|
|
|
/// If set, disk has been certified by a manufacturer
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public bool ManufacturerCertification;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bytes 8 to 11
|
|
|
|
|
/// How many times the DDS has been updated
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public uint UpdateCount;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bytes 12 to 13
|
|
|
|
|
/// How many groups the disk has
|
|
|
|
|
/// 24 for ECMA-272
|
|
|
|
|
/// 1 for ECMA-330
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public ushort Groups;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bytes 14 to 15
|
|
|
|
|
/// How many zones the disk has
|
|
|
|
|
/// Only in ECMA-330
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public ushort Zones;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bytes 14 to 19 in ECMA-272
|
|
|
|
|
/// Bytes 16 to 83 in ECMA-330
|
|
|
|
|
/// Reserved
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public byte[] Reserved;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bytes 20 to 43
|
|
|
|
|
/// Group certification flags
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public GroupCertificationFlag[] GroupCertificationFlags;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bytes 85 to 87
|
|
|
|
|
/// Location of first sector in the Primary Spare Area
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public uint SpareAreaFirstPSN;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bytes 89 to 91
|
|
|
|
|
/// Location of first sector in the Primary Spare Area
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public uint SpareAreaLastPSN;
|
2015-10-19 02:28:40 +01:00
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bytes 93 to 95
|
|
|
|
|
/// PSN for LSN 0
|
2015-10-19 02:28:40 +01:00
|
|
|
/// </summary>
|
2015-12-02 05:56:50 +00:00
|
|
|
public uint LSN0Location;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// The starting LSN of each zone
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public uint[] StartLSNForZone;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public struct GroupCertificationFlag
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bit 7
|
|
|
|
|
/// If set, formatting of this group is in process
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public bool InProcess;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bit 6
|
|
|
|
|
/// If set, formatting is using partial certification
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public bool PartialCertification;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bits 5 to 2
|
|
|
|
|
/// Reserved
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public byte Reserved1;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bit 1
|
|
|
|
|
/// If set, this group has been certified by user
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public bool UserCertification;
|
|
|
|
|
/// <summary>
|
2017-12-23 18:31:38 +00:00
|
|
|
/// Bit 0
|
|
|
|
|
/// Reserved
|
2015-12-02 05:56:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public bool Reserved2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DiscDefinitionStructure? Decode(byte[] response)
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
if(response?.Length != 2052) return null;
|
2015-12-02 05:56:50 +00:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
DiscDefinitionStructure dds =
|
|
|
|
|
new DiscDefinitionStructure {Identifier = (ushort)((response[4] << 8) + response[5])};
|
2015-12-02 05:56:50 +00:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(dds.Identifier != 0x0A0A) return null;
|
2015-12-02 05:56:50 +00:00
|
|
|
|
|
|
|
|
// Common to both DVD-RAM versions
|
2018-06-22 08:08:38 +01:00
|
|
|
dds.DataLength = (ushort)((response[0] << 8) + response[1]);
|
|
|
|
|
dds.Reserved1 = response[2];
|
|
|
|
|
dds.Reserved2 = response[3];
|
|
|
|
|
dds.Reserved3 = response[6];
|
|
|
|
|
dds.InProcess |= (response[7] & 0x80) == 0x80;
|
|
|
|
|
dds.UserCertification |= (response[7] & 0x02) == 0x02;
|
2015-12-02 05:56:50 +00:00
|
|
|
dds.ManufacturerCertification |= (response[7] & 0x01) == 0x01;
|
2018-06-22 08:08:38 +01:00
|
|
|
dds.UpdateCount =
|
|
|
|
|
(uint)((response[8] << 24) + (response[9] << 16) + (response[10] << 8) + response[11]);
|
|
|
|
|
dds.Groups =
|
|
|
|
|
(ushort)((response[12] << 8) + response[13]);
|
2015-12-02 05:56:50 +00:00
|
|
|
|
|
|
|
|
// ECMA-272
|
2016-04-19 02:11:47 +01:00
|
|
|
if(dds.Groups == 24)
|
2015-12-02 05:56:50 +00:00
|
|
|
{
|
|
|
|
|
dds.PartialCertification |= (response[7] & 0x40) == 0x40;
|
|
|
|
|
dds.FormattingOnlyAGroup |= (response[7] & 0x20) == 0x20;
|
2018-06-22 08:08:38 +01:00
|
|
|
dds.Reserved4 = (byte)((response[7] & 0x1C) >> 2);
|
|
|
|
|
dds.Reserved = new byte[6];
|
2015-12-02 05:56:50 +00:00
|
|
|
Array.Copy(response, 14, dds.Reserved, 0, 6);
|
|
|
|
|
dds.GroupCertificationFlags = new GroupCertificationFlag[24];
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < 24; i++)
|
2015-12-02 05:56:50 +00:00
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
dds.GroupCertificationFlags[i].InProcess |= (response[20 + i] & 0x80) == 0x80;
|
2015-12-02 05:56:50 +00:00
|
|
|
dds.GroupCertificationFlags[i].PartialCertification |= (response[20 + i] & 0x40) == 0x40;
|
2018-06-22 08:08:38 +01:00
|
|
|
dds.GroupCertificationFlags[i].Reserved1 = (byte)((response[20 + i] & 0x3C) >> 2);
|
|
|
|
|
dds.GroupCertificationFlags[i].UserCertification |= (response[20 + i] & 0x02) == 0x02;
|
|
|
|
|
dds.GroupCertificationFlags[i].Reserved2 |= (response[20 + i] & 0x01) == 0x01;
|
2015-12-02 05:56:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ECMA-330
|
2017-12-21 06:06:19 +00:00
|
|
|
if(dds.Groups != 1) return dds;
|
|
|
|
|
|
2015-12-02 05:56:50 +00:00
|
|
|
{
|
|
|
|
|
dds.Reserved4 = (byte)((response[7] & 0x7C) >> 2);
|
2018-06-22 08:08:38 +01:00
|
|
|
dds.Reserved = new byte[68];
|
2015-12-02 05:56:50 +00:00
|
|
|
Array.Copy(response, 16, dds.Reserved, 0, 68);
|
2018-06-22 08:08:38 +01:00
|
|
|
dds.Zones = (ushort)((response[14] << 8) + response[15]);
|
2015-12-02 05:56:50 +00:00
|
|
|
dds.SpareAreaFirstPSN = (uint)((response[85] << 16) + (response[86] << 8) + response[87]);
|
2018-06-22 08:08:38 +01:00
|
|
|
dds.SpareAreaLastPSN = (uint)((response[89] << 16) + (response[90] << 8) + response[91]);
|
|
|
|
|
dds.LSN0Location = (uint)((response[93] << 16) + (response[94] << 8) + response[95]);
|
|
|
|
|
dds.StartLSNForZone = new uint[dds.Zones];
|
2015-12-02 05:56:50 +00:00
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < dds.Zones; i++)
|
2017-12-19 20:33:03 +00:00
|
|
|
dds.StartLSNForZone[i] = (uint)((response[260 + i * 4 + 1] << 16) +
|
2018-06-22 08:08:38 +01:00
|
|
|
(response[260 + i * 4 + 2] << 8) + response[260 + i * 4 + 3]);
|
2015-12-02 05:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Prettify(DiscDefinitionStructure? dds)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(dds == null) return null;
|
2015-12-02 05:56:50 +00:00
|
|
|
|
|
|
|
|
DiscDefinitionStructure decoded = dds.Value;
|
2018-06-22 08:08:38 +01:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2015-12-02 05:56:50 +00:00
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
if(decoded.InProcess)
|
2015-12-02 05:56:50 +00:00
|
|
|
{
|
|
|
|
|
sb.AppendLine("Formatting in progress.");
|
2016-04-19 02:11:47 +01:00
|
|
|
if(decoded.Groups == 24)
|
2015-12-02 05:56:50 +00:00
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(decoded.PartialCertification) sb.AppendLine("Formatting is only using partial certification");
|
|
|
|
|
if(decoded.FormattingOnlyAGroup) sb.AppendLine("Only a group is being formatted");
|
2015-12-02 05:56:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(decoded.UserCertification) sb.AppendLine("Disc has been certified by an user");
|
|
|
|
|
if(decoded.ManufacturerCertification) sb.AppendLine("Disc has been certified by a manufacturer");
|
2015-12-02 05:56:50 +00:00
|
|
|
|
|
|
|
|
sb.AppendFormat("DDS has been updated {0} times", decoded.UpdateCount).AppendLine();
|
|
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
if(decoded.Groups == 24)
|
|
|
|
|
for(int i = 0; i < decoded.GroupCertificationFlags.Length; i++)
|
2015-12-02 05:56:50 +00:00
|
|
|
{
|
|
|
|
|
if(decoded.GroupCertificationFlags[i].InProcess)
|
|
|
|
|
{
|
|
|
|
|
sb.AppendFormat("Group {0} is being formatted", i).AppendLine();
|
2016-04-19 02:11:47 +01:00
|
|
|
if(decoded.GroupCertificationFlags[i].PartialCertification)
|
2015-12-02 05:56:50 +00:00
|
|
|
sb.AppendFormat("Group {0} is being certified partially", i).AppendLine();
|
|
|
|
|
}
|
2018-06-22 08:08:38 +01:00
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
if(decoded.GroupCertificationFlags[i].UserCertification)
|
2015-12-02 05:56:50 +00:00
|
|
|
sb.AppendFormat("Group {0} has been certified by an user", i).AppendLine();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(decoded.Groups != 1) return sb.ToString();
|
|
|
|
|
|
2015-12-02 05:56:50 +00:00
|
|
|
{
|
|
|
|
|
sb.AppendFormat("Disc has {0} zones", decoded.Zones).AppendLine();
|
2017-12-19 20:33:03 +00:00
|
|
|
sb.AppendFormat("Primary Spare Area stats at PSN {0:X}h and ends at PSN {1:X}h, inclusively",
|
|
|
|
|
decoded.SpareAreaFirstPSN, decoded.SpareAreaLastPSN).AppendLine();
|
2015-12-02 05:56:50 +00:00
|
|
|
sb.AppendFormat("LSN 0 is at PSN {0:X}h", decoded.LSN0Location).AppendLine();
|
|
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < decoded.StartLSNForZone.Length; i++)
|
2015-12-02 05:56:50 +00:00
|
|
|
sb.AppendFormat("Zone {0} starts at LSN {1}", i, decoded.StartLSNForZone[i]).AppendLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:17:27 +00:00
|
|
|
public static string Prettify(byte[] response) => Prettify(Decode(response));
|
2015-10-19 02:28:40 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|