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