HttpBootClient.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. /** @file
  2. Implementation of the boot file download function.
  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. Update the device path node to include the boot resource information.
  10. @param[in] Private The pointer to the driver's private data.
  11. @retval EFI_SUCCESS Device patch successfully updated.
  12. @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
  13. @retval Others Unexpected error happened.
  14. **/
  15. EFI_STATUS
  16. HttpBootUpdateDevicePath (
  17. IN HTTP_BOOT_PRIVATE_DATA *Private
  18. )
  19. {
  20. EFI_DEV_PATH *Node;
  21. EFI_DEVICE_PATH_PROTOCOL *TmpIpDevicePath;
  22. EFI_DEVICE_PATH_PROTOCOL *TmpDnsDevicePath;
  23. EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
  24. UINTN Length;
  25. EFI_STATUS Status;
  26. TmpIpDevicePath = NULL;
  27. TmpDnsDevicePath = NULL;
  28. //
  29. // Update the IP node with DHCP assigned information.
  30. //
  31. if (!Private->UsingIpv6) {
  32. Node = AllocateZeroPool (sizeof (IPv4_DEVICE_PATH));
  33. if (Node == NULL) {
  34. return EFI_OUT_OF_RESOURCES;
  35. }
  36. Node->Ipv4.Header.Type = MESSAGING_DEVICE_PATH;
  37. Node->Ipv4.Header.SubType = MSG_IPv4_DP;
  38. SetDevicePathNodeLength (Node, sizeof (IPv4_DEVICE_PATH));
  39. CopyMem (&Node->Ipv4.LocalIpAddress, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));
  40. Node->Ipv4.RemotePort = Private->Port;
  41. Node->Ipv4.Protocol = EFI_IP_PROTO_TCP;
  42. Node->Ipv4.StaticIpAddress = FALSE;
  43. CopyMem (&Node->Ipv4.GatewayIpAddress, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS));
  44. CopyMem (&Node->Ipv4.SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));
  45. } else {
  46. Node = AllocateZeroPool (sizeof (IPv6_DEVICE_PATH));
  47. if (Node == NULL) {
  48. return EFI_OUT_OF_RESOURCES;
  49. }
  50. Node->Ipv6.Header.Type = MESSAGING_DEVICE_PATH;
  51. Node->Ipv6.Header.SubType = MSG_IPv6_DP;
  52. SetDevicePathNodeLength (Node, sizeof (IPv6_DEVICE_PATH));
  53. Node->Ipv6.PrefixLength = IP6_PREFIX_LENGTH;
  54. Node->Ipv6.RemotePort = Private->Port;
  55. Node->Ipv6.Protocol = EFI_IP_PROTO_TCP;
  56. Node->Ipv6.IpAddressOrigin = 0;
  57. CopyMem (&Node->Ipv6.LocalIpAddress, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));
  58. CopyMem (&Node->Ipv6.RemoteIpAddress, &Private->ServerIp.v6, sizeof (EFI_IPv6_ADDRESS));
  59. CopyMem (&Node->Ipv6.GatewayIpAddress, &Private->GatewayIp.v6, sizeof (EFI_IPv6_ADDRESS));
  60. }
  61. TmpIpDevicePath = AppendDevicePathNode (Private->ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);
  62. FreePool (Node);
  63. if (TmpIpDevicePath == NULL) {
  64. return EFI_OUT_OF_RESOURCES;
  65. }
  66. //
  67. // Update the DNS node with DNS server IP list if existed.
  68. //
  69. if (Private->DnsServerIp != NULL) {
  70. Length = sizeof (EFI_DEVICE_PATH_PROTOCOL) + sizeof (Node->Dns.IsIPv6) + Private->DnsServerCount * sizeof (EFI_IP_ADDRESS);
  71. Node = AllocatePool (Length);
  72. if (Node == NULL) {
  73. FreePool (TmpIpDevicePath);
  74. return EFI_OUT_OF_RESOURCES;
  75. }
  76. Node->DevPath.Type = MESSAGING_DEVICE_PATH;
  77. Node->DevPath.SubType = MSG_DNS_DP;
  78. SetDevicePathNodeLength (Node, Length);
  79. Node->Dns.IsIPv6 = Private->UsingIpv6 ? 0x01 : 0x00;
  80. CopyMem ((UINT8*) Node + sizeof (EFI_DEVICE_PATH_PROTOCOL) + sizeof (Node->Dns.IsIPv6), Private->DnsServerIp, Private->DnsServerCount * sizeof (EFI_IP_ADDRESS));
  81. TmpDnsDevicePath = AppendDevicePathNode (TmpIpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);
  82. FreePool (Node);
  83. FreePool (TmpIpDevicePath);
  84. TmpIpDevicePath = NULL;
  85. if (TmpDnsDevicePath == NULL) {
  86. return EFI_OUT_OF_RESOURCES;
  87. }
  88. }
  89. //
  90. // Update the URI node with the boot file URI.
  91. //
  92. Length = sizeof (EFI_DEVICE_PATH_PROTOCOL) + AsciiStrSize (Private->BootFileUri);
  93. Node = AllocatePool (Length);
  94. if (Node == NULL) {
  95. if (TmpIpDevicePath != NULL) {
  96. FreePool (TmpIpDevicePath);
  97. }
  98. if (TmpDnsDevicePath != NULL) {
  99. FreePool (TmpDnsDevicePath);
  100. }
  101. return EFI_OUT_OF_RESOURCES;
  102. }
  103. Node->DevPath.Type = MESSAGING_DEVICE_PATH;
  104. Node->DevPath.SubType = MSG_URI_DP;
  105. SetDevicePathNodeLength (Node, Length);
  106. CopyMem ((UINT8*) Node + sizeof (EFI_DEVICE_PATH_PROTOCOL), Private->BootFileUri, AsciiStrSize (Private->BootFileUri));
  107. if (TmpDnsDevicePath != NULL) {
  108. NewDevicePath = AppendDevicePathNode (TmpDnsDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);
  109. FreePool (TmpDnsDevicePath);
  110. } else {
  111. ASSERT (TmpIpDevicePath != NULL);
  112. NewDevicePath = AppendDevicePathNode (TmpIpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);
  113. FreePool (TmpIpDevicePath);
  114. }
  115. FreePool (Node);
  116. if (NewDevicePath == NULL) {
  117. return EFI_OUT_OF_RESOURCES;
  118. }
  119. if (!Private->UsingIpv6) {
  120. //
  121. // Reinstall the device path protocol of the child handle.
  122. //
  123. Status = gBS->ReinstallProtocolInterface (
  124. Private->Ip4Nic->Controller,
  125. &gEfiDevicePathProtocolGuid,
  126. Private->Ip4Nic->DevicePath,
  127. NewDevicePath
  128. );
  129. if (EFI_ERROR (Status)) {
  130. return Status;
  131. }
  132. FreePool (Private->Ip4Nic->DevicePath);
  133. Private->Ip4Nic->DevicePath = NewDevicePath;
  134. } else {
  135. //
  136. // Reinstall the device path protocol of the child handle.
  137. //
  138. Status = gBS->ReinstallProtocolInterface (
  139. Private->Ip6Nic->Controller,
  140. &gEfiDevicePathProtocolGuid,
  141. Private->Ip6Nic->DevicePath,
  142. NewDevicePath
  143. );
  144. if (EFI_ERROR (Status)) {
  145. return Status;
  146. }
  147. FreePool (Private->Ip6Nic->DevicePath);
  148. Private->Ip6Nic->DevicePath = NewDevicePath;
  149. }
  150. return EFI_SUCCESS;
  151. }
  152. /**
  153. Parse the boot file URI information from the selected Dhcp4 offer packet.
  154. @param[in] Private The pointer to the driver's private data.
  155. @retval EFI_SUCCESS Successfully parsed out all the boot information.
  156. @retval Others Failed to parse out the boot information.
  157. **/
  158. EFI_STATUS
  159. HttpBootDhcp4ExtractUriInfo (
  160. IN HTTP_BOOT_PRIVATE_DATA *Private
  161. )
  162. {
  163. HTTP_BOOT_DHCP4_PACKET_CACHE *SelectOffer;
  164. HTTP_BOOT_DHCP4_PACKET_CACHE *HttpOffer;
  165. UINT32 SelectIndex;
  166. UINT32 ProxyIndex;
  167. UINT32 DnsServerIndex;
  168. EFI_DHCP4_PACKET_OPTION *Option;
  169. EFI_STATUS Status;
  170. ASSERT (Private != NULL);
  171. ASSERT (Private->SelectIndex != 0);
  172. SelectIndex = Private->SelectIndex - 1;
  173. ASSERT (SelectIndex < HTTP_BOOT_OFFER_MAX_NUM);
  174. DnsServerIndex = 0;
  175. Status = EFI_SUCCESS;
  176. //
  177. // SelectOffer contains the IP address configuration and name server configuration.
  178. // HttpOffer contains the boot file URL.
  179. //
  180. SelectOffer = &Private->OfferBuffer[SelectIndex].Dhcp4;
  181. if (Private->FilePathUri == NULL) {
  182. //
  183. // In Corporate environment, we need a HttpOffer.
  184. //
  185. if ((SelectOffer->OfferType == HttpOfferTypeDhcpIpUri) ||
  186. (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns) ||
  187. (SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns)) {
  188. HttpOffer = SelectOffer;
  189. } else {
  190. ASSERT (Private->SelectProxyType != HttpOfferTypeMax);
  191. ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];
  192. HttpOffer = &Private->OfferBuffer[ProxyIndex].Dhcp4;
  193. }
  194. Private->BootFileUriParser = HttpOffer->UriParser;
  195. Private->BootFileUri = (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data;
  196. } else {
  197. //
  198. // In Home environment the BootFileUri comes from the FilePath.
  199. //
  200. Private->BootFileUriParser = Private->FilePathUriParser;
  201. Private->BootFileUri = Private->FilePathUri;
  202. }
  203. //
  204. // Check the URI scheme.
  205. //
  206. Status = HttpBootCheckUriScheme (Private->BootFileUri);
  207. if (EFI_ERROR (Status)) {
  208. DEBUG ((EFI_D_ERROR, "HttpBootDhcp4ExtractUriInfo: %r.\n", Status));
  209. if (Status == EFI_INVALID_PARAMETER) {
  210. AsciiPrint ("\n Error: Invalid URI address.\n");
  211. } else if (Status == EFI_ACCESS_DENIED) {
  212. AsciiPrint ("\n Error: Access forbidden, only HTTPS connection is allowed.\n");
  213. }
  214. return Status;
  215. }
  216. if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) ||
  217. (SelectOffer->OfferType == HttpOfferTypeDhcpDns) ||
  218. (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns)) {
  219. Option = SelectOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER];
  220. ASSERT (Option != NULL);
  221. //
  222. // Record the Dns Server address list.
  223. //
  224. Private->DnsServerCount = (Option->Length) / sizeof (EFI_IPv4_ADDRESS);
  225. Private->DnsServerIp = AllocateZeroPool (Private->DnsServerCount * sizeof (EFI_IP_ADDRESS));
  226. if (Private->DnsServerIp == NULL) {
  227. return EFI_OUT_OF_RESOURCES;
  228. }
  229. for (DnsServerIndex = 0; DnsServerIndex < Private->DnsServerCount; DnsServerIndex++) {
  230. CopyMem (&(Private->DnsServerIp[DnsServerIndex].v4), &(((EFI_IPv4_ADDRESS *) Option->Data)[DnsServerIndex]), sizeof (EFI_IPv4_ADDRESS));
  231. }
  232. //
  233. // Configure the default DNS server if server assigned.
  234. //
  235. Status = HttpBootRegisterIp4Dns (
  236. Private,
  237. Option->Length,
  238. Option->Data
  239. );
  240. if (EFI_ERROR (Status)) {
  241. FreePool (Private->DnsServerIp);
  242. Private->DnsServerIp = NULL;
  243. return Status;
  244. }
  245. }
  246. //
  247. // Extract the port from URL, and use default HTTP port 80 if not provided.
  248. //
  249. Status = HttpUrlGetPort (
  250. Private->BootFileUri,
  251. Private->BootFileUriParser,
  252. &Private->Port
  253. );
  254. if (EFI_ERROR (Status) || Private->Port == 0) {
  255. Private->Port = 80;
  256. }
  257. //
  258. // All boot informations are valid here.
  259. //
  260. //
  261. // Update the device path to include the boot resource information.
  262. //
  263. Status = HttpBootUpdateDevicePath (Private);
  264. if (EFI_ERROR (Status) && Private->DnsServerIp != NULL) {
  265. FreePool (Private->DnsServerIp);
  266. Private->DnsServerIp = NULL;
  267. }
  268. return Status;
  269. }
  270. /**
  271. Parse the boot file URI information from the selected Dhcp6 offer packet.
  272. @param[in] Private The pointer to the driver's private data.
  273. @retval EFI_SUCCESS Successfully parsed out all the boot information.
  274. @retval Others Failed to parse out the boot information.
  275. **/
  276. EFI_STATUS
  277. HttpBootDhcp6ExtractUriInfo (
  278. IN HTTP_BOOT_PRIVATE_DATA *Private
  279. )
  280. {
  281. HTTP_BOOT_DHCP6_PACKET_CACHE *SelectOffer;
  282. HTTP_BOOT_DHCP6_PACKET_CACHE *HttpOffer;
  283. UINT32 SelectIndex;
  284. UINT32 ProxyIndex;
  285. UINT32 DnsServerIndex;
  286. EFI_DHCP6_PACKET_OPTION *Option;
  287. EFI_IPv6_ADDRESS IpAddr;
  288. CHAR8 *HostName;
  289. UINTN HostNameSize;
  290. CHAR16 *HostNameStr;
  291. EFI_STATUS Status;
  292. ASSERT (Private != NULL);
  293. ASSERT (Private->SelectIndex != 0);
  294. SelectIndex = Private->SelectIndex - 1;
  295. ASSERT (SelectIndex < HTTP_BOOT_OFFER_MAX_NUM);
  296. DnsServerIndex = 0;
  297. Status = EFI_SUCCESS;
  298. HostName = NULL;
  299. //
  300. // SelectOffer contains the IP address configuration and name server configuration.
  301. // HttpOffer contains the boot file URL.
  302. //
  303. SelectOffer = &Private->OfferBuffer[SelectIndex].Dhcp6;
  304. if (Private->FilePathUri == NULL) {
  305. //
  306. // In Corporate environment, we need a HttpOffer.
  307. //
  308. if ((SelectOffer->OfferType == HttpOfferTypeDhcpIpUri) ||
  309. (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns) ||
  310. (SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns)) {
  311. HttpOffer = SelectOffer;
  312. } else {
  313. ASSERT (Private->SelectProxyType != HttpOfferTypeMax);
  314. ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];
  315. HttpOffer = &Private->OfferBuffer[ProxyIndex].Dhcp6;
  316. }
  317. Private->BootFileUriParser = HttpOffer->UriParser;
  318. Private->BootFileUri = (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data;
  319. } else {
  320. //
  321. // In Home environment the BootFileUri comes from the FilePath.
  322. //
  323. Private->BootFileUriParser = Private->FilePathUriParser;
  324. Private->BootFileUri = Private->FilePathUri;
  325. }
  326. //
  327. // Check the URI scheme.
  328. //
  329. Status = HttpBootCheckUriScheme (Private->BootFileUri);
  330. if (EFI_ERROR (Status)) {
  331. DEBUG ((EFI_D_ERROR, "HttpBootDhcp6ExtractUriInfo: %r.\n", Status));
  332. if (Status == EFI_INVALID_PARAMETER) {
  333. AsciiPrint ("\n Error: Invalid URI address.\n");
  334. } else if (Status == EFI_ACCESS_DENIED) {
  335. AsciiPrint ("\n Error: Access forbidden, only HTTPS connection is allowed.\n");
  336. }
  337. return Status;
  338. }
  339. //
  340. // Set the Local station address to IP layer.
  341. //
  342. Status = HttpBootSetIp6Address (Private);
  343. if (EFI_ERROR (Status)) {
  344. return Status;
  345. }
  346. //
  347. // Register the IPv6 gateway address to the network device.
  348. //
  349. Status = HttpBootSetIp6Gateway (Private);
  350. if (EFI_ERROR (Status)) {
  351. return Status;
  352. }
  353. if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) ||
  354. (SelectOffer->OfferType == HttpOfferTypeDhcpDns) ||
  355. (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns)) {
  356. Option = SelectOffer->OptList[HTTP_BOOT_DHCP6_IDX_DNS_SERVER];
  357. ASSERT (Option != NULL);
  358. //
  359. // Record the Dns Server address list.
  360. //
  361. Private->DnsServerCount = HTONS (Option->OpLen) / sizeof (EFI_IPv6_ADDRESS);
  362. Private->DnsServerIp = AllocateZeroPool (Private->DnsServerCount * sizeof (EFI_IP_ADDRESS));
  363. if (Private->DnsServerIp == NULL) {
  364. return EFI_OUT_OF_RESOURCES;
  365. }
  366. for (DnsServerIndex = 0; DnsServerIndex < Private->DnsServerCount; DnsServerIndex++) {
  367. CopyMem (&(Private->DnsServerIp[DnsServerIndex].v6), &(((EFI_IPv6_ADDRESS *) Option->Data)[DnsServerIndex]), sizeof (EFI_IPv6_ADDRESS));
  368. }
  369. //
  370. // Configure the default DNS server if server assigned.
  371. //
  372. Status = HttpBootSetIp6Dns (
  373. Private,
  374. HTONS (Option->OpLen),
  375. Option->Data
  376. );
  377. if (EFI_ERROR (Status)) {
  378. goto Error;
  379. }
  380. }
  381. //
  382. // Extract the HTTP server Ip from URL. This is used to Check route table
  383. // whether can send message to HTTP Server Ip through the GateWay.
  384. //
  385. Status = HttpUrlGetIp6 (
  386. Private->BootFileUri,
  387. Private->BootFileUriParser,
  388. &IpAddr
  389. );
  390. if (EFI_ERROR (Status)) {
  391. //
  392. // The Http server address is expressed by Name Ip, so perform DNS resolution
  393. //
  394. Status = HttpUrlGetHostName (
  395. Private->BootFileUri,
  396. Private->BootFileUriParser,
  397. &HostName
  398. );
  399. if (EFI_ERROR (Status)) {
  400. goto Error;
  401. }
  402. HostNameSize = AsciiStrSize (HostName);
  403. HostNameStr = AllocateZeroPool (HostNameSize * sizeof (CHAR16));
  404. if (HostNameStr == NULL) {
  405. Status = EFI_OUT_OF_RESOURCES;
  406. goto Error;
  407. }
  408. AsciiStrToUnicodeStrS (HostName, HostNameStr, HostNameSize);
  409. if (HostName != NULL) {
  410. FreePool (HostName);
  411. }
  412. Status = HttpBootDns (Private, HostNameStr, &IpAddr);
  413. FreePool (HostNameStr);
  414. if (EFI_ERROR (Status)) {
  415. AsciiPrint ("\n Error: Could not retrieve the host address from DNS server.\n");
  416. goto Error;
  417. }
  418. }
  419. CopyMem (&Private->ServerIp.v6, &IpAddr, sizeof (EFI_IPv6_ADDRESS));
  420. //
  421. // Extract the port from URL, and use default HTTP port 80 if not provided.
  422. //
  423. Status = HttpUrlGetPort (
  424. Private->BootFileUri,
  425. Private->BootFileUriParser,
  426. &Private->Port
  427. );
  428. if (EFI_ERROR (Status) || Private->Port == 0) {
  429. Private->Port = 80;
  430. }
  431. //
  432. // All boot informations are valid here.
  433. //
  434. //
  435. // Update the device path to include the boot resource information.
  436. //
  437. Status = HttpBootUpdateDevicePath (Private);
  438. if (EFI_ERROR (Status)) {
  439. goto Error;
  440. }
  441. return Status;
  442. Error:
  443. if (Private->DnsServerIp != NULL) {
  444. FreePool (Private->DnsServerIp);
  445. Private->DnsServerIp = NULL;
  446. }
  447. return Status;
  448. }
  449. /**
  450. Discover all the boot information for boot file.
  451. @param[in, out] Private The pointer to the driver's private data.
  452. @retval EFI_SUCCESS Successfully obtained all the boot information .
  453. @retval Others Failed to retrieve the boot information.
  454. **/
  455. EFI_STATUS
  456. HttpBootDiscoverBootInfo (
  457. IN OUT HTTP_BOOT_PRIVATE_DATA *Private
  458. )
  459. {
  460. EFI_STATUS Status;
  461. //
  462. // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and
  463. // other Http boot information.
  464. //
  465. Status = HttpBootDhcp (Private);
  466. if (EFI_ERROR (Status)) {
  467. return Status;
  468. }
  469. if (!Private->UsingIpv6) {
  470. Status = HttpBootDhcp4ExtractUriInfo (Private);
  471. } else {
  472. Status = HttpBootDhcp6ExtractUriInfo (Private);
  473. }
  474. return Status;
  475. }
  476. /**
  477. HttpIo Callback function which will be invoked when specified HTTP_IO_CALLBACK_EVENT happened.
  478. @param[in] EventType Indicate the Event type that occurs in the current callback.
  479. @param[in] Message HTTP message which will be send to, or just received from HTTP server.
  480. @param[in] Context The Callback Context pointer.
  481. @retval EFI_SUCCESS Tells the HttpIo to continue the HTTP process.
  482. @retval Others Tells the HttpIo to abort the current HTTP process.
  483. **/
  484. EFI_STATUS
  485. EFIAPI
  486. HttpBootHttpIoCallback (
  487. IN HTTP_IO_CALLBACK_EVENT EventType,
  488. IN EFI_HTTP_MESSAGE *Message,
  489. IN VOID *Context
  490. )
  491. {
  492. HTTP_BOOT_PRIVATE_DATA *Private;
  493. EFI_STATUS Status;
  494. Private = (HTTP_BOOT_PRIVATE_DATA *) Context;
  495. if (Private->HttpBootCallback != NULL) {
  496. Status = Private->HttpBootCallback->Callback (
  497. Private->HttpBootCallback,
  498. EventType == HttpIoRequest ? HttpBootHttpRequest : HttpBootHttpResponse,
  499. EventType == HttpIoRequest ? FALSE : TRUE,
  500. sizeof (EFI_HTTP_MESSAGE),
  501. (VOID *) Message
  502. );
  503. return Status;
  504. }
  505. return EFI_SUCCESS;
  506. }
  507. /**
  508. Create a HttpIo instance for the file download.
  509. @param[in] Private The pointer to the driver's private data.
  510. @retval EFI_SUCCESS Successfully created.
  511. @retval Others Failed to create HttpIo.
  512. **/
  513. EFI_STATUS
  514. HttpBootCreateHttpIo (
  515. IN HTTP_BOOT_PRIVATE_DATA *Private
  516. )
  517. {
  518. HTTP_IO_CONFIG_DATA ConfigData;
  519. EFI_STATUS Status;
  520. EFI_HANDLE ImageHandle;
  521. ASSERT (Private != NULL);
  522. ZeroMem (&ConfigData, sizeof (HTTP_IO_CONFIG_DATA));
  523. if (!Private->UsingIpv6) {
  524. ConfigData.Config4.HttpVersion = HttpVersion11;
  525. ConfigData.Config4.RequestTimeOut = HTTP_BOOT_REQUEST_TIMEOUT;
  526. IP4_COPY_ADDRESS (&ConfigData.Config4.LocalIp, &Private->StationIp.v4);
  527. IP4_COPY_ADDRESS (&ConfigData.Config4.SubnetMask, &Private->SubnetMask.v4);
  528. ImageHandle = Private->Ip4Nic->ImageHandle;
  529. } else {
  530. ConfigData.Config6.HttpVersion = HttpVersion11;
  531. ConfigData.Config6.RequestTimeOut = HTTP_BOOT_REQUEST_TIMEOUT;
  532. IP6_COPY_ADDRESS (&ConfigData.Config6.LocalIp, &Private->StationIp.v6);
  533. ImageHandle = Private->Ip6Nic->ImageHandle;
  534. }
  535. Status = HttpIoCreateIo (
  536. ImageHandle,
  537. Private->Controller,
  538. Private->UsingIpv6 ? IP_VERSION_6 : IP_VERSION_4,
  539. &ConfigData,
  540. HttpBootHttpIoCallback,
  541. (VOID *) Private,
  542. &Private->HttpIo
  543. );
  544. if (EFI_ERROR (Status)) {
  545. return Status;
  546. }
  547. Private->HttpCreated = TRUE;
  548. return EFI_SUCCESS;
  549. }
  550. /**
  551. Release all the resource of a cache item.
  552. @param[in] Cache The pointer to the cache item.
  553. **/
  554. VOID
  555. HttpBootFreeCache (
  556. IN HTTP_BOOT_CACHE_CONTENT *Cache
  557. )
  558. {
  559. UINTN Index;
  560. LIST_ENTRY *Entry;
  561. LIST_ENTRY *NextEntry;
  562. HTTP_BOOT_ENTITY_DATA *EntityData;
  563. if (Cache != NULL) {
  564. //
  565. // Free the request data
  566. //
  567. if (Cache->RequestData != NULL) {
  568. if (Cache->RequestData->Url != NULL) {
  569. FreePool (Cache->RequestData->Url);
  570. }
  571. FreePool (Cache->RequestData);
  572. }
  573. //
  574. // Free the response header
  575. //
  576. if (Cache->ResponseData != NULL) {
  577. if (Cache->ResponseData->Headers != NULL) {
  578. for (Index = 0; Index < Cache->ResponseData->HeaderCount; Index++) {
  579. FreePool (Cache->ResponseData->Headers[Index].FieldName);
  580. FreePool (Cache->ResponseData->Headers[Index].FieldValue);
  581. }
  582. FreePool (Cache->ResponseData->Headers);
  583. }
  584. }
  585. //
  586. // Free the response body
  587. //
  588. NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Cache->EntityDataList) {
  589. EntityData = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_ENTITY_DATA, Link);
  590. if (EntityData->Block != NULL) {
  591. FreePool (EntityData->Block);
  592. }
  593. RemoveEntryList (&EntityData->Link);
  594. FreePool (EntityData);
  595. }
  596. FreePool (Cache);
  597. }
  598. }
  599. /**
  600. Clean up all cached data.
  601. @param[in] Private The pointer to the driver's private data.
  602. **/
  603. VOID
  604. HttpBootFreeCacheList (
  605. IN HTTP_BOOT_PRIVATE_DATA *Private
  606. )
  607. {
  608. LIST_ENTRY *Entry;
  609. LIST_ENTRY *NextEntry;
  610. HTTP_BOOT_CACHE_CONTENT *Cache;
  611. NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->CacheList) {
  612. Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);
  613. RemoveEntryList (&Cache->Link);
  614. HttpBootFreeCache (Cache);
  615. }
  616. }
  617. /**
  618. Get the file content from cached data.
  619. @param[in] Private The pointer to the driver's private data.
  620. @param[in] Uri Uri of the file to be retrieved from cache.
  621. @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
  622. code of EFI_SUCCESS, the amount of data transferred to
  623. Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
  624. the size of Buffer required to retrieve the requested file.
  625. @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
  626. then the size of the requested file is returned in
  627. BufferSize.
  628. @param[out] ImageType The image type of the downloaded file.
  629. @retval EFI_SUCCESS Successfully created.
  630. @retval Others Failed to create HttpIo.
  631. **/
  632. EFI_STATUS
  633. HttpBootGetFileFromCache (
  634. IN HTTP_BOOT_PRIVATE_DATA *Private,
  635. IN CHAR16 *Uri,
  636. IN OUT UINTN *BufferSize,
  637. OUT UINT8 *Buffer,
  638. OUT HTTP_BOOT_IMAGE_TYPE *ImageType
  639. )
  640. {
  641. LIST_ENTRY *Entry;
  642. LIST_ENTRY *Entry2;
  643. HTTP_BOOT_CACHE_CONTENT *Cache;
  644. HTTP_BOOT_ENTITY_DATA *EntityData;
  645. UINTN CopyedSize;
  646. if (Uri == NULL || BufferSize == NULL || Buffer == NULL || ImageType == NULL) {
  647. return EFI_INVALID_PARAMETER;
  648. }
  649. NET_LIST_FOR_EACH (Entry, &Private->CacheList) {
  650. Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);
  651. //
  652. // Compare the URI to see whether we already have a cache for this file.
  653. //
  654. if ((Cache->RequestData != NULL) &&
  655. (Cache->RequestData->Url != NULL) &&
  656. (StrCmp (Uri, Cache->RequestData->Url) == 0)) {
  657. //
  658. // Hit in cache, record image type.
  659. //
  660. *ImageType = Cache->ImageType;
  661. //
  662. // Check buffer size.
  663. //
  664. if (*BufferSize < Cache->EntityLength) {
  665. *BufferSize = Cache->EntityLength;
  666. return EFI_BUFFER_TOO_SMALL;
  667. }
  668. //
  669. // Fill data to buffer.
  670. //
  671. CopyedSize = 0;
  672. NET_LIST_FOR_EACH (Entry2, &Cache->EntityDataList) {
  673. EntityData = NET_LIST_USER_STRUCT (Entry2, HTTP_BOOT_ENTITY_DATA, Link);
  674. if (*BufferSize > CopyedSize) {
  675. CopyMem (
  676. Buffer + CopyedSize,
  677. EntityData->DataStart,
  678. MIN (EntityData->DataLength, *BufferSize - CopyedSize)
  679. );
  680. CopyedSize += MIN (EntityData->DataLength, *BufferSize - CopyedSize);
  681. }
  682. }
  683. *BufferSize = CopyedSize;
  684. return EFI_SUCCESS;
  685. }
  686. }
  687. return EFI_NOT_FOUND;
  688. }
  689. /**
  690. A callback function to intercept events during message parser.
  691. This function will be invoked during HttpParseMessageBody() with various events type. An error
  692. return status of the callback function will cause the HttpParseMessageBody() aborted.
  693. @param[in] EventType Event type of this callback call.
  694. @param[in] Data A pointer to data buffer.
  695. @param[in] Length Length in bytes of the Data.
  696. @param[in] Context Callback context set by HttpInitMsgParser().
  697. @retval EFI_SUCCESS Continue to parser the message body.
  698. @retval Others Abort the parse.
  699. **/
  700. EFI_STATUS
  701. EFIAPI
  702. HttpBootGetBootFileCallback (
  703. IN HTTP_BODY_PARSE_EVENT EventType,
  704. IN CHAR8 *Data,
  705. IN UINTN Length,
  706. IN VOID *Context
  707. )
  708. {
  709. HTTP_BOOT_CALLBACK_DATA *CallbackData;
  710. HTTP_BOOT_ENTITY_DATA *NewEntityData;
  711. EFI_STATUS Status;
  712. EFI_HTTP_BOOT_CALLBACK_PROTOCOL *HttpBootCallback;
  713. //
  714. // We only care about the entity data.
  715. //
  716. if (EventType != BodyParseEventOnData) {
  717. return EFI_SUCCESS;
  718. }
  719. CallbackData = (HTTP_BOOT_CALLBACK_DATA *) Context;
  720. HttpBootCallback = CallbackData->Private->HttpBootCallback;
  721. if (HttpBootCallback != NULL) {
  722. Status = HttpBootCallback->Callback (
  723. HttpBootCallback,
  724. HttpBootHttpEntityBody,
  725. TRUE,
  726. (UINT32)Length,
  727. Data
  728. );
  729. if (EFI_ERROR (Status)) {
  730. return Status;
  731. }
  732. }
  733. //
  734. // Copy data if caller has provided a buffer.
  735. //
  736. if (CallbackData->BufferSize > CallbackData->CopyedSize) {
  737. CopyMem (
  738. CallbackData->Buffer + CallbackData->CopyedSize,
  739. Data,
  740. MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize)
  741. );
  742. CallbackData->CopyedSize += MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize);
  743. }
  744. //
  745. // The caller doesn't provide a buffer, save the block into cache list.
  746. //
  747. if (CallbackData->Cache != NULL) {
  748. NewEntityData = AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA));
  749. if (NewEntityData == NULL) {
  750. return EFI_OUT_OF_RESOURCES;
  751. }
  752. if (CallbackData->NewBlock) {
  753. NewEntityData->Block = CallbackData->Block;
  754. CallbackData->Block = NULL;
  755. }
  756. NewEntityData->DataLength = Length;
  757. NewEntityData->DataStart = (UINT8*) Data;
  758. InsertTailList (&CallbackData->Cache->EntityDataList, &NewEntityData->Link);
  759. }
  760. return EFI_SUCCESS;
  761. }
  762. /**
  763. This function download the boot file by using UEFI HTTP protocol.
  764. @param[in] Private The pointer to the driver's private data.
  765. @param[in] HeaderOnly Only request the response header, it could save a lot of time if
  766. the caller only want to know the size of the requested file.
  767. @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
  768. code of EFI_SUCCESS, the amount of data transferred to
  769. Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
  770. the size of Buffer required to retrieve the requested file.
  771. @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
  772. then the size of the requested file is returned in
  773. BufferSize.
  774. @param[out] ImageType The image type of the downloaded file.
  775. @retval EFI_SUCCESS The file was loaded.
  776. @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.
  777. @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
  778. @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
  779. BufferSize has been updated with the size needed to complete
  780. the request.
  781. @retval Others Unexpected error happened.
  782. **/
  783. EFI_STATUS
  784. HttpBootGetBootFile (
  785. IN HTTP_BOOT_PRIVATE_DATA *Private,
  786. IN BOOLEAN HeaderOnly,
  787. IN OUT UINTN *BufferSize,
  788. OUT UINT8 *Buffer,
  789. OUT HTTP_BOOT_IMAGE_TYPE *ImageType
  790. )
  791. {
  792. EFI_STATUS Status;
  793. EFI_HTTP_STATUS_CODE StatusCode;
  794. CHAR8 *HostName;
  795. EFI_HTTP_REQUEST_DATA *RequestData;
  796. HTTP_IO_RESPONSE_DATA *ResponseData;
  797. HTTP_IO_RESPONSE_DATA ResponseBody;
  798. HTTP_IO *HttpIo;
  799. HTTP_IO_HEADER *HttpIoHeader;
  800. VOID *Parser;
  801. HTTP_BOOT_CALLBACK_DATA Context;
  802. UINTN ContentLength;
  803. HTTP_BOOT_CACHE_CONTENT *Cache;
  804. UINT8 *Block;
  805. UINTN UrlSize;
  806. CHAR16 *Url;
  807. BOOLEAN IdentityMode;
  808. UINTN ReceivedSize;
  809. ASSERT (Private != NULL);
  810. ASSERT (Private->HttpCreated);
  811. if (BufferSize == NULL || ImageType == NULL) {
  812. return EFI_INVALID_PARAMETER;
  813. }
  814. if (*BufferSize != 0 && Buffer == NULL) {
  815. return EFI_INVALID_PARAMETER;
  816. }
  817. //
  818. // First, check whether we already cached the requested Uri.
  819. //
  820. UrlSize = AsciiStrSize (Private->BootFileUri);
  821. Url = AllocatePool (UrlSize * sizeof (CHAR16));
  822. if (Url == NULL) {
  823. return EFI_OUT_OF_RESOURCES;
  824. }
  825. AsciiStrToUnicodeStrS (Private->BootFileUri, Url, UrlSize);
  826. if (!HeaderOnly && Buffer != NULL) {
  827. Status = HttpBootGetFileFromCache (Private, Url, BufferSize, Buffer, ImageType);
  828. if (Status != EFI_NOT_FOUND) {
  829. FreePool (Url);
  830. return Status;
  831. }
  832. }
  833. //
  834. // Not found in cache, try to download it through HTTP.
  835. //
  836. //
  837. // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.
  838. //
  839. Cache = NULL;
  840. if ((!HeaderOnly) && (*BufferSize == 0)) {
  841. Cache = AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT));
  842. if (Cache == NULL) {
  843. Status = EFI_OUT_OF_RESOURCES;
  844. goto ERROR_1;
  845. }
  846. Cache->ImageType = ImageTypeMax;
  847. InitializeListHead (&Cache->EntityDataList);
  848. }
  849. //
  850. // 2. Send HTTP request message.
  851. //
  852. //
  853. // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:
  854. // Host
  855. // Accept
  856. // User-Agent
  857. //
  858. HttpIoHeader = HttpBootCreateHeader (3);
  859. if (HttpIoHeader == NULL) {
  860. Status = EFI_OUT_OF_RESOURCES;
  861. goto ERROR_2;
  862. }
  863. //
  864. // Add HTTP header field 1: Host
  865. //
  866. HostName = NULL;
  867. Status = HttpUrlGetHostName (
  868. Private->BootFileUri,
  869. Private->BootFileUriParser,
  870. &HostName
  871. );
  872. if (EFI_ERROR (Status)) {
  873. goto ERROR_3;
  874. }
  875. Status = HttpBootSetHeader (
  876. HttpIoHeader,
  877. HTTP_HEADER_HOST,
  878. HostName
  879. );
  880. FreePool (HostName);
  881. if (EFI_ERROR (Status)) {
  882. goto ERROR_3;
  883. }
  884. //
  885. // Add HTTP header field 2: Accept
  886. //
  887. Status = HttpBootSetHeader (
  888. HttpIoHeader,
  889. HTTP_HEADER_ACCEPT,
  890. "*/*"
  891. );
  892. if (EFI_ERROR (Status)) {
  893. goto ERROR_3;
  894. }
  895. //
  896. // Add HTTP header field 3: User-Agent
  897. //
  898. Status = HttpBootSetHeader (
  899. HttpIoHeader,
  900. HTTP_HEADER_USER_AGENT,
  901. HTTP_USER_AGENT_EFI_HTTP_BOOT
  902. );
  903. if (EFI_ERROR (Status)) {
  904. goto ERROR_3;
  905. }
  906. //
  907. // 2.2 Build the rest of HTTP request info.
  908. //
  909. RequestData = AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA));
  910. if (RequestData == NULL) {
  911. Status = EFI_OUT_OF_RESOURCES;
  912. goto ERROR_3;
  913. }
  914. RequestData->Method = HeaderOnly ? HttpMethodHead : HttpMethodGet;
  915. RequestData->Url = Url;
  916. //
  917. // 2.3 Record the request info in a temp cache item.
  918. //
  919. if (Cache != NULL) {
  920. Cache->RequestData = RequestData;
  921. }
  922. //
  923. // 2.4 Send out the request to HTTP server.
  924. //
  925. HttpIo = &Private->HttpIo;
  926. Status = HttpIoSendRequest (
  927. HttpIo,
  928. RequestData,
  929. HttpIoHeader->HeaderCount,
  930. HttpIoHeader->Headers,
  931. 0,
  932. NULL
  933. );
  934. if (EFI_ERROR (Status)) {
  935. goto ERROR_4;
  936. }
  937. //
  938. // 3. Receive HTTP response message.
  939. //
  940. //
  941. // 3.1 First step, use zero BodyLength to only receive the response headers.
  942. //
  943. ResponseData = AllocateZeroPool (sizeof(HTTP_IO_RESPONSE_DATA));
  944. if (ResponseData == NULL) {
  945. Status = EFI_OUT_OF_RESOURCES;
  946. goto ERROR_4;
  947. }
  948. Status = HttpIoRecvResponse (
  949. &Private->HttpIo,
  950. TRUE,
  951. ResponseData
  952. );
  953. if (EFI_ERROR (Status) || EFI_ERROR (ResponseData->Status)) {
  954. if (EFI_ERROR (ResponseData->Status)) {
  955. StatusCode = HttpIo->RspToken.Message->Data.Response->StatusCode;
  956. HttpBootPrintErrorMessage (StatusCode);
  957. Status = ResponseData->Status;
  958. }
  959. goto ERROR_5;
  960. }
  961. //
  962. // Check the image type according to server's response.
  963. //
  964. Status = HttpBootCheckImageType (
  965. Private->BootFileUri,
  966. Private->BootFileUriParser,
  967. ResponseData->HeaderCount,
  968. ResponseData->Headers,
  969. ImageType
  970. );
  971. if (EFI_ERROR (Status)) {
  972. goto ERROR_5;
  973. }
  974. //
  975. // 3.2 Cache the response header.
  976. //
  977. if (Cache != NULL) {
  978. Cache->ResponseData = ResponseData;
  979. Cache->ImageType = *ImageType;
  980. }
  981. //
  982. // 3.3 Init a message-body parser from the header information.
  983. //
  984. Parser = NULL;
  985. Context.NewBlock = FALSE;
  986. Context.Block = NULL;
  987. Context.CopyedSize = 0;
  988. Context.Buffer = Buffer;
  989. Context.BufferSize = *BufferSize;
  990. Context.Cache = Cache;
  991. Context.Private = Private;
  992. Status = HttpInitMsgParser (
  993. HeaderOnly ? HttpMethodHead : HttpMethodGet,
  994. ResponseData->Response.StatusCode,
  995. ResponseData->HeaderCount,
  996. ResponseData->Headers,
  997. HttpBootGetBootFileCallback,
  998. (VOID*) &Context,
  999. &Parser
  1000. );
  1001. if (EFI_ERROR (Status)) {
  1002. goto ERROR_6;
  1003. }
  1004. //
  1005. // 3.4 Continue to receive and parse message-body if needed.
  1006. //
  1007. Block = NULL;
  1008. if (!HeaderOnly) {
  1009. //
  1010. // 3.4.1, check whether we are in identity transfer-coding.
  1011. //
  1012. ContentLength = 0;
  1013. Status = HttpGetEntityLength (Parser, &ContentLength);
  1014. if (!EFI_ERROR (Status)) {
  1015. IdentityMode = TRUE;
  1016. } else {
  1017. IdentityMode = FALSE;
  1018. }
  1019. //
  1020. // 3.4.2, start the message-body download, the identity and chunked transfer-coding
  1021. // is handled in different path here.
  1022. //
  1023. ZeroMem (&ResponseBody, sizeof (HTTP_IO_RESPONSE_DATA));
  1024. if (IdentityMode) {
  1025. //
  1026. // In identity transfer-coding there is no need to parse the message body,
  1027. // just download the message body to the user provided buffer directly.
  1028. //
  1029. ReceivedSize = 0;
  1030. while (ReceivedSize < ContentLength) {
  1031. ResponseBody.Body = (CHAR8*) Buffer + ReceivedSize;
  1032. ResponseBody.BodyLength = *BufferSize - ReceivedSize;
  1033. Status = HttpIoRecvResponse (
  1034. &Private->HttpIo,
  1035. FALSE,
  1036. &ResponseBody
  1037. );
  1038. if (EFI_ERROR (Status) || EFI_ERROR (ResponseBody.Status)) {
  1039. if (EFI_ERROR (ResponseBody.Status)) {
  1040. Status = ResponseBody.Status;
  1041. }
  1042. goto ERROR_6;
  1043. }
  1044. ReceivedSize += ResponseBody.BodyLength;
  1045. if (Private->HttpBootCallback != NULL) {
  1046. Status = Private->HttpBootCallback->Callback (
  1047. Private->HttpBootCallback,
  1048. HttpBootHttpEntityBody,
  1049. TRUE,
  1050. (UINT32)ResponseBody.BodyLength,
  1051. ResponseBody.Body
  1052. );
  1053. if (EFI_ERROR (Status)) {
  1054. goto ERROR_6;
  1055. }
  1056. }
  1057. }
  1058. } else {
  1059. //
  1060. // In "chunked" transfer-coding mode, so we need to parse the received
  1061. // data to get the real entity content.
  1062. //
  1063. Block = NULL;
  1064. while (!HttpIsMessageComplete (Parser)) {
  1065. //
  1066. // Allocate a buffer in Block to hold the message-body.
  1067. // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().
  1068. // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before
  1069. // every HttpIoRecvResponse().
  1070. //
  1071. if (Block == NULL || Context.BufferSize == 0) {
  1072. Block = AllocatePool (HTTP_BOOT_BLOCK_SIZE);
  1073. if (Block == NULL) {
  1074. Status = EFI_OUT_OF_RESOURCES;
  1075. goto ERROR_6;
  1076. }
  1077. Context.NewBlock = TRUE;
  1078. Context.Block = Block;
  1079. } else {
  1080. Context.NewBlock = FALSE;
  1081. }
  1082. ResponseBody.Body = (CHAR8*) Block;
  1083. ResponseBody.BodyLength = HTTP_BOOT_BLOCK_SIZE;
  1084. Status = HttpIoRecvResponse (
  1085. &Private->HttpIo,
  1086. FALSE,
  1087. &ResponseBody
  1088. );
  1089. if (EFI_ERROR (Status) || EFI_ERROR (ResponseBody.Status)) {
  1090. if (EFI_ERROR (ResponseBody.Status)) {
  1091. Status = ResponseBody.Status;
  1092. }
  1093. goto ERROR_6;
  1094. }
  1095. //
  1096. // Parse the new received block of the message-body, the block will be saved in cache.
  1097. //
  1098. Status = HttpParseMessageBody (
  1099. Parser,
  1100. ResponseBody.BodyLength,
  1101. ResponseBody.Body
  1102. );
  1103. if (EFI_ERROR (Status)) {
  1104. goto ERROR_6;
  1105. }
  1106. }
  1107. }
  1108. }
  1109. //
  1110. // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.
  1111. //
  1112. Status = HttpGetEntityLength (Parser, &ContentLength);
  1113. if (EFI_ERROR (Status)) {
  1114. goto ERROR_6;
  1115. }
  1116. if (*BufferSize < ContentLength) {
  1117. Status = EFI_BUFFER_TOO_SMALL;
  1118. } else {
  1119. Status = EFI_SUCCESS;
  1120. }
  1121. *BufferSize = ContentLength;
  1122. //
  1123. // 4. Save the cache item to driver's cache list and return.
  1124. //
  1125. if (Cache != NULL) {
  1126. Cache->EntityLength = ContentLength;
  1127. InsertTailList (&Private->CacheList, &Cache->Link);
  1128. }
  1129. if (Parser != NULL) {
  1130. HttpFreeMsgParser (Parser);
  1131. }
  1132. return Status;
  1133. ERROR_6:
  1134. if (Parser != NULL) {
  1135. HttpFreeMsgParser (Parser);
  1136. }
  1137. if (Context.Block != NULL) {
  1138. FreePool (Context.Block);
  1139. }
  1140. HttpBootFreeCache (Cache);
  1141. ERROR_5:
  1142. if (ResponseData != NULL) {
  1143. FreePool (ResponseData);
  1144. }
  1145. ERROR_4:
  1146. if (RequestData != NULL) {
  1147. FreePool (RequestData);
  1148. }
  1149. ERROR_3:
  1150. HttpBootFreeHeader (HttpIoHeader);
  1151. ERROR_2:
  1152. if (Cache != NULL) {
  1153. FreePool (Cache);
  1154. }
  1155. ERROR_1:
  1156. if (Url != NULL) {
  1157. FreePool (Url);
  1158. }
  1159. return Status;
  1160. }