Files
Aaru/Aaru.Filesystems/QNX6.cs

237 lines
10 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : QNX6.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : QNX6 filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies the QNX6 filesystem and shows information.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2025-08-14 02:49:52 +01:00
// Copyright © 2011-2025 Natalia Portillo
// ****************************************************************************/
using System;
using System.Runtime.InteropServices;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Interfaces;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
2017-12-21 14:30:38 +00:00
using Schemas;
2020-02-27 00:33:26 +00:00
using Marshal = Aaru.Helpers.Marshal;
2020-02-27 00:33:26 +00:00
namespace Aaru.Filesystems
{
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
/// <summary>Implements detection of QNX 6 filesystem</summary>
2020-07-22 13:20:25 +01:00
public sealed class QNX6 : IFilesystem
{
const uint QNX6_SUPER_BLOCK_SIZE = 0x1000;
const uint QNX6_BOOT_BLOCKS_SIZE = 0x2000;
2018-06-22 08:08:38 +01:00
const uint QNX6_MAGIC = 0x68191122;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2017-12-26 08:01:40 +00:00
public FileSystemType XmlFsType { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
public Encoding Encoding { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
public string Name => "QNX6 Plugin";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
public Guid Id => new Guid("3E610EA2-4D08-4D70-8947-830CD4C74FC0");
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
public string Author => "Natalia Portillo";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Identify(IMediaImage imagePlugin, Partition partition)
{
2018-06-22 08:08:38 +01:00
uint sectors = QNX6_SUPER_BLOCK_SIZE / imagePlugin.Info.SectorSize;
uint bootSectors = QNX6_BOOT_BLOCKS_SIZE / imagePlugin.Info.SectorSize;
2020-02-29 18:03:35 +00:00
if(partition.Start + bootSectors + sectors >= partition.End)
return false;
2017-07-23 19:58:11 +01:00
2020-02-29 18:03:35 +00:00
byte[] audiSector = imagePlugin.ReadSectors(partition.Start, sectors);
2018-06-22 08:08:38 +01:00
byte[] sector = imagePlugin.ReadSectors(partition.Start + bootSectors, sectors);
2020-02-29 18:03:35 +00:00
if(sector.Length < QNX6_SUPER_BLOCK_SIZE)
return false;
2020-07-20 21:11:32 +01:00
AudiSuperBlock audiSb = Marshal.ByteArrayToStructureLittleEndian<AudiSuperBlock>(audiSector);
2020-07-20 21:11:32 +01:00
SuperBlock qnxSb = Marshal.ByteArrayToStructureLittleEndian<SuperBlock>(sector);
return qnxSb.magic == QNX6_MAGIC || audiSb.magic == QNX6_MAGIC;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information,
2020-02-29 18:03:35 +00:00
Encoding encoding)
{
2018-06-22 08:08:38 +01:00
Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15");
information = "";
2020-02-29 18:03:35 +00:00
var sb = new StringBuilder();
uint sectors = QNX6_SUPER_BLOCK_SIZE / imagePlugin.Info.SectorSize;
uint bootSectors = QNX6_BOOT_BLOCKS_SIZE / imagePlugin.Info.SectorSize;
2020-02-29 18:03:35 +00:00
byte[] audiSector = imagePlugin.ReadSectors(partition.Start, sectors);
2018-06-22 08:08:38 +01:00
byte[] sector = imagePlugin.ReadSectors(partition.Start + bootSectors, sectors);
2020-02-29 18:03:35 +00:00
if(sector.Length < QNX6_SUPER_BLOCK_SIZE)
return;
2020-07-20 21:11:32 +01:00
AudiSuperBlock audiSb = Marshal.ByteArrayToStructureLittleEndian<AudiSuperBlock>(audiSector);
2020-07-20 21:11:32 +01:00
SuperBlock qnxSb = Marshal.ByteArrayToStructureLittleEndian<SuperBlock>(sector);
bool audi = audiSb.magic == QNX6_MAGIC;
if(audi)
{
sb.AppendLine("QNX6 (Audi) filesystem");
sb.AppendFormat("Checksum: 0x{0:X8}", audiSb.checksum).AppendLine();
sb.AppendFormat("Serial: 0x{0:X16}", audiSb.checksum).AppendLine();
sb.AppendFormat("{0} bytes per block", audiSb.blockSize).AppendLine();
sb.AppendFormat("{0} inodes free of {1}", audiSb.freeInodes, audiSb.numInodes).AppendLine();
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
sb.AppendFormat("{0} blocks ({1} bytes) free of {2} ({3} bytes)", audiSb.freeBlocks,
audiSb.freeBlocks * audiSb.blockSize, audiSb.numBlocks,
2018-06-22 08:08:38 +01:00
audiSb.numBlocks * audiSb.blockSize).AppendLine();
2017-12-26 08:01:40 +00:00
XmlFsType = new FileSystemType
{
2020-07-20 04:34:16 +01:00
Type = "QNX6 (Audi) filesystem",
Clusters = audiSb.numBlocks,
ClusterSize = audiSb.blockSize,
Bootable = true,
Files = audiSb.numInodes - audiSb.freeInodes,
FilesSpecified = true,
FreeClusters = audiSb.freeBlocks,
FreeClustersSpecified = true,
VolumeSerial = $"{audiSb.serial:X16}"
};
2020-02-29 18:03:35 +00:00
//xmlFSType.VolumeName = CurrentEncoding.GetString(audiSb.id);
information = sb.ToString();
2020-02-29 18:03:35 +00:00
return;
}
sb.AppendLine("QNX6 filesystem");
sb.AppendFormat("Checksum: 0x{0:X8}", qnxSb.checksum).AppendLine();
sb.AppendFormat("Serial: 0x{0:X16}", qnxSb.checksum).AppendLine();
2017-12-23 03:59:48 +00:00
sb.AppendFormat("Created on {0}", DateHandlers.UnixUnsignedToDateTime(qnxSb.ctime)).AppendLine();
sb.AppendFormat("Last mounted on {0}", DateHandlers.UnixUnsignedToDateTime(qnxSb.atime)).AppendLine();
sb.AppendFormat("Flags: 0x{0:X8}", qnxSb.flags).AppendLine();
sb.AppendFormat("Version1: 0x{0:X4}", qnxSb.version1).AppendLine();
sb.AppendFormat("Version2: 0x{0:X4}", qnxSb.version2).AppendLine();
2020-02-29 18:03:35 +00:00
//sb.AppendFormat("Volume ID: \"{0}\"", CurrentEncoding.GetString(qnxSb.volumeid)).AppendLine();
sb.AppendFormat("{0} bytes per block", qnxSb.blockSize).AppendLine();
sb.AppendFormat("{0} inodes free of {1}", qnxSb.freeInodes, qnxSb.numInodes).AppendLine();
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
sb.AppendFormat("{0} blocks ({1} bytes) free of {2} ({3} bytes)", qnxSb.freeBlocks,
2020-02-29 18:03:35 +00:00
qnxSb.freeBlocks * qnxSb.blockSize, qnxSb.numBlocks, qnxSb.numBlocks * qnxSb.blockSize).
AppendLine();
2017-12-26 08:01:40 +00:00
XmlFsType = new FileSystemType
{
2018-06-22 08:08:38 +01:00
Type = "QNX6 filesystem",
Clusters = qnxSb.numBlocks,
2020-07-20 04:34:16 +01:00
ClusterSize = qnxSb.blockSize,
Bootable = true,
2018-06-22 08:08:38 +01:00
Files = qnxSb.numInodes - qnxSb.freeInodes,
2020-07-20 04:34:16 +01:00
FilesSpecified = true,
FreeClusters = qnxSb.freeBlocks,
2018-06-22 08:08:38 +01:00
FreeClustersSpecified = true,
VolumeSerial = $"{qnxSb.serial:X16}",
CreationDate = DateHandlers.UnixUnsignedToDateTime(qnxSb.ctime),
CreationDateSpecified = true,
ModificationDate = DateHandlers.UnixUnsignedToDateTime(qnxSb.atime),
ModificationDateSpecified = true
};
2020-02-29 18:03:35 +00:00
//xmlFSType.VolumeName = CurrentEncoding.GetString(qnxSb.volumeid);
information = sb.ToString();
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct RootNode
{
2019-04-23 01:38:33 +01:00
public readonly ulong size;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
2019-04-23 01:38:33 +01:00
public readonly uint[] pointers;
public readonly byte levels;
public readonly byte mode;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
2019-04-23 01:38:33 +01:00
public readonly byte[] spare;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct SuperBlock
{
2019-04-23 01:38:33 +01:00
public readonly uint magic;
public readonly uint checksum;
public readonly ulong serial;
public readonly uint ctime;
public readonly uint atime;
public readonly uint flags;
public readonly ushort version1;
public readonly ushort version2;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
2019-04-23 01:38:33 +01:00
public readonly byte[] volumeid;
2020-07-20 21:11:32 +01:00
public readonly uint blockSize;
public readonly uint numInodes;
public readonly uint freeInodes;
public readonly uint numBlocks;
public readonly uint freeBlocks;
public readonly uint allocationGroup;
public readonly RootNode inode;
public readonly RootNode bitmap;
public readonly RootNode longfile;
public readonly RootNode unknown;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct AudiSuperBlock
{
2019-04-23 01:38:33 +01:00
public readonly uint magic;
public readonly uint checksum;
public readonly ulong serial;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
2019-04-23 01:38:33 +01:00
public readonly byte[] spare1;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
2019-04-23 01:38:33 +01:00
public readonly byte[] id;
2020-07-20 21:11:32 +01:00
public readonly uint blockSize;
public readonly uint numInodes;
public readonly uint freeInodes;
public readonly uint numBlocks;
public readonly uint freeBlocks;
public readonly uint spare2;
public readonly RootNode inode;
public readonly RootNode bitmap;
public readonly RootNode longfile;
public readonly RootNode unknown;
}
}
}