]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpBootDxe/HttpBootSupport.c
93d9dfc4643d224094008de65a1c1dd08d9a5537
[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 Create a HTTP_IO_HEADER to hold the HTTP header items.
496
497 @param[in] MaxHeaderCount The maximum number of HTTP header in this holder.
498
499 @return A pointer of the HTTP header holder or NULL if failed.
500
501 **/
502 HTTP_IO_HEADER *
503 HttpBootCreateHeader (
504 UINTN MaxHeaderCount
505 )
506 {
507 HTTP_IO_HEADER *HttpIoHeader;
508
509 if (MaxHeaderCount == 0) {
510 return NULL;
511 }
512
513 HttpIoHeader = AllocateZeroPool (sizeof (HTTP_IO_HEADER) + MaxHeaderCount * sizeof (EFI_HTTP_HEADER));
514 if (HttpIoHeader == NULL) {
515 return NULL;
516 }
517
518 HttpIoHeader->MaxHeaderCount = MaxHeaderCount;
519 HttpIoHeader->Headers = (EFI_HTTP_HEADER *) (HttpIoHeader + 1);
520
521 return HttpIoHeader;
522 }
523
524 /**
525 Destroy the HTTP_IO_HEADER and release the resources.
526
527 @param[in] HttpIoHeader Point to the HTTP header holder to be destroyed.
528
529 **/
530 VOID
531 HttpBootFreeHeader (
532 IN HTTP_IO_HEADER *HttpIoHeader
533 )
534 {
535 UINTN Index;
536
537 if (HttpIoHeader != NULL) {
538 if (HttpIoHeader->HeaderCount != 0) {
539 for (Index = 0; Index < HttpIoHeader->HeaderCount; Index++) {
540 FreePool (HttpIoHeader->Headers[Index].FieldName);
541 FreePool (HttpIoHeader->Headers[Index].FieldValue);
542 }
543 }
544 FreePool (HttpIoHeader);
545 }
546 }
547
548 /**
549 Set or update a HTTP header with the field name and corresponding value.
550
551 @param[in] HttpIoHeader Point to the HTTP header holder.
552 @param[in] FieldName Null terminated string which describes a field name.
553 @param[in] FieldValue Null terminated string which describes the corresponding field value.
554
555 @retval EFI_SUCCESS The HTTP header has been set or updated.
556 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
557 @retval EFI_OUT_OF_RESOURCES Insufficient resource to complete the operation.
558 @retval Other Unexpected error happened.
559
560 **/
561 EFI_STATUS
562 HttpBootSetHeader (
563 IN HTTP_IO_HEADER *HttpIoHeader,
564 IN CHAR8 *FieldName,
565 IN CHAR8 *FieldValue
566 )
567 {
568 EFI_HTTP_HEADER *Header;
569 UINTN StrSize;
570 CHAR8 *NewFieldValue;
571
572 if (HttpIoHeader == NULL || FieldName == NULL || FieldValue == NULL) {
573 return EFI_INVALID_PARAMETER;
574 }
575
576 Header = HttpFindHeader (HttpIoHeader->HeaderCount, HttpIoHeader->Headers, FieldName);
577 if (Header == NULL) {
578 //
579 // Add a new header.
580 //
581 if (HttpIoHeader->HeaderCount >= HttpIoHeader->MaxHeaderCount) {
582 return EFI_OUT_OF_RESOURCES;
583 }
584 Header = &HttpIoHeader->Headers[HttpIoHeader->HeaderCount];
585
586 StrSize = AsciiStrSize (FieldName);
587 Header->FieldName = AllocatePool (StrSize);
588 if (Header->FieldName == NULL) {
589 return EFI_OUT_OF_RESOURCES;
590 }
591 CopyMem (Header->FieldName, FieldName, StrSize);
592 Header->FieldName[StrSize -1] = '\0';
593
594 StrSize = AsciiStrSize (FieldValue);
595 Header->FieldValue = AllocatePool (StrSize);
596 if (Header->FieldValue == NULL) {
597 FreePool (Header->FieldName);
598 return EFI_OUT_OF_RESOURCES;
599 }
600 CopyMem (Header->FieldValue, FieldValue, StrSize);
601 Header->FieldValue[StrSize -1] = '\0';
602
603 HttpIoHeader->HeaderCount++;
604 } else {
605 //
606 // Update an existing one.
607 //
608 StrSize = AsciiStrSize (FieldValue);
609 NewFieldValue = AllocatePool (StrSize);
610 if (NewFieldValue == NULL) {
611 return EFI_OUT_OF_RESOURCES;
612 }
613 CopyMem (NewFieldValue, FieldValue, StrSize);
614 NewFieldValue[StrSize -1] = '\0';
615
616 if (Header->FieldValue != NULL) {
617 FreePool (Header->FieldValue);
618 }
619 Header->FieldValue = NewFieldValue;
620 }
621
622 return EFI_SUCCESS;
623 }
624
625 /**
626 This function checks the HTTP(S) URI scheme.
627
628 @param[in] Uri The pointer to the URI string.
629
630 @retval EFI_SUCCESS The URI scheme is valid.
631 @retval EFI_INVALID_PARAMETER The URI scheme is not HTTP or HTTPS.
632 @retval EFI_ACCESS_DENIED HTTP is disabled and the URI is HTTP.
633
634 **/
635 EFI_STATUS
636 HttpBootCheckUriScheme (
637 IN CHAR8 *Uri
638 )
639 {
640 UINTN Index;
641 EFI_STATUS Status;
642
643 Status = EFI_SUCCESS;
644
645 //
646 // Convert the scheme to all lower case.
647 //
648 for (Index = 0; Index < AsciiStrLen (Uri); Index++) {
649 if (Uri[Index] == ':') {
650 break;
651 }
652 if (Uri[Index] >= 'A' && Uri[Index] <= 'Z') {
653 Uri[Index] -= (CHAR8)('A' - 'a');
654 }
655 }
656
657 //
658 // Return EFI_INVALID_PARAMETER if the URI is not HTTP or HTTPS.
659 //
660 if ((AsciiStrnCmp (Uri, "http://", 7) != 0) && (AsciiStrnCmp (Uri, "https://", 8) != 0)) {
661 DEBUG ((EFI_D_ERROR, "HttpBootCheckUriScheme: Invalid Uri.\n"));
662 return EFI_INVALID_PARAMETER;
663 }
664
665 //
666 // HTTP is disabled, return EFI_ACCESS_DENIED if the URI is HTTP.
667 //
668 if (!PcdGetBool (PcdAllowHttpConnections) && (AsciiStrnCmp (Uri, "http://", 7) == 0)) {
669 DEBUG ((EFI_D_ERROR, "HttpBootCheckUriScheme: HTTP is disabled.\n"));
670 return EFI_ACCESS_DENIED;
671 }
672
673 return Status;
674 }
675
676 /**
677 Get the URI address string from the input device path.
678
679 Caller need to free the buffer in the UriAddress pointer.
680
681 @param[in] FilePath Pointer to the device path which contains a URI device path node.
682 @param[out] UriAddress The URI address string extract from the device path.
683
684 @retval EFI_SUCCESS The URI string is returned.
685 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
686
687 **/
688 EFI_STATUS
689 HttpBootParseFilePath (
690 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
691 OUT CHAR8 **UriAddress
692 )
693 {
694 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
695 URI_DEVICE_PATH *UriDevicePath;
696 CHAR8 *Uri;
697 UINTN UriStrLength;
698
699 if (FilePath == NULL) {
700 return EFI_INVALID_PARAMETER;
701 }
702
703 *UriAddress = NULL;
704
705 //
706 // Extract the URI address from the FilePath
707 //
708 TempDevicePath = FilePath;
709 while (!IsDevicePathEnd (TempDevicePath)) {
710 if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&
711 (DevicePathSubType (TempDevicePath) == MSG_URI_DP)) {
712 UriDevicePath = (URI_DEVICE_PATH*) TempDevicePath;
713 //
714 // UEFI Spec doesn't require the URI to be a NULL-terminated string
715 // So we allocate a new buffer and always append a '\0' to it.
716 //
717 UriStrLength = DevicePathNodeLength (UriDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);
718 if (UriStrLength == 0) {
719 //
720 // return a NULL UriAddress if it's a empty URI device path node.
721 //
722 break;
723 }
724 Uri = AllocatePool (UriStrLength + 1);
725 if (Uri == NULL) {
726 return EFI_OUT_OF_RESOURCES;
727 }
728 CopyMem (Uri, UriDevicePath->Uri, DevicePathNodeLength (UriDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL));
729 Uri[DevicePathNodeLength (UriDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL)] = '\0';
730
731 *UriAddress = Uri;
732 }
733 TempDevicePath = NextDevicePathNode (TempDevicePath);
734 }
735
736 return EFI_SUCCESS;
737 }
738
739 /**
740 This function returns the image type according to server replied HTTP message
741 and also the image's URI info.
742
743 @param[in] Uri The pointer to the image's URI string.
744 @param[in] UriParser URI Parse result returned by NetHttpParseUrl().
745 @param[in] HeaderCount Number of HTTP header structures in Headers list.
746 @param[in] Headers Array containing list of HTTP headers.
747 @param[out] ImageType The image type of the downloaded file.
748
749 @retval EFI_SUCCESS The image type is returned in ImageType.
750 @retval EFI_INVALID_PARAMETER ImageType, Uri or UriParser is NULL.
751 @retval EFI_INVALID_PARAMETER HeaderCount is not zero, and Headers is NULL.
752 @retval EFI_NOT_FOUND Failed to identify the image type.
753 @retval Others Unexpected error happened.
754
755 **/
756 EFI_STATUS
757 HttpBootCheckImageType (
758 IN CHAR8 *Uri,
759 IN VOID *UriParser,
760 IN UINTN HeaderCount,
761 IN EFI_HTTP_HEADER *Headers,
762 OUT HTTP_BOOT_IMAGE_TYPE *ImageType
763 )
764 {
765 EFI_STATUS Status;
766 EFI_HTTP_HEADER *Header;
767 CHAR8 *FilePath;
768 CHAR8 *FilePost;
769
770 if (Uri == NULL || UriParser == NULL || ImageType == NULL) {
771 return EFI_INVALID_PARAMETER;
772 }
773
774 if (HeaderCount != 0 && Headers == NULL) {
775 return EFI_INVALID_PARAMETER;
776 }
777
778 //
779 // Determine the image type by the HTTP Content-Type header field first.
780 // "application/efi" -> EFI Image
781 // "application/vnd.efi-iso" -> CD/DVD Image
782 // "application/vnd.efi-img" -> Virtual Disk Image
783 //
784 Header = HttpFindHeader (HeaderCount, Headers, HTTP_HEADER_CONTENT_TYPE);
785 if (Header != NULL) {
786 if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_EFI) == 0) {
787 *ImageType = ImageTypeEfi;
788 return EFI_SUCCESS;
789 } else if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_ISO) == 0) {
790 *ImageType = ImageTypeVirtualCd;
791 return EFI_SUCCESS;
792 } else if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_IMG) == 0) {
793 *ImageType = ImageTypeVirtualDisk;
794 return EFI_SUCCESS;
795 }
796 }
797
798 //
799 // Determine the image type by file extension:
800 // *.efi -> EFI Image
801 // *.iso -> CD/DVD Image
802 // *.img -> Virtual Disk Image
803 //
804 Status = HttpUrlGetPath (
805 Uri,
806 UriParser,
807 &FilePath
808 );
809 if (EFI_ERROR (Status)) {
810 return Status;
811 }
812
813 FilePost = FilePath + AsciiStrLen (FilePath) - 4;
814 if (AsciiStrCmp (FilePost, ".efi") == 0) {
815 *ImageType = ImageTypeEfi;
816 } else if (AsciiStrCmp (FilePost, ".iso") == 0) {
817 *ImageType = ImageTypeVirtualCd;
818 } else if (AsciiStrCmp (FilePost, ".img") == 0) {
819 *ImageType = ImageTypeVirtualDisk;
820 } else {
821 *ImageType = ImageTypeMax;
822 }
823
824 FreePool (FilePath);
825
826 return (*ImageType < ImageTypeMax) ? EFI_SUCCESS : EFI_NOT_FOUND;
827 }
828
829 /**
830 This function register the RAM disk info to the system.
831
832 @param[in] Private The pointer to the driver's private data.
833 @param[in] BufferSize The size of Buffer in bytes.
834 @param[in] Buffer The base address of the RAM disk.
835 @param[in] ImageType The image type of the file in Buffer.
836
837 @retval EFI_SUCCESS The RAM disk has been registered.
838 @retval EFI_NOT_FOUND No RAM disk protocol instances were found.
839 @retval EFI_UNSUPPORTED The ImageType is not supported.
840 @retval Others Unexpected error happened.
841
842 **/
843 EFI_STATUS
844 HttpBootRegisterRamDisk (
845 IN HTTP_BOOT_PRIVATE_DATA *Private,
846 IN UINTN BufferSize,
847 IN VOID *Buffer,
848 IN HTTP_BOOT_IMAGE_TYPE ImageType
849 )
850 {
851 EFI_RAM_DISK_PROTOCOL *RamDisk;
852 EFI_STATUS Status;
853 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
854 EFI_GUID *RamDiskType;
855
856 ASSERT (Private != NULL);
857 ASSERT (Buffer != NULL);
858 ASSERT (BufferSize != 0);
859
860 Status = gBS->LocateProtocol (&gEfiRamDiskProtocolGuid, NULL, (VOID**) &RamDisk);
861 if (EFI_ERROR (Status)) {
862 DEBUG ((EFI_D_ERROR, "HTTP Boot: Couldn't find the RAM Disk protocol - %r\n", Status));
863 return Status;
864 }
865
866 if (ImageType == ImageTypeVirtualCd) {
867 RamDiskType = &gEfiVirtualCdGuid;
868 } else if (ImageType == ImageTypeVirtualDisk) {
869 RamDiskType = &gEfiVirtualDiskGuid;
870 } else {
871 return EFI_UNSUPPORTED;
872 }
873
874 Status = RamDisk->Register (
875 (UINTN)Buffer,
876 (UINT64)BufferSize,
877 RamDiskType,
878 Private->UsingIpv6 ? Private->Ip6Nic->DevicePath : Private->Ip4Nic->DevicePath,
879 &DevicePath
880 );
881 if (EFI_ERROR (Status)) {
882 DEBUG ((EFI_D_ERROR, "HTTP Boot: Failed to register RAM Disk - %r\n", Status));
883 }
884
885 return Status;
886 }
887
888 /**
889 Indicate if the HTTP status code indicates a redirection.
890
891 @param[in] StatusCode HTTP status code from server.
892
893 @return TRUE if it's redirection.
894
895 **/
896 BOOLEAN
897 HttpBootIsHttpRedirectStatusCode (
898 IN EFI_HTTP_STATUS_CODE StatusCode
899 )
900 {
901 if (StatusCode == HTTP_STATUS_301_MOVED_PERMANENTLY ||
902 StatusCode == HTTP_STATUS_302_FOUND ||
903 StatusCode == HTTP_STATUS_307_TEMPORARY_REDIRECT ||
904 StatusCode == HTTP_STATUS_308_PERMANENT_REDIRECT) {
905 return TRUE;
906 }
907
908 return FALSE;
909 }