HttpBootImpl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /** @file
  2. The implementation of EFI_LOAD_FILE_PROTOCOL for UEFI HTTP boot.
  3. Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
  4. (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "HttpBootDxe.h"
  8. /**
  9. Install HTTP Boot Callback Protocol if not installed before.
  10. @param[in] Private Pointer to HTTP Boot private data.
  11. @retval EFI_SUCCESS HTTP Boot Callback Protocol installed succesfully.
  12. @retval Others Failed to install HTTP Boot Callback Protocol.
  13. **/
  14. EFI_STATUS
  15. HttpBootInstallCallback (
  16. IN HTTP_BOOT_PRIVATE_DATA *Private
  17. )
  18. {
  19. EFI_STATUS Status;
  20. EFI_HANDLE ControllerHandle;
  21. if (!Private->UsingIpv6) {
  22. ControllerHandle = Private->Ip4Nic->Controller;
  23. } else {
  24. ControllerHandle = Private->Ip6Nic->Controller;
  25. }
  26. //
  27. // Check whether gEfiHttpBootCallbackProtocolGuid already installed.
  28. //
  29. Status = gBS->HandleProtocol (
  30. ControllerHandle,
  31. &gEfiHttpBootCallbackProtocolGuid,
  32. (VOID **) &Private->HttpBootCallback
  33. );
  34. if (Status == EFI_UNSUPPORTED) {
  35. CopyMem (
  36. &Private->LoadFileCallback,
  37. &gHttpBootDxeHttpBootCallback,
  38. sizeof (EFI_HTTP_BOOT_CALLBACK_PROTOCOL)
  39. );
  40. //
  41. // Install a default callback if user didn't offer one.
  42. //
  43. Status = gBS->InstallProtocolInterface (
  44. &ControllerHandle,
  45. &gEfiHttpBootCallbackProtocolGuid,
  46. EFI_NATIVE_INTERFACE,
  47. &Private->LoadFileCallback
  48. );
  49. if (EFI_ERROR (Status)) {
  50. return Status;
  51. }
  52. Private->HttpBootCallback = &Private->LoadFileCallback;
  53. }
  54. return EFI_SUCCESS;
  55. }
  56. /**
  57. Uninstall HTTP Boot Callback Protocol if it's installed by this driver.
  58. @param[in] Private Pointer to HTTP Boot private data.
  59. **/
  60. VOID
  61. HttpBootUninstallCallback (
  62. IN HTTP_BOOT_PRIVATE_DATA *Private
  63. )
  64. {
  65. if (Private->HttpBootCallback == &Private->LoadFileCallback) {
  66. gBS->UninstallProtocolInterface (
  67. Private->Controller,
  68. &gEfiHttpBootCallbackProtocolGuid,
  69. &Private->HttpBootCallback
  70. );
  71. Private->HttpBootCallback = NULL;
  72. }
  73. }
  74. /**
  75. Enable the use of UEFI HTTP boot function.
  76. If the driver has already been started but not satisfy the requirement (IP stack and
  77. specified boot file path), this function will stop the driver and start it again.
  78. @param[in] Private The pointer to the driver's private data.
  79. @param[in] UsingIpv6 Specifies the type of IP addresses that are to be
  80. used during the session that is being started.
  81. Set to TRUE for IPv6, and FALSE for IPv4.
  82. @param[in] FilePath The device specific path of the file to load.
  83. @retval EFI_SUCCESS HTTP boot was successfully enabled.
  84. @retval EFI_INVALID_PARAMETER Private is NULL or FilePath is NULL.
  85. @retval EFI_INVALID_PARAMETER The FilePath doesn't contain a valid URI device path node.
  86. @retval EFI_ALREADY_STARTED The driver is already in started state.
  87. @retval EFI_OUT_OF_RESOURCES There are not enough resources.
  88. **/
  89. EFI_STATUS
  90. HttpBootStart (
  91. IN HTTP_BOOT_PRIVATE_DATA *Private,
  92. IN BOOLEAN UsingIpv6,
  93. IN EFI_DEVICE_PATH_PROTOCOL *FilePath
  94. )
  95. {
  96. UINTN Index;
  97. EFI_STATUS Status;
  98. CHAR8 *Uri;
  99. Uri = NULL;
  100. if (Private == NULL || FilePath == NULL) {
  101. return EFI_INVALID_PARAMETER;
  102. }
  103. //
  104. // Check the URI in the input FilePath, in order to see whether it is
  105. // required to boot from a new specified boot file.
  106. //
  107. Status = HttpBootParseFilePath (FilePath, &Uri);
  108. if (EFI_ERROR (Status)) {
  109. return EFI_INVALID_PARAMETER;
  110. }
  111. //
  112. // Check whether we need to stop and restart the HTTP boot driver.
  113. //
  114. if (Private->Started) {
  115. //
  116. // Restart is needed in 2 cases:
  117. // 1. Http boot driver has already been started but not on the required IP stack.
  118. // 2. The specified boot file URI in FilePath is different with the one we have
  119. // recorded before.
  120. //
  121. if ((UsingIpv6 != Private->UsingIpv6) ||
  122. ((Uri != NULL) && (AsciiStrCmp (Private->BootFileUri, Uri) != 0))) {
  123. //
  124. // Restart is required, first stop then continue this start function.
  125. //
  126. Status = HttpBootStop (Private);
  127. if (EFI_ERROR (Status)) {
  128. if (Uri != NULL) {
  129. FreePool (Uri);
  130. }
  131. return Status;
  132. }
  133. } else {
  134. //
  135. // Restart is not required.
  136. //
  137. if (Uri != NULL) {
  138. FreePool (Uri);
  139. }
  140. return EFI_ALREADY_STARTED;
  141. }
  142. }
  143. //
  144. // Detect whether using ipv6 or not, and set it to the private data.
  145. //
  146. if (UsingIpv6 && Private->Ip6Nic != NULL) {
  147. Private->UsingIpv6 = TRUE;
  148. } else if (!UsingIpv6 && Private->Ip4Nic != NULL) {
  149. Private->UsingIpv6 = FALSE;
  150. } else {
  151. if (Uri != NULL) {
  152. FreePool (Uri);
  153. }
  154. return EFI_UNSUPPORTED;
  155. }
  156. //
  157. // Record the specified URI and prepare the URI parser if needed.
  158. //
  159. Private->FilePathUri = Uri;
  160. if (Private->FilePathUri != NULL) {
  161. Status = HttpParseUrl (
  162. Private->FilePathUri,
  163. (UINT32) AsciiStrLen (Private->FilePathUri),
  164. FALSE,
  165. &Private->FilePathUriParser
  166. );
  167. if (EFI_ERROR (Status)) {
  168. FreePool (Private->FilePathUri);
  169. return Status;
  170. }
  171. }
  172. //
  173. // Init the content of cached DHCP offer list.
  174. //
  175. ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));
  176. if (!Private->UsingIpv6) {
  177. for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {
  178. Private->OfferBuffer[Index].Dhcp4.Packet.Offer.Size = HTTP_CACHED_DHCP4_PACKET_MAX_SIZE;
  179. }
  180. } else {
  181. for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {
  182. Private->OfferBuffer[Index].Dhcp6.Packet.Offer.Size = HTTP_CACHED_DHCP6_PACKET_MAX_SIZE;
  183. }
  184. }
  185. if (Private->UsingIpv6) {
  186. //
  187. // Set Ip6 policy to Automatic to start the Ip6 router discovery.
  188. //
  189. Status = HttpBootSetIp6Policy (Private);
  190. if (EFI_ERROR (Status)) {
  191. return Status;
  192. }
  193. }
  194. Private->Started = TRUE;
  195. Print (L"\n>>Start HTTP Boot over IPv%d", Private->UsingIpv6 ? 6 : 4);
  196. return EFI_SUCCESS;
  197. }
  198. /**
  199. Attempt to complete a DHCPv4 D.O.R.A or DHCPv6 S.R.A.A sequence to retrieve the boot resource information.
  200. @param[in] Private The pointer to the driver's private data.
  201. @retval EFI_SUCCESS Boot info was successfully retrieved.
  202. @retval EFI_INVALID_PARAMETER Private is NULL.
  203. @retval EFI_NOT_STARTED The driver is in stopped state.
  204. @retval EFI_DEVICE_ERROR An unexpected network error occurred.
  205. @retval Others Other errors as indicated.
  206. **/
  207. EFI_STATUS
  208. HttpBootDhcp (
  209. IN HTTP_BOOT_PRIVATE_DATA *Private
  210. )
  211. {
  212. EFI_STATUS Status;
  213. if (Private == NULL) {
  214. return EFI_INVALID_PARAMETER;
  215. }
  216. if (!Private->Started) {
  217. return EFI_NOT_STARTED;
  218. }
  219. Status = EFI_DEVICE_ERROR;
  220. if (!Private->UsingIpv6) {
  221. //
  222. // Start D.O.R.A process to get a IPv4 address and other boot information.
  223. //
  224. Status = HttpBootDhcp4Dora (Private);
  225. } else {
  226. //
  227. // Start S.A.R.R process to get a IPv6 address and other boot information.
  228. //
  229. Status = HttpBootDhcp6Sarr (Private);
  230. }
  231. return Status;
  232. }
  233. /**
  234. Attempt to download the boot file through HTTP message exchange.
  235. @param[in] Private The pointer to the driver's private data.
  236. @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
  237. code of EFI_SUCCESS, the amount of data transferred to
  238. Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
  239. the size of Buffer required to retrieve the requested file.
  240. @param[in] Buffer The memory buffer to transfer the file to. If Buffer is NULL,
  241. then the size of the requested file is returned in
  242. BufferSize.
  243. @param[out] ImageType The image type of the downloaded file.
  244. @retval EFI_SUCCESS Boot file was loaded successfully.
  245. @retval EFI_INVALID_PARAMETER Private is NULL, or ImageType is NULL, or BufferSize is NULL.
  246. @retval EFI_INVALID_PARAMETER *BufferSize is not zero, and Buffer is NULL.
  247. @retval EFI_NOT_STARTED The driver is in stopped state.
  248. @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the boot file. BufferSize has
  249. been updated with the size needed to complete the request.
  250. @retval EFI_DEVICE_ERROR An unexpected network error occurred.
  251. @retval Others Other errors as indicated.
  252. **/
  253. EFI_STATUS
  254. HttpBootLoadFile (
  255. IN HTTP_BOOT_PRIVATE_DATA *Private,
  256. IN OUT UINTN *BufferSize,
  257. IN VOID *Buffer, OPTIONAL
  258. OUT HTTP_BOOT_IMAGE_TYPE *ImageType
  259. )
  260. {
  261. EFI_STATUS Status;
  262. if (Private == NULL || ImageType == NULL || BufferSize == NULL ) {
  263. return EFI_INVALID_PARAMETER;
  264. }
  265. if (*BufferSize != 0 && Buffer == NULL) {
  266. return EFI_INVALID_PARAMETER;
  267. }
  268. if (!Private->Started) {
  269. return EFI_NOT_STARTED;
  270. }
  271. Status = HttpBootInstallCallback (Private);
  272. if (EFI_ERROR(Status)) {
  273. goto ON_EXIT;
  274. }
  275. if (Private->BootFileUri == NULL) {
  276. //
  277. // Parse the cached offer to get the boot file URL first.
  278. //
  279. Status = HttpBootDiscoverBootInfo (Private);
  280. if (EFI_ERROR (Status)) {
  281. AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n");
  282. goto ON_EXIT;
  283. }
  284. }
  285. if (!Private->HttpCreated) {
  286. //
  287. // Create HTTP child.
  288. //
  289. Status = HttpBootCreateHttpIo (Private);
  290. if (EFI_ERROR (Status)) {
  291. goto ON_EXIT;
  292. }
  293. }
  294. if (Private->BootFileSize == 0) {
  295. //
  296. // Discover the information about the bootfile if we haven't.
  297. //
  298. //
  299. // Try to use HTTP HEAD method.
  300. //
  301. Status = HttpBootGetBootFile (
  302. Private,
  303. TRUE,
  304. &Private->BootFileSize,
  305. NULL,
  306. &Private->ImageType
  307. );
  308. if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {
  309. //
  310. // Failed to get file size by HEAD method, may be trunked encoding, try HTTP GET method.
  311. //
  312. ASSERT (Private->BootFileSize == 0);
  313. Status = HttpBootGetBootFile (
  314. Private,
  315. FALSE,
  316. &Private->BootFileSize,
  317. NULL,
  318. &Private->ImageType
  319. );
  320. if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {
  321. AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n");
  322. goto ON_EXIT;
  323. }
  324. }
  325. }
  326. if (*BufferSize < Private->BootFileSize) {
  327. *BufferSize = Private->BootFileSize;
  328. *ImageType = Private->ImageType;
  329. Status = EFI_BUFFER_TOO_SMALL;
  330. goto ON_EXIT;
  331. }
  332. //
  333. // Load the boot file into Buffer
  334. //
  335. Status = HttpBootGetBootFile (
  336. Private,
  337. FALSE,
  338. BufferSize,
  339. Buffer,
  340. ImageType
  341. );
  342. ON_EXIT:
  343. HttpBootUninstallCallback (Private);
  344. if (EFI_ERROR (Status)) {
  345. if (Status == EFI_ACCESS_DENIED) {
  346. AsciiPrint ("\n Error: Could not establish connection with HTTP server.\n");
  347. } else if (Status == EFI_BUFFER_TOO_SMALL && Buffer != NULL) {
  348. AsciiPrint ("\n Error: Buffer size is smaller than the requested file.\n");
  349. } else if (Status == EFI_OUT_OF_RESOURCES) {
  350. AsciiPrint ("\n Error: Could not allocate I/O buffers.\n");
  351. } else if (Status == EFI_DEVICE_ERROR) {
  352. AsciiPrint ("\n Error: Network device error.\n");
  353. } else if (Status == EFI_TIMEOUT) {
  354. AsciiPrint ("\n Error: Server response timeout.\n");
  355. } else if (Status == EFI_ABORTED) {
  356. AsciiPrint ("\n Error: Remote boot cancelled.\n");
  357. } else if (Status != EFI_BUFFER_TOO_SMALL) {
  358. AsciiPrint ("\n Error: Unexpected network error.\n");
  359. }
  360. }
  361. return Status;
  362. }
  363. /**
  364. Disable the use of UEFI HTTP boot function.
  365. @param[in] Private The pointer to the driver's private data.
  366. @retval EFI_SUCCESS HTTP boot was successfully disabled.
  367. @retval EFI_NOT_STARTED The driver is already in stopped state.
  368. @retval EFI_INVALID_PARAMETER Private is NULL.
  369. @retval Others Unexpected error when stop the function.
  370. **/
  371. EFI_STATUS
  372. HttpBootStop (
  373. IN HTTP_BOOT_PRIVATE_DATA *Private
  374. )
  375. {
  376. UINTN Index;
  377. if (Private == NULL) {
  378. return EFI_INVALID_PARAMETER;
  379. }
  380. if (!Private->Started) {
  381. return EFI_NOT_STARTED;
  382. }
  383. if (Private->HttpCreated) {
  384. HttpIoDestroyIo (&Private->HttpIo);
  385. Private->HttpCreated = FALSE;
  386. }
  387. Private->Started = FALSE;
  388. ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS));
  389. ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS));
  390. ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS));
  391. Private->Port = 0;
  392. Private->BootFileUri = NULL;
  393. Private->BootFileUriParser = NULL;
  394. Private->BootFileSize = 0;
  395. Private->SelectIndex = 0;
  396. Private->SelectProxyType = HttpOfferTypeMax;
  397. if (!Private->UsingIpv6) {
  398. //
  399. // Stop and release the DHCP4 child.
  400. //
  401. Private->Dhcp4->Stop (Private->Dhcp4);
  402. Private->Dhcp4->Configure (Private->Dhcp4, NULL);
  403. for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {
  404. if (Private->OfferBuffer[Index].Dhcp4.UriParser) {
  405. HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp4.UriParser);
  406. }
  407. }
  408. } else {
  409. //
  410. // Stop and release the DHCP6 child.
  411. //
  412. Private->Dhcp6->Stop (Private->Dhcp6);
  413. Private->Dhcp6->Configure (Private->Dhcp6, NULL);
  414. for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {
  415. if (Private->OfferBuffer[Index].Dhcp6.UriParser) {
  416. HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp6.UriParser);
  417. }
  418. }
  419. }
  420. if (Private->DnsServerIp != NULL) {
  421. FreePool (Private->DnsServerIp);
  422. Private->DnsServerIp = NULL;
  423. }
  424. if (Private->FilePathUri!= NULL) {
  425. FreePool (Private->FilePathUri);
  426. HttpUrlFreeParser (Private->FilePathUriParser);
  427. Private->FilePathUri = NULL;
  428. Private->FilePathUriParser = NULL;
  429. }
  430. ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));
  431. Private->OfferNum = 0;
  432. ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
  433. ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
  434. HttpBootFreeCacheList (Private);
  435. return EFI_SUCCESS;
  436. }
  437. /**
  438. Causes the driver to load a specified file.
  439. @param This Protocol instance pointer.
  440. @param FilePath The device specific path of the file to load.
  441. @param BootPolicy If TRUE, indicates that the request originates from the
  442. boot manager is attempting to load FilePath as a boot
  443. selection. If FALSE, then FilePath must match as exact file
  444. to be loaded.
  445. @param BufferSize On input the size of Buffer in bytes. On output with a return
  446. code of EFI_SUCCESS, the amount of data transferred to
  447. Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
  448. the size of Buffer required to retrieve the requested file.
  449. @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
  450. then the size of the requested file is returned in
  451. BufferSize.
  452. @retval EFI_SUCCESS The file was loaded.
  453. @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy
  454. @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or
  455. BufferSize is NULL.
  456. @retval EFI_NO_MEDIA No medium was present to load the file.
  457. @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.
  458. @retval EFI_NO_RESPONSE The remote system did not respond.
  459. @retval EFI_NOT_FOUND The file was not found.
  460. @retval EFI_ABORTED The file load process was manually cancelled.
  461. @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
  462. BufferSize has been updated with the size needed to complete
  463. the request.
  464. **/
  465. EFI_STATUS
  466. EFIAPI
  467. HttpBootDxeLoadFile (
  468. IN EFI_LOAD_FILE_PROTOCOL *This,
  469. IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
  470. IN BOOLEAN BootPolicy,
  471. IN OUT UINTN *BufferSize,
  472. IN VOID *Buffer OPTIONAL
  473. )
  474. {
  475. HTTP_BOOT_PRIVATE_DATA *Private;
  476. HTTP_BOOT_VIRTUAL_NIC *VirtualNic;
  477. EFI_STATUS MediaStatus;
  478. BOOLEAN UsingIpv6;
  479. EFI_STATUS Status;
  480. HTTP_BOOT_IMAGE_TYPE ImageType;
  481. if (This == NULL || BufferSize == NULL || FilePath == NULL) {
  482. return EFI_INVALID_PARAMETER;
  483. }
  484. //
  485. // Only support BootPolicy
  486. //
  487. if (!BootPolicy) {
  488. return EFI_UNSUPPORTED;
  489. }
  490. VirtualNic = HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This);
  491. Private = VirtualNic->Private;
  492. //
  493. // Check media status before HTTP boot start
  494. //
  495. MediaStatus = EFI_SUCCESS;
  496. NetLibDetectMediaWaitTimeout (Private->Controller, HTTP_BOOT_CHECK_MEDIA_WAITING_TIME, &MediaStatus);
  497. if (MediaStatus != EFI_SUCCESS) {
  498. AsciiPrint ("\n Error: Could not detect network connection.\n");
  499. return EFI_NO_MEDIA;
  500. }
  501. //
  502. // Check whether the virtual nic is using IPv6 or not.
  503. //
  504. UsingIpv6 = FALSE;
  505. if (VirtualNic == Private->Ip6Nic) {
  506. UsingIpv6 = TRUE;
  507. }
  508. //
  509. // Initialize HTTP boot.
  510. //
  511. Status = HttpBootStart (Private, UsingIpv6, FilePath);
  512. if (Status != EFI_SUCCESS && Status != EFI_ALREADY_STARTED) {
  513. return Status;
  514. }
  515. //
  516. // Load the boot file.
  517. //
  518. ImageType = ImageTypeMax;
  519. Status = HttpBootLoadFile (Private, BufferSize, Buffer, &ImageType);
  520. if (EFI_ERROR (Status)) {
  521. if (Status == EFI_BUFFER_TOO_SMALL && (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk)) {
  522. Status = EFI_WARN_FILE_SYSTEM;
  523. } else if (Status != EFI_BUFFER_TOO_SMALL) {
  524. HttpBootStop (Private);
  525. }
  526. return Status;
  527. }
  528. //
  529. // Register the RAM Disk to the system if needed.
  530. //
  531. if (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk) {
  532. Status = HttpBootRegisterRamDisk (Private, *BufferSize, Buffer, ImageType);
  533. if (!EFI_ERROR (Status)) {
  534. Status = EFI_WARN_FILE_SYSTEM;
  535. } else {
  536. AsciiPrint ("\n Error: Could not register RAM disk to the system.\n");
  537. }
  538. }
  539. //
  540. // Stop the HTTP Boot service after the boot image is downloaded.
  541. //
  542. HttpBootStop (Private);
  543. return Status;
  544. }
  545. ///
  546. /// Load File Protocol instance
  547. ///
  548. GLOBAL_REMOVE_IF_UNREFERENCED
  549. EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile = {
  550. HttpBootDxeLoadFile
  551. };
  552. /**
  553. Callback function that is invoked when the HTTP Boot driver is about to transmit or has received a
  554. packet.
  555. This function is invoked when the HTTP Boot driver is about to transmit or has received packet.
  556. Parameters DataType and Received specify the type of event and the format of the buffer pointed
  557. to by Data. Due to the polling nature of UEFI device drivers, this callback function should not
  558. execute for more than 5 ms.
  559. The returned status code determines the behavior of the HTTP Boot driver.
  560. @param[in] This Pointer to the EFI_HTTP_BOOT_CALLBACK_PROTOCOL instance.
  561. @param[in] DataType The event that occurs in the current state.
  562. @param[in] Received TRUE if the callback is being invoked due to a receive event.
  563. FALSE if the callback is being invoked due to a transmit event.
  564. @param[in] DataLength The length in bytes of the buffer pointed to by Data.
  565. @param[in] Data A pointer to the buffer of data, the data type is specified by
  566. DataType.
  567. @retval EFI_SUCCESS Tells the HTTP Boot driver to continue the HTTP Boot process.
  568. @retval EFI_ABORTED Tells the HTTP Boot driver to abort the current HTTP Boot process.
  569. **/
  570. EFI_STATUS
  571. EFIAPI
  572. HttpBootCallback (
  573. IN EFI_HTTP_BOOT_CALLBACK_PROTOCOL *This,
  574. IN EFI_HTTP_BOOT_CALLBACK_DATA_TYPE DataType,
  575. IN BOOLEAN Received,
  576. IN UINT32 DataLength,
  577. IN VOID *Data OPTIONAL
  578. )
  579. {
  580. EFI_HTTP_MESSAGE *HttpMessage;
  581. EFI_HTTP_HEADER *HttpHeader;
  582. HTTP_BOOT_PRIVATE_DATA *Private;
  583. UINT32 Percentage;
  584. Private = HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_PROTOCOL(This);
  585. switch (DataType) {
  586. case HttpBootDhcp4:
  587. case HttpBootDhcp6:
  588. Print (L".");
  589. break;
  590. case HttpBootHttpRequest:
  591. if (Data != NULL) {
  592. HttpMessage = (EFI_HTTP_MESSAGE *) Data;
  593. if (HttpMessage->Data.Request->Method == HttpMethodGet &&
  594. HttpMessage->Data.Request->Url != NULL) {
  595. Print (L"\n URI: %s\n", HttpMessage->Data.Request->Url);
  596. }
  597. }
  598. break;
  599. case HttpBootHttpResponse:
  600. if (Data != NULL) {
  601. HttpMessage = (EFI_HTTP_MESSAGE *) Data;
  602. if (HttpMessage->Data.Response != NULL) {
  603. if (HttpBootIsHttpRedirectStatusCode (HttpMessage->Data.Response->StatusCode)) {
  604. //
  605. // Server indicates the resource has been redirected to a different URL
  606. // according to the section 6.4 of RFC7231 and the RFC 7538.
  607. // Display the redirect information on the screen.
  608. //
  609. HttpHeader = HttpFindHeader (
  610. HttpMessage->HeaderCount,
  611. HttpMessage->Headers,
  612. HTTP_HEADER_LOCATION
  613. );
  614. if (HttpHeader != NULL) {
  615. Print (L"\n HTTP ERROR: Resource Redirected.\n New Location: %a\n", HttpHeader->FieldValue);
  616. }
  617. break;
  618. }
  619. }
  620. HttpHeader = HttpFindHeader (
  621. HttpMessage->HeaderCount,
  622. HttpMessage->Headers,
  623. HTTP_HEADER_CONTENT_LENGTH
  624. );
  625. if (HttpHeader != NULL) {
  626. Private->FileSize = AsciiStrDecimalToUintn (HttpHeader->FieldValue);
  627. Private->ReceivedSize = 0;
  628. Private->Percentage = 0;
  629. }
  630. }
  631. break;
  632. case HttpBootHttpEntityBody:
  633. if (DataLength != 0) {
  634. if (Private->FileSize != 0) {
  635. //
  636. // We already know the file size, print in percentage format.
  637. //
  638. if (Private->ReceivedSize == 0) {
  639. Print (L" File Size: %lu Bytes\n", Private->FileSize);
  640. }
  641. Private->ReceivedSize += DataLength;
  642. Percentage = (UINT32) DivU64x64Remainder (MultU64x32 (Private->ReceivedSize, 100), Private->FileSize, NULL);
  643. if (Private->Percentage != Percentage) {
  644. Private->Percentage = Percentage;
  645. Print (L"\r Downloading...%d%%", Percentage);
  646. }
  647. } else {
  648. //
  649. // In some case we couldn't get the file size from the HTTP header, so we
  650. // just print the downloaded file size.
  651. //
  652. Private->ReceivedSize += DataLength;
  653. Print (L"\r Downloading...%lu Bytes", Private->ReceivedSize);
  654. }
  655. }
  656. break;
  657. default:
  658. break;
  659. };
  660. return EFI_SUCCESS;
  661. }
  662. ///
  663. /// HTTP Boot Callback Protocol instance
  664. ///
  665. GLOBAL_REMOVE_IF_UNREFERENCED
  666. EFI_HTTP_BOOT_CALLBACK_PROTOCOL gHttpBootDxeHttpBootCallback = {
  667. HttpBootCallback
  668. };