Windows NT 4.0 source code leak
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.

556 lines
16 KiB

4 years ago
  1. #include <nt.h>
  2. #include <ntddscsi.h>
  3. #include <ntdddisk.h>
  4. #include <ntddcdrm.h>
  5. #include <ntrtl.h>
  6. #include <nturtl.h>
  7. #include <windows.h>
  8. #include <stdio.h>
  9. #include <process.h>
  10. #include <memory.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <scsi.h>
  15. VOID
  16. GetDriverName(
  17. IN ULONG PortNumber
  18. )
  19. {
  20. UNICODE_STRING name;
  21. UNICODE_STRING unicodeString;
  22. ANSI_STRING ansiString;
  23. HANDLE key;
  24. HANDLE portKey;
  25. OBJECT_ATTRIBUTES objectAttributes;
  26. NTSTATUS status;
  27. UCHAR buffer[64];
  28. ULONG length;
  29. PKEY_VALUE_FULL_INFORMATION keyData = (PKEY_VALUE_FULL_INFORMATION)buffer;
  30. printf("\nSCSI PORT %d\n", PortNumber);
  31. //
  32. // Obtain handle to SCSI path in device map.
  33. //
  34. RtlInitUnicodeString(&name,
  35. L"\\Registry\\Machine\\Hardware\\DeviceMap\\Scsi");
  36. //
  37. // Initialize the object for the key.
  38. //
  39. InitializeObjectAttributes(&objectAttributes,
  40. &name,
  41. OBJ_CASE_INSENSITIVE,
  42. NULL,
  43. (PSECURITY_DESCRIPTOR) NULL);
  44. //
  45. // Open the key.
  46. //
  47. status = NtOpenKey(&key,
  48. KEY_READ,
  49. &objectAttributes);
  50. if (!NT_SUCCESS(status)) {
  51. return;
  52. }
  53. //
  54. // Create Scsi port name.
  55. //
  56. sprintf(buffer,
  57. "Scsi Port %d",
  58. PortNumber);
  59. RtlInitString(&ansiString, buffer);
  60. status = RtlAnsiStringToUnicodeString(&unicodeString,
  61. &ansiString,
  62. TRUE);
  63. if (!NT_SUCCESS(status)) {
  64. return;
  65. }
  66. InitializeObjectAttributes( &objectAttributes,
  67. &unicodeString,
  68. OBJ_CASE_INSENSITIVE,
  69. key,
  70. (PSECURITY_DESCRIPTOR) NULL );
  71. status = NtOpenKey(&portKey,
  72. KEY_READ,
  73. &objectAttributes);
  74. if (!NT_SUCCESS(status)) {
  75. return;
  76. }
  77. RtlInitUnicodeString(&name,
  78. L"Driver");
  79. status = NtQueryValueKey(portKey,
  80. &name,
  81. KeyValueFullInformation,
  82. keyData,
  83. 64,
  84. &length);
  85. if (!NT_SUCCESS(status)) {
  86. return;
  87. }
  88. printf("Driver name: %S\n",
  89. (PUCHAR)keyData + keyData->DataOffset);
  90. RtlInitUnicodeString(&name,
  91. L"Interrupt");
  92. status = NtQueryValueKey(portKey,
  93. &name,
  94. KeyValueFullInformation,
  95. keyData,
  96. 64,
  97. &length);
  98. if (!NT_SUCCESS(status)) {
  99. return;
  100. }
  101. printf("IRQ %d ",
  102. *((PUCHAR)keyData + keyData->DataOffset));
  103. RtlInitUnicodeString(&name,
  104. L"IOAddress");
  105. status = NtQueryValueKey(portKey,
  106. &name,
  107. KeyValueFullInformation,
  108. keyData,
  109. 64,
  110. &length);
  111. if (!NT_SUCCESS(status)) {
  112. printf("\n");
  113. return;
  114. }
  115. printf("IO Address %x\n",
  116. *((PULONG)keyData + keyData->DataOffset/4));
  117. return;
  118. }
  119. int _CRTAPI1
  120. main( int argc, char **argv )
  121. {
  122. BYTE buffer[32];
  123. HANDLE volumeHandle;
  124. STRING string;
  125. UNICODE_STRING unicodeString;
  126. OBJECT_ATTRIBUTES objectAttributes;
  127. NTSTATUS ntStatus;
  128. IO_STATUS_BLOCK statusBlock;
  129. ULONG portNumber = 0;
  130. PSCSI_ADAPTER_BUS_INFO adapterInfo;
  131. PSCSI_BUS_DATA busData;
  132. PSCSI_INQUIRY_DATA inquiryData;
  133. UCHAR prevDeviceInquiryData[INQUIRYDATABUFFERSIZE];
  134. PINQUIRYDATA deviceInquiryData;
  135. ULONG bytesTransferred, i, j;
  136. ULONG deviceNumber;
  137. BOOLEAN newDisk = FALSE;
  138. BOOLEAN newCdrom = FALSE;
  139. UCHAR prevPathId;
  140. UCHAR prevTargetId;
  141. UCHAR prevLun;
  142. BOOLEAN prevDeviceClaimed;
  143. UCHAR lunExtra;
  144. printf("\nWindows NT SCSI Bus Rescan Version 1.0\n");
  145. while (TRUE) {
  146. memset( buffer, 0, sizeof( buffer ) );
  147. sprintf( buffer,
  148. "\\\\.\\Scsi%d:",
  149. portNumber);
  150. //
  151. // Open the volume with the DOS name.
  152. //
  153. volumeHandle = CreateFile( buffer,
  154. GENERIC_READ,
  155. FILE_SHARE_READ | FILE_SHARE_WRITE,
  156. NULL,
  157. OPEN_EXISTING,
  158. 0,
  159. 0 );
  160. if( volumeHandle == INVALID_HANDLE_VALUE ) {
  161. break;
  162. }
  163. //
  164. // Issue rescan device control.
  165. //
  166. if( !DeviceIoControl( volumeHandle,
  167. IOCTL_SCSI_RESCAN_BUS,
  168. NULL,
  169. 0,
  170. NULL,
  171. 0,
  172. &bytesTransferred,
  173. NULL ) ) {
  174. printf( "Rescan SCSI port %d failed [Error %d].\n", portNumber, GetLastError() );
  175. CloseHandle( volumeHandle );
  176. exit(4);
  177. }
  178. //
  179. // Get a big chuck of memory to store the SCSI bus data.
  180. //
  181. adapterInfo = malloc( 0x400 );
  182. if (adapterInfo == NULL) {
  183. printf( "Can't allocate memory for bus data\n" );
  184. CloseHandle( volumeHandle );
  185. exit(4);
  186. }
  187. //
  188. // Issue device control to get configuration information.
  189. //
  190. if (!DeviceIoControl( volumeHandle,
  191. IOCTL_SCSI_GET_INQUIRY_DATA,
  192. NULL,
  193. 0,
  194. adapterInfo,
  195. 0x400,
  196. &bytesTransferred,
  197. NULL)) {
  198. printf( "Get SCSI bus data failed [Error %d].\n", GetLastError() );
  199. CloseHandle( volumeHandle );
  200. exit(4);
  201. }
  202. GetDriverName(portNumber);
  203. //
  204. // Display devices on buses.
  205. //
  206. for (i=0; i < adapterInfo->NumberOfBuses; i++) {
  207. busData = &adapterInfo->BusData[i];
  208. printf( "\nBus TID LUN In use Type Vendor FW Rev Advanced SCSI\n" );
  209. printf( "===============================================================================\n" );
  210. printf("%2d %2d %2d %2d Initiator",
  211. i,
  212. busData->InitiatorBusId & 0x7,
  213. 0,
  214. 1);
  215. inquiryData =
  216. (PSCSI_INQUIRY_DATA)((PUCHAR)adapterInfo + busData->InquiryDataOffset);
  217. memset(&prevDeviceInquiryData, 0, INQUIRYDATABUFFERSIZE);
  218. prevPathId = 0xFF;
  219. prevTargetId = 0xFF;
  220. prevLun = 0xFF;
  221. prevDeviceClaimed = 0xFF;
  222. for (j=0; j<busData->NumberOfLogicalUnits; j++) {
  223. //
  224. // Make sure VendorId string is null terminated.
  225. //
  226. deviceInquiryData = (PINQUIRYDATA)&inquiryData->InquiryData[0];
  227. deviceInquiryData->VendorSpecific[0] = '\0';
  228. if (prevPathId != inquiryData->PathId ||
  229. prevTargetId != inquiryData->TargetId ||
  230. prevLun != (inquiryData->Lun-1) ||
  231. prevDeviceClaimed != inquiryData->DeviceClaimed ||
  232. memcmp( &prevDeviceInquiryData, deviceInquiryData, INQUIRYDATABUFFERSIZE)
  233. ) {
  234. lunExtra = 0;
  235. printf("\n%2d %2d %2d %2d ",
  236. inquiryData->PathId,
  237. inquiryData->TargetId,
  238. inquiryData->Lun,
  239. inquiryData->DeviceClaimed);
  240. } else {
  241. lunExtra += 1;
  242. printf("\r%2d %2d %2d-%1d %2d ",
  243. inquiryData->PathId,
  244. inquiryData->TargetId,
  245. inquiryData->Lun-lunExtra,
  246. inquiryData->Lun,
  247. inquiryData->DeviceClaimed);
  248. }
  249. prevPathId = inquiryData->PathId;
  250. prevTargetId = inquiryData->TargetId;
  251. prevLun = inquiryData->Lun;
  252. prevDeviceClaimed = inquiryData->DeviceClaimed;
  253. memmove( &prevDeviceInquiryData, deviceInquiryData, INQUIRYDATABUFFERSIZE);
  254. //
  255. // Determine the perpherial type.
  256. //
  257. switch (deviceInquiryData->DeviceType) {
  258. case DIRECT_ACCESS_DEVICE:
  259. if (!inquiryData->DeviceClaimed) {
  260. newDisk = TRUE;
  261. }
  262. printf("Disk Drive ");
  263. break;
  264. case SEQUENTIAL_ACCESS_DEVICE:
  265. printf("Tape Drive ");
  266. break;
  267. case PRINTER_DEVICE:
  268. printf("Printer ");
  269. break;
  270. case WRITE_ONCE_READ_MULTIPLE_DEVICE:
  271. printf("Worm Drive ");
  272. break;
  273. case READ_ONLY_DIRECT_ACCESS_DEVICE:
  274. if (!inquiryData->DeviceClaimed) {
  275. newCdrom = TRUE;
  276. }
  277. printf("CdRom Drive");
  278. break;
  279. case SCANNER_DEVICE:
  280. printf("Scanner ");
  281. break;
  282. case OPTICAL_DEVICE:
  283. if (!inquiryData->DeviceClaimed) {
  284. newDisk = TRUE;
  285. }
  286. printf("OpticalDisk");
  287. break;
  288. case MEDIUM_CHANGER:
  289. printf("MediumChanger");
  290. break;
  291. case COMMUNICATION_DEVICE:
  292. printf("Communication");
  293. break;
  294. default:
  295. printf("OtherPeripheral");
  296. }
  297. //
  298. // Display product information.
  299. //
  300. printf(" %s", deviceInquiryData->VendorId);
  301. //
  302. // Display SCSI capabilities.
  303. //
  304. printf(" ");
  305. if (deviceInquiryData->Synchronous) {
  306. printf(" SN");
  307. }
  308. if (deviceInquiryData->CommandQueue) {
  309. printf(" CQ");
  310. }
  311. if (deviceInquiryData->Wide16Bit) {
  312. printf(" W16");
  313. }
  314. if (deviceInquiryData->Wide32Bit) {
  315. printf(" W32");
  316. }
  317. if (deviceInquiryData->SoftReset) {
  318. printf(" SR");
  319. }
  320. if (deviceInquiryData->LinkedCommands) {
  321. printf(" LC");
  322. }
  323. if (deviceInquiryData->RelativeAddressing) {
  324. printf(" RA");
  325. }
  326. //
  327. // Get next device data.
  328. //
  329. inquiryData =
  330. (PSCSI_INQUIRY_DATA)((PUCHAR)adapterInfo + inquiryData->NextInquiryDataOffset);
  331. }
  332. printf("\n");
  333. }
  334. free (adapterInfo);
  335. CloseHandle( volumeHandle );
  336. portNumber++;
  337. }
  338. if (newDisk) {
  339. //
  340. // Send IOCTL_DISK_FIND_NEW_DEVICES commands to each existing disk.
  341. //
  342. deviceNumber = 0;
  343. while (TRUE) {
  344. memset(buffer, 0, sizeof(buffer));
  345. sprintf(buffer,
  346. "\\Device\\Harddisk%d\\Partition0",
  347. deviceNumber);
  348. RtlInitString(&string,
  349. buffer);
  350. ntStatus = RtlAnsiStringToUnicodeString(&unicodeString,
  351. &string,
  352. TRUE);
  353. if (!NT_SUCCESS(ntStatus)) {
  354. continue;
  355. }
  356. InitializeObjectAttributes(&objectAttributes,
  357. &unicodeString,
  358. 0,
  359. NULL,
  360. NULL);
  361. ntStatus = NtOpenFile(&volumeHandle,
  362. FILE_READ_DATA |
  363. FILE_WRITE_DATA |
  364. SYNCHRONIZE,
  365. &objectAttributes,
  366. &statusBlock,
  367. FILE_SHARE_READ |
  368. FILE_SHARE_WRITE,
  369. FILE_SYNCHRONOUS_IO_ALERT);
  370. if (!NT_SUCCESS(ntStatus)) {
  371. break;
  372. }
  373. //
  374. // Issue find device device control.
  375. //
  376. if (DeviceIoControl( volumeHandle,
  377. IOCTL_DISK_FIND_NEW_DEVICES,
  378. NULL,
  379. 0,
  380. NULL,
  381. 0,
  382. &bytesTransferred,
  383. NULL ) ) {
  384. printf( "Found new disk (%d)\n", deviceNumber );
  385. }
  386. CloseHandle( volumeHandle );
  387. deviceNumber++;
  388. }
  389. }
  390. if (newCdrom) {
  391. //
  392. // Send IOCTL_CDROM_FIND_NEW_DEVICES commands to each existing cdrom.
  393. //
  394. deviceNumber = 0;
  395. while (TRUE) {
  396. memset(buffer, 0, sizeof(buffer));
  397. sprintf(buffer,
  398. "\\Device\\Cdrom%d",
  399. deviceNumber);
  400. RtlInitString(&string,
  401. buffer);
  402. ntStatus = RtlAnsiStringToUnicodeString(&unicodeString,
  403. &string,
  404. TRUE);
  405. if (!NT_SUCCESS(ntStatus)) {
  406. continue;
  407. }
  408. InitializeObjectAttributes(&objectAttributes,
  409. &unicodeString,
  410. 0,
  411. NULL,
  412. NULL);
  413. ntStatus = NtOpenFile(&volumeHandle,
  414. FILE_READ_DATA |
  415. FILE_WRITE_DATA |
  416. SYNCHRONIZE,
  417. &objectAttributes,
  418. &statusBlock,
  419. FILE_SHARE_READ |
  420. FILE_SHARE_WRITE,
  421. FILE_SYNCHRONOUS_IO_ALERT);
  422. if (!NT_SUCCESS(ntStatus)) {
  423. break;
  424. }
  425. //
  426. // Issue find device device control.
  427. //
  428. if (DeviceIoControl( volumeHandle,
  429. IOCTL_CDROM_FIND_NEW_DEVICES,
  430. NULL,
  431. 0,
  432. NULL,
  433. 0,
  434. &bytesTransferred,
  435. NULL ) ) {
  436. printf( "Found new cdrom (%d)\n", deviceNumber );
  437. }
  438. CloseHandle( volumeHandle );
  439. deviceNumber++;
  440. }
  441. }
  442. return(0);
  443. }