2 This implementation of EFI_PXE_BASE_CODE_PROTOCOL and EFI_LOAD_FILE_PROTOCOL.
4 Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "PxeBcImpl.h"
20 Enables the use of the PXE Base Code Protocol functions.
22 This function enables the use of the PXE Base Code Protocol functions. If the
23 Started field of the EFI_PXE_BASE_CODE_MODE structure is already TRUE, then
24 EFI_ALREADY_STARTED will be returned. If UseIpv6 is TRUE, then IPv6 formatted
25 addresses will be used in this session. If UseIpv6 is FALSE, then IPv4 formatted
26 addresses will be used in this session. If UseIpv6 is TRUE, and the Ipv6Supported
27 field of the EFI_PXE_BASE_CODE_MODE structure is FALSE, then EFI_UNSUPPORTED will
28 be returned. If there is not enough memory or other resources to start the PXE
29 Base Code Protocol, then EFI_OUT_OF_RESOURCES will be returned. Otherwise, the
30 PXE Base Code Protocol will be started.
32 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
33 @param[in] UseIpv6 Specifies the type of IP addresses that are to be
34 used during the session that is being started.
35 Set to TRUE for IPv6, and FALSE for IPv4.
37 @retval EFI_SUCCESS The PXE Base Code Protocol was started.
38 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
39 @retval EFI_UNSUPPORTED UseIpv6 is TRUE, but the Ipv6Supported field of the
40 EFI_PXE_BASE_CODE_MODE structure is FALSE.
41 @retval EFI_ALREADY_STARTED The PXE Base Code Protocol is already in the started state.
42 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
43 EFI_PXE_BASE_CODE_PROTOCOL structure.
44 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory or other resources to start the
45 PXE Base Code Protocol.
51 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
55 PXEBC_PRIVATE_DATA
*Private
;
56 EFI_PXE_BASE_CODE_MODE
*Mode
;
61 return EFI_INVALID_PARAMETER
;
64 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
65 Mode
= Private
->PxeBc
.Mode
;
68 return EFI_ALREADY_STARTED
;
72 // Detect whether using IPv6 or not, and set it into mode data.
74 if (UseIpv6
&& Mode
->Ipv6Available
&& Mode
->Ipv6Supported
&& Private
->Ip6Nic
!= NULL
) {
75 Mode
->UsingIpv6
= TRUE
;
76 } else if (!UseIpv6
&& Private
->Ip4Nic
!= NULL
) {
77 Mode
->UsingIpv6
= FALSE
;
79 return EFI_UNSUPPORTED
;
82 if (Mode
->UsingIpv6
) {
83 AsciiPrint ("\n>>Start PXE over IPv6");
85 // Configure block size for TFTP as a default value to handle all link layers.
87 Private
->BlockSize
= (UINTN
) (Private
->Ip6MaxPacketSize
-
88 PXEBC_DEFAULT_UDP_OVERHEAD_SIZE
- PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE
);
91 // PXE over IPv6 starts here, initialize the fields and list header.
93 Private
->Ip6Policy
= PXEBC_IP6_POLICY_MAX
;
94 Private
->ProxyOffer
.Dhcp6
.Packet
.Offer
.Size
= PXEBC_DHCP6_PACKET_MAX_SIZE
;
95 Private
->DhcpAck
.Dhcp6
.Packet
.Ack
.Size
= PXEBC_DHCP6_PACKET_MAX_SIZE
;
96 Private
->PxeReply
.Dhcp6
.Packet
.Ack
.Size
= PXEBC_DHCP6_PACKET_MAX_SIZE
;
98 for (Index
= 0; Index
< PXEBC_OFFER_MAX_NUM
; Index
++) {
99 Private
->OfferBuffer
[Index
].Dhcp6
.Packet
.Offer
.Size
= PXEBC_DHCP6_PACKET_MAX_SIZE
;
103 // Create event and set status for token to capture ICMP6 error message.
105 Private
->Icmp6Token
.Status
= EFI_NOT_READY
;
106 Status
= gBS
->CreateEvent (
109 PxeBcIcmp6ErrorUpdate
,
111 &Private
->Icmp6Token
.Event
113 if (EFI_ERROR (Status
)) {
117 AsciiPrint ("\n>>Start PXE over IPv4");
119 // Configure block size for TFTP as a default value to handle all link layers.
121 Private
->BlockSize
= (UINTN
) (Private
->Ip4MaxPacketSize
-
122 PXEBC_DEFAULT_UDP_OVERHEAD_SIZE
- PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE
);
125 // PXE over IPv4 starts here, initialize the fields.
127 Private
->ProxyOffer
.Dhcp4
.Packet
.Offer
.Size
= PXEBC_DHCP4_PACKET_MAX_SIZE
;
128 Private
->DhcpAck
.Dhcp4
.Packet
.Ack
.Size
= PXEBC_DHCP4_PACKET_MAX_SIZE
;
129 Private
->PxeReply
.Dhcp4
.Packet
.Ack
.Size
= PXEBC_DHCP4_PACKET_MAX_SIZE
;
131 for (Index
= 0; Index
< PXEBC_OFFER_MAX_NUM
; Index
++) {
132 Private
->OfferBuffer
[Index
].Dhcp4
.Packet
.Offer
.Size
= PXEBC_DHCP4_PACKET_MAX_SIZE
;
135 PxeBcSeedDhcp4Packet (&Private
->SeedPacket
, Private
->Udp4Read
);
138 // Create the event for Arp cache update.
140 Status
= gBS
->CreateEvent (
141 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
145 &Private
->ArpUpdateEvent
147 if (EFI_ERROR (Status
)) {
152 // Start a periodic timer by second to update Arp cache.
154 Status
= gBS
->SetTimer (
155 Private
->ArpUpdateEvent
,
159 if (EFI_ERROR (Status
)) {
164 // Create event and set status for token to capture ICMP error message.
166 Private
->Icmp6Token
.Status
= EFI_NOT_READY
;
167 Status
= gBS
->CreateEvent (
170 PxeBcIcmpErrorUpdate
,
172 &Private
->IcmpToken
.Event
174 if (EFI_ERROR (Status
)) {
180 // If PcdTftpBlockSize is set to non-zero, override the default value.
182 if (PcdGet64 (PcdTftpBlockSize
) != 0) {
183 Private
->BlockSize
= (UINTN
) PcdGet64 (PcdTftpBlockSize
);
187 // Create event for UdpRead/UdpWrite timeout since they are both blocking API.
189 Status
= gBS
->CreateEvent (
194 &Private
->UdpTimeOutEvent
196 if (EFI_ERROR (Status
)) {
200 Private
->IsAddressOk
= FALSE
;
201 Mode
->Started
= TRUE
;
206 if (Mode
->UsingIpv6
) {
207 if (Private
->Icmp6Token
.Event
!= NULL
) {
208 gBS
->CloseEvent (Private
->Icmp6Token
.Event
);
209 Private
->Icmp6Token
.Event
= NULL
;
211 Private
->Udp6Read
->Configure (Private
->Udp6Read
, NULL
);
212 Private
->Ip6
->Configure (Private
->Ip6
, NULL
);
214 if (Private
->ArpUpdateEvent
!= NULL
) {
215 gBS
->CloseEvent (Private
->ArpUpdateEvent
);
216 Private
->ArpUpdateEvent
= NULL
;
218 if (Private
->IcmpToken
.Event
!= NULL
) {
219 gBS
->CloseEvent (Private
->IcmpToken
.Event
);
220 Private
->IcmpToken
.Event
= NULL
;
222 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
223 Private
->Ip4
->Configure (Private
->Ip4
, NULL
);
230 Disable the use of the PXE Base Code Protocol functions.
232 This function stops all activity on the network device. All the resources allocated
233 in Start() are released, the Started field of the EFI_PXE_BASE_CODE_MODE structure is
234 set to FALSE, and EFI_SUCCESS is returned. If the Started field of the EFI_PXE_BASE_CODE_MODE
235 structure is already FALSE, then EFI_NOT_STARTED will be returned.
237 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
239 @retval EFI_SUCCESS The PXE Base Code Protocol was stopped.
240 @retval EFI_NOT_STARTED The PXE Base Code Protocol is already in the stopped state.
241 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
242 EFI_PXE_BASE_CODE_PROTOCOL structure.
249 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
252 PXEBC_PRIVATE_DATA
*Private
;
253 EFI_PXE_BASE_CODE_MODE
*Mode
;
254 BOOLEAN Ipv6Supported
;
255 BOOLEAN Ipv6Available
;
258 return EFI_INVALID_PARAMETER
;
261 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
262 Mode
= Private
->PxeBc
.Mode
;
263 Ipv6Supported
= Mode
->Ipv6Supported
;
264 Ipv6Available
= Mode
->Ipv6Available
;
266 if (!Mode
->Started
) {
267 return EFI_NOT_STARTED
;
270 if (Mode
->UsingIpv6
) {
272 // Configure all the instances for IPv6 as NULL.
274 ZeroMem (&Private
->Udp6CfgData
.StationAddress
, sizeof (EFI_IPv6_ADDRESS
));
275 ZeroMem (&Private
->Ip6CfgData
.StationAddress
, sizeof (EFI_IPv6_ADDRESS
));
276 Private
->Dhcp6
->Stop (Private
->Dhcp6
);
277 Private
->Dhcp6
->Configure (Private
->Dhcp6
, NULL
);
278 Private
->Udp6Write
->Configure (Private
->Udp6Write
, NULL
);
279 Private
->Udp6Read
->Groups (Private
->Udp6Read
, FALSE
, NULL
);
280 Private
->Udp6Read
->Configure (Private
->Udp6Read
, NULL
);
281 Private
->Ip6
->Cancel (Private
->Ip6
, &Private
->Icmp6Token
);
282 Private
->Ip6
->Configure (Private
->Ip6
, NULL
);
283 PxeBcUnregisterIp6Address (Private
);
284 if (Private
->Icmp6Token
.Event
!= NULL
) {
285 gBS
->CloseEvent (Private
->Icmp6Token
.Event
);
286 Private
->Icmp6Token
.Event
= NULL
;
288 if (Private
->Dhcp6Request
!= NULL
) {
289 FreePool (Private
->Dhcp6Request
);
290 Private
->Dhcp6Request
= NULL
;
292 if (Private
->BootFileName
!= NULL
) {
293 FreePool (Private
->BootFileName
);
294 Private
->BootFileName
= NULL
;
298 // Configure all the instances for IPv4 as NULL.
300 ZeroMem (&Private
->Udp4CfgData
.StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
301 ZeroMem (&Private
->Udp4CfgData
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
302 ZeroMem (&Private
->Ip4CfgData
.StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
303 ZeroMem (&Private
->Ip4CfgData
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
304 Private
->Dhcp4
->Stop (Private
->Dhcp4
);
305 Private
->Dhcp4
->Configure (Private
->Dhcp4
, NULL
);
306 Private
->Udp4Write
->Configure (Private
->Udp4Write
, NULL
);
307 Private
->Udp4Read
->Groups (Private
->Udp4Read
, FALSE
, NULL
);
308 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
309 Private
->Ip4
->Cancel (Private
->Ip4
, &Private
->IcmpToken
);
310 Private
->Ip4
->Configure (Private
->Ip4
, NULL
);
311 if (Private
->ArpUpdateEvent
!= NULL
) {
312 gBS
->CloseEvent (Private
->ArpUpdateEvent
);
313 Private
->ArpUpdateEvent
= NULL
;
315 if (Private
->IcmpToken
.Event
!= NULL
) {
316 gBS
->CloseEvent (Private
->IcmpToken
.Event
);
317 Private
->IcmpToken
.Event
= NULL
;
321 gBS
->CloseEvent (Private
->UdpTimeOutEvent
);
322 Private
->CurSrcPort
= 0;
323 Private
->BootFileSize
= 0;
324 Private
->SolicitTimes
= 0;
325 Private
->ElapsedTime
= 0;
328 // Reset the mode data.
330 ZeroMem (Mode
, sizeof (EFI_PXE_BASE_CODE_MODE
));
331 Mode
->Ipv6Available
= Ipv6Available
;
332 Mode
->Ipv6Supported
= Ipv6Supported
;
333 Mode
->AutoArp
= TRUE
;
334 Mode
->TTL
= DEFAULT_TTL
;
335 Mode
->ToS
= DEFAULT_ToS
;
342 Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / acknowledge) or DHCPv6
343 S.A.R.R (solicit / advertise / request / reply) sequence.
345 If SortOffers is TRUE, then the cached DHCP offer packets will be sorted before
346 they are tried. If SortOffers is FALSE, then the cached DHCP offer packets will
347 be tried in the order in which they are received. Please see the Preboot Execution
348 Environment (PXE) Specification and Unified Extensible Firmware Interface (UEFI)
349 Specification for additional details on the implementation of DHCP.
350 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
351 then the DHCP sequence will be stopped and EFI_ABORTED will be returned.
353 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
354 @param[in] SortOffers TRUE if the offers received should be sorted. Set to FALSE to
355 try the offers in the order that they are received.
357 @retval EFI_SUCCESS Valid DHCP has completed.
358 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
359 @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
360 EFI_PXE_BASE_CODE_PROTOCOL structure.
361 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
362 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete the DHCP Protocol.
363 @retval EFI_ABORTED The callback function aborted the DHCP Protocol.
364 @retval EFI_TIMEOUT The DHCP Protocol timed out.
365 @retval EFI_ICMP_ERROR An ICMP error packet was received during the DHCP session.
366 @retval EFI_NO_RESPONSE Valid PXE offer was not received.
372 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
373 IN BOOLEAN SortOffers
376 PXEBC_PRIVATE_DATA
*Private
;
377 EFI_PXE_BASE_CODE_MODE
*Mode
;
379 EFI_PXE_BASE_CODE_IP_FILTER IpFilter
;
382 return EFI_INVALID_PARAMETER
;
385 Status
= EFI_SUCCESS
;
386 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
387 Mode
= Private
->PxeBc
.Mode
;
388 Mode
->IcmpErrorReceived
= FALSE
;
389 Private
->Function
= EFI_PXE_BASE_CODE_FUNCTION_DHCP
;
390 Private
->IsOfferSorted
= SortOffers
;
392 if (!Mode
->Started
) {
393 return EFI_NOT_STARTED
;
396 if (Mode
->UsingIpv6
) {
399 // Stop Udp6Read instance
401 Private
->Udp6Read
->Configure (Private
->Udp6Read
, NULL
);
404 // Start S.A.R.R. process to get a IPv6 address and other boot information.
406 Status
= PxeBcDhcp6Sarr (Private
, Private
->Dhcp6
);
408 if (EFI_ERROR (Status
)) {
413 // Configure Udp6Read instance
415 Status
= Private
->Udp6Read
->Configure (Private
->Udp6Read
, &Private
->Udp6CfgData
);
419 // Stop Udp4Read instance
421 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
424 // Start D.O.R.A. process to get a IPv4 address and other boot information.
426 Status
= PxeBcDhcp4Dora (Private
, Private
->Dhcp4
);
428 if (EFI_ERROR (Status
)) {
433 // Configure Udp4Read instance
435 Status
= Private
->Udp4Read
->Configure (Private
->Udp4Read
, &Private
->Udp4CfgData
);
439 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
440 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
442 ZeroMem(&IpFilter
, sizeof (EFI_PXE_BASE_CODE_IP_FILTER
));
443 IpFilter
.Filters
= EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
;
444 This
->SetIpFilter (This
, &IpFilter
);
451 Attempts to complete the PXE Boot Server and/or boot image discovery sequence.
453 This function attempts to complete the PXE Boot Server and/or boot image discovery
454 sequence. If this sequence is completed, then EFI_SUCCESS is returned, and the
455 PxeDiscoverValid, PxeDiscover, PxeReplyReceived, and PxeReply fields of the
456 EFI_PXE_BASE_CODE_MODE structure are filled in. If UseBis is TRUE, then the
457 PxeBisReplyReceived and PxeBisReply fields of the EFI_PXE_BASE_CODE_MODE structure
458 will also be filled in. If UseBis is FALSE, then PxeBisReplyValid will be set to FALSE.
459 In the structure referenced by parameter Info, the PXE Boot Server list, SrvList[],
460 has two uses: It is the Boot Server IP address list used for unicast discovery
461 (if the UseUCast field is TRUE), and it is the list used for Boot Server verification
462 (if the MustUseList field is TRUE). Also, if the MustUseList field in that structure
463 is TRUE and the AcceptAnyResponse field in the SrvList[] array is TRUE, any Boot
464 Server reply of that type will be accepted. If the AcceptAnyResponse field is
465 FALSE, only responses from Boot Servers with matching IP addresses will be accepted.
466 This function can take at least 10 seconds to timeout and return control to the
467 caller. If the Discovery sequence does not complete, then EFI_TIMEOUT will be
468 returned. Please see the Preboot Execution Environment (PXE) Specification for
469 additional details on the implementation of the Discovery sequence.
470 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
471 then the Discovery sequence is stopped and EFI_ABORTED will be returned.
473 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
474 @param[in] Type The type of bootstrap to perform.
475 @param[in] Layer Pointer to the boot server layer number to discover, which must be
476 PXE_BOOT_LAYER_INITIAL when a new server type is being
478 @param[in] UseBis TRUE if Boot Integrity Services are to be used. FALSE otherwise.
479 @param[in] Info Pointer to a data structure that contains additional information
480 on the type of discovery operation that is to be performed.
483 @retval EFI_SUCCESS The Discovery sequence has been completed.
484 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
485 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
486 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
487 @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete Discovery.
488 @retval EFI_ABORTED The callback function aborted the Discovery sequence.
489 @retval EFI_TIMEOUT The Discovery sequence timed out.
490 @retval EFI_ICMP_ERROR An ICMP error packet was received during the PXE discovery
497 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
501 IN EFI_PXE_BASE_CODE_DISCOVER_INFO
*Info OPTIONAL
504 PXEBC_PRIVATE_DATA
*Private
;
505 EFI_PXE_BASE_CODE_MODE
*Mode
;
506 EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo
;
507 EFI_PXE_BASE_CODE_SRVLIST
*SrvList
;
508 PXEBC_BOOT_SVR_ENTRY
*BootSvrEntry
;
511 EFI_PXE_BASE_CODE_IP_FILTER IpFilter
;
514 return EFI_INVALID_PARAMETER
;
517 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
518 Mode
= Private
->PxeBc
.Mode
;
519 Mode
->IcmpErrorReceived
= FALSE
;
522 Status
= EFI_DEVICE_ERROR
;
523 Private
->Function
= EFI_PXE_BASE_CODE_FUNCTION_DISCOVER
;
525 if (!Mode
->Started
) {
526 return EFI_NOT_STARTED
;
530 // Station address should be ready before do discover.
532 if (!Private
->IsAddressOk
) {
533 return EFI_INVALID_PARAMETER
;
536 if (Mode
->UsingIpv6
) {
539 // Stop Udp6Read instance
541 Private
->Udp6Read
->Configure (Private
->Udp6Read
, NULL
);
545 // Stop Udp4Read instance
547 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
551 // There are 3 methods to get the information for discover.
553 if (*Layer
!= EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL
) {
555 // 1. Take the previous setting as the discover info.
557 if (!Mode
->PxeDiscoverValid
||
558 !Mode
->PxeReplyReceived
||
559 (!Mode
->PxeBisReplyReceived
&& UseBis
)) {
560 Status
= EFI_INVALID_PARAMETER
;
566 Info
->UseUCast
= TRUE
;
567 SrvList
= Info
->SrvList
;
568 SrvList
[0].Type
= Type
;
569 SrvList
[0].AcceptAnyResponse
= FALSE
;
571 CopyMem (&SrvList
->IpAddr
, &Private
->ServerIp
, sizeof (EFI_IP_ADDRESS
));
573 } else if (Info
== NULL
) {
575 // 2. Extract the discover information from the cached packets if unspecified.
578 Status
= PxeBcExtractDiscoverInfo (Private
, Type
, Info
, &BootSvrEntry
, &SrvList
);
579 if (EFI_ERROR (Status
)) {
585 // 3. Take the pass-in information as the discover info, and validate the server list.
587 SrvList
= Info
->SrvList
;
589 if (!SrvList
[0].AcceptAnyResponse
) {
590 for (Index
= 1; Index
< Info
->IpCnt
; Index
++) {
591 if (SrvList
[Index
].AcceptAnyResponse
) {
595 if (Index
!= Info
->IpCnt
) {
597 // It's invalid if the first server doesn't accecpt any response
598 // and meanwhile any of the rest servers accept any reponse.
600 Status
= EFI_INVALID_PARAMETER
;
607 // Info and BootSvrEntry/SrvList are all ready by now, so execute discover by UniCast/BroadCast/MultiCast.
609 if ((!Info
->UseUCast
&& !Info
->UseBCast
&& !Info
->UseMCast
) ||
610 (Info
->MustUseList
&& Info
->IpCnt
== 0)) {
611 Status
= EFI_INVALID_PARAMETER
;
615 Private
->IsDoDiscover
= TRUE
;
617 if (Info
->UseUCast
) {
619 // Do discover by unicast.
621 for (Index
= 0; Index
< Info
->IpCnt
; Index
++) {
622 if (BootSvrEntry
== NULL
) {
623 CopyMem (&Private
->ServerIp
, &SrvList
[Index
].IpAddr
, sizeof (EFI_IP_ADDRESS
));
625 ASSERT (!Mode
->UsingIpv6
);
626 ZeroMem (&Private
->ServerIp
, sizeof (EFI_IP_ADDRESS
));
627 CopyMem (&Private
->ServerIp
, &BootSvrEntry
->IpAddr
[Index
], sizeof (EFI_IPv4_ADDRESS
));
630 Status
= PxeBcDiscoverBootServer (
635 &SrvList
[Index
].IpAddr
,
640 } else if (Info
->UseMCast
) {
642 // Do discover by multicast.
644 Status
= PxeBcDiscoverBootServer (
649 &Info
->ServerMCastIp
,
654 } else if (Info
->UseBCast
) {
656 // Do discover by broadcast, but only valid for IPv4.
658 ASSERT (!Mode
->UsingIpv6
);
659 Status
= PxeBcDiscoverBootServer (
670 if (!EFI_ERROR (Status
)) {
672 // Parse the cached PXE reply packet, and store it into mode data if valid.
674 if (Mode
->UsingIpv6
) {
675 Status
= PxeBcParseDhcp6Packet (&Private
->PxeReply
.Dhcp6
);
676 if (!EFI_ERROR (Status
)) {
678 &Mode
->PxeReply
.Dhcpv6
,
679 &Private
->PxeReply
.Dhcp6
.Packet
.Offer
,
680 Private
->PxeReply
.Dhcp6
.Packet
.Offer
.Length
682 Mode
->PxeReplyReceived
= TRUE
;
683 Mode
->PxeDiscoverValid
= TRUE
;
686 Status
= PxeBcParseDhcp4Packet (&Private
->PxeReply
.Dhcp4
);
687 if (!EFI_ERROR (Status
)) {
689 &Mode
->PxeReply
.Dhcpv4
,
690 &Private
->PxeReply
.Dhcp4
.Packet
.Offer
,
691 Private
->PxeReply
.Dhcp4
.Packet
.Offer
.Length
693 Mode
->PxeReplyReceived
= TRUE
;
694 Mode
->PxeDiscoverValid
= TRUE
;
701 if (Mode
->UsingIpv6
) {
702 Status
= Private
->Udp6Read
->Configure (Private
->Udp6Read
, &Private
->Udp6CfgData
);
704 Status
= Private
->Udp4Read
->Configure (Private
->Udp4Read
, &Private
->Udp4CfgData
);
708 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
709 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
711 ZeroMem(&IpFilter
, sizeof (EFI_PXE_BASE_CODE_IP_FILTER
));
712 IpFilter
.Filters
= EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
;
713 This
->SetIpFilter (This
, &IpFilter
);
720 Used to perform TFTP and MTFTP services.
722 This function is used to perform TFTP and MTFTP services. This includes the
723 TFTP operations to get the size of a file, read a directory, read a file, and
724 write a file. It also includes the MTFTP operations to get the size of a file,
725 read a directory, and read a file. The type of operation is specified by Operation.
726 If the callback function that is invoked during the TFTP/MTFTP operation does
727 not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will
729 For read operations, the return data will be placed in the buffer specified by
730 BufferPtr. If BufferSize is too small to contain the entire downloaded file,
731 then EFI_BUFFER_TOO_SMALL will be returned and BufferSize will be set to zero,
732 or the size of the requested file. (NOTE: the size of the requested file is only returned
733 if the TFTP server supports TFTP options). If BufferSize is large enough for the
734 read operation, then BufferSize will be set to the size of the downloaded file,
735 and EFI_SUCCESS will be returned. Applications using the PxeBc.Mtftp() services
736 should use the get-file-size operations to determine the size of the downloaded
737 file prior to using the read-file operations-especially when downloading large
738 (greater than 64 MB) files-instead of making two calls to the read-file operation.
739 Following this recommendation will save time if the file is larger than expected
740 and the TFTP server does not support TFTP option extensions. Without TFTP option
741 extension support, the client must download the entire file, counting and discarding
742 the received packets, to determine the file size.
743 For write operations, the data to be sent is in the buffer specified by BufferPtr.
744 BufferSize specifies the number of bytes to send. If the write operation completes
745 successfully, then EFI_SUCCESS will be returned.
746 For TFTP "get file size" operations, the size of the requested file or directory
747 is returned in BufferSize, and EFI_SUCCESS will be returned. If the TFTP server
748 does not support options, the file will be downloaded into a bit bucket and the
749 length of the downloaded file will be returned. For MTFTP "get file size" operations,
750 if the MTFTP server does not support the "get file size" option, EFI_UNSUPPORTED
752 This function can take up to 10 seconds to timeout and return control to the caller.
753 If the TFTP sequence does not complete, EFI_TIMEOUT will be returned.
754 If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
755 then the TFTP sequence is stopped and EFI_ABORTED will be returned.
757 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
758 @param[in] Operation The type of operation to perform.
759 @param[in, out] BufferPtr A pointer to the data buffer.
760 @param[in] Overwrite Only used on write file operations. TRUE if a file on a remote
761 server can be overwritten.
762 @param[in, out] BufferSize For get-file-size operations, *BufferSize returns the size of the
764 @param[in] BlockSize The requested block size to be used during a TFTP transfer.
765 @param[in] ServerIp The TFTP / MTFTP server IP address.
766 @param[in] Filename A Null-terminated ASCII string that specifies a directory name
768 @param[in] Info Pointer to the MTFTP information.
769 @param[in] DontUseBuffer Set to FALSE for normal TFTP and MTFTP read file operation.
771 @retval EFI_SUCCESS The TFTP/MTFTP operation was completed.
772 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
773 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
774 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
775 @retval EFI_BUFFER_TOO_SMALL The buffer is not large enough to complete the read operation.
776 @retval EFI_ABORTED The callback function aborted the TFTP/MTFTP operation.
777 @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.
778 @retval EFI_ICMP_ERROR An ICMP error packet was received during the MTFTP session.
779 @retval EFI_TFTP_ERROR A TFTP error packet was received during the MTFTP session.
785 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
786 IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation
,
787 IN OUT VOID
*BufferPtr OPTIONAL
,
788 IN BOOLEAN Overwrite
,
789 IN OUT UINT64
*BufferSize
,
790 IN UINTN
*BlockSize OPTIONAL
,
791 IN EFI_IP_ADDRESS
*ServerIp
,
793 IN EFI_PXE_BASE_CODE_MTFTP_INFO
*Info OPTIONAL
,
794 IN BOOLEAN DontUseBuffer
797 PXEBC_PRIVATE_DATA
*Private
;
798 EFI_PXE_BASE_CODE_MODE
*Mode
;
799 EFI_MTFTP4_CONFIG_DATA Mtftp4Config
;
800 EFI_MTFTP6_CONFIG_DATA Mtftp6Config
;
803 EFI_PXE_BASE_CODE_IP_FILTER IpFilter
;
806 if ((This
== NULL
) ||
807 (Filename
== NULL
) ||
808 (BufferSize
== NULL
) ||
809 (ServerIp
== NULL
) ||
810 ((BufferPtr
== NULL
) && DontUseBuffer
) ||
811 ((BlockSize
!= NULL
) && (*BlockSize
< PXE_MTFTP_DEFAULT_BLOCK_SIZE
)) ||
812 (!NetIp4IsUnicast (NTOHL (ServerIp
->Addr
[0]), 0) && !NetIp6IsValidUnicast (&ServerIp
->v6
))) {
813 return EFI_INVALID_PARAMETER
;
817 Status
= EFI_DEVICE_ERROR
;
818 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
819 Mode
= Private
->PxeBc
.Mode
;
821 if (Mode
->UsingIpv6
) {
823 // Set configuration data for Mtftp6 instance.
825 ZeroMem (&Mtftp6Config
, sizeof (EFI_MTFTP6_CONFIG_DATA
));
826 Config
= &Mtftp6Config
;
827 Mtftp6Config
.TimeoutValue
= PXEBC_MTFTP_TIMEOUT
;
828 Mtftp6Config
.TryCount
= PXEBC_MTFTP_RETRIES
;
829 CopyMem (&Mtftp6Config
.StationIp
, &Private
->StationIp
.v6
, sizeof (EFI_IPv6_ADDRESS
));
830 CopyMem (&Mtftp6Config
.ServerIp
, &ServerIp
->v6
, sizeof (EFI_IPv6_ADDRESS
));
832 // Stop Udp6Read instance
834 Private
->Udp6Read
->Configure (Private
->Udp6Read
, NULL
);
837 // Set configuration data for Mtftp4 instance.
839 ZeroMem (&Mtftp4Config
, sizeof (EFI_MTFTP4_CONFIG_DATA
));
840 Config
= &Mtftp4Config
;
841 Mtftp4Config
.UseDefaultSetting
= FALSE
;
842 Mtftp4Config
.TimeoutValue
= PXEBC_MTFTP_TIMEOUT
;
843 Mtftp4Config
.TryCount
= PXEBC_MTFTP_RETRIES
;
844 CopyMem (&Mtftp4Config
.StationIp
, &Private
->StationIp
.v4
, sizeof (EFI_IPv4_ADDRESS
));
845 CopyMem (&Mtftp4Config
.SubnetMask
, &Private
->SubnetMask
.v4
, sizeof (EFI_IPv4_ADDRESS
));
846 CopyMem (&Mtftp4Config
.GatewayIp
, &Private
->GatewayIp
.v4
, sizeof (EFI_IPv4_ADDRESS
));
847 CopyMem (&Mtftp4Config
.ServerIp
, &ServerIp
->v4
, sizeof (EFI_IPv4_ADDRESS
));
849 // Stop Udp4Read instance
851 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
854 Mode
->TftpErrorReceived
= FALSE
;
855 Mode
->IcmpErrorReceived
= FALSE
;
859 case EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE
:
861 // Send TFTP request to get file size.
863 Status
= PxeBcTftpGetFileSize (
873 case EFI_PXE_BASE_CODE_TFTP_READ_FILE
:
875 // Send TFTP request to read file.
877 Status
= PxeBcTftpReadFile (
889 case EFI_PXE_BASE_CODE_TFTP_WRITE_FILE
:
891 // Send TFTP request to write file.
893 Status
= PxeBcTftpWriteFile (
905 case EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY
:
907 // Send TFTP request to read directory.
909 Status
= PxeBcTftpReadDirectory (
921 case EFI_PXE_BASE_CODE_MTFTP_GET_FILE_SIZE
:
922 case EFI_PXE_BASE_CODE_MTFTP_READ_FILE
:
923 case EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY
:
924 Status
= EFI_UNSUPPORTED
;
929 Status
= EFI_INVALID_PARAMETER
;
934 if (Status
== EFI_ICMP_ERROR
) {
935 Mode
->IcmpErrorReceived
= TRUE
;
938 if (Mode
->UsingIpv6
) {
939 Status
= Private
->Udp6Read
->Configure (Private
->Udp6Read
, &Private
->Udp6CfgData
);
941 Status
= Private
->Udp4Read
->Configure (Private
->Udp4Read
, &Private
->Udp4CfgData
);
945 // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
946 // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
948 ZeroMem(&IpFilter
, sizeof (EFI_PXE_BASE_CODE_IP_FILTER
));
949 IpFilter
.Filters
= EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
;
950 This
->SetIpFilter (This
, &IpFilter
);
957 Writes a UDP packet to the network interface.
959 This function writes a UDP packet specified by the (optional HeaderPtr and)
960 BufferPtr parameters to the network interface. The UDP header is automatically
961 built by this routine. It uses the parameters OpFlags, DestIp, DestPort, GatewayIp,
962 SrcIp, and SrcPort to build this header. If the packet is successfully built and
963 transmitted through the network interface, then EFI_SUCCESS will be returned.
964 If a timeout occurs during the transmission of the packet, then EFI_TIMEOUT will
965 be returned. If an ICMP error occurs during the transmission of the packet, then
966 the IcmpErrorReceived field is set to TRUE, the IcmpError field is filled in and
967 EFI_ICMP_ERROR will be returned. If the Callback Protocol does not return
968 EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will be returned.
970 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
971 @param[in] OpFlags The UDP operation flags.
972 @param[in] DestIp The destination IP address.
973 @param[in] DestPort The destination UDP port number.
974 @param[in] GatewayIp The gateway IP address.
975 @param[in] SrcIp The source IP address.
976 @param[in, out] SrcPort The source UDP port number.
977 @param[in] HeaderSize An optional field which may be set to the length of a header
978 at HeaderPtr to be prefixed to the data at BufferPtr.
979 @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be
980 prefixed to the data at BufferPtr.
981 @param[in] BufferSize A pointer to the size of the data at BufferPtr.
982 @param[in] BufferPtr A pointer to the data to be written.
984 @retval EFI_SUCCESS The UDP Write operation completed.
985 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
986 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
987 @retval EFI_BAD_BUFFER_SIZE The buffer is too long to be transmitted.
988 @retval EFI_ABORTED The callback function aborted the UDP Write operation.
989 @retval EFI_TIMEOUT The UDP Write operation timed out.
990 @retval EFI_ICMP_ERROR An ICMP error packet was received during the UDP write session.
996 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
998 IN EFI_IP_ADDRESS
*DestIp
,
999 IN EFI_PXE_BASE_CODE_UDP_PORT
*DestPort
,
1000 IN EFI_IP_ADDRESS
*GatewayIp OPTIONAL
,
1001 IN EFI_IP_ADDRESS
*SrcIp OPTIONAL
,
1002 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPort OPTIONAL
,
1003 IN UINTN
*HeaderSize OPTIONAL
,
1004 IN VOID
*HeaderPtr OPTIONAL
,
1005 IN UINTN
*BufferSize
,
1009 PXEBC_PRIVATE_DATA
*Private
;
1010 EFI_PXE_BASE_CODE_MODE
*Mode
;
1011 EFI_UDP4_SESSION_DATA Udp4Session
;
1012 EFI_UDP6_SESSION_DATA Udp6Session
;
1014 BOOLEAN DoNotFragment
;
1016 if (This
== NULL
|| DestIp
== NULL
|| DestPort
== NULL
) {
1017 return EFI_INVALID_PARAMETER
;
1020 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1021 Mode
= Private
->PxeBc
.Mode
;
1023 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_MAY_FRAGMENT
) != 0) {
1024 DoNotFragment
= FALSE
;
1026 DoNotFragment
= TRUE
;
1029 if (!Mode
->UsingIpv6
&& GatewayIp
!= NULL
&& !NetIp4IsUnicast (NTOHL (GatewayIp
->Addr
[0]), 0)) {
1031 // Gateway is provided but it's not a unicast IPv4 address, while it will be ignored for IPv6.
1033 return EFI_INVALID_PARAMETER
;
1036 if (HeaderSize
!= NULL
&& (*HeaderSize
== 0 || HeaderPtr
== NULL
)) {
1037 return EFI_INVALID_PARAMETER
;
1040 if (BufferSize
== NULL
|| (*BufferSize
!= 0 && BufferPtr
== NULL
)) {
1041 return EFI_INVALID_PARAMETER
;
1044 if (!Mode
->Started
) {
1045 return EFI_NOT_STARTED
;
1048 if (!Private
->IsAddressOk
&& SrcIp
== NULL
) {
1049 return EFI_INVALID_PARAMETER
;
1052 if (Private
->CurSrcPort
== 0 ||
1053 (SrcPort
!= NULL
&& *SrcPort
!= Private
->CurSrcPort
)) {
1055 // Reconfigure UDPv4/UDPv6 for UdpWrite if the source port changed.
1057 if (SrcPort
!= NULL
) {
1058 Private
->CurSrcPort
= *SrcPort
;
1062 if (Mode
->UsingIpv6
) {
1063 Status
= PxeBcConfigUdp6Write (
1065 &Private
->StationIp
.v6
,
1066 &Private
->CurSrcPort
1070 // Configure the UDPv4 instance with gateway information from DHCP server as default.
1072 Status
= PxeBcConfigUdp4Write (
1074 &Private
->StationIp
.v4
,
1075 &Private
->SubnetMask
.v4
,
1076 &Private
->GatewayIp
.v4
,
1077 &Private
->CurSrcPort
,
1082 if (EFI_ERROR (Status
)) {
1083 Private
->CurSrcPort
= 0;
1084 return EFI_INVALID_PARAMETER
;
1085 } else if (SrcPort
!= NULL
) {
1086 *SrcPort
= Private
->CurSrcPort
;
1090 // Start a timer as timeout event for this blocking API.
1092 gBS
->SetTimer (Private
->UdpTimeOutEvent
, TimerRelative
, PXEBC_UDP_TIMEOUT
);
1094 if (Mode
->UsingIpv6
) {
1096 // Construct UDPv6 session data.
1098 ZeroMem (&Udp6Session
, sizeof (EFI_UDP6_SESSION_DATA
));
1099 CopyMem (&Udp6Session
.DestinationAddress
, DestIp
, sizeof (EFI_IPv6_ADDRESS
));
1100 Udp6Session
.DestinationPort
= *DestPort
;
1101 if (SrcIp
!= NULL
) {
1102 CopyMem (&Udp6Session
.SourceAddress
, SrcIp
, sizeof (EFI_IPv6_ADDRESS
));
1104 if (SrcPort
!= NULL
) {
1105 Udp6Session
.SourcePort
= *SrcPort
;
1108 Status
= PxeBcUdp6Write (
1111 Private
->UdpTimeOutEvent
,
1119 // Construct UDPv4 session data.
1121 ZeroMem (&Udp4Session
, sizeof (EFI_UDP4_SESSION_DATA
));
1122 CopyMem (&Udp4Session
.DestinationAddress
, DestIp
, sizeof (EFI_IPv4_ADDRESS
));
1123 Udp4Session
.DestinationPort
= *DestPort
;
1124 if (SrcIp
!= NULL
) {
1125 CopyMem (&Udp4Session
.SourceAddress
, SrcIp
, sizeof (EFI_IPv4_ADDRESS
));
1127 if (SrcPort
!= NULL
) {
1128 Udp4Session
.SourcePort
= *SrcPort
;
1131 // Override the gateway information if user specified.
1133 Status
= PxeBcUdp4Write (
1136 Private
->UdpTimeOutEvent
,
1137 (EFI_IPv4_ADDRESS
*) GatewayIp
,
1145 gBS
->SetTimer (Private
->UdpTimeOutEvent
, TimerCancel
, 0);
1149 // Reset the UdpWrite instance.
1151 if (Mode
->UsingIpv6
) {
1152 Private
->Udp6Write
->Configure (Private
->Udp6Write
, NULL
);
1154 Private
->Udp4Write
->Configure (Private
->Udp4Write
, NULL
);
1162 Reads a UDP packet from the network interface.
1164 This function reads a UDP packet from a network interface. The data contents
1165 are returned in (the optional HeaderPtr and) BufferPtr, and the size of the
1166 buffer received is returned in BufferSize . If the input BufferSize is smaller
1167 than the UDP packet received (less optional HeaderSize), it will be set to the
1168 required size, and EFI_BUFFER_TOO_SMALL will be returned. In this case, the
1169 contents of BufferPtr are undefined, and the packet is lost. If a UDP packet is
1170 successfully received, then EFI_SUCCESS will be returned, and the information
1171 from the UDP header will be returned in DestIp, DestPort, SrcIp, and SrcPort if
1172 they are not NULL. Depending on the values of OpFlags and the DestIp, DestPort,
1173 SrcIp, and SrcPort input values, different types of UDP packet receive filtering
1174 will be performed. The following tables summarize these receive filter operations.
1176 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1177 @param[in] OpFlags The UDP operation flags.
1178 @param[in, out] DestIp The destination IP address.
1179 @param[in, out] DestPort The destination UDP port number.
1180 @param[in, out] SrcIp The source IP address.
1181 @param[in, out] SrcPort The source UDP port number.
1182 @param[in] HeaderSize An optional field which may be set to the length of a
1183 header at HeaderPtr to be prefixed to the data at BufferPtr.
1184 @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be
1185 prefixed to the data at BufferPtr.
1186 @param[in, out] BufferSize A pointer to the size of the data at BufferPtr.
1187 @param[in] BufferPtr A pointer to the data to be read.
1189 @retval EFI_SUCCESS The UDP Read operation was completed.
1190 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1191 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1192 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1193 @retval EFI_BUFFER_TOO_SMALL The packet is larger than Buffer can hold.
1194 @retval EFI_ABORTED The callback function aborted the UDP Read operation.
1195 @retval EFI_TIMEOUT The UDP Read operation timed out.
1201 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1203 IN OUT EFI_IP_ADDRESS
*DestIp OPTIONAL
,
1204 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*DestPort OPTIONAL
,
1205 IN OUT EFI_IP_ADDRESS
*SrcIp OPTIONAL
,
1206 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPort OPTIONAL
,
1207 IN UINTN
*HeaderSize OPTIONAL
,
1208 IN VOID
*HeaderPtr OPTIONAL
,
1209 IN OUT UINTN
*BufferSize
,
1213 PXEBC_PRIVATE_DATA
*Private
;
1214 EFI_PXE_BASE_CODE_MODE
*Mode
;
1215 EFI_UDP4_COMPLETION_TOKEN Udp4Token
;
1216 EFI_UDP6_COMPLETION_TOKEN Udp6Token
;
1217 EFI_UDP4_RECEIVE_DATA
*Udp4Rx
;
1218 EFI_UDP6_RECEIVE_DATA
*Udp6Rx
;
1224 if (This
== NULL
|| DestIp
== NULL
|| DestPort
== NULL
) {
1225 return EFI_INVALID_PARAMETER
;
1228 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1229 Mode
= Private
->PxeBc
.Mode
;
1235 if (((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT
) != 0 && DestPort
== NULL
) ||
1236 ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP
) != 0 && SrcIp
== NULL
) ||
1237 ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT
) != 0 && SrcPort
== NULL
)) {
1238 return EFI_INVALID_PARAMETER
;
1241 if ((HeaderSize
!= NULL
&& *HeaderSize
== 0) || (HeaderSize
!= NULL
&& HeaderPtr
== NULL
)) {
1242 return EFI_INVALID_PARAMETER
;
1245 if ((BufferSize
== NULL
) || (BufferPtr
== NULL
)) {
1246 return EFI_INVALID_PARAMETER
;
1249 if (!Mode
->Started
) {
1250 return EFI_NOT_STARTED
;
1253 ZeroMem (&Udp6Token
, sizeof (EFI_UDP6_COMPLETION_TOKEN
));
1254 ZeroMem (&Udp4Token
, sizeof (EFI_UDP4_COMPLETION_TOKEN
));
1256 if (Mode
->UsingIpv6
) {
1257 Status
= gBS
->CreateEvent (
1264 if (EFI_ERROR (Status
)) {
1265 return EFI_OUT_OF_RESOURCES
;
1268 Status
= gBS
->CreateEvent (
1275 if (EFI_ERROR (Status
)) {
1276 return EFI_OUT_OF_RESOURCES
;
1281 // Start a timer as timeout event for this blocking API.
1283 gBS
->SetTimer (Private
->UdpTimeOutEvent
, TimerRelative
, PXEBC_UDP_TIMEOUT
);
1284 Mode
->IcmpErrorReceived
= FALSE
;
1287 // Read packet by Udp4Read/Udp6Read until matched or timeout.
1289 while (!IsMatched
&& !EFI_ERROR (Status
)) {
1290 if (Mode
->UsingIpv6
) {
1291 Status
= PxeBcUdp6Read (
1295 Private
->UdpTimeOutEvent
,
1305 Status
= PxeBcUdp4Read (
1309 Private
->UdpTimeOutEvent
,
1321 if (Status
== EFI_ICMP_ERROR
||
1322 Status
== EFI_NETWORK_UNREACHABLE
||
1323 Status
== EFI_HOST_UNREACHABLE
||
1324 Status
== EFI_PROTOCOL_UNREACHABLE
||
1325 Status
== EFI_PORT_UNREACHABLE
) {
1327 // Get different return status for icmp error from Udp, refers to UEFI spec.
1329 Mode
->IcmpErrorReceived
= TRUE
;
1331 gBS
->SetTimer (Private
->UdpTimeOutEvent
, TimerCancel
, 0);
1335 // Copy the rececived packet to user if matched by filter.
1338 if (Mode
->UsingIpv6
) {
1339 Udp6Rx
= Udp6Token
.Packet
.RxData
;
1340 ASSERT (Udp6Rx
!= NULL
);
1342 // Copy the header part of received data.
1344 if (HeaderSize
!= NULL
) {
1345 CopiedLen
= MIN (*HeaderSize
, Udp6Rx
->DataLength
);
1346 *HeaderSize
= CopiedLen
;
1347 CopyMem (HeaderPtr
, Udp6Rx
->FragmentTable
[0].FragmentBuffer
, *HeaderSize
);
1350 // Copy the other part of received data.
1352 if (Udp6Rx
->DataLength
- CopiedLen
> *BufferSize
) {
1353 Status
= EFI_BUFFER_TOO_SMALL
;
1355 *BufferSize
= Udp6Rx
->DataLength
- CopiedLen
;
1356 CopyMem (BufferPtr
, (UINT8
*) Udp6Rx
->FragmentTable
[0].FragmentBuffer
+ CopiedLen
, *BufferSize
);
1359 // Recycle the receiving buffer after copy to user.
1361 gBS
->SignalEvent (Udp6Rx
->RecycleSignal
);
1363 Udp4Rx
= Udp4Token
.Packet
.RxData
;
1364 ASSERT (Udp4Rx
!= NULL
);
1366 // Copy the header part of received data.
1368 if (HeaderSize
!= NULL
) {
1369 CopiedLen
= MIN (*HeaderSize
, Udp4Rx
->DataLength
);
1370 *HeaderSize
= CopiedLen
;
1371 CopyMem (HeaderPtr
, Udp4Rx
->FragmentTable
[0].FragmentBuffer
, *HeaderSize
);
1374 // Copy the other part of received data.
1376 if (Udp4Rx
->DataLength
- CopiedLen
> *BufferSize
) {
1377 Status
= EFI_BUFFER_TOO_SMALL
;
1379 *BufferSize
= Udp4Rx
->DataLength
- CopiedLen
;
1380 CopyMem (BufferPtr
, (UINT8
*) Udp4Rx
->FragmentTable
[0].FragmentBuffer
+ CopiedLen
, *BufferSize
);
1383 // Recycle the receiving buffer after copy to user.
1385 gBS
->SignalEvent (Udp4Rx
->RecycleSignal
);
1389 if (Mode
->UsingIpv6
) {
1390 Private
->Udp6Read
->Cancel (Private
->Udp6Read
, &Udp6Token
);
1391 gBS
->CloseEvent (Udp6Token
.Event
);
1393 Private
->Udp4Read
->Cancel (Private
->Udp4Read
, &Udp4Token
);
1394 gBS
->CloseEvent (Udp4Token
.Event
);
1402 Updates the IP receive filters of a network device and enables software filtering.
1404 The NewFilter field is used to modify the network device's current IP receive
1405 filter settings and to enable a software filter. This function updates the IpFilter
1406 field of the EFI_PXE_BASE_CODE_MODE structure with the contents of NewIpFilter.
1407 The software filter is used when the USE_FILTER in OpFlags is set to UdpRead().
1408 The current hardware filter remains in effect no matter what the settings of OpFlags.
1409 This is so that the meaning of ANY_DEST_IP set in OpFlags to UdpRead() is from those
1410 packets whose reception is enabled in hardware-physical NIC address (unicast),
1411 broadcast address, logical address or addresses (multicast), or all (promiscuous).
1412 UdpRead() does not modify the IP filter settings.
1413 Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP receive
1414 filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
1415 If an application or driver wishes to preserve the IP receive filter settings,
1416 it will have to preserve the IP receive filter settings before these calls, and
1417 use SetIpFilter() to restore them after the calls. If incompatible filtering is
1418 requested (for example, PROMISCUOUS with anything else), or if the device does not
1419 support a requested filter setting and it cannot be accommodated in software
1420 (for example, PROMISCUOUS not supported), EFI_INVALID_PARAMETER will be returned.
1421 The IPlist field is used to enable IPs other than the StationIP. They may be
1422 multicast or unicast. If IPcnt is set as well as EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP,
1423 then both the StationIP and the IPs from the IPlist will be used.
1425 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1426 @param[in] NewFilter Pointer to the new set of IP receive filters.
1428 @retval EFI_SUCCESS The IP receive filter settings were updated.
1429 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1430 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1435 EfiPxeBcSetIpFilter (
1436 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1437 IN EFI_PXE_BASE_CODE_IP_FILTER
*NewFilter
1441 PXEBC_PRIVATE_DATA
*Private
;
1442 EFI_PXE_BASE_CODE_MODE
*Mode
;
1443 EFI_UDP4_CONFIG_DATA
*Udp4Cfg
;
1444 EFI_UDP6_CONFIG_DATA
*Udp6Cfg
;
1446 BOOLEAN NeedPromiscuous
;
1447 BOOLEAN AcceptPromiscuous
;
1448 BOOLEAN AcceptBroadcast
;
1449 BOOLEAN MultiCastUpdate
;
1451 if (This
== NULL
|| NewFilter
== NULL
) {
1452 return EFI_INVALID_PARAMETER
;
1455 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1456 Mode
= Private
->PxeBc
.Mode
;
1457 Status
= EFI_SUCCESS
;
1458 NeedPromiscuous
= FALSE
;
1460 if (!Mode
->Started
) {
1461 return EFI_NOT_STARTED
;
1464 for (Index
= 0; Index
< NewFilter
->IpCnt
; Index
++) {
1465 ASSERT (Index
< EFI_PXE_BASE_CODE_MAX_IPCNT
);
1466 if (!Mode
->UsingIpv6
&&
1467 IP4_IS_LOCAL_BROADCAST (EFI_IP4 (NewFilter
->IpList
[Index
].v4
))) {
1469 // IPv4 broadcast address should not be in IP filter.
1471 return EFI_INVALID_PARAMETER
;
1473 if ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
) != 0 &&
1474 (NetIp4IsUnicast (EFI_IP4 (NewFilter
->IpList
[Index
].v4
), 0) ||
1475 NetIp6IsValidUnicast (&NewFilter
->IpList
[Index
].v6
))) {
1477 // If EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP is set and IPv4/IPv6 address
1478 // is in IpList, promiscuous mode is needed.
1480 NeedPromiscuous
= TRUE
;
1484 AcceptPromiscuous
= FALSE
;
1485 AcceptBroadcast
= FALSE
;
1486 MultiCastUpdate
= FALSE
;
1488 if (NeedPromiscuous
||
1489 (NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS
) != 0 ||
1490 (NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST
) != 0) {
1492 // Configure UDPv4/UDPv6 as promiscuous mode to receive all packets.
1494 AcceptPromiscuous
= TRUE
;
1495 } else if ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST
) != 0) {
1497 // Configure UDPv4 to receive all broadcast packets.
1499 AcceptBroadcast
= TRUE
;
1503 // In multicast condition when Promiscuous FALSE and IpCnt no-zero.
1504 // Here check if there is any update of the multicast ip address. If yes,
1505 // we need leave the old multicast group (by Config UDP instance to NULL),
1506 // and join the new multicast group.
1508 if (!AcceptPromiscuous
) {
1509 if ((NewFilter
->Filters
& EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
) != 0) {
1510 if (Mode
->IpFilter
.IpCnt
!= NewFilter
->IpCnt
) {
1511 MultiCastUpdate
= TRUE
;
1512 } else if (CompareMem (Mode
->IpFilter
.IpList
, NewFilter
->IpList
, NewFilter
->IpCnt
* sizeof (EFI_IP_ADDRESS
)) != 0 ) {
1513 MultiCastUpdate
= TRUE
;
1518 if (!Mode
->UsingIpv6
) {
1520 // Check whether we need reconfigure the UDP4 instance.
1522 Udp4Cfg
= &Private
->Udp4CfgData
;
1523 if ((AcceptPromiscuous
!= Udp4Cfg
->AcceptPromiscuous
) ||
1524 (AcceptBroadcast
!= Udp4Cfg
->AcceptBroadcast
) || MultiCastUpdate
) {
1526 // Clear the UDP4 instance configuration, all joined groups will be left
1527 // during the operation.
1529 Private
->Udp4Read
->Configure (Private
->Udp4Read
, NULL
);
1532 // Configure the UDP instance with the new configuration.
1534 Udp4Cfg
->AcceptPromiscuous
= AcceptPromiscuous
;
1535 Udp4Cfg
->AcceptBroadcast
= AcceptBroadcast
;
1536 Status
= Private
->Udp4Read
->Configure (Private
->Udp4Read
, Udp4Cfg
);
1537 if (EFI_ERROR (Status
)) {
1542 // In not Promiscuous mode, need to join the new multicast group.
1544 if (!AcceptPromiscuous
) {
1545 for (Index
= 0; Index
< NewFilter
->IpCnt
; ++Index
) {
1546 if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter
->IpList
[Index
].v4
))) {
1548 // Join the mutilcast group.
1550 Status
= Private
->Udp4Read
->Groups (Private
->Udp4Read
, TRUE
, &NewFilter
->IpList
[Index
].v4
);
1551 if (EFI_ERROR (Status
)) {
1560 // Check whether we need reconfigure the UDP6 instance.
1562 Udp6Cfg
= &Private
->Udp6CfgData
;
1563 if ((AcceptPromiscuous
!= Udp6Cfg
->AcceptPromiscuous
) || MultiCastUpdate
) {
1565 // Clear the UDP6 instance configuration, all joined groups will be left
1566 // during the operation.
1568 Private
->Udp6Read
->Configure (Private
->Udp6Read
, NULL
);
1571 // Configure the UDP instance with the new configuration.
1573 Udp6Cfg
->AcceptPromiscuous
= AcceptPromiscuous
;
1574 Status
= Private
->Udp6Read
->Configure (Private
->Udp6Read
, Udp6Cfg
);
1575 if (EFI_ERROR (Status
)) {
1580 // In not Promiscuous mode, need to join the new multicast group.
1582 if (!AcceptPromiscuous
) {
1583 for (Index
= 0; Index
< NewFilter
->IpCnt
; ++Index
) {
1584 if (IP6_IS_MULTICAST (&NewFilter
->IpList
[Index
].v6
)) {
1586 // Join the mutilcast group.
1588 Status
= Private
->Udp6Read
->Groups (Private
->Udp6Read
, TRUE
, &NewFilter
->IpList
[Index
].v6
);
1589 if (EFI_ERROR (Status
)) {
1599 // Save the new IP filter into mode data.
1601 CopyMem (&Mode
->IpFilter
, NewFilter
, sizeof (Mode
->IpFilter
));
1608 Uses the ARP protocol to resolve a MAC address. It is not supported for IPv6.
1610 This function uses the ARP protocol to resolve a MAC address. The IP address specified
1611 by IpAddr is used to resolve a MAC address. If the ARP protocol succeeds in resolving
1612 the specified address, then the ArpCacheEntries and ArpCache fields of the mode data
1613 are updated, and EFI_SUCCESS is returned. If MacAddr is not NULL, the resolved
1614 MAC address is placed there as well. If the PXE Base Code protocol is in the
1615 stopped state, then EFI_NOT_STARTED is returned. If the ARP protocol encounters
1616 a timeout condition while attempting to resolve an address, then EFI_TIMEOUT is
1617 returned. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
1618 then EFI_ABORTED is returned.
1620 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1621 @param[in] IpAddr Pointer to the IP address that is used to resolve a MAC address.
1622 @param[in] MacAddr If not NULL, a pointer to the MAC address that was resolved with the
1625 @retval EFI_SUCCESS The IP or MAC address was resolved.
1626 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1627 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1628 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1629 @retval EFI_ICMP_ERROR An error occur with the ICMP packet message.
1635 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1636 IN EFI_IP_ADDRESS
*IpAddr
,
1637 IN EFI_MAC_ADDRESS
*MacAddr OPTIONAL
1640 PXEBC_PRIVATE_DATA
*Private
;
1641 EFI_PXE_BASE_CODE_MODE
*Mode
;
1642 EFI_EVENT ResolvedEvent
;
1644 EFI_MAC_ADDRESS TempMac
;
1645 EFI_MAC_ADDRESS ZeroMac
;
1648 if (This
== NULL
|| IpAddr
== NULL
) {
1649 return EFI_INVALID_PARAMETER
;
1652 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1653 Mode
= Private
->PxeBc
.Mode
;
1654 ResolvedEvent
= NULL
;
1655 Status
= EFI_SUCCESS
;
1658 if (!Mode
->Started
) {
1659 return EFI_NOT_STARTED
;
1662 if (Mode
->UsingIpv6
) {
1663 return EFI_UNSUPPORTED
;
1667 // Station address should be ready before do arp.
1669 if (!Private
->IsAddressOk
) {
1670 return EFI_INVALID_PARAMETER
;
1673 Mode
->IcmpErrorReceived
= FALSE
;
1674 ZeroMem (&TempMac
, sizeof (EFI_MAC_ADDRESS
));
1675 ZeroMem (&ZeroMac
, sizeof (EFI_MAC_ADDRESS
));
1677 if (!Mode
->AutoArp
) {
1679 // If AutoArp is FALSE, only search in the current Arp cache.
1681 PxeBcArpCacheUpdate (NULL
, Private
);
1682 if (!PxeBcCheckArpCache (Mode
, &IpAddr
->v4
, &TempMac
)) {
1683 Status
= EFI_DEVICE_ERROR
;
1687 Status
= gBS
->CreateEvent (
1694 if (EFI_ERROR (Status
)) {
1699 // If AutoArp is TRUE, try to send Arp request on initiative.
1701 Status
= Private
->Arp
->Request (Private
->Arp
, &IpAddr
->v4
, ResolvedEvent
, &TempMac
);
1702 if (EFI_ERROR (Status
) && Status
!= EFI_NOT_READY
) {
1706 while (!IsResolved
) {
1707 if (CompareMem (&TempMac
, &ZeroMac
, sizeof (EFI_MAC_ADDRESS
)) != 0) {
1711 if (CompareMem (&TempMac
, &ZeroMac
, sizeof (EFI_MAC_ADDRESS
)) != 0) {
1712 Status
= EFI_SUCCESS
;
1714 Status
= EFI_TIMEOUT
;
1719 // Copy the Mac address to user if needed.
1721 if (MacAddr
!= NULL
&& !EFI_ERROR (Status
)) {
1722 CopyMem (MacAddr
, &TempMac
, sizeof (EFI_MAC_ADDRESS
));
1726 if (ResolvedEvent
!= NULL
) {
1727 gBS
->CloseEvent (ResolvedEvent
);
1734 Updates the parameters that affect the operation of the PXE Base Code Protocol.
1736 This function sets parameters that affect the operation of the PXE Base Code Protocol.
1737 The parameter specified by NewAutoArp is used to control the generation of ARP
1738 protocol packets. If NewAutoArp is TRUE, then ARP Protocol packets will be generated
1739 as required by the PXE Base Code Protocol. If NewAutoArp is FALSE, then no ARP
1740 Protocol packets will be generated. In this case, the only mappings that are
1741 available are those stored in the ArpCache of the EFI_PXE_BASE_CODE_MODE structure.
1742 If there are not enough mappings in the ArpCache to perform a PXE Base Code Protocol
1743 service, then the service will fail. This function updates the AutoArp field of
1744 the EFI_PXE_BASE_CODE_MODE structure to NewAutoArp.
1745 The SetParameters() call must be invoked after a Callback Protocol is installed
1746 to enable the use of callbacks.
1748 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1749 @param[in] NewAutoArp If not NULL, a pointer to a value that specifies whether to replace the
1750 current value of AutoARP.
1751 @param[in] NewSendGUID If not NULL, a pointer to a value that specifies whether to replace the
1752 current value of SendGUID.
1753 @param[in] NewTTL If not NULL, a pointer to be used in place of the current value of TTL,
1754 the "time to live" field of the IP header.
1755 @param[in] NewToS If not NULL, a pointer to be used in place of the current value of ToS,
1756 the "type of service" field of the IP header.
1757 @param[in] NewMakeCallback If not NULL, a pointer to a value that specifies whether to replace the
1758 current value of the MakeCallback field of the Mode structure.
1760 @retval EFI_SUCCESS The new parameters values were updated.
1761 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1762 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1767 EfiPxeBcSetParameters (
1768 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1769 IN BOOLEAN
*NewAutoArp OPTIONAL
,
1770 IN BOOLEAN
*NewSendGUID OPTIONAL
,
1771 IN UINT8
*NewTTL OPTIONAL
,
1772 IN UINT8
*NewToS OPTIONAL
,
1773 IN BOOLEAN
*NewMakeCallback OPTIONAL
1776 PXEBC_PRIVATE_DATA
*Private
;
1777 EFI_PXE_BASE_CODE_MODE
*Mode
;
1778 EFI_GUID SystemGuid
;
1782 return EFI_INVALID_PARAMETER
;
1785 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1786 Mode
= Private
->PxeBc
.Mode
;
1788 if (!Mode
->Started
) {
1789 return EFI_NOT_STARTED
;
1792 if (NewMakeCallback
!= NULL
) {
1793 if (*NewMakeCallback
) {
1795 // Update the previous PxeBcCallback protocol.
1797 Status
= gBS
->HandleProtocol (
1798 Private
->Controller
,
1799 &gEfiPxeBaseCodeCallbackProtocolGuid
,
1800 (VOID
**) &Private
->PxeBcCallback
1803 if (EFI_ERROR (Status
) || (Private
->PxeBcCallback
->Callback
== NULL
)) {
1804 return EFI_INVALID_PARAMETER
;
1807 Private
->PxeBcCallback
= NULL
;
1809 Mode
->MakeCallbacks
= *NewMakeCallback
;
1812 if (NewSendGUID
!= NULL
) {
1813 if (*NewSendGUID
&& EFI_ERROR (NetLibGetSystemGuid (&SystemGuid
))) {
1814 return EFI_INVALID_PARAMETER
;
1816 Mode
->SendGUID
= *NewSendGUID
;
1819 if (NewAutoArp
!= NULL
) {
1820 Mode
->AutoArp
= *NewAutoArp
;
1823 if (NewTTL
!= NULL
) {
1824 Mode
->TTL
= *NewTTL
;
1827 if (NewToS
!= NULL
) {
1828 Mode
->ToS
= *NewToS
;
1836 Updates the station IP address and/or subnet mask values of a network device.
1838 This function updates the station IP address and/or subnet mask values of a network
1839 device. The NewStationIp field is used to modify the network device's current IP address.
1840 If NewStationIP is NULL, then the current IP address will not be modified. Otherwise,
1841 this function updates the StationIp field of the EFI_PXE_BASE_CODE_MODE structure
1842 with NewStationIp. The NewSubnetMask field is used to modify the network device's current subnet
1843 mask. If NewSubnetMask is NULL, then the current subnet mask will not be modified.
1844 Otherwise, this function updates the SubnetMask field of the EFI_PXE_BASE_CODE_MODE
1845 structure with NewSubnetMask.
1847 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1848 @param[in] NewStationIp Pointer to the new IP address to be used by the network device.
1849 @param[in] NewSubnetMask Pointer to the new subnet mask to be used by the network device.
1851 @retval EFI_SUCCESS The new station IP address and/or subnet mask were updated.
1852 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1853 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
1858 EfiPxeBcSetStationIP (
1859 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1860 IN EFI_IP_ADDRESS
*NewStationIp OPTIONAL
,
1861 IN EFI_IP_ADDRESS
*NewSubnetMask OPTIONAL
1865 PXEBC_PRIVATE_DATA
*Private
;
1866 EFI_PXE_BASE_CODE_MODE
*Mode
;
1867 EFI_ARP_CONFIG_DATA ArpConfigData
;
1870 return EFI_INVALID_PARAMETER
;
1873 if (NewStationIp
!= NULL
&&
1874 (!NetIp4IsUnicast (NTOHL (NewStationIp
->Addr
[0]), 0) &&
1875 !NetIp6IsValidUnicast (&NewStationIp
->v6
))) {
1876 return EFI_INVALID_PARAMETER
;
1879 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1880 Mode
= Private
->PxeBc
.Mode
;
1881 Status
= EFI_SUCCESS
;
1883 if (!Mode
->UsingIpv6
&&
1884 NewSubnetMask
!= NULL
&&
1885 !IP4_IS_VALID_NETMASK (NTOHL (NewSubnetMask
->Addr
[0]))) {
1886 return EFI_INVALID_PARAMETER
;
1889 if (!Mode
->Started
) {
1890 return EFI_NOT_STARTED
;
1893 if (Mode
->UsingIpv6
&& NewStationIp
!= NULL
) {
1895 // Set the IPv6 address by Ip6Config protocol.
1897 Status
= PxeBcRegisterIp6Address (Private
, &NewStationIp
->v6
);
1898 if (EFI_ERROR (Status
)) {
1901 } else if (!Mode
->UsingIpv6
&& NewStationIp
!= NULL
) {
1903 // Configure the corresponding ARP with the IPv4 address.
1905 ZeroMem (&ArpConfigData
, sizeof (EFI_ARP_CONFIG_DATA
));
1907 ArpConfigData
.SwAddressType
= 0x0800;
1908 ArpConfigData
.SwAddressLength
= (UINT8
) sizeof (EFI_IPv4_ADDRESS
);
1909 ArpConfigData
.StationAddress
= &NewStationIp
->v4
;
1911 Private
->Arp
->Configure (Private
->Arp
, NULL
);
1912 Private
->Arp
->Configure (Private
->Arp
, &ArpConfigData
);
1914 if (NewSubnetMask
!= NULL
) {
1915 Mode
->RouteTableEntries
= 1;
1916 Mode
->RouteTable
[0].IpAddr
.Addr
[0] = NewStationIp
->Addr
[0] & NewSubnetMask
->Addr
[0];
1917 Mode
->RouteTable
[0].SubnetMask
.Addr
[0] = NewSubnetMask
->Addr
[0];
1918 Mode
->RouteTable
[0].GwAddr
.Addr
[0] = 0;
1921 Private
->IsAddressOk
= TRUE
;
1924 if (NewStationIp
!= NULL
) {
1925 CopyMem (&Mode
->StationIp
, NewStationIp
, sizeof (EFI_IP_ADDRESS
));
1926 CopyMem (&Private
->StationIp
, NewStationIp
, sizeof (EFI_IP_ADDRESS
));
1929 if (!Mode
->UsingIpv6
&& NewSubnetMask
!= NULL
) {
1930 CopyMem (&Mode
->SubnetMask
, NewSubnetMask
, sizeof (EFI_IP_ADDRESS
));
1931 CopyMem (&Private
->SubnetMask
,NewSubnetMask
, sizeof (EFI_IP_ADDRESS
));
1934 Status
= PxeBcFlushStaionIp (Private
, NewStationIp
, NewSubnetMask
);
1941 Updates the contents of the cached DHCP and Discover packets.
1943 The pointers to the new packets are used to update the contents of the cached
1944 packets in the EFI_PXE_BASE_CODE_MODE structure.
1946 @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
1947 @param[in] NewDhcpDiscoverValid Pointer to a value that will replace the current
1948 DhcpDiscoverValid field.
1949 @param[in] NewDhcpAckReceived Pointer to a value that will replace the current
1950 DhcpAckReceived field.
1951 @param[in] NewProxyOfferReceived Pointer to a value that will replace the current
1952 ProxyOfferReceived field.
1953 @param[in] NewPxeDiscoverValid Pointer to a value that will replace the current
1954 ProxyOfferReceived field.
1955 @param[in] NewPxeReplyReceived Pointer to a value that will replace the current
1956 PxeReplyReceived field.
1957 @param[in] NewPxeBisReplyReceived Pointer to a value that will replace the current
1958 PxeBisReplyReceived field.
1959 @param[in] NewDhcpDiscover Pointer to the new cached DHCP Discover packet contents.
1960 @param[in] NewDhcpAck Pointer to the new cached DHCP Ack packet contents.
1961 @param[in] NewProxyOffer Pointer to the new cached Proxy Offer packet contents.
1962 @param[in] NewPxeDiscover Pointer to the new cached PXE Discover packet contents.
1963 @param[in] NewPxeReply Pointer to the new cached PXE Reply packet contents.
1964 @param[in] NewPxeBisReply Pointer to the new cached PXE BIS Reply packet contents.
1966 @retval EFI_SUCCESS The cached packet contents were updated.
1967 @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
1968 @retval EFI_INVALID_PARAMETER This is NULL or does not point to a valid
1969 EFI_PXE_BASE_CODE_PROTOCOL structure.
1974 EfiPxeBcSetPackets (
1975 IN EFI_PXE_BASE_CODE_PROTOCOL
*This
,
1976 IN BOOLEAN
*NewDhcpDiscoverValid OPTIONAL
,
1977 IN BOOLEAN
*NewDhcpAckReceived OPTIONAL
,
1978 IN BOOLEAN
*NewProxyOfferReceived OPTIONAL
,
1979 IN BOOLEAN
*NewPxeDiscoverValid OPTIONAL
,
1980 IN BOOLEAN
*NewPxeReplyReceived OPTIONAL
,
1981 IN BOOLEAN
*NewPxeBisReplyReceived OPTIONAL
,
1982 IN EFI_PXE_BASE_CODE_PACKET
*NewDhcpDiscover OPTIONAL
,
1983 IN EFI_PXE_BASE_CODE_PACKET
*NewDhcpAck OPTIONAL
,
1984 IN EFI_PXE_BASE_CODE_PACKET
*NewProxyOffer OPTIONAL
,
1985 IN EFI_PXE_BASE_CODE_PACKET
*NewPxeDiscover OPTIONAL
,
1986 IN EFI_PXE_BASE_CODE_PACKET
*NewPxeReply OPTIONAL
,
1987 IN EFI_PXE_BASE_CODE_PACKET
*NewPxeBisReply OPTIONAL
1990 PXEBC_PRIVATE_DATA
*Private
;
1991 EFI_PXE_BASE_CODE_MODE
*Mode
;
1994 return EFI_INVALID_PARAMETER
;
1997 Private
= PXEBC_PRIVATE_DATA_FROM_PXEBC (This
);
1998 Mode
= Private
->PxeBc
.Mode
;
2000 if (!Mode
->Started
) {
2001 return EFI_NOT_STARTED
;
2004 if (NewDhcpDiscoverValid
!= NULL
) {
2005 Mode
->DhcpDiscoverValid
= *NewDhcpDiscoverValid
;
2008 if (NewDhcpAckReceived
!= NULL
) {
2009 Mode
->DhcpAckReceived
= *NewDhcpAckReceived
;
2012 if (NewProxyOfferReceived
!= NULL
) {
2013 Mode
->ProxyOfferReceived
= *NewProxyOfferReceived
;
2016 if (NewPxeDiscoverValid
!= NULL
) {
2017 Mode
->PxeDiscoverValid
= *NewPxeDiscoverValid
;
2020 if (NewPxeReplyReceived
!= NULL
) {
2021 Mode
->PxeReplyReceived
= *NewPxeReplyReceived
;
2024 if (NewPxeBisReplyReceived
!= NULL
) {
2025 Mode
->PxeBisReplyReceived
= *NewPxeBisReplyReceived
;
2028 if (NewDhcpDiscover
!= NULL
) {
2029 CopyMem (&Mode
->DhcpDiscover
, NewDhcpDiscover
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2032 if (NewDhcpAck
!= NULL
) {
2033 CopyMem (&Mode
->DhcpAck
, NewDhcpAck
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2036 if (NewProxyOffer
!= NULL
) {
2037 CopyMem (&Mode
->ProxyOffer
, NewProxyOffer
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2040 if (NewPxeDiscover
!= NULL
) {
2041 CopyMem (&Mode
->PxeDiscover
, NewPxeDiscover
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2044 if (NewPxeReply
!= NULL
) {
2045 CopyMem (&Mode
->PxeReply
, NewPxeReply
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2048 if (NewPxeBisReply
!= NULL
) {
2049 CopyMem (&Mode
->PxeBisReply
, NewPxeBisReply
, sizeof (EFI_PXE_BASE_CODE_PACKET
));
2055 EFI_PXE_BASE_CODE_PROTOCOL gPxeBcProtocolTemplate
= {
2056 EFI_PXE_BASE_CODE_PROTOCOL_REVISION
,
2064 EfiPxeBcSetIpFilter
,
2066 EfiPxeBcSetParameters
,
2067 EfiPxeBcSetStationIP
,
2074 Callback function that is invoked when the PXE Base Code Protocol is about to transmit, has
2075 received, or is waiting to receive a packet.
2077 This function is invoked when the PXE Base Code Protocol is about to transmit, has received,
2078 or is waiting to receive a packet. Parameters Function and Received specify the type of event.
2079 Parameters PacketLen and Packet specify the packet that generated the event. If these fields
2080 are zero and NULL respectively, then this is a status update callback. If the operation specified
2081 by Function is to continue, then CALLBACK_STATUS_CONTINUE should be returned. If the operation
2082 specified by Function should be aborted, then CALLBACK_STATUS_ABORT should be returned. Due to
2083 the polling nature of UEFI device drivers, a callback function should not execute for more than 5 ms.
2084 The SetParameters() function must be called after a Callback Protocol is installed to enable the
2087 @param[in] This Pointer to the EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL instance.
2088 @param[in] Function The PXE Base Code Protocol function that is waiting for an event.
2089 @param[in] Received TRUE if the callback is being invoked due to a receive event. FALSE if
2090 the callback is being invoked due to a transmit event.
2091 @param[in] PacketLength The length, in bytes, of Packet. This field will have a value of zero if
2092 this is a wait for receive event.
2093 @param[in] PacketPtr If Received is TRUE, a pointer to the packet that was just received;
2094 otherwise a pointer to the packet that is about to be transmitted.
2096 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE If Function specifies a continue operation.
2097 @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT If Function specifies an abort operation.
2100 EFI_PXE_BASE_CODE_CALLBACK_STATUS
2102 EfiPxeLoadFileCallback (
2103 IN EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
*This
,
2104 IN EFI_PXE_BASE_CODE_FUNCTION Function
,
2105 IN BOOLEAN Received
,
2106 IN UINT32 PacketLength
,
2107 IN EFI_PXE_BASE_CODE_PACKET
*PacketPtr OPTIONAL
2114 // Catch Ctrl-C or ESC to abort.
2116 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
2118 if (!EFI_ERROR (Status
)) {
2120 if (Key
.ScanCode
== SCAN_ESC
|| Key
.UnicodeChar
== (0x1F & 'c')) {
2122 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT
;
2126 // No print if receive packet
2129 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
;
2132 // Print only for three functions
2136 case EFI_PXE_BASE_CODE_FUNCTION_MTFTP
:
2138 // Print only for open MTFTP packets, not every MTFTP packets
2140 if (PacketLength
!= 0 && PacketPtr
!= NULL
) {
2141 if (PacketPtr
->Raw
[0x1C] != 0x00 || PacketPtr
->Raw
[0x1D] != 0x01) {
2142 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
;
2147 case EFI_PXE_BASE_CODE_FUNCTION_DHCP
:
2148 case EFI_PXE_BASE_CODE_FUNCTION_DISCOVER
:
2152 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
;
2155 if (PacketLength
!= 0 && PacketPtr
!= NULL
) {
2157 // Print '.' when transmit a packet
2162 return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
;
2165 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL gPxeBcCallBackTemplate
= {
2166 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL_REVISION
,
2167 EfiPxeLoadFileCallback
2172 Causes the driver to load a specified file.
2174 @param[in] This Protocol instance pointer.
2175 @param[in] FilePath The device specific path of the file to load.
2176 @param[in] BootPolicy If TRUE, indicates that the request originates from the
2177 boot manager is attempting to load FilePath as a boot
2178 selection. If FALSE, then FilePath must match an exact file
2180 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
2181 code of EFI_SUCCESS, the amount of data transferred to
2182 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
2183 the size of Buffer required to retrieve the requested file.
2184 @param[in] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
2185 then no the size of the requested file is returned in
2188 @retval EFI_SUCCESS The file was loaded.
2189 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy.
2190 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or
2192 @retval EFI_NO_MEDIA No medium was present to load the file.
2193 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.
2194 @retval EFI_NO_RESPONSE The remote system did not respond.
2195 @retval EFI_NOT_FOUND The file was not found.
2196 @retval EFI_ABORTED The file load process was manually cancelled.
2202 IN EFI_LOAD_FILE_PROTOCOL
*This
,
2203 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
2204 IN BOOLEAN BootPolicy
,
2205 IN OUT UINTN
*BufferSize
,
2206 IN VOID
*Buffer OPTIONAL
2209 PXEBC_PRIVATE_DATA
*Private
;
2210 PXEBC_VIRTUAL_NIC
*VirtualNic
;
2211 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBc
;
2214 BOOLEAN MediaPresent
;
2216 VirtualNic
= PXEBC_VIRTUAL_NIC_FROM_LOADFILE (This
);
2217 Private
= VirtualNic
->Private
;
2218 PxeBc
= &Private
->PxeBc
;
2220 Status
= EFI_DEVICE_ERROR
;
2222 if (This
== NULL
|| BufferSize
== NULL
) {
2223 return EFI_INVALID_PARAMETER
;
2227 // Only support BootPolicy
2230 return EFI_UNSUPPORTED
;
2234 // Check media status before PXE start
2236 MediaPresent
= TRUE
;
2237 NetLibDetectMedia (Private
->Controller
, &MediaPresent
);
2238 if (!MediaPresent
) {
2239 return EFI_NO_MEDIA
;
2243 // Check whether the virtual nic is using IPv6 or not.
2245 if (VirtualNic
== Private
->Ip6Nic
) {
2250 // Start Pxe Base Code to initialize PXE boot.
2252 Status
= PxeBc
->Start (PxeBc
, UsingIpv6
);
2253 if (Status
== EFI_SUCCESS
|| Status
== EFI_ALREADY_STARTED
) {
2254 Status
= PxeBcLoadBootFile (Private
, BufferSize
, Buffer
);
2257 if (Status
!= EFI_SUCCESS
&&
2258 Status
!= EFI_UNSUPPORTED
&&
2259 Status
!= EFI_BUFFER_TOO_SMALL
) {
2261 // There are three cases, which needn't stop pxebc here.
2262 // 1. success to download file.
2263 // 2. success to get file size.
2266 PxeBc
->Stop (PxeBc
);
2272 EFI_LOAD_FILE_PROTOCOL gLoadFileProtocolTemplate
= { EfiPxeLoadFile
};