]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/HttpBootDxe/HttpBootClient.c
NetworkPkg:Enable Http Boot over Ipv6 stack
[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
459/**\r
460 Get the file content from cached data.\r
461\r
462 @param[in] Private The pointer to the driver's private data.\r
463 @param[in] Uri Uri of the file to be retrieved from cache.\r
464 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return\r
465 code of EFI_SUCCESS, the amount of data transferred to\r
466 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
467 the size of Buffer required to retrieve the requested file.\r
468 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r
469 then the size of the requested file is returned in\r
470 BufferSize.\r
471\r
472 @retval EFI_SUCCESS Successfully created.\r
473 @retval Others Failed to create HttpIo.\r
474\r
475**/\r
476EFI_STATUS\r
477HttpBootGetFileFromCache (\r
478 IN HTTP_BOOT_PRIVATE_DATA *Private,\r
479 IN CHAR16 *Uri,\r
480 IN OUT UINTN *BufferSize,\r
481 OUT UINT8 *Buffer\r
482 )\r
483{\r
484 LIST_ENTRY *Entry;\r
485 LIST_ENTRY *Entry2;\r
486 HTTP_BOOT_CACHE_CONTENT *Cache;\r
487 HTTP_BOOT_ENTITY_DATA *EntityData;\r
488 UINTN CopyedSize;\r
489 \r
490 if (Uri == NULL || BufferSize == 0 || Buffer == NULL) {\r
491 return EFI_INVALID_PARAMETER;\r
492 }\r
493\r
494 NET_LIST_FOR_EACH (Entry, &Private->CacheList) {\r
495 Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);\r
496 //\r
497 // Compare the URI to see whether we already have a cache for this file.\r
498 //\r
499 if ((Cache->RequestData != NULL) &&\r
500 (Cache->RequestData->Url != NULL) &&\r
501 (StrCmp (Uri, Cache->RequestData->Url) == 0)) \r
502 {\r
503 //\r
504 // Hit cache, check buffer size.\r
505 //\r
506 if (*BufferSize < Cache->EntityLength) {\r
507 *BufferSize = Cache->EntityLength;\r
508 return EFI_BUFFER_TOO_SMALL;\r
509 }\r
510\r
511 //\r
512 // Fill data to buffer.\r
513 //\r
514 CopyedSize = 0;\r
515 NET_LIST_FOR_EACH (Entry2, &Cache->EntityDataList) {\r
516 EntityData = NET_LIST_USER_STRUCT (Entry2, HTTP_BOOT_ENTITY_DATA, Link);\r
517 if (*BufferSize > CopyedSize) {\r
518 CopyMem (\r
519 Buffer + CopyedSize,\r
520 EntityData->DataStart,\r
521 MIN (EntityData->DataLength, *BufferSize - CopyedSize)\r
522 );\r
523 CopyedSize += MIN (EntityData->DataLength, *BufferSize - CopyedSize);\r
524 }\r
525 }\r
526 *BufferSize = CopyedSize;\r
527 return EFI_SUCCESS;\r
528 }\r
529 }\r
530\r
531 return EFI_NOT_FOUND;\r
532}\r
533\r
534/**\r
535 Release all the resource of a cache item.\r
536\r
537 @param[in] Cache The pointer to the cache item.\r
538\r
539**/\r
540VOID\r
541HttpBootFreeCache (\r
542 IN HTTP_BOOT_CACHE_CONTENT *Cache\r
543 )\r
544{\r
545 UINTN Index;\r
546 LIST_ENTRY *Entry;\r
547 LIST_ENTRY *NextEntry;\r
548 HTTP_BOOT_ENTITY_DATA *EntityData;\r
549\r
550 if (Cache != NULL) {\r
551 //\r
552 // Free the request data\r
553 //\r
554 if (Cache->RequestData != NULL) {\r
555 if (Cache->RequestData->Url != NULL) {\r
556 FreePool (Cache->RequestData->Url);\r
557 }\r
558 FreePool (Cache->RequestData);\r
559 }\r
560\r
561 //\r
562 // Free the response header\r
563 //\r
564 if (Cache->ResponseData != NULL) {\r
565 if (Cache->ResponseData->Headers != NULL) {\r
566 for (Index = 0; Index < Cache->ResponseData->HeaderCount; Index++) {\r
567 FreePool (Cache->ResponseData->Headers[Index].FieldName);\r
568 FreePool (Cache->ResponseData->Headers[Index].FieldValue);\r
569 }\r
570 FreePool (Cache->ResponseData->Headers);\r
571 }\r
572 }\r
573\r
574 //\r
575 // Free the response body\r
576 //\r
577 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Cache->EntityDataList) {\r
578 EntityData = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_ENTITY_DATA, Link);\r
579 if (EntityData->Block != NULL) {\r
580 FreePool (EntityData->Block);\r
581 }\r
582 RemoveEntryList (&EntityData->Link);\r
583 FreePool (EntityData);\r
584 }\r
585\r
586 FreePool (Cache);\r
587 }\r
588}\r
589\r
590/**\r
591 Clean up all cached data.\r
592\r
593 @param[in] Private The pointer to the driver's private data.\r
594\r
595**/\r
596VOID\r
597HttpBootFreeCacheList (\r
598 IN HTTP_BOOT_PRIVATE_DATA *Private\r
599 )\r
600{\r
601 LIST_ENTRY *Entry;\r
602 LIST_ENTRY *NextEntry;\r
603 HTTP_BOOT_CACHE_CONTENT *Cache;\r
604 \r
605 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->CacheList) {\r
606 Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);\r
607 RemoveEntryList (&Cache->Link);\r
608 HttpBootFreeCache (Cache);\r
609 }\r
610}\r
611\r
612/**\r
613 A callback function to intercept events during message parser.\r
614\r
615 This function will be invoked during HttpParseMessageBody() with various events type. An error\r
616 return status of the callback function will cause the HttpParseMessageBody() aborted.\r
617\r
618 @param[in] EventType Event type of this callback call.\r
619 @param[in] Data A pointer to data buffer.\r
620 @param[in] Length Length in bytes of the Data.\r
621 @param[in] Context Callback context set by HttpInitMsgParser().\r
622\r
623 @retval EFI_SUCCESS Continue to parser the message body.\r
624 @retval Others Abort the parse.\r
625 \r
626**/\r
627EFI_STATUS\r
628EFIAPI\r
629HttpBootGetBootFileCallback (\r
630 IN HTTP_BODY_PARSE_EVENT EventType,\r
631 IN CHAR8 *Data,\r
632 IN UINTN Length,\r
633 IN VOID *Context\r
634 )\r
635{\r
636 HTTP_BOOT_CALLBACK_DATA *CallbackData;\r
637 HTTP_BOOT_ENTITY_DATA *NewEntityData;\r
638\r
639 //\r
640 // We only care about the entity data.\r
641 //\r
642 if (EventType != BodyParseEventOnData) {\r
643 return EFI_SUCCESS;\r
644 }\r
645\r
646 CallbackData = (HTTP_BOOT_CALLBACK_DATA *) Context;\r
d933e70a
JW
647 //\r
648 // Copy data if caller has provided a buffer.\r
649 //\r
650 if (CallbackData->BufferSize > CallbackData->CopyedSize) {\r
651 CopyMem (\r
652 CallbackData->Buffer + CallbackData->CopyedSize,\r
653 Data,\r
654 MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize)\r
655 );\r
656 CallbackData->CopyedSize += MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize);\r
657 }\r
658\r
7fd71047
FS
659 //\r
660 // The caller doesn't provide a buffer, save the block into cache list.\r
661 //\r
662 if (CallbackData->Cache != NULL) {\r
663 NewEntityData = AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA));\r
664 if (NewEntityData == NULL) {\r
665 return EFI_OUT_OF_RESOURCES;\r
666 }\r
667 if (CallbackData->NewBlock) {\r
668 NewEntityData->Block = CallbackData->Block;\r
669 CallbackData->Block = NULL;\r
670 }\r
671 NewEntityData->DataLength = Length;\r
672 NewEntityData->DataStart = (UINT8*) Data;\r
673 InsertTailList (&CallbackData->Cache->EntityDataList, &NewEntityData->Link);\r
674 }\r
d933e70a
JW
675 return EFI_SUCCESS;\r
676}\r
677\r
678/**\r
679 This function download the boot file by using UEFI HTTP protocol.\r
680 \r
681 @param[in] Private The pointer to the driver's private data.\r
682 @param[in] HeaderOnly Only request the response header, it could save a lot of time if\r
683 the caller only want to know the size of the requested file.\r
684 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return\r
685 code of EFI_SUCCESS, the amount of data transferred to\r
686 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
687 the size of Buffer required to retrieve the requested file.\r
688 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r
689 then the size of the requested file is returned in\r
690 BufferSize.\r
691\r
692 @retval EFI_SUCCESS The file was loaded.\r
693 @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.\r
694 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources\r
695 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.\r
696 BufferSize has been updated with the size needed to complete\r
697 the request.\r
698 @retval Others Unexpected error happened.\r
699\r
700**/\r
701EFI_STATUS\r
702HttpBootGetBootFile (\r
703 IN HTTP_BOOT_PRIVATE_DATA *Private,\r
704 IN BOOLEAN HeaderOnly,\r
705 IN OUT UINTN *BufferSize,\r
706 OUT UINT8 *Buffer\r
707 )\r
708{\r
709 EFI_STATUS Status;\r
710 CHAR8 *HostName;\r
711 EFI_HTTP_REQUEST_DATA *RequestData;\r
712 HTTP_IO_RESOPNSE_DATA *ResponseData;\r
713 HTTP_IO_RESOPNSE_DATA ResponseBody;\r
714 HTTP_IO *HttpIo;\r
715 HTTP_IO_HEADER *HttpIoHeader;\r
716 VOID *Parser;\r
717 HTTP_BOOT_CALLBACK_DATA Context;\r
718 UINTN ContentLength;\r
719 HTTP_BOOT_CACHE_CONTENT *Cache;\r
720 UINT8 *Block;\r
721 CHAR16 *Url;\r
722 \r
723 ASSERT (Private != NULL);\r
724 ASSERT (Private->HttpCreated);\r
725\r
726 if (BufferSize == NULL) {\r
727 return EFI_INVALID_PARAMETER;\r
728 }\r
729\r
730 if (*BufferSize != 0 && Buffer == NULL) {\r
731 return EFI_INVALID_PARAMETER;\r
732 }\r
733\r
734 //\r
735 // First, check whether we already cached the requested Uri.\r
736 //\r
737 Url = AllocatePool ((AsciiStrLen (Private->BootFileUri) + 1) * sizeof (CHAR16));\r
738 if (Url == NULL) {\r
739 return EFI_OUT_OF_RESOURCES;\r
740 }\r
741 AsciiStrToUnicodeStr (Private->BootFileUri, Url);\r
742 if (!HeaderOnly) {\r
743 Status = HttpBootGetFileFromCache (Private, Url, BufferSize, Buffer);\r
744 if (Status != EFI_NOT_FOUND) {\r
745 FreePool (Url);\r
746 return Status;\r
747 }\r
748 }\r
749\r
750 //\r
751 // Not found in cache, try to download it through HTTP.\r
752 //\r
753\r
754 //\r
7fd71047 755 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.\r
d933e70a
JW
756 //\r
757 Cache = NULL;\r
7fd71047 758 if ((!HeaderOnly) && (*BufferSize == 0)) {\r
d933e70a
JW
759 Cache = AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT));\r
760 if (Cache == NULL) {\r
761 Status = EFI_OUT_OF_RESOURCES;\r
762 goto ERROR_1;\r
763 }\r
764 InitializeListHead (&Cache->EntityDataList);\r
765 }\r
766\r
767 //\r
768 // 2. Send HTTP request message.\r
769 //\r
770\r
771 //\r
772 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:\r
773 // Host\r
774 // Accept\r
775 // User-Agent\r
776 //\r
777 HttpIoHeader = HttpBootCreateHeader (3);\r
778 if (HttpIoHeader == NULL) {\r
779 Status = EFI_OUT_OF_RESOURCES;\r
780 goto ERROR_2;\r
781 }\r
782\r
783 //\r
784 // Add HTTP header field 1: Host\r
785 //\r
786 HostName = NULL;\r
787 Status = HttpUrlGetHostName (\r
788 Private->BootFileUri,\r
789 Private->BootFileUriParser,\r
790 &HostName\r
791 );\r
792 if (EFI_ERROR (Status)) {\r
793 goto ERROR_3;\r
794 }\r
795 Status = HttpBootSetHeader (\r
796 HttpIoHeader,\r
797 HTTP_FIELD_NAME_HOST,\r
798 HostName\r
799 );\r
800 FreePool (HostName);\r
801 if (EFI_ERROR (Status)) {\r
802 goto ERROR_3;\r
803 }\r
804\r
805 //\r
806 // Add HTTP header field 2: Accept\r
807 //\r
808 Status = HttpBootSetHeader (\r
809 HttpIoHeader,\r
810 HTTP_FIELD_NAME_ACCEPT,\r
811 "*/*"\r
812 );\r
813 if (EFI_ERROR (Status)) {\r
814 goto ERROR_3;\r
815 }\r
816\r
817 //\r
818 // Add HTTP header field 3: User-Agent\r
819 //\r
820 Status = HttpBootSetHeader (\r
821 HttpIoHeader,\r
822 HTTP_FIELD_NAME_USER_AGENT,\r
823 HTTP_USER_AGENT_EFI_HTTP_BOOT\r
824 );\r
825 if (EFI_ERROR (Status)) {\r
826 goto ERROR_3;\r
827 }\r
828\r
829 //\r
830 // 2.2 Build the rest of HTTP request info.\r
831 //\r
832 RequestData = AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA));\r
833 if (RequestData == NULL) {\r
834 Status = EFI_OUT_OF_RESOURCES;\r
835 goto ERROR_3;\r
836 }\r
837 RequestData->Method = HeaderOnly ? HttpMethodHead : HttpMethodGet;\r
838 RequestData->Url = Url;\r
839 if (RequestData->Url == NULL) {\r
840 Status = EFI_OUT_OF_RESOURCES;\r
841 goto ERROR_4;\r
842 }\r
843 AsciiStrToUnicodeStr (Private->BootFileUri, RequestData->Url);\r
844\r
845 //\r
846 // 2.3 Record the request info in a temp cache item.\r
847 //\r
7fd71047 848 if (Cache != NULL) {\r
d933e70a
JW
849 Cache->RequestData = RequestData;\r
850 }\r
851\r
852 //\r
853 // 2.4 Send out the request to HTTP server.\r
854 //\r
855 HttpIo = &Private->HttpIo;\r
856 Status = HttpIoSendRequest (\r
857 HttpIo,\r
858 RequestData,\r
859 HttpIoHeader->HeaderCount,\r
860 HttpIoHeader->Headers,\r
861 0,\r
862 NULL\r
863 );\r
864 if (EFI_ERROR (Status)) {\r
865 goto ERROR_4;\r
866 }\r
867\r
868 //\r
869 // 3. Receive HTTP response message.\r
870 //\r
871\r
872 //\r
873 // 3.1 First step, use zero BodyLength to only receive the response headers.\r
874 //\r
875 ResponseData = AllocateZeroPool (sizeof(HTTP_IO_RESOPNSE_DATA));\r
876 if (ResponseData == NULL) {\r
877 Status = EFI_OUT_OF_RESOURCES;\r
878 goto ERROR_4;\r
879 }\r
880 Status = HttpIoRecvResponse (\r
881 &Private->HttpIo,\r
882 TRUE,\r
883 ResponseData\r
884 );\r
885 if (EFI_ERROR (Status)) {\r
886 goto ERROR_5;\r
887 }\r
888\r
889 //\r
890 // 3.2 Cache the response header.\r
891 //\r
7fd71047 892 if (Cache != NULL) {\r
d933e70a
JW
893 Cache->ResponseData = ResponseData;\r
894 }\r
895 \r
896 //\r
897 // 3.3 Init a message-body parser from the header information.\r
898 //\r
899 Parser = NULL;\r
900 Context.NewBlock = FALSE;\r
901 Context.Block = NULL;\r
902 Context.CopyedSize = 0;\r
903 Context.Buffer = Buffer;\r
904 Context.BufferSize = *BufferSize;\r
905 Context.Cache = Cache;\r
906 Status = HttpInitMsgParser (\r
907 HeaderOnly? HttpMethodHead : HttpMethodGet,\r
908 ResponseData->Response.StatusCode,\r
909 ResponseData->HeaderCount,\r
910 ResponseData->Headers,\r
911 HttpBootGetBootFileCallback,\r
912 (VOID*) &Context,\r
913 &Parser\r
914 );\r
915 if (EFI_ERROR (Status)) {\r
916 goto ERROR_6;\r
917 }\r
918\r
919 //\r
920 // 3.4 Continue to receive and parse message-body if needed.\r
921 //\r
7fd71047 922 Block = NULL;\r
d933e70a
JW
923 if (!HeaderOnly) {\r
924 ZeroMem (&ResponseBody, sizeof (HTTP_IO_RESOPNSE_DATA));\r
925 while (!HttpIsMessageComplete (Parser)) {\r
926 //\r
7fd71047
FS
927 // Allocate a block to hold the message-body, if caller doesn't provide\r
928 // a buffer, the block will be cached and we will allocate a new one here.\r
d933e70a 929 //\r
7fd71047
FS
930 if (Block == NULL || Context.BufferSize == 0) {\r
931 Block = AllocatePool (HTTP_BOOT_BLOCK_SIZE);\r
932 if (Block == NULL) {\r
933 Status = EFI_OUT_OF_RESOURCES;\r
934 goto ERROR_6;\r
935 }\r
936 Context.NewBlock = TRUE;\r
937 Context.Block = Block;\r
938 } else {\r
939 Context.NewBlock = FALSE;\r
d933e70a 940 }\r
7fd71047 941\r
d933e70a
JW
942 ResponseBody.Body = (CHAR8*) Block;\r
943 ResponseBody.BodyLength = HTTP_BOOT_BLOCK_SIZE;\r
944 Status = HttpIoRecvResponse (\r
945 &Private->HttpIo,\r
946 FALSE,\r
947 &ResponseBody\r
948 );\r
949 if (EFI_ERROR (Status)) {\r
950 goto ERROR_6;\r
951 }\r
952\r
953 //\r
954 // Parse the new received block of the message-body, the block will be saved in cache.\r
955 //\r
d933e70a
JW
956 Status = HttpParseMessageBody (\r
957 Parser,\r
958 ResponseBody.BodyLength,\r
959 ResponseBody.Body\r
960 );\r
961 if (EFI_ERROR (Status)) {\r
962 goto ERROR_6;\r
963 }\r
964 }\r
965 }\r
966 \r
967 //\r
968 // 3.5 Message-body receive & parse is completed, get the file size.\r
969 //\r
970 Status = HttpGetEntityLength (Parser, &ContentLength);\r
971 if (EFI_ERROR (Status)) {\r
972 goto ERROR_6;\r
973 }\r
974\r
975 if (*BufferSize < ContentLength) {\r
976 Status = EFI_BUFFER_TOO_SMALL;\r
977 }\r
978 *BufferSize = ContentLength;\r
979\r
980 //\r
981 // 4. Save the cache item to driver's cache list and return.\r
982 //\r
7fd71047 983 if (Cache != NULL) {\r
d933e70a
JW
984 Cache->EntityLength = ContentLength;\r
985 InsertTailList (&Private->CacheList, &Cache->Link);\r
986 }\r
987\r
988 if (Parser != NULL) {\r
989 HttpFreeMsgParser (Parser);\r
990 }\r
991\r
992 return EFI_SUCCESS;\r
993 \r
994ERROR_6:\r
995 if (Parser != NULL) {\r
996 HttpFreeMsgParser (Parser);\r
997 }\r
998 if (Context.Block != NULL) {\r
999 FreePool (Context.Block);\r
1000 }\r
1001 HttpBootFreeCache (Cache);\r
1002 \r
1003ERROR_5:\r
1004 if (ResponseData != NULL) {\r
1005 FreePool (ResponseData);\r
1006 }\r
1007ERROR_4:\r
1008 if (RequestData != NULL) {\r
1009 FreePool (RequestData);\r
1010 }\r
1011ERROR_3:\r
1012 HttpBootFreeHeader (HttpIoHeader);\r
1013ERROR_2:\r
1014 if (Cache != NULL) {\r
1015 FreePool (Cache);\r
1016 }\r
1017ERROR_1:\r
1018 if (Url != NULL) {\r
1019 FreePool (Url);\r
1020 }\r
1021\r
1022 return Status;\r
1023}\r