]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/HttpBootDxe/HttpBootClient.c
NetworkPkg: Fix some typos 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
4Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
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
720 CHAR8 *HostName;\r
721 EFI_HTTP_REQUEST_DATA *RequestData;\r
ef422fc5
PA
722 HTTP_IO_RESPONSE_DATA *ResponseData;\r
723 HTTP_IO_RESPONSE_DATA ResponseBody;\r
d933e70a
JW
724 HTTP_IO *HttpIo;\r
725 HTTP_IO_HEADER *HttpIoHeader;\r
726 VOID *Parser;\r
727 HTTP_BOOT_CALLBACK_DATA Context;\r
728 UINTN ContentLength;\r
729 HTTP_BOOT_CACHE_CONTENT *Cache;\r
730 UINT8 *Block;\r
731 CHAR16 *Url;\r
7552c24e
FS
732 BOOLEAN IdentityMode;\r
733 UINTN ReceivedSize;\r
d933e70a
JW
734 \r
735 ASSERT (Private != NULL);\r
736 ASSERT (Private->HttpCreated);\r
737\r
738 if (BufferSize == NULL) {\r
739 return EFI_INVALID_PARAMETER;\r
740 }\r
741\r
742 if (*BufferSize != 0 && Buffer == NULL) {\r
743 return EFI_INVALID_PARAMETER;\r
744 }\r
745\r
746 //\r
747 // First, check whether we already cached the requested Uri.\r
748 //\r
749 Url = AllocatePool ((AsciiStrLen (Private->BootFileUri) + 1) * sizeof (CHAR16));\r
750 if (Url == NULL) {\r
751 return EFI_OUT_OF_RESOURCES;\r
752 }\r
753 AsciiStrToUnicodeStr (Private->BootFileUri, Url);\r
754 if (!HeaderOnly) {\r
755 Status = HttpBootGetFileFromCache (Private, Url, BufferSize, Buffer);\r
756 if (Status != EFI_NOT_FOUND) {\r
757 FreePool (Url);\r
758 return Status;\r
759 }\r
760 }\r
761\r
762 //\r
763 // Not found in cache, try to download it through HTTP.\r
764 //\r
765\r
766 //\r
7fd71047 767 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.\r
d933e70a
JW
768 //\r
769 Cache = NULL;\r
7fd71047 770 if ((!HeaderOnly) && (*BufferSize == 0)) {\r
d933e70a
JW
771 Cache = AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT));\r
772 if (Cache == NULL) {\r
773 Status = EFI_OUT_OF_RESOURCES;\r
774 goto ERROR_1;\r
775 }\r
776 InitializeListHead (&Cache->EntityDataList);\r
777 }\r
778\r
779 //\r
780 // 2. Send HTTP request message.\r
781 //\r
782\r
783 //\r
784 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:\r
785 // Host\r
786 // Accept\r
787 // User-Agent\r
788 //\r
789 HttpIoHeader = HttpBootCreateHeader (3);\r
790 if (HttpIoHeader == NULL) {\r
791 Status = EFI_OUT_OF_RESOURCES;\r
792 goto ERROR_2;\r
793 }\r
794\r
795 //\r
796 // Add HTTP header field 1: Host\r
797 //\r
798 HostName = NULL;\r
799 Status = HttpUrlGetHostName (\r
800 Private->BootFileUri,\r
801 Private->BootFileUriParser,\r
802 &HostName\r
803 );\r
804 if (EFI_ERROR (Status)) {\r
805 goto ERROR_3;\r
806 }\r
807 Status = HttpBootSetHeader (\r
808 HttpIoHeader,\r
809 HTTP_FIELD_NAME_HOST,\r
810 HostName\r
811 );\r
812 FreePool (HostName);\r
813 if (EFI_ERROR (Status)) {\r
814 goto ERROR_3;\r
815 }\r
816\r
817 //\r
818 // Add HTTP header field 2: Accept\r
819 //\r
820 Status = HttpBootSetHeader (\r
821 HttpIoHeader,\r
822 HTTP_FIELD_NAME_ACCEPT,\r
823 "*/*"\r
824 );\r
825 if (EFI_ERROR (Status)) {\r
826 goto ERROR_3;\r
827 }\r
828\r
829 //\r
830 // Add HTTP header field 3: User-Agent\r
831 //\r
832 Status = HttpBootSetHeader (\r
833 HttpIoHeader,\r
834 HTTP_FIELD_NAME_USER_AGENT,\r
835 HTTP_USER_AGENT_EFI_HTTP_BOOT\r
836 );\r
837 if (EFI_ERROR (Status)) {\r
838 goto ERROR_3;\r
839 }\r
840\r
841 //\r
842 // 2.2 Build the rest of HTTP request info.\r
843 //\r
844 RequestData = AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA));\r
845 if (RequestData == NULL) {\r
846 Status = EFI_OUT_OF_RESOURCES;\r
847 goto ERROR_3;\r
848 }\r
849 RequestData->Method = HeaderOnly ? HttpMethodHead : HttpMethodGet;\r
850 RequestData->Url = Url;\r
851 if (RequestData->Url == NULL) {\r
852 Status = EFI_OUT_OF_RESOURCES;\r
853 goto ERROR_4;\r
854 }\r
855 AsciiStrToUnicodeStr (Private->BootFileUri, RequestData->Url);\r
856\r
857 //\r
858 // 2.3 Record the request info in a temp cache item.\r
859 //\r
7fd71047 860 if (Cache != NULL) {\r
d933e70a
JW
861 Cache->RequestData = RequestData;\r
862 }\r
863\r
864 //\r
865 // 2.4 Send out the request to HTTP server.\r
866 //\r
867 HttpIo = &Private->HttpIo;\r
868 Status = HttpIoSendRequest (\r
869 HttpIo,\r
870 RequestData,\r
871 HttpIoHeader->HeaderCount,\r
872 HttpIoHeader->Headers,\r
873 0,\r
874 NULL\r
875 );\r
876 if (EFI_ERROR (Status)) {\r
877 goto ERROR_4;\r
878 }\r
879\r
880 //\r
881 // 3. Receive HTTP response message.\r
882 //\r
883\r
884 //\r
885 // 3.1 First step, use zero BodyLength to only receive the response headers.\r
886 //\r
ef422fc5 887 ResponseData = AllocateZeroPool (sizeof(HTTP_IO_RESPONSE_DATA));\r
d933e70a
JW
888 if (ResponseData == NULL) {\r
889 Status = EFI_OUT_OF_RESOURCES;\r
890 goto ERROR_4;\r
891 }\r
892 Status = HttpIoRecvResponse (\r
893 &Private->HttpIo,\r
894 TRUE,\r
895 ResponseData\r
896 );\r
897 if (EFI_ERROR (Status)) {\r
898 goto ERROR_5;\r
899 }\r
900\r
901 //\r
902 // 3.2 Cache the response header.\r
903 //\r
7fd71047 904 if (Cache != NULL) {\r
d933e70a
JW
905 Cache->ResponseData = ResponseData;\r
906 }\r
907 \r
908 //\r
909 // 3.3 Init a message-body parser from the header information.\r
910 //\r
911 Parser = NULL;\r
912 Context.NewBlock = FALSE;\r
913 Context.Block = NULL;\r
914 Context.CopyedSize = 0;\r
915 Context.Buffer = Buffer;\r
916 Context.BufferSize = *BufferSize;\r
917 Context.Cache = Cache;\r
918 Status = HttpInitMsgParser (\r
919 HeaderOnly? HttpMethodHead : HttpMethodGet,\r
920 ResponseData->Response.StatusCode,\r
921 ResponseData->HeaderCount,\r
922 ResponseData->Headers,\r
923 HttpBootGetBootFileCallback,\r
924 (VOID*) &Context,\r
925 &Parser\r
926 );\r
927 if (EFI_ERROR (Status)) {\r
928 goto ERROR_6;\r
929 }\r
930\r
931 //\r
932 // 3.4 Continue to receive and parse message-body if needed.\r
933 //\r
7fd71047 934 Block = NULL;\r
d933e70a 935 if (!HeaderOnly) {\r
7552c24e
FS
936 //\r
937 // 3.4.1, check whether we are in identity transfer-coding.\r
938 //\r
939 ContentLength = 0;\r
940 Status = HttpGetEntityLength (Parser, &ContentLength);\r
941 if (!EFI_ERROR (Status)) {\r
942 IdentityMode = TRUE;\r
943 } else {\r
944 IdentityMode = FALSE;\r
945 }\r
946\r
947 //\r
948 // 3.4.2, start the message-body download, the identity and chunked transfer-coding\r
949 // is handled in different path here.\r
950 //\r
ef422fc5 951 ZeroMem (&ResponseBody, sizeof (HTTP_IO_RESPONSE_DATA));\r
7552c24e 952 if (IdentityMode) {\r
d933e70a 953 //\r
7552c24e
FS
954 // In identity transfer-coding there is no need to parse the message body,\r
955 // just download the message body to the user provided buffer directly.\r
d933e70a 956 //\r
7552c24e
FS
957 ReceivedSize = 0;\r
958 while (ReceivedSize < ContentLength) {\r
959 ResponseBody.Body = (CHAR8*) Buffer + ReceivedSize;\r
960 ResponseBody.BodyLength = *BufferSize - ReceivedSize;\r
961 Status = HttpIoRecvResponse (\r
962 &Private->HttpIo,\r
963 FALSE,\r
964 &ResponseBody\r
965 );\r
966 if (EFI_ERROR (Status)) {\r
7fd71047
FS
967 goto ERROR_6;\r
968 }\r
7552c24e 969 ReceivedSize += ResponseBody.BodyLength;\r
d933e70a 970 }\r
7552c24e 971 } else {\r
d933e70a 972 //\r
7552c24e
FS
973 // In "chunked" transfer-coding mode, so we need to parse the received\r
974 // data to get the real entity content.\r
d933e70a 975 //\r
7552c24e
FS
976 Block = NULL;\r
977 while (!HttpIsMessageComplete (Parser)) {\r
978 //\r
979 // Allocate a buffer in Block to hold the message-body.\r
980 // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().\r
981 // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before\r
982 // every HttpIoRecvResponse().\r
983 //\r
984 if (Block == NULL || Context.BufferSize == 0) {\r
985 Block = AllocatePool (HTTP_BOOT_BLOCK_SIZE);\r
986 if (Block == NULL) {\r
987 Status = EFI_OUT_OF_RESOURCES;\r
988 goto ERROR_6;\r
989 }\r
990 Context.NewBlock = TRUE;\r
991 Context.Block = Block;\r
992 } else {\r
993 Context.NewBlock = FALSE;\r
994 }\r
995\r
996 ResponseBody.Body = (CHAR8*) Block;\r
997 ResponseBody.BodyLength = HTTP_BOOT_BLOCK_SIZE;\r
998 Status = HttpIoRecvResponse (\r
999 &Private->HttpIo,\r
1000 FALSE,\r
1001 &ResponseBody\r
1002 );\r
1003 if (EFI_ERROR (Status)) {\r
1004 goto ERROR_6;\r
1005 }\r
1006\r
1007 //\r
1008 // Parse the new received block of the message-body, the block will be saved in cache.\r
1009 //\r
1010 Status = HttpParseMessageBody (\r
1011 Parser,\r
1012 ResponseBody.BodyLength,\r
1013 ResponseBody.Body\r
1014 );\r
1015 if (EFI_ERROR (Status)) {\r
1016 goto ERROR_6;\r
1017 }\r
d933e70a
JW
1018 }\r
1019 }\r
1020 }\r
7552c24e 1021\r
d933e70a 1022 //\r
7552c24e 1023 // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.\r
d933e70a
JW
1024 //\r
1025 Status = HttpGetEntityLength (Parser, &ContentLength);\r
1026 if (EFI_ERROR (Status)) {\r
1027 goto ERROR_6;\r
1028 }\r
1029\r
1030 if (*BufferSize < ContentLength) {\r
1031 Status = EFI_BUFFER_TOO_SMALL;\r
1032 }\r
1033 *BufferSize = ContentLength;\r
1034\r
1035 //\r
1036 // 4. Save the cache item to driver's cache list and return.\r
1037 //\r
7fd71047 1038 if (Cache != NULL) {\r
d933e70a
JW
1039 Cache->EntityLength = ContentLength;\r
1040 InsertTailList (&Private->CacheList, &Cache->Link);\r
1041 }\r
1042\r
1043 if (Parser != NULL) {\r
1044 HttpFreeMsgParser (Parser);\r
1045 }\r
1046\r
1047 return EFI_SUCCESS;\r
1048 \r
1049ERROR_6:\r
1050 if (Parser != NULL) {\r
1051 HttpFreeMsgParser (Parser);\r
1052 }\r
1053 if (Context.Block != NULL) {\r
1054 FreePool (Context.Block);\r
1055 }\r
1056 HttpBootFreeCache (Cache);\r
1057 \r
1058ERROR_5:\r
1059 if (ResponseData != NULL) {\r
1060 FreePool (ResponseData);\r
1061 }\r
1062ERROR_4:\r
1063 if (RequestData != NULL) {\r
1064 FreePool (RequestData);\r
1065 }\r
1066ERROR_3:\r
1067 HttpBootFreeHeader (HttpIoHeader);\r
1068ERROR_2:\r
1069 if (Cache != NULL) {\r
1070 FreePool (Cache);\r
1071 }\r
1072ERROR_1:\r
1073 if (Url != NULL) {\r
1074 FreePool (Url);\r
1075 }\r
1076\r
1077 return Status;\r
1078}\r