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