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 OnDiskPartitionNumber; /* Enumerated partition number (primary partitions first, excluding the extended partition container, then the logical partitions) */ 50 ULONG PartitionNumber; /* Current partition number, only valid for the currently running NTOS instance */ 51 ULONG PartitionIndex; /* Index in the LayoutBuffer->PartitionEntry[] cached array of the corresponding DiskEntry */ 52 53 WCHAR DriveLetter; 54 WCHAR VolumeLabel[20]; 55 // CHAR FileSystemName[9]; // NOTE: Superseded by the FileSystem member 56 57 BOOLEAN LogicalPartition; 58 59 /* Partition is partitioned disk space */ 60 BOOLEAN IsPartitioned; 61 62 /* Partition is new, table does not exist on disk yet */ 63 BOOLEAN New; 64 65 /* Partition was created automatically */ 66 BOOLEAN AutoCreate; 67 68 /* Partition must be checked */ 69 BOOLEAN NeedsCheck; 70 71 FORMATSTATE FormatState; 72 struct _FILE_SYSTEM* FileSystem; 73 74 } PARTENTRY, *PPARTENTRY; 75 76 77 typedef struct _BIOSDISKENTRY 78 { 79 LIST_ENTRY ListEntry; 80 ULONG DiskNumber; 81 ULONG Signature; 82 ULONG Checksum; 83 BOOLEAN Recognized; 84 CM_DISK_GEOMETRY_DEVICE_DATA DiskGeometry; 85 CM_INT13_DRIVE_PARAMETER Int13DiskData; 86 } BIOSDISKENTRY, *PBIOSDISKENTRY; 87 88 89 typedef struct _DISKENTRY 90 { 91 LIST_ENTRY ListEntry; 92 93 /* Disk geometry */ 94 95 ULONGLONG Cylinders; 96 ULONG TracksPerCylinder; 97 ULONG SectorsPerTrack; 98 ULONG BytesPerSector; 99 100 ULARGE_INTEGER SectorCount; 101 ULONG SectorAlignment; 102 ULONG CylinderAlignment; 103 104 /* BIOS parameters */ 105 BOOLEAN BiosFound; 106 ULONG BiosDiskNumber; 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 140 typedef struct _PARTLIST 141 { 142 /* 143 * Disk & Partition iterators. 144 * 145 * NOTE that when CurrentPartition != NULL, then CurrentPartition->DiskEntry 146 * must be the same as CurrentDisk. We should however keep the two members 147 * separated as we can have a current (selected) disk without any current 148 * partition, if the former does not contain any. 149 */ 150 PDISKENTRY CurrentDisk; 151 PPARTENTRY CurrentPartition; 152 153 /* 154 * The system partition where the boot manager resides. 155 * The corresponding system disk is obtained via: 156 * SystemPartition->DiskEntry. 157 */ 158 PPARTENTRY SystemPartition; 159 /* 160 * The original system partition in case we are redefining it because 161 * we do not have write support on it. 162 * Please note that this is partly a HACK and MUST NEVER happen on 163 * architectures where real system partitions are mandatory (because then 164 * they are formatted in FAT FS and we support write operation on them). 165 * The corresponding original system disk is obtained via: 166 * OriginalSystemPartition->DiskEntry. 167 */ 168 PPARTENTRY OriginalSystemPartition; 169 170 LIST_ENTRY DiskListHead; 171 LIST_ENTRY BiosDiskListHead; 172 173 } PARTLIST, *PPARTLIST; 174 175 #define PARTITION_TBL_SIZE 4 176 177 #define PARTITION_MAGIC 0xAA55 178 179 /* Defines system type for MBR showing that a GPT is following */ 180 #define EFI_PMBR_OSTYPE_EFI 0xEE 181 182 #include <pshpack1.h> 183 184 typedef struct _PARTITION 185 { 186 unsigned char BootFlags; /* bootable? 0=no, 128=yes */ 187 unsigned char StartingHead; /* beginning head number */ 188 unsigned char StartingSector; /* beginning sector number */ 189 unsigned char StartingCylinder; /* 10 bit nmbr, with high 2 bits put in begsect */ 190 unsigned char PartitionType; /* Operating System type indicator code */ 191 unsigned char EndingHead; /* ending head number */ 192 unsigned char EndingSector; /* ending sector number */ 193 unsigned char EndingCylinder; /* also a 10 bit nmbr, with same high 2 bit trick */ 194 unsigned int StartingBlock; /* first sector relative to start of disk */ 195 unsigned int SectorCount; /* number of sectors in partition */ 196 } PARTITION, *PPARTITION; 197 198 typedef struct _PARTITION_SECTOR 199 { 200 UCHAR BootCode[440]; /* 0x000 */ 201 ULONG Signature; /* 0x1B8 */ 202 UCHAR Reserved[2]; /* 0x1BC */ 203 PARTITION Partition[PARTITION_TBL_SIZE]; /* 0x1BE */ 204 USHORT Magic; /* 0x1FE */ 205 } PARTITION_SECTOR, *PPARTITION_SECTOR; 206 207 #include <poppack.h> 208 209 typedef struct 210 { 211 LIST_ENTRY ListEntry; 212 ULONG DiskNumber; 213 ULONG Identifier; 214 ULONG Signature; 215 } BIOS_DISK, *PBIOS_DISK; 216 217 218 219 ULONGLONG 220 AlignDown( 221 IN ULONGLONG Value, 222 IN ULONG Alignment); 223 224 ULONGLONG 225 AlignUp( 226 IN ULONGLONG Value, 227 IN ULONG Alignment); 228 229 ULONGLONG 230 RoundingDivide( 231 IN ULONGLONG Dividend, 232 IN ULONGLONG Divisor); 233 234 235 236 PPARTLIST 237 CreatePartitionList(VOID); 238 239 VOID 240 DestroyPartitionList( 241 IN PPARTLIST List); 242 243 PDISKENTRY 244 GetDiskByBiosNumber( 245 IN PPARTLIST List, 246 IN ULONG BiosDiskNumber); 247 248 PDISKENTRY 249 GetDiskByNumber( 250 IN PPARTLIST List, 251 IN ULONG DiskNumber); 252 253 PDISKENTRY 254 GetDiskBySCSI( 255 IN PPARTLIST List, 256 IN USHORT Port, 257 IN USHORT Bus, 258 IN USHORT Id); 259 260 PDISKENTRY 261 GetDiskBySignature( 262 IN PPARTLIST List, 263 IN ULONG Signature); 264 265 PPARTENTRY 266 GetPartition( 267 // IN PPARTLIST List, 268 IN PDISKENTRY DiskEntry, 269 IN ULONG PartitionNumber); 270 271 BOOLEAN 272 GetDiskOrPartition( 273 IN PPARTLIST List, 274 IN ULONG DiskNumber, 275 IN ULONG PartitionNumber OPTIONAL, 276 OUT PDISKENTRY* pDiskEntry, 277 OUT PPARTENTRY* pPartEntry OPTIONAL); 278 279 BOOLEAN 280 SelectPartition( 281 IN PPARTLIST List, 282 IN ULONG DiskNumber, 283 IN ULONG PartitionNumber); 284 285 PPARTENTRY 286 GetNextPartition( 287 IN PPARTLIST List); 288 289 PPARTENTRY 290 GetPrevPartition( 291 IN PPARTLIST List); 292 293 BOOLEAN 294 CreatePrimaryPartition( 295 IN PPARTLIST List, 296 IN ULONGLONG SectorCount, 297 IN BOOLEAN AutoCreate); 298 299 BOOLEAN 300 CreateExtendedPartition( 301 IN PPARTLIST List, 302 IN ULONGLONG SectorCount); 303 304 BOOLEAN 305 CreateLogicalPartition( 306 IN PPARTLIST List, 307 IN ULONGLONG SectorCount, 308 IN BOOLEAN AutoCreate); 309 310 VOID 311 DeleteCurrentPartition( 312 IN PPARTLIST List); 313 314 VOID 315 CheckActiveSystemPartition( 316 IN PPARTLIST List); 317 318 BOOLEAN 319 WritePartitionsToDisk( 320 IN PPARTLIST List); 321 322 BOOLEAN 323 SetMountedDeviceValue( 324 IN WCHAR Letter, 325 IN ULONG Signature, 326 IN LARGE_INTEGER StartingOffset); 327 328 BOOLEAN 329 SetMountedDeviceValues( 330 IN PPARTLIST List); 331 332 VOID 333 SetPartitionType( 334 IN PPARTENTRY PartEntry, 335 IN UCHAR PartitionType); 336 337 ERROR_NUMBER 338 PrimaryPartitionCreationChecks( 339 IN PPARTLIST List); 340 341 ERROR_NUMBER 342 ExtendedPartitionCreationChecks( 343 IN PPARTLIST List); 344 345 ERROR_NUMBER 346 LogicalPartitionCreationChecks( 347 IN PPARTLIST List); 348 349 BOOLEAN 350 GetNextUnformattedPartition( 351 IN PPARTLIST List, 352 OUT PDISKENTRY *pDiskEntry OPTIONAL, 353 OUT PPARTENTRY *pPartEntry); 354 355 BOOLEAN 356 GetNextUncheckedPartition( 357 IN PPARTLIST List, 358 OUT PDISKENTRY *pDiskEntry OPTIONAL, 359 OUT PPARTENTRY *pPartEntry); 360 361 /* EOF */ 362