[SETUPLIB][USETUP] Minor improvements.
[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 HiddenSectors;
48 ULONG OnDiskPartitionNumber; /* Enumerated partition number (primary partitions first, excluding the extended partition container, then the logical partitions) */
49 ULONG PartitionNumber; /* Current partition number, only valid for the currently running NTOS instance */
50 ULONG PartitionIndex; /* Index in the LayoutBuffer->PartitionEntry[] cached array of the corresponding DiskEntry */
51
52 WCHAR DriveLetter;
53 WCHAR VolumeLabel[20];
54 WCHAR FileSystem[MAX_PATH+1];
55 FORMATSTATE FormatState;
56
57 BOOLEAN LogicalPartition;
58
59 /* Partition is partitioned disk space */
60 BOOLEAN IsPartitioned;
61
62 /** The following three properties may be replaced by flags **/
63
64 /* Partition is new, table does not exist on disk yet */
65 BOOLEAN New;
66
67 /* Partition was created automatically */
68 BOOLEAN AutoCreate;
69
70 /* Partition must be checked */
71 BOOLEAN NeedsCheck;
72
73 } PARTENTRY, *PPARTENTRY;
74
75
76 typedef struct _BIOSDISKENTRY
77 {
78 LIST_ENTRY ListEntry;
79 ULONG DiskNumber;
80 ULONG Signature;
81 ULONG Checksum;
82 BOOLEAN Recognized;
83 CM_DISK_GEOMETRY_DEVICE_DATA DiskGeometry;
84 CM_INT13_DRIVE_PARAMETER Int13DiskData;
85 } BIOSDISKENTRY, *PBIOSDISKENTRY;
86
87
88 typedef struct _DISKENTRY
89 {
90 LIST_ENTRY ListEntry;
91
92 /* Disk geometry */
93
94 ULONGLONG Cylinders;
95 ULONG TracksPerCylinder;
96 ULONG SectorsPerTrack;
97 ULONG BytesPerSector;
98
99 ULARGE_INTEGER SectorCount;
100 ULONG SectorAlignment;
101 ULONG CylinderAlignment;
102
103 /* BIOS parameters */
104 BOOLEAN BiosFound;
105 ULONG BiosDiskNumber;
106 // ULONG Signature; // Obtained from LayoutBuffer->Signature
107 // ULONG Checksum;
108
109 /* SCSI parameters */
110 ULONG DiskNumber;
111 // SCSI_ADDRESS;
112 USHORT Port;
113 USHORT Bus;
114 USHORT Id;
115
116 /* Has the partition list been modified? */
117 BOOLEAN Dirty;
118
119 BOOLEAN NewDisk; /* If TRUE, the disk is uninitialized */
120 PARTITION_STYLE DiskStyle; /* MBR/GPT-partitioned disk, or uninitialized disk (RAW) */
121
122 UNICODE_STRING DriverName;
123
124 PDRIVE_LAYOUT_INFORMATION LayoutBuffer;
125 // TODO: When adding support for GPT disks:
126 // Use PDRIVE_LAYOUT_INFORMATION_EX which indicates whether
127 // the disk is MBR, GPT, or unknown (uninitialized).
128 // Depending on the style, either use the MBR or GPT partition info.
129
130 LIST_ENTRY PrimaryPartListHead; /* List of primary partitions */
131 LIST_ENTRY LogicalPartListHead; /* List of logical partitions (Valid only for MBR-partitioned disks) */
132
133 /* Pointer to the unique extended partition on this disk (Valid only for MBR-partitioned disks) */
134 PPARTENTRY ExtendedPartition;
135
136 } DISKENTRY, *PDISKENTRY;
137
138
139 typedef struct _PARTLIST
140 {
141 /*
142 * Disk & Partition iterators.
143 *
144 * NOTE that when CurrentPartition != NULL, then CurrentPartition->DiskEntry
145 * must be the same as CurrentDisk. We should however keep the two members
146 * separated as we can have a current (selected) disk without any current
147 * partition, if the former does not contain any.
148 */
149 PDISKENTRY CurrentDisk;
150 PPARTENTRY CurrentPartition;
151
152 /*
153 * The system partition where the boot manager resides.
154 * The corresponding system disk is obtained via:
155 * SystemPartition->DiskEntry.
156 */
157 PPARTENTRY SystemPartition;
158 /*
159 * The original system partition in case we are redefining it because
160 * we do not have write support on it.
161 * Please note that this is partly a HACK and MUST NEVER happen on
162 * architectures where real system partitions are mandatory (because then
163 * they are formatted in FAT FS and we support write operation on them).
164 * The corresponding original system disk is obtained via:
165 * OriginalSystemPartition->DiskEntry.
166 */
167 PPARTENTRY OriginalSystemPartition;
168
169 LIST_ENTRY DiskListHead;
170 LIST_ENTRY BiosDiskListHead;
171
172 } PARTLIST, *PPARTLIST;
173
174 #define PARTITION_TBL_SIZE 4
175
176 #define PARTITION_MAGIC 0xAA55
177
178 /* Defines system type for MBR showing that a GPT is following */
179 #define EFI_PMBR_OSTYPE_EFI 0xEE
180
181 #include <pshpack1.h>
182
183 typedef struct _PARTITION
184 {
185 unsigned char BootFlags; /* bootable? 0=no, 128=yes */
186 unsigned char StartingHead; /* beginning head number */
187 unsigned char StartingSector; /* beginning sector number */
188 unsigned char StartingCylinder; /* 10 bit nmbr, with high 2 bits put in begsect */
189 unsigned char PartitionType; /* Operating System type indicator code */
190 unsigned char EndingHead; /* ending head number */
191 unsigned char EndingSector; /* ending sector number */
192 unsigned char EndingCylinder; /* also a 10 bit nmbr, with same high 2 bit trick */
193 unsigned int StartingBlock; /* first sector relative to start of disk */
194 unsigned int SectorCount; /* number of sectors in partition */
195 } PARTITION, *PPARTITION;
196
197 typedef struct _PARTITION_SECTOR
198 {
199 UCHAR BootCode[440]; /* 0x000 */
200 ULONG Signature; /* 0x1B8 */
201 UCHAR Reserved[2]; /* 0x1BC */
202 PARTITION Partition[PARTITION_TBL_SIZE]; /* 0x1BE */
203 USHORT Magic; /* 0x1FE */
204 } PARTITION_SECTOR, *PPARTITION_SECTOR;
205
206 #include <poppack.h>
207
208 typedef struct
209 {
210 LIST_ENTRY ListEntry;
211 ULONG DiskNumber;
212 ULONG Identifier;
213 ULONG Signature;
214 } BIOS_DISK, *PBIOS_DISK;
215
216
217
218 ULONGLONG
219 AlignDown(
220 IN ULONGLONG Value,
221 IN ULONG Alignment);
222
223 ULONGLONG
224 AlignUp(
225 IN ULONGLONG Value,
226 IN ULONG Alignment);
227
228 ULONGLONG
229 RoundingDivide(
230 IN ULONGLONG Dividend,
231 IN ULONGLONG Divisor);
232
233
234
235 PPARTLIST
236 CreatePartitionList(VOID);
237
238 VOID
239 DestroyPartitionList(
240 IN PPARTLIST List);
241
242 PDISKENTRY
243 GetDiskByBiosNumber(
244 IN PPARTLIST List,
245 IN ULONG BiosDiskNumber);
246
247 PDISKENTRY
248 GetDiskByNumber(
249 IN PPARTLIST List,
250 IN ULONG DiskNumber);
251
252 PDISKENTRY
253 GetDiskBySCSI(
254 IN PPARTLIST List,
255 IN USHORT Port,
256 IN USHORT Bus,
257 IN USHORT Id);
258
259 PDISKENTRY
260 GetDiskBySignature(
261 IN PPARTLIST List,
262 IN ULONG Signature);
263
264 PPARTENTRY
265 GetPartition(
266 // IN PPARTLIST List,
267 IN PDISKENTRY DiskEntry,
268 IN ULONG PartitionNumber);
269
270 BOOLEAN
271 GetDiskOrPartition(
272 IN PPARTLIST List,
273 IN ULONG DiskNumber,
274 IN ULONG PartitionNumber OPTIONAL,
275 OUT PDISKENTRY* pDiskEntry,
276 OUT PPARTENTRY* pPartEntry OPTIONAL);
277
278 BOOLEAN
279 SelectPartition(
280 IN PPARTLIST List,
281 IN ULONG DiskNumber,
282 IN ULONG PartitionNumber);
283
284 PPARTENTRY
285 GetNextPartition(
286 IN PPARTLIST List);
287
288 PPARTENTRY
289 GetPrevPartition(
290 IN PPARTLIST List);
291
292 BOOLEAN
293 CreatePrimaryPartition(
294 IN PPARTLIST List,
295 IN PPARTENTRY SelectedEntry,
296 IN ULONGLONG SectorCount,
297 IN BOOLEAN AutoCreate);
298
299 BOOLEAN
300 CreateExtendedPartition(
301 IN PPARTLIST List,
302 IN PPARTENTRY SelectedEntry,
303 IN ULONGLONG SectorCount);
304
305 BOOLEAN
306 CreateLogicalPartition(
307 IN PPARTLIST List,
308 IN PPARTENTRY SelectedEntry,
309 IN ULONGLONG SectorCount,
310 IN BOOLEAN AutoCreate);
311
312 VOID
313 DeletePartition(
314 IN PPARTLIST List,
315 IN PPARTENTRY PartEntry);
316
317 VOID
318 DeleteCurrentPartition(
319 IN PPARTLIST List);
320
321 VOID
322 CheckActiveSystemPartition(
323 IN PPARTLIST List);
324
325 NTSTATUS
326 WritePartitions(
327 IN PDISKENTRY DiskEntry);
328
329 BOOLEAN
330 WritePartitionsToDisk(
331 IN PPARTLIST List);
332
333 BOOLEAN
334 SetMountedDeviceValue(
335 IN WCHAR Letter,
336 IN ULONG Signature,
337 IN LARGE_INTEGER StartingOffset);
338
339 BOOLEAN
340 SetMountedDeviceValues(
341 IN PPARTLIST List);
342
343 VOID
344 SetPartitionType(
345 IN PPARTENTRY PartEntry,
346 IN UCHAR PartitionType);
347
348 ERROR_NUMBER
349 PrimaryPartitionCreationChecks(
350 IN PPARTENTRY PartEntry);
351
352 ERROR_NUMBER
353 ExtendedPartitionCreationChecks(
354 IN PPARTENTRY PartEntry);
355
356 ERROR_NUMBER
357 LogicalPartitionCreationChecks(
358 IN PPARTENTRY PartEntry);
359
360 BOOLEAN
361 GetNextUnformattedPartition(
362 IN PPARTLIST List,
363 OUT PDISKENTRY *pDiskEntry OPTIONAL,
364 OUT PPARTENTRY *pPartEntry);
365
366 BOOLEAN
367 GetNextUncheckedPartition(
368 IN PPARTLIST List,
369 OUT PDISKENTRY *pDiskEntry OPTIONAL,
370 OUT PPARTENTRY *pPartEntry);
371
372 /* EOF */