2 Support functions implementation for UefiPxeBc Driver.
4 Copyright (c) 2007 - 2017, 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
, OPTIONAL
34 EFI_IP_ADDRESS
*SubnetMask OPTIONAL
37 EFI_PXE_BASE_CODE_MODE
*Mode
;
40 Mode
= Private
->PxeBc
.Mode
;
43 if (Mode
->UsingIpv6
) {
45 if (StationIp
!= NULL
) {
46 CopyMem (&Private
->Udp6CfgData
.StationAddress
, StationIp
, sizeof (EFI_IPv6_ADDRESS
));
47 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 if (StationIp
!= NULL
) {
64 CopyMem (&Private
->Udp4CfgData
.StationAddress
, StationIp
, sizeof (EFI_IPv4_ADDRESS
));
65 CopyMem (&Private
->Ip4CfgData
.StationAddress
, StationIp
, sizeof (EFI_IPv4_ADDRESS
));
68 if (SubnetMask
!= NULL
) {
69 CopyMem (&Private
->Udp4CfgData
.SubnetMask
, SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
70 CopyMem (&Private
->Ip4CfgData
.SubnetMask
, SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
74 // Reconfigure the Ip4 instance to capture background ICMP packets with new station Ip address.
76 Private
->Ip4
->Cancel (Private
->Ip4
, &Private
->IcmpToken
);
77 Private
->Ip4
->Configure (Private
->Ip4
, NULL
);
79 Status
= Private
->Ip4
->Configure (Private
->Ip4
, &Private
->Ip4CfgData
);
80 if (EFI_ERROR (Status
)) {
84 Status
= Private
->Ip4
->Receive (Private
->Ip4
, &Private
->IcmpToken
);
93 Notify the callback function when an event is triggered.
95 @param[in] Event The triggered event.
96 @param[in] Context The opaque parameter to the function.
106 *((BOOLEAN
*) Context
) = TRUE
;
111 Do arp resolution from arp cache in PxeBcMode.
113 @param Mode The pointer to EFI_PXE_BASE_CODE_MODE.
114 @param Ip4Addr The Ip4 address for resolution.
115 @param MacAddress The resoluted MAC address if the resolution is successful.
116 The value is undefined if the resolution fails.
118 @retval TRUE Found an matched entry.
119 @retval FALSE Did not find a matched entry.
124 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
125 IN EFI_IPv4_ADDRESS
*Ip4Addr
,
126 OUT EFI_MAC_ADDRESS
*MacAddress
131 ASSERT (!Mode
->UsingIpv6
);
134 // Check whether the current Arp cache in mode data contains this information or not.
136 for (Index
= 0; Index
< Mode
->ArpCacheEntries
; Index
++) {
137 if (EFI_IP4_EQUAL (&Mode
->ArpCache
[Index
].IpAddr
.v4
, Ip4Addr
)) {
140 &Mode
->ArpCache
[Index
].MacAddr
,
141 sizeof (EFI_MAC_ADDRESS
)
152 Update the arp cache periodically.
154 @param Event The pointer to EFI_PXE_BC_PROTOCOL.
155 @param Context Context of the timer event.
160 PxeBcArpCacheUpdate (
165 PXEBC_PRIVATE_DATA
*Private
;
166 EFI_PXE_BASE_CODE_MODE
*Mode
;
167 EFI_ARP_FIND_DATA
*ArpEntry
;
173 Private
= (PXEBC_PRIVATE_DATA
*) Context
;
174 Mode
= Private
->PxeBc
.Mode
;
176 ASSERT (!Mode
->UsingIpv6
);
179 // Get the current Arp cache from Arp driver.
181 Status
= Private
->Arp
->Find (
190 if (EFI_ERROR (Status
)) {
195 // Update the Arp cache in mode data.
197 Mode
->ArpCacheEntries
= MIN (EntryCount
, EFI_PXE_BASE_CODE_MAX_ARP_ENTRIES
);
199 for (Index
= 0; Index
< Mode
->ArpCacheEntries
; Index
++) {
201 &Mode
->ArpCache
[Index
].IpAddr
,
203 ArpEntry
->SwAddressLength
206 &Mode
->ArpCache
[Index
].MacAddr
,
207 (UINT8
*) (ArpEntry
+ 1) + ArpEntry
->SwAddressLength
,
208 ArpEntry
->HwAddressLength
210 ArpEntry
= (EFI_ARP_FIND_DATA
*) ((UINT8
*) ArpEntry
+ EntryLength
);
216 Notify function to handle the received ICMP message in DPC.
218 @param Context The PXEBC private data.
223 PxeBcIcmpErrorDpcHandle (
228 EFI_IP4_RECEIVE_DATA
*RxData
;
229 EFI_IP4_PROTOCOL
*Ip4
;
230 PXEBC_PRIVATE_DATA
*Private
;
231 EFI_PXE_BASE_CODE_MODE
*Mode
;
237 Private
= (PXEBC_PRIVATE_DATA
*) Context
;
238 Mode
= &Private
->Mode
;
239 Status
= Private
->IcmpToken
.Status
;
240 RxData
= Private
->IcmpToken
.Packet
.RxData
;
243 ASSERT (!Mode
->UsingIpv6
);
245 if (Status
== EFI_ABORTED
) {
247 // It's triggered by user cancellation.
252 if (RxData
== NULL
) {
256 if (Status
!= EFI_ICMP_ERROR
) {
258 // The return status should be recognized as EFI_ICMP_ERROR.
260 gBS
->SignalEvent (RxData
->RecycleSignal
);
264 if (EFI_IP4 (RxData
->Header
->SourceAddress
) != 0 &&
265 (NTOHL (Mode
->SubnetMask
.Addr
[0]) != 0) &&
266 IP4_NET_EQUAL (NTOHL(Mode
->StationIp
.Addr
[0]), EFI_NTOHL (RxData
->Header
->SourceAddress
), NTOHL (Mode
->SubnetMask
.Addr
[0])) &&
267 !NetIp4IsUnicast (EFI_NTOHL (RxData
->Header
->SourceAddress
), NTOHL (Mode
->SubnetMask
.Addr
[0]))) {
269 // The source address of the received packet should be a valid unicast address.
271 gBS
->SignalEvent (RxData
->RecycleSignal
);
275 if (!EFI_IP4_EQUAL (&RxData
->Header
->DestinationAddress
, &Mode
->StationIp
.v4
)) {
277 // The destination address of the received packet should be equal to the host address.
279 gBS
->SignalEvent (RxData
->RecycleSignal
);
283 if (RxData
->Header
->Protocol
!= EFI_IP_PROTO_ICMP
) {
285 // The protocol value in the header of the receveid packet should be EFI_IP_PROTO_ICMP.
287 gBS
->SignalEvent (RxData
->RecycleSignal
);
291 Type
= *((UINT8
*) RxData
->FragmentTable
[0].FragmentBuffer
);
293 if (Type
!= ICMP_DEST_UNREACHABLE
&&
294 Type
!= ICMP_SOURCE_QUENCH
&&
295 Type
!= ICMP_REDIRECT
&&
296 Type
!= ICMP_TIME_EXCEEDED
&&
297 Type
!= ICMP_PARAMETER_PROBLEM
) {
299 // The type of the receveid ICMP message should be ICMP_ERROR_MESSAGE.
301 gBS
->SignalEvent (RxData
->RecycleSignal
);
306 // Copy the right ICMP error message into mode data.
309 IcmpError
= (UINT8
*) &Mode
->IcmpError
;
311 for (Index
= 0; Index
< RxData
->FragmentCount
; Index
++) {
312 CopiedLen
+= RxData
->FragmentTable
[Index
].FragmentLength
;
313 if (CopiedLen
<= sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR
)) {
316 RxData
->FragmentTable
[Index
].FragmentBuffer
,
317 RxData
->FragmentTable
[Index
].FragmentLength
322 RxData
->FragmentTable
[Index
].FragmentBuffer
,
323 CopiedLen
- sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR
)
326 IcmpError
+= CopiedLen
;
330 Private
->IcmpToken
.Status
= EFI_NOT_READY
;
331 Ip4
->Receive (Ip4
, &Private
->IcmpToken
);
336 Callback function to update the latest ICMP6 error message.
338 @param Event The event signalled.
339 @param Context The context passed in using the event notifier.
344 PxeBcIcmpErrorUpdate (
349 QueueDpc (TPL_CALLBACK
, PxeBcIcmpErrorDpcHandle
, Context
);
354 Notify function to handle the received ICMP6 message in DPC.
356 @param Context The PXEBC private data.
361 PxeBcIcmp6ErrorDpcHandle (
365 PXEBC_PRIVATE_DATA
*Private
;
366 EFI_IP6_RECEIVE_DATA
*RxData
;
367 EFI_IP6_PROTOCOL
*Ip6
;
368 EFI_PXE_BASE_CODE_MODE
*Mode
;
375 Private
= (PXEBC_PRIVATE_DATA
*) Context
;
376 Mode
= &Private
->Mode
;
377 Status
= Private
->Icmp6Token
.Status
;
378 RxData
= Private
->Icmp6Token
.Packet
.RxData
;
381 ASSERT (Mode
->UsingIpv6
);
383 if (Status
== EFI_ABORTED
) {
385 // It's triggered by user cancellation.
390 if (RxData
== NULL
) {
394 if (Status
!= EFI_ICMP_ERROR
) {
396 // The return status should be recognized as EFI_ICMP_ERROR.
398 gBS
->SignalEvent (RxData
->RecycleSignal
);
402 if (!NetIp6IsValidUnicast (&RxData
->Header
->SourceAddress
)) {
404 // The source address of the received packet should be a valid unicast address.
406 gBS
->SignalEvent (RxData
->RecycleSignal
);
410 if (!NetIp6IsUnspecifiedAddr (&Mode
->StationIp
.v6
) &&
411 !EFI_IP6_EQUAL (&RxData
->Header
->DestinationAddress
, &Mode
->StationIp
.v6
)) {
413 // The destination address of the received packet should be equal to the host address.
415 gBS
->SignalEvent (RxData
->RecycleSignal
);
419 if (RxData
->Header
->NextHeader
!= IP6_ICMP
) {
421 // The nextheader in the header of the receveid packet should be IP6_ICMP.
423 gBS
->SignalEvent (RxData
->RecycleSignal
);
427 Type
= *((UINT8
*) RxData
->FragmentTable
[0].FragmentBuffer
);
429 if (Type
!= ICMP_V6_DEST_UNREACHABLE
&&
430 Type
!= ICMP_V6_PACKET_TOO_BIG
&&
431 Type
!= ICMP_V6_TIME_EXCEEDED
&&
432 Type
!= ICMP_V6_PARAMETER_PROBLEM
) {
434 // The type of the receveid packet should be an ICMP6 error message.
436 gBS
->SignalEvent (RxData
->RecycleSignal
);
441 // Copy the right ICMP6 error message into mode data.
444 Icmp6Error
= (UINT8
*) &Mode
->IcmpError
;
446 for (Index
= 0; Index
< RxData
->FragmentCount
; Index
++) {
447 CopiedLen
+= RxData
->FragmentTable
[Index
].FragmentLength
;
448 if (CopiedLen
<= sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR
)) {
451 RxData
->FragmentTable
[Index
].FragmentBuffer
,
452 RxData
->FragmentTable
[Index
].FragmentLength
457 RxData
->FragmentTable
[Index
].FragmentBuffer
,
458 CopiedLen
- sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR
)
461 Icmp6Error
+= CopiedLen
;
465 Private
->Icmp6Token
.Status
= EFI_NOT_READY
;
466 Ip6
->Receive (Ip6
, &Private
->Icmp6Token
);
471 Callback function to update the latest ICMP6 error message.
473 @param Event The event signalled.
474 @param Context The context passed in using the event notifier.
479 PxeBcIcmp6ErrorUpdate (
484 QueueDpc (TPL_CALLBACK
, PxeBcIcmp6ErrorDpcHandle
, Context
);
489 This function is to configure a UDPv4 instance for UdpWrite.
491 @param[in] Udp4 The pointer to EFI_UDP4_PROTOCOL.
492 @param[in] StationIp The pointer to the station address.
493 @param[in] SubnetMask The pointer to the subnet mask.
494 @param[in] Gateway The pointer to the gateway address.
495 @param[in, out] SrcPort The pointer to the source port.
496 @param[in] DoNotFragment If TRUE, fragment is not enabled.
497 Otherwise, fragment is enabled.
498 @param[in] Ttl The time to live field of the IP header.
499 @param[in] ToS The type of service field of the IP header.
501 @retval EFI_SUCCESS Successfully configured this instance.
502 @retval Others Failed to configure this instance.
506 PxeBcConfigUdp4Write (
507 IN EFI_UDP4_PROTOCOL
*Udp4
,
508 IN EFI_IPv4_ADDRESS
*StationIp
,
509 IN EFI_IPv4_ADDRESS
*SubnetMask
,
510 IN EFI_IPv4_ADDRESS
*Gateway
,
511 IN OUT UINT16
*SrcPort
,
512 IN BOOLEAN DoNotFragment
,
517 EFI_UDP4_CONFIG_DATA Udp4CfgData
;
520 ZeroMem (&Udp4CfgData
, sizeof (Udp4CfgData
));
522 Udp4CfgData
.TransmitTimeout
= PXEBC_DEFAULT_LIFETIME
;
523 Udp4CfgData
.ReceiveTimeout
= PXEBC_DEFAULT_LIFETIME
;
524 Udp4CfgData
.TypeOfService
= ToS
;
525 Udp4CfgData
.TimeToLive
= Ttl
;
526 Udp4CfgData
.AllowDuplicatePort
= TRUE
;
527 Udp4CfgData
.DoNotFragment
= DoNotFragment
;
529 CopyMem (&Udp4CfgData
.StationAddress
, StationIp
, sizeof (*StationIp
));
530 CopyMem (&Udp4CfgData
.SubnetMask
, SubnetMask
, sizeof (*SubnetMask
));
532 Udp4CfgData
.StationPort
= *SrcPort
;
535 // Reset the UDPv4 instance.
537 Udp4
->Configure (Udp4
, NULL
);
539 Status
= Udp4
->Configure (Udp4
, &Udp4CfgData
);
540 if (!EFI_ERROR (Status
) && !EFI_IP4_EQUAL (Gateway
, &mZeroIp4Addr
)) {
542 // The basic configuration is OK, need to add the default route entry
544 Status
= Udp4
->Routes (Udp4
, FALSE
, &mZeroIp4Addr
, &mZeroIp4Addr
, Gateway
);
545 if (EFI_ERROR (Status
)) {
546 Udp4
->Configure (Udp4
, NULL
);
550 if (!EFI_ERROR (Status
) && *SrcPort
== 0) {
551 Udp4
->GetModeData (Udp4
, &Udp4CfgData
, NULL
, NULL
, NULL
);
552 *SrcPort
= Udp4CfgData
.StationPort
;
560 This function is to configure a UDPv6 instance for UdpWrite.
562 @param[in] Udp6 The pointer to EFI_UDP6_PROTOCOL.
563 @param[in] StationIp The pointer to the station address.
564 @param[in, out] SrcPort The pointer to the source port.
566 @retval EFI_SUCCESS Successfully configured this instance.
567 @retval Others Failed to configure this instance.
571 PxeBcConfigUdp6Write (
572 IN EFI_UDP6_PROTOCOL
*Udp6
,
573 IN EFI_IPv6_ADDRESS
*StationIp
,
574 IN OUT UINT16
*SrcPort
577 EFI_UDP6_CONFIG_DATA CfgData
;
580 ZeroMem (&CfgData
, sizeof (EFI_UDP6_CONFIG_DATA
));
582 CfgData
.ReceiveTimeout
= PXEBC_DEFAULT_LIFETIME
;
583 CfgData
.TransmitTimeout
= PXEBC_DEFAULT_LIFETIME
;
584 CfgData
.HopLimit
= PXEBC_DEFAULT_HOPLIMIT
;
585 CfgData
.AllowDuplicatePort
= TRUE
;
586 CfgData
.StationPort
= *SrcPort
;
588 CopyMem (&CfgData
.StationAddress
, StationIp
, sizeof (EFI_IPv6_ADDRESS
));
591 // Reset the UDPv6 instance.
593 Udp6
->Configure (Udp6
, NULL
);
595 Status
= Udp6
->Configure (Udp6
, &CfgData
);
596 if (EFI_ERROR (Status
)) {
600 if (!EFI_ERROR (Status
) && *SrcPort
== 0) {
601 Udp6
->GetModeData (Udp6
, &CfgData
, NULL
, NULL
, NULL
);
602 *SrcPort
= CfgData
.StationPort
;
610 This function is to configure a UDPv4 instance for UdpWrite.
612 @param[in] Udp4 The pointer to EFI_UDP4_PROTOCOL.
613 @param[in] Session The pointer to the UDP4 session data.
614 @param[in] TimeoutEvent The event for timeout.
615 @param[in] Gateway The pointer to the gateway address.
616 @param[in] HeaderSize An optional field which may be set to the length of a header
617 at HeaderPtr to be prefixed to the data at BufferPtr.
618 @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be
619 prefixed to the data at BufferPtr.
620 @param[in] BufferSize A pointer to the size of the data at BufferPtr.
621 @param[in] BufferPtr A pointer to the data to be written.
623 @retval EFI_SUCCESS Successfully send out data using Udp4Write.
624 @retval Others Failed to send out data.
629 IN EFI_UDP4_PROTOCOL
*Udp4
,
630 IN EFI_UDP4_SESSION_DATA
*Session
,
631 IN EFI_EVENT TimeoutEvent
,
632 IN EFI_IPv4_ADDRESS
*Gateway OPTIONAL
,
633 IN UINTN
*HeaderSize OPTIONAL
,
634 IN VOID
*HeaderPtr OPTIONAL
,
635 IN UINTN
*BufferSize
,
639 EFI_UDP4_COMPLETION_TOKEN Token
;
640 EFI_UDP4_TRANSMIT_DATA
*TxData
;
648 // Arrange one fragment buffer for data, and another fragment buffer for header if has.
650 FragCount
= (HeaderSize
!= NULL
) ? 2 : 1;
651 TxLength
= sizeof (EFI_UDP4_TRANSMIT_DATA
) + (FragCount
- 1) * sizeof (EFI_UDP4_FRAGMENT_DATA
);
652 TxData
= (EFI_UDP4_TRANSMIT_DATA
*) AllocateZeroPool (TxLength
);
653 if (TxData
== NULL
) {
654 return EFI_OUT_OF_RESOURCES
;
657 TxData
->FragmentCount
= FragCount
;
658 TxData
->FragmentTable
[FragCount
- 1].FragmentLength
= (UINT32
) *BufferSize
;
659 TxData
->FragmentTable
[FragCount
- 1].FragmentBuffer
= BufferPtr
;
660 DataLength
= (UINT32
) *BufferSize
;
662 if (HeaderSize
!= NULL
) {
663 TxData
->FragmentTable
[0].FragmentLength
= (UINT32
) *HeaderSize
;
664 TxData
->FragmentTable
[0].FragmentBuffer
= HeaderPtr
;
665 DataLength
+= (UINT32
) *HeaderSize
;
668 if (Gateway
!= NULL
) {
669 TxData
->GatewayAddress
= Gateway
;
672 TxData
->UdpSessionData
= Session
;
673 TxData
->DataLength
= DataLength
;
674 Token
.Packet
.TxData
= TxData
;
675 Token
.Status
= EFI_NOT_READY
;
678 Status
= gBS
->CreateEvent (
685 if (EFI_ERROR (Status
)) {
689 Status
= Udp4
->Transmit (Udp4
, &Token
);
690 if (EFI_ERROR (Status
)) {
695 // Poll the UDPv6 read instance if no packet received and no timeout triggered.
698 Token
.Status
== EFI_NOT_READY
&&
699 EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
703 Status
= (Token
.Status
== EFI_NOT_READY
) ? EFI_TIMEOUT
: Token
.Status
;
706 if (Token
.Event
!= NULL
) {
707 gBS
->CloseEvent (Token
.Event
);
716 This function is to configure a UDPv4 instance for UdpWrite.
718 @param[in] Udp6 The pointer to EFI_UDP6_PROTOCOL.
719 @param[in] Session The pointer to the UDP6 session data.
720 @param[in] TimeoutEvent The event for timeout.
721 @param[in] HeaderSize An optional field which may be set to the length of a header
722 at HeaderPtr to be prefixed to the data at BufferPtr.
723 @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be
724 prefixed to the data at BufferPtr.
725 @param[in] BufferSize A pointer to the size of the data at BufferPtr.
726 @param[in] BufferPtr A pointer to the data to be written.
728 @retval EFI_SUCCESS Successfully sent out data using Udp6Write.
729 @retval Others Failed to send out data.
734 IN EFI_UDP6_PROTOCOL
*Udp6
,
735 IN EFI_UDP6_SESSION_DATA
*Session
,
736 IN EFI_EVENT TimeoutEvent
,
737 IN UINTN
*HeaderSize OPTIONAL
,
738 IN VOID
*HeaderPtr OPTIONAL
,
739 IN UINTN
*BufferSize
,
743 EFI_UDP6_COMPLETION_TOKEN Token
;
744 EFI_UDP6_TRANSMIT_DATA
*TxData
;
752 // Arrange one fragment buffer for data, and another fragment buffer for header if has.
754 FragCount
= (HeaderSize
!= NULL
) ? 2 : 1;
755 TxLength
= sizeof (EFI_UDP6_TRANSMIT_DATA
) + (FragCount
- 1) * sizeof (EFI_UDP6_FRAGMENT_DATA
);
756 TxData
= (EFI_UDP6_TRANSMIT_DATA
*) AllocateZeroPool (TxLength
);
757 if (TxData
== NULL
) {
758 return EFI_OUT_OF_RESOURCES
;
761 TxData
->FragmentCount
= FragCount
;
762 TxData
->FragmentTable
[FragCount
- 1].FragmentLength
= (UINT32
) *BufferSize
;
763 TxData
->FragmentTable
[FragCount
- 1].FragmentBuffer
= BufferPtr
;
764 DataLength
= (UINT32
) *BufferSize
;
766 if (HeaderSize
!= NULL
) {
767 TxData
->FragmentTable
[0].FragmentLength
= (UINT32
) *HeaderSize
;
768 TxData
->FragmentTable
[0].FragmentBuffer
= HeaderPtr
;
769 DataLength
+= (UINT32
) *HeaderSize
;
772 TxData
->UdpSessionData
= Session
;
773 TxData
->DataLength
= DataLength
;
774 Token
.Packet
.TxData
= TxData
;
775 Token
.Status
= EFI_NOT_READY
;
778 Status
= gBS
->CreateEvent (
785 if (EFI_ERROR (Status
)) {
789 Status
= Udp6
->Transmit (Udp6
, &Token
);
790 if (EFI_ERROR (Status
)) {
795 // Poll the UDPv6 read instance if no packet received and no timeout triggered.
798 Token
.Status
== EFI_NOT_READY
&&
799 EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
803 Status
= (Token
.Status
== EFI_NOT_READY
) ? EFI_TIMEOUT
: Token
.Status
;
806 if (Token
.Event
!= NULL
) {
807 gBS
->CloseEvent (Token
.Event
);
816 Check the received packet using the Ip filter.
818 @param[in] Mode The pointer to the mode data of PxeBc.
819 @param[in] Session The pointer to the current UDPv4 session.
820 @param[in] OpFlags Operation flag for UdpRead/UdpWrite.
822 @retval TRUE Passed the Ip filter successfully.
823 @retval FALSE Failed to pass the Ip filter.
827 PxeBcCheckByIpFilter (
828 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
833 EFI_IP_ADDRESS DestinationIp
;
836 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER
) == 0) {
840 if ((Mode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS
) != 0) {
845 // Convert the destination address in session data to host order.
847 if (Mode
->UsingIpv6
) {
850 &((EFI_UDP6_SESSION_DATA
*) Session
)->DestinationAddress
,
851 sizeof (EFI_IPv6_ADDRESS
)
853 NTOHLLL (&DestinationIp
.v6
);
855 ZeroMem (&DestinationIp
, sizeof (EFI_IP_ADDRESS
));
858 &((EFI_UDP4_SESSION_DATA
*) Session
)->DestinationAddress
,
859 sizeof (EFI_IPv4_ADDRESS
)
861 EFI_NTOHL (DestinationIp
);
864 if ((Mode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST
) != 0 &&
865 (IP4_IS_MULTICAST (DestinationIp
.Addr
[0]) ||
866 IP6_IS_MULTICAST (&DestinationIp
))) {
870 if ((Mode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST
) != 0 &&
871 IP4_IS_LOCAL_BROADCAST (DestinationIp
.Addr
[0])) {
872 ASSERT (!Mode
->UsingIpv6
);
876 if ((Mode
->IpFilter
.Filters
& EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP
) != 0 &&
877 (EFI_IP4_EQUAL (&Mode
->StationIp
.v4
, &DestinationIp
) ||
878 EFI_IP6_EQUAL (&Mode
->StationIp
.v6
, &DestinationIp
))) {
880 // Matched if the dest address is equal to the station address.
885 for (Index
= 0; Index
< Mode
->IpFilter
.IpCnt
; Index
++) {
886 ASSERT (Index
< EFI_PXE_BASE_CODE_MAX_IPCNT
);
887 if (EFI_IP4_EQUAL (&Mode
->IpFilter
.IpList
[Index
].v4
, &DestinationIp
) ||
888 EFI_IP6_EQUAL (&Mode
->IpFilter
.IpList
[Index
].v6
, &DestinationIp
)) {
890 // Matched if the dest address is equal to any of address in the filter list.
901 Filter the received packet using the destination Ip.
903 @param[in] Mode The pointer to the mode data of PxeBc.
904 @param[in] Session The pointer to the current UDPv4 session.
905 @param[in, out] DestIp The pointer to the destination Ip address.
906 @param[in] OpFlags Operation flag for UdpRead/UdpWrite.
908 @retval TRUE Passed the IPv4 filter successfully.
909 @retval FALSE Failed to pass the IPv4 filter.
914 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
916 IN OUT EFI_IP_ADDRESS
*DestIp
,
920 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP
) != 0) {
922 // Copy the destination address from the received packet if accept any.
924 if (DestIp
!= NULL
) {
925 if (Mode
->UsingIpv6
) {
928 &((EFI_UDP6_SESSION_DATA
*)Session
)->DestinationAddress
,
929 sizeof (EFI_IPv6_ADDRESS
)
932 ZeroMem (DestIp
, sizeof (EFI_IP_ADDRESS
));
935 &((EFI_UDP4_SESSION_DATA
*)Session
)->DestinationAddress
,
936 sizeof (EFI_IPv4_ADDRESS
)
942 } else if (DestIp
!= NULL
&&
943 (EFI_IP4_EQUAL (DestIp
, &((EFI_UDP4_SESSION_DATA
*)Session
)->DestinationAddress
) ||
944 EFI_IP6_EQUAL (DestIp
, &((EFI_UDP6_SESSION_DATA
*)Session
)->DestinationAddress
))) {
946 // The destination address in the received packet is matched if present.
949 } else if (EFI_IP4_EQUAL (&Mode
->StationIp
, &((EFI_UDP4_SESSION_DATA
*)Session
)->DestinationAddress
) ||
950 EFI_IP6_EQUAL (&Mode
->StationIp
, &((EFI_UDP6_SESSION_DATA
*)Session
)->DestinationAddress
)) {
952 // The destination address in the received packet is equal to the host address.
962 Check the received packet using the destination port.
964 @param[in] Mode The pointer to the mode data of PxeBc.
965 @param[in] Session The pointer to the current UDPv4 session.
966 @param[in, out] DestPort The pointer to the destination port.
967 @param[in] OpFlags Operation flag for UdpRead/UdpWrite.
969 @retval TRUE Passed the IPv4 filter successfully.
970 @retval FALSE Failed to pass the IPv4 filter.
974 PxeBcCheckByDestPort (
975 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
977 IN OUT UINT16
*DestPort
,
983 if (Mode
->UsingIpv6
) {
984 Port
= ((EFI_UDP6_SESSION_DATA
*) Session
)->DestinationPort
;
986 Port
= ((EFI_UDP4_SESSION_DATA
*) Session
)->DestinationPort
;
989 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT
) != 0) {
991 // Return the destination port in the received packet if accept any.
993 if (DestPort
!= NULL
) {
997 } else if (DestPort
!= NULL
&& *DestPort
== Port
) {
999 // The destination port in the received packet is matched if present.
1009 Filter the received packet using the source Ip.
1011 @param[in] Mode The pointer to the mode data of PxeBc.
1012 @param[in] Session The pointer to the current UDPv4 session.
1013 @param[in, out] SrcIp The pointer to the source Ip address.
1014 @param[in] OpFlags Operation flag for UdpRead/UdpWrite.
1016 @retval TRUE Passed the IPv4 filter successfully.
1017 @retval FALSE Failed to pass the IPv4 filter.
1021 PxeBcFilterBySrcIp (
1022 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
1024 IN OUT EFI_IP_ADDRESS
*SrcIp
,
1028 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP
) != 0) {
1030 // Copy the source address from the received packet if accept any.
1032 if (SrcIp
!= NULL
) {
1033 if (Mode
->UsingIpv6
) {
1036 &((EFI_UDP6_SESSION_DATA
*)Session
)->SourceAddress
,
1037 sizeof (EFI_IPv6_ADDRESS
)
1040 ZeroMem (SrcIp
, sizeof (EFI_IP_ADDRESS
));
1043 &((EFI_UDP4_SESSION_DATA
*)Session
)->SourceAddress
,
1044 sizeof (EFI_IPv4_ADDRESS
)
1050 } else if (SrcIp
!= NULL
&&
1051 (EFI_IP4_EQUAL (SrcIp
, &((EFI_UDP4_SESSION_DATA
*)Session
)->SourceAddress
) ||
1052 EFI_IP6_EQUAL (SrcIp
, &((EFI_UDP6_SESSION_DATA
*)Session
)->SourceAddress
))) {
1054 // The source address in the received packet is matched if present.
1064 Filter the received packet using the source port.
1066 @param[in] Mode The pointer to the mode data of PxeBc.
1067 @param[in] Session The pointer to the current UDPv4 session.
1068 @param[in, out] SrcPort The pointer to the source port.
1069 @param[in] OpFlags Operation flag for UdpRead/UdpWrite.
1071 @retval TRUE Passed the IPv4 filter successfully.
1072 @retval FALSE Failed to pass the IPv4 filter.
1076 PxeBcFilterBySrcPort (
1077 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
1079 IN OUT UINT16
*SrcPort
,
1085 if (Mode
->UsingIpv6
) {
1086 Port
= ((EFI_UDP6_SESSION_DATA
*) Session
)->SourcePort
;
1088 Port
= ((EFI_UDP4_SESSION_DATA
*) Session
)->SourcePort
;
1091 if ((OpFlags
& EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT
) != 0) {
1093 // Return the source port in the received packet if accept any.
1095 if (SrcPort
!= NULL
) {
1099 } else if (SrcPort
!= NULL
&& *SrcPort
== Port
) {
1101 // The source port in the received packet is matched if present.
1111 This function is to receive packet using Udp4Read.
1113 @param[in] Udp4 The pointer to EFI_UDP4_PROTOCOL.
1114 @param[in] Token The pointer to EFI_UDP4_COMPLETION_TOKEN.
1115 @param[in] Mode The pointer to EFI_PXE_BASE_CODE_MODE.
1116 @param[in] TimeoutEvent The event for timeout.
1117 @param[in] OpFlags The UDP operation flags.
1118 @param[in] IsDone The pointer to the IsDone flag.
1119 @param[out] IsMatched The pointer to the IsMatched flag.
1120 @param[in, out] DestIp The pointer to the destination address.
1121 @param[in, out] DestPort The pointer to the destination port.
1122 @param[in, out] SrcIp The pointer to the source address.
1123 @param[in, out] SrcPort The pointer to the source port.
1125 @retval EFI_SUCCESS Successfully read the data using Udp4.
1126 @retval Others Failed to send out data.
1131 IN EFI_UDP4_PROTOCOL
*Udp4
,
1132 IN EFI_UDP4_COMPLETION_TOKEN
*Token
,
1133 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
1134 IN EFI_EVENT TimeoutEvent
,
1137 OUT BOOLEAN
*IsMatched
,
1138 IN OUT EFI_IP_ADDRESS
*DestIp OPTIONAL
,
1139 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*DestPort OPTIONAL
,
1140 IN OUT EFI_IP_ADDRESS
*SrcIp OPTIONAL
,
1141 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPort OPTIONAL
1144 EFI_UDP4_RECEIVE_DATA
*RxData
;
1145 EFI_UDP4_SESSION_DATA
*Session
;
1148 Token
->Status
= EFI_NOT_READY
;
1151 Status
= Udp4
->Receive (Udp4
, Token
);
1152 if (EFI_ERROR (Status
)) {
1157 // Poll the UDPv6 read instance if no packet received and no timeout triggered.
1159 while (!(*IsDone
) &&
1160 Token
->Status
== EFI_NOT_READY
&&
1161 EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
1163 // Poll the token utill reply/ICMPv6 error message received or timeout.
1166 if (Token
->Status
== EFI_ICMP_ERROR
||
1167 Token
->Status
== EFI_NETWORK_UNREACHABLE
||
1168 Token
->Status
== EFI_HOST_UNREACHABLE
||
1169 Token
->Status
== EFI_PROTOCOL_UNREACHABLE
||
1170 Token
->Status
== EFI_PORT_UNREACHABLE
) {
1175 Status
= (Token
->Status
== EFI_NOT_READY
) ? EFI_TIMEOUT
: Token
->Status
;
1177 if (!EFI_ERROR (Status
)) {
1179 // check whether this packet matches the filters
1181 RxData
= Token
->Packet
.RxData
;
1182 Session
= &RxData
->UdpSession
;
1184 *IsMatched
= PxeBcCheckByIpFilter (Mode
, Session
, OpFlags
);
1187 *IsMatched
= PxeBcCheckByDestIp (Mode
, Session
, DestIp
, OpFlags
);
1191 *IsMatched
= PxeBcCheckByDestPort (Mode
, Session
, DestPort
, OpFlags
);
1195 *IsMatched
= PxeBcFilterBySrcIp (Mode
, Session
, SrcIp
, OpFlags
);
1199 *IsMatched
= PxeBcFilterBySrcPort (Mode
, Session
, SrcPort
, OpFlags
);
1202 if (!(*IsMatched
)) {
1204 // Recycle the receiving buffer if not matched.
1206 gBS
->SignalEvent (RxData
->RecycleSignal
);
1215 This function is to receive packets using Udp6Read.
1217 @param[in] Udp6 The pointer to EFI_UDP6_PROTOCOL.
1218 @param[in] Token The pointer to EFI_UDP6_COMPLETION_TOKEN.
1219 @param[in] Mode The pointer to EFI_PXE_BASE_CODE_MODE.
1220 @param[in] TimeoutEvent The event for timeout.
1221 @param[in] OpFlags The UDP operation flags.
1222 @param[in] IsDone The pointer to the IsDone flag.
1223 @param[out] IsMatched The pointer to the IsMatched flag.
1224 @param[in, out] DestIp The pointer to the destination address.
1225 @param[in, out] DestPort The pointer to the destination port.
1226 @param[in, out] SrcIp The pointer to the source address.
1227 @param[in, out] SrcPort The pointer to the source port.
1229 @retval EFI_SUCCESS Successfully read data using Udp6.
1230 @retval Others Failed to send out data.
1235 IN EFI_UDP6_PROTOCOL
*Udp6
,
1236 IN EFI_UDP6_COMPLETION_TOKEN
*Token
,
1237 IN EFI_PXE_BASE_CODE_MODE
*Mode
,
1238 IN EFI_EVENT TimeoutEvent
,
1241 OUT BOOLEAN
*IsMatched
,
1242 IN OUT EFI_IP_ADDRESS
*DestIp OPTIONAL
,
1243 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*DestPort OPTIONAL
,
1244 IN OUT EFI_IP_ADDRESS
*SrcIp OPTIONAL
,
1245 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPort OPTIONAL
1248 EFI_UDP6_RECEIVE_DATA
*RxData
;
1249 EFI_UDP6_SESSION_DATA
*Session
;
1252 Token
->Status
= EFI_NOT_READY
;
1255 Status
= Udp6
->Receive (Udp6
, Token
);
1256 if (EFI_ERROR (Status
)) {
1261 // Poll the UDPv6 read instance if no packet received and no timeout triggered.
1263 while (!(*IsDone
) &&
1264 Token
->Status
== EFI_NOT_READY
&&
1265 EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
1267 // Poll the token utill reply/ICMPv6 error message received or timeout.
1270 if (Token
->Status
== EFI_ICMP_ERROR
||
1271 Token
->Status
== EFI_NETWORK_UNREACHABLE
||
1272 Token
->Status
== EFI_HOST_UNREACHABLE
||
1273 Token
->Status
== EFI_PROTOCOL_UNREACHABLE
||
1274 Token
->Status
== EFI_PORT_UNREACHABLE
) {
1279 Status
= (Token
->Status
== EFI_NOT_READY
) ? EFI_TIMEOUT
: Token
->Status
;
1281 if (!EFI_ERROR (Status
)) {
1283 // check whether this packet matches the filters
1285 RxData
= Token
->Packet
.RxData
;
1286 Session
= &RxData
->UdpSession
;
1288 *IsMatched
= PxeBcCheckByIpFilter (Mode
, Session
, OpFlags
);
1291 *IsMatched
= PxeBcCheckByDestIp (Mode
, Session
, DestIp
, OpFlags
);
1295 *IsMatched
= PxeBcCheckByDestPort (Mode
, Session
, DestPort
, OpFlags
);
1299 *IsMatched
= PxeBcFilterBySrcIp (Mode
, Session
, SrcIp
, OpFlags
);
1303 *IsMatched
= PxeBcFilterBySrcPort (Mode
, Session
, SrcPort
, OpFlags
);
1306 if (!(*IsMatched
)) {
1308 // Recycle the receiving buffer if not matched.
1310 gBS
->SignalEvent (RxData
->RecycleSignal
);
1319 This function is to display the IPv4 address.
1321 @param[in] Ip The pointer to the IPv4 address.
1326 IN EFI_IPv4_ADDRESS
*Ip
1331 for (Index
= 0; Index
< 4; Index
++) {
1332 AsciiPrint ("%d", Ip
->Addr
[Index
]);
1341 This function is to display the IPv6 address.
1343 @param[in] Ip The pointer to the IPv6 address.
1348 IN EFI_IPv6_ADDRESS
*Ip
1353 for (Index
= 0; Index
< 16; Index
++) {
1355 if (Ip
->Addr
[Index
] != 0) {
1356 AsciiPrint ("%x", Ip
->Addr
[Index
]);
1362 if (((Ip
->Addr
[Index
] & 0xf0) == 0) && (Ip
->Addr
[Index
- 1] != 0)) {
1365 AsciiPrint ("%x", Ip
->Addr
[Index
]);
1374 This function is to convert UINTN to ASCII string with the required formatting.
1376 @param[in] Number Numeric value to be converted.
1377 @param[in] Buffer The pointer to the buffer for ASCII string.
1378 @param[in] Length The length of the required format.
1382 PxeBcUintnToAscDecWithFormat (
1390 for (; Length
> 0; Length
--) {
1391 Remainder
= Number
% 10;
1393 Buffer
[Length
- 1] = (UINT8
) ('0' + Remainder
);
1399 This function is to convert a UINTN to a ASCII string, and return the
1400 actual length of the buffer.
1402 @param[in] Number Numeric value to be converted.
1403 @param[in] Buffer The pointer to the buffer for ASCII string.
1404 @param[in] BufferSize The maxsize of the buffer.
1406 @return Length The actual length of the ASCII string.
1410 PxeBcUintnToAscDec (
1425 TempStr
[Index
] = (CHAR8
) ('0' + (Number
% 10));
1426 Number
= (UINTN
) (Number
/ 10);
1427 } while (Number
!= 0);
1429 AsciiStrCpyS ((CHAR8
*) Buffer
, BufferSize
, &TempStr
[Index
]);
1431 Length
= AsciiStrLen ((CHAR8
*) Buffer
);
1438 This function is to convert unicode hex number to a UINT8.
1440 @param[out] Digit The converted UINT8 for output.
1441 @param[in] Char The unicode hex number to be converted.
1443 @retval EFI_SUCCESS Successfully converted the unicode hex.
1444 @retval EFI_INVALID_PARAMETER Failed to convert the unicode hex.
1448 PxeBcUniHexToUint8 (
1453 if ((Char
>= L
'0') && (Char
<= L
'9')) {
1454 *Digit
= (UINT8
) (Char
- L
'0');
1458 if ((Char
>= L
'A') && (Char
<= L
'F')) {
1459 *Digit
= (UINT8
) (Char
- L
'A' + 0x0A);
1463 if ((Char
>= L
'a') && (Char
<= L
'f')) {
1464 *Digit
= (UINT8
) (Char
- L
'a' + 0x0A);
1468 return EFI_INVALID_PARAMETER
;
1472 Calculate the elapsed time.
1474 @param[in] Private The pointer to PXE private data
1479 IN PXEBC_PRIVATE_DATA
*Private
1483 UINT64 CurrentStamp
;
1484 UINT64 ElapsedTimeValue
;
1487 // Generate a time stamp of the centiseconds from 1900/1/1, assume 30day/month.
1489 ZeroMem (&Time
, sizeof (EFI_TIME
));
1490 gRT
->GetTime (&Time
, NULL
);
1491 CurrentStamp
= (UINT64
)
1493 ((((((Time
.Year
- 1900) * 360 +
1494 (Time
.Month
- 1)) * 30 +
1495 (Time
.Day
- 1)) * 24 + Time
.Hour
) * 60 +
1496 Time
.Minute
) * 60 + Time
.Second
) * 100
1497 + DivU64x32(Time
.Nanosecond
, 10000000)
1501 // Sentinel value of 0 means that this is the first DHCP packet that we are
1502 // sending and that we need to initialize the value. First DHCP Solicit
1503 // gets 0 elapsed-time. Otherwise, calculate based on StartTime.
1505 if (Private
->ElapsedTime
== 0) {
1506 Private
->ElapsedTime
= CurrentStamp
;
1508 ElapsedTimeValue
= CurrentStamp
- Private
->ElapsedTime
;
1511 // If elapsed time cannot fit in two bytes, set it to 0xffff.
1513 if (ElapsedTimeValue
> 0xffff) {
1514 ElapsedTimeValue
= 0xffff;
1517 // Save the elapsed time
1519 Private
->ElapsedTime
= ElapsedTimeValue
;