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