[SETUPLIB][USETUP] Code simplifications & fixes.
[reactos.git] / base / setup / lib / utils / partlist.h
1 /*
2 * PROJECT: ReactOS Setup Library
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Partition list functions
5 * COPYRIGHT: Copyright 2003-2019 Casper S. Hornstrup (chorns@users.sourceforge.net)
6 * Copyright 2018-2019 Hermes Belusca-Maito
7 */
8
9 #pragma once
10
11 /* HELPERS FOR PARTITION TYPES **********************************************/
12
13 typedef struct _PARTITION_TYPE
14 {
15 UCHAR Type;
16 PCHAR Description;
17 } PARTITION_TYPE, *PPARTITION_TYPE;
18
19 #define NUM_PARTITION_TYPE_ENTRIES 143
20 extern PARTITION_TYPE PartitionTypes[NUM_PARTITION_TYPE_ENTRIES];
21
22
23 /* PARTITION UTILITY FUNCTIONS **********************************************/
24
25 typedef enum _FORMATSTATE
26 {
27 Unformatted,
28 UnformattedOrDamaged,
29 UnknownFormat,
30 Preformatted,
31 Formatted
32 } FORMATSTATE, *PFORMATSTATE;
33
34 typedef struct _PARTENTRY
35 {
36 LIST_ENTRY ListEntry;
37
38 /* The disk this partition belongs to */
39 struct _DISKENTRY *DiskEntry;
40
41 /* Partition geometry */
42 ULARGE_INTEGER StartSector;
43 ULARGE_INTEGER SectorCount;
44
45 BOOLEAN BootIndicator;
46 UCHAR PartitionType;
47 ULONG OnDiskPartitionNumber; /* Enumerated partition number (primary partitions first, excluding the extended partition container, then the logical partitions) */
48 ULONG PartitionNumber; /* Current partition number, only valid for the currently running NTOS instance */
49 ULONG PartitionIndex; /* Index in the LayoutBuffer->PartitionEntry[] cached array of the corresponding DiskEntry */
50
51 WCHAR DriveLetter;
52 WCHAR VolumeLabel[20];
53 WCHAR FileSystem[MAX_PATH+1];
54 FORMATSTATE FormatState;
55
56 BOOLEAN LogicalPartition;
57
58 /* Partition is partitioned disk space */
59 BOOLEAN IsPartitioned;
60
61 /** The following three properties may be replaced by flags **/
62
63 /* Partition is new, table does not exist on disk yet */
64 BOOLEAN New;
65
66 /* Partition was created automatically */
67 BOOLEAN AutoCreate;
68
69 /* Partition must be checked */
70 BOOLEAN NeedsCheck;
71
72 } PARTENTRY, *PPARTENTRY;
73
74 typedef struct _DISKENTRY
75 {
76 LIST_ENTRY ListEntry;
77
78 /* The list of disks/partitions this disk belongs to */
79 struct _PARTLIST *PartList;
80
81 MEDIA_TYPE MediaType; /* FixedMedia or RemovableMedia */
82
83 /* Disk geometry */
84
85 ULONGLONG Cylinders;
86 ULONG TracksPerCylinder;
87 ULONG SectorsPerTrack;
88 ULONG BytesPerSector;
89
90 ULARGE_INTEGER SectorCount;
91 ULONG SectorAlignment;
92 ULONG CylinderAlignment;
93
94 /* BIOS Firmware parameters */
95 BOOLEAN BiosFound;
96 ULONG HwAdapterNumber;
97 ULONG HwControllerNumber;
98 ULONG HwDiskNumber; /* Disk number currently assigned on the system */
99 ULONG HwFixedDiskNumber; /* Disk number on the system when *ALL* removable disks are not connected */
100 // ULONG Signature; // Obtained from LayoutBuffer->Signature
101 // ULONG Checksum;
102
103 /* SCSI parameters */
104 ULONG DiskNumber;
105 // SCSI_ADDRESS;
106 USHORT Port;
107 USHORT Bus;
108 USHORT Id;
109
110 /* Has the partition list been modified? */
111 BOOLEAN Dirty;
112
113 BOOLEAN NewDisk; /* If TRUE, the disk is uninitialized */
114 PARTITION_STYLE DiskStyle; /* MBR/GPT-partitioned disk, or uninitialized disk (RAW) */
115
116 UNICODE_STRING DriverName;
117
118 PDRIVE_LAYOUT_INFORMATION LayoutBuffer;
119 // TODO: When adding support for GPT disks:
120 // Use PDRIVE_LAYOUT_INFORMATION_EX which indicates whether
121 // the disk is MBR, GPT, or unknown (uninitialized).
122 // Depending on the style, either use the MBR or GPT partition info.
123
124 LIST_ENTRY PrimaryPartListHead; /* List of primary partitions */
125 LIST_ENTRY LogicalPartListHead; /* List of logical partitions (Valid only for MBR-partitioned disks) */
126
127 /* Pointer to the unique extended partition on this disk (Valid only for MBR-partitioned disks) */
128 PPARTENTRY ExtendedPartition;
129
130 } DISKENTRY, *PDISKENTRY;
131
132 typedef struct _BIOSDISKENTRY
133 {
134 LIST_ENTRY ListEntry;
135 ULONG AdapterNumber;
136 ULONG ControllerNumber;
137 ULONG DiskNumber;
138 ULONG Signature;
139 ULONG Checksum;
140 PDISKENTRY DiskEntry; /* Corresponding recognized disk; is NULL if the disk is not recognized */ // RecognizedDiskEntry;
141 CM_DISK_GEOMETRY_DEVICE_DATA DiskGeometry;
142 CM_INT13_DRIVE_PARAMETER Int13DiskData;
143 } BIOSDISKENTRY, *PBIOSDISKENTRY;
144
145 typedef struct _PARTLIST
146 {
147 /*
148 * The system partition where the boot manager resides.
149 * The corresponding system disk is obtained via:
150 * SystemPartition->DiskEntry.
151 */
152 PPARTENTRY SystemPartition;
153 /*
154 * The original system partition in case we are redefining it because
155 * we do not have write support on it.
156 * Please note that this is partly a HACK and MUST NEVER happen on
157 * architectures where real system partitions are mandatory (because then
158 * they are formatted in FAT FS and we support write operation on them).
159 * The corresponding original system disk is obtained via:
160 * OriginalSystemPartition->DiskEntry.
161 */
162 PPARTENTRY OriginalSystemPartition;
163
164 LIST_ENTRY DiskListHead;
165 LIST_ENTRY BiosDiskListHead;
166
167 } PARTLIST, *PPARTLIST;
168
169 #define PARTITION_TBL_SIZE 4
170
171 #define PARTITION_MAGIC 0xAA55
172
173 /* Defines system type for MBR showing that a GPT is following */
174 #define EFI_PMBR_OSTYPE_EFI 0xEE
175
176 #include <pshpack1.h>
177
178 typedef struct _PARTITION
179 {
180 unsigned char BootFlags; /* bootable? 0=no, 128=yes */
181 unsigned char StartingHead; /* beginning head number */
182 unsigned char StartingSector; /* beginning sector number */
183 unsigned char StartingCylinder; /* 10 bit nmbr, with high 2 bits put in begsect */
184 unsigned char PartitionType; /* Operating System type indicator code */
185 unsigned char EndingHead; /* ending head number */
186 unsigned char EndingSector; /* ending sector number */
187 unsigned char EndingCylinder; /* also a 10 bit nmbr, with same high 2 bit trick */
188 unsigned int StartingBlock; /* first sector relative to start of disk */
189 unsigned int SectorCount; /* number of sectors in partition */
190 } PARTITION, *PPARTITION;
191
192 typedef struct _PARTITION_SECTOR
193 {
194 UCHAR BootCode[440]; /* 0x000 */
195 ULONG Signature; /* 0x1B8 */
196 UCHAR Reserved[2]; /* 0x1BC */
197 PARTITION Partition[PARTITION_TBL_SIZE]; /* 0x1BE */
198 USHORT Magic; /* 0x1FE */
199 } PARTITION_SECTOR, *PPARTITION_SECTOR;
200
201 #include <poppack.h>
202
203 typedef struct
204 {
205 LIST_ENTRY ListEntry;
206 ULONG DiskNumber;
207 ULONG Identifier;
208 ULONG Signature;
209 } BIOS_DISK, *PBIOS_DISK;
210
211
212
213 ULONGLONG
214 AlignDown(
215 IN ULONGLONG Value,
216 IN ULONG Alignment);
217
218 ULONGLONG
219 AlignUp(
220 IN ULONGLONG Value,
221 IN ULONG Alignment);
222
223 ULONGLONG
224 RoundingDivide(
225 IN ULONGLONG Dividend,
226 IN ULONGLONG Divisor);
227
228
229 BOOLEAN
230 IsSuperFloppy(
231 IN PDISKENTRY DiskEntry);
232
233 BOOLEAN
234 IsPartitionActive(
235 IN PPARTENTRY PartEntry);
236
237 PPARTLIST
238 CreatePartitionList(VOID);
239
240 VOID
241 DestroyPartitionList(
242 IN PPARTLIST List);
243
244 PDISKENTRY
245 GetDiskByBiosNumber(
246 IN PPARTLIST List,
247 IN ULONG HwDiskNumber);
248
249 PDISKENTRY
250 GetDiskByNumber(
251 IN PPARTLIST List,
252 IN ULONG DiskNumber);
253
254 PDISKENTRY
255 GetDiskBySCSI(
256 IN PPARTLIST List,
257 IN USHORT Port,
258 IN USHORT Bus,
259 IN USHORT Id);
260
261 PDISKENTRY
262 GetDiskBySignature(
263 IN PPARTLIST List,
264 IN ULONG Signature);
265
266 PPARTENTRY
267 GetPartition(
268 // IN PPARTLIST List,
269 IN PDISKENTRY DiskEntry,
270 IN ULONG PartitionNumber);
271
272 BOOLEAN
273 GetDiskOrPartition(
274 IN PPARTLIST List,
275 IN ULONG DiskNumber,
276 IN ULONG PartitionNumber OPTIONAL,
277 OUT PDISKENTRY* pDiskEntry,
278 OUT PPARTENTRY* pPartEntry OPTIONAL);
279
280 PPARTENTRY
281 SelectPartition(
282 IN PPARTLIST List,
283 IN ULONG DiskNumber,
284 IN ULONG PartitionNumber);
285
286 PPARTENTRY
287 GetNextPartition(
288 IN PPARTLIST List,
289 IN PPARTENTRY CurrentPart OPTIONAL);
290
291 PPARTENTRY
292 GetPrevPartition(
293 IN PPARTLIST List,
294 IN PPARTENTRY CurrentPart OPTIONAL);
295
296 BOOLEAN
297 CreatePrimaryPartition(
298 IN PPARTLIST List,
299 IN OUT PPARTENTRY PartEntry,
300 IN ULONGLONG SectorCount,
301 IN BOOLEAN AutoCreate);
302
303 BOOLEAN
304 CreateExtendedPartition(
305 IN PPARTLIST List,
306 IN OUT PPARTENTRY PartEntry,
307 IN ULONGLONG SectorCount);
308
309 BOOLEAN
310 CreateLogicalPartition(
311 IN PPARTLIST List,
312 IN OUT PPARTENTRY PartEntry,
313 IN ULONGLONG SectorCount,
314 IN BOOLEAN AutoCreate);
315
316 NTSTATUS
317 DismountVolume(
318 IN PPARTENTRY PartEntry);
319
320 BOOLEAN
321 DeletePartition(
322 IN PPARTLIST List,
323 IN PPARTENTRY PartEntry,
324 OUT PPARTENTRY* FreeRegion OPTIONAL);
325
326 VOID
327 CheckActiveSystemPartition(
328 IN PPARTLIST List,
329 IN BOOLEAN ForceSelect,
330 IN PDISKENTRY AlternateDisk OPTIONAL,
331 IN PPARTENTRY AlternatePart OPTIONAL);
332
333 NTSTATUS
334 WritePartitions(
335 IN PDISKENTRY DiskEntry);
336
337 BOOLEAN
338 WritePartitionsToDisk(
339 IN PPARTLIST List);
340
341 BOOLEAN
342 SetMountedDeviceValue(
343 IN WCHAR Letter,
344 IN ULONG Signature,
345 IN LARGE_INTEGER StartingOffset);
346
347 BOOLEAN
348 SetMountedDeviceValues(
349 IN PPARTLIST List);
350
351 VOID
352 SetPartitionType(
353 IN PPARTENTRY PartEntry,
354 IN UCHAR PartitionType);
355
356 ERROR_NUMBER
357 PrimaryPartitionCreationChecks(
358 IN PPARTENTRY PartEntry);
359
360 ERROR_NUMBER
361 ExtendedPartitionCreationChecks(
362 IN PPARTENTRY PartEntry);
363
364 ERROR_NUMBER
365 LogicalPartitionCreationChecks(
366 IN PPARTENTRY PartEntry);
367
368 BOOLEAN
369 GetNextUnformattedPartition(
370 IN PPARTLIST List,
371 OUT PDISKENTRY *pDiskEntry OPTIONAL,
372 OUT PPARTENTRY *pPartEntry);
373
374 BOOLEAN
375 GetNextUncheckedPartition(
376 IN PPARTLIST List,
377 OUT PDISKENTRY *pDiskEntry OPTIONAL,
378 OUT PPARTENTRY *pPartEntry);
379
380 /* EOF */