2 Boot functions implementation for UefiPxeBc Driver.
4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "PxeBcImpl.h"
21 Display the string of the boot item.
23 If the length of the boot item string beyond 70 Char, just display 70 Char.
25 @param[in] Str The pointer to the string.
26 @param[in] Len The length of the string.
30 PxeBcDisplayBootItem (
38 // Cut off the chars behind 70th.
40 Len
= (UINT8
) MIN (PXEBC_DISPLAY_MAX_LINE
, Len
);
43 AsciiPrint ("%a \n", Str
);
46 // Restore the original 70th char.
53 Select and maintain the boot prompt if needed.
55 @param[in] Private Pointer to PxeBc private data.
57 @retval EFI_SUCCESS Selected boot prompt done.
58 @retval EFI_TIMEOUT Selected boot prompt timed out.
59 @retval EFI_NOT_FOUND The proxy offer is not Pxe10.
60 @retval EFI_ABORTED User cancelled the operation.
61 @retval EFI_NOT_READY Reading the input key from the keyboard has not finish.
65 PxeBcSelectBootPrompt (
66 IN PXEBC_PRIVATE_DATA
*Private
69 PXEBC_DHCP_PACKET_CACHE
*Cache
;
70 PXEBC_VENDOR_OPTION
*VendorOpt
;
71 EFI_PXE_BASE_CODE_MODE
*Mode
;
72 EFI_EVENT TimeoutEvent
;
73 EFI_EVENT DescendEvent
;
74 EFI_INPUT_KEY InputKey
;
85 Mode
= Private
->PxeBc
.Mode
;
86 Cache
= Mode
->ProxyOfferReceived
? &Private
->ProxyOffer
: &Private
->DhcpAck
;
87 OfferType
= Mode
->UsingIpv6
? Cache
->Dhcp6
.OfferType
: Cache
->Dhcp4
.OfferType
;
90 // Only DhcpPxe10 and ProxyPxe10 offer needs boot prompt.
92 if (OfferType
!= PxeOfferTypeProxyPxe10
&& OfferType
!= PxeOfferTypeDhcpPxe10
) {
97 // There is no specified ProxyPxe10 for IPv6 in PXE and UEFI spec.
99 ASSERT (!Mode
->UsingIpv6
);
101 VendorOpt
= &Cache
->Dhcp4
.VendorOpt
;
103 // According to the PXE specification 2.1, Table 2-1 PXE DHCP Options,
104 // we must not consider a boot prompt or boot menu if all of the following hold:
105 // - the PXE_DISCOVERY_CONTROL tag(6) is present inside the Vendor Options(43), and has bit 3 set
106 // - a boot file name has been presented in the initial DHCP or ProxyDHCP offer packet.
108 if (IS_DISABLE_PROMPT_MENU (VendorOpt
->DiscoverCtrl
) &&
109 Cache
->Dhcp4
.OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
) {
113 if (!IS_VALID_BOOT_PROMPT (VendorOpt
->BitMap
)) {
117 Timeout
= VendorOpt
->MenuPrompt
->Timeout
;
118 Prompt
= VendorOpt
->MenuPrompt
->Prompt
;
119 PromptLen
= (UINT8
) (VendorOpt
->MenuPromptLen
- 1);
122 // The valid scope of Timeout refers to PXE2.1 spec.
127 if (Timeout
== 255) {
132 // Create and start a timer as timeout event.
134 Status
= gBS
->CreateEvent (
141 if (EFI_ERROR (Status
)) {
145 Status
= gBS
->SetTimer (
148 MultU64x32 (Timeout
, TICKS_PER_SECOND
)
150 if (EFI_ERROR (Status
)) {
155 // Create and start a periodic timer as descend event by second.
157 Status
= gBS
->CreateEvent (
164 if (EFI_ERROR (Status
)) {
168 Status
= gBS
->SetTimer (
173 if (EFI_ERROR (Status
)) {
178 // Display the boot item and cursor on the screen.
180 SecCol
= gST
->ConOut
->Mode
->CursorColumn
;
181 SecRow
= gST
->ConOut
->Mode
->CursorRow
;
183 PxeBcDisplayBootItem (Prompt
, PromptLen
);
185 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, SecCol
+ PromptLen
, SecRow
);
186 AsciiPrint ("(%d) ", Timeout
--);
188 Status
= EFI_TIMEOUT
;
189 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
190 if (!EFI_ERROR (gBS
->CheckEvent (DescendEvent
))) {
191 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, SecCol
+ PromptLen
, SecRow
);
192 AsciiPrint ("(%d) ", Timeout
--);
194 if (gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &InputKey
) == EFI_NOT_READY
) {
195 gBS
->Stall (10 * TICKS_PER_MS
);
199 // Parse the input key by user.
200 // If <F8> or <Ctrl> + <M> is pressed, return success to display the boot menu.
202 if (InputKey
.ScanCode
== 0) {
204 switch (InputKey
.UnicodeChar
) {
207 Status
= EFI_ABORTED
;
213 Status
= EFI_SUCCESS
;
222 switch (InputKey
.ScanCode
) {
225 Status
= EFI_SUCCESS
;
229 Status
= EFI_ABORTED
;
241 // Reset the cursor on the screen.
243 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0 , SecRow
+ 1);
246 if (DescendEvent
!= NULL
) {
247 gBS
->CloseEvent (DescendEvent
);
249 if (TimeoutEvent
!= NULL
) {
250 gBS
->CloseEvent (TimeoutEvent
);
258 Select the boot menu by user's input.
260 @param[in] Private Pointer to PxeBc private data.
261 @param[out] Type The type of the menu.
262 @param[in] UseDefaultItem Use default item or not.
264 @retval EFI_ABORTED User cancel operation.
265 @retval EFI_SUCCESS Select the boot menu success.
266 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.
270 PxeBcSelectBootMenu (
271 IN PXEBC_PRIVATE_DATA
*Private
,
273 IN BOOLEAN UseDefaultItem
276 EFI_PXE_BASE_CODE_MODE
*Mode
;
277 PXEBC_DHCP_PACKET_CACHE
*Cache
;
278 PXEBC_VENDOR_OPTION
*VendorOpt
;
279 EFI_INPUT_KEY InputKey
;
288 CHAR8 Blank
[PXEBC_DISPLAY_MAX_LINE
];
289 PXEBC_BOOT_MENU_ENTRY
*MenuItem
;
290 PXEBC_BOOT_MENU_ENTRY
*MenuArray
[PXEBC_MENU_MAX_NUM
];
296 Mode
= Private
->PxeBc
.Mode
;
297 Cache
= Mode
->ProxyOfferReceived
? &Private
->ProxyOffer
: &Private
->DhcpAck
;
298 OfferType
= Mode
->UsingIpv6
? Cache
->Dhcp6
.OfferType
: Cache
->Dhcp4
.OfferType
;
301 // There is no specified DhcpPxe10/ProxyPxe10 for IPv6 in PXE and UEFI spec.
303 ASSERT (!Mode
->UsingIpv6
);
304 ASSERT (OfferType
== PxeOfferTypeProxyPxe10
|| OfferType
== PxeOfferTypeDhcpPxe10
);
306 VendorOpt
= &Cache
->Dhcp4
.VendorOpt
;
307 if (!IS_VALID_BOOT_MENU (VendorOpt
->BitMap
)) {
312 // Display the boot menu on the screen.
314 SetMem (Blank
, sizeof(Blank
), ' ');
316 MenuSize
= VendorOpt
->BootMenuLen
;
317 MenuItem
= VendorOpt
->BootMenu
;
320 return EFI_DEVICE_ERROR
;
323 while (MenuSize
> 0 && Index
< PXEBC_MENU_MAX_NUM
) {
324 ASSERT (MenuItem
!= NULL
);
325 MenuArray
[Index
] = MenuItem
;
326 MenuSize
= (UINT8
) (MenuSize
- (MenuItem
->DescLen
+ 3));
327 MenuItem
= (PXEBC_BOOT_MENU_ENTRY
*) ((UINT8
*) MenuItem
+ MenuItem
->DescLen
+ 3);
331 if (UseDefaultItem
) {
332 ASSERT (MenuArray
[0] != NULL
);
333 CopyMem (Type
, &MenuArray
[0]->Type
, sizeof (UINT16
));
334 *Type
= NTOHS (*Type
);
340 for (Index
= 0; Index
< MenuNum
; Index
++) {
341 ASSERT (MenuArray
[Index
] != NULL
);
342 PxeBcDisplayBootItem (MenuArray
[Index
]->DescStr
, MenuArray
[Index
]->DescLen
);
345 TopRow
= gST
->ConOut
->Mode
->CursorRow
- MenuNum
;
348 // Select the boot item by user in the boot menu.
352 // Highlight selected row.
354 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_TEXT_ATTR (EFI_BLACK
, EFI_LIGHTGRAY
));
355 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ Select
);
356 ASSERT (Select
< PXEBC_MENU_MAX_NUM
);
357 ASSERT (MenuArray
[Select
] != NULL
);
358 Blank
[MenuArray
[Select
]->DescLen
] = 0;
359 AsciiPrint ("%a\r", Blank
);
360 PxeBcDisplayBootItem (MenuArray
[Select
]->DescStr
, MenuArray
[Select
]->DescLen
);
361 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ MenuNum
);
364 while (gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &InputKey
) == EFI_NOT_READY
) {
365 gBS
->Stall (10 * TICKS_PER_MS
);
368 if (InputKey
.ScanCode
== 0) {
369 switch (InputKey
.UnicodeChar
) {
371 InputKey
.ScanCode
= SCAN_ESC
;
374 case CTRL ('j'): /* linefeed */
375 case CTRL ('m'): /* return */
379 case CTRL ('i'): /* tab */
383 InputKey
.ScanCode
= SCAN_DOWN
;
386 case CTRL ('h'): /* backspace */
389 InputKey
.ScanCode
= SCAN_UP
;
393 InputKey
.ScanCode
= 0;
397 switch (InputKey
.ScanCode
) {
407 if (++Select
== MenuNum
) {
419 Select
= (UINT16
) (MenuNum
- 1);
427 // Unhighlight the last selected row.
429 gST
->ConOut
->SetAttribute (gST
->ConOut
, EFI_TEXT_ATTR (EFI_LIGHTGRAY
, EFI_BLACK
));
430 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ LastSelect
);
431 ASSERT (LastSelect
< PXEBC_MENU_MAX_NUM
);
432 ASSERT (MenuArray
[LastSelect
] != NULL
);
433 Blank
[MenuArray
[LastSelect
]->DescLen
] = 0;
434 AsciiPrint ("%a\r", Blank
);
435 PxeBcDisplayBootItem (MenuArray
[LastSelect
]->DescStr
, MenuArray
[LastSelect
]->DescLen
);
436 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, TopRow
+ MenuNum
);
440 // Swap the byte order.
442 ASSERT (Select
< PXEBC_MENU_MAX_NUM
);
443 ASSERT (MenuArray
[Select
] != NULL
);
444 CopyMem (Type
, &MenuArray
[Select
]->Type
, sizeof (UINT16
));
445 *Type
= NTOHS (*Type
);
452 Parse out the boot information from the last Dhcp4 reply packet.
454 @param[in, out] Private Pointer to PxeBc private data.
455 @param[out] BufferSize Size of the boot file to be downloaded.
457 @retval EFI_SUCCESS Successfully parsed out all the boot information.
458 @retval Others Failed to parse out the boot information.
463 IN OUT PXEBC_PRIVATE_DATA
*Private
,
464 OUT UINT64
*BufferSize
467 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
468 EFI_PXE_BASE_CODE_MODE
*Mode
;
470 PXEBC_DHCP4_PACKET_CACHE
*Cache4
;
472 PXEBC_VENDOR_OPTION
*VendorOpt
;
473 PXEBC_BOOT_SVR_ENTRY
*Entry
;
475 PxeBc
= &Private
->PxeBc
;
477 Status
= EFI_SUCCESS
;
481 // Get the last received Dhcp4 reply packet.
483 if (Mode
->PxeReplyReceived
) {
484 Cache4
= &Private
->PxeReply
.Dhcp4
;
485 } else if (Mode
->ProxyOfferReceived
) {
486 Cache4
= &Private
->ProxyOffer
.Dhcp4
;
488 Cache4
= &Private
->DhcpAck
.Dhcp4
;
491 ASSERT (Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
);
494 // Parse the boot server address.
495 // If prompt/discover is disabled, get the first boot server from the boot servers list.
496 // Otherwise, parse the boot server Ipv4 address from next server address field in DHCP header.
497 // If all these fields are not available, use option 54 instead.
499 VendorOpt
= &Cache4
->VendorOpt
;
500 if (IS_DISABLE_PROMPT_MENU (VendorOpt
->DiscoverCtrl
) && IS_VALID_BOOT_SERVERS (VendorOpt
->BitMap
)) {
501 Entry
= VendorOpt
->BootSvr
;
502 if (VendorOpt
->BootSvrLen
>= sizeof (PXEBC_BOOT_SVR_ENTRY
) && Entry
->IpCnt
> 0) {
506 sizeof (EFI_IPv4_ADDRESS
)
510 if (Private
->ServerIp
.Addr
[0] == 0) {
512 // ServerIp.Addr[0] equals zero means we failed to get IP address from boot server list.
513 // Try to use next server address field.
517 &Cache4
->Packet
.Offer
.Dhcp4
.Header
.ServerAddr
,
518 sizeof (EFI_IPv4_ADDRESS
)
521 if (Private
->ServerIp
.Addr
[0] == 0) {
523 // Still failed , use the IP address from option 54.
527 Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_SERVER_ID
]->Data
,
528 sizeof (EFI_IPv4_ADDRESS
)
533 // Parse the boot file name by option.
535 Private
->BootFileName
= Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Data
;
537 if (Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN
] != NULL
) {
539 // Parse the boot file size by option.
541 CopyMem (&Value
, Cache4
->OptList
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN
]->Data
, sizeof (Value
));
542 Value
= NTOHS (Value
);
544 // The field of boot file size is 512 bytes in unit.
546 *BufferSize
= 512 * Value
;
549 // Get the bootfile size by tftp command if no option available.
551 Status
= PxeBc
->Mtftp (
553 EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE
,
559 Private
->BootFileName
,
566 // Save the value of boot file size.
568 Private
->BootFileSize
= (UINTN
) *BufferSize
;
571 // Display all the information: boot server address, boot file name and boot file size.
573 AsciiPrint ("\n Server IP address is ");
574 PxeBcShowIp4Addr (&Private
->ServerIp
.v4
);
575 AsciiPrint ("\n NBP filename is %a", Private
->BootFileName
);
576 AsciiPrint ("\n NBP filesize is %d Bytes", Private
->BootFileSize
);
583 Parse out the boot information from the last Dhcp6 reply packet.
585 @param[in, out] Private Pointer to PxeBc private data.
586 @param[out] BufferSize Size of the boot file to be downloaded.
588 @retval EFI_SUCCESS Successfully parsed out all the boot information.
589 @retval EFI_BUFFER_TOO_SMALL
590 @retval Others Failed to parse out the boot information.
595 IN OUT PXEBC_PRIVATE_DATA
*Private
,
596 OUT UINT64
*BufferSize
599 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
600 EFI_PXE_BASE_CODE_MODE
*Mode
;
602 PXEBC_DHCP6_PACKET_CACHE
*Cache6
;
605 PxeBc
= &Private
->PxeBc
;
607 Status
= EFI_SUCCESS
;
611 // Get the last received Dhcp6 reply packet.
613 if (Mode
->PxeReplyReceived
) {
614 Cache6
= &Private
->PxeReply
.Dhcp6
;
615 } else if (Mode
->ProxyOfferReceived
) {
616 Cache6
= &Private
->ProxyOffer
.Dhcp6
;
618 Cache6
= &Private
->DhcpAck
.Dhcp6
;
621 ASSERT (Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_URL
] != NULL
);
624 // Set the station address to IP layer.
626 Status
= PxeBcSetIp6Address (Private
);
627 if (EFI_ERROR (Status
)) {
633 // Parse (m)tftp server ip address and bootfile name.
635 Status
= PxeBcExtractBootFileUrl (
637 &Private
->BootFileName
,
638 &Private
->ServerIp
.v6
,
639 (CHAR8
*) (Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_URL
]->Data
),
640 NTOHS (Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_URL
]->OpLen
)
642 if (EFI_ERROR (Status
)) {
647 // Parse the value of boot file size.
649 if (Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM
] != NULL
) {
651 // Parse it out if have the boot file parameter option.
653 Status
= PxeBcExtractBootFileParam ((CHAR8
*) Cache6
->OptList
[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM
]->Data
, &Value
);
654 if (EFI_ERROR (Status
)) {
658 // The field of boot file size is 512 bytes in unit.
660 *BufferSize
= 512 * Value
;
663 // Send get file size command by tftp if option unavailable.
665 Status
= PxeBc
->Mtftp (
667 EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE
,
673 Private
->BootFileName
,
680 // Save the value of boot file size.
682 Private
->BootFileSize
= (UINTN
) *BufferSize
;
685 // Display all the information: boot server address, boot file name and boot file size.
687 AsciiPrint ("\n Server IP address is ");
688 PxeBcShowIp6Addr (&Private
->ServerIp
.v6
);
689 AsciiPrint ("\n NBP filename is %a", Private
->BootFileName
);
690 AsciiPrint ("\n NBP filesize is %d Bytes", Private
->BootFileSize
);
697 Extract the discover information and boot server entry from the
698 cached packets if unspecified.
700 @param[in] Private Pointer to PxeBc private data.
701 @param[in] Type The type of bootstrap to perform.
702 @param[in, out] DiscoverInfo Pointer to EFI_PXE_BASE_CODE_DISCOVER_INFO.
703 @param[out] BootEntry Pointer to PXEBC_BOOT_SVR_ENTRY.
704 @param[out] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST.
706 @retval EFI_SUCCESS Successfully extracted the information.
707 @retval EFI_DEVICE_ERROR Failed to extract the information.
711 PxeBcExtractDiscoverInfo (
712 IN PXEBC_PRIVATE_DATA
*Private
,
714 IN OUT EFI_PXE_BASE_CODE_DISCOVER_INFO
**DiscoverInfo
,
715 OUT PXEBC_BOOT_SVR_ENTRY
**BootEntry
,
716 OUT EFI_PXE_BASE_CODE_SRVLIST
**SrvList
719 EFI_PXE_BASE_CODE_MODE
*Mode
;
720 PXEBC_DHCP4_PACKET_CACHE
*Cache4
;
721 PXEBC_VENDOR_OPTION
*VendorOpt
;
722 PXEBC_BOOT_SVR_ENTRY
*Entry
;
724 EFI_PXE_BASE_CODE_DISCOVER_INFO
*Info
;
727 Mode
= Private
->PxeBc
.Mode
;
728 Info
= *DiscoverInfo
;
730 if (Mode
->UsingIpv6
) {
732 Info
->UseUCast
= TRUE
;
734 Info
->SrvList
[0].Type
= Type
;
735 Info
->SrvList
[0].AcceptAnyResponse
= FALSE
;
738 // There is no vendor options specified in DHCPv6, so take BootFileUrl in the last cached packet.
740 CopyMem (&Info
->SrvList
[0].IpAddr
, &Private
->ServerIp
, sizeof (EFI_IP_ADDRESS
));
742 *SrvList
= Info
->SrvList
;
746 Cache4
= (Mode
->ProxyOfferReceived
) ? &Private
->ProxyOffer
.Dhcp4
: &Private
->DhcpAck
.Dhcp4
;
747 VendorOpt
= &Cache4
->VendorOpt
;
749 if (!Mode
->DhcpAckReceived
|| !IS_VALID_DISCOVER_VENDOR_OPTION (VendorOpt
->BitMap
)) {
751 // Address is not acquired or no discovery options.
753 return EFI_INVALID_PARAMETER
;
757 // Parse the boot server entry from the vendor option in the last cached packet.
759 Info
->UseMCast
= (BOOLEAN
) !IS_DISABLE_MCAST_DISCOVER (VendorOpt
->DiscoverCtrl
);
760 Info
->UseBCast
= (BOOLEAN
) !IS_DISABLE_BCAST_DISCOVER (VendorOpt
->DiscoverCtrl
);
761 Info
->MustUseList
= (BOOLEAN
) IS_ENABLE_USE_SERVER_LIST (VendorOpt
->DiscoverCtrl
);
762 Info
->UseUCast
= (BOOLEAN
) IS_VALID_BOOT_SERVERS (VendorOpt
->BitMap
);
764 if (Info
->UseMCast
) {
766 // Get the multicast discover ip address from vendor option if has.
768 CopyMem (&Info
->ServerMCastIp
.v4
, &VendorOpt
->DiscoverMcastIp
, sizeof (EFI_IPv4_ADDRESS
));
773 if (Info
->UseUCast
) {
774 Entry
= VendorOpt
->BootSvr
;
776 while (((UINT8
) (Entry
- VendorOpt
->BootSvr
)) < VendorOpt
->BootSvrLen
) {
777 if (Entry
->Type
== HTONS (Type
)) {
781 Entry
= GET_NEXT_BOOT_SVR_ENTRY (Entry
);
785 return EFI_DEVICE_ERROR
;
788 Info
->IpCnt
= Entry
->IpCnt
;
789 if (Info
->IpCnt
>= 1) {
790 *DiscoverInfo
= AllocatePool (sizeof (*Info
) + (Info
->IpCnt
- 1) * sizeof (**SrvList
));
791 if (*DiscoverInfo
== NULL
) {
792 return EFI_OUT_OF_RESOURCES
;
794 CopyMem (*DiscoverInfo
, Info
, sizeof (*Info
));
795 Info
= *DiscoverInfo
;
798 for (Index
= 0; Index
< Info
->IpCnt
; Index
++) {
799 CopyMem (&Info
->SrvList
[Index
].IpAddr
, &Entry
->IpAddr
[Index
], sizeof (EFI_IPv4_ADDRESS
));
800 Info
->SrvList
[Index
].AcceptAnyResponse
= !Info
->MustUseList
;
801 Info
->SrvList
[Index
].Type
= NTOHS (Entry
->Type
);
806 *SrvList
= Info
->SrvList
;
814 Build the discover packet and send out for boot server.
816 @param[in] Private Pointer to PxeBc private data.
817 @param[in] Type PxeBc option boot item type.
818 @param[in] Layer Pointer to option boot item layer.
819 @param[in] UseBis Use BIS or not.
820 @param[in] DestIp Pointer to the destination address.
821 @param[in] IpCount The count of the server address.
822 @param[in] SrvList Pointer to the server address list.
824 @retval EFI_SUCCESS Successfully discovered boot file.
825 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
826 @retval EFI_NOT_FOUND Can't get the PXE reply packet.
827 @retval Others Failed to discover boot file.
831 PxeBcDiscoverBootServer (
832 IN PXEBC_PRIVATE_DATA
*Private
,
836 IN EFI_IP_ADDRESS
*DestIp
,
838 IN EFI_PXE_BASE_CODE_SRVLIST
*SrvList
841 if (Private
->PxeBc
.Mode
->UsingIpv6
) {
842 return PxeBcDhcp6Discover (
850 return PxeBcDhcp4Discover (
864 Discover all the boot information for boot file.
866 @param[in, out] Private Pointer to PxeBc private data.
867 @param[out] BufferSize Size of the boot file to be downloaded.
869 @retval EFI_SUCCESS Successfully obtained all the boot information .
870 @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file.
871 @retval EFI_ABORTED User cancel current operation.
872 @retval Others Failed to parse out the boot information.
876 PxeBcDiscoverBootFile (
877 IN OUT PXEBC_PRIVATE_DATA
*Private
,
878 OUT UINT64
*BufferSize
881 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
882 EFI_PXE_BASE_CODE_MODE
*Mode
;
888 PxeBc
= &Private
->PxeBc
;
890 Type
= EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP
;
891 Layer
= EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL
;
894 // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and
895 // other pxe boot information.
897 Status
= PxeBc
->Dhcp (PxeBc
, TRUE
);
898 if (EFI_ERROR (Status
)) {
903 // Select a boot server from boot server list.
905 Status
= PxeBcSelectBootPrompt (Private
);
907 if (Status
== EFI_SUCCESS
) {
909 // Choose by user's input.
911 Status
= PxeBcSelectBootMenu (Private
, &Type
, FALSE
);
912 } else if (Status
== EFI_TIMEOUT
) {
914 // Choose by default item.
916 Status
= PxeBcSelectBootMenu (Private
, &Type
, TRUE
);
919 if (!EFI_ERROR (Status
)) {
921 if (Type
== EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP
) {
923 // Local boot(PXE bootstrap server) need abort
929 // Start to discover the boot server to get (m)tftp server ip address, bootfile
930 // name and bootfile size.
932 UseBis
= (BOOLEAN
) (Mode
->BisSupported
&& Mode
->BisDetected
);
933 Status
= PxeBc
->Discover (PxeBc
, Type
, &Layer
, UseBis
, NULL
);
934 if (EFI_ERROR (Status
)) {
938 if (Mode
->PxeReplyReceived
&& !Mode
->ProxyOfferReceived
) {
940 // Some network boot loader only search the packet in Mode.ProxyOffer to get its server
941 // IP address, so we need to store a copy of Mode.PxeReply packet into Mode.ProxyOffer.
943 if (Mode
->UsingIpv6
) {
945 &Mode
->ProxyOffer
.Dhcpv6
,
946 &Mode
->PxeReply
.Dhcpv6
,
947 Private
->PxeReply
.Dhcp6
.Packet
.Ack
.Length
951 &Mode
->ProxyOffer
.Dhcpv4
,
952 &Mode
->PxeReply
.Dhcpv4
,
953 Private
->PxeReply
.Dhcp4
.Packet
.Ack
.Length
956 Mode
->ProxyOfferReceived
= TRUE
;
961 // Parse the boot information.
963 if (Mode
->UsingIpv6
) {
964 Status
= PxeBcDhcp6BootInfo (Private
, BufferSize
);
966 Status
= PxeBcDhcp4BootInfo (Private
, BufferSize
);
974 Install PxeBaseCodeCallbackProtocol if not installed before.
976 @param[in, out] Private Pointer to PxeBc private data.
977 @param[out] NewMakeCallback If TRUE, it is a new callback.
978 Otherwise, it is not new callback.
979 @retval EFI_SUCCESS PxeBaseCodeCallbackProtocol installed succesfully.
980 @retval Others Failed to install PxeBaseCodeCallbackProtocol.
984 PxeBcInstallCallback (
985 IN OUT PXEBC_PRIVATE_DATA
*Private
,
986 OUT BOOLEAN
*NewMakeCallback
989 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
993 // Check whether PxeBaseCodeCallbackProtocol already installed.
995 PxeBc
= &Private
->PxeBc
;
996 Status
= gBS
->HandleProtocol (
998 &gEfiPxeBaseCodeCallbackProtocolGuid
,
999 (VOID
**) &Private
->PxeBcCallback
1001 if (Status
== EFI_UNSUPPORTED
) {
1004 &Private
->LoadFileCallback
,
1005 &gPxeBcCallBackTemplate
,
1006 sizeof (EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
)
1010 // Install a default callback if user didn't offer one.
1012 Status
= gBS
->InstallProtocolInterface (
1013 &Private
->Controller
,
1014 &gEfiPxeBaseCodeCallbackProtocolGuid
,
1015 EFI_NATIVE_INTERFACE
,
1016 &Private
->LoadFileCallback
1019 (*NewMakeCallback
) = (BOOLEAN
) (Status
== EFI_SUCCESS
);
1021 Status
= PxeBc
->SetParameters (PxeBc
, NULL
, NULL
, NULL
, NULL
, NewMakeCallback
);
1022 if (EFI_ERROR (Status
)) {
1023 PxeBc
->Stop (PxeBc
);
1033 Uninstall PxeBaseCodeCallbackProtocol.
1035 @param[in] Private Pointer to PxeBc private data.
1036 @param[in] NewMakeCallback If TRUE, it is a new callback.
1037 Otherwise, it is not new callback.
1041 PxeBcUninstallCallback (
1042 IN PXEBC_PRIVATE_DATA
*Private
,
1043 IN BOOLEAN NewMakeCallback
1046 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
1048 PxeBc
= &Private
->PxeBc
;
1050 if (NewMakeCallback
) {
1052 NewMakeCallback
= FALSE
;
1054 PxeBc
->SetParameters (PxeBc
, NULL
, NULL
, NULL
, NULL
, &NewMakeCallback
);
1056 gBS
->UninstallProtocolInterface (
1057 Private
->Controller
,
1058 &gEfiPxeBaseCodeCallbackProtocolGuid
,
1059 &Private
->LoadFileCallback
1066 Download one of boot file in the list, and it's special for IPv6.
1068 @param[in] Private Pointer to PxeBc private data.
1069 @param[in, out] BufferSize Size of user buffer for input;
1070 required buffer size for output.
1071 @param[in] Buffer Pointer to user buffer.
1073 @retval EFI_SUCCESS Read one of boot file in the list successfully.
1074 @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file.
1075 @retval EFI_NOT_FOUND There is no proper boot file available.
1076 @retval Others Failed to download boot file in the list.
1080 PxeBcReadBootFileList (
1081 IN PXEBC_PRIVATE_DATA
*Private
,
1082 IN OUT UINT64
*BufferSize
,
1083 IN VOID
*Buffer OPTIONAL
1087 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
1089 PxeBc
= &Private
->PxeBc
;
1092 // Try to download the boot file if everything is ready.
1094 if (Buffer
!= NULL
) {
1095 Status
= PxeBc
->Mtftp (
1097 EFI_PXE_BASE_CODE_TFTP_READ_FILE
,
1101 &Private
->BlockSize
,
1103 Private
->BootFileName
,
1110 Status
= EFI_BUFFER_TOO_SMALL
;
1118 Load boot file into user buffer.
1120 @param[in] Private Pointer to PxeBc private data.
1121 @param[in, out] BufferSize Size of user buffer for input;
1122 required buffer size for output.
1123 @param[in] Buffer Pointer to user buffer.
1125 @retval EFI_SUCCESS Get all the boot information successfully.
1126 @retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file.
1127 @retval EFI_ABORTED User cancelled the current operation.
1128 @retval Others Failed to parse out the boot information.
1133 IN PXEBC_PRIVATE_DATA
*Private
,
1134 IN OUT UINTN
*BufferSize
,
1135 IN VOID
*Buffer OPTIONAL
1138 BOOLEAN NewMakeCallback
;
1139 UINT64 RequiredSize
;
1142 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
1143 EFI_PXE_BASE_CODE_MODE
*PxeBcMode
;
1145 NewMakeCallback
= FALSE
;
1146 PxeBc
= &Private
->PxeBc
;
1147 PxeBcMode
= &Private
->Mode
;
1148 CurrentSize
= *BufferSize
;
1152 // Install pxebc callback protocol if hasn't been installed yet.
1154 Status
= PxeBcInstallCallback (Private
, &NewMakeCallback
);
1155 if (EFI_ERROR(Status
)) {
1159 if (Private
->BootFileSize
== 0) {
1161 // Discover the boot information about the bootfile if hasn't.
1163 Status
= PxeBcDiscoverBootFile (Private
, &RequiredSize
);
1164 if (EFI_ERROR (Status
)) {
1168 if (PXEBC_IS_SIZE_OVERFLOWED (RequiredSize
)) {
1170 // It's error if the required buffer size is beyond the system scope.
1172 Status
= EFI_DEVICE_ERROR
;
1174 } else if (RequiredSize
> 0) {
1176 // Get the right buffer size of the bootfile required.
1178 if (CurrentSize
< RequiredSize
|| Buffer
== NULL
) {
1180 // It's buffer too small if the size of user buffer is smaller than the required.
1182 CurrentSize
= RequiredSize
;
1183 Status
= EFI_BUFFER_TOO_SMALL
;
1186 CurrentSize
= RequiredSize
;
1187 } else if (RequiredSize
== 0 && PxeBcMode
->UsingIpv6
) {
1189 // Try to download another bootfile in list if failed to get the filesize of the last one.
1190 // It's special for the case of IPv6.
1192 Status
= PxeBcReadBootFileList (Private
, &CurrentSize
, Buffer
);
1195 } else if (CurrentSize
< Private
->BootFileSize
|| Buffer
== NULL
) {
1197 // It's buffer too small if the size of user buffer is smaller than the required.
1199 CurrentSize
= Private
->BootFileSize
;
1200 Status
= EFI_BUFFER_TOO_SMALL
;
1205 // Begin to download the bootfile if everything is ready.
1207 AsciiPrint ("\n Downloading NBP file...\n");
1208 if (PxeBcMode
->UsingIpv6
) {
1209 Status
= PxeBcReadBootFileList (
1215 Status
= PxeBc
->Mtftp (
1217 EFI_PXE_BASE_CODE_TFTP_READ_FILE
,
1221 &Private
->BlockSize
,
1223 Private
->BootFileName
,
1230 *BufferSize
= (UINTN
) CurrentSize
;
1231 PxeBcUninstallCallback(Private
, NewMakeCallback
);
1233 if (Status
== EFI_SUCCESS
) {
1234 AsciiPrint ("\n NBP file downloaded successfully.\n");
1236 } else if (Status
== EFI_BUFFER_TOO_SMALL
&& Buffer
!= NULL
) {
1237 AsciiPrint ("\n PXE-E05: Buffer size is smaller than the requested file.\n");
1238 } else if (Status
== EFI_DEVICE_ERROR
) {
1239 AsciiPrint ("\n PXE-E07: Network device error.\n");
1240 } else if (Status
== EFI_OUT_OF_RESOURCES
) {
1241 AsciiPrint ("\n PXE-E09: Could not allocate I/O buffers.\n");
1242 } else if (Status
== EFI_NO_MEDIA
) {
1243 AsciiPrint ("\n PXE-E12: Could not detect network connection.\n");
1244 } else if (Status
== EFI_NO_RESPONSE
) {
1245 AsciiPrint ("\n PXE-E16: No offer received.\n");
1246 } else if (Status
== EFI_TIMEOUT
) {
1247 AsciiPrint ("\n PXE-E18: Server response timeout.\n");
1248 } else if (Status
== EFI_ABORTED
) {
1249 AsciiPrint ("\n PXE-E21: Remote boot cancelled.\n");
1250 } else if (Status
== EFI_ICMP_ERROR
) {
1251 AsciiPrint ("\n PXE-E22: Client received ICMP error from server.\n");
1252 } else if (Status
== EFI_TFTP_ERROR
) {
1253 AsciiPrint ("\n PXE-E23: Client received TFTP error from server.\n");
1254 } else if (Status
== EFI_NOT_FOUND
) {
1255 AsciiPrint ("\n PXE-E53: No boot filename received.\n");
1256 } else if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1257 AsciiPrint ("\n PXE-E99: Unexpected network error.\n");