Reactos
1/*
2 * PROJECT: ReactOS Setup Library
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Partition list functions
5 * COPYRIGHT: Copyright 2003-2019 Casper S. Hornstrup (chorns@users.sourceforge.net)
6 * Copyright 2018-2024 Hermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
7 */
8
9#pragma once
10
11/* EXTRA HANDFUL MACROS *****************************************************/
12
13// NOTE: They should be moved into some global header.
14
15/* OEM MBR partition types recognized by NT (see [MS-DMRP] Appendix B) */
16#define PARTITION_EISA 0x12 // EISA partition
17#define PARTITION_HIBERNATION 0x84 // Hibernation partition for laptops
18#define PARTITION_DIAGNOSTIC 0xA0 // Diagnostic partition on some Hewlett-Packard (HP) notebooks
19#define PARTITION_DELL 0xDE // Dell partition
20#define PARTITION_IBM 0xFE // IBM Initial Microprogram Load (IML) partition
21
22#define IsOEMPartition(PartitionType) \
23 ( ((PartitionType) == PARTITION_EISA) || \
24 ((PartitionType) == PARTITION_HIBERNATION) || \
25 ((PartitionType) == PARTITION_DIAGNOSTIC) || \
26 ((PartitionType) == PARTITION_DELL) || \
27 ((PartitionType) == PARTITION_IBM) )
28
29
30/* PARTITION UTILITY FUNCTIONS **********************************************/
31
32typedef enum _FORMATSTATE
33{
34 Unformatted,
35 UnformattedOrDamaged,
36 UnknownFormat,
37 Formatted
38} FORMATSTATE, *PFORMATSTATE;
39
40#include "volutil.h"
41
42typedef struct _PARTENTRY PARTENTRY, *PPARTENTRY;
43typedef struct _VOLENTRY
44{
45 LIST_ENTRY ListEntry; ///< Entry in VolumesList
46
47 VOLINFO Info;
48 FORMATSTATE FormatState;
49
50 /* Volume must be checked */
51 BOOLEAN NeedsCheck;
52 /* Volume is new and has not yet been actually formatted and mounted */
53 BOOLEAN New;
54
55 // union {
56 // PVOLUME_DISK_EXTENTS pExtents;
57 PPARTENTRY PartEntry;
58 // };
59} VOLENTRY, *PVOLENTRY;
60
61typedef struct _PARTENTRY
62{
63 LIST_ENTRY ListEntry;
64
65 /* The disk this partition belongs to */
66 struct _DISKENTRY *DiskEntry;
67
68 /* Partition geometry */
69 ULARGE_INTEGER StartSector;
70 ULARGE_INTEGER SectorCount;
71
72 BOOLEAN BootIndicator; // NOTE: See comment for the PARTLIST::SystemPartition member.
73 UCHAR PartitionType;
74 ULONG OnDiskPartitionNumber; /* Enumerated partition number (primary partitions first, excluding the extended partition container, then the logical partitions) */
75 ULONG PartitionNumber; /* Current partition number, only valid for the currently running NTOS instance */
76 ULONG PartitionIndex; /* Index in the LayoutBuffer->PartitionEntry[] cached array of the corresponding DiskEntry */
77 WCHAR DeviceName[MAX_PATH]; ///< NT device name: "\Device\HarddiskM\PartitionN"
78
79 BOOLEAN LogicalPartition;
80
81 /* Partition is partitioned disk space */
82 BOOLEAN IsPartitioned;
83
84 /* Partition is new, table does not exist on disk yet */
85 BOOLEAN New;
86
87 /*
88 * Volume-related properties:
89 * NULL: No volume is associated to this partition (either because it is
90 * an empty disk region, or the partition type is unrecognized).
91 * 0x1 : TBD.
92 * Valid pointer: A basic volume associated to this partition is (or will)
93 * be mounted by the PARTMGR and enumerated by the MOUNTMGR.
94 */
95 PVOLENTRY Volume;
96
97} PARTENTRY, *PPARTENTRY;
98
99typedef struct _DISKENTRY
100{
101 LIST_ENTRY ListEntry;
102
103 /* The list of disks/partitions this disk belongs to */
104 struct _PARTLIST *PartList;
105
106 MEDIA_TYPE MediaType; /* FixedMedia or RemovableMedia */
107
108 /* Disk geometry */
109
110 ULONGLONG Cylinders;
111 ULONG TracksPerCylinder;
112 ULONG SectorsPerTrack;
113 ULONG BytesPerSector;
114
115 ULARGE_INTEGER SectorCount;
116 ULONG SectorAlignment;
117 ULONG CylinderAlignment;
118
119 /* BIOS Firmware parameters */
120 BOOLEAN BiosFound;
121 ULONG HwAdapterNumber;
122 ULONG HwControllerNumber;
123 ULONG HwDiskNumber; /* Disk number currently assigned on the system */
124 ULONG HwFixedDiskNumber; /* Disk number on the system when *ALL* removable disks are not connected */
125// ULONG Signature; // Obtained from LayoutBuffer->Signature
126// ULONG Checksum;
127
128 /* SCSI parameters */
129 ULONG DiskNumber;
130// SCSI_ADDRESS;
131 USHORT Port;
132 USHORT Bus;
133 USHORT Id;
134
135 /* Has the partition list been modified? */
136 BOOLEAN Dirty;
137
138 BOOLEAN NewDisk; /* If TRUE, the disk is uninitialized */
139 PARTITION_STYLE DiskStyle; /* MBR/GPT-partitioned disk, or uninitialized disk (RAW) */
140
141 UNICODE_STRING DriverName;
142
143 PDRIVE_LAYOUT_INFORMATION LayoutBuffer;
144 // TODO: When adding support for GPT disks:
145 // Use PDRIVE_LAYOUT_INFORMATION_EX which indicates whether
146 // the disk is MBR, GPT, or unknown (uninitialized).
147 // Depending on the style, either use the MBR or GPT partition info.
148
149 LIST_ENTRY PrimaryPartListHead; /* List of primary partitions */
150 LIST_ENTRY LogicalPartListHead; /* List of logical partitions (Valid only for MBR-partitioned disks) */
151
152 /* Pointer to the unique extended partition on this disk (Valid only for MBR-partitioned disks) */
153 PPARTENTRY ExtendedPartition;
154
155} DISKENTRY, *PDISKENTRY;
156
157typedef struct _BIOSDISKENTRY
158{
159 LIST_ENTRY ListEntry;
160 ULONG AdapterNumber;
161 ULONG ControllerNumber;
162 ULONG DiskNumber;
163 ULONG Signature;
164 ULONG Checksum;
165 PDISKENTRY DiskEntry; /* Corresponding recognized disk; is NULL if the disk is not recognized */ // RecognizedDiskEntry;
166 CM_DISK_GEOMETRY_DEVICE_DATA DiskGeometry;
167 CM_INT13_DRIVE_PARAMETER Int13DiskData;
168} BIOSDISKENTRY, *PBIOSDISKENTRY;
169
170typedef struct _PARTLIST
171{
172 /*
173 * The system partition where the boot manager resides.
174 * The corresponding system disk is obtained via:
175 * SystemPartition->DiskEntry.
176 */
177 // NOTE: It seems to appear that the specifications of ARC and (u)EFI
178 // actually allow for multiple system partitions to exist on the system.
179 // If so we should instead rely on the BootIndicator bit of the PARTENTRY
180 // structure in order to find these.
181 PPARTENTRY SystemPartition;
182
183 LIST_ENTRY DiskListHead;
184 LIST_ENTRY BiosDiskListHead;
185
186 /* (Basic) Volumes management */
187 LIST_ENTRY VolumesList;
188
189} PARTLIST, *PPARTLIST;
190
191#define PARTITION_TBL_SIZE 4
192
193#define PARTITION_MAGIC 0xAA55
194
195/* Defines system type for MBR showing that a GPT is following */
196#define EFI_PMBR_OSTYPE_EFI 0xEE
197
198#include <pshpack1.h>
199
200typedef struct _PARTITION
201{
202 unsigned char BootFlags; /* bootable? 0=no, 128=yes */
203 unsigned char StartingHead; /* beginning head number */
204 unsigned char StartingSector; /* beginning sector number */
205 unsigned char StartingCylinder; /* 10 bit nmbr, with high 2 bits put in begsect */
206 unsigned char PartitionType; /* Operating System type indicator code */
207 unsigned char EndingHead; /* ending head number */
208 unsigned char EndingSector; /* ending sector number */
209 unsigned char EndingCylinder; /* also a 10 bit nmbr, with same high 2 bit trick */
210 unsigned int StartingBlock; /* first sector relative to start of disk */
211 unsigned int SectorCount; /* number of sectors in partition */
212} PARTITION, *PPARTITION;
213
214typedef struct _PARTITION_SECTOR
215{
216 UCHAR BootCode[440]; /* 0x000 */
217 ULONG Signature; /* 0x1B8 */
218 UCHAR Reserved[2]; /* 0x1BC */
219 PARTITION Partition[PARTITION_TBL_SIZE]; /* 0x1BE */
220 USHORT Magic; /* 0x1FE */
221} PARTITION_SECTOR, *PPARTITION_SECTOR;
222
223#include <poppack.h>
224
225typedef struct
226{
227 LIST_ENTRY ListEntry;
228 ULONG DiskNumber;
229 ULONG Identifier;
230 ULONG Signature;
231} BIOS_DISK, *PBIOS_DISK;
232
233
234
235ULONGLONG
236AlignDown(
237 IN ULONGLONG Value,
238 IN ULONG Alignment);
239
240ULONGLONG
241AlignUp(
242 IN ULONGLONG Value,
243 IN ULONG Alignment);
244
245ULONGLONG
246RoundingDivide(
247 IN ULONGLONG Dividend,
248 IN ULONGLONG Divisor);
249
250
251#define GetPartEntryOffsetInBytes(PartEntry) \
252 ((PartEntry)->StartSector.QuadPart * (PartEntry)->DiskEntry->BytesPerSector)
253
254#define GetPartEntrySizeInBytes(PartEntry) \
255 ((PartEntry)->SectorCount.QuadPart * (PartEntry)->DiskEntry->BytesPerSector)
256
257#define GetDiskSizeInBytes(DiskEntry) \
258 ((DiskEntry)->SectorCount.QuadPart * (DiskEntry)->BytesPerSector)
259
260
261BOOLEAN
262IsDiskSuperFloppy2(
263 _In_ const DISK_PARTITION_INFO* DiskInfo,
264 _In_opt_ const ULONGLONG* DiskSize,
265 _In_ const PARTITION_INFORMATION* PartitionInfo);
266
267BOOLEAN
268IsDiskSuperFloppy(
269 _In_ const DRIVE_LAYOUT_INFORMATION* Layout,
270 _In_opt_ const ULONGLONG* DiskSize);
271
272BOOLEAN
273IsDiskSuperFloppyEx(
274 _In_ const DRIVE_LAYOUT_INFORMATION_EX* LayoutEx,
275 _In_opt_ const ULONGLONG* DiskSize);
276
277BOOLEAN
278IsSuperFloppy(
279 _In_ PDISKENTRY DiskEntry);
280
281BOOLEAN
282IsPartitionActive(
283 IN PPARTENTRY PartEntry);
284
285PPARTLIST
286NTAPI
287CreatePartitionList(VOID);
288
289VOID
290NTAPI
291DestroyPartitionList(
292 IN PPARTLIST List);
293
294PDISKENTRY
295GetDiskByBiosNumber(
296 _In_ PPARTLIST List,
297 _In_ ULONG HwDiskNumber);
298
299PDISKENTRY
300GetDiskByNumber(
301 _In_ PPARTLIST List,
302 _In_ ULONG DiskNumber);
303
304PDISKENTRY
305GetDiskBySCSI(
306 _In_ PPARTLIST List,
307 _In_ USHORT Port,
308 _In_ USHORT Bus,
309 _In_ USHORT Id);
310
311PDISKENTRY
312GetDiskBySignature(
313 _In_ PPARTLIST List,
314 _In_ ULONG Signature);
315
316PPARTENTRY
317GetPartition(
318 _In_ PDISKENTRY DiskEntry,
319 _In_ ULONG PartitionNumber);
320
321PPARTENTRY
322SelectPartition(
323 _In_ PPARTLIST List,
324 _In_ ULONG DiskNumber,
325 _In_ ULONG PartitionNumber);
326
327PPARTENTRY
328NTAPI
329GetNextPartition(
330 IN PPARTLIST List,
331 IN PPARTENTRY CurrentPart OPTIONAL);
332
333PPARTENTRY
334NTAPI
335GetPrevPartition(
336 IN PPARTLIST List,
337 IN PPARTENTRY CurrentPart OPTIONAL);
338
339PPARTENTRY
340NTAPI
341GetAdjUnpartitionedEntry(
342 _In_ PPARTENTRY PartEntry,
343 _In_ BOOLEAN Direction);
344
345ERROR_NUMBER
346NTAPI
347PartitionCreateChecks(
348 _In_ PPARTENTRY PartEntry,
349 _In_opt_ ULONGLONG SizeBytes,
350 _In_opt_ ULONG_PTR PartitionInfo);
351
352BOOLEAN
353NTAPI
354CreatePartition(
355 _In_ PPARTLIST List,
356 _Inout_ PPARTENTRY PartEntry,
357 _In_opt_ ULONGLONG SizeBytes,
358 _In_opt_ ULONG_PTR PartitionInfo);
359
360BOOLEAN
361NTAPI
362DeletePartition(
363 _In_ PPARTLIST List,
364 _In_ PPARTENTRY PartEntry,
365 _Out_opt_ PPARTENTRY* FreeRegion);
366
367PPARTENTRY
368FindSupportedSystemPartition(
369 IN PPARTLIST List,
370 IN BOOLEAN ForceSelect,
371 IN PDISKENTRY AlternativeDisk OPTIONAL,
372 IN PPARTENTRY AlternativePart OPTIONAL);
373
374BOOLEAN
375SetActivePartition(
376 IN PPARTLIST List,
377 IN PPARTENTRY PartEntry,
378 IN PPARTENTRY OldActivePart OPTIONAL);
379
380NTSTATUS
381WritePartitions(
382 IN PDISKENTRY DiskEntry);
383
384BOOLEAN
385WritePartitionsToDisk(
386 IN PPARTLIST List);
387
388BOOLEAN
389SetMountedDeviceValues(
390 _In_ PPARTLIST List);
391
392VOID
393SetMBRPartitionType(
394 IN PPARTENTRY PartEntry,
395 IN UCHAR PartitionType);
396
397/* EOF */