2 Interface routines for PxeBc.
4 Copyright (c) 2007 - 2016, 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 (NTOHL (Mode
->SubnetMask
.Addr
[0]) != 0) &&
180 IP4_NET_EQUAL (NTOHL(Mode
->StationIp
.Addr
[0]), EFI_NTOHL (RxData
->Header
->SourceAddress
), NTOHL (Mode
->SubnetMask
.Addr
[0])) &&
181 !NetIp4IsUnicast (EFI_NTOHL (RxData
->Header
->SourceAddress
), NTOHL (Mode
->SubnetMask
.Addr
[0]))) {
183 // The source address is not zero and it's not a unicast IP address, discard it.
188 if (!EFI_IP4_EQUAL (&RxData
->Header
->DestinationAddress
, &Mode
->StationIp
.v4
)) {
190 // The dest address is not equal to Station Ip address, discard it.
196 // Constructor ICMP error packet
199 CopiedPointer
= (UINT8
*) &Mode
->IcmpError
;
201 for (Index
= 0; Index
< RxData
->FragmentCount
; Index
++) {
202 CopiedLen
+= RxData
->FragmentTable
[Index
].FragmentLength
;
203 if (CopiedLen
<= sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR
)) {
206 RxData
->FragmentTable
[Index
].FragmentBuffer
,
207 RxData
->FragmentTable
[Index
].FragmentLength
212 RxData
->FragmentTable
[Index
].FragmentBuffer
,
213 CopiedLen
- sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR
)
216 CopiedPointer
+= CopiedLen
;
222 gBS
->SignalEvent (RxData
->RecycleSignal
);
225 Ip4
->Receive (Ip4
, &(Private
->IcmpErrorRcvToken
));
229 Request IcmpErrorListenHandlerDpc as a DPC at TPL_CALLBACK
231 @param Event The event signaled.
232 @param Context The context passed in by the event notifier.
237 IcmpErrorListenHandler (
243 // Request IpIoListenHandlerDpc as a DPC at TPL_CALLBACK
245 QueueDpc (TPL_CALLBACK
, IcmpErrorListenHandlerDpc
, Context
);
249 Enables the use of the PXE Base Code Protocol functions.
251 This function enables the use of the PXE Base Code Protocol functions. If the
252 Started field of the EFI_PXE_BASE_CODE_MODE structure is already TRUE, then
253 EFI_ALREADY_STARTED will be returned. If UseIpv6 is TRUE, then IPv6 formatted
254 addresses will be used in this session. If UseIpv6 is FALSE, then IPv4 formatted
255 addresses will be used in this session. If UseIpv6 is TRUE, and the Ipv6Supported
256 field of the EFI_PXE_BASE_CODE_MODE structure is FALSE, then EFI_UNSUPPORTED will
257 be returned. If there is not enough memory or other resources to start the PXE
258 Base Code Protocol, then EFI_OUT_OF_RESOURCES will be returned. Otherwise, the
259 PXE Base Code Protocol will be started, and all of the fields of the EFI_PXE_BASE_CODE_MODE
260 structure will be initialized as follows:
262 Ipv6SupportedUnchanged.
263 Ipv6AvailableUnchanged.
264 UsingIpv6Set to UseIpv6.
265 BisSupportedUnchanged.
266 BisDetectedUnchanged.
268 SendGUIDSet to FALSE.
269 TTLSet to DEFAULT_TTL.
270 ToSSet to DEFAULT_ToS.
271 DhcpCompletedSet to FALSE.
272 ProxyOfferReceivedSet to FALSE.
273 StationIpSet to an address of all zeros.
274 SubnetMaskSet to a subnet mask of all zeros.
275 DhcpDiscoverZero-filled.
277 ProxyOfferZero-filled.
278 PxeDiscoverValidSet to FALSE.
279 PxeDiscoverZero-filled.
280 PxeReplyValidSet to FALSE.
282 PxeBisReplyValidSet to FALSE.
283 PxeBisReplyZero-filled.
284 IpFilterSet the Filters field to 0 and the IpCnt field to 0.
285 ArpCacheEntriesSet to 0.
287 RouteTableEntriesSet to 0.
288 RouteTableZero-filled.
289 IcmpErrorReceivedSet to FALSE.
290 IcmpErrorZero-filled.
291 TftpErroReceivedSet to FALSE.
292 TftpErrorZero-filled.
293 MakeCallbacksSet to TRUE if the PXE Base Code Callback Protocol is available.
294 Set to FALSE if the PXE Base Code Callback Protocol is not available.
296 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
297 @param UseIpv6 Specifies the type of IP addresses that are to be used during the session
298 that is being started. Set to TRUE for IPv6 addresses, and FALSE for
301 @retval EFI_SUCCESS The PXE Base Code Protocol was started.
302 @retval EFI_DEVICE_ERROR The network device encountered an error during this oper
303 @retval EFI_UNSUPPORTED UseIpv6 is TRUE, but the Ipv6Supported field of the
304 EFI_PXE_BASE_CODE_MODE structure is FALSE.
305 @retval EFI_ALREADY_STARTED The PXE Base Code Protocol is already in the started state.
306 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
307 EFI_PXE_BASE_CODE_PROTOCOL structure.
308 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory or other resources to start the
309 PXE Base Code Protocol.
315 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
319 PXEBC_PRIVATE_DATA
*Private
;
320 EFI_PXE_BASE_CODE_MODE
*Mode
;
324 return EFI_INVALID_PARAMETER
;
327 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
328 Mode
= Private
->PxeBc
.Mode
;
331 return EFI_ALREADY_STARTED
;
336 // IPv6 is not supported now.
338 return EFI_UNSUPPORTED
;
342 // Configure the udp4 instance to let it receive data
344 Status
= Private
->Udp4Read
->Configure (
346 &Private
->Udp4CfgData
348 if (EFI_ERROR (Status
)) {
354 // Configure block size for TFTP as a default value to handle all link layers.
356 Private
->BlockSize
= (UINTN
) (MIN (Private
->Ip4MaxPacketSize
, PXEBC_DEFAULT_PACKET_SIZE
) -
357 PXEBC_DEFAULT_UDP_OVERHEAD_SIZE
- PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE
);
359 // If PcdTftpBlockSize is set to non-zero, override the default value.
361 if (PcdGet64 (PcdTftpBlockSize
) != 0) {
362 Private
->BlockSize
= (UINTN
) PcdGet64 (PcdTftpBlockSize
);
365 Private
->AddressIsOk
= FALSE
;
367 ZeroMem (Mode
, sizeof (EFI_PXE_BASE_CODE_MODE
));
369 Mode
->Started
= TRUE
;
370 Mode
->TTL
= DEFAULT_TTL
;
371 Mode
->ToS
= DEFAULT_ToS
;
372 Mode
->AutoArp
= TRUE
;
375 // Create the event for Arp Cache checking.
377 Status
= gBS
->CreateEvent (
378 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
380 ArpCacheUpdateTimeout
,
382 &Private
->GetArpCacheEvent
384 if (EFI_ERROR (Status
)) {
389 // Start the timeout timer event.
391 Status
= gBS
->SetTimer (
392 Private
->GetArpCacheEvent
,
397 if (EFI_ERROR (Status
)) {
402 // Create ICMP error receiving event
404 Status
= gBS
->CreateEvent (
407 IcmpErrorListenHandler
,
409 &(Private
->IcmpErrorRcvToken
.Event
)
411 if (EFI_ERROR (Status
)) {
416 //DHCP4 service allows only one of its children to be configured in
417 //the active state, If the DHCP4 D.O.R.A started by IP4 auto
418 //configuration and has not been completed, the Dhcp4 state machine
419 //will not be in the right state for the PXE to start a new round D.O.R.A.
420 //so we need to switch it's policy to static.
422 Status
= PxeBcSetIp4Policy (Private
);
423 if (EFI_ERROR (Status
)) {
427 Status
= Private
->Ip4
->Configure (Private
->Ip4
, &Private
->Ip4ConfigData
);
428 if (EFI_ERROR (Status
)) {
433 // start to listen incoming packet
435 Status
= Private
->Ip4
->Receive (Private
->Ip4
, &Private
->IcmpErrorRcvToken
);
436 if (!EFI_ERROR (Status
)) {
441 Private
->Ip4
->Configure (Private
->Ip4
, NULL
);
443 if (Private
->IcmpErrorRcvToken
.Event
!= NULL
) {
444 gBS
->CloseEvent (Private
->IcmpErrorRcvToken
.Event
);
447 if (Private
->GetArpCacheEvent
!= NULL
) {
448 gBS
->SetTimer (Private
->GetArpCacheEvent
, TimerCancel
, 0);
449 gBS
->CloseEvent (Private
->GetArpCacheEvent
);
452 Mode
->Started
= FALSE
;
455 Mode
->AutoArp
= FALSE
;
462 Disables the use of the PXE Base Code Protocol functions.
464 This function stops all activity on the network device. All the resources allocated
465 in Start() are released, the Started field of the EFI_PXE_BASE_CODE_MODE structure is
466 set to FALSE and EFI_SUCCESS is returned. If the Started field of the EFI_PXE_BASE_CODE_MODE
467 structure is already FALSE, then EFI_NOT_STARTED will be returned.
469 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
471 @retval EFI_SUCCESS The PXE Base Code Protocol was stopped.
472 @retval EFI_NOT_STARTED The PXE Base Code Protocol is already in the stopped state.
473 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
474 EFI_PXE_BASE_CODE_PROTOCOL structure.
475 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
481 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
484 PXEBC_PRIVATE_DATA
*Private
;
485 EFI_PXE_BASE_CODE_MODE
*Mode
;
488 return EFI_INVALID_PARAMETER
;
491 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
492 Mode
= Private
->PxeBc
.Mode
;
494 if (!Mode
->Started
) {
495 return EFI_NOT_STARTED
;
498 Private
->Ip4
->Cancel (Private
->Ip4
, NULL
);
500 // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's
505 Private
->Ip4
->Configure (Private
->Ip4
, NULL
);
508 // Close the ICMP error receiving event.
510 gBS
->CloseEvent (Private
->IcmpErrorRcvToken
.Event
);
513 // Cancel the TimeoutEvent timer.
515 gBS
->SetTimer (Private
->GetArpCacheEvent
, TimerCancel
, 0);
518 // Close the TimeoutEvent event.
520 gBS
->CloseEvent (Private
->GetArpCacheEvent
);
522 Mode
->Started
= FALSE
;
524 Private
->CurrentUdpSrcPort
= 0;
525 Private
->Udp4Write
->Configure (Private
->Udp4Write
, NULL
);
526 Private
->Udp4Read
->Groups (Private
->Udp4Read
, FALSE
, NULL
);
527 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
529 Private
->Dhcp4
->Stop (Private
->Dhcp4
);
530 Private
->Dhcp4
->Configure (Private
->Dhcp4
, NULL
);
532 Private
->FileSize
= 0;
539 Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / acknowledge) or DHCPv6
540 S.A.R.R (solicit / advertise / request / reply) sequence.
542 This function attempts to complete the DHCP sequence. If this sequence is completed,
543 then EFI_SUCCESS is returned, and the DhcpCompleted, ProxyOfferReceived, StationIp,
544 SubnetMask, DhcpDiscover, DhcpAck, and ProxyOffer fields of the EFI_PXE_BASE_CODE_MODE
545 structure are filled in.
546 If SortOffers is TRUE, then the cached DHCP offer packets will be sorted before
547 they are tried. If SortOffers is FALSE, then the cached DHCP offer packets will
548 be tried in the order in which they are received. Please see the Preboot Execution
549 Environment (PXE) Specification for additional details on the implementation of DHCP.
550 This function can take at least 31 seconds to timeout and return control to the
551 caller. If the DHCP sequence does not complete, then EFI_TIMEOUT will be returned.
552 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
553 then the DHCP sequence will be stopped and EFI_ABORTED will be returned.
555 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
556 @param SortOffers TRUE if the offers received should be sorted. Set to FALSE to try the
557 offers in the order that they are received.
559 @retval EFI_SUCCESS Valid DHCP has completed.
560 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
561 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
562 EFI_PXE_BASE_CODE_PROTOCOL structure.
563 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
564 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete the DHCP Protocol.
565 @retval EFI_ABORTED The callback function aborted the DHCP Protocol.
566 @retval EFI_TIMEOUT The DHCP Protocol timed out.
567 @retval EFI_ICMP_ERROR An ICMP error packet was received during the DHCP session.
568 @retval EFI_NO_RESPONSE Valid PXE offer was not received.
574 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
575 IN BOOLEAN SortOffers
578 PXEBC_PRIVATE_DATA
*Private
;
579 EFI_PXE_BASE_CODE_MODE
*Mode
;
580 EFI_DHCP4_PROTOCOL
*Dhcp4
;
581 EFI_DHCP4_CONFIG_DATA Dhcp4CfgData
;
582 EFI_DHCP4_MODE_DATA Dhcp4Mode
;
583 EFI_DHCP4_PACKET_OPTION
*OptList
[PXEBC_DHCP4_MAX_OPTION_NUM
];
586 EFI_ARP_CONFIG_DATA ArpConfigData
;
587 EFI_PXE_BASE_CODE_IP_FILTER IpFilter
;
590 return EFI_INVALID_PARAMETER
;
593 Status
= EFI_SUCCESS
;
594 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
595 Mode
= Private
->PxeBc
.Mode
;
596 Dhcp4
= Private
->Dhcp4
;
597 Private
->Function
= EFI_PXE_BASE_CODE_FUNCTION_DHCP
;
598 Private
->SortOffers
= SortOffers
;
600 if (!Mode
->Started
) {
601 return EFI_NOT_STARTED
;
604 Mode
->IcmpErrorReceived
= FALSE
;
607 // Stop Udp4Read instance
609 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
612 // Initialize the DHCP options and build the option list
614 OptCount
= PxeBcBuildDhcpOptions (Private
, OptList
, TRUE
);
617 // Set the DHCP4 config data.
618 // The four discovery timeouts are 4, 8, 16, 32 seconds respectively.
620 ZeroMem (&Dhcp4CfgData
, sizeof (EFI_DHCP4_CONFIG_DATA
));
621 Dhcp4CfgData
.OptionCount
= OptCount
;
622 Dhcp4CfgData
.OptionList
= OptList
;
623 Dhcp4CfgData
.Dhcp4Callback
= PxeBcDhcpCallBack
;
624 Dhcp4CfgData
.CallbackContext
= Private
;
625 Dhcp4CfgData
.DiscoverTryCount
= 4;
626 Dhcp4CfgData
.DiscoverTimeout
= mPxeDhcpTimeout
;
628 Status
= Dhcp4
->Configure (Dhcp4
, &Dhcp4CfgData
);
629 if (EFI_ERROR (Status
)) {
634 // Zero those arrays to record the varies numbers of DHCP OFFERS.
636 Private
->GotProxyOffer
= FALSE
;
637 Private
->NumOffers
= 0;
638 Private
->BootpIndex
= 0;
639 ZeroMem (Private
->ServerCount
, sizeof (Private
->ServerCount
));
640 ZeroMem (Private
->ProxyIndex
, sizeof (Private
->ProxyIndex
));
642 Status
= Dhcp4
->Start (Dhcp4
, NULL
);
643 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
644 if (Status
== EFI_ICMP_ERROR
) {
645 Mode
->IcmpErrorReceived
= TRUE
;
650 Status
= Dhcp4
->GetModeData (Dhcp4
, &Dhcp4Mode
);
651 if (EFI_ERROR (Status
)) {
655 ASSERT (Dhcp4Mode
.State
== Dhcp4Bound
);
657 CopyMem (&Private
->StationIp
, &Dhcp4Mode
.ClientAddress
, sizeof (EFI_IPv4_ADDRESS
));
658 CopyMem (&Private
->SubnetMask
, &Dhcp4Mode
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
659 CopyMem (&Private
->GatewayIp
, &Dhcp4Mode
.RouterAddress
, sizeof (EFI_IPv4_ADDRESS
));
661 CopyMem (&Mode
->StationIp
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
662 CopyMem (&Mode
->SubnetMask
, &Private
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
665 // Check the selected offer to see whether BINL is required, if no or BINL is
666 // finished, set the various Mode members.
668 Status
= PxeBcCheckSelectedOffer (Private
);
671 if (EFI_ERROR (Status
)) {
673 Dhcp4
->Configure (Dhcp4
, NULL
);
676 // Remove the previously configured option list and callback function
678 ZeroMem (&Dhcp4CfgData
, sizeof (EFI_DHCP4_CONFIG_DATA
));
679 Dhcp4
->Configure (Dhcp4
, &Dhcp4CfgData
);
681 Private
->AddressIsOk
= TRUE
;
683 if (!Mode
->UsingIpv6
) {
685 // If in IPv4 mode, configure the corresponding ARP with this new
686 // station IP address.
688 ZeroMem (&ArpConfigData
, sizeof (EFI_ARP_CONFIG_DATA
));
690 ArpConfigData
.SwAddressType
= 0x0800;
691 ArpConfigData
.SwAddressLength
= (UINT8
) sizeof (EFI_IPv4_ADDRESS
);
692 ArpConfigData
.StationAddress
= &Private
->StationIp
.v4
;
694 Private
->Arp
->Configure (Private
->Arp
, NULL
);
695 Private
->Arp
->Configure (Private
->Arp
, &ArpConfigData
);
698 // Updated the route table. Fill the first entry.
700 Mode
->RouteTableEntries
= 1;
701 Mode
->RouteTable
[0].IpAddr
.Addr
[0] = Private
->StationIp
.Addr
[0] & Private
->SubnetMask
.Addr
[0];
702 Mode
->RouteTable
[0].SubnetMask
.Addr
[0] = Private
->SubnetMask
.Addr
[0];
703 Mode
->RouteTable
[0].GwAddr
.Addr
[0] = 0;
706 // Create the default route entry if there is a default router.
708 if (Private
->GatewayIp
.Addr
[0] != 0) {
709 Mode
->RouteTableEntries
= 2;
710 Mode
->RouteTable
[1].IpAddr
.Addr
[0] = 0;
711 Mode
->RouteTable
[1].SubnetMask
.Addr
[0] = 0;
712 Mode
->RouteTable
[1].GwAddr
.Addr
[0] = Private
->GatewayIp
.Addr
[0];
716 // Flush new station IP address into Udp4CfgData and Ip4ConfigData
718 CopyMem (&Private
->Udp4CfgData
.StationAddress
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
719 CopyMem (&Private
->Udp4CfgData
.SubnetMask
, &Private
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
720 CopyMem (&Private
->Ip4ConfigData
.StationAddress
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
721 CopyMem (&Private
->Ip4ConfigData
.SubnetMask
, &Private
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
724 // Reconfigure the Ip4 instance to capture background ICMP packets with new station Ip address.
726 Private
->Ip4
->Cancel (Private
->Ip4
, &Private
->IcmpErrorRcvToken
);
727 Private
->Ip4
->Configure (Private
->Ip4
, NULL
);
729 Status
= Private
->Ip4
->Configure (Private
->Ip4
, &Private
->Ip4ConfigData
);
730 if (EFI_ERROR (Status
)) {
734 Status
= Private
->Ip4
->Receive (Private
->Ip4
, &Private
->IcmpErrorRcvToken
);
735 if (EFI_ERROR (Status
)) {
741 Private
->Udp4Read
->Configure (Private
->Udp4Read
, &Private
->Udp4CfgData
);
744 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
745 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
747 ZeroMem(&IpFilter
, sizeof (EFI_PXE_BASE_CODE_IP_FILTER
));
748 IpFilter
.Filters
= EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
;
749 This
->SetIpFilter (This
, &IpFilter
);
756 Attempts to complete the PXE Boot Server and/or boot image discovery sequence.
758 This function attempts to complete the PXE Boot Server and/or boot image discovery
759 sequence. If this sequence is completed, then EFI_SUCCESS is returned, and the
760 PxeDiscoverValid, PxeDiscover, PxeReplyReceived, and PxeReply fields of the
761 EFI_PXE_BASE_CODE_MODE structure are filled in. If UseBis is TRUE, then the
762 PxeBisReplyReceived and PxeBisReply fields of the EFI_PXE_BASE_CODE_MODE structure
763 will also be filled in. If UseBis is FALSE, then PxeBisReplyValid will be set to FALSE.
764 In the structure referenced by parameter Info, the PXE Boot Server list, SrvList[],
765 has two uses: It is the Boot Server IP address list used for unicast discovery
766 (if the UseUCast field is TRUE), and it is the list used for Boot Server verification
767 (if the MustUseList field is TRUE). Also, if the MustUseList field in that structure
768 is TRUE and the AcceptAnyResponse field in the SrvList[] array is TRUE, any Boot
769 Server reply of that type will be accepted. If the AcceptAnyResponse field is
770 FALSE, only responses from Boot Servers with matching IP addresses will be accepted.
771 This function can take at least 10 seconds to timeout and return control to the
772 caller. If the Discovery sequence does not complete, then EFI_TIMEOUT will be
773 returned. Please see the Preboot Execution Environment (PXE) Specification for
774 additional details on the implementation of the Discovery sequence.
775 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
776 then the Discovery sequence is stopped and EFI_ABORTED will be returned.
778 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
779 @param Type The type of bootstrap to perform.
780 @param Layer Pointer to the boot server layer number to discover, which must be
781 PXE_BOOT_LAYER_INITIAL when a new server type is being
783 @param UseBis TRUE if Boot Integrity Services are to be used. FALSE otherwise.
784 @param Info Pointer to a data structure that contains additional information on the
785 type of discovery operation that is to be performed.
787 @retval EFI_SUCCESS The Discovery sequence has been completed.
788 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
789 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
790 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
791 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete Discovery.
792 @retval EFI_ABORTED The callback function aborted the Discovery sequence.
793 @retval EFI_TIMEOUT The Discovery sequence timed out.
794 @retval EFI_ICMP_ERROR An ICMP error packet was received during the PXE discovery
801 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
805 IN EFI_PXE_BASE_CODE_DISCOVER_INFO
*Info OPTIONAL
808 PXEBC_PRIVATE_DATA
*Private
;
809 EFI_PXE_BASE_CODE_MODE
*Mode
;
810 EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo
;
811 EFI_PXE_BASE_CODE_DISCOVER_INFO
*CreatedInfo
;
812 EFI_PXE_BASE_CODE_SRVLIST
*SrvList
;
813 EFI_PXE_BASE_CODE_SRVLIST DefaultSrvList
;
814 PXEBC_CACHED_DHCP4_PACKET
*Packet
;
815 PXEBC_VENDOR_OPTION
*VendorOpt
;
818 PXEBC_BOOT_SVR_ENTRY
*BootSvrEntry
;
819 EFI_PXE_BASE_CODE_IP_FILTER IpFilter
;
822 return EFI_INVALID_PARAMETER
;
825 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
826 Mode
= Private
->PxeBc
.Mode
;
830 Status
= EFI_DEVICE_ERROR
;
831 Private
->Function
= EFI_PXE_BASE_CODE_FUNCTION_DISCOVER
;
833 if (!Private
->AddressIsOk
) {
834 return EFI_INVALID_PARAMETER
;
837 if (!Mode
->Started
) {
838 return EFI_NOT_STARTED
;
842 // Stop Udp4Read instance
844 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
846 Mode
->IcmpErrorReceived
= FALSE
;
849 // If layer isn't EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL,
850 // use the previous setting;
851 // If info isn't offered,
852 // use the cached DhcpAck and ProxyOffer packets.
854 ZeroMem (&DefaultInfo
, sizeof (EFI_PXE_BASE_CODE_DISCOVER_INFO
));
855 if (*Layer
!= EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL
) {
857 if (!Mode
->PxeDiscoverValid
|| !Mode
->PxeReplyReceived
|| (!Mode
->PxeBisReplyReceived
&& UseBis
)) {
859 Status
= EFI_INVALID_PARAMETER
;
863 DefaultInfo
.IpCnt
= 1;
864 DefaultInfo
.UseUCast
= TRUE
;
866 DefaultSrvList
.Type
= Type
;
867 DefaultSrvList
.AcceptAnyResponse
= FALSE
;
868 DefaultSrvList
.IpAddr
.Addr
[0] = Private
->ServerIp
.Addr
[0];
870 SrvList
= &DefaultSrvList
;
872 } else if (Info
== NULL
) {
874 // Create info by the cached packet before
876 Packet
= (Mode
->ProxyOfferReceived
) ? &Private
->ProxyOffer
: &Private
->Dhcp4Ack
;
877 VendorOpt
= &Packet
->PxeVendorOption
;
879 if (!Mode
->DhcpAckReceived
|| !IS_VALID_DISCOVER_VENDOR_OPTION (VendorOpt
->BitMap
)) {
881 // Address is not acquired or no discovery options.
883 Status
= EFI_INVALID_PARAMETER
;
887 DefaultInfo
.UseMCast
= (BOOLEAN
)!IS_DISABLE_MCAST_DISCOVER (VendorOpt
->DiscoverCtrl
);
888 DefaultInfo
.UseBCast
= (BOOLEAN
)!IS_DISABLE_BCAST_DISCOVER (VendorOpt
->DiscoverCtrl
);
889 DefaultInfo
.MustUseList
= (BOOLEAN
) IS_ENABLE_USE_SERVER_LIST (VendorOpt
->DiscoverCtrl
);
890 DefaultInfo
.UseUCast
= DefaultInfo
.MustUseList
;
892 if (DefaultInfo
.UseMCast
) {
894 // Get the multicast discover ip address from vendor option.
897 &DefaultInfo
.ServerMCastIp
.Addr
,
898 &VendorOpt
->DiscoverMcastIp
,
899 sizeof (EFI_IPv4_ADDRESS
)
903 DefaultInfo
.IpCnt
= 0;
905 SrvList
= Info
->SrvList
;
907 if (DefaultInfo
.MustUseList
) {
908 BootSvrEntry
= VendorOpt
->BootSvr
;
909 Status
= EFI_INVALID_PARAMETER
;
911 while (((UINT8
) (BootSvrEntry
- VendorOpt
->BootSvr
)) < VendorOpt
->BootSvrLen
) {
913 if (BootSvrEntry
->Type
== HTONS (Type
)) {
914 Status
= EFI_SUCCESS
;
918 BootSvrEntry
= GET_NEXT_BOOT_SVR_ENTRY (BootSvrEntry
);
921 if (EFI_ERROR (Status
)) {
925 DefaultInfo
.IpCnt
= BootSvrEntry
->IpCnt
;
927 if (DefaultInfo
.IpCnt
>= 1) {
928 CreatedInfo
= AllocatePool (sizeof (DefaultInfo
) + (DefaultInfo
.IpCnt
- 1) * sizeof (*SrvList
));
929 if (CreatedInfo
== NULL
) {
930 Status
= EFI_OUT_OF_RESOURCES
;
935 CopyMem (CreatedInfo
, &DefaultInfo
, sizeof (DefaultInfo
));
937 SrvList
= Info
->SrvList
;
940 for (Index
= 0; Index
< DefaultInfo
.IpCnt
; Index
++) {
941 CopyMem (&SrvList
[Index
].IpAddr
, &BootSvrEntry
->IpAddr
[Index
], sizeof (EFI_IPv4_ADDRESS
));
942 SrvList
[Index
].AcceptAnyResponse
= FALSE
;
943 SrvList
[Index
].Type
= BootSvrEntry
->Type
;
949 SrvList
= Info
->SrvList
;
951 if (!SrvList
[0].AcceptAnyResponse
) {
953 for (Index
= 1; Index
< Info
->IpCnt
; Index
++) {
954 if (SrvList
[Index
].AcceptAnyResponse
) {
959 if (Index
!= Info
->IpCnt
) {
960 Status
= EFI_INVALID_PARAMETER
;
966 if ((!Info
->UseUCast
&& !Info
->UseBCast
&& !Info
->UseMCast
) || (Info
->MustUseList
&& Info
->IpCnt
== 0)) {
968 Status
= EFI_INVALID_PARAMETER
;
972 // Execute discover by UniCast/BroadCast/MultiCast
974 if (Info
->UseUCast
) {
976 for (Index
= 0; Index
< Info
->IpCnt
; Index
++) {
978 if (BootSvrEntry
== NULL
) {
979 Private
->ServerIp
.Addr
[0] = SrvList
[Index
].IpAddr
.Addr
[0];
983 &BootSvrEntry
->IpAddr
[Index
],
984 sizeof (EFI_IPv4_ADDRESS
)
988 Status
= PxeBcDiscvBootService (
993 &SrvList
[Index
].IpAddr
,
997 &Private
->PxeReply
.Packet
.Ack
999 if (!EFI_ERROR (Status
)) {
1004 } else if (Info
->UseMCast
) {
1006 Status
= PxeBcDiscvBootService (
1011 &Info
->ServerMCastIp
,
1015 &Private
->PxeReply
.Packet
.Ack
1018 } else if (Info
->UseBCast
) {
1020 Status
= PxeBcDiscvBootService (
1029 &Private
->PxeReply
.Packet
.Ack
1033 if (EFI_ERROR (Status
) || !Mode
->PxeReplyReceived
|| (!Mode
->PxeBisReplyReceived
&& UseBis
)) {
1034 if (Status
== EFI_ICMP_ERROR
) {
1035 Mode
->IcmpErrorReceived
= TRUE
;
1037 Status
= EFI_DEVICE_ERROR
;
1041 PxeBcParseCachedDhcpPacket (&Private
->PxeReply
);
1044 if (Mode
->PxeBisReplyReceived
) {
1047 &Mode
->PxeReply
.Dhcpv4
.BootpSiAddr
,
1048 sizeof (EFI_IPv4_ADDRESS
)
1052 if (CreatedInfo
!= NULL
) {
1053 FreePool (CreatedInfo
);
1058 Private
->Udp4Read
->Configure (Private
->Udp4Read
, &Private
->Udp4CfgData
);
1061 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
1062 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
1064 ZeroMem(&IpFilter
, sizeof (EFI_PXE_BASE_CODE_IP_FILTER
));
1065 IpFilter
.Filters
= EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
;
1066 This
->SetIpFilter (This
, &IpFilter
);
1073 Used to perform TFTP and MTFTP services.
1075 This function is used to perform TFTP and MTFTP services. This includes the
1076 TFTP operations to get the size of a file, read a directory, read a file, and
1077 write a file. It also includes the MTFTP operations to get the size of a file,
1078 read a directory, and read a file. The type of operation is specified by Operation.
1079 If the callback function that is invoked during the TFTP/MTFTP operation does
1080 not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will
1082 For read operations, the return data will be placed in the buffer specified by
1083 BufferPtr. If BufferSize is too small to contain the entire downloaded file,
1084 then EFI_BUFFER_TOO_SMALL will be returned and BufferSize will be set to zero
1085 or the size of the requested file (the size of the requested file is only returned
1086 if the TFTP server supports TFTP options). If BufferSize is large enough for the
1087 read operation, then BufferSize will be set to the size of the downloaded file,
1088 and EFI_SUCCESS will be returned. Applications using the PxeBc.Mtftp() services
1089 should use the get-file-size operations to determine the size of the downloaded
1090 file prior to using the read-file operations-especially when downloading large
1091 (greater than 64 MB) files-instead of making two calls to the read-file operation.
1092 Following this recommendation will save time if the file is larger than expected
1093 and the TFTP server does not support TFTP option extensions. Without TFTP option
1094 extension support, the client has to download the entire file, counting and discarding
1095 the received packets, to determine the file size.
1096 For write operations, the data to be sent is in the buffer specified by BufferPtr.
1097 BufferSize specifies the number of bytes to send. If the write operation completes
1098 successfully, then EFI_SUCCESS will be returned.
1099 For TFTP "get file size" operations, the size of the requested file or directory
1100 is returned in BufferSize, and EFI_SUCCESS will be returned. If the TFTP server
1101 does not support options, the file will be downloaded into a bit bucket and the
1102 length of the downloaded file will be returned. For MTFTP "get file size" operations,
1103 if the MTFTP server does not support the "get file size" option, EFI_UNSUPPORTED
1105 This function can take up to 10 seconds to timeout and return control to the caller.
1106 If the TFTP sequence does not complete, EFI_TIMEOUT will be returned.
1107 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
1108 then the TFTP sequence is stopped and EFI_ABORTED will be returned.
1109 The format of the data returned from a TFTP read directory operation is a null-terminated
1110 filename followed by a null-terminated information string, of the form
1111 "size year-month-day hour:minute:second" (i.e. %d %d-%d-%d %d:%d:%f - note that
1112 the seconds field can be a decimal number), where the date and time are UTC. For
1113 an MTFTP read directory command, there is additionally a null-terminated multicast
1114 IP address preceding the filename of the form %d.%d.%d.%d for IP v4. The final
1115 entry is itself null-terminated, so that the final information string is terminated
1116 with two null octets.
1118 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1119 @param Operation The type of operation to perform.
1120 @param BufferPtr A pointer to the data buffer.
1121 @param Overwrite Only used on write file operations. TRUE if a file on a remote server can
1123 @param BufferSize For get-file-size operations, *BufferSize returns the size of the
1125 @param BlockSize The requested block size to be used during a TFTP transfer.
1126 @param ServerIp The TFTP / MTFTP server IP address.
1127 @param Filename A Null-terminated ASCII string that specifies a directory name or a file
1129 @param Info Pointer to the MTFTP information.
1130 @param DontUseBuffer Set to FALSE for normal TFTP and MTFTP read file operation.
1132 @retval EFI_SUCCESS The TFTP/MTFTP operation was completed.
1133 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1134 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1135 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1136 @retval EFI_BUFFER_TOO_SMALL The buffer is not large enough to complete the read operation.
1137 @retval EFI_ABORTED The callback function aborted the TFTP/MTFTP operation.
1138 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.
1139 @retval EFI_ICMP_ERROR An ICMP error packet was received during the MTFTP session.
1140 @retval EFI_TFTP_ERROR A TFTP error packet was received during the MTFTP session.
1146 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1147 IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation
,
1148 IN OUT VOID
*BufferPtr
,
1149 IN BOOLEAN Overwrite
,
1150 IN OUT UINT64
*BufferSize
,
1151 IN UINTN
*BlockSize OPTIONAL
,
1152 IN EFI_IP_ADDRESS
*ServerIp
,
1154 IN EFI_PXE_BASE_CODE_MTFTP_INFO
*Info OPTIONAL
,
1155 IN BOOLEAN DontUseBuffer
1158 PXEBC_PRIVATE_DATA
*Private
;
1159 EFI_MTFTP4_CONFIG_DATA Mtftp4Config
;
1161 EFI_PXE_BASE_CODE_MODE
*Mode
;
1162 EFI_MAC_ADDRESS TempMacAddr
;
1163 EFI_PXE_BASE_CODE_IP_FILTER IpFilter
;
1165 if ((This
== NULL
) ||
1166 (Filename
== NULL
) ||
1167 (BufferSize
== NULL
) ||
1168 ((ServerIp
== NULL
) ||
1169 (IP4_IS_UNSPECIFIED (NTOHL (ServerIp
->Addr
[0])) ||
1170 IP4_IS_LOCAL_BROADCAST (NTOHL (ServerIp
->Addr
[0])))) ||
1171 ((BufferPtr
== NULL
) && DontUseBuffer
) ||
1172 ((BlockSize
!= NULL
) && (*BlockSize
< 512))) {
1174 return EFI_INVALID_PARAMETER
;
1177 Status
= EFI_DEVICE_ERROR
;
1178 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1179 Mode
= &Private
->Mode
;
1181 if (!Mode
->AutoArp
) {
1183 // If AutoArp is set false, check arp cache
1185 UpdateArpCache (This
);
1186 if (!FindInArpCache (Mode
, &ServerIp
->v4
, &TempMacAddr
)) {
1187 return EFI_DEVICE_ERROR
;
1192 // Stop Udp4Read instance
1194 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
1196 Mode
->TftpErrorReceived
= FALSE
;
1197 Mode
->IcmpErrorReceived
= FALSE
;
1199 Mtftp4Config
.UseDefaultSetting
= FALSE
;
1200 Mtftp4Config
.TimeoutValue
= PXEBC_MTFTP_TIMEOUT
;
1201 Mtftp4Config
.TryCount
= PXEBC_MTFTP_RETRIES
;
1204 &Mtftp4Config
.StationIp
,
1205 &Private
->StationIp
,
1206 sizeof (EFI_IPv4_ADDRESS
)
1209 &Mtftp4Config
.SubnetMask
,
1210 &Private
->SubnetMask
,
1211 sizeof (EFI_IPv4_ADDRESS
)
1214 &Mtftp4Config
.GatewayIp
,
1215 &Private
->GatewayIp
,
1216 sizeof (EFI_IPv4_ADDRESS
)
1219 &Mtftp4Config
.ServerIp
,
1221 sizeof (EFI_IPv4_ADDRESS
)
1224 switch (Operation
) {
1226 case EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE
:
1228 Status
= PxeBcTftpGetFileSize (
1238 case EFI_PXE_BASE_CODE_TFTP_READ_FILE
:
1240 Status
= PxeBcTftpReadFile (
1252 case EFI_PXE_BASE_CODE_TFTP_WRITE_FILE
:
1254 Status
= PxeBcTftpWriteFile (
1266 case EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY
:
1268 Status
= PxeBcTftpReadDirectory (
1280 case EFI_PXE_BASE_CODE_MTFTP_GET_FILE_SIZE
:
1281 case EFI_PXE_BASE_CODE_MTFTP_READ_FILE
:
1282 case EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY
:
1283 Status
= EFI_UNSUPPORTED
;
1288 Status
= EFI_INVALID_PARAMETER
;
1292 if (Status
== EFI_ICMP_ERROR
) {
1293 Mode
->IcmpErrorReceived
= TRUE
;
1296 if (EFI_ERROR (Status
)) {
1301 Private
->Udp4Read
->Configure (Private
->Udp4Read
, &Private
->Udp4CfgData
);
1303 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
1304 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
1306 ZeroMem(&IpFilter
, sizeof (EFI_PXE_BASE_CODE_IP_FILTER
));
1307 IpFilter
.Filters
= EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
;
1308 This
->SetIpFilter (This
, &IpFilter
);
1315 Writes a UDP packet to the network interface.
1317 This function writes a UDP packet specified by the (optional HeaderPtr and)
1318 BufferPtr parameters to the network interface. The UDP header is automatically
1319 built by this routine. It uses the parameters OpFlags, DestIp, DestPort, GatewayIp,
1320 SrcIp, and SrcPort to build this header. If the packet is successfully built and
1321 transmitted through the network interface, then EFI_SUCCESS will be returned.
1322 If a timeout occurs during the transmission of the packet, then EFI_TIMEOUT will
1323 be returned. If an ICMP error occurs during the transmission of the packet, then
1324 the IcmpErrorReceived field is set to TRUE, the IcmpError field is filled in and
1325 EFI_ICMP_ERROR will be returned. If the Callback Protocol does not return
1326 EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will be returned.
1328 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1329 @param OpFlags The UDP operation flags.
1330 @param DestIp The destination IP address.
1331 @param DestPort The destination UDP port number.
1332 @param GatewayIp The gateway IP address.
1333 @param SrcIp The source IP address.
1334 @param SrcPort The source UDP port number.
1335 @param HeaderSize An optional field which may be set to the length of a header at
1336 HeaderPtr to be prefixed to the data at BufferPtr.
1337 @param HeaderPtr If HeaderSize is not NULL, a pointer to a header to be prefixed to the
1339 @param BufferSize A pointer to the size of the data at BufferPtr.
1340 @param BufferPtr A pointer to the data to be written.
1342 @retval EFI_SUCCESS The UDP Write operation was completed.
1343 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1344 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1345 @retval EFI_BAD_BUFFER_SIZE The buffer is too long to be transmitted.
1346 @retval EFI_ABORTED The callback function aborted the UDP Write operation.
1347 @retval EFI_TIMEOUT The UDP Write operation timed out.
1348 @retval EFI_ICMP_ERROR An ICMP error packet was received during the UDP write session.
1354 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1356 IN EFI_IP_ADDRESS
*DestIp
,
1357 IN EFI_PXE_BASE_CODE_UDP_PORT
*DestPort
,
1358 IN EFI_IP_ADDRESS
*GatewayIp OPTIONAL
,
1359 IN EFI_IP_ADDRESS
*SrcIp OPTIONAL
,
1360 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPort OPTIONAL
,
1361 IN UINTN
*HeaderSize OPTIONAL
,
1362 IN VOID
*HeaderPtr OPTIONAL
,
1363 IN UINTN
*BufferSize
,
1367 PXEBC_PRIVATE_DATA
*Private
;
1368 EFI_UDP4_PROTOCOL
*Udp4
;
1369 EFI_UDP4_COMPLETION_TOKEN Token
;
1370 EFI_UDP4_TRANSMIT_DATA
*Udp4TxData
;
1373 EFI_UDP4_SESSION_DATA Udp4Session
;
1376 EFI_PXE_BASE_CODE_MODE
*Mode
;
1377 EFI_MAC_ADDRESS TempMacAddr
;
1381 if ((This
== NULL
) || (DestIp
== NULL
) || (DestPort
== NULL
)) {
1382 return EFI_INVALID_PARAMETER
;
1385 if ((GatewayIp
!= NULL
) && (IP4_IS_UNSPECIFIED (NTOHL (GatewayIp
->Addr
[0])) || IP4_IS_LOCAL_BROADCAST (NTOHL (GatewayIp
->Addr
[0])))) {
1387 // Gateway is provided but it's not a unicast IP address.
1389 return EFI_INVALID_PARAMETER
;
1392 if ((HeaderSize
!= NULL
) && ((*HeaderSize
== 0) || (HeaderPtr
== NULL
))) {
1394 // The HeaderSize ptr isn't NULL and: 1. the value is zero; or 2. the HeaderPtr
1397 return EFI_INVALID_PARAMETER
;
1400 if ((BufferSize
== NULL
) || ((*BufferSize
!= 0) && (BufferPtr
== NULL
))) {
1401 return EFI_INVALID_PARAMETER
;
1404 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1405 Udp4
= Private
->Udp4Write
;
1406 Mode
= &Private
->Mode
;
1407 if (!Mode
->Started
) {
1408 return EFI_NOT_STARTED
;
1411 if (!Private
->AddressIsOk
&& (SrcIp
== NULL
)) {
1412 return EFI_INVALID_PARAMETER
;
1415 if (!Mode
->AutoArp
) {
1417 // If AutoArp is set false, check arp cache
1419 UpdateArpCache (This
);
1420 if (!FindInArpCache (Mode
, &DestIp
->v4
, &TempMacAddr
)) {
1421 return EFI_DEVICE_ERROR
;
1425 Mode
->IcmpErrorReceived
= FALSE
;
1427 if ((Private
->CurrentUdpSrcPort
== 0) ||
1428 ((SrcPort
!= NULL
) && (*SrcPort
!= Private
->CurrentUdpSrcPort
))) {
1430 // Port is changed, (re)configure the Udp4Write instance
1432 if (SrcPort
!= NULL
) {
1433 Private
->CurrentUdpSrcPort
= *SrcPort
;
1437 Status
= PxeBcConfigureUdpWriteInstance (
1439 &Private
->StationIp
.v4
,
1440 &Private
->SubnetMask
.v4
,
1441 &Private
->GatewayIp
.v4
,
1442 &Private
->CurrentUdpSrcPort
,
1446 if (EFI_ERROR (Status
)) {
1447 Private
->CurrentUdpSrcPort
= 0;
1448 return EFI_INVALID_PARAMETER
;
1451 ZeroMem (&Token
, sizeof (EFI_UDP4_COMPLETION_TOKEN
));
1452 ZeroMem (&Udp4Session
, sizeof (EFI_UDP4_SESSION_DATA
));
1454 CopyMem (&Udp4Session
.DestinationAddress
, DestIp
, sizeof (EFI_IPv4_ADDRESS
));
1455 Udp4Session
.DestinationPort
= *DestPort
;
1456 if (SrcIp
!= NULL
) {
1457 CopyMem (&Udp4Session
.SourceAddress
, SrcIp
, sizeof (EFI_IPv4_ADDRESS
));
1459 if (SrcPort
!= NULL
) {
1460 Udp4Session
.SourcePort
= *SrcPort
;
1463 FragCount
= (HeaderSize
!= NULL
) ? 2 : 1;
1464 Udp4TxData
= (EFI_UDP4_TRANSMIT_DATA
*) AllocateZeroPool (sizeof (EFI_UDP4_TRANSMIT_DATA
) + (FragCount
- 1) * sizeof (EFI_UDP4_FRAGMENT_DATA
));
1465 if (Udp4TxData
== NULL
) {
1466 return EFI_OUT_OF_RESOURCES
;
1469 Udp4TxData
->FragmentCount
= FragCount
;
1470 Udp4TxData
->FragmentTable
[FragCount
- 1].FragmentLength
= (UINT32
) *BufferSize
;
1471 Udp4TxData
->FragmentTable
[FragCount
- 1].FragmentBuffer
= BufferPtr
;
1472 DataLength
= (UINT32
) *BufferSize
;
1474 if (FragCount
== 2) {
1476 Udp4TxData
->FragmentTable
[0].FragmentLength
= (UINT32
) *HeaderSize
;
1477 Udp4TxData
->FragmentTable
[0].FragmentBuffer
= HeaderPtr
;
1478 DataLength
+= (UINT32
) *HeaderSize
;
1481 if (GatewayIp
!= NULL
) {
1482 Udp4TxData
->GatewayAddress
= (EFI_IPv4_ADDRESS
*) GatewayIp
;
1484 Udp4TxData
->UdpSessionData
= &Udp4Session
;
1485 Udp4TxData
->DataLength
= DataLength
;
1486 Token
.Packet
.TxData
= Udp4TxData
;
1488 Status
= gBS
->CreateEvent (
1495 if (EFI_ERROR (Status
)) {
1499 Status
= Udp4
->Transmit (Udp4
, &Token
);
1500 if (EFI_ERROR (Status
)) {
1501 if (Status
== EFI_ICMP_ERROR
) {
1502 Mode
->IcmpErrorReceived
= TRUE
;
1512 Status
= Token
.Status
;
1516 if (Token
.Event
!= NULL
) {
1517 gBS
->CloseEvent (Token
.Event
);
1520 FreePool (Udp4TxData
);
1523 // Reset the instance.
1525 Udp4
->Configure (Udp4
, NULL
);
1530 Decide whether the incoming UDP packet is acceptable per IP filter settings
1531 in provided PxeBcMode.
1533 @param PxeBcMode Pointer to EFI_PXE_BASE_CODE_MODE.
1534 @param Session Received UDP session.
1536 @retval TRUE The UDP package matches IP filters.
1537 @retval FALSE The UDP package doesn't matches IP filters.
1542 IN EFI_PXE_BASE_CODE_MODE
*PxeBcMode
,
1543 IN EFI_UDP4_SESSION_DATA
*Session
1547 EFI_IPv4_ADDRESS Ip4Address
;
1548 EFI_IPv4_ADDRESS DestIp4Address
;
1550 if ((PxeBcMode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS
) != 0) {
1554 CopyMem (&DestIp4Address
, &Session
->DestinationAddress
, sizeof (DestIp4Address
));
1555 if (((PxeBcMode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST
) != 0) &&
1556 IP4_IS_MULTICAST (EFI_NTOHL (DestIp4Address
))
1561 if (((PxeBcMode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST
) != 0) &&
1562 IP4_IS_LOCAL_BROADCAST (EFI_NTOHL (DestIp4Address
))
1567 CopyMem (&Ip4Address
, &PxeBcMode
->StationIp
.v4
, sizeof (Ip4Address
));
1568 if (((PxeBcMode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
) != 0) &&
1569 EFI_IP4_EQUAL (&Ip4Address
, &DestIp4Address
)
1574 ASSERT (PxeBcMode
->IpFilter
.IpCnt
< EFI_PXE_BASE_CODE_MAX_IPCNT
);
1576 for (Index
= 0; Index
< PxeBcMode
->IpFilter
.IpCnt
; Index
++) {
1579 &PxeBcMode
->IpFilter
.IpList
[Index
].v4
,
1582 if (EFI_IP4_EQUAL (&Ip4Address
, &DestIp4Address
)) {
1591 Reads a UDP packet from the network interface.
1593 This function reads a UDP packet from a network interface. The data contents
1594 are returned in (the optional HeaderPtr and) BufferPtr, and the size of the
1595 buffer received is returned in BufferSize . If the input BufferSize is smaller
1596 than the UDP packet received (less optional HeaderSize), it will be set to the
1597 required size, and EFI_BUFFER_TOO_SMALL will be returned. In this case, the
1598 contents of BufferPtr are undefined, and the packet is lost. If a UDP packet is
1599 successfully received, then EFI_SUCCESS will be returned, and the information
1600 from the UDP header will be returned in DestIp, DestPort, SrcIp, and SrcPort if
1601 they are not NULL. Depending on the values of OpFlags and the DestIp, DestPort,
1602 SrcIp, and SrcPort input values, different types of UDP packet receive filtering
1603 will be performed. The following tables summarize these receive filter operations.
1605 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1606 @param OpFlags The UDP operation flags.
1607 @param DestIp The destination IP address.
1608 @param DestPort The destination UDP port number.
1609 @param SrcIp The source IP address.
1610 @param SrcPort The source UDP port number.
1611 @param HeaderSize An optional field which may be set to the length of a header at
1612 HeaderPtr to be prefixed to the data at BufferPtr.
1613 @param HeaderPtr If HeaderSize is not NULL, a pointer to a header to be prefixed to the
1615 @param BufferSize A pointer to the size of the data at BufferPtr.
1616 @param BufferPtr A pointer to the data to be read.
1618 @retval EFI_SUCCESS The UDP Read operation was completed.
1619 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1620 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1621 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1622 @retval EFI_BUFFER_TOO_SMALL The packet is larger than Buffer can hold.
1623 @retval EFI_ABORTED The callback function aborted the UDP Read operation.
1624 @retval EFI_TIMEOUT The UDP Read operation timed out.
1630 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1632 IN OUT EFI_IP_ADDRESS
*DestIp OPTIONAL
,
1633 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*DestPort OPTIONAL
,
1634 IN OUT EFI_IP_ADDRESS
*SrcIp OPTIONAL
,
1635 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPort OPTIONAL
,
1636 IN UINTN
*HeaderSize OPTIONAL
,
1637 IN VOID
*HeaderPtr OPTIONAL
,
1638 IN OUT UINTN
*BufferSize
,
1642 PXEBC_PRIVATE_DATA
*Private
;
1643 EFI_PXE_BASE_CODE_MODE
*Mode
;
1644 EFI_UDP4_PROTOCOL
*Udp4
;
1645 EFI_UDP4_COMPLETION_TOKEN Token
;
1646 EFI_UDP4_RECEIVE_DATA
*RxData
;
1647 EFI_UDP4_SESSION_DATA
*Session
;
1653 UINTN HeaderCopiedLen
;
1654 UINTN BufferCopiedLen
;
1655 UINT32 FragmentLength
;
1656 UINTN FragmentIndex
;
1657 UINT8
*FragmentBuffer
;
1659 if (This
== NULL
|| DestIp
== NULL
|| DestPort
== NULL
) {
1660 return EFI_INVALID_PARAMETER
;
1663 if (((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT
) == 0 && (DestPort
== NULL
)) ||
1664 ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT
) == 0 && (SrcIp
== NULL
)) ||
1665 ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT
) == 0 && (SrcPort
== NULL
))) {
1666 return EFI_INVALID_PARAMETER
;
1669 if (((HeaderSize
!= NULL
) && (*HeaderSize
== 0)) || ((HeaderSize
!= NULL
) && (HeaderPtr
== NULL
))) {
1670 return EFI_INVALID_PARAMETER
;
1673 if ((BufferSize
== NULL
) || (BufferPtr
== NULL
)) {
1674 return EFI_INVALID_PARAMETER
;
1677 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1678 Mode
= Private
->PxeBc
.Mode
;
1679 Udp4
= Private
->Udp4Read
;
1681 if (!Mode
->Started
) {
1682 return EFI_NOT_STARTED
;
1685 Mode
->IcmpErrorReceived
= FALSE
;
1687 Status
= gBS
->CreateEvent (
1694 if (EFI_ERROR (Status
)) {
1695 return EFI_OUT_OF_RESOURCES
;
1701 Status
= Udp4
->Receive (Udp4
, &Token
);
1702 if (EFI_ERROR (Status
)) {
1703 if (Status
== EFI_ICMP_ERROR
) {
1704 Mode
->IcmpErrorReceived
= TRUE
;
1712 Status
= EFI_TIMEOUT
;
1716 // check whether this packet matches the filters
1718 if (EFI_ERROR (Token
.Status
)){
1722 RxData
= Token
.Packet
.RxData
;
1723 Session
= &RxData
->UdpSession
;
1727 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER
) != 0) {
1730 // Check UDP package by IP filter settings
1732 if (CheckIpByFilter (Mode
, Session
)) {
1741 // Match the destination ip of the received udp dgram
1743 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP
) != 0) {
1746 if (DestIp
!= NULL
) {
1747 CopyMem (DestIp
, &Session
->DestinationAddress
, sizeof (EFI_IPv4_ADDRESS
));
1750 if (DestIp
!= NULL
) {
1751 if (EFI_IP4_EQUAL (DestIp
, &Session
->DestinationAddress
)) {
1755 if (EFI_IP4_EQUAL (&Private
->StationIp
, &Session
->DestinationAddress
)) {
1764 // Match the destination port of the received udp dgram
1766 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT
) != 0) {
1768 if (DestPort
!= NULL
) {
1769 *DestPort
= Session
->DestinationPort
;
1773 if (*DestPort
!= Session
->DestinationPort
) {
1781 // Match the source ip of the received udp dgram
1783 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP
) != 0) {
1785 if (SrcIp
!= NULL
) {
1786 CopyMem (SrcIp
, &Session
->SourceAddress
, sizeof (EFI_IPv4_ADDRESS
));
1790 if (!EFI_IP4_EQUAL (SrcIp
, &Session
->SourceAddress
)) {
1798 // Match the source port of the received udp dgram
1800 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT
) != 0) {
1802 if (SrcPort
!= NULL
) {
1803 *SrcPort
= Session
->SourcePort
;
1807 if (*SrcPort
!= Session
->SourcePort
) {
1814 ASSERT (RxData
!= NULL
);
1817 if (HeaderSize
!= NULL
) {
1818 HeaderLen
= MIN (*HeaderSize
, RxData
->DataLength
);
1821 if (RxData
->DataLength
- HeaderLen
> *BufferSize
) {
1822 Status
= EFI_BUFFER_TOO_SMALL
;
1824 *HeaderSize
= HeaderLen
;
1825 *BufferSize
= RxData
->DataLength
- HeaderLen
;
1827 HeaderCopiedLen
= 0;
1828 BufferCopiedLen
= 0;
1829 for (FragmentIndex
= 0; FragmentIndex
< RxData
->FragmentCount
; FragmentIndex
++) {
1830 FragmentLength
= RxData
->FragmentTable
[FragmentIndex
].FragmentLength
;
1831 FragmentBuffer
= RxData
->FragmentTable
[FragmentIndex
].FragmentBuffer
;
1832 if (HeaderCopiedLen
+ FragmentLength
< HeaderLen
) {
1834 // Copy the header part of received data.
1836 CopyMem ((UINT8
*) HeaderPtr
+ HeaderCopiedLen
, FragmentBuffer
, FragmentLength
);
1837 HeaderCopiedLen
+= FragmentLength
;
1838 } else if (HeaderCopiedLen
< HeaderLen
) {
1840 // Copy the header part of received data.
1842 CopiedLen
= HeaderLen
- HeaderCopiedLen
;
1843 CopyMem ((UINT8
*) HeaderPtr
+ HeaderCopiedLen
, FragmentBuffer
, CopiedLen
);
1844 HeaderCopiedLen
+= CopiedLen
;
1847 // Copy the other part of received data.
1849 CopyMem ((UINT8
*) BufferPtr
+ BufferCopiedLen
, FragmentBuffer
+ CopiedLen
, FragmentLength
- CopiedLen
);
1850 BufferCopiedLen
+= (FragmentLength
- CopiedLen
);
1853 // Copy the other part of received data.
1855 CopyMem ((UINT8
*) BufferPtr
+ BufferCopiedLen
, FragmentBuffer
, FragmentLength
);
1856 BufferCopiedLen
+= FragmentLength
;
1862 Status
= EFI_TIMEOUT
;
1866 // Recycle the RxData
1868 gBS
->SignalEvent (RxData
->RecycleSignal
);
1877 Udp4
->Cancel (Udp4
, &Token
);
1879 gBS
->CloseEvent (Token
.Event
);
1885 Updates the IP receive filters of a network device and enables software filtering.
1887 The NewFilter field is used to modify the network device's current IP receive
1888 filter settings and to enable a software filter. This function updates the IpFilter
1889 field of the EFI_PXE_BASE_CODE_MODE structure with the contents of NewIpFilter.
1890 The software filter is used when the USE_FILTER in OpFlags is set to UdpRead().
1891 The current hardware filter remains in effect no matter what the settings of OpFlags
1892 are, so that the meaning of ANY_DEST_IP set in OpFlags to UdpRead() is from those
1893 packets whose reception is enabled in hardware-physical NIC address (unicast),
1894 broadcast address, logical address or addresses (multicast), or all (promiscuous).
1895 UdpRead() does not modify the IP filter settings.
1896 Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP receive
1897 filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
1898 If an application or driver wishes to preserve the IP receive filter settings,
1899 it will have to preserve the IP receive filter settings before these calls, and
1900 use SetIpFilter() to restore them after the calls. If incompatible filtering is
1901 requested (for example, PROMISCUOUS with anything else) or if the device does not
1902 support a requested filter setting and it cannot be accommodated in software
1903 (for example, PROMISCUOUS not supported), EFI_INVALID_PARAMETER will be returned.
1904 The IPlist field is used to enable IPs other than the StationIP. They may be
1905 multicast or unicast. If IPcnt is set as well as EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP,
1906 then both the StationIP and the IPs from the IPlist will be used.
1908 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1909 @param NewFilter Pointer to the new set of IP receive filters.
1911 @retval EFI_SUCCESS The IP receive filter settings were updated.
1912 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1913 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1918 EfiPxeBcSetIpFilter (
1919 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1920 IN EFI_PXE_BASE_CODE_IP_FILTER
*NewFilter
1924 PXEBC_PRIVATE_DATA
*Private
;
1925 EFI_PXE_BASE_CODE_MODE
*Mode
;
1927 EFI_UDP4_CONFIG_DATA
*Udp4Cfg
;
1928 BOOLEAN PromiscuousNeed
;
1929 BOOLEAN AcceptPromiscuous
;
1930 BOOLEAN AcceptBroadcast
;
1931 BOOLEAN MultiCastUpdate
;
1934 DEBUG ((EFI_D_ERROR
, "This == NULL.\n"));
1935 return EFI_INVALID_PARAMETER
;
1938 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1939 Mode
= Private
->PxeBc
.Mode
;
1941 if (NewFilter
== NULL
) {
1942 DEBUG ((EFI_D_ERROR
, "NewFilter == NULL.\n"));
1943 return EFI_INVALID_PARAMETER
;
1946 if (NewFilter
->IpCnt
> EFI_PXE_BASE_CODE_MAX_IPCNT
) {
1947 DEBUG ((EFI_D_ERROR
, "NewFilter->IpCnt > %d.\n", EFI_PXE_BASE_CODE_MAX_IPCNT
));
1948 return EFI_INVALID_PARAMETER
;
1951 if (!Mode
->Started
) {
1952 DEBUG ((EFI_D_ERROR
, "BC was not started.\n"));
1953 return EFI_NOT_STARTED
;
1956 if (Mode
->UsingIpv6
) {
1957 DEBUG ((EFI_D_ERROR
, "This driver is PXE for IPv4 Only.\n"));
1958 return EFI_INVALID_PARAMETER
;
1961 PromiscuousNeed
= FALSE
;
1963 for (Index
= 0; Index
< NewFilter
->IpCnt
; ++Index
) {
1964 if (IP4_IS_LOCAL_BROADCAST (EFI_IP4 (NewFilter
->IpList
[Index
].v4
))) {
1966 // The IP is a broadcast address.
1968 DEBUG ((EFI_D_ERROR
, "There is broadcast address in NewFilter.\n"));
1969 return EFI_INVALID_PARAMETER
;
1971 if ((EFI_NTOHL(Mode
->StationIp
) != 0) &&
1972 (EFI_NTOHL(Mode
->SubnetMask
) != 0) &&
1973 IP4_NET_EQUAL(EFI_NTOHL(Mode
->StationIp
), EFI_NTOHL(NewFilter
->IpList
[Index
].v4
), EFI_NTOHL(Mode
->SubnetMask
)) &&
1974 NetIp4IsUnicast (EFI_IP4 (NewFilter
->IpList
[Index
].v4
), EFI_NTOHL(Mode
->SubnetMask
)) &&
1975 ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
) != 0)) {
1977 // If EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP is set and IP4 address is in IpList,
1978 // promiscuous mode is needed.
1980 PromiscuousNeed
= TRUE
;
1984 AcceptPromiscuous
= FALSE
;
1985 AcceptBroadcast
= FALSE
;
1986 MultiCastUpdate
= FALSE
;
1988 if (PromiscuousNeed
||
1989 ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS
) != 0) ||
1990 ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST
) != 0)
1993 // Configure the udp4 filter to receive all packages.
1995 AcceptPromiscuous
= TRUE
;
1996 } else if ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST
) != 0) {
1998 // Configure the udp4 filter to receive all broadcast packages.
2000 AcceptBroadcast
= TRUE
;
2004 // In multicast condition when Promiscuous FALSE and IpCnt no-zero.
2005 // Here check if there is any update of the multicast ip address. If yes,
2006 // we need leave the old multicast group (by Config UDP instance to NULL),
2007 // and join the new multicast group.
2009 if (!AcceptPromiscuous
) {
2010 if ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
) != 0) {
2011 if (Mode
->IpFilter
.IpCnt
!= NewFilter
->IpCnt
) {
2012 MultiCastUpdate
= TRUE
;
2013 } else if (CompareMem (Mode
->IpFilter
.IpList
, NewFilter
->IpList
, NewFilter
->IpCnt
* sizeof (EFI_IP_ADDRESS
)) != 0 ) {
2014 MultiCastUpdate
= TRUE
;
2020 // Check whether we need reconfigure the UDP instance.
2022 Udp4Cfg
= &Private
->Udp4CfgData
;
2023 if ((AcceptPromiscuous
!= Udp4Cfg
->AcceptPromiscuous
) ||
2024 (AcceptBroadcast
!= Udp4Cfg
->AcceptBroadcast
) || MultiCastUpdate
) {
2026 // Clear the UDP instance configuration, all joined groups will be left
2027 // during the operation.
2029 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
2032 // Configure the UDP instance with the new configuration.
2034 Udp4Cfg
->AcceptPromiscuous
= AcceptPromiscuous
;
2035 Udp4Cfg
->AcceptBroadcast
= AcceptBroadcast
;
2036 Status
= Private
->Udp4Read
->Configure (Private
->Udp4Read
, Udp4Cfg
);
2037 if (EFI_ERROR (Status
)) {
2042 // In not Promiscuous mode, need to join the new multicast group.
2044 if (!AcceptPromiscuous
) {
2045 for (Index
= 0; Index
< NewFilter
->IpCnt
; ++Index
) {
2046 if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter
->IpList
[Index
].v4
))) {
2048 // Join the mutilcast group.
2050 Status
= Private
->Udp4Read
->Groups (Private
->Udp4Read
, TRUE
, &NewFilter
->IpList
[Index
].v4
);
2051 if (EFI_ERROR (Status
)) {
2061 // Save the new filter.
2063 CopyMem (&Mode
->IpFilter
, NewFilter
, sizeof (Mode
->IpFilter
));
2070 Uses the ARP protocol to resolve a MAC address.
2072 This function uses the ARP protocol to resolve a MAC address. The UsingIpv6 field
2073 of the EFI_PXE_BASE_CODE_MODE structure is used to determine if IPv4 or IPv6
2074 addresses are being used. The IP address specified by IpAddr is used to resolve
2075 a MAC address. If the ARP protocol succeeds in resolving the specified address,
2076 then the ArpCacheEntries and ArpCache fields of the EFI_PXE_BASE_CODE_MODE structure
2077 are updated, and EFI_SUCCESS is returned. If MacAddr is not NULL, the resolved
2078 MAC address is placed there as well. If the PXE Base Code protocol is in the
2079 stopped state, then EFI_NOT_STARTED is returned. If the ARP protocol encounters
2080 a timeout condition while attempting to resolve an address, then EFI_TIMEOUT is
2081 returned. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
2082 then EFI_ABORTED is returned.
2084 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2085 @param IpAddr Pointer to the IP address that is used to resolve a MAC address.
2086 @param MacAddr If not NULL, a pointer to the MAC address that was resolved with the
2089 @retval EFI_SUCCESS The IP or MAC address was resolved.
2090 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2091 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
2092 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
2093 @retval EFI_ICMP_ERROR Something error occur with the ICMP packet message.
2099 IN EFI_PXE_BASE_CODE_PROTOCOL
* This
,
2100 IN EFI_IP_ADDRESS
* IpAddr
,
2101 IN EFI_MAC_ADDRESS
* MacAddr OPTIONAL
2104 PXEBC_PRIVATE_DATA
*Private
;
2105 EFI_PXE_BASE_CODE_MODE
*Mode
;
2107 EFI_MAC_ADDRESS TempMacAddr
;
2109 if (This
== NULL
|| IpAddr
== NULL
) {
2110 return EFI_INVALID_PARAMETER
;
2113 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
2114 Mode
= Private
->PxeBc
.Mode
;
2116 if (!Mode
->Started
) {
2117 return EFI_NOT_STARTED
;
2120 if (!Private
->AddressIsOk
|| Mode
->UsingIpv6
) {
2122 // We can't resolve the IP address if we don't have a local address now.
2123 // Don't have ARP for IPv6.
2125 return EFI_INVALID_PARAMETER
;
2128 Mode
->IcmpErrorReceived
= FALSE
;
2130 if (!Mode
->AutoArp
) {
2132 // If AutoArp is set false, check arp cache
2134 UpdateArpCache (This
);
2135 if (!FindInArpCache (Mode
, &IpAddr
->v4
, &TempMacAddr
)) {
2136 return EFI_DEVICE_ERROR
;
2139 Status
= Private
->Arp
->Request (Private
->Arp
, &IpAddr
->v4
, NULL
, &TempMacAddr
);
2140 if (EFI_ERROR (Status
)) {
2141 if (Status
== EFI_ICMP_ERROR
) {
2142 Mode
->IcmpErrorReceived
= TRUE
;
2148 if (MacAddr
!= NULL
) {
2149 CopyMem (MacAddr
, &TempMacAddr
, sizeof (EFI_MAC_ADDRESS
));
2156 Updates the parameters that affect the operation of the PXE Base Code Protocol.
2158 This function sets parameters that affect the operation of the PXE Base Code Protocol.
2159 The parameter specified by NewAutoArp is used to control the generation of ARP
2160 protocol packets. If NewAutoArp is TRUE, then ARP Protocol packets will be generated
2161 as required by the PXE Base Code Protocol. If NewAutoArp is FALSE, then no ARP
2162 Protocol packets will be generated. In this case, the only mappings that are
2163 available are those stored in the ArpCache of the EFI_PXE_BASE_CODE_MODE structure.
2164 If there are not enough mappings in the ArpCache to perform a PXE Base Code Protocol
2165 service, then the service will fail. This function updates the AutoArp field of
2166 the EFI_PXE_BASE_CODE_MODE structure to NewAutoArp.
2167 The SetParameters() call must be invoked after a Callback Protocol is installed
2168 to enable the use of callbacks.
2170 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2171 @param NewAutoArp If not NULL, a pointer to a value that specifies whether to replace the
2172 current value of AutoARP.
2173 @param NewSendGUID If not NULL, a pointer to a value that specifies whether to replace the
2174 current value of SendGUID.
2175 @param NewTTL If not NULL, a pointer to be used in place of the current value of TTL,
2176 the "time to live" field of the IP header.
2177 @param NewToS If not NULL, a pointer to be used in place of the current value of ToS,
2178 the "type of service" field of the IP header.
2179 @param NewMakeCallback If not NULL, a pointer to a value that specifies whether to replace the
2180 current value of the MakeCallback field of the Mode structure.
2182 @retval EFI_SUCCESS The new parameters values were updated.
2183 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2184 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
2189 EfiPxeBcSetParameters (
2190 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
2191 IN BOOLEAN
*NewAutoArp OPTIONAL
,
2192 IN BOOLEAN
*NewSendGUID OPTIONAL
,
2193 IN UINT8
*NewTTL OPTIONAL
,
2194 IN UINT8
*NewToS OPTIONAL
,
2195 IN BOOLEAN
*NewMakeCallback
// OPTIONAL
2198 PXEBC_PRIVATE_DATA
*Private
;
2199 EFI_PXE_BASE_CODE_MODE
*Mode
;
2202 Status
= EFI_SUCCESS
;
2205 Status
= EFI_INVALID_PARAMETER
;
2209 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
2210 Mode
= Private
->PxeBc
.Mode
;
2212 if (NewSendGUID
!= NULL
&& *NewSendGUID
) {
2214 // FixMe, cann't locate SendGuid
2218 if (NewMakeCallback
!= NULL
&& *NewMakeCallback
) {
2220 Status
= gBS
->HandleProtocol (
2221 Private
->Controller
,
2222 &gEfiPxeBaseCodeCallbackProtocolGuid
,
2223 (VOID
**) &Private
->PxeBcCallback
2225 if (EFI_ERROR (Status
) || (Private
->PxeBcCallback
->Callback
== NULL
)) {
2227 Status
= EFI_INVALID_PARAMETER
;
2232 if (!Mode
->Started
) {
2233 Status
= EFI_NOT_STARTED
;
2237 if (NewMakeCallback
!= NULL
) {
2239 if (*NewMakeCallback
) {
2241 // Update the Callback protocol.
2243 Status
= gBS
->HandleProtocol (
2244 Private
->Controller
,
2245 &gEfiPxeBaseCodeCallbackProtocolGuid
,
2246 (VOID
**) &Private
->PxeBcCallback
2249 if (EFI_ERROR (Status
) || (Private
->PxeBcCallback
->Callback
== NULL
)) {
2250 Status
= EFI_INVALID_PARAMETER
;
2254 Private
->PxeBcCallback
= NULL
;
2257 Mode
->MakeCallbacks
= *NewMakeCallback
;
2260 if (NewAutoArp
!= NULL
) {
2261 Mode
->AutoArp
= *NewAutoArp
;
2264 if (NewSendGUID
!= NULL
) {
2265 Mode
->SendGUID
= *NewSendGUID
;
2268 if (NewTTL
!= NULL
) {
2269 Mode
->TTL
= *NewTTL
;
2272 if (NewToS
!= NULL
) {
2273 Mode
->ToS
= *NewToS
;
2281 Updates the station IP address and/or subnet mask values of a network device.
2283 This function updates the station IP address and/or subnet mask values of a network
2284 device. The NewStationIp field is used to modify the network device's current IP address.
2285 If NewStationIP is NULL, then the current IP address will not be modified. Otherwise,
2286 this function updates the StationIp field of the EFI_PXE_BASE_CODE_MODE structure
2287 with NewStationIp. The NewSubnetMask field is used to modify the network device's current subnet
2288 mask. If NewSubnetMask is NULL, then the current subnet mask will not be modified.
2289 Otherwise, this function updates the SubnetMask field of the EFI_PXE_BASE_CODE_MODE
2290 structure with NewSubnetMask.
2292 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2293 @param NewStationIp Pointer to the new IP address to be used by the network device.
2294 @param NewSubnetMask Pointer to the new subnet mask to be used by the network device.
2296 @retval EFI_SUCCESS The new station IP address and/or subnet mask were updated.
2297 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2298 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
2303 EfiPxeBcSetStationIP (
2304 IN EFI_PXE_BASE_CODE_PROTOCOL
* This
,
2305 IN EFI_IP_ADDRESS
* NewStationIp OPTIONAL
,
2306 IN EFI_IP_ADDRESS
* NewSubnetMask OPTIONAL
2309 PXEBC_PRIVATE_DATA
*Private
;
2310 EFI_PXE_BASE_CODE_MODE
*Mode
;
2311 EFI_ARP_CONFIG_DATA ArpConfigData
;
2314 return EFI_INVALID_PARAMETER
;
2317 if (NewSubnetMask
!= NULL
&& !IP4_IS_VALID_NETMASK (NTOHL (NewSubnetMask
->Addr
[0]))) {
2318 return EFI_INVALID_PARAMETER
;
2321 if (NewStationIp
!= NULL
) {
2322 if (IP4_IS_UNSPECIFIED(NTOHL (NewStationIp
->Addr
[0])) ||
2323 IP4_IS_LOCAL_BROADCAST(NTOHL (NewStationIp
->Addr
[0])) ||
2324 (NewSubnetMask
!= NULL
&& !NetIp4IsUnicast (NTOHL (NewStationIp
->Addr
[0]), NTOHL (NewSubnetMask
->Addr
[0])))) {
2325 return EFI_INVALID_PARAMETER
;
2329 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
2330 Mode
= Private
->PxeBc
.Mode
;
2332 if (!Mode
->Started
) {
2333 return EFI_NOT_STARTED
;
2336 if (NewStationIp
!= NULL
) {
2337 CopyMem (&Mode
->StationIp
, NewStationIp
, sizeof (EFI_IP_ADDRESS
));
2338 CopyMem (&Private
->StationIp
, NewStationIp
, sizeof (EFI_IP_ADDRESS
));
2341 if (NewSubnetMask
!= NULL
) {
2342 CopyMem (&Mode
->SubnetMask
, NewSubnetMask
, sizeof (EFI_IP_ADDRESS
));
2343 CopyMem (&Private
->SubnetMask
,NewSubnetMask
, sizeof (EFI_IP_ADDRESS
));
2346 Private
->AddressIsOk
= TRUE
;
2348 if (!Mode
->UsingIpv6
) {
2350 // If in IPv4 mode, configure the corresponding ARP with this new
2351 // station IP address.
2353 ZeroMem (&ArpConfigData
, sizeof (EFI_ARP_CONFIG_DATA
));
2355 ArpConfigData
.SwAddressType
= 0x0800;
2356 ArpConfigData
.SwAddressLength
= (UINT8
) sizeof (EFI_IPv4_ADDRESS
);
2357 ArpConfigData
.StationAddress
= &Private
->StationIp
.v4
;
2359 Private
->Arp
->Configure (Private
->Arp
, NULL
);
2360 Private
->Arp
->Configure (Private
->Arp
, &ArpConfigData
);
2363 // Update the route table.
2365 Mode
->RouteTableEntries
= 1;
2366 Mode
->RouteTable
[0].IpAddr
.Addr
[0] = Private
->StationIp
.Addr
[0] & Private
->SubnetMask
.Addr
[0];
2367 Mode
->RouteTable
[0].SubnetMask
.Addr
[0] = Private
->SubnetMask
.Addr
[0];
2368 Mode
->RouteTable
[0].GwAddr
.Addr
[0] = 0;
2375 Updates the contents of the cached DHCP and Discover packets.
2377 The pointers to the new packets are used to update the contents of the cached
2378 packets in the EFI_PXE_BASE_CODE_MODE structure.
2380 @param This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
2381 @param NewDhcpDiscoverValid Pointer to a value that will replace the current
2382 DhcpDiscoverValid field.
2383 @param NewDhcpAckReceived Pointer to a value that will replace the current
2384 DhcpAckReceived field.
2385 @param NewProxyOfferReceived Pointer to a value that will replace the current
2386 ProxyOfferReceived field.
2387 @param NewPxeDiscoverValid Pointer to a value that will replace the current
2388 ProxyOfferReceived field.
2389 @param NewPxeReplyReceived Pointer to a value that will replace the current
2390 PxeReplyReceived field.
2391 @param NewPxeBisReplyReceived Pointer to a value that will replace the current
2392 PxeBisReplyReceived field.
2393 @param NewDhcpDiscover Pointer to the new cached DHCP Discover packet contents.
2394 @param NewDhcpAck Pointer to the new cached DHCP Ack packet contents.
2395 @param NewProxyOffer Pointer to the new cached Proxy Offer packet contents.
2396 @param NewPxeDiscover Pointer to the new cached PXE Discover packet contents.
2397 @param NewPxeReply Pointer to the new cached PXE Reply packet contents.
2398 @param NewPxeBisReply Pointer to the new cached PXE BIS Reply packet contents.
2400 @retval EFI_SUCCESS The cached packet contents were updated.
2401 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
2402 @retval EFI_INVALID_PARAMETER This is NULL or not point to a valid EFI_PXE_BASE_CODE_PROTOCOL structure.
2407 EfiPxeBcSetPackets (
2408 IN EFI_PXE_BASE_CODE_PROTOCOL
* This
,
2409 IN BOOLEAN
* NewDhcpDiscoverValid OPTIONAL
,
2410 IN BOOLEAN
* NewDhcpAckReceived OPTIONAL
,
2411 IN BOOLEAN
* NewProxyOfferReceived OPTIONAL
,
2412 IN BOOLEAN
* NewPxeDiscoverValid OPTIONAL
,
2413 IN BOOLEAN
* NewPxeReplyReceived OPTIONAL
,
2414 IN BOOLEAN
* NewPxeBisReplyReceived OPTIONAL
,
2415 IN EFI_PXE_BASE_CODE_PACKET
* NewDhcpDiscover OPTIONAL
,
2416 IN EFI_PXE_BASE_CODE_PACKET
* NewDhcpAck OPTIONAL
,
2417 IN EFI_PXE_BASE_CODE_PACKET
* NewProxyOffer OPTIONAL
,
2418 IN EFI_PXE_BASE_CODE_PACKET
* NewPxeDiscover OPTIONAL
,
2419 IN EFI_PXE_BASE_CODE_PACKET
* NewPxeReply OPTIONAL
,
2420 IN EFI_PXE_BASE_CODE_PACKET
* NewPxeBisReply OPTIONAL
2423 PXEBC_PRIVATE_DATA
*Private
;
2424 EFI_PXE_BASE_CODE_MODE
*Mode
;
2427 return EFI_INVALID_PARAMETER
;
2430 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
2431 Mode
= Private
->PxeBc
.Mode
;
2433 if (!Mode
->Started
) {
2434 return EFI_NOT_STARTED
;
2437 if (NewDhcpDiscoverValid
!= NULL
) {
2438 Mode
->DhcpDiscoverValid
= *NewDhcpDiscoverValid
;
2441 if (NewDhcpAckReceived
!= NULL
) {
2442 Mode
->DhcpAckReceived
= *NewDhcpAckReceived
;
2445 if (NewProxyOfferReceived
!= NULL
) {
2446 Mode
->ProxyOfferReceived
= *NewProxyOfferReceived
;
2449 if (NewPxeDiscoverValid
!= NULL
) {
2450 Mode
->PxeDiscoverValid
= *NewPxeDiscoverValid
;
2453 if (NewPxeReplyReceived
!= NULL
) {
2454 Mode
->PxeReplyReceived
= *NewPxeReplyReceived
;
2457 if (NewPxeBisReplyReceived
!= NULL
) {
2458 Mode
->PxeBisReplyReceived
= *NewPxeBisReplyReceived
;
2461 if (NewDhcpDiscover
!= NULL
) {
2462 CopyMem (&Mode
->DhcpDiscover
, NewDhcpDiscover
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2465 if (NewDhcpAck
!= NULL
) {
2466 CopyMem (&Mode
->DhcpAck
, NewDhcpAck
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2469 if (NewProxyOffer
!= NULL
) {
2470 CopyMem (&Mode
->ProxyOffer
, NewProxyOffer
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2473 if (NewPxeDiscover
!= NULL
) {
2474 CopyMem (&Mode
->PxeDiscover
, NewPxeDiscover
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2477 if (NewPxeReply
!= NULL
) {
2478 CopyMem (&Mode
->PxeReply
, NewPxeReply
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2481 if (NewPxeBisReply
!= NULL
) {
2482 CopyMem (&Mode
->PxeBisReply
, NewPxeBisReply
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2488 EFI_PXE_BASE_CODE_PROTOCOL mPxeBcProtocolTemplate
= {
2489 EFI_PXE_BASE_CODE_PROTOCOL_REVISION
,
2497 EfiPxeBcSetIpFilter
,
2499 EfiPxeBcSetParameters
,
2500 EfiPxeBcSetStationIP
,
2506 Callback function that is invoked when the PXE Base Code Protocol is about to transmit, has
2507 received, or is waiting to receive a packet.
2509 This function is invoked when the PXE Base Code Protocol is about to transmit, has received,
2510 or is waiting to receive a packet. Parameters Function and Received specify the type of event.
2511 Parameters PacketLen and Packet specify the packet that generated the event. If these fields
2512 are zero and NULL respectively, then this is a status update callback. If the operation specified
2513 by Function is to continue, then CALLBACK_STATUS_CONTINUE should be returned. If the operation
2514 specified by Function should be aborted, then CALLBACK_STATUS_ABORT should be returned. Due to
2515 the polling nature of UEFI device drivers, a callback function should not execute for more than 5 ms.
2516 The SetParameters() function must be called after a Callback Protocol is installed to enable the
2519 @param This Pointer to the EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL instance.
2520 @param Function The PXE Base Code Protocol function that is waiting for an event.
2521 @param Received TRUE if the callback is being invoked due to a receive event. FALSE if
2522 the callback is being invoked due to a transmit event.
2523 @param PacketLength The length, in bytes, of Packet. This field will have a value of zero if
2524 this is a wait for receive event.
2525 @param PacketPtr If Received is TRUE, a pointer to the packet that was just received;
2526 otherwise a pointer to the packet that is about to be transmitted.
2528 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE if Function specifies a continue operation
2529 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT if Function specifies an abort operation
2532 EFI_PXE_BASE_CODE_CALLBACK_STATUS
2534 EfiPxeLoadFileCallback (
2535 IN EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
* This
,
2536 IN EFI_PXE_BASE_CODE_FUNCTION Function
,
2537 IN BOOLEAN Received
,
2538 IN UINT32 PacketLength
,
2539 IN EFI_PXE_BASE_CODE_PACKET
* PacketPtr OPTIONAL
2546 // Catch Ctrl-C or ESC to abort.
2548 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
2550 if (!EFI_ERROR (Status
)) {
2552 if (Key
.ScanCode
== SCAN_ESC
|| Key
.UnicodeChar
== (0x1F & 'c')) {
2554 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT
;
2558 // No print if receive packet
2561 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
;
2564 // Print only for three functions
2568 case EFI_PXE_BASE_CODE_FUNCTION_MTFTP
:
2570 // Print only for open MTFTP packets, not every MTFTP packets
2572 if (PacketLength
!= 0 && PacketPtr
!= NULL
) {
2573 if (PacketPtr
->Raw
[0x1C] != 0x00 || PacketPtr
->Raw
[0x1D] != 0x01) {
2574 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
;
2579 case EFI_PXE_BASE_CODE_FUNCTION_DHCP
:
2580 case EFI_PXE_BASE_CODE_FUNCTION_DISCOVER
:
2584 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
;
2587 if (PacketLength
!= 0 && PacketPtr
!= NULL
) {
2589 // Print '.' when transmit a packet
2595 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
;
2598 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL mPxeBcCallBackTemplate
= {
2599 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL_REVISION
,
2600 EfiPxeLoadFileCallback
2607 @param Private Pointer to PxeBc private data.
2608 @param BufferSize Pointer to buffer size.
2609 @param Buffer Pointer to buffer.
2611 @retval EFI_SUCCESS Discover the boot file successfully.
2612 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.
2613 @retval EFI_ABORTED PXE bootstrap server, so local boot need abort.
2614 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to load the boot file.
2619 IN PXEBC_PRIVATE_DATA
*Private
,
2620 IN OUT UINT64
*BufferSize
,
2624 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
2625 EFI_PXE_BASE_CODE_MODE
*Mode
;
2630 PXEBC_CACHED_DHCP4_PACKET
*Packet
;
2633 PxeBc
= &Private
->PxeBc
;
2635 Type
= EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP
;
2636 Layer
= EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL
;
2641 Status
= PxeBc
->Dhcp (PxeBc
, TRUE
);
2642 if (EFI_ERROR (Status
)) {
2647 // Select a boot server
2649 Status
= PxeBcSelectBootPrompt (Private
);
2651 if (Status
== EFI_SUCCESS
) {
2652 Status
= PxeBcSelectBootMenu (Private
, &Type
, TRUE
);
2653 } else if (Status
== EFI_TIMEOUT
) {
2654 Status
= PxeBcSelectBootMenu (Private
, &Type
, FALSE
);
2657 if (!EFI_ERROR (Status
)) {
2659 if (Type
== EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP
) {
2661 // Local boot(PXE bootstrap server) need abort
2666 UseBis
= (BOOLEAN
) (Mode
->BisSupported
&& Mode
->BisDetected
);
2667 Status
= PxeBc
->Discover (PxeBc
, Type
, &Layer
, UseBis
, NULL
);
2668 if (EFI_ERROR (Status
)) {
2676 // Get bootfile name and (m)tftp server ip addresss
2678 if (Mode
->PxeReplyReceived
) {
2679 Packet
= &Private
->PxeReply
;
2680 } else if (Mode
->ProxyOfferReceived
) {
2681 Packet
= &Private
->ProxyOffer
;
2683 Packet
= &Private
->Dhcp4Ack
;
2687 // Use siaddr(next server) in DHCPOFFER packet header, if zero, use option 54(server identifier)
2688 // in DHCPOFFER packet.
2689 // (It does not comply with PXE Spec, Ver2.1)
2691 if (EFI_IP4_EQUAL (&Packet
->Packet
.Offer
.Dhcp4
.Header
.ServerAddr
, &mZeroIp4Addr
)) {
2694 Packet
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_SERVER_ID
]->Data
,
2695 sizeof (EFI_IPv4_ADDRESS
)
2700 &Packet
->Packet
.Offer
.Dhcp4
.Header
.ServerAddr
,
2701 sizeof (EFI_IPv4_ADDRESS
)
2704 if (Private
->ServerIp
.Addr
[0] == 0) {
2705 return EFI_DEVICE_ERROR
;
2708 ASSERT (Packet
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
] != NULL
);
2713 Private
->BootFileName
= (CHAR8
*) (Packet
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE
]->Data
);
2715 if (Packet
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN
] != NULL
) {
2717 // Already have the bootfile length option, compute the file size
2719 CopyMem (&Value
, Packet
->Dhcp4Option
[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN
]->Data
, sizeof (Value
));
2720 Value
= NTOHS (Value
);
2721 *BufferSize
= 512 * Value
;
2722 Status
= EFI_BUFFER_TOO_SMALL
;
2725 // Get the bootfile size from tftp
2727 Status
= PxeBc
->Mtftp (
2729 EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE
,
2733 &Private
->BlockSize
,
2735 (UINT8
*) Private
->BootFileName
,
2741 Private
->FileSize
= (UINTN
) *BufferSize
;
2747 Causes the driver to load a specified file.
2749 @param This Protocol instance pointer.
2750 @param FilePath The device specific path of the file to load.
2751 @param BootPolicy If TRUE, indicates that the request originates from the
2752 boot manager is attempting to load FilePath as a boot
2753 selection. If FALSE, then FilePath must match as exact file
2755 @param BufferSize On input the size of Buffer in bytes. On output with a return
2756 code of EFI_SUCCESS, the amount of data transferred to
2757 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
2758 the size of Buffer required to retrieve the requested file.
2759 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
2760 then no the size of the requested file is returned in
2763 @retval EFI_SUCCESS The file was loaded.
2764 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy
2765 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or
2767 @retval EFI_NO_MEDIA No medium was present to load the file.
2768 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.
2769 @retval EFI_NO_RESPONSE The remote system did not respond.
2770 @retval EFI_NOT_FOUND The file was not found.
2771 @retval EFI_ABORTED The file load process was manually cancelled.
2777 IN EFI_LOAD_FILE_PROTOCOL
* This
,
2778 IN EFI_DEVICE_PATH_PROTOCOL
* FilePath
,
2779 IN BOOLEAN BootPolicy
,
2780 IN OUT UINTN
*BufferSize
,
2781 IN VOID
*Buffer OPTIONAL
2784 PXEBC_PRIVATE_DATA
*Private
;
2785 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
2786 BOOLEAN NewMakeCallback
;
2789 BOOLEAN MediaPresent
;
2791 if (FilePath
== NULL
|| !IsDevicePathEnd (FilePath
)) {
2792 return EFI_INVALID_PARAMETER
;
2795 Private
= PXEBC_PRIVATE_DATA_FROM_LOADFILE (This
);
2796 PxeBc
= &Private
->PxeBc
;
2797 NewMakeCallback
= FALSE
;
2798 Status
= EFI_DEVICE_ERROR
;
2800 if (This
== NULL
|| BufferSize
== NULL
) {
2802 return EFI_INVALID_PARAMETER
;
2806 // Only support BootPolicy
2809 return EFI_UNSUPPORTED
;
2813 // Check media status before PXE start
2815 MediaPresent
= TRUE
;
2816 NetLibDetectMedia (Private
->Controller
, &MediaPresent
);
2817 if (!MediaPresent
) {
2818 return EFI_NO_MEDIA
;
2821 Status
= PxeBc
->Start (PxeBc
, FALSE
);
2822 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
2826 Status
= gBS
->HandleProtocol (
2827 Private
->Controller
,
2828 &gEfiPxeBaseCodeCallbackProtocolGuid
,
2829 (VOID
**) &Private
->PxeBcCallback
2831 if (Status
== EFI_UNSUPPORTED
) {
2833 CopyMem (&Private
->LoadFileCallback
, &mPxeBcCallBackTemplate
, sizeof (Private
->LoadFileCallback
));
2835 Status
= gBS
->InstallProtocolInterface (
2836 &Private
->Controller
,
2837 &gEfiPxeBaseCodeCallbackProtocolGuid
,
2838 EFI_NATIVE_INTERFACE
,
2839 &Private
->LoadFileCallback
2842 NewMakeCallback
= (BOOLEAN
) (Status
== EFI_SUCCESS
);
2844 Status
= PxeBc
->SetParameters (PxeBc
, NULL
, NULL
, NULL
, NULL
, &NewMakeCallback
);
2845 if (EFI_ERROR (Status
)) {
2846 PxeBc
->Stop (PxeBc
);
2851 if (Private
->FileSize
== 0) {
2853 Status
= DiscoverBootFile (Private
, &TmpBufSize
, Buffer
);
2855 if (sizeof (UINTN
) < sizeof (UINT64
) && (TmpBufSize
> 0xFFFFFFFF)) {
2856 Status
= EFI_DEVICE_ERROR
;
2857 } else if (TmpBufSize
> 0 && *BufferSize
>= (UINTN
) TmpBufSize
&& Buffer
!= NULL
) {
2858 *BufferSize
= (UINTN
) TmpBufSize
;
2859 Status
= PxeBc
->Mtftp (
2861 EFI_PXE_BASE_CODE_TFTP_READ_FILE
,
2865 &Private
->BlockSize
,
2867 (UINT8
*) Private
->BootFileName
,
2871 } else if (TmpBufSize
> 0) {
2872 *BufferSize
= (UINTN
) TmpBufSize
;
2873 Status
= EFI_BUFFER_TOO_SMALL
;
2875 } else if (Buffer
== NULL
|| Private
->FileSize
> *BufferSize
) {
2876 *BufferSize
= Private
->FileSize
;
2877 Status
= EFI_BUFFER_TOO_SMALL
;
2880 // Download the file.
2882 TmpBufSize
= (UINT64
) (*BufferSize
);
2883 Status
= PxeBc
->Mtftp (
2885 EFI_PXE_BASE_CODE_TFTP_READ_FILE
,
2889 &Private
->BlockSize
,
2891 (UINT8
*) Private
->BootFileName
,
2897 // If we added a callback protocol, now is the time to remove it.
2899 if (NewMakeCallback
) {
2901 NewMakeCallback
= FALSE
;
2903 PxeBc
->SetParameters (PxeBc
, NULL
, NULL
, NULL
, NULL
, &NewMakeCallback
);
2905 gBS
->UninstallProtocolInterface (
2906 Private
->Controller
,
2907 &gEfiPxeBaseCodeCallbackProtocolGuid
,
2908 &Private
->LoadFileCallback
2913 // Check download status
2915 if (Status
== EFI_SUCCESS
) {
2917 // The DHCP4 can have only one configured child instance so we need to stop
2918 // reset the DHCP4 child before we return. Otherwise the other programs which
2919 // also need to use DHCP4 will be impacted.
2920 // The functionality of PXE Base Code protocol will not be stopped,
2921 // when downloading is successfully.
2923 Private
->Dhcp4
->Stop (Private
->Dhcp4
);
2924 Private
->Dhcp4
->Configure (Private
->Dhcp4
, NULL
);
2927 } else if (Status
== EFI_BUFFER_TOO_SMALL
) {
2928 if (Buffer
!= NULL
) {
2929 AsciiPrint ("PXE-E05: Download buffer is smaller than requested file.\n");
2932 // The functionality of PXE Base Code protocol will not be stopped.
2937 } else if (Status
== EFI_DEVICE_ERROR
) {
2938 AsciiPrint ("PXE-E07: Network device error.\n");
2940 } else if (Status
== EFI_OUT_OF_RESOURCES
) {
2941 AsciiPrint ("PXE-E09: Could not allocate I/O buffers.\n");
2943 } else if (Status
== EFI_NO_MEDIA
) {
2944 AsciiPrint ("PXE-E12: Could not detect network connection.\n");
2946 } else if (Status
== EFI_NO_RESPONSE
) {
2947 AsciiPrint ("PXE-E16: No offer received.\n");
2949 } else if (Status
== EFI_TIMEOUT
) {
2950 AsciiPrint ("PXE-E18: Server response timeout.\n");
2952 } else if (Status
== EFI_ABORTED
) {
2953 AsciiPrint ("PXE-E21: Remote boot cancelled.\n");
2955 } else if (Status
== EFI_ICMP_ERROR
) {
2956 AsciiPrint ("PXE-E22: Client received ICMP error from server.\n");
2958 } else if (Status
== EFI_TFTP_ERROR
) {
2959 AsciiPrint ("PXE-E23: Client received TFTP error from server.\n");
2962 AsciiPrint ("PXE-E99: Unexpected network error.\n");
2965 PxeBc
->Stop (PxeBc
);
2970 EFI_LOAD_FILE_PROTOCOL mLoadFileProtocolTemplate
= { EfiPxeLoadFile
};