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.

1750 lines
44 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. syspart.c
  5. Abstract:
  6. Routines to determine the system partition on x86 machines.
  7. Author:
  8. Ted Miller (tedm) 30-June-1994
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. #define IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS_ADMIN CTL_CODE(IOCTL_VOLUME_BASE, 0, METHOD_BUFFERED, FILE_READ_ACCESS)
  14. //
  15. // Command-line param that allows someone to force a particular drive
  16. // to be the system partition. Useful in certain preinstall scenarios.
  17. //
  18. TCHAR ForcedSystemPartition;
  19. #define WINNT_DONT_MATCH_PARTITION 0
  20. #define WINNT_MATCH_PARTITION_NUMBER 1
  21. #define WINNT_MATCH_PARTITION_STARTING_OFFSET 2
  22. #define BUFFERSIZE 1024
  23. //
  24. // NT-specific routines we use from ntdll.dll and kernel32.dll
  25. //
  26. //NTSYSAPI
  27. NTSTATUS
  28. (NTAPI *NtOpenSymLinkRoutine)(
  29. OUT PHANDLE LinkHandle,
  30. IN ACCESS_MASK DesiredAccess,
  31. IN POBJECT_ATTRIBUTES ObjectAttributes
  32. );
  33. //NTSYSAPI
  34. NTSTATUS
  35. (NTAPI *NtQuerSymLinkRoutine)(
  36. IN HANDLE LinkHandle,
  37. IN OUT PUNICODE_STRING LinkTarget,
  38. OUT PULONG ReturnedLength OPTIONAL
  39. );
  40. //NTSYSAPI
  41. NTSTATUS
  42. (NTAPI *NtQuerDirRoutine) (
  43. IN HANDLE DirectoryHandle,
  44. OUT PVOID Buffer,
  45. IN ULONG Length,
  46. IN BOOLEAN ReturnSingleEntry,
  47. IN BOOLEAN RestartScan,
  48. IN OUT PULONG Context,
  49. OUT PULONG ReturnLength OPTIONAL
  50. );
  51. //NTSYSAPI
  52. NTSTATUS
  53. (NTAPI *NtOpenDirRoutine) (
  54. OUT PHANDLE DirectoryHandle,
  55. IN ACCESS_MASK DesiredAccess,
  56. IN POBJECT_ATTRIBUTES ObjectAttributes
  57. );
  58. //WINBASEAPI
  59. HANDLE
  60. (WINAPI *FindFirstVolume) (
  61. LPWSTR lpszVolumeName,
  62. DWORD cchBufferLength
  63. );
  64. //WINBASEAPI
  65. BOOL
  66. (WINAPI *FindNextVolume)(
  67. HANDLE hFindVolume,
  68. LPWSTR lpszVolumeName,
  69. DWORD cchBufferLength
  70. );
  71. //WINBASEAPI
  72. BOOL
  73. (WINAPI *FindVolumeClose)(
  74. HANDLE hFindVolume
  75. );
  76. //WINBASEAPI
  77. BOOL
  78. (WINAPI *GetVolumeNameForVolumeMountPoint)(
  79. LPCWSTR lpszVolumeMountPoint,
  80. LPWSTR lpszVolumeName,
  81. DWORD cchBufferLength
  82. );
  83. DWORD
  84. FindSystemPartitionSignature(
  85. IN LPCTSTR AdapterKeyName,
  86. OUT LPTSTR Signature
  87. );
  88. DWORD
  89. GetSystemVolumeGUID(
  90. IN LPTSTR Signature,
  91. OUT LPTSTR SysVolGuid
  92. );
  93. BOOL
  94. DoDiskSignaturesCompare(
  95. IN LPCTSTR Signature,
  96. IN LPCTSTR DriveName,
  97. IN OUT PVOID Compare,
  98. IN DWORD Action
  99. );
  100. DWORD
  101. GetNT4SystemPartition(
  102. IN LPTSTR Signature,
  103. OUT LPTSTR SysPart
  104. );
  105. BOOL
  106. ArcPathToNtPath(
  107. IN LPCTSTR ArcPath,
  108. OUT LPTSTR NtPath,
  109. IN UINT NtPathBufferLen
  110. )
  111. {
  112. WCHAR arcPath[256];
  113. UNICODE_STRING UnicodeString;
  114. OBJECT_ATTRIBUTES Obja;
  115. HANDLE ObjectHandle;
  116. NTSTATUS Status;
  117. WCHAR Buffer[512];
  118. PWSTR ntPath;
  119. lstrcpyW(arcPath,L"\\ArcName\\");
  120. #ifdef UNICODE
  121. lstrcpynW(arcPath+9,ArcPath,(sizeof(arcPath)/sizeof(WCHAR))-9);
  122. #else
  123. MultiByteToWideChar(
  124. CP_ACP,
  125. 0,
  126. ArcPath,
  127. -1,
  128. arcPath+9,
  129. (sizeof(arcPath)/sizeof(WCHAR))-9
  130. );
  131. #endif
  132. UnicodeString.Buffer = arcPath;
  133. UnicodeString.Length = lstrlenW(arcPath)*sizeof(WCHAR);
  134. UnicodeString.MaximumLength = UnicodeString.Length + sizeof(WCHAR);
  135. InitializeObjectAttributes(
  136. &Obja,
  137. &UnicodeString,
  138. OBJ_CASE_INSENSITIVE,
  139. NULL,
  140. NULL
  141. );
  142. Status = (*NtOpenSymLinkRoutine)(
  143. &ObjectHandle,
  144. READ_CONTROL | SYMBOLIC_LINK_QUERY,
  145. &Obja
  146. );
  147. if(NT_SUCCESS(Status)) {
  148. //
  149. // Query the object to get the link target.
  150. //
  151. UnicodeString.Buffer = Buffer;
  152. UnicodeString.Length = 0;
  153. UnicodeString.MaximumLength = sizeof(Buffer)-sizeof(WCHAR);
  154. Status = (*NtQuerSymLinkRoutine)(ObjectHandle,&UnicodeString,NULL);
  155. CloseHandle(ObjectHandle);
  156. if(NT_SUCCESS(Status)) {
  157. Buffer[UnicodeString.Length/sizeof(WCHAR)] = 0;
  158. #ifdef UNICODE
  159. lstrcpyn(NtPath,Buffer,NtPathBufferLen);
  160. #else
  161. WideCharToMultiByte(CP_ACP,0,Buffer,-1,NtPath,NtPathBufferLen,NULL,NULL);
  162. #endif
  163. return(TRUE);
  164. }
  165. }
  166. return(FALSE);
  167. }
  168. BOOL
  169. AppearsToBeSysPart(
  170. IN PDRIVE_LAYOUT_INFORMATION DriveLayout,
  171. IN TCHAR Drive
  172. )
  173. {
  174. PARTITION_INFORMATION PartitionInfo,*p;
  175. BOOL IsPrimary;
  176. UINT i;
  177. DWORD d;
  178. LPTSTR BootFiles[] = { TEXT("BOOT.INI"),
  179. TEXT("NTLDR"),
  180. TEXT("NTDETECT.COM"),
  181. NULL
  182. };
  183. TCHAR FileName[64];
  184. //
  185. // Get partition information for this partition.
  186. //
  187. if(!GetPartitionInfo(Drive,&PartitionInfo)) {
  188. return(FALSE);
  189. }
  190. //
  191. // See if the drive is a primary partition.
  192. //
  193. IsPrimary = FALSE;
  194. for(i=0; i<min(DriveLayout->PartitionCount,4); i++) {
  195. p = &DriveLayout->PartitionEntry[i];
  196. if((p->PartitionType != PARTITION_ENTRY_UNUSED)
  197. && (p->StartingOffset.QuadPart == PartitionInfo.StartingOffset.QuadPart)
  198. && (p->PartitionLength.QuadPart == PartitionInfo.PartitionLength.QuadPart)) {
  199. IsPrimary = TRUE;
  200. break;
  201. }
  202. }
  203. if(!IsPrimary) {
  204. return(FALSE);
  205. }
  206. //
  207. // Don't rely on the active partition flag. This could easily not be accurate
  208. // (like user is using os/2 boot manager, for example).
  209. //
  210. //
  211. // See whether all NT boot files are present on this drive.
  212. //
  213. for(i=0; BootFiles[i]; i++) {
  214. wsprintf(FileName,TEXT("%c:\\%s"),Drive,BootFiles[i]);
  215. d = GetFileAttributes(FileName);
  216. if(d == (DWORD)(-1)) {
  217. return(FALSE);
  218. }
  219. }
  220. return(TRUE);
  221. }
  222. DWORD
  223. QueryHardDiskNumber(
  224. IN TCHAR DriveLetter
  225. )
  226. {
  227. TCHAR driveName[10];
  228. HANDLE h;
  229. BOOL b;
  230. STORAGE_DEVICE_NUMBER number;
  231. DWORD bytes;
  232. driveName[0] = '\\';
  233. driveName[1] = '\\';
  234. driveName[2] = '.';
  235. driveName[3] = '\\';
  236. driveName[4] = DriveLetter;
  237. driveName[5] = ':';
  238. driveName[6] = 0;
  239. h = CreateFile(driveName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  240. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  241. INVALID_HANDLE_VALUE);
  242. if (h == INVALID_HANDLE_VALUE) {
  243. return (DWORD) -1;
  244. }
  245. b = DeviceIoControl(h, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0,
  246. &number, sizeof(number), &bytes, NULL);
  247. CloseHandle(h);
  248. if (!b) {
  249. return (DWORD) -1;
  250. }
  251. return number.DeviceNumber;
  252. }
  253. BOOL
  254. MarkPartitionActive(
  255. IN TCHAR DriveLetter
  256. )
  257. {
  258. DWORD DriveNum;
  259. TCHAR DosName[7];
  260. TCHAR Name[MAX_PATH];
  261. DISK_GEOMETRY DiskGeom;
  262. PARTITION_INFORMATION PartitionInfo;
  263. HANDLE h;
  264. BOOL b;
  265. DWORD Bytes;
  266. PUCHAR UnalignedBuffer,Buffer;
  267. unsigned i;
  268. BOOL Rewrite;
  269. BOOL FoundIt;
  270. //
  271. // This concept is n/a for PC98 and the stuff below
  272. // won't work on Win9x.
  273. //
  274. if(IsNEC98() || !ISNT()) {
  275. return(TRUE);
  276. }
  277. //
  278. // Get geometry info and partition info for this drive.
  279. // We get geometry info because we need the bytes per sector info.
  280. //
  281. wsprintf(DosName,TEXT("\\\\.\\%c:"),DriveLetter);
  282. h = CreateFile(
  283. DosName,
  284. GENERIC_READ,
  285. FILE_SHARE_READ | FILE_SHARE_WRITE,
  286. NULL,
  287. OPEN_EXISTING,
  288. 0,
  289. NULL
  290. );
  291. if(h == INVALID_HANDLE_VALUE) {
  292. return(FALSE);
  293. }
  294. b = DeviceIoControl(
  295. h,
  296. IOCTL_DISK_GET_DRIVE_GEOMETRY,
  297. NULL,
  298. 0,
  299. &DiskGeom,
  300. sizeof(DISK_GEOMETRY),
  301. &Bytes,
  302. NULL
  303. );
  304. if(!b || (DiskGeom.BytesPerSector < 512)) {
  305. CloseHandle(h);
  306. return(FALSE);
  307. }
  308. b = DeviceIoControl(
  309. h,
  310. IOCTL_DISK_GET_PARTITION_INFO,
  311. NULL,
  312. 0,
  313. &PartitionInfo,
  314. sizeof(PARTITION_INFORMATION),
  315. &Bytes,
  316. NULL
  317. );
  318. CloseHandle(h);
  319. if(!b) {
  320. return(FALSE);
  321. }
  322. //
  323. // Figure out which physical drive this partition is on.
  324. //
  325. DriveNum = QueryHardDiskNumber(DriveLetter);
  326. if(DriveNum == (DWORD)(-1)) {
  327. //
  328. // Have to do it the old-fashioned way. Convert to an NT path
  329. // and parse the result.
  330. //
  331. if(!QueryDosDevice(DosName+4,Name,MAX_PATH)) {
  332. return(FALSE);
  333. }
  334. if( _tcsnicmp( Name, TEXT("\\device\\harddisk"), 16 )) {
  335. //
  336. // We have no idea what this name represents. Punt.
  337. //
  338. return(FALSE);
  339. }
  340. DriveNum = _tcstoul(Name+16,NULL,10);
  341. }
  342. //
  343. // Allocate a buffer and align it.
  344. //
  345. UnalignedBuffer = MALLOC(2*DiskGeom.BytesPerSector);
  346. if(!UnalignedBuffer) {
  347. return(FALSE);
  348. }
  349. Buffer = (PVOID)(((DWORD)UnalignedBuffer + (DiskGeom.BytesPerSector - 1)) & ~(DiskGeom.BytesPerSector - 1));
  350. //
  351. // Now we open up the physical drive and read the partition table.
  352. // We try to locate the partition by matching start offsets.
  353. // Note that active status is only meaningful for primary partitions.
  354. //
  355. wsprintf(Name,TEXT("\\\\.\\PhysicalDrive%u"),DriveNum);
  356. h = CreateFile(
  357. Name,
  358. GENERIC_READ | GENERIC_WRITE,
  359. FILE_SHARE_READ | FILE_SHARE_WRITE,
  360. NULL,
  361. OPEN_EXISTING,
  362. FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING,
  363. NULL
  364. );
  365. if(h == INVALID_HANDLE_VALUE) {
  366. FREE(UnalignedBuffer);
  367. return(FALSE);
  368. }
  369. if(!ReadFile(h,Buffer,DiskGeom.BytesPerSector,&Bytes,NULL)) {
  370. FREE(UnalignedBuffer);
  371. CloseHandle(h);
  372. return(FALSE);
  373. }
  374. Rewrite = FALSE;
  375. FoundIt = FALSE;
  376. for(i=0; i<4; i++) {
  377. if(*(DWORD *)(Buffer + 0x1be + 8 + (i*16))
  378. == (DWORD)(PartitionInfo.StartingOffset.QuadPart / DiskGeom.BytesPerSector)) {
  379. FoundIt = TRUE;
  380. if(Buffer[0x1be+(i*16)] != 0x80) {
  381. //
  382. // Not already active, or active for some other bios unit #,
  383. // so we need to whack it.
  384. //
  385. Buffer[0x1be+(i*16)] = 0x80;
  386. Rewrite = TRUE;
  387. }
  388. } else {
  389. if(Buffer[0x1be+(i*16)]) {
  390. //
  391. // Not inactive, and needs to be, so whack it.
  392. //
  393. Buffer[0x1be+(i*16)] = 0;
  394. Rewrite = TRUE;
  395. }
  396. }
  397. }
  398. if(FoundIt) {
  399. if(Rewrite) {
  400. Bytes = 0;
  401. if(SetFilePointer(h,0,&Bytes,FILE_BEGIN) || Bytes) {
  402. b = FALSE;
  403. } else {
  404. b = WriteFile(h,Buffer,DiskGeom.BytesPerSector,&Bytes,NULL);
  405. }
  406. } else {
  407. b = TRUE;
  408. }
  409. } else {
  410. b = FALSE;
  411. }
  412. CloseHandle(h);
  413. FREE(UnalignedBuffer);
  414. return(b);
  415. }
  416. BOOL
  417. x86DetermineSystemPartition(
  418. IN HWND ParentWindow,
  419. OUT PTCHAR SysPartDrive
  420. )
  421. /*++
  422. Routine Description:
  423. Determine the system partition on x86 machines.
  424. On Win95, we always return C:. For NT, read on.
  425. The system partition is the primary partition on the boot disk.
  426. Usually this is the active partition on disk 0 and usually it's C:.
  427. However the user could have remapped drive letters and generally
  428. determining the system partition with 100% accuracy is not possible.
  429. With there being differences in the IO system mapping and introduction of Volumes for NT 50
  430. this has now become complicated. Listed below are the algorithms
  431. NT 5.0 Beta 2 and above :
  432. 1. Get the signature from the registry. Located at
  433. HKLM\Hardware\Description\System\<MultifunctionAdapter or EisaAdapter>\<some Bus No.>\DiskController\0\DiskPeripheral\0\Identifier
  434. 2. Go Through all of the volumes in the system with FindFirstVolume/FindNextVolume/FindVolumeClose.
  435. 3. Take off the trailing backslash to the name returne to get \\?\Volume{guid}.
  436. 4. IOCTL_STORAGE_GET_DEVICE_NUMBER with \\?\Volume{guid} => Check for FILE_DEVICE_DISK. Remember the partition number. Goto 6
  437. 5. If IOCTL_STORAGE_GET_DEVICE_NUMBER fails then use IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS which returns a list of harddisks.
  438. For each harddisk remember the starting offset and goto 6.
  439. 6. Check Harddisk # by using \\.\PhysicalDrive# with IOCTL_DISK_GET_DRIVE_LAYOUT. If the signature matches then this is the disk we boot from.
  440. 7. To find the partition that we boot from we look for boot indicator. If we used 2 we try to match the partition number stored in 6
  441. else if 3 we try to match the starting offset.Then you have a \\?\Volume{guid}\ name for the SYSTEM volume.
  442. 8. Call GetVolumeNameForVolumeMountPoint with A:\, B:\, C:\, ... and check the result of the form \\?\Volume{guid}\ against your match
  443. to see what the drive letter is.
  444. Important: Since the *Volume* APIs are post Beta2 we do dynamic loading of kernel32.dll based on the build number returned.
  445. Versions below NT 5.0 Beta 2
  446. 1. Get the signature from the registry. Located at
  447. HKLM\Hardware\Description\System\<MultifunctionAdapter or EisaAdapter>\<some Bus No.>\DiskController\0\DiskPeripheral\0\Identifier
  448. 2. Enumerate the \?? directory and look for all entries that begin with PhysicalDrive#.
  449. 3. For each of the Disks look for a match with the signature above and if they match then find out the partition number used to boot
  450. using IOCTL_DISK_GET_DRIVE_LAYOUT and the BootIndicator bit.
  451. 4. On finding the Boot partition create a name of the form \Device\Harddisk#\Partition#
  452. 5. Then go through c:,d:...,z: calling QueryDosDeviceName and look for a match. That would be your system partition drive letter
  453. Arguments:
  454. ParentWindow - supplies window handle for window to be the parent for
  455. any dialogs, etc.
  456. SysPartDrive - if successful, receives drive letter of system partition.
  457. Return Value:
  458. Boolean value indicating whether SysPartDrive has been filled in.
  459. If FALSE, the user will have been infomred about why.
  460. --*/
  461. {
  462. TCHAR DriveName[4];
  463. BOOL GotIt=FALSE;
  464. TCHAR Buffer[512];
  465. TCHAR Drive;
  466. BOOL b;
  467. TCHAR SysPartSig[20], PartitionNum[MAX_PATH], SysVolGuid[MAX_PATH];
  468. TCHAR DriveVolGuid[MAX_PATH];
  469. if(ForcedSystemPartition) {
  470. //
  471. // NT5 for NEC98 can't boot up from ATA Card and
  472. // removable drive. We need check dive type.
  473. //
  474. if (IsNEC98() &&
  475. ((MyGetDriveType(ForcedSystemPartition) != DRIVE_FIXED) ||
  476. // Drive is not Fixed.
  477. !IsValidDrive(ForcedSystemPartition))){
  478. // HD format type is not NEC98.
  479. return(FALSE);
  480. }
  481. *SysPartDrive = ForcedSystemPartition;
  482. return(TRUE);
  483. }
  484. if(IsNEC98()) {
  485. if(!MyGetWindowsDirectory(Buffer,sizeof(Buffer)/sizeof(TCHAR)))
  486. return FALSE;
  487. *SysPartDrive = Buffer[0];
  488. return(TRUE);
  489. }
  490. if(!ISNT()) {
  491. *SysPartDrive = TEXT('C');
  492. return(TRUE);
  493. }
  494. // Code for NT starts here
  495. //Get signature from registry - Step 1 listed above
  496. if( (FindSystemPartitionSignature(TEXT("Hardware\\Description\\System\\EisaAdapter"),SysPartSig) != ERROR_SUCCESS )
  497. && (FindSystemPartitionSignature(TEXT("Hardware\\Description\\System\\MultiFunctionAdapter"),SysPartSig) != ERROR_SUCCESS ) ){
  498. GotIt = FALSE;
  499. goto c0;
  500. }
  501. if( ISNT() && (BUILDNUM() >= 1877) ){
  502. //Get the SystemVolumeGUID - steps 2 through 7 listed above ( Beta 2 and after )
  503. if( GetSystemVolumeGUID( SysPartSig, SysVolGuid ) != ERROR_SUCCESS ){
  504. GotIt = FALSE;
  505. goto c0;
  506. }
  507. }else{
  508. if( GetNT4SystemPartition( SysPartSig, PartitionNum ) != ERROR_SUCCESS){
  509. GotIt = FALSE;
  510. goto c0;
  511. }
  512. }
  513. DriveName[1] = TEXT(':');
  514. //
  515. // Enumerate all drive letters and compare their device names
  516. //
  517. for(Drive=TEXT('A'); Drive<=TEXT('Z'); Drive++) {
  518. if(MyGetDriveType(Drive) == DRIVE_FIXED) {
  519. DriveName[0] = Drive;
  520. if( BUILDNUM() >= 1877){ //Versions Beta2 and after
  521. DriveName[2] = '\\';
  522. DriveName[3] = 0;
  523. if((*GetVolumeNameForVolumeMountPoint)((LPWSTR)DriveName, (LPWSTR)DriveVolGuid, MAX_PATH*sizeof(TCHAR))){
  524. if(!lstrcmp(DriveVolGuid, SysVolGuid) ){
  525. GotIt = TRUE; // Found it
  526. break;
  527. }
  528. }
  529. }else{
  530. DriveName[2] = 0;
  531. if(QueryDosDevice(DriveName,Buffer,sizeof(Buffer)/sizeof(TCHAR))) {
  532. if( !lstrcmpi(Buffer, PartitionNum) ) {
  533. GotIt = TRUE; // Found it
  534. break;
  535. }
  536. }
  537. }//Versions earlier than Beta 2
  538. }
  539. }
  540. // This helps for some builds ~1500 < buildnum < 1877 that are in a tough spot
  541. if(!GotIt) {
  542. //
  543. // Strange case, just assume C:
  544. //
  545. GotIt = TRUE;
  546. Drive = TEXT('C');
  547. }
  548. c0:
  549. if(GotIt) {
  550. *SysPartDrive = Drive;
  551. #if defined(REMOTE_BOOT)
  552. } else if (RemoteBoot) {
  553. GotIt = TRUE;
  554. *SysPartDrive = TEXT('C');
  555. #endif
  556. }
  557. return(GotIt);
  558. }
  559. DWORD
  560. GetSystemVolumeGUID(
  561. IN LPTSTR Signature,
  562. OUT LPTSTR SysVolGuid
  563. )
  564. /*++
  565. Routine Description:
  566. This routine enumerates all the volumes and if successful returns the \\?\Volume{guid} name for the system partition.
  567. Arguments:
  568. Signature - supplies a disk signature of the Boot disk so that it can be compared against. The details
  569. to getting this value are detailed in the comments for x86DetermineSystemPartition.
  570. SysVolGuid - If successful, will contain a name of form \\?\Volume{guid} for the System Partition (the one we use to boot)
  571. Return Value:
  572. Returns NO_ERROR if successful, otherwise it contains the error code.
  573. --*/
  574. {
  575. HANDLE hVolume, h;
  576. TCHAR VolumeName[MAX_PATH];
  577. PTSTR q;
  578. TCHAR driveName[30];
  579. BOOL b,ret, DoExtent, MatchFound;
  580. STORAGE_DEVICE_NUMBER number;
  581. DWORD Err,cnt;
  582. PVOLUME_DISK_EXTENTS Extent;
  583. PDISK_EXTENT Start, i;
  584. DWORD ExtentSize, bytes;
  585. PVOID p;
  586. ULONG PartitionNumToCompare;
  587. LARGE_INTEGER StartingOffToCompare;
  588. DWORD ioctlCode;
  589. Err = NO_ERROR;
  590. //Enuberate all volumes
  591. hVolume = (*FindFirstVolume)( (LPWSTR)VolumeName, MAX_PATH );
  592. if( hVolume == INVALID_HANDLE_VALUE ){
  593. return GetLastError();
  594. }
  595. MatchFound = FALSE;
  596. do{
  597. //Remove trailing backslash
  598. DoExtent = FALSE;
  599. if( q=_tcsrchr( VolumeName,TEXT('\\')) ){
  600. *q = 0;
  601. }else{
  602. continue;
  603. }
  604. //Open the volume
  605. h = CreateFile(VolumeName, GENERIC_READ, FILE_SHARE_READ |
  606. FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
  607. FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE);
  608. if (h == INVALID_HANDLE_VALUE) {
  609. Err = GetLastError();
  610. continue; // Move on to next volume
  611. }
  612. //Get the disk number
  613. ret = DeviceIoControl(h, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0,
  614. &number, sizeof(number), &bytes, NULL);
  615. if( !ret ){
  616. // Try using IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS if the above failed
  617. Extent = MALLOC(1024);
  618. ExtentSize = 1024;
  619. if(!Extent) {
  620. CloseHandle( h );
  621. Err = ERROR_NOT_ENOUGH_MEMORY;
  622. goto cleanup;
  623. }
  624. ioctlCode = IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS;
  625. retry:
  626. ret = DeviceIoControl( h, ioctlCode,
  627. NULL,0,(PVOID)Extent,ExtentSize,&bytes,NULL);
  628. if(!ret) {
  629. if((Err=GetLastError()) == ERROR_MORE_DATA) {
  630. ExtentSize += 1024;
  631. if(p = REALLOC((PVOID)Extent, ExtentSize)) {
  632. (PVOID)Extent = p;
  633. } else {
  634. CloseHandle( h );
  635. Err = ERROR_NOT_ENOUGH_MEMORY;
  636. goto cleanup;
  637. }
  638. goto retry;
  639. } else {
  640. if (ioctlCode == IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS) {
  641. ioctlCode = IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS_ADMIN;
  642. goto retry;
  643. }
  644. CloseHandle( h );
  645. continue;
  646. }
  647. }else{
  648. DoExtent = TRUE;
  649. }
  650. }
  651. // Done with the handle this time around
  652. CloseHandle( h );
  653. if( !DoExtent ){
  654. //
  655. // Check to see if this is a disk and not CDROM etc.
  656. //
  657. if( number.DeviceType == FILE_DEVICE_DISK ){
  658. // Remember the partition number
  659. PartitionNumToCompare = number.PartitionNumber;
  660. wsprintf( driveName, TEXT("\\\\.\\PhysicalDrive%lu"), number.DeviceNumber );
  661. if(DoDiskSignaturesCompare( Signature, driveName, (PVOID)&PartitionNumToCompare, WINNT_MATCH_PARTITION_NUMBER ) ){
  662. MatchFound = TRUE;
  663. Err = NO_ERROR;
  664. lstrcpy( SysVolGuid, VolumeName );
  665. SysVolGuid[lstrlen(VolumeName)]=TEXT('\\');
  666. SysVolGuid[lstrlen(VolumeName)+1]=0;
  667. break;
  668. }
  669. }
  670. // Move on ..
  671. continue;
  672. }else{
  673. // Go through all disks and try for match
  674. Start = Extent->Extents;
  675. cnt = 0;
  676. for( i = Start; cnt < Extent->NumberOfDiskExtents; i++ ){
  677. cnt++;
  678. // Remember the starting offset
  679. StartingOffToCompare = i->StartingOffset;
  680. wsprintf( driveName, TEXT("\\\\.\\PhysicalDrive%lu"), i->DiskNumber );
  681. if(DoDiskSignaturesCompare( Signature, driveName, (PVOID)&StartingOffToCompare, WINNT_MATCH_PARTITION_STARTING_OFFSET ) ){
  682. MatchFound = TRUE;
  683. Err = NO_ERROR;
  684. lstrcpy( SysVolGuid, VolumeName );
  685. SysVolGuid[lstrlen(VolumeName)]=TEXT('\\');
  686. SysVolGuid[lstrlen(VolumeName)+1]=0;
  687. break;
  688. }
  689. }
  690. }
  691. if( MatchFound )
  692. break;
  693. }while( (*FindNextVolume)( hVolume, (LPWSTR)VolumeName, MAX_PATH ));
  694. cleanup:
  695. if( hVolume != INVALID_HANDLE_VALUE )
  696. (*FindVolumeClose)( hVolume );
  697. return Err;
  698. }
  699. BOOL
  700. DoDiskSignaturesCompare(
  701. IN LPCTSTR Signature,
  702. IN LPCTSTR DriveName,
  703. IN OUT PVOID Compare,
  704. IN DWORD Action
  705. )
  706. /*++
  707. Routine Description:
  708. This routine compares the given disk signature with the one for the specified physical disk.
  709. Arguments:
  710. Signature - supplies a disk signature of the Boot disk so that it can be compared against. The details
  711. to getting this value are detailed in the comments for x86DetermineSystemPartition.
  712. DriveName - Physical Drive name of the form \\.\PhysicalDrive#
  713. Compare - A pointer to a storage variable. The type depends on the value of Action
  714. Action - Should be one of the following
  715. WINNT_DONT_MATCH_PARTITION - Once disk signatures match it returns the boot partition number in Compare. Compare should be a PULONG.
  716. WINNT_MATCH_PARTITION_NUMBER - Once disk signatures match it tries to match the boot partition number with the number passed in
  717. through Compare. Compare should be PULONG.
  718. WINNT_MATCH_PARTITION_STARTING_OFFSET - Once disk signatures match it tries to match the boot partition starting offset with the
  719. starting offset number passed in through Compare. Compare should be PLARGE_INTEGER.
  720. Return Value:
  721. Returns TRUE if successful in getting a match.
  722. --*/
  723. {
  724. TCHAR temp[30];
  725. BOOL b,Found = FALSE;
  726. PLARGE_INTEGER Starting_Off;
  727. PPARTITION_INFORMATION Start, i;
  728. HANDLE hDisk;
  729. PDRIVE_LAYOUT_INFORMATION DriveLayout;
  730. DWORD DriveLayoutSize;
  731. DWORD cnt;
  732. DWORD DataSize;
  733. LPTSTR p;
  734. PULONG Disk_Num;
  735. ULONG Sig;
  736. if(!Compare )
  737. return FALSE;
  738. if ((Action != WINNT_DONT_MATCH_PARTITION) &&
  739. (Action != WINNT_MATCH_PARTITION_NUMBER) &&
  740. (Action != WINNT_MATCH_PARTITION_STARTING_OFFSET))
  741. return FALSE;
  742. if( (Action==WINNT_MATCH_PARTITION_STARTING_OFFSET) && Compare )
  743. Starting_Off = (PLARGE_INTEGER) Compare;
  744. else
  745. Disk_Num = (PULONG) Compare;
  746. // On any failure return FALSE
  747. //
  748. // Get drive layout info for this physical disk.
  749. // If we can't do this something is very wrong.
  750. //
  751. hDisk = CreateFile(
  752. DriveName,
  753. FILE_READ_ATTRIBUTES | FILE_READ_DATA,
  754. FILE_SHARE_READ | FILE_SHARE_WRITE,
  755. NULL,
  756. OPEN_EXISTING,
  757. 0,
  758. NULL
  759. );
  760. if(hDisk == INVALID_HANDLE_VALUE) {
  761. return FALSE;
  762. }
  763. //
  764. // Get partition information.
  765. //
  766. DriveLayout = MALLOC(1024);
  767. DriveLayoutSize = 1024;
  768. if(!DriveLayout) {
  769. goto cleanexit;
  770. }
  771. retry:
  772. b = DeviceIoControl(
  773. hDisk,
  774. IOCTL_DISK_GET_DRIVE_LAYOUT,
  775. NULL,
  776. 0,
  777. (PVOID)DriveLayout,
  778. DriveLayoutSize,
  779. &DataSize,
  780. NULL
  781. );
  782. if(!b) {
  783. if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  784. DriveLayoutSize += 1024;
  785. if(p = REALLOC((PVOID)DriveLayout,DriveLayoutSize)) {
  786. (PVOID)DriveLayout = p;
  787. } else {
  788. goto cleanexit;
  789. }
  790. goto retry;
  791. } else {
  792. goto cleanexit;
  793. }
  794. }else{
  795. // Now walk the Drive_Layout to find the boot partition
  796. Start = DriveLayout->PartitionEntry;
  797. cnt = 0;
  798. /*
  799. _ultot( DriveLayout->Signature, temp, 16 );
  800. if( lstrcmpi( temp, Signature ) )
  801. goto cleanexit;
  802. */
  803. Sig = _tcstoul( Signature, NULL, 16 );
  804. if( Sig != DriveLayout->Signature )
  805. goto cleanexit;
  806. for( i = Start; cnt < DriveLayout->PartitionCount; i++ ){
  807. cnt++;
  808. if( i->BootIndicator == TRUE ){
  809. if( Action == WINNT_DONT_MATCH_PARTITION ){
  810. *Disk_Num = i->PartitionNumber;
  811. Found = TRUE;
  812. goto cleanexit;
  813. }
  814. if( Action == WINNT_MATCH_PARTITION_NUMBER ){
  815. if( *Disk_Num == i->PartitionNumber ){
  816. Found = TRUE;
  817. goto cleanexit;
  818. }
  819. }else{
  820. if( Starting_Off->QuadPart == i->StartingOffset.QuadPart ){
  821. Found = TRUE;
  822. goto cleanexit;
  823. }
  824. }
  825. break;
  826. }
  827. }
  828. }
  829. cleanexit:
  830. if( hDisk != INVALID_HANDLE_VALUE )
  831. CloseHandle( hDisk );
  832. return Found;
  833. }
  834. DWORD
  835. FindSystemPartitionSignature(
  836. IN LPCTSTR AdapterKeyName,
  837. OUT LPTSTR Signature
  838. )
  839. /*++
  840. Routine Description:
  841. This routine fetches the disk signature for the first disk that the BIOS sees. This has to be the disk that we boot from on x86s.
  842. It is at location <AdapterKeyName>\<some Bus No.>\DiskController\0\DiskPeripheral\0\Identifier
  843. Arguments:
  844. Signature - If successful will contain the signature of the disk we boot off from in Hex.
  845. Return Value:
  846. Returns ERROR_SUCCESS if successful, otherwise it contains the error code.
  847. --*/
  848. {
  849. DWORD Err, dwSize;
  850. HKEY hkey, BusKey, Controller, SystemDiskKey;
  851. int busnumber;
  852. TCHAR BusString[20], Identifier[100];
  853. Err = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  854. AdapterKeyName,
  855. 0,
  856. KEY_READ,
  857. &hkey );
  858. if( Err != ERROR_SUCCESS )
  859. return Err;
  860. // Start enumerating the buses
  861. for( busnumber=0; ;busnumber++){
  862. wsprintf( BusString, TEXT("%d"), busnumber );
  863. Err = RegOpenKeyEx( hkey,
  864. BusString,
  865. 0,
  866. KEY_READ,
  867. &BusKey );
  868. if( Err != ERROR_SUCCESS ){
  869. RegCloseKey( hkey );
  870. return Err;
  871. }
  872. Err = RegOpenKeyEx( BusKey,
  873. TEXT("DiskController"),
  874. 0,
  875. KEY_READ,
  876. &Controller );
  877. RegCloseKey(BusKey); // Not needed anymore
  878. if( Err != ERROR_SUCCESS ) // Move on to next bus
  879. continue;
  880. RegCloseKey( hkey ); // Not needed anymore
  881. Err = RegOpenKeyEx( Controller,
  882. TEXT("0\\DiskPeripheral\\0"),
  883. 0,
  884. KEY_READ,
  885. &SystemDiskKey );
  886. if( Err != ERROR_SUCCESS ){
  887. RegCloseKey( Controller );
  888. return Err;
  889. }
  890. RegCloseKey( Controller ); // Not needed anymore
  891. dwSize = sizeof(Identifier);
  892. Err = RegQueryValueEx( SystemDiskKey,
  893. TEXT("Identifier"),
  894. NULL,
  895. NULL,
  896. (PBYTE) Identifier,
  897. &dwSize);
  898. if( Err != ERROR_SUCCESS ){
  899. RegCloseKey( SystemDiskKey );
  900. return Err;
  901. }
  902. if( Identifier && (lstrlen(Identifier) > 9 ) ){
  903. lstrcpy( Signature,Identifier+9);
  904. *_tcsrchr( Signature,TEXT('-') ) = 0;
  905. RegCloseKey( SystemDiskKey );
  906. return ERROR_SUCCESS;
  907. }
  908. else{
  909. RegCloseKey( SystemDiskKey );
  910. return Err;
  911. }
  912. }
  913. // Should never get here
  914. RegCloseKey( hkey );
  915. return ERROR_PATH_NOT_FOUND;
  916. }
  917. #if defined(UNICODE)
  918. #define EMPTY_STRING L""
  919. #define DEF_INF_BUFFER_SIZE 1024
  920. #define MULTI_SZ_NEXT_STRING(x) ((x) + wcslen(x) + 1)
  921. #ifndef ARRAYSIZE
  922. #define ARRAYSIZE(x) (sizeof(x)/sizeof((x)[0]))
  923. #endif
  924. BOOL
  925. GetSystemRootNtPath(
  926. OUT LPWSTR NtPath,
  927. IN USHORT NtPathBufferLen
  928. )
  929. {
  930. UNICODE_STRING UnicodeString;
  931. OBJECT_ATTRIBUTES Obja;
  932. HANDLE ObjectHandle;
  933. NTSTATUS Status;
  934. WCHAR Buffer[512];
  935. PWSTR ntPath;
  936. #define SYSTEM_ROOT_NAME L"\\SystemRoot"
  937. UnicodeString.Buffer = SYSTEM_ROOT_NAME;
  938. UnicodeString.Length = (ARRAYSIZE(SYSTEM_ROOT_NAME) - 1) * sizeof(WCHAR);
  939. UnicodeString.MaximumLength = UnicodeString.Length + sizeof(WCHAR);
  940. InitializeObjectAttributes(
  941. &Obja,
  942. &UnicodeString,
  943. OBJ_CASE_INSENSITIVE,
  944. NULL,
  945. NULL
  946. );
  947. Status = (*NtOpenSymLinkRoutine)(
  948. &ObjectHandle,
  949. READ_CONTROL | SYMBOLIC_LINK_QUERY,
  950. &Obja
  951. );
  952. if(NT_SUCCESS(Status)) {
  953. //
  954. // Query the object to get the link target.
  955. //
  956. UnicodeString.Buffer = NtPath;
  957. UnicodeString.Length = 0;
  958. UnicodeString.MaximumLength = NtPathBufferLen;
  959. Status = (*NtQuerSymLinkRoutine)(ObjectHandle,&UnicodeString,NULL);
  960. CloseHandle(ObjectHandle);
  961. if(NT_SUCCESS(Status)) {
  962. UnicodeString.Buffer[UnicodeString.Length/sizeof(WCHAR)] = 0;
  963. return TRUE;
  964. }
  965. }
  966. return FALSE;
  967. }
  968. BOOL
  969. DoesCurrentSystemHasThirdPartyKernel(
  970. VOID
  971. )
  972. {
  973. WCHAR BootIniName[16];
  974. PWSTR pSectionsBuffer = NULL;
  975. PWSTR pKeysBuffer = NULL;
  976. PWSTR pString = NULL;
  977. PWSTR pNtPathString = NULL;
  978. PWSTR pNtPathSystemRoot = NULL;
  979. PWSTR pKey = NULL;
  980. PWSTR pDirectory;
  981. UINT sizeOfSectionBuffer = 0;
  982. UINT sizeOfBuffer = 0;
  983. UINT directoryNameSize;
  984. BOOL bResult = FALSE;
  985. wsprintfW(BootIniName, L"%c:\\BOOT.INI", SystemPartitionDriveLetter);
  986. __try{
  987. do{
  988. if(pKeysBuffer){
  989. FREE(pKeysBuffer);
  990. }
  991. sizeOfSectionBuffer += DEF_INF_BUFFER_SIZE;
  992. pKeysBuffer = (PWSTR)MALLOC(sizeOfSectionBuffer * sizeof (WCHAR));
  993. if(!pKeysBuffer){
  994. __leave;
  995. }
  996. pKeysBuffer[0] = '\0';
  997. }while((sizeOfSectionBuffer - 2) ==
  998. GetPrivateProfileStringW(L"operating systems",
  999. NULL,
  1000. EMPTY_STRING,
  1001. pKeysBuffer,
  1002. sizeOfSectionBuffer,
  1003. BootIniName));
  1004. if(!pKeysBuffer[0]){
  1005. __leave;
  1006. }
  1007. sizeOfBuffer = DEF_INF_BUFFER_SIZE;
  1008. pString = (PWSTR)MALLOC(sizeOfBuffer * sizeof (WCHAR));
  1009. if(!pString){
  1010. __leave;
  1011. }
  1012. pNtPathString = (PWSTR)MALLOC(sizeOfBuffer * sizeof (WCHAR));
  1013. if(!pNtPathString){
  1014. __leave;
  1015. }
  1016. for(pKey = pKeysBuffer; pKey[0]; pKey = MULTI_SZ_NEXT_STRING(pKey))
  1017. {
  1018. GetPrivateProfileStringW(L"operating systems",
  1019. pKey,
  1020. EMPTY_STRING,
  1021. pString,
  1022. sizeOfBuffer,
  1023. BootIniName);
  1024. _wcslwr(pString);
  1025. if(!wcsstr(pString, L"/kernel")){
  1026. continue;
  1027. }
  1028. pDirectory = wcschr(pKey, '\\');
  1029. MYASSERT(pDirectory);
  1030. if(pDirectory){
  1031. directoryNameSize = wcslen(pDirectory) * sizeof(WCHAR);
  1032. pDirectory[0] = '\0';
  1033. }
  1034. else{
  1035. directoryNameSize = 0;
  1036. }
  1037. if(!ArcPathToNtPath(pKey, pNtPathString, sizeOfBuffer - directoryNameSize)){
  1038. MYASSERT(FALSE);
  1039. continue;
  1040. }
  1041. if(pDirectory){
  1042. pDirectory[0] = '\\';
  1043. wcscat(pNtPathString, pDirectory);
  1044. }
  1045. if(!pNtPathSystemRoot){
  1046. pNtPathSystemRoot = (PWSTR)MALLOC(sizeOfBuffer * sizeof (WCHAR));
  1047. if(!pNtPathSystemRoot){
  1048. __leave;
  1049. }
  1050. if(!GetSystemRootNtPath(pNtPathSystemRoot, sizeOfBuffer * sizeof (WCHAR))){
  1051. MYASSERT(FALSE);
  1052. }
  1053. }
  1054. if(!_wcsicmp(pNtPathString, pNtPathSystemRoot)){
  1055. bResult = TRUE;
  1056. __leave;
  1057. }
  1058. }
  1059. }
  1060. __finally{
  1061. DWORD rc = GetLastError();
  1062. if(pKeysBuffer){
  1063. FREE(pKeysBuffer);
  1064. }
  1065. if(pString){
  1066. FREE(pString);
  1067. }
  1068. if(pNtPathString){
  1069. FREE(pNtPathString);
  1070. }
  1071. if(pNtPathSystemRoot){
  1072. FREE(pNtPathSystemRoot);
  1073. }
  1074. SetLastError (rc);
  1075. }
  1076. return bResult;
  1077. }
  1078. #endif
  1079. BOOL
  1080. InitializeArcStuff(
  1081. IN HWND Parent
  1082. )
  1083. {
  1084. HMODULE NtdllLib, Kernel32Lib;
  1085. if(ISNT()) {
  1086. //
  1087. // On NT ntdll.dll had better be already loaded.
  1088. //
  1089. NtdllLib = LoadLibrary(TEXT("NTDLL"));
  1090. if(!NtdllLib) {
  1091. MessageBoxFromMessage(
  1092. Parent,
  1093. MSG_UNKNOWN_SYSTEM_ERROR,
  1094. FALSE,
  1095. AppTitleStringId,
  1096. MB_OK | MB_ICONERROR | MB_TASKMODAL,
  1097. GetLastError()
  1098. );
  1099. return(FALSE);
  1100. }
  1101. (FARPROC)NtOpenSymLinkRoutine = GetProcAddress(NtdllLib,"NtOpenSymbolicLinkObject");
  1102. (FARPROC)NtQuerSymLinkRoutine = GetProcAddress(NtdllLib,"NtQuerySymbolicLinkObject");
  1103. (FARPROC)NtOpenDirRoutine = GetProcAddress(NtdllLib,"NtOpenDirectoryObject");
  1104. (FARPROC)NtQuerDirRoutine = GetProcAddress(NtdllLib,"NtQueryDirectoryObject");
  1105. if(!NtOpenSymLinkRoutine || !NtQuerSymLinkRoutine || !NtOpenDirRoutine || !NtQuerDirRoutine) {
  1106. MessageBoxFromMessage(
  1107. Parent,
  1108. MSG_UNKNOWN_SYSTEM_ERROR,
  1109. FALSE,
  1110. AppTitleStringId,
  1111. MB_OK | MB_ICONERROR | MB_TASKMODAL,
  1112. GetLastError()
  1113. );
  1114. FreeLibrary(NtdllLib);
  1115. return(FALSE);
  1116. }
  1117. //
  1118. // We don't need the extraneous handle any more.
  1119. //
  1120. FreeLibrary(NtdllLib);
  1121. if(BUILDNUM() >= 1877){
  1122. //Load the kernel32.dll stuff too
  1123. Kernel32Lib = LoadLibrary(TEXT("KERNEL32"));
  1124. if(!Kernel32Lib) {
  1125. MessageBoxFromMessage(
  1126. Parent,
  1127. MSG_UNKNOWN_SYSTEM_ERROR,
  1128. FALSE,
  1129. AppTitleStringId,
  1130. MB_OK | MB_ICONERROR | MB_TASKMODAL,
  1131. GetLastError()
  1132. );
  1133. return(FALSE);
  1134. }
  1135. (FARPROC)FindFirstVolume = GetProcAddress(Kernel32Lib,"FindFirstVolumeW");
  1136. (FARPROC)FindNextVolume = GetProcAddress(Kernel32Lib,"FindNextVolumeW");
  1137. (FARPROC)FindVolumeClose = GetProcAddress(Kernel32Lib,"FindVolumeClose");
  1138. (FARPROC)GetVolumeNameForVolumeMountPoint = GetProcAddress(Kernel32Lib,"GetVolumeNameForVolumeMountPointW");
  1139. if(!FindFirstVolume || !FindNextVolume ) {
  1140. MessageBoxFromMessage(
  1141. Parent,
  1142. MSG_UNKNOWN_SYSTEM_ERROR,
  1143. FALSE,
  1144. AppTitleStringId,
  1145. MB_OK | MB_ICONERROR | MB_TASKMODAL,
  1146. GetLastError()
  1147. );
  1148. FreeLibrary(Kernel32Lib);
  1149. return(FALSE);
  1150. }
  1151. FreeLibrary(Kernel32Lib);
  1152. }
  1153. }
  1154. if(!x86DetermineSystemPartition(Parent,&SystemPartitionDriveLetter)) {
  1155. MessageBoxFromMessage(
  1156. Parent,
  1157. MSG_SYSTEM_PARTITION_INVALID,
  1158. FALSE,
  1159. AppTitleStringId,
  1160. MB_OK | MB_ICONERROR | MB_TASKMODAL
  1161. );
  1162. return(FALSE);
  1163. }
  1164. SystemPartitionDriveLetters[0] = SystemPartitionDriveLetter;
  1165. SystemPartitionDriveLetters[1] = 0;
  1166. LocalBootDirectory[0] = SystemPartitionDriveLetter;
  1167. LocalBootDirectory[1] = TEXT(':');
  1168. LocalBootDirectory[2] = TEXT('\\');
  1169. lstrcpy(LocalBootDirectory+3,LOCAL_BOOT_DIR);
  1170. if(IsNEC98()) {
  1171. LocalBackupDirectory[0] = SystemPartitionDriveLetter;
  1172. LocalBackupDirectory[1] = TEXT(':');
  1173. LocalBackupDirectory[2] = TEXT('\\');
  1174. lstrcpy(LocalBackupDirectory+3,LOCAL_BACKUP_DIR);
  1175. }
  1176. return(TRUE);
  1177. }
  1178. DWORD
  1179. GetNT4SystemPartition(
  1180. IN LPTSTR Signature,
  1181. OUT LPTSTR SysPart
  1182. )
  1183. /*++
  1184. Routine Description:
  1185. This routine enumerates all the volumes and if successful returns the \Device\Harddisk#\Partition# name of the system partition
  1186. on systems prior to NT 5 Beta 2.
  1187. Arguments:
  1188. Signature - supplies a disk signature of the Boot disk so that it can be compared against. The details
  1189. to getting this value are detailed in the comments for x86DetermineSystemPartition.
  1190. SysPart - If successful, will contain a name of form \Device\Harddisk#\Partition# for the System Partition (the one we use to boot)
  1191. Return Value:
  1192. Returns NO_ERROR if successful, otherwise it contains the error code.
  1193. --*/
  1194. {
  1195. NTSTATUS Status;
  1196. UNICODE_STRING UnicodeString;
  1197. OBJECT_ATTRIBUTES Attributes;
  1198. HANDLE DirectoryHandle;
  1199. POBJECT_DIRECTORY_INFORMATION DirInfo;
  1200. UCHAR DirInfoBuffer[ BUFFERSIZE ];
  1201. TCHAR DirName[20];
  1202. TCHAR ObjName[1024];
  1203. TCHAR Buffer[1024];
  1204. WCHAR pSignature[512];
  1205. ULONG Context = 0;
  1206. ULONG ReturnedLength, PartNum;
  1207. LPTSTR Num_Str;
  1208. RtlZeroMemory( DirInfoBuffer, BUFFERSIZE );
  1209. #ifdef UNICODE
  1210. lstrcpyW( pSignature,Signature);
  1211. #else
  1212. MultiByteToWideChar(
  1213. CP_ACP,
  1214. 0,
  1215. Signature,
  1216. -1,
  1217. pSignature,
  1218. (sizeof(pSignature)/sizeof(WCHAR))
  1219. );
  1220. #endif
  1221. //We open the \?? Directory
  1222. lstrcpy( DirName, TEXT("\\DosDevices") );
  1223. UnicodeString.Buffer = (PWSTR)DirName;
  1224. UnicodeString.Length = lstrlenW(UnicodeString.Buffer)*sizeof(WCHAR);
  1225. UnicodeString.MaximumLength = UnicodeString.Length + sizeof(WCHAR);
  1226. InitializeObjectAttributes( &Attributes,
  1227. &UnicodeString,
  1228. OBJ_CASE_INSENSITIVE,
  1229. NULL,
  1230. NULL
  1231. );
  1232. Status = (*NtOpenDirRoutine)( &DirectoryHandle,
  1233. DIRECTORY_QUERY,
  1234. &Attributes
  1235. );
  1236. if (!NT_SUCCESS( Status ))
  1237. return(Status);
  1238. DirInfo = (POBJECT_DIRECTORY_INFORMATION)&DirInfoBuffer;
  1239. // Go through the directory looking for all instances beginning with PhysicalDrive#
  1240. for (Status = (*NtQuerDirRoutine)( DirectoryHandle,
  1241. DirInfoBuffer,
  1242. BUFFERSIZE,
  1243. FALSE,
  1244. TRUE,
  1245. &Context,
  1246. &ReturnedLength );
  1247. NT_SUCCESS( Status );
  1248. Status = (*NtQuerDirRoutine)( DirectoryHandle,
  1249. DirInfoBuffer,
  1250. BUFFERSIZE,
  1251. FALSE,
  1252. FALSE,
  1253. &Context,
  1254. &ReturnedLength ) ) {
  1255. //
  1256. // Check the status of the operation.
  1257. //
  1258. if (!NT_SUCCESS( Status ) && (Status != STATUS_NO_MORE_ENTRIES))
  1259. break;
  1260. DirInfo = (POBJECT_DIRECTORY_INFORMATION)&DirInfoBuffer[0];
  1261. while( TRUE ){
  1262. //
  1263. // Check if there is another record. If there isn't, then get out
  1264. // of the loop now
  1265. //
  1266. if (DirInfo->Name.Length == 0) {
  1267. break;
  1268. }
  1269. memmove( ObjName, DirInfo->Name.Buffer, DirInfo->Name.Length );
  1270. ObjName[DirInfo->Name.Length/(sizeof(WCHAR))] = 0;
  1271. if( _tcsstr(ObjName, TEXT("PhysicalDrive") )){
  1272. Num_Str = ObjName+13;
  1273. wsprintf(Buffer,TEXT("\\\\.\\%s"),ObjName);
  1274. if( DoDiskSignaturesCompare( (LPCTSTR)pSignature, Buffer, &PartNum, WINNT_DONT_MATCH_PARTITION ) ){
  1275. wsprintf(SysPart,TEXT("\\Device\\Harddisk%s\\Partition%lu"),Num_Str, PartNum);
  1276. Status = ERROR_SUCCESS;
  1277. goto cleanup;
  1278. }
  1279. }
  1280. //
  1281. // There is another record so advance DirInfo to the next entry
  1282. //
  1283. DirInfo = (POBJECT_DIRECTORY_INFORMATION) (((PUCHAR) DirInfo) +
  1284. sizeof( OBJECT_DIRECTORY_INFORMATION ) );
  1285. }
  1286. RtlZeroMemory( DirInfoBuffer, BUFFERSIZE );
  1287. }
  1288. cleanup:
  1289. CloseHandle( DirectoryHandle );
  1290. return( Status );
  1291. }