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