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