xref: /reactos/base/setup/lib/utils/partlist.h (revision 1fe58c40)
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 typedef struct _DISKENTRY
76 {
77     LIST_ENTRY ListEntry;
78 
79     /* The list of disks/partitions this disk belongs to */
80     struct _PARTLIST *PartList;
81 
82     MEDIA_TYPE MediaType;   /* FixedMedia or RemovableMedia */
83 
84     /* Disk geometry */
85 
86     ULONGLONG Cylinders;
87     ULONG TracksPerCylinder;
88     ULONG SectorsPerTrack;
89     ULONG BytesPerSector;
90 
91     ULARGE_INTEGER SectorCount;
92     ULONG SectorAlignment;
93     ULONG CylinderAlignment;
94 
95     /* BIOS Firmware parameters */
96     BOOLEAN BiosFound;
97     ULONG HwAdapterNumber;
98     ULONG HwControllerNumber;
99     ULONG HwDiskNumber;         /* Disk number currently assigned on the system */
100     ULONG HwFixedDiskNumber;    /* Disk number on the system when *ALL* removable disks are not connected */
101 //    ULONG Signature;  // Obtained from LayoutBuffer->Signature
102 //    ULONG Checksum;
103 
104     /* SCSI parameters */
105     ULONG DiskNumber;
106 //  SCSI_ADDRESS;
107     USHORT Port;
108     USHORT Bus;
109     USHORT Id;
110 
111     /* Has the partition list been modified? */
112     BOOLEAN Dirty;
113 
114     BOOLEAN NewDisk; /* If TRUE, the disk is uninitialized */
115     PARTITION_STYLE DiskStyle;  /* MBR/GPT-partitioned disk, or uninitialized disk (RAW) */
116 
117     UNICODE_STRING DriverName;
118 
119     PDRIVE_LAYOUT_INFORMATION LayoutBuffer;
120     // TODO: When adding support for GPT disks:
121     // Use PDRIVE_LAYOUT_INFORMATION_EX which indicates whether
122     // the disk is MBR, GPT, or unknown (uninitialized).
123     // Depending on the style, either use the MBR or GPT partition info.
124 
125     LIST_ENTRY PrimaryPartListHead; /* List of primary partitions */
126     LIST_ENTRY LogicalPartListHead; /* List of logical partitions (Valid only for MBR-partitioned disks) */
127 
128     /* Pointer to the unique extended partition on this disk (Valid only for MBR-partitioned disks) */
129     PPARTENTRY ExtendedPartition;
130 
131 } DISKENTRY, *PDISKENTRY;
132 
133 typedef struct _BIOSDISKENTRY
134 {
135     LIST_ENTRY ListEntry;
136     ULONG AdapterNumber;
137     ULONG ControllerNumber;
138     ULONG DiskNumber;
139     ULONG Signature;
140     ULONG Checksum;
141     PDISKENTRY DiskEntry;   /* Corresponding recognized disk; is NULL if the disk is not recognized */ // RecognizedDiskEntry;
142     CM_DISK_GEOMETRY_DEVICE_DATA DiskGeometry;
143     CM_INT13_DRIVE_PARAMETER Int13DiskData;
144 } BIOSDISKENTRY, *PBIOSDISKENTRY;
145 
146 typedef struct _PARTLIST
147 {
148     /*
149      * The system partition where the boot manager resides.
150      * The corresponding system disk is obtained via:
151      *    SystemPartition->DiskEntry.
152      */
153     PPARTENTRY SystemPartition;
154     /*
155      * The original system partition in case we are redefining it because
156      * we do not have write support on it.
157      * Please note that this is partly a HACK and MUST NEVER happen on
158      * architectures where real system partitions are mandatory (because then
159      * they are formatted in FAT FS and we support write operation on them).
160      * The corresponding original system disk is obtained via:
161      *    OriginalSystemPartition->DiskEntry.
162      */
163     PPARTENTRY OriginalSystemPartition;
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 
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 HwDiskNumber);
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 PPARTENTRY
279 SelectPartition(
280     IN PPARTLIST List,
281     IN ULONG DiskNumber,
282     IN ULONG PartitionNumber);
283 
284 PPARTENTRY
285 GetNextPartition(
286     IN PPARTLIST List,
287     IN PPARTENTRY CurrentPart OPTIONAL);
288 
289 PPARTENTRY
290 GetPrevPartition(
291     IN PPARTLIST List,
292     IN PPARTENTRY CurrentPart OPTIONAL);
293 
294 BOOLEAN
295 CreatePrimaryPartition(
296     IN PPARTLIST List,
297     IN PPARTENTRY SelectedEntry,
298     IN ULONGLONG SectorCount,
299     IN BOOLEAN AutoCreate);
300 
301 BOOLEAN
302 CreateExtendedPartition(
303     IN PPARTLIST List,
304     IN PPARTENTRY SelectedEntry,
305     IN ULONGLONG SectorCount);
306 
307 BOOLEAN
308 CreateLogicalPartition(
309     IN PPARTLIST List,
310     IN PPARTENTRY SelectedEntry,
311     IN ULONGLONG SectorCount,
312     IN BOOLEAN AutoCreate);
313 
314 BOOLEAN
315 DeletePartition(
316     IN PPARTLIST List,
317     IN PPARTENTRY PartEntry,
318     OUT PPARTENTRY* FreeRegion OPTIONAL);
319 
320 VOID
321 CheckActiveSystemPartition(
322     IN PPARTLIST List,
323     IN BOOLEAN ForceSelect,
324     IN PDISKENTRY AlternateDisk OPTIONAL,
325     IN PPARTENTRY AlternatePart OPTIONAL);
326 
327 NTSTATUS
328 WritePartitions(
329     IN PDISKENTRY DiskEntry);
330 
331 BOOLEAN
332 WritePartitionsToDisk(
333     IN PPARTLIST List);
334 
335 BOOLEAN
336 SetMountedDeviceValue(
337     IN WCHAR Letter,
338     IN ULONG Signature,
339     IN LARGE_INTEGER StartingOffset);
340 
341 BOOLEAN
342 SetMountedDeviceValues(
343     IN PPARTLIST List);
344 
345 VOID
346 SetPartitionType(
347     IN PPARTENTRY PartEntry,
348     IN UCHAR PartitionType);
349 
350 ERROR_NUMBER
351 PrimaryPartitionCreationChecks(
352     IN PPARTENTRY PartEntry);
353 
354 ERROR_NUMBER
355 ExtendedPartitionCreationChecks(
356     IN PPARTENTRY PartEntry);
357 
358 ERROR_NUMBER
359 LogicalPartitionCreationChecks(
360     IN PPARTENTRY PartEntry);
361 
362 BOOLEAN
363 GetNextUnformattedPartition(
364     IN PPARTLIST List,
365     OUT PDISKENTRY *pDiskEntry OPTIONAL,
366     OUT PPARTENTRY *pPartEntry);
367 
368 BOOLEAN
369 GetNextUncheckedPartition(
370     IN PPARTLIST List,
371     OUT PDISKENTRY *pDiskEntry OPTIONAL,
372     OUT PPARTENTRY *pPartEntry);
373 
374 /* EOF */
375