]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - NetworkPkg/HttpBootDxe/HttpBootClient.c
NetworkPkg: Handling timeout case in httpboot driver
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootClient.c
... / ...
CommitLineData
1/** @file\r
2 Implementation of the boot file download function.\r
3\r
4Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
5(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
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
58 } else {\r
59 Node = AllocateZeroPool (sizeof (IPv6_DEVICE_PATH));\r
60 if (Node == NULL) {\r
61 return EFI_OUT_OF_RESOURCES;\r
62 }\r
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
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
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
133 }\r
134 \r
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
148HttpBootDhcp4ExtractUriInfo (\r
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 (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
186 } else {\r
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
192 }\r
193\r
194 //\r
195 // Configure the default DNS server if server assigned.\r
196 //\r
197 if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) || \r
198 (SelectOffer->OfferType == HttpOfferTypeDhcpDns) ||\r
199 (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns)) {\r
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
216 Private->BootFileUri,\r
217 Private->BootFileUriParser,\r
218 &Private->Port\r
219 );\r
220 if (EFI_ERROR (Status) || Private->Port == 0) {\r
221 Private->Port = 80;\r
222 }\r
223 \r
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
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
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
288 } else {\r
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
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
307 if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) || \r
308 (SelectOffer->OfferType == HttpOfferTypeDhcpDns) ||\r
309 (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns)) {\r
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
327 Private->BootFileUri,\r
328 Private->BootFileUriParser,\r
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
337 Private->BootFileUri,\r
338 Private->BootFileUriParser,\r
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
373 Private->BootFileUri,\r
374 Private->BootFileUriParser,\r
375 &Private->Port\r
376 );\r
377 if (EFI_ERROR (Status) || Private->Port == 0) {\r
378 Private->Port = 80;\r
379 }\r
380 \r
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
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
426 Status = HttpBootDhcp4ExtractUriInfo (Private);\r
427 } else {\r
428 Status = HttpBootDhcp6ExtractUriInfo (Private);\r
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 EFI_HANDLE ImageHandle;\r
451\r
452 ASSERT (Private != NULL);\r
453\r
454 ZeroMem (&ConfigData, sizeof (HTTP_IO_CONFIG_DATA));\r
455 if (!Private->UsingIpv6) {\r
456 ConfigData.Config4.HttpVersion = HttpVersion11;\r
457 ConfigData.Config4.RequestTimeOut = HTTP_BOOT_REQUEST_TIMEOUT;\r
458 IP4_COPY_ADDRESS (&ConfigData.Config4.LocalIp, &Private->StationIp.v4);\r
459 IP4_COPY_ADDRESS (&ConfigData.Config4.SubnetMask, &Private->SubnetMask.v4);\r
460 ImageHandle = Private->Ip4Nic->ImageHandle;\r
461 } else {\r
462 ConfigData.Config6.HttpVersion = HttpVersion11;\r
463 ConfigData.Config6.RequestTimeOut = HTTP_BOOT_REQUEST_TIMEOUT;\r
464 IP6_COPY_ADDRESS (&ConfigData.Config6.LocalIp, &Private->StationIp.v6);\r
465 ImageHandle = Private->Ip6Nic->ImageHandle;\r
466 }\r
467\r
468 Status = HttpIoCreateIo (\r
469 ImageHandle,\r
470 Private->Controller,\r
471 Private->UsingIpv6 ? IP_VERSION_6 : IP_VERSION_4,\r
472 &ConfigData,\r
473 &Private->HttpIo\r
474 );\r
475 if (EFI_ERROR (Status)) {\r
476 return Status;\r
477 }\r
478\r
479 Private->HttpCreated = TRUE;\r
480 return EFI_SUCCESS;\r
481}\r
482\r
483/**\r
484 Release all the resource of a cache item.\r
485\r
486 @param[in] Cache The pointer to the cache item.\r
487\r
488**/\r
489VOID\r
490HttpBootFreeCache (\r
491 IN HTTP_BOOT_CACHE_CONTENT *Cache\r
492 )\r
493{\r
494 UINTN Index;\r
495 LIST_ENTRY *Entry;\r
496 LIST_ENTRY *NextEntry;\r
497 HTTP_BOOT_ENTITY_DATA *EntityData;\r
498\r
499 if (Cache != NULL) {\r
500 //\r
501 // Free the request data\r
502 //\r
503 if (Cache->RequestData != NULL) {\r
504 if (Cache->RequestData->Url != NULL) {\r
505 FreePool (Cache->RequestData->Url);\r
506 }\r
507 FreePool (Cache->RequestData);\r
508 }\r
509\r
510 //\r
511 // Free the response header\r
512 //\r
513 if (Cache->ResponseData != NULL) {\r
514 if (Cache->ResponseData->Headers != NULL) {\r
515 for (Index = 0; Index < Cache->ResponseData->HeaderCount; Index++) {\r
516 FreePool (Cache->ResponseData->Headers[Index].FieldName);\r
517 FreePool (Cache->ResponseData->Headers[Index].FieldValue);\r
518 }\r
519 FreePool (Cache->ResponseData->Headers);\r
520 }\r
521 }\r
522\r
523 //\r
524 // Free the response body\r
525 //\r
526 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Cache->EntityDataList) {\r
527 EntityData = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_ENTITY_DATA, Link);\r
528 if (EntityData->Block != NULL) {\r
529 FreePool (EntityData->Block);\r
530 }\r
531 RemoveEntryList (&EntityData->Link);\r
532 FreePool (EntityData);\r
533 }\r
534\r
535 FreePool (Cache);\r
536 }\r
537}\r
538\r
539/**\r
540 Clean up all cached data.\r
541\r
542 @param[in] Private The pointer to the driver's private data.\r
543\r
544**/\r
545VOID\r
546HttpBootFreeCacheList (\r
547 IN HTTP_BOOT_PRIVATE_DATA *Private\r
548 )\r
549{\r
550 LIST_ENTRY *Entry;\r
551 LIST_ENTRY *NextEntry;\r
552 HTTP_BOOT_CACHE_CONTENT *Cache;\r
553 \r
554 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->CacheList) {\r
555 Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);\r
556 RemoveEntryList (&Cache->Link);\r
557 HttpBootFreeCache (Cache);\r
558 }\r
559}\r
560\r
561/**\r
562 Get the file content from cached data.\r
563\r
564 @param[in] Private The pointer to the driver's private data.\r
565 @param[in] Uri Uri of the file to be retrieved from cache.\r
566 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return\r
567 code of EFI_SUCCESS, the amount of data transferred to\r
568 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
569 the size of Buffer required to retrieve the requested file.\r
570 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r
571 then the size of the requested file is returned in\r
572 BufferSize.\r
573 @param[out] ImageType The image type of the downloaded file.\r
574\r
575 @retval EFI_SUCCESS Successfully created.\r
576 @retval Others Failed to create HttpIo.\r
577\r
578**/\r
579EFI_STATUS\r
580HttpBootGetFileFromCache (\r
581 IN HTTP_BOOT_PRIVATE_DATA *Private,\r
582 IN CHAR16 *Uri,\r
583 IN OUT UINTN *BufferSize,\r
584 OUT UINT8 *Buffer,\r
585 OUT HTTP_BOOT_IMAGE_TYPE *ImageType\r
586 )\r
587{\r
588 LIST_ENTRY *Entry;\r
589 LIST_ENTRY *Entry2;\r
590 HTTP_BOOT_CACHE_CONTENT *Cache;\r
591 HTTP_BOOT_ENTITY_DATA *EntityData;\r
592 UINTN CopyedSize;\r
593 \r
594 if (Uri == NULL || BufferSize == 0 || Buffer == NULL || ImageType == NULL) {\r
595 return EFI_INVALID_PARAMETER;\r
596 }\r
597\r
598 NET_LIST_FOR_EACH (Entry, &Private->CacheList) {\r
599 Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);\r
600 //\r
601 // Compare the URI to see whether we already have a cache for this file.\r
602 //\r
603 if ((Cache->RequestData != NULL) &&\r
604 (Cache->RequestData->Url != NULL) &&\r
605 (StrCmp (Uri, Cache->RequestData->Url) == 0)) \r
606 {\r
607 //\r
608 // Hit in cache, record image type.\r
609 //\r
610 *ImageType = Cache->ImageType;\r
611\r
612 //\r
613 // Check buffer size.\r
614 //\r
615 if (*BufferSize < Cache->EntityLength) {\r
616 *BufferSize = Cache->EntityLength;\r
617 return EFI_BUFFER_TOO_SMALL;\r
618 }\r
619\r
620 //\r
621 // Fill data to buffer.\r
622 //\r
623 CopyedSize = 0;\r
624 NET_LIST_FOR_EACH (Entry2, &Cache->EntityDataList) {\r
625 EntityData = NET_LIST_USER_STRUCT (Entry2, HTTP_BOOT_ENTITY_DATA, Link);\r
626 if (*BufferSize > CopyedSize) {\r
627 CopyMem (\r
628 Buffer + CopyedSize,\r
629 EntityData->DataStart,\r
630 MIN (EntityData->DataLength, *BufferSize - CopyedSize)\r
631 );\r
632 CopyedSize += MIN (EntityData->DataLength, *BufferSize - CopyedSize);\r
633 }\r
634 }\r
635 *BufferSize = CopyedSize;\r
636 return EFI_SUCCESS;\r
637 }\r
638 }\r
639\r
640 return EFI_NOT_FOUND;\r
641}\r
642\r
643/**\r
644 A callback function to intercept events during message parser.\r
645\r
646 This function will be invoked during HttpParseMessageBody() with various events type. An error\r
647 return status of the callback function will cause the HttpParseMessageBody() aborted.\r
648\r
649 @param[in] EventType Event type of this callback call.\r
650 @param[in] Data A pointer to data buffer.\r
651 @param[in] Length Length in bytes of the Data.\r
652 @param[in] Context Callback context set by HttpInitMsgParser().\r
653\r
654 @retval EFI_SUCCESS Continue to parser the message body.\r
655 @retval Others Abort the parse.\r
656 \r
657**/\r
658EFI_STATUS\r
659EFIAPI\r
660HttpBootGetBootFileCallback (\r
661 IN HTTP_BODY_PARSE_EVENT EventType,\r
662 IN CHAR8 *Data,\r
663 IN UINTN Length,\r
664 IN VOID *Context\r
665 )\r
666{\r
667 HTTP_BOOT_CALLBACK_DATA *CallbackData;\r
668 HTTP_BOOT_ENTITY_DATA *NewEntityData;\r
669\r
670 //\r
671 // We only care about the entity data.\r
672 //\r
673 if (EventType != BodyParseEventOnData) {\r
674 return EFI_SUCCESS;\r
675 }\r
676\r
677 CallbackData = (HTTP_BOOT_CALLBACK_DATA *) Context;\r
678 //\r
679 // Copy data if caller has provided a buffer.\r
680 //\r
681 if (CallbackData->BufferSize > CallbackData->CopyedSize) {\r
682 CopyMem (\r
683 CallbackData->Buffer + CallbackData->CopyedSize,\r
684 Data,\r
685 MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize)\r
686 );\r
687 CallbackData->CopyedSize += MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize);\r
688 }\r
689\r
690 //\r
691 // The caller doesn't provide a buffer, save the block into cache list.\r
692 //\r
693 if (CallbackData->Cache != NULL) {\r
694 NewEntityData = AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA));\r
695 if (NewEntityData == NULL) {\r
696 return EFI_OUT_OF_RESOURCES;\r
697 }\r
698 if (CallbackData->NewBlock) {\r
699 NewEntityData->Block = CallbackData->Block;\r
700 CallbackData->Block = NULL;\r
701 }\r
702 NewEntityData->DataLength = Length;\r
703 NewEntityData->DataStart = (UINT8*) Data;\r
704 InsertTailList (&CallbackData->Cache->EntityDataList, &NewEntityData->Link);\r
705 }\r
706 return EFI_SUCCESS;\r
707}\r
708\r
709/**\r
710 This function download the boot file by using UEFI HTTP protocol.\r
711 \r
712 @param[in] Private The pointer to the driver's private data.\r
713 @param[in] HeaderOnly Only request the response header, it could save a lot of time if\r
714 the caller only want to know the size of the requested file.\r
715 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return\r
716 code of EFI_SUCCESS, the amount of data transferred to\r
717 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
718 the size of Buffer required to retrieve the requested file.\r
719 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r
720 then the size of the requested file is returned in\r
721 BufferSize.\r
722 @param[out] ImageType The image type of the downloaded file.\r
723\r
724 @retval EFI_SUCCESS The file was loaded.\r
725 @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.\r
726 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources\r
727 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.\r
728 BufferSize has been updated with the size needed to complete\r
729 the request.\r
730 @retval Others Unexpected error happened.\r
731\r
732**/\r
733EFI_STATUS\r
734HttpBootGetBootFile (\r
735 IN HTTP_BOOT_PRIVATE_DATA *Private,\r
736 IN BOOLEAN HeaderOnly,\r
737 IN OUT UINTN *BufferSize,\r
738 OUT UINT8 *Buffer,\r
739 OUT HTTP_BOOT_IMAGE_TYPE *ImageType\r
740 )\r
741{\r
742 EFI_STATUS Status;\r
743 EFI_HTTP_STATUS_CODE StatusCode;\r
744 CHAR8 *HostName;\r
745 EFI_HTTP_REQUEST_DATA *RequestData;\r
746 HTTP_IO_RESPONSE_DATA *ResponseData;\r
747 HTTP_IO_RESPONSE_DATA ResponseBody;\r
748 HTTP_IO *HttpIo;\r
749 HTTP_IO_HEADER *HttpIoHeader;\r
750 VOID *Parser;\r
751 HTTP_BOOT_CALLBACK_DATA Context;\r
752 UINTN ContentLength;\r
753 HTTP_BOOT_CACHE_CONTENT *Cache;\r
754 UINT8 *Block;\r
755 CHAR16 *Url;\r
756 BOOLEAN IdentityMode;\r
757 UINTN ReceivedSize;\r
758 \r
759 ASSERT (Private != NULL);\r
760 ASSERT (Private->HttpCreated);\r
761\r
762 if (BufferSize == NULL || ImageType == NULL) {\r
763 return EFI_INVALID_PARAMETER;\r
764 }\r
765\r
766 if (*BufferSize != 0 && Buffer == NULL) {\r
767 return EFI_INVALID_PARAMETER;\r
768 }\r
769\r
770 //\r
771 // First, check whether we already cached the requested Uri.\r
772 //\r
773 Url = AllocatePool ((AsciiStrLen (Private->BootFileUri) + 1) * sizeof (CHAR16));\r
774 if (Url == NULL) {\r
775 return EFI_OUT_OF_RESOURCES;\r
776 }\r
777 AsciiStrToUnicodeStr (Private->BootFileUri, Url);\r
778 if (!HeaderOnly) {\r
779 Status = HttpBootGetFileFromCache (Private, Url, BufferSize, Buffer, ImageType);\r
780 if (Status != EFI_NOT_FOUND) {\r
781 FreePool (Url);\r
782 return Status;\r
783 }\r
784 }\r
785\r
786 //\r
787 // Not found in cache, try to download it through HTTP.\r
788 //\r
789\r
790 //\r
791 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.\r
792 //\r
793 Cache = NULL;\r
794 if ((!HeaderOnly) && (*BufferSize == 0)) {\r
795 Cache = AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT));\r
796 if (Cache == NULL) {\r
797 Status = EFI_OUT_OF_RESOURCES;\r
798 goto ERROR_1;\r
799 }\r
800 Cache->ImageType = ImageTypeMax;\r
801 InitializeListHead (&Cache->EntityDataList);\r
802 }\r
803\r
804 //\r
805 // 2. Send HTTP request message.\r
806 //\r
807\r
808 //\r
809 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:\r
810 // Host\r
811 // Accept\r
812 // User-Agent\r
813 //\r
814 HttpIoHeader = HttpBootCreateHeader (3);\r
815 if (HttpIoHeader == NULL) {\r
816 Status = EFI_OUT_OF_RESOURCES;\r
817 goto ERROR_2;\r
818 }\r
819\r
820 //\r
821 // Add HTTP header field 1: Host\r
822 //\r
823 HostName = NULL;\r
824 Status = HttpUrlGetHostName (\r
825 Private->BootFileUri,\r
826 Private->BootFileUriParser,\r
827 &HostName\r
828 );\r
829 if (EFI_ERROR (Status)) {\r
830 goto ERROR_3;\r
831 }\r
832 Status = HttpBootSetHeader (\r
833 HttpIoHeader,\r
834 HTTP_HEADER_HOST,\r
835 HostName\r
836 );\r
837 FreePool (HostName);\r
838 if (EFI_ERROR (Status)) {\r
839 goto ERROR_3;\r
840 }\r
841\r
842 //\r
843 // Add HTTP header field 2: Accept\r
844 //\r
845 Status = HttpBootSetHeader (\r
846 HttpIoHeader,\r
847 HTTP_HEADER_ACCEPT,\r
848 "*/*"\r
849 );\r
850 if (EFI_ERROR (Status)) {\r
851 goto ERROR_3;\r
852 }\r
853\r
854 //\r
855 // Add HTTP header field 3: User-Agent\r
856 //\r
857 Status = HttpBootSetHeader (\r
858 HttpIoHeader,\r
859 HTTP_HEADER_USER_AGENT,\r
860 HTTP_USER_AGENT_EFI_HTTP_BOOT\r
861 );\r
862 if (EFI_ERROR (Status)) {\r
863 goto ERROR_3;\r
864 }\r
865\r
866 //\r
867 // 2.2 Build the rest of HTTP request info.\r
868 //\r
869 RequestData = AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA));\r
870 if (RequestData == NULL) {\r
871 Status = EFI_OUT_OF_RESOURCES;\r
872 goto ERROR_3;\r
873 }\r
874 RequestData->Method = HeaderOnly ? HttpMethodHead : HttpMethodGet;\r
875 RequestData->Url = Url;\r
876 if (RequestData->Url == NULL) {\r
877 Status = EFI_OUT_OF_RESOURCES;\r
878 goto ERROR_4;\r
879 }\r
880 AsciiStrToUnicodeStr (Private->BootFileUri, RequestData->Url);\r
881\r
882 //\r
883 // 2.3 Record the request info in a temp cache item.\r
884 //\r
885 if (Cache != NULL) {\r
886 Cache->RequestData = RequestData;\r
887 }\r
888\r
889 //\r
890 // 2.4 Send out the request to HTTP server.\r
891 //\r
892 HttpIo = &Private->HttpIo;\r
893 Status = HttpIoSendRequest (\r
894 HttpIo,\r
895 RequestData,\r
896 HttpIoHeader->HeaderCount,\r
897 HttpIoHeader->Headers,\r
898 0,\r
899 NULL\r
900 );\r
901 if (EFI_ERROR (Status)) {\r
902 goto ERROR_4;\r
903 }\r
904\r
905 //\r
906 // 3. Receive HTTP response message.\r
907 //\r
908\r
909 //\r
910 // 3.1 First step, use zero BodyLength to only receive the response headers.\r
911 //\r
912 ResponseData = AllocateZeroPool (sizeof(HTTP_IO_RESPONSE_DATA));\r
913 if (ResponseData == NULL) {\r
914 Status = EFI_OUT_OF_RESOURCES;\r
915 goto ERROR_4;\r
916 }\r
917 Status = HttpIoRecvResponse (\r
918 &Private->HttpIo,\r
919 TRUE,\r
920 ResponseData\r
921 );\r
922 if (EFI_ERROR (Status) || EFI_ERROR (ResponseData->Status)) {\r
923 if (EFI_ERROR (ResponseData->Status)) {\r
924 StatusCode = HttpIo->RspToken.Message->Data.Response->StatusCode;\r
925 HttpBootPrintErrorMessage (StatusCode);\r
926 Status = ResponseData->Status;\r
927 }\r
928 goto ERROR_5;\r
929 }\r
930\r
931 //\r
932 // Check the image type according to server's response.\r
933 //\r
934 Status = HttpBootCheckImageType (\r
935 Private->BootFileUri,\r
936 Private->BootFileUriParser,\r
937 ResponseData->HeaderCount,\r
938 ResponseData->Headers,\r
939 ImageType\r
940 );\r
941 if (EFI_ERROR (Status)) {\r
942 goto ERROR_5;\r
943 }\r
944\r
945 //\r
946 // 3.2 Cache the response header.\r
947 //\r
948 if (Cache != NULL) {\r
949 Cache->ResponseData = ResponseData;\r
950 Cache->ImageType = *ImageType;\r
951 }\r
952 \r
953 //\r
954 // 3.3 Init a message-body parser from the header information.\r
955 //\r
956 Parser = NULL;\r
957 Context.NewBlock = FALSE;\r
958 Context.Block = NULL;\r
959 Context.CopyedSize = 0;\r
960 Context.Buffer = Buffer;\r
961 Context.BufferSize = *BufferSize;\r
962 Context.Cache = Cache;\r
963 Status = HttpInitMsgParser (\r
964 HeaderOnly? HttpMethodHead : HttpMethodGet,\r
965 ResponseData->Response.StatusCode,\r
966 ResponseData->HeaderCount,\r
967 ResponseData->Headers,\r
968 HttpBootGetBootFileCallback,\r
969 (VOID*) &Context,\r
970 &Parser\r
971 );\r
972 if (EFI_ERROR (Status)) {\r
973 goto ERROR_6;\r
974 }\r
975\r
976 //\r
977 // 3.4 Continue to receive and parse message-body if needed.\r
978 //\r
979 Block = NULL;\r
980 if (!HeaderOnly) {\r
981 //\r
982 // 3.4.1, check whether we are in identity transfer-coding.\r
983 //\r
984 ContentLength = 0;\r
985 Status = HttpGetEntityLength (Parser, &ContentLength);\r
986 if (!EFI_ERROR (Status)) {\r
987 IdentityMode = TRUE;\r
988 } else {\r
989 IdentityMode = FALSE;\r
990 }\r
991\r
992 //\r
993 // 3.4.2, start the message-body download, the identity and chunked transfer-coding\r
994 // is handled in different path here.\r
995 //\r
996 ZeroMem (&ResponseBody, sizeof (HTTP_IO_RESPONSE_DATA));\r
997 if (IdentityMode) {\r
998 //\r
999 // In identity transfer-coding there is no need to parse the message body,\r
1000 // just download the message body to the user provided buffer directly.\r
1001 //\r
1002 ReceivedSize = 0;\r
1003 while (ReceivedSize < ContentLength) {\r
1004 ResponseBody.Body = (CHAR8*) Buffer + ReceivedSize;\r
1005 ResponseBody.BodyLength = *BufferSize - ReceivedSize;\r
1006 Status = HttpIoRecvResponse (\r
1007 &Private->HttpIo,\r
1008 FALSE,\r
1009 &ResponseBody\r
1010 );\r
1011 if (EFI_ERROR (Status) || EFI_ERROR (ResponseBody.Status)) {\r
1012 if (EFI_ERROR (ResponseBody.Status)) {\r
1013 Status = ResponseBody.Status;\r
1014 }\r
1015 goto ERROR_6;\r
1016 }\r
1017 ReceivedSize += ResponseBody.BodyLength;\r
1018 }\r
1019 } else {\r
1020 //\r
1021 // In "chunked" transfer-coding mode, so we need to parse the received\r
1022 // data to get the real entity content.\r
1023 //\r
1024 Block = NULL;\r
1025 while (!HttpIsMessageComplete (Parser)) {\r
1026 //\r
1027 // Allocate a buffer in Block to hold the message-body.\r
1028 // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().\r
1029 // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before\r
1030 // every HttpIoRecvResponse().\r
1031 //\r
1032 if (Block == NULL || Context.BufferSize == 0) {\r
1033 Block = AllocatePool (HTTP_BOOT_BLOCK_SIZE);\r
1034 if (Block == NULL) {\r
1035 Status = EFI_OUT_OF_RESOURCES;\r
1036 goto ERROR_6;\r
1037 }\r
1038 Context.NewBlock = TRUE;\r
1039 Context.Block = Block;\r
1040 } else {\r
1041 Context.NewBlock = FALSE;\r
1042 }\r
1043\r
1044 ResponseBody.Body = (CHAR8*) Block;\r
1045 ResponseBody.BodyLength = HTTP_BOOT_BLOCK_SIZE;\r
1046 Status = HttpIoRecvResponse (\r
1047 &Private->HttpIo,\r
1048 FALSE,\r
1049 &ResponseBody\r
1050 );\r
1051 if (EFI_ERROR (Status) || EFI_ERROR (ResponseBody.Status)) {\r
1052 if (EFI_ERROR (ResponseBody.Status)) {\r
1053 Status = ResponseBody.Status;\r
1054 }\r
1055 goto ERROR_6;\r
1056 }\r
1057\r
1058 //\r
1059 // Parse the new received block of the message-body, the block will be saved in cache.\r
1060 //\r
1061 Status = HttpParseMessageBody (\r
1062 Parser,\r
1063 ResponseBody.BodyLength,\r
1064 ResponseBody.Body\r
1065 );\r
1066 if (EFI_ERROR (Status)) {\r
1067 goto ERROR_6;\r
1068 }\r
1069 }\r
1070 }\r
1071 }\r
1072\r
1073 //\r
1074 // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.\r
1075 //\r
1076 Status = HttpGetEntityLength (Parser, &ContentLength);\r
1077 if (EFI_ERROR (Status)) {\r
1078 goto ERROR_6;\r
1079 }\r
1080\r
1081 if (*BufferSize < ContentLength) {\r
1082 Status = EFI_BUFFER_TOO_SMALL;\r
1083 } else {\r
1084 Status = EFI_SUCCESS;\r
1085 }\r
1086 *BufferSize = ContentLength;\r
1087\r
1088 //\r
1089 // 4. Save the cache item to driver's cache list and return.\r
1090 //\r
1091 if (Cache != NULL) {\r
1092 Cache->EntityLength = ContentLength;\r
1093 InsertTailList (&Private->CacheList, &Cache->Link);\r
1094 }\r
1095\r
1096 if (Parser != NULL) {\r
1097 HttpFreeMsgParser (Parser);\r
1098 }\r
1099\r
1100 return Status;\r
1101 \r
1102ERROR_6:\r
1103 if (Parser != NULL) {\r
1104 HttpFreeMsgParser (Parser);\r
1105 }\r
1106 if (Context.Block != NULL) {\r
1107 FreePool (Context.Block);\r
1108 }\r
1109 HttpBootFreeCache (Cache);\r
1110 \r
1111ERROR_5:\r
1112 if (ResponseData != NULL) {\r
1113 FreePool (ResponseData);\r
1114 }\r
1115ERROR_4:\r
1116 if (RequestData != NULL) {\r
1117 FreePool (RequestData);\r
1118 }\r
1119ERROR_3:\r
1120 HttpBootFreeHeader (HttpIoHeader);\r
1121ERROR_2:\r
1122 if (Cache != NULL) {\r
1123 FreePool (Cache);\r
1124 }\r
1125ERROR_1:\r
1126 if (Url != NULL) {\r
1127 FreePool (Url);\r
1128 }\r
1129\r
1130 return Status;\r
1131}\r
1132\r