2 Boot functions implementation for UefiPxeBc Driver.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "PxeBcImpl.h"
15 Display the string of the boot item.
17 If the length of the boot item string beyond 70 Char, just display 70 Char.
19 @param[in] Str The pointer to the string.
20 @param[in] Len The length of the string.
24 PxeBcDisplayBootItem (
32 // Cut off the chars behind 70th.
34 Len
= (UINT8
) MIN (PXEBC_DISPLAY_MAX_LINE
, Len
);
37 AsciiPrint ("%a \n", Str
);
40 // Restore the original 70th char.
47 Select and maintain the boot prompt if needed.
49 @param[in] Private Pointer to PxeBc private data.
51 @retval EFI_SUCCESS Selected boot prompt done.
52 @retval EFI_TIMEOUT Selected boot prompt timed out.
53 @retval EFI_NOT_FOUND The proxy offer is not Pxe10.
54 @retval EFI_ABORTED User cancelled the operation.
55 @retval EFI_NOT_READY Reading the input key from the keyboard has not finish.
59 PxeBcSelectBootPrompt (
60 IN PXEBC_PRIVATE_DATA
*Private
63 PXEBC_DHCP_PACKET_CACHE
*Cache
;
64 PXEBC_VENDOR_OPTION
*VendorOpt
;
65 EFI_PXE_BASE_CODE_MODE
*Mode
;
66 EFI_EVENT TimeoutEvent
;
67 EFI_EVENT DescendEvent
;
68 EFI_INPUT_KEY InputKey
;
79 Mode
= Private
->PxeBc
.Mode
;
80 Cache
= Mode
->ProxyOfferReceived
? &Private
->ProxyOffer
: &Private
->DhcpAck
;
81 OfferType
= Mode
->UsingIpv6
? Cache
->Dhcp6
.OfferType
: Cache
->Dhcp4
.OfferType
;
84 // Only DhcpPxe10 and ProxyPxe10 offer needs boot prompt.
86 if (OfferType
!= PxeOfferTypeProxyPxe10
&& OfferType
!= PxeOfferTypeDhcpPxe10
) {
91 // There is no specified ProxyPxe10 for IPv6 in PXE and UEFI spec.
93 ASSERT (!Mode
->UsingIpv6
);
95 VendorOpt
= &Cache
->Dhcp4
.VendorOpt
;
97 // According to the PXE specification 2.1, Table 2-1 PXE DHCP Options,
98 // we must not consider a boot prompt or boot menu if all of the following hold:
99 // - the PXE_DISCOVERY_CONTROL tag(6) is present inside the Vendor Options(43), and has bit 3 set
100 // - a boot file name has been presented in the initial DHCP or ProxyDHCP offer packet.
102 if (IS_DISABLE_PROMPT_MENU (VendorOpt
->DiscoverCtrl
) &&
103 Cache
->Dhcp4
.OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
107 if (!IS_VALID_BOOT_PROMPT (VendorOpt
->BitMap
)) {
111 Timeout
= VendorOpt
->MenuPrompt
->Timeout
;
112 Prompt
= VendorOpt
->MenuPrompt
->Prompt
;
113 PromptLen
= (UINT8
) (VendorOpt
->MenuPromptLen
- 1);
116 // The valid scope of Timeout refers to PXE2.1 spec.
121 if (Timeout
== 255) {
126 // Create and start a timer as timeout event.
128 Status
= gBS
->CreateEvent (
135 if (EFI_ERROR (Status
)) {
139 Status
= gBS
->SetTimer (
142 MultU64x32 (Timeout
, TICKS_PER_SECOND
)
144 if (EFI_ERROR (Status
)) {
149 // Create and start a periodic timer as descend event by second.
151 Status
= gBS
->CreateEvent (
158 if (EFI_ERROR (Status
)) {
162 Status
= gBS
->SetTimer (
167 if (EFI_ERROR (Status
)) {
172 // Display the boot item and cursor on the screen.
174 SecCol
= gST
->ConOut
->Mode
->CursorColumn
;
175 SecRow
= gST
->ConOut
->Mode
->CursorRow
;
177 PxeBcDisplayBootItem (Prompt
, PromptLen
);
179 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, SecCol
+ PromptLen
, SecRow
);
180 AsciiPrint ("(%d) ", Timeout
--);
182 Status
= EFI_TIMEOUT
;
183 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
184 if (!EFI_ERROR (gBS
->CheckEvent (DescendEvent
))) {
185 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, SecCol
+ PromptLen
, SecRow
);
186 AsciiPrint ("(%d) ", Timeout
--);
188 if (gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &InputKey
) == EFI_NOT_READY
) {
189 gBS
->Stall (10 * TICKS_PER_MS
);
193 // Parse the input key by user.
194 // If <F8> or <Ctrl> + <M> is pressed, return success to display the boot menu.
196 if (InputKey
.ScanCode
== 0) {
198 switch (InputKey
.UnicodeChar
) {
201 Status
= EFI_ABORTED
;
207 Status
= EFI_SUCCESS
;
216 switch (InputKey
.ScanCode
) {
219 Status
= EFI_SUCCESS
;
223 Status
= EFI_ABORTED
;
235 // Reset the cursor on the screen.
237 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0 , SecRow
+ 1);
240 if (DescendEvent
!= NULL
) {
241 gBS
->CloseEvent (DescendEvent
);
243 if (TimeoutEvent
!= NULL
) {
244 gBS
->CloseEvent (TimeoutEvent
);
252 Select the boot menu by user's input.
254 @param[in] Private Pointer to PxeBc private data.
255 @param[out] Type The type of the menu.
256 @param[in] UseDefaultItem Use default item or not.
258 @retval EFI_ABORTED User cancel operation.
259 @retval EFI_SUCCESS Select the boot menu success.
260 @retval EFI_NOT_READY Read the input key from the keyboard has not finish.
264 PxeBcSelectBootMenu (
265 IN PXEBC_PRIVATE_DATA
*Private
,
267 IN BOOLEAN UseDefaultItem
270 EFI_PXE_BASE_CODE_MODE
*Mode
;
271 PXEBC_DHCP_PACKET_CACHE
*Cache
;
272 PXEBC_VENDOR_OPTION
*VendorOpt
;
273 EFI_INPUT_KEY InputKey
;
282 CHAR8 Blank
[PXEBC_DISPLAY_MAX_LINE
];
283 PXEBC_BOOT_MENU_ENTRY
*MenuItem
;
284 PXEBC_BOOT_MENU_ENTRY
*MenuArray
[PXEBC_MENU_MAX_NUM
];
290 Mode
= Private
->PxeBc
.Mode
;
291 Cache
= Mode
->ProxyOfferReceived
? &Private
->ProxyOffer
: &Private
->DhcpAck
;
292 OfferType
= Mode
->UsingIpv6
? Cache
->Dhcp6
.OfferType
: Cache
->Dhcp4
.OfferType
;
295 // There is no specified DhcpPxe10/ProxyPxe10 for IPv6 in PXE and UEFI spec.
297 ASSERT (!Mode
->UsingIpv6
);
298 ASSERT (OfferType
== PxeOfferTypeProxyPxe10
|| OfferType
== PxeOfferTypeDhcpPxe10
);
300 VendorOpt
= &Cache
->Dhcp4
.VendorOpt
;
301 if (!IS_VALID_BOOT_MENU (VendorOpt
->BitMap
)) {
306 // Display the boot menu on the screen.
308 SetMem (Blank
, sizeof(Blank
), ' ');
310 MenuSize
= VendorOpt
->BootMenuLen
;
311 MenuItem
= VendorOpt
->BootMenu
;
314 return EFI_DEVICE_ERROR
;
317 while (MenuSize
> 0 && Index
< PXEBC_MENU_MAX_NUM
) {
318 ASSERT (MenuItem
!= NULL
);
319 MenuArray
[Index
] = MenuItem
;
320 MenuSize
= (UINT8
) (MenuSize
- (MenuItem
->DescLen
+ 3));
321 MenuItem
= (PXEBC_BOOT_MENU_ENTRY
*) ((UINT8
*) MenuItem
+ MenuItem
->DescLen
+ 3);
325 if (UseDefaultItem
) {
326 ASSERT (MenuArray
[0] != NULL
);
327 CopyMem (Type
, &MenuArray
[0]->Type
, sizeof (UINT16
));
328 *Type
= NTOHS (*Type
);
334 for (Index
= 0; Index
< MenuNum
; Index
++) {
335 ASSERT (MenuArray
[Index
] != NULL
);
336 PxeBcDisplayBootItem (MenuArray
[Index
]->DescStr
, MenuArray
[Index
]->DescLen
);
339 TopRow
= gST
->ConOut
->Mode
->CursorRow
- MenuNum
;
342 // Select the boot item by user in the boot menu.
346 // Highlight selected row.
348 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_TEXT_ATTR (EFI_BLACK
, EFI_LIGHTGRAY
));
349 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ Select
);
350 ASSERT (Select
< PXEBC_MENU_MAX_NUM
);
351 ASSERT (MenuArray
[Select
] != NULL
);
352 Blank
[MenuArray
[Select
]->DescLen
] = 0;
353 AsciiPrint ("%a\r", Blank
);
354 PxeBcDisplayBootItem (MenuArray
[Select
]->DescStr
, MenuArray
[Select
]->DescLen
);
355 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ MenuNum
);
358 while (gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &InputKey
) == EFI_NOT_READY
) {
359 gBS
->Stall (10 * TICKS_PER_MS
);
362 if (InputKey
.ScanCode
== 0) {
363 switch (InputKey
.UnicodeChar
) {
365 InputKey
.ScanCode
= SCAN_ESC
;
368 case CTRL ('j'): /* linefeed */
369 case CTRL ('m'): /* return */
373 case CTRL ('i'): /* tab */
377 InputKey
.ScanCode
= SCAN_DOWN
;
380 case CTRL ('h'): /* backspace */
383 InputKey
.ScanCode
= SCAN_UP
;
387 InputKey
.ScanCode
= 0;
391 switch (InputKey
.ScanCode
) {
401 if (++Select
== MenuNum
) {
413 Select
= (UINT16
) (MenuNum
- 1);
421 // Unhighlight the last selected row.
423 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_TEXT_ATTR (EFI_LIGHTGRAY
, EFI_BLACK
));
424 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ LastSelect
);
425 ASSERT (LastSelect
< PXEBC_MENU_MAX_NUM
);
426 ASSERT (MenuArray
[LastSelect
] != NULL
);
427 Blank
[MenuArray
[LastSelect
]->DescLen
] = 0;
428 AsciiPrint ("%a\r", Blank
);
429 PxeBcDisplayBootItem (MenuArray
[LastSelect
]->DescStr
, MenuArray
[LastSelect
]->DescLen
);
430 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ MenuNum
);
434 // Swap the byte order.
436 ASSERT (Select
< PXEBC_MENU_MAX_NUM
);
437 ASSERT (MenuArray
[Select
] != NULL
);
438 CopyMem (Type
, &MenuArray
[Select
]->Type
, sizeof (UINT16
));
439 *Type
= NTOHS (*Type
);
446 Parse out the boot information from the last Dhcp4 reply packet.
448 @param[in, out] Private Pointer to PxeBc private data.
449 @param[out] BufferSize Size of the boot file to be downloaded.
451 @retval EFI_SUCCESS Successfully parsed out all the boot information.
452 @retval Others Failed to parse out the boot information.
457 IN OUT PXEBC_PRIVATE_DATA
*Private
,
458 OUT UINT64
*BufferSize
461 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
462 EFI_PXE_BASE_CODE_MODE
*Mode
;
464 PXEBC_DHCP4_PACKET_CACHE
*Cache4
;
466 PXEBC_VENDOR_OPTION
*VendorOpt
;
467 PXEBC_BOOT_SVR_ENTRY
*Entry
;
469 PxeBc
= &Private
->PxeBc
;
471 Status
= EFI_SUCCESS
;
475 // Get the last received Dhcp4 reply packet.
477 if (Mode
->PxeReplyReceived
) {
478 Cache4
= &Private
->PxeReply
.Dhcp4
;
479 } else if (Mode
->ProxyOfferReceived
) {
480 Cache4
= &Private
->ProxyOffer
.Dhcp4
;
482 Cache4
= &Private
->DhcpAck
.Dhcp4
;
485 if (Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] == NULL
) {
487 // This should never happen in a correctly configured DHCP / PXE
488 // environment. One misconfiguration that can cause it is two DHCP servers
489 // mistakenly running on the same network segment at the same time, and
490 // racing each other in answering DHCP requests. Thus, the DHCP packets
491 // that the edk2 PXE client considers "belonging together" may actually be
492 // entirely independent, coming from two (competing) DHCP servers.
494 // Try to deal with this gracefully. Note that this check is not
495 // comprehensive, as we don't try to identify all such errors.
497 return EFI_PROTOCOL_ERROR
;
501 // Parse the boot server address.
502 // If prompt/discover is disabled, get the first boot server from the boot servers list.
503 // Otherwise, parse the boot server Ipv4 address from next server address field in DHCP header.
504 // If all these fields are not available, use option 54 instead.
506 VendorOpt
= &Cache4
->VendorOpt
;
507 if (IS_DISABLE_PROMPT_MENU (VendorOpt
->DiscoverCtrl
) && IS_VALID_BOOT_SERVERS (VendorOpt
->BitMap
)) {
508 Entry
= VendorOpt
->BootSvr
;
509 if (VendorOpt
->BootSvrLen
>= sizeof (PXEBC_BOOT_SVR_ENTRY
) && Entry
->IpCnt
> 0) {
513 sizeof (EFI_IPv4_ADDRESS
)
517 if (Private
->ServerIp
.Addr
[0] == 0) {
519 // ServerIp.Addr[0] equals zero means we failed to get IP address from boot server list.
520 // Try to use next server address field.
524 &Cache4
->Packet
.Offer
.Dhcp4
.Header
.ServerAddr
,
525 sizeof (EFI_IPv4_ADDRESS
)
528 if (Private
->ServerIp
.Addr
[0] == 0) {
530 // Still failed , use the IP address from option 54.
534 Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_SERVER_ID
]->Data
,
535 sizeof (EFI_IPv4_ADDRESS
)
540 // Parse the boot file name by option.
542 Private
->BootFileName
= Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Data
;
544 if (Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN
] != NULL
) {
546 // Parse the boot file size by option.
548 CopyMem (&Value
, Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN
]->Data
, sizeof (Value
));
549 Value
= NTOHS (Value
);
551 // The field of boot file size is 512 bytes in unit.
553 *BufferSize
= 512 * Value
;
556 // Get the bootfile size by tftp command if no option available.
558 Status
= PxeBc
->Mtftp (
560 EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE
,
566 Private
->BootFileName
,
573 // Save the value of boot file size.
575 Private
->BootFileSize
= (UINTN
) *BufferSize
;
578 // Display all the information: boot server address, boot file name and boot file size.
580 AsciiPrint ("\n Server IP address is ");
581 PxeBcShowIp4Addr (&Private
->ServerIp
.v4
);
582 AsciiPrint ("\n NBP filename is %a", Private
->BootFileName
);
583 AsciiPrint ("\n NBP filesize is %d Bytes", Private
->BootFileSize
);
590 Parse out the boot information from the last Dhcp6 reply packet.
592 @param[in, out] Private Pointer to PxeBc private data.
593 @param[out] BufferSize Size of the boot file to be downloaded.
595 @retval EFI_SUCCESS Successfully parsed out all the boot information.
596 @retval EFI_BUFFER_TOO_SMALL
597 @retval Others Failed to parse out the boot information.
602 IN OUT PXEBC_PRIVATE_DATA
*Private
,
603 OUT UINT64
*BufferSize
606 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
607 EFI_PXE_BASE_CODE_MODE
*Mode
;
609 PXEBC_DHCP6_PACKET_CACHE
*Cache6
;
612 PxeBc
= &Private
->PxeBc
;
614 Status
= EFI_SUCCESS
;
618 // Get the last received Dhcp6 reply packet.
620 if (Mode
->PxeReplyReceived
) {
621 Cache6
= &Private
->PxeReply
.Dhcp6
;
622 } else if (Mode
->ProxyOfferReceived
) {
623 Cache6
= &Private
->ProxyOffer
.Dhcp6
;
625 Cache6
= &Private
->DhcpAck
.Dhcp6
;
628 if (Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_URL
] == NULL
) {
630 // This should never happen in a correctly configured DHCP / PXE
631 // environment. One misconfiguration that can cause it is two DHCP servers
632 // mistakenly running on the same network segment at the same time, and
633 // racing each other in answering DHCP requests. Thus, the DHCP packets
634 // that the edk2 PXE client considers "belonging together" may actually be
635 // entirely independent, coming from two (competing) DHCP servers.
637 // Try to deal with this gracefully. Note that this check is not
638 // comprehensive, as we don't try to identify all such errors.
640 return EFI_PROTOCOL_ERROR
;
644 // Set the station address to IP layer.
646 Status
= PxeBcSetIp6Address (Private
);
647 if (EFI_ERROR (Status
)) {
653 // Parse (m)tftp server ip address and bootfile name.
655 Status
= PxeBcExtractBootFileUrl (
657 &Private
->BootFileName
,
658 &Private
->ServerIp
.v6
,
659 (CHAR8
*) (Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_URL
]->Data
),
660 NTOHS (Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_URL
]->OpLen
)
662 if (EFI_ERROR (Status
)) {
667 // Parse the value of boot file size.
669 if (Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM
] != NULL
) {
671 // Parse it out if have the boot file parameter option.
673 Status
= PxeBcExtractBootFileParam ((CHAR8
*) Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM
]->Data
, &Value
);
674 if (EFI_ERROR (Status
)) {
678 // The field of boot file size is 512 bytes in unit.
680 *BufferSize
= 512 * Value
;
683 // Send get file size command by tftp if option unavailable.
685 Status
= PxeBc
->Mtftp (
687 EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE
,
693 Private
->BootFileName
,
700 // Save the value of boot file size.
702 Private
->BootFileSize
= (UINTN
) *BufferSize
;
705 // Display all the information: boot server address, boot file name and boot file size.
707 AsciiPrint ("\n Server IP address is ");
708 PxeBcShowIp6Addr (&Private
->ServerIp
.v6
);
709 AsciiPrint ("\n NBP filename is %a", Private
->BootFileName
);
710 AsciiPrint ("\n NBP filesize is %d Bytes", Private
->BootFileSize
);
717 Extract the discover information and boot server entry from the
718 cached packets if unspecified.
720 @param[in] Private Pointer to PxeBc private data.
721 @param[in] Type The type of bootstrap to perform.
722 @param[in, out] DiscoverInfo Pointer to EFI_PXE_BASE_CODE_DISCOVER_INFO.
723 @param[out] BootEntry Pointer to PXEBC_BOOT_SVR_ENTRY.
724 @param[out] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST.
726 @retval EFI_SUCCESS Successfully extracted the information.
727 @retval EFI_DEVICE_ERROR Failed to extract the information.
731 PxeBcExtractDiscoverInfo (
732 IN PXEBC_PRIVATE_DATA
*Private
,
734 IN OUT EFI_PXE_BASE_CODE_DISCOVER_INFO
**DiscoverInfo
,
735 OUT PXEBC_BOOT_SVR_ENTRY
**BootEntry
,
736 OUT EFI_PXE_BASE_CODE_SRVLIST
**SrvList
739 EFI_PXE_BASE_CODE_MODE
*Mode
;
740 PXEBC_DHCP4_PACKET_CACHE
*Cache4
;
741 PXEBC_VENDOR_OPTION
*VendorOpt
;
742 PXEBC_BOOT_SVR_ENTRY
*Entry
;
744 EFI_PXE_BASE_CODE_DISCOVER_INFO
*Info
;
747 Mode
= Private
->PxeBc
.Mode
;
748 Info
= *DiscoverInfo
;
750 if (Mode
->UsingIpv6
) {
752 Info
->UseUCast
= TRUE
;
754 Info
->SrvList
[0].Type
= Type
;
755 Info
->SrvList
[0].AcceptAnyResponse
= FALSE
;
758 // There is no vendor options specified in DHCPv6, so take BootFileUrl in the last cached packet.
760 CopyMem (&Info
->SrvList
[0].IpAddr
, &Private
->ServerIp
, sizeof (EFI_IP_ADDRESS
));
762 *SrvList
= Info
->SrvList
;
766 Cache4
= (Mode
->ProxyOfferReceived
) ? &Private
->ProxyOffer
.Dhcp4
: &Private
->DhcpAck
.Dhcp4
;
767 VendorOpt
= &Cache4
->VendorOpt
;
769 if (!Mode
->DhcpAckReceived
|| !IS_VALID_DISCOVER_VENDOR_OPTION (VendorOpt
->BitMap
)) {
771 // Address is not acquired or no discovery options.
773 return EFI_INVALID_PARAMETER
;
777 // Parse the boot server entry from the vendor option in the last cached packet.
779 Info
->UseMCast
= (BOOLEAN
) !IS_DISABLE_MCAST_DISCOVER (VendorOpt
->DiscoverCtrl
);
780 Info
->UseBCast
= (BOOLEAN
) !IS_DISABLE_BCAST_DISCOVER (VendorOpt
->DiscoverCtrl
);
781 Info
->MustUseList
= (BOOLEAN
) IS_ENABLE_USE_SERVER_LIST (VendorOpt
->DiscoverCtrl
);
782 Info
->UseUCast
= (BOOLEAN
) IS_VALID_BOOT_SERVERS (VendorOpt
->BitMap
);
784 if (Info
->UseMCast
) {
786 // Get the multicast discover ip address from vendor option if has.
788 CopyMem (&Info
->ServerMCastIp
.v4
, &VendorOpt
->DiscoverMcastIp
, sizeof (EFI_IPv4_ADDRESS
));
793 if (Info
->UseUCast
) {
794 Entry
= VendorOpt
->BootSvr
;
796 while (((UINT8
) (Entry
- VendorOpt
->BootSvr
)) < VendorOpt
->BootSvrLen
) {
797 if (Entry
->Type
== HTONS (Type
)) {
801 Entry
= GET_NEXT_BOOT_SVR_ENTRY (Entry
);
805 return EFI_DEVICE_ERROR
;
808 Info
->IpCnt
= Entry
->IpCnt
;
809 if (Info
->IpCnt
>= 1) {
810 *DiscoverInfo
= AllocatePool (sizeof (*Info
) + (Info
->IpCnt
- 1) * sizeof (**SrvList
));
811 if (*DiscoverInfo
== NULL
) {
812 return EFI_OUT_OF_RESOURCES
;
814 CopyMem (*DiscoverInfo
, Info
, sizeof (*Info
));
815 Info
= *DiscoverInfo
;
818 for (Index
= 0; Index
< Info
->IpCnt
; Index
++) {
819 CopyMem (&Info
->SrvList
[Index
].IpAddr
, &Entry
->IpAddr
[Index
], sizeof (EFI_IPv4_ADDRESS
));
820 Info
->SrvList
[Index
].AcceptAnyResponse
= !Info
->MustUseList
;
821 Info
->SrvList
[Index
].Type
= NTOHS (Entry
->Type
);
826 *SrvList
= Info
->SrvList
;
834 Build the discover packet and send out for boot server.
836 @param[in] Private Pointer to PxeBc private data.
837 @param[in] Type PxeBc option boot item type.
838 @param[in] Layer Pointer to option boot item layer.
839 @param[in] UseBis Use BIS or not.
840 @param[in] DestIp Pointer to the destination address.
841 @param[in] IpCount The count of the server address.
842 @param[in] SrvList Pointer to the server address list.
844 @retval EFI_SUCCESS Successfully discovered boot file.
845 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
846 @retval EFI_NOT_FOUND Can't get the PXE reply packet.
847 @retval Others Failed to discover boot file.
851 PxeBcDiscoverBootServer (
852 IN PXEBC_PRIVATE_DATA
*Private
,
856 IN EFI_IP_ADDRESS
*DestIp
,
858 IN EFI_PXE_BASE_CODE_SRVLIST
*SrvList
861 if (Private
->PxeBc
.Mode
->UsingIpv6
) {
862 return PxeBcDhcp6Discover (
870 return PxeBcDhcp4Discover (
884 Discover all the boot information for boot file.
886 @param[in, out] Private Pointer to PxeBc private data.
887 @param[out] BufferSize Size of the boot file to be downloaded.
889 @retval EFI_SUCCESS Successfully obtained all the boot information .
890 @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file.
891 @retval EFI_ABORTED User cancel current operation.
892 @retval Others Failed to parse out the boot information.
896 PxeBcDiscoverBootFile (
897 IN OUT PXEBC_PRIVATE_DATA
*Private
,
898 OUT UINT64
*BufferSize
901 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
902 EFI_PXE_BASE_CODE_MODE
*Mode
;
908 PxeBc
= &Private
->PxeBc
;
910 Type
= EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP
;
911 Layer
= EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL
;
914 // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and
915 // other pxe boot information.
917 Status
= PxeBc
->Dhcp (PxeBc
, TRUE
);
918 if (EFI_ERROR (Status
)) {
923 // Select a boot server from boot server list.
925 Status
= PxeBcSelectBootPrompt (Private
);
927 if (Status
== EFI_SUCCESS
) {
929 // Choose by user's input.
931 Status
= PxeBcSelectBootMenu (Private
, &Type
, FALSE
);
932 } else if (Status
== EFI_TIMEOUT
) {
934 // Choose by default item.
936 Status
= PxeBcSelectBootMenu (Private
, &Type
, TRUE
);
939 if (!EFI_ERROR (Status
)) {
941 if (Type
== EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP
) {
943 // Local boot(PXE bootstrap server) need abort
949 // Start to discover the boot server to get (m)tftp server ip address, bootfile
950 // name and bootfile size.
952 UseBis
= (BOOLEAN
) (Mode
->BisSupported
&& Mode
->BisDetected
);
953 Status
= PxeBc
->Discover (PxeBc
, Type
, &Layer
, UseBis
, NULL
);
954 if (EFI_ERROR (Status
)) {
958 if (Mode
->PxeReplyReceived
&& !Mode
->ProxyOfferReceived
) {
960 // Some network boot loader only search the packet in Mode.ProxyOffer to get its server
961 // IP address, so we need to store a copy of Mode.PxeReply packet into Mode.ProxyOffer.
963 if (Mode
->UsingIpv6
) {
965 &Mode
->ProxyOffer
.Dhcpv6
,
966 &Mode
->PxeReply
.Dhcpv6
,
967 Private
->PxeReply
.Dhcp6
.Packet
.Ack
.Length
971 &Mode
->ProxyOffer
.Dhcpv4
,
972 &Mode
->PxeReply
.Dhcpv4
,
973 Private
->PxeReply
.Dhcp4
.Packet
.Ack
.Length
976 Mode
->ProxyOfferReceived
= TRUE
;
981 // Parse the boot information.
983 if (Mode
->UsingIpv6
) {
984 Status
= PxeBcDhcp6BootInfo (Private
, BufferSize
);
986 Status
= PxeBcDhcp4BootInfo (Private
, BufferSize
);
994 Install PxeBaseCodeCallbackProtocol if not installed before.
996 @param[in, out] Private Pointer to PxeBc private data.
997 @param[out] NewMakeCallback If TRUE, it is a new callback.
998 Otherwise, it is not new callback.
999 @retval EFI_SUCCESS PxeBaseCodeCallbackProtocol installed successfully.
1000 @retval Others Failed to install PxeBaseCodeCallbackProtocol.
1004 PxeBcInstallCallback (
1005 IN OUT PXEBC_PRIVATE_DATA
*Private
,
1006 OUT BOOLEAN
*NewMakeCallback
1009 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
1013 // Check whether PxeBaseCodeCallbackProtocol already installed.
1015 PxeBc
= &Private
->PxeBc
;
1016 Status
= gBS
->HandleProtocol (
1017 Private
->Mode
.UsingIpv6
? Private
->Ip6Nic
->Controller
: Private
->Ip4Nic
->Controller
,
1018 &gEfiPxeBaseCodeCallbackProtocolGuid
,
1019 (VOID
**) &Private
->PxeBcCallback
1021 if (Status
== EFI_UNSUPPORTED
) {
1024 &Private
->LoadFileCallback
,
1025 &gPxeBcCallBackTemplate
,
1026 sizeof (EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
)
1030 // Install a default callback if user didn't offer one.
1032 Status
= gBS
->InstallProtocolInterface (
1033 Private
->Mode
.UsingIpv6
? &Private
->Ip6Nic
->Controller
: &Private
->Ip4Nic
->Controller
,
1034 &gEfiPxeBaseCodeCallbackProtocolGuid
,
1035 EFI_NATIVE_INTERFACE
,
1036 &Private
->LoadFileCallback
1039 (*NewMakeCallback
) = (BOOLEAN
) (Status
== EFI_SUCCESS
);
1041 Status
= PxeBc
->SetParameters (PxeBc
, NULL
, NULL
, NULL
, NULL
, NewMakeCallback
);
1042 if (EFI_ERROR (Status
)) {
1043 PxeBc
->Stop (PxeBc
);
1053 Uninstall PxeBaseCodeCallbackProtocol.
1055 @param[in] Private Pointer to PxeBc private data.
1056 @param[in] NewMakeCallback If TRUE, it is a new callback.
1057 Otherwise, it is not new callback.
1061 PxeBcUninstallCallback (
1062 IN PXEBC_PRIVATE_DATA
*Private
,
1063 IN BOOLEAN NewMakeCallback
1066 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
1068 PxeBc
= &Private
->PxeBc
;
1070 if (NewMakeCallback
) {
1072 NewMakeCallback
= FALSE
;
1074 PxeBc
->SetParameters (PxeBc
, NULL
, NULL
, NULL
, NULL
, &NewMakeCallback
);
1076 gBS
->UninstallProtocolInterface (
1077 Private
->Mode
.UsingIpv6
? Private
->Ip6Nic
->Controller
: Private
->Ip4Nic
->Controller
,
1078 &gEfiPxeBaseCodeCallbackProtocolGuid
,
1079 &Private
->LoadFileCallback
1086 Download one of boot file in the list, and it's special for IPv6.
1088 @param[in] Private Pointer to PxeBc private data.
1089 @param[in, out] BufferSize Size of user buffer for input;
1090 required buffer size for output.
1091 @param[in] Buffer Pointer to user buffer.
1093 @retval EFI_SUCCESS Read one of boot file in the list successfully.
1094 @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file.
1095 @retval EFI_NOT_FOUND There is no proper boot file available.
1096 @retval Others Failed to download boot file in the list.
1100 PxeBcReadBootFileList (
1101 IN PXEBC_PRIVATE_DATA
*Private
,
1102 IN OUT UINT64
*BufferSize
,
1103 IN VOID
*Buffer OPTIONAL
1107 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
1109 PxeBc
= &Private
->PxeBc
;
1112 // Try to download the boot file if everything is ready.
1114 if (Buffer
!= NULL
) {
1115 Status
= PxeBc
->Mtftp (
1117 EFI_PXE_BASE_CODE_TFTP_READ_FILE
,
1121 &Private
->BlockSize
,
1123 Private
->BootFileName
,
1130 Status
= EFI_BUFFER_TOO_SMALL
;
1138 Load boot file into user buffer.
1140 @param[in] Private Pointer to PxeBc private data.
1141 @param[in, out] BufferSize Size of user buffer for input;
1142 required buffer size for output.
1143 @param[in] Buffer Pointer to user buffer.
1145 @retval EFI_SUCCESS Get all the boot information successfully.
1146 @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file.
1147 @retval EFI_ABORTED User cancelled the current operation.
1148 @retval Others Failed to parse out the boot information.
1153 IN PXEBC_PRIVATE_DATA
*Private
,
1154 IN OUT UINTN
*BufferSize
,
1155 IN VOID
*Buffer OPTIONAL
1158 BOOLEAN NewMakeCallback
;
1159 UINT64 RequiredSize
;
1162 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
1163 EFI_PXE_BASE_CODE_MODE
*PxeBcMode
;
1165 NewMakeCallback
= FALSE
;
1166 PxeBc
= &Private
->PxeBc
;
1167 PxeBcMode
= &Private
->Mode
;
1168 CurrentSize
= *BufferSize
;
1172 // Install pxebc callback protocol if hasn't been installed yet.
1174 Status
= PxeBcInstallCallback (Private
, &NewMakeCallback
);
1175 if (EFI_ERROR(Status
)) {
1179 if (Private
->BootFileSize
== 0) {
1181 // Discover the boot information about the bootfile if hasn't.
1183 Status
= PxeBcDiscoverBootFile (Private
, &RequiredSize
);
1184 if (EFI_ERROR (Status
)) {
1188 if (PXEBC_IS_SIZE_OVERFLOWED (RequiredSize
)) {
1190 // It's error if the required buffer size is beyond the system scope.
1192 Status
= EFI_DEVICE_ERROR
;
1194 } else if (RequiredSize
> 0) {
1196 // Get the right buffer size of the bootfile required.
1198 if (CurrentSize
< RequiredSize
|| Buffer
== NULL
) {
1200 // It's buffer too small if the size of user buffer is smaller than the required.
1202 CurrentSize
= RequiredSize
;
1203 Status
= EFI_BUFFER_TOO_SMALL
;
1206 CurrentSize
= RequiredSize
;
1207 } else if (RequiredSize
== 0 && PxeBcMode
->UsingIpv6
) {
1209 // Try to download another bootfile in list if failed to get the filesize of the last one.
1210 // It's special for the case of IPv6.
1212 Status
= PxeBcReadBootFileList (Private
, &CurrentSize
, Buffer
);
1215 } else if (CurrentSize
< Private
->BootFileSize
|| Buffer
== NULL
) {
1217 // It's buffer too small if the size of user buffer is smaller than the required.
1219 CurrentSize
= Private
->BootFileSize
;
1220 Status
= EFI_BUFFER_TOO_SMALL
;
1225 // Begin to download the bootfile if everything is ready.
1227 AsciiPrint ("\n Downloading NBP file...\n");
1228 if (PxeBcMode
->UsingIpv6
) {
1229 Status
= PxeBcReadBootFileList (
1235 Status
= PxeBc
->Mtftp (
1237 EFI_PXE_BASE_CODE_TFTP_READ_FILE
,
1241 &Private
->BlockSize
,
1243 Private
->BootFileName
,
1250 *BufferSize
= (UINTN
) CurrentSize
;
1251 PxeBcUninstallCallback(Private
, NewMakeCallback
);
1253 if (Status
== EFI_SUCCESS
) {
1254 AsciiPrint ("\n NBP file downloaded successfully.\n");
1256 } else if (Status
== EFI_BUFFER_TOO_SMALL
&& Buffer
!= NULL
) {
1257 AsciiPrint ("\n PXE-E05: Buffer size is smaller than the requested file.\n");
1258 } else if (Status
== EFI_DEVICE_ERROR
) {
1259 AsciiPrint ("\n PXE-E07: Network device error.\n");
1260 } else if (Status
== EFI_OUT_OF_RESOURCES
) {
1261 AsciiPrint ("\n PXE-E09: Could not allocate I/O buffers.\n");
1262 } else if (Status
== EFI_NO_MEDIA
) {
1263 AsciiPrint ("\n PXE-E12: Could not detect network connection.\n");
1264 } else if (Status
== EFI_NO_RESPONSE
) {
1265 AsciiPrint ("\n PXE-E16: No valid offer received.\n");
1266 } else if (Status
== EFI_TIMEOUT
) {
1267 AsciiPrint ("\n PXE-E18: Server response timeout.\n");
1268 } else if (Status
== EFI_ABORTED
) {
1269 AsciiPrint ("\n PXE-E21: Remote boot cancelled.\n");
1270 } else if (Status
== EFI_ICMP_ERROR
) {
1271 AsciiPrint ("\n PXE-E22: Client received ICMP error from server.\n");
1272 } else if (Status
== EFI_TFTP_ERROR
) {
1273 AsciiPrint ("\n PXE-E23: Client received TFTP error from server.\n");
1274 } else if (Status
== EFI_NOT_FOUND
) {
1275 AsciiPrint ("\n PXE-E53: No boot filename received.\n");
1276 } else if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1277 AsciiPrint ("\n PXE-E99: Unexpected network error.\n");