-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathPC98.cs
215 lines (179 loc) · 8.83 KB
/
PC98.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : PC98.cs
// Author(s) : Natalia Portillo <[email protected]>
//
// Component : Partitioning scheme plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Manages NEC PC-9800 partitions.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2024 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Console;
using Aaru.Helpers;
using Marshal = Aaru.Helpers.Marshal;
namespace Aaru.Partitions;
/// <inheritdoc />
/// <summary>Implements decoding of NEC PC-9800 partitions</summary>
public sealed class PC98 : IPartition
{
const string MODULE_NAME = "PC-98 partitions plugin";
#region IPartition Members
/// <inheritdoc />
public string Name => Localization.PC98_Name;
/// <inheritdoc />
public Guid Id => new("27333401-C7C2-447D-961C-22AD0641A09A");
/// <inheritdoc />
public string Author => Authors.NATALIA_PORTILLO;
/// <inheritdoc />
public bool GetInformation(IMediaImage imagePlugin, out List<CommonTypes.Partition> partitions, ulong sectorOffset)
{
partitions = [];
if(sectorOffset != 0) return false;
ErrorNumber errno = imagePlugin.ReadSector(0, out byte[] bootSector);
if(errno != ErrorNumber.NoError || bootSector[^2] != 0x55 || bootSector[^1] != 0xAA) return false;
errno = imagePlugin.ReadSector(1, out byte[] sector);
if(errno != ErrorNumber.NoError) return false;
// Prevent false positives with some FAT BPBs
if(Encoding.ASCII.GetString(bootSector, 0x36, 3) == Localization.FAT) return false;
Table table = Marshal.ByteArrayToStructureLittleEndian<Table>(sector);
ulong counter = 0;
foreach(Partition entry in table.entries)
{
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_mid = {0}", entry.dp_mid);
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_sid = {0}", entry.dp_sid);
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_dum1 = {0}", entry.dp_dum1);
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_dum2 = {0}", entry.dp_dum2);
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_ipl_sct = {0}", entry.dp_ipl_sct);
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_ipl_head = {0}", entry.dp_ipl_head);
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_ipl_cyl = {0}", entry.dp_ipl_cyl);
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_ssect = {0}", entry.dp_ssect);
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_shd = {0}", entry.dp_shd);
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_scyl = {0}", entry.dp_scyl);
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_esect = {0}", entry.dp_esect);
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_ehd = {0}", entry.dp_ehd);
AaruConsole.DebugWriteLine(MODULE_NAME, "entry.dp_ecyl = {0}", entry.dp_ecyl);
AaruConsole.DebugWriteLine(MODULE_NAME,
"entry.dp_name = \"{0}\"",
StringHandlers.CToString(entry.dp_name, Encoding.GetEncoding(932)));
if(entry.dp_scyl == entry.dp_ecyl ||
entry.dp_ecyl <= 0 ||
entry.dp_scyl > imagePlugin.Info.Cylinders ||
entry.dp_ecyl > imagePlugin.Info.Cylinders ||
entry.dp_shd > imagePlugin.Info.Heads ||
entry.dp_ehd > imagePlugin.Info.Heads ||
entry.dp_ssect > imagePlugin.Info.SectorsPerTrack ||
entry.dp_esect > imagePlugin.Info.SectorsPerTrack)
continue;
var part = new CommonTypes.Partition
{
Start =
CHS.ToLBA(entry.dp_scyl,
entry.dp_shd,
(uint)(entry.dp_ssect + 1),
imagePlugin.Info.Heads,
imagePlugin.Info.SectorsPerTrack),
Type = DecodePC98Sid(entry.dp_sid),
Name = StringHandlers.CToString(entry.dp_name, Encoding.GetEncoding(932)).Trim(),
Sequence = counter,
Scheme = Name
};
part.Offset = part.Start * imagePlugin.Info.SectorSize;
part.Length = CHS.ToLBA(entry.dp_ecyl,
entry.dp_ehd,
(uint)(entry.dp_esect + 1),
imagePlugin.Info.Heads,
imagePlugin.Info.SectorsPerTrack) -
part.Start;
part.Size = part.Length * imagePlugin.Info.SectorSize;
AaruConsole.DebugWriteLine(MODULE_NAME, "part.Start = {0}", part.Start);
AaruConsole.DebugWriteLine(MODULE_NAME, "part.Type = {0}", part.Type);
AaruConsole.DebugWriteLine(MODULE_NAME, "part.Name = {0}", part.Name);
AaruConsole.DebugWriteLine(MODULE_NAME, "part.Sequence = {0}", part.Sequence);
AaruConsole.DebugWriteLine(MODULE_NAME, "part.Offset = {0}", part.Offset);
AaruConsole.DebugWriteLine(MODULE_NAME, "part.Length = {0}", part.Length);
AaruConsole.DebugWriteLine(MODULE_NAME, "part.Size = {0}", part.Size);
if((entry.dp_mid & 0x20) != 0x20 && (entry.dp_mid & 0x44) != 0x44 ||
part.Start >= imagePlugin.Info.Sectors ||
part.End > imagePlugin.Info.Sectors)
continue;
partitions.Add(part);
counter++;
}
return partitions.Count > 0;
}
#endregion
static string DecodePC98Sid(byte sid)
{
return (sid & 0x7F) switch
{
0x01 => Localization.FAT12,
0x04 => Localization.PC_UX,
0x06 => Localization.N88_BASIC_86,
// Supposedly for FAT16 < 32 MiB, seen in bigger partitions
0x11 or 0x21 => Localization.FAT16,
0x28 or 0x41 or 0x48 => Localization.Windows_Volume_Set,
0x44 => Localization.FreeBSD,
0x61 => Localization.FAT32,
0x62 => Localization.Linux,
_ => Localization.Unknown_partition_type
};
}
#region Nested type: Partition
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct Partition
{
/// <summary>Some ID, if 0x80 bit is set, it is bootable</summary>
public readonly byte dp_mid;
/// <summary>Some ID, if 0x80 bit is set, it is active</summary>
public readonly byte dp_sid;
public readonly byte dp_dum1;
public readonly byte dp_dum2;
public readonly byte dp_ipl_sct;
public readonly byte dp_ipl_head;
public readonly ushort dp_ipl_cyl;
public readonly byte dp_ssect;
public readonly byte dp_shd;
public readonly ushort dp_scyl;
public readonly byte dp_esect;
public readonly byte dp_ehd;
public readonly ushort dp_ecyl;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public readonly byte[] dp_name;
}
#endregion
#region Nested type: Table
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct Table
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public readonly Partition[] entries;
}
#endregion
}