]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpBootDxe/HttpBootSupport.c
NetworkPkg/DxeHttpLib: Migrate HTTP header manipulation APIs
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootSupport.c
1 /** @file
2 Support functions implementation for UEFI HTTP boot driver.
3
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 - 2020 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "HttpBootDxe.h"
11
12
13 /**
14 Get the Nic handle using any child handle in the IPv4 stack.
15
16 @param[in] ControllerHandle Pointer to child handle over IPv4.
17
18 @return NicHandle The pointer to the Nic handle.
19 @return NULL Can't find the Nic handle.
20
21 **/
22 EFI_HANDLE
23 HttpBootGetNicByIp4Children (
24 IN EFI_HANDLE ControllerHandle
25 )
26 {
27 EFI_HANDLE NicHandle;
28
29 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiHttpProtocolGuid);
30 if (NicHandle == NULL) {
31 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp4ProtocolGuid);
32 if (NicHandle == NULL) {
33 return NULL;
34 }
35 }
36
37 return NicHandle;
38 }
39
40 /**
41 Get the Nic handle using any child handle in the IPv6 stack.
42
43 @param[in] ControllerHandle Pointer to child handle over IPv6.
44
45 @return NicHandle The pointer to the Nic handle.
46 @return NULL Can't find the Nic handle.
47
48 **/
49 EFI_HANDLE
50 HttpBootGetNicByIp6Children (
51 IN EFI_HANDLE ControllerHandle
52 )
53 {
54 EFI_HANDLE NicHandle;
55 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiHttpProtocolGuid);
56 if (NicHandle == NULL) {
57 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp6ProtocolGuid);
58 if (NicHandle == NULL) {
59 return NULL;
60 }
61 }
62
63 return NicHandle;
64 }
65
66 /**
67 This function is to convert UINTN to ASCII string with the required formatting.
68
69 @param[in] Number Numeric value to be converted.
70 @param[in] Buffer The pointer to the buffer for ASCII string.
71 @param[in] Length The length of the required format.
72
73 **/
74 VOID
75 HttpBootUintnToAscDecWithFormat (
76 IN UINTN Number,
77 IN UINT8 *Buffer,
78 IN INTN Length
79 )
80 {
81 UINTN Remainder;
82
83 for (; Length > 0; Length--) {
84 Remainder = Number % 10;
85 Number /= 10;
86 Buffer[Length - 1] = (UINT8) ('0' + Remainder);
87 }
88 }
89
90 /**
91 This function is to display the IPv4 address.
92
93 @param[in] Ip The pointer to the IPv4 address.
94
95 **/
96 VOID
97 HttpBootShowIp4Addr (
98 IN EFI_IPv4_ADDRESS *Ip
99 )
100 {
101 UINTN Index;
102
103 for (Index = 0; Index < 4; Index++) {
104 AsciiPrint ("%d", Ip->Addr[Index]);
105 if (Index < 3) {
106 AsciiPrint (".");
107 }
108 }
109 }
110
111 /**
112 This function is to display the IPv6 address.
113
114 @param[in] Ip The pointer to the IPv6 address.
115
116 **/
117 VOID
118 HttpBootShowIp6Addr (
119 IN EFI_IPv6_ADDRESS *Ip
120 )
121 {
122 UINTN Index;
123
124 for (Index = 0; Index < 16; Index++) {
125
126 if (Ip->Addr[Index] != 0) {
127 AsciiPrint ("%x", Ip->Addr[Index]);
128 }
129 Index++;
130 if (Index > 15) {
131 return;
132 }
133 if (((Ip->Addr[Index] & 0xf0) == 0) && (Ip->Addr[Index - 1] != 0)) {
134 AsciiPrint ("0");
135 }
136 AsciiPrint ("%x", Ip->Addr[Index]);
137 if (Index < 15) {
138 AsciiPrint (":");
139 }
140 }
141 }
142
143 /**
144 This function is to display the HTTP error status.
145
146 @param[in] StatusCode The status code value in HTTP message.
147
148 **/
149 VOID
150 HttpBootPrintErrorMessage (
151 EFI_HTTP_STATUS_CODE StatusCode
152 )
153 {
154 AsciiPrint ("\n");
155
156 switch (StatusCode) {
157 case HTTP_STATUS_300_MULTIPLE_CHOICES:
158 AsciiPrint ("\n Redirection: 300 Multiple Choices");
159 break;
160
161 case HTTP_STATUS_301_MOVED_PERMANENTLY:
162 AsciiPrint ("\n Redirection: 301 Moved Permanently");
163 break;
164
165 case HTTP_STATUS_302_FOUND:
166 AsciiPrint ("\n Redirection: 302 Found");
167 break;
168
169 case HTTP_STATUS_303_SEE_OTHER:
170 AsciiPrint ("\n Redirection: 303 See Other");
171 break;
172
173 case HTTP_STATUS_304_NOT_MODIFIED:
174 AsciiPrint ("\n Redirection: 304 Not Modified");
175 break;
176
177 case HTTP_STATUS_305_USE_PROXY:
178 AsciiPrint ("\n Redirection: 305 Use Proxy");
179 break;
180
181 case HTTP_STATUS_307_TEMPORARY_REDIRECT:
182 AsciiPrint ("\n Redirection: 307 Temporary Redirect");
183 break;
184
185 case HTTP_STATUS_308_PERMANENT_REDIRECT:
186 AsciiPrint ("\n Redirection: 308 Permanent Redirect");
187 break;
188
189 case HTTP_STATUS_400_BAD_REQUEST:
190 AsciiPrint ("\n Client Error: 400 Bad Request");
191 break;
192
193 case HTTP_STATUS_401_UNAUTHORIZED:
194 AsciiPrint ("\n Client Error: 401 Unauthorized");
195 break;
196
197 case HTTP_STATUS_402_PAYMENT_REQUIRED:
198 AsciiPrint ("\n Client Error: 402 Payment Required");
199 break;
200
201 case HTTP_STATUS_403_FORBIDDEN:
202 AsciiPrint ("\n Client Error: 403 Forbidden");
203 break;
204
205 case HTTP_STATUS_404_NOT_FOUND:
206 AsciiPrint ("\n Client Error: 404 Not Found");
207 break;
208
209 case HTTP_STATUS_405_METHOD_NOT_ALLOWED:
210 AsciiPrint ("\n Client Error: 405 Method Not Allowed");
211 break;
212
213 case HTTP_STATUS_406_NOT_ACCEPTABLE:
214 AsciiPrint ("\n Client Error: 406 Not Acceptable");
215 break;
216
217 case HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED:
218 AsciiPrint ("\n Client Error: 407 Proxy Authentication Required");
219 break;
220
221 case HTTP_STATUS_408_REQUEST_TIME_OUT:
222 AsciiPrint ("\n Client Error: 408 Request Timeout");
223 break;
224
225 case HTTP_STATUS_409_CONFLICT:
226 AsciiPrint ("\n Client Error: 409 Conflict");
227 break;
228
229 case HTTP_STATUS_410_GONE:
230 AsciiPrint ("\n Client Error: 410 Gone");
231 break;
232
233 case HTTP_STATUS_411_LENGTH_REQUIRED:
234 AsciiPrint ("\n Client Error: 411 Length Required");
235 break;
236
237 case HTTP_STATUS_412_PRECONDITION_FAILED:
238 AsciiPrint ("\n Client Error: 412 Precondition Failed");
239 break;
240
241 case HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE:
242 AsciiPrint ("\n Client Error: 413 Request Entity Too Large");
243 break;
244
245 case HTTP_STATUS_414_REQUEST_URI_TOO_LARGE:
246 AsciiPrint ("\n Client Error: 414 Request URI Too Long");
247 break;
248
249 case HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE:
250 AsciiPrint ("\n Client Error: 415 Unsupported Media Type");
251 break;
252
253 case HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED:
254 AsciiPrint ("\n Client Error: 416 Requested Range Not Satisfiable");
255 break;
256
257 case HTTP_STATUS_417_EXPECTATION_FAILED:
258 AsciiPrint ("\n Client Error: 417 Expectation Failed");
259 break;
260
261 case HTTP_STATUS_500_INTERNAL_SERVER_ERROR:
262 AsciiPrint ("\n Server Error: 500 Internal Server Error");
263 break;
264
265 case HTTP_STATUS_501_NOT_IMPLEMENTED:
266 AsciiPrint ("\n Server Error: 501 Not Implemented");
267 break;
268
269 case HTTP_STATUS_502_BAD_GATEWAY:
270 AsciiPrint ("\n Server Error: 502 Bad Gateway");
271 break;
272
273 case HTTP_STATUS_503_SERVICE_UNAVAILABLE:
274 AsciiPrint ("\n Server Error: 503 Service Unavailable");
275 break;
276
277 case HTTP_STATUS_504_GATEWAY_TIME_OUT:
278 AsciiPrint ("\n Server Error: 504 Gateway Timeout");
279 break;
280
281 case HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED:
282 AsciiPrint ("\n Server Error: 505 HTTP Version Not Supported");
283 break;
284
285 default: ;
286
287 }
288 }
289
290 /**
291 Notify the callback function when an event is triggered.
292
293 @param[in] Event The triggered event.
294 @param[in] Context The opaque parameter to the function.
295
296 **/
297 VOID
298 EFIAPI
299 HttpBootCommonNotify (
300 IN EFI_EVENT Event,
301 IN VOID *Context
302 )
303 {
304 *((BOOLEAN *) Context) = TRUE;
305 }
306
307 /**
308 Retrieve the host address using the EFI_DNS6_PROTOCOL.
309
310 @param[in] Private The pointer to the driver's private data.
311 @param[in] HostName Pointer to buffer containing hostname.
312 @param[out] IpAddress On output, pointer to buffer containing IPv6 address.
313
314 @retval EFI_SUCCESS Operation succeeded.
315 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
316 @retval Others Other errors as indicated.
317 **/
318 EFI_STATUS
319 HttpBootDns (
320 IN HTTP_BOOT_PRIVATE_DATA *Private,
321 IN CHAR16 *HostName,
322 OUT EFI_IPv6_ADDRESS *IpAddress
323 )
324 {
325 EFI_STATUS Status;
326 EFI_DNS6_PROTOCOL *Dns6;
327 EFI_DNS6_CONFIG_DATA Dns6ConfigData;
328 EFI_DNS6_COMPLETION_TOKEN Token;
329 EFI_HANDLE Dns6Handle;
330 EFI_IP6_CONFIG_PROTOCOL *Ip6Config;
331 EFI_IPv6_ADDRESS *DnsServerList;
332 UINTN DnsServerListCount;
333 UINTN DataSize;
334 BOOLEAN IsDone;
335
336 DnsServerList = NULL;
337 DnsServerListCount = 0;
338 Dns6 = NULL;
339 Dns6Handle = NULL;
340 ZeroMem (&Token, sizeof (EFI_DNS6_COMPLETION_TOKEN));
341
342 //
343 // Get DNS server list from EFI IPv6 Configuration protocol.
344 //
345 Status = gBS->HandleProtocol (Private->Controller, &gEfiIp6ConfigProtocolGuid, (VOID **) &Ip6Config);
346 if (!EFI_ERROR (Status)) {
347 //
348 // Get the required size.
349 //
350 DataSize = 0;
351 Status = Ip6Config->GetData (Ip6Config, Ip6ConfigDataTypeDnsServer, &DataSize, NULL);
352 if (Status == EFI_BUFFER_TOO_SMALL) {
353 DnsServerList = AllocatePool (DataSize);
354 if (DnsServerList == NULL) {
355 return EFI_OUT_OF_RESOURCES;
356 }
357
358 Status = Ip6Config->GetData (Ip6Config, Ip6ConfigDataTypeDnsServer, &DataSize, DnsServerList);
359 if (EFI_ERROR (Status)) {
360 FreePool (DnsServerList);
361 DnsServerList = NULL;
362 } else {
363 DnsServerListCount = DataSize / sizeof (EFI_IPv6_ADDRESS);
364 }
365 }
366 }
367 //
368 // Create a DNSv6 child instance and get the protocol.
369 //
370 Status = NetLibCreateServiceChild (
371 Private->Controller,
372 Private->Ip6Nic->ImageHandle,
373 &gEfiDns6ServiceBindingProtocolGuid,
374 &Dns6Handle
375 );
376 if (EFI_ERROR (Status)) {
377 goto Exit;
378 }
379
380 Status = gBS->OpenProtocol (
381 Dns6Handle,
382 &gEfiDns6ProtocolGuid,
383 (VOID **) &Dns6,
384 Private->Ip6Nic->ImageHandle,
385 Private->Controller,
386 EFI_OPEN_PROTOCOL_BY_DRIVER
387 );
388 if (EFI_ERROR (Status)) {
389 goto Exit;
390 }
391
392 //
393 // Configure DNS6 instance for the DNS server address and protocol.
394 //
395 ZeroMem (&Dns6ConfigData, sizeof (EFI_DNS6_CONFIG_DATA));
396 Dns6ConfigData.DnsServerCount = (UINT32)DnsServerListCount;
397 Dns6ConfigData.DnsServerList = DnsServerList;
398 Dns6ConfigData.EnableDnsCache = TRUE;
399 Dns6ConfigData.Protocol = EFI_IP_PROTO_UDP;
400 IP6_COPY_ADDRESS (&Dns6ConfigData.StationIp,&Private->StationIp.v6);
401 Status = Dns6->Configure (
402 Dns6,
403 &Dns6ConfigData
404 );
405 if (EFI_ERROR (Status)) {
406 goto Exit;
407 }
408
409 Token.Status = EFI_NOT_READY;
410 IsDone = FALSE;
411 //
412 // Create event to set the IsDone flag when name resolution is finished.
413 //
414 Status = gBS->CreateEvent (
415 EVT_NOTIFY_SIGNAL,
416 TPL_NOTIFY,
417 HttpBootCommonNotify,
418 &IsDone,
419 &Token.Event
420 );
421 if (EFI_ERROR (Status)) {
422 goto Exit;
423 }
424
425 //
426 // Start asynchronous name resolution.
427 //
428 Status = Dns6->HostNameToIp (Dns6, HostName, &Token);
429 if (EFI_ERROR (Status)) {
430 goto Exit;
431 }
432
433 while (!IsDone) {
434 Dns6->Poll (Dns6);
435 }
436
437 //
438 // Name resolution is done, check result.
439 //
440 Status = Token.Status;
441 if (!EFI_ERROR (Status)) {
442 if (Token.RspData.H2AData == NULL) {
443 Status = EFI_DEVICE_ERROR;
444 goto Exit;
445 }
446 if (Token.RspData.H2AData->IpCount == 0 || Token.RspData.H2AData->IpList == NULL) {
447 Status = EFI_DEVICE_ERROR;
448 goto Exit;
449 }
450 //
451 // We just return the first IPv6 address from DNS protocol.
452 //
453 IP6_COPY_ADDRESS (IpAddress, Token.RspData.H2AData->IpList);
454 Status = EFI_SUCCESS;
455 }
456 Exit:
457
458 if (Token.Event != NULL) {
459 gBS->CloseEvent (Token.Event);
460 }
461 if (Token.RspData.H2AData != NULL) {
462 if (Token.RspData.H2AData->IpList != NULL) {
463 FreePool (Token.RspData.H2AData->IpList);
464 }
465 FreePool (Token.RspData.H2AData);
466 }
467
468 if (Dns6 != NULL) {
469 Dns6->Configure (Dns6, NULL);
470
471 gBS->CloseProtocol (
472 Dns6Handle,
473 &gEfiDns6ProtocolGuid,
474 Private->Ip6Nic->ImageHandle,
475 Private->Controller
476 );
477 }
478
479 if (Dns6Handle != NULL) {
480 NetLibDestroyServiceChild (
481 Private->Controller,
482 Private->Ip6Nic->ImageHandle,
483 &gEfiDns6ServiceBindingProtocolGuid,
484 Dns6Handle
485 );
486 }
487
488 if (DnsServerList != NULL) {
489 FreePool (DnsServerList);
490 }
491
492 return Status;
493 }
494
495 /**
496 This function checks the HTTP(S) URI scheme.
497
498 @param[in] Uri The pointer to the URI string.
499
500 @retval EFI_SUCCESS The URI scheme is valid.
501 @retval EFI_INVALID_PARAMETER The URI scheme is not HTTP or HTTPS.
502 @retval EFI_ACCESS_DENIED HTTP is disabled and the URI is HTTP.
503
504 **/
505 EFI_STATUS
506 HttpBootCheckUriScheme (
507 IN CHAR8 *Uri
508 )
509 {
510 UINTN Index;
511 EFI_STATUS Status;
512
513 Status = EFI_SUCCESS;
514
515 //
516 // Convert the scheme to all lower case.
517 //
518 for (Index = 0; Index < AsciiStrLen (Uri); Index++) {
519 if (Uri[Index] == ':') {
520 break;
521 }
522 if (Uri[Index] >= 'A' && Uri[Index] <= 'Z') {
523 Uri[Index] -= (CHAR8)('A' - 'a');
524 }
525 }
526
527 //
528 // Return EFI_INVALID_PARAMETER if the URI is not HTTP or HTTPS.
529 //
530 if ((AsciiStrnCmp (Uri, "http://", 7) != 0) && (AsciiStrnCmp (Uri, "https://", 8) != 0)) {
531 DEBUG ((EFI_D_ERROR, "HttpBootCheckUriScheme: Invalid Uri.\n"));
532 return EFI_INVALID_PARAMETER;
533 }
534
535 //
536 // HTTP is disabled, return EFI_ACCESS_DENIED if the URI is HTTP.
537 //
538 if (!PcdGetBool (PcdAllowHttpConnections) && (AsciiStrnCmp (Uri, "http://", 7) == 0)) {
539 DEBUG ((EFI_D_ERROR, "HttpBootCheckUriScheme: HTTP is disabled.\n"));
540 return EFI_ACCESS_DENIED;
541 }
542
543 return Status;
544 }
545
546 /**
547 Get the URI address string from the input device path.
548
549 Caller need to free the buffer in the UriAddress pointer.
550
551 @param[in] FilePath Pointer to the device path which contains a URI device path node.
552 @param[out] UriAddress The URI address string extract from the device path.
553
554 @retval EFI_SUCCESS The URI string is returned.
555 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
556
557 **/
558 EFI_STATUS
559 HttpBootParseFilePath (
560 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
561 OUT CHAR8 **UriAddress
562 )
563 {
564 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
565 URI_DEVICE_PATH *UriDevicePath;
566 CHAR8 *Uri;
567 UINTN UriStrLength;
568
569 if (FilePath == NULL) {
570 return EFI_INVALID_PARAMETER;
571 }
572
573 *UriAddress = NULL;
574
575 //
576 // Extract the URI address from the FilePath
577 //
578 TempDevicePath = FilePath;
579 while (!IsDevicePathEnd (TempDevicePath)) {
580 if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&
581 (DevicePathSubType (TempDevicePath) == MSG_URI_DP)) {
582 UriDevicePath = (URI_DEVICE_PATH*) TempDevicePath;
583 //
584 // UEFI Spec doesn't require the URI to be a NULL-terminated string
585 // So we allocate a new buffer and always append a '\0' to it.
586 //
587 UriStrLength = DevicePathNodeLength (UriDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);
588 if (UriStrLength == 0) {
589 //
590 // return a NULL UriAddress if it's a empty URI device path node.
591 //
592 break;
593 }
594 Uri = AllocatePool (UriStrLength + 1);
595 if (Uri == NULL) {
596 return EFI_OUT_OF_RESOURCES;
597 }
598 CopyMem (Uri, UriDevicePath->Uri, DevicePathNodeLength (UriDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL));
599 Uri[DevicePathNodeLength (UriDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL)] = '\0';
600
601 *UriAddress = Uri;
602 }
603 TempDevicePath = NextDevicePathNode (TempDevicePath);
604 }
605
606 return EFI_SUCCESS;
607 }
608
609 /**
610 This function returns the image type according to server replied HTTP message
611 and also the image's URI info.
612
613 @param[in] Uri The pointer to the image's URI string.
614 @param[in] UriParser URI Parse result returned by NetHttpParseUrl().
615 @param[in] HeaderCount Number of HTTP header structures in Headers list.
616 @param[in] Headers Array containing list of HTTP headers.
617 @param[out] ImageType The image type of the downloaded file.
618
619 @retval EFI_SUCCESS The image type is returned in ImageType.
620 @retval EFI_INVALID_PARAMETER ImageType, Uri or UriParser is NULL.
621 @retval EFI_INVALID_PARAMETER HeaderCount is not zero, and Headers is NULL.
622 @retval EFI_NOT_FOUND Failed to identify the image type.
623 @retval Others Unexpected error happened.
624
625 **/
626 EFI_STATUS
627 HttpBootCheckImageType (
628 IN CHAR8 *Uri,
629 IN VOID *UriParser,
630 IN UINTN HeaderCount,
631 IN EFI_HTTP_HEADER *Headers,
632 OUT HTTP_BOOT_IMAGE_TYPE *ImageType
633 )
634 {
635 EFI_STATUS Status;
636 EFI_HTTP_HEADER *Header;
637 CHAR8 *FilePath;
638 CHAR8 *FilePost;
639
640 if (Uri == NULL || UriParser == NULL || ImageType == NULL) {
641 return EFI_INVALID_PARAMETER;
642 }
643
644 if (HeaderCount != 0 && Headers == NULL) {
645 return EFI_INVALID_PARAMETER;
646 }
647
648 //
649 // Determine the image type by the HTTP Content-Type header field first.
650 // "application/efi" -> EFI Image
651 // "application/vnd.efi-iso" -> CD/DVD Image
652 // "application/vnd.efi-img" -> Virtual Disk Image
653 //
654 Header = HttpFindHeader (HeaderCount, Headers, HTTP_HEADER_CONTENT_TYPE);
655 if (Header != NULL) {
656 if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_EFI) == 0) {
657 *ImageType = ImageTypeEfi;
658 return EFI_SUCCESS;
659 } else if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_ISO) == 0) {
660 *ImageType = ImageTypeVirtualCd;
661 return EFI_SUCCESS;
662 } else if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_IMG) == 0) {
663 *ImageType = ImageTypeVirtualDisk;
664 return EFI_SUCCESS;
665 }
666 }
667
668 //
669 // Determine the image type by file extension:
670 // *.efi -> EFI Image
671 // *.iso -> CD/DVD Image
672 // *.img -> Virtual Disk Image
673 //
674 Status = HttpUrlGetPath (
675 Uri,
676 UriParser,
677 &FilePath
678 );
679 if (EFI_ERROR (Status)) {
680 return Status;
681 }
682
683 FilePost = FilePath + AsciiStrLen (FilePath) - 4;
684 if (AsciiStrCmp (FilePost, ".efi") == 0) {
685 *ImageType = ImageTypeEfi;
686 } else if (AsciiStrCmp (FilePost, ".iso") == 0) {
687 *ImageType = ImageTypeVirtualCd;
688 } else if (AsciiStrCmp (FilePost, ".img") == 0) {
689 *ImageType = ImageTypeVirtualDisk;
690 } else {
691 *ImageType = ImageTypeMax;
692 }
693
694 FreePool (FilePath);
695
696 return (*ImageType < ImageTypeMax) ? EFI_SUCCESS : EFI_NOT_FOUND;
697 }
698
699 /**
700 This function register the RAM disk info to the system.
701
702 @param[in] Private The pointer to the driver's private data.
703 @param[in] BufferSize The size of Buffer in bytes.
704 @param[in] Buffer The base address of the RAM disk.
705 @param[in] ImageType The image type of the file in Buffer.
706
707 @retval EFI_SUCCESS The RAM disk has been registered.
708 @retval EFI_NOT_FOUND No RAM disk protocol instances were found.
709 @retval EFI_UNSUPPORTED The ImageType is not supported.
710 @retval Others Unexpected error happened.
711
712 **/
713 EFI_STATUS
714 HttpBootRegisterRamDisk (
715 IN HTTP_BOOT_PRIVATE_DATA *Private,
716 IN UINTN BufferSize,
717 IN VOID *Buffer,
718 IN HTTP_BOOT_IMAGE_TYPE ImageType
719 )
720 {
721 EFI_RAM_DISK_PROTOCOL *RamDisk;
722 EFI_STATUS Status;
723 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
724 EFI_GUID *RamDiskType;
725
726 ASSERT (Private != NULL);
727 ASSERT (Buffer != NULL);
728 ASSERT (BufferSize != 0);
729
730 Status = gBS->LocateProtocol (&gEfiRamDiskProtocolGuid, NULL, (VOID**) &RamDisk);
731 if (EFI_ERROR (Status)) {
732 DEBUG ((EFI_D_ERROR, "HTTP Boot: Couldn't find the RAM Disk protocol - %r\n", Status));
733 return Status;
734 }
735
736 if (ImageType == ImageTypeVirtualCd) {
737 RamDiskType = &gEfiVirtualCdGuid;
738 } else if (ImageType == ImageTypeVirtualDisk) {
739 RamDiskType = &gEfiVirtualDiskGuid;
740 } else {
741 return EFI_UNSUPPORTED;
742 }
743
744 Status = RamDisk->Register (
745 (UINTN)Buffer,
746 (UINT64)BufferSize,
747 RamDiskType,
748 Private->UsingIpv6 ? Private->Ip6Nic->DevicePath : Private->Ip4Nic->DevicePath,
749 &DevicePath
750 );
751 if (EFI_ERROR (Status)) {
752 DEBUG ((EFI_D_ERROR, "HTTP Boot: Failed to register RAM Disk - %r\n", Status));
753 }
754
755 return Status;
756 }
757
758 /**
759 Indicate if the HTTP status code indicates a redirection.
760
761 @param[in] StatusCode HTTP status code from server.
762
763 @return TRUE if it's redirection.
764
765 **/
766 BOOLEAN
767 HttpBootIsHttpRedirectStatusCode (
768 IN EFI_HTTP_STATUS_CODE StatusCode
769 )
770 {
771 if (StatusCode == HTTP_STATUS_301_MOVED_PERMANENTLY ||
772 StatusCode == HTTP_STATUS_302_FOUND ||
773 StatusCode == HTTP_STATUS_307_TEMPORARY_REDIRECT ||
774 StatusCode == HTTP_STATUS_308_PERMANENT_REDIRECT) {
775 return TRUE;
776 }
777
778 return FALSE;
779 }