2 Interface routines for PxeBc.
4 Copyright (c) 2007 - 2013, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "PxeBcImpl.h"
18 UINT32 mPxeDhcpTimeout
[4] = { 4, 8, 16, 32 };
21 Get and record the arp cache.
23 @param This Pointer to EFI_PXE_BC_PROTOCOL
25 @retval EFI_SUCCESS Arp cache updated successfully
26 @retval others If error occurs when getting arp cache
31 IN EFI_PXE_BASE_CODE_PROTOCOL
* This
34 PXEBC_PRIVATE_DATA
*Private
;
35 EFI_PXE_BASE_CODE_MODE
*Mode
;
39 EFI_ARP_FIND_DATA
*Entries
;
42 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
43 Mode
= Private
->PxeBc
.Mode
;
45 Status
= Private
->Arp
->Find (
54 if (EFI_ERROR (Status
)) {
58 Mode
->ArpCacheEntries
= MIN (
60 EFI_PXE_BASE_CODE_MAX_ARP_ENTRIES
62 for (Index
= 0; Index
< Mode
->ArpCacheEntries
; Index
++) {
64 &Mode
->ArpCache
[Index
].IpAddr
,
66 Entries
->SwAddressLength
69 &Mode
->ArpCache
[Index
].MacAddr
,
70 (UINT8
*) (Entries
+ 1) + Entries
->SwAddressLength
,
71 Entries
->HwAddressLength
74 // Slip to the next FindData.
76 Entries
= (EFI_ARP_FIND_DATA
*) ((UINT8
*) Entries
+ EntryLength
);
83 Timeout routine to update arp cache.
85 @param Event Pointer to EFI_PXE_BC_PROTOCOL
86 @param Context Context of the timer event
91 ArpCacheUpdateTimeout (
96 UpdateArpCache ((EFI_PXE_BASE_CODE_PROTOCOL
*) Context
);
100 Do arp resolution from arp cache in PxeBcMode.
102 @param PxeBcMode The PXE BC mode to look into.
103 @param Ip4Addr The Ip4 address for resolution.
104 @param MacAddress The resoluted MAC address if the resolution is successful.
105 The value is undefined if resolution fails.
107 @retval TRUE The resolution is successful.
108 @retval FALSE Otherwise.
113 IN EFI_PXE_BASE_CODE_MODE
*PxeBcMode
,
114 IN EFI_IPv4_ADDRESS
*Ip4Addr
,
115 OUT EFI_MAC_ADDRESS
*MacAddress
120 for (Index
= 0; Index
< PxeBcMode
->ArpCacheEntries
; Index
++) {
121 if (EFI_IP4_EQUAL (&PxeBcMode
->ArpCache
[Index
].IpAddr
.v4
, Ip4Addr
)) {
124 &PxeBcMode
->ArpCache
[Index
].MacAddr
,
125 sizeof (EFI_MAC_ADDRESS
)
135 Notify function for the ICMP receive token, used to process
136 the received ICMP packets.
138 @param Context The PXEBC private data.
143 IcmpErrorListenHandlerDpc (
148 EFI_IP4_RECEIVE_DATA
*RxData
;
149 EFI_IP4_PROTOCOL
*Ip4
;
150 PXEBC_PRIVATE_DATA
*Private
;
151 EFI_PXE_BASE_CODE_MODE
*Mode
;
154 UINT8
*CopiedPointer
;
156 Private
= (PXEBC_PRIVATE_DATA
*) Context
;
157 Mode
= &Private
->Mode
;
158 Status
= Private
->IcmpErrorRcvToken
.Status
;
159 RxData
= Private
->IcmpErrorRcvToken
.Packet
.RxData
;
162 if (Status
== EFI_ABORTED
) {
164 // The reception is actively aborted by the consumer, directly return.
169 if (EFI_ERROR (Status
) || (RxData
== NULL
)) {
171 // Only process the normal packets and the icmp error packets, if RxData is NULL
172 // with Status == EFI_SUCCESS or EFI_ICMP_ERROR, just resume the receive although
173 // this should be a bug of the low layer (IP).
178 if (EFI_IP4 (RxData
->Header
->SourceAddress
) != 0 &&
179 !NetIp4IsUnicast (EFI_NTOHL (RxData
->Header
->SourceAddress
), 0)) {
181 // The source address is not zero and it's not a unicast IP address, discard it.
186 if (!EFI_IP4_EQUAL (&RxData
->Header
->DestinationAddress
, &Mode
->StationIp
.v4
)) {
188 // The dest address is not equal to Station Ip address, discard it.
194 // Constructor ICMP error packet
197 CopiedPointer
= (UINT8
*) &Mode
->IcmpError
;
199 for (Index
= 0; Index
< RxData
->FragmentCount
; Index
++) {
200 CopiedLen
+= RxData
->FragmentTable
[Index
].FragmentLength
;
201 if (CopiedLen
<= sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR
)) {
204 RxData
->FragmentTable
[Index
].FragmentBuffer
,
205 RxData
->FragmentTable
[Index
].FragmentLength
210 RxData
->FragmentTable
[Index
].FragmentBuffer
,
211 CopiedLen
- sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR
)
214 CopiedPointer
+= CopiedLen
;
220 gBS
->SignalEvent (RxData
->RecycleSignal
);
223 Ip4
->Receive (Ip4
, &(Private
->IcmpErrorRcvToken
));
227 Request IcmpErrorListenHandlerDpc as a DPC at TPL_CALLBACK
229 @param Event The event signaled.
230 @param Context The context passed in by the event notifier.
235 IcmpErrorListenHandler (
241 // Request IpIoListenHandlerDpc as a DPC at TPL_CALLBACK
243 QueueDpc (TPL_CALLBACK
, IcmpErrorListenHandlerDpc
, Context
);
247 Enables the use of the PXE Base Code Protocol functions.
249 This function enables the use of the PXE Base Code Protocol functions. If the
250 Started field of the EFI_PXE_BASE_CODE_MODE structure is already TRUE, then
251 EFI_ALREADY_STARTED will be returned. If UseIpv6 is TRUE, then IPv6 formatted
252 addresses will be used in this session. If UseIpv6 is FALSE, then IPv4 formatted
253 addresses will be used in this session. If UseIpv6 is TRUE, and the Ipv6Supported
254 field of the EFI_PXE_BASE_CODE_MODE structure is FALSE, then EFI_UNSUPPORTED will
255 be returned. If there is not enough memory or other resources to start the PXE
256 Base Code Protocol, then EFI_OUT_OF_RESOURCES will be returned. Otherwise, the
257 PXE Base Code Protocol will be started, and all of the fields of the EFI_PXE_BASE_CODE_MODE
258 structure will be initialized as follows:
260 Ipv6SupportedUnchanged.
261 Ipv6AvailableUnchanged.
262 UsingIpv6Set to UseIpv6.
263 BisSupportedUnchanged.
264 BisDetectedUnchanged.
266 SendGUIDSet to FALSE.
267 TTLSet to DEFAULT_TTL.
268 ToSSet to DEFAULT_ToS.
269 DhcpCompletedSet to FALSE.
270 ProxyOfferReceivedSet to FALSE.
271 StationIpSet to an address of all zeros.
272 SubnetMaskSet to a subnet mask of all zeros.
273 DhcpDiscoverZero-filled.
275 ProxyOfferZero-filled.
276 PxeDiscoverValidSet to FALSE.
277 PxeDiscoverZero-filled.
278 PxeReplyValidSet to FALSE.
280 PxeBisReplyValidSet to FALSE.
281 PxeBisReplyZero-filled.
282 IpFilterSet the Filters field to 0 and the IpCnt field to 0.
283 ArpCacheEntriesSet to 0.
285 RouteTableEntriesSet to 0.
286 RouteTableZero-filled.
287 IcmpErrorReceivedSet to FALSE.
288 IcmpErrorZero-filled.
289 TftpErroReceivedSet to FALSE.
290 TftpErrorZero-filled.
291 MakeCallbacksSet to TRUE if the PXE Base Code Callback Protocol is available.
292 Set to FALSE if the PXE Base Code Callback Protocol is not available.
294 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
295 @param UseIpv6 Specifies the type of IP addresses that are to be used during the session
296 that is being started. Set to TRUE for IPv6 addresses, and FALSE for
299 @retval EFI_SUCCESS The PXE Base Code Protocol was started.
300 @retval EFI_DEVICE_ERROR The network device encountered an error during this oper
301 @retval EFI_UNSUPPORTED UseIpv6 is TRUE, but the Ipv6Supported field of the
302 EFI_PXE_BASE_CODE_MODE structure is FALSE.
303 @retval EFI_ALREADY_STARTED The PXE Base Code Protocol is already in the started state.
304 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
305 EFI_PXE_BASE_CODE_PROTOCOL structure.
306 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory or other resources to start the
307 PXE Base Code Protocol.
313 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
317 PXEBC_PRIVATE_DATA
*Private
;
318 EFI_PXE_BASE_CODE_MODE
*Mode
;
322 return EFI_INVALID_PARAMETER
;
325 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
326 Mode
= Private
->PxeBc
.Mode
;
329 return EFI_ALREADY_STARTED
;
334 // IPv6 is not supported now.
336 return EFI_UNSUPPORTED
;
340 // Configure the udp4 instance to let it receive data
342 Status
= Private
->Udp4Read
->Configure (
344 &Private
->Udp4CfgData
346 if (EFI_ERROR (Status
)) {
352 // Configure block size for TFTP as a default value to handle all link layers.
354 Private
->BlockSize
= (UINTN
) (MIN (Private
->Ip4MaxPacketSize
, PXEBC_DEFAULT_PACKET_SIZE
) -
355 PXEBC_DEFAULT_UDP_OVERHEAD_SIZE
- PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE
);
357 // If PcdTftpBlockSize is set to non-zero, override the default value.
359 if (PcdGet64 (PcdTftpBlockSize
) != 0) {
360 Private
->BlockSize
= (UINTN
) PcdGet64 (PcdTftpBlockSize
);
363 Private
->AddressIsOk
= FALSE
;
365 ZeroMem (Mode
, sizeof (EFI_PXE_BASE_CODE_MODE
));
367 Mode
->Started
= TRUE
;
368 Mode
->TTL
= DEFAULT_TTL
;
369 Mode
->ToS
= DEFAULT_ToS
;
370 Mode
->AutoArp
= TRUE
;
373 // Create the event for Arp Cache checking.
375 Status
= gBS
->CreateEvent (
376 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
378 ArpCacheUpdateTimeout
,
380 &Private
->GetArpCacheEvent
382 if (EFI_ERROR (Status
)) {
387 // Start the timeout timer event.
389 Status
= gBS
->SetTimer (
390 Private
->GetArpCacheEvent
,
395 if (EFI_ERROR (Status
)) {
400 // Create ICMP error receiving event
402 Status
= gBS
->CreateEvent (
405 IcmpErrorListenHandler
,
407 &(Private
->IcmpErrorRcvToken
.Event
)
409 if (EFI_ERROR (Status
)) {
413 Status
= Private
->Ip4
->Configure (Private
->Ip4
, &Private
->Ip4ConfigData
);
414 if (EFI_ERROR (Status
)) {
419 // start to listen incoming packet
421 Status
= Private
->Ip4
->Receive (Private
->Ip4
, &Private
->IcmpErrorRcvToken
);
422 if (!EFI_ERROR (Status
)) {
427 Private
->Ip4
->Configure (Private
->Ip4
, NULL
);
429 if (Private
->IcmpErrorRcvToken
.Event
!= NULL
) {
430 gBS
->CloseEvent (Private
->IcmpErrorRcvToken
.Event
);
433 if (Private
->GetArpCacheEvent
!= NULL
) {
434 gBS
->SetTimer (Private
->GetArpCacheEvent
, TimerCancel
, 0);
435 gBS
->CloseEvent (Private
->GetArpCacheEvent
);
438 Mode
->Started
= FALSE
;
441 Mode
->AutoArp
= FALSE
;
448 Disables the use of the PXE Base Code Protocol functions.
450 This function stops all activity on the network device. All the resources allocated
451 in Start() are released, the Started field of the EFI_PXE_BASE_CODE_MODE structure is
452 set to FALSE and EFI_SUCCESS is returned. If the Started field of the EFI_PXE_BASE_CODE_MODE
453 structure is already FALSE, then EFI_NOT_STARTED will be returned.
455 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
457 @retval EFI_SUCCESS The PXE Base Code Protocol was stopped.
458 @retval EFI_NOT_STARTED The PXE Base Code Protocol is already in the stopped state.
459 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
460 EFI_PXE_BASE_CODE_PROTOCOL structure.
461 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
467 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
470 PXEBC_PRIVATE_DATA
*Private
;
471 EFI_PXE_BASE_CODE_MODE
*Mode
;
474 return EFI_INVALID_PARAMETER
;
477 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
478 Mode
= Private
->PxeBc
.Mode
;
480 if (!Mode
->Started
) {
481 return EFI_NOT_STARTED
;
484 Private
->Ip4
->Cancel (Private
->Ip4
, NULL
);
486 // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's
491 Private
->Ip4
->Configure (Private
->Ip4
, NULL
);
494 // Close the ICMP error receiving event.
496 gBS
->CloseEvent (Private
->IcmpErrorRcvToken
.Event
);
499 // Cancel the TimeoutEvent timer.
501 gBS
->SetTimer (Private
->GetArpCacheEvent
, TimerCancel
, 0);
504 // Close the TimeoutEvent event.
506 gBS
->CloseEvent (Private
->GetArpCacheEvent
);
508 Mode
->Started
= FALSE
;
510 Private
->CurrentUdpSrcPort
= 0;
511 Private
->Udp4Write
->Configure (Private
->Udp4Write
, NULL
);
512 Private
->Udp4Read
->Groups (Private
->Udp4Read
, FALSE
, NULL
);
513 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
515 Private
->Dhcp4
->Stop (Private
->Dhcp4
);
516 Private
->Dhcp4
->Configure (Private
->Dhcp4
, NULL
);
518 Private
->FileSize
= 0;
525 Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / acknowledge) or DHCPv6
526 S.A.R.R (solicit / advertise / request / reply) sequence.
528 This function attempts to complete the DHCP sequence. If this sequence is completed,
529 then EFI_SUCCESS is returned, and the DhcpCompleted, ProxyOfferReceived, StationIp,
530 SubnetMask, DhcpDiscover, DhcpAck, and ProxyOffer fields of the EFI_PXE_BASE_CODE_MODE
531 structure are filled in.
532 If SortOffers is TRUE, then the cached DHCP offer packets will be sorted before
533 they are tried. If SortOffers is FALSE, then the cached DHCP offer packets will
534 be tried in the order in which they are received. Please see the Preboot Execution
535 Environment (PXE) Specification for additional details on the implementation of DHCP.
536 This function can take at least 31 seconds to timeout and return control to the
537 caller. If the DHCP sequence does not complete, then EFI_TIMEOUT will be returned.
538 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
539 then the DHCP sequence will be stopped and EFI_ABORTED will be returned.
541 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
542 @param SortOffers TRUE if the offers received should be sorted. Set to FALSE to try the
543 offers in the order that they are received.
545 @retval EFI_SUCCESS Valid DHCP has completed.
546 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
547 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
548 EFI_PXE_BASE_CODE_PROTOCOL structure.
549 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
550 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete the DHCP Protocol.
551 @retval EFI_ABORTED The callback function aborted the DHCP Protocol.
552 @retval EFI_TIMEOUT The DHCP Protocol timed out.
553 @retval EFI_ICMP_ERROR An ICMP error packet was received during the DHCP session.
554 @retval EFI_NO_RESPONSE Valid PXE offer was not received.
560 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
561 IN BOOLEAN SortOffers
564 PXEBC_PRIVATE_DATA
*Private
;
565 EFI_PXE_BASE_CODE_MODE
*Mode
;
566 EFI_DHCP4_PROTOCOL
*Dhcp4
;
567 EFI_DHCP4_CONFIG_DATA Dhcp4CfgData
;
568 EFI_DHCP4_MODE_DATA Dhcp4Mode
;
569 EFI_DHCP4_PACKET_OPTION
*OptList
[PXEBC_DHCP4_MAX_OPTION_NUM
];
572 EFI_ARP_CONFIG_DATA ArpConfigData
;
573 EFI_PXE_BASE_CODE_IP_FILTER IpFilter
;
576 return EFI_INVALID_PARAMETER
;
579 Status
= EFI_SUCCESS
;
580 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
581 Mode
= Private
->PxeBc
.Mode
;
582 Dhcp4
= Private
->Dhcp4
;
583 Private
->Function
= EFI_PXE_BASE_CODE_FUNCTION_DHCP
;
584 Private
->SortOffers
= SortOffers
;
586 if (!Mode
->Started
) {
587 return EFI_NOT_STARTED
;
590 Mode
->IcmpErrorReceived
= FALSE
;
593 // Stop Udp4Read instance
595 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
598 // Initialize the DHCP options and build the option list
600 OptCount
= PxeBcBuildDhcpOptions (Private
, OptList
, TRUE
);
603 // Set the DHCP4 config data.
604 // The four discovery timeouts are 4, 8, 16, 32 seconds respectively.
606 ZeroMem (&Dhcp4CfgData
, sizeof (EFI_DHCP4_CONFIG_DATA
));
607 Dhcp4CfgData
.OptionCount
= OptCount
;
608 Dhcp4CfgData
.OptionList
= OptList
;
609 Dhcp4CfgData
.Dhcp4Callback
= PxeBcDhcpCallBack
;
610 Dhcp4CfgData
.CallbackContext
= Private
;
611 Dhcp4CfgData
.DiscoverTryCount
= 4;
612 Dhcp4CfgData
.DiscoverTimeout
= mPxeDhcpTimeout
;
614 Status
= Dhcp4
->Configure (Dhcp4
, &Dhcp4CfgData
);
615 if (EFI_ERROR (Status
)) {
620 // Zero those arrays to record the varies numbers of DHCP OFFERS.
622 Private
->GotProxyOffer
= FALSE
;
623 Private
->NumOffers
= 0;
624 Private
->BootpIndex
= 0;
625 ZeroMem (Private
->ServerCount
, sizeof (Private
->ServerCount
));
626 ZeroMem (Private
->ProxyIndex
, sizeof (Private
->ProxyIndex
));
628 Status
= Dhcp4
->Start (Dhcp4
, NULL
);
629 if (EFI_ERROR (Status
)) {
630 if (Status
== EFI_ICMP_ERROR
) {
631 Mode
->IcmpErrorReceived
= TRUE
;
636 Status
= Dhcp4
->GetModeData (Dhcp4
, &Dhcp4Mode
);
637 if (EFI_ERROR (Status
)) {
641 ASSERT (Dhcp4Mode
.State
== Dhcp4Bound
);
643 CopyMem (&Private
->StationIp
, &Dhcp4Mode
.ClientAddress
, sizeof (EFI_IPv4_ADDRESS
));
644 CopyMem (&Private
->SubnetMask
, &Dhcp4Mode
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
645 CopyMem (&Private
->GatewayIp
, &Dhcp4Mode
.RouterAddress
, sizeof (EFI_IPv4_ADDRESS
));
647 CopyMem (&Mode
->StationIp
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
648 CopyMem (&Mode
->SubnetMask
, &Private
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
651 // Check the selected offer to see whether BINL is required, if no or BINL is
652 // finished, set the various Mode members.
654 Status
= PxeBcCheckSelectedOffer (Private
);
657 if (EFI_ERROR (Status
)) {
659 Dhcp4
->Configure (Dhcp4
, NULL
);
662 // Remove the previously configured option list and callback function
664 ZeroMem (&Dhcp4CfgData
, sizeof (EFI_DHCP4_CONFIG_DATA
));
665 Dhcp4
->Configure (Dhcp4
, &Dhcp4CfgData
);
667 Private
->AddressIsOk
= TRUE
;
669 if (!Mode
->UsingIpv6
) {
671 // If in IPv4 mode, configure the corresponding ARP with this new
672 // station IP address.
674 ZeroMem (&ArpConfigData
, sizeof (EFI_ARP_CONFIG_DATA
));
676 ArpConfigData
.SwAddressType
= 0x0800;
677 ArpConfigData
.SwAddressLength
= (UINT8
) sizeof (EFI_IPv4_ADDRESS
);
678 ArpConfigData
.StationAddress
= &Private
->StationIp
.v4
;
680 Private
->Arp
->Configure (Private
->Arp
, NULL
);
681 Private
->Arp
->Configure (Private
->Arp
, &ArpConfigData
);
684 // Updated the route table. Fill the first entry.
686 Mode
->RouteTableEntries
= 1;
687 Mode
->RouteTable
[0].IpAddr
.Addr
[0] = Private
->StationIp
.Addr
[0] & Private
->SubnetMask
.Addr
[0];
688 Mode
->RouteTable
[0].SubnetMask
.Addr
[0] = Private
->SubnetMask
.Addr
[0];
689 Mode
->RouteTable
[0].GwAddr
.Addr
[0] = 0;
692 // Create the default route entry if there is a default router.
694 if (Private
->GatewayIp
.Addr
[0] != 0) {
695 Mode
->RouteTableEntries
= 2;
696 Mode
->RouteTable
[1].IpAddr
.Addr
[0] = 0;
697 Mode
->RouteTable
[1].SubnetMask
.Addr
[0] = 0;
698 Mode
->RouteTable
[1].GwAddr
.Addr
[0] = Private
->GatewayIp
.Addr
[0];
702 // Flush new station IP address into Udp4CfgData and Ip4ConfigData
704 CopyMem (&Private
->Udp4CfgData
.StationAddress
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
705 CopyMem (&Private
->Udp4CfgData
.SubnetMask
, &Private
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
706 CopyMem (&Private
->Ip4ConfigData
.StationAddress
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
707 CopyMem (&Private
->Ip4ConfigData
.SubnetMask
, &Private
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
710 // Reconfigure the Ip4 instance to capture background ICMP packets with new station Ip address.
712 Private
->Ip4
->Cancel (Private
->Ip4
, &Private
->IcmpErrorRcvToken
);
713 Private
->Ip4
->Configure (Private
->Ip4
, NULL
);
715 Status
= Private
->Ip4
->Configure (Private
->Ip4
, &Private
->Ip4ConfigData
);
716 if (EFI_ERROR (Status
)) {
720 Status
= Private
->Ip4
->Receive (Private
->Ip4
, &Private
->IcmpErrorRcvToken
);
721 if (EFI_ERROR (Status
)) {
727 Private
->Udp4Read
->Configure (Private
->Udp4Read
, &Private
->Udp4CfgData
);
730 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
731 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
733 ZeroMem(&IpFilter
, sizeof (EFI_PXE_BASE_CODE_IP_FILTER
));
734 IpFilter
.Filters
= EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
;
735 This
->SetIpFilter (This
, &IpFilter
);
742 Attempts to complete the PXE Boot Server and/or boot image discovery sequence.
744 This function attempts to complete the PXE Boot Server and/or boot image discovery
745 sequence. If this sequence is completed, then EFI_SUCCESS is returned, and the
746 PxeDiscoverValid, PxeDiscover, PxeReplyReceived, and PxeReply fields of the
747 EFI_PXE_BASE_CODE_MODE structure are filled in. If UseBis is TRUE, then the
748 PxeBisReplyReceived and PxeBisReply fields of the EFI_PXE_BASE_CODE_MODE structure
749 will also be filled in. If UseBis is FALSE, then PxeBisReplyValid will be set to FALSE.
750 In the structure referenced by parameter Info, the PXE Boot Server list, SrvList[],
751 has two uses: It is the Boot Server IP address list used for unicast discovery
752 (if the UseUCast field is TRUE), and it is the list used for Boot Server verification
753 (if the MustUseList field is TRUE). Also, if the MustUseList field in that structure
754 is TRUE and the AcceptAnyResponse field in the SrvList[] array is TRUE, any Boot
755 Server reply of that type will be accepted. If the AcceptAnyResponse field is
756 FALSE, only responses from Boot Servers with matching IP addresses will be accepted.
757 This function can take at least 10 seconds to timeout and return control to the
758 caller. If the Discovery sequence does not complete, then EFI_TIMEOUT will be
759 returned. Please see the Preboot Execution Environment (PXE) Specification for
760 additional details on the implementation of the Discovery sequence.
761 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
762 then the Discovery sequence is stopped and EFI_ABORTED will be returned.
764 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
765 @param Type The type of bootstrap to perform.
766 @param Layer Pointer to the boot server layer number to discover, which must be
767 PXE_BOOT_LAYER_INITIAL when a new server type is being
769 @param UseBis TRUE if Boot Integrity Services are to be used. FALSE otherwise.
770 @param Info Pointer to a data structure that contains additional information on the
771 type of discovery operation that is to be performed.
773 @retval EFI_SUCCESS The Discovery sequence has been completed.
774 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
775 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
776 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
777 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete Discovery.
778 @retval EFI_ABORTED The callback function aborted the Discovery sequence.
779 @retval EFI_TIMEOUT The Discovery sequence timed out.
780 @retval EFI_ICMP_ERROR An ICMP error packet was received during the PXE discovery
787 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
791 IN EFI_PXE_BASE_CODE_DISCOVER_INFO
*Info OPTIONAL
794 PXEBC_PRIVATE_DATA
*Private
;
795 EFI_PXE_BASE_CODE_MODE
*Mode
;
796 EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo
;
797 EFI_PXE_BASE_CODE_DISCOVER_INFO
*CreatedInfo
;
798 EFI_PXE_BASE_CODE_SRVLIST
*SrvList
;
799 EFI_PXE_BASE_CODE_SRVLIST DefaultSrvList
;
800 PXEBC_CACHED_DHCP4_PACKET
*Packet
;
801 PXEBC_VENDOR_OPTION
*VendorOpt
;
804 PXEBC_BOOT_SVR_ENTRY
*BootSvrEntry
;
805 EFI_PXE_BASE_CODE_IP_FILTER IpFilter
;
808 return EFI_INVALID_PARAMETER
;
811 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
812 Mode
= Private
->PxeBc
.Mode
;
816 Status
= EFI_DEVICE_ERROR
;
817 Private
->Function
= EFI_PXE_BASE_CODE_FUNCTION_DISCOVER
;
819 if (!Private
->AddressIsOk
) {
820 return EFI_INVALID_PARAMETER
;
823 if (!Mode
->Started
) {
824 return EFI_NOT_STARTED
;
828 // Stop Udp4Read instance
830 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
832 Mode
->IcmpErrorReceived
= FALSE
;
835 // If layer isn't EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL,
836 // use the previous setting;
837 // If info isn't offered,
838 // use the cached DhcpAck and ProxyOffer packets.
840 ZeroMem (&DefaultInfo
, sizeof (EFI_PXE_BASE_CODE_DISCOVER_INFO
));
841 if (*Layer
!= EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL
) {
843 if (!Mode
->PxeDiscoverValid
|| !Mode
->PxeReplyReceived
|| (!Mode
->PxeBisReplyReceived
&& UseBis
)) {
845 Status
= EFI_INVALID_PARAMETER
;
849 DefaultInfo
.IpCnt
= 1;
850 DefaultInfo
.UseUCast
= TRUE
;
852 DefaultSrvList
.Type
= Type
;
853 DefaultSrvList
.AcceptAnyResponse
= FALSE
;
854 DefaultSrvList
.IpAddr
.Addr
[0] = Private
->ServerIp
.Addr
[0];
856 SrvList
= &DefaultSrvList
;
858 } else if (Info
== NULL
) {
860 // Create info by the cached packet before
862 Packet
= (Mode
->ProxyOfferReceived
) ? &Private
->ProxyOffer
: &Private
->Dhcp4Ack
;
863 VendorOpt
= &Packet
->PxeVendorOption
;
865 if (!Mode
->DhcpAckReceived
|| !IS_VALID_DISCOVER_VENDOR_OPTION (VendorOpt
->BitMap
)) {
867 // Address is not acquired or no discovery options.
869 Status
= EFI_INVALID_PARAMETER
;
873 DefaultInfo
.UseMCast
= (BOOLEAN
)!IS_DISABLE_MCAST_DISCOVER (VendorOpt
->DiscoverCtrl
);
874 DefaultInfo
.UseBCast
= (BOOLEAN
)!IS_DISABLE_BCAST_DISCOVER (VendorOpt
->DiscoverCtrl
);
875 DefaultInfo
.MustUseList
= (BOOLEAN
) IS_ENABLE_USE_SERVER_LIST (VendorOpt
->DiscoverCtrl
);
876 DefaultInfo
.UseUCast
= DefaultInfo
.MustUseList
;
878 if (DefaultInfo
.UseMCast
) {
880 // Get the multicast discover ip address from vendor option.
883 &DefaultInfo
.ServerMCastIp
.Addr
,
884 &VendorOpt
->DiscoverMcastIp
,
885 sizeof (EFI_IPv4_ADDRESS
)
889 DefaultInfo
.IpCnt
= 0;
891 SrvList
= Info
->SrvList
;
893 if (DefaultInfo
.MustUseList
) {
894 BootSvrEntry
= VendorOpt
->BootSvr
;
895 Status
= EFI_INVALID_PARAMETER
;
897 while (((UINT8
) (BootSvrEntry
- VendorOpt
->BootSvr
)) < VendorOpt
->BootSvrLen
) {
899 if (BootSvrEntry
->Type
== HTONS (Type
)) {
900 Status
= EFI_SUCCESS
;
904 BootSvrEntry
= GET_NEXT_BOOT_SVR_ENTRY (BootSvrEntry
);
907 if (EFI_ERROR (Status
)) {
911 DefaultInfo
.IpCnt
= BootSvrEntry
->IpCnt
;
913 if (DefaultInfo
.IpCnt
>= 1) {
914 CreatedInfo
= AllocatePool (sizeof (DefaultInfo
) + (DefaultInfo
.IpCnt
- 1) * sizeof (*SrvList
));
915 if (CreatedInfo
== NULL
) {
916 Status
= EFI_OUT_OF_RESOURCES
;
921 CopyMem (CreatedInfo
, &DefaultInfo
, sizeof (DefaultInfo
));
923 SrvList
= Info
->SrvList
;
926 for (Index
= 0; Index
< DefaultInfo
.IpCnt
; Index
++) {
927 CopyMem (&SrvList
[Index
].IpAddr
, &BootSvrEntry
->IpAddr
[Index
], sizeof (EFI_IPv4_ADDRESS
));
928 SrvList
[Index
].AcceptAnyResponse
= FALSE
;
929 SrvList
[Index
].Type
= BootSvrEntry
->Type
;
935 SrvList
= Info
->SrvList
;
937 if (!SrvList
[0].AcceptAnyResponse
) {
939 for (Index
= 1; Index
< Info
->IpCnt
; Index
++) {
940 if (SrvList
[Index
].AcceptAnyResponse
) {
945 if (Index
!= Info
->IpCnt
) {
946 Status
= EFI_INVALID_PARAMETER
;
952 if ((!Info
->UseUCast
&& !Info
->UseBCast
&& !Info
->UseMCast
) || (Info
->MustUseList
&& Info
->IpCnt
== 0)) {
954 Status
= EFI_INVALID_PARAMETER
;
958 // Execute discover by UniCast/BroadCast/MultiCast
960 if (Info
->UseUCast
) {
962 for (Index
= 0; Index
< Info
->IpCnt
; Index
++) {
964 if (BootSvrEntry
== NULL
) {
965 Private
->ServerIp
.Addr
[0] = SrvList
[Index
].IpAddr
.Addr
[0];
969 &BootSvrEntry
->IpAddr
[Index
],
970 sizeof (EFI_IPv4_ADDRESS
)
974 Status
= PxeBcDiscvBootService (
979 &SrvList
[Index
].IpAddr
,
983 &Private
->PxeReply
.Packet
.Ack
985 if (!EFI_ERROR (Status
)) {
990 } else if (Info
->UseMCast
) {
992 Status
= PxeBcDiscvBootService (
997 &Info
->ServerMCastIp
,
1001 &Private
->PxeReply
.Packet
.Ack
1004 } else if (Info
->UseBCast
) {
1006 Status
= PxeBcDiscvBootService (
1015 &Private
->PxeReply
.Packet
.Ack
1019 if (EFI_ERROR (Status
) || !Mode
->PxeReplyReceived
|| (!Mode
->PxeBisReplyReceived
&& UseBis
)) {
1020 if (Status
== EFI_ICMP_ERROR
) {
1021 Mode
->IcmpErrorReceived
= TRUE
;
1023 Status
= EFI_DEVICE_ERROR
;
1027 PxeBcParseCachedDhcpPacket (&Private
->PxeReply
);
1030 if (Mode
->PxeBisReplyReceived
) {
1033 &Mode
->PxeReply
.Dhcpv4
.BootpSiAddr
,
1034 sizeof (EFI_IPv4_ADDRESS
)
1038 if (CreatedInfo
!= NULL
) {
1039 FreePool (CreatedInfo
);
1044 Private
->Udp4Read
->Configure (Private
->Udp4Read
, &Private
->Udp4CfgData
);
1047 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
1048 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
1050 ZeroMem(&IpFilter
, sizeof (EFI_PXE_BASE_CODE_IP_FILTER
));
1051 IpFilter
.Filters
= EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
;
1052 This
->SetIpFilter (This
, &IpFilter
);
1059 Used to perform TFTP and MTFTP services.
1061 This function is used to perform TFTP and MTFTP services. This includes the
1062 TFTP operations to get the size of a file, read a directory, read a file, and
1063 write a file. It also includes the MTFTP operations to get the size of a file,
1064 read a directory, and read a file. The type of operation is specified by Operation.
1065 If the callback function that is invoked during the TFTP/MTFTP operation does
1066 not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will
1068 For read operations, the return data will be placed in the buffer specified by
1069 BufferPtr. If BufferSize is too small to contain the entire downloaded file,
1070 then EFI_BUFFER_TOO_SMALL will be returned and BufferSize will be set to zero
1071 or the size of the requested file (the size of the requested file is only returned
1072 if the TFTP server supports TFTP options). If BufferSize is large enough for the
1073 read operation, then BufferSize will be set to the size of the downloaded file,
1074 and EFI_SUCCESS will be returned. Applications using the PxeBc.Mtftp() services
1075 should use the get-file-size operations to determine the size of the downloaded
1076 file prior to using the read-file operations-especially when downloading large
1077 (greater than 64 MB) files-instead of making two calls to the read-file operation.
1078 Following this recommendation will save time if the file is larger than expected
1079 and the TFTP server does not support TFTP option extensions. Without TFTP option
1080 extension support, the client has to download the entire file, counting and discarding
1081 the received packets, to determine the file size.
1082 For write operations, the data to be sent is in the buffer specified by BufferPtr.
1083 BufferSize specifies the number of bytes to send. If the write operation completes
1084 successfully, then EFI_SUCCESS will be returned.
1085 For TFTP "get file size" operations, the size of the requested file or directory
1086 is returned in BufferSize, and EFI_SUCCESS will be returned. If the TFTP server
1087 does not support options, the file will be downloaded into a bit bucket and the
1088 length of the downloaded file will be returned. For MTFTP "get file size" operations,
1089 if the MTFTP server does not support the "get file size" option, EFI_UNSUPPORTED
1091 This function can take up to 10 seconds to timeout and return control to the caller.
1092 If the TFTP sequence does not complete, EFI_TIMEOUT will be returned.
1093 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
1094 then the TFTP sequence is stopped and EFI_ABORTED will be returned.
1095 The format of the data returned from a TFTP read directory operation is a null-terminated
1096 filename followed by a null-terminated information string, of the form
1097 "size year-month-day hour:minute:second" (i.e. %d %d-%d-%d %d:%d:%f - note that
1098 the seconds field can be a decimal number), where the date and time are UTC. For
1099 an MTFTP read directory command, there is additionally a null-terminated multicast
1100 IP address preceding the filename of the form %d.%d.%d.%d for IP v4. The final
1101 entry is itself null-terminated, so that the final information string is terminated
1102 with two null octets.
1104 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1105 @param Operation The type of operation to perform.
1106 @param BufferPtr A pointer to the data buffer.
1107 @param Overwrite Only used on write file operations. TRUE if a file on a remote server can
1109 @param BufferSize For get-file-size operations, *BufferSize returns the size of the
1111 @param BlockSize The requested block size to be used during a TFTP transfer.
1112 @param ServerIp The TFTP / MTFTP server IP address.
1113 @param Filename A Null-terminated ASCII string that specifies a directory name or a file
1115 @param Info Pointer to the MTFTP information.
1116 @param DontUseBuffer Set to FALSE for normal TFTP and MTFTP read file operation.
1118 @retval EFI_SUCCESS The TFTP/MTFTP operation was completed.
1119 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1120 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1121 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1122 @retval EFI_BUFFER_TOO_SMALL The buffer is not large enough to complete the read operation.
1123 @retval EFI_ABORTED The callback function aborted the TFTP/MTFTP operation.
1124 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.
1125 @retval EFI_ICMP_ERROR An ICMP error packet was received during the MTFTP session.
1126 @retval EFI_TFTP_ERROR A TFTP error packet was received during the MTFTP session.
1132 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1133 IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation
,
1134 IN OUT VOID
*BufferPtr
,
1135 IN BOOLEAN Overwrite
,
1136 IN OUT UINT64
*BufferSize
,
1137 IN UINTN
*BlockSize OPTIONAL
,
1138 IN EFI_IP_ADDRESS
*ServerIp
,
1140 IN EFI_PXE_BASE_CODE_MTFTP_INFO
*Info OPTIONAL
,
1141 IN BOOLEAN DontUseBuffer
1144 PXEBC_PRIVATE_DATA
*Private
;
1145 EFI_MTFTP4_CONFIG_DATA Mtftp4Config
;
1147 EFI_PXE_BASE_CODE_MODE
*Mode
;
1148 EFI_MAC_ADDRESS TempMacAddr
;
1149 EFI_PXE_BASE_CODE_IP_FILTER IpFilter
;
1151 if ((This
== NULL
) ||
1152 (Filename
== NULL
) ||
1153 (BufferSize
== NULL
) ||
1154 ((ServerIp
== NULL
) || !NetIp4IsUnicast (NTOHL (ServerIp
->Addr
[0]), 0)) ||
1155 ((BufferPtr
== NULL
) && DontUseBuffer
) ||
1156 ((BlockSize
!= NULL
) && (*BlockSize
< 512))) {
1158 return EFI_INVALID_PARAMETER
;
1161 Status
= EFI_DEVICE_ERROR
;
1162 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1163 Mode
= &Private
->Mode
;
1165 if (!Mode
->AutoArp
) {
1167 // If AutoArp is set false, check arp cache
1169 UpdateArpCache (This
);
1170 if (!FindInArpCache (Mode
, &ServerIp
->v4
, &TempMacAddr
)) {
1171 return EFI_DEVICE_ERROR
;
1176 // Stop Udp4Read instance
1178 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
1180 Mode
->TftpErrorReceived
= FALSE
;
1181 Mode
->IcmpErrorReceived
= FALSE
;
1183 Mtftp4Config
.UseDefaultSetting
= FALSE
;
1184 Mtftp4Config
.TimeoutValue
= PXEBC_MTFTP_TIMEOUT
;
1185 Mtftp4Config
.TryCount
= PXEBC_MTFTP_RETRIES
;
1188 &Mtftp4Config
.StationIp
,
1189 &Private
->StationIp
,
1190 sizeof (EFI_IPv4_ADDRESS
)
1193 &Mtftp4Config
.SubnetMask
,
1194 &Private
->SubnetMask
,
1195 sizeof (EFI_IPv4_ADDRESS
)
1198 &Mtftp4Config
.GatewayIp
,
1199 &Private
->GatewayIp
,
1200 sizeof (EFI_IPv4_ADDRESS
)
1203 &Mtftp4Config
.ServerIp
,
1205 sizeof (EFI_IPv4_ADDRESS
)
1208 switch (Operation
) {
1210 case EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE
:
1212 Status
= PxeBcTftpGetFileSize (
1222 case EFI_PXE_BASE_CODE_TFTP_READ_FILE
:
1224 Status
= PxeBcTftpReadFile (
1236 case EFI_PXE_BASE_CODE_TFTP_WRITE_FILE
:
1238 Status
= PxeBcTftpWriteFile (
1250 case EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY
:
1252 Status
= PxeBcTftpReadDirectory (
1264 case EFI_PXE_BASE_CODE_MTFTP_GET_FILE_SIZE
:
1265 case EFI_PXE_BASE_CODE_MTFTP_READ_FILE
:
1266 case EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY
:
1267 Status
= EFI_UNSUPPORTED
;
1272 Status
= EFI_INVALID_PARAMETER
;
1276 if (Status
== EFI_ICMP_ERROR
) {
1277 Mode
->IcmpErrorReceived
= TRUE
;
1280 if (EFI_ERROR (Status
)) {
1285 Private
->Udp4Read
->Configure (Private
->Udp4Read
, &Private
->Udp4CfgData
);
1287 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
1288 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
1290 ZeroMem(&IpFilter
, sizeof (EFI_PXE_BASE_CODE_IP_FILTER
));
1291 IpFilter
.Filters
= EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
;
1292 This
->SetIpFilter (This
, &IpFilter
);
1299 Writes a UDP packet to the network interface.
1301 This function writes a UDP packet specified by the (optional HeaderPtr and)
1302 BufferPtr parameters to the network interface. The UDP header is automatically
1303 built by this routine. It uses the parameters OpFlags, DestIp, DestPort, GatewayIp,
1304 SrcIp, and SrcPort to build this header. If the packet is successfully built and
1305 transmitted through the network interface, then EFI_SUCCESS will be returned.
1306 If a timeout occurs during the transmission of the packet, then EFI_TIMEOUT will
1307 be returned. If an ICMP error occurs during the transmission of the packet, then
1308 the IcmpErrorReceived field is set to TRUE, the IcmpError field is filled in and
1309 EFI_ICMP_ERROR will be returned. If the Callback Protocol does not return
1310 EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will be returned.
1312 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1313 @param OpFlags The UDP operation flags.
1314 @param DestIp The destination IP address.
1315 @param DestPort The destination UDP port number.
1316 @param GatewayIp The gateway IP address.
1317 @param SrcIp The source IP address.
1318 @param SrcPort The source UDP port number.
1319 @param HeaderSize An optional field which may be set to the length of a header at
1320 HeaderPtr to be prefixed to the data at BufferPtr.
1321 @param HeaderPtr If HeaderSize is not NULL, a pointer to a header to be prefixed to the
1323 @param BufferSize A pointer to the size of the data at BufferPtr.
1324 @param BufferPtr A pointer to the data to be written.
1326 @retval EFI_SUCCESS The UDP Write operation was completed.
1327 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1328 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1329 @retval EFI_BAD_BUFFER_SIZE The buffer is too long to be transmitted.
1330 @retval EFI_ABORTED The callback function aborted the UDP Write operation.
1331 @retval EFI_TIMEOUT The UDP Write operation timed out.
1332 @retval EFI_ICMP_ERROR An ICMP error packet was received during the UDP write session.
1338 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1340 IN EFI_IP_ADDRESS
*DestIp
,
1341 IN EFI_PXE_BASE_CODE_UDP_PORT
*DestPort
,
1342 IN EFI_IP_ADDRESS
*GatewayIp OPTIONAL
,
1343 IN EFI_IP_ADDRESS
*SrcIp OPTIONAL
,
1344 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPort OPTIONAL
,
1345 IN UINTN
*HeaderSize OPTIONAL
,
1346 IN VOID
*HeaderPtr OPTIONAL
,
1347 IN UINTN
*BufferSize
,
1351 PXEBC_PRIVATE_DATA
*Private
;
1352 EFI_UDP4_PROTOCOL
*Udp4
;
1353 EFI_UDP4_COMPLETION_TOKEN Token
;
1354 EFI_UDP4_TRANSMIT_DATA
*Udp4TxData
;
1357 EFI_UDP4_SESSION_DATA Udp4Session
;
1360 EFI_PXE_BASE_CODE_MODE
*Mode
;
1361 EFI_MAC_ADDRESS TempMacAddr
;
1365 if ((This
== NULL
) || (DestIp
== NULL
) || (DestPort
== NULL
)) {
1366 return EFI_INVALID_PARAMETER
;
1369 if ((GatewayIp
!= NULL
) && !NetIp4IsUnicast (NTOHL (GatewayIp
->Addr
[0]), 0)) {
1371 // Gateway is provided but it's not a unicast IP address.
1373 return EFI_INVALID_PARAMETER
;
1376 if ((HeaderSize
!= NULL
) && ((*HeaderSize
== 0) || (HeaderPtr
== NULL
))) {
1378 // The HeaderSize ptr isn't NULL and: 1. the value is zero; or 2. the HeaderPtr
1381 return EFI_INVALID_PARAMETER
;
1384 if ((BufferSize
== NULL
) || ((*BufferSize
!= 0) && (BufferPtr
== NULL
))) {
1385 return EFI_INVALID_PARAMETER
;
1388 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1389 Udp4
= Private
->Udp4Write
;
1390 Mode
= &Private
->Mode
;
1391 if (!Mode
->Started
) {
1392 return EFI_NOT_STARTED
;
1395 if (!Private
->AddressIsOk
&& (SrcIp
== NULL
)) {
1396 return EFI_INVALID_PARAMETER
;
1399 if (!Mode
->AutoArp
) {
1401 // If AutoArp is set false, check arp cache
1403 UpdateArpCache (This
);
1404 if (!FindInArpCache (Mode
, &DestIp
->v4
, &TempMacAddr
)) {
1405 return EFI_DEVICE_ERROR
;
1409 Mode
->IcmpErrorReceived
= FALSE
;
1411 if ((Private
->CurrentUdpSrcPort
== 0) ||
1412 ((SrcPort
!= NULL
) && (*SrcPort
!= Private
->CurrentUdpSrcPort
))) {
1414 // Port is changed, (re)configure the Udp4Write instance
1416 if (SrcPort
!= NULL
) {
1417 Private
->CurrentUdpSrcPort
= *SrcPort
;
1421 Status
= PxeBcConfigureUdpWriteInstance (
1423 &Private
->StationIp
.v4
,
1424 &Private
->SubnetMask
.v4
,
1425 &Private
->GatewayIp
.v4
,
1426 &Private
->CurrentUdpSrcPort
1428 if (EFI_ERROR (Status
)) {
1429 Private
->CurrentUdpSrcPort
= 0;
1430 return EFI_INVALID_PARAMETER
;
1433 ZeroMem (&Token
, sizeof (EFI_UDP4_COMPLETION_TOKEN
));
1434 ZeroMem (&Udp4Session
, sizeof (EFI_UDP4_SESSION_DATA
));
1436 CopyMem (&Udp4Session
.DestinationAddress
, DestIp
, sizeof (EFI_IPv4_ADDRESS
));
1437 Udp4Session
.DestinationPort
= *DestPort
;
1438 if (SrcIp
!= NULL
) {
1439 CopyMem (&Udp4Session
.SourceAddress
, SrcIp
, sizeof (EFI_IPv4_ADDRESS
));
1441 if (SrcPort
!= NULL
) {
1442 Udp4Session
.SourcePort
= *SrcPort
;
1445 FragCount
= (HeaderSize
!= NULL
) ? 2 : 1;
1446 Udp4TxData
= (EFI_UDP4_TRANSMIT_DATA
*) AllocateZeroPool (sizeof (EFI_UDP4_TRANSMIT_DATA
) + (FragCount
- 1) * sizeof (EFI_UDP4_FRAGMENT_DATA
));
1447 if (Udp4TxData
== NULL
) {
1448 return EFI_OUT_OF_RESOURCES
;
1451 Udp4TxData
->FragmentCount
= FragCount
;
1452 Udp4TxData
->FragmentTable
[FragCount
- 1].FragmentLength
= (UINT32
) *BufferSize
;
1453 Udp4TxData
->FragmentTable
[FragCount
- 1].FragmentBuffer
= BufferPtr
;
1454 DataLength
= (UINT32
) *BufferSize
;
1456 if (FragCount
== 2) {
1458 Udp4TxData
->FragmentTable
[0].FragmentLength
= (UINT32
) *HeaderSize
;
1459 Udp4TxData
->FragmentTable
[0].FragmentBuffer
= HeaderPtr
;
1460 DataLength
+= (UINT32
) *HeaderSize
;
1463 if (GatewayIp
!= NULL
) {
1464 Udp4TxData
->GatewayAddress
= (EFI_IPv4_ADDRESS
*) GatewayIp
;
1466 Udp4TxData
->UdpSessionData
= &Udp4Session
;
1467 Udp4TxData
->DataLength
= DataLength
;
1468 Token
.Packet
.TxData
= Udp4TxData
;
1470 Status
= gBS
->CreateEvent (
1477 if (EFI_ERROR (Status
)) {
1481 Status
= Udp4
->Transmit (Udp4
, &Token
);
1482 if (EFI_ERROR (Status
)) {
1483 if (Status
== EFI_ICMP_ERROR
) {
1484 Mode
->IcmpErrorReceived
= TRUE
;
1494 Status
= Token
.Status
;
1498 if (Token
.Event
!= NULL
) {
1499 gBS
->CloseEvent (Token
.Event
);
1502 FreePool (Udp4TxData
);
1505 // Reset the instance.
1507 Udp4
->Configure (Udp4
, NULL
);
1512 Decide whether the incoming UDP packet is acceptable per IP filter settings
1513 in provided PxeBcMode.
1515 @param PxeBcMode Pointer to EFI_PXE_BASE_CODE_MODE.
1516 @param Session Received UDP session.
1518 @retval TRUE The UDP package matches IP filters.
1519 @retval FALSE The UDP package doesn't matches IP filters.
1524 IN EFI_PXE_BASE_CODE_MODE
*PxeBcMode
,
1525 IN EFI_UDP4_SESSION_DATA
*Session
1529 EFI_IPv4_ADDRESS Ip4Address
;
1530 EFI_IPv4_ADDRESS DestIp4Address
;
1532 if ((PxeBcMode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS
) != 0) {
1536 CopyMem (&DestIp4Address
, &Session
->DestinationAddress
, sizeof (DestIp4Address
));
1537 if (((PxeBcMode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST
) != 0) &&
1538 IP4_IS_MULTICAST (EFI_NTOHL (DestIp4Address
))
1543 if (((PxeBcMode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST
) != 0) &&
1544 IP4_IS_LOCAL_BROADCAST (EFI_NTOHL (DestIp4Address
))
1549 CopyMem (&Ip4Address
, &PxeBcMode
->StationIp
.v4
, sizeof (Ip4Address
));
1550 if (((PxeBcMode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
) != 0) &&
1551 EFI_IP4_EQUAL (&Ip4Address
, &DestIp4Address
)
1556 ASSERT (PxeBcMode
->IpFilter
.IpCnt
< EFI_PXE_BASE_CODE_MAX_IPCNT
);
1558 for (Index
= 0; Index
< PxeBcMode
->IpFilter
.IpCnt
; Index
++) {
1561 &PxeBcMode
->IpFilter
.IpList
[Index
].v4
,
1564 if (EFI_IP4_EQUAL (&Ip4Address
, &DestIp4Address
)) {
1573 Reads a UDP packet from the network interface.
1575 This function reads a UDP packet from a network interface. The data contents
1576 are returned in (the optional HeaderPtr and) BufferPtr, and the size of the
1577 buffer received is returned in BufferSize . If the input BufferSize is smaller
1578 than the UDP packet received (less optional HeaderSize), it will be set to the
1579 required size, and EFI_BUFFER_TOO_SMALL will be returned. In this case, the
1580 contents of BufferPtr are undefined, and the packet is lost. If a UDP packet is
1581 successfully received, then EFI_SUCCESS will be returned, and the information
1582 from the UDP header will be returned in DestIp, DestPort, SrcIp, and SrcPort if
1583 they are not NULL. Depending on the values of OpFlags and the DestIp, DestPort,
1584 SrcIp, and SrcPort input values, different types of UDP packet receive filtering
1585 will be performed. The following tables summarize these receive filter operations.
1587 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1588 @param OpFlags The UDP operation flags.
1589 @param DestIp The destination IP address.
1590 @param DestPort The destination UDP port number.
1591 @param SrcIp The source IP address.
1592 @param SrcPort The source UDP port number.
1593 @param HeaderSize An optional field which may be set to the length of a header at
1594 HeaderPtr to be prefixed to the data at BufferPtr.
1595 @param HeaderPtr If HeaderSize is not NULL, a pointer to a header to be prefixed to the
1597 @param BufferSize A pointer to the size of the data at BufferPtr.
1598 @param BufferPtr A pointer to the data to be read.
1600 @retval EFI_SUCCESS The UDP Read operation was completed.
1601 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1602 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1603 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1604 @retval EFI_BUFFER_TOO_SMALL The packet is larger than Buffer can hold.
1605 @retval EFI_ABORTED The callback function aborted the UDP Read operation.
1606 @retval EFI_TIMEOUT The UDP Read operation timed out.
1612 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1614 IN OUT EFI_IP_ADDRESS
*DestIp OPTIONAL
,
1615 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*DestPort OPTIONAL
,
1616 IN OUT EFI_IP_ADDRESS
*SrcIp OPTIONAL
,
1617 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPort OPTIONAL
,
1618 IN UINTN
*HeaderSize OPTIONAL
,
1619 IN VOID
*HeaderPtr OPTIONAL
,
1620 IN OUT UINTN
*BufferSize
,
1624 PXEBC_PRIVATE_DATA
*Private
;
1625 EFI_PXE_BASE_CODE_MODE
*Mode
;
1626 EFI_UDP4_PROTOCOL
*Udp4
;
1627 EFI_UDP4_COMPLETION_TOKEN Token
;
1628 EFI_UDP4_RECEIVE_DATA
*RxData
;
1629 EFI_UDP4_SESSION_DATA
*Session
;
1635 UINTN HeaderCopiedLen
;
1636 UINTN BufferCopiedLen
;
1637 UINT32 FragmentLength
;
1638 UINTN FragmentIndex
;
1639 UINT8
*FragmentBuffer
;
1641 if (This
== NULL
|| DestIp
== NULL
|| DestPort
== NULL
) {
1642 return EFI_INVALID_PARAMETER
;
1645 if (((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT
) == 0 && (DestPort
== NULL
)) ||
1646 ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT
) == 0 && (SrcIp
== NULL
)) ||
1647 ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT
) == 0 && (SrcPort
== NULL
))) {
1648 return EFI_INVALID_PARAMETER
;
1651 if (((HeaderSize
!= NULL
) && (*HeaderSize
== 0)) || ((HeaderSize
!= NULL
) && (HeaderPtr
== NULL
))) {
1652 return EFI_INVALID_PARAMETER
;
1655 if ((BufferSize
== NULL
) || (BufferPtr
== NULL
)) {
1656 return EFI_INVALID_PARAMETER
;
1659 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1660 Mode
= Private
->PxeBc
.Mode
;
1661 Udp4
= Private
->Udp4Read
;
1663 if (!Mode
->Started
) {
1664 return EFI_NOT_STARTED
;
1667 Mode
->IcmpErrorReceived
= FALSE
;
1669 Status
= gBS
->CreateEvent (
1676 if (EFI_ERROR (Status
)) {
1677 return EFI_OUT_OF_RESOURCES
;
1683 Status
= Udp4
->Receive (Udp4
, &Token
);
1684 if (EFI_ERROR (Status
)) {
1685 if (Status
== EFI_ICMP_ERROR
) {
1686 Mode
->IcmpErrorReceived
= TRUE
;
1694 Status
= EFI_TIMEOUT
;
1698 // check whether this packet matches the filters
1700 if (EFI_ERROR (Token
.Status
)){
1704 RxData
= Token
.Packet
.RxData
;
1705 Session
= &RxData
->UdpSession
;
1709 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER
) != 0) {
1712 // Check UDP package by IP filter settings
1714 if (CheckIpByFilter (Mode
, Session
)) {
1723 // Match the destination ip of the received udp dgram
1725 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP
) != 0) {
1728 if (DestIp
!= NULL
) {
1729 CopyMem (DestIp
, &Session
->DestinationAddress
, sizeof (EFI_IPv4_ADDRESS
));
1732 if (DestIp
!= NULL
) {
1733 if (EFI_IP4_EQUAL (DestIp
, &Session
->DestinationAddress
)) {
1737 if (EFI_IP4_EQUAL (&Private
->StationIp
, &Session
->DestinationAddress
)) {
1746 // Match the destination port of the received udp dgram
1748 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT
) != 0) {
1750 if (DestPort
!= NULL
) {
1751 *DestPort
= Session
->DestinationPort
;
1755 if (*DestPort
!= Session
->DestinationPort
) {
1763 // Match the source ip of the received udp dgram
1765 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP
) != 0) {
1767 if (SrcIp
!= NULL
) {
1768 CopyMem (SrcIp
, &Session
->SourceAddress
, sizeof (EFI_IPv4_ADDRESS
));
1772 if (!EFI_IP4_EQUAL (SrcIp
, &Session
->SourceAddress
)) {
1780 // Match the source port of the received udp dgram
1782 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT
) != 0) {
1784 if (SrcPort
!= NULL
) {
1785 *SrcPort
= Session
->SourcePort
;
1789 if (*SrcPort
!= Session
->SourcePort
) {
1796 ASSERT (RxData
!= NULL
);
1799 if (HeaderSize
!= NULL
) {
1800 HeaderLen
= MIN (*HeaderSize
, RxData
->DataLength
);
1803 if (RxData
->DataLength
- HeaderLen
> *BufferSize
) {
1804 Status
= EFI_BUFFER_TOO_SMALL
;
1806 *HeaderSize
= HeaderLen
;
1807 *BufferSize
= RxData
->DataLength
- HeaderLen
;
1809 HeaderCopiedLen
= 0;
1810 BufferCopiedLen
= 0;
1811 for (FragmentIndex
= 0; FragmentIndex
< RxData
->FragmentCount
; FragmentIndex
++) {
1812 FragmentLength
= RxData
->FragmentTable
[FragmentIndex
].FragmentLength
;
1813 FragmentBuffer
= RxData
->FragmentTable
[FragmentIndex
].FragmentBuffer
;
1814 if (HeaderCopiedLen
+ FragmentLength
< HeaderLen
) {
1816 // Copy the header part of received data.
1818 CopyMem ((UINT8
*) HeaderPtr
+ HeaderCopiedLen
, FragmentBuffer
, FragmentLength
);
1819 HeaderCopiedLen
+= FragmentLength
;
1820 } else if (HeaderCopiedLen
< HeaderLen
) {
1822 // Copy the header part of received data.
1824 CopiedLen
= HeaderLen
- HeaderCopiedLen
;
1825 CopyMem ((UINT8
*) HeaderPtr
+ HeaderCopiedLen
, FragmentBuffer
, CopiedLen
);
1826 HeaderCopiedLen
+= CopiedLen
;
1829 // Copy the other part of received data.
1831 CopyMem ((UINT8
*) BufferPtr
+ BufferCopiedLen
, FragmentBuffer
+ CopiedLen
, FragmentLength
- CopiedLen
);
1832 BufferCopiedLen
+= (FragmentLength
- CopiedLen
);
1835 // Copy the other part of received data.
1837 CopyMem ((UINT8
*) BufferPtr
+ BufferCopiedLen
, FragmentBuffer
, FragmentLength
);
1838 BufferCopiedLen
+= FragmentLength
;
1844 Status
= EFI_TIMEOUT
;
1848 // Recycle the RxData
1850 gBS
->SignalEvent (RxData
->RecycleSignal
);
1859 Udp4
->Cancel (Udp4
, &Token
);
1861 gBS
->CloseEvent (Token
.Event
);
1867 Updates the IP receive filters of a network device and enables software filtering.
1869 The NewFilter field is used to modify the network device's current IP receive
1870 filter settings and to enable a software filter. This function updates the IpFilter
1871 field of the EFI_PXE_BASE_CODE_MODE structure with the contents of NewIpFilter.
1872 The software filter is used when the USE_FILTER in OpFlags is set to UdpRead().
1873 The current hardware filter remains in effect no matter what the settings of OpFlags
1874 are, so that the meaning of ANY_DEST_IP set in OpFlags to UdpRead() is from those
1875 packets whose reception is enabled in hardware-physical NIC address (unicast),
1876 broadcast address, logical address or addresses (multicast), or all (promiscuous).
1877 UdpRead() does not modify the IP filter settings.
1878 Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP receive
1879 filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
1880 If an application or driver wishes to preserve the IP receive filter settings,
1881 it will have to preserve the IP receive filter settings before these calls, and
1882 use SetIpFilter() to restore them after the calls. If incompatible filtering is
1883 requested (for example, PROMISCUOUS with anything else) or if the device does not
1884 support a requested filter setting and it cannot be accommodated in software
1885 (for example, PROMISCUOUS not supported), EFI_INVALID_PARAMETER will be returned.
1886 The IPlist field is used to enable IPs other than the StationIP. They may be
1887 multicast or unicast. If IPcnt is set as well as EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP,
1888 then both the StationIP and the IPs from the IPlist will be used.
1890 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1891 @param NewFilter Pointer to the new set of IP receive filters.
1893 @retval EFI_SUCCESS The IP receive filter settings were updated.
1894 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1895 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1900 EfiPxeBcSetIpFilter (
1901 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1902 IN EFI_PXE_BASE_CODE_IP_FILTER
*NewFilter
1906 PXEBC_PRIVATE_DATA
*Private
;
1907 EFI_PXE_BASE_CODE_MODE
*Mode
;
1909 EFI_UDP4_CONFIG_DATA
*Udp4Cfg
;
1910 BOOLEAN PromiscuousNeed
;
1911 BOOLEAN AcceptPromiscuous
;
1912 BOOLEAN AcceptBroadcast
;
1913 BOOLEAN MultiCastUpdate
;
1916 DEBUG ((EFI_D_ERROR
, "This == NULL.\n"));
1917 return EFI_INVALID_PARAMETER
;
1920 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1921 Mode
= Private
->PxeBc
.Mode
;
1923 if (NewFilter
== NULL
) {
1924 DEBUG ((EFI_D_ERROR
, "NewFilter == NULL.\n"));
1925 return EFI_INVALID_PARAMETER
;
1928 if (NewFilter
->IpCnt
> EFI_PXE_BASE_CODE_MAX_IPCNT
) {
1929 DEBUG ((EFI_D_ERROR
, "NewFilter->IpCnt > %d.\n", EFI_PXE_BASE_CODE_MAX_IPCNT
));
1930 return EFI_INVALID_PARAMETER
;
1933 if (!Mode
->Started
) {
1934 DEBUG ((EFI_D_ERROR
, "BC was not started.\n"));
1935 return EFI_NOT_STARTED
;
1938 if (Mode
->UsingIpv6
) {
1939 DEBUG ((EFI_D_ERROR
, "This driver is PXE for IPv4 Only.\n"));
1940 return EFI_INVALID_PARAMETER
;
1943 PromiscuousNeed
= FALSE
;
1945 for (Index
= 0; Index
< NewFilter
->IpCnt
; ++Index
) {
1946 if (IP4_IS_LOCAL_BROADCAST (EFI_IP4 (NewFilter
->IpList
[Index
].v4
))) {
1948 // The IP is a broadcast address.
1950 DEBUG ((EFI_D_ERROR
, "There is broadcast address in NewFilter.\n"));
1951 return EFI_INVALID_PARAMETER
;
1953 if (NetIp4IsUnicast (EFI_IP4 (NewFilter
->IpList
[Index
].v4
), 0) &&
1954 ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
) != 0)
1957 // If EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP is set and IP4 address is in IpList,
1958 // promiscuous mode is needed.
1960 PromiscuousNeed
= TRUE
;
1964 AcceptPromiscuous
= FALSE
;
1965 AcceptBroadcast
= FALSE
;
1966 MultiCastUpdate
= FALSE
;
1968 if (PromiscuousNeed
||
1969 ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS
) != 0) ||
1970 ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST
) != 0)
1973 // Configure the udp4 filter to receive all packages.
1975 AcceptPromiscuous
= TRUE
;
1976 } else if ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST
) != 0) {
1978 // Configure the udp4 filter to receive all broadcast packages.
1980 AcceptBroadcast
= TRUE
;
1984 // In multicast condition when Promiscuous FALSE and IpCnt no-zero.
1985 // Here check if there is any update of the multicast ip address. If yes,
1986 // we need leave the old multicast group (by Config UDP instance to NULL),
1987 // and join the new multicast group.
1989 if (!AcceptPromiscuous
) {
1990 if ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
) != 0) {
1991 if (Mode
->IpFilter
.IpCnt
!= NewFilter
->IpCnt
) {
1992 MultiCastUpdate
= TRUE
;
1993 } else if (CompareMem (Mode
->IpFilter
.IpList
, NewFilter
->IpList
, NewFilter
->IpCnt
* sizeof (EFI_IP_ADDRESS
)) != 0 ) {
1994 MultiCastUpdate
= TRUE
;
2000 // Check whether we need reconfigure the UDP instance.
2002 Udp4Cfg
= &Private
->Udp4CfgData
;
2003 if ((AcceptPromiscuous
!= Udp4Cfg
->AcceptPromiscuous
) ||
2004 (AcceptBroadcast
!= Udp4Cfg
->AcceptBroadcast
) || MultiCastUpdate
) {
2006 // Clear the UDP instance configuration, all joined groups will be left
2007 // during the operation.
2009 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
2012 // Configure the UDP instance with the new configuration.
2014 Udp4Cfg
->AcceptPromiscuous
= AcceptPromiscuous
;
2015 Udp4Cfg
->AcceptBroadcast
= AcceptBroadcast
;
2016 Status
= Private
->Udp4Read
->Configure (Private
->Udp4Read
, Udp4Cfg
);
2017 if (EFI_ERROR (Status
)) {
2022 // In not Promiscuous mode, need to join the new multicast group.
2024 if (!AcceptPromiscuous
) {
2025 for (Index
= 0; Index
< NewFilter
->IpCnt
; ++Index
) {
2026 if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter
->IpList
[Index
].v4
))) {
2028 // Join the mutilcast group.
2030 Status
= Private
->Udp4Read
->Groups (Private
->Udp4Read
, TRUE
, &NewFilter
->IpList
[Index
].v4
);
2031 if (EFI_ERROR (Status
)) {
2041 // Save the new filter.
2043 CopyMem (&Mode
->IpFilter
, NewFilter
, sizeof (Mode
->IpFilter
));
2050 Uses the ARP protocol to resolve a MAC address.
2052 This function uses the ARP protocol to resolve a MAC address. The UsingIpv6 field
2053 of the EFI_PXE_BASE_CODE_MODE structure is used to determine if IPv4 or IPv6
2054 addresses are being used. The IP address specified by IpAddr is used to resolve
2055 a MAC address. If the ARP protocol succeeds in resolving the specified address,
2056 then the ArpCacheEntries and ArpCache fields of the EFI_PXE_BASE_CODE_MODE structure
2057 are updated, and EFI_SUCCESS is returned. If MacAddr is not NULL, the resolved
2058 MAC address is placed there as well. If the PXE Base Code protocol is in the
2059 stopped state, then EFI_NOT_STARTED is returned. If the ARP protocol encounters
2060 a timeout condition while attempting to resolve an address, then EFI_TIMEOUT is
2061 returned. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
2062 then EFI_ABORTED is returned.
2064 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2065 @param IpAddr Pointer to the IP address that is used to resolve a MAC address.
2066 @param MacAddr If not NULL, a pointer to the MAC address that was resolved with the
2069 @retval EFI_SUCCESS The IP or MAC address was resolved.
2070 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2071 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
2072 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
2073 @retval EFI_ICMP_ERROR Something error occur with the ICMP packet message.
2079 IN EFI_PXE_BASE_CODE_PROTOCOL
* This
,
2080 IN EFI_IP_ADDRESS
* IpAddr
,
2081 IN EFI_MAC_ADDRESS
* MacAddr OPTIONAL
2084 PXEBC_PRIVATE_DATA
*Private
;
2085 EFI_PXE_BASE_CODE_MODE
*Mode
;
2087 EFI_MAC_ADDRESS TempMacAddr
;
2089 if (This
== NULL
|| IpAddr
== NULL
) {
2090 return EFI_INVALID_PARAMETER
;
2093 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
2094 Mode
= Private
->PxeBc
.Mode
;
2096 if (!Mode
->Started
) {
2097 return EFI_NOT_STARTED
;
2100 if (!Private
->AddressIsOk
|| Mode
->UsingIpv6
) {
2102 // We can't resolve the IP address if we don't have a local address now.
2103 // Don't have ARP for IPv6.
2105 return EFI_INVALID_PARAMETER
;
2108 Mode
->IcmpErrorReceived
= FALSE
;
2110 if (!Mode
->AutoArp
) {
2112 // If AutoArp is set false, check arp cache
2114 UpdateArpCache (This
);
2115 if (!FindInArpCache (Mode
, &IpAddr
->v4
, &TempMacAddr
)) {
2116 return EFI_DEVICE_ERROR
;
2119 Status
= Private
->Arp
->Request (Private
->Arp
, &IpAddr
->v4
, NULL
, &TempMacAddr
);
2120 if (EFI_ERROR (Status
)) {
2121 if (Status
== EFI_ICMP_ERROR
) {
2122 Mode
->IcmpErrorReceived
= TRUE
;
2128 if (MacAddr
!= NULL
) {
2129 CopyMem (MacAddr
, &TempMacAddr
, sizeof (EFI_MAC_ADDRESS
));
2136 Updates the parameters that affect the operation of the PXE Base Code Protocol.
2138 This function sets parameters that affect the operation of the PXE Base Code Protocol.
2139 The parameter specified by NewAutoArp is used to control the generation of ARP
2140 protocol packets. If NewAutoArp is TRUE, then ARP Protocol packets will be generated
2141 as required by the PXE Base Code Protocol. If NewAutoArp is FALSE, then no ARP
2142 Protocol packets will be generated. In this case, the only mappings that are
2143 available are those stored in the ArpCache of the EFI_PXE_BASE_CODE_MODE structure.
2144 If there are not enough mappings in the ArpCache to perform a PXE Base Code Protocol
2145 service, then the service will fail. This function updates the AutoArp field of
2146 the EFI_PXE_BASE_CODE_MODE structure to NewAutoArp.
2147 The SetParameters() call must be invoked after a Callback Protocol is installed
2148 to enable the use of callbacks.
2150 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2151 @param NewAutoArp If not NULL, a pointer to a value that specifies whether to replace the
2152 current value of AutoARP.
2153 @param NewSendGUID If not NULL, a pointer to a value that specifies whether to replace the
2154 current value of SendGUID.
2155 @param NewTTL If not NULL, a pointer to be used in place of the current value of TTL,
2156 the "time to live" field of the IP header.
2157 @param NewToS If not NULL, a pointer to be used in place of the current value of ToS,
2158 the "type of service" field of the IP header.
2159 @param NewMakeCallback If not NULL, a pointer to a value that specifies whether to replace the
2160 current value of the MakeCallback field of the Mode structure.
2162 @retval EFI_SUCCESS The new parameters values were updated.
2163 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2164 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
2169 EfiPxeBcSetParameters (
2170 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
2171 IN BOOLEAN
*NewAutoArp OPTIONAL
,
2172 IN BOOLEAN
*NewSendGUID OPTIONAL
,
2173 IN UINT8
*NewTTL OPTIONAL
,
2174 IN UINT8
*NewToS OPTIONAL
,
2175 IN BOOLEAN
*NewMakeCallback
// OPTIONAL
2178 PXEBC_PRIVATE_DATA
*Private
;
2179 EFI_PXE_BASE_CODE_MODE
*Mode
;
2182 Status
= EFI_SUCCESS
;
2185 Status
= EFI_INVALID_PARAMETER
;
2189 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
2190 Mode
= Private
->PxeBc
.Mode
;
2192 if (NewSendGUID
!= NULL
&& *NewSendGUID
) {
2194 // FixMe, cann't locate SendGuid
2198 if (NewMakeCallback
!= NULL
&& *NewMakeCallback
) {
2200 Status
= gBS
->HandleProtocol (
2201 Private
->Controller
,
2202 &gEfiPxeBaseCodeCallbackProtocolGuid
,
2203 (VOID
**) &Private
->PxeBcCallback
2205 if (EFI_ERROR (Status
) || (Private
->PxeBcCallback
->Callback
== NULL
)) {
2207 Status
= EFI_INVALID_PARAMETER
;
2212 if (!Mode
->Started
) {
2213 Status
= EFI_NOT_STARTED
;
2217 if (NewMakeCallback
!= NULL
) {
2219 if (*NewMakeCallback
) {
2221 // Update the Callback protocol.
2223 Status
= gBS
->HandleProtocol (
2224 Private
->Controller
,
2225 &gEfiPxeBaseCodeCallbackProtocolGuid
,
2226 (VOID
**) &Private
->PxeBcCallback
2229 if (EFI_ERROR (Status
) || (Private
->PxeBcCallback
->Callback
== NULL
)) {
2230 Status
= EFI_INVALID_PARAMETER
;
2234 Private
->PxeBcCallback
= NULL
;
2237 Mode
->MakeCallbacks
= *NewMakeCallback
;
2240 if (NewAutoArp
!= NULL
) {
2241 Mode
->AutoArp
= *NewAutoArp
;
2244 if (NewSendGUID
!= NULL
) {
2245 Mode
->SendGUID
= *NewSendGUID
;
2248 if (NewTTL
!= NULL
) {
2249 Mode
->TTL
= *NewTTL
;
2252 if (NewToS
!= NULL
) {
2253 Mode
->ToS
= *NewToS
;
2261 Updates the station IP address and/or subnet mask values of a network device.
2263 This function updates the station IP address and/or subnet mask values of a network
2264 device. The NewStationIp field is used to modify the network device's current IP address.
2265 If NewStationIP is NULL, then the current IP address will not be modified. Otherwise,
2266 this function updates the StationIp field of the EFI_PXE_BASE_CODE_MODE structure
2267 with NewStationIp. The NewSubnetMask field is used to modify the network device's current subnet
2268 mask. If NewSubnetMask is NULL, then the current subnet mask will not be modified.
2269 Otherwise, this function updates the SubnetMask field of the EFI_PXE_BASE_CODE_MODE
2270 structure with NewSubnetMask.
2272 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2273 @param NewStationIp Pointer to the new IP address to be used by the network device.
2274 @param NewSubnetMask Pointer to the new subnet mask to be used by the network device.
2276 @retval EFI_SUCCESS The new station IP address and/or subnet mask were updated.
2277 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2278 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
2283 EfiPxeBcSetStationIP (
2284 IN EFI_PXE_BASE_CODE_PROTOCOL
* This
,
2285 IN EFI_IP_ADDRESS
* NewStationIp OPTIONAL
,
2286 IN EFI_IP_ADDRESS
* NewSubnetMask OPTIONAL
2289 PXEBC_PRIVATE_DATA
*Private
;
2290 EFI_PXE_BASE_CODE_MODE
*Mode
;
2291 EFI_ARP_CONFIG_DATA ArpConfigData
;
2294 return EFI_INVALID_PARAMETER
;
2297 if (NewStationIp
!= NULL
&& !NetIp4IsUnicast (NTOHL (NewStationIp
->Addr
[0]), 0)) {
2298 return EFI_INVALID_PARAMETER
;
2301 if (NewSubnetMask
!= NULL
&& !IP4_IS_VALID_NETMASK (NTOHL (NewSubnetMask
->Addr
[0]))) {
2302 return EFI_INVALID_PARAMETER
;
2305 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
2306 Mode
= Private
->PxeBc
.Mode
;
2308 if (!Mode
->Started
) {
2309 return EFI_NOT_STARTED
;
2312 if (NewStationIp
!= NULL
) {
2313 CopyMem (&Mode
->StationIp
, NewStationIp
, sizeof (EFI_IP_ADDRESS
));
2314 CopyMem (&Private
->StationIp
, NewStationIp
, sizeof (EFI_IP_ADDRESS
));
2317 if (NewSubnetMask
!= NULL
) {
2318 CopyMem (&Mode
->SubnetMask
, NewSubnetMask
, sizeof (EFI_IP_ADDRESS
));
2319 CopyMem (&Private
->SubnetMask
,NewSubnetMask
, sizeof (EFI_IP_ADDRESS
));
2322 Private
->AddressIsOk
= TRUE
;
2324 if (!Mode
->UsingIpv6
) {
2326 // If in IPv4 mode, configure the corresponding ARP with this new
2327 // station IP address.
2329 ZeroMem (&ArpConfigData
, sizeof (EFI_ARP_CONFIG_DATA
));
2331 ArpConfigData
.SwAddressType
= 0x0800;
2332 ArpConfigData
.SwAddressLength
= (UINT8
) sizeof (EFI_IPv4_ADDRESS
);
2333 ArpConfigData
.StationAddress
= &Private
->StationIp
.v4
;
2335 Private
->Arp
->Configure (Private
->Arp
, NULL
);
2336 Private
->Arp
->Configure (Private
->Arp
, &ArpConfigData
);
2339 // Update the route table.
2341 Mode
->RouteTableEntries
= 1;
2342 Mode
->RouteTable
[0].IpAddr
.Addr
[0] = Private
->StationIp
.Addr
[0] & Private
->SubnetMask
.Addr
[0];
2343 Mode
->RouteTable
[0].SubnetMask
.Addr
[0] = Private
->SubnetMask
.Addr
[0];
2344 Mode
->RouteTable
[0].GwAddr
.Addr
[0] = 0;
2351 Updates the contents of the cached DHCP and Discover packets.
2353 The pointers to the new packets are used to update the contents of the cached
2354 packets in the EFI_PXE_BASE_CODE_MODE structure.
2356 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2357 @param NewDhcpDiscoverValid Pointer to a value that will replace the current
2358 DhcpDiscoverValid field.
2359 @param NewDhcpAckReceived Pointer to a value that will replace the current
2360 DhcpAckReceived field.
2361 @param NewProxyOfferReceived Pointer to a value that will replace the current
2362 ProxyOfferReceived field.
2363 @param NewPxeDiscoverValid Pointer to a value that will replace the current
2364 ProxyOfferReceived field.
2365 @param NewPxeReplyReceived Pointer to a value that will replace the current
2366 PxeReplyReceived field.
2367 @param NewPxeBisReplyReceived Pointer to a value that will replace the current
2368 PxeBisReplyReceived field.
2369 @param NewDhcpDiscover Pointer to the new cached DHCP Discover packet contents.
2370 @param NewDhcpAck Pointer to the new cached DHCP Ack packet contents.
2371 @param NewProxyOffer Pointer to the new cached Proxy Offer packet contents.
2372 @param NewPxeDiscover Pointer to the new cached PXE Discover packet contents.
2373 @param NewPxeReply Pointer to the new cached PXE Reply packet contents.
2374 @param NewPxeBisReply Pointer to the new cached PXE BIS Reply packet contents.
2376 @retval EFI_SUCCESS The cached packet contents were updated.
2377 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2378 @retval EFI_INVALID_PARAMETER This is NULL or not point to a valid EFI_PXE_BASE_CODE_PROTOCOL structure.
2383 EfiPxeBcSetPackets (
2384 IN EFI_PXE_BASE_CODE_PROTOCOL
* This
,
2385 IN BOOLEAN
* NewDhcpDiscoverValid OPTIONAL
,
2386 IN BOOLEAN
* NewDhcpAckReceived OPTIONAL
,
2387 IN BOOLEAN
* NewProxyOfferReceived OPTIONAL
,
2388 IN BOOLEAN
* NewPxeDiscoverValid OPTIONAL
,
2389 IN BOOLEAN
* NewPxeReplyReceived OPTIONAL
,
2390 IN BOOLEAN
* NewPxeBisReplyReceived OPTIONAL
,
2391 IN EFI_PXE_BASE_CODE_PACKET
* NewDhcpDiscover OPTIONAL
,
2392 IN EFI_PXE_BASE_CODE_PACKET
* NewDhcpAck OPTIONAL
,
2393 IN EFI_PXE_BASE_CODE_PACKET
* NewProxyOffer OPTIONAL
,
2394 IN EFI_PXE_BASE_CODE_PACKET
* NewPxeDiscover OPTIONAL
,
2395 IN EFI_PXE_BASE_CODE_PACKET
* NewPxeReply OPTIONAL
,
2396 IN EFI_PXE_BASE_CODE_PACKET
* NewPxeBisReply OPTIONAL
2399 PXEBC_PRIVATE_DATA
*Private
;
2400 EFI_PXE_BASE_CODE_MODE
*Mode
;
2403 return EFI_INVALID_PARAMETER
;
2406 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
2407 Mode
= Private
->PxeBc
.Mode
;
2409 if (!Mode
->Started
) {
2410 return EFI_NOT_STARTED
;
2413 if (NewDhcpDiscoverValid
!= NULL
) {
2414 Mode
->DhcpDiscoverValid
= *NewDhcpDiscoverValid
;
2417 if (NewDhcpAckReceived
!= NULL
) {
2418 Mode
->DhcpAckReceived
= *NewDhcpAckReceived
;
2421 if (NewProxyOfferReceived
!= NULL
) {
2422 Mode
->ProxyOfferReceived
= *NewProxyOfferReceived
;
2425 if (NewPxeDiscoverValid
!= NULL
) {
2426 Mode
->PxeDiscoverValid
= *NewPxeDiscoverValid
;
2429 if (NewPxeReplyReceived
!= NULL
) {
2430 Mode
->PxeReplyReceived
= *NewPxeReplyReceived
;
2433 if (NewPxeBisReplyReceived
!= NULL
) {
2434 Mode
->PxeBisReplyReceived
= *NewPxeBisReplyReceived
;
2437 if (NewDhcpDiscover
!= NULL
) {
2438 CopyMem (&Mode
->DhcpDiscover
, NewDhcpDiscover
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2441 if (NewDhcpAck
!= NULL
) {
2442 CopyMem (&Mode
->DhcpAck
, NewDhcpAck
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2445 if (NewProxyOffer
!= NULL
) {
2446 CopyMem (&Mode
->ProxyOffer
, NewProxyOffer
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2449 if (NewPxeDiscover
!= NULL
) {
2450 CopyMem (&Mode
->PxeDiscover
, NewPxeDiscover
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2453 if (NewPxeReply
!= NULL
) {
2454 CopyMem (&Mode
->PxeReply
, NewPxeReply
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2457 if (NewPxeBisReply
!= NULL
) {
2458 CopyMem (&Mode
->PxeBisReply
, NewPxeBisReply
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2464 EFI_PXE_BASE_CODE_PROTOCOL mPxeBcProtocolTemplate
= {
2465 EFI_PXE_BASE_CODE_PROTOCOL_REVISION
,
2473 EfiPxeBcSetIpFilter
,
2475 EfiPxeBcSetParameters
,
2476 EfiPxeBcSetStationIP
,
2482 Callback function that is invoked when the PXE Base Code Protocol is about to transmit, has
2483 received, or is waiting to receive a packet.
2485 This function is invoked when the PXE Base Code Protocol is about to transmit, has received,
2486 or is waiting to receive a packet. Parameters Function and Received specify the type of event.
2487 Parameters PacketLen and Packet specify the packet that generated the event. If these fields
2488 are zero and NULL respectively, then this is a status update callback. If the operation specified
2489 by Function is to continue, then CALLBACK_STATUS_CONTINUE should be returned. If the operation
2490 specified by Function should be aborted, then CALLBACK_STATUS_ABORT should be returned. Due to
2491 the polling nature of UEFI device drivers, a callback function should not execute for more than 5 ms.
2492 The SetParameters() function must be called after a Callback Protocol is installed to enable the
2495 @param This Pointer to the EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL instance.
2496 @param Function The PXE Base Code Protocol function that is waiting for an event.
2497 @param Received TRUE if the callback is being invoked due to a receive event. FALSE if
2498 the callback is being invoked due to a transmit event.
2499 @param PacketLength The length, in bytes, of Packet. This field will have a value of zero if
2500 this is a wait for receive event.
2501 @param PacketPtr If Received is TRUE, a pointer to the packet that was just received;
2502 otherwise a pointer to the packet that is about to be transmitted.
2504 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE if Function specifies a continue operation
2505 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT if Function specifies an abort operation
2508 EFI_PXE_BASE_CODE_CALLBACK_STATUS
2510 EfiPxeLoadFileCallback (
2511 IN EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
* This
,
2512 IN EFI_PXE_BASE_CODE_FUNCTION Function
,
2513 IN BOOLEAN Received
,
2514 IN UINT32 PacketLength
,
2515 IN EFI_PXE_BASE_CODE_PACKET
* PacketPtr OPTIONAL
2522 // Catch Ctrl-C or ESC to abort.
2524 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
2526 if (!EFI_ERROR (Status
)) {
2528 if (Key
.ScanCode
== SCAN_ESC
|| Key
.UnicodeChar
== (0x1F & 'c')) {
2530 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT
;
2534 // No print if receive packet
2537 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
;
2540 // Print only for three functions
2544 case EFI_PXE_BASE_CODE_FUNCTION_MTFTP
:
2546 // Print only for open MTFTP packets, not every MTFTP packets
2548 if (PacketLength
!= 0 && PacketPtr
!= NULL
) {
2549 if (PacketPtr
->Raw
[0x1C] != 0x00 || PacketPtr
->Raw
[0x1D] != 0x01) {
2550 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
;
2555 case EFI_PXE_BASE_CODE_FUNCTION_DHCP
:
2556 case EFI_PXE_BASE_CODE_FUNCTION_DISCOVER
:
2560 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
;
2563 if (PacketLength
!= 0 && PacketPtr
!= NULL
) {
2565 // Print '.' when transmit a packet
2571 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
;
2574 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL mPxeBcCallBackTemplate
= {
2575 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL_REVISION
,
2576 EfiPxeLoadFileCallback
2583 @param Private Pointer to PxeBc private data.
2584 @param BufferSize Pointer to buffer size.
2585 @param Buffer Pointer to buffer.
2587 @retval EFI_SUCCESS Discover the boot file successfully.
2588 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.
2589 @retval EFI_ABORTED PXE bootstrap server, so local boot need abort.
2590 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to load the boot file.
2595 IN PXEBC_PRIVATE_DATA
*Private
,
2596 IN OUT UINT64
*BufferSize
,
2600 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
2601 EFI_PXE_BASE_CODE_MODE
*Mode
;
2606 PXEBC_CACHED_DHCP4_PACKET
*Packet
;
2609 PxeBc
= &Private
->PxeBc
;
2611 Type
= EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP
;
2612 Layer
= EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL
;
2617 Status
= PxeBc
->Dhcp (PxeBc
, TRUE
);
2618 if (EFI_ERROR (Status
)) {
2623 // Select a boot server
2625 Status
= PxeBcSelectBootPrompt (Private
);
2627 if (Status
== EFI_SUCCESS
) {
2628 Status
= PxeBcSelectBootMenu (Private
, &Type
, TRUE
);
2629 } else if (Status
== EFI_TIMEOUT
) {
2630 Status
= PxeBcSelectBootMenu (Private
, &Type
, FALSE
);
2633 if (!EFI_ERROR (Status
)) {
2635 if (Type
== EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP
) {
2637 // Local boot(PXE bootstrap server) need abort
2642 UseBis
= (BOOLEAN
) (Mode
->BisSupported
&& Mode
->BisDetected
);
2643 Status
= PxeBc
->Discover (PxeBc
, Type
, &Layer
, UseBis
, NULL
);
2644 if (EFI_ERROR (Status
)) {
2652 // Get bootfile name and (m)tftp server ip addresss
2654 if (Mode
->PxeReplyReceived
) {
2655 Packet
= &Private
->PxeReply
;
2656 } else if (Mode
->ProxyOfferReceived
) {
2657 Packet
= &Private
->ProxyOffer
;
2659 Packet
= &Private
->Dhcp4Ack
;
2663 // Use siaddr(next server) in DHCPOFFER packet header, if zero, use option 54(server identifier)
2664 // in DHCPOFFER packet.
2665 // (It does not comply with PXE Spec, Ver2.1)
2667 if (EFI_IP4_EQUAL (&Packet
->Packet
.Offer
.Dhcp4
.Header
.ServerAddr
, &mZeroIp4Addr
)) {
2670 Packet
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_SERVER_ID
]->Data
,
2671 sizeof (EFI_IPv4_ADDRESS
)
2676 &Packet
->Packet
.Offer
.Dhcp4
.Header
.ServerAddr
,
2677 sizeof (EFI_IPv4_ADDRESS
)
2680 if (Private
->ServerIp
.Addr
[0] == 0) {
2681 return EFI_DEVICE_ERROR
;
2684 ASSERT (Packet
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
);
2689 Private
->BootFileName
= (CHAR8
*) (Packet
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Data
);
2691 if (Packet
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN
] != NULL
) {
2693 // Already have the bootfile length option, compute the file size
2695 CopyMem (&Value
, Packet
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN
]->Data
, sizeof (Value
));
2696 Value
= NTOHS (Value
);
2697 *BufferSize
= 512 * Value
;
2698 Status
= EFI_BUFFER_TOO_SMALL
;
2701 // Get the bootfile size from tftp
2703 Status
= PxeBc
->Mtftp (
2705 EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE
,
2709 &Private
->BlockSize
,
2711 (UINT8
*) Private
->BootFileName
,
2717 Private
->FileSize
= (UINTN
) *BufferSize
;
2723 Causes the driver to load a specified file.
2725 @param This Protocol instance pointer.
2726 @param FilePath The device specific path of the file to load.
2727 @param BootPolicy If TRUE, indicates that the request originates from the
2728 boot manager is attempting to load FilePath as a boot
2729 selection. If FALSE, then FilePath must match as exact file
2731 @param BufferSize On input the size of Buffer in bytes. On output with a return
2732 code of EFI_SUCCESS, the amount of data transferred to
2733 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
2734 the size of Buffer required to retrieve the requested file.
2735 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
2736 then no the size of the requested file is returned in
2739 @retval EFI_SUCCESS The file was loaded.
2740 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy
2741 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or
2743 @retval EFI_NO_MEDIA No medium was present to load the file.
2744 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.
2745 @retval EFI_NO_RESPONSE The remote system did not respond.
2746 @retval EFI_NOT_FOUND The file was not found.
2747 @retval EFI_ABORTED The file load process was manually cancelled.
2753 IN EFI_LOAD_FILE_PROTOCOL
* This
,
2754 IN EFI_DEVICE_PATH_PROTOCOL
* FilePath
,
2755 IN BOOLEAN BootPolicy
,
2756 IN OUT UINTN
*BufferSize
,
2757 IN VOID
*Buffer OPTIONAL
2760 PXEBC_PRIVATE_DATA
*Private
;
2761 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
2762 BOOLEAN NewMakeCallback
;
2765 BOOLEAN MediaPresent
;
2767 Private
= PXEBC_PRIVATE_DATA_FROM_LOADFILE (This
);
2768 PxeBc
= &Private
->PxeBc
;
2769 NewMakeCallback
= FALSE
;
2770 Status
= EFI_DEVICE_ERROR
;
2772 if (This
== NULL
|| BufferSize
== NULL
) {
2774 return EFI_INVALID_PARAMETER
;
2778 // Only support BootPolicy
2781 return EFI_UNSUPPORTED
;
2785 // Check media status before PXE start
2787 MediaPresent
= TRUE
;
2788 NetLibDetectMedia (Private
->Controller
, &MediaPresent
);
2789 if (!MediaPresent
) {
2790 return EFI_NO_MEDIA
;
2793 Status
= PxeBc
->Start (PxeBc
, FALSE
);
2794 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
2798 Status
= gBS
->HandleProtocol (
2799 Private
->Controller
,
2800 &gEfiPxeBaseCodeCallbackProtocolGuid
,
2801 (VOID
**) &Private
->PxeBcCallback
2803 if (Status
== EFI_UNSUPPORTED
) {
2805 CopyMem (&Private
->LoadFileCallback
, &mPxeBcCallBackTemplate
, sizeof (Private
->LoadFileCallback
));
2807 Status
= gBS
->InstallProtocolInterface (
2808 &Private
->Controller
,
2809 &gEfiPxeBaseCodeCallbackProtocolGuid
,
2810 EFI_NATIVE_INTERFACE
,
2811 &Private
->LoadFileCallback
2814 NewMakeCallback
= (BOOLEAN
) (Status
== EFI_SUCCESS
);
2816 Status
= PxeBc
->SetParameters (PxeBc
, NULL
, NULL
, NULL
, NULL
, &NewMakeCallback
);
2817 if (EFI_ERROR (Status
)) {
2818 PxeBc
->Stop (PxeBc
);
2823 if (Private
->FileSize
== 0) {
2825 Status
= DiscoverBootFile (Private
, &TmpBufSize
, Buffer
);
2827 if (sizeof (UINTN
) < sizeof (UINT64
) && (TmpBufSize
> 0xFFFFFFFF)) {
2828 Status
= EFI_DEVICE_ERROR
;
2829 } else if (TmpBufSize
> 0 && *BufferSize
>= (UINTN
) TmpBufSize
&& Buffer
!= NULL
) {
2830 *BufferSize
= (UINTN
) TmpBufSize
;
2831 Status
= PxeBc
->Mtftp (
2833 EFI_PXE_BASE_CODE_TFTP_READ_FILE
,
2837 &Private
->BlockSize
,
2839 (UINT8
*) Private
->BootFileName
,
2843 } else if (TmpBufSize
> 0) {
2844 *BufferSize
= (UINTN
) TmpBufSize
;
2845 Status
= EFI_BUFFER_TOO_SMALL
;
2847 } else if (Buffer
== NULL
|| Private
->FileSize
> *BufferSize
) {
2848 *BufferSize
= Private
->FileSize
;
2849 Status
= EFI_BUFFER_TOO_SMALL
;
2852 // Download the file.
2854 TmpBufSize
= (UINT64
) (*BufferSize
);
2855 Status
= PxeBc
->Mtftp (
2857 EFI_PXE_BASE_CODE_TFTP_READ_FILE
,
2861 &Private
->BlockSize
,
2863 (UINT8
*) Private
->BootFileName
,
2869 // If we added a callback protocol, now is the time to remove it.
2871 if (NewMakeCallback
) {
2873 NewMakeCallback
= FALSE
;
2875 PxeBc
->SetParameters (PxeBc
, NULL
, NULL
, NULL
, NULL
, &NewMakeCallback
);
2877 gBS
->UninstallProtocolInterface (
2878 Private
->Controller
,
2879 &gEfiPxeBaseCodeCallbackProtocolGuid
,
2880 &Private
->LoadFileCallback
2885 // Check download status
2887 if (Status
== EFI_SUCCESS
) {
2889 // The functionality of PXE Base Code protocol will not be stopped,
2890 // when downloading is successfully.
2894 } else if (Status
== EFI_BUFFER_TOO_SMALL
) {
2895 if (Buffer
!= NULL
) {
2896 AsciiPrint ("PXE-E05: Download buffer is smaller than requested file.\n");
2899 // The functionality of PXE Base Code protocol will not be stopped.
2904 } else if (Status
== EFI_DEVICE_ERROR
) {
2905 AsciiPrint ("PXE-E07: Network device error.\n");
2907 } else if (Status
== EFI_OUT_OF_RESOURCES
) {
2908 AsciiPrint ("PXE-E09: Could not allocate I/O buffers.\n");
2910 } else if (Status
== EFI_NO_MEDIA
) {
2911 AsciiPrint ("PXE-E12: Could not detect network connection.\n");
2913 } else if (Status
== EFI_NO_RESPONSE
) {
2914 AsciiPrint ("PXE-E16: No offer received.\n");
2916 } else if (Status
== EFI_TIMEOUT
) {
2917 AsciiPrint ("PXE-E18: Server response timeout.\n");
2919 } else if (Status
== EFI_ABORTED
) {
2920 AsciiPrint ("PXE-E21: Remote boot cancelled.\n");
2922 } else if (Status
== EFI_ICMP_ERROR
) {
2923 AsciiPrint ("PXE-E22: Client received ICMP error from server.\n");
2925 } else if (Status
== EFI_TFTP_ERROR
) {
2926 AsciiPrint ("PXE-E23: Client received TFTP error from server.\n");
2929 AsciiPrint ("PXE-E99: Unexpected network error.\n");
2932 PxeBc
->Stop (PxeBc
);
2937 EFI_LOAD_FILE_PROTOCOL mLoadFileProtocolTemplate
= { EfiPxeLoadFile
};