[SETUPLIB][USETUP] Additional partition validity checks.
[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-2018 Casper S. Hornstrup (chorns@users.sourceforge.net)
6 */
7
8 #pragma once
9
10 /* HELPERS FOR PARTITION TYPES **********************************************/
11
12 typedef struct _PARTITION_TYPE
13 {
14 UCHAR Type;
15 PCHAR Description;
16 } PARTITION_TYPE, *PPARTITION_TYPE;
17
18 #define NUM_PARTITION_TYPE_ENTRIES 143
19 extern PARTITION_TYPE PartitionTypes[NUM_PARTITION_TYPE_ENTRIES];
20
21
22 /* PARTITION UTILITY FUNCTIONS **********************************************/
23
24 typedef enum _FORMATSTATE
25 {
26 Unformatted,
27 UnformattedOrDamaged,
28 UnknownFormat,
29 Preformatted,
30 Formatted
31 } FORMATSTATE, *PFORMATSTATE;
32
33 struct _FILE_SYSTEM;
34
35 typedef struct _PARTENTRY
36 {
37 LIST_ENTRY ListEntry;
38
39 /* The disk this partition belongs to */
40 struct _DISKENTRY *DiskEntry;
41
42 /* Partition geometry */
43 ULARGE_INTEGER StartSector;
44 ULARGE_INTEGER SectorCount;
45
46 BOOLEAN BootIndicator;
47 UCHAR PartitionType;
48 ULONG HiddenSectors;
49 ULONG PartitionNumber; /* Enumerated partition number (primary partitions first -- excluding the extended partition container --, then the logical partitions) */
50 ULONG PartitionIndex; /* Index in the LayoutBuffer->PartitionEntry[] cached array of the corresponding DiskEntry */
51
52 WCHAR DriveLetter;
53 WCHAR VolumeLabel[20];
54 // CHAR FileSystemName[9]; // NOTE: Superseded by the FileSystem member
55
56 BOOLEAN LogicalPartition;
57
58 /* Partition is partitioned disk space */
59 BOOLEAN IsPartitioned;
60
61 /* Partition is new, table does not exist on disk yet */
62 BOOLEAN New;
63
64 /* Partition was created automatically */
65 BOOLEAN AutoCreate;
66
67 /* Partition must be checked */
68 BOOLEAN NeedsCheck;
69
70 FORMATSTATE FormatState;
71 struct _FILE_SYSTEM* FileSystem;
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;
120 BOOLEAN NoMbr; /* MBR is absent */ // See r40437
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 /* Pointer to the unique extended partition on this disk */
131 PPARTENTRY ExtendedPartition;
132
133 LIST_ENTRY PrimaryPartListHead;
134 LIST_ENTRY LogicalPartListHead;
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 #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
230 PPARTLIST
231 CreatePartitionList(VOID);
232
233 VOID
234 DestroyPartitionList(
235 IN PPARTLIST List);
236
237 PDISKENTRY
238 GetDiskByBiosNumber(
239 IN PPARTLIST List,
240 IN ULONG BiosDiskNumber);
241
242 PDISKENTRY
243 GetDiskByNumber(
244 IN PPARTLIST List,
245 IN ULONG DiskNumber);
246
247 PDISKENTRY
248 GetDiskBySCSI(
249 IN PPARTLIST List,
250 IN USHORT Port,
251 IN USHORT Bus,
252 IN USHORT Id);
253
254 PDISKENTRY
255 GetDiskBySignature(
256 IN PPARTLIST List,
257 IN ULONG Signature);
258
259 PPARTENTRY
260 GetPartition(
261 // IN PPARTLIST List,
262 IN PDISKENTRY DiskEntry,
263 IN ULONG PartitionNumber);
264
265 BOOLEAN
266 GetDiskOrPartition(
267 IN PPARTLIST List,
268 IN ULONG DiskNumber,
269 IN ULONG PartitionNumber OPTIONAL,
270 OUT PDISKENTRY* pDiskEntry,
271 OUT PPARTENTRY* pPartEntry OPTIONAL);
272
273 BOOLEAN
274 SelectPartition(
275 IN PPARTLIST List,
276 IN ULONG DiskNumber,
277 IN ULONG PartitionNumber);
278
279 PPARTENTRY
280 GetNextPartition(
281 IN PPARTLIST List);
282
283 PPARTENTRY
284 GetPrevPartition(
285 IN PPARTLIST List);
286
287 BOOLEAN
288 CreatePrimaryPartition(
289 IN PPARTLIST List,
290 IN ULONGLONG SectorCount,
291 IN BOOLEAN AutoCreate);
292
293 BOOLEAN
294 CreateExtendedPartition(
295 IN PPARTLIST List,
296 IN ULONGLONG SectorCount);
297
298 BOOLEAN
299 CreateLogicalPartition(
300 IN PPARTLIST List,
301 IN ULONGLONG SectorCount,
302 IN BOOLEAN AutoCreate);
303
304 VOID
305 DeleteCurrentPartition(
306 IN PPARTLIST List);
307
308 VOID
309 CheckActiveSystemPartition(
310 IN PPARTLIST List);
311
312 BOOLEAN
313 WritePartitionsToDisk(
314 IN PPARTLIST List);
315
316 BOOLEAN
317 SetMountedDeviceValue(
318 IN WCHAR Letter,
319 IN ULONG Signature,
320 IN LARGE_INTEGER StartingOffset);
321
322 BOOLEAN
323 SetMountedDeviceValues(
324 IN PPARTLIST List);
325
326 VOID
327 SetPartitionType(
328 IN PPARTENTRY PartEntry,
329 IN UCHAR PartitionType);
330
331 ERROR_NUMBER
332 PrimaryPartitionCreationChecks(
333 IN PPARTLIST List);
334
335 ERROR_NUMBER
336 ExtendedPartitionCreationChecks(
337 IN PPARTLIST List);
338
339 ERROR_NUMBER
340 LogicalPartitionCreationChecks(
341 IN PPARTLIST List);
342
343 BOOLEAN
344 GetNextUnformattedPartition(
345 IN PPARTLIST List,
346 OUT PDISKENTRY *pDiskEntry OPTIONAL,
347 OUT PPARTENTRY *pPartEntry);
348
349 BOOLEAN
350 GetNextUncheckedPartition(
351 IN PPARTLIST List,
352 OUT PDISKENTRY *pDiskEntry OPTIONAL,
353 OUT PPARTENTRY *pPartEntry);
354
355 /* EOF */