Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7738 lines
237 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. sppartit.c
  5. Abstract:
  6. Partitioning module in text setup.
  7. Author:
  8. Ted Miller (tedm) 7-September-1993
  9. Revision History:
  10. --*/
  11. #include "spprecmp.h"
  12. #pragma hdrstop
  13. #include <bootmbr.h>
  14. //
  15. // For NEC98 boot memu code.
  16. //
  17. #include <x86mboot.h> //NEC98
  18. extern BOOLEAN DriveAssignFromA; //NEC98
  19. extern BOOLEAN ConsoleRunning;
  20. extern BOOLEAN ForceConsole;
  21. extern BOOLEAN ValidArcSystemPartition;
  22. extern PSETUP_COMMUNICATION CommunicationParams;
  23. PPARTITIONED_DISK PartitionedDisks;
  24. //
  25. // Disk region containing the local source directory
  26. // in the winnt.exe setup case.
  27. //
  28. // If WinntSetup is TRUE and WinntFromCd is FALSE, then this
  29. // should be non-null. If it is not non-null, then we couldn't locate
  30. // the local source.
  31. //
  32. //
  33. PDISK_REGION LocalSourceRegion;
  34. #if defined(REMOTE_BOOT)
  35. //
  36. // For remote boot, we create a fake disk region for the net(0) device.
  37. //
  38. PDISK_REGION RemoteBootTargetRegion = NULL;
  39. #endif // defined(REMOTE_BOOT)
  40. //
  41. // RemoteBootSetup is true when Source and target paths are through the redirector
  42. // with possibly no system partition.
  43. //
  44. // RemoteInstallSetup is true when we are doing a remote install.
  45. //
  46. // RemoteSysPrepSetup is true when we are doing a remote install of a sys prep image.
  47. //
  48. // RemoteSysPrepVolumeIsNtfs is true when the sysprep image we're copying down
  49. // represents an ntfs volume.
  50. //
  51. BOOLEAN RemoteBootSetup = FALSE;
  52. BOOLEAN RemoteInstallSetup = FALSE;
  53. BOOLEAN RemoteSysPrepSetup = FALSE;
  54. BOOLEAN RemoteSysPrepVolumeIsNtfs = FALSE;
  55. VOID
  56. SpPtReadPartitionTables(
  57. IN PPARTITIONED_DISK pDisk
  58. );
  59. VOID
  60. SpPtInitializePartitionStructures(
  61. IN ULONG DiskNumber
  62. );
  63. VOID
  64. SpPtDeterminePartitionTypes(
  65. IN ULONG DiskNumber
  66. );
  67. VOID
  68. SpPtDetermineVolumeFreeSpace(
  69. IN ULONG DiskNumber
  70. );
  71. VOID
  72. SpPtLocateSystemPartitions(
  73. VOID
  74. );
  75. VOID
  76. SpPtDeleteDriveLetters(
  77. VOID
  78. );
  79. ValidationValue
  80. SpPtnGetSizeCB(
  81. IN ULONG Key
  82. );
  83. //begin NEC98
  84. NTSTATUS
  85. SpInitializeHardDisk_Nec98(
  86. PDISK_REGION
  87. );
  88. VOID
  89. SpReassignOnDiskOrdinals(
  90. IN PPARTITIONED_DISK pDisk
  91. );
  92. VOID
  93. ConvertPartitionTable(
  94. IN PPARTITIONED_DISK pDisk,
  95. IN PUCHAR Buffer,
  96. IN ULONG bps
  97. );
  98. //end NEC98
  99. NTSTATUS
  100. SpMasterBootCode(
  101. IN ULONG DiskNumber,
  102. IN HANDLE Partition0Handle,
  103. OUT PULONG NewNTFTSignature
  104. );
  105. VOID
  106. SpPtAssignDriveLetters(
  107. VOID
  108. );
  109. //begin NEC98
  110. VOID
  111. SpPtRemapDriveLetters(
  112. IN BOOLEAN DriveAssign_AT
  113. );
  114. VOID
  115. SpPtUnAssignDriveLetters(
  116. VOID
  117. );
  118. WCHAR
  119. SpDeleteDriveLetter(
  120. IN PWSTR DeviceName
  121. );
  122. VOID
  123. SpTranslatePteInfo(
  124. IN PON_DISK_PTE pPte,
  125. IN PREAL_DISK_PTE pRealPte,
  126. IN BOOLEAN Write // into real PTE
  127. );
  128. VOID
  129. SpTranslateMbrInfo(
  130. IN PON_DISK_MBR pMbr,
  131. IN PREAL_DISK_MBR pRealMbr,
  132. IN ULONG bps,
  133. IN BOOLEAN Write // into real MBR
  134. );
  135. VOID
  136. SpDetermineFormatTypeNec98(
  137. IN PPARTITIONED_DISK pDisk,
  138. IN PREAL_DISK_MBR_NEC98 pRealMbrNec98
  139. );
  140. //end NEC98
  141. PDISK_PARTITION
  142. SpGetPartitionDescriptionFromRegistry(
  143. IN PVOID Buffer,
  144. IN ULONG DiskSignature,
  145. IN PLARGE_INTEGER StartingOffset,
  146. IN PLARGE_INTEGER Length
  147. );
  148. VOID
  149. SpPtFindLocalSourceRegionOnDynamicVolumes(
  150. VOID
  151. );
  152. NTSTATUS
  153. SpPtCheckDynamicVolumeForOSInstallation(
  154. IN PDISK_REGION Region
  155. );
  156. #ifndef NEW_PARTITION_ENGINE
  157. NTSTATUS
  158. SpPtInitialize(
  159. VOID
  160. )
  161. {
  162. ULONG disk;
  163. PHARD_DISK harddisk;
  164. PPARTITIONED_DISK partdisk;
  165. ULONG Disk0Ordinal = 0;
  166. ASSERT(HardDisksDetermined);
  167. //
  168. // If there are no hard disks, bail now.
  169. //
  170. if(!HardDiskCount) {
  171. #if defined(REMOTE_BOOT)
  172. //
  173. // If this is a diskless remote boot setup, it's OK for there to be
  174. // no hard disks. Otherwise, this is a fatal error.
  175. //
  176. if (!RemoteBootSetup || RemoteInstallSetup)
  177. #endif // defined(REMOTE_BOOT)
  178. {
  179. SpDisplayScreen(SP_SCRN_NO_HARD_DRIVES,3,HEADER_HEIGHT+1);
  180. SpDisplayStatusOptions(DEFAULT_STATUS_ATTRIBUTE,SP_STAT_F3_EQUALS_EXIT,0);
  181. SpInputDrain();
  182. while(SpInputGetKeypress() != KEY_F3) ;
  183. SpDone(0,FALSE,TRUE);
  184. }
  185. return STATUS_SUCCESS;
  186. }
  187. CLEAR_CLIENT_SCREEN();
  188. #ifdef _X86_
  189. Disk0Ordinal = SpDetermineDisk0();
  190. //
  191. // If the user booted off of a high-density floppy (e.g. an ls-120), then
  192. // it's possible that we've locked the device in its bay. For this
  193. // reason, we're going to tell the drive to unlock floppy0.
  194. //
  195. {
  196. NTSTATUS Status;
  197. IO_STATUS_BLOCK IoStatusBlock;
  198. OBJECT_ATTRIBUTES ObjectAttributes;
  199. UNICODE_STRING UnicodeString;
  200. HANDLE Handle;
  201. WCHAR OpenPath[64];
  202. PREVENT_MEDIA_REMOVAL PMRemoval;
  203. wcscpy(OpenPath,L"\\device\\floppy0");
  204. INIT_OBJA(&ObjectAttributes,&UnicodeString,OpenPath);
  205. //
  206. // Open him.
  207. //
  208. Status = ZwCreateFile(
  209. &Handle,
  210. FILE_GENERIC_WRITE,
  211. &ObjectAttributes,
  212. &IoStatusBlock,
  213. NULL, // allocation size
  214. FILE_ATTRIBUTE_NORMAL,
  215. FILE_SHARE_VALID_FLAGS, // full sharing
  216. FILE_OPEN,
  217. FILE_SYNCHRONOUS_IO_NONALERT,
  218. NULL, // no EAs
  219. 0
  220. );
  221. if( NT_SUCCESS(Status) ) {
  222. //
  223. // Tell him to let go.
  224. //
  225. PMRemoval.PreventMediaRemoval = FALSE;
  226. Status = ZwDeviceIoControlFile(
  227. Handle,
  228. NULL,
  229. NULL,
  230. NULL,
  231. &IoStatusBlock,
  232. IOCTL_STORAGE_MEDIA_REMOVAL,
  233. &PMRemoval,
  234. sizeof(PMRemoval),
  235. NULL,
  236. 0
  237. );
  238. ZwClose(Handle);
  239. if( !NT_SUCCESS(Status) ) {
  240. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "Setup: SpPtInitialize - Failed to tell the floppy to release its media.\n"));
  241. }
  242. } else {
  243. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "Setup: SpPtInitialize - Failed to open the floppy.\n"));
  244. }
  245. }
  246. #endif
  247. //
  248. // Allocate an array for the partitioned disk descriptors.
  249. //
  250. PartitionedDisks = SpMemAlloc(HardDiskCount * sizeof(PARTITIONED_DISK));
  251. if(!PartitionedDisks) {
  252. return(STATUS_NO_MEMORY);
  253. }
  254. RtlZeroMemory(PartitionedDisks,HardDiskCount * sizeof(PARTITIONED_DISK));
  255. //
  256. // For each hard disk attached to the system, read its partition table.
  257. //
  258. for(disk=0; disk<HardDiskCount; disk++) {
  259. #ifdef GPT_PARTITION_ENGINE
  260. if (SPPT_IS_GPT_DISK(disk)) {
  261. SpPtnInitializeDiskDrive(disk);
  262. continue;
  263. }
  264. #endif
  265. harddisk = &HardDisks[disk];
  266. SpDisplayStatusText(
  267. SP_STAT_EXAMINING_DISK_N,
  268. DEFAULT_STATUS_ATTRIBUTE,
  269. harddisk->Description
  270. );
  271. partdisk = &PartitionedDisks[disk];
  272. partdisk->HardDisk = harddisk;
  273. //
  274. // Read the partition tables.
  275. //
  276. SpPtReadPartitionTables(partdisk);
  277. //
  278. // Initialize structures that are based on the partition tables.
  279. //
  280. SpPtInitializePartitionStructures(disk);
  281. //
  282. // Determine the type name for each partition on this disk.
  283. //
  284. SpPtDeterminePartitionTypes(disk);
  285. }
  286. //
  287. // Assign drive letters to the various partitions
  288. //
  289. SpPtAssignDriveLetters();
  290. //
  291. // DoubleSpace initialization.
  292. //
  293. //
  294. // Load dblspace.ini file
  295. //
  296. if( SpLoadDblspaceIni() ) {
  297. SpDisplayStatusText(
  298. SP_STAT_EXAMINING_DISK_N,
  299. DEFAULT_STATUS_ATTRIBUTE,
  300. HardDisks[Disk0Ordinal].Description
  301. );
  302. //
  303. // Build lists of compressed drives and add them to the DISK_REGION
  304. // structures
  305. //
  306. SpInitializeCompressedDrives();
  307. }
  308. for(disk=0; disk<HardDiskCount; disk++) {
  309. SpDisplayStatusText(
  310. SP_STAT_EXAMINING_DISK_N,
  311. DEFAULT_STATUS_ATTRIBUTE,
  312. HardDisks[disk].Description
  313. );
  314. //
  315. // Determine the amount of free space on recognized volumes.
  316. //
  317. SpPtDetermineVolumeFreeSpace(disk);
  318. }
  319. if(WinntSetup && !WinntFromCd && !LocalSourceRegion) {
  320. //
  321. // If we got that far and we still don't know where the local source files are,
  322. // then serch for them in the dynamic volumes that are not listed on the MBR or EBR.
  323. //
  324. SpPtFindLocalSourceRegionOnDynamicVolumes();
  325. }
  326. #ifdef _X86_
  327. //
  328. // If the mbr on disk 0 was not valid, inform the user that
  329. // continuing will mean the loss of whatever was on the disk.
  330. //
  331. // We won't actually write it out here. We know that in order to
  332. // continue, the user will HAVE to create a C: partition on this drive
  333. // so we'll end up writing the master boot code when that change is comitted.
  334. //
  335. // Bootable partition on NEC98 is not only C: so don't check it.
  336. //
  337. // If doing a remote install or remote sysprep setup, don't check it.
  338. //
  339. if((!IsNEC_98) && //NEC98
  340. (!ForceConsole) &&
  341. (!(RemoteInstallSetup || RemoteSysPrepSetup)) &&
  342. (!PartitionedDisks[Disk0Ordinal].MbrWasValid)) {
  343. ULONG ValidKeys[2] = { KEY_F3, 0 };
  344. ULONG Mnemonics[2] = { MnemonicContinueSetup,0 };
  345. while(1) {
  346. SpDisplayScreen(SP_SCRN_INVALID_MBR_0,3,HEADER_HEIGHT+1);
  347. SpDisplayStatusOptions(
  348. DEFAULT_STATUS_ATTRIBUTE,
  349. SP_STAT_C_EQUALS_CONTINUE_SETUP,
  350. SP_STAT_F3_EQUALS_EXIT,
  351. 0
  352. );
  353. switch(SpWaitValidKey(ValidKeys,NULL,Mnemonics)) {
  354. case KEY_F3:
  355. SpConfirmExit();
  356. break;
  357. default:
  358. //
  359. // must be c=continue
  360. //
  361. goto x1;
  362. }
  363. }
  364. }
  365. x1:
  366. #endif
  367. //
  368. // Figure out which partitions are system partitions.
  369. //
  370. SpPtLocateSystemPartitions();
  371. return(STATUS_SUCCESS);
  372. }
  373. VOID
  374. SpPtDeterminePartitionTypes(
  375. IN ULONG DiskNumber
  376. )
  377. /*++
  378. Routine Description:
  379. Determine the partition types of each partition currently on a disk.
  380. The partition type is determined by the system id byte in the partition
  381. table entry. If the partition type is one we recognize as a Windows NT
  382. compatible filesystem (types 1,4,6,7) then we dig a little deeper and
  383. actually determine the filesystem on the volume and use the result as
  384. the type name.
  385. Unused spaces are not given type names.
  386. Arguments:
  387. DiskNumber - supplies the disk number of the disk whose partitions
  388. we want to inspect for determining their types.
  389. Return Value:
  390. None.
  391. --*/
  392. {
  393. PPARTITIONED_DISK pDisk;
  394. PDISK_REGION pRegion;
  395. ULONG NameId;
  396. UCHAR SysId;
  397. FilesystemType FsType;
  398. unsigned pass;
  399. ULONG ValidKeys[3] = { KEY_F3,ASCI_CR,0 };
  400. pDisk = &PartitionedDisks[DiskNumber];
  401. for(pass=0; pass<2; pass++) {
  402. pRegion = pass ? pDisk->ExtendedDiskRegions : pDisk->PrimaryDiskRegions;
  403. for( ; pRegion; pRegion=pRegion->Next) {
  404. pRegion->TypeName[0] = 0;
  405. pRegion->Filesystem = FilesystemUnknown;
  406. //
  407. // If this is a free space, skip it.
  408. //
  409. if(!pRegion->PartitionedSpace) {
  410. continue;
  411. }
  412. //
  413. // Fetch the system id.
  414. //
  415. // SysId = pRegion->MbrInfo->OnDiskMbr.PartitionTable[pRegion->TablePosition].SystemId;
  416. SysId = SpPtGetPartitionType(pRegion);
  417. //
  418. // If this is the extended partition, skip it.
  419. //
  420. if(IsContainerPartition(SysId)) {
  421. continue;
  422. }
  423. //
  424. // Initialize the FT related information
  425. //
  426. if( IsRecognizedPartition(SysId) &&
  427. (((SysId & VALID_NTFT) == VALID_NTFT) ||
  428. ((SysId & PARTITION_NTFT) == PARTITION_NTFT))
  429. ) {
  430. pRegion->FtPartition = TRUE;
  431. }
  432. //
  433. // Initialize the dynamic volume relatated information
  434. //
  435. if( (SysId == PARTITION_LDM)
  436. ) {
  437. pRegion->DynamicVolume = TRUE;
  438. //
  439. // Find out if the dynamic volume is suitable for OS installation
  440. //
  441. SpPtCheckDynamicVolumeForOSInstallation(pRegion);
  442. }
  443. //
  444. // If this is a 'recognized' partition type, then determine
  445. // the filesystem on it. Otherwise use a precanned name.
  446. // Note that we also determine the file system type if this is an
  447. // FT partition of type 'mirror', that is not the mirror shadow.
  448. // We don't care about the shadow since we cannot determine
  449. // its file system anyway (we can't access sector 0 of the shadow).
  450. //
  451. if((PartitionNameIds[SysId] == (UCHAR)(-1)) ||
  452. ( pRegion->FtPartition ) ||
  453. ( pRegion->DynamicVolume )
  454. ) {
  455. FsType = SpIdentifyFileSystem(
  456. HardDisks[DiskNumber].DevicePath,
  457. HardDisks[DiskNumber].Geometry.BytesPerSector,
  458. SpPtGetOrdinal(pRegion,PartitionOrdinalOnDisk)
  459. );
  460. NameId = SP_TEXT_FS_NAME_BASE + FsType;
  461. pRegion->Filesystem = FsType;
  462. } else {
  463. NameId = SP_TEXT_PARTITION_NAME_BASE + (ULONG)PartitionNameIds[SysId];
  464. }
  465. //
  466. // Get the final type name from the resources.
  467. //
  468. SpFormatMessage(
  469. pRegion->TypeName,
  470. sizeof(pRegion->TypeName),
  471. NameId
  472. );
  473. }
  474. }
  475. }
  476. #endif // ! NEW_PARTITION_ENGINE
  477. VOID
  478. SpPtDetermineRegionSpace(
  479. IN PDISK_REGION pRegion
  480. )
  481. {
  482. HANDLE Handle;
  483. OBJECT_ATTRIBUTES Obja;
  484. UNICODE_STRING UnicodeString;
  485. IO_STATUS_BLOCK IoStatusBlock;
  486. FILE_FS_SIZE_INFORMATION SizeInfo;
  487. ULONG r;
  488. NTSTATUS Status;
  489. WCHAR Buffer[512];
  490. struct LABEL_BUFFER {
  491. FILE_FS_VOLUME_INFORMATION VolumeInfo;
  492. WCHAR Label[256];
  493. } LabelBuffer;
  494. PFILE_FS_VOLUME_INFORMATION LabelInfo;
  495. #ifdef _X86_
  496. static BOOLEAN LookForUndelete = TRUE;
  497. PWSTR UndeleteFiles[1] = { L"SENTRY" };
  498. #endif
  499. PWSTR LocalSourceFiles[1] = { LocalSourceDirectory };
  500. ULONG ExtraSpace;
  501. //
  502. // Assume unknown.
  503. //
  504. pRegion->FreeSpaceKB = SPPT_REGION_FREESPACE_KB(pRegion);
  505. pRegion->AdjustedFreeSpaceKB = pRegion->FreeSpaceKB;
  506. pRegion->BytesPerCluster = (ULONG)(-1);
  507. //
  508. // If region is free space of an unknown type, skip it.
  509. //
  510. if(pRegion->Filesystem >= FilesystemFirstKnown) {
  511. //
  512. // Form the name of the root directory.
  513. //
  514. SpNtNameFromRegion(pRegion,Buffer,sizeof(Buffer),PartitionOrdinalCurrent);
  515. SpConcatenatePaths(Buffer,L"");
  516. //
  517. // Delete \pagefile.sys if it's there. This makes disk free space
  518. // calculations a little easier.
  519. //
  520. SpDeleteFile(Buffer,L"pagefile.sys",NULL);
  521. #ifdef _X86_
  522. //
  523. // Check to see if Undelete (dos 6) delete sentry or delete tracking
  524. // methods are in use. If so, give a warning because the free space
  525. // value we will display for this drive will be off.
  526. //
  527. if(LookForUndelete
  528. && (pRegion->Filesystem == FilesystemFat)
  529. && SpNFilesExist(Buffer,UndeleteFiles,ELEMENT_COUNT(UndeleteFiles),TRUE)) {
  530. SpDisplayScreen(SP_SCRN_FOUND_UNDELETE,3,HEADER_HEIGHT+1);
  531. SpDisplayStatusText(SP_STAT_ENTER_EQUALS_CONTINUE,DEFAULT_STATUS_ATTRIBUTE);
  532. SpInputDrain();
  533. while(SpInputGetKeypress() != ASCI_CR) ;
  534. LookForUndelete = FALSE;
  535. }
  536. #endif
  537. //
  538. // If this is a winnt setup, then look for the local source
  539. // on this drive if we haven't found it already.
  540. //
  541. if(WinntSetup && !WinntFromCd && !LocalSourceRegion
  542. && SpNFilesExist(Buffer,LocalSourceFiles,ELEMENT_COUNT(LocalSourceFiles),TRUE)) {
  543. PWSTR SifName;
  544. PVOID SifHandle;
  545. ULONG ErrorLine;
  546. NTSTATUS Status;
  547. PWSTR p;
  548. LocalSourceRegion = pRegion;
  549. pRegion->IsLocalSource = TRUE;
  550. ExtraSpace = 0;
  551. //
  552. // Open the small ini file that text setup put there to tell us
  553. // how much space is taken up by the local source.
  554. //
  555. wcscpy(TemporaryBuffer,Buffer);
  556. SpConcatenatePaths(TemporaryBuffer,LocalSourceDirectory);
  557. SpConcatenatePaths(TemporaryBuffer,L"size.sif");
  558. SifName = SpDupStringW(TemporaryBuffer);
  559. Status = SpLoadSetupTextFile(SifName,NULL,0,&SifHandle,&ErrorLine,TRUE,FALSE);
  560. if(NT_SUCCESS(Status)) {
  561. p = SpGetSectionKeyIndex(SifHandle,L"Data",L"Size",0);
  562. if(p) {
  563. ExtraSpace = (ULONG)SpStringToLong(p,NULL,10);
  564. }
  565. SpFreeTextFile(SifHandle);
  566. }
  567. SpMemFree(SifName);
  568. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: %ws is the local source (occupying %lx bytes)\n",Buffer,ExtraSpace));
  569. }
  570. //
  571. // Open the root directory on the partition's filesystem.
  572. //
  573. INIT_OBJA(&Obja,&UnicodeString,Buffer);
  574. Status = ZwCreateFile(
  575. &Handle,
  576. FILE_GENERIC_READ,
  577. &Obja,
  578. &IoStatusBlock,
  579. NULL,
  580. FILE_ATTRIBUTE_NORMAL,
  581. FILE_SHARE_READ | FILE_SHARE_WRITE,
  582. FILE_OPEN,
  583. FILE_SYNCHRONOUS_IO_NONALERT,
  584. NULL,
  585. 0
  586. );
  587. if(!NT_SUCCESS(Status)) {
  588. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: Unable to open %ws (%lx)\n",Buffer,Status));
  589. //pRegion->Filesystem = FilesystemUnknown;
  590. return;
  591. }
  592. //
  593. // Fetch volume size info.
  594. //
  595. Status = ZwQueryVolumeInformationFile(
  596. Handle,
  597. &IoStatusBlock,
  598. &SizeInfo,
  599. sizeof(SizeInfo),
  600. FileFsSizeInformation
  601. );
  602. if(NT_SUCCESS(Status)) {
  603. LARGE_INTEGER FreeBytes;
  604. LARGE_INTEGER AdjustedFreeBytes;
  605. //
  606. // Calculate the amount of free space on the drive.
  607. // Use the Rtl multiply routine because there is a compiler
  608. // problem/chip errata on MIPS with 64-bit arithmetic
  609. // (tedm 2/28/96).
  610. //
  611. FreeBytes = RtlExtendedIntegerMultiply(
  612. SizeInfo.AvailableAllocationUnits,
  613. SizeInfo.SectorsPerAllocationUnit * SizeInfo.BytesPerSector
  614. );
  615. AdjustedFreeBytes = FreeBytes;
  616. if(pRegion->IsLocalSource) {
  617. //
  618. // Only about 1/4 of the total space is moved during textmode.
  619. // Remember too that gui-mode copies the files, so only 25%
  620. // of this space is reusable during setup...
  621. //
  622. AdjustedFreeBytes.QuadPart += (ExtraSpace >> 2);
  623. }
  624. //
  625. // convert this to a number of KB.
  626. //
  627. pRegion->FreeSpaceKB = RtlExtendedLargeIntegerDivide(FreeBytes,1024,&r).LowPart;
  628. if(r >= 512) {
  629. pRegion->FreeSpaceKB++;
  630. }
  631. pRegion->AdjustedFreeSpaceKB = RtlExtendedLargeIntegerDivide(AdjustedFreeBytes,1024,&r).LowPart;
  632. if(r >= 512) {
  633. pRegion->AdjustedFreeSpaceKB++;
  634. }
  635. pRegion->BytesPerCluster = SizeInfo.SectorsPerAllocationUnit * SizeInfo.BytesPerSector;
  636. if( pRegion->Filesystem == FilesystemDoubleSpace ) {
  637. //
  638. // If this the regison is a double space drive, then initialize
  639. // sector count correctly, so that the drive size can be calculated
  640. // correctly later on.
  641. //
  642. pRegion->SectorCount = (ULONG)( SizeInfo.TotalAllocationUnits.QuadPart
  643. * SizeInfo.SectorsPerAllocationUnit
  644. );
  645. }
  646. } else {
  647. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: ZwQueryVolumeInformationFile for freespace failed (%lx)\n",Status));
  648. }
  649. //
  650. // Fetch volume label info.
  651. //
  652. Status = ZwQueryVolumeInformationFile(
  653. Handle,
  654. &IoStatusBlock,
  655. &LabelBuffer,
  656. sizeof(LabelBuffer),
  657. FileFsVolumeInformation
  658. );
  659. if(NT_SUCCESS(Status)) {
  660. ULONG SaveCharCount;
  661. LabelInfo = &LabelBuffer.VolumeInfo;
  662. //
  663. // We'll only save away the first <n> characters of
  664. // the volume label.
  665. //
  666. SaveCharCount = min(
  667. LabelInfo->VolumeLabelLength + sizeof(WCHAR),
  668. sizeof(pRegion->VolumeLabel)
  669. )
  670. / sizeof(WCHAR);
  671. if(SaveCharCount) {
  672. SaveCharCount--; // allow for terminating NUL.
  673. }
  674. wcsncpy(pRegion->VolumeLabel,LabelInfo->VolumeLabel,SaveCharCount);
  675. pRegion->VolumeLabel[SaveCharCount] = 0;
  676. } else {
  677. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: ZwQueryVolumeInformationFile for label failed (%lx)\n",Status));
  678. }
  679. ZwClose(Handle);
  680. }
  681. }
  682. VOID
  683. SpPtDetermineVolumeFreeSpace(
  684. IN ULONG DiskNumber
  685. )
  686. {
  687. PPARTITIONED_DISK pDisk;
  688. PDISK_REGION pRegion;
  689. unsigned pass;
  690. #ifdef FULL_DOUBLE_SPACE_SUPPORT
  691. PDISK_REGION CompressedDrive;
  692. #endif // FULL_DOUBLE_SPACE_SUPPORT
  693. pDisk = &PartitionedDisks[DiskNumber];
  694. for(pass=0; pass<2; pass++) {
  695. pRegion = pass ? pDisk->ExtendedDiskRegions : pDisk->PrimaryDiskRegions;
  696. for( ; pRegion; pRegion=pRegion->Next) {
  697. SpPtDetermineRegionSpace( pRegion );
  698. #ifdef FULL_DOUBLE_SPACE_SUPPORT
  699. if( ( pRegion->Filesystem == FilesystemFat ) &&
  700. ( pRegion->NextCompressed != NULL ) ) {
  701. //
  702. // If the region is a FAT partition that contains compressed
  703. // volumes, then determine the available space on each
  704. // compressed volume
  705. //
  706. for( CompressedDrive = pRegion->NextCompressed;
  707. CompressedDrive;
  708. CompressedDrive = CompressedDrive->NextCompressed ) {
  709. SpPtDetermineRegionSpace( CompressedDrive );
  710. }
  711. }
  712. #endif // FULL_DOUBLE_SPACE_SUPPORT
  713. }
  714. }
  715. }
  716. #ifdef OLD_PARTITION_ENGINE
  717. VOID
  718. SpPtLocateSystemPartitions(
  719. VOID
  720. )
  721. {
  722. if(!SpIsArc()) {
  723. //
  724. // NEC98 must not write boot.ini on C:
  725. //
  726. if (!IsNEC_98) { //NEC98
  727. PDISK_REGION pRegion;
  728. ULONG Disk0Ordinal = SpDetermineDisk0();
  729. //
  730. // Note: On X86 we currently don't allow system partitions to reside
  731. // on GPT disks
  732. //
  733. if (SPPT_IS_MBR_DISK(Disk0Ordinal)) {
  734. //
  735. // On x86 machines, we will mark any primary partitions on drive 0
  736. // as system partition, since such a partition is potentially bootable.
  737. //
  738. for(pRegion=PartitionedDisks[Disk0Ordinal].PrimaryDiskRegions;
  739. pRegion;
  740. pRegion=pRegion->Next) {
  741. //
  742. // Skip if free space or extended partition.
  743. //
  744. if(pRegion->PartitionedSpace &&
  745. !IsContainerPartition(SpPtGetPartitionType(pRegion)) &&
  746. (pRegion->ExtendedType == 0)) {
  747. //
  748. // It's a primary partition -- declare it a system partition.
  749. //
  750. pRegion->IsSystemPartition = TRUE;
  751. }
  752. }
  753. }
  754. }
  755. } else {
  756. PDISK_REGION pRegion;
  757. PPARTITIONED_DISK pDisk;
  758. unsigned pass;
  759. ULONG disk;
  760. PSP_BOOT_ENTRY BootEntry;
  761. //
  762. // On ARC machines, system partitions are specifically enumerated
  763. // in the NVRAM boot environment.
  764. //
  765. for(disk=0; disk<HardDiskCount; disk++) {
  766. if (SPPT_IS_GPT_DISK(disk)) {
  767. #ifndef OLD_PARTITION_ENGINE
  768. SpPtnLocateDiskSystemPartitions(disk);
  769. #endif
  770. } else {
  771. pDisk = &PartitionedDisks[disk];
  772. for(pass=0; pass<2; pass++) {
  773. pRegion = pass ?
  774. pDisk->ExtendedDiskRegions : pDisk->PrimaryDiskRegions;
  775. for( ; pRegion; pRegion=pRegion->Next) {
  776. UCHAR SystemId = SpPtGetPartitionType(pRegion);
  777. //
  778. // Skip if not a partition or extended partition.
  779. //
  780. if(pRegion->PartitionedSpace && !IsContainerPartition(SystemId)) {
  781. //
  782. // Get the nt pathname for this region.
  783. //
  784. SpNtNameFromRegion(
  785. pRegion,
  786. TemporaryBuffer,
  787. sizeof(TemporaryBuffer),
  788. PartitionOrdinalOriginal
  789. );
  790. //
  791. // Determine if it is a system partition.
  792. //
  793. for(BootEntry = SpBootEntries; BootEntry != NULL; BootEntry = BootEntry->Next) {
  794. if((BootEntry->LoaderPartitionNtName != NULL) &&
  795. !_wcsicmp(BootEntry->LoaderPartitionNtName,TemporaryBuffer)) {
  796. pRegion->IsSystemPartition = TRUE;
  797. break;
  798. }
  799. }
  800. }
  801. }
  802. }
  803. }
  804. }
  805. }
  806. }
  807. #endif
  808. VOID
  809. SpPtReadPartitionTables(
  810. IN PPARTITIONED_DISK pDisk
  811. )
  812. /*++
  813. Routine Description:
  814. Read partition tables from a given disk.
  815. Arguments:
  816. pDisk - supplies pointer to disk descriptor to be filled in.
  817. Return Value:
  818. None.
  819. --*/
  820. {
  821. NTSTATUS Status;
  822. HANDLE Handle;
  823. PUCHAR Buffer;
  824. PUCHAR UnalignedBuffer;
  825. PON_DISK_MBR pBr;
  826. BOOLEAN InMbr;
  827. ULONG ExtendedStart;
  828. ULONG NextSector;
  829. PMBR_INFO pEbr,pLastEbr;
  830. BOOLEAN FoundLink;
  831. ULONG i,x;
  832. BOOLEAN Ignore;
  833. ULONG bps;
  834. ULONG SectorsInBootrec;
  835. //
  836. // If this disk is off-line, nothing to do.
  837. //
  838. if(pDisk->HardDisk->Status != DiskOnLine) {
  839. return;
  840. }
  841. //
  842. // Open partition 0 of this disk.
  843. //
  844. Status = SpOpenPartition0(pDisk->HardDisk->DevicePath,&Handle,FALSE);
  845. if(!NT_SUCCESS(Status)) {
  846. pDisk->HardDisk->Status = DiskOffLine;
  847. return;
  848. }
  849. bps = pDisk->HardDisk->Geometry.BytesPerSector;
  850. if (!IsNEC_98) { //NEC98
  851. SectorsInBootrec = (512/bps) ? (512/bps) : 1;
  852. } else {
  853. // we read two sectors because 0 sector include BootCode , 1 sector include
  854. // PatitionTables. (In AT Machine,0 sector include BootCode and PartitionTable.)
  855. SectorsInBootrec = 2;
  856. } //NEC98
  857. //
  858. // Allocate and align a buffer for sector i/o.
  859. //
  860. // MBR size is not 512 on NEC98.
  861. //
  862. if (!IsNEC_98) {
  863. ASSERT(sizeof(ON_DISK_MBR)==512);
  864. }
  865. UnalignedBuffer = SpMemAlloc(2 * SectorsInBootrec * bps);
  866. Buffer = ALIGN(UnalignedBuffer,bps);
  867. //
  868. // Read the MBR (sector 0).
  869. //
  870. NextSector = 0;
  871. #ifdef _X86_
  872. readmbr:
  873. #endif
  874. Status = SpReadWriteDiskSectors(Handle,NextSector,SectorsInBootrec,bps,Buffer,FALSE);
  875. if(!NT_SUCCESS(Status)) {
  876. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: Unable to read mbr for disk %ws (%lx)\n",pDisk->HardDisk->DevicePath,Status));
  877. pDisk->HardDisk->Status = DiskOffLine;
  878. ZwClose(Handle);
  879. SpMemFree(UnalignedBuffer);
  880. return;
  881. }
  882. //
  883. // Move the data we just read into the partitioned disk descriptor.
  884. //
  885. if (!IsNEC_98) { //NEC98
  886. RtlMoveMemory(&pDisk->MbrInfo.OnDiskMbr,Buffer,sizeof(ON_DISK_MBR));
  887. } else {
  888. SpDetermineFormatTypeNec98(pDisk,(PREAL_DISK_MBR_NEC98)Buffer);
  889. if(pDisk->HardDisk->FormatType == DISK_FORMAT_TYPE_PCAT) {
  890. //
  891. // Move the data we just read into the partitioned disk descriptor.
  892. //
  893. SpTranslateMbrInfo(&pDisk->MbrInfo.OnDiskMbr,(PREAL_DISK_MBR)Buffer,bps,FALSE);
  894. } else {
  895. //
  896. // Translate patririon table information from NEC98 format to PC/AT format.
  897. //
  898. ConvertPartitionTable(pDisk,Buffer,bps);
  899. //
  900. // Read NTFT Signature at 16th sector to check if hard disk is valid.
  901. //
  902. RtlZeroMemory(Buffer,bps);
  903. SpReadWriteDiskSectors(Handle,16,1,bps,Buffer,FALSE);
  904. //
  905. // check "AA55" at the end of 16th sector.
  906. //
  907. if(((PUSHORT)Buffer)[bps/2 - 1] == BOOT_RECORD_SIGNATURE){
  908. U_ULONG(pDisk->MbrInfo.OnDiskMbr.NTFTSignature) = (((PULONG)Buffer)[0]);
  909. } else {
  910. U_ULONG(pDisk->MbrInfo.OnDiskMbr.NTFTSignature) = 0x00000000;
  911. }
  912. }
  913. } //NEC98
  914. //
  915. // If this MBR is not valid, initialize it. Otherwise, fetch all logical drives
  916. // (EBR) info as well.
  917. //
  918. if(U_USHORT(pDisk->MbrInfo.OnDiskMbr.AA55Signature) == MBR_SIGNATURE) {
  919. #ifdef _X86_
  920. //
  921. // No NEC98 supports EZ Drive.
  922. //
  923. if (!IsNEC_98) { //NEC98
  924. //
  925. // EZDrive support: if the first entry in the partition table is
  926. // type 0x55, then the actual partition table is on sector 1.
  927. //
  928. // Only for x86 because on non-x86, the firmware can't see EZDrive
  929. // partitions, so we don't want to install on them!
  930. //
  931. if(!NextSector && (pDisk->MbrInfo.OnDiskMbr.PartitionTable[0].SystemId == 0x55)) {
  932. NextSector = 1;
  933. pDisk->HardDisk->Int13Hooker = HookerEZDrive;
  934. goto readmbr;
  935. }
  936. //
  937. // Also check for on-track.
  938. //
  939. if(!NextSector && (pDisk->MbrInfo.OnDiskMbr.PartitionTable[0].SystemId == 0x54)) {
  940. pDisk->HardDisk->Int13Hooker = HookerOnTrackDiskManager;
  941. }
  942. } //NEC98
  943. #endif
  944. #if defined(REMOTE_BOOT)
  945. if (RemoteBootSetup && !RemoteInstallSetup &&
  946. (U_ULONG(pDisk->MbrInfo.OnDiskMbr.NTFTSignature) == 0)) {
  947. //
  948. // Uh, oh, we've got a case where the signature on the disk is 0, which is
  949. // bad for remote boot because we use 0 as flag for a diskless machine. Let's
  950. // write a new signature on the disk.
  951. //
  952. U_ULONG(pDisk->MbrInfo.OnDiskMbr.NTFTSignature) = SpComputeSerialNumber();
  953. RtlMoveMemory(Buffer, &pDisk->MbrInfo.OnDiskMbr, sizeof(ON_DISK_MBR));
  954. Status = SpReadWriteDiskSectors(Handle,NextSector,SectorsInBootrec,bps,Buffer,TRUE);
  955. //
  956. // Ignore the status - if it failed, then it failed. The only thing that will
  957. // happen is that the user will get a warning that they need to reformat later.
  958. //
  959. }
  960. #endif // defined(REMOTE_BOOT)
  961. pDisk->MbrWasValid = TRUE;
  962. pBr = &pDisk->MbrInfo.OnDiskMbr;
  963. InMbr = TRUE;
  964. ExtendedStart = 0;
  965. pLastEbr = NULL;
  966. do {
  967. //
  968. // Look at all the entries in the current boot record to see if there
  969. // is a link entry.
  970. //
  971. FoundLink = FALSE;
  972. for(i=0; i<PTABLE_DIMENSION; i++) {
  973. if(IsContainerPartition(pBr->PartitionTable[i].SystemId)) {
  974. FoundLink = TRUE;
  975. NextSector = ExtendedStart + U_ULONG(pBr->PartitionTable[i].RelativeSectors);
  976. if(NextSector == 0) {
  977. //
  978. // Then we've got ourselves one seriously messed up boot record. We'll
  979. // just return, and present this mess as free space.
  980. //
  981. // NOTE: maybe we should warn the user that we are going to ignore
  982. // partitions past this point because the structures are damaged.
  983. //
  984. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: Bad partition table for %ws\n",pDisk->HardDisk->DevicePath));
  985. ZwClose(Handle);
  986. SpMemFree(UnalignedBuffer);
  987. return;
  988. }
  989. pEbr = SpMemAlloc(sizeof(MBR_INFO));
  990. ASSERT(pEbr);
  991. RtlZeroMemory(pEbr,sizeof(MBR_INFO));
  992. //
  993. // Sector number on the disk where this boot sector is.
  994. //
  995. pEbr->OnDiskSector = NextSector;
  996. if(InMbr) {
  997. ExtendedStart = NextSector;
  998. InMbr = FALSE;
  999. }
  1000. //
  1001. // Read the next boot sector and break out of the loop through
  1002. // the current partition table.
  1003. //
  1004. Status = SpReadWriteDiskSectors(
  1005. Handle,
  1006. NextSector,
  1007. SectorsInBootrec,
  1008. bps,
  1009. Buffer,
  1010. FALSE
  1011. );
  1012. if(!IsNEC_98) {
  1013. RtlMoveMemory(&pEbr->OnDiskMbr,Buffer,sizeof(ON_DISK_MBR));
  1014. } else {
  1015. if(pDisk->HardDisk->FormatType == DISK_FORMAT_TYPE_PCAT) {
  1016. SpTranslateMbrInfo(&pEbr->OnDiskMbr,(PREAL_DISK_MBR)Buffer,bps,FALSE);
  1017. } else {
  1018. ConvertPartitionTable(pDisk,Buffer,bps);
  1019. }
  1020. }
  1021. if(!NT_SUCCESS(Status)
  1022. || (U_USHORT(pEbr->OnDiskMbr.AA55Signature) != MBR_SIGNATURE))
  1023. {
  1024. //
  1025. // NOTE: maybe we should warn the user that we are going to ignore
  1026. // partitions part this point because we could not read the disk
  1027. // or the structures are damaged.
  1028. //
  1029. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: Unable to read ebr on %ws at sector %lx (%lx)\n",pDisk->HardDisk->DevicePath,NextSector,Status));
  1030. ZwClose(Handle);
  1031. if(pLastEbr) {
  1032. SpMemFree(pEbr);
  1033. }
  1034. SpMemFree(UnalignedBuffer);
  1035. return;
  1036. }
  1037. pBr = &pEbr->OnDiskMbr;
  1038. //
  1039. // We just read the next boot sector. If all that boot sector contains
  1040. // is a link entry, the only thing we need the boot sector for is to find
  1041. // the next boot sector. This happens when there is free space at the start
  1042. // of the extended partition.
  1043. //
  1044. Ignore = TRUE;
  1045. for(x=0; x<PTABLE_DIMENSION; x++) {
  1046. if((pBr->PartitionTable[x].SystemId != PARTITION_ENTRY_UNUSED)
  1047. && !IsContainerPartition(pBr->PartitionTable[x].SystemId)) {
  1048. Ignore = FALSE;
  1049. break;
  1050. }
  1051. }
  1052. //
  1053. // Link the Ebr into the logical volume list if we're not ignoring it.
  1054. //
  1055. if(!Ignore) {
  1056. if(pLastEbr) {
  1057. pLastEbr->Next = pEbr;
  1058. } else {
  1059. ASSERT(pDisk->FirstEbrInfo.Next == NULL);
  1060. pDisk->FirstEbrInfo.Next = pEbr;
  1061. }
  1062. pLastEbr = pEbr;
  1063. }
  1064. break;
  1065. }
  1066. }
  1067. } while(FoundLink);
  1068. } else {
  1069. pDisk->MbrWasValid = FALSE;
  1070. if(!IsNEC_98) {
  1071. RtlZeroMemory(&pDisk->MbrInfo,sizeof(MBR_INFO));
  1072. } else {
  1073. RtlZeroMemory(Buffer,bps*SectorsInBootrec);
  1074. SpTranslateMbrInfo(&pDisk->MbrInfo.OnDiskMbr,(PREAL_DISK_MBR)Buffer,bps,FALSE);
  1075. }
  1076. U_USHORT(pDisk->MbrInfo.OnDiskMbr.AA55Signature) = MBR_SIGNATURE;
  1077. U_ULONG(pDisk->MbrInfo.OnDiskMbr.NTFTSignature) = SpComputeSerialNumber();
  1078. }
  1079. #if 0
  1080. if (IsNEC_98) { //NEC98
  1081. //
  1082. // Read NTFT Signature at 16th sector to check if hard disk is valid.
  1083. // (I wish to replace below codes by HAL function later.)
  1084. //
  1085. RtlZeroMemory(Buffer,bps);
  1086. SpReadWriteDiskSectors(Handle,
  1087. 16,
  1088. 1,
  1089. bps,
  1090. Buffer,
  1091. FALSE);
  1092. if(((PUSHORT)Buffer)[bps/2 - 1] == BOOT_RECORD_SIGNATURE){
  1093. U_ULONG(pDisk->MbrInfo.OnDiskMbr.NTFTSignature) = (((PULONG)Buffer)[0]);
  1094. } else {
  1095. U_ULONG(pDisk->MbrInfo.OnDiskMbr.NTFTSignature) = 0x00000000;
  1096. }
  1097. } //NEC98
  1098. #endif //0
  1099. //
  1100. // Close partition0.
  1101. //
  1102. ZwClose(Handle);
  1103. SpMemFree(UnalignedBuffer);
  1104. return;
  1105. }
  1106. PDISK_REGION
  1107. SpPtAllocateDiskRegionStructure(
  1108. IN ULONG DiskNumber,
  1109. IN ULONGLONG StartSector,
  1110. IN ULONGLONG SectorCount,
  1111. IN BOOLEAN PartitionedSpace,
  1112. IN PMBR_INFO MbrInfo,
  1113. IN ULONG TablePosition
  1114. )
  1115. /*++
  1116. Routine Description:
  1117. Allcoate and initialize a structure of type DISK_REGION.
  1118. Arguments:
  1119. Values to be filled into the fields of the newly allocated
  1120. disk region structure.
  1121. Return Value:
  1122. Pointer to new disk region structure.
  1123. --*/
  1124. {
  1125. PDISK_REGION p;
  1126. p = SpMemAlloc(sizeof(DISK_REGION));
  1127. ASSERT(p);
  1128. if(p) {
  1129. RtlZeroMemory(p,sizeof(DISK_REGION));
  1130. p->DiskNumber = DiskNumber;
  1131. p->StartSector = StartSector;
  1132. p->SectorCount = SectorCount;
  1133. p->PartitionedSpace = PartitionedSpace;
  1134. p->MbrInfo = MbrInfo;
  1135. p->TablePosition = TablePosition;
  1136. p->FtPartition = FALSE;
  1137. p->DynamicVolume = FALSE;
  1138. p->DynamicVolumeSuitableForOS = FALSE;
  1139. }
  1140. return(p);
  1141. }
  1142. VOID
  1143. SpPtInsertDiskRegionStructure(
  1144. IN PDISK_REGION Region,
  1145. IN OUT PDISK_REGION *ListHead
  1146. )
  1147. {
  1148. PDISK_REGION RegionCur,RegionPrev;
  1149. //
  1150. // Insert the region entry into the relevent list of region entries.
  1151. // Note that these lists are kept sorted by start sector.
  1152. //
  1153. if(RegionCur = *ListHead) {
  1154. if(Region->StartSector < RegionCur->StartSector) {
  1155. //
  1156. // Stick at head of list.
  1157. //
  1158. Region->Next = RegionCur;
  1159. *ListHead = Region;
  1160. } else {
  1161. while(1) {
  1162. RegionPrev = RegionCur;
  1163. RegionCur = RegionCur->Next;
  1164. if(RegionCur) {
  1165. if(RegionCur->StartSector > Region->StartSector) {
  1166. Region->Next = RegionCur;
  1167. RegionPrev->Next = Region;
  1168. break;
  1169. }
  1170. } else {
  1171. //
  1172. // Stick at end of list.
  1173. //
  1174. RegionPrev->Next = Region;
  1175. break;
  1176. }
  1177. }
  1178. }
  1179. } else {
  1180. *ListHead = Region;
  1181. }
  1182. }
  1183. VOID
  1184. SpPtAssignOrdinals(
  1185. IN PPARTITIONED_DISK pDisk,
  1186. IN BOOLEAN InitCurrentOrdinals,
  1187. IN BOOLEAN InitOnDiskOrdinals,
  1188. IN BOOLEAN InitOriginalOrdinals
  1189. )
  1190. {
  1191. PMBR_INFO pBrInfo;
  1192. ULONG i;
  1193. USHORT ordinal;
  1194. ordinal = 0;
  1195. for(pBrInfo=&pDisk->MbrInfo; pBrInfo; pBrInfo=pBrInfo->Next) {
  1196. for(i=0; i<PTABLE_DIMENSION; i++) {
  1197. PON_DISK_PTE pte = &pBrInfo->OnDiskMbr.PartitionTable[i];
  1198. if((pte->SystemId != PARTITION_ENTRY_UNUSED)
  1199. && !IsContainerPartition(pte->SystemId)) {
  1200. ordinal++;
  1201. if(InitCurrentOrdinals) {
  1202. pBrInfo->CurrentOrdinals[i] = ordinal;
  1203. }
  1204. if(InitOnDiskOrdinals) {
  1205. pBrInfo->OnDiskOrdinals[i] = ordinal;
  1206. }
  1207. if(InitOriginalOrdinals) {
  1208. pBrInfo->OriginalOrdinals[i] = ordinal;
  1209. }
  1210. } else {
  1211. if(InitCurrentOrdinals) {
  1212. pBrInfo->CurrentOrdinals[i] = 0;
  1213. }
  1214. if(InitOnDiskOrdinals) {
  1215. pBrInfo->OnDiskOrdinals[i] = 0;
  1216. }
  1217. if(InitOriginalOrdinals) {
  1218. pBrInfo->OriginalOrdinals[i] = 0;
  1219. }
  1220. }
  1221. }
  1222. }
  1223. }
  1224. VOID
  1225. SpPtInitializePartitionStructures(
  1226. IN ULONG DiskNumber
  1227. )
  1228. /*++
  1229. Routine Description:
  1230. Perform additional initialization on the partition structures,
  1231. beyond what has been performed in SpPtReadPartitionTables.
  1232. Specifically, determine partition ordinals, offsets, and sizes.
  1233. Arguments:
  1234. DiskNumber - disk ordinal of disk descriptor to be filled in.
  1235. Return Value:
  1236. None.
  1237. --*/
  1238. {
  1239. ULONG i,pass;
  1240. PMBR_INFO pBrInfo;
  1241. BOOLEAN InMbr;
  1242. ULONGLONG ExtendedStart = 0;
  1243. ULONGLONG ExtendedEnd,ExtendedSize;
  1244. ULONGLONG offset,size;
  1245. ULONG bps;
  1246. PDISK_REGION pRegion,pRegionCur,pRegionPrev;
  1247. PPARTITIONED_DISK pDisk = &PartitionedDisks[DiskNumber];
  1248. //
  1249. // If this disk is off-line, nothing to do.
  1250. //
  1251. if(pDisk->HardDisk->Status != DiskOnLine) {
  1252. return;
  1253. }
  1254. InMbr = TRUE;
  1255. bps = pDisk->HardDisk->Geometry.BytesPerSector;
  1256. //
  1257. // Link the EBR chain to the MBR.
  1258. //
  1259. if(!IsNEC_98 || (pDisk->HardDisk->FormatType == DISK_FORMAT_TYPE_PCAT)) {
  1260. pDisk->MbrInfo.Next = &pDisk->FirstEbrInfo;
  1261. } else {
  1262. //
  1263. // There are no extended partition on NEC98.
  1264. //
  1265. pDisk->MbrInfo.Next = NULL;;
  1266. } //NEC98
  1267. for(pBrInfo=&pDisk->MbrInfo; pBrInfo; pBrInfo=pBrInfo->Next) {
  1268. for(i=0; i<PTABLE_DIMENSION; i++) {
  1269. PON_DISK_PTE pte = &pBrInfo->OnDiskMbr.PartitionTable[i];
  1270. if(pte->SystemId != PARTITION_ENTRY_UNUSED) {
  1271. if(IsContainerPartition(pte->SystemId)) {
  1272. //
  1273. // If we're in the MBR, ExtendedStart will be 0.
  1274. //
  1275. offset = ExtendedStart + U_ULONG(pte->RelativeSectors);
  1276. size = U_ULONG(pte->SectorCount);
  1277. //
  1278. // Track the start of the extended partition.
  1279. //
  1280. if(InMbr) {
  1281. ExtendedStart = U_ULONG(pte->RelativeSectors);
  1282. ExtendedEnd = ExtendedStart + U_ULONG(pte->SectorCount);
  1283. ExtendedSize = ExtendedEnd - ExtendedStart;
  1284. }
  1285. } else {
  1286. //
  1287. // In the MBR, the relative sectors field is the sector offset
  1288. // to the partition. In EBRs, the relative sectors field is the
  1289. // number of sectors between the start of the boot sector and
  1290. // the start of the filesystem data area. We will consider such
  1291. // partitions to start with their boot sectors.
  1292. //
  1293. offset = InMbr ? U_ULONG(pte->RelativeSectors) : pBrInfo->OnDiskSector;
  1294. size = U_ULONG(pte->SectorCount)
  1295. + (InMbr ? 0 : U_ULONG(pte->RelativeSectors));
  1296. }
  1297. if(InMbr || !IsContainerPartition(pte->SystemId)) {
  1298. //
  1299. // Create a region entry for this used space.
  1300. //
  1301. pRegion = SpPtAllocateDiskRegionStructure(
  1302. DiskNumber,
  1303. offset,
  1304. size,
  1305. TRUE,
  1306. pBrInfo,
  1307. i
  1308. );
  1309. ASSERT(pRegion);
  1310. //
  1311. // Insert the region entry into the relevent list of region entries.
  1312. // Note that these lists are kept sorted by start sector.
  1313. //
  1314. SpPtInsertDiskRegionStructure(
  1315. pRegion,
  1316. InMbr ? &pDisk->PrimaryDiskRegions : &pDisk->ExtendedDiskRegions
  1317. );
  1318. }
  1319. }
  1320. }
  1321. if(InMbr) {
  1322. InMbr = FALSE;
  1323. }
  1324. }
  1325. //
  1326. // Initialize partition ordinals.
  1327. //
  1328. SpPtAssignOrdinals(pDisk,TRUE,TRUE,TRUE);
  1329. //
  1330. // Now go through the regions for this disk and insert free space descriptors
  1331. // where necessary.
  1332. //
  1333. // Pass 0 for the MBR; pass 1 for logical drives.
  1334. //
  1335. for(pass=0; pass<(ULONG)(ExtendedStart ? 2 : 1); pass++) {
  1336. if(pRegionPrev = (pass ? pDisk->ExtendedDiskRegions : pDisk->PrimaryDiskRegions)) {
  1337. ULONGLONG EndSector,FreeSpaceSize;
  1338. ASSERT(pRegionPrev->PartitionedSpace);
  1339. //
  1340. // Handle any space occurring *before* the first partition.
  1341. //
  1342. if(pRegionPrev->StartSector != (pass ? ExtendedStart : 0)) {
  1343. ASSERT(pRegionPrev->StartSector > (pass ? ExtendedStart : 0));
  1344. pRegion = SpPtAllocateDiskRegionStructure(
  1345. DiskNumber,
  1346. pass ? ExtendedStart : 0,
  1347. pRegionPrev->StartSector - (pass ? ExtendedStart : 0),
  1348. FALSE,
  1349. NULL,
  1350. 0
  1351. );
  1352. ASSERT(pRegion);
  1353. pRegion->Next = pRegionPrev;
  1354. if(pass) {
  1355. // extended
  1356. pDisk->ExtendedDiskRegions = pRegion;
  1357. } else {
  1358. // mbr
  1359. pDisk->PrimaryDiskRegions = pRegion;
  1360. }
  1361. }
  1362. pRegionCur = pRegionPrev->Next;
  1363. while(pRegionCur) {
  1364. //
  1365. // If the start of this partition plus its size is less than the
  1366. // start of the next partition, then we need a new region.
  1367. //
  1368. EndSector = pRegionPrev->StartSector + pRegionPrev->SectorCount;
  1369. FreeSpaceSize = pRegionCur->StartSector - EndSector;
  1370. if((LONG)FreeSpaceSize > 0) {
  1371. pRegion = SpPtAllocateDiskRegionStructure(
  1372. DiskNumber,
  1373. EndSector,
  1374. FreeSpaceSize,
  1375. FALSE,
  1376. NULL,
  1377. 0
  1378. );
  1379. ASSERT(pRegion);
  1380. pRegionPrev->Next = pRegion;
  1381. pRegion->Next = pRegionCur;
  1382. }
  1383. pRegionPrev = pRegionCur;
  1384. pRegionCur = pRegionCur->Next;
  1385. }
  1386. //
  1387. // Space at end of disk/extended partition.
  1388. //
  1389. EndSector = pRegionPrev->StartSector + pRegionPrev->SectorCount;
  1390. FreeSpaceSize = (pass ? ExtendedEnd : pDisk->HardDisk->DiskSizeSectors) - EndSector;
  1391. if((LONG)FreeSpaceSize > 0) {
  1392. pRegionPrev->Next = SpPtAllocateDiskRegionStructure(
  1393. DiskNumber,
  1394. EndSector,
  1395. FreeSpaceSize,
  1396. FALSE,
  1397. NULL,
  1398. 0
  1399. );
  1400. ASSERT(pRegionPrev->Next);
  1401. }
  1402. } else {
  1403. //
  1404. // Show whole disk/extended partition as free.
  1405. //
  1406. if(pass) {
  1407. //
  1408. // Extended partition.
  1409. //
  1410. ASSERT(ExtendedStart);
  1411. pDisk->ExtendedDiskRegions = SpPtAllocateDiskRegionStructure(
  1412. DiskNumber,
  1413. ExtendedStart,
  1414. ExtendedSize,
  1415. FALSE,
  1416. NULL,
  1417. 0
  1418. );
  1419. ASSERT(pDisk->ExtendedDiskRegions);
  1420. } else {
  1421. //
  1422. // MBR.
  1423. //
  1424. pDisk->PrimaryDiskRegions = SpPtAllocateDiskRegionStructure(
  1425. DiskNumber,
  1426. 0,
  1427. pDisk->HardDisk->DiskSizeSectors,
  1428. FALSE,
  1429. NULL,
  1430. 0
  1431. );
  1432. ASSERT(pDisk->PrimaryDiskRegions);
  1433. }
  1434. }
  1435. }
  1436. }
  1437. VOID
  1438. SpPtCountPrimaryPartitions(
  1439. IN PPARTITIONED_DISK pDisk,
  1440. OUT PULONG TotalPrimaryPartitionCount,
  1441. OUT PULONG RecognizedPrimaryPartitionCount,
  1442. OUT PBOOLEAN ExtendedExists
  1443. )
  1444. {
  1445. ULONG TotalCount;
  1446. ULONG RecognizedCount;
  1447. ULONG u;
  1448. UCHAR SysId;
  1449. TotalCount = 0;
  1450. RecognizedCount = 0;
  1451. *ExtendedExists = FALSE;
  1452. for(u=0; u<PTABLE_DIMENSION; u++) {
  1453. SysId = pDisk->MbrInfo.OnDiskMbr.PartitionTable[u].SystemId;
  1454. if(SysId != PARTITION_ENTRY_UNUSED) {
  1455. TotalCount++;
  1456. if(IsRecognizedPartition(SysId)
  1457. && !(SysId & VALID_NTFT) && !(SysId & PARTITION_NTFT)) {
  1458. RecognizedCount++;
  1459. }
  1460. if(IsContainerPartition(SysId)) {
  1461. *ExtendedExists = TRUE;
  1462. }
  1463. }
  1464. }
  1465. *TotalPrimaryPartitionCount = TotalCount;
  1466. *RecognizedPrimaryPartitionCount = RecognizedCount;
  1467. }
  1468. PDISK_REGION
  1469. SpPtLookupRegionByStart(
  1470. IN PPARTITIONED_DISK pDisk,
  1471. IN BOOLEAN ExtendedPartition,
  1472. IN ULONGLONG StartSector
  1473. )
  1474. /*++
  1475. Routine Description:
  1476. Locate a disk region, based on its starting sector.
  1477. The starting sector must match the starting sector of an existing
  1478. region EXACTLY for it to be considered a match.
  1479. Arguments:
  1480. pDisk - supplies disk on which to look for the region.
  1481. ExtendedPartition - if TRUE, then look in the extended partition to find
  1482. a match. Otherwise look in the main list.
  1483. StartSector - supplies the sector number of the first sector of the region.
  1484. Return Value:
  1485. NULL is region could not be found; otherwise a pointer to the matching
  1486. disk region structure.
  1487. --*/
  1488. {
  1489. PDISK_REGION Region = NULL;
  1490. #ifdef NEW_PARTITION_ENGINE
  1491. ExtendedPartition = FALSE;
  1492. #else
  1493. #ifdef GPT_PARTITION_ENGINE
  1494. if (pDisk->HardDisk->DiskFormatType == DISK_FORMAT_TYPE_GPT))
  1495. ExtendedPartition = FALSE;
  1496. #endif // GPT_PARTITION_ENGINE
  1497. #endif // NEW_PARTITION_ENGINE
  1498. Region = (ExtendedPartition) ?
  1499. pDisk->ExtendedDiskRegions : pDisk->PrimaryDiskRegions;
  1500. while (Region && (StartSector != Region->StartSector)) {
  1501. Region = Region->Next;
  1502. }
  1503. return Region;
  1504. }
  1505. ULONG
  1506. SpPtAlignStart(
  1507. IN PHARD_DISK pHardDisk,
  1508. IN ULONGLONG StartSector,
  1509. IN BOOLEAN ForExtended
  1510. )
  1511. /*++
  1512. Routine Description:
  1513. Snap a start sector to a cylinder boundary if it is not already
  1514. on a cylinder boundary. Any alignment that is necessary
  1515. is performed towards the end of the disk.
  1516. If the start sector is on cylinder 0, then alignment is to track 1
  1517. for primary partitions, or to track 0 on cylinder 1 for extended partitions.
  1518. Arguments:
  1519. pHardDisk - supplies disk descriptor for disk that the start sector is on.
  1520. StartSector - supplies the sector number of the first sector of the region.
  1521. ForExtended - if TRUE, then align the start sector as appropriate for creating
  1522. an extended partition. Otherwise align for a pimary partition or logical drive.
  1523. Return Value:
  1524. New (aligned) start sector. May or may not be different than StartSector.
  1525. --*/
  1526. {
  1527. PDISK_GEOMETRY pGeometry;
  1528. ULONGLONG r;
  1529. ULONGLONG C,H,S;
  1530. pGeometry = &pHardDisk->Geometry;
  1531. //
  1532. // Convert the start sector into cylinder, head, sector address.
  1533. //
  1534. C = StartSector / pHardDisk->SectorsPerCylinder;
  1535. r = StartSector % pHardDisk->SectorsPerCylinder;
  1536. H = r / pGeometry->SectorsPerTrack;
  1537. S = r % pGeometry->SectorsPerTrack;
  1538. //
  1539. // Align as necessary.
  1540. //
  1541. if(C) {
  1542. if(H || S) {
  1543. H = S = 0;
  1544. C++;
  1545. }
  1546. } else {
  1547. //
  1548. // Start cylinder is 0. If the caller wants to create an
  1549. // extended partition, bump the start cylinder up to 1.
  1550. //
  1551. if(ForExtended) {
  1552. C = 1;
  1553. H = S = 0;
  1554. } else {
  1555. if (!IsNEC_98 || (pHardDisk->FormatType == DISK_FORMAT_TYPE_PCAT)) { //NEC98
  1556. //
  1557. // Start cylinder is 0 and the caller does not want to
  1558. // create an extended partition. In this case, we want
  1559. // to start the partition on cylinder 0, track 1. If the
  1560. // start is beyond this already, start on cylinder 1.
  1561. //
  1562. if((H == 0) || ((H == 1) && !S)) {
  1563. H = 1;
  1564. S = 0;
  1565. } else {
  1566. H = S = 0;
  1567. C = 1;
  1568. }
  1569. } else {
  1570. //
  1571. // if Start cylinder is 0, force start Cylinder 1.
  1572. //
  1573. C = 1;
  1574. H = S = 0;
  1575. } //NEC98
  1576. }
  1577. }
  1578. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  1579. "SETUP:SpPtAlignStart():C:%I64d,H:%I64d,S:%I64d\n",
  1580. C, H, S));
  1581. //
  1582. // Now calculate and return the new start sector.
  1583. //
  1584. return (ULONG)((C * pHardDisk->SectorsPerCylinder) + (H * pGeometry->SectorsPerTrack) + S);
  1585. }
  1586. VOID
  1587. SpPtQueryMinMaxCreationSizeMB(
  1588. IN ULONG DiskNumber,
  1589. IN ULONGLONG StartSector,
  1590. IN BOOLEAN ForExtended,
  1591. IN BOOLEAN InExtended,
  1592. OUT PULONGLONG MinSize,
  1593. OUT PULONGLONG MaxSize,
  1594. OUT PBOOLEAN ReservedRegion
  1595. )
  1596. /*++
  1597. Routine Description:
  1598. Given the starting sector of an unpartitioned space on a disk,
  1599. determine the minimum and maximum size in megabytes of the partition that can
  1600. be created in the space, taking all alignment and rounding
  1601. requirements into account.
  1602. Arguments:
  1603. DiskNumber - ordinal of disk on which partition will be created.
  1604. StartSector - starting sector of an unpartitioned space on the disk.
  1605. ForExtended - if TRUE, then the caller wants to know how large an
  1606. extended partition in that space could be. This may be smaller
  1607. than the general case, because an extended partition cannot start
  1608. on cylinder 0.
  1609. InExtended - if TRUE, then we want to create a logical drive. Otherwise
  1610. we want to create a primary (including extended) partition.
  1611. If TRUE, ForExtended must be FALSE.
  1612. MinSize - receives minimum size in megabytes for a partition in the space.
  1613. MaxSize - receives maximum size in megabytes for a partition in the space.
  1614. ReservedRegion - Receives a flag that indicates if the region is entirely
  1615. in the last cylinder. Because the last cylinder should be
  1616. reserved for dynamic volumes, this routine will return 0
  1617. as MaxSize, if the region is in such a cylinder
  1618. Return Value:
  1619. None.
  1620. --*/
  1621. {
  1622. PPARTITIONED_DISK pDisk;
  1623. ULONGLONG AlignedStartSector;
  1624. ULONGLONG AlignedEndSector;
  1625. ULONGLONG SectorCount;
  1626. PDISK_REGION pRegion;
  1627. ULONGLONG MB, ByteSize;
  1628. ULONGLONG Remainder;
  1629. ULONGLONG LeftOverSectors;
  1630. *MinSize = 0;
  1631. *MaxSize = 0;
  1632. *ReservedRegion = FALSE;
  1633. ASSERT(DiskNumber < HardDiskCount);
  1634. if(InExtended) {
  1635. ASSERT(!ForExtended);
  1636. }
  1637. pDisk = &PartitionedDisks[DiskNumber];
  1638. //
  1639. // Look up this region.
  1640. //
  1641. pRegion = SpPtLookupRegionByStart(pDisk, InExtended, StartSector);
  1642. ASSERT(pRegion);
  1643. if(!pRegion) {
  1644. return;
  1645. }
  1646. ASSERT(!pRegion->PartitionedSpace);
  1647. if(pRegion->PartitionedSpace) {
  1648. return;
  1649. }
  1650. //
  1651. // If this is the first free space inside the extended partition
  1652. // we need to decrement the StartSector so that while creating
  1653. // first logical inside the extended we don't create the
  1654. // logical at one cylinder offset
  1655. //
  1656. if (SPPT_IS_REGION_NEXT_TO_FIRST_CONTAINER(pRegion) && StartSector) {
  1657. StartSector--;
  1658. }
  1659. //
  1660. // Align the start to a proper boundary.
  1661. //
  1662. AlignedStartSector = SpPtAlignStart(pDisk->HardDisk,StartSector,ForExtended);
  1663. //
  1664. // Determine the maximum aligned end sector.
  1665. //
  1666. AlignedEndSector = StartSector + pRegion->SectorCount;
  1667. if(LeftOverSectors = AlignedEndSector % pDisk->HardDisk->SectorsPerCylinder) {
  1668. AlignedEndSector -= LeftOverSectors;
  1669. }
  1670. //
  1671. // Find out if last sector is in the last cylinder. If it is then align it down.
  1672. // This is because we should not allow the user to create a partition that contains the last cylinder.
  1673. // This is necessary so that we reserve a cylinder at the end of the disk, so that users
  1674. // can convert the disk to dynamic after the system is installed.
  1675. //
  1676. // (guhans) Don't align down if this is ASR. ASR already takes this into account.
  1677. //
  1678. if(!DockableMachine && !SpDrEnabled() && SPPT_IS_MBR_DISK(DiskNumber) && (!pRegion->Next) &&
  1679. (AlignedEndSector >= (pDisk->HardDisk->CylinderCount - 1) * pDisk->HardDisk->SectorsPerCylinder)) {
  1680. AlignedEndSector -= pDisk->HardDisk->SectorsPerCylinder;
  1681. if(AlignedEndSector == AlignedStartSector) {
  1682. //
  1683. // If after alignment, the partition size is zero, then the user was attempting to
  1684. // create a partition in the last cylinder of the disk. Since this cylinder is
  1685. // reserved for LDM (dynamic volume), just return 0 as maximum partition size, and
  1686. // also indicate to the caller that the region is reserved.
  1687. //
  1688. *ReservedRegion = TRUE;
  1689. *MinSize = 0;
  1690. *MaxSize = 0;
  1691. return;
  1692. }
  1693. }
  1694. //
  1695. // Calculate the number of sectors in the properly aligned space.
  1696. //
  1697. SectorCount = AlignedEndSector - AlignedStartSector;
  1698. //
  1699. // Convert sectors to MB.
  1700. //
  1701. ByteSize = SectorCount * pDisk->HardDisk->Geometry.BytesPerSector;
  1702. MB = ByteSize / (1024 * 1024);
  1703. Remainder = ByteSize % (1024 * 1024);
  1704. //
  1705. // If the remainder was greater than or equal to a half meg,
  1706. // bump up the number of megabytes.
  1707. //
  1708. *MaxSize = (MB + ((Remainder >= (512 * 1024)) ? 1 : 0));
  1709. //
  1710. // The mimimum size is one cylinder except that if a cylinder
  1711. // is smaller than 1 meg, the min size is 1 meg.
  1712. //
  1713. ByteSize = pDisk->HardDisk->SectorsPerCylinder *
  1714. pDisk->HardDisk->Geometry.BytesPerSector;
  1715. *MinSize = ByteSize / (1024 * 1024);
  1716. Remainder = ByteSize % (1024 * 1024);
  1717. if((*MinSize == 0) || (Remainder >= (512 * 1024))) {
  1718. (*MinSize)++;
  1719. }
  1720. }
  1721. ULONGLONG
  1722. SpPtSectorCountToMB(
  1723. IN PHARD_DISK pHardDisk,
  1724. IN ULONGLONG SectorCount
  1725. )
  1726. {
  1727. ULONGLONG ByteCount;
  1728. ULONGLONG MB,r;
  1729. //
  1730. // Calculate the number of bytes that this number of
  1731. // sectors represents.
  1732. //
  1733. ByteCount = (pHardDisk->Geometry.BytesPerSector * SectorCount);
  1734. //
  1735. // Calculate the number of megabytes this represents.
  1736. //
  1737. r = ByteCount % (1024 * 1204);
  1738. MB = ByteCount / (1024 * 1024);
  1739. //
  1740. // Round up if necessary.
  1741. //
  1742. if(r >= (512*1024)) {
  1743. MB++;
  1744. }
  1745. return (MB);
  1746. }
  1747. VOID
  1748. SpPtInitializeCHSFields(
  1749. IN PHARD_DISK HardDisk,
  1750. IN ULONGLONG AbsoluteStartSector,
  1751. IN ULONGLONG AbsoluteSectorCount,
  1752. OUT PON_DISK_PTE pte
  1753. )
  1754. {
  1755. ULONGLONG sC,sH,sS,r;
  1756. ULONGLONG eC,eH,eS;
  1757. ULONGLONG LastSector;
  1758. sC = AbsoluteStartSector / HardDisk->SectorsPerCylinder;
  1759. r = AbsoluteStartSector % HardDisk->SectorsPerCylinder;
  1760. sH = r / HardDisk->Geometry.SectorsPerTrack;
  1761. sS = r % HardDisk->Geometry.SectorsPerTrack;
  1762. LastSector = AbsoluteStartSector + AbsoluteSectorCount - 1;
  1763. eC = LastSector / HardDisk->SectorsPerCylinder;
  1764. r = LastSector % HardDisk->SectorsPerCylinder;
  1765. eH = r / HardDisk->Geometry.SectorsPerTrack;
  1766. eS = r % HardDisk->Geometry.SectorsPerTrack;
  1767. //
  1768. // If this partition extends past the 1024th cylinder,
  1769. // place reasonable values in the CHS fields.
  1770. //
  1771. #if defined(NEC_98) //NEC98
  1772. if (!IsNEC_98 || (HardDisk->FormatType == DISK_FORMAT_TYPE_PCAT)) { //NEC98
  1773. #endif //NEC98
  1774. if(eC >= 1024) {
  1775. sC = 1023;
  1776. sH = HardDisk->Geometry.TracksPerCylinder - 1;
  1777. sS = HardDisk->Geometry.SectorsPerTrack - 1;
  1778. eC = sC;
  1779. eH = sH;
  1780. eS = sS;
  1781. }
  1782. //
  1783. // Pack the CHS values into int13 format.
  1784. //
  1785. pte->StartCylinder = (UCHAR)sC;
  1786. pte->StartHead = (UCHAR)sH;
  1787. pte->StartSector = (UCHAR)((sS & 0x3f) | ((sC >> 2) & 0xc0)) + 1;
  1788. pte->EndCylinder = (UCHAR)eC;
  1789. pte->EndHead = (UCHAR)eH;
  1790. pte->EndSector = (UCHAR)((eS & 0x3f) | ((eC >> 2) & 0xc0)) + 1;
  1791. #if defined(NEC_98) //NEC98
  1792. } else {
  1793. //
  1794. // No NEC98 have "1024th cylinder limit".
  1795. //
  1796. pte->StartCylinderLow = (UCHAR)sC;
  1797. pte->StartCylinderHigh = (UCHAR)(sC >> 4);
  1798. pte->StartHead = (UCHAR)sH;
  1799. pte->StartSector = (UCHAR)sS;
  1800. pte->EndCylinderLow = (UCHAR)eC;
  1801. pte->EndCylinderHigh = (UCHAR)(eC >> 4);
  1802. pte->EndHead = (UCHAR)eH;
  1803. pte->EndSector = (UCHAR)eS;
  1804. } //NEC98
  1805. #endif //NEC98
  1806. }
  1807. #ifndef NEW_PARTITION_ENGINE
  1808. BOOLEAN
  1809. SpPtCreate(
  1810. IN ULONG DiskNumber,
  1811. IN ULONGLONG StartSector,
  1812. IN ULONGLONG SizeMB,
  1813. IN BOOLEAN InExtended,
  1814. IN PPARTITION_INFORMATION_EX PartInfo,
  1815. OUT PDISK_REGION *ActualDiskRegion OPTIONAL
  1816. )
  1817. /*++
  1818. Routine Description:
  1819. Create a partition in a given free space.
  1820. Arguments:
  1821. DiskNumber - supplies the number of the disk on which we are
  1822. creating the partition.
  1823. StartSector - supplies the start sector of the free space in which
  1824. the parititon is to be created. This must exactly match the
  1825. start sector of the free space, and can be in either the primary
  1826. space list or the list of spaces in the extended partition.
  1827. SizeMB - supplies the size in megabytes of the partition.
  1828. InExtended - if TRUE, then the free space is within the extended partition,
  1829. and thus we are creating a logical drive. If FALSE, then the free
  1830. space is an ordinary unpartitioned space, and we are creating a
  1831. primary partition.
  1832. SysId - supplies the system id to give the partition. This may not
  1833. be 5/f (PARTITION_EXTENDED) if InExtended is TRUE or is an extended
  1834. partition already exists. No other checks are performed on this value.
  1835. ActualDiskRegion - if supplied, receives a pointer to the disk region in which
  1836. the partition was created.
  1837. Return Value:
  1838. TRUE if the partition was created successfully.
  1839. FALSE otherwise.
  1840. --*/
  1841. {
  1842. PPARTITIONED_DISK pDisk;
  1843. ULONGLONG SectorCount;
  1844. ULONGLONG AlignedStartSector;
  1845. ULONGLONG AlignedEndSector;
  1846. PDISK_REGION pRegion,pRegionPrev,pRegionNew,*pRegionHead;
  1847. ULONGLONG LeftOverSectors;
  1848. PMBR_INFO pBrInfo;
  1849. ULONG slot,i,spt;
  1850. PON_DISK_PTE pte;
  1851. ULONGLONG ExtendedStart;
  1852. UCHAR SysId;
  1853. #ifdef GPT_PARTITION_ENGINE
  1854. if (SPPT_IS_GPT_DISK(DiskNumber)) {
  1855. return SpPtnCreate(DiskNumber,
  1856. StartSector,
  1857. 0, // SizeInSectors: Not used except in ASR
  1858. SizeMB,
  1859. InExtended,
  1860. TRUE,
  1861. PartInfo,
  1862. ActualDiskRegion);
  1863. }
  1864. #endif
  1865. SysId = PartInfo->Mbr.PartitionType;
  1866. //
  1867. // Look up the disk region that describes this free space.
  1868. //
  1869. pDisk = &PartitionedDisks[DiskNumber];
  1870. pRegion = SpPtLookupRegionByStart(pDisk,InExtended,StartSector);
  1871. ASSERT(pRegion);
  1872. if(!pRegion) {
  1873. return(FALSE);
  1874. }
  1875. if(ActualDiskRegion) {
  1876. *ActualDiskRegion = pRegion;
  1877. }
  1878. ASSERT(!pRegion->PartitionedSpace);
  1879. if(pRegion->PartitionedSpace) {
  1880. return(FALSE);
  1881. }
  1882. if(InExtended) {
  1883. ASSERT(!IsContainerPartition(SysId));
  1884. //
  1885. // Locate the start sector of the extended partition.
  1886. //
  1887. for(i=0; i<PTABLE_DIMENSION; i++) {
  1888. if(IsContainerPartition(pDisk->MbrInfo.OnDiskMbr.PartitionTable[i].SystemId)) {
  1889. ExtendedStart = U_ULONG(pDisk->MbrInfo.OnDiskMbr.PartitionTable[i].RelativeSectors);
  1890. break;
  1891. }
  1892. }
  1893. ASSERT(ExtendedStart);
  1894. if(!ExtendedStart) {
  1895. return(FALSE);
  1896. }
  1897. }
  1898. //
  1899. // Determine the number of sectors in the size passed in.
  1900. // Note: the calculation is performed such that intermediate results
  1901. // won't overflow a ULONG.
  1902. //
  1903. SectorCount = SizeMB * ((1024*1024)/pDisk->HardDisk->Geometry.BytesPerSector);
  1904. //
  1905. // Align the start sector.
  1906. //
  1907. AlignedStartSector = SpPtAlignStart(
  1908. pDisk->HardDisk,
  1909. StartSector,
  1910. (BOOLEAN)IsContainerPartition(SysId)
  1911. );
  1912. //
  1913. // Determine the end sector based on the size passed in.
  1914. //
  1915. AlignedEndSector = AlignedStartSector + SectorCount;
  1916. //
  1917. // Align the ending sector to a cylinder boundary. If it is not already
  1918. // aligned and is more than half way into the final cylinder, align it up,
  1919. // otherwise align it down.
  1920. //
  1921. if(LeftOverSectors = AlignedEndSector % pDisk->HardDisk->SectorsPerCylinder) {
  1922. AlignedEndSector -= LeftOverSectors;
  1923. if(LeftOverSectors > pDisk->HardDisk->SectorsPerCylinder/2) {
  1924. AlignedEndSector += pDisk->HardDisk->SectorsPerCylinder;
  1925. }
  1926. }
  1927. //
  1928. // If the ending sector is past the end of the free space, shrink it
  1929. // so it fits.
  1930. //
  1931. while(AlignedEndSector > pRegion->StartSector + pRegion->SectorCount) {
  1932. AlignedEndSector -= pDisk->HardDisk->SectorsPerCylinder;
  1933. }
  1934. //
  1935. // Find out if last sector is in the last cylinder. If it is then align it down.
  1936. // This is necessary so that we reserve a cylinder at the end of the disk, so that users
  1937. // can convert the disk to dynamic after the system is installed.
  1938. //
  1939. // (guhans) Don't align down if this is ASR. ASR already takes this into account.
  1940. //
  1941. if( !DockableMachine && !SpDrEnabled() &&
  1942. (AlignedEndSector > (pDisk->HardDisk->CylinderCount - 1) * pDisk->HardDisk->SectorsPerCylinder)
  1943. ) {
  1944. AlignedEndSector -= pDisk->HardDisk->SectorsPerCylinder;
  1945. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: End of partition was aligned down 1 cylinder \n"));
  1946. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: AlignedStartSector = %lx \n", AlignedStartSector));
  1947. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: AlignedEndSector = %lx \n", AlignedEndSector));
  1948. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: SectorsPerCylinder = %lx \n", pDisk->HardDisk->SectorsPerCylinder));
  1949. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: CylinderCount = %lx \n", pDisk->HardDisk->CylinderCount));
  1950. }
  1951. ASSERT((LONG)AlignedEndSector > 0);
  1952. if((LONG)AlignedEndSector < 0) {
  1953. return(FALSE);
  1954. }
  1955. //
  1956. // If we are creating a logical drive, create a new mbr structure
  1957. // for it.
  1958. //
  1959. if(InExtended) {
  1960. //
  1961. // Create a boot record for this new logical drive; use slot #0
  1962. // for the partition entry (and slot #1 for the extended record,
  1963. // if necessary).
  1964. //
  1965. pBrInfo = SpMemAlloc(sizeof(MBR_INFO));
  1966. ASSERT(pBrInfo);
  1967. RtlZeroMemory(pBrInfo,sizeof(MBR_INFO));
  1968. slot = 0;
  1969. } else {
  1970. //
  1971. // Look for a free slot in the MBR's partition table.
  1972. //
  1973. pBrInfo = &pDisk->MbrInfo;
  1974. for(slot=0; slot<PTABLE_DIMENSION; slot++) {
  1975. if(pBrInfo->OnDiskMbr.PartitionTable[slot].SystemId == PARTITION_ENTRY_UNUSED) {
  1976. break;
  1977. }
  1978. }
  1979. if(slot == PTABLE_DIMENSION) {
  1980. ASSERT(0);
  1981. return(FALSE);
  1982. }
  1983. }
  1984. //
  1985. // Initialize the partition table entry.
  1986. //
  1987. spt = pDisk->HardDisk->Geometry.SectorsPerTrack;
  1988. pte = &pBrInfo->OnDiskMbr.PartitionTable[slot];
  1989. pte->ActiveFlag = 0;
  1990. pte->SystemId = SysId;
  1991. U_ULONG(pte->RelativeSectors) = (ULONG)(InExtended ? spt : AlignedStartSector);
  1992. U_ULONG(pte->SectorCount) = (ULONG)(AlignedEndSector - AlignedStartSector - (InExtended ? spt : 0));
  1993. SpPtInitializeCHSFields(
  1994. pDisk->HardDisk,
  1995. AlignedStartSector + (InExtended ? spt : 0),
  1996. AlignedEndSector - AlignedStartSector - (InExtended ? spt : 0),
  1997. pte
  1998. );
  1999. //
  2000. // If we're in the extended partition we mark all entries in the
  2001. // boot record as dirty. Sometimes there is a turd boot record on
  2002. // the disk already, and by setting all entries to dirty we get
  2003. // the crud cleaned out if necessary. The only entries that should be
  2004. // in an EBR are the type 6 or whatever and a type 5 if there are
  2005. // additional logical drives in the extended partition.
  2006. //
  2007. if(InExtended) {
  2008. for(i=0; i<PTABLE_DIMENSION; i++) {
  2009. pBrInfo->Dirty[i] = TRUE;
  2010. }
  2011. } else {
  2012. pBrInfo->Dirty[slot] = TRUE;
  2013. }
  2014. //
  2015. // Don't zap the first sector of the extended partition,
  2016. // as this wipes out the first logical drive, and precludes
  2017. // access to all logical drives!
  2018. //
  2019. if(!IsContainerPartition(SysId)) {
  2020. pBrInfo->ZapBootSector[slot] = TRUE;
  2021. }
  2022. //
  2023. // Find the previous region (ie, the one that points to this one).
  2024. // This region (if it exists) will be partitioned space (otherwise
  2025. // it would have been part of the region we are trying to create
  2026. // a partition in!)
  2027. //
  2028. pRegionHead = InExtended ? &pDisk->ExtendedDiskRegions : &pDisk->PrimaryDiskRegions;
  2029. if(*pRegionHead == pRegion) {
  2030. pRegionPrev = NULL;
  2031. } else {
  2032. for(pRegionPrev = *pRegionHead; pRegionPrev; pRegionPrev = pRegionPrev->Next) {
  2033. if(pRegionPrev->Next == pRegion) {
  2034. ASSERT(pRegionPrev->PartitionedSpace);
  2035. break;
  2036. }
  2037. }
  2038. }
  2039. if(InExtended) {
  2040. PMBR_INFO PrevEbr;
  2041. //
  2042. // The new logical drive goes immediately after the
  2043. // previous logical drive (if any). Remember that if there is
  2044. // a previous region, it will be partitioned space (otherwise
  2045. // it would be a part of the region we are trying to create
  2046. // a partition in).
  2047. //
  2048. PrevEbr = pRegionPrev ? pRegionPrev->MbrInfo : NULL;
  2049. if(PrevEbr) {
  2050. pBrInfo->Next = PrevEbr->Next;
  2051. PrevEbr->Next = pBrInfo;
  2052. } else {
  2053. //
  2054. // No previous EBR or region. This means we are creating
  2055. // a logical drive at the beginning of the extended partition
  2056. // so set the First Ebr pointer to point to the new Ebr.
  2057. // Note that this does not mean that the extended partition
  2058. // is empty; the Next pointer in the new Ebr structure is
  2059. // set later.
  2060. //
  2061. pDisk->FirstEbrInfo.Next = pBrInfo;
  2062. if(pRegion->Next) {
  2063. //
  2064. // If there is a region following the one we're creating
  2065. // the partition in, it must be partitioned space, or else
  2066. // it would be part of the region we're creating the partition in.
  2067. //
  2068. ASSERT(pRegion->Next->PartitionedSpace);
  2069. ASSERT(pRegion->Next->MbrInfo);
  2070. pBrInfo->Next = pRegion->Next->MbrInfo;
  2071. } else {
  2072. //
  2073. // No more partitioned space in the extended partition;
  2074. // the logical drive we are creating is the only one.
  2075. //
  2076. pBrInfo->Next = NULL;
  2077. }
  2078. }
  2079. pBrInfo->OnDiskSector = AlignedStartSector;
  2080. //
  2081. // Create a link entry in the previous logical drive (if any).
  2082. //
  2083. if(PrevEbr) {
  2084. //
  2085. // If there is a link entry in there already, blow it away.
  2086. //
  2087. for(i=0; i<PTABLE_DIMENSION; i++) {
  2088. if(IsContainerPartition(PrevEbr->OnDiskMbr.PartitionTable[i].SystemId)) {
  2089. RtlZeroMemory(&PrevEbr->OnDiskMbr.PartitionTable[i],sizeof(ON_DISK_PTE));
  2090. PrevEbr->Dirty[i] = TRUE;
  2091. break;
  2092. }
  2093. }
  2094. //
  2095. // Find a free slot for the link entry.
  2096. //
  2097. for(i=0; i<PTABLE_DIMENSION; i++) {
  2098. pte = &PrevEbr->OnDiskMbr.PartitionTable[i];
  2099. if(pte->SystemId == PARTITION_ENTRY_UNUSED) {
  2100. pte->SystemId = PARTITION_EXTENDED;
  2101. pte->ActiveFlag = 0;
  2102. U_ULONG(pte->RelativeSectors) = (ULONG)(AlignedStartSector - ExtendedStart);
  2103. U_ULONG(pte->SectorCount) = (ULONG)(AlignedEndSector - AlignedStartSector);
  2104. SpPtInitializeCHSFields(
  2105. pDisk->HardDisk,
  2106. AlignedStartSector,
  2107. U_ULONG(pte->SectorCount),
  2108. pte
  2109. );
  2110. PrevEbr->Dirty[i] = TRUE;
  2111. break;
  2112. }
  2113. }
  2114. }
  2115. //
  2116. // Create a link entry in this new logical drive if necessary.
  2117. //
  2118. if(pBrInfo->Next) {
  2119. //
  2120. // Find the next entry's logical drive.
  2121. //
  2122. for(i=0; i<PTABLE_DIMENSION; i++) {
  2123. if((pBrInfo->Next->OnDiskMbr.PartitionTable[i].SystemId != PARTITION_ENTRY_UNUSED)
  2124. && !IsContainerPartition(pBrInfo->Next->OnDiskMbr.PartitionTable[i].SystemId))
  2125. {
  2126. pte = &pBrInfo->OnDiskMbr.PartitionTable[1];
  2127. pte->SystemId = PARTITION_EXTENDED;
  2128. pte->ActiveFlag = 0;
  2129. U_ULONG(pte->RelativeSectors) = (ULONG)(pBrInfo->Next->OnDiskSector - ExtendedStart);
  2130. U_ULONG(pte->SectorCount) = U_ULONG(pBrInfo->Next->OnDiskMbr.PartitionTable[i].RelativeSectors)
  2131. + U_ULONG(pBrInfo->Next->OnDiskMbr.PartitionTable[i].SectorCount);
  2132. SpPtInitializeCHSFields(
  2133. pDisk->HardDisk,
  2134. pBrInfo->Next->OnDiskSector,
  2135. U_ULONG(pte->SectorCount),
  2136. pte
  2137. );
  2138. break;
  2139. }
  2140. }
  2141. }
  2142. }
  2143. //
  2144. // If we just created a new extended partition, we need to
  2145. // create a blank region descriptor for it in the extended region list.
  2146. //
  2147. if(!InExtended && IsContainerPartition(SysId)) {
  2148. ASSERT(pDisk->ExtendedDiskRegions == NULL);
  2149. pDisk->ExtendedDiskRegions = SpPtAllocateDiskRegionStructure(
  2150. DiskNumber,
  2151. AlignedStartSector,
  2152. AlignedEndSector - AlignedStartSector,
  2153. FALSE,
  2154. NULL,
  2155. 0
  2156. );
  2157. ASSERT(pDisk->ExtendedDiskRegions);
  2158. }
  2159. //
  2160. // Create a new disk region for the new free space at the
  2161. // beginning and end of the free space, if any.
  2162. //
  2163. if(AlignedStartSector - pRegion->StartSector) {
  2164. pRegionNew = SpPtAllocateDiskRegionStructure(
  2165. DiskNumber,
  2166. pRegion->StartSector,
  2167. AlignedStartSector - pRegion->StartSector,
  2168. FALSE,
  2169. NULL,
  2170. 0
  2171. );
  2172. ASSERT(pRegionNew);
  2173. if(pRegionPrev) {
  2174. pRegionPrev->Next = pRegionNew;
  2175. } else {
  2176. *pRegionHead = pRegionNew;
  2177. }
  2178. pRegionNew->Next = pRegion;
  2179. }
  2180. if(pRegion->StartSector + pRegion->SectorCount - AlignedEndSector) {
  2181. pRegionNew = SpPtAllocateDiskRegionStructure(
  2182. DiskNumber,
  2183. AlignedEndSector,
  2184. pRegion->StartSector + pRegion->SectorCount - AlignedEndSector,
  2185. FALSE,
  2186. NULL,
  2187. 0
  2188. );
  2189. pRegionNew->Next = pRegion->Next;
  2190. pRegion->Next = pRegionNew;
  2191. }
  2192. //
  2193. // Adjust the current disk region.
  2194. //
  2195. pRegion->StartSector = AlignedStartSector;
  2196. pRegion->SectorCount = AlignedEndSector - AlignedStartSector;
  2197. pRegion->PartitionedSpace = TRUE;
  2198. pRegion->TablePosition = slot;
  2199. pRegion->MbrInfo = pBrInfo;
  2200. pRegion->VolumeLabel[0] = 0;
  2201. pRegion->Filesystem = FilesystemNewlyCreated;
  2202. pRegion->FreeSpaceKB = (ULONG)(-1);
  2203. pRegion->AdjustedFreeSpaceKB = (ULONG)(-1);
  2204. SpFormatMessage(
  2205. pRegion->TypeName,
  2206. sizeof(pRegion->TypeName),
  2207. SP_TEXT_FS_NAME_BASE + pRegion->Filesystem
  2208. );
  2209. SpPtCommitChanges(DiskNumber,(PUCHAR)&i);
  2210. //
  2211. // Adjust partition ordinals on this disk.
  2212. //
  2213. SpPtAssignOrdinals(pDisk,FALSE,FALSE,FALSE);
  2214. //
  2215. // Get the nt pathname for this region.
  2216. //
  2217. if (!IsContainerPartition(SysId)) {
  2218. SpNtNameFromRegion(
  2219. pRegion,
  2220. TemporaryBuffer,
  2221. sizeof(TemporaryBuffer),
  2222. PartitionOrdinalCurrent
  2223. );
  2224. //
  2225. // Assign a drive letter for this region
  2226. //
  2227. if (!SpDrEnabled()) {
  2228. pRegion->DriveLetter = SpGetDriveLetter( TemporaryBuffer, NULL );
  2229. if (pRegion->DriveLetter == 0) {
  2230. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: SpGetDriveLetter failed on %ls\n", TemporaryBuffer));
  2231. } else {
  2232. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: Partition = %ls (%ls), DriveLetter = %wc: \n", TemporaryBuffer, (InExtended)? L"Extended" : L"Primary", pRegion->DriveLetter));
  2233. }
  2234. }
  2235. }
  2236. return(TRUE);
  2237. }
  2238. BOOLEAN
  2239. SpPtDelete(
  2240. IN ULONG DiskNumber,
  2241. IN ULONGLONG StartSector
  2242. )
  2243. {
  2244. PPARTITIONED_DISK pDisk;
  2245. PDISK_REGION pRegion,pRegionPrev,*pRegionHead,pRegionNext;
  2246. BOOLEAN InExtended;
  2247. PON_DISK_PTE pte;
  2248. PMBR_INFO pEbrPrev,pEbr;
  2249. ULONG i,j;
  2250. PHARD_DISK pHardDisk;
  2251. ULONG PartitionOrdinal;
  2252. NTSTATUS Status;
  2253. HANDLE Handle;
  2254. #ifdef GPT_PARTITION_ENGINE
  2255. if (SPPT_IS_GPT_DISK(DiskNumber))
  2256. return SpPtnDelete(DiskNumber, StartSector);
  2257. #endif
  2258. //
  2259. // First try to look up this region in the extended partition.
  2260. // If we can find it, assume it's a logical drive.
  2261. //
  2262. pDisk = &PartitionedDisks[DiskNumber];
  2263. pRegion = SpPtLookupRegionByStart(pDisk,TRUE,StartSector);
  2264. if(pRegion && pRegion->PartitionedSpace) {
  2265. InExtended = TRUE;
  2266. } else {
  2267. InExtended = FALSE;
  2268. pRegion = SpPtLookupRegionByStart(pDisk,FALSE,StartSector);
  2269. }
  2270. ASSERT(pRegion);
  2271. if(!pRegion) {
  2272. return(FALSE);
  2273. }
  2274. ASSERT(pRegion->PartitionedSpace);
  2275. if(!pRegion->PartitionedSpace) {
  2276. return(FALSE);
  2277. }
  2278. //
  2279. // At this point, we dismount the volume (if it's not newly created),
  2280. // so we don't run into problems later on when we go to format
  2281. //
  2282. if(pRegion->Filesystem > FilesystemNewlyCreated) {
  2283. pHardDisk = &HardDisks[pRegion->DiskNumber];
  2284. PartitionOrdinal = SpPtGetOrdinal(pRegion, PartitionOrdinalOnDisk);
  2285. //
  2286. // Open the partition for read/write access.
  2287. // This shouldn't lock the volume so we need to lock it below.
  2288. //
  2289. Status = SpOpenPartition(
  2290. pHardDisk->DevicePath,
  2291. PartitionOrdinal,
  2292. &Handle,
  2293. TRUE
  2294. );
  2295. if(!NT_SUCCESS(Status)) {
  2296. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL,
  2297. "SETUP: SpPtDelete: unable to open %ws partition %u (%lx)\n",
  2298. pHardDisk->DevicePath,
  2299. PartitionOrdinal,
  2300. Status
  2301. ));
  2302. goto AfterDismount;
  2303. }
  2304. //
  2305. // Lock the drive.
  2306. //
  2307. Status = SpLockUnlockVolume(Handle, TRUE);
  2308. //
  2309. // We shouldn't have any file opened that would cause this volume
  2310. // to already be locked, so if we get failure (ie, STATUS_ACCESS_DENIED)
  2311. // something is really wrong. This typically indicates something is
  2312. // wrong with the hard disk that won't allow us to access it.
  2313. //
  2314. if(!NT_SUCCESS(Status)) {
  2315. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: SpPtDelete: status %lx, unable to lock drive\n", Status));
  2316. ZwClose(Handle);
  2317. goto AfterDismount;
  2318. }
  2319. //
  2320. // Dismount the drive
  2321. //
  2322. Status = SpDismountVolume(Handle);
  2323. if(!NT_SUCCESS(Status)) {
  2324. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: SpPtDelete: status %lx, unable to dismount drive\n", Status));
  2325. SpLockUnlockVolume(Handle, FALSE);
  2326. ZwClose(Handle);
  2327. goto AfterDismount;
  2328. }
  2329. //
  2330. // Unlock the drive
  2331. //
  2332. Status = SpLockUnlockVolume(Handle, FALSE);
  2333. if(!NT_SUCCESS(Status)) {
  2334. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: SpPtDelete: status %lx, unable to unlock drive\n", Status));
  2335. }
  2336. ZwClose(Handle);
  2337. }
  2338. AfterDismount:
  2339. //
  2340. // Find the previous region (ie, the one that points to this one).
  2341. //
  2342. pRegionHead = InExtended ? &pDisk->ExtendedDiskRegions : &pDisk->PrimaryDiskRegions;
  2343. if(*pRegionHead == pRegion) {
  2344. pRegionPrev = NULL;
  2345. } else {
  2346. for(pRegionPrev = *pRegionHead; pRegionPrev; pRegionPrev = pRegionPrev->Next) {
  2347. if(pRegionPrev->Next == pRegion) {
  2348. break;
  2349. }
  2350. }
  2351. }
  2352. //
  2353. // Additional processing for logical drives.
  2354. //
  2355. if(InExtended) {
  2356. //
  2357. // Locate the previous and next logical drives (if any).
  2358. //
  2359. pEbr = pRegion->MbrInfo;
  2360. for(pEbrPrev=pDisk->FirstEbrInfo.Next; pEbrPrev; pEbrPrev=pEbrPrev->Next) {
  2361. if(pEbrPrev->Next == pEbr) {
  2362. break;
  2363. }
  2364. }
  2365. //
  2366. // If there is a previous logical drive, blow aways its link
  2367. // entry, because it points to the logical drive we're deleting.
  2368. //
  2369. if(pEbrPrev) {
  2370. for(i=0; i<PTABLE_DIMENSION; i++) {
  2371. pte = &pEbrPrev->OnDiskMbr.PartitionTable[i];
  2372. if(IsContainerPartition(pte->SystemId)) {
  2373. RtlZeroMemory(pte,sizeof(ON_DISK_PTE));
  2374. pEbrPrev->Dirty[i] = TRUE;
  2375. break;
  2376. }
  2377. }
  2378. }
  2379. //
  2380. // If there is a next logical drive and a previous logical drive,
  2381. // set a new link entry in previous logical drive to point to
  2382. // the next logical drive.
  2383. //
  2384. if(pEbrPrev && pEbr->Next) {
  2385. //
  2386. // Locate the link entry in the logical drive being deleted.
  2387. //
  2388. for(i=0; i<PTABLE_DIMENSION; i++) {
  2389. if(IsContainerPartition(pEbr->OnDiskMbr.PartitionTable[i].SystemId)) {
  2390. //
  2391. // Locate an empty slot in the previous logical drive's boot record
  2392. // and copy the link entry
  2393. //
  2394. for(j=0; j<PTABLE_DIMENSION; j++) {
  2395. if(pEbrPrev->OnDiskMbr.PartitionTable[j].SystemId == PARTITION_ENTRY_UNUSED) {
  2396. //
  2397. // Copy the link entry and mark the new link entry dirty
  2398. // so it gets updated on-disk. We do this even though on the
  2399. // typical disk it will have been marked dirty above. This one here
  2400. // handles the case of a wierd situation where the type 6/7/whatever
  2401. // is in slot 0 and the link entry was in slot 2 or 3. In that case,
  2402. // the RtlZeroMemory code above will have cleaned out a slot that is
  2403. // different than the one we're using here for the new link entry.
  2404. //
  2405. RtlMoveMemory(
  2406. &pEbrPrev->OnDiskMbr.PartitionTable[j],
  2407. &pEbr->OnDiskMbr.PartitionTable[i],
  2408. sizeof(ON_DISK_PTE)
  2409. );
  2410. pEbrPrev->Dirty[j] = TRUE;
  2411. break;
  2412. }
  2413. }
  2414. break;
  2415. }
  2416. }
  2417. }
  2418. //
  2419. // Remove the EBR for this logical drive.
  2420. //
  2421. if(pEbrPrev) {
  2422. pEbrPrev->Next = pEbr->Next;
  2423. } else {
  2424. ASSERT(pDisk->FirstEbrInfo.Next == pEbr);
  2425. pDisk->FirstEbrInfo.Next = pEbr->Next;
  2426. }
  2427. SpMemFree(pEbr);
  2428. } else {
  2429. ASSERT(pRegion->MbrInfo == &pDisk->MbrInfo);
  2430. pte = &pRegion->MbrInfo->OnDiskMbr.PartitionTable[pRegion->TablePosition];
  2431. ASSERT(pte->SystemId != PARTITION_ENTRY_UNUSED);
  2432. //
  2433. // Mark the entry dirty in the MBR.
  2434. //
  2435. pDisk->MbrInfo.Dirty[pRegion->TablePosition] = TRUE;
  2436. //
  2437. // If this is the extended partition, verify that it is empty.
  2438. //
  2439. if(IsContainerPartition(pte->SystemId)) {
  2440. ASSERT(pDisk->ExtendedDiskRegions);
  2441. ASSERT(pDisk->ExtendedDiskRegions->PartitionedSpace == FALSE);
  2442. ASSERT(pDisk->ExtendedDiskRegions->Next == NULL);
  2443. ASSERT(pDisk->FirstEbrInfo.Next == NULL);
  2444. if(pDisk->ExtendedDiskRegions->Next || pDisk->FirstEbrInfo.Next) {
  2445. return(FALSE);
  2446. }
  2447. //
  2448. // Free the single disk region that covers the entire extended partition.
  2449. //
  2450. SpMemFree(pDisk->ExtendedDiskRegions);
  2451. pDisk->ExtendedDiskRegions = NULL;
  2452. }
  2453. //
  2454. // Adjust the PTE for this partition by zeroing it out.
  2455. //
  2456. RtlZeroMemory(pte,sizeof(ON_DISK_PTE));
  2457. }
  2458. //
  2459. // Adjust fields in the region to describe this space as free.
  2460. //
  2461. pRegion->MbrInfo->ZapBootSector[pRegion->TablePosition] = FALSE;
  2462. pRegion->PartitionedSpace = FALSE;
  2463. pRegion->MbrInfo = NULL;
  2464. pRegion->TablePosition = 0;
  2465. pRegion->DriveLetter = L'\0';
  2466. //
  2467. // If previous region is free space, coalesce it and the region
  2468. // we just made free.
  2469. //
  2470. if(pRegionPrev && !pRegionPrev->PartitionedSpace) {
  2471. PDISK_REGION p;
  2472. ASSERT(pRegionPrev->StartSector + pRegionPrev->SectorCount == pRegion->StartSector);
  2473. pRegion->SectorCount = pRegion->StartSector + pRegion->SectorCount - pRegionPrev->StartSector;
  2474. pRegion->StartSector = pRegionPrev->StartSector;
  2475. //
  2476. // Delete the previous region.
  2477. //
  2478. if(pRegionPrev == *pRegionHead) {
  2479. //
  2480. // The previous region was the first region.
  2481. //
  2482. *pRegionHead = pRegion;
  2483. } else {
  2484. for(p = *pRegionHead; p; p=p->Next) {
  2485. if(p->Next == pRegionPrev) {
  2486. ASSERT(p->PartitionedSpace);
  2487. p->Next = pRegion;
  2488. break;
  2489. }
  2490. }
  2491. }
  2492. SpMemFree(pRegionPrev);
  2493. }
  2494. //
  2495. // If the next region is free space, coalesce it and the region
  2496. // we just made free.
  2497. //
  2498. if((pRegionNext = pRegion->Next) && !pRegionNext->PartitionedSpace) {
  2499. ASSERT(pRegion->StartSector + pRegion->SectorCount == pRegionNext->StartSector);
  2500. pRegion->SectorCount = pRegionNext->StartSector + pRegionNext->SectorCount - pRegion->StartSector;
  2501. //
  2502. // Delete the next region.
  2503. //
  2504. pRegion->Next = pRegionNext->Next;
  2505. SpMemFree(pRegionNext);
  2506. }
  2507. SpPtCommitChanges(DiskNumber,(PUCHAR)&i);
  2508. //
  2509. // Adjust the partition ordinals on this disk.
  2510. //
  2511. SpPtAssignOrdinals(pDisk,FALSE,FALSE,FALSE);
  2512. //
  2513. // No need to reassign drive letters
  2514. //
  2515. return(TRUE);
  2516. }
  2517. #endif // !NEW_PARTITION_ENGINE
  2518. BOOLEAN
  2519. SpPtExtend(
  2520. IN PDISK_REGION Region,
  2521. IN ULONGLONG SizeMB OPTIONAL
  2522. )
  2523. /*++
  2524. Routine Description:
  2525. Extends a partition by claiming any free space immedately following it
  2526. on the disk. The end boundary of the existing partition is adjusted
  2527. so that the partition encompasses the free space.
  2528. The partition may not be the extended partition and it may not be
  2529. a logical drive within the extended partition.
  2530. Note that no filesystem structures are manipulated or examined by
  2531. this routine. Essentially it deals only with the partition table entry.
  2532. Arguments:
  2533. Region - supplies the region descriptor for the partition to be
  2534. extended. That partition must not be the extended partition and
  2535. it cannot be a logical drive either.
  2536. SizeMB - if specified, indicates the size in MB by which the partition
  2537. will grow. If not specified, the partition grows to encompass all
  2538. the free space in the adjacent free space.
  2539. Return Value:
  2540. Boolean value indicating whether anything actually changed.
  2541. --*/
  2542. {
  2543. PDISK_REGION NextRegion;
  2544. PPARTITIONED_DISK pDisk;
  2545. PMBR_INFO pBrInfo;
  2546. PON_DISK_PTE pte;
  2547. ULONG BytesPerSector;
  2548. ULONGLONG NewEndSector;
  2549. ULONGLONG SectorCount;
  2550. PVOID UnalignedBuffer;
  2551. PON_DISK_MBR AlignedBuffer;
  2552. HANDLE Handle;
  2553. NTSTATUS Status;
  2554. //
  2555. // We aren't going to support this anymore on NT5. It's too messy.
  2556. //
  2557. return FALSE;
  2558. /*
  2559. pDisk = &PartitionedDisks[Region->DiskNumber];
  2560. BytesPerSector = pDisk->HardDisk->Geometry.BytesPerSector;
  2561. ASSERT(Region->PartitionedSpace);
  2562. if(!Region->PartitionedSpace) {
  2563. return(FALSE);
  2564. }
  2565. pBrInfo = Region->MbrInfo;
  2566. pte = &pBrInfo->OnDiskMbr.PartitionTable[Region->TablePosition];
  2567. //
  2568. // Make sure it's not the extended partition and is not
  2569. // in the extended partition.
  2570. //
  2571. if(pBrInfo->OnDiskSector || IsContainerPartition(pte->SystemId)) {
  2572. return(FALSE);
  2573. }
  2574. //
  2575. // If there's no next region then there's nothing to do.
  2576. // If there is a next region make sure it's empty.
  2577. //
  2578. NextRegion = Region->Next;
  2579. if(!NextRegion) {
  2580. return(FALSE);
  2581. }
  2582. if(NextRegion->PartitionedSpace) {
  2583. return(FALSE);
  2584. }
  2585. //
  2586. // Convert the passed in size to a sector count.
  2587. //
  2588. if(SizeMB) {
  2589. SectorCount = SizeMB * ((1024*1024)/BytesPerSector);
  2590. if(SectorCount > NextRegion->SectorCount) {
  2591. SectorCount = NextRegion->SectorCount;
  2592. }
  2593. } else {
  2594. SectorCount = NextRegion->SectorCount;
  2595. }
  2596. //
  2597. // Claim the part of the free region we need and align the ending sector
  2598. // to a cylinder boundary.
  2599. //
  2600. NewEndSector = NextRegion->StartSector + SectorCount;
  2601. NewEndSector -= NewEndSector % pDisk->HardDisk->SectorsPerCylinder;
  2602. //
  2603. // Fix up the size and end CHS fields in the partition table entry
  2604. // for the partition.
  2605. //
  2606. U_ULONG(pte->SectorCount) = NewEndSector - Region->StartSector;
  2607. SpPtInitializeCHSFields(
  2608. pDisk->HardDisk,
  2609. Region->StartSector,
  2610. NewEndSector - Region->StartSector,
  2611. pte
  2612. );
  2613. //pBrInfo->Dirty[Region->TablePosition] = TRUE;
  2614. //
  2615. // If there is space left over at the end of the free region
  2616. // we just stuck onto the end of the existing partition,
  2617. // adjust the free region's descriptor. Else get rid of it.
  2618. //
  2619. if(NextRegion->StartSector + NextRegion->SectorCount == NewEndSector) {
  2620. Region->Next = NextRegion->Next;
  2621. SpMemFree(NextRegion);
  2622. } else {
  2623. NextRegion->SectorCount = NextRegion->StartSector + NextRegion->SectorCount - NewEndSector;
  2624. NextRegion->StartSector = NewEndSector;
  2625. }
  2626. //
  2627. // Now we have to something tricky. We don't want to inform the disk driver
  2628. // about what we just did because he will delete the device object for
  2629. // the partition, which causes problems the next time we hit the disk, say to
  2630. // page in part of usetup.exe to get a message. We whack the partition table
  2631. // entry directly, knowing that a) we've been called after SpPtCommitChanges
  2632. // and b) no one cares about the new size until after we've rebooted.
  2633. //
  2634. UnalignedBuffer = SpMemAlloc(2*BytesPerSector);
  2635. AlignedBuffer = ALIGN(UnalignedBuffer,BytesPerSector);
  2636. Status = SpOpenPartition0(pDisk->HardDisk->DevicePath,&Handle,TRUE);
  2637. if(NT_SUCCESS(Status)) {
  2638. Status = SpReadWriteDiskSectors(Handle,0,1,BytesPerSector,AlignedBuffer,FALSE);
  2639. if(NT_SUCCESS(Status)) {
  2640. if(!IsNEC_98) {
  2641. RtlMoveMemory(
  2642. &AlignedBuffer->PartitionTable[Region->TablePosition],
  2643. &Region->MbrInfo->OnDiskMbr.PartitionTable[Region->TablePosition],
  2644. sizeof(ON_DISK_PTE)
  2645. );
  2646. } else {
  2647. PREAL_DISK_MBR pRealBuffer = (PREAL_DISK_MBR)AlignedBuffer;
  2648. ASSERT(pDisk->HardDisk->FormatType == DISK_FORMAT_TYPE_PCAT);
  2649. SpTranslatePteInfo(
  2650. &Region->MbrInfo->OnDiskMbr.PartitionTable[Region->TablePosition],
  2651. &pRealBuffer->PartitionTable[Region->TablePosition],
  2652. TRUE
  2653. );
  2654. }
  2655. Status = SpReadWriteDiskSectors(Handle,0,1,BytesPerSector,AlignedBuffer,TRUE);
  2656. if(!NT_SUCCESS(Status)) {
  2657. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: SpPtExtend: can't write sector 0, status %lx",Status));
  2658. }
  2659. } else {
  2660. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: SpPtExtend: can't read sector 0, status %lx",Status));
  2661. }
  2662. ZwClose(Handle);
  2663. } else {
  2664. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: SpPtExtend: can't open disk, status %lx",Status));
  2665. }
  2666. SpMemFree(UnalignedBuffer);
  2667. if(!NT_SUCCESS(Status)) {
  2668. FatalPartitionUpdateError(pDisk->HardDisk->Description);
  2669. }
  2670. return(TRUE);
  2671. */
  2672. }
  2673. VOID
  2674. SpPtMarkActive(
  2675. IN ULONG TablePosition
  2676. )
  2677. /*++
  2678. Routine Description:
  2679. Mark a partition on drive 0 active, and deactivate all others.
  2680. Arguments:
  2681. TablePosition - supplies offset within partition table (0-3)
  2682. of the partition entry to be activated.
  2683. Return Value:
  2684. None.
  2685. --*/
  2686. {
  2687. ULONG i;
  2688. PON_DISK_PTE pte;
  2689. ULONG Disk0Ordinal;
  2690. ASSERT(TablePosition < PTABLE_DIMENSION);
  2691. Disk0Ordinal = SpDetermineDisk0();
  2692. //
  2693. // Deactivate all others.
  2694. //
  2695. for(i=0; i<PTABLE_DIMENSION; i++) {
  2696. pte = &PartitionedDisks[Disk0Ordinal].MbrInfo.OnDiskMbr.PartitionTable[i];
  2697. if((pte->SystemId != PARTITION_ENTRY_UNUSED)
  2698. && pte->ActiveFlag
  2699. && (i != TablePosition))
  2700. {
  2701. pte->ActiveFlag = 0;
  2702. PartitionedDisks[0].MbrInfo.Dirty[i] = TRUE;
  2703. }
  2704. }
  2705. //
  2706. // Activate the one we want to activate.
  2707. //
  2708. pte = &PartitionedDisks[Disk0Ordinal].MbrInfo.OnDiskMbr.PartitionTable[TablePosition];
  2709. ASSERT(pte->SystemId != PARTITION_ENTRY_UNUSED);
  2710. ASSERT(!IsContainerPartition(pte->SystemId));
  2711. // @mtp - Original ASSERT(( PartitionNameIds[pte->SystemId] == (UCHAR)(-1)) || (pte->SystemId == PARTITION_LDM));
  2712. ASSERT((PartitionNameIds[pte->SystemId] == (UCHAR)(-1)) || (pte->SystemId == PARTITION_LDM) ||
  2713. ( SpDrEnabled() &&
  2714. IsRecognizedPartition(pte->SystemId) &&
  2715. ( ((pte->SystemId & VALID_NTFT) == VALID_NTFT ) ||
  2716. ((pte->SystemId & PARTITION_NTFT) == PARTITION_NTFT)
  2717. )
  2718. )
  2719. );
  2720. if(!pte->ActiveFlag) {
  2721. pte->ActiveFlag = 0x80;
  2722. PartitionedDisks[Disk0Ordinal].MbrInfo.Dirty[TablePosition] = TRUE;
  2723. }
  2724. }
  2725. #ifndef NEW_PARTITION_ENGINE
  2726. NTSTATUS
  2727. SpPtCommitChanges(
  2728. IN ULONG DiskNumber,
  2729. OUT PBOOLEAN AnyChanges
  2730. )
  2731. {
  2732. PPARTITIONED_DISK pDisk;
  2733. ULONG DiskLayoutSize;
  2734. PDISK_REGION pRegion;
  2735. PMBR_INFO pBrInfo;
  2736. ULONG BootRecordCount;
  2737. BOOLEAN NeedDummyEbr;
  2738. PDRIVE_LAYOUT_INFORMATION DriveLayout;
  2739. PPARTITION_INFORMATION PartitionInfo;
  2740. ULONG PartitionEntry;
  2741. ULONG bps;
  2742. PON_DISK_PTE pte;
  2743. ULONGLONG ExtendedStart;
  2744. ULONGLONG Offset;
  2745. NTSTATUS Status;
  2746. HANDLE Handle;
  2747. IO_STATUS_BLOCK IoStatusBlock;
  2748. ULONG i;
  2749. ULONGLONG ZapSector;
  2750. PUCHAR Buffer,UBuffer;
  2751. ULONG NewSig;
  2752. ULONGLONG RewriteSector[PTABLE_DIMENSION]; //NEC98
  2753. ULONG cnt,RewriteCnt=0; //NEC98
  2754. #ifdef GPT_PARTITION_ENGINE
  2755. if (SPPT_IS_GPT_DISK(DiskNumber))
  2756. return SpPtnCommitChanges(DiskNumber, AnyChanges);
  2757. #endif
  2758. ASSERT(DiskNumber < HardDiskCount);
  2759. pDisk = &PartitionedDisks[DiskNumber];
  2760. *AnyChanges = FALSE;
  2761. bps = pDisk->HardDisk->Geometry.BytesPerSector;
  2762. ExtendedStart = 0;
  2763. //
  2764. // Determine the number of boot records that will used on this disk.
  2765. // There is one for the MBR, and one for each logical drive.
  2766. //
  2767. BootRecordCount = 1;
  2768. for(pRegion=pDisk->ExtendedDiskRegions; pRegion; pRegion=pRegion->Next) {
  2769. if(pRegion->PartitionedSpace) {
  2770. BootRecordCount++;
  2771. }
  2772. }
  2773. if (IsNEC_98) { //NEC98
  2774. ZapSector = 0;
  2775. #if defined(NEC_98) //NEC98
  2776. //
  2777. // Set RealDiskPosition. This value will be valid after changing partition.
  2778. //
  2779. for(i=0,pRegion=pDisk->PrimaryDiskRegions; pRegion; pRegion=pRegion->Next) {
  2780. if(pRegion->PartitionedSpace) {
  2781. pRegion->MbrInfo->OnDiskMbr.PartitionTable[pRegion->TablePosition].RealDiskPosition = (UCHAR)i;
  2782. i++;
  2783. }
  2784. }
  2785. #endif //NEC98
  2786. } //NEC98
  2787. //
  2788. // Determine whether a dummy boot record is rquired at the start
  2789. // of the extended partition. This is the case when there is free
  2790. // space at its start.
  2791. //
  2792. if(pDisk->ExtendedDiskRegions
  2793. && !pDisk->ExtendedDiskRegions->PartitionedSpace
  2794. && pDisk->ExtendedDiskRegions->Next)
  2795. {
  2796. NeedDummyEbr = TRUE;
  2797. BootRecordCount++;
  2798. *AnyChanges = TRUE;
  2799. } else {
  2800. NeedDummyEbr = FALSE;
  2801. }
  2802. //
  2803. // Allocate a disk layout structure whose size is based on the
  2804. // number of boot records. This assumes that the structure contains
  2805. // one partition information structure in its definition.
  2806. //
  2807. DiskLayoutSize = sizeof(DRIVE_LAYOUT_INFORMATION)
  2808. + (BootRecordCount * PTABLE_DIMENSION * sizeof(PARTITION_INFORMATION))
  2809. - sizeof(PARTITION_INFORMATION);
  2810. DriveLayout = SpMemAlloc(DiskLayoutSize);
  2811. ASSERT(DriveLayout);
  2812. RtlZeroMemory(DriveLayout,DiskLayoutSize);
  2813. //
  2814. // Set up some of the fields of the drive layout structure.
  2815. //
  2816. DriveLayout->PartitionCount =
  2817. (!IsNEC_98) ? (BootRecordCount * sizeof(PTABLE_DIMENSION))
  2818. : (BootRecordCount * PTABLE_DIMENSION); //NEC98
  2819. //
  2820. // Go through each boot record and initialize the matching
  2821. // partition information structure in the drive layout structure.
  2822. //
  2823. for(PartitionEntry=0,pBrInfo=&pDisk->MbrInfo; pBrInfo; pBrInfo=pBrInfo->Next) {
  2824. for(i=0; i<PTABLE_DIMENSION; i++) {
  2825. pBrInfo->UserData[i] = NULL;
  2826. }
  2827. //
  2828. // If we are going to need a dummy logical drive,
  2829. // leave space for it here.
  2830. //
  2831. if(pBrInfo == &pDisk->FirstEbrInfo) {
  2832. if(NeedDummyEbr) {
  2833. PartitionEntry += PTABLE_DIMENSION;
  2834. }
  2835. continue;
  2836. }
  2837. ASSERT(PartitionEntry < BootRecordCount*PTABLE_DIMENSION);
  2838. for(i=0; i<PTABLE_DIMENSION; i++) {
  2839. //
  2840. // Point to partition information structure within
  2841. // drive layout structure.
  2842. //
  2843. PartitionInfo = &DriveLayout->PartitionEntry[PartitionEntry+i];
  2844. //
  2845. // Transfer this partition table entry
  2846. // into the drive layout structure, field by field.
  2847. //
  2848. pte = &pBrInfo->OnDiskMbr.PartitionTable[i];
  2849. //
  2850. // If this is the extended partition, remember where it starts.
  2851. //
  2852. if((pBrInfo == &pDisk->MbrInfo)
  2853. && IsContainerPartition(pte->SystemId)
  2854. && !ExtendedStart)
  2855. {
  2856. ExtendedStart = U_ULONG(pte->RelativeSectors);
  2857. }
  2858. if(pte->SystemId != PARTITION_ENTRY_UNUSED) {
  2859. if(!IsContainerPartition(pte->SystemId)) {
  2860. pBrInfo->UserData[i] = PartitionInfo;
  2861. }
  2862. //
  2863. // Calculate starting offset. If we are within
  2864. // the extended parttion and this is a type 5 entry,
  2865. // then the relative sector field counts the number of sectors
  2866. // between the main extended partition's first sector and
  2867. // the logical drive described by this entry.
  2868. // Otherwise, the relative sectors field describes the number
  2869. // of sectors between the boot record and the actual start
  2870. // of the partition.
  2871. //
  2872. if((pBrInfo != &pDisk->MbrInfo) && IsContainerPartition(pte->SystemId)) {
  2873. ASSERT(ExtendedStart);
  2874. Offset = ExtendedStart + U_ULONG(pte->RelativeSectors);
  2875. } else {
  2876. Offset = pBrInfo->OnDiskSector + U_ULONG(pte->RelativeSectors);
  2877. }
  2878. PartitionInfo->StartingOffset.QuadPart = UInt32x32To64(Offset,bps);
  2879. //
  2880. // Calculate size.
  2881. //
  2882. PartitionInfo->PartitionLength.QuadPart = UInt32x32To64(U_ULONG(pte->SectorCount),bps);
  2883. //
  2884. // Store start offset of newly created partition to clear sector later.
  2885. //
  2886. if(IsNEC_98 && pBrInfo->Dirty[i]) {
  2887. RewriteSector[RewriteCnt++] = Offset;
  2888. }
  2889. }
  2890. //
  2891. // Other fields.
  2892. //
  2893. PartitionInfo->PartitionType = pte->SystemId;
  2894. PartitionInfo->BootIndicator = pte->ActiveFlag;
  2895. PartitionInfo->RewritePartition = pBrInfo->Dirty[i];
  2896. if(pBrInfo->Dirty[i]) {
  2897. *AnyChanges = TRUE;
  2898. }
  2899. pBrInfo->Dirty[i] = FALSE;
  2900. }
  2901. PartitionEntry += PTABLE_DIMENSION;
  2902. }
  2903. //
  2904. // If there are no changes, just return success now.
  2905. //
  2906. if(!(*AnyChanges)) {
  2907. SpMemFree(DriveLayout);
  2908. return(STATUS_SUCCESS);
  2909. }
  2910. //
  2911. // If there is free space at the start of the extended partition,
  2912. // then we need to generate a dummy boot record.
  2913. //
  2914. if(NeedDummyEbr) {
  2915. pRegion = pDisk->ExtendedDiskRegions->Next;
  2916. ASSERT(pRegion->PartitionedSpace);
  2917. ASSERT(pRegion->StartSector == pRegion->MbrInfo->OnDiskSector);
  2918. ASSERT(ExtendedStart == pDisk->ExtendedDiskRegions->StartSector);
  2919. PartitionInfo = &DriveLayout->PartitionEntry[PTABLE_DIMENSION];
  2920. PartitionInfo->StartingOffset.QuadPart = UInt32x32To64(pRegion->StartSector,bps);
  2921. PartitionInfo->PartitionLength.QuadPart = UInt32x32To64(pRegion->SectorCount,bps);
  2922. PartitionInfo->PartitionType = PARTITION_EXTENDED;
  2923. PartitionInfo->RewritePartition = TRUE;
  2924. //
  2925. // Rewrite all other entries to ensure that if there was logica drive (first in the chain)
  2926. // that was deleted, it will really go away. There won't be any effect if we overwrite a
  2927. // logical drive that didn't exist
  2928. //
  2929. for( i = 1; i < PTABLE_DIMENSION; i ++ ) {
  2930. PartitionInfo = &DriveLayout->PartitionEntry[PTABLE_DIMENSION + i];
  2931. PartitionInfo->RewritePartition = TRUE;
  2932. }
  2933. }
  2934. //
  2935. // We now have everything set up. Open partition 0 on the disk.
  2936. //
  2937. Status = SpOpenPartition0(pDisk->HardDisk->DevicePath,&Handle,TRUE);
  2938. if(!NT_SUCCESS(Status)) {
  2939. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: committing changes, unable to open disk %u (%lx)\n",DiskNumber,Status));
  2940. SpMemFree(DriveLayout);
  2941. return(Status);
  2942. }
  2943. //
  2944. // Make sure the mbr is valid before writing the changes.
  2945. // Note that we slam in new boot code whenever any changes have been made.
  2946. // We do this to guaranteee proper support for xint13 booting, etc.
  2947. //
  2948. if (!IsNEC_98) { //NEC98
  2949. //
  2950. // If MBR of target hard disk is invalid, initialize it when select target partition.
  2951. // so don't rewrite MBR now.
  2952. //
  2953. Status = SpMasterBootCode(DiskNumber,Handle,&NewSig);
  2954. if(NT_SUCCESS(Status)) {
  2955. //
  2956. // If a new NTFT signature was generated, propagate it.
  2957. //
  2958. if(NewSig) {
  2959. U_ULONG(pDisk->MbrInfo.OnDiskMbr.NTFTSignature) = NewSig;
  2960. }
  2961. } else {
  2962. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: committing changes on disk %u, SpMasterBootCode returns %lx\n",DiskNumber,Status));
  2963. ZwClose(Handle);
  2964. SpMemFree(DriveLayout);
  2965. return(Status);
  2966. }
  2967. } //NEC98
  2968. DriveLayout->Signature = U_ULONG(pDisk->MbrInfo.OnDiskMbr.NTFTSignature);
  2969. #if 0
  2970. //
  2971. // We dump after the call to the IOCTL because it can change some of the data in the structure,
  2972. // such as PartitionNumber
  2973. //
  2974. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: Dumping DriveLayout before calling IOCTL_DISK_SET_DRIVE_LAYOUT: \n"));
  2975. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: DriveLayout->PartitionCount = %lx\n", DriveLayout->PartitionCount));
  2976. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: DriveLayout->Signature = %lx \n\n", DriveLayout->Signature));
  2977. for(i = 0; i < DriveLayout->PartitionCount; i++) {
  2978. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].StartingOffset = 0x%08lx%08lx\n", i, DriveLayout->PartitionEntry[i].StartingOffset.u.HighPart, DriveLayout->PartitionEntry[i].StartingOffset.u.LowPart));
  2979. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].PartitionLength = 0x%08lx%08lx\n", i, DriveLayout->PartitionEntry[i].PartitionLength.u.HighPart, DriveLayout->PartitionEntry[i].PartitionLength.u.LowPart));
  2980. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].HiddenSectors = 0x%08lx\n", i, DriveLayout->PartitionEntry[i].HiddenSectors));
  2981. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].PartitionNumber = %d\n", i, DriveLayout->PartitionEntry[i].PartitionNumber));
  2982. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].PartitionType = 0x%02x\n", i, DriveLayout->PartitionEntry[i].PartitionType));
  2983. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].BootIndicator = %ls\n", i, DriveLayout->PartitionEntry[i].BootIndicator? L"TRUE" : L"FALSE"));
  2984. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].RecognizedPartition = %ls\n", i, DriveLayout->PartitionEntry[i].RecognizedPartition? L"TRUE" : L"FALSE"));
  2985. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].RewritePartition = %ls\n\n", i, DriveLayout->PartitionEntry[i].RewritePartition? L"TRUE" : L"FALSE"));
  2986. }
  2987. #endif
  2988. //
  2989. // Write the changes.
  2990. //
  2991. Status = ZwDeviceIoControlFile(
  2992. Handle,
  2993. NULL,
  2994. NULL,
  2995. NULL,
  2996. &IoStatusBlock,
  2997. IOCTL_DISK_SET_DRIVE_LAYOUT,
  2998. DriveLayout,
  2999. DiskLayoutSize,
  3000. DriveLayout,
  3001. DiskLayoutSize
  3002. );
  3003. // Deferred freeing memory till later on because we still need info in this structure (lonnym)
  3004. // SpMemFree(DriveLayout);
  3005. if(!NT_SUCCESS(Status)) {
  3006. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: committing changes on disk %u, ioctl returns %lx\n",DiskNumber,Status));
  3007. SpMemFree(DriveLayout);
  3008. ZwClose(Handle);
  3009. return(Status);
  3010. }
  3011. #if 0
  3012. //
  3013. // We dump after the call to the IOCTL because it can change some of the data in the structure,
  3014. // such as PartitionNumber
  3015. //
  3016. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: Dumping DriveLayout after IOCTL_DISK_SET_DRIVE_LAYOUT was called: \n"));
  3017. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: DriveLayout->PartitionCount = %lx\n", DriveLayout->PartitionCount));
  3018. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: DriveLayout->Signature = %lx \n\n", DriveLayout->Signature));
  3019. for(i = 0; i < DriveLayout->PartitionCount; i++) {
  3020. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].StartingOffset = 0x%08lx%08lx\n", i, DriveLayout->PartitionEntry[i].StartingOffset.u.HighPart, DriveLayout->PartitionEntry[i].StartingOffset.u.LowPart));
  3021. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].PartitionLength = 0x%08lx%08lx\n", i, DriveLayout->PartitionEntry[i].PartitionLength.u.HighPart, DriveLayout->PartitionEntry[i].PartitionLength.u.LowPart));
  3022. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].HiddenSectors = 0x%08lx\n", i, DriveLayout->PartitionEntry[i].HiddenSectors));
  3023. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].PartitionNumber = %d\n", i, DriveLayout->PartitionEntry[i].PartitionNumber));
  3024. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].PartitionType = 0x%02x\n", i, DriveLayout->PartitionEntry[i].PartitionType));
  3025. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].BootIndicator = %ls\n", i, DriveLayout->PartitionEntry[i].BootIndicator? L"TRUE" : L"FALSE"));
  3026. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].RecognizedPartition = %ls\n", i, DriveLayout->PartitionEntry[i].RecognizedPartition? L"TRUE" : L"FALSE"));
  3027. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: DriveLayout->PartitionEntry[%d].RewritePartition = %ls\n\n", i, DriveLayout->PartitionEntry[i].RewritePartition? L"TRUE" : L"FALSE"));
  3028. }
  3029. #endif
  3030. //
  3031. // Allocate a buffer for zapping.
  3032. //
  3033. UBuffer = SpMemAlloc(2*bps);
  3034. ASSERT(UBuffer);
  3035. Buffer = ALIGN(UBuffer,bps);
  3036. RtlZeroMemory(Buffer,bps);
  3037. if (IsNEC_98) { //NEC98
  3038. //
  3039. // Clear 1st sector of target partition.
  3040. //
  3041. for(cnt = 0; cnt < RewriteCnt; cnt++){
  3042. Status = SpReadWriteDiskSectors(Handle,
  3043. RewriteSector[cnt],
  3044. 1,
  3045. bps,
  3046. Buffer,
  3047. TRUE);
  3048. if(!NT_SUCCESS(Status)) {
  3049. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: clear sector %lx on disk %u returned %lx\n",ZapSector,DiskNumber,Status));
  3050. SpMemFree(DriveLayout);
  3051. SpMemFree(UBuffer);
  3052. ZwClose(Handle);
  3053. return(Status);
  3054. }
  3055. }
  3056. } //NEC98
  3057. for(pBrInfo=&pDisk->MbrInfo; pBrInfo; pBrInfo=pBrInfo->Next) {
  3058. for(i=0; i<PTABLE_DIMENSION; i++) {
  3059. //
  3060. // Update current partition ordinals.
  3061. //
  3062. if (IsNEC_98) {
  3063. pte = &pBrInfo->OnDiskMbr.PartitionTable[i];
  3064. }
  3065. if ((!IsNEC_98) ? (pBrInfo->UserData[i]) :
  3066. (PVOID)(pte->SystemId != PARTITION_ENTRY_UNUSED)) { //NEC98
  3067. #if defined(NEC_98) //NEC98
  3068. PartitionInfo = (!IsNEC_98) ? (PPARTITION_INFORMATION)pBrInfo->UserData[i] :
  3069. &DriveLayout->PartitionEntry[pte->RealDiskPosition]; //NEC98
  3070. #else
  3071. PartitionInfo = (PPARTITION_INFORMATION)pBrInfo->UserData[i];
  3072. #endif
  3073. //
  3074. // The partition ordinal better be non-0!
  3075. //
  3076. if(PartitionInfo->PartitionNumber) {
  3077. //
  3078. // Update current partition ordinal.
  3079. //
  3080. pBrInfo->CurrentOrdinals[i] = (USHORT)PartitionInfo->PartitionNumber;
  3081. } else {
  3082. SpBugCheck(
  3083. SETUP_BUGCHECK_PARTITION,
  3084. PARTITIONBUG_A,
  3085. DiskNumber,
  3086. pBrInfo->CurrentOrdinals[i]
  3087. );
  3088. }
  3089. }
  3090. if (!IsNEC_98) { //NEC98
  3091. //
  3092. // If there were any newly created partitions in this boot record,
  3093. // zap their filesystem boot sectors.
  3094. //
  3095. if(pBrInfo->ZapBootSector[i]) {
  3096. //
  3097. // We shouldn't be zapping any partitions that don't exist.
  3098. //
  3099. ASSERT(pBrInfo->OnDiskMbr.PartitionTable[i].SystemId != PARTITION_ENTRY_UNUSED);
  3100. //
  3101. // This calculation is correct for partitions and logical drives.
  3102. //
  3103. ZapSector = pBrInfo->OnDiskSector
  3104. + U_ULONG(pBrInfo->OnDiskMbr.PartitionTable[i].RelativeSectors);
  3105. //
  3106. // The consequences for messing up here are so huge that a special check
  3107. // is warranted to make sure we're not clobbering the MBR.
  3108. //
  3109. ASSERT(ZapSector);
  3110. if(ZapSector) {
  3111. Status = SpReadWriteDiskSectors(
  3112. Handle,
  3113. ZapSector,
  3114. 1,
  3115. bps,
  3116. Buffer,
  3117. TRUE
  3118. );
  3119. } else {
  3120. Status = STATUS_SUCCESS;
  3121. }
  3122. if(!NT_SUCCESS(Status)) {
  3123. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: zapping sector %lx on disk %u returned %lx\n",ZapSector,DiskNumber,Status));
  3124. SpMemFree(DriveLayout);
  3125. SpMemFree(UBuffer);
  3126. ZwClose(Handle);
  3127. return(Status);
  3128. }
  3129. pBrInfo->ZapBootSector[i] = FALSE;
  3130. }
  3131. } //NEC98
  3132. }
  3133. }
  3134. SpMemFree(UBuffer);
  3135. ZwClose(Handle);
  3136. //
  3137. // Reassign on-disk ordinals (but not original ones).
  3138. //
  3139. SpPtAssignOrdinals(pDisk,FALSE,TRUE,FALSE);
  3140. if (IsNEC_98) { //NEC98
  3141. //
  3142. // If newly created partition's position is before existing partition,
  3143. // OnDiskOrdinals is not equal number of volume infomation position on NEC98
  3144. //
  3145. SpReassignOnDiskOrdinals(pDisk);
  3146. } //NEC98
  3147. SpMemFree(DriveLayout);
  3148. return(STATUS_SUCCESS);
  3149. }
  3150. #endif // ! NEW_PARTITION_ENGINE
  3151. NTSTATUS
  3152. SpMasterBootCode(
  3153. IN ULONG DiskNumber,
  3154. IN HANDLE Partition0Handle,
  3155. OUT PULONG NewNTFTSignature
  3156. )
  3157. /*++
  3158. Routine Description:
  3159. Write new master boot code onto a drive.
  3160. If the mbr has a valid signature, the existing partition table
  3161. and NTFT signature are preserved. Otherwise the partition table
  3162. is zeroed out and a new ntft signature is generated.
  3163. Arguments:
  3164. DiskNumber - supplies 0-based system ordinal for the disk.
  3165. Partition0Handle - supplies an open handle for partition 0 on
  3166. the disk. The handle must have read and write access.
  3167. NewNTFTSignature - receives a value indicating the new NTFT signature,
  3168. if one was generated and written to the disk. If 0 is received,
  3169. then a new ntft signature was not generated and written.
  3170. Return Value:
  3171. NT Status code indicating outcome.
  3172. --*/
  3173. {
  3174. NTSTATUS Status;
  3175. ULONG BytesPerSector;
  3176. PUCHAR Buffer;
  3177. ULONG SectorCount;
  3178. PON_DISK_MBR Mbr;
  3179. BytesPerSector = HardDisks[DiskNumber].Geometry.BytesPerSector;
  3180. SectorCount = max(1,sizeof(ON_DISK_MBR)/BytesPerSector);
  3181. *NewNTFTSignature = 0;
  3182. //
  3183. // Allocate and align a buffer.
  3184. //
  3185. Buffer = SpMemAlloc(2 * SectorCount * BytesPerSector);
  3186. Mbr = ALIGN(Buffer,BytesPerSector);
  3187. //
  3188. // Read mbr
  3189. //
  3190. Status = SpReadWriteDiskSectors(
  3191. Partition0Handle,
  3192. (HardDisks[DiskNumber].Int13Hooker == HookerEZDrive) ? 1 : 0,
  3193. SectorCount,
  3194. BytesPerSector,
  3195. Mbr,
  3196. FALSE
  3197. );
  3198. if(NT_SUCCESS(Status)) {
  3199. if(U_USHORT(Mbr->AA55Signature) == MBR_SIGNATURE) {
  3200. //
  3201. // Valid. Slam in new boot code if there's no int13 hooker.
  3202. //
  3203. if(HardDisks[DiskNumber].Int13Hooker == NoHooker) {
  3204. ASSERT(&((PON_DISK_MBR)0)->BootCode == 0);
  3205. RtlMoveMemory(Mbr,x86BootCode,sizeof(Mbr->BootCode));
  3206. Status = SpReadWriteDiskSectors(
  3207. Partition0Handle,
  3208. 0,
  3209. SectorCount,
  3210. BytesPerSector,
  3211. Mbr,
  3212. TRUE
  3213. );
  3214. }
  3215. } else {
  3216. //
  3217. // Invalid. Construct a boot sector.
  3218. //
  3219. ASSERT(X86BOOTCODE_SIZE == sizeof(ON_DISK_MBR));
  3220. RtlMoveMemory(Mbr,x86BootCode,X86BOOTCODE_SIZE);
  3221. *NewNTFTSignature = SpComputeSerialNumber();
  3222. U_ULONG(Mbr->NTFTSignature) = *NewNTFTSignature;
  3223. U_USHORT(Mbr->AA55Signature) = MBR_SIGNATURE;
  3224. //
  3225. // Write the sector(s).
  3226. //
  3227. Status = SpReadWriteDiskSectors(
  3228. Partition0Handle,
  3229. (HardDisks[DiskNumber].Int13Hooker == HookerEZDrive) ? 1 : 0,
  3230. SectorCount,
  3231. BytesPerSector,
  3232. Mbr,
  3233. TRUE
  3234. );
  3235. if (NT_SUCCESS(Status)) {
  3236. PHARD_DISK Disk = SPPT_GET_HARDDISK(DiskNumber);
  3237. Disk->Signature = Disk->DriveLayout.Mbr.Signature = *NewNTFTSignature;
  3238. }
  3239. }
  3240. }
  3241. SpMemFree(Buffer);
  3242. return(Status);
  3243. }
  3244. #ifndef NEW_PARTITION_ENGINE
  3245. VOID
  3246. SpPtGetSectorLayoutInformation(
  3247. IN PDISK_REGION Region,
  3248. OUT PULONGLONG HiddenSectors,
  3249. OUT PULONGLONG VolumeSectorCount
  3250. )
  3251. /*++
  3252. Routine Description:
  3253. Given a region describing a partition or logical drive, return information
  3254. about its layout on disk appropriate for the BPB when the volume is
  3255. formatted.
  3256. Arguments:
  3257. Region - supplies a pointer to the disk region descriptor for the
  3258. partition or logical drive in question.
  3259. HiidenSectors - receives the value that should be placed in the
  3260. hidden sectors field of the BPB when the volume is formatted.
  3261. HiidenSectors - receives the value that should be placed in the
  3262. sector count field of the BPB when the volume is formatted.
  3263. Return Value:
  3264. None.
  3265. --*/
  3266. {
  3267. PON_DISK_PTE pte;
  3268. #ifdef GPT_PARTITION_ENGINE
  3269. if (SPPT_IS_GPT_DISK(Region->DiskNumber)) {
  3270. SpPtnGetSectorLayoutInformation(Region,
  3271. HiddenSectors,
  3272. VolumeSectorCount);
  3273. return;
  3274. }
  3275. #endif
  3276. ASSERT(Region->PartitionedSpace);
  3277. pte = &Region->MbrInfo->OnDiskMbr.PartitionTable[Region->TablePosition];
  3278. *HiddenSectors = U_ULONG(pte->RelativeSectors);
  3279. *VolumeSectorCount = U_ULONG(pte->SectorCount);
  3280. }
  3281. ULONG
  3282. SpPtGetOrdinal(
  3283. IN PDISK_REGION Region,
  3284. IN PartitionOrdinalType OrdinalType
  3285. )
  3286. {
  3287. ULONG ord;
  3288. #ifdef GPT_PARTITION_ENGINE
  3289. if (SPPT_IS_GPT_DISK(Region->DiskNumber))
  3290. return SpPtnGetOrdinal(Region, OrdinalType);
  3291. #endif
  3292. if(Region->PartitionedSpace && (!Region->DynamicVolume || Region->MbrInfo) ) {
  3293. //
  3294. // This is either a basic volume, or a dynamic volume that is listed on the MBR/EBR
  3295. //
  3296. switch(OrdinalType) {
  3297. case PartitionOrdinalOriginal:
  3298. ord = Region->MbrInfo->OriginalOrdinals[Region->TablePosition];
  3299. break;
  3300. case PartitionOrdinalOnDisk:
  3301. ord = Region->MbrInfo->OnDiskOrdinals[Region->TablePosition];
  3302. break;
  3303. case PartitionOrdinalCurrent:
  3304. ord = Region->MbrInfo->CurrentOrdinals[Region->TablePosition];
  3305. break;
  3306. }
  3307. } else {
  3308. //
  3309. // Dynamic volume that is not listed on MBR or EBR
  3310. //
  3311. ord = Region->TablePosition;
  3312. }
  3313. return(ord);
  3314. }
  3315. #endif // NEW_PARTITION_ENGINE
  3316. #define MENU_LEFT_X 3
  3317. #define MENU_WIDTH (VideoVars.ScreenWidth-(2*MENU_LEFT_X))
  3318. #define MENU_INDENT 4
  3319. BOOLEAN
  3320. SpPtRegionDescription(
  3321. IN PPARTITIONED_DISK pDisk,
  3322. IN PDISK_REGION pRegion,
  3323. OUT PWCHAR Buffer,
  3324. IN ULONG BufferSize
  3325. )
  3326. {
  3327. WCHAR DriveLetter[3];
  3328. ULONGLONG RegionSizeMB;
  3329. ULONGLONG FreeSpace;
  3330. ULONG MessageId;
  3331. WCHAR TypeName[((sizeof(pRegion->TypeName)+sizeof(pRegion->VolumeLabel))/sizeof(WCHAR))+4];
  3332. BOOLEAN NewDescription = FALSE;
  3333. //
  3334. // Get the size of the region.
  3335. //
  3336. RegionSizeMB = SpPtSectorCountToMB(pDisk->HardDisk, pRegion->SectorCount);
  3337. //
  3338. // Don't show spaces smaller than 1 MB.
  3339. //
  3340. if(!RegionSizeMB) {
  3341. return(FALSE);
  3342. }
  3343. //
  3344. // Get the drive letter field, type of region, and amount of free space,
  3345. // if this is a used region.
  3346. //
  3347. if(pRegion->PartitionedSpace) {
  3348. if(pRegion->DriveLetter) {
  3349. if( pRegion->Filesystem != FilesystemFat ) {
  3350. DriveLetter[0] = pRegion->DriveLetter;
  3351. } else {
  3352. if( pRegion->NextCompressed == NULL ) {
  3353. DriveLetter[0] = pRegion->DriveLetter;
  3354. } else {
  3355. DriveLetter[0] = pRegion->HostDrive;
  3356. }
  3357. }
  3358. DriveLetter[1] = L':';
  3359. } else {
  3360. if( pRegion->Filesystem != FilesystemDoubleSpace ) {
  3361. DriveLetter[0] = L'-';
  3362. DriveLetter[1] = L'-';
  3363. } else {
  3364. DriveLetter[0] = pRegion->MountDrive;
  3365. DriveLetter[1] = L':';
  3366. }
  3367. }
  3368. DriveLetter[2] = 0;
  3369. #ifdef NEW_PARTITION_ENGINE
  3370. NewDescription = TRUE;
  3371. #endif
  3372. #ifdef GPT_PARTITION_ENGINE
  3373. if (SPPT_IS_GPT_DISK(pRegion->DiskNumber)) {
  3374. NewDescription = TRUE;
  3375. } else {
  3376. NewDescription = FALSE;
  3377. }
  3378. #endif
  3379. //
  3380. // Format the partition name
  3381. //
  3382. TypeName[0] = 0;
  3383. if (SPPT_IS_REGION_PARTITIONED(pRegion)) {
  3384. SpPtnGetPartitionName(pRegion,
  3385. TypeName,
  3386. sizeof(TypeName) / sizeof(TypeName[0]));
  3387. } else {
  3388. swprintf( TypeName,
  3389. L"\\Harddisk%u\\Partition%u",
  3390. pRegion->DiskNumber,
  3391. pRegion->PartitionNumber );
  3392. }
  3393. //
  3394. // Format the text based on whether we know the amount of free space.
  3395. //
  3396. if(pRegion->FreeSpaceKB == (ULONG)(-1)) {
  3397. SpFormatMessage(
  3398. Buffer,
  3399. BufferSize,
  3400. SP_TEXT_REGION_DESCR_2,
  3401. DriveLetter,
  3402. SplangPadString(-35,TypeName),
  3403. (ULONG)RegionSizeMB
  3404. );
  3405. } else {
  3406. ULONGLONG AuxFreeSpaceKB;
  3407. AuxFreeSpaceKB = (pRegion->IsLocalSource)? pRegion->AdjustedFreeSpaceKB :
  3408. pRegion->FreeSpaceKB;
  3409. //
  3410. // If there is less than 1 meg of free space,
  3411. // then use KB as the units for free space.
  3412. // Otherwise, use MB.
  3413. //
  3414. if(AuxFreeSpaceKB < 1024) {
  3415. MessageId = SP_TEXT_REGION_DESCR_1a;
  3416. FreeSpace = AuxFreeSpaceKB;
  3417. } else {
  3418. MessageId = SP_TEXT_REGION_DESCR_1;
  3419. FreeSpace = AuxFreeSpaceKB / 1024;
  3420. //
  3421. // Make sure we don't look bad...
  3422. //
  3423. if( FreeSpace > RegionSizeMB ) {
  3424. FreeSpace = RegionSizeMB;
  3425. }
  3426. }
  3427. SpFormatMessage(
  3428. Buffer,
  3429. BufferSize,
  3430. MessageId,
  3431. DriveLetter,
  3432. SplangPadString(-35,TypeName),
  3433. (ULONG)RegionSizeMB,
  3434. (ULONG)FreeSpace
  3435. );
  3436. }
  3437. } else {
  3438. //
  3439. // Not a used region, use a separate format string.
  3440. //
  3441. SpFormatMessage(Buffer,
  3442. BufferSize,
  3443. SP_TEXT_REGION_DESCR_3,
  3444. (ULONG)RegionSizeMB);
  3445. }
  3446. return(TRUE);
  3447. }
  3448. BOOLEAN
  3449. SpPtIterateRegionList(
  3450. IN PVOID Menu,
  3451. IN PPARTITIONED_DISK pDisk,
  3452. IN PDISK_REGION pRegion,
  3453. IN BOOLEAN InMbr,
  3454. OUT PDISK_REGION *FirstRegion
  3455. )
  3456. {
  3457. WCHAR Buffer[256];
  3458. #ifdef FULL_DOUBLE_SPACE_SUPPORT
  3459. PDISK_REGION Pointer;
  3460. #endif // FULL_DOUBLE_SPACE_SUPPORT
  3461. Buffer[0] = UNICODE_NULL;
  3462. for( ;pRegion; pRegion=pRegion->Next) {
  3463. PMBR_INFO pBrInfo = pRegion->MbrInfo;
  3464. //
  3465. // If this is the extended partition,
  3466. // iterate its contents now.
  3467. //
  3468. if(pRegion->PartitionedSpace
  3469. && IsContainerPartition(pBrInfo->OnDiskMbr.PartitionTable[pRegion->TablePosition].SystemId))
  3470. {
  3471. //
  3472. // This better be in the MBR!
  3473. //
  3474. ASSERT(InMbr);
  3475. if(!SpPtIterateRegionList(Menu,pDisk,pDisk->ExtendedDiskRegions,FALSE,FirstRegion)) {
  3476. return(FALSE);
  3477. }
  3478. } else {
  3479. //
  3480. // Format a description of this region and add it to the menu.
  3481. //
  3482. if(SpPtRegionDescription(pDisk,pRegion,Buffer,sizeof(Buffer))) {
  3483. if(*FirstRegion == NULL) {
  3484. *FirstRegion = pRegion;
  3485. }
  3486. if(!SpMnAddItem(Menu,Buffer,MENU_LEFT_X+MENU_INDENT,MENU_WIDTH-(2*MENU_INDENT),TRUE,(ULONG_PTR)pRegion)) {
  3487. return(FALSE);
  3488. }
  3489. #ifdef FULL_DOUBLE_SPACE_SUPPORT
  3490. if( ( pRegion->Filesystem == FilesystemFat ) &&
  3491. ( ( Pointer = pRegion->NextCompressed ) != NULL ) ) {
  3492. for( ; Pointer;
  3493. Pointer = Pointer->NextCompressed ) {
  3494. if(SpPtRegionDescription(pDisk,Pointer,Buffer,sizeof(Buffer))) {
  3495. if(!SpMnAddItem(Menu,Buffer,MENU_LEFT_X+MENU_INDENT,MENU_WIDTH-(2*MENU_INDENT),TRUE,(ULONG)Pointer)) {
  3496. return(FALSE);
  3497. }
  3498. }
  3499. }
  3500. }
  3501. #endif // FULL_DOUBLE_SPACE_SUPPORT
  3502. }
  3503. }
  3504. }
  3505. return(TRUE);
  3506. }
  3507. BOOLEAN
  3508. SpPtGenerateMenu(
  3509. IN PVOID Menu,
  3510. IN PPARTITIONED_DISK pDisk,
  3511. OUT PDISK_REGION *FirstRegion
  3512. )
  3513. {
  3514. WCHAR Buffer[256];
  3515. //
  3516. // Add the disk name/description.
  3517. //
  3518. if(!SpMnAddItem(Menu,pDisk->HardDisk->Description,MENU_LEFT_X,MENU_WIDTH,FALSE,0)) {
  3519. return(FALSE);
  3520. }
  3521. //
  3522. // Only add a line between the disk anme and partitions if we have space on
  3523. // the screen. Not fatal if the space can't be added.
  3524. //
  3525. if(!SplangQueryMinimizeExtraSpacing()) {
  3526. SpMnAddItem(Menu,L"",MENU_LEFT_X,MENU_WIDTH,FALSE,0);
  3527. }
  3528. //
  3529. // If the disk is off-line, add a message indicating such.
  3530. //
  3531. // Also disallow installation or create/delete partition into
  3532. // removable meida on NEC98. Because NT cannot boot from it.
  3533. //
  3534. if(pDisk->HardDisk->Status == DiskOffLine) {
  3535. SpFormatMessage(
  3536. Buffer,
  3537. sizeof(Buffer),
  3538. (pDisk->HardDisk->Characteristics & FILE_REMOVABLE_MEDIA)
  3539. ? (!IsNEC_98 ? SP_TEXT_HARD_DISK_NO_MEDIA : SP_TEXT_DISK_OFF_LINE)
  3540. : SP_TEXT_DISK_OFF_LINE
  3541. );
  3542. return(SpMnAddItem(Menu,Buffer,MENU_LEFT_X+MENU_INDENT,MENU_WIDTH-(2*MENU_INDENT),FALSE,0));
  3543. }
  3544. #if 0
  3545. else if(IsNEC_98 && (pDisk->HardDisk->Characteristics & FILE_REMOVABLE_MEDIA)) {
  3546. SpFormatMessage(Buffer,sizeof(Buffer),SP_TEXT_DISK_OFF_LINE);
  3547. return(SpMnAddItem(Menu,Buffer,MENU_LEFT_X+MENU_INDENT,MENU_WIDTH-(2*MENU_INDENT),FALSE,0));
  3548. }
  3549. #endif //0
  3550. if(!SpPtIterateRegionList(Menu,pDisk,pDisk->PrimaryDiskRegions,TRUE,FirstRegion)) {
  3551. return(FALSE);
  3552. }
  3553. return(SplangQueryMinimizeExtraSpacing() ? TRUE : SpMnAddItem(Menu,L"",MENU_LEFT_X,MENU_WIDTH,FALSE,0));
  3554. }
  3555. //
  3556. // We will change item #0 in the array below as appropriate for
  3557. // the currently highlighted region.
  3558. //
  3559. ULONG PartitionMnemonics[4] = {0};
  3560. VOID
  3561. SpPtMenuCallback(
  3562. IN ULONG_PTR UserData
  3563. )
  3564. {
  3565. PDISK_REGION pRegion = (PDISK_REGION)UserData;
  3566. //
  3567. // Don't allow deletion of the partition if the 'partition' is really
  3568. // a DoubleSpace drive.
  3569. //
  3570. if(pRegion->Filesystem == FilesystemDoubleSpace) {
  3571. PartitionMnemonics[0] = 0;
  3572. if (ConsoleRunning) {
  3573. SpDisplayStatusOptions(
  3574. DEFAULT_STATUS_ATTRIBUTE,
  3575. SP_STAT_ESC_EQUALS_CANCEL,
  3576. 0
  3577. );
  3578. } else {
  3579. SpDisplayStatusOptions(
  3580. DEFAULT_STATUS_ATTRIBUTE,
  3581. SP_STAT_ENTER_EQUALS_INSTALL,
  3582. SP_STAT_F3_EQUALS_EXIT,
  3583. 0
  3584. );
  3585. }
  3586. } else {
  3587. PHARD_DISK Disk = SPPT_GET_HARDDISK(pRegion->DiskNumber);
  3588. BOOLEAN FlipStyle = FALSE;
  3589. BOOLEAN MakeSysPart = FALSE;
  3590. FilesystemType FsType = pRegion->Filesystem;
  3591. #ifndef OLD_PARTITION_ENGINE
  3592. FlipStyle = SpPtnIsDiskStyleChangeAllowed(pRegion->DiskNumber);
  3593. #endif
  3594. PartitionMnemonics[0] = pRegion->PartitionedSpace ?
  3595. MnemonicDeletePartition : MnemonicCreatePartition;
  3596. PartitionMnemonics[1] = FlipStyle ? MnemonicChangeDiskStyle : 0;
  3597. #ifdef NEW_PARTITION_ENGINE
  3598. if (SPPT_IS_REGION_SYSTEMPARTITION(pRegion)) {
  3599. ValidArcSystemPartition = TRUE;
  3600. }
  3601. if (!ValidArcSystemPartition && !FlipStyle && SpIsArc() &&
  3602. (FsType != FilesystemNtfs) && SpPtnIsValidESPPartition(pRegion)) {
  3603. //
  3604. // Need to allow conversion to system partition
  3605. //
  3606. MakeSysPart = TRUE;
  3607. PartitionMnemonics[1] = MnemonicMakeSystemPartition;
  3608. }
  3609. #endif
  3610. if (ConsoleRunning) {
  3611. if (MakeSysPart) {
  3612. SpDisplayStatusOptions(
  3613. DEFAULT_STATUS_ATTRIBUTE,
  3614. SP_STAT_ESC_EQUALS_CANCEL,
  3615. pRegion->PartitionedSpace ?
  3616. SP_STAT_D_EQUALS_DELETE_PARTITION : SP_STAT_C_EQUALS_CREATE_PARTITION,
  3617. SP_STAT_M_EQUALS_MAKE_SYSPART,
  3618. FlipStyle ? SP_STAT_S_EQUALS_CHANGE_DISK_STYLE : 0,
  3619. 0
  3620. );
  3621. } else {
  3622. SpDisplayStatusOptions(
  3623. DEFAULT_STATUS_ATTRIBUTE,
  3624. SP_STAT_ESC_EQUALS_CANCEL,
  3625. pRegion->PartitionedSpace ?
  3626. SP_STAT_D_EQUALS_DELETE_PARTITION : SP_STAT_C_EQUALS_CREATE_PARTITION,
  3627. FlipStyle ? SP_STAT_S_EQUALS_CHANGE_DISK_STYLE : 0,
  3628. 0
  3629. );
  3630. }
  3631. } else {
  3632. if (FlipStyle) {
  3633. SpDisplayStatusOptions(
  3634. DEFAULT_STATUS_ATTRIBUTE,
  3635. SP_STAT_ENTER_EQUALS_INSTALL,
  3636. pRegion->PartitionedSpace ?
  3637. SP_STAT_D_EQUALS_DELETE_PARTITION : SP_STAT_C_EQUALS_CREATE_PARTITION,
  3638. SP_STAT_S_EQUALS_CHANGE_DISK_STYLE,
  3639. SP_STAT_F3_EQUALS_EXIT,
  3640. 0
  3641. );
  3642. } else {
  3643. if (MakeSysPart) {
  3644. SpDisplayStatusOptions(
  3645. DEFAULT_STATUS_ATTRIBUTE,
  3646. SP_STAT_ENTER_EQUALS_INSTALL,
  3647. pRegion->PartitionedSpace ?
  3648. SP_STAT_D_EQUALS_DELETE_PARTITION : SP_STAT_C_EQUALS_CREATE_PARTITION,
  3649. SP_STAT_M_EQUALS_MAKE_SYSPART,
  3650. SP_STAT_F3_EQUALS_EXIT,
  3651. 0
  3652. );
  3653. } else {
  3654. SpDisplayStatusOptions(
  3655. DEFAULT_STATUS_ATTRIBUTE,
  3656. SP_STAT_ENTER_EQUALS_INSTALL,
  3657. pRegion->PartitionedSpace ?
  3658. SP_STAT_D_EQUALS_DELETE_PARTITION : SP_STAT_C_EQUALS_CREATE_PARTITION,
  3659. SP_STAT_F3_EQUALS_EXIT,
  3660. 0
  3661. );
  3662. }
  3663. }
  3664. }
  3665. }
  3666. }
  3667. void
  3668. SpEnumerateDiskRegions(
  3669. IN PSPENUMERATEDISKREGIONS EnumRoutine,
  3670. IN ULONG_PTR Context
  3671. )
  3672. {
  3673. ULONG DiskNo;
  3674. PDISK_REGION pThisRegion;
  3675. for(DiskNo=0; (DiskNo<HardDiskCount); DiskNo++) {
  3676. for(pThisRegion=PartitionedDisks[DiskNo].PrimaryDiskRegions; pThisRegion; pThisRegion=pThisRegion->Next) {
  3677. if (!EnumRoutine( &PartitionedDisks[DiskNo], pThisRegion, Context )) {
  3678. return;
  3679. }
  3680. }
  3681. for(pThisRegion=PartitionedDisks[DiskNo].ExtendedDiskRegions; pThisRegion; pThisRegion=pThisRegion->Next) {
  3682. if (!EnumRoutine( &PartitionedDisks[DiskNo], pThisRegion, Context )) {
  3683. return;
  3684. }
  3685. }
  3686. }
  3687. }
  3688. #if DBG
  3689. void
  3690. SpPtDumpPartitionData(
  3691. void
  3692. )
  3693. {
  3694. ULONG DiskNo;
  3695. PDISK_REGION pThisRegion;
  3696. for(DiskNo=0; (DiskNo<HardDiskCount); DiskNo++) {
  3697. for(pThisRegion=PartitionedDisks[DiskNo].PrimaryDiskRegions; pThisRegion; pThisRegion=pThisRegion->Next) {
  3698. if (pThisRegion->FreeSpaceKB != -1) {
  3699. KdPrintEx(( DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: diskno=%d, sector-start=%d, sector-count=%d, free=%dKB\n",
  3700. pThisRegion->DiskNumber,
  3701. pThisRegion->StartSector,
  3702. pThisRegion->SectorCount,
  3703. pThisRegion->FreeSpaceKB
  3704. ));
  3705. }
  3706. }
  3707. }
  3708. }
  3709. #endif
  3710. #ifdef OLD_PARTITION_ENGINE
  3711. NTSTATUS
  3712. SpPtPrepareDisks(
  3713. IN PVOID SifHandle,
  3714. OUT PDISK_REGION *InstallRegion,
  3715. OUT PDISK_REGION *SystemPartitionRegion,
  3716. IN PWSTR SetupSourceDevicePath,
  3717. IN PWSTR DirectoryOnSetupSource,
  3718. IN BOOLEAN RemoteBootRepartition
  3719. )
  3720. {
  3721. PPARTITIONED_DISK pDisk;
  3722. WCHAR Buffer[256];
  3723. ULONG DiskNo;
  3724. PVOID Menu;
  3725. ULONG MenuTopY;
  3726. ULONG ValidKeys[3] = { ASCI_CR, KEY_F3, 0 };
  3727. ULONG ValidKeysCmdCons[2] = { ASCI_ESC, 0 };
  3728. ULONG Keypress;
  3729. PDISK_REGION pRegion;
  3730. PDISK_REGION FirstRegion,DefaultRegion;
  3731. BOOLEAN unattended;
  3732. BOOLEAN createdMenu;
  3733. //SpPtDumpPartitionData();
  3734. if (SpIsArc()) {
  3735. //
  3736. // Select a system partition from among those defined in NV-RAM.
  3737. //
  3738. *SystemPartitionRegion = SpPtValidSystemPartitionArc(SifHandle,
  3739. SetupSourceDevicePath,
  3740. DirectoryOnSetupSource);
  3741. (*SystemPartitionRegion)->IsSystemPartition = 2;
  3742. }
  3743. unattended = UnattendedOperation;
  3744. while(1) {
  3745. createdMenu = FALSE;
  3746. Keypress = 0;
  3747. #if defined(REMOTE_BOOT)
  3748. if (RemoteBootSetup && !RemoteInstallSetup && HardDiskCount == 0) {
  3749. //
  3750. // If there are no hard disks, allow diskless install
  3751. //
  3752. pRegion = NULL;
  3753. //
  3754. // Run through the rest of the code as if the user had just
  3755. // hit enter to select this partition.
  3756. //
  3757. Keypress = ASCI_CR;
  3758. } else
  3759. #endif // defined(REMOTE_BOOT)
  3760. if (unattended && RemoteBootRepartition) {
  3761. ULONG DiskNumber;
  3762. //
  3763. // Prepare the disk for remote boot installation. This involves
  3764. // converting disk 0 into as big a partition as possible.
  3765. //
  3766. if (*SystemPartitionRegion != NULL) {
  3767. DiskNumber = (*SystemPartitionRegion)->DiskNumber;
  3768. } else {
  3769. #ifdef _X86_
  3770. DiskNumber = SpDetermineDisk0();
  3771. #else
  3772. DiskNumber = 0;
  3773. #endif
  3774. }
  3775. if (NT_SUCCESS(SpPtPartitionDiskForRemoteBoot(DiskNumber, &pRegion))) {
  3776. SpPtRegionDescription(
  3777. &PartitionedDisks[pRegion->DiskNumber],
  3778. pRegion,
  3779. Buffer,
  3780. sizeof(Buffer)
  3781. );
  3782. //
  3783. // Run through the rest of the code as if the user had just
  3784. // hit enter to select this partition.
  3785. //
  3786. Keypress = ASCI_CR;
  3787. }
  3788. }
  3789. if (Keypress == 0) {
  3790. //
  3791. // Display the text that goes above the menu on the partitioning screen.
  3792. //
  3793. SpDisplayScreen(ConsoleRunning?SP_SCRN_PARTITION_CMDCONS:SP_SCRN_PARTITION,3,CLIENT_TOP+1);
  3794. //
  3795. // Calculate menu placement. Leave one blank line
  3796. // and one line for a frame.
  3797. //
  3798. MenuTopY = NextMessageTopLine+2;
  3799. //
  3800. // Create a menu.
  3801. //
  3802. Menu = SpMnCreate(
  3803. MENU_LEFT_X,
  3804. MenuTopY,
  3805. MENU_WIDTH,
  3806. VideoVars.ScreenHeight-MenuTopY-(SplangQueryMinimizeExtraSpacing() ? 1 : 2)-STATUS_HEIGHT
  3807. );
  3808. if(!Menu) {
  3809. return(STATUS_NO_MEMORY);
  3810. }
  3811. createdMenu = TRUE;
  3812. //
  3813. // Build up a menu of partitions and free spaces.
  3814. //
  3815. FirstRegion = NULL;
  3816. for(DiskNo=0; DiskNo<HardDiskCount; DiskNo++) {
  3817. pDisk = &PartitionedDisks[DiskNo];
  3818. if(!SpPtGenerateMenu(Menu,pDisk,&FirstRegion)) {
  3819. SpMnDestroy(Menu);
  3820. return(STATUS_NO_MEMORY);
  3821. }
  3822. }
  3823. ASSERT(FirstRegion);
  3824. //
  3825. // If this is unattended operation, try to use the local source
  3826. // region if there is one. If this fails, the user will have to
  3827. // intervene manually.
  3828. //
  3829. if(unattended &&
  3830. LocalSourceRegion &&
  3831. (!LocalSourceRegion->DynamicVolume || LocalSourceRegion->DynamicVolumeSuitableForOS)
  3832. ) {
  3833. pRegion = LocalSourceRegion;
  3834. Keypress = ASCI_CR;
  3835. } else {
  3836. pRegion = NULL;
  3837. if (AutoPartitionPicker && !ConsoleRunning
  3838. #if defined(REMOTE_BOOT)
  3839. && (!RemoteBootSetup || RemoteInstallSetup)
  3840. #endif // defined(REMOTE_BOOT)
  3841. ) {
  3842. PDISK_REGION pThisRegion;
  3843. ULONG RequiredKB = 0;
  3844. ULONG SectorNo;
  3845. ULONG pass;
  3846. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: -------------------------------------------------------------\n" ));
  3847. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: Looking for an install partition\n\n" ));
  3848. for(DiskNo=0; (DiskNo<HardDiskCount); DiskNo++) {
  3849. for( pass = 0; ((pass < 2) && (pRegion == NULL)); pass ++ ) {
  3850. for(pThisRegion= (pass == 0) ? PartitionedDisks[DiskNo].PrimaryDiskRegions : PartitionedDisks[DiskNo].ExtendedDiskRegions,SectorNo=0; pThisRegion; pThisRegion=pThisRegion->Next,SectorNo++) {
  3851. //
  3852. // Fetch the amount of free space required on the windows nt drive.
  3853. //
  3854. SpFetchDiskSpaceRequirements( SifHandle,
  3855. pThisRegion->BytesPerCluster,
  3856. &RequiredKB,
  3857. NULL);
  3858. if (SpPtDeterminePartitionGood(pThisRegion,RequiredKB,TRUE))
  3859. {
  3860. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: Selected install partition = (%d,%d),(%wc:),(%ws)\n",
  3861. DiskNo,SectorNo,pThisRegion->DriveLetter,pThisRegion->VolumeLabel));
  3862. pRegion = pThisRegion;
  3863. Keypress = ASCI_CR;
  3864. break;
  3865. }
  3866. }
  3867. }
  3868. }
  3869. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: -------------------------------------------------------------\n" ));
  3870. }
  3871. if( !pRegion ) {
  3872. //
  3873. // If there is a local source, make it the default partition.
  3874. //
  3875. DefaultRegion = (LocalSourceRegion &&
  3876. (!LocalSourceRegion->DynamicVolume || LocalSourceRegion->DynamicVolumeSuitableForOS))?
  3877. LocalSourceRegion : FirstRegion;
  3878. //
  3879. // Call the menu callback to initialize the status line.
  3880. //
  3881. SpPtMenuCallback((ULONG_PTR)DefaultRegion);
  3882. SpMnDisplay(
  3883. Menu,
  3884. (ULONG_PTR)DefaultRegion,
  3885. TRUE,
  3886. ConsoleRunning?ValidKeysCmdCons:ValidKeys,
  3887. PartitionMnemonics,
  3888. SpPtMenuCallback,
  3889. &Keypress,
  3890. (PULONG_PTR)(&pRegion)
  3891. );
  3892. }
  3893. }
  3894. }
  3895. //
  3896. // Now act on the user's selection.
  3897. //
  3898. if(Keypress & KEY_MNEMONIC) {
  3899. Keypress &= ~KEY_MNEMONIC;
  3900. }
  3901. if (IsNEC_98) { //NEC98
  3902. //
  3903. // If target hard drive has no/wrong MBR, force initialize it right now.
  3904. //
  3905. PPARTITIONED_DISK pDisk;
  3906. ULONG ValidKeysInit[] = {ASCI_ESC, 0 };
  3907. ULONG MnemonicKeysInit[] = { MnemonicInitializeDisk, 0 };
  3908. pDisk = &PartitionedDisks[pRegion->DiskNumber];
  3909. if(!(pDisk->HardDisk->Characteristics & FILE_REMOVABLE_MEDIA) &&
  3910. ((U_USHORT(pDisk->MbrInfo.OnDiskMbr.AA55Signature) != MBR_SIGNATURE) ||
  3911. (pDisk->HardDisk->FormatType != DISK_FORMAT_TYPE_NEC98)) &&
  3912. ((Keypress == MnemonicCreatePartition) ||
  3913. (Keypress == MnemonicDeletePartition) || (Keypress == ASCI_CR))) {
  3914. //SpDisplayScreen(SP_SCRN_INIT_DISK_NEC98,3,HEADER_HEIGHT+1);
  3915. SpStartScreen(
  3916. SP_SCRN_INIT_DISK_NEC98,
  3917. 3,
  3918. CLIENT_TOP+1,
  3919. FALSE,
  3920. FALSE,
  3921. DEFAULT_ATTRIBUTE,
  3922. pDisk->HardDisk->Description
  3923. );
  3924. SpDisplayStatusOptions(
  3925. DEFAULT_STATUS_ATTRIBUTE,
  3926. SP_STAT_I_EQUALS_INIT_NEC98,
  3927. SP_STAT_ESC_EQUALS_CANCEL,
  3928. 0
  3929. );
  3930. if(SpWaitValidKey(ValidKeysInit,NULL,MnemonicKeysInit) == ASCI_ESC) {
  3931. SpMnDestroy(Menu);
  3932. continue;
  3933. }
  3934. //
  3935. // It will be not return, if successfully complete.
  3936. //
  3937. return( SpInitializeHardDisk_Nec98(pRegion) );
  3938. }
  3939. } //NEC98
  3940. switch(Keypress) {
  3941. case MnemonicCreatePartition:
  3942. SpPtDoCreate(pRegion,NULL,FALSE,0,0,TRUE);
  3943. break;
  3944. case MnemonicDeletePartition:
  3945. SpPtDoDelete(pRegion,SpMnGetText(Menu,(ULONG_PTR)pRegion),TRUE);
  3946. break;
  3947. case KEY_F3:
  3948. SpConfirmExit();
  3949. break;
  3950. case ASCI_ESC:
  3951. if (ConsoleRunning) {
  3952. SpPtDoCommitChanges();
  3953. }
  3954. if (createdMenu) {
  3955. SpMnDestroy(Menu);
  3956. return(STATUS_SUCCESS);
  3957. }
  3958. return(STATUS_SUCCESS);
  3959. case ASCI_CR:
  3960. if(SpPtDoPartitionSelection(&pRegion,
  3961. (!createdMenu) ? Buffer :
  3962. SpMnGetText(Menu,(ULONG_PTR)pRegion),
  3963. SifHandle,
  3964. unattended,
  3965. SetupSourceDevicePath,
  3966. DirectoryOnSetupSource,
  3967. RemoteBootRepartition)) {
  3968. //
  3969. // We're done here.
  3970. //
  3971. if (createdMenu) {
  3972. SpMnDestroy(Menu);
  3973. }
  3974. *InstallRegion = pRegion;
  3975. #if defined(REMOTE_BOOT)
  3976. //
  3977. // Set the install region differently if this is a remote
  3978. // boot -- in that case, the install region is always remote.
  3979. //
  3980. if (RemoteBootSetup && !RemoteInstallSetup) {
  3981. *InstallRegion = RemoteBootTargetRegion;
  3982. }
  3983. #endif // defined(REMOTE_BOOT)
  3984. if (!SpIsArc()) {
  3985. if (!IsNEC_98) { //NEC98
  3986. *SystemPartitionRegion = SpPtValidSystemPartition();
  3987. } else {
  3988. *SystemPartitionRegion = *InstallRegion;
  3989. } //NEC98
  3990. }else{
  3991. //
  3992. // Select a system partition from among those defined in NV-RAM.
  3993. // We have to do this again because the user may have deleted the
  3994. // system partition previously detected.
  3995. // Note that SpPtValidSystemPartitionArc(SifHandle) will not return if
  3996. // a valid system partition is not found.
  3997. //
  3998. *SystemPartitionRegion = SpPtValidSystemPartitionArc(SifHandle,
  3999. SetupSourceDevicePath,
  4000. DirectoryOnSetupSource);
  4001. }
  4002. #if defined(REMOTE_BOOT)
  4003. ASSERT(*SystemPartitionRegion ||
  4004. (RemoteBootSetup && !RemoteInstallSetup && (HardDiskCount == 0)));
  4005. #else
  4006. ASSERT(*SystemPartitionRegion);
  4007. #endif // defined(REMOTE_BOOT)
  4008. return(STATUS_SUCCESS);
  4009. } else {
  4010. //
  4011. // Something happened when we tried to select the
  4012. // partition. Make sure that autopartition-picker
  4013. // doesn't invoke next time through our while loop.
  4014. //
  4015. AutoPartitionPicker = FALSE;
  4016. }
  4017. break;
  4018. }
  4019. if (createdMenu) {
  4020. SpMnDestroy(Menu);
  4021. }
  4022. unattended = FALSE;
  4023. }
  4024. }
  4025. VOID
  4026. SpPtDoDelete(
  4027. IN PDISK_REGION pRegion,
  4028. IN PWSTR RegionDescription,
  4029. IN BOOLEAN ConfirmIt
  4030. )
  4031. {
  4032. ULONG ValidKeys[3] = { ASCI_ESC, ASCI_CR, 0 }; // do not change order
  4033. ULONG Mnemonics[2] = { MnemonicDeletePartition2, 0 };
  4034. ULONG k;
  4035. BOOLEAN b;
  4036. PPARTITIONED_DISK pDisk;
  4037. BOOLEAN LastLogical;
  4038. ULONG Count;
  4039. #ifdef GPT_PARTITION_ENGINE
  4040. if (SPPT_IS_GPT_DISK(pRegion->DiskNumber)) {
  4041. SpPtnDoDelete(pRegion,
  4042. RegionDescription,
  4043. ConfirmIt);
  4044. return;
  4045. }
  4046. #endif
  4047. //
  4048. // Special warning if this is a system partition.
  4049. //
  4050. // Do not check system partition on NEC98.
  4051. //
  4052. if (!IsNEC_98) { //NEC98
  4053. if(ConfirmIt && pRegion->IsSystemPartition) {
  4054. SpDisplayScreen(SP_SCRN_CONFIRM_REMOVE_SYSPART,3,HEADER_HEIGHT+1);
  4055. SpDisplayStatusOptions(
  4056. DEFAULT_STATUS_ATTRIBUTE,
  4057. SP_STAT_ENTER_EQUALS_CONTINUE,
  4058. SP_STAT_ESC_EQUALS_CANCEL,
  4059. 0
  4060. );
  4061. if(SpWaitValidKey(ValidKeys,NULL,NULL) == ASCI_ESC) {
  4062. return;
  4063. }
  4064. }
  4065. } //NEC98
  4066. if(ConfirmIt && pRegion->DynamicVolume) {
  4067. SpDisplayScreen(SP_SCRN_CONFIRM_REMOVE_DYNVOL,3,HEADER_HEIGHT+1);
  4068. SpDisplayStatusOptions(
  4069. DEFAULT_STATUS_ATTRIBUTE,
  4070. SP_STAT_ENTER_EQUALS_CONTINUE,
  4071. SP_STAT_ESC_EQUALS_CANCEL,
  4072. 0
  4073. );
  4074. if(SpWaitValidKey(ValidKeys,NULL,NULL) == ASCI_ESC) {
  4075. return;
  4076. }
  4077. }
  4078. //
  4079. // CR is no longer a valid key.
  4080. //
  4081. ValidKeys[1] = 0;
  4082. pDisk = &PartitionedDisks[pRegion->DiskNumber];
  4083. //
  4084. // Put up the confirmation screen.
  4085. //
  4086. if (ConfirmIt) {
  4087. if( ( pRegion->Filesystem == FilesystemFat ) &&
  4088. ( pRegion->NextCompressed != NULL ) ) {
  4089. //
  4090. // Warn the user that the partition contains compressed volumes
  4091. //
  4092. Count = SpGetNumberOfCompressedDrives( pRegion );
  4093. SpStartScreen(
  4094. SP_SCRN_CONFIRM_REMOVE_PARTITION_COMPRESSED,
  4095. 3,
  4096. CLIENT_TOP+1,
  4097. FALSE,
  4098. FALSE,
  4099. DEFAULT_ATTRIBUTE,
  4100. RegionDescription,
  4101. pDisk->HardDisk->Description,
  4102. Count
  4103. );
  4104. } else {
  4105. SpStartScreen(
  4106. SP_SCRN_CONFIRM_REMOVE_PARTITION,
  4107. 3,
  4108. CLIENT_TOP+1,
  4109. FALSE,
  4110. FALSE,
  4111. DEFAULT_ATTRIBUTE,
  4112. RegionDescription,
  4113. pDisk->HardDisk->Description
  4114. );
  4115. }
  4116. }
  4117. //
  4118. // Display the staus text.
  4119. //
  4120. if (ConfirmIt) {
  4121. SpDisplayStatusOptions(
  4122. DEFAULT_STATUS_ATTRIBUTE,
  4123. SP_STAT_L_EQUALS_DELETE,
  4124. SP_STAT_ESC_EQUALS_CANCEL,
  4125. 0
  4126. );
  4127. k = SpWaitValidKey(ValidKeys,NULL,Mnemonics);
  4128. if(k == ASCI_ESC) {
  4129. return;
  4130. }
  4131. SpDisplayStatusOptions(
  4132. DEFAULT_STATUS_ATTRIBUTE,
  4133. SP_STAT_PLEASE_WAIT,
  4134. 0);
  4135. }
  4136. //
  4137. // User wants to go ahead.
  4138. // Determine whether this is the last logical drive in the
  4139. // extended partition.
  4140. //
  4141. if((pRegion->MbrInfo == pDisk->FirstEbrInfo.Next)
  4142. && (pDisk->FirstEbrInfo.Next->Next == NULL))
  4143. {
  4144. LastLogical = TRUE;
  4145. } else {
  4146. LastLogical = FALSE;
  4147. }
  4148. //
  4149. // Get rid of the compressed drives, if any
  4150. //
  4151. if( pRegion->NextCompressed != NULL ) {
  4152. SpDisposeCompressedDrives( pRegion->NextCompressed );
  4153. pRegion->NextCompressed = NULL;
  4154. pRegion->MountDrive = 0;
  4155. pRegion->HostDrive = 0;
  4156. }
  4157. b = SpPtDelete(pRegion->DiskNumber,pRegion->StartSector);
  4158. if (!b) {
  4159. if (ConfirmIt) {
  4160. SpDisplayScreen(SP_SCRN_PARTITION_DELETE_FAILED,3,HEADER_HEIGHT+1);
  4161. SpDisplayStatusText(SP_STAT_ENTER_EQUALS_CONTINUE,DEFAULT_STATUS_ATTRIBUTE);
  4162. SpInputDrain();
  4163. while(SpInputGetKeypress() != ASCI_CR) ;
  4164. }
  4165. return;
  4166. }
  4167. //
  4168. // If we deleted the last logical drive in the extended partition,
  4169. // then remove the extended partition also.
  4170. //
  4171. // Do not check system partition on NEC98.
  4172. //
  4173. if (!IsNEC_98) { //NEC98
  4174. if(LastLogical) {
  4175. //
  4176. // Locate the extended partition.
  4177. //
  4178. for(pRegion=pDisk->PrimaryDiskRegions; pRegion; pRegion=pRegion->Next) {
  4179. if(pRegion->PartitionedSpace
  4180. && IsContainerPartition(pRegion->MbrInfo->OnDiskMbr.PartitionTable[pRegion->TablePosition].SystemId))
  4181. {
  4182. //
  4183. // Found it -- now delete it.
  4184. //
  4185. b = SpPtDelete(pRegion->DiskNumber,pRegion->StartSector);
  4186. ASSERT(b);
  4187. break;
  4188. }
  4189. }
  4190. }
  4191. } //NEC98
  4192. //
  4193. // Delete the drive letters if the necessary. This is to ensure that the drive letters assigned to CD-ROM
  4194. // drives will go away, when the the disks have no partitioned space.
  4195. //
  4196. SpPtDeleteDriveLetters();
  4197. }
  4198. BOOLEAN
  4199. SpPtDoCreate(
  4200. IN PDISK_REGION pRegion,
  4201. OUT PDISK_REGION *pActualRegion, OPTIONAL
  4202. IN BOOLEAN ForNT,
  4203. IN ULONGLONG DesiredMB OPTIONAL,
  4204. IN PPARTITION_INFORMATION_EX PartInfo OPTIONAL,
  4205. IN BOOLEAN ConfirmIt
  4206. )
  4207. {
  4208. ULONG ValidKeys[3] = { ASCI_ESC, ASCI_CR, 0 };
  4209. BOOLEAN b;
  4210. PPARTITIONED_DISK pDisk;
  4211. ULONGLONG MinMB,MaxMB;
  4212. ULONG TotalPrimary,RecogPrimary;
  4213. BOOLEAN InExtended;
  4214. UCHAR CreateSysId;
  4215. UCHAR RealSysId;
  4216. BOOLEAN ExtendedExists;
  4217. ULONGLONG SizeMB,RealSizeMB;
  4218. WCHAR Buffer[200];
  4219. WCHAR SizeBuffer[10];
  4220. BOOLEAN Beyond1024;
  4221. BOOLEAN ReservedRegion;
  4222. UCHAR DesiredSysId = 0;
  4223. PARTITION_INFORMATION_EX NewPartInfo;
  4224. #ifdef GPT_PARTITION_ENGINE
  4225. if (SPPT_IS_GPT_DISK(pRegion->DiskNumber)) {
  4226. return SpPtnDoCreate(pRegion,
  4227. pActualRegion,
  4228. ForNT,
  4229. DesiredMB,
  4230. PartInfo,
  4231. ConfirmIt);
  4232. }
  4233. #endif
  4234. RtlZeroMemory(&NewPartInfo, sizeof(PARTITION_INFORMATION_EX));
  4235. DesiredSysId = PartInfo ? PartInfo->Mbr.PartitionType : 0;
  4236. ASSERT(!pRegion->PartitionedSpace);
  4237. pDisk = &PartitionedDisks[pRegion->DiskNumber];
  4238. //
  4239. // Determine whether this space is within the extended partition.
  4240. //
  4241. # if 0
  4242. //
  4243. // No NEC98 has Extended partition.
  4244. // All of partition on NEC98 are Primary.
  4245. //
  4246. InExtended = (!IsNEC_98) ? (BOOLEAN)(SpPtLookupRegionByStart(pDisk,TRUE,pRegion->StartSector) != NULL) : FALSE; //NEC98
  4247. # endif //0
  4248. InExtended = (BOOLEAN)(SpPtLookupRegionByStart(pDisk,TRUE,pRegion->StartSector) != NULL);
  4249. Beyond1024 = SpIsRegionBeyondCylinder1024(pRegion);
  4250. if( pDisk->HardDisk->Geometry.MediaType == RemovableMedia ) {
  4251. ULONG pass;
  4252. PDISK_REGION p;
  4253. //
  4254. // If the user is attempting to create a partition on a removable drive, then make sure that
  4255. // the drive doesn't already contain a primary partition or a logical drive.
  4256. //
  4257. for( pass = 0; pass < 2; pass++ ) {
  4258. for( p = (pass == 0)? pDisk->PrimaryDiskRegions : pDisk->ExtendedDiskRegions;
  4259. p;
  4260. p = p->Next ) {
  4261. if( p->PartitionedSpace ) {
  4262. PON_DISK_PTE pte;
  4263. UCHAR TmpSysId;
  4264. pte = &p->MbrInfo->OnDiskMbr.PartitionTable[p->TablePosition];
  4265. TmpSysId = pte->SystemId;
  4266. if( !IsContainerPartition(TmpSysId) ) {
  4267. ULONG ValidKeys1[2] = { ASCI_CR ,0 };
  4268. //
  4269. // Disk is already partitioned
  4270. //
  4271. SpDisplayScreen(SP_SCRN_REMOVABLE_ALREADY_PARTITIONED,3,HEADER_HEIGHT+1);
  4272. SpDisplayStatusOptions(DEFAULT_STATUS_ATTRIBUTE,SP_STAT_ENTER_EQUALS_CONTINUE,0);
  4273. SpWaitValidKey(ValidKeys1,NULL,NULL);
  4274. return( FALSE );
  4275. }
  4276. }
  4277. }
  4278. }
  4279. }
  4280. //
  4281. // Determine the type of partition to create for this space,
  4282. // excluding any issues with extended partitions.
  4283. //
  4284. if (DesiredSysId != 0) {
  4285. //
  4286. // If the caller specified a partition type, use it unless it
  4287. // won't work due to being beyond 1024 cylinders.
  4288. //
  4289. #if 0
  4290. RealSysId = DesiredSysId;
  4291. if (Beyond1024) {
  4292. if (RealSysId == PARTITION_FAT32) {
  4293. RealSysId = PARTITION_FAT32_XINT13;
  4294. } else {
  4295. RealSysId = PARTITION_XINT13;
  4296. }
  4297. }
  4298. #else
  4299. //
  4300. // Keep this code in until I determine if we will be explicitly
  4301. // creating extended partitions.
  4302. //
  4303. RealSysId = Beyond1024 ? PARTITION_XINT13 : PARTITION_HUGE;
  4304. #endif
  4305. } else {
  4306. RealSysId = Beyond1024 ? PARTITION_XINT13 : PARTITION_HUGE;
  4307. }
  4308. //
  4309. // Determine the type of partition to create in the space.
  4310. //
  4311. // If the free space is within the extended partition, create
  4312. // a logical drive.
  4313. //
  4314. // If there is no primary partition, create a primary partition.
  4315. //
  4316. // If there is a primary partition and no extended partition,
  4317. // create an extended partition spanning the entire space and
  4318. // then a logical drive within it of the size given by the user.
  4319. //
  4320. // If there is space in the partition table, create a primary partition.
  4321. //
  4322. if(InExtended) {
  4323. CreateSysId = RealSysId;
  4324. } else {
  4325. //
  4326. // Get statistics about primary partitions.
  4327. //
  4328. SpPtCountPrimaryPartitions(pDisk,&TotalPrimary,&RecogPrimary,&ExtendedExists);
  4329. //
  4330. // If there is no primary partition, create one.
  4331. //
  4332. if(!RecogPrimary) {
  4333. CreateSysId = RealSysId;
  4334. } else {
  4335. //
  4336. // Make sure we can create a new primary/extended partition.
  4337. //
  4338. if(TotalPrimary < PTABLE_DIMENSION) {
  4339. //
  4340. // If there is an extended partition, then we have no choice but
  4341. // to create another primary.
  4342. //
  4343. if(ExtendedExists) {
  4344. CreateSysId = RealSysId;
  4345. } else {
  4346. //
  4347. // Firmware doesn't understand type F link partitions.
  4348. // No great need to use on x86 either; assume that creating
  4349. // logical drives with the correct type is good enough.
  4350. //
  4351. //
  4352. // No NEC98 has PARTITION_EXTENDED, just PARTITION_HUGE only.
  4353. //
  4354. CreateSysId = (!IsNEC_98 ||
  4355. (pDisk->HardDisk->FormatType == DISK_FORMAT_TYPE_PCAT))
  4356. ? PARTITION_EXTENDED : PARTITION_HUGE; //NEC98
  4357. if((CreateSysId == PARTITION_EXTENDED) && Beyond1024) {
  4358. CreateSysId = PARTITION_XINT13_EXTENDED;
  4359. }
  4360. }
  4361. } else {
  4362. if (ConfirmIt) {
  4363. while (TRUE) {
  4364. ULONG ks[2] = { ASCI_CR,0 };
  4365. SpDisplayScreen(SP_SCRN_PART_TABLE_FULL,3,CLIENT_HEIGHT+1);
  4366. SpDisplayStatusOptions(
  4367. DEFAULT_STATUS_ATTRIBUTE,
  4368. SP_STAT_ENTER_EQUALS_CONTINUE,
  4369. 0
  4370. );
  4371. switch(SpWaitValidKey(ks,NULL,NULL)) {
  4372. case ASCI_CR:
  4373. return(FALSE);
  4374. }
  4375. }
  4376. } else {
  4377. return TRUE;
  4378. }
  4379. }
  4380. }
  4381. }
  4382. //
  4383. // Get the mimimum and maximum sizes for the partition.
  4384. //
  4385. ReservedRegion = FALSE;
  4386. SpPtQueryMinMaxCreationSizeMB(
  4387. pRegion->DiskNumber,
  4388. pRegion->StartSector,
  4389. (BOOLEAN)IsContainerPartition(CreateSysId),
  4390. InExtended,
  4391. &MinMB,
  4392. &MaxMB,
  4393. &ReservedRegion
  4394. );
  4395. if( ReservedRegion ) {
  4396. ULONG ValidKeys1[2] = { ASCI_CR ,0 };
  4397. SpStartScreen(
  4398. SP_SCRN_REGION_RESERVED,
  4399. 3,
  4400. HEADER_HEIGHT+1,
  4401. FALSE,
  4402. FALSE,
  4403. DEFAULT_ATTRIBUTE
  4404. );
  4405. SpDisplayStatusOptions(DEFAULT_STATUS_ATTRIBUTE,SP_STAT_ENTER_EQUALS_CONTINUE,0);
  4406. SpWaitValidKey(ValidKeys1,NULL,NULL);
  4407. return(FALSE);
  4408. }
  4409. if(ForNT) {
  4410. //
  4411. // If a size was requested then try to use that, otherwise use
  4412. // the maximum.
  4413. //
  4414. if (DesiredMB != 0) {
  4415. if (DesiredMB <= MaxMB) {
  4416. SizeMB = DesiredMB;
  4417. } else {
  4418. return FALSE;
  4419. }
  4420. } else {
  4421. SizeMB = MaxMB;
  4422. }
  4423. } else {
  4424. //
  4425. // Put up a screen displaying min/max size info.
  4426. //
  4427. SpStartScreen(
  4428. SP_SCRN_CONFIRM_CREATE_PARTITION,
  4429. 3,
  4430. CLIENT_TOP+1,
  4431. FALSE,
  4432. FALSE,
  4433. DEFAULT_ATTRIBUTE,
  4434. pDisk->HardDisk->Description,
  4435. MinMB,
  4436. MaxMB
  4437. );
  4438. //
  4439. // Display the staus text.
  4440. //
  4441. SpDisplayStatusOptions(
  4442. DEFAULT_STATUS_ATTRIBUTE,
  4443. SP_STAT_ENTER_EQUALS_CREATE,
  4444. SP_STAT_ESC_EQUALS_CANCEL,
  4445. 0
  4446. );
  4447. //
  4448. // Get and display the size prompt.
  4449. //
  4450. SpFormatMessage(Buffer,sizeof(Buffer),SP_TEXT_SIZE_PROMPT);
  4451. SpvidDisplayString(Buffer,DEFAULT_ATTRIBUTE,3,NextMessageTopLine);
  4452. //
  4453. // Get the size from the user.
  4454. //
  4455. do {
  4456. swprintf(SizeBuffer,L"%u",MaxMB);
  4457. if(!SpGetInput(SpPtnGetSizeCB,SplangGetColumnCount(Buffer)+5,NextMessageTopLine,5,SizeBuffer,TRUE)) {
  4458. //
  4459. // User pressed escape and bailed.
  4460. //
  4461. return(FALSE);
  4462. }
  4463. SizeMB = (ULONG)SpStringToLong(SizeBuffer,NULL,10);
  4464. } while((SizeMB < MinMB) || (SizeMB > MaxMB));
  4465. }
  4466. if(IsContainerPartition(CreateSysId)) {
  4467. RealSizeMB = SizeMB;
  4468. SizeMB = MaxMB;
  4469. }
  4470. NewPartInfo.PartitionStyle = PARTITION_STYLE_MBR;
  4471. NewPartInfo.Mbr.PartitionType = CreateSysId;
  4472. //
  4473. // Create the partition.
  4474. //
  4475. b = SpPtCreate(
  4476. pRegion->DiskNumber,
  4477. pRegion->StartSector,
  4478. SizeMB,
  4479. InExtended,
  4480. &NewPartInfo,
  4481. pActualRegion
  4482. );
  4483. ASSERT(b);
  4484. //
  4485. // Create the logical drive if we just created the extended partition.
  4486. //
  4487. if(IsContainerPartition(CreateSysId)) {
  4488. ASSERT(!InExtended);
  4489. NewPartInfo.Mbr.PartitionType = RealSysId;
  4490. b = SpPtCreate(
  4491. pRegion->DiskNumber,
  4492. pRegion->StartSector,
  4493. RealSizeMB,
  4494. TRUE,
  4495. &NewPartInfo,
  4496. pActualRegion
  4497. );
  4498. ASSERT(b);
  4499. }
  4500. return(TRUE);
  4501. }
  4502. #endif // NEW_PARTITION_ENGINE
  4503. //
  4504. // The following table contains offsets from SP_TEXT_PARTITION_NAME_BASE
  4505. // to get the message id of the name of each type of partition.
  4506. // A -1 entry means there is no name in the message file for this type
  4507. // of partition or that the filesystem should be determined instead.
  4508. //
  4509. //
  4510. #define PT(id) ((UCHAR)((SP_TEXT_PARTITION_NAME_##id)-SP_TEXT_PARTITION_NAME_BASE))
  4511. #define UNKNOWN PT(UNK)
  4512. #define M1 ((UCHAR)(-1))
  4513. UCHAR PartitionNameIds[256] = {
  4514. M1,M1,PT(XENIX),PT(XENIX), // 00-03
  4515. M1,M1,M1,M1, // 04-07
  4516. UNKNOWN,UNKNOWN,PT(BOOTMANAGER),M1, // 08-0b
  4517. M1,UNKNOWN,M1,M1, // 0c-0f
  4518. UNKNOWN,UNKNOWN,PT(EISA),UNKNOWN, // 10-13
  4519. UNKNOWN,UNKNOWN,PT(BMHIDE),PT(BMHIDE), // 14-17
  4520. UNKNOWN,UNKNOWN,UNKNOWN,PT(BMHIDE), // 18-1b
  4521. PT(BMHIDE),UNKNOWN,UNKNOWN,UNKNOWN, // 1c-1f
  4522. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 20-23
  4523. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 24-27
  4524. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 28-2b
  4525. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 2c-2f
  4526. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 30-33
  4527. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 34-37
  4528. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 38-3b
  4529. PT(PWRQST),UNKNOWN,UNKNOWN,UNKNOWN, // 3c-3f
  4530. UNKNOWN,PT(PPCBOOT),PT(VERIT),PT(VERIT), // 40-43
  4531. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 44-47
  4532. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 48-4b
  4533. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 4c-4f
  4534. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 50-53
  4535. PT(ONTRACK),PT(EZDRIVE),UNKNOWN,UNKNOWN, // 54-57
  4536. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 58-5b
  4537. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 5c-5f
  4538. UNKNOWN,UNKNOWN,UNKNOWN,PT(UNIX), // 60-63
  4539. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 64-67
  4540. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 68-6b
  4541. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 6c-6f
  4542. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 70-73
  4543. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 74-77
  4544. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 78-7b
  4545. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 7c-7f
  4546. UNKNOWN,PT(NTFT),UNKNOWN,UNKNOWN, // 80-83
  4547. PT(NTFT),UNKNOWN,PT(NTFT),PT(NTFT), // 84-87
  4548. UNKNOWN,UNKNOWN,UNKNOWN,PT(NTFT), // 88-8b
  4549. PT(NTFT),UNKNOWN,PT(NTFT),UNKNOWN, // 8c-8f
  4550. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 90-93
  4551. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 94-97
  4552. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 98-9b
  4553. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // 9c-9f
  4554. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // a0-a3
  4555. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // a4-a7
  4556. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // a8-ab
  4557. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // ac-af
  4558. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // b0-b3
  4559. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // b4-b7
  4560. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // b8-bb
  4561. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // bc-bf
  4562. UNKNOWN,PT(NTFT),UNKNOWN,UNKNOWN, // c0-c3
  4563. PT(NTFT),UNKNOWN,PT(NTFT),PT(NTFT), // c4-c7
  4564. UNKNOWN,UNKNOWN,UNKNOWN,PT(NTFT), // c8-cb
  4565. PT(NTFT),UNKNOWN,PT(NTFT),UNKNOWN, // cc-cf
  4566. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // d0-d3
  4567. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // d4-d7
  4568. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // d8-db
  4569. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // dc-df
  4570. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // e0-e3
  4571. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // e4-e7
  4572. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // e8-eb
  4573. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // ec-ef
  4574. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // f0-f3
  4575. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // f4-f7
  4576. UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN, // f8-fb
  4577. UNKNOWN,UNKNOWN,UNKNOWN,PT(XENIXTABLE) // fc-ff
  4578. };
  4579. WCHAR
  4580. SpGetDriveLetter(
  4581. IN PWSTR DeviceName,
  4582. OUT PMOUNTMGR_MOUNT_POINT * MountPoint OPTIONAL
  4583. )
  4584. /*++
  4585. Routine Description:
  4586. This routine returns the drive letter associated to a given device.
  4587. Arguments:
  4588. DeviceName - Supplies the device name.
  4589. MountPoint - If specified, causes the function to allocate a mount
  4590. manager point and fills it in.
  4591. Return Value:
  4592. A drive letter, if one exists.
  4593. --*/
  4594. {
  4595. NTSTATUS Status;
  4596. OBJECT_ATTRIBUTES Obja;
  4597. UNICODE_STRING UnicodeString;
  4598. IO_STATUS_BLOCK IoStatusBlock;
  4599. HANDLE Handle;
  4600. DWORD nameLen;
  4601. DWORD mountPointSize;
  4602. PMOUNTMGR_MOUNT_POINT mountPoint;
  4603. PMOUNTMGR_MOUNT_POINTS mountPoints;
  4604. PMOUNTMGR_TARGET_NAME mountTarget;
  4605. DWORD bytes;
  4606. WCHAR driveLetter;
  4607. DWORD i;
  4608. PWSTR s;
  4609. LARGE_INTEGER DelayTime;
  4610. INIT_OBJA(&Obja,&UnicodeString,MOUNTMGR_DEVICE_NAME);
  4611. Status = ZwOpenFile(
  4612. &Handle,
  4613. (ACCESS_MASK)(FILE_GENERIC_READ),
  4614. &Obja,
  4615. &IoStatusBlock,
  4616. FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE ,
  4617. FILE_NON_DIRECTORY_FILE
  4618. );
  4619. if( !NT_SUCCESS( Status ) ) {
  4620. return L'\0';
  4621. }
  4622. //
  4623. // setup a good device name
  4624. //
  4625. nameLen = wcslen(DeviceName);
  4626. mountPointSize = sizeof(MOUNTMGR_TARGET_NAME) + nameLen*sizeof(WCHAR) + 28;
  4627. mountTarget = SpMemAlloc(mountPointSize);
  4628. if (!mountTarget) {
  4629. ZwClose(Handle);
  4630. return L'\0';
  4631. }
  4632. RtlZeroMemory(mountTarget, mountPointSize);
  4633. mountTarget->DeviceNameLength = (USHORT) nameLen*sizeof(WCHAR);
  4634. RtlCopyMemory((PCHAR) &mountTarget->DeviceName, DeviceName, nameLen*sizeof(WCHAR));
  4635. //
  4636. // this loop is necessary as a synchronization
  4637. // method. we have previously committed changes, but
  4638. // the volume manager has not had a chance to
  4639. // do it's thing so here we wait......
  4640. //
  4641. for (i=0; i<20; i++) {
  4642. Status = ZwDeviceIoControlFile(
  4643. Handle,
  4644. NULL,
  4645. NULL,
  4646. NULL,
  4647. &IoStatusBlock,
  4648. IOCTL_MOUNTMGR_VOLUME_ARRIVAL_NOTIFICATION,
  4649. mountTarget,
  4650. mountPointSize,
  4651. NULL,
  4652. 0
  4653. );
  4654. if (!NT_SUCCESS( Status )) {
  4655. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: IOCTL_MOUNTMGR_VOLUME_ARRIVAL_NOTIFICATION failed - %08x\n",Status));
  4656. DelayTime.HighPart = -1;
  4657. DelayTime.LowPart = (ULONG)(-5000000);
  4658. KeDelayExecutionThread(KernelMode,FALSE,&DelayTime);
  4659. } else {
  4660. //
  4661. // On removable disks, a drive letter may not have been assigned yet.
  4662. // So make sure one is assigned on this case.
  4663. //
  4664. MOUNTMGR_DRIVE_LETTER_INFORMATION DriveLetterInformation;
  4665. NTSTATUS Status1;
  4666. Status1 = ZwDeviceIoControlFile(
  4667. Handle,
  4668. NULL,
  4669. NULL,
  4670. NULL,
  4671. &IoStatusBlock,
  4672. IOCTL_MOUNTMGR_NEXT_DRIVE_LETTER,
  4673. mountTarget,
  4674. mountPointSize,
  4675. &DriveLetterInformation,
  4676. sizeof(MOUNTMGR_DRIVE_LETTER_INFORMATION)
  4677. );
  4678. if (!NT_SUCCESS( Status1 )) {
  4679. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: IOCTL_MOUNTMGR_NEXT_DRIVE_LETTER failed. Status = %lx \n",Status1));
  4680. }
  4681. break;
  4682. }
  4683. }
  4684. if (!NT_SUCCESS( Status )) {
  4685. SpMemFree(mountTarget);
  4686. ZwClose(Handle);
  4687. return L'\0';
  4688. }
  4689. SpMemFree(mountTarget);
  4690. nameLen = wcslen(DeviceName);
  4691. mountPointSize = sizeof(MOUNTMGR_MOUNT_POINT) + nameLen*sizeof(WCHAR) + 28;
  4692. mountPoint = SpMemAlloc(mountPointSize);
  4693. if (!mountPoint) {
  4694. ZwClose(Handle);
  4695. return L'\0';
  4696. }
  4697. RtlZeroMemory(mountPoint, mountPointSize);
  4698. mountPoint->DeviceNameOffset = sizeof(MOUNTMGR_MOUNT_POINT);
  4699. mountPoint->DeviceNameLength = (USHORT) nameLen*sizeof(WCHAR);
  4700. RtlCopyMemory((PCHAR) mountPoint + sizeof(MOUNTMGR_MOUNT_POINT),
  4701. DeviceName, nameLen*sizeof(WCHAR));
  4702. mountPoints = SpMemAlloc( 4096 );
  4703. if (!mountPoints) {
  4704. SpMemFree(mountPoint);
  4705. ZwClose(Handle);
  4706. return L'\0';
  4707. }
  4708. Status = ZwDeviceIoControlFile(
  4709. Handle,
  4710. NULL,
  4711. NULL,
  4712. NULL,
  4713. &IoStatusBlock,
  4714. IOCTL_MOUNTMGR_QUERY_POINTS,
  4715. mountPoint,
  4716. mountPointSize,
  4717. mountPoints,
  4718. 4096
  4719. );
  4720. if (!NT_SUCCESS( Status )) {
  4721. if (Status == STATUS_BUFFER_OVERFLOW) {
  4722. bytes = mountPoints->Size;
  4723. SpMemFree(mountPoints);
  4724. mountPoints = SpMemAlloc(bytes);
  4725. if (!mountPoints) {
  4726. SpMemFree(mountPoint);
  4727. ZwClose(Handle);
  4728. return L'\0';
  4729. }
  4730. Status = ZwDeviceIoControlFile(
  4731. Handle,
  4732. NULL,
  4733. NULL,
  4734. NULL,
  4735. &IoStatusBlock,
  4736. IOCTL_MOUNTMGR_QUERY_POINTS,
  4737. mountPoint,
  4738. mountPointSize,
  4739. mountPoints,
  4740. bytes
  4741. );
  4742. if (!NT_SUCCESS( Status )) {
  4743. SpMemFree(mountPoints);
  4744. SpMemFree(mountPoint);
  4745. ZwClose(Handle);
  4746. return L'\0';
  4747. }
  4748. } else {
  4749. mountPoints->NumberOfMountPoints = 0;
  4750. }
  4751. }
  4752. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  4753. "SETUP: IOCTL_MOUNTMGR_QUERY_POINTS : Number = %d \n",
  4754. mountPoints->NumberOfMountPoints));
  4755. driveLetter = 0;
  4756. for (i = 0; i < mountPoints->NumberOfMountPoints; i++) {
  4757. if (mountPoints->MountPoints[i].SymbolicLinkNameLength != 28) {
  4758. continue;
  4759. }
  4760. s = (PWSTR) ((PCHAR) mountPoints +
  4761. mountPoints->MountPoints[i].SymbolicLinkNameOffset);
  4762. if (s[0] != L'\\' ||
  4763. (s[1] != L'D' && s[1] != L'd') ||
  4764. (s[2] != L'O' && s[2] != L'o') ||
  4765. (s[3] != L'S' && s[3] != L's') ||
  4766. (s[4] != L'D' && s[4] != L'd') ||
  4767. (s[5] != L'E' && s[5] != L'e') ||
  4768. (s[6] != L'V' && s[6] != L'v') ||
  4769. (s[7] != L'I' && s[7] != L'i') ||
  4770. (s[8] != L'C' && s[8] != L'c') ||
  4771. (s[9] != L'E' && s[9] != L'e') ||
  4772. (s[10]!= L'S' && s[10]!= L's') ||
  4773. s[11] != L'\\' ||
  4774. s[13] != L':') {
  4775. continue;
  4776. }
  4777. if (s[12] < ((!IsNEC_98) ? L'C' : L'A') || s[12] > L'Z') { //NEC98
  4778. continue;
  4779. }
  4780. driveLetter = s[12];
  4781. if (ARGUMENT_PRESENT( MountPoint )) {
  4782. ULONG newMountPointSize;
  4783. PMOUNTMGR_MOUNT_POINT newMountPoint, oldMountPoint;
  4784. ULONG currentOffset;
  4785. //
  4786. // The caller wants us to return the actual mount point information.
  4787. //
  4788. oldMountPoint = &mountPoints->MountPoints[i];
  4789. newMountPointSize = sizeof(MOUNTMGR_MOUNT_POINT) +
  4790. oldMountPoint->SymbolicLinkNameLength +
  4791. oldMountPoint->UniqueIdLength +
  4792. oldMountPoint->DeviceNameLength;
  4793. newMountPoint = SpMemAlloc(newMountPointSize);
  4794. if (newMountPoint) {
  4795. currentOffset = sizeof(MOUNTMGR_MOUNT_POINT);
  4796. newMountPoint->SymbolicLinkNameLength = oldMountPoint->SymbolicLinkNameLength;
  4797. newMountPoint->SymbolicLinkNameOffset = currentOffset;
  4798. memcpy((PCHAR)newMountPoint + newMountPoint->SymbolicLinkNameOffset,
  4799. (PCHAR)mountPoints + oldMountPoint->SymbolicLinkNameOffset,
  4800. oldMountPoint->SymbolicLinkNameLength);
  4801. currentOffset += oldMountPoint->SymbolicLinkNameLength;
  4802. newMountPoint->UniqueIdLength = oldMountPoint->UniqueIdLength;
  4803. newMountPoint->UniqueIdOffset = currentOffset;
  4804. memcpy((PCHAR)newMountPoint + newMountPoint->UniqueIdOffset,
  4805. (PCHAR)mountPoints + oldMountPoint->UniqueIdOffset,
  4806. oldMountPoint->UniqueIdLength);
  4807. currentOffset += oldMountPoint->UniqueIdLength;
  4808. newMountPoint->DeviceNameLength = oldMountPoint->DeviceNameLength;
  4809. newMountPoint->DeviceNameOffset = currentOffset;
  4810. memcpy((PCHAR)newMountPoint + newMountPoint->DeviceNameOffset,
  4811. (PCHAR)mountPoints + oldMountPoint->DeviceNameOffset,
  4812. oldMountPoint->DeviceNameLength);
  4813. *MountPoint = newMountPoint;
  4814. }
  4815. }
  4816. break;
  4817. }
  4818. SpMemFree(mountPoints);
  4819. SpMemFree(mountPoint);
  4820. ZwClose(Handle);
  4821. return driveLetter;
  4822. }
  4823. WCHAR
  4824. SpDeleteDriveLetter(
  4825. IN PWSTR DeviceName
  4826. )
  4827. /*++
  4828. Routine Description:
  4829. This routine returns the drive letter associated to a given device.
  4830. Arguments:
  4831. DeviceName - Supplies the device name.
  4832. Return Value:
  4833. A drive letter, if one exists.
  4834. --*/
  4835. {
  4836. NTSTATUS Status;
  4837. OBJECT_ATTRIBUTES Obja;
  4838. UNICODE_STRING UnicodeString;
  4839. IO_STATUS_BLOCK IoStatusBlock;
  4840. HANDLE Handle;
  4841. DWORD nameLen;
  4842. DWORD mountPointSize;
  4843. PMOUNTMGR_MOUNT_POINT mountPoint;
  4844. PMOUNTMGR_MOUNT_POINTS mountPoints;
  4845. DWORD bytes;
  4846. WCHAR driveLetter;
  4847. INIT_OBJA(&Obja,&UnicodeString,MOUNTMGR_DEVICE_NAME);
  4848. Status = ZwOpenFile(
  4849. &Handle,
  4850. (ACCESS_MASK)(FILE_GENERIC_READ),
  4851. &Obja,
  4852. &IoStatusBlock,
  4853. FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE ,
  4854. FILE_NON_DIRECTORY_FILE
  4855. );
  4856. if( !NT_SUCCESS( Status ) ) {
  4857. return L'\0';
  4858. }
  4859. nameLen = wcslen(DeviceName);
  4860. mountPointSize = sizeof(MOUNTMGR_MOUNT_POINT) + nameLen*sizeof(WCHAR) + 28;
  4861. mountPoint = SpMemAlloc(mountPointSize);
  4862. if (!mountPoint) {
  4863. ZwClose(Handle);
  4864. return L'\0';
  4865. }
  4866. RtlZeroMemory(mountPoint, sizeof(MOUNTMGR_MOUNT_POINT));
  4867. mountPoint->DeviceNameOffset = sizeof(MOUNTMGR_MOUNT_POINT);
  4868. mountPoint->DeviceNameLength = (USHORT) nameLen*sizeof(WCHAR);
  4869. RtlCopyMemory((PCHAR) mountPoint + sizeof(MOUNTMGR_MOUNT_POINT),
  4870. DeviceName, nameLen*sizeof(WCHAR));
  4871. mountPoints = SpMemAlloc( 4096 );
  4872. if (!mountPoints) {
  4873. SpMemFree(mountPoint);
  4874. ZwClose(Handle);
  4875. return L'\0';
  4876. }
  4877. Status = ZwDeviceIoControlFile(
  4878. Handle,
  4879. NULL,
  4880. NULL,
  4881. NULL,
  4882. &IoStatusBlock,
  4883. IOCTL_MOUNTMGR_DELETE_POINTS,
  4884. mountPoint,
  4885. mountPointSize,
  4886. mountPoints,
  4887. 4096
  4888. );
  4889. if (!NT_SUCCESS( Status )) {
  4890. if (Status == STATUS_BUFFER_OVERFLOW) {
  4891. bytes = mountPoints->Size;
  4892. SpMemFree(mountPoints);
  4893. mountPoints = SpMemAlloc(bytes);
  4894. if (!mountPoints) {
  4895. SpMemFree(mountPoint);
  4896. ZwClose(Handle);
  4897. return L'\0';
  4898. }
  4899. Status = ZwDeviceIoControlFile(
  4900. Handle,
  4901. NULL,
  4902. NULL,
  4903. NULL,
  4904. &IoStatusBlock,
  4905. IOCTL_MOUNTMGR_DELETE_POINTS,
  4906. mountPoint,
  4907. mountPointSize,
  4908. mountPoints,
  4909. bytes
  4910. );
  4911. if (!NT_SUCCESS( Status )) {
  4912. SpMemFree(mountPoints);
  4913. SpMemFree(mountPoint);
  4914. ZwClose(Handle);
  4915. return L'\0';
  4916. }
  4917. } else {
  4918. mountPoints->NumberOfMountPoints = 0;
  4919. }
  4920. }
  4921. driveLetter = 0;
  4922. SpMemFree(mountPoints);
  4923. SpMemFree(mountPoint);
  4924. ZwClose(Handle);
  4925. return driveLetter;
  4926. }
  4927. VOID
  4928. SpPtDeleteDriveLetters(
  4929. VOID
  4930. )
  4931. /*++
  4932. Routine Description:
  4933. This routine will delete all drive letters assigned to disks and CD-ROM drives. The deletion will
  4934. occur only if setup was started booting from the CD or boot floppies (in which case drive letter
  4935. migration does not take place), and only if the non-removable dissks have no partitioned spaces.
  4936. This ensures that on a clean install from the CD or boot floppies, the drive letters assigned to
  4937. partitions on removable disks and CD-ROM drives will always be greater than the drive letters assigned
  4938. to partitions on non-removable disks (unless the partitions on the removable disks were created before
  4939. the ones in the removable disks, during textmode setup).
  4940. Arguments:
  4941. None.
  4942. Return Value:
  4943. None.
  4944. --*/
  4945. {
  4946. ULONG disk;
  4947. PDISK_REGION pRegion;
  4948. unsigned pass;
  4949. BOOLEAN PartitionedSpaceFound = FALSE;
  4950. if( WinntSetup ) {
  4951. //
  4952. // If setup started from winnt32.exe then do not delete the drive letters since we want to preserve them
  4953. //
  4954. return;
  4955. }
  4956. //
  4957. // Setup started booting from a CD or from the boot floppies
  4958. // Find out if the disks contain at least one partition that is not a container.
  4959. // Note that we do not take into consideration partitions that are on removable media.
  4960. // This is to avoid the situation in which a newly created partition on a non-removable disk ends up with
  4961. // a drive letter that is greater than the one assigned to an existing partition on a removable disk.
  4962. //
  4963. for(disk = 0;
  4964. !PartitionedSpaceFound &&
  4965. (disk<HardDiskCount);
  4966. disk++) {
  4967. if((PartitionedDisks[disk].HardDisk)->Geometry.MediaType != RemovableMedia) {
  4968. for(pass=0; !PartitionedSpaceFound && (pass<2); pass++) {
  4969. pRegion = pass ? PartitionedDisks[disk].ExtendedDiskRegions : PartitionedDisks[disk].PrimaryDiskRegions;
  4970. for( ; !PartitionedSpaceFound && pRegion; pRegion=pRegion->Next) {
  4971. UCHAR SystemId = PARTITION_ENTRY_UNUSED;
  4972. #ifdef OLD_PARTITION_TABLE
  4973. SystemId = pRegion->MbrInfo->OnDiskMbr.PartitionTable[pRegion->TablePosition].SystemId;
  4974. #else
  4975. if (SPPT_IS_MBR_DISK(disk) && SPPT_IS_REGION_PARTITIONED(pRegion)) {
  4976. SystemId = SPPT_GET_PARTITION_TYPE(pRegion);
  4977. }
  4978. #endif
  4979. if(pRegion->PartitionedSpace && !IsContainerPartition(SystemId)) {
  4980. PartitionedSpaceFound = TRUE;
  4981. }
  4982. }
  4983. }
  4984. }
  4985. }
  4986. if( !PartitionedSpaceFound ) {
  4987. //
  4988. // If the disks have no partitioned regions that are not a container,
  4989. // then delete all drive letters, so that the drive letters for each CD-ROM drive
  4990. // also get deleted.
  4991. //
  4992. NTSTATUS Status;
  4993. OBJECT_ATTRIBUTES Obja;
  4994. IO_STATUS_BLOCK IoStatusBlock;
  4995. UNICODE_STRING UnicodeString;
  4996. HANDLE Handle;
  4997. INIT_OBJA(&Obja,&UnicodeString,MOUNTMGR_DEVICE_NAME);
  4998. Status = ZwOpenFile( &Handle,
  4999. (ACCESS_MASK)(FILE_GENERIC_READ | FILE_GENERIC_WRITE),
  5000. &Obja,
  5001. &IoStatusBlock,
  5002. FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
  5003. FILE_NON_DIRECTORY_FILE );
  5004. if( NT_SUCCESS( Status ) ) {
  5005. MOUNTMGR_MOUNT_POINT MountMgrMountPoint;
  5006. MountMgrMountPoint.SymbolicLinkNameOffset = 0;
  5007. MountMgrMountPoint.SymbolicLinkNameLength = 0;
  5008. MountMgrMountPoint.UniqueIdOffset = 0;
  5009. MountMgrMountPoint.UniqueIdLength = 0;
  5010. MountMgrMountPoint.DeviceNameOffset = 0;
  5011. MountMgrMountPoint.DeviceNameLength = 0;
  5012. Status = ZwDeviceIoControlFile( Handle,
  5013. NULL,
  5014. NULL,
  5015. NULL,
  5016. &IoStatusBlock,
  5017. IOCTL_MOUNTMGR_DELETE_POINTS,
  5018. &MountMgrMountPoint,
  5019. sizeof( MOUNTMGR_MOUNT_POINT ),
  5020. TemporaryBuffer,
  5021. sizeof( TemporaryBuffer ) );
  5022. if( !NT_SUCCESS( Status ) ) {
  5023. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: Unable to delete drive letters. ZwDeviceIoControl( IOCTL_MOUNTMGR_DELETE_POINTS ) failed. Status = %lx \n", Status));
  5024. } else {
  5025. //
  5026. // If the drive letters got deleted then reset the drive letters assigned to all partitions.
  5027. // Note that we only really care about resetting the drive letters on the partitions on the
  5028. // removable disks, since, if we got that far, there won't be any partition on the non-removable
  5029. // disks
  5030. //
  5031. for(disk = 0; (disk<HardDiskCount); disk++) {
  5032. if ((PartitionedDisks[disk].HardDisk)->Geometry.MediaType == RemovableMedia) {
  5033. for(pass=0; pass<2; pass++) {
  5034. pRegion = pass ? PartitionedDisks[disk].ExtendedDiskRegions : PartitionedDisks[disk].PrimaryDiskRegions;
  5035. for( ; pRegion; pRegion=pRegion->Next) {
  5036. UCHAR SystemId = SpPtGetPartitionType(pRegion);
  5037. if(pRegion->PartitionedSpace && !IsContainerPartition(SystemId)) {
  5038. pRegion->DriveLetter = 0;
  5039. }
  5040. }
  5041. }
  5042. }
  5043. }
  5044. }
  5045. ZwClose( Handle );
  5046. } else {
  5047. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: Unable to delete drive letters. ZwOpenFile( %ls ) failed. Status = %lx \n", MOUNTMGR_DEVICE_NAME, Status));
  5048. }
  5049. }
  5050. }
  5051. VOID
  5052. SpPtAssignDriveLetters(
  5053. VOID
  5054. )
  5055. {
  5056. ULONG disk;
  5057. PDISK_REGION pRegion;
  5058. unsigned pass;
  5059. //
  5060. // Before initializing the drive letters, delete them if necessary.
  5061. // This is to get rid of the letters assigned to CD-ROM drives and removables, when the disks have no
  5062. // partitioned space.
  5063. //
  5064. SpPtDeleteDriveLetters();
  5065. //
  5066. // Initialize all drive letters to nothing.
  5067. // If it the region is a partitioned space, then assign a drive letter also.
  5068. //
  5069. for(disk=0; disk<HardDiskCount; disk++) {
  5070. // assign drive letters for removeable media also for command console
  5071. if(ForceConsole || ((PartitionedDisks[disk].HardDisk)->Geometry.MediaType != RemovableMedia)) {
  5072. for(pass=0; pass<2; pass++) {
  5073. pRegion = pass ? PartitionedDisks[disk].ExtendedDiskRegions : PartitionedDisks[disk].PrimaryDiskRegions;
  5074. for( ; pRegion; pRegion=pRegion->Next) {
  5075. UCHAR SystemId = SpPtGetPartitionType(pRegion);
  5076. pRegion->DriveLetter = 0;
  5077. if(pRegion->PartitionedSpace && !IsContainerPartition(SystemId)) {
  5078. //
  5079. // Get the nt pathname for this region.
  5080. //
  5081. SpNtNameFromRegion(
  5082. pRegion,
  5083. TemporaryBuffer,
  5084. sizeof(TemporaryBuffer),
  5085. PartitionOrdinalCurrent
  5086. );
  5087. //
  5088. // Assign a drive letter for this region
  5089. //
  5090. pRegion->DriveLetter = SpGetDriveLetter( TemporaryBuffer, NULL );
  5091. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: Partition = %ls (%ls), DriveLetter = %wc: \n", TemporaryBuffer, (pass)? L"Extended" : L"Primary", pRegion->DriveLetter));
  5092. }
  5093. }
  5094. }
  5095. }
  5096. }
  5097. }
  5098. VOID
  5099. SpPtRemapDriveLetters(
  5100. IN BOOLEAN DriveAssign_AT
  5101. )
  5102. {
  5103. PWSTR p;
  5104. NTSTATUS Status;
  5105. UNICODE_STRING StartDriveLetterFrom;
  5106. UNICODE_STRING Dummy;
  5107. STRING ntDeviceName;
  5108. UCHAR deviceNameBuffer[256] = "\\Device\\Harddisk0\\Partition1";
  5109. UCHAR systemRootBuffer[256] = "C:\\$WIN_NT$.~BT";
  5110. ANSI_STRING ansiString;
  5111. BOOLEAN ForceUnmap = FALSE;
  5112. RTL_QUERY_REGISTRY_TABLE SetupTypeTable[]=
  5113. {
  5114. {NULL,
  5115. RTL_QUERY_REGISTRY_DIRECT,
  5116. L"DriveLetter",
  5117. &StartDriveLetterFrom,
  5118. REG_SZ,
  5119. &Dummy,
  5120. 0
  5121. },
  5122. {NULL,0,NULL,NULL,REG_NONE,NULL,0}
  5123. };
  5124. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: DriveAssign_AT = %d.\n",(DriveAssign_AT ? 1 : 0)));
  5125. //
  5126. // Determin whether how to drive assign is 98 (HD start is A) or
  5127. // AT (HD start C).
  5128. //
  5129. RtlInitUnicodeString(&StartDriveLetterFrom, NULL);
  5130. RtlInitUnicodeString(&Dummy, NULL);
  5131. Status = RtlQueryRegistryValues( RTL_REGISTRY_ABSOLUTE,
  5132. L"\\Registry\\MACHINE\\SYSTEM\\Setup",
  5133. SetupTypeTable,
  5134. NULL,
  5135. NULL);
  5136. if (NT_SUCCESS(Status)) {
  5137. if ((StartDriveLetterFrom.Buffer[0] == L'C') ||
  5138. (StartDriveLetterFrom.Buffer[0] == L'c')) {
  5139. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: DriveLetter is in setupreg.hiv.\n"));
  5140. if (!DriveAssign_AT) {
  5141. //
  5142. // Delete hive value "DriveLetter".
  5143. //
  5144. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: Re-assign as NEC assign.\n"));
  5145. Status = RtlDeleteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  5146. L"\\Registry\\MACHINE\\SYSTEM\\Setup",
  5147. L"DriveLetter");
  5148. if (!NT_SUCCESS(Status)) {
  5149. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: Fail to delete KEY DriveLetter.\n"));
  5150. }
  5151. }
  5152. } else {
  5153. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: There is no DriveLetter.\n"));
  5154. if (DriveAssign_AT) {
  5155. //
  5156. // Add hive value "DriveLetter" as "C".
  5157. //
  5158. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: Re-assign as AT assign.\n"));
  5159. Status = RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
  5160. L"\\Registry\\Machine\\System\\Setup",
  5161. L"DriveLetter",
  5162. REG_SZ,
  5163. L"C",
  5164. sizeof(L"C")+sizeof(WCHAR));
  5165. if (!NT_SUCCESS(Status)) {
  5166. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: Fail to add KEY DriveLetter.\n"));
  5167. }
  5168. }
  5169. }
  5170. ForceUnmap = TRUE;
  5171. }
  5172. //
  5173. // Cancel all drive letters and Remap drive letters.
  5174. //
  5175. if (ForceUnmap) {
  5176. SpPtUnAssignDriveLetters();
  5177. ntDeviceName.Buffer = deviceNameBuffer;
  5178. ntDeviceName.MaximumLength = sizeof(deviceNameBuffer);
  5179. ntDeviceName.Length = 0;
  5180. ansiString.MaximumLength = sizeof(systemRootBuffer);
  5181. ansiString.Length = 0;
  5182. ansiString.Buffer = systemRootBuffer;
  5183. IoAssignDriveLetters( *(PLOADER_PARAMETER_BLOCK *)KeLoaderBlock,
  5184. &ntDeviceName,
  5185. ansiString.Buffer,
  5186. &ansiString );
  5187. }
  5188. RtlFreeUnicodeString(&StartDriveLetterFrom);
  5189. RtlFreeUnicodeString(&Dummy);
  5190. }
  5191. VOID
  5192. SpPtUnAssignDriveLetters(
  5193. VOID
  5194. )
  5195. {
  5196. ULONG disk;
  5197. PDISK_REGION pRegion;
  5198. unsigned pass;
  5199. ULONG CdCount, cdrom, dlet;
  5200. UNICODE_STRING linkString;
  5201. WCHAR tempBuffer[] = L"\\DosDevices\\A:";
  5202. //
  5203. // Release all drive letters of device.
  5204. // If it the region is a partitioned space, then assign a drive letter also.
  5205. //
  5206. for(disk=0; disk<HardDiskCount; disk++) {
  5207. for(pass=0; pass<2; pass++) {
  5208. pRegion = pass ? PartitionedDisks[disk].ExtendedDiskRegions : PartitionedDisks[disk].PrimaryDiskRegions;
  5209. for( ; pRegion; pRegion=pRegion->Next) {
  5210. UCHAR SystemId = SpPtGetPartitionType(pRegion);
  5211. //pRegion->DriveLetter = 0;
  5212. if(pRegion->PartitionedSpace && !IsContainerPartition(SystemId)) {
  5213. //
  5214. // Get the nt pathname for this region.
  5215. //
  5216. SpNtNameFromRegion(
  5217. pRegion,
  5218. TemporaryBuffer,
  5219. sizeof(TemporaryBuffer),
  5220. PartitionOrdinalOriginal
  5221. );
  5222. //
  5223. // Assign a drive letter for this region
  5224. //
  5225. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: delete Partition = %ls (%ls), DriveLetter = %wc: \n", TemporaryBuffer, (pass)? L"Extended" : L"Primary", pRegion->DriveLetter));
  5226. SpDeleteDriveLetter( TemporaryBuffer );
  5227. pRegion->DriveLetter = 0;
  5228. }
  5229. }
  5230. }
  5231. }
  5232. if(CdCount = IoGetConfigurationInformation()->CdRomCount) {
  5233. //
  5234. // Unlink CD-ROM drive letters.
  5235. //
  5236. for(cdrom=0; cdrom<CdCount; cdrom++) {
  5237. swprintf(TemporaryBuffer,L"\\Device\\Cdrom%u",cdrom);
  5238. SpDeleteDriveLetter( TemporaryBuffer );
  5239. }
  5240. }
  5241. //
  5242. // Delete all symbolic link related in drive letter.
  5243. //
  5244. for (dlet=0; dlet<26; dlet++) {
  5245. tempBuffer[12] = (WCHAR)(L'A' + dlet);
  5246. RtlInitUnicodeString( &linkString, tempBuffer);
  5247. IoDeleteSymbolicLink (&linkString);
  5248. }
  5249. }
  5250. #ifndef NEW_PARTITION_ENGINE
  5251. VOID
  5252. SpPtDeletePartitionsForRemoteBoot(
  5253. PPARTITIONED_DISK pDisk,
  5254. PDISK_REGION startRegion,
  5255. PDISK_REGION endRegion,
  5256. BOOLEAN Extended
  5257. )
  5258. {
  5259. PDISK_REGION pRegion;
  5260. PDISK_REGION pNextDeleteRegion;
  5261. BOOLEAN passedEndRegion = FALSE;
  5262. BOOLEAN b;
  5263. #ifdef GPT_PARTITION_ENGINE
  5264. if (pDisk->HardDisk->FormatType == DISK_FORMAT_TYPE_GPT) {
  5265. SpPtnDeletePartitionsForRemoteBoot(pDisk,
  5266. startRegion,
  5267. endRegion,
  5268. Extended);
  5269. return;
  5270. }
  5271. #endif
  5272. //
  5273. // Delete all disk regions from startRegion to endRegion.
  5274. //
  5275. pRegion = startRegion;
  5276. while (pRegion) {
  5277. //
  5278. // Before deleting this region, we need to save the next region
  5279. // to delete, since the list may get modified as a result of
  5280. // deleting this one (but a partitioned region won't get
  5281. // changed, only free ones). Note that endRegion might
  5282. // be unpartitioned so we need to be careful to check for
  5283. // the exit case.
  5284. //
  5285. pNextDeleteRegion = pRegion->Next;
  5286. while (pNextDeleteRegion) {
  5287. if (pNextDeleteRegion->PartitionedSpace) {
  5288. break;
  5289. } else {
  5290. if (pNextDeleteRegion == endRegion) {
  5291. passedEndRegion = TRUE;
  5292. }
  5293. pNextDeleteRegion = pNextDeleteRegion->Next;
  5294. }
  5295. }
  5296. //
  5297. // If this is the extended partition, first kill all the
  5298. // logical drives.
  5299. //
  5300. if (IsContainerPartition(pRegion->MbrInfo->OnDiskMbr.PartitionTable[pRegion->TablePosition].SystemId)) {
  5301. ASSERT(!Extended);
  5302. SpPtDeletePartitionsForRemoteBoot(
  5303. pDisk,
  5304. pDisk->ExtendedDiskRegions,
  5305. NULL,
  5306. TRUE // used to check for another recursion
  5307. );
  5308. }
  5309. //
  5310. // Remove any boot entries pointing to this region.
  5311. //
  5312. SpPtDeleteBootSetsForRegion(pRegion);
  5313. //
  5314. // Get rid of the compressed drives, if any
  5315. //
  5316. if( pRegion->NextCompressed != NULL ) {
  5317. SpDisposeCompressedDrives( pRegion->NextCompressed );
  5318. pRegion->NextCompressed = NULL;
  5319. pRegion->MountDrive = 0;
  5320. pRegion->HostDrive = 0;
  5321. }
  5322. if (pRegion->PartitionedSpace) {
  5323. b = SpPtDelete(pRegion->DiskNumber,pRegion->StartSector);
  5324. }
  5325. ASSERT(b);
  5326. if ((pRegion == endRegion) ||
  5327. passedEndRegion) {
  5328. break;
  5329. }
  5330. pRegion = pNextDeleteRegion;
  5331. }
  5332. }
  5333. #endif // ! NEW_PARTITION_ENGINE
  5334. NTSTATUS
  5335. SpPtPartitionDiskForRemoteBoot(
  5336. IN ULONG DiskNumber,
  5337. OUT PDISK_REGION *RemainingRegion
  5338. )
  5339. {
  5340. PPARTITIONED_DISK pDisk;
  5341. PDISK_REGION pRegion;
  5342. ULONG PartitionCount = 0;
  5343. ULONGLONG firstRegionStartSector;
  5344. PDISK_REGION firstRegion = NULL, lastRegion = NULL;
  5345. BOOLEAN IsGPTDisk = FALSE;
  5346. pDisk = &PartitionedDisks[DiskNumber];
  5347. IsGPTDisk = SPPT_IS_GPT_DISK(DiskNumber);
  5348. //
  5349. // Scan through the disk and see how many contiguous recognized
  5350. // partitions there are.
  5351. //
  5352. if (pDisk->HardDisk->Status == DiskOffLine) {
  5353. return STATUS_DEVICE_OFF_LINE;
  5354. }
  5355. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  5356. "SpPtPartitionDiskForRemoteBoot: cylinder size is %lx\n",
  5357. pDisk->HardDisk->SectorsPerCylinder));
  5358. pRegion = pDisk->PrimaryDiskRegions;
  5359. for( ; pRegion; pRegion=pRegion->Next) {
  5360. if (!pRegion->PartitionedSpace) {
  5361. //
  5362. // If the region is not partitioned, then add it to our list
  5363. // to merge if we have one.
  5364. //
  5365. if (firstRegion) {
  5366. //
  5367. // If this is a final free region covering the last
  5368. // partial cylinder on the disk, then don't add it.
  5369. //
  5370. if ((pRegion->Next == NULL) &&
  5371. (pRegion->SectorCount < pDisk->HardDisk->SectorsPerCylinder) &&
  5372. ((pRegion->StartSector % pDisk->HardDisk->SectorsPerCylinder) == 0)) {
  5373. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  5374. "Skipping final partial cylinder free region %lx for %lx\n",
  5375. pRegion->StartSector, pRegion->SectorCount));
  5376. } else {
  5377. lastRegion = pRegion;
  5378. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  5379. "Adding free region %lx for %lx\n",
  5380. pRegion->StartSector, pRegion->SectorCount));
  5381. }
  5382. } else {
  5383. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  5384. "Skipping free region %lx for %lx\n",
  5385. pRegion->StartSector, pRegion->SectorCount));
  5386. }
  5387. } else {
  5388. PON_DISK_PTE pte;
  5389. UCHAR SystemId = 0;
  5390. if (IsGPTDisk) {
  5391. if (SPPT_IS_RECOGNIZED_FILESYSTEM(pRegion->Filesystem)) {
  5392. //
  5393. // TBD : Fix for cases where FT / Dynamic volumes can
  5394. // reside on the GPT disk
  5395. //
  5396. SystemId = PARTITION_FAT32;
  5397. } else {
  5398. SystemId = PARTITION_ENTRY_UNUSED;
  5399. }
  5400. } else {
  5401. SystemId = SpPtGetPartitionType(pRegion);
  5402. }
  5403. if (IsContainerPartition(SystemId)) {
  5404. //
  5405. // If this is the extended partition, we want to remove it.
  5406. //
  5407. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  5408. "Adding extended region [type %d] %lx for %lx\n",
  5409. SystemId, pRegion->StartSector, pRegion->SectorCount));
  5410. if (!firstRegion) {
  5411. firstRegion = pRegion;
  5412. }
  5413. lastRegion = pRegion;
  5414. } else if ((PartitionNameIds[SystemId] == (UCHAR)(-1)) ||
  5415. (PartitionNameIds[SystemId] == PT(VERIT))) {
  5416. //
  5417. // For a recognized partition, remove it if we have already found
  5418. // a firstRegion; otherwise we will start our list with this
  5419. // region.
  5420. //
  5421. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  5422. "Adding recognized region [type %d] %lx for %lx\n",
  5423. SystemId, pRegion->StartSector, pRegion->SectorCount));
  5424. if (!firstRegion) {
  5425. firstRegion = pRegion;
  5426. }
  5427. lastRegion = pRegion;
  5428. } else {
  5429. //
  5430. // If the partition is *not* recognized, and we have a list we
  5431. // have been keeping, then stop before this one, otherwise
  5432. // skip it.
  5433. //
  5434. if (firstRegion) {
  5435. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  5436. "Stopping at unrecognized region [type %d] %lx for %lx\n",
  5437. SystemId, pRegion->StartSector, pRegion->SectorCount));
  5438. break;
  5439. } else {
  5440. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  5441. "Skipping unrecognized region [type %d] %lx for %lx\n",
  5442. SystemId, pRegion->StartSector, pRegion->SectorCount));
  5443. }
  5444. }
  5445. }
  5446. }
  5447. //
  5448. // We should have found at least one region. If we didn't then the
  5449. // disk is alternating unpartitioned and unrecognized regions. In this
  5450. // case, use the largest unpartitioned region.
  5451. //
  5452. if (firstRegion == NULL) {
  5453. ULONGLONG BiggestUnpartitionedSectorCount = 0;
  5454. pRegion = pDisk->PrimaryDiskRegions;
  5455. for( ; pRegion; pRegion=pRegion->Next) {
  5456. if (!pRegion->PartitionedSpace) {
  5457. if (pRegion->SectorCount > BiggestUnpartitionedSectorCount) {
  5458. firstRegion = pRegion;
  5459. BiggestUnpartitionedSectorCount = pRegion->SectorCount;
  5460. }
  5461. }
  5462. }
  5463. if (firstRegion == NULL) {
  5464. return STATUS_DEVICE_OFF_LINE;
  5465. }
  5466. lastRegion = firstRegion;
  5467. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  5468. "Adding single free region %lx for %lx\n",
  5469. firstRegion->StartSector, firstRegion->SectorCount));
  5470. }
  5471. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  5472. "first is %lx, last is %lx\n", firstRegion, lastRegion));
  5473. //
  5474. // If we found exactly one region and it has a known filesystem on
  5475. // it, then we don't need to do any repartitioning. We still delete
  5476. // if the filesystem is unknown because later in setup there are
  5477. // some checks that the Filesystem is valid for this region, so by
  5478. // deleting it here we will ensure that Filesystem becomes
  5479. // NewlyCreated which is considered acceptable.
  5480. //
  5481. // We also don't need to repartition if we have just one region
  5482. // and it is already unpartitioned.
  5483. //
  5484. if (firstRegion == lastRegion) {
  5485. SpPtDeleteBootSetsForRegion(firstRegion);
  5486. if (!firstRegion->PartitionedSpace) {
  5487. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  5488. "One region, unpartitioned, not repartitioning\n"));
  5489. *RemainingRegion = firstRegion;
  5490. return STATUS_SUCCESS;
  5491. } else if ((firstRegion->Filesystem == FilesystemNtfs) ||
  5492. (firstRegion->Filesystem == FilesystemFat) ||
  5493. (firstRegion->Filesystem == FilesystemFat32)) {
  5494. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL,
  5495. "One region, filesystem %d, not repartitioning\n",
  5496. firstRegion->Filesystem));
  5497. *RemainingRegion = firstRegion;
  5498. return STATUS_SUCCESS;
  5499. }
  5500. }
  5501. //
  5502. // We need to remove all the regions between firstRegion and
  5503. // lastRegion. Save the start sector of firstRegion for later,
  5504. // since after this call firstRegion may be invalid.
  5505. //
  5506. firstRegionStartSector = firstRegion->StartSector;
  5507. SpPtDeletePartitionsForRemoteBoot(
  5508. pDisk,
  5509. firstRegion,
  5510. lastRegion,
  5511. FALSE // these are not extended regions
  5512. );
  5513. //
  5514. // Now we need to find the region occupying the space we have
  5515. // freed. We scan for the region that includes firstRegionStartSector
  5516. // (the region we find may start before then if there was a small free
  5517. // region before it).
  5518. //
  5519. for (pRegion = pDisk->PrimaryDiskRegions;
  5520. pRegion;
  5521. pRegion=pRegion->Next) {
  5522. if (pRegion->StartSector <= firstRegionStartSector) {
  5523. firstRegion = pRegion;
  5524. } else {
  5525. break;
  5526. }
  5527. }
  5528. //
  5529. // Return this -- SpPtPrepareDisks handles the case where the
  5530. // selected region is free.
  5531. //
  5532. *RemainingRegion = firstRegion;
  5533. return STATUS_SUCCESS;
  5534. }
  5535. //
  5536. // Hard Disk Inialize data for NEC98
  5537. //
  5538. #define IPL_SIZE 0x8000 //NEC98
  5539. NTSTATUS
  5540. SpInitializeHardDisk_Nec98(
  5541. IN PDISK_REGION pRegionDisk
  5542. )
  5543. {
  5544. PHARD_DISK pHardDisk;
  5545. WCHAR DevicePath[(sizeof(DISK_DEVICE_NAME_BASE)+sizeof(L"000"))/sizeof(WCHAR)];
  5546. ULONG i,bps;
  5547. HANDLE Handle;
  5548. NTSTATUS Sts;
  5549. PUCHAR Buffer,UBuffer;
  5550. ULONG buffersize;
  5551. ULONG sectoraddress;
  5552. PUCHAR HdutlBuffer;
  5553. IO_STATUS_BLOCK IoStatusBlock;
  5554. pHardDisk = &HardDisks[pRegionDisk->DiskNumber];
  5555. bps = HardDisks[pRegionDisk->DiskNumber].Geometry.BytesPerSector;
  5556. Sts = SpOpenPartition0(pHardDisk->DevicePath,&Handle,TRUE);
  5557. if(!NT_SUCCESS(Sts)) {
  5558. return(Sts);
  5559. }
  5560. //
  5561. // Initialize Hard Disk
  5562. //
  5563. if(bps==256){
  5564. bps=512;
  5565. }
  5566. HdutlBuffer = SpMemAlloc(IPL_SIZE);
  5567. if(!HdutlBuffer) {
  5568. SpMemFree(HdutlBuffer);
  5569. ZwClose(Handle);
  5570. return(STATUS_NO_MEMORY);
  5571. }
  5572. RtlZeroMemory(HdutlBuffer,IPL_SIZE);
  5573. //
  5574. // Clear head of hard drive, instead of Physical Format.
  5575. //
  5576. Sts = SpReadWriteDiskSectors(Handle,0,(ULONG)(IPL_SIZE/bps),bps,HdutlBuffer,TRUE);
  5577. if(!NT_SUCCESS(Sts)) {
  5578. SpMemFree(HdutlBuffer);
  5579. ZwClose(Handle);
  5580. return(Sts);
  5581. }
  5582. //
  5583. // Set IPL Information
  5584. //
  5585. //
  5586. // Write Boot Code
  5587. //
  5588. sectoraddress=0;
  5589. switch(bps){
  5590. case 2048: buffersize=0x800; break;
  5591. case 1024: buffersize=0x400; break;
  5592. case 256: buffersize=0x100; break;
  5593. case 512: buffersize=0x200; break;
  5594. default : buffersize=0x800; //***max***
  5595. bps=0x800;
  5596. }
  5597. Sts = SpReadWriteDiskSectors(Handle,sectoraddress,(ULONG)(buffersize/bps),bps,x86PC98BootCode,TRUE);
  5598. if(!NT_SUCCESS(Sts)) {
  5599. SpMemFree(HdutlBuffer);
  5600. ZwClose(Handle);
  5601. return(Sts);
  5602. }
  5603. //
  5604. // Write Volume Info
  5605. //
  5606. sectoraddress=1;
  5607. switch(bps){
  5608. case 2048: buffersize=0x800; break; //***1sec***
  5609. case 1024: buffersize=0xc00; break; //***3sec***
  5610. case 256: buffersize=0x300; break; //***3sec***
  5611. case 512: buffersize=0x200; break; //***1sec***
  5612. default : buffersize=0x800; //***max****
  5613. }
  5614. Sts = SpReadWriteDiskSectors(Handle,sectoraddress,(ULONG)(buffersize/bps),bps,HdutlBuffer,TRUE);
  5615. if(!NT_SUCCESS(Sts)) {
  5616. SpMemFree(HdutlBuffer);
  5617. ZwClose(Handle);
  5618. return(Sts);
  5619. }
  5620. //
  5621. // Write Boot Menu
  5622. //
  5623. switch(bps){
  5624. case 2048: buffersize=0x2000; //***8KB***
  5625. sectoraddress=2;
  5626. break;
  5627. case 1024: buffersize=0x2000; //***8KB***
  5628. sectoraddress=4;
  5629. break;
  5630. case 256: buffersize=0x1c00; //***7KB***
  5631. sectoraddress=4;
  5632. break;
  5633. case 512: buffersize=0x1c00; //***7KB***
  5634. sectoraddress=2;
  5635. break;
  5636. default : buffersize=0x1c00; //***min***
  5637. }
  5638. Sts = SpReadWriteDiskSectors(Handle,sectoraddress,(ULONG)(buffersize/bps),bps,x86PC98BootMenu,TRUE);
  5639. if(!NT_SUCCESS(Sts)) {
  5640. SpMemFree(HdutlBuffer);
  5641. return(Sts);
  5642. }
  5643. //
  5644. // Write NTFT Signature.
  5645. //
  5646. RtlZeroMemory(HdutlBuffer,bps);
  5647. ((PULONG)HdutlBuffer)[0] = SpComputeSerialNumber();
  5648. ((PUSHORT)HdutlBuffer)[bps/2 - 1] = BOOT_RECORD_SIGNATURE;
  5649. Sts = SpReadWriteDiskSectors(Handle,16,1,bps,HdutlBuffer,TRUE);
  5650. if(!NT_SUCCESS(Sts)) {
  5651. SpMemFree(HdutlBuffer);
  5652. ZwClose(Handle);
  5653. return(Sts);
  5654. }
  5655. SpMemFree(HdutlBuffer);
  5656. ZwClose(Handle);
  5657. //
  5658. // Do ShutDown
  5659. //
  5660. SpDisplayScreen(SP_SCRN_INIT_REQUIRES_REBOOT_NEC98,3,4);
  5661. SpDisplayStatusOptions(
  5662. DEFAULT_STATUS_ATTRIBUTE,
  5663. SP_STAT_F3_EQUALS_REBOOT,
  5664. 0
  5665. );
  5666. SpInputDrain();
  5667. while(SpInputGetKeypress() != KEY_F3) ;
  5668. HalReturnToFirmware(HalRebootRoutine);
  5669. return(STATUS_SUCCESS);
  5670. }
  5671. VOID
  5672. SpReassignOnDiskOrdinals(
  5673. IN PPARTITIONED_DISK pDisk
  5674. )
  5675. {
  5676. #if defined(NEC_98) //NEC98
  5677. PMBR_INFO pBrInfo;
  5678. ULONG i;
  5679. for(pBrInfo=&pDisk->MbrInfo; pBrInfo; pBrInfo=pBrInfo->Next) {
  5680. for(i=0; i<PTABLE_DIMENSION; i++) {
  5681. PON_DISK_PTE pte = &pBrInfo->OnDiskMbr.PartitionTable[i];
  5682. if((pte->SystemId != PARTITION_ENTRY_UNUSED)
  5683. && !IsContainerPartition(pte->SystemId)) {
  5684. //
  5685. // Reset real disk potition into OnDiskordinals.
  5686. // RealDiskPosition value is zero origin, but partition
  5687. // number start one.
  5688. //
  5689. pBrInfo->OnDiskOrdinals[i] = pte->RealDiskPosition + 1;
  5690. } else {
  5691. pBrInfo->OnDiskOrdinals[i] = 0;
  5692. }
  5693. }
  5694. }
  5695. #endif //NEC98
  5696. }
  5697. //
  5698. // Now, only for NEC98.
  5699. //
  5700. VOID
  5701. SpTranslatePteInfo(
  5702. IN PON_DISK_PTE pPte,
  5703. IN PREAL_DISK_PTE pRealPte,
  5704. IN BOOLEAN Write // into real PTE
  5705. )
  5706. {
  5707. ASSERT(pRealPte);
  5708. ASSERT(pPte);
  5709. if( Write ) {
  5710. //
  5711. // Initialize PTE
  5712. //
  5713. RtlZeroMemory(pRealPte, sizeof(REAL_DISK_PTE));
  5714. //
  5715. // Copy PTE entries from real on-disk PTE.
  5716. //
  5717. pRealPte->ActiveFlag = pPte->ActiveFlag;
  5718. pRealPte->StartHead = pPte->StartHead;
  5719. pRealPte->StartSector = pPte->StartSector;
  5720. pRealPte->StartCylinder = pPte->StartCylinder;
  5721. pRealPte->SystemId = pPte->SystemId;
  5722. pRealPte->EndHead = pPte->EndHead;
  5723. pRealPte->EndSector = pPte->EndSector;
  5724. pRealPte->EndCylinder = pPte->EndCylinder;
  5725. RtlMoveMemory(&pRealPte->RelativeSectors, &pPte->RelativeSectors,
  5726. sizeof(pPte->RelativeSectors)); //4
  5727. RtlMoveMemory(&pRealPte->SectorCount, &pPte->SectorCount,
  5728. sizeof(pPte->SectorCount)); //4
  5729. } else {
  5730. //
  5731. // Initialize PTE
  5732. //
  5733. RtlZeroMemory(pPte, sizeof(ON_DISK_PTE));
  5734. //
  5735. // Copy PTE entries from real on-disk PTE.
  5736. //
  5737. pPte->ActiveFlag = pRealPte->ActiveFlag;
  5738. pPte->StartHead = pRealPte->StartHead;
  5739. pPte->StartSector = pRealPte->StartSector;
  5740. pPte->StartCylinder = pRealPte->StartCylinder;
  5741. pPte->SystemId = pRealPte->SystemId;
  5742. pPte->EndHead = pRealPte->EndHead;
  5743. pPte->EndSector = pRealPte->EndSector;
  5744. pPte->EndCylinder = pRealPte->EndCylinder;
  5745. RtlMoveMemory(&pPte->RelativeSectors, &pRealPte->RelativeSectors,
  5746. sizeof(pRealPte->RelativeSectors)); //4
  5747. RtlMoveMemory(&pPte->SectorCount, &pRealPte->SectorCount,
  5748. sizeof(pPte->SectorCount)); //4
  5749. }
  5750. }
  5751. //
  5752. // Now, only for NEC98.
  5753. //
  5754. VOID
  5755. SpTranslateMbrInfo(
  5756. IN PON_DISK_MBR pMbr,
  5757. IN PREAL_DISK_MBR pRealMbr,
  5758. IN ULONG bps,
  5759. IN BOOLEAN Write // into real MBR
  5760. )
  5761. {
  5762. PREAL_DISK_PTE pRealPte;
  5763. PON_DISK_PTE pPte;
  5764. ULONG TmpData;
  5765. ULONG i;
  5766. pRealPte = pRealMbr->PartitionTable;
  5767. pPte = pMbr->PartitionTable;
  5768. ASSERT(pRealMbr);
  5769. ASSERT(pMbr);
  5770. if( Write ) {
  5771. //
  5772. // Initialize REAL_DISK_MBR
  5773. //
  5774. RtlZeroMemory(pRealMbr, sizeof(REAL_DISK_MBR));
  5775. //
  5776. // Copy MBR entries into real on-disk MBR.
  5777. //
  5778. RtlMoveMemory(&pRealMbr->BootCode, &pMbr->BootCode,
  5779. sizeof(pMbr->BootCode)); //440
  5780. RtlMoveMemory(&pRealMbr->NTFTSignature, &pMbr->NTFTSignature,
  5781. sizeof(pMbr->NTFTSignature)); //4
  5782. RtlMoveMemory(&pRealMbr->Filler, &pMbr->Filler,
  5783. sizeof(pMbr->Filler)); //2
  5784. RtlMoveMemory(&pRealMbr->AA55Signature, &pMbr->AA55Signature,
  5785. sizeof(pMbr->AA55Signature)); //2
  5786. } else {
  5787. //
  5788. // Initialize ON_DISK_MBR
  5789. //
  5790. RtlZeroMemory(pMbr, sizeof(ON_DISK_MBR));
  5791. //
  5792. // Copy MBR entries from real on-disk MBR.
  5793. //
  5794. RtlMoveMemory(&pMbr->BootCode, &pRealMbr->BootCode,
  5795. sizeof(pMbr->BootCode)); //440
  5796. RtlMoveMemory(&pMbr->NTFTSignature, &pRealMbr->NTFTSignature,
  5797. sizeof(pMbr->NTFTSignature)); //4
  5798. RtlMoveMemory(&pMbr->Filler, &pRealMbr->Filler,
  5799. sizeof(pMbr->Filler)); //2
  5800. RtlMoveMemory(&pMbr->AA55Signature, &pRealMbr->AA55Signature,
  5801. sizeof(pMbr->AA55Signature)); //2
  5802. }
  5803. //
  5804. // Translate PTEs from real on-disk PTEs.
  5805. //
  5806. for(i=0; i<NUM_PARTITION_TABLE_ENTRIES; i++) {
  5807. SpTranslatePteInfo(&pPte[i], &pRealPte[i], Write);
  5808. }
  5809. }
  5810. VOID
  5811. ConvertPartitionTable(
  5812. IN PPARTITIONED_DISK pDisk,
  5813. IN PUCHAR Buffer,
  5814. IN ULONG bps
  5815. )
  5816. {
  5817. #if defined(NEC_98) //NEC98
  5818. PREAL_DISK_PTE_NEC98 PteNec;
  5819. PON_DISK_PTE p;
  5820. ULONG TmpData;
  5821. ULONG i;
  5822. PteNec = (PREAL_DISK_PTE_NEC98)(Buffer + bps);
  5823. p = pDisk->MbrInfo.OnDiskMbr.PartitionTable;
  5824. for(i=0; i<PTABLE_DIMENSION; i++) {
  5825. switch (PteNec[i].SystemId){
  5826. case 0x00: // not use
  5827. p[i].SystemId = PARTITION_ENTRY_UNUSED;
  5828. break;
  5829. case 0x01: // FAT 12bit
  5830. case 0x81:
  5831. p[i].SystemId = PARTITION_FAT_12;
  5832. break;
  5833. case 0x11: // FAT 16bit
  5834. case 0x91:
  5835. p[i].SystemId = PARTITION_FAT_16;
  5836. break;
  5837. case 0x21: // FAT huge
  5838. case 0xa1:
  5839. p[i].SystemId = PARTITION_HUGE;
  5840. break;
  5841. case 0x31: // IFS
  5842. case 0xb1:
  5843. p[i].SystemId = PARTITION_IFS;
  5844. break;
  5845. case 0x41: // IFS 2nd,orphan
  5846. case 0xc1:
  5847. p[i].SystemId = (PARTITION_IFS | PARTITION_NTFT);
  5848. break;
  5849. case 0x51: // IFS deleted
  5850. case 0xd1:
  5851. p[i].SystemId = (PARTITION_IFS | VALID_NTFT);
  5852. break;
  5853. case 0x61: // FAT32
  5854. case 0xe1:
  5855. p[i].SystemId = PARTITION_FAT32;
  5856. break;
  5857. case 0x08: // FAT 12bit 2nd,orphan
  5858. case 0x88:
  5859. p[i].SystemId = (PARTITION_FAT_12 | PARTITION_NTFT);
  5860. break;
  5861. case 0x18: // FAT 12bit deleted
  5862. case 0x98:
  5863. p[i].SystemId = (PARTITION_FAT_12 | VALID_NTFT);
  5864. break;
  5865. case 0x28: // FAT 16bit 2nd,orphan
  5866. case 0xa8:
  5867. p[i].SystemId = (PARTITION_FAT_16 | PARTITION_NTFT);
  5868. break;
  5869. case 0x38: // FAT 16bit deleted
  5870. case 0xb8:
  5871. p[i].SystemId = (PARTITION_FAT_16 | VALID_NTFT);
  5872. break;
  5873. case 0x48: // FAT huge 2nd,orphan
  5874. case 0xc8:
  5875. p[i].SystemId = (PARTITION_HUGE | PARTITION_NTFT);
  5876. break;
  5877. case 0x58: // FAT huge deleted
  5878. case 0xd8:
  5879. p[i].SystemId = (PARTITION_HUGE | VALID_NTFT);
  5880. break;
  5881. case 0x68: // LDM partition
  5882. case 0xe8:
  5883. p[i].SystemId = PARTITION_LDM;
  5884. break;
  5885. default: // other
  5886. p[i].SystemId = PARTITION_XENIX_1;
  5887. }
  5888. if(p[i].SystemId == PARTITION_ENTRY_UNUSED) {
  5889. p[i].ActiveFlag = 0x00;
  5890. p[i].StartHead = 0x00;
  5891. p[i].StartSector = 0x00;
  5892. p[i].StartCylinderLow = 0x00;
  5893. p[i].StartCylinderHigh = 0x00;
  5894. p[i].EndHead = 0x00;
  5895. p[i].EndSector = 0x00;
  5896. p[i].EndCylinderLow = 0x00;
  5897. p[i].EndCylinderHigh = 0x00;
  5898. p[i].RelativeSectors[0] = 0x00;
  5899. p[i].RelativeSectors[1] = 0x00;
  5900. p[i].RelativeSectors[2] = 0x00;
  5901. p[i].RelativeSectors[3] = 0x00;
  5902. p[i].SectorCount[0] = 0x00;
  5903. p[i].SectorCount[1] = 0x00;
  5904. p[i].SectorCount[2] = 0x00;
  5905. p[i].SectorCount[3] = 0x00;
  5906. p[i].IPLSector = 0x00;
  5907. p[i].IPLHead = 0x00;
  5908. p[i].IPLCylinderLow = 0x00;
  5909. p[i].IPLCylinderHigh = 0x00;
  5910. //p[i].Reserved[2] = 0x00;
  5911. p[i].Reserved[0] = 0x00;
  5912. p[i].Reserved[1] = 0x00;
  5913. p[i].OldSystemId = 0x00;
  5914. memset(p[i].SystemName,0,16);
  5915. } else {
  5916. p[i].ActiveFlag = (PteNec[i].ActiveFlag & 0x80);
  5917. p[i].StartHead = PteNec[i].StartHead;
  5918. p[i].StartSector = PteNec[i].StartSector;
  5919. p[i].StartCylinderLow = PteNec[i].StartCylinderLow;
  5920. p[i].StartCylinderHigh = PteNec[i].StartCylinderHigh;
  5921. p[i].EndHead = PteNec[i].EndHead;
  5922. p[i].EndSector = PteNec[i].EndSector;
  5923. p[i].EndCylinderLow = PteNec[i].EndCylinderLow;
  5924. p[i].EndCylinderHigh = PteNec[i].EndCylinderHigh;
  5925. p[i].IPLSector = PteNec[i].IPLSector;
  5926. p[i].IPLHead = PteNec[i].IPLHead;
  5927. p[i].IPLCylinderLow = PteNec[i].IPLCylinderLow;
  5928. p[i].IPLCylinderHigh = PteNec[i].IPLCylinderHigh;
  5929. p[i].Reserved[0] = PteNec[i].Reserved[0];
  5930. p[i].Reserved[1] = PteNec[i].Reserved[1];
  5931. p[i].OldSystemId = PteNec[i].SystemId;
  5932. memcpy(p[i].SystemName , PteNec[i].SystemName , 16);
  5933. TmpData = (ULONG)PteNec[i].StartCylinderLow;
  5934. TmpData |= ((ULONG)PteNec[i].StartCylinderHigh << 8);
  5935. U_ULONG(p[i].RelativeSectors) = RtlEnlargedUnsignedMultiply(TmpData,
  5936. pDisk->HardDisk->SectorsPerCylinder).LowPart;
  5937. TmpData = (ULONG)(PteNec[i].EndCylinderLow + 1);
  5938. // In case of Low is 0xFF, Overflowed bit will be loss by OR.
  5939. TmpData += ((ULONG)PteNec[i].EndCylinderHigh << 8);
  5940. U_ULONG(p[i].SectorCount) = RtlEnlargedUnsignedMultiply(TmpData,
  5941. pDisk->HardDisk->SectorsPerCylinder).LowPart - U_ULONG(p[i].RelativeSectors);
  5942. //
  5943. // Set Ipl Address
  5944. //
  5945. TmpData = (ULONG)PteNec[i].IPLCylinderLow;
  5946. TmpData |= ((ULONG)PteNec[i].IPLCylinderHigh << 8);
  5947. TmpData = RtlEnlargedUnsignedMultiply(TmpData,pDisk->HardDisk->SectorsPerCylinder).LowPart;
  5948. TmpData += (ULONG)(PteNec[i].IPLHead * pDisk->HardDisk->Geometry.SectorsPerTrack);
  5949. TmpData += PteNec[i].IPLSector;
  5950. U_ULONG(p[i].IPLSectors) = TmpData;
  5951. }
  5952. }
  5953. U_USHORT(pDisk->MbrInfo.OnDiskMbr.AA55Signature) = ((PUSHORT)Buffer)[bps/2 - 1];
  5954. if(bps == 256){
  5955. U_USHORT(pDisk->MbrInfo.OnDiskMbr.AA55Signature) = 0x0000;
  5956. }
  5957. #endif //NEC98
  5958. }
  5959. #define IPL_SIGNATURE_NEC98 "IPL1"
  5960. VOID
  5961. SpDetermineFormatTypeNec98(
  5962. IN PPARTITIONED_DISK pDisk,
  5963. IN PREAL_DISK_MBR_NEC98 pRealMbrNec98
  5964. )
  5965. {
  5966. UCHAR FormatType;
  5967. if(!IsNEC_98) {
  5968. FormatType = DISK_FORMAT_TYPE_PCAT;
  5969. } else {
  5970. if(pDisk->HardDisk->Characteristics & FILE_REMOVABLE_MEDIA) {
  5971. //
  5972. // All removable media are AT format.
  5973. //
  5974. FormatType = DISK_FORMAT_TYPE_PCAT;
  5975. } else {
  5976. if(U_USHORT(pRealMbrNec98->AA55Signature) == MBR_SIGNATURE) {
  5977. if(!_strnicmp(pRealMbrNec98->IPLSignature,IPL_SIGNATURE_NEC98,
  5978. sizeof(IPL_SIGNATURE_NEC98)-1)) {
  5979. //
  5980. // NEC98-format requires AA55Signature and "IPL1".
  5981. //
  5982. FormatType = DISK_FORMAT_TYPE_NEC98;
  5983. } else {
  5984. FormatType = DISK_FORMAT_TYPE_PCAT;
  5985. }
  5986. } else {
  5987. FormatType = DISK_FORMAT_TYPE_UNKNOWN;
  5988. }
  5989. }
  5990. }
  5991. pDisk->HardDisk->FormatType = FormatType;
  5992. #if 0
  5993. pDisk->HardDisk->MaxPartitionTables = ((FormatType == DISK_FORMAT_TYPE_PCAT) ?
  5994. NUM_PARTITION_TABLE_ENTRIES : NUM_PARTITION_TABLE_ENTRIES_NEC98);
  5995. #endif //0
  5996. return;
  5997. }
  5998. NTSTATUS
  5999. SpPtSearchLocalSourcesInDynamicDisk(
  6000. IN ULONG disk
  6001. )
  6002. {
  6003. NTSTATUS Status;
  6004. UNICODE_STRING UnicodeString;
  6005. OBJECT_ATTRIBUTES Obja;
  6006. HANDLE DirectoryHandle;
  6007. BOOLEAN RestartScan;
  6008. ULONG Context;
  6009. BOOLEAN MoreEntries;
  6010. POBJECT_DIRECTORY_INFORMATION DirInfo;
  6011. //
  6012. // Open the \ArcName directory.
  6013. //
  6014. INIT_OBJA(&Obja,&UnicodeString,HardDisks[disk].DevicePath);
  6015. Status = ZwOpenDirectoryObject(&DirectoryHandle,DIRECTORY_ALL_ACCESS,&Obja);
  6016. if(NT_SUCCESS(Status)) {
  6017. RestartScan = TRUE;
  6018. Context = 0;
  6019. MoreEntries = TRUE;
  6020. do {
  6021. Status = SpQueryDirectoryObject(
  6022. DirectoryHandle,
  6023. RestartScan,
  6024. &Context
  6025. );
  6026. if(NT_SUCCESS(Status)) {
  6027. PWSTR DirectoryName;
  6028. DirInfo = (POBJECT_DIRECTORY_INFORMATION)
  6029. ((PSERVICE_QUERY_DIRECTORY_OBJECT)&CommunicationParams->Buffer)->Buffer;
  6030. wcsncpy(TemporaryBuffer,DirInfo->Name.Buffer,DirInfo->Name.Length / sizeof(WCHAR));
  6031. (TemporaryBuffer)[DirInfo->Name.Length/sizeof(WCHAR)] = 0;
  6032. DirectoryName = SpDupStringW(TemporaryBuffer);
  6033. SpStringToLower(TemporaryBuffer);
  6034. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: Checking directory object %ws\\%ws \n", HardDisks[disk].DevicePath, DirectoryName));
  6035. if( _wcsicmp(TemporaryBuffer,L"partition0") &&
  6036. wcsstr(TemporaryBuffer,L"partition") ) {
  6037. FilesystemType FsType;
  6038. WCHAR FsName[32];
  6039. ULONG NameId;
  6040. ULONG PartitionNumber;
  6041. PartitionNumber = SpStringToLong( DirectoryName + ((sizeof(L"partition") - sizeof(WCHAR)) / sizeof(WCHAR)),
  6042. NULL,
  6043. 10 );
  6044. FsType = SpIdentifyFileSystem( HardDisks[disk].DevicePath,
  6045. HardDisks[disk].Geometry.BytesPerSector,
  6046. PartitionNumber );
  6047. NameId = SP_TEXT_FS_NAME_BASE + FsType;
  6048. SpFormatMessage( FsName,
  6049. sizeof(FsName),
  6050. NameId );
  6051. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: File system in dynamic volume %ws\\%ws is %ws. \n", HardDisks[disk].DevicePath, DirectoryName, FsName));
  6052. if( FsType >= FilesystemFirstKnown ) {
  6053. PWSTR LocalSourceFiles[1] = { LocalSourceDirectory };
  6054. wcscpy( TemporaryBuffer,HardDisks[disk].DevicePath );
  6055. SpConcatenatePaths( TemporaryBuffer,DirectoryName );
  6056. if(SpNFilesExist(TemporaryBuffer,LocalSourceFiles,ELEMENT_COUNT(LocalSourceFiles),TRUE)) {
  6057. //
  6058. // Found local source directory
  6059. //
  6060. PDISK_REGION pRegion;
  6061. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: Found %ws in dynamic volume %ws\\%ws. \n", LocalSourceDirectory, HardDisks[disk].DevicePath, DirectoryName));
  6062. pRegion = SpPtAllocateDiskRegionStructure( disk,
  6063. 0,
  6064. 0,
  6065. TRUE,
  6066. NULL,
  6067. PartitionNumber );
  6068. pRegion->DynamicVolume = TRUE;
  6069. pRegion->DynamicVolumeSuitableForOS = FALSE;
  6070. pRegion->IsLocalSource = TRUE;
  6071. pRegion->Filesystem = FsType;
  6072. LocalSourceRegion = pRegion;
  6073. MoreEntries = FALSE;
  6074. }
  6075. }
  6076. }
  6077. SpMemFree( DirectoryName );
  6078. } else {
  6079. MoreEntries = FALSE;
  6080. if(Status == STATUS_NO_MORE_ENTRIES) {
  6081. Status = STATUS_SUCCESS;
  6082. }
  6083. }
  6084. RestartScan = FALSE;
  6085. } while(MoreEntries);
  6086. ZwClose(DirectoryHandle);
  6087. } else {
  6088. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: Unable to open %ws directory. Status = %lx\n", HardDisks[disk].DevicePath, Status));
  6089. }
  6090. return( Status );
  6091. }
  6092. VOID
  6093. SpPtFindLocalSourceRegionOnDynamicVolumes(
  6094. VOID
  6095. )
  6096. {
  6097. ULONG disk;
  6098. PPARTITIONED_DISK partdisk;
  6099. PDISK_REGION pRegion;
  6100. BOOLEAN DiskIsDynamic;
  6101. ULONG pass;
  6102. ASSERT(HardDisksDetermined);
  6103. //
  6104. // For each hard disk attached to the system, read its partition table.
  6105. //
  6106. for(disk=0; disk<HardDiskCount && !LocalSourceRegion; disk++) {
  6107. partdisk = &PartitionedDisks[disk];
  6108. DiskIsDynamic = FALSE;
  6109. for( pass=0;
  6110. (pass < 2) && !DiskIsDynamic;
  6111. pass++ ) {
  6112. for( pRegion = ((pass == 0)? partdisk->PrimaryDiskRegions : partdisk->ExtendedDiskRegions);
  6113. pRegion && !DiskIsDynamic;
  6114. pRegion = pRegion->Next ) {
  6115. if( pRegion->DynamicVolume ) {
  6116. //
  6117. // This is a dynamic disk.
  6118. //
  6119. DiskIsDynamic = TRUE;
  6120. //
  6121. // Scan all dynamic volumes in the disk for the $win_nt$.~ls
  6122. //
  6123. SpPtSearchLocalSourcesInDynamicDisk( disk );
  6124. }
  6125. }
  6126. }
  6127. }
  6128. }
  6129. NTSTATUS
  6130. SpPtCheckDynamicVolumeForOSInstallation(
  6131. IN PDISK_REGION Region
  6132. )
  6133. {
  6134. NTSTATUS Status;
  6135. HANDLE Handle;
  6136. IO_STATUS_BLOCK IoStatusBlock;
  6137. PARTITION_INFORMATION PartitionInfo;
  6138. ULONG bps;
  6139. ULONG r;
  6140. ULONG StartSector;
  6141. ULONG SectorCount;
  6142. ULONG RelativeSectors;
  6143. ASSERT(Region->DynamicVolume);
  6144. Status = SpOpenPartition( HardDisks[Region->DiskNumber].DevicePath,
  6145. SpPtGetOrdinal(Region,PartitionOrdinalOnDisk),
  6146. &Handle,
  6147. FALSE );
  6148. #if DBG
  6149. SpNtNameFromRegion( Region,
  6150. TemporaryBuffer,
  6151. sizeof(TemporaryBuffer),
  6152. PartitionOrdinalOnDisk);
  6153. #endif
  6154. if( !NT_SUCCESS(Status) ) {
  6155. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: Unable to open dynamic volume %ws. Status = %lx\n",TemporaryBuffer, Status));
  6156. return(Status);
  6157. }
  6158. Status = ZwDeviceIoControlFile(
  6159. Handle,
  6160. NULL,
  6161. NULL,
  6162. NULL,
  6163. &IoStatusBlock,
  6164. IOCTL_DISK_GET_PARTITION_INFO,
  6165. NULL,
  6166. 0,
  6167. &PartitionInfo,
  6168. sizeof(PartitionInfo)
  6169. );
  6170. if(NT_SUCCESS(Status)) {
  6171. bps = HardDisks[Region->DiskNumber].Geometry.BytesPerSector;
  6172. RelativeSectors = 0;
  6173. if( SpPtLookupRegionByStart(&PartitionedDisks[Region->DiskNumber],
  6174. TRUE,
  6175. Region->StartSector) == Region ) {
  6176. //
  6177. // The region is on an extended partition (logical drive)
  6178. //
  6179. PON_DISK_PTE pte;
  6180. //
  6181. // TBD : fix this
  6182. //
  6183. pte = &Region->MbrInfo->OnDiskMbr.PartitionTable[Region->TablePosition];
  6184. RelativeSectors = U_ULONG(pte->RelativeSectors);
  6185. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: Dynamic volume %ws is logical drive on extended partition. RelativeSectors = %lx \n",TemporaryBuffer, RelativeSectors));
  6186. }
  6187. StartSector = RtlExtendedLargeIntegerDivide(PartitionInfo.StartingOffset,bps,&r).LowPart;
  6188. SectorCount = RtlExtendedLargeIntegerDivide(PartitionInfo.PartitionLength,bps,&r).LowPart;
  6189. Region->DynamicVolumeSuitableForOS = ((Region->StartSector + RelativeSectors) == StartSector) &&
  6190. ((Region->SectorCount - RelativeSectors) == SectorCount);
  6191. if( Region->DynamicVolumeSuitableForOS ) {
  6192. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: Dynamic volume %ws is suitable for OS installation\n",TemporaryBuffer));
  6193. } else {
  6194. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: Dynamic volume %ws is not suitable for OS installation\n",TemporaryBuffer));
  6195. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: StartSector = %lx (from MBR)\n", Region->StartSector));
  6196. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: SectorCount = %lx (from MBR)\n", Region->SectorCount));
  6197. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: StartSector = %lx (from IOCTL_DISK_GET_PARTITION_INFO)\n", StartSector));
  6198. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, "SETUP: SectorCount = %lx (from IOCTL_DISK_GET_PARTITION_INFO)\n", SectorCount));
  6199. }
  6200. } else {
  6201. KdPrintEx((DPFLTR_SETUP_ID, DPFLTR_ERROR_LEVEL, "SETUP: Unable to get partition info for dynamic volume %ws. Status = %lx\n",TemporaryBuffer, Status));
  6202. }
  6203. ZwClose(Handle);
  6204. return(Status);
  6205. }
  6206. UCHAR
  6207. SpPtGetPartitionType(
  6208. IN PDISK_REGION Region
  6209. )
  6210. {
  6211. UCHAR SystemId = PARTITION_ENTRY_UNUSED;
  6212. if (!Region->PartitionedSpace)
  6213. return SystemId;
  6214. #ifdef OLD_PARTITION_ENGINE
  6215. SystemId = Region->MbrInfo->OnDiskMbr.PartitionTable[Region->TablePosition].SystemId;
  6216. #endif
  6217. #ifdef NEW_PARTITION_ENGINE
  6218. SystemId = PARTITION_FAT32;
  6219. if (SPPT_IS_MBR_DISK(Region->DiskNumber)) {
  6220. SystemId = SPPT_GET_PARTITION_TYPE(Region);
  6221. }
  6222. #endif
  6223. #ifdef GPT_PARTITION_ENGINE
  6224. SystemId = PARTITION_FAT32;
  6225. if (SPPT_IS_MBR_DISK(Region->DiskNumber)) {
  6226. SystemId = Region->MbrInfo->OnDiskMbr.PartitionTable[Region->TablePosition].SystemId;
  6227. }
  6228. #endif
  6229. return SystemId;
  6230. }
  6231. BOOLEAN
  6232. SpPtnIsRegionSpecialMBRPartition(
  6233. IN PDISK_REGION Region
  6234. )
  6235. {
  6236. BOOLEAN Result = FALSE;
  6237. if (Region && SPPT_IS_MBR_DISK(Region->DiskNumber) &&
  6238. SPPT_IS_REGION_PARTITIONED(Region)) {
  6239. UCHAR PartId = PartitionNameIds[SPPT_GET_PARTITION_TYPE(Region)];
  6240. Result = (PartId != (UCHAR)0xFF) &&
  6241. (SPPT_GET_PARTITION_TYPE(Region) != PARTITION_LDM) &&
  6242. ((PartId + SP_TEXT_PARTITION_NAME_BASE) !=
  6243. SP_TEXT_PARTITION_NAME_UNK);
  6244. }
  6245. return Result;
  6246. }
  6247. PWSTR
  6248. SpPtnGetPartitionName(
  6249. IN PDISK_REGION Region,
  6250. IN OUT PWSTR NameBuffer,
  6251. IN ULONG NameBufferSize
  6252. )
  6253. /*++
  6254. Routine Description:
  6255. Formats the name of the partition, with volume label
  6256. and file system type and returns it.
  6257. Note : Region is assumed to be of partitioned type
  6258. Arguments:
  6259. Region - The region whose name is to be formatted
  6260. NameBuffer - Buffer in which the name needs to be formatted
  6261. NameBuffer - The size of the NameBuffer (in characters)
  6262. Return Value:
  6263. Formatted partition name for the region, if any.
  6264. --*/
  6265. {
  6266. BOOLEAN SpecialPartition = FALSE;
  6267. if (NameBuffer) {
  6268. if (Region) {
  6269. if (SpPtnIsRegionSpecialMBRPartition(Region)) {
  6270. WCHAR Buffer[128];
  6271. SpFormatMessage(Buffer, sizeof(Buffer),
  6272. SP_TEXT_PARTITION_NAME_BASE +
  6273. PartitionNameIds[SPPT_GET_PARTITION_TYPE(Region)]);
  6274. SpFormatMessage(TemporaryBuffer,
  6275. sizeof(TemporaryBuffer),
  6276. SP_TEXT_PARTNAME_DESCR_3,
  6277. Region->PartitionNumber,
  6278. Buffer);
  6279. } else if (Region->VolumeLabel[0]) {
  6280. SpFormatMessage(TemporaryBuffer,
  6281. sizeof(TemporaryBuffer),
  6282. SP_TEXT_PARTNAME_DESCR_1,
  6283. Region->PartitionNumber,
  6284. Region->VolumeLabel,
  6285. Region->TypeName);
  6286. } else {
  6287. SpFormatMessage(TemporaryBuffer,
  6288. sizeof(TemporaryBuffer),
  6289. SP_TEXT_PARTNAME_DESCR_2,
  6290. Region->PartitionNumber,
  6291. Region->TypeName);
  6292. }
  6293. wcsncpy(NameBuffer, TemporaryBuffer, NameBufferSize - 1);
  6294. NameBuffer[NameBufferSize - 1] = 0; // Null terminate
  6295. } else {
  6296. *NameBuffer = 0; // Null terminate
  6297. }
  6298. }
  6299. return NameBuffer;
  6300. }