]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/HttpBootDxe/HttpBootClient.c
NetworkPkg: Fix the driver model issue in HTTP Boot driver.
[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
75372581 450 EFI_HANDLE ImageHandle;\r
d933e70a
JW
451\r
452 ASSERT (Private != NULL);\r
453\r
454 ZeroMem (&ConfigData, sizeof (HTTP_IO_CONFIG_DATA));\r
455 if (!Private->UsingIpv6) {\r
b659408b 456 ConfigData.Config4.HttpVersion = HttpVersion11;\r
d933e70a
JW
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
75372581 460 ImageHandle = Private->Ip4Nic->ImageHandle;\r
d933e70a 461 } else {\r
b659408b
ZL
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
75372581 465 ImageHandle = Private->Ip6Nic->ImageHandle;\r
d933e70a
JW
466 }\r
467\r
468 Status = HttpIoCreateIo (\r
75372581 469 ImageHandle,\r
d933e70a
JW
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
d933e70a
JW
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
7552c24e
FS
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\r
574 @retval EFI_SUCCESS Successfully created.\r
575 @retval Others Failed to create HttpIo.\r
576\r
577**/\r
578EFI_STATUS\r
579HttpBootGetFileFromCache (\r
580 IN HTTP_BOOT_PRIVATE_DATA *Private,\r
581 IN CHAR16 *Uri,\r
582 IN OUT UINTN *BufferSize,\r
583 OUT UINT8 *Buffer\r
584 )\r
585{\r
586 LIST_ENTRY *Entry;\r
587 LIST_ENTRY *Entry2;\r
588 HTTP_BOOT_CACHE_CONTENT *Cache;\r
589 HTTP_BOOT_ENTITY_DATA *EntityData;\r
590 UINTN CopyedSize;\r
591 \r
592 if (Uri == NULL || BufferSize == 0 || Buffer == NULL) {\r
593 return EFI_INVALID_PARAMETER;\r
594 }\r
595\r
7552c24e
FS
596 NET_LIST_FOR_EACH (Entry, &Private->CacheList) {\r
597 Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);\r
598 //\r
599 // Compare the URI to see whether we already have a cache for this file.\r
600 //\r
601 if ((Cache->RequestData != NULL) &&\r
602 (Cache->RequestData->Url != NULL) &&\r
603 (StrCmp (Uri, Cache->RequestData->Url) == 0)) \r
604 {\r
605 //\r
606 // Hit cache, check buffer size.\r
607 //\r
608 if (*BufferSize < Cache->EntityLength) {\r
609 *BufferSize = Cache->EntityLength;\r
610 return EFI_BUFFER_TOO_SMALL;\r
611 }\r
612\r
613 //\r
614 // Fill data to buffer.\r
615 //\r
616 CopyedSize = 0;\r
617 NET_LIST_FOR_EACH (Entry2, &Cache->EntityDataList) {\r
618 EntityData = NET_LIST_USER_STRUCT (Entry2, HTTP_BOOT_ENTITY_DATA, Link);\r
619 if (*BufferSize > CopyedSize) {\r
620 CopyMem (\r
621 Buffer + CopyedSize,\r
622 EntityData->DataStart,\r
623 MIN (EntityData->DataLength, *BufferSize - CopyedSize)\r
624 );\r
625 CopyedSize += MIN (EntityData->DataLength, *BufferSize - CopyedSize);\r
626 }\r
627 }\r
628 *BufferSize = CopyedSize;\r
7552c24e
FS
629 return EFI_SUCCESS;\r
630 }\r
631 }\r
632\r
633 return EFI_NOT_FOUND;\r
634}\r
635\r
d933e70a
JW
636/**\r
637 A callback function to intercept events during message parser.\r
638\r
639 This function will be invoked during HttpParseMessageBody() with various events type. An error\r
640 return status of the callback function will cause the HttpParseMessageBody() aborted.\r
641\r
642 @param[in] EventType Event type of this callback call.\r
643 @param[in] Data A pointer to data buffer.\r
644 @param[in] Length Length in bytes of the Data.\r
645 @param[in] Context Callback context set by HttpInitMsgParser().\r
646\r
647 @retval EFI_SUCCESS Continue to parser the message body.\r
648 @retval Others Abort the parse.\r
649 \r
650**/\r
651EFI_STATUS\r
652EFIAPI\r
653HttpBootGetBootFileCallback (\r
654 IN HTTP_BODY_PARSE_EVENT EventType,\r
655 IN CHAR8 *Data,\r
656 IN UINTN Length,\r
657 IN VOID *Context\r
658 )\r
659{\r
660 HTTP_BOOT_CALLBACK_DATA *CallbackData;\r
661 HTTP_BOOT_ENTITY_DATA *NewEntityData;\r
662\r
663 //\r
664 // We only care about the entity data.\r
665 //\r
666 if (EventType != BodyParseEventOnData) {\r
667 return EFI_SUCCESS;\r
668 }\r
669\r
670 CallbackData = (HTTP_BOOT_CALLBACK_DATA *) Context;\r
d933e70a
JW
671 //\r
672 // Copy data if caller has provided a buffer.\r
673 //\r
674 if (CallbackData->BufferSize > CallbackData->CopyedSize) {\r
675 CopyMem (\r
676 CallbackData->Buffer + CallbackData->CopyedSize,\r
677 Data,\r
678 MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize)\r
679 );\r
680 CallbackData->CopyedSize += MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize);\r
681 }\r
682\r
7fd71047
FS
683 //\r
684 // The caller doesn't provide a buffer, save the block into cache list.\r
685 //\r
686 if (CallbackData->Cache != NULL) {\r
687 NewEntityData = AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA));\r
688 if (NewEntityData == NULL) {\r
689 return EFI_OUT_OF_RESOURCES;\r
690 }\r
691 if (CallbackData->NewBlock) {\r
692 NewEntityData->Block = CallbackData->Block;\r
693 CallbackData->Block = NULL;\r
694 }\r
695 NewEntityData->DataLength = Length;\r
696 NewEntityData->DataStart = (UINT8*) Data;\r
697 InsertTailList (&CallbackData->Cache->EntityDataList, &NewEntityData->Link);\r
698 }\r
d933e70a
JW
699 return EFI_SUCCESS;\r
700}\r
701\r
702/**\r
703 This function download the boot file by using UEFI HTTP protocol.\r
704 \r
705 @param[in] Private The pointer to the driver's private data.\r
706 @param[in] HeaderOnly Only request the response header, it could save a lot of time if\r
707 the caller only want to know the size of the requested file.\r
708 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return\r
709 code of EFI_SUCCESS, the amount of data transferred to\r
710 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
711 the size of Buffer required to retrieve the requested file.\r
712 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r
713 then the size of the requested file is returned in\r
714 BufferSize.\r
715\r
716 @retval EFI_SUCCESS The file was loaded.\r
717 @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.\r
718 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources\r
719 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.\r
720 BufferSize has been updated with the size needed to complete\r
721 the request.\r
722 @retval Others Unexpected error happened.\r
723\r
724**/\r
725EFI_STATUS\r
726HttpBootGetBootFile (\r
727 IN HTTP_BOOT_PRIVATE_DATA *Private,\r
728 IN BOOLEAN HeaderOnly,\r
729 IN OUT UINTN *BufferSize,\r
730 OUT UINT8 *Buffer\r
731 )\r
732{\r
733 EFI_STATUS Status;\r
072289f4 734 EFI_HTTP_STATUS_CODE StatusCode;\r
d933e70a
JW
735 CHAR8 *HostName;\r
736 EFI_HTTP_REQUEST_DATA *RequestData;\r
ef422fc5
PA
737 HTTP_IO_RESPONSE_DATA *ResponseData;\r
738 HTTP_IO_RESPONSE_DATA ResponseBody;\r
d933e70a
JW
739 HTTP_IO *HttpIo;\r
740 HTTP_IO_HEADER *HttpIoHeader;\r
741 VOID *Parser;\r
742 HTTP_BOOT_CALLBACK_DATA Context;\r
743 UINTN ContentLength;\r
744 HTTP_BOOT_CACHE_CONTENT *Cache;\r
745 UINT8 *Block;\r
746 CHAR16 *Url;\r
7552c24e
FS
747 BOOLEAN IdentityMode;\r
748 UINTN ReceivedSize;\r
d933e70a
JW
749 \r
750 ASSERT (Private != NULL);\r
751 ASSERT (Private->HttpCreated);\r
752\r
753 if (BufferSize == NULL) {\r
754 return EFI_INVALID_PARAMETER;\r
755 }\r
756\r
757 if (*BufferSize != 0 && Buffer == NULL) {\r
758 return EFI_INVALID_PARAMETER;\r
759 }\r
760\r
761 //\r
762 // First, check whether we already cached the requested Uri.\r
763 //\r
764 Url = AllocatePool ((AsciiStrLen (Private->BootFileUri) + 1) * sizeof (CHAR16));\r
765 if (Url == NULL) {\r
766 return EFI_OUT_OF_RESOURCES;\r
767 }\r
768 AsciiStrToUnicodeStr (Private->BootFileUri, Url);\r
769 if (!HeaderOnly) {\r
770 Status = HttpBootGetFileFromCache (Private, Url, BufferSize, Buffer);\r
771 if (Status != EFI_NOT_FOUND) {\r
772 FreePool (Url);\r
773 return Status;\r
774 }\r
775 }\r
776\r
777 //\r
778 // Not found in cache, try to download it through HTTP.\r
779 //\r
780\r
781 //\r
7fd71047 782 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.\r
d933e70a
JW
783 //\r
784 Cache = NULL;\r
7fd71047 785 if ((!HeaderOnly) && (*BufferSize == 0)) {\r
d933e70a
JW
786 Cache = AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT));\r
787 if (Cache == NULL) {\r
788 Status = EFI_OUT_OF_RESOURCES;\r
789 goto ERROR_1;\r
790 }\r
791 InitializeListHead (&Cache->EntityDataList);\r
792 }\r
793\r
794 //\r
795 // 2. Send HTTP request message.\r
796 //\r
797\r
798 //\r
799 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:\r
800 // Host\r
801 // Accept\r
802 // User-Agent\r
803 //\r
804 HttpIoHeader = HttpBootCreateHeader (3);\r
805 if (HttpIoHeader == NULL) {\r
806 Status = EFI_OUT_OF_RESOURCES;\r
807 goto ERROR_2;\r
808 }\r
809\r
810 //\r
811 // Add HTTP header field 1: Host\r
812 //\r
813 HostName = NULL;\r
814 Status = HttpUrlGetHostName (\r
815 Private->BootFileUri,\r
816 Private->BootFileUriParser,\r
817 &HostName\r
818 );\r
819 if (EFI_ERROR (Status)) {\r
820 goto ERROR_3;\r
821 }\r
822 Status = HttpBootSetHeader (\r
823 HttpIoHeader,\r
90f658c4 824 HTTP_HEADER_HOST,\r
d933e70a
JW
825 HostName\r
826 );\r
827 FreePool (HostName);\r
828 if (EFI_ERROR (Status)) {\r
829 goto ERROR_3;\r
830 }\r
831\r
832 //\r
833 // Add HTTP header field 2: Accept\r
834 //\r
835 Status = HttpBootSetHeader (\r
836 HttpIoHeader,\r
90f658c4 837 HTTP_HEADER_ACCEPT,\r
d933e70a
JW
838 "*/*"\r
839 );\r
840 if (EFI_ERROR (Status)) {\r
841 goto ERROR_3;\r
842 }\r
843\r
844 //\r
845 // Add HTTP header field 3: User-Agent\r
846 //\r
847 Status = HttpBootSetHeader (\r
848 HttpIoHeader,\r
90f658c4 849 HTTP_HEADER_USER_AGENT,\r
d933e70a
JW
850 HTTP_USER_AGENT_EFI_HTTP_BOOT\r
851 );\r
852 if (EFI_ERROR (Status)) {\r
853 goto ERROR_3;\r
854 }\r
855\r
856 //\r
857 // 2.2 Build the rest of HTTP request info.\r
858 //\r
859 RequestData = AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA));\r
860 if (RequestData == NULL) {\r
861 Status = EFI_OUT_OF_RESOURCES;\r
862 goto ERROR_3;\r
863 }\r
864 RequestData->Method = HeaderOnly ? HttpMethodHead : HttpMethodGet;\r
865 RequestData->Url = Url;\r
866 if (RequestData->Url == NULL) {\r
867 Status = EFI_OUT_OF_RESOURCES;\r
868 goto ERROR_4;\r
869 }\r
870 AsciiStrToUnicodeStr (Private->BootFileUri, RequestData->Url);\r
871\r
872 //\r
873 // 2.3 Record the request info in a temp cache item.\r
874 //\r
7fd71047 875 if (Cache != NULL) {\r
d933e70a
JW
876 Cache->RequestData = RequestData;\r
877 }\r
878\r
879 //\r
880 // 2.4 Send out the request to HTTP server.\r
881 //\r
882 HttpIo = &Private->HttpIo;\r
883 Status = HttpIoSendRequest (\r
884 HttpIo,\r
885 RequestData,\r
886 HttpIoHeader->HeaderCount,\r
887 HttpIoHeader->Headers,\r
888 0,\r
889 NULL\r
890 );\r
891 if (EFI_ERROR (Status)) {\r
892 goto ERROR_4;\r
893 }\r
894\r
895 //\r
896 // 3. Receive HTTP response message.\r
897 //\r
898\r
899 //\r
900 // 3.1 First step, use zero BodyLength to only receive the response headers.\r
901 //\r
ef422fc5 902 ResponseData = AllocateZeroPool (sizeof(HTTP_IO_RESPONSE_DATA));\r
d933e70a
JW
903 if (ResponseData == NULL) {\r
904 Status = EFI_OUT_OF_RESOURCES;\r
905 goto ERROR_4;\r
906 }\r
907 Status = HttpIoRecvResponse (\r
908 &Private->HttpIo,\r
909 TRUE,\r
910 ResponseData\r
911 );\r
072289f4
ZL
912 if (EFI_ERROR (Status) || EFI_ERROR (ResponseData->Status)) {\r
913 if (EFI_ERROR (ResponseData->Status)) {\r
914 StatusCode = HttpIo->RspToken.Message->Data.Response->StatusCode;\r
915 HttpBootPrintErrorMessage (StatusCode);\r
916 Status = ResponseData->Status;\r
917 }\r
d933e70a
JW
918 goto ERROR_5;\r
919 }\r
920\r
921 //\r
922 // 3.2 Cache the response header.\r
923 //\r
7fd71047 924 if (Cache != NULL) {\r
d933e70a
JW
925 Cache->ResponseData = ResponseData;\r
926 }\r
927 \r
928 //\r
929 // 3.3 Init a message-body parser from the header information.\r
930 //\r
931 Parser = NULL;\r
932 Context.NewBlock = FALSE;\r
933 Context.Block = NULL;\r
934 Context.CopyedSize = 0;\r
935 Context.Buffer = Buffer;\r
936 Context.BufferSize = *BufferSize;\r
937 Context.Cache = Cache;\r
938 Status = HttpInitMsgParser (\r
939 HeaderOnly? HttpMethodHead : HttpMethodGet,\r
940 ResponseData->Response.StatusCode,\r
941 ResponseData->HeaderCount,\r
942 ResponseData->Headers,\r
943 HttpBootGetBootFileCallback,\r
944 (VOID*) &Context,\r
945 &Parser\r
946 );\r
947 if (EFI_ERROR (Status)) {\r
948 goto ERROR_6;\r
949 }\r
950\r
951 //\r
952 // 3.4 Continue to receive and parse message-body if needed.\r
953 //\r
7fd71047 954 Block = NULL;\r
d933e70a 955 if (!HeaderOnly) {\r
7552c24e
FS
956 //\r
957 // 3.4.1, check whether we are in identity transfer-coding.\r
958 //\r
959 ContentLength = 0;\r
960 Status = HttpGetEntityLength (Parser, &ContentLength);\r
961 if (!EFI_ERROR (Status)) {\r
962 IdentityMode = TRUE;\r
963 } else {\r
964 IdentityMode = FALSE;\r
965 }\r
966\r
967 //\r
968 // 3.4.2, start the message-body download, the identity and chunked transfer-coding\r
969 // is handled in different path here.\r
970 //\r
ef422fc5 971 ZeroMem (&ResponseBody, sizeof (HTTP_IO_RESPONSE_DATA));\r
7552c24e 972 if (IdentityMode) {\r
d933e70a 973 //\r
7552c24e
FS
974 // In identity transfer-coding there is no need to parse the message body,\r
975 // just download the message body to the user provided buffer directly.\r
d933e70a 976 //\r
7552c24e
FS
977 ReceivedSize = 0;\r
978 while (ReceivedSize < ContentLength) {\r
979 ResponseBody.Body = (CHAR8*) Buffer + ReceivedSize;\r
980 ResponseBody.BodyLength = *BufferSize - ReceivedSize;\r
981 Status = HttpIoRecvResponse (\r
982 &Private->HttpIo,\r
983 FALSE,\r
984 &ResponseBody\r
985 );\r
986 if (EFI_ERROR (Status)) {\r
7fd71047
FS
987 goto ERROR_6;\r
988 }\r
7552c24e 989 ReceivedSize += ResponseBody.BodyLength;\r
d933e70a 990 }\r
7552c24e 991 } else {\r
d933e70a 992 //\r
7552c24e
FS
993 // In "chunked" transfer-coding mode, so we need to parse the received\r
994 // data to get the real entity content.\r
d933e70a 995 //\r
7552c24e
FS
996 Block = NULL;\r
997 while (!HttpIsMessageComplete (Parser)) {\r
998 //\r
999 // Allocate a buffer in Block to hold the message-body.\r
1000 // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().\r
1001 // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before\r
1002 // every HttpIoRecvResponse().\r
1003 //\r
1004 if (Block == NULL || Context.BufferSize == 0) {\r
1005 Block = AllocatePool (HTTP_BOOT_BLOCK_SIZE);\r
1006 if (Block == NULL) {\r
1007 Status = EFI_OUT_OF_RESOURCES;\r
1008 goto ERROR_6;\r
1009 }\r
1010 Context.NewBlock = TRUE;\r
1011 Context.Block = Block;\r
1012 } else {\r
1013 Context.NewBlock = FALSE;\r
1014 }\r
1015\r
1016 ResponseBody.Body = (CHAR8*) Block;\r
1017 ResponseBody.BodyLength = HTTP_BOOT_BLOCK_SIZE;\r
1018 Status = HttpIoRecvResponse (\r
1019 &Private->HttpIo,\r
1020 FALSE,\r
1021 &ResponseBody\r
1022 );\r
1023 if (EFI_ERROR (Status)) {\r
1024 goto ERROR_6;\r
1025 }\r
1026\r
1027 //\r
1028 // Parse the new received block of the message-body, the block will be saved in cache.\r
1029 //\r
1030 Status = HttpParseMessageBody (\r
1031 Parser,\r
1032 ResponseBody.BodyLength,\r
1033 ResponseBody.Body\r
1034 );\r
1035 if (EFI_ERROR (Status)) {\r
1036 goto ERROR_6;\r
1037 }\r
d933e70a
JW
1038 }\r
1039 }\r
1040 }\r
7552c24e 1041\r
d933e70a 1042 //\r
7552c24e 1043 // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.\r
d933e70a
JW
1044 //\r
1045 Status = HttpGetEntityLength (Parser, &ContentLength);\r
1046 if (EFI_ERROR (Status)) {\r
1047 goto ERROR_6;\r
1048 }\r
1049\r
1050 if (*BufferSize < ContentLength) {\r
1051 Status = EFI_BUFFER_TOO_SMALL;\r
1052 }\r
1053 *BufferSize = ContentLength;\r
1054\r
1055 //\r
1056 // 4. Save the cache item to driver's cache list and return.\r
1057 //\r
7fd71047 1058 if (Cache != NULL) {\r
d933e70a
JW
1059 Cache->EntityLength = ContentLength;\r
1060 InsertTailList (&Private->CacheList, &Cache->Link);\r
1061 }\r
1062\r
1063 if (Parser != NULL) {\r
1064 HttpFreeMsgParser (Parser);\r
1065 }\r
1066\r
1067 return EFI_SUCCESS;\r
1068 \r
1069ERROR_6:\r
1070 if (Parser != NULL) {\r
1071 HttpFreeMsgParser (Parser);\r
1072 }\r
1073 if (Context.Block != NULL) {\r
1074 FreePool (Context.Block);\r
1075 }\r
1076 HttpBootFreeCache (Cache);\r
1077 \r
1078ERROR_5:\r
1079 if (ResponseData != NULL) {\r
1080 FreePool (ResponseData);\r
1081 }\r
1082ERROR_4:\r
1083 if (RequestData != NULL) {\r
1084 FreePool (RequestData);\r
1085 }\r
1086ERROR_3:\r
1087 HttpBootFreeHeader (HttpIoHeader);\r
1088ERROR_2:\r
1089 if (Cache != NULL) {\r
1090 FreePool (Cache);\r
1091 }\r
1092ERROR_1:\r
1093 if (Url != NULL) {\r
1094 FreePool (Url);\r
1095 }\r
1096\r
1097 return Status;\r
1098}\r
fa848a40 1099\r