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