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.

3146 lines
90 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. ramdisk.c
  5. Abstract:
  6. Provides the ARC emulation routines for I/O to a RAM disk device.
  7. Author:
  8. Chuck Lenzmeier (chuckl) 29-Apr-2001
  9. Revision History:
  10. Bassam Tabbara (bassamt) 06-Aug-2001 Added Ramdisk Building Support
  11. --*/
  12. #include "bootlib.h"
  13. #include "arccodes.h"
  14. #include "stdlib.h"
  15. #include "string.h"
  16. #if defined(_X86_)
  17. #include "bootx86.h"
  18. #endif
  19. #if defined(_IA64_)
  20. #include "bootia64.h"
  21. #endif
  22. #include "ramdisk.h"
  23. #include "netfs.h"
  24. #if defined(POST_XPSP)
  25. #include "bmbuild.h"
  26. #endif
  27. #include "ntexapi.h"
  28. #include "haldtect.h"
  29. #include "pci.h"
  30. #include "pbios.h"
  31. #include "bldr.h"
  32. #include <sdistructs.h>
  33. //
  34. // Debug helpers
  35. //
  36. #define ERR 0
  37. #define INFO 1
  38. #define VERBOSE 2
  39. #define PAINFUL 3
  40. #define DBGPRINT(lvl, _fmt_) if (RamdiskDebug && lvl <= RamdiskDebugLevel) DbgPrint _fmt_
  41. #define DBGLVL(x) (RamdiskDebug && RamdiskDebugLevel == x)
  42. BOOLEAN RamdiskDebug = TRUE;
  43. BOOLEAN RamdiskDebugLevel = INFO;
  44. BOOLEAN RamdiskBreak = FALSE;
  45. //
  46. // Macros
  47. //
  48. #define BL_INVALID_FILE_ID (ULONG)-1
  49. #define TEST_BIT(value, b) (((value) & (b)) == (b))
  50. #define PCI_ITERATOR_IS_VALID(i) (i & 0x8000)
  51. #define PCI_ITERATOR_TO_BUS(i) (UCHAR)(((i) >> 8) & 0x7f)
  52. #define PCI_ITERATOR_TO_DEVICE(i) (UCHAR)(((i) >> 3) & 0x1f)
  53. #define PCI_ITERATOR_TO_FUNCTION(i) (UCHAR)(((i) >> 0) & 0x7)
  54. #define PCI_TO_ITERATOR(b,d,f) ((USHORT)(0x8000 | ((b)<<8) | ((d)<<3) | (f)))
  55. //
  56. // PCI Device struct as persisted in registry by ntdetect.com
  57. //
  58. #include <pshpack1.h>
  59. typedef struct _PCIDEVICE {
  60. USHORT BusDevFunc;
  61. PCI_COMMON_CONFIG Config;
  62. } PCIDEVICE, *PPCIDEVICE;
  63. #include <poppack.h>
  64. //
  65. // Externs
  66. //
  67. extern PVOID InfFile;
  68. extern BOOLEAN GraphicsMode;
  69. extern BOOLEAN BlShowProgressBar;
  70. extern BOOLEAN BlOutputDots;
  71. extern BOOLEAN DisplayLogoOnBoot;
  72. //
  73. // Global Ramdisk options.
  74. // NOTE: All Ip addresses and ports are in network byte order.
  75. //
  76. BOOLEAN RamdiskBuild = FALSE;
  77. //
  78. // Used if downloading a ramdisk directly. RamdiskBuild = FALSE
  79. //
  80. PCHAR RamdiskPath = NULL;
  81. ULONG RamdiskTFTPAddr = 0; // network byte order
  82. ULONG RamdiskMTFTPAddr = 0; // network byte order
  83. USHORT RamdiskMTFTPCPort = 0; // network byte order
  84. USHORT RamdiskMTFTPSPort = 0; // network byte order
  85. USHORT RamdiskMTFTPTimeout = 5;
  86. USHORT RamdiskMTFTPDelay = 5;
  87. LONGLONG RamdiskMTFTPFileSize = 0;
  88. LONGLONG RamdiskMTFTPChunkSize = 0;
  89. //
  90. // Used if Building a ramdisk. RamdiskBuild = TRUE
  91. //
  92. #define RAMDISK_MAX_SERVERS 10
  93. #define RAMDISK_DISCOVERY_MULTICAST 0x00000001
  94. #define RAMDISK_DISCOVERY_BROADCAST 0x00000002
  95. #define RAMDISK_DISCOVERY_UNICAST 0x00000004
  96. #define RAMDISK_DISCOVERY_RESTRICT 0x00000008
  97. GUID RamdiskGuid = {0,0,0,0};
  98. ULONG RamdiskDiscovery = 0;
  99. ULONG RamdiskMCastAddr = 0; // network byte order
  100. ULONG RamdiskServerCount = 0;
  101. ULONG RamdiskServers[RAMDISK_MAX_SERVERS]; // network byte order
  102. USHORT RamdiskTimeout = 15;
  103. USHORT RamdiskRetry = 5;
  104. //
  105. // Globals
  106. //
  107. BOOLEAN RamdiskActive = FALSE;
  108. ULONG RamdiskBasePage = 0;
  109. LONGLONG RamdiskFileSize = 0;
  110. ULONG RamdiskFileSizeInPages = 0;
  111. ULONG RamdiskImageOffset = 0;
  112. LONGLONG RamdiskImageLength = 0;
  113. ULONG_PTR SdiAddress = 0;
  114. ULONG RamdiskMaxPacketSize = 0;
  115. ULONG RamdiskXID = 0;
  116. BL_DEVICE_ENTRY_TABLE RamdiskEntryTable =
  117. {
  118. (PARC_CLOSE_ROUTINE)RamdiskClose,
  119. (PARC_MOUNT_ROUTINE)RamdiskMount,
  120. (PARC_OPEN_ROUTINE)RamdiskOpen,
  121. (PARC_READ_ROUTINE)RamdiskRead,
  122. (PARC_READ_STATUS_ROUTINE)RamdiskReadStatus,
  123. (PARC_SEEK_ROUTINE)RamdiskSeek,
  124. (PARC_WRITE_ROUTINE)RamdiskWrite,
  125. (PARC_GET_FILE_INFO_ROUTINE)RamdiskGetFileInfo,
  126. (PARC_SET_FILE_INFO_ROUTINE)RamdiskSetFileInfo,
  127. (PRENAME_ROUTINE)RamdiskRename,
  128. (PARC_GET_DIRECTORY_ENTRY_ROUTINE)RamdiskGetDirectoryEntry,
  129. (PBOOTFS_INFO)NULL
  130. };
  131. //
  132. // forward decls
  133. //
  134. PVOID
  135. MapRamdisk (
  136. IN LONGLONG Offset,
  137. OUT PLONGLONG AvailableLength
  138. );
  139. ARC_STATUS
  140. RamdiskParseOptions (
  141. IN PCHAR LoadOptions
  142. );
  143. ARC_STATUS
  144. RamdiskInitializeFromPath(
  145. );
  146. ARC_STATUS
  147. RamdiskBuildAndInitialize(
  148. );
  149. VOID
  150. RamdiskFatalError(
  151. IN ULONG Message1,
  152. IN ULONG Message2
  153. );
  154. ARC_STATUS
  155. RamdiskInitialize(
  156. IN PCHAR LoadOptions,
  157. IN BOOLEAN SdiBoot
  158. )
  159. /*++
  160. Routine Description:
  161. This function will initiate the boot from a RAMDISK. Depending
  162. on the options passed in the the boot will either happen from
  163. a static RAMDISK (using the /RDPATH option) or from a dynamic
  164. RAMDISK (using the /RDBUILD option).
  165. Arguments:
  166. LoadOptions - boot.ini parameters
  167. SdiBoot - indicates whether this is an SDI boot. If it is, LoadOptions
  168. is ignored. The global variable SdiAddress gives the pointer to
  169. the SDI image.
  170. Return Value:
  171. none
  172. --*/
  173. {
  174. ARC_STATUS status;
  175. BOOLEAN OldOutputDots = FALSE;
  176. BOOLEAN OldShowProgressBar = FALSE;
  177. ULONG oldBase;
  178. ULONG oldLimit;
  179. //
  180. // Debug Break on entry
  181. //
  182. if (RamdiskBreak) {
  183. DbgBreakPoint();
  184. }
  185. //
  186. // If the ramdisk has already been initialized, just return. We know the
  187. // ramdisk has been initialized if SdiBoot is FALSE (implying that this is
  188. // NOT the call from BlStartup(), but the call from BlOsLoader()) and
  189. // RamdiskBasePage is not NULL (implying that we were previously called
  190. // from BlStartup() to initialize the SDI boot.
  191. //
  192. if ( !SdiBoot && (RamdiskBasePage != 0) ) {
  193. //
  194. // Now that ntdetect has been run, we can free up the pages that
  195. // we allocated earlier (see below).
  196. //
  197. BlFreeDescriptor( 0x10 );
  198. return ESUCCESS;
  199. }
  200. //
  201. // If this is an SDI boot, then we must have a pointer to the SDI image.
  202. //
  203. if ( SdiBoot && (SdiAddress == 0) ) {
  204. RamdiskFatalError( RAMDISK_GENERAL_FAILURE,
  205. RAMDISK_INVALID_OPTIONS );
  206. return EINVAL;
  207. }
  208. //
  209. // If this is not an SDI boot, parse all ramdisk options (if any).
  210. //
  211. if ( !SdiBoot ) {
  212. status = RamdiskParseOptions ( LoadOptions );
  213. if (status != ESUCCESS) {
  214. RamdiskFatalError( RAMDISK_GENERAL_FAILURE,
  215. RAMDISK_INVALID_OPTIONS );
  216. return status;
  217. }
  218. }
  219. #if defined(_IA64_)
  220. // Ramdisk boot path not supported on IA64 as of yet
  221. if ( RamdiskBuild ) {
  222. return ESUCCESS;
  223. }
  224. #endif
  225. //
  226. // Show the progress bar in text mode
  227. //
  228. if ( RamdiskBuild || RamdiskPath ) {
  229. // If booting from a ramdisk, graphics mode is off permanently
  230. DisplayLogoOnBoot = FALSE;
  231. GraphicsMode = FALSE;
  232. OldShowProgressBar = BlShowProgressBar;
  233. BlShowProgressBar = TRUE;
  234. OldOutputDots = BlOutputDots;
  235. BlOutputDots = TRUE;
  236. }
  237. #if defined(POST_XPSP)
  238. #if defined(i386)
  239. if ( RamdiskBuild ) {
  240. //
  241. // We will need to build the ramdisk first
  242. //
  243. ASSERT( RamdiskPath == NULL );
  244. status = RamdiskBuildAndInitialize();
  245. if (status != ESUCCESS) {
  246. RamdiskFatalError( RAMDISK_GENERAL_FAILURE,
  247. RAMDISK_BUILD_FAILURE );
  248. return status;
  249. }
  250. }
  251. #endif
  252. #endif
  253. if ( RamdiskPath ) {
  254. //
  255. // Initialize the Ramdisk from the RamdiskPath
  256. //
  257. status = RamdiskInitializeFromPath();
  258. if (status != ESUCCESS) {
  259. RamdiskFatalError( RAMDISK_GENERAL_FAILURE,
  260. RAMDISK_BOOT_FAILURE );
  261. return status;
  262. }
  263. } else if ( SdiBoot ) {
  264. //
  265. // This is an SDI boot. Find the ramdisk image within the SDI image
  266. // and allocate the pages in which the ramdisk image resides.
  267. //
  268. ULONG basePage;
  269. ULONG pageCount;
  270. PSDI_HEADER sdiHeader;
  271. ULONG i;
  272. ULONG_PTR ramdiskAddress;
  273. //
  274. // Temporarily allocate the pages that will be occupied by ntdetect
  275. // while it runs. BlDetectHardware() just assumes that these pages
  276. // are free for loading ntdetect. But we're going to allocate and map
  277. // the ramdisk image, which will result in the allocation of many
  278. // page table pages, some of which might end up in the place where
  279. // ntdetect will be loaded. So we allocate the ntdetect range here,
  280. // then free it later (see above).
  281. //
  282. basePage = 0x10;
  283. pageCount = 0x10;
  284. status = BlAllocateAlignedDescriptor(
  285. LoaderFirmwareTemporary,
  286. basePage,
  287. pageCount,
  288. 0,
  289. &basePage
  290. );
  291. //
  292. // Allocate the page that contains the SDI header. This will cause
  293. // it to be mapped, which will allow us to read the header to find
  294. // the ramdisk image.
  295. //
  296. oldBase = BlUsableBase;
  297. oldLimit = BlUsableLimit;
  298. BlUsableBase = BL_XIPROM_RANGE_LOW;
  299. BlUsableLimit = BL_XIPROM_RANGE_HIGH;
  300. basePage = (ULONG)(SdiAddress >> PAGE_SHIFT);
  301. pageCount = ADDRESS_AND_SIZE_TO_SPAN_PAGES( SdiAddress, sizeof(SDI_HEADER) );
  302. status = BlAllocateAlignedDescriptor(
  303. LoaderFirmwareTemporary,
  304. basePage,
  305. pageCount,
  306. 0,
  307. &basePage
  308. );
  309. BlUsableBase = oldBase;
  310. BlUsableLimit = oldLimit;
  311. //
  312. // Find the ramdisk image by looking through the TOC in the SDI header.
  313. //
  314. sdiHeader = (PSDI_HEADER)SdiAddress;
  315. for ( i = 0; i < SDI_TOCMAXENTRIES; i++ ) {
  316. if ( sdiHeader->ToC[i].dwType == SDI_BLOBTYPE_PART ) {
  317. break;
  318. }
  319. }
  320. if ( i >= SDI_TOCMAXENTRIES ) {
  321. RamdiskFatalError( RAMDISK_GENERAL_FAILURE,
  322. RAMDISK_BOOT_FAILURE );
  323. return ENOENT;
  324. }
  325. //
  326. // Calculate the starting address and page of the ramdisk image, the
  327. // length of the ramdisk image, and the offset within the starting page
  328. // to the image. The offset should be 0, because everything in the SDI
  329. // image should be page-aligned.
  330. //
  331. ramdiskAddress = (ULONG_PTR)(SdiAddress + sdiHeader->ToC[i].llOffset.QuadPart);
  332. RamdiskBasePage = (ULONG)(ramdiskAddress >> PAGE_SHIFT);
  333. RamdiskImageOffset = (ULONG)(ramdiskAddress - ((ULONG_PTR)RamdiskBasePage << PAGE_SHIFT));
  334. RamdiskImageLength = sdiHeader->ToC[i].llSize.QuadPart;
  335. RamdiskFileSizeInPages = ADDRESS_AND_SIZE_TO_SPAN_PAGES(
  336. ramdiskAddress,
  337. RamdiskImageLength
  338. );
  339. RamdiskFileSize = (LONGLONG)RamdiskFileSizeInPages << PAGE_SHIFT;
  340. //
  341. // Release the page(s) occupied by the SDI header.
  342. //
  343. BlFreeDescriptor( basePage );
  344. //
  345. // Tell the memory allocator about the pages occupied by the ramdisk
  346. // by allocating those pages.
  347. //
  348. oldBase = BlUsableBase;
  349. oldLimit = BlUsableLimit;
  350. BlUsableBase = BL_XIPROM_RANGE_LOW;
  351. BlUsableLimit = BL_XIPROM_RANGE_HIGH;
  352. basePage = RamdiskBasePage;
  353. pageCount = RamdiskFileSizeInPages;
  354. status = BlAllocateAlignedDescriptor(
  355. LoaderXIPRom,
  356. basePage,
  357. pageCount,
  358. 0,
  359. &basePage
  360. );
  361. BlUsableBase = oldBase;
  362. BlUsableLimit = oldLimit;
  363. ASSERT( status == ESUCCESS );
  364. ASSERT( basePage == RamdiskBasePage );
  365. DBGPRINT(VERBOSE, ("Ramdisk is active\n") );
  366. RamdiskActive = TRUE;
  367. }
  368. //
  369. // Restore old progress bar settings
  370. //
  371. if ( RamdiskBuild || RamdiskPath ) {
  372. BlShowProgressBar = OldShowProgressBar;
  373. BlOutputDots = OldOutputDots;
  374. BlClearScreen();
  375. }
  376. return ESUCCESS;
  377. }
  378. ARC_STATUS
  379. RamdiskReadImage(
  380. PCHAR RamdiskPath
  381. )
  382. /*++
  383. Routine Description:
  384. This function will load a ramdisk image from the network
  385. or another ARC boot device.
  386. Arguments:
  387. RamdiskPath - name of the file to load
  388. Return Value:
  389. status
  390. --*/
  391. {
  392. ARC_STATUS status;
  393. ULONG RamdiskDeviceId;
  394. ULONG RamdiskFileId = BL_INVALID_FILE_ID;
  395. PCHAR p;
  396. FILE_INFORMATION fileInformation;
  397. LARGE_INTEGER offset;
  398. LONGLONG remainingLength;
  399. ULONG oldBase;
  400. ULONG oldLimit;
  401. BOOLEAN retry = TRUE;
  402. ULONG lastProgressPercent = 0;
  403. BOOLEAN ForceDisplayFirstTime = TRUE; // force display initially
  404. ULONG currentProgressPercent;
  405. PUCHAR ip;
  406. PTCHAR FormatString = NULL;
  407. TCHAR Buffer[256];
  408. //
  409. // Show text progress bar
  410. //
  411. BlOutputStartupMsg(RAMDISK_DOWNLOAD);
  412. BlUpdateProgressBar(0);
  413. DBGPRINT(VERBOSE, ("RamdiskReadImage(%s)\n", RamdiskPath));
  414. //
  415. // Open the device that the RAM disk image is on.
  416. //
  417. p = strchr(RamdiskPath, '\\');
  418. if (p == NULL) {
  419. DBGPRINT(ERR, ("no \\ found in path\n"));
  420. return EINVAL;
  421. }
  422. *p = 0;
  423. try_again:
  424. status = ArcOpen(RamdiskPath, ArcOpenReadWrite, &RamdiskDeviceId);
  425. if (status != ESUCCESS) {
  426. DBGPRINT(ERR, ("ArcOpen(%s) failed: %d\n", RamdiskPath, status));
  427. if ( retry ) {
  428. retry = FALSE;
  429. _strlwr(RamdiskPath);
  430. goto try_again;
  431. }
  432. *p = '\\';
  433. return status;
  434. }
  435. *p++ = '\\';
  436. //
  437. // If the RAM disk image is on the network, use TftpGetPut to read it.
  438. // Otherwise, use normal I/O.
  439. //
  440. oldBase = BlUsableBase;
  441. oldLimit = BlUsableLimit;
  442. BlUsableBase = BL_XIPROM_RANGE_LOW;
  443. BlUsableLimit = BL_XIPROM_RANGE_HIGH;
  444. #ifdef EFI // multicast ramdisk download only supported on non-EFI machines for now
  445. if ( RamdiskDeviceId == NET_DEVICE_ID && RamdiskMTFTPAddr != 0 )
  446. {
  447. return EBADF;
  448. }
  449. #endif
  450. if ( RamdiskDeviceId == NET_DEVICE_ID && RamdiskMTFTPAddr == 0) {
  451. //
  452. // Network device using UNICAST download. We will use the TFTP
  453. // client implementation in TFTPLIB for the download.
  454. //
  455. TFTP_REQUEST request;
  456. NTSTATUS ntStatus;
  457. request.RemoteFileName = (PUCHAR)p;
  458. request.ServerIpAddress = RamdiskTFTPAddr;
  459. request.MemoryAddress = NULL;
  460. request.MaximumLength = 0;
  461. request.BytesTransferred = 0xbadf00d;
  462. request.Operation = TFTP_RRQ;
  463. request.MemoryType = LoaderXIPRom;
  464. #if defined(REMOTE_BOOT_SECURITY)
  465. request.SecurityHandle = TftpSecurityHandle;
  466. #endif // defined(REMOTE_BOOT_SECURITY)
  467. request.ShowProgress = TRUE;
  468. //
  469. // Print progress message
  470. //
  471. ip = (PUCHAR) &RamdiskTFTPAddr;
  472. FormatString = BlFindMessage( RAMDISK_DOWNLOAD_NETWORK );
  473. if ( FormatString != NULL ) {
  474. _stprintf(Buffer, FormatString, ip[0], ip[1], ip[2], ip[3] );
  475. BlOutputTrailerMsgStr( Buffer );
  476. }
  477. //
  478. // Download the image using TFTP
  479. //
  480. DBGPRINT(VERBOSE, ("calling TftpGetPut(%s,0x%x)\n", p, NetServerIpAddress));
  481. ntStatus = TftpGetPut( &request );
  482. DBGPRINT(VERBOSE, ("status from TftpGetPut 0x%x\n", ntStatus));
  483. BlUsableBase = oldBase;
  484. BlUsableLimit = oldLimit;
  485. if ( !NT_SUCCESS(ntStatus) ) {
  486. if ( request.MemoryAddress != NULL ) {
  487. BlFreeDescriptor( (ULONG)((ULONG_PTR)request.MemoryAddress >> PAGE_SHIFT ));
  488. }
  489. ArcClose( RamdiskDeviceId );
  490. if ( ntStatus == STATUS_INSUFFICIENT_RESOURCES ) {
  491. return ENOMEM;
  492. }
  493. return EROFS;
  494. }
  495. RamdiskBasePage = (ULONG)((ULONG_PTR)request.MemoryAddress >> PAGE_SHIFT);
  496. RamdiskFileSize = request.MaximumLength;
  497. RamdiskFileSizeInPages = (ULONG)BYTES_TO_PAGES(RamdiskFileSize);
  498. if ( (RamdiskImageLength == 0) ||
  499. (RamdiskImageLength > (RamdiskFileSize - RamdiskImageOffset)) ) {
  500. RamdiskImageLength = RamdiskFileSize - RamdiskImageOffset;
  501. }
  502. #ifndef EFI // multicast ramdisk download only supported on non-EFI machines for now
  503. } else if ( RamdiskDeviceId == NET_DEVICE_ID && RamdiskMTFTPAddr != 0) {
  504. LONGLONG FileOffset = 0;
  505. LONGLONG physicalAddressOfOffset;
  506. ULONG DownloadSize;
  507. USHORT ClientPort;
  508. USHORT ServerPort;
  509. ULONG iSession = 0;
  510. //
  511. // Network device and using multicast download. For multicast
  512. // downloads we will use the MTFTP implementation in the ROM.
  513. // A single MTFTP transfer is limited to 16-bit block counts.
  514. // This translates to ~32MB for 512 block sizes and ~90MB for
  515. // 1468 block sizes. In order to support larger files, we will
  516. // use multiple MTFTP sessions to bring the file down in chunks.
  517. // The MTFTP server will need to understand the chunking semantics.
  518. //
  519. //
  520. // Print progress message
  521. //
  522. ip = (PUCHAR) &RamdiskMTFTPAddr;
  523. FormatString = BlFindMessage( RAMDISK_DOWNLOAD_NETWORK_MCAST );
  524. if ( FormatString != NULL ) {
  525. _stprintf(Buffer, FormatString, ip[0], ip[1], ip[2], ip[3], SWAP_WORD( RamdiskMTFTPSPort ) );
  526. BlOutputTrailerMsgStr( Buffer );
  527. }
  528. //
  529. // Allocate the memory for the entire RAMDisk
  530. //
  531. RamdiskFileSize = RamdiskMTFTPFileSize;
  532. RamdiskFileSizeInPages = (ULONG)BYTES_TO_PAGES(RamdiskFileSize);
  533. if ( (RamdiskImageLength == 0) ||
  534. (RamdiskImageLength > (RamdiskFileSize - RamdiskImageOffset)) ) {
  535. RamdiskImageLength = RamdiskFileSize - RamdiskImageOffset;
  536. }
  537. DBGPRINT(INFO, ("Downloading Ramdisk using MTFTP. File Size=0x%I64x Chunk Size=0x%I64x\n", RamdiskFileSize, RamdiskMTFTPChunkSize ));
  538. status = BlAllocateAlignedDescriptor(
  539. LoaderXIPRom,
  540. 0,
  541. RamdiskFileSizeInPages,
  542. 0,
  543. &RamdiskBasePage
  544. );
  545. BlUsableBase = oldBase;
  546. BlUsableLimit = oldLimit;
  547. if (status != ESUCCESS) {
  548. DBGPRINT(ERR, ("BlAllocateAlignedDescriptor(%d pages) failed: %d\n", RamdiskFileSizeInPages, status));
  549. return status;
  550. }
  551. DBGPRINT(VERBOSE, ("Allocated %d pages at page %x for RAM disk\n", RamdiskFileSizeInPages, RamdiskBasePage ));
  552. //
  553. // Download the ramdisk file using MTFTP
  554. //
  555. if ( RamdiskMTFTPChunkSize == 0 ) {
  556. RamdiskMTFTPChunkSize = RamdiskMTFTPFileSize;
  557. }
  558. // starting client and server port (in Intel byte order to
  559. // allow increment operators to work )
  560. ClientPort = SWAP_WORD( RamdiskMTFTPCPort );
  561. ServerPort = SWAP_WORD( RamdiskMTFTPSPort );
  562. while ( FileOffset < RamdiskFileSize ) {
  563. //
  564. // Call the ROM implementation to download a single chunk
  565. //
  566. physicalAddressOfOffset = ((LONGLONG)RamdiskBasePage << PAGE_SHIFT) + FileOffset;
  567. ip = (PUCHAR)&RamdiskMTFTPAddr;
  568. DBGPRINT(INFO, ("MTFTP Session %d: %s from %u.%u.%u.%u sport=%d cport=%d offset=0x%I64x\n",
  569. iSession, p,
  570. ip[0], ip[1], ip[2], ip[3], ClientPort, ServerPort,
  571. physicalAddressOfOffset ));
  572. //
  573. // the high 32 bits are going to be lost when calling RomMtftpReadFile.
  574. // find out now, if this is happening
  575. //
  576. ASSERT( (physicalAddressOfOffset >> 32) == 0 );
  577. status = RomMtftpReadFile ( (PUCHAR)p,
  578. (PVOID)(ULONG)physicalAddressOfOffset,
  579. (ULONG)RamdiskMTFTPChunkSize,
  580. RamdiskTFTPAddr,
  581. RamdiskMTFTPAddr,
  582. SWAP_WORD( ClientPort ),
  583. SWAP_WORD( ServerPort ),
  584. RamdiskMTFTPTimeout,
  585. RamdiskMTFTPDelay,
  586. &DownloadSize );
  587. if ( status != ESUCCESS ) {
  588. DBGPRINT(ERR, ("RomMtftpReadFile failed %d\n", status ));
  589. BlFreeDescriptor( RamdiskBasePage );
  590. return status;
  591. }
  592. #if 1 || INTEL_MTFTP_SERVER_TEST
  593. p[strlen(p) - 1]++;
  594. RamdiskMTFTPAddr += 0x01000000;
  595. #else
  596. ClientPort++;
  597. ServerPort++;
  598. #endif
  599. FileOffset += DownloadSize;
  600. iSession++;
  601. // update progress bar
  602. currentProgressPercent = (ULONG)(((LONGLONG)FileOffset * 100) / RamdiskFileSize);
  603. if ( ForceDisplayFirstTime || (currentProgressPercent != lastProgressPercent) ) {
  604. BlUpdateProgressBar( currentProgressPercent );
  605. ForceDisplayFirstTime = FALSE;
  606. }
  607. lastProgressPercent = currentProgressPercent;
  608. }
  609. DBGPRINT(INFO, ("MTFTP Download complete. 0x%I64x bytes transferred using %d sessions\n", RamdiskFileSize, iSession));
  610. #endif
  611. } else {
  612. //
  613. // Open the RAM disk image.
  614. //
  615. status = BlOpen( RamdiskDeviceId, p, ArcOpenReadOnly, &RamdiskFileId );
  616. if (status != ESUCCESS) {
  617. DBGPRINT(ERR, ("BlOpen(%s) failed: %d\n", p, status));
  618. ArcClose( RamdiskDeviceId );
  619. return status;
  620. }
  621. //
  622. // Get the size of the RAM disk image.
  623. //
  624. status = BlGetFileInformation( RamdiskFileId, &fileInformation );
  625. if (status != ESUCCESS) {
  626. DBGPRINT(ERR, ("BlGetFileInformation(%s) failed: %d\n", p, status));
  627. BlClose( RamdiskFileId );
  628. ArcClose( RamdiskDeviceId );
  629. return status;
  630. }
  631. RamdiskFileSize = fileInformation.EndingAddress.QuadPart;
  632. RamdiskFileSizeInPages = (ULONG)BYTES_TO_PAGES(RamdiskFileSize);
  633. if ( (RamdiskImageLength == 0) ||
  634. (RamdiskImageLength > (RamdiskFileSize - RamdiskImageOffset)) ) {
  635. RamdiskImageLength = RamdiskFileSize - RamdiskImageOffset;
  636. }
  637. //
  638. // Allocate pages to hold the RAM disk image.
  639. //
  640. status = BlAllocateAlignedDescriptor(
  641. LoaderXIPRom,
  642. 0,
  643. RamdiskFileSizeInPages,
  644. 0,
  645. &RamdiskBasePage
  646. );
  647. BlUsableBase = oldBase;
  648. BlUsableLimit = oldLimit;
  649. if (status != ESUCCESS) {
  650. DBGPRINT(ERR, ("BlAllocateAlignedDescriptor(%d pages) failed: %d\n", RamdiskFileSizeInPages, status));
  651. BlClose( RamdiskFileId );
  652. ArcClose( RamdiskDeviceId );
  653. return status;
  654. }
  655. DBGPRINT(VERBOSE, ("Allocated %d pages at page %x for RAM disk\n", RamdiskFileSizeInPages, RamdiskBasePage ));
  656. //
  657. // Read the RAM disk image into memory.
  658. //
  659. #define MAX_DISK_READ (1024 * 1024)
  660. offset.QuadPart = 0;
  661. remainingLength = RamdiskFileSize;
  662. while ( offset.QuadPart < RamdiskFileSize ) {
  663. LONGLONG availableLength;
  664. ULONG readLength;
  665. PVOID va;
  666. ULONG count;
  667. va = MapRamdisk( offset.QuadPart, &availableLength );
  668. if ( remainingLength > availableLength ) {
  669. readLength = (ULONG)availableLength;
  670. } else {
  671. readLength = (ULONG)remainingLength;
  672. }
  673. if ( readLength > MAX_DISK_READ ) {
  674. readLength = MAX_DISK_READ;
  675. }
  676. status = BlSeek( RamdiskFileId, &offset, SeekAbsolute );
  677. if ( status != ESUCCESS ) {
  678. DBGPRINT(ERR, ("Unable to seek RAM disk image: %d\n", status));
  679. BlClose( RamdiskFileId );
  680. ArcClose( RamdiskDeviceId );
  681. return status;
  682. }
  683. status = BlRead( RamdiskFileId, va, readLength, &count );
  684. if ( (status != ESUCCESS) || (count != readLength) ) {
  685. DBGPRINT(ERR, ( "Unable to read RAM disk image: status %d count %x (wanted %x)\n", status, count, readLength) );
  686. BlClose( RamdiskFileId );
  687. ArcClose( RamdiskDeviceId );
  688. return status;
  689. }
  690. offset.QuadPart += readLength;
  691. remainingLength -= readLength;
  692. // update progress bar
  693. currentProgressPercent = (ULONG)(((LONGLONG)offset.QuadPart * 100) / RamdiskFileSize);
  694. if ( ForceDisplayFirstTime || (currentProgressPercent != lastProgressPercent) ) {
  695. BlUpdateProgressBar( currentProgressPercent );
  696. ForceDisplayFirstTime = FALSE;
  697. }
  698. lastProgressPercent = currentProgressPercent;
  699. }
  700. DBGPRINT(VERBOSE, ( "Done reading ramdisk\n" ) );
  701. BlClose( RamdiskFileId );
  702. RamdiskFileId = BL_INVALID_FILE_ID;
  703. }
  704. ArcClose( RamdiskDeviceId );
  705. return status;
  706. } // RamdiskReadImage
  707. ARC_STATUS
  708. RamdiskInitializeFromPath(
  709. )
  710. /*++
  711. Routine Description:
  712. This function will load a ramdisk image from the network
  713. or another ARC boot device.
  714. Arguments:
  715. none
  716. Return Value:
  717. status
  718. --*/
  719. {
  720. ARC_STATUS status;
  721. ASSERT( RamdiskPath );
  722. DBGPRINT(VERBOSE, ("RamdiskInitializeFromPath(%s)\n", RamdiskPath));
  723. status = RamdiskReadImage( RamdiskPath );
  724. if ( status == ESUCCESS ) {
  725. DBGPRINT(VERBOSE, ("Ramdisk is active\n") );
  726. RamdiskActive = TRUE;
  727. }
  728. return status;
  729. } // RamdiskInitializeFromPath
  730. ARC_STATUS
  731. RamdiskClose(
  732. IN ULONG FileId
  733. )
  734. /*++
  735. Routine Description:
  736. Closes the specified device
  737. Arguments:
  738. FileId - Supplies file id of the device to be closed
  739. Return Value:
  740. ESUCCESS - Device closed successfully
  741. !ESUCCESS - Device was not closed.
  742. --*/
  743. {
  744. if (BlFileTable[FileId].Flags.Open == 0) {
  745. #if DBG
  746. BlPrint(TEXT("ERROR - Unopened fileid %lx closed\r\n"),FileId);
  747. #endif
  748. }
  749. BlFileTable[FileId].Flags.Open = 0;
  750. return(ESUCCESS);
  751. }
  752. ARC_STATUS
  753. RamdiskOpen(
  754. IN PCHAR OpenPath,
  755. IN OPEN_MODE OpenMode,
  756. OUT PULONG FileId
  757. )
  758. /*++
  759. Routine Description:
  760. Opens a RAM disk for raw sector access.
  761. Arguments:
  762. OpenPath - Supplies a pointer to the name of the RAM disk.
  763. OpenMode - Supplies the mode of the open
  764. FileId - Supplies a pointer to a variable that specifies the file
  765. table entry that is filled in if the open is successful.
  766. Return Value:
  767. ESUCCESS is returned if the open operation is successful. Otherwise,
  768. an unsuccessful status is returned that describes the reason for failure.
  769. --*/
  770. {
  771. ULONG Key;
  772. PDRIVE_CONTEXT Context;
  773. UNREFERENCED_PARAMETER( OpenMode );
  774. //BlPrint(TEXT("RamdiskOpen entered\r\n"));
  775. if ( !RamdiskActive ) {
  776. //BlPrint(TEXT("RamdiskOpen: not active\r\n"));
  777. return EBADF;
  778. }
  779. if(FwGetPathMnemonicKey(OpenPath,"ramdisk",&Key)) {
  780. DBGPRINT(VERBOSE, ("RamdiskOpen: not a ramdisk path\n"));
  781. return EBADF;
  782. }
  783. if ( Key != 0 ) {
  784. DBGPRINT(ERR, ("RamdiskOpen: not ramdisk 0\n"));
  785. return EBADF;
  786. }
  787. //
  788. // Find an available FileId descriptor to open the device with
  789. //
  790. *FileId=2;
  791. while (BlFileTable[*FileId].Flags.Open != 0) {
  792. *FileId += 1;
  793. if(*FileId == BL_FILE_TABLE_SIZE) {
  794. DBGPRINT(ERR, ("RamdiskOpen: no file table entry available\n"));
  795. return(ENOENT);
  796. }
  797. }
  798. //
  799. // We found an entry we can use, so mark it as open.
  800. //
  801. BlFileTable[*FileId].Flags.Open = 1;
  802. BlFileTable[*FileId].DeviceEntryTable = &RamdiskEntryTable;
  803. Context = &(BlFileTable[*FileId].u.DriveContext);
  804. Context->Drive = (UCHAR)Key;
  805. Context->xInt13 = TRUE;
  806. DBGPRINT(VERBOSE, ("RamdiskOpen: exit success\n"));
  807. return(ESUCCESS);
  808. }
  809. ARC_STATUS
  810. RamdiskSeek (
  811. IN ULONG FileId,
  812. IN PLARGE_INTEGER Offset,
  813. IN SEEK_MODE SeekMode
  814. )
  815. /*++
  816. Routine Description:
  817. Changes the current offset of the file specified by FileId
  818. Arguments:
  819. FileId - specifies the file on which the current offset is to
  820. be changed.
  821. Offset - New offset into file.
  822. SeekMode - Either SeekAbsolute or SeekRelative
  823. SeekEndRelative is not supported
  824. Return Value:
  825. ESUCCESS - Operation completed succesfully
  826. EBADF - Operation did not complete successfully.
  827. --*/
  828. {
  829. switch (SeekMode) {
  830. case SeekAbsolute:
  831. BlFileTable[FileId].Position = *Offset;
  832. break;
  833. case SeekRelative:
  834. BlFileTable[FileId].Position.QuadPart += Offset->QuadPart;
  835. break;
  836. default:
  837. #if DBG
  838. BlPrint(TEXT("SeekMode %lx not supported\r\n"),SeekMode);
  839. #endif
  840. return(EACCES);
  841. }
  842. return(ESUCCESS);
  843. }
  844. ARC_STATUS
  845. RamdiskWrite(
  846. IN ULONG FileId,
  847. OUT PVOID Buffer,
  848. IN ULONG Length,
  849. OUT PULONG Count
  850. )
  851. /*++
  852. Routine Description:
  853. Writes sectors directly to an open RAM disk.
  854. Arguments:
  855. FileId - Supplies the file to write to
  856. Buffer - Supplies buffer with data to write
  857. Length - Supplies number of bytes to write
  858. Count - Returns actual bytes written
  859. Return Value:
  860. ESUCCESS - write completed successfully
  861. !ESUCCESS - write failed
  862. --*/
  863. {
  864. PUCHAR buffer;
  865. LONGLONG offset;
  866. ULONG remainingLength;
  867. LONGLONG availableLength;
  868. ULONG bytesWritten;
  869. ULONG bytesThisPage;
  870. PVOID va;
  871. DBGPRINT(ERR, ("RamdiskWrite entered\n"));
  872. //DbgBreakPoint();
  873. buffer = Buffer;
  874. offset = BlFileTable[FileId].Position.QuadPart;
  875. remainingLength = Length;
  876. if ( offset >= RamdiskImageLength ) {
  877. return EINVAL;
  878. }
  879. if ( remainingLength > (RamdiskImageLength - offset) ) {
  880. remainingLength = (ULONG)(RamdiskImageLength - offset);
  881. }
  882. bytesWritten = 0;
  883. while ( remainingLength != 0 ) {
  884. va = MapRamdisk( RamdiskImageOffset + offset, &availableLength );
  885. bytesThisPage = remainingLength;
  886. if ( remainingLength > availableLength ) {
  887. bytesThisPage = (ULONG)availableLength;
  888. }
  889. memcpy( va, buffer, bytesThisPage );
  890. offset += bytesThisPage;
  891. buffer += bytesThisPage;
  892. remainingLength -= bytesThisPage;
  893. bytesWritten += bytesThisPage;
  894. }
  895. BlFileTable[FileId].Position.QuadPart += bytesWritten;
  896. *Count = bytesWritten;
  897. return ESUCCESS;
  898. }
  899. ARC_STATUS
  900. RamdiskRead(
  901. IN ULONG FileId,
  902. OUT PVOID Buffer,
  903. IN ULONG Length,
  904. OUT PULONG Count
  905. )
  906. /*++
  907. Routine Description:
  908. Reads sectors directly from an open RAM disk.
  909. Arguments:
  910. FileId - Supplies the file to read from
  911. Buffer - Supplies buffer to read into
  912. Length - Supplies number of bytes to read
  913. Count - Returns actual bytes read
  914. Return Value:
  915. ESUCCESS - read completed successfully
  916. !ESUCCESS - read failed
  917. --*/
  918. {
  919. PUCHAR buffer;
  920. LONGLONG offset;
  921. ULONG remainingLength;
  922. LONGLONG availableLength;
  923. ULONG bytesRead;
  924. ULONG bytesThisPage;
  925. PVOID va;
  926. buffer = Buffer;
  927. offset = BlFileTable[FileId].Position.QuadPart;
  928. DBGPRINT(VERBOSE, ( "RamdiskRead: offset %x, length %x, buffer %p\n", (ULONG)offset, Length, buffer ));
  929. remainingLength = Length;
  930. if ( offset >= RamdiskImageLength ) {
  931. DBGPRINT(ERR, ( "RamdiskRead: read beyond EOF\n" ) );
  932. return EINVAL;
  933. }
  934. if ( remainingLength > (RamdiskImageLength - offset) ) {
  935. remainingLength = (ULONG)(RamdiskImageLength - offset);
  936. }
  937. bytesRead = 0;
  938. while ( remainingLength != 0 ) {
  939. va = MapRamdisk( RamdiskImageOffset + offset, &availableLength );
  940. DBGPRINT(VERBOSE, ( "Mapped offset %x, va %p, availableLength %x\n", (ULONG)offset, va, availableLength ) );
  941. bytesThisPage = remainingLength;
  942. if ( remainingLength > availableLength ) {
  943. bytesThisPage = (ULONG)availableLength;
  944. }
  945. memcpy( buffer, va, bytesThisPage );
  946. offset += bytesThisPage;
  947. buffer += bytesThisPage;
  948. remainingLength -= bytesThisPage;
  949. bytesRead += bytesThisPage;
  950. }
  951. BlFileTable[FileId].Position.QuadPart += bytesRead;
  952. *Count = bytesRead;
  953. return ESUCCESS;
  954. }
  955. ARC_STATUS
  956. RamdiskGetFileInfo(
  957. IN ULONG FileId,
  958. OUT PFILE_INFORMATION Finfo
  959. )
  960. /*++
  961. Routine Description:
  962. Returns file information about a RAMDISK file.
  963. Arguments:
  964. FileId - id of the file
  965. Finfo - file information structure to be filled in
  966. Return Value:
  967. ESUCCESS - write completed successfully
  968. !ESUCCESS - write failed
  969. --*/
  970. {
  971. RtlZeroMemory(Finfo, sizeof(FILE_INFORMATION));
  972. Finfo->EndingAddress.QuadPart = RamdiskImageLength;
  973. Finfo->CurrentPosition.QuadPart = BlFileTable[FileId].Position.QuadPart;
  974. Finfo->Type = DiskPeripheral;
  975. return ESUCCESS;
  976. }
  977. ARC_STATUS
  978. RamdiskMount(
  979. IN CHAR * FIRMWARE_PTR MountPath,
  980. IN MOUNT_OPERATION Operation
  981. )
  982. {
  983. UNREFERENCED_PARAMETER( MountPath );
  984. UNREFERENCED_PARAMETER( Operation );
  985. DBGPRINT(VERBOSE, ( "RamdiskMount called\n" ));
  986. return EINVAL;
  987. }
  988. ARC_STATUS
  989. RamdiskReadStatus(
  990. IN ULONG FileId
  991. )
  992. {
  993. UNREFERENCED_PARAMETER( FileId );
  994. DBGPRINT(VERBOSE, ( "RamdiskReadStatus called\n" ) );
  995. return EINVAL;
  996. }
  997. ARC_STATUS
  998. RamdiskSetFileInfo (
  999. IN ULONG FileId,
  1000. IN ULONG AttributeFlags,
  1001. IN ULONG AttributeMask
  1002. )
  1003. {
  1004. UNREFERENCED_PARAMETER( FileId );
  1005. UNREFERENCED_PARAMETER( AttributeFlags );
  1006. UNREFERENCED_PARAMETER( AttributeMask );
  1007. DBGPRINT(VERBOSE, ( "RamdiskSetFileInfo called\n" ));
  1008. return EINVAL;
  1009. }
  1010. ARC_STATUS
  1011. RamdiskRename (
  1012. IN ULONG FileId,
  1013. IN CHAR * FIRMWARE_PTR NewName
  1014. )
  1015. {
  1016. UNREFERENCED_PARAMETER( FileId );
  1017. UNREFERENCED_PARAMETER( NewName );
  1018. DBGPRINT(VERBOSE, ( "RamdiskRename called\n" ));
  1019. return EINVAL;
  1020. }
  1021. ARC_STATUS
  1022. RamdiskGetDirectoryEntry (
  1023. IN ULONG FileId,
  1024. OUT PDIRECTORY_ENTRY Buffer,
  1025. IN ULONG Length,
  1026. OUT ULONG * FIRMWARE_PTR Count
  1027. )
  1028. {
  1029. UNREFERENCED_PARAMETER( FileId );
  1030. UNREFERENCED_PARAMETER( Buffer );
  1031. UNREFERENCED_PARAMETER( Length );
  1032. UNREFERENCED_PARAMETER( Count );
  1033. DBGPRINT(VERBOSE, ( "RamdiskGetDirectoryEntry called\n" ));
  1034. return EINVAL;
  1035. }
  1036. PVOID
  1037. MapRamdisk (
  1038. LONGLONG Offset,
  1039. PLONGLONG AvailableLength
  1040. )
  1041. {
  1042. LONGLONG physicalAddressOfOffset;
  1043. physicalAddressOfOffset = ((LONGLONG)RamdiskBasePage << PAGE_SHIFT) + Offset;
  1044. *AvailableLength = RamdiskFileSize - Offset;
  1045. #if defined(_X86_)
  1046. //
  1047. // the high 32 bits of physicalAddressOfOffset are
  1048. // going to be lost when returning the address as a pvoid.
  1049. // find out if this is happening now.
  1050. //
  1051. ASSERT( (physicalAddressOfOffset >> 32) == 0 );
  1052. return (PVOID)(ULONG)physicalAddressOfOffset;
  1053. #else
  1054. return (PVOID)physicalAddressOfOffset;
  1055. #endif
  1056. }
  1057. PCHAR
  1058. RamdiskGetOptionValue(
  1059. IN PCHAR LoadOptions,
  1060. IN PCHAR OptionName
  1061. )
  1062. /*++
  1063. Routine Description:
  1064. Parse the load options string returning a value of one of the
  1065. options.
  1066. Format supported: /OPTIONNAME=VALUE
  1067. Note there is no space before or after the '='.
  1068. Value is terminated with a '\r','\n',' ','/', or '\t'
  1069. Arguments:
  1070. LoadOptions - Loader options from boot.ini. Must be all caps.
  1071. OptionName - Name of the option to find.
  1072. Return Value:
  1073. Pointer to a value string that has been allocated with
  1074. BlAllocateHeap or NULL if the option has not found.
  1075. --*/
  1076. {
  1077. PCHAR retValue = NULL;
  1078. PCHAR value;
  1079. PCHAR p;
  1080. ULONG n;
  1081. ASSERT( LoadOptions );
  1082. ASSERT( OptionName );
  1083. if ( (p = strstr( LoadOptions, OptionName )) != 0 ) {
  1084. value = strchr( p , '=' );
  1085. if (value) {
  1086. value++;
  1087. for (p = value; *p; p++) {
  1088. if (*p == ' ') break;
  1089. if (*p == '/') break;
  1090. if (*p == '\n') break;
  1091. if (*p == '\r') break;
  1092. if (*p == '\t') break;
  1093. }
  1094. n = (ULONG)(p - value);
  1095. retValue = (PCHAR)BlAllocateHeap( n+1 );
  1096. if ( retValue ) {
  1097. strncpy( retValue, value, n );
  1098. }
  1099. }
  1100. }
  1101. return retValue;
  1102. }
  1103. ULONG
  1104. RamdiskParseIPAddr(
  1105. IN PCHAR psz
  1106. )
  1107. /*++
  1108. Routine Description:
  1109. parses an ip address from a string
  1110. Arguments: [psz] - Ip address string
  1111. Returns: ipaddress (in network byte order) or 0.
  1112. --*/
  1113. {
  1114. ULONG nAddr = 0;
  1115. ULONG nDigit = 0;
  1116. ULONG cDigits = 0;
  1117. for (; (psz!= NULL && *psz != 0); psz++) {
  1118. if (*psz >= '0' && *psz <= '9') {
  1119. nDigit = nDigit * 10 + *psz - '0';
  1120. if ( nDigit > 255 ) {
  1121. return 0;
  1122. }
  1123. }
  1124. else if (*psz == '.') {
  1125. nAddr = (nAddr << 8) | nDigit;
  1126. nDigit = 0;
  1127. cDigits++;
  1128. } else {
  1129. break;
  1130. }
  1131. }
  1132. if (cDigits != 3) {
  1133. return 0;
  1134. }
  1135. nAddr = (nAddr << 8) | nDigit;
  1136. return SWAP_DWORD( nAddr );
  1137. }
  1138. BOOLEAN
  1139. RamdiskHexStringToDword(
  1140. IN PCHAR psz,
  1141. OUT PULONG RetValue,
  1142. IN USHORT cDigits,
  1143. IN CHAR chDelim
  1144. )
  1145. /*++
  1146. Routine Description:
  1147. scan psz for a number of hex digits (at most 8); update psz
  1148. return value in Value; check for chDelim;
  1149. Arguments: [psz] - the hex string to convert
  1150. [Value] - the returned value
  1151. [cDigits] - count of digits
  1152. Returns: TRUE for success
  1153. --*/
  1154. {
  1155. USHORT Count;
  1156. ULONG Value;
  1157. Value = 0;
  1158. for (Count = 0; Count < cDigits; Count++, psz++)
  1159. {
  1160. if (*psz >= '0' && *psz <= '9') {
  1161. Value = (Value << 4) + *psz - '0';
  1162. } else if (*psz >= 'A' && *psz <= 'F') {
  1163. Value = (Value << 4) + *psz - 'A' + 10;
  1164. } else if (*psz >= 'a' && *psz <= 'f') {
  1165. Value = (Value << 4) + *psz - 'a' + 10;
  1166. } else {
  1167. return(FALSE);
  1168. }
  1169. }
  1170. *RetValue = Value;
  1171. if (chDelim != 0) {
  1172. return *psz++ == chDelim;
  1173. } else {
  1174. return TRUE;
  1175. }
  1176. }
  1177. BOOLEAN
  1178. RamdiskUUIDFromString(
  1179. IN PCHAR psz,
  1180. OUT LPGUID pguid
  1181. )
  1182. /**
  1183. Routine Description:
  1184. Parse UUID such as 00000000-0000-0000-0000-000000000000
  1185. Arguments:
  1186. [psz] - Supplies the UUID string to convert
  1187. [pguid] - Returns the GUID.
  1188. Returns: TRUE if successful
  1189. **/
  1190. {
  1191. ULONG dw;
  1192. if (!RamdiskHexStringToDword(psz, &pguid->Data1, sizeof(ULONG)*2, '-')) {
  1193. return FALSE;
  1194. }
  1195. psz += sizeof(ULONG)*2 + 1;
  1196. if (!RamdiskHexStringToDword(psz, &dw, sizeof(USHORT)*2, '-')) {
  1197. return FALSE;
  1198. }
  1199. psz += sizeof(USHORT)*2 + 1;
  1200. pguid->Data2 = (USHORT)dw;
  1201. if (!RamdiskHexStringToDword(psz, &dw, sizeof(USHORT)*2, '-')) {
  1202. return FALSE;
  1203. }
  1204. psz += sizeof(USHORT)*2 + 1;
  1205. pguid->Data3 = (USHORT)dw;
  1206. if (!RamdiskHexStringToDword(psz, &dw, sizeof(UCHAR)*2, 0)) {
  1207. return FALSE;
  1208. }
  1209. psz += sizeof(UCHAR)*2;
  1210. pguid->Data4[0] = (UCHAR)dw;
  1211. if (!RamdiskHexStringToDword(psz, &dw, sizeof(UCHAR)*2, '-')) {
  1212. return FALSE;
  1213. }
  1214. psz += sizeof(UCHAR)*2+1;
  1215. pguid->Data4[1] = (UCHAR)dw;
  1216. if (!RamdiskHexStringToDword(psz, &dw, sizeof(UCHAR)*2, 0)) {
  1217. return FALSE;
  1218. }
  1219. psz += sizeof(UCHAR)*2;
  1220. pguid->Data4[2] = (UCHAR)dw;
  1221. if (!RamdiskHexStringToDword(psz, &dw, sizeof(UCHAR)*2, 0)) {
  1222. return FALSE;
  1223. }
  1224. psz += sizeof(UCHAR)*2;
  1225. pguid->Data4[3] = (UCHAR)dw;
  1226. if (!RamdiskHexStringToDword(psz, &dw, sizeof(UCHAR)*2, 0)) {
  1227. return FALSE;
  1228. }
  1229. psz += sizeof(UCHAR)*2;
  1230. pguid->Data4[4] = (UCHAR)dw;
  1231. if (!RamdiskHexStringToDword(psz, &dw, sizeof(UCHAR)*2, 0)) {
  1232. return FALSE;
  1233. }
  1234. psz += sizeof(UCHAR)*2;
  1235. pguid->Data4[5] = (UCHAR)dw;
  1236. if (!RamdiskHexStringToDword(psz, &dw, sizeof(UCHAR)*2, 0)) {
  1237. return FALSE;
  1238. }
  1239. psz += sizeof(UCHAR)*2;
  1240. pguid->Data4[6] = (UCHAR)dw;
  1241. if (!RamdiskHexStringToDword(psz, &dw, sizeof(UCHAR)*2, 0)) {
  1242. return FALSE;
  1243. }
  1244. psz += sizeof(UCHAR)*2;
  1245. pguid->Data4[7] = (UCHAR)dw;
  1246. return TRUE;
  1247. }
  1248. BOOLEAN
  1249. RamdiskGUIDFromString(
  1250. IN PCHAR psz,
  1251. OUT LPGUID pguid
  1252. )
  1253. /**
  1254. Routine Description:
  1255. Parse GUID such as {00000000-0000-0000-0000-000000000000}
  1256. Arguments:
  1257. [psz] - Supplies the UUID string to convert
  1258. [pguid] - Returns the GUID.
  1259. Returns: TRUE if successful
  1260. **/
  1261. {
  1262. if (*psz == '{' ) {
  1263. psz++;
  1264. }
  1265. if (RamdiskUUIDFromString(psz, pguid) != TRUE) {
  1266. return FALSE;
  1267. }
  1268. psz += 36;
  1269. if (*psz == '}' ) {
  1270. psz++;
  1271. }
  1272. if (*psz != '\0') {
  1273. return FALSE;
  1274. }
  1275. return TRUE;
  1276. }
  1277. ARC_STATUS
  1278. RamdiskParseOptions (
  1279. IN PCHAR LoadOptions
  1280. )
  1281. /*++
  1282. Routine Description:
  1283. Parses all the Ramdisk params from the boot.ini option string.
  1284. Arguments:
  1285. LoadOptions - Loader options from boot.ini. Must be all caps.
  1286. /RDPATH - Indicates that the boot ramdisk should be downloaded
  1287. from the specified path. This option takes
  1288. precedence over RDBUILD.
  1289. Example: /RDPATH=net(0)\boot\ramdisk.dat
  1290. /RDMTFTPADDR - Specifies the Multicast Address where the ramdisk
  1291. image should be downloaded from. If not specified
  1292. a unicast download from the PXE boot server will
  1293. be performed.
  1294. /RDMTFTPCPORT - Specifies the Multicast Client port to use.
  1295. /RDMTFTPSPORT - Specifies the Multicast Server port to use.
  1296. /RDMTFTPDELAY - Specifies the delay before starting a new MTFTP session.
  1297. /RDMTFTPTIMEOUT - Specifies the timeout before restarting a MTFTP session.
  1298. /RDIMAGEOFFSET - Specifies the offset into the downloaded file at which the
  1299. actual disk image begins. If not specified, 0 is used.
  1300. /RDIMAGELENGTH - Specifies the length of the actual disk image. If not
  1301. specified, the size of the downloaded file minus the offset
  1302. to the image (RDIMAGEOFFSET) is used.
  1303. /RDFILESIZE - Specifies the size of the file to be downloaded.
  1304. /RDCHUNKSIZE - Specifies the size of each file chunck when more than
  1305. one MTFTP session is required to download a large file. If the
  1306. file is to be downloaded with one chunk this option is omitted
  1307. or is set to zero.
  1308. This is used to workaround a size limitation in the MTFTP
  1309. protcol. MTFTP currently has 16-bit block counts, therefore
  1310. when using 512 byte blocks we are limited to ~32MB files.
  1311. Example 1: assume we want to download a 85MB file
  1312. using 512 byte TFTP block sizes.
  1313. /RDMTFTPADDR=224.1.1.1 /RDMTFTPCPORT=100 /RDMTFTPSPORT=200
  1314. /RDCHUNKSIZE=31457280 /RDFILESIZE=89128960
  1315. 1st MTFTP session on CPort=100, SPort=200 Size=31457280 (30MB)
  1316. 2nd MTFTP session on CPort=101, SPort=201 Size=31457280 (30MB)
  1317. 3rd MTFTP session on CPort=102, SPort=202 Size=26214400 (25MB)
  1318. Example 2: assume we want to download a 300MB file
  1319. using 1468 byte TFTP block sizes.
  1320. /RDMTFTPADDR=224.1.1.2 /RDMTFTPCPORT=100 /RDMTFTPSPORT=200
  1321. /RDCHUNKSIZE=94371840 /RDFILESIZE=314572800
  1322. 1st MTFTP session on CPort=100, SPort=200 Size=94371840 (90MB)
  1323. 2nd MTFTP session on CPort=101, SPort=201 Size=94371840 (90MB)
  1324. 3rd MTFTP session on CPort=102, SPort=202 Size=94371840 (90MB)
  1325. 4th MTFTP session on CPort=103, SPort=203 Size=31457280 (30MB)
  1326. /RDBUILD - Indicates that the boot ramdisk should be built
  1327. from the build server. This is ignored if the RDBUILD
  1328. option is set.
  1329. Example: /RDBUILD
  1330. /RDGUID - Specifies the GUID of the configuration to be built
  1331. by the build server.
  1332. Example: /RDGUID={54C7D140-09EF-11D1-B25A-F5FE627ED95E}
  1333. /RDDISCOVERY - Describes how we should discover the build server.
  1334. Possible values include:
  1335. M - A packet will be sent on the multicast
  1336. address specified by /RDMCASTADDR. The
  1337. first server to respond will be used. If
  1338. /RDMCASTADDR is not specified multicast
  1339. discovery is ignored.
  1340. B - A packet will be broadcasted. First server
  1341. to respond will be used. This is the default.
  1342. U - A packet will be unicast to all the servers
  1343. specified in the /RMSERVERS. Or if /RMSERVERS
  1344. is not specified to the PXE Boot server.
  1345. R - Will only use servers that are included in
  1346. the /RMSERVERS. This option is used to filter
  1347. servers when specified in conjunction with broadcast
  1348. and Multicast discovery. It can also be used with
  1349. unicast discovery to specify a list of servers
  1350. to use.
  1351. If more than one discovery option is specified then
  1352. exactly one method will be selected according in the
  1353. following order:
  1354. 1) Multicast - M
  1355. 2) Broadcast - B (Default)
  1356. 3) Unicast U
  1357. Examples:
  1358. /RDDISCOVERY=U
  1359. This will send a unicast packet to the same
  1360. server as the PXE Boot server.
  1361. /RDDISCOVERY=M /RDMCASTADDR=204.1.1.1
  1362. This will send a multicast packet to the
  1363. address sepcified.
  1364. /RDDISCOVERY=MBU /RDMCASTADDR=204.1.1.1
  1365. This will use multicast discovery to the address
  1366. specified. If no boot server responds within the
  1367. timeout period. Note that Broadcast and Unicast
  1368. discovery are ignored.
  1369. /RDDISCOVERY=BR /RDSERVERS={10.0.0.3, 10.0.0.4}
  1370. This will send a broadcast packet but will only
  1371. accept responses from 10.0.0.3 or 10.0.0.4 .
  1372. /RDDISCOVERY=UR /RDSERVERS={10.0.0.3, 10.0.0.4}
  1373. A discovery packet will be sent to the two servers
  1374. specified and one of them will be selected.
  1375. /RDMCASTADDR Specifies the Multicast address to use for Build Server
  1376. multicast discovery.
  1377. /RDSERVERS Specifies a list of Build Servers to accpet responses
  1378. from. A maximum of 10 servers are supported.
  1379. Example: /RDSERVERS={10.0.0.3, 10.0.0.4}
  1380. /RDTIMEOUT Specifies the timeout period to wait for a response in
  1381. seconds. Default is 10 secs.
  1382. Example: /RDTIMEOUT=10
  1383. /RDRETRY Specifies the number of times to retry finding a build
  1384. server. Default is 5 times.
  1385. Example: /RDRETRY=5
  1386. Return Value:
  1387. ESUCCESS - read completed successfully
  1388. !ESUCCESS - read failed
  1389. --*/
  1390. {
  1391. PCHAR value;
  1392. PCHAR p;
  1393. USHORT i;
  1394. if ( LoadOptions == NULL ) {
  1395. return ESUCCESS;
  1396. }
  1397. //
  1398. // Get RDPATH and its associated options
  1399. //
  1400. RamdiskPath = RamdiskGetOptionValue( LoadOptions, "RDPATH" );
  1401. if (RamdiskPath) {
  1402. value = RamdiskGetOptionValue( LoadOptions, "RDIMAGEOFFSET" );
  1403. if (value) RamdiskImageOffset = atoi( value );
  1404. value = RamdiskGetOptionValue( LoadOptions, "RDIMAGELENGTH" );
  1405. if (value) RamdiskImageLength = _atoi64( value );
  1406. //
  1407. // By Default the PXE Boot Server is the TFTP address
  1408. //
  1409. RamdiskTFTPAddr = NetServerIpAddress;
  1410. //
  1411. // Get the MTFTP Address used to download the image.
  1412. // if not specified, the image will be downloaded
  1413. // from the same place as ntldr (i.e. the PXE
  1414. // boot server).
  1415. //
  1416. value = RamdiskGetOptionValue( LoadOptions, "RDMTFTPADDR" );
  1417. if ( value ) {
  1418. RamdiskMTFTPAddr = RamdiskParseIPAddr( value );
  1419. value = RamdiskGetOptionValue( LoadOptions, "RDMTFTPCPORT" );
  1420. if ( value ) RamdiskMTFTPCPort = SWAP_WORD( (USHORT)atoi( value ) );
  1421. value = RamdiskGetOptionValue( LoadOptions, "RDMTFTPSPORT" );
  1422. if (value) RamdiskMTFTPSPort = SWAP_WORD( (USHORT)atoi( value ) );
  1423. value = RamdiskGetOptionValue( LoadOptions, "RDMTFTPDELAY" );
  1424. if (value) RamdiskMTFTPDelay = (USHORT)atoi( value );
  1425. value = RamdiskGetOptionValue( LoadOptions, "RDMTFTPTIMEOUT" );
  1426. if (value) RamdiskMTFTPTimeout = (USHORT)atoi( value );
  1427. value = RamdiskGetOptionValue( LoadOptions, "RDFILESIZE" );
  1428. if (value) RamdiskMTFTPFileSize = _atoi64( value );
  1429. value = RamdiskGetOptionValue( LoadOptions, "RDCHUNKSIZE" );
  1430. if (value) RamdiskMTFTPChunkSize = _atoi64( value );
  1431. // Validate options
  1432. if ( RamdiskMTFTPAddr == 0 ||
  1433. RamdiskMTFTPCPort == 0 ||
  1434. RamdiskMTFTPSPort == 0 ||
  1435. RamdiskMTFTPDelay == 0 ||
  1436. RamdiskMTFTPTimeout == 0 ||
  1437. RamdiskMTFTPFileSize == 0 ||
  1438. RamdiskMTFTPChunkSize > RamdiskMTFTPFileSize ) {
  1439. return EINVAL;
  1440. }
  1441. }
  1442. if (DBGLVL(INFO)) {
  1443. DbgPrint( "RAMDISK options:\n");
  1444. DbgPrint( "RDPATH = %s\n", RamdiskPath);
  1445. p = (PCHAR) &RamdiskMTFTPAddr;
  1446. DbgPrint( "RDMTFTPADDR = %u.%u.%u.%u\n", p[0], p[1], p[2], p[3]);
  1447. DbgPrint( "RDMTFTPCPORT = %d\n", SWAP_WORD( RamdiskMTFTPCPort ));
  1448. DbgPrint( "RDMTFTPSPORT = %d\n", SWAP_WORD( RamdiskMTFTPSPort ));
  1449. DbgPrint( "RDMTFTPDELAY = %d\n", RamdiskMTFTPDelay);
  1450. DbgPrint( "RDMTFTPTIMEOUT = %d\n", RamdiskMTFTPTimeout);
  1451. DbgPrint( "RDFILESIZE = 0x%0I64x bytes\n", RamdiskMTFTPFileSize );
  1452. DbgPrint( "RDCHUNKSIZE = 0x%0I64x bytes\n", RamdiskMTFTPChunkSize );
  1453. DbgPrint( "RDIMAGEOFFSET = 0x%x bytes\n", RamdiskImageOffset );
  1454. DbgPrint( "RDIMAGELENGTH = 0x%0I64x bytes\n", RamdiskImageLength );
  1455. }
  1456. // we are done if RDPATH was specified.
  1457. return ESUCCESS;
  1458. }
  1459. #if defined(POST_XPSP)
  1460. //
  1461. // Check if RDBUILD exists
  1462. //
  1463. if ( strstr( LoadOptions, "RDBUILD" ) ) {
  1464. RamdiskBuild = TRUE;
  1465. value = RamdiskGetOptionValue( LoadOptions, "RDGUID" );
  1466. if ( value == NULL ||
  1467. RamdiskGUIDFromString( value, &RamdiskGuid ) == FALSE ) {
  1468. return EINVAL;
  1469. }
  1470. value = RamdiskGetOptionValue( LoadOptions, "RDMCASTADDR" );
  1471. if ( value ) RamdiskMCastAddr = RamdiskParseIPAddr( value );
  1472. value = RamdiskGetOptionValue( LoadOptions, "RDSERVERS" );
  1473. if ( value && *value == '{' ) {
  1474. PCHAR e = strchr( value, '}' );
  1475. p = value;
  1476. if ( e && (ULONG)(e - p) > 7 ) { // at least seven characters for X.X.X.X
  1477. while ( p && p < e && RamdiskServerCount < RAMDISK_MAX_SERVERS) {
  1478. RamdiskServers[RamdiskServerCount] = RamdiskParseIPAddr( p + 1 );
  1479. RamdiskServerCount++;
  1480. p = strchr( p + 1, ',' );
  1481. }
  1482. }
  1483. }
  1484. value = RamdiskGetOptionValue( LoadOptions, "RDDISCOVERY" );
  1485. if ( value ) {
  1486. // NOTE : order of these checks is important since
  1487. // they override each other.
  1488. if ( strchr( value, 'U' ) ) {
  1489. RamdiskDiscovery = RAMDISK_DISCOVERY_UNICAST;
  1490. }
  1491. if ( strchr( value, 'B' ) ) {
  1492. RamdiskDiscovery = RAMDISK_DISCOVERY_BROADCAST;
  1493. }
  1494. if ( strchr( value, 'M' ) && RamdiskMCastAddr != 0 ) {
  1495. RamdiskDiscovery = RAMDISK_DISCOVERY_MULTICAST;
  1496. }
  1497. if ( strchr( value, 'R' ) && RamdiskServerCount > 0 ) {
  1498. RamdiskDiscovery |= RAMDISK_DISCOVERY_RESTRICT;
  1499. }
  1500. } else {
  1501. // default is broadcast discovery
  1502. RamdiskDiscovery = RAMDISK_DISCOVERY_BROADCAST;
  1503. }
  1504. value = RamdiskGetOptionValue( LoadOptions, "RDTIMEOUT" );
  1505. if (value) RamdiskTimeout = (USHORT)atoi( value );
  1506. value = RamdiskGetOptionValue( LoadOptions, "RDRETRY" );
  1507. if (value) RamdiskRetry = (USHORT)atoi( value );
  1508. //
  1509. // Normalize options
  1510. //
  1511. if ( !TEST_BIT( RamdiskDiscovery, RAMDISK_DISCOVERY_RESTRICT) &&
  1512. RamdiskServerCount > 0 ) {
  1513. RamdiskServerCount = 0;
  1514. }
  1515. if ( RamdiskDiscovery == RAMDISK_DISCOVERY_UNICAST ) {
  1516. RamdiskServerCount = 1;
  1517. RamdiskServers[0] = NetServerIpAddress;
  1518. }
  1519. //
  1520. // Print out debug information
  1521. //
  1522. if (DBGLVL(INFO)) {
  1523. DbgPrint("RDBUILD options:\n");
  1524. DbgPrint("RDGUID = {%x-%x-%x-%x%x%x%x%x%x%x%x}\n",
  1525. RamdiskGuid.Data1, RamdiskGuid.Data2,
  1526. RamdiskGuid.Data3,
  1527. RamdiskGuid.Data4[0], RamdiskGuid.Data4[1],
  1528. RamdiskGuid.Data4[2], RamdiskGuid.Data4[3],
  1529. RamdiskGuid.Data4[4], RamdiskGuid.Data4[5],
  1530. RamdiskGuid.Data4[6], RamdiskGuid.Data4[7]);
  1531. DbgPrint("RDDISCOVERY = %d\n", RamdiskDiscovery);
  1532. p = (PCHAR) &RamdiskMCastAddr;
  1533. DbgPrint("RDMCASTADDR = %u.%u.%u.%u\n", p[0], p[1], p[2], p[3]);
  1534. DbgPrint("RDSERVERS = %d\n", RamdiskServerCount);
  1535. for (i = 0; i < RamdiskServerCount; i++) {
  1536. p = (PCHAR) &RamdiskServers[i];
  1537. DbgPrint("RDSERVER[%d] = %u.%u.%u.%u\n", i, p[0], p[1], p[2], p[3]);
  1538. }
  1539. DbgPrint("RDTIMEOUT = %d\n", RamdiskTimeout);
  1540. DbgPrint("RDRETRY = %d\n", RamdiskRetry);
  1541. }
  1542. }
  1543. #endif
  1544. return ESUCCESS;
  1545. }
  1546. #if defined(POST_XPSP)
  1547. #if defined(i386) // RDBUILD is only supported on x86 machines for now
  1548. VOID
  1549. RamdiskDeviceInfoToString(
  1550. PDEVICE_INFO pDevice,
  1551. PCHAR DeviceString
  1552. )
  1553. /*++
  1554. Routine Description:
  1555. This routine generates a string representation of the Device info for
  1556. debugging purposes.
  1557. Arguments:
  1558. pDefive - Pointer to the device info structure
  1559. DeviceString - a pointer to a buffer that will hold the final string. The buffer
  1560. must be at least 128 * sizeof(CHAR) bytes.
  1561. Return Value:
  1562. NONE.
  1563. --*/
  1564. {
  1565. const CHAR HexToCharTable[17] = "0123456789ABCDEF";
  1566. if (pDevice->DeviceType == BMBUILD_DEVICE_TYPE_PCI) {
  1567. sprintf ( DeviceString,
  1568. "%d.%d.%d PCI\\VEN_%04X&DEV_%04X&SUBSYS_%04X%04X&REV_%02X&CC_%02X%02X%02X",
  1569. PCI_ITERATOR_TO_BUS( pDevice->info.pci.BusDevFunc ),
  1570. PCI_ITERATOR_TO_DEVICE( pDevice->info.pci.BusDevFunc ),
  1571. PCI_ITERATOR_TO_FUNCTION( pDevice->info.pci.BusDevFunc ),
  1572. pDevice->info.pci.VendorID,
  1573. pDevice->info.pci.DeviceID,
  1574. pDevice->info.pci.SubDeviceID,
  1575. pDevice->info.pci.SubVendorID,
  1576. pDevice->info.pci.RevisionID,
  1577. pDevice->info.pci.BaseClass,
  1578. pDevice->info.pci.SubClass,
  1579. pDevice->info.pci.ProgIntf );
  1580. } else if (pDevice->DeviceType == BMBUILD_DEVICE_TYPE_PCI_BRIDGE ) {
  1581. sprintf ( DeviceString,
  1582. "%d.%d.%d PCI\\VEN_%04X&DEV_%04X&REV_%02X&CC_%02X%02X%02X Bridge %d->%d Sub = %d",
  1583. PCI_ITERATOR_TO_BUS( pDevice->info.pci_bridge.BusDevFunc ),
  1584. PCI_ITERATOR_TO_DEVICE( pDevice->info.pci_bridge.BusDevFunc ),
  1585. PCI_ITERATOR_TO_FUNCTION( pDevice->info.pci_bridge.BusDevFunc ),
  1586. pDevice->info.pci_bridge.VendorID,
  1587. pDevice->info.pci_bridge.DeviceID,
  1588. pDevice->info.pci_bridge.RevisionID,
  1589. pDevice->info.pci_bridge.BaseClass,
  1590. pDevice->info.pci_bridge.SubClass,
  1591. pDevice->info.pci_bridge.ProgIntf,
  1592. pDevice->info.pci_bridge.PrimaryBus,
  1593. pDevice->info.pci_bridge.SecondaryBus,
  1594. pDevice->info.pci_bridge.SubordinateBus );
  1595. } else if (pDevice->DeviceType == BMBUILD_DEVICE_TYPE_PNP) {
  1596. CHAR ProductIDStr[8];
  1597. PUCHAR id = (PUCHAR)&pDevice->info.pnp.EISADevID;
  1598. ProductIDStr[0] = (id[0] >> 2) + 0x40;
  1599. ProductIDStr[1] = (((id[0] & 0x03) << 3) | (id[1] >> 5)) + 0x40;
  1600. ProductIDStr[2] = (id[1] & 0x1f) + 0x40;
  1601. ProductIDStr[3] = HexToCharTable[id[2] >> 4];
  1602. ProductIDStr[4] = HexToCharTable[id[2] & 0x0F];
  1603. ProductIDStr[5] = HexToCharTable[id[3] >> 4];
  1604. ProductIDStr[6] = HexToCharTable[id[3] & 0x0F];
  1605. ProductIDStr[7] = 0x00;
  1606. sprintf( DeviceString,
  1607. "%s CC_%02X%02X%02X",
  1608. ProductIDStr,
  1609. pDevice->info.pnp.BaseClass,
  1610. pDevice->info.pnp.SubClass,
  1611. pDevice->info.pnp.ProgIntf );
  1612. }
  1613. }
  1614. ARC_STATUS
  1615. RamdiskBuildRequest(
  1616. IN PBMBUILD_REQUEST_PACKET pRequest
  1617. )
  1618. /*++
  1619. Routine Description:
  1620. This routine will form the build request packet.
  1621. Arguments:
  1622. pRequest - a pointer to a buffer that will hold the request
  1623. Return Value:
  1624. ESUCCESS - read completed successfully
  1625. !ESUCCESS - read failed
  1626. --*/
  1627. {
  1628. ARC_STATUS status;
  1629. PDEVICE_INFO pDevice;
  1630. t_PXENV_UNDI_GET_NIC_TYPE PxeNicType;
  1631. PCONFIGURATION_COMPONENT_DATA Node = NULL;
  1632. PCONFIGURATION_COMPONENT_DATA CurrentNode = NULL;
  1633. PCONFIGURATION_COMPONENT_DATA ResumeNode = NULL;
  1634. PPCIDEVICE pPCIDevice;
  1635. PPNP_BIOS_INSTALLATION_CHECK pPNPBios;
  1636. PPNP_BIOS_DEVICE_NODE pDevNode;
  1637. PCM_PARTIAL_RESOURCE_LIST pPartialList;
  1638. PUCHAR pCurr;
  1639. USHORT cDevices;
  1640. USHORT i;
  1641. ULONG lengthRemaining;
  1642. ULONG x;
  1643. PCHAR HalName = NULL;
  1644. BOOLEAN fNICFound = FALSE;
  1645. pRequest->Version = BMBUILD_PACKET_VERSION;
  1646. pRequest->OpCode = BMBUILD_OPCODE_REQUEST;
  1647. pRequest->Length = BMBUILD_REQUEST_FIXED_PACKET_LENGTH;
  1648. //
  1649. // Get the SMBIOS UUID (or PXE MAC address)
  1650. //
  1651. GetGuid( (PUCHAR*)&pRequest->MachineGuid, &x );
  1652. ASSERT( x == sizeof( pRequest->MachineGuid ) );
  1653. memcpy( &pRequest->ProductGuid, &RamdiskGuid, sizeof( GUID ) );
  1654. #ifdef _IA64_
  1655. pRequest->Architecture = PROCESSOR_ARCHITECTURE_IA64;
  1656. #else
  1657. pRequest->Architecture = PROCESSOR_ARCHITECTURE_INTEL;
  1658. #endif
  1659. //
  1660. // Detect the appropriate HAL using TextMode Setup methods.
  1661. //
  1662. #ifdef DOWNLOAD_TXTSETUP_SIF
  1663. status = SlInitIniFile( "net(0)",
  1664. 0,
  1665. "boot\\txtsetup.sif",
  1666. &InfFile,
  1667. NULL,
  1668. NULL,
  1669. &x);
  1670. #endif
  1671. HalName = SlDetectHal();
  1672. ASSERT( HalName != NULL );
  1673. strcpy( (PCHAR) pRequest->Data, HalName );
  1674. pRequest->HalDataOffset = BMBUILD_FIELD_OFFSET(BMBUILD_REQUEST_PACKET, Data);
  1675. pRequest->Flags = 0;
  1676. pRequest->DeviceCount = 0;
  1677. pRequest->DeviceOffset = RESET_SIZE_AT_USHORT_MAX((ULONG)pRequest->HalDataOffset + strlen( HalName ) + 1);
  1678. pDevice = (PDEVICE_INFO)( (PUCHAR)pRequest + pRequest->DeviceOffset );
  1679. //
  1680. // Get the PXE NIC information
  1681. //
  1682. RtlZeroMemory( &PxeNicType, sizeof( PxeNicType ) );
  1683. status = RomGetNicType( &PxeNicType );
  1684. if ( ( status != PXENV_EXIT_SUCCESS ) || ( PxeNicType.Status != PXENV_EXIT_SUCCESS ) ) {
  1685. DBGPRINT( ERR, ( "RAMDISK ERROR: Couldn't get the NIC type from PXE. Failed with %x, status = %x\n", status, PxeNicType.Status ) );
  1686. return ENODEV;
  1687. }
  1688. //
  1689. // Fill in PCI Device information
  1690. //
  1691. Node = KeFindConfigurationEntry(FwConfigurationTree,
  1692. PeripheralClass,
  1693. RealModePCIEnumeration,
  1694. NULL);
  1695. ASSERT( Node != NULL );
  1696. ASSERT( Node->ComponentEntry.ConfigurationDataLength > 0 );
  1697. ASSERT( Node->ConfigurationData != NULL );
  1698. pPCIDevice = (PPCIDEVICE)( (PUCHAR)Node->ConfigurationData + sizeof( CM_PARTIAL_RESOURCE_LIST ) );
  1699. cDevices = (USHORT)( Node->ComponentEntry.ConfigurationDataLength - sizeof ( CM_PARTIAL_RESOURCE_LIST ) ) / sizeof ( PCIDEVICE );
  1700. if (cDevices > BMBUILD_MAX_DEVICES( RamdiskMaxPacketSize ) ) {
  1701. DBGPRINT(ERR, ("RAMDISK ERROR: Too many PCI devices to fit in a request\n"));
  1702. return EINVAL;
  1703. }
  1704. for (i = 0; i < cDevices; i++ ) {
  1705. //
  1706. // check if this is a bridge or a normal device
  1707. //
  1708. if ( (pPCIDevice->Config.HeaderType & (~PCI_MULTIFUNCTION) ) == PCI_BRIDGE_TYPE) {
  1709. //
  1710. // Bridge.
  1711. //
  1712. pDevice[i].DeviceType = BMBUILD_DEVICE_TYPE_PCI_BRIDGE;
  1713. pDevice[i].info.pci_bridge.BusDevFunc = pPCIDevice->BusDevFunc;
  1714. pDevice[i].info.pci_bridge.VendorID = pPCIDevice->Config.VendorID;
  1715. pDevice[i].info.pci_bridge.DeviceID = pPCIDevice->Config.DeviceID;
  1716. pDevice[i].info.pci_bridge.BaseClass = pPCIDevice->Config.BaseClass;
  1717. pDevice[i].info.pci_bridge.SubClass = pPCIDevice->Config.SubClass;
  1718. pDevice[i].info.pci_bridge.ProgIntf = 0;
  1719. pDevice[i].info.pci_bridge.RevisionID = pPCIDevice->Config.RevisionID;
  1720. pDevice[i].info.pci_bridge.PrimaryBus = pPCIDevice->Config.u.type1.PrimaryBus;
  1721. pDevice[i].info.pci_bridge.SecondaryBus = pPCIDevice->Config.u.type1.SecondaryBus;
  1722. pDevice[i].info.pci_bridge.SubordinateBus = pPCIDevice->Config.u.type1.SubordinateBus;
  1723. } else {
  1724. //
  1725. // Non-bridge PCI device
  1726. //
  1727. pDevice[i].DeviceType = BMBUILD_DEVICE_TYPE_PCI;
  1728. pDevice[i].info.pci.BusDevFunc = pPCIDevice->BusDevFunc;
  1729. pDevice[i].info.pci.VendorID = pPCIDevice->Config.VendorID;
  1730. pDevice[i].info.pci.DeviceID = pPCIDevice->Config.DeviceID;
  1731. pDevice[i].info.pci.BaseClass = pPCIDevice->Config.BaseClass;
  1732. pDevice[i].info.pci.SubClass = pPCIDevice->Config.SubClass;
  1733. pDevice[i].info.pci.ProgIntf = 0;
  1734. pDevice[i].info.pci.RevisionID = pPCIDevice->Config.RevisionID;
  1735. pDevice[i].info.pci.SubVendorID = pPCIDevice->Config.u.type0.SubVendorID;
  1736. pDevice[i].info.pci.SubDeviceID = pPCIDevice->Config.u.type0.SubSystemID;
  1737. //
  1738. // Check if this device is the PXE boot device
  1739. //
  1740. if ( PxeNicType.NicType == 2 &&
  1741. PxeNicType.pci_pnp_info.pci.BusDevFunc == ( pPCIDevice->BusDevFunc & 0x7FFF ) ) {
  1742. pRequest->PrimaryNicIndex = i;
  1743. fNICFound = TRUE;
  1744. }
  1745. }
  1746. pPCIDevice++;
  1747. }
  1748. pRequest->DeviceCount = pRequest->DeviceCount + cDevices;
  1749. pDevice += cDevices;
  1750. //
  1751. // Fill in PNP Device information (if there)
  1752. //
  1753. Node = NULL;
  1754. while ((CurrentNode = KeFindConfigurationNextEntry(
  1755. FwConfigurationTree,
  1756. AdapterClass,
  1757. MultiFunctionAdapter,
  1758. NULL,
  1759. &ResumeNode)) != 0) {
  1760. if (!(strcmp(CurrentNode->ComponentEntry.Identifier,"PNP BIOS"))) {
  1761. Node = CurrentNode;
  1762. break;
  1763. }
  1764. ResumeNode = CurrentNode;
  1765. }
  1766. if ( Node != NULL ) {
  1767. //
  1768. // Set the PnP BIOS devices if found
  1769. //
  1770. ASSERT( Node->ComponentEntry.ConfigurationDataLength > 0 );
  1771. ASSERT( Node->ConfigurationData != NULL );
  1772. pPartialList = (PCM_PARTIAL_RESOURCE_LIST)Node->ConfigurationData;
  1773. pPNPBios = (PPNP_BIOS_INSTALLATION_CHECK)( (PUCHAR)Node->ConfigurationData + sizeof( CM_PARTIAL_RESOURCE_LIST ) );
  1774. pCurr = (PUCHAR)pPNPBios + pPNPBios->Length;
  1775. lengthRemaining = pPartialList->PartialDescriptors[0].u.DeviceSpecificData.DataSize - pPNPBios->Length;
  1776. for (cDevices = 0; lengthRemaining > sizeof(PNP_BIOS_DEVICE_NODE); cDevices++) {
  1777. if ((pRequest->DeviceCount + cDevices + 1) > BMBUILD_MAX_DEVICES( RamdiskMaxPacketSize ) ) {
  1778. DBGPRINT(ERR, ("RAMDISK ERROR: Too many PNP devices to fit in a request\n"));
  1779. return EINVAL;
  1780. }
  1781. pDevNode = (PPNP_BIOS_DEVICE_NODE)pCurr;
  1782. if (pDevNode->Size > lengthRemaining) {
  1783. DBGPRINT( ERR,
  1784. ( "PNP Node # %d, invalid size (%d), length remaining (%d)\n",
  1785. pDevNode->Node,
  1786. pDevNode->Size,
  1787. lengthRemaining ) );
  1788. ASSERT( FALSE );
  1789. // REVIEW: [bassamt] Should I fail here?
  1790. break;
  1791. }
  1792. pDevice->DeviceType = BMBUILD_DEVICE_TYPE_PNP;
  1793. pDevice->info.pnp.EISADevID = pDevNode->ProductId;
  1794. pDevice->info.pnp.BaseClass = pDevNode->DeviceType[0];
  1795. pDevice->info.pnp.SubClass = pDevNode->DeviceType[1];
  1796. pDevice->info.pnp.ProgIntf = pDevNode->DeviceType[2];
  1797. pDevice->info.pnp.CardSelNum = pDevNode->Node;
  1798. if ( PxeNicType.NicType == 3 &&
  1799. PxeNicType.pci_pnp_info.pnp.EISA_Dev_ID == pDevNode->ProductId &&
  1800. PxeNicType.pci_pnp_info.pnp.CardSelNum == pDevNode->Node) {
  1801. pRequest->PrimaryNicIndex = pRequest->DeviceCount + cDevices;
  1802. fNICFound = TRUE;
  1803. }
  1804. pCurr += pDevNode->Size;
  1805. lengthRemaining -= pDevNode->Size;
  1806. pDevice++;
  1807. }
  1808. pRequest->DeviceCount = pRequest->DeviceCount + cDevices;
  1809. }
  1810. //
  1811. // We better have found the primary NIC or the packet is invalid
  1812. //
  1813. if (!fNICFound) {
  1814. DBGPRINT(ERR, ("RAMDISK ERROR: Could not find the primary NIC\n"));
  1815. return ENODEV;
  1816. }
  1817. //
  1818. // Set the packet length
  1819. //
  1820. pRequest->Length += pRequest->DeviceCount * sizeof( DEVICE_INFO );
  1821. ASSERT ( pRequest->Length <= RamdiskMaxPacketSize );
  1822. //
  1823. // Debug prints
  1824. //
  1825. if (DBGLVL(INFO)) {
  1826. DbgPrint("RAMDISK Build Request\n");
  1827. DbgPrint("Architecture = %d\n", pRequest->Architecture);
  1828. DbgPrint("MachineGuid = {%x-%x-%x-%x%x%x%x%x%x%x%x}\n",
  1829. pRequest->MachineGuid.Data1, pRequest->MachineGuid.Data2,
  1830. pRequest->MachineGuid.Data3,
  1831. pRequest->MachineGuid.Data4[0], pRequest->MachineGuid.Data4[1],
  1832. pRequest->MachineGuid.Data4[2], pRequest->MachineGuid.Data4[3],
  1833. pRequest->MachineGuid.Data4[4], pRequest->MachineGuid.Data4[5],
  1834. pRequest->MachineGuid.Data4[6], pRequest->MachineGuid.Data4[7]);
  1835. DbgPrint("ProductGuid = {%x-%x-%x-%x%x%x%x%x%x%x%x}\n",
  1836. pRequest->ProductGuid.Data1, pRequest->ProductGuid.Data2,
  1837. pRequest->ProductGuid.Data3,
  1838. pRequest->ProductGuid.Data4[0], pRequest->ProductGuid.Data4[1],
  1839. pRequest->ProductGuid.Data4[2], pRequest->ProductGuid.Data4[3],
  1840. pRequest->ProductGuid.Data4[4], pRequest->ProductGuid.Data4[5],
  1841. pRequest->ProductGuid.Data4[6], pRequest->ProductGuid.Data4[7]);
  1842. DbgPrint("HALName = %s\n", HalName);
  1843. DbgPrint("Flags = 0x%x\n", pRequest->Flags);
  1844. DbgPrint("DeviceCount = %d\n", pRequest->DeviceCount);
  1845. pDevice = (PDEVICE_INFO)( (PUCHAR)pRequest + pRequest->DeviceOffset );
  1846. for (i = 0; i < pRequest->DeviceCount; i++ ) {
  1847. CHAR DeviceString[128];
  1848. RamdiskDeviceInfoToString( pDevice, DeviceString );
  1849. DbgPrint( "[%d] %s %s\n", i, DeviceString, (i == pRequest->PrimaryNicIndex? "PRIMARY NIC" : "") );
  1850. pDevice++;
  1851. }
  1852. }
  1853. return ESUCCESS;
  1854. }
  1855. ARC_STATUS
  1856. RamdiskSendDiscoverPacketAndWait(
  1857. IN PBMBUILD_DISCOVER_PACKET pDiscover,
  1858. IN ULONG DestinationAddress,
  1859. IN ULONG Timeout,
  1860. IN BOOLEAN fExcludeServers,
  1861. IN ULONG DiscoveryStartTime,
  1862. IN ULONG TotalExpectedWaitTime,
  1863. OUT PULONG pBuildServerChosen
  1864. )
  1865. /*++
  1866. Routine Description:
  1867. This routine will send a discovery packet on the network in
  1868. accordance with the RamdiskDiscovery paramters. It will then
  1869. select wait for a timeout period for replies and select the best
  1870. response.
  1871. Arguments:
  1872. pDiscover - Discover packet to send out
  1873. DestinationAddress - destination address of the discovery packet
  1874. Timeout - timeout interval
  1875. fExcludeServers - if TRUE the response are filtered through the server list
  1876. DiscoveryStartTime - time when discovery was started
  1877. TotalExpectedWaitTime - total time we expect to be in discovery. for updating the progress bar
  1878. pBuildServerChosen - pointer to storage that will hold the chosen server
  1879. Return Value:
  1880. ESUCCESS - successfully selected a boot server
  1881. !ESUCCESS - no build server chosen
  1882. --*/
  1883. {
  1884. ULONG WaitStartTime;
  1885. BMBUILD_ACCEPT_PACKET Accept;
  1886. PUCHAR p = (PUCHAR) &DestinationAddress;
  1887. ULONG lastProgressPercent = 0;
  1888. BOOLEAN ForceDisplayFirstTime = TRUE;
  1889. ULONG currentProgressPercent;
  1890. USHORT BestBuildTime = 0xFFFF;
  1891. USHORT iServer;
  1892. ULONG Length;
  1893. ULONG RemoteHost = 0;
  1894. USHORT RemotePort = 0;
  1895. //
  1896. // No server chosen initially
  1897. //
  1898. *pBuildServerChosen = 0;
  1899. //
  1900. // Send the discovery packet to the destination address
  1901. //
  1902. if ( RomSendUdpPacket( (PVOID)pDiscover,
  1903. sizeof( BMBUILD_DISCOVER_PACKET ),
  1904. DestinationAddress,
  1905. BMBUILD_SERVER_PORT ) != sizeof( BMBUILD_DISCOVER_PACKET ) ) {
  1906. DBGPRINT(ERR, ("FAILED to send discovery packet to %u.%u.%u.%u:%u\n", p[0], p[1], p[2], p[3], SWAP_WORD( BMBUILD_SERVER_PORT )));
  1907. //
  1908. // update progress bar. this happens here since a timed out packet
  1909. // might take some time.
  1910. //
  1911. currentProgressPercent = ((SysGetRelativeTime() - DiscoveryStartTime ) * 100) / TotalExpectedWaitTime;
  1912. if ( ForceDisplayFirstTime || (currentProgressPercent != lastProgressPercent) ) {
  1913. BlUpdateProgressBar( currentProgressPercent );
  1914. ForceDisplayFirstTime = FALSE;
  1915. }
  1916. lastProgressPercent = currentProgressPercent;
  1917. return EINVAL;
  1918. }
  1919. DBGPRINT(INFO, ("Sent discovery packet to %u.%u.%u.%u:%u\n", p[0], p[1], p[2], p[3], SWAP_WORD( BMBUILD_SERVER_PORT )));
  1920. //
  1921. // Wait for the responses. We will wait for the timeout period and
  1922. // select the best ACCEPT we get within this timeout. The best accept
  1923. // is the one with the lowest build time.
  1924. //
  1925. WaitStartTime = SysGetRelativeTime();
  1926. while ( (SysGetRelativeTime() - WaitStartTime) < Timeout ) {
  1927. Length = RomReceiveUdpPacket( (PVOID)&Accept, sizeof(Accept), 0, &RemoteHost, &RemotePort);
  1928. if ( Length == sizeof( BMBUILD_ACCEPT_PACKET ) &&
  1929. RemotePort == BMBUILD_SERVER_PORT &&
  1930. Accept.Version == BMBUILD_PACKET_VERSION &&
  1931. Accept.OpCode == BMBUILD_OPCODE_ACCEPT &&
  1932. Accept.Length == sizeof( BMBUILD_ACCEPT_PACKET ) - BMBUILD_COMMON_PACKET_LENGTH &&
  1933. Accept.XID == pDiscover->XID ) {
  1934. ULONG AcceptedBuildServer = RemoteHost;
  1935. ASSERT( RemoteHost != 0 );
  1936. ASSERT( RemoteHost != 0xFFFFFFFF );
  1937. p = (PUCHAR) &RemoteHost;
  1938. DBGPRINT(INFO, ("Received ACCEPT packet XID = %d from %u.%u.%u.%u:%u\n", Accept.XID, p[0], p[1], p[2], p[3], SWAP_WORD( BMBUILD_SERVER_PORT )));
  1939. //
  1940. // Exclude servers if we were asked to
  1941. //
  1942. if ( fExcludeServers ) {
  1943. for (iServer = 0; iServer < RamdiskServerCount; iServer++) {
  1944. if (RemoteHost == RamdiskServers[iServer]) {
  1945. p = (PUCHAR) &RemoteHost;
  1946. DBGPRINT(INFO, ("Ignoring ACCEPT packet from %u.%u.%u.%u:%u\n", p[0], p[1], p[2], p[3], SWAP_WORD( BMBUILD_SERVER_PORT )));
  1947. AcceptedBuildServer = 0;
  1948. break;
  1949. }
  1950. }
  1951. }
  1952. //
  1953. // We have a valid packet from a build server. Check
  1954. // if it is the best one. "Best" is determined by
  1955. // the first server to respond with the lowest
  1956. // builtime.
  1957. //
  1958. if ( AcceptedBuildServer != 0 &&
  1959. Accept.BuildTime < BestBuildTime) {
  1960. p = (PUCHAR) &AcceptedBuildServer;
  1961. DBGPRINT(INFO, ("We picked server at %u.%u.%u.%u:%u for this build\n", p[0], p[1], p[2], p[3], SWAP_WORD( BMBUILD_SERVER_PORT )));
  1962. *pBuildServerChosen = AcceptedBuildServer;
  1963. return ESUCCESS;
  1964. }
  1965. }
  1966. //
  1967. // update progress bar
  1968. //
  1969. currentProgressPercent = ((SysGetRelativeTime() - DiscoveryStartTime ) * 100) / TotalExpectedWaitTime;
  1970. if ( ForceDisplayFirstTime || (currentProgressPercent != lastProgressPercent) ) {
  1971. BlUpdateProgressBar( currentProgressPercent );
  1972. ForceDisplayFirstTime = FALSE;
  1973. }
  1974. lastProgressPercent = currentProgressPercent;
  1975. }
  1976. return ENOENT;
  1977. }
  1978. ARC_STATUS
  1979. RamdiskDiscoverBuildServer(
  1980. OUT PULONG BuildServerIpAddress
  1981. )
  1982. /*++
  1983. Routine Description:
  1984. This routine will send a discovery packet on the network in
  1985. accordance with the RamdiskDiscovery paramters. It will then
  1986. select a acceptance from one of the build servers that respond
  1987. and return its IP Address.
  1988. Arguments:
  1989. BuildServerIpAddress - returned Build server IP address
  1990. Return Value:
  1991. ESUCCESS - successfully selected a boot server
  1992. !ESUCCESS - no build server responded
  1993. --*/
  1994. {
  1995. #define DISCOVER_RETRIES 4 // 4 retries
  1996. #define DISCOVER_TIMEOUT 4 // starting at 4 then 8, 16, and 32
  1997. #define DISCOVER_TOTAL_TIMEOUT_TIME (4+8+16+32) // total time that we could be waiting for responses
  1998. #define DISCOVER_SEND_TIMEOUT 3 // Time it takes to send a packet to an invalid address
  1999. BMBUILD_DISCOVER_PACKET Discover;
  2000. BOOLEAN fExcludeServers = FALSE;
  2001. ULONG iRetry;
  2002. ULONG cRetries = DISCOVER_RETRIES;
  2003. ULONG Timeout = DISCOVER_TIMEOUT;
  2004. ULONG x;
  2005. ULONG DestinationAddress = 0xFFFFFFFF;
  2006. ULONG DiscoveryStartTime = SysGetRelativeTime();
  2007. ULONG TotalExpectedWaitTime;
  2008. USHORT iServer;
  2009. ASSERT( BuildServerIpAddress );
  2010. *BuildServerIpAddress = 0;
  2011. //
  2012. // Short-circuit discovery if we are unicasting to one server
  2013. //
  2014. if ( RamdiskDiscovery == RAMDISK_DISCOVERY_UNICAST &&
  2015. RamdiskServerCount == 1 ) {
  2016. ASSERT( RamdiskServers[0] != 0 );
  2017. ASSERT( RamdiskServers[0] != 0xFFFFFFFF );
  2018. *BuildServerIpAddress = RamdiskServers[0];
  2019. return ESUCCESS;
  2020. }
  2021. //
  2022. // Fill in Discover packet
  2023. //
  2024. Discover.Version = BMBUILD_PACKET_VERSION;
  2025. Discover.OpCode = BMBUILD_OPCODE_DISCOVER;
  2026. Discover.Length = sizeof( BMBUILD_DISCOVER_PACKET ) - BMBUILD_COMMON_PACKET_LENGTH;
  2027. GetGuid( (PUCHAR*)&Discover.MachineGuid, &x );
  2028. ASSERT( x == sizeof( Discover.MachineGuid ) );
  2029. memcpy( &Discover.ProductGuid, &RamdiskGuid, sizeof( GUID ) );
  2030. //
  2031. // Start the discovery. Note that this will be repeated a number
  2032. // of times to account for network congestion and load on the servers.
  2033. //
  2034. BlOutputStartupMsg(RAMDISK_BUILD_DISCOVER);
  2035. BlUpdateProgressBar(0);
  2036. //
  2037. // If we are doing multicast or broadcast discovery
  2038. //
  2039. if ( TEST_BIT( RamdiskDiscovery, RAMDISK_DISCOVERY_BROADCAST ) ||
  2040. TEST_BIT( RamdiskDiscovery, RAMDISK_DISCOVERY_MULTICAST ) ) {
  2041. TotalExpectedWaitTime = DISCOVER_TOTAL_TIMEOUT_TIME;
  2042. for (iRetry = 0; iRetry < cRetries; iRetry++) {
  2043. // Each Discover / Accpet gets its own transaction ID.
  2044. Discover.XID = ++RamdiskXID;
  2045. DBGPRINT(INFO, ("Sending Discovery packet XID = %d. Retry %d out of %d. Timeout = %d\n", Discover.XID, iRetry, cRetries, Timeout));
  2046. if ( TEST_BIT( RamdiskDiscovery, RAMDISK_DISCOVERY_MULTICAST) ) {
  2047. DestinationAddress = RamdiskMCastAddr;
  2048. } else if ( TEST_BIT( RamdiskDiscovery, RAMDISK_DISCOVERY_BROADCAST ) ) {
  2049. DestinationAddress = 0xFFFFFFFF;
  2050. }
  2051. if ( TEST_BIT( RamdiskDiscovery, RAMDISK_DISCOVERY_RESTRICT ) ) {
  2052. ASSERT( RamdiskServerCount > 0 );
  2053. fExcludeServers = TRUE;
  2054. }
  2055. if ( RamdiskSendDiscoverPacketAndWait( &Discover,
  2056. DestinationAddress,
  2057. Timeout,
  2058. fExcludeServers,
  2059. DiscoveryStartTime,
  2060. TotalExpectedWaitTime,
  2061. BuildServerIpAddress ) == ESUCCESS ) {
  2062. // we found a server. we are done.
  2063. BlUpdateProgressBar( 100 );
  2064. return ESUCCESS;
  2065. }
  2066. // double the timeout
  2067. Timeout = Timeout * 2;
  2068. }
  2069. } else {
  2070. //
  2071. // We are doing Unicast discovery to a fixed list of server addresses
  2072. // in the order that they appear in the list. first to respond within
  2073. // the timeout period wins
  2074. //
  2075. TotalExpectedWaitTime = RamdiskServerCount * ( RamdiskTimeout + DISCOVER_SEND_TIMEOUT );
  2076. for (iServer = 0; iServer < RamdiskServerCount; iServer++) {
  2077. // Each Discover / Accpet gets its own transaction ID.
  2078. Discover.XID = ++RamdiskXID;
  2079. if (RamdiskSendDiscoverPacketAndWait( &Discover,
  2080. RamdiskServers[iServer],
  2081. RamdiskTimeout,
  2082. FALSE,
  2083. DiscoveryStartTime,
  2084. TotalExpectedWaitTime,
  2085. BuildServerIpAddress ) == ESUCCESS ) {
  2086. // we found a server. we are done.
  2087. BlUpdateProgressBar( 100 );
  2088. return ESUCCESS;
  2089. }
  2090. }
  2091. }
  2092. BlUpdateProgressBar( 100 );
  2093. return EINVAL;
  2094. }
  2095. VOID
  2096. RamdiskWait(
  2097. ULONG WaitTime
  2098. )
  2099. {
  2100. ULONG startTime = SysGetRelativeTime();
  2101. while ( (SysGetRelativeTime() - startTime) < WaitTime ) {
  2102. }
  2103. }
  2104. VOID
  2105. RamdiskPrintBuildProgress(
  2106. ULONG uMsgId,
  2107. ULONG BuildServerIpAddress
  2108. )
  2109. {
  2110. PUCHAR p;
  2111. PTCHAR FormatString = NULL;
  2112. TCHAR Buffer[256];
  2113. //
  2114. // print progress message
  2115. //
  2116. p = (PUCHAR) &BuildServerIpAddress;
  2117. FormatString = BlFindMessage( uMsgId );
  2118. if ( FormatString != NULL ) {
  2119. _stprintf(Buffer, FormatString, p[0], p[1], p[2], p[3] );
  2120. BlOutputTrailerMsgStr( Buffer );
  2121. }
  2122. }
  2123. ARC_STATUS
  2124. RamdiskBuildAndInitialize(
  2125. )
  2126. /*++
  2127. Routine Description:
  2128. This routine will communicate with a build server to build
  2129. a ramdisk and obtain a RDPATH.
  2130. Arguments:
  2131. Return Value:
  2132. ESUCCESS - read completed successfully
  2133. !ESUCCESS - read failed
  2134. --*/
  2135. {
  2136. ARC_STATUS status;
  2137. PBMBUILD_REQUEST_PACKET pRequest = NULL;
  2138. PBMBUILD_RESPONSE_PACKET pResponse = NULL;
  2139. USHORT iRetry = 0;
  2140. ULONG Length;
  2141. ULONG RemoteHost = 0;
  2142. USHORT RemotePort = 0;
  2143. PUCHAR p;
  2144. BOOLEAN fSuccess = FALSE;
  2145. ULONG BuildServerIpAddress = 0;
  2146. //
  2147. // Set the max packet size. This is calculate from the
  2148. // MTU size of the network (1500 for Ethernet) minus
  2149. // the IP and UDP headers ( which account to 28 bytes ).
  2150. //
  2151. RamdiskMaxPacketSize = NetMaxTranUnit - 28;
  2152. ASSERT( RamdiskMaxPacketSize > 0 );
  2153. pRequest = BlAllocateHeap( RamdiskMaxPacketSize );
  2154. if ( pRequest == NULL ) {
  2155. DBGPRINT(ERR, ("Failed to allocate request packet of size %d.\n", RamdiskMaxPacketSize));
  2156. return ENOMEM;
  2157. }
  2158. pResponse = BlAllocateHeap( RamdiskMaxPacketSize );
  2159. if ( pRequest == NULL ) {
  2160. DBGPRINT(ERR, ("Failed to allocate request packet of size %d.\n", RamdiskMaxPacketSize));
  2161. return ENOMEM;
  2162. }
  2163. memset(pRequest, 0, RamdiskMaxPacketSize);
  2164. memset(pResponse, 0, RamdiskMaxPacketSize);
  2165. //
  2166. // All packets sent from client will go from the client port
  2167. //
  2168. RomSetReceiveStatus( BMBUILD_CLIENT_PORT );
  2169. //
  2170. // Discover build server
  2171. //
  2172. status = RamdiskDiscoverBuildServer( &BuildServerIpAddress );
  2173. if (status != ESUCCESS ) {
  2174. goto Error;
  2175. }
  2176. //
  2177. // Build request packet
  2178. //
  2179. status = RamdiskBuildRequest( pRequest );
  2180. if ( status != ESUCCESS ) {
  2181. goto Error;
  2182. }
  2183. //
  2184. // Start communication with boot server. Display the text progress
  2185. // bar when booting of a ramdisk
  2186. //
  2187. BlOutputStartupMsg(RAMDISK_BUILD_REQUEST);
  2188. BlUpdateProgressBar(0);
  2189. RamdiskPrintBuildProgress( RAMDISK_BUILD_PROGRESS, BuildServerIpAddress );
  2190. DBGPRINT(INFO, ("Requesting appropriate image for this computer...\n"));
  2191. while ( iRetry < RamdiskRetry ) {
  2192. BlUpdateProgressBar( (iRetry * 100) / RamdiskRetry );
  2193. Length = pRequest->Length + BMBUILD_COMMON_PACKET_LENGTH;
  2194. // allocate a new transaction ID for this session
  2195. pRequest->XID = ++RamdiskXID;
  2196. p = (PUCHAR) &NetServerIpAddress;
  2197. DBGPRINT(INFO, ( "Sending request packet (%d bytes) XID = %d to %u.%u.%u.%u:%u.\n",
  2198. Length, pRequest->XID, p[0], p[1], p[2], p[3], SWAP_WORD( BMBUILD_SERVER_PORT ) ) );
  2199. if ( RomSendUdpPacket( (PVOID)pRequest, Length, BuildServerIpAddress, BMBUILD_SERVER_PORT ) != Length ) {
  2200. RamdiskWait( RamdiskTimeout );
  2201. iRetry++;
  2202. RamdiskPrintBuildProgress( RAMDISK_BUILD_PROGRESS_ERROR, BuildServerIpAddress );
  2203. continue;
  2204. }
  2205. DBGPRINT(INFO, ( "Waiting for response (Timeout = %d secs).\n", RamdiskTimeout ) );
  2206. Length = RomReceiveUdpPacket( (PVOID)pResponse, RamdiskMaxPacketSize, RamdiskTimeout, &RemoteHost, &RemotePort);
  2207. if ( Length == 0 ) {
  2208. DBGPRINT(INFO, ( "Receive timed out.\n") );
  2209. iRetry++;
  2210. RamdiskPrintBuildProgress( RAMDISK_BUILD_PROGRESS_TIMEOUT, BuildServerIpAddress );
  2211. continue;
  2212. }
  2213. if ( Length < BMBUILD_RESPONSE_FIXED_PACKET_LENGTH - BMBUILD_COMMON_PACKET_LENGTH ||
  2214. RemoteHost != BuildServerIpAddress ||
  2215. RemotePort != BMBUILD_SERVER_PORT ||
  2216. pResponse->OpCode != BMBUILD_OPCODE_RESPONSE ||
  2217. pResponse->Version != BMBUILD_PACKET_VERSION ||
  2218. pResponse->XID != pRequest->XID ) {
  2219. p = (PUCHAR) &RemoteHost;
  2220. DBGPRINT(INFO, ("Received invalid packet OpCode = %d Length = %d Version = %d XID = %d from %u.%u.%u.%u:%u\n",
  2221. pResponse->OpCode,
  2222. pResponse->Length, pResponse->Version, pResponse->XID,
  2223. p[0], p[1], p[2], p[3], SWAP_WORD( RemotePort ) ) );
  2224. RamdiskWait( RamdiskTimeout );
  2225. iRetry++;
  2226. RamdiskPrintBuildProgress( RAMDISK_BUILD_PROGRESS_ERROR, BuildServerIpAddress );
  2227. continue;
  2228. }
  2229. p = (PUCHAR) &RemoteHost;
  2230. DBGPRINT(INFO, ("Received VALID packet OpCode = %d Length = %d Version = %d XID = %d from %u.%u.%u.%u:%u\n",
  2231. pResponse->OpCode,
  2232. pResponse->Length, pResponse->Version, pResponse->XID,
  2233. p[0], p[1], p[2], p[3], SWAP_WORD( RemotePort )) );
  2234. if ( BMBUILD_IS_E( pResponse->Error ) ) {
  2235. DBGPRINT(INFO, ("Request Failed. Error = %x\n", pResponse->Error) );
  2236. RamdiskWait( RamdiskTimeout );
  2237. iRetry++;
  2238. RamdiskPrintBuildProgress( RAMDISK_BUILD_PROGRESS_PENDING, BuildServerIpAddress );
  2239. continue;
  2240. }
  2241. if ( pResponse->Error == BMBUILD_S_REQUEST_PENDING ) {
  2242. DBGPRINT(INFO, ("Request is pending. Instructed to wait for %d secs.\n", pResponse->WaitTime ) );
  2243. RamdiskWait( pResponse->WaitTime );
  2244. RamdiskPrintBuildProgress( RAMDISK_BUILD_PROGRESS_ERROR, BuildServerIpAddress );
  2245. continue;
  2246. }
  2247. ASSERT( BMBUILD_S ( pResponse->Error ) );
  2248. fSuccess = TRUE;
  2249. break;
  2250. }
  2251. if (fSuccess) {
  2252. ASSERT ( RamdiskPath == NULL );
  2253. RamdiskPrintBuildProgress( RAMDISK_BUILD_PROGRESS, BuildServerIpAddress );
  2254. BlUpdateProgressBar( 100 );
  2255. //
  2256. // Set the MTFTP options
  2257. //
  2258. RamdiskTFTPAddr = pResponse->TFTPAddr.Address;
  2259. RamdiskMTFTPAddr = pResponse->MTFTPAddr.Address;
  2260. RamdiskMTFTPCPort = pResponse->MTFTPCPort;
  2261. RamdiskMTFTPSPort = pResponse->MTFTPSPort;
  2262. RamdiskMTFTPTimeout = pResponse->MTFTPTimeout;
  2263. RamdiskMTFTPDelay = pResponse->MTFTPDelay;
  2264. RamdiskMTFTPFileSize = pResponse->MTFTPFileSize;
  2265. RamdiskMTFTPChunkSize = pResponse->MTFTPChunkSize;
  2266. //
  2267. // Set the image offset and length
  2268. //
  2269. RamdiskImageOffset = pResponse->ImageFileOffset;
  2270. RamdiskImageLength = pResponse->ImageFileLength;
  2271. p = (PUCHAR)((ULONG_PTR)pResponse + pResponse->ImagePathOffset);
  2272. RamdiskPath = BlAllocateHeap( strlen((PCHAR)p ) + sizeof ( "net(0)\\" ) );
  2273. if ( RamdiskPath == NULL ) {
  2274. DBGPRINT(ERR, ("Failed to allocate memory for RamdiskPath size %d.\n", strlen((PCHAR)p ) + sizeof ( "net(0)\\" )));
  2275. return ENOMEM;
  2276. }
  2277. strcpy( RamdiskPath, "net(0)\\" );
  2278. strcat( RamdiskPath, (PCHAR)p );
  2279. if (DBGLVL(INFO)) {
  2280. DbgPrint( "RDPATH = %s\n", RamdiskPath);
  2281. p = (PUCHAR) &RamdiskTFTPAddr;
  2282. DbgPrint( "RDTFTPADDR = %u.%u.%u.%u\n", p[0], p[1], p[2], p[3]);
  2283. p = (PUCHAR) &RamdiskMTFTPAddr;
  2284. DbgPrint( "RDMTFTPADDR = %u.%u.%u.%u\n", p[0], p[1], p[2], p[3]);
  2285. DbgPrint( "RDMTFTPCPORT = %d\n", SWAP_WORD( RamdiskMTFTPCPort ));
  2286. DbgPrint( "RDMTFTPSPORT = %d\n", SWAP_WORD( RamdiskMTFTPSPort ));
  2287. DbgPrint( "RDMTFTPDELAY = %d\n", RamdiskMTFTPDelay);
  2288. DbgPrint( "RDMTFTPTIMEOUT = %d\n", RamdiskMTFTPTimeout);
  2289. DbgPrint( "RDFILESIZE = 0x%0I64x bytes\n", RamdiskMTFTPFileSize );
  2290. DbgPrint( "RDCHUNKSIZE = 0x%0I64x bytes\n", RamdiskMTFTPChunkSize );
  2291. DbgPrint( "RDIMAGEOFFSET = 0x%x bytes\n", RamdiskImageOffset );
  2292. DbgPrint( "RDIMAGELENGTH = 0x%0I64x bytes\n", RamdiskImageLength );
  2293. }
  2294. } else {
  2295. DBGPRINT(ERR, ("RamdiskBuildAndInitialize: Failed.\n"));
  2296. return EINVAL;
  2297. }
  2298. return ESUCCESS;
  2299. Error:
  2300. return status;
  2301. }
  2302. #endif
  2303. #endif // defined(POST_XPSP)
  2304. VOID
  2305. RamdiskFatalError(
  2306. IN ULONG Message1,
  2307. IN ULONG Message2
  2308. )
  2309. /*++
  2310. Routine Description:
  2311. This function looks up a message to display at a error condition.
  2312. Arguments:
  2313. Message - message that describes the class of problem.
  2314. Return Value:
  2315. none
  2316. --*/
  2317. {
  2318. PTCHAR Text;
  2319. TCHAR Buffer[40];
  2320. ULONG Count;
  2321. BlClearScreen();
  2322. Text = BlFindMessage(Message1);
  2323. if (Text == NULL) {
  2324. _stprintf(Buffer,TEXT("%08lx\r\n"),Message1);
  2325. Text = Buffer;
  2326. }
  2327. ArcWrite(BlConsoleOutDeviceId,
  2328. Text,
  2329. (ULONG)_tcslen(Text)*sizeof(TCHAR),
  2330. &Count);
  2331. Text = BlFindMessage(Message2);
  2332. if (Text == NULL) {
  2333. _stprintf(Buffer,TEXT("%08lx\r\n"),Message2);
  2334. Text = Buffer;
  2335. }
  2336. ArcWrite(BlConsoleOutDeviceId,
  2337. Text,
  2338. (ULONG)_tcslen(Text)*sizeof(TCHAR),
  2339. &Count);
  2340. #if defined(ENABLE_LOADER_DEBUG) || DBG
  2341. #if (defined(_X86_) || defined(_ALPHA_) || defined(_IA64_)) && !defined(ARCI386) // everything but ARCI386
  2342. if(BdDebuggerEnabled) {
  2343. DbgBreakPoint();
  2344. }
  2345. #endif
  2346. #endif
  2347. return;
  2348. }
  2349. #if defined(_X86_)
  2350. VOID
  2351. RamdiskSdiBoot(
  2352. IN PCHAR SdiFile
  2353. )
  2354. {
  2355. ARC_STATUS status;
  2356. PSDI_HEADER sdiHeader;
  2357. PUCHAR startromAddress;
  2358. ULONG startromLength;
  2359. BOOLEAN OldShowProgressBar;
  2360. LONGLONG availableLength;
  2361. //
  2362. // Read the SDI image into memory.
  2363. //
  2364. RamdiskTFTPAddr = NetServerIpAddress;
  2365. RamdiskImageOffset = 0;
  2366. RamdiskImageLength = 0;
  2367. OldShowProgressBar = BlShowProgressBar;
  2368. BlShowProgressBar = TRUE;
  2369. status = RamdiskReadImage( SdiFile );
  2370. if ( status != ESUCCESS ) {
  2371. RamdiskFatalError( RAMDISK_GENERAL_FAILURE,
  2372. RAMDISK_BOOT_FAILURE );
  2373. return;
  2374. }
  2375. BlShowProgressBar = OldShowProgressBar;
  2376. //
  2377. // Copy startrom.com from the SDI image to 0x7c00.
  2378. //
  2379. sdiHeader = MapRamdisk( 0, &availableLength );
  2380. ASSERT( availableLength >= sizeof(SDI_HEADER) );
  2381. ASSERT( availableLength >=
  2382. (sdiHeader->liBootCodeOffset.QuadPart + sdiHeader->liBootCodeSize.QuadPart) );
  2383. ASSERT( sdiHeader->liBootCodeOffset.HighPart == 0 );
  2384. ASSERT( sdiHeader->liBootCodeSize.HighPart == 0 );
  2385. startromAddress = (PUCHAR)sdiHeader + sdiHeader->liBootCodeOffset.LowPart;
  2386. startromLength = sdiHeader->liBootCodeSize.LowPart;
  2387. RtlMoveMemory( (PVOID)0x7c00, startromAddress, startromLength );
  2388. //
  2389. // Shut down PXE.
  2390. //
  2391. if ( BlBootingFromNet ) {
  2392. NetTerminate();
  2393. }
  2394. //
  2395. // Inform boot debugger that the boot phase is complete.
  2396. //
  2397. #if defined(ENABLE_LOADER_DEBUG) || DBG
  2398. #if (defined(_X86_) || defined(_ALPHA_)) && !defined(ARCI386)
  2399. {
  2400. if (BdDebuggerEnabled == TRUE) {
  2401. DbgUnLoadImageSymbols(NULL, (PVOID)-1, 0);
  2402. }
  2403. }
  2404. #endif
  2405. #endif
  2406. REBOOT( (ULONG)sdiHeader | 3 );
  2407. return;
  2408. }
  2409. #endif