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