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