Files
Aaru/Aaru.Filesystems/HAMMER.cs

242 lines
11 KiB
C#
Raw Normal View History

2017-07-26 04:16:36 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2017-07-26 04:16:36 +01:00
// ----------------------------------------------------------------------------
//
// Filename : HAMMER.cs
2017-07-26 04:16:36 +01:00
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : HAMMER filesystem plugin.
2017-07-26 04:16:36 +01:00
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies the HAMMER filesystem and shows information.
2017-07-26 04:16:36 +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/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
2017-07-26 04:16:36 +01:00
// ****************************************************************************/
using System;
using System.Diagnostics.CodeAnalysis;
2017-07-26 04:16:36 +01:00
using System.Runtime.InteropServices;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Interfaces;
2017-12-21 14:30:38 +00:00
using Schemas;
2017-07-26 04:21:47 +01:00
using hammer_crc_t = System.UInt32;
2017-12-19 19:33:46 +00:00
using hammer_off_t = System.UInt64;
using hammer_tid_t = System.UInt64;
2020-02-27 00:33:26 +00:00
using Marshal = Aaru.Helpers.Marshal;
#pragma warning disable 169
2017-07-26 04:16:36 +01:00
2020-02-27 00:33:26 +00:00
namespace Aaru.Filesystems
2017-07-26 04:16:36 +01:00
{
public class HAMMER : IFilesystem
2017-07-26 04:16:36 +01:00
{
2018-06-22 08:08:38 +01:00
const ulong HAMMER_FSBUF_VOLUME = 0xC8414D4DC5523031;
2017-07-26 04:21:47 +01:00
const ulong HAMMER_FSBUF_VOLUME_REV = 0x313052C54D4D41C8;
2018-06-22 08:08:38 +01:00
const uint HAMMER_VOLHDR_SIZE = 1928;
const int HAMMER_BIGBLOCK_SIZE = 8192 * 1024;
2017-07-26 04:21:47 +01:00
2017-12-26 08:01:40 +00:00
public FileSystemType XmlFsType { get; private set; }
2018-06-22 08:08:38 +01:00
public Encoding Encoding { get; private set; }
public string Name => "HAMMER Filesystem";
public Guid Id => new Guid("91A188BF-5FD7-4677-BBD3-F59EBA9C864D");
2018-08-29 22:15:43 +01:00
public string Author => "Natalia Portillo";
public bool Identify(IMediaImage imagePlugin, Partition partition)
2017-07-26 04:16:36 +01:00
{
uint run = HAMMER_VOLHDR_SIZE / imagePlugin.Info.SectorSize;
2017-07-26 04:16:36 +01:00
2020-02-29 18:03:35 +00:00
if(HAMMER_VOLHDR_SIZE % imagePlugin.Info.SectorSize > 0)
run++;
2017-07-26 04:16:36 +01:00
2020-02-29 18:03:35 +00:00
if(run + partition.Start >= partition.End)
return false;
2017-07-26 04:16:36 +01:00
byte[] sbSector = imagePlugin.ReadSectors(partition.Start, run);
2017-07-26 04:16:36 +01:00
2018-06-20 22:22:21 +01:00
ulong magic = BitConverter.ToUInt64(sbSector, 0);
2017-07-26 04:16:36 +01:00
2017-07-26 04:21:47 +01:00
return magic == HAMMER_FSBUF_VOLUME || magic == HAMMER_FSBUF_VOLUME_REV;
2017-07-26 04:16:36 +01:00
}
2017-12-26 08:01:40 +00:00
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information,
2020-02-29 18:03:35 +00:00
Encoding encoding)
2017-07-26 04:16:36 +01:00
{
2018-06-22 08:08:38 +01:00
Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15");
2017-07-26 04:16:36 +01:00
information = "";
2020-02-29 18:03:35 +00:00
var sb = new StringBuilder();
2017-07-26 04:16:36 +01:00
HammerSuperBlock hammerSb;
2017-07-26 04:16:36 +01:00
uint run = HAMMER_VOLHDR_SIZE / imagePlugin.Info.SectorSize;
2017-07-26 04:21:47 +01:00
2020-02-29 18:03:35 +00:00
if(HAMMER_VOLHDR_SIZE % imagePlugin.Info.SectorSize > 0)
run++;
2017-07-26 04:21:47 +01:00
byte[] sbSector = imagePlugin.ReadSectors(partition.Start, run);
2017-07-26 04:21:47 +01:00
2018-06-20 22:22:21 +01:00
ulong magic = BitConverter.ToUInt64(sbSector, 0);
2017-07-26 04:21:47 +01:00
if(magic == HAMMER_FSBUF_VOLUME)
2020-02-29 18:03:35 +00:00
hammerSb = Marshal.ByteArrayToStructureLittleEndian<HammerSuperBlock>(sbSector);
else
hammerSb = Marshal.ByteArrayToStructureBigEndian<HammerSuperBlock>(sbSector);
2017-07-26 04:21:47 +01:00
sb.AppendLine("HAMMER filesystem");
sb.AppendFormat("Volume version: {0}", hammerSb.vol_version).AppendLine();
2020-02-29 18:03:35 +00:00
sb.AppendFormat("Volume {0} of {1} on this filesystem", hammerSb.vol_no + 1, hammerSb.vol_count).
AppendLine();
2017-12-26 08:01:40 +00:00
sb.AppendFormat("Volume name: {0}", StringHandlers.CToString(hammerSb.vol_label, Encoding)).AppendLine();
sb.AppendFormat("Volume serial: {0}", hammerSb.vol_fsid).AppendLine();
sb.AppendFormat("Filesystem type: {0}", hammerSb.vol_fstype).AppendLine();
sb.AppendFormat("Boot area starts at {0}", hammerSb.vol_bot_beg).AppendLine();
sb.AppendFormat("Memory log starts at {0}", hammerSb.vol_mem_beg).AppendLine();
sb.AppendFormat("First volume buffer starts at {0}", hammerSb.vol_buf_beg).AppendLine();
sb.AppendFormat("Volume ends at {0}", hammerSb.vol_buf_end).AppendLine();
2017-12-26 08:01:40 +00:00
XmlFsType = new FileSystemType
2017-07-26 04:16:36 +01:00
{
2020-07-20 04:34:16 +01:00
Clusters = partition.Size / HAMMER_BIGBLOCK_SIZE,
ClusterSize = HAMMER_BIGBLOCK_SIZE,
Dirty = false,
2018-06-22 08:08:38 +01:00
Type = "HAMMER",
VolumeName = StringHandlers.CToString(hammerSb.vol_label, Encoding),
VolumeSerial = hammerSb.vol_fsid.ToString()
2017-07-26 04:16:36 +01:00
};
2017-07-26 04:21:47 +01:00
if(hammerSb.vol_no == hammerSb.vol_rootvol)
2017-07-26 04:21:47 +01:00
{
sb.AppendFormat("Filesystem contains {0} \"big-blocks\" ({1} bytes)", hammerSb.vol0_stat_bigblocks,
hammerSb.vol0_stat_bigblocks * HAMMER_BIGBLOCK_SIZE).AppendLine();
2020-02-29 18:03:35 +00:00
sb.AppendFormat("Filesystem has {0} \"big-blocks\" free ({1} bytes)", hammerSb.vol0_stat_freebigblocks,
hammerSb.vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE).AppendLine();
2020-02-29 18:03:35 +00:00
sb.AppendFormat("Filesystem has {0} inode used", hammerSb.vol0_stat_inodes).AppendLine();
2019-04-23 01:38:33 +01:00
XmlFsType.Clusters = (ulong)hammerSb.vol0_stat_bigblocks;
XmlFsType.FreeClusters = (ulong)hammerSb.vol0_stat_freebigblocks;
2017-12-26 08:01:40 +00:00
XmlFsType.FreeClustersSpecified = true;
2019-04-23 01:38:33 +01:00
XmlFsType.Files = (ulong)hammerSb.vol0_stat_inodes;
2018-06-22 08:08:38 +01:00
XmlFsType.FilesSpecified = true;
2017-07-26 04:21:47 +01:00
}
2020-02-29 18:03:35 +00:00
2017-07-26 04:21:47 +01:00
// 0 ?
//sb.AppendFormat("Volume header CRC: 0x{0:X8}", afs_sb.vol_crc).AppendLine();
information = sb.ToString();
2017-07-26 04:16:36 +01:00
}
2020-02-29 18:03:35 +00:00
/// <summary>Hammer superblock</summary>
[StructLayout(LayoutKind.Sequential, Pack = 1), SuppressMessage("ReSharper", "BuiltInTypeReferenceStyle")]
2017-07-26 04:21:47 +01:00
struct HammerSuperBlock
{
/// <summary><see cref="HAMMER_FSBUF_VOLUME" /> for a valid header</summary>
2019-04-23 01:38:33 +01:00
public readonly ulong vol_signature;
2017-07-26 04:21:47 +01:00
2017-12-19 20:33:03 +00:00
/* These are relative to block device offset, not zone offsets. */
2017-07-26 04:21:47 +01:00
/// <summary>offset of boot area</summary>
2019-04-23 01:38:33 +01:00
public readonly long vol_bot_beg;
2017-07-26 04:21:47 +01:00
/// <summary>offset of memory log</summary>
2019-04-23 01:38:33 +01:00
public readonly long vol_mem_beg;
2017-07-26 04:21:47 +01:00
/// <summary>offset of the first buffer in volume</summary>
2019-04-23 01:38:33 +01:00
public readonly long vol_buf_beg;
2017-07-26 04:21:47 +01:00
/// <summary>offset of volume EOF (on buffer boundary)</summary>
2019-04-23 01:38:33 +01:00
public readonly long vol_buf_end;
public readonly long vol_reserved01;
2017-07-26 04:21:47 +01:00
/// <summary>identify filesystem</summary>
2019-04-23 01:38:33 +01:00
public readonly Guid vol_fsid;
2017-07-26 04:21:47 +01:00
/// <summary>identify filesystem type</summary>
2019-04-23 01:38:33 +01:00
public readonly Guid vol_fstype;
2017-07-26 04:21:47 +01:00
/// <summary>filesystem label</summary>
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
2019-04-23 01:38:33 +01:00
public readonly byte[] vol_label;
2017-07-26 04:21:47 +01:00
/// <summary>volume number within filesystem</summary>
2019-04-23 01:38:33 +01:00
public readonly int vol_no;
2017-07-26 04:21:47 +01:00
/// <summary>number of volumes making up filesystem</summary>
2019-04-23 01:38:33 +01:00
public readonly int vol_count;
2017-07-26 04:21:47 +01:00
/// <summary>version control information</summary>
2019-04-23 01:38:33 +01:00
public readonly uint vol_version;
2017-07-26 04:21:47 +01:00
/// <summary>header crc</summary>
2019-04-23 01:38:33 +01:00
public readonly hammer_crc_t vol_crc;
2017-07-26 04:21:47 +01:00
/// <summary>volume flags</summary>
2019-04-23 01:38:33 +01:00
public readonly uint vol_flags;
2017-07-26 04:21:47 +01:00
/// <summary>the root volume number (must be 0)</summary>
2019-04-23 01:38:33 +01:00
public readonly uint vol_rootvol;
2017-07-26 04:21:47 +01:00
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
2019-04-23 01:38:33 +01:00
public readonly uint[] vol_reserved;
2017-07-26 04:21:47 +01:00
/*
* These fields are initialized and space is reserved in every
* volume making up a HAMMER filesytem, but only the root volume
* contains valid data. Note that vol0_stat_bigblocks does not
* include big-blocks for freemap and undomap initially allocated
* by newfs_hammer(8).
*/
/// <summary>total big-blocks when fs is empty</summary>
2019-04-23 01:38:33 +01:00
public readonly long vol0_stat_bigblocks;
2017-07-26 04:21:47 +01:00
/// <summary>number of free big-blocks</summary>
2019-04-23 01:38:33 +01:00
public readonly long vol0_stat_freebigblocks;
public readonly long vol0_reserved01;
2017-07-26 04:21:47 +01:00
/// <summary>for statfs only</summary>
2019-04-23 01:38:33 +01:00
public readonly long vol0_stat_inodes;
public readonly long vol0_reserved02;
2017-07-26 04:21:47 +01:00
/// <summary>B-Tree root offset in zone-8</summary>
2019-04-23 01:38:33 +01:00
public readonly hammer_off_t vol0_btree_root;
2017-07-26 04:21:47 +01:00
/// <summary>highest partially synchronized TID</summary>
2019-04-23 01:38:33 +01:00
public readonly hammer_tid_t vol0_next_tid;
public readonly hammer_off_t vol0_reserved03;
2017-07-26 04:21:47 +01:00
/// <summary>
/// Blockmaps for zones. Not all zones use a blockmap. Note that the entire root blockmap is cached in the
/// hammer_mount structure.
/// </summary>
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
2019-04-23 01:38:33 +01:00
public readonly HammerBlockMap[] vol0_blockmap;
2017-07-26 04:21:47 +01:00
/// <summary>Array of zone-2 addresses for undo FIFO.</summary>
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
2019-04-23 01:38:33 +01:00
public readonly hammer_off_t[] vol0_undo_array;
2017-07-26 04:21:47 +01:00
}
2020-02-29 18:03:35 +00:00
[SuppressMessage("ReSharper", "InconsistentNaming"), SuppressMessage("ReSharper", "BuiltInTypeReferenceStyle")]
2017-07-26 04:21:47 +01:00
struct HammerBlockMap
2017-07-26 04:16:36 +01:00
{
2017-07-26 04:21:47 +01:00
/// <summary>zone-2 offset only used by zone-4</summary>
public hammer_off_t phys_offset;
/// <summary>zone-X offset only used by zone-3</summary>
public hammer_off_t first_offset;
/// <summary>zone-X offset for allocation</summary>
public hammer_off_t next_offset;
/// <summary>zone-X offset only used by zone-3</summary>
public hammer_off_t alloc_offset;
2018-06-22 08:08:38 +01:00
public uint reserved01;
2017-07-26 04:21:47 +01:00
public hammer_crc_t entry_crc;
2017-07-26 04:16:36 +01:00
}
}
2017-12-19 20:33:03 +00:00
}