2 Boot functions implementation for UefiPxeBc Driver.
4 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
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.
16 #include "PxeBcImpl.h"
20 Display the string of the boot item.
22 If the length of the boot item string beyond 70 Char, just display 70 Char.
24 @param[in] Str The pointer to the string.
25 @param[in] Len The length of the string.
29 PxeBcDisplayBootItem (
37 // Cut off the chars behind 70th.
39 Len
= (UINT8
) MIN (PXEBC_DISPLAY_MAX_LINE
, Len
);
42 AsciiPrint ("%a \n", Str
);
45 // Restore the original 70th char.
52 Select and maintain the boot prompt if needed.
54 @param[in] Private Pointer to PxeBc private data.
56 @retval EFI_SUCCESS Selected boot prompt done.
57 @retval EFI_TIMEOUT Selected boot prompt timed out.
58 @retval EFI_NOT_FOUND The proxy offer is not Pxe10.
59 @retval EFI_ABORTED User cancelled the operation.
60 @retval EFI_NOT_READY Reading the input key from the keyboard has not finish.
64 PxeBcSelectBootPrompt (
65 IN PXEBC_PRIVATE_DATA
*Private
68 PXEBC_DHCP_PACKET_CACHE
*Cache
;
69 PXEBC_VENDOR_OPTION
*VendorOpt
;
70 EFI_PXE_BASE_CODE_MODE
*Mode
;
71 EFI_EVENT TimeoutEvent
;
72 EFI_EVENT DescendEvent
;
73 EFI_INPUT_KEY InputKey
;
84 Mode
= Private
->PxeBc
.Mode
;
85 Cache
= Mode
->ProxyOfferReceived
? &Private
->ProxyOffer
: &Private
->DhcpAck
;
86 OfferType
= Mode
->UsingIpv6
? Cache
->Dhcp6
.OfferType
: Cache
->Dhcp4
.OfferType
;
89 // Only DhcpPxe10 and ProxyPxe10 offer needs boot prompt.
91 if (OfferType
!= PxeOfferTypeProxyPxe10
&& OfferType
!= PxeOfferTypeDhcpPxe10
) {
96 // There is no specified ProxyPxe10 for IPv6 in PXE and UEFI spec.
98 ASSERT (!Mode
->UsingIpv6
);
100 VendorOpt
= &Cache
->Dhcp4
.VendorOpt
;
102 // According to the PXE specification 2.1, Table 2-1 PXE DHCP Options,
103 // we must not consider a boot prompt or boot menu if all of the following hold:
104 // - the PXE_DISCOVERY_CONTROL tag(6) is present inside the Vendor Options(43), and has bit 3 set
105 // - a boot file name has been presented in the initial DHCP or ProxyDHCP offer packet.
107 if (IS_DISABLE_PROMPT_MENU (VendorOpt
->DiscoverCtrl
) &&
108 Cache
->Dhcp4
.OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
112 if (!IS_VALID_BOOT_PROMPT (VendorOpt
->BitMap
)) {
116 Timeout
= VendorOpt
->MenuPrompt
->Timeout
;
117 Prompt
= VendorOpt
->MenuPrompt
->Prompt
;
118 PromptLen
= (UINT8
) (VendorOpt
->MenuPromptLen
- 1);
121 // The valid scope of Timeout refers to PXE2.1 spec.
126 if (Timeout
== 255) {
131 // Create and start a timer as timeout event.
133 Status
= gBS
->CreateEvent (
140 if (EFI_ERROR (Status
)) {
144 Status
= gBS
->SetTimer (
147 Timeout
* TICKS_PER_SECOND
149 if (EFI_ERROR (Status
)) {
154 // Create and start a periodic timer as descend event by second.
156 Status
= gBS
->CreateEvent (
163 if (EFI_ERROR (Status
)) {
167 Status
= gBS
->SetTimer (
172 if (EFI_ERROR (Status
)) {
177 // Display the boot item and cursor on the screen.
179 SecCol
= gST
->ConOut
->Mode
->CursorColumn
;
180 SecRow
= gST
->ConOut
->Mode
->CursorRow
;
182 PxeBcDisplayBootItem (Prompt
, PromptLen
);
184 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, SecCol
+ PromptLen
, SecRow
);
185 AsciiPrint ("(%d) ", Timeout
--);
187 Status
= EFI_TIMEOUT
;
188 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
189 if (!EFI_ERROR (gBS
->CheckEvent (DescendEvent
))) {
190 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, SecCol
+ PromptLen
, SecRow
);
191 AsciiPrint ("(%d) ", Timeout
--);
193 if (gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &InputKey
) == EFI_NOT_READY
) {
194 gBS
->Stall (10 * TICKS_PER_MS
);
198 // Parse the input key by user.
199 // If <F8> or <Ctrl> + <M> is pressed, return success to display the boot menu.
201 if (InputKey
.ScanCode
== 0) {
203 switch (InputKey
.UnicodeChar
) {
206 Status
= EFI_ABORTED
;
212 Status
= EFI_SUCCESS
;
221 switch (InputKey
.ScanCode
) {
224 Status
= EFI_SUCCESS
;
228 Status
= EFI_ABORTED
;
240 // Reset the cursor on the screen.
242 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0 , SecRow
+ 1);
245 if (DescendEvent
!= NULL
) {
246 gBS
->CloseEvent (DescendEvent
);
248 if (TimeoutEvent
!= NULL
) {
249 gBS
->CloseEvent (TimeoutEvent
);
257 Select the boot menu by user's input.
259 @param[in] Private Pointer to PxeBc private data.
260 @param[out] Type The type of the menu.
261 @param[in] UseDefaultItem Use default item or not.
263 @retval EFI_ABORTED User cancel operation.
264 @retval EFI_SUCCESS Select the boot menu success.
265 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.
269 PxeBcSelectBootMenu (
270 IN PXEBC_PRIVATE_DATA
*Private
,
272 IN BOOLEAN UseDefaultItem
275 EFI_PXE_BASE_CODE_MODE
*Mode
;
276 PXEBC_DHCP_PACKET_CACHE
*Cache
;
277 PXEBC_VENDOR_OPTION
*VendorOpt
;
278 EFI_INPUT_KEY InputKey
;
287 CHAR8 Blank
[PXEBC_DISPLAY_MAX_LINE
];
288 PXEBC_BOOT_MENU_ENTRY
*MenuItem
;
289 PXEBC_BOOT_MENU_ENTRY
*MenuArray
[PXEBC_MENU_MAX_NUM
];
295 Mode
= Private
->PxeBc
.Mode
;
296 Cache
= Mode
->ProxyOfferReceived
? &Private
->ProxyOffer
: &Private
->DhcpAck
;
297 OfferType
= Mode
->UsingIpv6
? Cache
->Dhcp6
.OfferType
: Cache
->Dhcp4
.OfferType
;
300 // There is no specified DhcpPxe10/ProxyPxe10 for IPv6 in PXE and UEFI spec.
302 ASSERT (!Mode
->UsingIpv6
);
303 ASSERT (OfferType
== PxeOfferTypeProxyPxe10
|| OfferType
== PxeOfferTypeDhcpPxe10
);
305 VendorOpt
= &Cache
->Dhcp4
.VendorOpt
;
306 if (!IS_VALID_BOOT_MENU (VendorOpt
->BitMap
)) {
311 // Display the boot menu on the screen.
313 SetMem (Blank
, sizeof(Blank
), ' ');
315 MenuSize
= VendorOpt
->BootMenuLen
;
316 MenuItem
= VendorOpt
->BootMenu
;
319 return EFI_DEVICE_ERROR
;
322 while (MenuSize
> 0 && Index
< PXEBC_MENU_MAX_NUM
) {
323 ASSERT (MenuItem
!= NULL
);
324 MenuArray
[Index
] = MenuItem
;
325 MenuSize
= (UINT8
) (MenuSize
- (MenuItem
->DescLen
+ 3));
326 MenuItem
= (PXEBC_BOOT_MENU_ENTRY
*) ((UINT8
*) MenuItem
+ MenuItem
->DescLen
+ 3);
330 if (UseDefaultItem
) {
331 ASSERT (MenuArray
[0] != NULL
);
332 CopyMem (Type
, &MenuArray
[0]->Type
, sizeof (UINT16
));
333 *Type
= NTOHS (*Type
);
339 for (Index
= 0; Index
< MenuNum
; Index
++) {
340 ASSERT (MenuArray
[Index
] != NULL
);
341 PxeBcDisplayBootItem (MenuArray
[Index
]->DescStr
, MenuArray
[Index
]->DescLen
);
344 TopRow
= gST
->ConOut
->Mode
->CursorRow
- MenuNum
;
347 // Select the boot item by user in the boot menu.
351 // Highlight selected row.
353 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_TEXT_ATTR (EFI_BLACK
, EFI_LIGHTGRAY
));
354 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ Select
);
355 ASSERT (Select
< PXEBC_MENU_MAX_NUM
);
356 ASSERT (MenuArray
[Select
] != NULL
);
357 Blank
[MenuArray
[Select
]->DescLen
] = 0;
358 AsciiPrint ("%a\r", Blank
);
359 PxeBcDisplayBootItem (MenuArray
[Select
]->DescStr
, MenuArray
[Select
]->DescLen
);
360 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ MenuNum
);
363 while (gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &InputKey
) == EFI_NOT_READY
) {
364 gBS
->Stall (10 * TICKS_PER_MS
);
367 if (InputKey
.ScanCode
== 0) {
368 switch (InputKey
.UnicodeChar
) {
370 InputKey
.ScanCode
= SCAN_ESC
;
373 case CTRL ('j'): /* linefeed */
374 case CTRL ('m'): /* return */
378 case CTRL ('i'): /* tab */
382 InputKey
.ScanCode
= SCAN_DOWN
;
385 case CTRL ('h'): /* backspace */
388 InputKey
.ScanCode
= SCAN_UP
;
392 InputKey
.ScanCode
= 0;
396 switch (InputKey
.ScanCode
) {
406 if (++Select
== MenuNum
) {
418 Select
= (UINT16
) (MenuNum
- 1);
426 // Unhighlight the last selected row.
428 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_TEXT_ATTR (EFI_LIGHTGRAY
, EFI_BLACK
));
429 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ LastSelect
);
430 ASSERT (LastSelect
< PXEBC_MENU_MAX_NUM
);
431 ASSERT (MenuArray
[LastSelect
] != NULL
);
432 Blank
[MenuArray
[LastSelect
]->DescLen
] = 0;
433 AsciiPrint ("%a\r", Blank
);
434 PxeBcDisplayBootItem (MenuArray
[LastSelect
]->DescStr
, MenuArray
[LastSelect
]->DescLen
);
435 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ MenuNum
);
439 // Swap the byte order.
441 ASSERT (Select
< PXEBC_MENU_MAX_NUM
);
442 ASSERT (MenuArray
[Select
] != NULL
);
443 CopyMem (Type
, &MenuArray
[Select
]->Type
, sizeof (UINT16
));
444 *Type
= NTOHS (*Type
);
451 Parse out the boot information from the last Dhcp4 reply packet.
453 @param[in, out] Private Pointer to PxeBc private data.
454 @param[out] BufferSize Size of the boot file to be downloaded.
456 @retval EFI_SUCCESS Successfully parsed out all the boot information.
457 @retval Others Failed to parse out the boot information.
462 IN OUT PXEBC_PRIVATE_DATA
*Private
,
463 OUT UINT64
*BufferSize
466 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
467 EFI_PXE_BASE_CODE_MODE
*Mode
;
469 PXEBC_DHCP4_PACKET_CACHE
*Cache4
;
471 PXEBC_VENDOR_OPTION
*VendorOpt
;
472 PXEBC_BOOT_SVR_ENTRY
*Entry
;
474 PxeBc
= &Private
->PxeBc
;
476 Status
= EFI_SUCCESS
;
480 // Get the last received Dhcp4 reply packet.
482 if (Mode
->PxeReplyReceived
) {
483 Cache4
= &Private
->PxeReply
.Dhcp4
;
484 } else if (Mode
->ProxyOfferReceived
) {
485 Cache4
= &Private
->ProxyOffer
.Dhcp4
;
487 Cache4
= &Private
->DhcpAck
.Dhcp4
;
490 ASSERT (Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
);
493 // Parse the boot server address.
494 // If prompt/discover is disabled, get the first boot server from the boot servers list.
495 // Otherwise, parse the boot server Ipv4 address from next server address field in DHCP header.
496 // If all these fields are not available, use option 54 instead.
498 VendorOpt
= &Cache4
->VendorOpt
;
499 if (IS_DISABLE_PROMPT_MENU (VendorOpt
->DiscoverCtrl
) && IS_VALID_BOOT_SERVERS (VendorOpt
->BitMap
)) {
500 Entry
= VendorOpt
->BootSvr
;
501 if (VendorOpt
->BootSvrLen
>= sizeof (PXEBC_BOOT_SVR_ENTRY
) && Entry
->IpCnt
> 0) {
505 sizeof (EFI_IPv4_ADDRESS
)
509 if (Private
->ServerIp
.Addr
[0] == 0) {
511 // ServerIp.Addr[0] equals zero means we failed to get IP address from boot server list.
512 // Try to use next server address field.
516 &Cache4
->Packet
.Offer
.Dhcp4
.Header
.ServerAddr
,
517 sizeof (EFI_IPv4_ADDRESS
)
520 if (Private
->ServerIp
.Addr
[0] == 0) {
522 // Still failed , use the IP address from option 54.
526 Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_SERVER_ID
]->Data
,
527 sizeof (EFI_IPv4_ADDRESS
)
532 // Parse the boot file name by option.
534 Private
->BootFileName
= Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Data
;
536 if (Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN
] != NULL
) {
538 // Parse the boot file size by option.
540 CopyMem (&Value
, Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN
]->Data
, sizeof (Value
));
541 Value
= NTOHS (Value
);
543 // The field of boot file size is 512 bytes in unit.
545 *BufferSize
= 512 * Value
;
548 // Get the bootfile size by tftp command if no option available.
550 Status
= PxeBc
->Mtftp (
552 EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE
,
558 Private
->BootFileName
,
565 // Save the value of boot file size.
567 Private
->BootFileSize
= (UINTN
) *BufferSize
;
570 // Display all the information: boot server address, boot file name and boot file size.
572 AsciiPrint ("\n Server IP address is ");
573 PxeBcShowIp4Addr (&Private
->ServerIp
.v4
);
574 AsciiPrint ("\n NBP filename is %a", Private
->BootFileName
);
575 AsciiPrint ("\n NBP filesize is %d Bytes", Private
->BootFileSize
);
582 Parse out the boot information from the last Dhcp6 reply packet.
584 @param[in, out] Private Pointer to PxeBc private data.
585 @param[out] BufferSize Size of the boot file to be downloaded.
587 @retval EFI_SUCCESS Successfully parsed out all the boot information.
588 @retval EFI_BUFFER_TOO_SMALL
589 @retval Others Failed to parse out the boot information.
594 IN OUT PXEBC_PRIVATE_DATA
*Private
,
595 OUT UINT64
*BufferSize
598 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
599 EFI_PXE_BASE_CODE_MODE
*Mode
;
601 PXEBC_DHCP6_PACKET_CACHE
*Cache6
;
604 PxeBc
= &Private
->PxeBc
;
606 Status
= EFI_SUCCESS
;
610 // Get the last received Dhcp6 reply packet.
612 if (Mode
->PxeReplyReceived
) {
613 Cache6
= &Private
->PxeReply
.Dhcp6
;
614 } else if (Mode
->ProxyOfferReceived
) {
615 Cache6
= &Private
->ProxyOffer
.Dhcp6
;
617 Cache6
= &Private
->DhcpAck
.Dhcp6
;
620 ASSERT (Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_URL
] != NULL
);
623 // Parse (m)tftp server ip address and bootfile name.
625 Status
= PxeBcExtractBootFileUrl (
626 &Private
->BootFileName
,
627 &Private
->ServerIp
.v6
,
628 (CHAR8
*) (Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_URL
]->Data
),
629 NTOHS (Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_URL
]->OpLen
)
631 if (EFI_ERROR (Status
)) {
636 // Parse the value of boot file size.
638 if (Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM
] != NULL
) {
640 // Parse it out if have the boot file parameter option.
642 Status
= PxeBcExtractBootFileParam ((CHAR8
*) Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM
]->Data
, &Value
);
643 if (EFI_ERROR (Status
)) {
647 // The field of boot file size is 512 bytes in unit.
649 *BufferSize
= 512 * Value
;
652 // Send get file size command by tftp if option unavailable.
654 Status
= PxeBc
->Mtftp (
656 EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE
,
662 Private
->BootFileName
,
669 // Save the value of boot file size.
671 Private
->BootFileSize
= (UINTN
) *BufferSize
;
674 // Display all the information: boot server address, boot file name and boot file size.
676 AsciiPrint ("\n Server IP address is ");
677 PxeBcShowIp6Addr (&Private
->ServerIp
.v6
);
678 AsciiPrint ("\n NBP filename is %a", Private
->BootFileName
);
679 AsciiPrint ("\n NBP filesize is %d Bytes", Private
->BootFileSize
);
686 Extract the discover information and boot server entry from the
687 cached packets if unspecified.
689 @param[in] Private Pointer to PxeBc private data.
690 @param[in] Type The type of bootstrap to perform.
691 @param[in, out] DiscoverInfo Pointer to EFI_PXE_BASE_CODE_DISCOVER_INFO.
692 @param[out] BootEntry Pointer to PXEBC_BOOT_SVR_ENTRY.
693 @param[out] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST.
695 @retval EFI_SUCCESS Successfully extracted the information.
696 @retval EFI_DEVICE_ERROR Failed to extract the information.
700 PxeBcExtractDiscoverInfo (
701 IN PXEBC_PRIVATE_DATA
*Private
,
703 IN OUT EFI_PXE_BASE_CODE_DISCOVER_INFO
**DiscoverInfo
,
704 OUT PXEBC_BOOT_SVR_ENTRY
**BootEntry
,
705 OUT EFI_PXE_BASE_CODE_SRVLIST
**SrvList
708 EFI_PXE_BASE_CODE_MODE
*Mode
;
709 PXEBC_DHCP4_PACKET_CACHE
*Cache4
;
710 PXEBC_VENDOR_OPTION
*VendorOpt
;
711 PXEBC_BOOT_SVR_ENTRY
*Entry
;
713 EFI_PXE_BASE_CODE_DISCOVER_INFO
*Info
;
716 Mode
= Private
->PxeBc
.Mode
;
717 Info
= *DiscoverInfo
;
719 if (Mode
->UsingIpv6
) {
721 Info
->UseUCast
= TRUE
;
723 Info
->SrvList
[0].Type
= Type
;
724 Info
->SrvList
[0].AcceptAnyResponse
= FALSE
;
727 // There is no vendor options specified in DHCPv6, so take BootFileUrl in the last cached packet.
729 CopyMem (&Info
->SrvList
[0].IpAddr
, &Private
->ServerIp
, sizeof (EFI_IP_ADDRESS
));
731 *SrvList
= Info
->SrvList
;
735 Cache4
= (Mode
->ProxyOfferReceived
) ? &Private
->ProxyOffer
.Dhcp4
: &Private
->DhcpAck
.Dhcp4
;
736 VendorOpt
= &Cache4
->VendorOpt
;
738 if (!Mode
->DhcpAckReceived
|| !IS_VALID_DISCOVER_VENDOR_OPTION (VendorOpt
->BitMap
)) {
740 // Address is not acquired or no discovery options.
742 return EFI_INVALID_PARAMETER
;
746 // Parse the boot server entry from the vendor option in the last cached packet.
748 Info
->UseMCast
= (BOOLEAN
) !IS_DISABLE_MCAST_DISCOVER (VendorOpt
->DiscoverCtrl
);
749 Info
->UseBCast
= (BOOLEAN
) !IS_DISABLE_BCAST_DISCOVER (VendorOpt
->DiscoverCtrl
);
750 Info
->MustUseList
= (BOOLEAN
) IS_ENABLE_USE_SERVER_LIST (VendorOpt
->DiscoverCtrl
);
751 Info
->UseUCast
= (BOOLEAN
) IS_VALID_BOOT_SERVERS (VendorOpt
->BitMap
);
753 if (Info
->UseMCast
) {
755 // Get the multicast discover ip address from vendor option if has.
757 CopyMem (&Info
->ServerMCastIp
.v4
, &VendorOpt
->DiscoverMcastIp
, sizeof (EFI_IPv4_ADDRESS
));
762 if (Info
->UseUCast
) {
763 Entry
= VendorOpt
->BootSvr
;
765 while (((UINT8
) (Entry
- VendorOpt
->BootSvr
)) < VendorOpt
->BootSvrLen
) {
766 if (Entry
->Type
== HTONS (Type
)) {
770 Entry
= GET_NEXT_BOOT_SVR_ENTRY (Entry
);
774 return EFI_DEVICE_ERROR
;
777 Info
->IpCnt
= Entry
->IpCnt
;
778 if (Info
->IpCnt
>= 1) {
779 *DiscoverInfo
= AllocatePool (sizeof (*Info
) + (Info
->IpCnt
- 1) * sizeof (**SrvList
));
780 if (*DiscoverInfo
== NULL
) {
781 return EFI_OUT_OF_RESOURCES
;
783 CopyMem (*DiscoverInfo
, Info
, sizeof (*Info
));
784 Info
= *DiscoverInfo
;
787 for (Index
= 0; Index
< Info
->IpCnt
; Index
++) {
788 CopyMem (&Info
->SrvList
[Index
].IpAddr
, &Entry
->IpAddr
[Index
], sizeof (EFI_IPv4_ADDRESS
));
789 Info
->SrvList
[Index
].AcceptAnyResponse
= !Info
->MustUseList
;
790 Info
->SrvList
[Index
].Type
= NTOHS (Entry
->Type
);
795 *SrvList
= Info
->SrvList
;
803 Build the discover packet and send out for boot server.
805 @param[in] Private Pointer to PxeBc private data.
806 @param[in] Type PxeBc option boot item type.
807 @param[in] Layer Pointer to option boot item layer.
808 @param[in] UseBis Use BIS or not.
809 @param[in] DestIp Pointer to the destination address.
810 @param[in] IpCount The count of the server address.
811 @param[in] SrvList Pointer to the server address list.
813 @retval EFI_SUCCESS Successfully discovered boot file.
814 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
815 @retval EFI_NOT_FOUND Can't get the PXE reply packet.
816 @retval Others Failed to discover boot file.
820 PxeBcDiscoverBootServer (
821 IN PXEBC_PRIVATE_DATA
*Private
,
825 IN EFI_IP_ADDRESS
*DestIp
,
827 IN EFI_PXE_BASE_CODE_SRVLIST
*SrvList
830 if (Private
->PxeBc
.Mode
->UsingIpv6
) {
831 return PxeBcDhcp6Discover (
839 return PxeBcDhcp4Discover (
853 Discover all the boot information for boot file.
855 @param[in, out] Private Pointer to PxeBc private data.
856 @param[out] BufferSize Size of the boot file to be downloaded.
858 @retval EFI_SUCCESS Successfully obtained all the boot information .
859 @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file.
860 @retval EFI_ABORTED User cancel current operation.
861 @retval Others Failed to parse out the boot information.
865 PxeBcDiscoverBootFile (
866 IN OUT PXEBC_PRIVATE_DATA
*Private
,
867 OUT UINT64
*BufferSize
870 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
871 EFI_PXE_BASE_CODE_MODE
*Mode
;
877 PxeBc
= &Private
->PxeBc
;
879 Type
= EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP
;
880 Layer
= EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL
;
883 // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and
884 // other pxe boot information.
886 Status
= PxeBc
->Dhcp (PxeBc
, TRUE
);
887 if (EFI_ERROR (Status
)) {
892 // Select a boot server from boot server list.
894 Status
= PxeBcSelectBootPrompt (Private
);
896 if (Status
== EFI_SUCCESS
) {
898 // Choose by user's input.
900 Status
= PxeBcSelectBootMenu (Private
, &Type
, FALSE
);
901 } else if (Status
== EFI_TIMEOUT
) {
903 // Choose by default item.
905 Status
= PxeBcSelectBootMenu (Private
, &Type
, TRUE
);
908 if (!EFI_ERROR (Status
)) {
910 if (Type
== EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP
) {
912 // Local boot(PXE bootstrap server) need abort
918 // Start to discover the boot server to get (m)tftp server ip address, bootfile
919 // name and bootfile size.
921 UseBis
= (BOOLEAN
) (Mode
->BisSupported
&& Mode
->BisDetected
);
922 Status
= PxeBc
->Discover (PxeBc
, Type
, &Layer
, UseBis
, NULL
);
923 if (EFI_ERROR (Status
)) {
927 if (Mode
->PxeReplyReceived
&& !Mode
->ProxyOfferReceived
) {
929 // Some network boot loader only search the packet in Mode.ProxyOffer to get its server
930 // IP address, so we need to store a copy of Mode.PxeReply packet into Mode.ProxyOffer.
932 if (Mode
->UsingIpv6
) {
934 &Mode
->ProxyOffer
.Dhcpv6
,
935 &Mode
->PxeReply
.Dhcpv6
,
936 Private
->PxeReply
.Dhcp6
.Packet
.Ack
.Length
940 &Mode
->ProxyOffer
.Dhcpv4
,
941 &Mode
->PxeReply
.Dhcpv4
,
942 Private
->PxeReply
.Dhcp4
.Packet
.Ack
.Length
945 Mode
->ProxyOfferReceived
= TRUE
;
950 // Parse the boot information.
952 if (Mode
->UsingIpv6
) {
953 Status
= PxeBcDhcp6BootInfo (Private
, BufferSize
);
955 Status
= PxeBcDhcp4BootInfo (Private
, BufferSize
);
963 Install PxeBaseCodeCallbackProtocol if not installed before.
965 @param[in, out] Private Pointer to PxeBc private data.
966 @param[out] NewMakeCallback If TRUE, it is a new callback.
967 Otherwise, it is not new callback.
968 @retval EFI_SUCCESS PxeBaseCodeCallbackProtocol installed succesfully.
969 @retval Others Failed to install PxeBaseCodeCallbackProtocol.
973 PxeBcInstallCallback (
974 IN OUT PXEBC_PRIVATE_DATA
*Private
,
975 OUT BOOLEAN
*NewMakeCallback
978 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
982 // Check whether PxeBaseCodeCallbackProtocol already installed.
984 PxeBc
= &Private
->PxeBc
;
985 Status
= gBS
->HandleProtocol (
987 &gEfiPxeBaseCodeCallbackProtocolGuid
,
988 (VOID
**) &Private
->PxeBcCallback
990 if (Status
== EFI_UNSUPPORTED
) {
993 &Private
->LoadFileCallback
,
994 &gPxeBcCallBackTemplate
,
995 sizeof (EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
)
999 // Install a default callback if user didn't offer one.
1001 Status
= gBS
->InstallProtocolInterface (
1002 &Private
->Controller
,
1003 &gEfiPxeBaseCodeCallbackProtocolGuid
,
1004 EFI_NATIVE_INTERFACE
,
1005 &Private
->LoadFileCallback
1008 (*NewMakeCallback
) = (BOOLEAN
) (Status
== EFI_SUCCESS
);
1010 Status
= PxeBc
->SetParameters (PxeBc
, NULL
, NULL
, NULL
, NULL
, NewMakeCallback
);
1011 if (EFI_ERROR (Status
)) {
1012 PxeBc
->Stop (PxeBc
);
1022 Uninstall PxeBaseCodeCallbackProtocol.
1024 @param[in] Private Pointer to PxeBc private data.
1025 @param[in] NewMakeCallback If TRUE, it is a new callback.
1026 Otherwise, it is not new callback.
1030 PxeBcUninstallCallback (
1031 IN PXEBC_PRIVATE_DATA
*Private
,
1032 IN BOOLEAN NewMakeCallback
1035 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
1037 PxeBc
= &Private
->PxeBc
;
1039 if (NewMakeCallback
) {
1041 NewMakeCallback
= FALSE
;
1043 PxeBc
->SetParameters (PxeBc
, NULL
, NULL
, NULL
, NULL
, &NewMakeCallback
);
1045 gBS
->UninstallProtocolInterface (
1046 Private
->Controller
,
1047 &gEfiPxeBaseCodeCallbackProtocolGuid
,
1048 &Private
->LoadFileCallback
1055 Download one of boot file in the list, and it's special for IPv6.
1057 @param[in] Private Pointer to PxeBc private data.
1058 @param[in, out] BufferSize Size of user buffer for input;
1059 required buffer size for output.
1060 @param[in] Buffer Pointer to user buffer.
1062 @retval EFI_SUCCESS Read one of boot file in the list successfully.
1063 @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file.
1064 @retval EFI_NOT_FOUND There is no proper boot file available.
1065 @retval Others Failed to download boot file in the list.
1069 PxeBcReadBootFileList (
1070 IN PXEBC_PRIVATE_DATA
*Private
,
1071 IN OUT UINT64
*BufferSize
,
1072 IN VOID
*Buffer OPTIONAL
1076 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
1078 PxeBc
= &Private
->PxeBc
;
1081 // Try to download the boot file if everything is ready.
1083 if (Buffer
!= NULL
) {
1084 Status
= PxeBc
->Mtftp (
1086 EFI_PXE_BASE_CODE_TFTP_READ_FILE
,
1090 &Private
->BlockSize
,
1092 Private
->BootFileName
,
1099 Status
= EFI_BUFFER_TOO_SMALL
;
1107 Load boot file into user buffer.
1109 @param[in] Private Pointer to PxeBc private data.
1110 @param[in, out] BufferSize Size of user buffer for input;
1111 required buffer size for output.
1112 @param[in] Buffer Pointer to user buffer.
1114 @retval EFI_SUCCESS Get all the boot information successfully.
1115 @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file.
1116 @retval EFI_ABORTED User cancelled the current operation.
1117 @retval Others Failed to parse out the boot information.
1122 IN PXEBC_PRIVATE_DATA
*Private
,
1123 IN OUT UINTN
*BufferSize
,
1124 IN VOID
*Buffer OPTIONAL
1127 BOOLEAN NewMakeCallback
;
1128 UINT64 RequiredSize
;
1131 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
1132 EFI_PXE_BASE_CODE_MODE
*PxeBcMode
;
1134 NewMakeCallback
= FALSE
;
1135 PxeBc
= &Private
->PxeBc
;
1136 PxeBcMode
= &Private
->Mode
;
1137 CurrentSize
= *BufferSize
;
1141 // Install pxebc callback protocol if hasn't been installed yet.
1143 Status
= PxeBcInstallCallback (Private
, &NewMakeCallback
);
1144 if (EFI_ERROR(Status
)) {
1148 if (Private
->BootFileSize
== 0) {
1150 // Discover the boot information about the bootfile if hasn't.
1152 Status
= PxeBcDiscoverBootFile (Private
, &RequiredSize
);
1153 if (EFI_ERROR (Status
)) {
1157 if (PXEBC_IS_SIZE_OVERFLOWED (RequiredSize
)) {
1159 // It's error if the required buffer size is beyond the system scope.
1161 Status
= EFI_DEVICE_ERROR
;
1163 } else if (RequiredSize
> 0) {
1165 // Get the right buffer size of the bootfile required.
1167 if (CurrentSize
< RequiredSize
|| Buffer
== NULL
) {
1169 // It's buffer too small if the size of user buffer is smaller than the required.
1171 CurrentSize
= RequiredSize
;
1172 Status
= EFI_BUFFER_TOO_SMALL
;
1175 CurrentSize
= RequiredSize
;
1176 } else if (RequiredSize
== 0 && PxeBcMode
->UsingIpv6
) {
1178 // Try to download another bootfile in list if failed to get the filesize of the last one.
1179 // It's special for the case of IPv6.
1181 Status
= PxeBcReadBootFileList (Private
, &CurrentSize
, Buffer
);
1184 } else if (CurrentSize
< Private
->BootFileSize
|| Buffer
== NULL
) {
1186 // It's buffer too small if the size of user buffer is smaller than the required.
1188 CurrentSize
= Private
->BootFileSize
;
1189 Status
= EFI_BUFFER_TOO_SMALL
;
1194 // Begin to download the bootfile if everything is ready.
1196 AsciiPrint ("\n Downloading NBP file...\n");
1197 if (PxeBcMode
->UsingIpv6
) {
1198 Status
= PxeBcReadBootFileList (
1204 Status
= PxeBc
->Mtftp (
1206 EFI_PXE_BASE_CODE_TFTP_READ_FILE
,
1210 &Private
->BlockSize
,
1212 Private
->BootFileName
,
1219 *BufferSize
= (UINTN
) CurrentSize
;
1220 PxeBcUninstallCallback(Private
, NewMakeCallback
);
1222 if (Status
== EFI_SUCCESS
) {
1223 AsciiPrint ("\n Succeed to download NBP file.\n");
1225 } else if (Status
== EFI_BUFFER_TOO_SMALL
&& Buffer
!= NULL
) {
1226 AsciiPrint ("\n PXE-E05: Buffer size is smaller than the requested file.\n");
1227 } else if (Status
== EFI_DEVICE_ERROR
) {
1228 AsciiPrint ("\n PXE-E07: Network device error.\n");
1229 } else if (Status
== EFI_OUT_OF_RESOURCES
) {
1230 AsciiPrint ("\n PXE-E09: Could not allocate I/O buffers.\n");
1231 } else if (Status
== EFI_NO_MEDIA
) {
1232 AsciiPrint ("\n PXE-E12: Could not detect network connection.\n");
1233 } else if (Status
== EFI_NO_RESPONSE
) {
1234 AsciiPrint ("\n PXE-E16: No offer received.\n");
1235 } else if (Status
== EFI_TIMEOUT
) {
1236 AsciiPrint ("\n PXE-E18: Server response timeout.\n");
1237 } else if (Status
== EFI_ABORTED
) {
1238 AsciiPrint ("\n PXE-E21: Remote boot cancelled.\n");
1239 } else if (Status
== EFI_ICMP_ERROR
) {
1240 AsciiPrint ("\n PXE-E22: Client received ICMP error from server.\n");
1241 } else if (Status
== EFI_TFTP_ERROR
) {
1242 AsciiPrint ("\n PXE-E23: Client received TFTP error from server.\n");
1243 } else if (Status
== EFI_NOT_FOUND
) {
1244 AsciiPrint ("\n PXE-E53: No boot filename received.\n");
1245 } else if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1246 AsciiPrint ("\n PXE-E99: Unexpected network error.\n");