Files
Aaru/Aaru.Filesystems/UDF/Info.cs

347 lines
15 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
2022-12-07 13:07:31 +00:00
// Filename : Info.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Universal Disk Format plugin.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
// ****************************************************************************/
using System;
2020-07-20 07:47:12 +01:00
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using Aaru.CommonTypes.AaruMetadata;
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
using Aaru.Logging;
using Partition = Aaru.CommonTypes.Partition;
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
// TODO: Detect bootable
/// <inheritdoc />
/// <summary>Implements detection of the Universal Disk Format filesystem</summary>
[SuppressMessage("ReSharper", "UnusedMember.Local")]
2022-12-07 13:07:31 +00:00
public sealed partial class UDF
{
#region IFilesystem Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(IMediaImage imagePlugin, Partition partition)
{
// UDF needs at least that
2024-05-01 04:05:22 +01:00
if(partition.End - partition.Start < 256) return false;
2022-03-06 13:29:38 +00:00
// UDF needs at least that
2024-05-01 04:05:22 +01:00
if(imagePlugin.Info.SectorSize < 512) return false;
2020-02-29 18:03:35 +00:00
var anchor = new AnchorVolumeDescriptorPointer();
2022-03-06 13:29:38 +00:00
// All positions where anchor may reside, with the ratio between 512 and 2048bps
ulong[][] positions =
2024-05-01 04:39:38 +01:00
[
[256, 1], [512, 1], [partition.End - 256, 1], [partition.End, 1], [1024, 4], [2048, 4],
[partition.End - 1024, 4], [partition.End - 4, 4]
];
2020-02-29 18:03:35 +00:00
var anchorFound = false;
2023-10-04 08:44:14 +01:00
uint ratio = 1;
byte[] sector = null;
2022-11-15 01:35:06 +00:00
foreach(ulong[] position in from position in
positions.Where(position =>
position[0] + partition.Start + position[1] <=
partition.End &&
position[0] < partition.End)
let errno =
imagePlugin.ReadSectors(position[0],
false,
(uint)position[1],
out sector,
out _)
where errno == ErrorNumber.NoError
select position)
2022-03-06 13:29:38 +00:00
{
anchor = Marshal.ByteArrayToStructureLittleEndian<AnchorVolumeDescriptorPointer>(sector);
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, "anchor.tag.tagIdentifier = {0}", anchor.tag.tagIdentifier);
2020-02-29 18:03:35 +00:00
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, "anchor.tag.descriptorVersion = {0}", anchor.tag.descriptorVersion);
2020-02-29 18:03:35 +00:00
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, "anchor.tag.tagChecksum = 0x{0:X2}", anchor.tag.tagChecksum);
AaruLogging.Debug(MODULE_NAME, "anchor.tag.reserved = {0}", anchor.tag.reserved);
2020-02-29 18:03:35 +00:00
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, "anchor.tag.tagSerialNumber = {0}", anchor.tag.tagSerialNumber);
2020-02-29 18:03:35 +00:00
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, "anchor.tag.descriptorCrc = 0x{0:X4}", anchor.tag.descriptorCrc);
2020-02-29 18:03:35 +00:00
AaruLogging.Debug(MODULE_NAME, "anchor.tag.descriptorCrcLength = {0}", anchor.tag.descriptorCrcLength);
2020-02-29 18:03:35 +00:00
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, "anchor.tag.tagLocation = {0}", anchor.tag.tagLocation);
2020-02-29 18:03:35 +00:00
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
"anchor.mainVolumeDescriptorSequenceExtent.length = {0}",
anchor.mainVolumeDescriptorSequenceExtent.length);
2020-02-29 18:03:35 +00:00
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
"anchor.mainVolumeDescriptorSequenceExtent.location = {0}",
anchor.mainVolumeDescriptorSequenceExtent.location);
2020-02-29 18:03:35 +00:00
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
"anchor.reserveVolumeDescriptorSequenceExtent.length = {0}",
anchor.reserveVolumeDescriptorSequenceExtent.length);
2020-02-29 18:03:35 +00:00
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
"anchor.reserveVolumeDescriptorSequenceExtent.location = {0}",
anchor.reserveVolumeDescriptorSequenceExtent.location);
2022-03-06 13:29:38 +00:00
if(anchor.tag.tagIdentifier != TagIdentifier.AnchorVolumeDescriptorPointer ||
2022-03-07 07:36:44 +00:00
anchor.tag.tagLocation != position[0] / position[1] ||
anchor.mainVolumeDescriptorSequenceExtent.location * position[1] + partition.Start >= partition.End)
2022-03-06 13:29:38 +00:00
continue;
2022-03-06 13:29:38 +00:00
anchorFound = true;
ratio = (uint)position[1];
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
}
2024-05-01 04:05:22 +01:00
if(!anchorFound) return false;
2022-03-06 13:29:38 +00:00
ulong count = 0;
2022-03-06 13:29:38 +00:00
while(count < 256)
{
ErrorNumber errno =
2024-05-01 04:05:22 +01:00
imagePlugin.ReadSectors(partition.Start +
anchor.mainVolumeDescriptorSequenceExtent.location * ratio +
count * ratio,
false,
2024-05-01 04:05:22 +01:00
ratio,
out sector,
out _);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
{
2022-03-06 13:29:38 +00:00
count++;
2022-03-06 13:29:38 +00:00
continue;
}
var tagId = (TagIdentifier)BitConverter.ToUInt16(sector, 0);
var location = BitConverter.ToUInt32(sector, 0x0C);
2020-02-29 18:03:35 +00:00
if(location == partition.Start / ratio + anchor.mainVolumeDescriptorSequenceExtent.location + count)
2022-03-06 13:29:38 +00:00
{
2024-05-01 04:05:22 +01:00
if(tagId == TagIdentifier.TerminatingDescriptor) break;
2022-03-06 13:29:38 +00:00
if(tagId == TagIdentifier.LogicalVolumeDescriptor)
{
2022-03-06 13:29:38 +00:00
LogicalVolumeDescriptor lvd =
Marshal.ByteArrayToStructureLittleEndian<LogicalVolumeDescriptor>(sector);
2022-03-06 13:29:38 +00:00
return _magic.SequenceEqual(lvd.domainIdentifier.identifier);
}
}
2022-03-06 13:29:38 +00:00
else
break;
2022-03-06 13:29:38 +00:00
count++;
}
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public void GetInformation(IMediaImage imagePlugin, Partition partition, Encoding encoding, out string information,
out FileSystem metadata)
2022-03-06 13:29:38 +00:00
{
information = "";
ErrorNumber errno;
metadata = new FileSystem();
2022-03-06 13:29:38 +00:00
// UDF is always UTF-8
encoding = Encoding.UTF8;
2022-03-06 13:29:38 +00:00
byte[] sector;
2022-03-06 13:29:38 +00:00
var sbInformation = new StringBuilder();
sbInformation.AppendLine(Localization.Universal_Disk_Format);
2022-03-06 13:29:38 +00:00
var anchor = new AnchorVolumeDescriptorPointer();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// All positions where anchor may reside, with the ratio between 512 and 2048bps
ulong[][] positions =
2024-05-01 04:39:38 +01:00
[
[256, 1], [512, 1], [partition.End - 256, 1], [partition.End, 1], [1024, 4], [2048, 4],
[partition.End - 1024, 4], [partition.End - 4, 4]
];
2022-03-06 13:29:38 +00:00
uint ratio = 1;
2022-03-06 13:29:38 +00:00
foreach(ulong[] position in positions)
{
errno = imagePlugin.ReadSectors(position[0], false, (uint)position[1], out sector, out _);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) continue;
2022-03-06 13:29:38 +00:00
anchor = Marshal.ByteArrayToStructureLittleEndian<AnchorVolumeDescriptorPointer>(sector);
2022-03-06 13:29:38 +00:00
if(anchor.tag.tagIdentifier != TagIdentifier.AnchorVolumeDescriptorPointer ||
anchor.tag.tagLocation != position[0] / position[1] ||
anchor.mainVolumeDescriptorSequenceExtent.location + partition.Start >= partition.End)
continue;
2022-03-06 13:29:38 +00:00
ratio = (uint)position[1];
2022-03-06 13:29:38 +00:00
break;
}
2022-03-06 13:29:38 +00:00
ulong count = 0;
var pvd = new PrimaryVolumeDescriptor();
var lvd = new LogicalVolumeDescriptor();
var lvidiu = new LogicalVolumeIntegrityDescriptorImplementationUse();
2022-03-06 13:29:38 +00:00
while(count < 256)
{
2024-05-01 04:05:22 +01:00
errno = imagePlugin.ReadSectors(partition.Start +
anchor.mainVolumeDescriptorSequenceExtent.location * ratio +
count * ratio,
false,
2024-05-01 04:05:22 +01:00
ratio,
out sector,
out _);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) continue;
2020-02-29 18:03:35 +00:00
var tagId = (TagIdentifier)BitConverter.ToUInt16(sector, 0);
var location = BitConverter.ToUInt32(sector, 0x0C);
if(location == partition.Start / ratio + anchor.mainVolumeDescriptorSequenceExtent.location + count)
2022-03-06 13:29:38 +00:00
{
2024-05-01 04:05:22 +01:00
if(tagId == TagIdentifier.TerminatingDescriptor) break;
2022-03-06 13:29:38 +00:00
switch(tagId)
{
2022-03-06 13:29:38 +00:00
case TagIdentifier.LogicalVolumeDescriptor:
lvd = Marshal.ByteArrayToStructureLittleEndian<LogicalVolumeDescriptor>(sector);
2020-02-29 18:03:35 +00:00
break;
2022-03-06 13:29:38 +00:00
case TagIdentifier.PrimaryVolumeDescriptor:
pvd = Marshal.ByteArrayToStructureLittleEndian<PrimaryVolumeDescriptor>(sector);
2022-03-06 13:29:38 +00:00
break;
}
}
else
break;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
count++;
}
2020-02-29 18:03:35 +00:00
errno = imagePlugin.ReadSectors(lvd.integritySequenceExtent.location * ratio, false, ratio, out sector, out _);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return;
LogicalVolumeIntegrityDescriptor lvid =
Marshal.ByteArrayToStructureLittleEndian<LogicalVolumeIntegrityDescriptor>(sector);
2022-03-06 13:29:38 +00:00
if(lvid.tag.tagIdentifier == TagIdentifier.LogicalVolumeIntegrityDescriptor &&
lvid.tag.tagLocation == lvd.integritySequenceExtent.location)
{
2024-05-01 04:05:22 +01:00
lvidiu = Marshal.ByteArrayToStructureLittleEndian<LogicalVolumeIntegrityDescriptorImplementationUse>(sector,
(int)(lvid.numberOfPartitions * 8 + 80),
System.Runtime.InteropServices.Marshal.SizeOf(lvidiu));
}
2022-03-06 13:29:38 +00:00
else
lvid = new LogicalVolumeIntegrityDescriptor();
2024-05-01 04:05:22 +01:00
sbInformation.AppendFormat(Localization.Volume_is_number_0_of_1,
pvd.volumeSequenceNumber,
pvd.maximumVolumeSequenceNumber)
.AppendLine();
sbInformation.AppendFormat(Localization.Volume_set_identifier_0,
2024-05-01 04:05:22 +01:00
StringHandlers.DecompressUnicode(pvd.volumeSetIdentifier))
.AppendLine();
2017-12-19 20:33:03 +00:00
2024-05-01 04:05:22 +01:00
sbInformation
.AppendFormat(Localization.Volume_name_0, StringHandlers.DecompressUnicode(lvd.logicalVolumeIdentifier))
.AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Volume_uses_0_bytes_per_block, lvd.logicalBlockSize).AppendLine();
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
sbInformation.AppendFormat(Localization.Volume_was_last_written_on_0, EcmaToDateTime(lvid.recordingDateTime))
.AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Volume_contains_0_partitions, lvid.numberOfPartitions).AppendLine();
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
sbInformation
.AppendFormat(Localization.Volume_contains_0_files_and_1_directories, lvidiu.files, lvidiu.directories)
.AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Volume_conforms_to_0,
2024-05-01 04:05:22 +01:00
encoding.GetString(lvd.domainIdentifier.identifier).TrimEnd('\u0000'))
.AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Volume_was_last_written_by_0,
2024-05-01 04:05:22 +01:00
encoding.GetString(pvd.implementationIdentifier.identifier).TrimEnd('\u0000'))
.AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Volume_requires_UDF_version_0_1_to_be_read,
2022-03-06 13:29:38 +00:00
Convert.ToInt32($"{(lvidiu.minimumReadUDF & 0xFF00) >> 8}", 10),
2024-05-01 04:05:22 +01:00
Convert.ToInt32($"{lvidiu.minimumReadUDF & 0xFF}", 10))
.AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Volume_requires_UDF_version_0_1_to_be_written_to,
2022-03-06 13:29:38 +00:00
Convert.ToInt32($"{(lvidiu.minimumWriteUDF & 0xFF00) >> 8}", 10),
2024-05-01 04:05:22 +01:00
Convert.ToInt32($"{lvidiu.minimumWriteUDF & 0xFF}", 10))
.AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Volume_cannot_be_written_by_any_UDF_version_higher_than_0_1,
2022-03-06 13:29:38 +00:00
Convert.ToInt32($"{(lvidiu.maximumWriteUDF & 0xFF00) >> 8}", 10),
2024-05-01 04:05:22 +01:00
Convert.ToInt32($"{lvidiu.maximumWriteUDF & 0xFF}", 10))
.AppendLine();
2020-02-29 18:03:35 +00:00
metadata = new FileSystem
2022-03-06 13:29:38 +00:00
{
Type = FS_TYPE,
ApplicationIdentifier = encoding.GetString(pvd.implementationIdentifier.identifier).TrimEnd('\u0000'),
ClusterSize = lvd.logicalBlockSize,
ModificationDate = EcmaToDateTime(lvid.recordingDateTime),
Files = lvidiu.files,
VolumeName = StringHandlers.DecompressUnicode(lvd.logicalVolumeIdentifier),
VolumeSetIdentifier = StringHandlers.DecompressUnicode(pvd.volumeSetIdentifier),
VolumeSerial = StringHandlers.DecompressUnicode(pvd.volumeSetIdentifier),
SystemIdentifier = encoding.GetString(pvd.implementationIdentifier.identifier).TrimEnd('\u0000')
2022-03-06 13:29:38 +00:00
};
2020-02-29 18:03:35 +00:00
metadata.Clusters = (partition.End - partition.Start + 1) * imagePlugin.Info.SectorSize / metadata.ClusterSize;
2022-03-06 13:29:38 +00:00
information = sbInformation.ToString();
}
#endregion
}