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.

2322 lines
66 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. netboot.c
  5. Abstract:
  6. This module contains the code to initialize network boot.
  7. Author:
  8. Chuck Lenzmeier (chuckl) December 27, 1996
  9. Environment:
  10. Kernel mode, system initialization code
  11. Revision History:
  12. Colin Watson (colinw) November 1997 Add CSC support
  13. --*/
  14. #include "iop.h"
  15. #pragma hdrstop
  16. #include <regstrp.h>
  17. #include <ntddip.h>
  18. #include <nbtioctl.h>
  19. #include <ntddnfs.h>
  20. #include <ntddbrow.h>
  21. #include <ntddtcp.h>
  22. #include <setupblk.h>
  23. #include <remboot.h>
  24. #ifdef ALLOC_DATA_PRAGMA
  25. #pragma const_seg("INITCONST")
  26. #endif
  27. #include <oscpkt.h>
  28. #include <windef.h>
  29. #include <tdiinfo.h>
  30. #ifndef NT
  31. #define NT
  32. #include <ipinfo.h>
  33. #undef NT
  34. #else
  35. #include <ipinfo.h>
  36. #endif
  37. #include <devguid.h>
  38. extern BOOLEAN ExpInTextModeSetup;
  39. BOOLEAN IopRemoteBootCardInitialized = FALSE;
  40. //
  41. // TCP/IP definitions
  42. //
  43. #define DEFAULT_DEST 0
  44. #define DEFAULT_DEST_MASK 0
  45. #define DEFAULT_METRIC 1
  46. NTSTATUS
  47. IopWriteIpAddressToRegistry(
  48. HANDLE handle,
  49. PWCHAR regkey,
  50. PUCHAR value
  51. );
  52. NTSTATUS
  53. IopTCPQueryInformationEx(
  54. IN HANDLE TCPHandle,
  55. IN TDIObjectID FAR *ID,
  56. OUT void FAR *Buffer,
  57. IN OUT DWORD FAR *BufferSize,
  58. IN OUT BYTE FAR *Context
  59. );
  60. NTSTATUS
  61. IopTCPSetInformationEx(
  62. IN HANDLE TCPHandle,
  63. IN TDIObjectID FAR *ID,
  64. IN void FAR *Buffer,
  65. IN DWORD FAR BufferSize
  66. );
  67. NTSTATUS
  68. IopSetDefaultGateway(
  69. IN ULONG GatewayAddress
  70. );
  71. NTSTATUS
  72. IopCacheNetbiosNameForIpAddress(
  73. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  74. );
  75. VOID
  76. IopAssignNetworkDriveLetter (
  77. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  78. );
  79. //
  80. // The following allows the I/O system's initialization routines to be
  81. // paged out of memory.
  82. //
  83. #ifdef ALLOC_PRAGMA
  84. __inline long
  85. htonl(long x);
  86. #pragma alloc_text(INIT,IopAddRemoteBootValuesToRegistry)
  87. #pragma alloc_text(INIT,IopStartNetworkForRemoteBoot)
  88. #pragma alloc_text(INIT,IopStartTcpIpForRemoteBoot)
  89. #pragma alloc_text(INIT,IopIsRemoteBootCard)
  90. #pragma alloc_text(INIT,IopSetupRemoteBootCard)
  91. #pragma alloc_text(INIT,IopAssignNetworkDriveLetter)
  92. #pragma alloc_text(INIT,IopWriteIpAddressToRegistry)
  93. #pragma alloc_text(INIT,IopSetDefaultGateway)
  94. #pragma alloc_text(INIT,htonl)
  95. #pragma alloc_text(INIT,IopCacheNetbiosNameForIpAddress)
  96. #pragma alloc_text(INIT,IopTCPQueryInformationEx)
  97. #pragma alloc_text(INIT,IopTCPSetInformationEx)
  98. #endif
  99. NTSTATUS
  100. IopAddRemoteBootValuesToRegistry (
  101. PLOADER_PARAMETER_BLOCK LoaderBlock
  102. )
  103. {
  104. NTSTATUS status = STATUS_SUCCESS;
  105. HANDLE handle;
  106. HANDLE serviceHandle;
  107. OBJECT_ATTRIBUTES objectAttributes;
  108. UNICODE_STRING string;
  109. CHAR addressA[16];
  110. WCHAR addressW[16];
  111. STRING addressStringA;
  112. UNICODE_STRING addressStringW;
  113. PUCHAR addressPointer;
  114. PUCHAR p;
  115. PUCHAR q;
  116. UCHAR ntName[128];
  117. WCHAR imagePath[128];
  118. STRING ansiString;
  119. UNICODE_STRING unicodeString;
  120. UNICODE_STRING dnsNameString;
  121. UNICODE_STRING netbiosNameString;
  122. ULONG tmpValue;
  123. if (LoaderBlock->SetupLoaderBlock->ComputerName[0] != 0) {
  124. //
  125. // Convert the name to a Netbios name.
  126. //
  127. _wcsupr( LoaderBlock->SetupLoaderBlock->ComputerName );
  128. RtlInitUnicodeString( &dnsNameString, LoaderBlock->SetupLoaderBlock->ComputerName );
  129. status = RtlDnsHostNameToComputerName(
  130. &netbiosNameString,
  131. &dnsNameString,
  132. TRUE); // allocate netbiosNameString
  133. if ( !NT_SUCCESS(status) ) {
  134. KdPrint(( "IopAddRemoteBootValuesToRegistry: Failed RtlDnsHostNameToComputerName: %x\n", status ));
  135. goto cleanup;
  136. }
  137. //
  138. // Add a value for the computername.
  139. //
  140. RtlInitUnicodeString( &string, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\ComputerName\\ComputerName" );
  141. InitializeObjectAttributes(
  142. &objectAttributes,
  143. &string,
  144. OBJ_CASE_INSENSITIVE,
  145. NULL,
  146. NULL
  147. );
  148. status = NtOpenKey( &handle, KEY_ALL_ACCESS, &objectAttributes );
  149. if ( !NT_SUCCESS(status) ) {
  150. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to open ComputerName key: %x\n", status ));
  151. RtlFreeUnicodeString( &netbiosNameString );
  152. goto cleanup;
  153. }
  154. RtlInitUnicodeString( &string, L"ComputerName" );
  155. status = NtSetValueKey(
  156. handle,
  157. &string,
  158. 0,
  159. REG_SZ,
  160. netbiosNameString.Buffer,
  161. netbiosNameString.Length + sizeof(WCHAR)
  162. );
  163. NtClose( handle );
  164. RtlFreeUnicodeString( &netbiosNameString );
  165. if ( !NT_SUCCESS(status) ) {
  166. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to set ComputerName value: %x\n", status ));
  167. goto cleanup;
  168. }
  169. //
  170. // Add a value for the host name.
  171. //
  172. RtlInitUnicodeString( &string, L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\Tcpip\\Parameters" );
  173. InitializeObjectAttributes(
  174. &objectAttributes,
  175. &string,
  176. OBJ_CASE_INSENSITIVE,
  177. NULL,
  178. NULL
  179. );
  180. status = NtOpenKey( &handle, KEY_ALL_ACCESS, &objectAttributes );
  181. if ( !NT_SUCCESS(status) ) {
  182. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to open Tcpip\\Parameters key: %x\n", status ));
  183. goto cleanup;
  184. }
  185. _wcslwr( LoaderBlock->SetupLoaderBlock->ComputerName );
  186. RtlInitUnicodeString( &string, L"Hostname" );
  187. status = NtSetValueKey(
  188. handle,
  189. &string,
  190. 0,
  191. REG_SZ,
  192. LoaderBlock->SetupLoaderBlock->ComputerName,
  193. (wcslen(LoaderBlock->SetupLoaderBlock->ComputerName) + 1) * sizeof(WCHAR)
  194. );
  195. NtClose( handle );
  196. if ( !NT_SUCCESS(status) ) {
  197. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to set Hostname value: %x\n", status ));
  198. goto cleanup;
  199. }
  200. }
  201. //
  202. // If the UNC path to the system files is supplied then store it in the registry.
  203. //
  204. ASSERT( _stricmp(LoaderBlock->ArcBootDeviceName,"net(0)") == 0 );
  205. RtlInitUnicodeString( &string, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control" );
  206. InitializeObjectAttributes(
  207. &objectAttributes,
  208. &string,
  209. OBJ_CASE_INSENSITIVE,
  210. NULL,
  211. NULL
  212. );
  213. status = NtOpenKey( &handle, KEY_ALL_ACCESS, &objectAttributes );
  214. if ( !NT_SUCCESS(status) ) {
  215. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to open Control key: %x\n", status ));
  216. goto skiproot;
  217. }
  218. p = strrchr( LoaderBlock->NtBootPathName, '\\' ); // find last separator
  219. if ( (p != NULL) && (*(p+1) == 0) ) {
  220. //
  221. // NtBootPathName ends with a backslash, so we need to back up
  222. // to the previous backslash.
  223. //
  224. q = p;
  225. *q = 0;
  226. p = strrchr( LoaderBlock->NtBootPathName, '\\' ); // find last separator
  227. *q = '\\';
  228. }
  229. if ( p == NULL ) {
  230. KdPrint(( "IopAddRemoteBootValuesToRegistry: malformed NtBootPathName: %s\n", LoaderBlock->NtBootPathName ));
  231. NtClose( handle );
  232. goto skiproot;
  233. }
  234. *p = 0; // terminate \server\share\images\machine
  235. strcpy( ntName, "\\Device\\LanmanRedirector");
  236. strcat( ntName, LoaderBlock->NtBootPathName ); // append \server\share\images\machine
  237. *p = '\\';
  238. RtlInitAnsiString( &ansiString, ntName );
  239. RtlAnsiStringToUnicodeString( &unicodeString, &ansiString, TRUE );
  240. RtlInitUnicodeString( &string, L"RemoteBootRoot" );
  241. status = NtSetValueKey(
  242. handle,
  243. &string,
  244. 0,
  245. REG_SZ,
  246. unicodeString.Buffer,
  247. unicodeString.Length + sizeof(WCHAR)
  248. );
  249. RtlFreeUnicodeString( &unicodeString );
  250. if ( !NT_SUCCESS(status) ) {
  251. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to set RemoteBootRoot value: %x\n", status ));
  252. }
  253. if ((LoaderBlock->SetupLoaderBlock->Flags & SETUPBLK_FLAGS_IS_TEXTMODE) != 0) {
  254. strcpy( ntName, "\\Device\\LanmanRedirector");
  255. strcat( ntName, LoaderBlock->SetupLoaderBlock->MachineDirectoryPath );
  256. RtlInitAnsiString( &ansiString, ntName );
  257. RtlAnsiStringToUnicodeString( &unicodeString, &ansiString, TRUE );
  258. RtlInitUnicodeString( &string, L"RemoteBootMachineDirectory" );
  259. status = NtSetValueKey(
  260. handle,
  261. &string,
  262. 0,
  263. REG_SZ,
  264. unicodeString.Buffer,
  265. unicodeString.Length + sizeof(WCHAR)
  266. );
  267. RtlFreeUnicodeString( &unicodeString );
  268. if ( !NT_SUCCESS(status) ) {
  269. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to set RemoteBootMachineDirectory value: %x\n", status ));
  270. }
  271. }
  272. NtClose( handle );
  273. skiproot:
  274. //
  275. // Add registry values for the IP address and subnet mask received
  276. // from DHCP. These are stored under the Tcpip service key and are
  277. // read by both Tcpip and Netbt. The adapter name used is the known
  278. // GUID for the NetbootCard.
  279. //
  280. RtlInitUnicodeString( &string, L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\{54C7D140-09EF-11D1-B25A-F5FE627ED95E}" );
  281. InitializeObjectAttributes(
  282. &objectAttributes,
  283. &string,
  284. OBJ_CASE_INSENSITIVE,
  285. NULL,
  286. NULL
  287. );
  288. status = NtOpenKey( &handle, KEY_ALL_ACCESS, &objectAttributes );
  289. if ( !NT_SUCCESS(status) ) {
  290. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to open Tcpip\\Parameters\\Interfaces\\{54C7D140-09EF-11D1-B25A-F5FE627ED95E} key: %x\n", status ));
  291. goto cleanup;
  292. }
  293. status = IopWriteIpAddressToRegistry(handle,
  294. L"DhcpIPAddress",
  295. (PUCHAR)&(LoaderBlock->SetupLoaderBlock->IpAddress)
  296. );
  297. if ( !NT_SUCCESS(status)) {
  298. NtClose(handle);
  299. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to write DhcpIPAddress: %x\n", status ));
  300. goto cleanup;
  301. }
  302. status = IopWriteIpAddressToRegistry(handle,
  303. L"DhcpSubnetMask",
  304. (PUCHAR)&(LoaderBlock->SetupLoaderBlock->SubnetMask)
  305. );
  306. if ( !NT_SUCCESS(status)) {
  307. NtClose(handle);
  308. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to write DhcpSubnetMask: %x\n", status ));
  309. goto cleanup;
  310. }
  311. status = IopWriteIpAddressToRegistry(handle,
  312. L"DhcpDefaultGateway",
  313. (PUCHAR)&(LoaderBlock->SetupLoaderBlock->DefaultRouter)
  314. );
  315. NtClose(handle);
  316. if ( !NT_SUCCESS(status)) {
  317. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to write DhcpDefaultGateway: %x\n", status ));
  318. goto cleanup;
  319. }
  320. //
  321. // Create the service key for the netboot card. We need to have
  322. // the Type value there or the card won't be initialized.
  323. //
  324. status = IopOpenRegistryKey(&handle,
  325. NULL,
  326. &CmRegistryMachineSystemCurrentControlSetServices,
  327. KEY_ALL_ACCESS,
  328. FALSE
  329. );
  330. if (!NT_SUCCESS(status)) {
  331. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to open CurrentControlSet\\Services: %x\n", status ));
  332. goto cleanup;
  333. }
  334. RtlInitUnicodeString(&string, LoaderBlock->SetupLoaderBlock->NetbootCardServiceName);
  335. InitializeObjectAttributes(&objectAttributes,
  336. &string,
  337. OBJ_CASE_INSENSITIVE,
  338. handle,
  339. (PSECURITY_DESCRIPTOR)NULL
  340. );
  341. status = ZwCreateKey(&serviceHandle,
  342. KEY_ALL_ACCESS,
  343. &objectAttributes,
  344. 0,
  345. (PUNICODE_STRING)NULL,
  346. 0,
  347. &tmpValue // disposition
  348. );
  349. ZwClose(handle);
  350. if (!NT_SUCCESS(status)) {
  351. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to open/create netboot card service key: %x\n", status ));
  352. goto cleanup;
  353. }
  354. //
  355. // Store the image path.
  356. //
  357. IopWstrToUnicodeString(&string, L"ImagePath");
  358. wcscpy(imagePath, L"system32\\drivers\\");
  359. wcscat(imagePath, LoaderBlock->SetupLoaderBlock->NetbootCardDriverName);
  360. status = ZwSetValueKey(serviceHandle,
  361. &string,
  362. TITLE_INDEX_VALUE,
  363. REG_SZ,
  364. imagePath,
  365. (wcslen(imagePath) + 1) * sizeof(WCHAR)
  366. );
  367. if (!NT_SUCCESS(status)) {
  368. NtClose(serviceHandle);
  369. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to write ImagePath: %x\n", status ));
  370. goto cleanup;
  371. }
  372. //
  373. // Store the type.
  374. //
  375. IopWstrToUnicodeString(&string, L"Type");
  376. tmpValue = 1;
  377. ZwSetValueKey(serviceHandle,
  378. &string,
  379. TITLE_INDEX_VALUE,
  380. REG_DWORD,
  381. &tmpValue,
  382. sizeof(tmpValue)
  383. );
  384. NtClose(serviceHandle);
  385. if (!NT_SUCCESS(status)) {
  386. KdPrint(( "IopAddRemoteBootValuesToRegistry: Unable to write Type: %x\n", status ));
  387. }
  388. cleanup:
  389. return status;
  390. }
  391. NTSTATUS
  392. IopStartNetworkForRemoteBoot (
  393. PLOADER_PARAMETER_BLOCK LoaderBlock
  394. )
  395. {
  396. NTSTATUS status;
  397. HANDLE dgHandle;
  398. HANDLE keyHandle;
  399. OBJECT_ATTRIBUTES objectAttributes;
  400. IO_STATUS_BLOCK ioStatusBlock;
  401. UNICODE_STRING string;
  402. UNICODE_STRING computerName;
  403. UNICODE_STRING domainName;
  404. PUCHAR buffer;
  405. ULONG bufferLength;
  406. PLMR_REQUEST_PACKET rrp;
  407. PLMDR_REQUEST_PACKET drrp;
  408. WKSTA_INFO_502 wkstaConfig;
  409. WKSTA_TRANSPORT_INFO_0 wkstaTransportInfo;
  410. LARGE_INTEGER interval;
  411. ULONG length;
  412. PKEY_VALUE_PARTIAL_INFORMATION keyValue;
  413. BOOLEAN startDatagramReceiver;
  414. ULONG enumerateAttempts;
  415. HANDLE RdrHandle;
  416. //
  417. // Initialize for cleanup.
  418. //
  419. buffer = NULL;
  420. computerName.Buffer = NULL;
  421. domainName.Buffer = NULL;
  422. dgHandle = NULL;
  423. RdrHandle = NULL;
  424. //
  425. // Allocate a temporary buffer. It has to be big enough for all the
  426. // various FSCTLs we send down.
  427. //
  428. bufferLength = max(sizeof(LMR_REQUEST_PACKET) + (MAX_PATH + 1) * sizeof(WCHAR) +
  429. (DNLEN + 1) * sizeof(WCHAR),
  430. max(sizeof(LMDR_REQUEST_PACKET),
  431. FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data) + MAX_PATH));
  432. bufferLength = max(bufferLength, sizeof(LMMR_RI_INITIALIZE_SECRET));
  433. buffer = ExAllocatePoolWithTag( NonPagedPool, bufferLength, 'bRoI' );
  434. if (buffer == NULL) {
  435. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to allocate buffer\n"));
  436. status = STATUS_INSUFFICIENT_RESOURCES;
  437. goto cleanup;
  438. }
  439. rrp = (PLMR_REQUEST_PACKET)buffer;
  440. drrp = (PLMDR_REQUEST_PACKET)buffer;
  441. //
  442. // Open the redirector and the datagram receiver.
  443. //
  444. RtlInitUnicodeString( &string, L"\\Device\\LanmanRedirector" );
  445. InitializeObjectAttributes(
  446. &objectAttributes,
  447. &string,
  448. OBJ_CASE_INSENSITIVE,
  449. NULL,
  450. NULL
  451. );
  452. status = NtCreateFile(
  453. &RdrHandle,
  454. GENERIC_READ | GENERIC_WRITE,
  455. &objectAttributes,
  456. &ioStatusBlock,
  457. NULL,
  458. 0,
  459. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  460. FILE_OPEN,
  461. FILE_SYNCHRONOUS_IO_NONALERT,
  462. NULL,
  463. 0
  464. );
  465. if ( !NT_SUCCESS(status) ) {
  466. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to open redirector: %x\n", status ));
  467. goto cleanup;
  468. }
  469. RtlInitUnicodeString( &string, DD_BROWSER_DEVICE_NAME_U );
  470. InitializeObjectAttributes(
  471. &objectAttributes,
  472. &string,
  473. OBJ_CASE_INSENSITIVE,
  474. NULL,
  475. NULL
  476. );
  477. status = NtCreateFile(
  478. &dgHandle,
  479. GENERIC_READ | GENERIC_WRITE,
  480. &objectAttributes,
  481. &ioStatusBlock,
  482. NULL,
  483. 0,
  484. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  485. FILE_OPEN,
  486. FILE_SYNCHRONOUS_IO_NONALERT,
  487. NULL,
  488. 0
  489. );
  490. if ( !NT_SUCCESS(status) ) {
  491. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to open datagram receiver: %x\n", status ));
  492. goto cleanup;
  493. }
  494. //
  495. // If the setup loader block has a disk secret in it provided by the
  496. // loader, pass this down to the redirector (do this before sending
  497. // the LMR_START, since that uses this information).
  498. //
  499. {
  500. PLMMR_RI_INITIALIZE_SECRET RbInit = (PLMMR_RI_INITIALIZE_SECRET)buffer;
  501. ASSERT(LoaderBlock->SetupLoaderBlock->NetBootSecret != NULL);
  502. RtlCopyMemory(
  503. &RbInit->Secret,
  504. LoaderBlock->SetupLoaderBlock->NetBootSecret,
  505. sizeof(RI_SECRET));
  506. status = NtFsControlFile(
  507. RdrHandle,
  508. NULL,
  509. NULL,
  510. NULL,
  511. &ioStatusBlock,
  512. FSCTL_LMMR_RI_INITIALIZE_SECRET,
  513. buffer,
  514. sizeof(LMMR_RI_INITIALIZE_SECRET),
  515. NULL,
  516. 0
  517. );
  518. if ( NT_SUCCESS(status) ) {
  519. status = ioStatusBlock.Status;
  520. }
  521. if ( !NT_SUCCESS(status) ) {
  522. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to FSCTL(RB initialize) redirector: %x\n", status ));
  523. goto cleanup;
  524. }
  525. }
  526. //
  527. // Read the computer name and domain name from the registry so we
  528. // can give them to the datagram receiver. During textmode setup
  529. // the domain name will not be there, so we won't start the datagram
  530. // receiver, which is fine.
  531. //
  532. RtlInitUnicodeString( &string, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\ComputerName\\ComputerName" );
  533. InitializeObjectAttributes(
  534. &objectAttributes,
  535. &string,
  536. OBJ_CASE_INSENSITIVE,
  537. NULL,
  538. NULL
  539. );
  540. status = NtOpenKey( &keyHandle, KEY_ALL_ACCESS, &objectAttributes );
  541. if ( !NT_SUCCESS(status) ) {
  542. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to open ComputerName key: %x\n", status ));
  543. goto cleanup;
  544. }
  545. RtlInitUnicodeString( &string, L"ComputerName" );
  546. keyValue = (PKEY_VALUE_PARTIAL_INFORMATION)buffer;
  547. RtlZeroMemory(buffer, bufferLength);
  548. status = NtQueryValueKey(
  549. keyHandle,
  550. &string,
  551. KeyValuePartialInformation,
  552. keyValue,
  553. bufferLength,
  554. &length);
  555. NtClose( keyHandle );
  556. if ( !NT_SUCCESS(status) ) {
  557. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to query ComputerName value: %x\n", status ));
  558. goto cleanup;
  559. }
  560. if ( !RtlCreateUnicodeString(&computerName, (PWSTR)keyValue->Data) ) {
  561. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to create ComputerName string\n" ));
  562. status = STATUS_INSUFFICIENT_RESOURCES;
  563. goto cleanup;
  564. }
  565. domainName.Length = 0;
  566. RtlInitUnicodeString( &string, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\ComputerName\\DomainName" );
  567. InitializeObjectAttributes(
  568. &objectAttributes,
  569. &string,
  570. OBJ_CASE_INSENSITIVE,
  571. NULL,
  572. NULL
  573. );
  574. status = NtOpenKey( &keyHandle, KEY_ALL_ACCESS, &objectAttributes );
  575. if ( !NT_SUCCESS(status) ) {
  576. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to open DomainName key: %x\n", status ));
  577. startDatagramReceiver = FALSE;
  578. } else {
  579. RtlInitUnicodeString( &string, L"DomainName" );
  580. keyValue = (PKEY_VALUE_PARTIAL_INFORMATION)buffer;
  581. RtlZeroMemory(buffer, bufferLength);
  582. status = NtQueryValueKey(
  583. keyHandle,
  584. &string,
  585. KeyValuePartialInformation,
  586. keyValue,
  587. bufferLength,
  588. &length);
  589. NtClose( keyHandle );
  590. if ( !NT_SUCCESS(status) ) {
  591. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to query Domain value: %x\n", status ));
  592. startDatagramReceiver = FALSE;
  593. } else {
  594. if ( !RtlCreateUnicodeString(&domainName, (PWSTR)keyValue->Data) ) {
  595. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to create DomainName string\n" ));
  596. status = STATUS_INSUFFICIENT_RESOURCES;
  597. goto cleanup;
  598. }
  599. startDatagramReceiver = TRUE;
  600. }
  601. }
  602. //
  603. // Tell the redir to start.
  604. //
  605. rrp->Type = ConfigInformation;
  606. rrp->Version = REQUEST_PACKET_VERSION;
  607. rrp->Parameters.Start.RedirectorNameLength = computerName.Length;
  608. RtlCopyMemory(rrp->Parameters.Start.RedirectorName,
  609. computerName.Buffer,
  610. computerName.Length);
  611. rrp->Parameters.Start.DomainNameLength = domainName.Length;
  612. RtlCopyMemory(((PUCHAR)rrp->Parameters.Start.RedirectorName) + computerName.Length,
  613. domainName.Buffer,
  614. domainName.Length);
  615. RtlFreeUnicodeString(&computerName);
  616. RtlFreeUnicodeString(&domainName);
  617. wkstaConfig.wki502_char_wait = 3600;
  618. wkstaConfig.wki502_maximum_collection_count = 16;
  619. wkstaConfig.wki502_collection_time = 250;
  620. wkstaConfig.wki502_keep_conn = 600;
  621. wkstaConfig.wki502_max_cmds = 5;
  622. wkstaConfig.wki502_sess_timeout = 45;
  623. wkstaConfig.wki502_siz_char_buf = 512;
  624. wkstaConfig.wki502_max_threads = 17;
  625. wkstaConfig.wki502_lock_quota = 6144;
  626. wkstaConfig.wki502_lock_increment = 10;
  627. wkstaConfig.wki502_lock_maximum = 500;
  628. wkstaConfig.wki502_pipe_increment = 10;
  629. wkstaConfig.wki502_pipe_maximum = 500;
  630. wkstaConfig.wki502_cache_file_timeout = 40;
  631. wkstaConfig.wki502_dormant_file_limit = 45;
  632. wkstaConfig.wki502_read_ahead_throughput = MAXULONG;
  633. wkstaConfig.wki502_num_mailslot_buffers = 3;
  634. wkstaConfig.wki502_num_srv_announce_buffers = 20;
  635. wkstaConfig.wki502_max_illegal_datagram_events = 5;
  636. wkstaConfig.wki502_illegal_datagram_event_reset_frequency = 60;
  637. wkstaConfig.wki502_log_election_packets = FALSE;
  638. wkstaConfig.wki502_use_opportunistic_locking = TRUE;
  639. wkstaConfig.wki502_use_unlock_behind = TRUE;
  640. wkstaConfig.wki502_use_close_behind = TRUE;
  641. wkstaConfig.wki502_buf_named_pipes = TRUE;
  642. wkstaConfig.wki502_use_lock_read_unlock = TRUE;
  643. wkstaConfig.wki502_utilize_nt_caching = TRUE;
  644. wkstaConfig.wki502_use_raw_read = TRUE;
  645. wkstaConfig.wki502_use_raw_write = TRUE;
  646. wkstaConfig.wki502_use_write_raw_data = TRUE;
  647. wkstaConfig.wki502_use_encryption = TRUE;
  648. wkstaConfig.wki502_buf_files_deny_write = TRUE;
  649. wkstaConfig.wki502_buf_read_only_files = TRUE;
  650. wkstaConfig.wki502_force_core_create_mode = TRUE;
  651. wkstaConfig.wki502_use_512_byte_max_transfer = FALSE;
  652. status = NtFsControlFile(
  653. RdrHandle,
  654. NULL,
  655. NULL,
  656. NULL,
  657. &ioStatusBlock,
  658. FSCTL_LMR_START | 0x80000000,
  659. rrp,
  660. sizeof(LMR_REQUEST_PACKET) +
  661. rrp->Parameters.Start.RedirectorNameLength +
  662. rrp->Parameters.Start.DomainNameLength,
  663. &wkstaConfig,
  664. sizeof(wkstaConfig)
  665. );
  666. if ( NT_SUCCESS(status) ) {
  667. status = ioStatusBlock.Status;
  668. }
  669. if ( !NT_SUCCESS(status) ) {
  670. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to FSCTL(start) redirector: %x\n", status ));
  671. goto cleanup;
  672. }
  673. if (startDatagramReceiver) {
  674. //
  675. // Tell the datagram receiver to start.
  676. //
  677. drrp->Version = LMDR_REQUEST_PACKET_VERSION;
  678. drrp->Parameters.Start.NumberOfMailslotBuffers = 16;
  679. drrp->Parameters.Start.NumberOfServerAnnounceBuffers = 20;
  680. drrp->Parameters.Start.IllegalDatagramThreshold = 5;
  681. drrp->Parameters.Start.EventLogResetFrequency = 60;
  682. drrp->Parameters.Start.LogElectionPackets = FALSE;
  683. drrp->Parameters.Start.IsLanManNt = FALSE;
  684. status = NtDeviceIoControlFile(
  685. dgHandle,
  686. NULL,
  687. NULL,
  688. NULL,
  689. &ioStatusBlock,
  690. IOCTL_LMDR_START,
  691. drrp,
  692. sizeof(LMDR_REQUEST_PACKET),
  693. NULL,
  694. 0
  695. );
  696. if ( NT_SUCCESS(status) ) {
  697. status = ioStatusBlock.Status;
  698. }
  699. NtClose( dgHandle );
  700. dgHandle = NULL;
  701. if ( !NT_SUCCESS(status) ) {
  702. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to IOCTL(start) datagram receiver: %x\n", status ));
  703. goto cleanup;
  704. }
  705. } else {
  706. NtClose( dgHandle );
  707. dgHandle = NULL;
  708. //
  709. // Tell the redir to bind to the transports.
  710. //
  711. // Note: In the current redirector implementation, this call just
  712. // tells the redirector to register for TDI PnP notifications.
  713. // Starting the datagram receiver also does this, so we only issue
  714. // this FSCTL if we're not starting the datagram receiver.
  715. //
  716. status = NtFsControlFile(
  717. RdrHandle,
  718. NULL,
  719. NULL,
  720. NULL,
  721. &ioStatusBlock,
  722. FSCTL_LMR_BIND_TO_TRANSPORT | 0x80000000,
  723. NULL,
  724. 0,
  725. NULL,
  726. 0
  727. );
  728. if ( NT_SUCCESS(status) ) {
  729. status = ioStatusBlock.Status;
  730. }
  731. if ( !NT_SUCCESS(status) ) {
  732. KdPrint(( "IopStartNetworkForRemoteBoot: Unable to FSCTL(bind) redirector: %x\n", status ));
  733. goto cleanup;
  734. }
  735. }
  736. {
  737. //
  738. // Loop until the redirector is bound to the transport. It may take a
  739. // while because TDI defers notification of binding to a worker thread.
  740. // We start with a half a second wait and double it each time, trying
  741. // five times total.
  742. //
  743. interval.QuadPart = -500 * 1000 * 10; // 1/2 second, relative
  744. enumerateAttempts = 0;
  745. while (TRUE) {
  746. KeDelayExecutionThread(KernelMode, FALSE, &interval);
  747. RtlZeroMemory(rrp, sizeof(LMR_REQUEST_PACKET));
  748. rrp->Type = EnumerateTransports;
  749. rrp->Version = REQUEST_PACKET_VERSION;
  750. status = NtFsControlFile(
  751. RdrHandle,
  752. NULL,
  753. NULL,
  754. NULL,
  755. &ioStatusBlock,
  756. FSCTL_LMR_ENUMERATE_TRANSPORTS,
  757. rrp,
  758. sizeof(LMR_REQUEST_PACKET),
  759. &wkstaTransportInfo,
  760. sizeof(wkstaTransportInfo)
  761. );
  762. if ( NT_SUCCESS(status) ) {
  763. status = ioStatusBlock.Status;
  764. }
  765. if ( !NT_SUCCESS(status) ) {
  766. //KdPrint(( "IopStartNetworkForRemoteBoot: Unable to FSCTL(enumerate) redirector: %x\n", status ));
  767. } else if (rrp->Parameters.Get.TotalBytesNeeded == 0) {
  768. //KdPrint(( "IopStartNetworkForRemoteBoot: FSCTL(enumerate) returned 0 entries\n" ));
  769. } else {
  770. break;
  771. }
  772. ++enumerateAttempts;
  773. if (enumerateAttempts == 5) {
  774. KdPrint(( "IopStartNetworkForRemoteBoot: Redirector didn't start\n" ));
  775. status = STATUS_REDIRECTOR_NOT_STARTED;
  776. goto cleanup;
  777. }
  778. interval.QuadPart *= 2;
  779. }
  780. }
  781. //
  782. // Prime the transport.
  783. //
  784. IopSetDefaultGateway(LoaderBlock->SetupLoaderBlock->DefaultRouter);
  785. IopCacheNetbiosNameForIpAddress(LoaderBlock);
  786. IopAssignNetworkDriveLetter(LoaderBlock);
  787. cleanup:
  788. RtlFreeUnicodeString( &computerName );
  789. RtlFreeUnicodeString( &domainName );
  790. if ( buffer != NULL ) {
  791. ExFreePool( buffer );
  792. }
  793. if ( dgHandle != NULL ) {
  794. NtClose( dgHandle );
  795. }
  796. return status;
  797. }
  798. VOID
  799. IopAssignNetworkDriveLetter (
  800. PLOADER_PARAMETER_BLOCK LoaderBlock
  801. )
  802. {
  803. PUCHAR p;
  804. PUCHAR q;
  805. UCHAR ntName[128];
  806. STRING ansiString;
  807. UNICODE_STRING unicodeString;
  808. UNICODE_STRING unicodeString2;
  809. NTSTATUS status;
  810. //
  811. // Create the symbolic link of X: to the redirector. We do this
  812. // after the redirector has loaded, but before AssignDriveLetters
  813. // is called the first time in textmode setup (once that has
  814. // happened, the drive letters will stick).
  815. //
  816. // Note that we use X: for the textmode setup phase of a remote
  817. // installation. But for a true remote boot, we use C:.
  818. //
  819. if ((LoaderBlock->SetupLoaderBlock->Flags & (SETUPBLK_FLAGS_REMOTE_INSTALL |
  820. SETUPBLK_FLAGS_SYSPREP_INSTALL)) != 0) {
  821. RtlInitUnicodeString( &unicodeString2, L"\\DosDevices\\X:");
  822. } else {
  823. RtlInitUnicodeString( &unicodeString2, L"\\DosDevices\\C:");
  824. }
  825. //
  826. // If this is a remote boot setup boot, NtBootPathName is of the
  827. // form \<server>\<share>\setup\<install-directory>\<platform>.
  828. // We want the root of the X: drive to be the root of the install
  829. // directory.
  830. //
  831. // If this is a normal remote boot, NtBootPathName is of the form
  832. // \<server>\<share>\images\<machine>\winnt. We want the root of
  833. // the X: drive to be the root of the machine directory.
  834. //
  835. // Thus in either case, we need to remove the last element of the
  836. // path.
  837. //
  838. p = strrchr( LoaderBlock->NtBootPathName, '\\' ); // find last separator
  839. if ( (p != NULL) && (*(p+1) == 0) ) {
  840. //
  841. // NtBootPathName ends with a backslash, so we need to back up
  842. // to the previous backslash.
  843. //
  844. q = p;
  845. *q = 0;
  846. p = strrchr( LoaderBlock->NtBootPathName, '\\' ); // find last separator
  847. *q = '\\';
  848. }
  849. if ( p == NULL ) {
  850. KdPrint(( "IopAssignNetworkDriveLetter: malformed NtBootPathName: %s\n", LoaderBlock->NtBootPathName ));
  851. KeBugCheck( ASSIGN_DRIVE_LETTERS_FAILED );
  852. }
  853. *p = 0; // terminate \server\share\images\machine
  854. strcpy( ntName, "\\Device\\LanmanRedirector");
  855. strcat( ntName, LoaderBlock->NtBootPathName ); // append \server\share\images\machine
  856. RtlInitAnsiString( &ansiString, ntName );
  857. RtlAnsiStringToUnicodeString( &unicodeString, &ansiString, TRUE );
  858. status = IoCreateSymbolicLink(&unicodeString2, &unicodeString);
  859. if (!NT_SUCCESS(status)) {
  860. KdPrint(( "IopAssignNetworkDriveLetter: unable to create DOS link for redirected boot drive: %x\n", status ));
  861. KeBugCheck( ASSIGN_DRIVE_LETTERS_FAILED );
  862. }
  863. // DbgPrint("IopAssignNetworkDriveLetter: assigned %wZ to %wZ\n", &unicodeString2, &unicodeString);
  864. RtlFreeUnicodeString( &unicodeString );
  865. *p = '\\'; // restore string
  866. return;
  867. }
  868. NTSTATUS
  869. IopStartTcpIpForRemoteBoot (
  870. PLOADER_PARAMETER_BLOCK LoaderBlock
  871. )
  872. {
  873. UNICODE_STRING IpString;
  874. NTSTATUS status = STATUS_SUCCESS;
  875. HANDLE handle;
  876. OBJECT_ATTRIBUTES objectAttributes;
  877. IO_STATUS_BLOCK ioStatusBlock;
  878. IP_SET_ADDRESS_REQUEST IpRequest;
  879. RtlInitUnicodeString( &IpString, DD_IP_DEVICE_NAME );
  880. InitializeObjectAttributes(
  881. &objectAttributes,
  882. &IpString,
  883. OBJ_CASE_INSENSITIVE,
  884. NULL,
  885. NULL
  886. );
  887. IpRequest.Context = (USHORT)2;
  888. IpRequest.Address = LoaderBlock->SetupLoaderBlock->IpAddress;
  889. IpRequest.SubnetMask = LoaderBlock->SetupLoaderBlock->SubnetMask;
  890. status = NtCreateFile(
  891. &handle,
  892. GENERIC_READ | GENERIC_WRITE,
  893. &objectAttributes,
  894. &ioStatusBlock,
  895. NULL,
  896. 0,
  897. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  898. FILE_OPEN,
  899. FILE_SYNCHRONOUS_IO_NONALERT,
  900. NULL,
  901. 0
  902. );
  903. if ( !NT_SUCCESS(status) ) {
  904. KdPrint(( "IopStartTcpIpForRemoteBoot: Unable to open IP: %x\n", status ));
  905. goto cleanup;
  906. }
  907. status = NtDeviceIoControlFile(
  908. handle,
  909. NULL,
  910. NULL,
  911. NULL,
  912. &ioStatusBlock,
  913. IOCTL_IP_SET_ADDRESS_DUP,
  914. &IpRequest,
  915. sizeof(IP_SET_ADDRESS_REQUEST),
  916. NULL,
  917. 0
  918. );
  919. NtClose( handle );
  920. if ( !NT_SUCCESS(status) ) {
  921. KdPrint(( "IopStartTcpIpForRemoteBoot: Unable to IOCTL IP: %x\n", status ));
  922. goto cleanup;
  923. }
  924. cleanup:
  925. return status;
  926. }
  927. BOOLEAN
  928. IopIsRemoteBootCard(
  929. IN PIO_RESOURCE_REQUIREMENTS_LIST ResourceRequirements,
  930. IN PLOADER_PARAMETER_BLOCK LoaderBlock,
  931. IN PWCHAR HwIds
  932. )
  933. /*++
  934. Routine Description:
  935. This function determines if the card described by the hwIds is the
  936. remote boot network card. It checks against the hardware ID for the
  937. card that is stored in the setup loader block.
  938. THIS ASSUMES THAT IOREMOTEBOOTCLIENT IS TRUE AND THAT LOADERBLOCK
  939. IS VALID.
  940. Arguments:
  941. DeviceNode - Device node for the card in question.
  942. LoaderBlock - Supplies a pointer to the loader parameter block that was
  943. created by the OS Loader.
  944. HwIds - The hardware IDs for the device in question.
  945. Return Value:
  946. TRUE or FALSE.
  947. --*/
  948. {
  949. PSETUP_LOADER_BLOCK setupLoaderBlock;
  950. PWCHAR curHwId;
  951. //
  952. // setupLoaderBlock will always be non-NULL if we are
  953. // remote booting, even if we are not in setup.
  954. //
  955. setupLoaderBlock = LoaderBlock->SetupLoaderBlock;
  956. //
  957. // Scan through the HwIds for a match.
  958. //
  959. curHwId = HwIds;
  960. while (*curHwId != L'\0') {
  961. if (wcscmp(curHwId, setupLoaderBlock->NetbootCardHardwareId) == 0) {
  962. ULONG BusNumber, DeviceNumber, FunctionNumber;
  963. //
  964. // PCI's encoding is this: fff ddddd
  965. // PXE's encoding is this: ddddd fff
  966. //
  967. BusNumber = (ULONG)((((PNET_CARD_INFO)setupLoaderBlock->NetbootCardInfo)->pci.BusDevFunc) >> 8);
  968. DeviceNumber = (ULONG)(((((PNET_CARD_INFO)setupLoaderBlock->NetbootCardInfo)->pci.BusDevFunc) & 0xf8) >> 3);
  969. FunctionNumber = (ULONG)(((((PNET_CARD_INFO)setupLoaderBlock->NetbootCardInfo)->pci.BusDevFunc) & 0x3));
  970. KdPrint(("IopIsRemoteBootCard: FOUND %ws\n", setupLoaderBlock->NetbootCardHardwareId));
  971. if ((ResourceRequirements->BusNumber != BusNumber) ||
  972. ((ResourceRequirements->SlotNumber & 0x1f) != DeviceNumber) ||
  973. (((ResourceRequirements->SlotNumber >> 5) & 0x3) != FunctionNumber)) {
  974. KdPrint(("IopIsRemoteBootCard: ignoring non-matching card:\n"));
  975. KdPrint((" devnode bus %d, busdevfunc bus %d\n",
  976. ResourceRequirements->BusNumber,
  977. BusNumber));
  978. KdPrint((" devnode slot %d (%d %d), busdevfunc slot %d (%d %d)\n",
  979. ResourceRequirements->SlotNumber,
  980. (ResourceRequirements->SlotNumber & 0x1f),
  981. ((ResourceRequirements->SlotNumber >> 5) & 0x3),
  982. (ULONG)(((PNET_CARD_INFO)setupLoaderBlock->NetbootCardInfo)->pci.BusDevFunc),
  983. DeviceNumber,
  984. FunctionNumber));
  985. return FALSE;
  986. } else {
  987. return TRUE;
  988. }
  989. }
  990. curHwId += (wcslen(curHwId) + 1);
  991. }
  992. return FALSE;
  993. }
  994. NTSTATUS
  995. IopSetupRemoteBootCard(
  996. IN PLOADER_PARAMETER_BLOCK LoaderBlock,
  997. IN HANDLE UniqueIdHandle,
  998. IN PUNICODE_STRING UnicodeDeviceInstance
  999. )
  1000. /*++
  1001. Routine Description:
  1002. This function modifies the registry to set up the netboot card.
  1003. We must do this here since the card is needed to boot, we can't
  1004. wait for the class installer to run.
  1005. THIS ASSUMES THAT IOREMOTEBOOTCLIENT IS TRUE.
  1006. Arguments:
  1007. LoaderBlock - Supplies a pointer to the loader parameter block that was
  1008. created by the OS Loader.
  1009. UniqueIdHandle - A handle to the device's unique node under the
  1010. Enum key.
  1011. UnicodeDeviceInstance - The device instance assigned to the device.
  1012. Return Value:
  1013. Status of the operation.
  1014. --*/
  1015. {
  1016. PSETUP_LOADER_BLOCK setupLoaderBlock;
  1017. UNICODE_STRING unicodeName, pnpInstanceId, keyName;
  1018. HANDLE tmpHandle;
  1019. HANDLE parametersHandle = NULL;
  1020. HANDLE currentControlSetHandle = NULL;
  1021. HANDLE remoteBootHandle = NULL;
  1022. HANDLE instanceHandle = NULL;
  1023. PWCHAR componentIdBuffer, curComponentIdLoc;
  1024. PCHAR registryList;
  1025. ULONG componentIdLength;
  1026. WCHAR tempNameBuffer[32];
  1027. WCHAR tempValueBuffer[128];
  1028. NTSTATUS status;
  1029. ULONG tmpValue, length;
  1030. PKEY_VALUE_PARTIAL_INFORMATION keyValue;
  1031. PKEY_VALUE_BASIC_INFORMATION keyValueBasic;
  1032. UCHAR dataBuffer[FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data) + 128];
  1033. ULONG enumerateIndex;
  1034. OBJECT_ATTRIBUTES objectAttributes;
  1035. ULONG disposition;
  1036. //
  1037. // If we already think we have initialized a remote boot card, then
  1038. // exit (should not really happen once we identify cards using the
  1039. // bus/slot.
  1040. //
  1041. if (IopRemoteBootCardInitialized) {
  1042. return STATUS_SUCCESS;
  1043. }
  1044. //
  1045. // setupLoaderBlock will always be non-NULL if we are
  1046. // remote booting, even if we are not in setup.
  1047. //
  1048. setupLoaderBlock = LoaderBlock->SetupLoaderBlock;
  1049. //
  1050. // Open the current control set.
  1051. //
  1052. status = IopOpenRegistryKey(&currentControlSetHandle,
  1053. NULL,
  1054. &CmRegistryMachineSystemCurrentControlSet,
  1055. KEY_ALL_ACCESS,
  1056. FALSE
  1057. );
  1058. if (!NT_SUCCESS(status)) {
  1059. goto cleanup;
  1060. }
  1061. //
  1062. // Open the Control\RemoteBoot key, which may not exist.
  1063. //
  1064. IopWstrToUnicodeString(&unicodeName, L"Control\\RemoteBoot");
  1065. InitializeObjectAttributes(&objectAttributes,
  1066. &unicodeName,
  1067. OBJ_CASE_INSENSITIVE,
  1068. currentControlSetHandle,
  1069. (PSECURITY_DESCRIPTOR)NULL
  1070. );
  1071. status = ZwCreateKey(&remoteBootHandle,
  1072. KEY_ALL_ACCESS,
  1073. &objectAttributes,
  1074. 0,
  1075. (PUNICODE_STRING)NULL,
  1076. 0,
  1077. &disposition
  1078. );
  1079. if (!NT_SUCCESS(status)) {
  1080. goto cleanup;
  1081. }
  1082. //
  1083. // Open the key where the netui code stores information about the cards.
  1084. // During textmode setup this will fail because the Control\Network
  1085. // key is not there. After that it should work, although we may need
  1086. // to create the last node in the path.
  1087. //
  1088. IopWstrToUnicodeString(&unicodeName, L"Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\{54C7D140-09EF-11D1-B25A-F5FE627ED95E}");
  1089. InitializeObjectAttributes(&objectAttributes,
  1090. &unicodeName,
  1091. OBJ_CASE_INSENSITIVE,
  1092. currentControlSetHandle,
  1093. (PSECURITY_DESCRIPTOR)NULL
  1094. );
  1095. status = ZwCreateKey(&instanceHandle,
  1096. KEY_ALL_ACCESS,
  1097. &objectAttributes,
  1098. 0,
  1099. (PUNICODE_STRING)NULL,
  1100. 0,
  1101. &disposition
  1102. );
  1103. if (NT_SUCCESS(status)) {
  1104. //
  1105. // If the PnpInstanceID of the first netboot card matches the one
  1106. // for this device node, and the NET_CARD_INFO that the loader
  1107. // found is the same as the one we saved, then this is the same
  1108. // card with the same instance ID as before, so we don't need to
  1109. // do anything.
  1110. //
  1111. IopWstrToUnicodeString(&unicodeName, L"PnPInstanceID");
  1112. keyValue = (PKEY_VALUE_PARTIAL_INFORMATION)dataBuffer;
  1113. RtlZeroMemory(dataBuffer, sizeof(dataBuffer));
  1114. status = ZwQueryValueKey(
  1115. instanceHandle,
  1116. &unicodeName,
  1117. KeyValuePartialInformation,
  1118. keyValue,
  1119. sizeof(dataBuffer),
  1120. &length);
  1121. //
  1122. // Check that it matches. We can init the string because we zeroed
  1123. // the dataBuffer before reading the key, so even if the
  1124. // registry value had no NULL at the end that is OK.
  1125. //
  1126. if ((NT_SUCCESS(status)) &&
  1127. (keyValue->Type == REG_SZ)) {
  1128. RtlInitUnicodeString(&pnpInstanceId, (PWSTR)(keyValue->Data));
  1129. if (RtlEqualUnicodeString(UnicodeDeviceInstance, &pnpInstanceId, TRUE)) {
  1130. //
  1131. // Instance ID matched, see if the NET_CARD_INFO matches.
  1132. //
  1133. IopWstrToUnicodeString(&unicodeName, L"NetCardInfo");
  1134. RtlZeroMemory(dataBuffer, sizeof(dataBuffer));
  1135. status = ZwQueryValueKey(
  1136. remoteBootHandle,
  1137. &unicodeName,
  1138. KeyValuePartialInformation,
  1139. keyValue,
  1140. sizeof(dataBuffer),
  1141. &length);
  1142. if ((NT_SUCCESS(status)) &&
  1143. (keyValue->Type == REG_BINARY) &&
  1144. (keyValue->DataLength == sizeof(NET_CARD_INFO)) &&
  1145. (memcmp(keyValue->Data, setupLoaderBlock->NetbootCardInfo, sizeof(NET_CARD_INFO)) == 0)) {
  1146. //
  1147. // Everything matched, so no need to do any setup.
  1148. //
  1149. status = STATUS_SUCCESS;
  1150. goto cleanup;
  1151. }
  1152. }
  1153. }
  1154. }
  1155. //
  1156. // We come through here if the saved registry data was missing or
  1157. // not correct. Write all the relevant values to the registry.
  1158. //
  1159. //
  1160. // Service name is in the loader block.
  1161. //
  1162. IopWstrToUnicodeString(&unicodeName, REGSTR_VALUE_SERVICE);
  1163. ZwSetValueKey(UniqueIdHandle,
  1164. &unicodeName,
  1165. TITLE_INDEX_VALUE,
  1166. REG_SZ,
  1167. setupLoaderBlock->NetbootCardServiceName,
  1168. (wcslen(setupLoaderBlock->NetbootCardServiceName) + 1) * sizeof(WCHAR)
  1169. );
  1170. //
  1171. // ClassGUID is the known net card GUID.
  1172. //
  1173. IopWstrToUnicodeString(&unicodeName, REGSTR_VALUE_CLASSGUID);
  1174. ZwSetValueKey(UniqueIdHandle,
  1175. &unicodeName,
  1176. TITLE_INDEX_VALUE,
  1177. REG_SZ,
  1178. L"{4D36E972-E325-11CE-BFC1-08002BE10318}",
  1179. sizeof(L"{4D36E972-E325-11CE-BFC1-08002BE10318}")
  1180. );
  1181. //
  1182. // Driver is the first net card.
  1183. //
  1184. IopWstrToUnicodeString(&unicodeName, REGSTR_VALUE_DRIVER);
  1185. ZwSetValueKey(UniqueIdHandle,
  1186. &unicodeName,
  1187. TITLE_INDEX_VALUE,
  1188. REG_SZ,
  1189. L"{4D36E972-E325-11CE-BFC1-08002BE10318}\\0000",
  1190. sizeof(L"{4D36E972-E325-11CE-BFC1-08002BE10318}\\0000")
  1191. );
  1192. //
  1193. // Open a handle for card parameters. We write RemoteBootCard plus
  1194. // whatever the BINL server told us to write.
  1195. //
  1196. status = IopOpenRegistryKey(&tmpHandle,
  1197. NULL,
  1198. &CmRegistryMachineSystemCurrentControlSetControlClass,
  1199. KEY_ALL_ACCESS,
  1200. FALSE
  1201. );
  1202. if (!NT_SUCCESS(status)) {
  1203. goto cleanup;
  1204. }
  1205. IopWstrToUnicodeString(&unicodeName, L"{4D36E972-E325-11CE-BFC1-08002BE10318}\\0000");
  1206. status = IopOpenRegistryKey(&parametersHandle,
  1207. tmpHandle,
  1208. &unicodeName,
  1209. KEY_ALL_ACCESS,
  1210. FALSE
  1211. );
  1212. ZwClose(tmpHandle);
  1213. if (!NT_SUCCESS(status)) {
  1214. goto cleanup;
  1215. }
  1216. //
  1217. // We know that this is a different NIC, so remove all the old parameters.
  1218. //
  1219. keyValueBasic = (PKEY_VALUE_BASIC_INFORMATION)dataBuffer;
  1220. enumerateIndex = 0;
  1221. while (TRUE) {
  1222. RtlZeroMemory(dataBuffer, sizeof(dataBuffer));
  1223. status = ZwEnumerateValueKey(
  1224. parametersHandle,
  1225. enumerateIndex,
  1226. KeyValueBasicInformation,
  1227. keyValueBasic,
  1228. sizeof(dataBuffer),
  1229. &length
  1230. );
  1231. if (status == STATUS_NO_MORE_ENTRIES) {
  1232. status = STATUS_SUCCESS;
  1233. break;
  1234. }
  1235. if (!NT_SUCCESS(status)) {
  1236. goto cleanup;
  1237. }
  1238. //
  1239. // We don't delete "NetCfgInstanceID", it won't change and
  1240. // its presence signifies to the net class installer that
  1241. // this is a replacement not a clean install.
  1242. //
  1243. if (_wcsicmp(keyValueBasic->Name, L"NetCfgInstanceID") != 0) {
  1244. RtlInitUnicodeString(&keyName, keyValueBasic->Name);
  1245. status = ZwDeleteValueKey(
  1246. parametersHandle,
  1247. &keyName
  1248. );
  1249. if (!NT_SUCCESS(status)) {
  1250. goto cleanup;
  1251. }
  1252. } else {
  1253. enumerateIndex = 1; // leave NetCfgInstanceID at index 0
  1254. }
  1255. }
  1256. //
  1257. // Write a parameter called RemoteBootCard set to TRUE, this
  1258. // is primarily so NDIS can recognize this as such.
  1259. //
  1260. IopWstrToUnicodeString(&unicodeName, L"RemoteBootCard");
  1261. tmpValue = 1;
  1262. ZwSetValueKey(parametersHandle,
  1263. &unicodeName,
  1264. TITLE_INDEX_VALUE,
  1265. REG_DWORD,
  1266. &tmpValue,
  1267. sizeof(tmpValue)
  1268. );
  1269. //
  1270. // Store any other parameters sent from the server.
  1271. //
  1272. registryList = setupLoaderBlock->NetbootCardRegistry;
  1273. if (registryList != NULL) {
  1274. STRING aString;
  1275. UNICODE_STRING uString, uString2;
  1276. //
  1277. // The registry list is a series of name\0type\0value\0, with
  1278. // a final \0 at the end. It is in ANSI, not UNICODE.
  1279. //
  1280. // All values are stored under parametersHandle. Type is 1 for
  1281. // DWORD and 2 for SZ.
  1282. //
  1283. uString.Buffer = tempNameBuffer;
  1284. uString.MaximumLength = sizeof(tempNameBuffer);
  1285. while (*registryList != '\0') {
  1286. //
  1287. // First the name.
  1288. //
  1289. RtlInitString(&aString, registryList);
  1290. RtlAnsiStringToUnicodeString(&uString, &aString, FALSE);
  1291. //
  1292. // Now the type.
  1293. //
  1294. registryList += (strlen(registryList) + 1);
  1295. if (*registryList == '1') {
  1296. //
  1297. // A DWORD, parse it.
  1298. //
  1299. registryList += 2; // skip "1\0"
  1300. tmpValue = 0;
  1301. while (*registryList != '\0') {
  1302. tmpValue = (tmpValue * 10) + (*registryList - '0');
  1303. ++registryList;
  1304. }
  1305. ZwSetValueKey(parametersHandle,
  1306. &uString,
  1307. TITLE_INDEX_VALUE,
  1308. REG_DWORD,
  1309. &tmpValue,
  1310. sizeof(tmpValue)
  1311. );
  1312. registryList += (strlen(registryList) + 1);
  1313. } else if (*registryList == '2') {
  1314. //
  1315. // An SZ, convert to Unicode.
  1316. //
  1317. registryList += 2; // skip "2\0"
  1318. uString2.Buffer = tempValueBuffer;
  1319. uString2.MaximumLength = sizeof(tempValueBuffer);
  1320. RtlInitAnsiString(&aString, registryList);
  1321. RtlAnsiStringToUnicodeString(&uString2, &aString, FALSE);
  1322. ZwSetValueKey(parametersHandle,
  1323. &uString,
  1324. TITLE_INDEX_VALUE,
  1325. REG_SZ,
  1326. uString2.Buffer,
  1327. uString2.Length + sizeof(WCHAR)
  1328. );
  1329. registryList += (strlen(registryList) + 1);
  1330. } else {
  1331. //
  1332. // Not "1" or "2", so stop processing registryList.
  1333. //
  1334. break;
  1335. }
  1336. }
  1337. }
  1338. //
  1339. // Save the NET_CARD_INFO so we can check it next time.
  1340. //
  1341. IopWstrToUnicodeString(&unicodeName, L"NetCardInfo");
  1342. ZwSetValueKey(remoteBootHandle,
  1343. &unicodeName,
  1344. TITLE_INDEX_VALUE,
  1345. REG_BINARY,
  1346. setupLoaderBlock->NetbootCardInfo,
  1347. sizeof(NET_CARD_INFO)
  1348. );
  1349. //
  1350. // Save the hardware ID, driver name, and service name,
  1351. // so the loader can read those if the server is down
  1352. // on subsequent boots.
  1353. //
  1354. IopWstrToUnicodeString(&unicodeName, L"HardwareId");
  1355. ZwSetValueKey(remoteBootHandle,
  1356. &unicodeName,
  1357. TITLE_INDEX_VALUE,
  1358. REG_SZ,
  1359. setupLoaderBlock->NetbootCardHardwareId,
  1360. (wcslen(setupLoaderBlock->NetbootCardHardwareId) + 1) * sizeof(WCHAR)
  1361. );
  1362. IopWstrToUnicodeString(&unicodeName, L"DriverName");
  1363. ZwSetValueKey(remoteBootHandle,
  1364. &unicodeName,
  1365. TITLE_INDEX_VALUE,
  1366. REG_SZ,
  1367. setupLoaderBlock->NetbootCardDriverName,
  1368. (wcslen(setupLoaderBlock->NetbootCardDriverName) + 1) * sizeof(WCHAR)
  1369. );
  1370. IopWstrToUnicodeString(&unicodeName, L"ServiceName");
  1371. ZwSetValueKey(remoteBootHandle,
  1372. &unicodeName,
  1373. TITLE_INDEX_VALUE,
  1374. REG_SZ,
  1375. setupLoaderBlock->NetbootCardServiceName,
  1376. (wcslen(setupLoaderBlock->NetbootCardServiceName) + 1) * sizeof(WCHAR)
  1377. );
  1378. //
  1379. // Save the device instance, in case we need to ID the card later.
  1380. //
  1381. IopWstrToUnicodeString(&unicodeName, L"DeviceInstance");
  1382. ZwSetValueKey(remoteBootHandle,
  1383. &unicodeName,
  1384. TITLE_INDEX_VALUE,
  1385. REG_SZ,
  1386. UnicodeDeviceInstance->Buffer,
  1387. UnicodeDeviceInstance->Length + sizeof(WCHAR)
  1388. );
  1389. //
  1390. // Make sure we only pick one card to setup this way!
  1391. //
  1392. IopRemoteBootCardInitialized = TRUE;
  1393. cleanup:
  1394. if (instanceHandle != NULL) {
  1395. ZwClose(instanceHandle);
  1396. }
  1397. if (remoteBootHandle != NULL) {
  1398. ZwClose(remoteBootHandle);
  1399. }
  1400. if (parametersHandle != NULL) {
  1401. ZwClose(parametersHandle);
  1402. }
  1403. if (currentControlSetHandle != NULL) {
  1404. ZwClose(currentControlSetHandle);
  1405. }
  1406. return status;
  1407. }
  1408. NTSTATUS
  1409. IopWriteIpAddressToRegistry(
  1410. HANDLE handle,
  1411. PWCHAR regkey,
  1412. PUCHAR value
  1413. )
  1414. {
  1415. NTSTATUS status;
  1416. UNICODE_STRING string;
  1417. CHAR addressA[16];
  1418. WCHAR addressW[16];
  1419. STRING addressStringA;
  1420. UNICODE_STRING addressStringW;
  1421. RtlInitUnicodeString( &string, regkey );
  1422. sprintf(addressA, "%d.%d.%d.%d",
  1423. value[0],
  1424. value[1],
  1425. value[2],
  1426. value[3]);
  1427. RtlInitAnsiString(&addressStringA, addressA);
  1428. addressStringW.Buffer = addressW;
  1429. addressStringW.MaximumLength = sizeof(addressW);
  1430. RtlAnsiStringToUnicodeString(&addressStringW, &addressStringA, FALSE);
  1431. status = NtSetValueKey(
  1432. handle,
  1433. &string,
  1434. 0,
  1435. REG_MULTI_SZ,
  1436. addressW,
  1437. addressStringW.Length + sizeof(WCHAR)
  1438. );
  1439. if ( !NT_SUCCESS(status) ) {
  1440. KdPrint(( "IopWriteIpAddressToRegistry: Unable to set %ws value: %x\n", regkey, status ));
  1441. }
  1442. return status;
  1443. }
  1444. NTSTATUS
  1445. IopSetDefaultGateway(
  1446. IN ULONG GatewayAddress
  1447. )
  1448. /*++
  1449. Routine Description:
  1450. This function adds a default gateway entry from the router table.
  1451. Arguments:
  1452. GatewayAddress - Address of the default gateway.
  1453. Return Value:
  1454. Error Code.
  1455. --*/
  1456. {
  1457. NTSTATUS Status;
  1458. HANDLE Handle = NULL;
  1459. BYTE Context[CONTEXT_SIZE];
  1460. TDIObjectID ID;
  1461. DWORD Size;
  1462. IPSNMPInfo IPStats;
  1463. IPAddrEntry *AddrTable = NULL;
  1464. DWORD NumReturned;
  1465. DWORD Type;
  1466. DWORD i;
  1467. DWORD MatchIndex;
  1468. IPRouteEntry RouteEntry;
  1469. OBJECT_ATTRIBUTES objectAttributes;
  1470. UNICODE_STRING NameString;
  1471. IO_STATUS_BLOCK ioStatusBlock;
  1472. if (GatewayAddress == 0) {
  1473. return STATUS_SUCCESS;
  1474. }
  1475. RtlInitUnicodeString( &NameString, DD_TCP_DEVICE_NAME );
  1476. InitializeObjectAttributes(
  1477. &objectAttributes,
  1478. &NameString,
  1479. OBJ_CASE_INSENSITIVE,
  1480. NULL,
  1481. NULL
  1482. );
  1483. Status = NtCreateFile(
  1484. &Handle,
  1485. GENERIC_READ | GENERIC_WRITE,
  1486. &objectAttributes,
  1487. &ioStatusBlock,
  1488. NULL,
  1489. 0,
  1490. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  1491. FILE_OPEN,
  1492. FILE_SYNCHRONOUS_IO_NONALERT,
  1493. NULL,
  1494. 0
  1495. );
  1496. if ( !NT_SUCCESS(Status) ) {
  1497. KdPrint(( "IopSetDefaultGateway: Unable to open TCPIP: %x\n", Status ));
  1498. return Status;
  1499. }
  1500. //
  1501. // Get the NetAddr info, to find an interface index for the gateway.
  1502. //
  1503. ID.toi_entity.tei_entity = CL_NL_ENTITY;
  1504. ID.toi_entity.tei_instance = 0;
  1505. ID.toi_class = INFO_CLASS_PROTOCOL;
  1506. ID.toi_type = INFO_TYPE_PROVIDER;
  1507. ID.toi_id = IP_MIB_STATS_ID;
  1508. Size = sizeof(IPStats);
  1509. memset(&IPStats, 0x0, Size);
  1510. memset(Context, 0x0, CONTEXT_SIZE);
  1511. Status = IopTCPQueryInformationEx(
  1512. Handle,
  1513. &ID,
  1514. &IPStats,
  1515. &Size,
  1516. Context);
  1517. if (!NT_SUCCESS(Status)) {
  1518. KdPrint(( "IopSetDefaultGateway: Unable to query TCPIP(1): %x\n", Status ));
  1519. goto Cleanup;
  1520. }
  1521. Size = IPStats.ipsi_numaddr * sizeof(IPAddrEntry);
  1522. AddrTable = ExAllocatePoolWithTag(PagedPool, Size, 'bRoI');
  1523. if (AddrTable == NULL) {
  1524. Status = STATUS_NO_MEMORY;
  1525. goto Cleanup;
  1526. }
  1527. ID.toi_id = IP_MIB_ADDRTABLE_ENTRY_ID;
  1528. memset(Context, 0x0, CONTEXT_SIZE);
  1529. Status = IopTCPQueryInformationEx(
  1530. Handle,
  1531. &ID,
  1532. AddrTable,
  1533. &Size,
  1534. Context);
  1535. if (!NT_SUCCESS(Status)) {
  1536. KdPrint(( "IopSetDefaultGateway: Unable to query TCPIP(2): %x\n", Status ));
  1537. goto Cleanup;
  1538. }
  1539. NumReturned = Size/sizeof(IPAddrEntry);
  1540. //
  1541. // We've got the address table. Loop through it. If we find an exact
  1542. // match for the gateway, then we're adding or deleting a direct route
  1543. // and we're done. Otherwise try to find a match on the subnet mask,
  1544. // and remember the first one we find.
  1545. //
  1546. Type = IRE_TYPE_INDIRECT;
  1547. for (i = 0, MatchIndex = 0xffff; i < NumReturned; i++) {
  1548. if( AddrTable[i].iae_addr == GatewayAddress ) {
  1549. //
  1550. // Found an exact match.
  1551. //
  1552. MatchIndex = i;
  1553. Type = IRE_TYPE_DIRECT;
  1554. break;
  1555. }
  1556. //
  1557. // The next hop is on the same subnet as this address. If
  1558. // we haven't already found a match, remember this one.
  1559. //
  1560. if ( (MatchIndex == 0xffff) &&
  1561. (AddrTable[i].iae_addr != 0) &&
  1562. (AddrTable[i].iae_mask != 0) &&
  1563. ((AddrTable[i].iae_addr & AddrTable[i].iae_mask) ==
  1564. (GatewayAddress & AddrTable[i].iae_mask)) ) {
  1565. MatchIndex = i;
  1566. }
  1567. }
  1568. //
  1569. // We've looked at all of the entries. See if we found a match.
  1570. //
  1571. if (MatchIndex == 0xffff) {
  1572. //
  1573. // Didn't find a match.
  1574. //
  1575. Status = STATUS_UNSUCCESSFUL;
  1576. KdPrint(( "IopSetDefaultGateway: Unable to find match for gateway\n" ));
  1577. goto Cleanup;
  1578. }
  1579. //
  1580. // We've found a match. Fill in the route entry, and call the
  1581. // Set API.
  1582. //
  1583. RouteEntry.ire_dest = DEFAULT_DEST;
  1584. RouteEntry.ire_index = AddrTable[MatchIndex].iae_index;
  1585. RouteEntry.ire_metric1 = DEFAULT_METRIC;
  1586. RouteEntry.ire_metric2 = (DWORD)(-1);
  1587. RouteEntry.ire_metric3 = (DWORD)(-1);
  1588. RouteEntry.ire_metric4 = (DWORD)(-1);
  1589. RouteEntry.ire_nexthop = GatewayAddress;
  1590. RouteEntry.ire_type = Type;
  1591. RouteEntry.ire_proto = IRE_PROTO_NETMGMT;
  1592. RouteEntry.ire_age = 0;
  1593. RouteEntry.ire_mask = DEFAULT_DEST_MASK;
  1594. RouteEntry.ire_metric5 = (DWORD)(-1);
  1595. RouteEntry.ire_context = 0;
  1596. Size = sizeof(RouteEntry);
  1597. ID.toi_id = IP_MIB_RTTABLE_ENTRY_ID;
  1598. Status = IopTCPSetInformationEx(
  1599. Handle,
  1600. &ID,
  1601. &RouteEntry,
  1602. Size );
  1603. if (!NT_SUCCESS(Status)) {
  1604. KdPrint(( "IopSetDefaultGateway: Unable to set default gateway: %x\n", Status ));
  1605. }
  1606. NtClose(Handle);
  1607. Handle = NULL;
  1608. Cleanup:
  1609. if (Handle != NULL) {
  1610. NtClose(Handle);
  1611. }
  1612. if( AddrTable != NULL ) {
  1613. ExFreePool( AddrTable );
  1614. }
  1615. return Status;
  1616. }
  1617. __inline long
  1618. htonl(long x)
  1619. {
  1620. return((((x) >> 24) & 0x000000FFL) |
  1621. (((x) >> 8) & 0x0000FF00L) |
  1622. (((x) << 8) & 0x00FF0000L) |
  1623. (((x) << 24) & 0xFF000000L));
  1624. }
  1625. NTSTATUS
  1626. IopCacheNetbiosNameForIpAddress(
  1627. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  1628. )
  1629. /*++
  1630. Routine Description:
  1631. This function takes an IP address, and submits it to NetBt for name resolution.
  1632. Arguments:
  1633. IpAddress - Address to resolve
  1634. Return Value:
  1635. Error Code.
  1636. --*/
  1637. {
  1638. NTSTATUS Status;
  1639. HANDLE Handle = NULL;
  1640. BYTE Context[CONTEXT_SIZE];
  1641. DWORD Size;
  1642. OBJECT_ATTRIBUTES objectAttributes;
  1643. UNICODE_STRING NameString;
  1644. IO_STATUS_BLOCK ioStatusBlock;
  1645. tREMOTE_CACHE cacheInfo;
  1646. PCHAR serverName;
  1647. PCHAR endOfServerName;
  1648. //
  1649. // Open NetBT.
  1650. //
  1651. RtlInitUnicodeString(
  1652. &NameString,
  1653. L"\\Device\\NetBT_Tcpip_{54C7D140-09EF-11D1-B25A-F5FE627ED95E}"
  1654. );
  1655. InitializeObjectAttributes(
  1656. &objectAttributes,
  1657. &NameString,
  1658. OBJ_CASE_INSENSITIVE,
  1659. NULL,
  1660. NULL
  1661. );
  1662. Status = NtCreateFile(
  1663. &Handle,
  1664. GENERIC_READ | GENERIC_WRITE,
  1665. &objectAttributes,
  1666. &ioStatusBlock,
  1667. NULL,
  1668. 0,
  1669. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  1670. FILE_OPEN,
  1671. FILE_SYNCHRONOUS_IO_NONALERT,
  1672. NULL,
  1673. 0
  1674. );
  1675. if ( !NT_SUCCESS(Status) ) {
  1676. KdPrint(( "IopCacheNetbiosNameForIpAddress: Unable to open NETBT: %x\n", Status ));
  1677. return Status;
  1678. }
  1679. //
  1680. // Get the server's name.
  1681. //
  1682. // If this is a remote boot setup boot, NtBootPathName is of the
  1683. // form \<server>\<share>\setup\<install-directory>\<platform>.
  1684. // If this is a normal remote boot, NtBootPathName is of the form
  1685. // \<server>\<share>\images\<machine>\winnt.
  1686. //
  1687. // Thus in either case, we need to isolate the first element of the
  1688. // path.
  1689. //
  1690. serverName = LoaderBlock->NtBootPathName;
  1691. if ( *serverName == '\\' ) {
  1692. serverName++;
  1693. }
  1694. endOfServerName = strchr( serverName, '\\' );
  1695. if ( endOfServerName == NULL ) {
  1696. endOfServerName = strchr( serverName, '\0' );
  1697. }
  1698. //
  1699. // Fill in the tREMOTE_CACHE structure.
  1700. //
  1701. memset(&cacheInfo, 0x0, sizeof(cacheInfo));
  1702. memset(cacheInfo.name, ' ', NETBIOS_NAMESIZE);
  1703. memcpy(cacheInfo.name, serverName, (ULONG)(endOfServerName - serverName));
  1704. cacheInfo.IpAddress = htonl(LoaderBlock->SetupLoaderBlock->ServerIpAddress);
  1705. cacheInfo.Ttl = MAXULONG;
  1706. //
  1707. // Submit the IOCTL.
  1708. //
  1709. Status = NtDeviceIoControlFile(
  1710. Handle,
  1711. NULL,
  1712. NULL,
  1713. NULL,
  1714. &ioStatusBlock,
  1715. IOCTL_NETBT_ADD_TO_REMOTE_TABLE,
  1716. &cacheInfo,
  1717. sizeof(cacheInfo),
  1718. Context,
  1719. sizeof(Context)
  1720. );
  1721. ASSERT( Status != STATUS_PENDING );
  1722. if ( NT_SUCCESS(Status) ) {
  1723. Status = ioStatusBlock.Status;
  1724. }
  1725. if ( !NT_SUCCESS(Status) ) {
  1726. KdPrint(( "IopCacheNetbiosNameForIpAddress: Adapter status failed: %x\n", Status ));
  1727. }
  1728. NtClose(Handle);
  1729. return Status;
  1730. }
  1731. NTSTATUS
  1732. IopTCPQueryInformationEx(
  1733. IN HANDLE TCPHandle,
  1734. IN TDIObjectID FAR *ID,
  1735. OUT void FAR *Buffer,
  1736. IN OUT DWORD FAR *BufferSize,
  1737. IN OUT BYTE FAR *Context
  1738. )
  1739. /*++
  1740. Routine Description:
  1741. This routine provides the interface to the TDI QueryInformationEx
  1742. facility of the TCP/IP stack on NT. Someday, this facility will be
  1743. part of TDI.
  1744. Arguments:
  1745. TCPHandle - Open handle to the TCP driver
  1746. ID - The TDI Object ID to query
  1747. Buffer - Data buffer to contain the query results
  1748. BufferSize - Pointer to the size of the results buffer. Filled in
  1749. with the amount of results data on return.
  1750. Context - Context value for the query. Should be zeroed for a
  1751. new query. It will be filled with context
  1752. information for linked enumeration queries.
  1753. Return Value:
  1754. An NTSTATUS value.
  1755. --*/
  1756. {
  1757. TCP_REQUEST_QUERY_INFORMATION_EX queryBuffer;
  1758. DWORD queryBufferSize;
  1759. NTSTATUS status;
  1760. IO_STATUS_BLOCK ioStatusBlock;
  1761. if (TCPHandle == NULL) {
  1762. return(STATUS_INVALID_PARAMETER);
  1763. }
  1764. queryBufferSize = sizeof(TCP_REQUEST_QUERY_INFORMATION_EX);
  1765. memcpy(&(queryBuffer.ID), ID, sizeof(TDIObjectID));
  1766. memcpy(&(queryBuffer.Context), Context, CONTEXT_SIZE);
  1767. status = NtDeviceIoControlFile(
  1768. TCPHandle, // Driver handle
  1769. NULL, // Event
  1770. NULL, // APC Routine
  1771. NULL, // APC context
  1772. &ioStatusBlock, // Status block
  1773. IOCTL_TCP_QUERY_INFORMATION_EX, // Control code
  1774. &queryBuffer, // Input buffer
  1775. queryBufferSize, // Input buffer size
  1776. Buffer, // Output buffer
  1777. *BufferSize // Output buffer size
  1778. );
  1779. ASSERT( status != STATUS_PENDING );
  1780. if ( NT_SUCCESS(status) ) {
  1781. status = ioStatusBlock.Status;
  1782. }
  1783. if (status == STATUS_SUCCESS) {
  1784. //
  1785. // Copy the return context to the caller's context buffer
  1786. //
  1787. memcpy(Context, &(queryBuffer.Context), CONTEXT_SIZE);
  1788. *BufferSize = (ULONG)ioStatusBlock.Information;
  1789. status = ioStatusBlock.Status;
  1790. } else {
  1791. *BufferSize = 0;
  1792. }
  1793. return(status);
  1794. }
  1795. NTSTATUS
  1796. IopTCPSetInformationEx(
  1797. IN HANDLE TCPHandle,
  1798. IN TDIObjectID FAR *ID,
  1799. IN void FAR *Buffer,
  1800. IN DWORD FAR BufferSize
  1801. )
  1802. /*++
  1803. Routine Description:
  1804. This routine provides the interface to the TDI SetInformationEx
  1805. facility of the TCP/IP stack on NT. Someday, this facility will be
  1806. part of TDI.
  1807. Arguments:
  1808. TCPHandle - Open handle to the TCP driver
  1809. ID - The TDI Object ID to set
  1810. Buffer - Data buffer containing the information to be set
  1811. BufferSize - The size of the set data buffer.
  1812. Return Value:
  1813. An NTSTATUS value.
  1814. --*/
  1815. {
  1816. PTCP_REQUEST_SET_INFORMATION_EX setBuffer;
  1817. NTSTATUS status;
  1818. IO_STATUS_BLOCK ioStatusBlock;
  1819. DWORD setBufferSize;
  1820. if (TCPHandle == NULL) {
  1821. return(STATUS_INVALID_PARAMETER);
  1822. }
  1823. setBufferSize = FIELD_OFFSET(TCP_REQUEST_SET_INFORMATION_EX, Buffer) + BufferSize;
  1824. setBuffer = ExAllocatePoolWithTag(PagedPool, setBufferSize, 'bRoI');
  1825. if (setBuffer == NULL) {
  1826. return(STATUS_INSUFFICIENT_RESOURCES);
  1827. }
  1828. setBuffer->BufferSize = BufferSize;
  1829. memcpy(&(setBuffer->ID), ID, sizeof(TDIObjectID));
  1830. memcpy(&(setBuffer->Buffer[0]), Buffer, BufferSize);
  1831. status = NtDeviceIoControlFile(
  1832. TCPHandle, // Driver handle
  1833. NULL, // Event
  1834. NULL, // APC Routine
  1835. NULL, // APC context
  1836. &ioStatusBlock, // Status block
  1837. IOCTL_TCP_SET_INFORMATION_EX, // Control code
  1838. setBuffer, // Input buffer
  1839. setBufferSize, // Input buffer size
  1840. NULL, // Output buffer
  1841. 0 // Output buffer size
  1842. );
  1843. ASSERT( status != STATUS_PENDING );
  1844. if ( NT_SUCCESS(status) ) {
  1845. status = ioStatusBlock.Status;
  1846. }
  1847. ExFreePool(setBuffer);
  1848. return(status);
  1849. }