Leaked source code of windows server 2003
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.

275 lines
7.6 KiB

  1. /*++
  2. Copyright (c) 1995 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 "setupp.h"
  12. #pragma hdrstop
  13. BOOL
  14. AppearsToBeSysPart(
  15. IN PDRIVE_LAYOUT_INFORMATION DriveLayout,
  16. IN WCHAR Drive
  17. )
  18. {
  19. PARTITION_INFORMATION PartitionInfo,*p;
  20. BOOL IsPrimary;
  21. unsigned i;
  22. HANDLE FindHandle;
  23. WIN32_FIND_DATA FindData;
  24. PWSTR BootFiles[] = { L"BOOT.INI",
  25. L"NTLDR",
  26. L"NTDETECT.COM",
  27. NULL
  28. };
  29. WCHAR FileName[64];
  30. //
  31. // Get partition information for this partition.
  32. //
  33. if(!GetPartitionInfo(Drive,&PartitionInfo)) {
  34. return(FALSE);
  35. }
  36. //
  37. // See if the drive is a primary partition.
  38. //
  39. IsPrimary = FALSE;
  40. for(i=0; i<min(DriveLayout->PartitionCount,4); i++) {
  41. p = &DriveLayout->PartitionEntry[i];
  42. if((p->PartitionType != PARTITION_ENTRY_UNUSED)
  43. && (p->StartingOffset.QuadPart == PartitionInfo.StartingOffset.QuadPart)
  44. && (p->PartitionLength.QuadPart == PartitionInfo.PartitionLength.QuadPart)) {
  45. IsPrimary = TRUE;
  46. break;
  47. }
  48. }
  49. if(!IsPrimary) {
  50. return(FALSE);
  51. }
  52. //
  53. // Don't rely on the active partition flag. This could easily not be accurate
  54. // (like user is using os/2 boot manager, for example).
  55. //
  56. //
  57. // See whether nt boot files are present on this drive.
  58. //
  59. for(i=0; BootFiles[i]; i++) {
  60. wsprintf(FileName,L"%wc:\\%s",Drive,BootFiles[i]);
  61. FindHandle = FindFirstFile(FileName,&FindData);
  62. if(FindHandle == INVALID_HANDLE_VALUE) {
  63. return(FALSE);
  64. } else {
  65. FindClose(FindHandle);
  66. }
  67. }
  68. return(TRUE);
  69. }
  70. WCHAR
  71. x86DetermineSystemPartition(
  72. VOID
  73. )
  74. /*++
  75. Routine Description:
  76. Determine the system partition on x86 machines.
  77. The system partition is the primary partition on the boot disk.
  78. Usually this is the active partition on disk 0 and usually it's C:.
  79. However the user could have remapped drive letters and generally
  80. determining the system partition with 100% accuracy is not possible.
  81. The one thing we can be sure of is that the system partition is on
  82. the physical hard disk with the arc path multi(0)disk(0)rdisk(0).
  83. We can be sure of this because by definition this is the arc path
  84. for bios drive 0x80.
  85. This routine determines which drive letters represent drives on
  86. that physical hard drive, and checks each for the nt boot files.
  87. The first drive found with those files is assumed to be the system
  88. partition.
  89. If for some reason we cannot determine the system partition by the above
  90. method, we simply assume it's C:.
  91. Arguments:
  92. None.
  93. Return Value:
  94. Drive letter of system partition.
  95. --*/
  96. {
  97. BOOL GotIt;
  98. PWSTR NtDevicePath;
  99. WCHAR Drive;
  100. WCHAR DriveName[3];
  101. WCHAR Buffer[512];
  102. DWORD NtDevicePathLen;
  103. PWSTR p;
  104. DWORD PhysicalDriveNumber;
  105. HANDLE hDisk;
  106. BOOL b;
  107. DWORD DataSize;
  108. PVOID DriveLayout;
  109. DWORD DriveLayoutSize;
  110. DWORD hardDiskNumber;
  111. DriveName[1] = L':';
  112. DriveName[2] = 0;
  113. GotIt = FALSE;
  114. //
  115. // The system partition must be on multi(0)disk(0)rdisk(0)
  116. //
  117. if(NtDevicePath = ArcDevicePathToNtPath(L"multi(0)disk(0)rdisk(0)")) {
  118. //
  119. // The arc path for a disk device is usually linked
  120. // to partition0. Get rid of the partition part of the name.
  121. //
  122. CharLower(NtDevicePath);
  123. if(p = wcsstr(NtDevicePath,L"\\partition")) {
  124. *p = 0;
  125. }
  126. NtDevicePathLen = lstrlen(NtDevicePath);
  127. //
  128. // Determine the physical drive number of this drive.
  129. // If the name is not of the form \device\harddiskx then
  130. // something is very wrong.
  131. //
  132. if(!wcsncmp(NtDevicePath,L"\\device\\harddisk",16)) {
  133. PhysicalDriveNumber = wcstoul(NtDevicePath+16,NULL,10);
  134. wsprintf(Buffer,L"\\\\.\\PhysicalDrive%u",PhysicalDriveNumber);
  135. //
  136. // Get drive layout info for this physical disk.
  137. //
  138. hDisk = CreateFile(
  139. Buffer,
  140. GENERIC_READ,
  141. FILE_SHARE_READ | FILE_SHARE_WRITE,
  142. NULL,
  143. OPEN_EXISTING,
  144. 0,
  145. NULL
  146. );
  147. if(hDisk != INVALID_HANDLE_VALUE) {
  148. //
  149. // Get partition information.
  150. //
  151. DriveLayout = MyMalloc(1024);
  152. DriveLayoutSize = 1024;
  153. retry:
  154. b = DeviceIoControl(
  155. hDisk,
  156. IOCTL_DISK_GET_DRIVE_LAYOUT,
  157. NULL,
  158. 0,
  159. DriveLayout,
  160. DriveLayoutSize,
  161. &DataSize,
  162. NULL
  163. );
  164. if(!b && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
  165. DriveLayoutSize += 1024;
  166. DriveLayout = MyRealloc(DriveLayout,DriveLayoutSize);
  167. goto retry;
  168. }
  169. CloseHandle(hDisk);
  170. if(b) {
  171. //
  172. // The system partition can only be a drive that is on
  173. // this disk. We make this determination by looking at NT drive names
  174. // for each drive letter and seeing if the nt equivalent of
  175. // multi(0)disk(0)rdisk(0) is a prefix.
  176. //
  177. for(Drive=L'C'; Drive<=L'Z'; Drive++) {
  178. if(MyGetDriveType(Drive) == DRIVE_FIXED) {
  179. DriveName[0] = Drive;
  180. if(QueryDosDevice(DriveName,Buffer,sizeof(Buffer)/sizeof(WCHAR))) {
  181. if (_wcsnicmp(Buffer, L"\\device\\harddisk", 16)) {
  182. hardDiskNumber = QueryHardDiskNumber((UCHAR) Drive);
  183. if (hardDiskNumber != (DWORD) -1) {
  184. swprintf(Buffer, TEXT("\\device\\harddisk%d"),
  185. hardDiskNumber);
  186. }
  187. }
  188. if(!_wcsnicmp(NtDevicePath,Buffer,NtDevicePathLen)) {
  189. //
  190. // Now look to see whether there's an nt boot sector and
  191. // boot files on this drive.
  192. //
  193. if(AppearsToBeSysPart(DriveLayout,Drive)) {
  194. GotIt = TRUE;
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. MyFree(DriveLayout);
  203. }
  204. }
  205. MyFree(NtDevicePath);
  206. }
  207. return(GotIt ? Drive : L'C');
  208. }