[AppleMap] Use new source generator based big endian marshaller

This commit is contained in:
2025-10-21 11:20:28 +01:00
parent 29e2aebaf1
commit ccbcd6ab45

View File

@@ -36,6 +36,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using Aaru.CommonTypes; using Aaru.CommonTypes;
using Aaru.CommonTypes.Attributes;
using Aaru.CommonTypes.Enums; using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces; using Aaru.CommonTypes.Interfaces;
using Aaru.Helpers; using Aaru.Helpers;
@@ -49,7 +50,7 @@ namespace Aaru.Partitions;
/// <inheritdoc /> /// <inheritdoc />
/// <summary>Implements decoding of the Apple Partition Map</summary> /// <summary>Implements decoding of the Apple Partition Map</summary>
[SuppressMessage("ReSharper", "UnusedMember.Local")] [SuppressMessage("ReSharper", "UnusedMember.Local")]
public sealed class AppleMap : IPartition public sealed partial class AppleMap : IPartition
{ {
/// <summary>"ER", driver descriptor magic</summary> /// <summary>"ER", driver descriptor magic</summary>
const ushort DDM_MAGIC = 0x4552; const ushort DDM_MAGIC = 0x4552;
@@ -91,7 +92,7 @@ public sealed class AppleMap : IPartition
{ {
case 256: case 256:
{ {
byte[] tmp = new byte[512]; var tmp = new byte[512];
Array.Copy(ddmSector, 0, tmp, 0, 256); Array.Copy(ddmSector, 0, tmp, 0, 256);
ddmSector = tmp; ddmSector = tmp;
maxDrivers = 29; maxDrivers = 29;
@@ -102,7 +103,8 @@ public sealed class AppleMap : IPartition
return false; return false;
} }
AppleDriverDescriptorMap ddm = Marshal.ByteArrayToStructureBigEndian<AppleDriverDescriptorMap>(ddmSector); AppleDriverDescriptorMap ddm =
Marshal.ByteArrayToStructureBigEndianGenerated<AppleDriverDescriptorMap>(ddmSector);
AaruLogging.Debug(MODULE_NAME, "ddm.sbSig = 0x{0:X4}", ddm.sbSig); AaruLogging.Debug(MODULE_NAME, "ddm.sbSig = 0x{0:X4}", ddm.sbSig);
AaruLogging.Debug(MODULE_NAME, "ddm.sbBlockSize = {0}", ddm.sbBlockSize); AaruLogging.Debug(MODULE_NAME, "ddm.sbBlockSize = {0}", ddm.sbBlockSize);
@@ -120,11 +122,11 @@ public sealed class AppleMap : IPartition
{ {
ddm.sbMap = new AppleDriverEntry[ddm.sbDrvrCount]; ddm.sbMap = new AppleDriverEntry[ddm.sbDrvrCount];
for(int i = 0; i < ddm.sbDrvrCount; i++) for(var i = 0; i < ddm.sbDrvrCount; i++)
{ {
byte[] tmp = new byte[8]; var tmp = new byte[8];
Array.Copy(ddmSector, 18 + i * 8, tmp, 0, 8); Array.Copy(ddmSector, 18 + i * 8, tmp, 0, 8);
ddm.sbMap[i] = Marshal.ByteArrayToStructureBigEndian<AppleDriverEntry>(tmp); ddm.sbMap[i] = Marshal.ByteArrayToStructureBigEndianGenerated<AppleDriverEntry>(tmp);
AaruLogging.Debug(MODULE_NAME, "ddm.sbMap[{1}].ddBlock = {0}", ddm.sbMap[i].ddBlock, i); AaruLogging.Debug(MODULE_NAME, "ddm.sbMap[{1}].ddBlock = {0}", ddm.sbMap[i].ddBlock, i);
@@ -158,33 +160,24 @@ public sealed class AppleMap : IPartition
if(errno != ErrorNumber.NoError) return false; if(errno != ErrorNumber.NoError) return false;
AppleOldDevicePartitionMap oldMap = AppleOldDevicePartitionMap oldMap =
Marshal.ByteArrayToStructureBigEndian<AppleOldDevicePartitionMap>(partSector); Marshal.ByteArrayToStructureBigEndianGenerated<AppleOldDevicePartitionMap>(partSector);
// This is the easy one, no sector size mixing // This is the easy one, no sector size mixing
if(oldMap.pdSig == APM_MAGIC_OLD) if(oldMap.pdSig == APM_MAGIC_OLD)
{ {
for(int i = 2; i < partSector.Length; i += 12) for(var i = 2; i < partSector.Length; i += 12)
{ {
byte[] tmp = new byte[12]; var tmp = new byte[12];
Array.Copy(partSector, i, tmp, 0, 12); Array.Copy(partSector, i, tmp, 0, 12);
AppleMapOldPartitionEntry oldEntry = AppleMapOldPartitionEntry oldEntry =
Marshal.ByteArrayToStructureBigEndian<AppleMapOldPartitionEntry>(tmp); Marshal.ByteArrayToStructureBigEndianGenerated<AppleMapOldPartitionEntry>(tmp);
AaruLogging.Debug(MODULE_NAME, AaruLogging.Debug(MODULE_NAME, "old_map.sbMap[{1}].pdStart = {0}", oldEntry.pdStart, (i - 2) / 12);
"old_map.sbMap[{1}].pdStart = {0}",
oldEntry.pdStart,
(i - 2) / 12);
AaruLogging.Debug(MODULE_NAME, AaruLogging.Debug(MODULE_NAME, "old_map.sbMap[{1}].pdSize = {0}", oldEntry.pdSize, (i - 2) / 12);
"old_map.sbMap[{1}].pdSize = {0}",
oldEntry.pdSize,
(i - 2) / 12);
AaruLogging.Debug(MODULE_NAME, AaruLogging.Debug(MODULE_NAME, "old_map.sbMap[{1}].pdFSID = 0x{0:X8}", oldEntry.pdFSID, (i - 2) / 12);
"old_map.sbMap[{1}].pdFSID = 0x{0:X8}",
oldEntry.pdFSID,
(i - 2) / 12);
if(oldEntry is { pdSize: 0, pdFSID: 0 }) if(oldEntry is { pdSize: 0, pdFSID: 0 })
{ {
@@ -221,9 +214,9 @@ public sealed class AppleMap : IPartition
// If sector is bigger than 512 // If sector is bigger than 512
if(ddmSector.Length > 512) if(ddmSector.Length > 512)
{ {
byte[] tmp = new byte[512]; var tmp = new byte[512];
Array.Copy(ddmSector, 512, tmp, 0, 512); Array.Copy(ddmSector, 512, tmp, 0, 512);
entry = Marshal.ByteArrayToStructureBigEndian<AppleMapPartitionEntry>(tmp); entry = Marshal.ByteArrayToStructureBigEndianGenerated<AppleMapPartitionEntry>(tmp);
// Check for a partition entry that's 512-byte aligned // Check for a partition entry that's 512-byte aligned
if(entry.signature == APM_MAGIC) if(entry.signature == APM_MAGIC)
@@ -236,7 +229,7 @@ public sealed class AppleMap : IPartition
} }
else else
{ {
entry = Marshal.ByteArrayToStructureBigEndian<AppleMapPartitionEntry>(partSector); entry = Marshal.ByteArrayToStructureBigEndianGenerated<AppleMapPartitionEntry>(partSector);
if(entry.signature == APM_MAGIC) if(entry.signature == APM_MAGIC)
{ {
@@ -252,7 +245,7 @@ public sealed class AppleMap : IPartition
} }
else else
{ {
entry = Marshal.ByteArrayToStructureBigEndian<AppleMapPartitionEntry>(partSector); entry = Marshal.ByteArrayToStructureBigEndianGenerated<AppleMapPartitionEntry>(partSector);
if(entry.signature == APM_MAGIC) if(entry.signature == APM_MAGIC)
{ {
@@ -275,15 +268,15 @@ public sealed class AppleMap : IPartition
AaruLogging.Debug(MODULE_NAME, "skip_ddm = {0}", skipDdm); AaruLogging.Debug(MODULE_NAME, "skip_ddm = {0}", skipDdm);
AaruLogging.Debug(MODULE_NAME, "sectors_to_read = {0}", sectorsToRead); AaruLogging.Debug(MODULE_NAME, "sectors_to_read = {0}", sectorsToRead);
byte[] copy = new byte[entries.Length - skipDdm]; var copy = new byte[entries.Length - skipDdm];
Array.Copy(entries, skipDdm, copy, 0, copy.Length); Array.Copy(entries, skipDdm, copy, 0, copy.Length);
entries = copy; entries = copy;
for(int i = 0; i < entryCount; i++) for(var i = 0; i < entryCount; i++)
{ {
byte[] tmp = new byte[entrySize]; var tmp = new byte[entrySize];
Array.Copy(entries, i * entrySize, tmp, 0, entrySize); Array.Copy(entries, i * entrySize, tmp, 0, entrySize);
entry = Marshal.ByteArrayToStructureBigEndian<AppleMapPartitionEntry>(tmp); entry = Marshal.ByteArrayToStructureBigEndianGenerated<AppleMapPartitionEntry>(tmp);
if(entry.signature != APM_MAGIC) continue; if(entry.signature != APM_MAGIC) continue;
@@ -293,15 +286,9 @@ public sealed class AppleMap : IPartition
AaruLogging.Debug(MODULE_NAME, "dpme[{0}].start = {1}", i, entry.start); AaruLogging.Debug(MODULE_NAME, "dpme[{0}].start = {1}", i, entry.start);
AaruLogging.Debug(MODULE_NAME, "dpme[{0}].sectors = {1}", i, entry.sectors); AaruLogging.Debug(MODULE_NAME, "dpme[{0}].sectors = {1}", i, entry.sectors);
AaruLogging.Debug(MODULE_NAME, AaruLogging.Debug(MODULE_NAME, "dpme[{0}].name = \"{1}\"", i, StringHandlers.CToString(entry.name));
"dpme[{0}].name = \"{1}\"",
i,
StringHandlers.CToString(entry.name));
AaruLogging.Debug(MODULE_NAME, AaruLogging.Debug(MODULE_NAME, "dpme[{0}].type = \"{1}\"", i, StringHandlers.CToString(entry.type));
"dpme[{0}].type = \"{1}\"",
i,
StringHandlers.CToString(entry.type));
AaruLogging.Debug(MODULE_NAME, "dpme[{0}].first_data_block = {1}", i, entry.first_data_block); AaruLogging.Debug(MODULE_NAME, "dpme[{0}].first_data_block = {1}", i, entry.first_data_block);
@@ -413,22 +400,23 @@ public sealed class AppleMap : IPartition
#region Nested type: AppleDriverDescriptorMap #region Nested type: AppleDriverDescriptorMap
[StructLayout(LayoutKind.Sequential, Pack = 1)] [StructLayout(LayoutKind.Sequential, Pack = 1)]
struct AppleDriverDescriptorMap [SwapEndian]
partial struct AppleDriverDescriptorMap
{ {
/// <summary>Signature <see cref="DDM_MAGIC" /></summary> /// <summary>Signature <see cref="DDM_MAGIC" /></summary>
public readonly ushort sbSig; public ushort sbSig;
/// <summary>Bytes per sector</summary> /// <summary>Bytes per sector</summary>
public readonly ushort sbBlockSize; public ushort sbBlockSize;
/// <summary>Sectors of the disk</summary> /// <summary>Sectors of the disk</summary>
public readonly uint sbBlocks; public uint sbBlocks;
/// <summary>Device type</summary> /// <summary>Device type</summary>
public readonly ushort sbDevType; public ushort sbDevType;
/// <summary>Device ID</summary> /// <summary>Device ID</summary>
public readonly ushort sbDevId; public ushort sbDevId;
/// <summary>Reserved</summary> /// <summary>Reserved</summary>
public readonly uint sbData; public uint sbData;
/// <summary>Number of entries of the driver descriptor</summary> /// <summary>Number of entries of the driver descriptor</summary>
public readonly ushort sbDrvrCount; public ushort sbDrvrCount;
/// <summary>Entries of the driver descriptor</summary> /// <summary>Entries of the driver descriptor</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 61)] [MarshalAs(UnmanagedType.ByValArray, SizeConst = 61)]
public AppleDriverEntry[] sbMap; public AppleDriverEntry[] sbMap;
@@ -439,14 +427,15 @@ public sealed class AppleMap : IPartition
#region Nested type: AppleDriverEntry #region Nested type: AppleDriverEntry
[StructLayout(LayoutKind.Sequential, Pack = 1)] [StructLayout(LayoutKind.Sequential, Pack = 1)]
struct AppleDriverEntry [SwapEndian]
partial struct AppleDriverEntry
{ {
/// <summary>First sector of the driver</summary> /// <summary>First sector of the driver</summary>
public readonly uint ddBlock; public uint ddBlock;
/// <summary>Size in 512bytes sectors of the driver</summary> /// <summary>Size in 512bytes sectors of the driver</summary>
public readonly ushort ddSize; public ushort ddSize;
/// <summary>Operating system (MacOS = 1)</summary> /// <summary>Operating system (MacOS = 1)</summary>
public readonly ushort ddType; public ushort ddType;
} }
#endregion #endregion
@@ -487,14 +476,15 @@ public sealed class AppleMap : IPartition
#region Nested type: AppleMapOldPartitionEntry #region Nested type: AppleMapOldPartitionEntry
[StructLayout(LayoutKind.Sequential, Pack = 1)] [StructLayout(LayoutKind.Sequential, Pack = 1)]
struct AppleMapOldPartitionEntry [SwapEndian]
partial struct AppleMapOldPartitionEntry
{ {
/// <summary>First sector of the partition</summary> /// <summary>First sector of the partition</summary>
public readonly uint pdStart; public uint pdStart;
/// <summary>Number of sectors of the partition</summary> /// <summary>Number of sectors of the partition</summary>
public readonly uint pdSize; public uint pdSize;
/// <summary>Partition type</summary> /// <summary>Partition type</summary>
public readonly uint pdFSID; public uint pdFSID;
} }
#endregion #endregion
@@ -502,50 +492,51 @@ public sealed class AppleMap : IPartition
#region Nested type: AppleMapPartitionEntry #region Nested type: AppleMapPartitionEntry
[StructLayout(LayoutKind.Sequential, Pack = 1)] [StructLayout(LayoutKind.Sequential, Pack = 1)]
struct AppleMapPartitionEntry [SwapEndian]
partial struct AppleMapPartitionEntry
{ {
/// <summary>Signature <see cref="APM_MAGIC" /></summary> /// <summary>Signature <see cref="APM_MAGIC" /></summary>
public readonly ushort signature; public ushort signature;
/// <summary>Reserved</summary> /// <summary>Reserved</summary>
public readonly ushort reserved1; public ushort reserved1;
/// <summary>Number of entries on the partition map, each one sector</summary> /// <summary>Number of entries on the partition map, each one sector</summary>
public readonly uint entries; public uint entries;
/// <summary>First sector of the partition</summary> /// <summary>First sector of the partition</summary>
public readonly uint start; public uint start;
/// <summary>Number of sectos of the partition</summary> /// <summary>Number of sectos of the partition</summary>
public readonly uint sectors; public uint sectors;
/// <summary>Partition name, 32 bytes, null-padded</summary> /// <summary>Partition name, 32 bytes, null-padded</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public readonly byte[] name; public byte[] name;
/// <summary>Partition type. 32 bytes, null-padded</summary> /// <summary>Partition type. 32 bytes, null-padded</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public readonly byte[] type; public byte[] type;
/// <summary>First sector of the data area</summary> /// <summary>First sector of the data area</summary>
public readonly uint first_data_block; public uint first_data_block;
/// <summary>Number of sectors of the data area</summary> /// <summary>Number of sectors of the data area</summary>
public readonly uint data_sectors; public uint data_sectors;
/// <summary>Partition flags</summary> /// <summary>Partition flags</summary>
public readonly uint flags; public uint flags;
/// <summary>First sector of the boot code</summary> /// <summary>First sector of the boot code</summary>
public readonly uint first_boot_block; public uint first_boot_block;
/// <summary>Size in bytes of the boot code</summary> /// <summary>Size in bytes of the boot code</summary>
public readonly uint boot_size; public uint boot_size;
/// <summary>Load address of the boot code</summary> /// <summary>Load address of the boot code</summary>
public readonly uint load_address; public uint load_address;
/// <summary>Load address of the boot code</summary> /// <summary>Load address of the boot code</summary>
public readonly uint load_address2; public uint load_address2;
/// <summary>Entry point of the boot code</summary> /// <summary>Entry point of the boot code</summary>
public readonly uint entry_point; public uint entry_point;
/// <summary>Entry point of the boot code</summary> /// <summary>Entry point of the boot code</summary>
public readonly uint entry_point2; public uint entry_point2;
/// <summary>Boot code checksum</summary> /// <summary>Boot code checksum</summary>
public readonly uint checksum; public uint checksum;
/// <summary>Processor type, 16 bytes, null-padded</summary> /// <summary>Processor type, 16 bytes, null-padded</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public readonly byte[] processor; public byte[] processor;
/// <summary>Boot arguments</summary> /// <summary>Boot arguments</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public readonly uint[] boot_arguments; public uint[] boot_arguments;
} }
#endregion #endregion
@@ -553,13 +544,14 @@ public sealed class AppleMap : IPartition
#region Nested type: AppleOldDevicePartitionMap #region Nested type: AppleOldDevicePartitionMap
[StructLayout(LayoutKind.Sequential, Pack = 1)] [StructLayout(LayoutKind.Sequential, Pack = 1)]
struct AppleOldDevicePartitionMap [SwapEndian]
partial struct AppleOldDevicePartitionMap
{ {
/// <summary>Signature <see cref="APM_MAGIC_OLD" /></summary> /// <summary>Signature <see cref="APM_MAGIC_OLD" /></summary>
public readonly ushort pdSig; public ushort pdSig;
/// <summary>Entries of the driver descriptor</summary> /// <summary>Entries of the driver descriptor</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 42)] [MarshalAs(UnmanagedType.ByValArray, SizeConst = 42)]
public readonly AppleMapOldPartitionEntry[] pdMap; public AppleMapOldPartitionEntry[] pdMap;
} }
#endregion #endregion