2 Support functions implementation for UefiPxeBc Driver.
4 Copyright (c) 2007 - 2016, 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 Flush the previous configration using the new station Ip address.
22 @param[in] Private The pointer to the PxeBc private data.
23 @param[in] StationIp The pointer to the station Ip address.
24 @param[in] SubnetMask The pointer to the subnet mask address for v4.
26 @retval EFI_SUCCESS Successfully flushed the previous configuration.
27 @retval Others Failed to flush using the new station Ip.
32 PXEBC_PRIVATE_DATA
*Private
,
33 EFI_IP_ADDRESS
*StationIp
,
34 EFI_IP_ADDRESS
*SubnetMask OPTIONAL
37 EFI_PXE_BASE_CODE_MODE
*Mode
;
40 ASSERT (StationIp
!= NULL
);
42 Mode
= Private
->PxeBc
.Mode
;
45 if (Mode
->UsingIpv6
) {
47 CopyMem (&Private
->Udp6CfgData
.StationAddress
, StationIp
, sizeof (EFI_IPv6_ADDRESS
));
48 CopyMem (&Private
->Ip6CfgData
.StationAddress
, StationIp
, sizeof (EFI_IPv6_ADDRESS
));
51 // Reconfigure the Ip6 instance to capture background ICMP6 packets with new station Ip address.
53 Private
->Ip6
->Cancel (Private
->Ip6
, &Private
->Icmp6Token
);
54 Private
->Ip6
->Configure (Private
->Ip6
, NULL
);
56 Status
= Private
->Ip6
->Configure (Private
->Ip6
, &Private
->Ip6CfgData
);
57 if (EFI_ERROR (Status
)) {
61 Status
= Private
->Ip6
->Receive (Private
->Ip6
, &Private
->Icmp6Token
);
63 ASSERT (SubnetMask
!= NULL
);
64 CopyMem (&Private
->Udp4CfgData
.StationAddress
, StationIp
, sizeof (EFI_IPv4_ADDRESS
));
65 CopyMem (&Private
->Udp4CfgData
.SubnetMask
, SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
66 CopyMem (&Private
->Ip4CfgData
.StationAddress
, StationIp
, sizeof (EFI_IPv4_ADDRESS
));
67 CopyMem (&Private
->Ip4CfgData
.SubnetMask
, SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
70 // Reconfigure the Ip4 instance to capture background ICMP packets with new station Ip address.
72 Private
->Ip4
->Cancel (Private
->Ip4
, &Private
->IcmpToken
);
73 Private
->Ip4
->Configure (Private
->Ip4
, NULL
);
75 Status
= Private
->Ip4
->Configure (Private
->Ip4
, &Private
->Ip4CfgData
);
76 if (EFI_ERROR (Status
)) {
80 Status
= Private
->Ip4
->Receive (Private
->Ip4
, &Private
->IcmpToken
);
89 Notify the callback function when an event is triggered.
91 @param[in] Event The triggered event.
92 @param[in] Context The opaque parameter to the function.
102 *((BOOLEAN
*) Context
) = TRUE
;
107 Do arp resolution from arp cache in PxeBcMode.
109 @param Mode The pointer to EFI_PXE_BASE_CODE_MODE.
110 @param Ip4Addr The Ip4 address for resolution.
111 @param MacAddress The resoluted MAC address if the resolution is successful.
112 The value is undefined if the resolution fails.
114 @retval TRUE Found an matched entry.
115 @retval FALSE Did not find a matched entry.
120 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
121 IN EFI_IPv4_ADDRESS
*Ip4Addr
,
122 OUT EFI_MAC_ADDRESS
*MacAddress
127 ASSERT (!Mode
->UsingIpv6
);
130 // Check whether the current Arp cache in mode data contains this information or not.
132 for (Index
= 0; Index
< Mode
->ArpCacheEntries
; Index
++) {
133 if (EFI_IP4_EQUAL (&Mode
->ArpCache
[Index
].IpAddr
.v4
, Ip4Addr
)) {
136 &Mode
->ArpCache
[Index
].MacAddr
,
137 sizeof (EFI_MAC_ADDRESS
)
148 Update the arp cache periodically.
150 @param Event The pointer to EFI_PXE_BC_PROTOCOL.
151 @param Context Context of the timer event.
156 PxeBcArpCacheUpdate (
161 PXEBC_PRIVATE_DATA
*Private
;
162 EFI_PXE_BASE_CODE_MODE
*Mode
;
163 EFI_ARP_FIND_DATA
*ArpEntry
;
169 Private
= (PXEBC_PRIVATE_DATA
*) Context
;
170 Mode
= Private
->PxeBc
.Mode
;
172 ASSERT (!Mode
->UsingIpv6
);
175 // Get the current Arp cache from Arp driver.
177 Status
= Private
->Arp
->Find (
186 if (EFI_ERROR (Status
)) {
191 // Update the Arp cache in mode data.
193 Mode
->ArpCacheEntries
= MIN (EntryCount
, EFI_PXE_BASE_CODE_MAX_ARP_ENTRIES
);
195 for (Index
= 0; Index
< Mode
->ArpCacheEntries
; Index
++) {
197 &Mode
->ArpCache
[Index
].IpAddr
,
199 ArpEntry
->SwAddressLength
202 &Mode
->ArpCache
[Index
].MacAddr
,
203 (UINT8
*) (ArpEntry
+ 1) + ArpEntry
->SwAddressLength
,
204 ArpEntry
->HwAddressLength
206 ArpEntry
= (EFI_ARP_FIND_DATA
*) ((UINT8
*) ArpEntry
+ EntryLength
);
212 Notify function to handle the received ICMP message in DPC.
214 @param Context The PXEBC private data.
219 PxeBcIcmpErrorDpcHandle (
224 EFI_IP4_RECEIVE_DATA
*RxData
;
225 EFI_IP4_PROTOCOL
*Ip4
;
226 PXEBC_PRIVATE_DATA
*Private
;
227 EFI_PXE_BASE_CODE_MODE
*Mode
;
233 Private
= (PXEBC_PRIVATE_DATA
*) Context
;
234 Mode
= &Private
->Mode
;
235 Status
= Private
->IcmpToken
.Status
;
236 RxData
= Private
->IcmpToken
.Packet
.RxData
;
239 ASSERT (!Mode
->UsingIpv6
);
241 if (Status
== EFI_ABORTED
) {
243 // It's triggered by user cancellation.
248 if (RxData
== NULL
) {
252 if (Status
!= EFI_ICMP_ERROR
) {
254 // The return status should be recognized as EFI_ICMP_ERROR.
256 gBS
->SignalEvent (RxData
->RecycleSignal
);
260 if (EFI_IP4 (RxData
->Header
->SourceAddress
) != 0 &&
261 !NetIp4IsUnicast (EFI_NTOHL (RxData
->Header
->SourceAddress
), 0)) {
263 // The source address of the received packet should be a valid unicast address.
265 gBS
->SignalEvent (RxData
->RecycleSignal
);
269 if (!EFI_IP4_EQUAL (&RxData
->Header
->DestinationAddress
, &Mode
->StationIp
.v4
)) {
271 // The destination address of the received packet should be equal to the host address.
273 gBS
->SignalEvent (RxData
->RecycleSignal
);
277 if (RxData
->Header
->Protocol
!= EFI_IP_PROTO_ICMP
) {
279 // The protocol value in the header of the receveid packet should be EFI_IP_PROTO_ICMP.
281 gBS
->SignalEvent (RxData
->RecycleSignal
);
285 Type
= *((UINT8
*) RxData
->FragmentTable
[0].FragmentBuffer
);
287 if (Type
!= ICMP_DEST_UNREACHABLE
&&
288 Type
!= ICMP_SOURCE_QUENCH
&&
289 Type
!= ICMP_REDIRECT
&&
290 Type
!= ICMP_TIME_EXCEEDED
&&
291 Type
!= ICMP_PARAMETER_PROBLEM
) {
293 // The type of the receveid ICMP message should be ICMP_ERROR_MESSAGE.
295 gBS
->SignalEvent (RxData
->RecycleSignal
);
300 // Copy the right ICMP error message into mode data.
303 IcmpError
= (UINT8
*) &Mode
->IcmpError
;
305 for (Index
= 0; Index
< RxData
->FragmentCount
; Index
++) {
306 CopiedLen
+= RxData
->FragmentTable
[Index
].FragmentLength
;
307 if (CopiedLen
<= sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR
)) {
310 RxData
->FragmentTable
[Index
].FragmentBuffer
,
311 RxData
->FragmentTable
[Index
].FragmentLength
316 RxData
->FragmentTable
[Index
].FragmentBuffer
,
317 CopiedLen
- sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR
)
320 IcmpError
+= CopiedLen
;
324 Private
->IcmpToken
.Status
= EFI_NOT_READY
;
325 Ip4
->Receive (Ip4
, &Private
->IcmpToken
);
330 Callback function to update the latest ICMP6 error message.
332 @param Event The event signalled.
333 @param Context The context passed in using the event notifier.
338 PxeBcIcmpErrorUpdate (
343 QueueDpc (TPL_CALLBACK
, PxeBcIcmpErrorDpcHandle
, Context
);
348 Notify function to handle the received ICMP6 message in DPC.
350 @param Context The PXEBC private data.
355 PxeBcIcmp6ErrorDpcHandle (
359 PXEBC_PRIVATE_DATA
*Private
;
360 EFI_IP6_RECEIVE_DATA
*RxData
;
361 EFI_IP6_PROTOCOL
*Ip6
;
362 EFI_PXE_BASE_CODE_MODE
*Mode
;
369 Private
= (PXEBC_PRIVATE_DATA
*) Context
;
370 Mode
= &Private
->Mode
;
371 Status
= Private
->Icmp6Token
.Status
;
372 RxData
= Private
->Icmp6Token
.Packet
.RxData
;
375 ASSERT (Mode
->UsingIpv6
);
377 if (Status
== EFI_ABORTED
) {
379 // It's triggered by user cancellation.
384 if (RxData
== NULL
) {
388 if (Status
!= EFI_ICMP_ERROR
) {
390 // The return status should be recognized as EFI_ICMP_ERROR.
392 gBS
->SignalEvent (RxData
->RecycleSignal
);
396 if (!NetIp6IsValidUnicast (&RxData
->Header
->SourceAddress
)) {
398 // The source address of the received packet should be a valid unicast address.
400 gBS
->SignalEvent (RxData
->RecycleSignal
);
404 if (!NetIp6IsUnspecifiedAddr (&Mode
->StationIp
.v6
) &&
405 !EFI_IP6_EQUAL (&RxData
->Header
->DestinationAddress
, &Mode
->StationIp
.v6
)) {
407 // The destination address of the received packet should be equal to the host address.
409 gBS
->SignalEvent (RxData
->RecycleSignal
);
413 if (RxData
->Header
->NextHeader
!= IP6_ICMP
) {
415 // The nextheader in the header of the receveid packet should be IP6_ICMP.
417 gBS
->SignalEvent (RxData
->RecycleSignal
);
421 Type
= *((UINT8
*) RxData
->FragmentTable
[0].FragmentBuffer
);
423 if (Type
!= ICMP_V6_DEST_UNREACHABLE
&&
424 Type
!= ICMP_V6_PACKET_TOO_BIG
&&
425 Type
!= ICMP_V6_PACKET_TOO_BIG
&&
426 Type
!= ICMP_V6_PARAMETER_PROBLEM
) {
428 // The type of the receveid packet should be an ICMP6 error message.
430 gBS
->SignalEvent (RxData
->RecycleSignal
);
435 // Copy the right ICMP6 error message into mode data.
438 Icmp6Error
= (UINT8
*) &Mode
->IcmpError
;
440 for (Index
= 0; Index
< RxData
->FragmentCount
; Index
++) {
441 CopiedLen
+= RxData
->FragmentTable
[Index
].FragmentLength
;
442 if (CopiedLen
<= sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR
)) {
445 RxData
->FragmentTable
[Index
].FragmentBuffer
,
446 RxData
->FragmentTable
[Index
].FragmentLength
451 RxData
->FragmentTable
[Index
].FragmentBuffer
,
452 CopiedLen
- sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR
)
455 Icmp6Error
+= CopiedLen
;
459 Private
->Icmp6Token
.Status
= EFI_NOT_READY
;
460 Ip6
->Receive (Ip6
, &Private
->Icmp6Token
);
465 Callback function to update the latest ICMP6 error message.
467 @param Event The event signalled.
468 @param Context The context passed in using the event notifier.
473 PxeBcIcmp6ErrorUpdate (
478 QueueDpc (TPL_CALLBACK
, PxeBcIcmp6ErrorDpcHandle
, Context
);
483 This function is to configure a UDPv4 instance for UdpWrite.
485 @param[in] Udp4 The pointer to EFI_UDP4_PROTOCOL.
486 @param[in] StationIp The pointer to the station address.
487 @param[in] SubnetMask The pointer to the subnet mask.
488 @param[in] Gateway The pointer to the gateway address.
489 @param[in, out] SrcPort The pointer to the source port.
490 @param[in] DoNotFragment If TRUE, fragment is not enabled.
491 Otherwise, fragment is enabled.
492 @param[in] TTL The time to live field of the IP header.
493 @param[in] ToS The type of service field of the IP header.
495 @retval EFI_SUCCESS Successfully configured this instance.
496 @retval Others Failed to configure this instance.
500 PxeBcConfigUdp4Write (
501 IN EFI_UDP4_PROTOCOL
*Udp4
,
502 IN EFI_IPv4_ADDRESS
*StationIp
,
503 IN EFI_IPv4_ADDRESS
*SubnetMask
,
504 IN EFI_IPv4_ADDRESS
*Gateway
,
505 IN OUT UINT16
*SrcPort
,
506 IN BOOLEAN DoNotFragment
,
511 EFI_UDP4_CONFIG_DATA Udp4CfgData
;
514 ZeroMem (&Udp4CfgData
, sizeof (Udp4CfgData
));
516 Udp4CfgData
.TransmitTimeout
= PXEBC_DEFAULT_LIFETIME
;
517 Udp4CfgData
.ReceiveTimeout
= PXEBC_DEFAULT_LIFETIME
;
518 Udp4CfgData
.TypeOfService
= ToS
;
519 Udp4CfgData
.TimeToLive
= TTL
;
520 Udp4CfgData
.AllowDuplicatePort
= TRUE
;
521 Udp4CfgData
.DoNotFragment
= DoNotFragment
;
523 CopyMem (&Udp4CfgData
.StationAddress
, StationIp
, sizeof (*StationIp
));
524 CopyMem (&Udp4CfgData
.SubnetMask
, SubnetMask
, sizeof (*SubnetMask
));
526 Udp4CfgData
.StationPort
= *SrcPort
;
529 // Reset the UDPv4 instance.
531 Udp4
->Configure (Udp4
, NULL
);
533 Status
= Udp4
->Configure (Udp4
, &Udp4CfgData
);
534 if (!EFI_ERROR (Status
) && !EFI_IP4_EQUAL (Gateway
, &mZeroIp4Addr
)) {
536 // The basic configuration is OK, need to add the default route entry
538 Status
= Udp4
->Routes (Udp4
, FALSE
, &mZeroIp4Addr
, &mZeroIp4Addr
, Gateway
);
539 if (EFI_ERROR (Status
)) {
540 Udp4
->Configure (Udp4
, NULL
);
544 if (!EFI_ERROR (Status
) && *SrcPort
== 0) {
545 Udp4
->GetModeData (Udp4
, &Udp4CfgData
, NULL
, NULL
, NULL
);
546 *SrcPort
= Udp4CfgData
.StationPort
;
554 This function is to configure a UDPv6 instance for UdpWrite.
556 @param[in] Udp6 The pointer to EFI_UDP6_PROTOCOL.
557 @param[in] StationIp The pointer to the station address.
558 @param[in, out] SrcPort The pointer to the source port.
560 @retval EFI_SUCCESS Successfully configured this instance.
561 @retval Others Failed to configure this instance.
565 PxeBcConfigUdp6Write (
566 IN EFI_UDP6_PROTOCOL
*Udp6
,
567 IN EFI_IPv6_ADDRESS
*StationIp
,
568 IN OUT UINT16
*SrcPort
571 EFI_UDP6_CONFIG_DATA CfgData
;
574 ZeroMem (&CfgData
, sizeof (EFI_UDP6_CONFIG_DATA
));
576 CfgData
.ReceiveTimeout
= PXEBC_DEFAULT_LIFETIME
;
577 CfgData
.TransmitTimeout
= PXEBC_DEFAULT_LIFETIME
;
578 CfgData
.HopLimit
= PXEBC_DEFAULT_HOPLIMIT
;
579 CfgData
.AllowDuplicatePort
= TRUE
;
580 CfgData
.StationPort
= *SrcPort
;
582 CopyMem (&CfgData
.StationAddress
, StationIp
, sizeof (EFI_IPv6_ADDRESS
));
585 // Reset the UDPv6 instance.
587 Udp6
->Configure (Udp6
, NULL
);
589 Status
= Udp6
->Configure (Udp6
, &CfgData
);
590 if (EFI_ERROR (Status
)) {
594 if (!EFI_ERROR (Status
) && *SrcPort
== 0) {
595 Udp6
->GetModeData (Udp6
, &CfgData
, NULL
, NULL
, NULL
);
596 *SrcPort
= CfgData
.StationPort
;
604 This function is to configure a UDPv4 instance for UdpWrite.
606 @param[in] Udp4 The pointer to EFI_UDP4_PROTOCOL.
607 @param[in] Session The pointer to the UDP4 session data.
608 @param[in] TimeoutEvent The event for timeout.
609 @param[in] Gateway The pointer to the gateway address.
610 @param[in] HeaderSize An optional field which may be set to the length of a header
611 at HeaderPtr to be prefixed to the data at BufferPtr.
612 @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be
613 prefixed to the data at BufferPtr.
614 @param[in] BufferSize A pointer to the size of the data at BufferPtr.
615 @param[in] BufferPtr A pointer to the data to be written.
617 @retval EFI_SUCCESS Successfully send out data using Udp4Write.
618 @retval Others Failed to send out data.
623 IN EFI_UDP4_PROTOCOL
*Udp4
,
624 IN EFI_UDP4_SESSION_DATA
*Session
,
625 IN EFI_EVENT TimeoutEvent
,
626 IN EFI_IPv4_ADDRESS
*Gateway OPTIONAL
,
627 IN UINTN
*HeaderSize OPTIONAL
,
628 IN VOID
*HeaderPtr OPTIONAL
,
629 IN UINTN
*BufferSize
,
633 EFI_UDP4_COMPLETION_TOKEN Token
;
634 EFI_UDP4_TRANSMIT_DATA
*TxData
;
642 // Arrange one fragment buffer for data, and another fragment buffer for header if has.
644 FragCount
= (HeaderSize
!= NULL
) ? 2 : 1;
645 TxLength
= sizeof (EFI_UDP4_TRANSMIT_DATA
) + (FragCount
- 1) * sizeof (EFI_UDP4_FRAGMENT_DATA
);
646 TxData
= (EFI_UDP4_TRANSMIT_DATA
*) AllocateZeroPool (TxLength
);
647 if (TxData
== NULL
) {
648 return EFI_OUT_OF_RESOURCES
;
651 TxData
->FragmentCount
= FragCount
;
652 TxData
->FragmentTable
[FragCount
- 1].FragmentLength
= (UINT32
) *BufferSize
;
653 TxData
->FragmentTable
[FragCount
- 1].FragmentBuffer
= BufferPtr
;
654 DataLength
= (UINT32
) *BufferSize
;
656 if (HeaderSize
!= NULL
) {
657 TxData
->FragmentTable
[0].FragmentLength
= (UINT32
) *HeaderSize
;
658 TxData
->FragmentTable
[0].FragmentBuffer
= HeaderPtr
;
659 DataLength
+= (UINT32
) *HeaderSize
;
662 if (Gateway
!= NULL
) {
663 TxData
->GatewayAddress
= Gateway
;
666 TxData
->UdpSessionData
= Session
;
667 TxData
->DataLength
= DataLength
;
668 Token
.Packet
.TxData
= TxData
;
669 Token
.Status
= EFI_NOT_READY
;
672 Status
= gBS
->CreateEvent (
679 if (EFI_ERROR (Status
)) {
683 Status
= Udp4
->Transmit (Udp4
, &Token
);
684 if (EFI_ERROR (Status
)) {
689 // Poll the UDPv6 read instance if no packet received and no timeout triggered.
692 Token
.Status
== EFI_NOT_READY
&&
693 EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
697 Status
= (Token
.Status
== EFI_NOT_READY
) ? EFI_TIMEOUT
: Token
.Status
;
700 if (Token
.Event
!= NULL
) {
701 gBS
->CloseEvent (Token
.Event
);
710 This function is to configure a UDPv4 instance for UdpWrite.
712 @param[in] Udp6 The pointer to EFI_UDP6_PROTOCOL.
713 @param[in] Session The pointer to the UDP6 session data.
714 @param[in] TimeoutEvent The event for timeout.
715 @param[in] HeaderSize An optional field which may be set to the length of a header
716 at HeaderPtr to be prefixed to the data at BufferPtr.
717 @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be
718 prefixed to the data at BufferPtr.
719 @param[in] BufferSize A pointer to the size of the data at BufferPtr.
720 @param[in] BufferPtr A pointer to the data to be written.
722 @retval EFI_SUCCESS Successfully sent out data using Udp6Write.
723 @retval Others Failed to send out data.
728 IN EFI_UDP6_PROTOCOL
*Udp6
,
729 IN EFI_UDP6_SESSION_DATA
*Session
,
730 IN EFI_EVENT TimeoutEvent
,
731 IN UINTN
*HeaderSize OPTIONAL
,
732 IN VOID
*HeaderPtr OPTIONAL
,
733 IN UINTN
*BufferSize
,
737 EFI_UDP6_COMPLETION_TOKEN Token
;
738 EFI_UDP6_TRANSMIT_DATA
*TxData
;
746 // Arrange one fragment buffer for data, and another fragment buffer for header if has.
748 FragCount
= (HeaderSize
!= NULL
) ? 2 : 1;
749 TxLength
= sizeof (EFI_UDP6_TRANSMIT_DATA
) + (FragCount
- 1) * sizeof (EFI_UDP6_FRAGMENT_DATA
);
750 TxData
= (EFI_UDP6_TRANSMIT_DATA
*) AllocateZeroPool (TxLength
);
751 if (TxData
== NULL
) {
752 return EFI_OUT_OF_RESOURCES
;
755 TxData
->FragmentCount
= FragCount
;
756 TxData
->FragmentTable
[FragCount
- 1].FragmentLength
= (UINT32
) *BufferSize
;
757 TxData
->FragmentTable
[FragCount
- 1].FragmentBuffer
= BufferPtr
;
758 DataLength
= (UINT32
) *BufferSize
;
760 if (HeaderSize
!= NULL
) {
761 TxData
->FragmentTable
[0].FragmentLength
= (UINT32
) *HeaderSize
;
762 TxData
->FragmentTable
[0].FragmentBuffer
= HeaderPtr
;
763 DataLength
+= (UINT32
) *HeaderSize
;
766 TxData
->UdpSessionData
= Session
;
767 TxData
->DataLength
= DataLength
;
768 Token
.Packet
.TxData
= TxData
;
769 Token
.Status
= EFI_NOT_READY
;
772 Status
= gBS
->CreateEvent (
779 if (EFI_ERROR (Status
)) {
783 Status
= Udp6
->Transmit (Udp6
, &Token
);
784 if (EFI_ERROR (Status
)) {
789 // Poll the UDPv6 read instance if no packet received and no timeout triggered.
792 Token
.Status
== EFI_NOT_READY
&&
793 EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
797 Status
= (Token
.Status
== EFI_NOT_READY
) ? EFI_TIMEOUT
: Token
.Status
;
800 if (Token
.Event
!= NULL
) {
801 gBS
->CloseEvent (Token
.Event
);
810 Check the received packet using the Ip filter.
812 @param[in] Mode The pointer to the mode data of PxeBc.
813 @param[in] Session The pointer to the current UDPv4 session.
814 @param[in] OpFlags Operation flag for UdpRead/UdpWrite.
816 @retval TRUE Passed the Ip filter successfully.
817 @retval FALSE Failed to pass the Ip filter.
821 PxeBcCheckByIpFilter (
822 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
827 EFI_IP_ADDRESS DestinationIp
;
830 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER
) == 0) {
834 if ((Mode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS
) != 0) {
839 // Convert the destination address in session data to host order.
841 if (Mode
->UsingIpv6
) {
844 &((EFI_UDP6_SESSION_DATA
*) Session
)->DestinationAddress
,
845 sizeof (EFI_IPv6_ADDRESS
)
847 NTOHLLL (&DestinationIp
.v6
);
849 ZeroMem (&DestinationIp
, sizeof (EFI_IP_ADDRESS
));
852 &((EFI_UDP4_SESSION_DATA
*) Session
)->DestinationAddress
,
853 sizeof (EFI_IPv4_ADDRESS
)
855 EFI_NTOHL (DestinationIp
);
858 if ((Mode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST
) != 0 &&
859 (IP4_IS_MULTICAST (DestinationIp
.Addr
[0]) ||
860 IP6_IS_MULTICAST (&DestinationIp
))) {
864 if ((Mode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST
) != 0 &&
865 IP4_IS_LOCAL_BROADCAST (DestinationIp
.Addr
[0])) {
866 ASSERT (!Mode
->UsingIpv6
);
870 if ((Mode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
) != 0 &&
871 (EFI_IP4_EQUAL (&Mode
->StationIp
.v4
, &DestinationIp
) ||
872 EFI_IP6_EQUAL (&Mode
->StationIp
.v6
, &DestinationIp
))) {
874 // Matched if the dest address is equal to the station address.
879 for (Index
= 0; Index
< Mode
->IpFilter
.IpCnt
; Index
++) {
880 ASSERT (Index
< EFI_PXE_BASE_CODE_MAX_IPCNT
);
881 if (EFI_IP4_EQUAL (&Mode
->IpFilter
.IpList
[Index
].v4
, &DestinationIp
) ||
882 EFI_IP6_EQUAL (&Mode
->IpFilter
.IpList
[Index
].v6
, &DestinationIp
)) {
884 // Matched if the dest address is equal to any of address in the filter list.
895 Filter the received packet using the destination Ip.
897 @param[in] Mode The pointer to the mode data of PxeBc.
898 @param[in] Session The pointer to the current UDPv4 session.
899 @param[in, out] DestIp The pointer to the destination Ip address.
900 @param[in] OpFlags Operation flag for UdpRead/UdpWrite.
902 @retval TRUE Passed the IPv4 filter successfully.
903 @retval FALSE Failed to pass the IPv4 filter.
908 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
910 IN OUT EFI_IP_ADDRESS
*DestIp
,
914 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP
) != 0) {
916 // Copy the destination address from the received packet if accept any.
918 if (DestIp
!= NULL
) {
919 if (Mode
->UsingIpv6
) {
922 &((EFI_UDP6_SESSION_DATA
*)Session
)->DestinationAddress
,
923 sizeof (EFI_IPv6_ADDRESS
)
926 ZeroMem (DestIp
, sizeof (EFI_IP_ADDRESS
));
929 &((EFI_UDP4_SESSION_DATA
*)Session
)->DestinationAddress
,
930 sizeof (EFI_IPv4_ADDRESS
)
936 } else if (DestIp
!= NULL
&&
937 (EFI_IP4_EQUAL (DestIp
, &((EFI_UDP4_SESSION_DATA
*)Session
)->DestinationAddress
) ||
938 EFI_IP6_EQUAL (DestIp
, &((EFI_UDP6_SESSION_DATA
*)Session
)->DestinationAddress
))) {
940 // The destination address in the received packet is matched if present.
943 } else if (EFI_IP4_EQUAL (&Mode
->StationIp
, &((EFI_UDP4_SESSION_DATA
*)Session
)->DestinationAddress
) ||
944 EFI_IP6_EQUAL (&Mode
->StationIp
, &((EFI_UDP6_SESSION_DATA
*)Session
)->DestinationAddress
)) {
946 // The destination address in the received packet is equal to the host address.
956 Check the received packet using the destination port.
958 @param[in] Mode The pointer to the mode data of PxeBc.
959 @param[in] Session The pointer to the current UDPv4 session.
960 @param[in, out] DestPort The pointer to the destination port.
961 @param[in] OpFlags Operation flag for UdpRead/UdpWrite.
963 @retval TRUE Passed the IPv4 filter successfully.
964 @retval FALSE Failed to pass the IPv4 filter.
968 PxeBcCheckByDestPort (
969 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
971 IN OUT UINT16
*DestPort
,
977 if (Mode
->UsingIpv6
) {
978 Port
= ((EFI_UDP6_SESSION_DATA
*) Session
)->DestinationPort
;
980 Port
= ((EFI_UDP4_SESSION_DATA
*) Session
)->DestinationPort
;
983 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT
) != 0) {
985 // Return the destination port in the received packet if accept any.
987 if (DestPort
!= NULL
) {
991 } else if (DestPort
!= NULL
&& *DestPort
== Port
) {
993 // The destination port in the received packet is matched if present.
1003 Filter the received packet using the source Ip.
1005 @param[in] Mode The pointer to the mode data of PxeBc.
1006 @param[in] Session The pointer to the current UDPv4 session.
1007 @param[in, out] SrcIp The pointer to the source Ip address.
1008 @param[in] OpFlags Operation flag for UdpRead/UdpWrite.
1010 @retval TRUE Passed the IPv4 filter successfully.
1011 @retval FALSE Failed to pass the IPv4 filter.
1015 PxeBcFilterBySrcIp (
1016 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
1018 IN OUT EFI_IP_ADDRESS
*SrcIp
,
1022 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP
) != 0) {
1024 // Copy the source address from the received packet if accept any.
1026 if (SrcIp
!= NULL
) {
1027 if (Mode
->UsingIpv6
) {
1030 &((EFI_UDP6_SESSION_DATA
*)Session
)->SourceAddress
,
1031 sizeof (EFI_IPv6_ADDRESS
)
1034 ZeroMem (SrcIp
, sizeof (EFI_IP_ADDRESS
));
1037 &((EFI_UDP4_SESSION_DATA
*)Session
)->SourceAddress
,
1038 sizeof (EFI_IPv4_ADDRESS
)
1044 } else if (SrcIp
!= NULL
&&
1045 (EFI_IP4_EQUAL (SrcIp
, &((EFI_UDP4_SESSION_DATA
*)Session
)->SourceAddress
) ||
1046 EFI_IP6_EQUAL (SrcIp
, &((EFI_UDP6_SESSION_DATA
*)Session
)->SourceAddress
))) {
1048 // The source address in the received packet is matched if present.
1058 Filter the received packet using the source port.
1060 @param[in] Mode The pointer to the mode data of PxeBc.
1061 @param[in] Session The pointer to the current UDPv4 session.
1062 @param[in, out] SrcPort The pointer to the source port.
1063 @param[in] OpFlags Operation flag for UdpRead/UdpWrite.
1065 @retval TRUE Passed the IPv4 filter successfully.
1066 @retval FALSE Failed to pass the IPv4 filter.
1070 PxeBcFilterBySrcPort (
1071 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
1073 IN OUT UINT16
*SrcPort
,
1079 if (Mode
->UsingIpv6
) {
1080 Port
= ((EFI_UDP6_SESSION_DATA
*) Session
)->SourcePort
;
1082 Port
= ((EFI_UDP4_SESSION_DATA
*) Session
)->SourcePort
;
1085 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT
) != 0) {
1087 // Return the source port in the received packet if accept any.
1089 if (SrcPort
!= NULL
) {
1093 } else if (SrcPort
!= NULL
&& *SrcPort
== Port
) {
1095 // The source port in the received packet is matched if present.
1105 This function is to receive packet using Udp4Read.
1107 @param[in] Udp4 The pointer to EFI_UDP4_PROTOCOL.
1108 @param[in] Token The pointer to EFI_UDP4_COMPLETION_TOKEN.
1109 @param[in] Mode The pointer to EFI_PXE_BASE_CODE_MODE.
1110 @param[in] TimeoutEvent The event for timeout.
1111 @param[in] OpFlags The UDP operation flags.
1112 @param[in] IsDone The pointer to the IsDone flag.
1113 @param[out] IsMatched The pointer to the IsMatched flag.
1114 @param[in, out] DestIp The pointer to the destination address.
1115 @param[in, out] DestPort The pointer to the destination port.
1116 @param[in, out] SrcIp The pointer to the source address.
1117 @param[in, out] SrcPort The pointer to the source port.
1119 @retval EFI_SUCCESS Successfully read the data using Udp4.
1120 @retval Others Failed to send out data.
1125 IN EFI_UDP4_PROTOCOL
*Udp4
,
1126 IN EFI_UDP4_COMPLETION_TOKEN
*Token
,
1127 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
1128 IN EFI_EVENT TimeoutEvent
,
1131 OUT BOOLEAN
*IsMatched
,
1132 IN OUT EFI_IP_ADDRESS
*DestIp OPTIONAL
,
1133 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*DestPort OPTIONAL
,
1134 IN OUT EFI_IP_ADDRESS
*SrcIp OPTIONAL
,
1135 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPort OPTIONAL
1138 EFI_UDP4_RECEIVE_DATA
*RxData
;
1139 EFI_UDP4_SESSION_DATA
*Session
;
1142 Token
->Status
= EFI_NOT_READY
;
1145 Status
= Udp4
->Receive (Udp4
, Token
);
1146 if (EFI_ERROR (Status
)) {
1151 // Poll the UDPv6 read instance if no packet received and no timeout triggered.
1153 while (!(*IsDone
) &&
1154 Token
->Status
== EFI_NOT_READY
&&
1155 EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
1157 // Poll the token utill reply/ICMPv6 error message received or timeout.
1160 if (Token
->Status
== EFI_ICMP_ERROR
||
1161 Token
->Status
== EFI_NETWORK_UNREACHABLE
||
1162 Token
->Status
== EFI_HOST_UNREACHABLE
||
1163 Token
->Status
== EFI_PROTOCOL_UNREACHABLE
||
1164 Token
->Status
== EFI_PORT_UNREACHABLE
) {
1169 Status
= (Token
->Status
== EFI_NOT_READY
) ? EFI_TIMEOUT
: Token
->Status
;
1171 if (!EFI_ERROR (Status
)) {
1173 // check whether this packet matches the filters
1175 RxData
= Token
->Packet
.RxData
;
1176 Session
= &RxData
->UdpSession
;
1178 *IsMatched
= PxeBcCheckByIpFilter (Mode
, Session
, OpFlags
);
1181 *IsMatched
= PxeBcCheckByDestIp (Mode
, Session
, DestIp
, OpFlags
);
1185 *IsMatched
= PxeBcCheckByDestPort (Mode
, Session
, DestPort
, OpFlags
);
1189 *IsMatched
= PxeBcFilterBySrcIp (Mode
, Session
, SrcIp
, OpFlags
);
1193 *IsMatched
= PxeBcFilterBySrcPort (Mode
, Session
, SrcPort
, OpFlags
);
1196 if (!(*IsMatched
)) {
1198 // Recycle the receiving buffer if not matched.
1200 gBS
->SignalEvent (RxData
->RecycleSignal
);
1209 This function is to receive packets using Udp6Read.
1211 @param[in] Udp6 The pointer to EFI_UDP6_PROTOCOL.
1212 @param[in] Token The pointer to EFI_UDP6_COMPLETION_TOKEN.
1213 @param[in] Mode The pointer to EFI_PXE_BASE_CODE_MODE.
1214 @param[in] TimeoutEvent The event for timeout.
1215 @param[in] OpFlags The UDP operation flags.
1216 @param[in] IsDone The pointer to the IsDone flag.
1217 @param[out] IsMatched The pointer to the IsMatched flag.
1218 @param[in, out] DestIp The pointer to the destination address.
1219 @param[in, out] DestPort The pointer to the destination port.
1220 @param[in, out] SrcIp The pointer to the source address.
1221 @param[in, out] SrcPort The pointer to the source port.
1223 @retval EFI_SUCCESS Successfully read data using Udp6.
1224 @retval Others Failed to send out data.
1229 IN EFI_UDP6_PROTOCOL
*Udp6
,
1230 IN EFI_UDP6_COMPLETION_TOKEN
*Token
,
1231 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
1232 IN EFI_EVENT TimeoutEvent
,
1235 OUT BOOLEAN
*IsMatched
,
1236 IN OUT EFI_IP_ADDRESS
*DestIp OPTIONAL
,
1237 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*DestPort OPTIONAL
,
1238 IN OUT EFI_IP_ADDRESS
*SrcIp OPTIONAL
,
1239 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPort OPTIONAL
1242 EFI_UDP6_RECEIVE_DATA
*RxData
;
1243 EFI_UDP6_SESSION_DATA
*Session
;
1246 Token
->Status
= EFI_NOT_READY
;
1249 Status
= Udp6
->Receive (Udp6
, Token
);
1250 if (EFI_ERROR (Status
)) {
1255 // Poll the UDPv6 read instance if no packet received and no timeout triggered.
1257 while (!(*IsDone
) &&
1258 Token
->Status
== EFI_NOT_READY
&&
1259 EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
1261 // Poll the token utill reply/ICMPv6 error message received or timeout.
1264 if (Token
->Status
== EFI_ICMP_ERROR
||
1265 Token
->Status
== EFI_NETWORK_UNREACHABLE
||
1266 Token
->Status
== EFI_HOST_UNREACHABLE
||
1267 Token
->Status
== EFI_PROTOCOL_UNREACHABLE
||
1268 Token
->Status
== EFI_PORT_UNREACHABLE
) {
1273 Status
= (Token
->Status
== EFI_NOT_READY
) ? EFI_TIMEOUT
: Token
->Status
;
1275 if (!EFI_ERROR (Status
)) {
1277 // check whether this packet matches the filters
1279 RxData
= Token
->Packet
.RxData
;
1280 Session
= &RxData
->UdpSession
;
1282 *IsMatched
= PxeBcCheckByIpFilter (Mode
, Session
, OpFlags
);
1285 *IsMatched
= PxeBcCheckByDestIp (Mode
, Session
, DestIp
, OpFlags
);
1289 *IsMatched
= PxeBcCheckByDestPort (Mode
, Session
, DestPort
, OpFlags
);
1293 *IsMatched
= PxeBcFilterBySrcIp (Mode
, Session
, SrcIp
, OpFlags
);
1297 *IsMatched
= PxeBcFilterBySrcPort (Mode
, Session
, SrcPort
, OpFlags
);
1300 if (!(*IsMatched
)) {
1302 // Recycle the receiving buffer if not matched.
1304 gBS
->SignalEvent (RxData
->RecycleSignal
);
1313 This function is to display the IPv4 address.
1315 @param[in] Ip The pointer to the IPv4 address.
1320 IN EFI_IPv4_ADDRESS
*Ip
1325 for (Index
= 0; Index
< 4; Index
++) {
1326 AsciiPrint ("%d", Ip
->Addr
[Index
]);
1335 This function is to display the IPv6 address.
1337 @param[in] Ip The pointer to the IPv6 address.
1342 IN EFI_IPv6_ADDRESS
*Ip
1347 for (Index
= 0; Index
< 16; Index
++) {
1349 if (Ip
->Addr
[Index
] != 0) {
1350 AsciiPrint ("%x", Ip
->Addr
[Index
]);
1356 if (((Ip
->Addr
[Index
] & 0xf0) == 0) && (Ip
->Addr
[Index
- 1] != 0)) {
1359 AsciiPrint ("%x", Ip
->Addr
[Index
]);
1368 This function is to convert UINTN to ASCII string with the required formatting.
1370 @param[in] Number Numeric value to be converted.
1371 @param[in] Buffer The pointer to the buffer for ASCII string.
1372 @param[in] Length The length of the required format.
1376 PxeBcUintnToAscDecWithFormat (
1384 while (Length
> 0) {
1386 Remainder
= Number
% 10;
1388 Buffer
[Length
] = (UINT8
) ('0' + Remainder
);
1394 This function is to convert a UINTN to a ASCII string, and return the
1395 actual length of the buffer.
1397 @param[in] Number Numeric value to be converted.
1398 @param[in] Buffer The pointer to the buffer for ASCII string.
1399 @param[in] BufferSize The maxsize of the buffer.
1401 @return Length The actual length of the ASCII string.
1405 PxeBcUintnToAscDec (
1420 TempStr
[Index
] = (CHAR8
) ('0' + (Number
% 10));
1421 Number
= (UINTN
) (Number
/ 10);
1422 } while (Number
!= 0);
1424 AsciiStrCpyS ((CHAR8
*) Buffer
, BufferSize
, &TempStr
[Index
]);
1426 Length
= AsciiStrLen ((CHAR8
*) Buffer
);
1433 This function is to convert unicode hex number to a UINT8.
1435 @param[out] Digit The converted UINT8 for output.
1436 @param[in] Char The unicode hex number to be converted.
1438 @retval EFI_SUCCESS Successfully converted the unicode hex.
1439 @retval EFI_INVALID_PARAMETER Failed to convert the unicode hex.
1443 PxeBcUniHexToUint8 (
1448 if ((Char
>= L
'0') && (Char
<= L
'9')) {
1449 *Digit
= (UINT8
) (Char
- L
'0');
1453 if ((Char
>= L
'A') && (Char
<= L
'F')) {
1454 *Digit
= (UINT8
) (Char
- L
'A' + 0x0A);
1458 if ((Char
>= L
'a') && (Char
<= L
'f')) {
1459 *Digit
= (UINT8
) (Char
- L
'a' + 0x0A);
1463 return EFI_INVALID_PARAMETER
;
1467 Calculate the elapsed time.
1469 @param[in] Private The pointer to PXE private data
1474 IN PXEBC_PRIVATE_DATA
*Private
1478 UINT64 CurrentStamp
;
1479 UINT64 ElapsedTimeValue
;
1482 // Generate a time stamp of the centiseconds from 1900/1/1, assume 30day/month.
1484 ZeroMem (&Time
, sizeof (EFI_TIME
));
1485 gRT
->GetTime (&Time
, NULL
);
1486 CurrentStamp
= (UINT64
)
1488 ((((((Time
.Year
- 1900) * 360 +
1489 (Time
.Month
- 1)) * 30 +
1490 (Time
.Day
- 1)) * 24 + Time
.Hour
) * 60 +
1491 Time
.Minute
) * 60 + Time
.Second
) * 100
1492 + DivU64x32(Time
.Nanosecond
, 10000000)
1496 // Sentinel value of 0 means that this is the first DHCP packet that we are
1497 // sending and that we need to initialize the value. First DHCP Solicit
1498 // gets 0 elapsed-time. Otherwise, calculate based on StartTime.
1500 if (Private
->ElapsedTime
== 0) {
1501 Private
->ElapsedTime
= CurrentStamp
;
1503 ElapsedTimeValue
= CurrentStamp
- Private
->ElapsedTime
;
1506 // If elapsed time cannot fit in two bytes, set it to 0xffff.
1508 if (ElapsedTimeValue
> 0xffff) {
1509 ElapsedTimeValue
= 0xffff;
1512 // Save the elapsed time
1514 Private
->ElapsedTime
= ElapsedTimeValue
;