4 Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<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.
17 #include <IndustryStandard/SmBios.h>
19 #include <Protocol/DriverBinding.h>
20 #include <Protocol/ServiceBinding.h>
21 #include <Protocol/SimpleNetwork.h>
22 #include <Protocol/ManagedNetwork.h>
23 #include <Protocol/Ip4Config2.h>
24 #include <Protocol/ComponentName.h>
25 #include <Protocol/ComponentName2.h>
27 #include <Guid/SmBios.h>
29 #include <Library/NetLib.h>
30 #include <Library/BaseLib.h>
31 #include <Library/DebugLib.h>
32 #include <Library/BaseMemoryLib.h>
33 #include <Library/UefiBootServicesTableLib.h>
34 #include <Library/UefiRuntimeServicesTableLib.h>
35 #include <Library/MemoryAllocationLib.h>
36 #include <Library/DevicePathLib.h>
37 #include <Library/PrintLib.h>
38 #include <Library/UefiLib.h>
40 #define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE
41 #define DEFAULT_ZERO_START ((UINTN) ~0)
44 // All the supported IP4 maskes in host byte order.
46 GLOBAL_REMOVE_IF_UNREFERENCED IP4_ADDR gIp4AllMasks
[IP4_MASK_NUM
] = {
85 GLOBAL_REMOVE_IF_UNREFERENCED EFI_IPv4_ADDRESS mZeroIp4Addr
= {{0, 0, 0, 0}};
88 // Any error level digitally larger than mNetDebugLevelMax
89 // will be silently discarded.
91 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mNetDebugLevelMax
= NETDEBUG_LEVEL_ERROR
;
92 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogPacketSeq
= 0xDEADBEEF;
95 // You can change mSyslogDstMac mSyslogDstIp and mSyslogSrcIp
96 // here to direct the syslog packets to the syslog deamon. The
97 // default is broadcast to both the ethernet and IP.
99 GLOBAL_REMOVE_IF_UNREFERENCED UINT8 mSyslogDstMac
[NET_ETHER_ADDR_LEN
] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
100 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogDstIp
= 0xffffffff;
101 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogSrcIp
= 0;
103 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8
*mMonthName
[] = {
119 // VLAN device path node template
121 GLOBAL_REMOVE_IF_UNREFERENCED VLAN_DEVICE_PATH mNetVlanDevicePathTemplate
= {
123 MESSAGING_DEVICE_PATH
,
126 (UINT8
) (sizeof (VLAN_DEVICE_PATH
)),
127 (UINT8
) ((sizeof (VLAN_DEVICE_PATH
)) >> 8)
134 Locate the handles that support SNP, then open one of them
135 to send the syslog packets. The caller isn't required to close
136 the SNP after use because the SNP is opened by HandleProtocol.
138 @return The point to SNP if one is properly openned. Otherwise NULL
141 EFI_SIMPLE_NETWORK_PROTOCOL
*
146 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
153 // Locate the handles which has SNP installed.
156 Status
= gBS
->LocateHandleBuffer (
158 &gEfiSimpleNetworkProtocolGuid
,
164 if (EFI_ERROR (Status
) || (HandleCount
== 0)) {
169 // Try to open one of the ethernet SNP protocol to send packet
173 for (Index
= 0; Index
< HandleCount
; Index
++) {
174 Status
= gBS
->HandleProtocol (
176 &gEfiSimpleNetworkProtocolGuid
,
180 if ((Status
== EFI_SUCCESS
) && (Snp
!= NULL
) &&
181 (Snp
->Mode
->IfType
== NET_IFTYPE_ETHERNET
) &&
182 (Snp
->Mode
->MaxPacketSize
>= NET_SYSLOG_PACKET_LEN
)) {
195 Transmit a syslog packet synchronously through SNP. The Packet
196 already has the ethernet header prepended. This function should
197 fill in the source MAC because it will try to locate a SNP each
198 time it is called to avoid the problem if SNP is unloaded.
199 This code snip is copied from MNP.
201 @param[in] Packet The Syslog packet
202 @param[in] Length The length of the packet
204 @retval EFI_DEVICE_ERROR Failed to locate a usable SNP protocol
205 @retval EFI_TIMEOUT Timeout happened to send the packet.
206 @retval EFI_SUCCESS Packet is sent.
215 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
218 EFI_EVENT TimeoutEvent
;
221 Snp
= SyslogLocateSnp ();
224 return EFI_DEVICE_ERROR
;
227 Ether
= (ETHER_HEAD
*) Packet
;
228 CopyMem (Ether
->SrcMac
, Snp
->Mode
->CurrentAddress
.Addr
, NET_ETHER_ADDR_LEN
);
231 // Start the timeout event.
233 Status
= gBS
->CreateEvent (
241 if (EFI_ERROR (Status
)) {
245 Status
= gBS
->SetTimer (TimeoutEvent
, TimerRelative
, NET_SYSLOG_TX_TIMEOUT
);
247 if (EFI_ERROR (Status
)) {
253 // Transmit the packet through SNP.
255 Status
= Snp
->Transmit (Snp
, 0, Length
, Packet
, NULL
, NULL
, NULL
);
257 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_READY
)) {
258 Status
= EFI_DEVICE_ERROR
;
263 // If Status is EFI_SUCCESS, the packet is put in the transmit queue.
264 // if Status is EFI_NOT_READY, the transmit engine of the network
265 // interface is busy. Both need to sync SNP.
271 // Get the recycled transmit buffer status.
273 Snp
->GetStatus (Snp
, NULL
, (VOID
**) &TxBuf
);
275 if (!EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
276 Status
= EFI_TIMEOUT
;
280 } while (TxBuf
== NULL
);
282 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_TIMEOUT
)) {
287 // Status is EFI_NOT_READY. Restart the timer event and
288 // call Snp->Transmit again.
290 gBS
->SetTimer (TimeoutEvent
, TimerRelative
, NET_SYSLOG_TX_TIMEOUT
);
293 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
296 gBS
->CloseEvent (TimeoutEvent
);
301 Build a syslog packet, including the Ethernet/Ip/Udp headers
304 @param[in] Level Syslog severity level
305 @param[in] Module The module that generates the log
306 @param[in] File The file that contains the current log
307 @param[in] Line The line of code in the File that contains the current log
308 @param[in] Message The log message
309 @param[in] BufLen The lenght of the Buf
310 @param[out] Buf The buffer to put the packet data
312 @return The length of the syslog packet built.
328 EFI_UDP_HEADER
*Udp4
;
334 // Fill in the Ethernet header. Leave alone the source MAC.
335 // SyslogSendPacket will fill in the address for us.
337 Ether
= (ETHER_HEAD
*) Buf
;
338 CopyMem (Ether
->DstMac
, mSyslogDstMac
, NET_ETHER_ADDR_LEN
);
339 ZeroMem (Ether
->SrcMac
, NET_ETHER_ADDR_LEN
);
341 Ether
->EtherType
= HTONS (0x0800); // IPv4 protocol
343 Buf
+= sizeof (ETHER_HEAD
);
344 BufLen
-= sizeof (ETHER_HEAD
);
347 // Fill in the IP header
349 Ip4
= (IP4_HEAD
*) Buf
;
354 Ip4
->Id
= (UINT16
) mSyslogPacketSeq
;
357 Ip4
->Protocol
= 0x11;
359 Ip4
->Src
= mSyslogSrcIp
;
360 Ip4
->Dst
= mSyslogDstIp
;
362 Buf
+= sizeof (IP4_HEAD
);
363 BufLen
-= sizeof (IP4_HEAD
);
366 // Fill in the UDP header, Udp checksum is optional. Leave it zero.
368 Udp4
= (EFI_UDP_HEADER
*) Buf
;
369 Udp4
->SrcPort
= HTONS (514);
370 Udp4
->DstPort
= HTONS (514);
374 Buf
+= sizeof (EFI_UDP_HEADER
);
375 BufLen
-= sizeof (EFI_UDP_HEADER
);
378 // Build the syslog message body with <PRI> Timestamp machine module Message
380 Pri
= ((NET_SYSLOG_FACILITY
& 31) << 3) | (Level
& 7);
381 gRT
->GetTime (&Time
, NULL
);
382 ASSERT ((Time
.Month
<= 12) && (Time
.Month
>= 1));
385 // Use %a to format the ASCII strings, %s to format UNICODE strings
388 Len
+= (UINT32
) AsciiSPrint (
391 "<%d> %a %d %d:%d:%d ",
393 mMonthName
[Time
.Month
-1],
401 Len
+= (UINT32
) AsciiSPrint (
404 "Tiano %a: %a (Line: %d File: %a)",
413 // OK, patch the IP length/checksum and UDP length fields.
415 Len
+= sizeof (EFI_UDP_HEADER
);
416 Udp4
->Length
= HTONS ((UINT16
) Len
);
418 Len
+= sizeof (IP4_HEAD
);
419 Ip4
->TotalLen
= HTONS ((UINT16
) Len
);
420 Ip4
->Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Ip4
, sizeof (IP4_HEAD
)));
422 return Len
+ sizeof (ETHER_HEAD
);
426 Allocate a buffer, then format the message to it. This is a
427 help function for the NET_DEBUG_XXX macros. The PrintArg of
428 these macros treats the variable length print parameters as a
429 single parameter, and pass it to the NetDebugASPrint. For
430 example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name))
434 NETDEBUG_LEVEL_TRACE,
438 NetDebugASPrint ("State transit to %a\n", Name)
441 @param Format The ASCII format string.
442 @param ... The variable length parameter whose format is determined
443 by the Format string.
445 @return The buffer containing the formatted message,
446 or NULL if failed to allocate memory.
459 Buf
= (CHAR8
*) AllocatePool (NET_DEBUG_MSG_LEN
);
465 VA_START (Marker
, Format
);
466 AsciiVSPrint (Buf
, NET_DEBUG_MSG_LEN
, Format
, Marker
);
473 Builds an UDP4 syslog packet and send it using SNP.
475 This function will locate a instance of SNP then send the message through it.
476 Because it isn't open the SNP BY_DRIVER, apply caution when using it.
478 @param Level The severity level of the message.
479 @param Module The Moudle that generates the log.
480 @param File The file that contains the log.
481 @param Line The exact line that contains the log.
482 @param Message The user message to log.
484 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
485 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet
486 @retval EFI_SUCCESS The log is discard because that it is more verbose
487 than the mNetDebugLevelMax. Or, it has been sent out.
504 // Check whether the message should be sent out
506 if (Message
== NULL
) {
507 return EFI_INVALID_PARAMETER
;
510 if (Level
> mNetDebugLevelMax
) {
511 Status
= EFI_SUCCESS
;
516 // Allocate a maxium of 1024 bytes, the caller should ensure
517 // that the message plus the ethernet/ip/udp header is shorter
520 Packet
= (CHAR8
*) AllocatePool (NET_SYSLOG_PACKET_LEN
);
522 if (Packet
== NULL
) {
523 Status
= EFI_OUT_OF_RESOURCES
;
528 // Build the message: Ethernet header + IP header + Udp Header + user data
530 Len
= SyslogBuildPacket (
536 NET_SYSLOG_PACKET_LEN
,
541 Status
= SyslogSendPacket (Packet
, Len
);
549 Return the length of the mask.
551 Return the length of the mask, the correct value is from 0 to 32.
552 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.
553 NetMask is in the host byte order.
555 @param[in] NetMask The netmask to get the length from.
557 @return The length of the netmask, IP4_MASK_NUM if the mask is invalid.
568 for (Index
= 0; Index
<= IP4_MASK_MAX
; Index
++) {
569 if (NetMask
== gIp4AllMasks
[Index
]) {
580 Return the class of the IP address, such as class A, B, C.
581 Addr is in host byte order.
584 Classful addressing (IP class A/B/C) has been deprecated according to RFC4632.
585 Caller of this function could only check the returned value against
586 IP4_ADDR_CLASSD (multicast) or IP4_ADDR_CLASSE (reserved) now.
588 The address of class A starts with 0.
589 If the address belong to class A, return IP4_ADDR_CLASSA.
590 The address of class B starts with 10.
591 If the address belong to class B, return IP4_ADDR_CLASSB.
592 The address of class C starts with 110.
593 If the address belong to class C, return IP4_ADDR_CLASSC.
594 The address of class D starts with 1110.
595 If the address belong to class D, return IP4_ADDR_CLASSD.
596 The address of class E starts with 1111.
597 If the address belong to class E, return IP4_ADDR_CLASSE.
600 @param[in] Addr The address to get the class from.
602 @return IP address class, such as IP4_ADDR_CLASSA.
613 ByteOne
= (UINT8
) (Addr
>> 24);
615 if ((ByteOne
& 0x80) == 0) {
616 return IP4_ADDR_CLASSA
;
618 } else if ((ByteOne
& 0xC0) == 0x80) {
619 return IP4_ADDR_CLASSB
;
621 } else if ((ByteOne
& 0xE0) == 0xC0) {
622 return IP4_ADDR_CLASSC
;
624 } else if ((ByteOne
& 0xF0) == 0xE0) {
625 return IP4_ADDR_CLASSD
;
628 return IP4_ADDR_CLASSE
;
635 Check whether the IP is a valid unicast address according to
638 ASSERT if NetMask is zero.
640 If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address.
642 @param[in] Ip The IP to check against.
643 @param[in] NetMask The mask of the IP.
645 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.
655 ASSERT (NetMask
!= 0);
657 if (Ip
== 0 || IP4_IS_LOCAL_BROADCAST (Ip
)) {
661 if (((Ip
&~NetMask
) == ~NetMask
) || ((Ip
&~NetMask
) == 0)) {
669 Check whether the incoming IPv6 address is a valid unicast address.
671 If the address is a multicast address has binary 0xFF at the start, it is not
672 a valid unicast address. If the address is unspecified ::, it is not a valid
673 unicast address to be assigned to any node. If the address is loopback address
674 ::1, it is also not a valid unicast address to be assigned to any physical
677 @param[in] Ip6 The IPv6 address to check against.
679 @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE.
684 NetIp6IsValidUnicast (
685 IN EFI_IPv6_ADDRESS
*Ip6
691 if (Ip6
->Addr
[0] == 0xFF) {
695 for (Index
= 0; Index
< 15; Index
++) {
696 if (Ip6
->Addr
[Index
] != 0) {
701 Byte
= Ip6
->Addr
[Index
];
703 if (Byte
== 0x0 || Byte
== 0x1) {
711 Check whether the incoming Ipv6 address is the unspecified address or not.
713 @param[in] Ip6 - Ip6 address, in network order.
715 @retval TRUE - Yes, unspecified
721 NetIp6IsUnspecifiedAddr (
722 IN EFI_IPv6_ADDRESS
*Ip6
727 for (Index
= 0; Index
< 16; Index
++) {
728 if (Ip6
->Addr
[Index
] != 0) {
737 Check whether the incoming Ipv6 address is a link-local address.
739 @param[in] Ip6 - Ip6 address, in network order.
741 @retval TRUE - Yes, link-local address
747 NetIp6IsLinkLocalAddr (
748 IN EFI_IPv6_ADDRESS
*Ip6
753 ASSERT (Ip6
!= NULL
);
755 if (Ip6
->Addr
[0] != 0xFE) {
759 if (Ip6
->Addr
[1] != 0x80) {
763 for (Index
= 2; Index
< 8; Index
++) {
764 if (Ip6
->Addr
[Index
] != 0) {
773 Check whether the Ipv6 address1 and address2 are on the connected network.
775 @param[in] Ip1 - Ip6 address1, in network order.
776 @param[in] Ip2 - Ip6 address2, in network order.
777 @param[in] PrefixLength - The prefix length of the checking net.
779 @retval TRUE - Yes, connected.
786 EFI_IPv6_ADDRESS
*Ip1
,
787 EFI_IPv6_ADDRESS
*Ip2
,
795 ASSERT ((Ip1
!= NULL
) && (Ip2
!= NULL
) && (PrefixLength
<= IP6_PREFIX_MAX
));
797 if (PrefixLength
== 0) {
801 Byte
= (UINT8
) (PrefixLength
/ 8);
802 Bit
= (UINT8
) (PrefixLength
% 8);
804 if (CompareMem (Ip1
, Ip2
, Byte
) != 0) {
809 Mask
= (UINT8
) (0xFF << (8 - Bit
));
812 if ((Ip1
->Addr
[Byte
] & Mask
) != (Ip2
->Addr
[Byte
] & Mask
)) {
822 Switches the endianess of an IPv6 address
824 This function swaps the bytes in a 128-bit IPv6 address to switch the value
825 from little endian to big endian or vice versa. The byte swapped value is
828 @param Ip6 Points to an IPv6 address
830 @return The byte swapped IPv6 address.
836 EFI_IPv6_ADDRESS
*Ip6
842 CopyMem (&High
, Ip6
, sizeof (UINT64
));
843 CopyMem (&Low
, &Ip6
->Addr
[8], sizeof (UINT64
));
845 High
= SwapBytes64 (High
);
846 Low
= SwapBytes64 (Low
);
848 CopyMem (Ip6
, &Low
, sizeof (UINT64
));
849 CopyMem (&Ip6
->Addr
[8], &High
, sizeof (UINT64
));
855 Initialize a random seed using current time and monotonic count.
857 Get current time and monotonic count first. Then initialize a random seed
858 based on some basic mathematics operation on the hour, day, minute, second,
859 nanosecond and year of the current time and the monotonic count value.
861 @return The random seed initialized with current time.
872 UINT64 MonotonicCount
;
874 gRT
->GetTime (&Time
, NULL
);
875 Seed
= (~Time
.Hour
<< 24 | Time
.Day
<< 16 | Time
.Minute
<< 8 | Time
.Second
);
876 Seed
^= Time
.Nanosecond
;
877 Seed
^= Time
.Year
<< 7;
879 gBS
->GetNextMonotonicCount (&MonotonicCount
);
880 Seed
+= (UINT32
) MonotonicCount
;
887 Extract a UINT32 from a byte stream.
889 Copy a UINT32 from a byte stream, then converts it from Network
890 byte order to host byte order. Use this function to avoid alignment error.
892 @param[in] Buf The buffer to extract the UINT32.
894 @return The UINT32 extracted.
905 CopyMem (&Value
, Buf
, sizeof (UINT32
));
906 return NTOHL (Value
);
911 Put a UINT32 to the byte stream in network byte order.
913 Converts a UINT32 from host byte order to network byte order. Then copy it to the
916 @param[in, out] Buf The buffer to put the UINT32.
917 @param[in] Data The data to be converted and put into the byte stream.
928 CopyMem (Buf
, &Data
, sizeof (UINT32
));
933 Remove the first node entry on the list, and return the removed node entry.
935 Removes the first node Entry from a doubly linked list. It is up to the caller of
936 this function to release the memory used by the first node if that is required. On
937 exit, the removed node is returned.
939 If Head is NULL, then ASSERT().
940 If Head was not initialized, then ASSERT().
941 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
942 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
945 @param[in, out] Head The list header.
947 @return The first node entry that is removed from the list, NULL if the list is empty.
953 IN OUT LIST_ENTRY
*Head
958 ASSERT (Head
!= NULL
);
960 if (IsListEmpty (Head
)) {
964 First
= Head
->ForwardLink
;
965 Head
->ForwardLink
= First
->ForwardLink
;
966 First
->ForwardLink
->BackLink
= Head
;
969 First
->ForwardLink
= (LIST_ENTRY
*) NULL
;
970 First
->BackLink
= (LIST_ENTRY
*) NULL
;
978 Remove the last node entry on the list and and return the removed node entry.
980 Removes the last node entry from a doubly linked list. It is up to the caller of
981 this function to release the memory used by the first node if that is required. On
982 exit, the removed node is returned.
984 If Head is NULL, then ASSERT().
985 If Head was not initialized, then ASSERT().
986 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
987 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
990 @param[in, out] Head The list head.
992 @return The last node entry that is removed from the list, NULL if the list is empty.
998 IN OUT LIST_ENTRY
*Head
1003 ASSERT (Head
!= NULL
);
1005 if (IsListEmpty (Head
)) {
1009 Last
= Head
->BackLink
;
1010 Head
->BackLink
= Last
->BackLink
;
1011 Last
->BackLink
->ForwardLink
= Head
;
1014 Last
->ForwardLink
= (LIST_ENTRY
*) NULL
;
1015 Last
->BackLink
= (LIST_ENTRY
*) NULL
;
1023 Insert a new node entry after a designated node entry of a doubly linked list.
1025 Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry
1026 of the doubly linked list.
1028 @param[in, out] PrevEntry The previous entry to insert after.
1029 @param[in, out] NewEntry The new entry to insert.
1034 NetListInsertAfter (
1035 IN OUT LIST_ENTRY
*PrevEntry
,
1036 IN OUT LIST_ENTRY
*NewEntry
1039 NewEntry
->BackLink
= PrevEntry
;
1040 NewEntry
->ForwardLink
= PrevEntry
->ForwardLink
;
1041 PrevEntry
->ForwardLink
->BackLink
= NewEntry
;
1042 PrevEntry
->ForwardLink
= NewEntry
;
1047 Insert a new node entry before a designated node entry of a doubly linked list.
1049 Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry
1050 of the doubly linked list.
1052 @param[in, out] PostEntry The entry to insert before.
1053 @param[in, out] NewEntry The new entry to insert.
1058 NetListInsertBefore (
1059 IN OUT LIST_ENTRY
*PostEntry
,
1060 IN OUT LIST_ENTRY
*NewEntry
1063 NewEntry
->ForwardLink
= PostEntry
;
1064 NewEntry
->BackLink
= PostEntry
->BackLink
;
1065 PostEntry
->BackLink
->ForwardLink
= NewEntry
;
1066 PostEntry
->BackLink
= NewEntry
;
1070 Safe destroy nodes in a linked list, and return the length of the list after all possible operations finished.
1072 Destroy network child instance list by list traversals is not safe due to graph dependencies between nodes.
1073 This function performs a safe traversal to destroy these nodes by checking to see if the node being destroyed
1074 has been removed from the list or not.
1075 If it has been removed, then restart the traversal from the head.
1076 If it hasn't been removed, then continue with the next node directly.
1077 This function will end the iterate and return the CallBack's last return value if error happens,
1078 or retrun EFI_SUCCESS if 2 complete passes are made with no changes in the number of children in the list.
1080 @param[in] List The head of the list.
1081 @param[in] CallBack Pointer to the callback function to destroy one node in the list.
1082 @param[in] Context Pointer to the callback function's context: corresponds to the
1083 parameter Context in NET_DESTROY_LINK_LIST_CALLBACK.
1084 @param[out] ListLength The length of the link list if the function returns successfully.
1086 @retval EFI_SUCCESS Two complete passes are made with no changes in the number of children.
1087 @retval EFI_INVALID_PARAMETER The input parameter is invalid.
1088 @retval Others Return the CallBack's last return value.
1093 NetDestroyLinkList (
1094 IN LIST_ENTRY
*List
,
1095 IN NET_DESTROY_LINK_LIST_CALLBACK CallBack
,
1096 IN VOID
*Context
, OPTIONAL
1097 OUT UINTN
*ListLength OPTIONAL
1100 UINTN PreviousLength
;
1106 if (List
== NULL
|| CallBack
== NULL
) {
1107 return EFI_INVALID_PARAMETER
;
1112 PreviousLength
= Length
;
1113 Entry
= GetFirstNode (List
);
1114 while (!IsNull (List
, Entry
)) {
1115 Status
= CallBack (Entry
, Context
);
1116 if (EFI_ERROR (Status
)) {
1120 // Walk through the list to see whether the Entry has been removed or not.
1121 // If the Entry still exists, just try to destroy the next one.
1122 // If not, go back to the start point to iterate the list again.
1124 for (Ptr
= List
->ForwardLink
; Ptr
!= List
; Ptr
= Ptr
->ForwardLink
) {
1130 Entry
= GetNextNode (List
, Entry
);
1132 Entry
= GetFirstNode (List
);
1135 for (Length
= 0, Ptr
= List
->ForwardLink
; Ptr
!= List
; Length
++, Ptr
= Ptr
->ForwardLink
);
1136 } while (Length
!= PreviousLength
);
1138 if (ListLength
!= NULL
) {
1139 *ListLength
= Length
;
1145 This function checks the input Handle to see if it's one of these handles in ChildHandleBuffer.
1147 @param[in] Handle Handle to be checked.
1148 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer.
1149 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
1150 if NumberOfChildren is 0.
1152 @retval TRUE Found the input Handle in ChildHandleBuffer.
1153 @retval FALSE Can't find the input Handle in ChildHandleBuffer.
1158 NetIsInHandleBuffer (
1159 IN EFI_HANDLE Handle
,
1160 IN UINTN NumberOfChildren
,
1161 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
1166 if (NumberOfChildren
== 0 || ChildHandleBuffer
== NULL
) {
1170 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
1171 if (Handle
== ChildHandleBuffer
[Index
]) {
1181 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
1183 Initialize the forward and backward links of two head nodes donated by Map->Used
1184 and Map->Recycled of two doubly linked lists.
1185 Initializes the count of the <Key, Value> pairs in the netmap to zero.
1187 If Map is NULL, then ASSERT().
1188 If the address of Map->Used is NULL, then ASSERT().
1189 If the address of Map->Recycled is NULl, then ASSERT().
1191 @param[in, out] Map The netmap to initialize.
1200 ASSERT (Map
!= NULL
);
1202 InitializeListHead (&Map
->Used
);
1203 InitializeListHead (&Map
->Recycled
);
1209 To clean up the netmap, that is, release allocated memories.
1211 Removes all nodes of the Used doubly linked list and free memory of all related netmap items.
1212 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.
1213 The number of the <Key, Value> pairs in the netmap is set to be zero.
1215 If Map is NULL, then ASSERT().
1217 @param[in, out] Map The netmap to clean up.
1230 ASSERT (Map
!= NULL
);
1232 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Map
->Used
) {
1233 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1235 RemoveEntryList (&Item
->Link
);
1238 gBS
->FreePool (Item
);
1241 ASSERT ((Map
->Count
== 0) && IsListEmpty (&Map
->Used
));
1243 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Map
->Recycled
) {
1244 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1246 RemoveEntryList (&Item
->Link
);
1247 gBS
->FreePool (Item
);
1250 ASSERT (IsListEmpty (&Map
->Recycled
));
1255 Test whether the netmap is empty and return true if it is.
1257 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.
1259 If Map is NULL, then ASSERT().
1262 @param[in] Map The net map to test.
1264 @return TRUE if the netmap is empty, otherwise FALSE.
1273 ASSERT (Map
!= NULL
);
1274 return (BOOLEAN
) (Map
->Count
== 0);
1279 Return the number of the <Key, Value> pairs in the netmap.
1281 @param[in] Map The netmap to get the entry number.
1283 @return The entry number in the netmap.
1297 Return one allocated item.
1299 If the Recycled doubly linked list of the netmap is empty, it will try to allocate
1300 a batch of items if there are enough resources and add corresponding nodes to the begining
1301 of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove
1302 the fist node entry of the Recycled doubly linked list and return the corresponding item.
1304 If Map is NULL, then ASSERT().
1306 @param[in, out] Map The netmap to allocate item for.
1308 @return The allocated item. If NULL, the
1309 allocation failed due to resource limit.
1321 ASSERT (Map
!= NULL
);
1323 Head
= &Map
->Recycled
;
1325 if (IsListEmpty (Head
)) {
1326 for (Index
= 0; Index
< NET_MAP_INCREAMENT
; Index
++) {
1327 Item
= AllocatePool (sizeof (NET_MAP_ITEM
));
1337 InsertHeadList (Head
, &Item
->Link
);
1341 Item
= NET_LIST_HEAD (Head
, NET_MAP_ITEM
, Link
);
1342 NetListRemoveHead (Head
);
1349 Allocate an item to save the <Key, Value> pair to the head of the netmap.
1351 Allocate an item to save the <Key, Value> pair and add corresponding node entry
1352 to the beginning of the Used doubly linked list. The number of the <Key, Value>
1353 pairs in the netmap increase by 1.
1355 If Map is NULL, then ASSERT().
1357 @param[in, out] Map The netmap to insert into.
1358 @param[in] Key The user's key.
1359 @param[in] Value The user's value for the key.
1361 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
1362 @retval EFI_SUCCESS The item is inserted to the head.
1368 IN OUT NET_MAP
*Map
,
1370 IN VOID
*Value OPTIONAL
1375 ASSERT (Map
!= NULL
);
1377 Item
= NetMapAllocItem (Map
);
1380 return EFI_OUT_OF_RESOURCES
;
1384 Item
->Value
= Value
;
1385 InsertHeadList (&Map
->Used
, &Item
->Link
);
1393 Allocate an item to save the <Key, Value> pair to the tail of the netmap.
1395 Allocate an item to save the <Key, Value> pair and add corresponding node entry
1396 to the tail of the Used doubly linked list. The number of the <Key, Value>
1397 pairs in the netmap increase by 1.
1399 If Map is NULL, then ASSERT().
1401 @param[in, out] Map The netmap to insert into.
1402 @param[in] Key The user's key.
1403 @param[in] Value The user's value for the key.
1405 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
1406 @retval EFI_SUCCESS The item is inserted to the tail.
1412 IN OUT NET_MAP
*Map
,
1414 IN VOID
*Value OPTIONAL
1419 ASSERT (Map
!= NULL
);
1421 Item
= NetMapAllocItem (Map
);
1424 return EFI_OUT_OF_RESOURCES
;
1428 Item
->Value
= Value
;
1429 InsertTailList (&Map
->Used
, &Item
->Link
);
1438 Check whether the item is in the Map and return TRUE if it is.
1440 @param[in] Map The netmap to search within.
1441 @param[in] Item The item to search.
1443 @return TRUE if the item is in the netmap, otherwise FALSE.
1449 IN NET_MAP_ITEM
*Item
1452 LIST_ENTRY
*ListEntry
;
1454 NET_LIST_FOR_EACH (ListEntry
, &Map
->Used
) {
1455 if (ListEntry
== &Item
->Link
) {
1465 Find the key in the netmap and returns the point to the item contains the Key.
1467 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every
1468 item with the key to search. It returns the point to the item contains the Key if found.
1470 If Map is NULL, then ASSERT().
1472 @param[in] Map The netmap to search within.
1473 @param[in] Key The key to search.
1475 @return The point to the item contains the Key, or NULL if Key isn't in the map.
1488 ASSERT (Map
!= NULL
);
1490 NET_LIST_FOR_EACH (Entry
, &Map
->Used
) {
1491 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1493 if (Item
->Key
== Key
) {
1503 Remove the node entry of the item from the netmap and return the key of the removed item.
1505 Remove the node entry of the item from the Used doubly linked list of the netmap.
1506 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1507 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,
1508 Value will point to the value of the item. It returns the key of the removed item.
1510 If Map is NULL, then ASSERT().
1511 If Item is NULL, then ASSERT().
1512 if item in not in the netmap, then ASSERT().
1514 @param[in, out] Map The netmap to remove the item from.
1515 @param[in, out] Item The item to remove.
1516 @param[out] Value The variable to receive the value if not NULL.
1518 @return The key of the removed item.
1524 IN OUT NET_MAP
*Map
,
1525 IN OUT NET_MAP_ITEM
*Item
,
1526 OUT VOID
**Value OPTIONAL
1529 ASSERT ((Map
!= NULL
) && (Item
!= NULL
));
1530 ASSERT (NetItemInMap (Map
, Item
));
1532 RemoveEntryList (&Item
->Link
);
1534 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
1536 if (Value
!= NULL
) {
1537 *Value
= Item
->Value
;
1545 Remove the first node entry on the netmap and return the key of the removed item.
1547 Remove the first node entry from the Used doubly linked list of the netmap.
1548 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1549 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
1550 parameter Value will point to the value of the item. It returns the key of the removed item.
1552 If Map is NULL, then ASSERT().
1553 If the Used doubly linked list is empty, then ASSERT().
1555 @param[in, out] Map The netmap to remove the head from.
1556 @param[out] Value The variable to receive the value if not NULL.
1558 @return The key of the item removed.
1564 IN OUT NET_MAP
*Map
,
1565 OUT VOID
**Value OPTIONAL
1571 // Often, it indicates a programming error to remove
1572 // the first entry in an empty list
1574 ASSERT (Map
&& !IsListEmpty (&Map
->Used
));
1576 Item
= NET_LIST_HEAD (&Map
->Used
, NET_MAP_ITEM
, Link
);
1577 RemoveEntryList (&Item
->Link
);
1579 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
1581 if (Value
!= NULL
) {
1582 *Value
= Item
->Value
;
1590 Remove the last node entry on the netmap and return the key of the removed item.
1592 Remove the last node entry from the Used doubly linked list of the netmap.
1593 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1594 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
1595 parameter Value will point to the value of the item. It returns the key of the removed item.
1597 If Map is NULL, then ASSERT().
1598 If the Used doubly linked list is empty, then ASSERT().
1600 @param[in, out] Map The netmap to remove the tail from.
1601 @param[out] Value The variable to receive the value if not NULL.
1603 @return The key of the item removed.
1609 IN OUT NET_MAP
*Map
,
1610 OUT VOID
**Value OPTIONAL
1616 // Often, it indicates a programming error to remove
1617 // the last entry in an empty list
1619 ASSERT (Map
&& !IsListEmpty (&Map
->Used
));
1621 Item
= NET_LIST_TAIL (&Map
->Used
, NET_MAP_ITEM
, Link
);
1622 RemoveEntryList (&Item
->Link
);
1624 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
1626 if (Value
!= NULL
) {
1627 *Value
= Item
->Value
;
1635 Iterate through the netmap and call CallBack for each item.
1637 It will continue the traverse if CallBack returns EFI_SUCCESS, otherwise, break
1638 from the loop. It returns the CallBack's last return value. This function is
1639 delete safe for the current item.
1641 If Map is NULL, then ASSERT().
1642 If CallBack is NULL, then ASSERT().
1644 @param[in] Map The Map to iterate through.
1645 @param[in] CallBack The callback function to call for each item.
1646 @param[in] Arg The opaque parameter to the callback.
1648 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item
1650 @retval Others It returns the CallBack's last return value.
1657 IN NET_MAP_CALLBACK CallBack
,
1658 IN VOID
*Arg OPTIONAL
1668 ASSERT ((Map
!= NULL
) && (CallBack
!= NULL
));
1672 if (IsListEmpty (Head
)) {
1676 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Head
) {
1677 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1678 Result
= CallBack (Map
, Item
, Arg
);
1680 if (EFI_ERROR (Result
)) {
1690 This is the default unload handle for all the network drivers.
1692 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
1693 Uninstall all the protocols installed in the driver entry point.
1695 @param[in] ImageHandle The drivers' driver image.
1697 @retval EFI_SUCCESS The image is unloaded.
1698 @retval Others Failed to unload the image.
1703 NetLibDefaultUnload (
1704 IN EFI_HANDLE ImageHandle
1708 EFI_HANDLE
*DeviceHandleBuffer
;
1709 UINTN DeviceHandleCount
;
1712 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
1713 EFI_COMPONENT_NAME_PROTOCOL
*ComponentName
;
1714 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
1717 // Get the list of all the handles in the handle database.
1718 // If there is an error getting the list, then the unload
1721 Status
= gBS
->LocateHandleBuffer (
1729 if (EFI_ERROR (Status
)) {
1733 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1734 Status
= gBS
->HandleProtocol (
1735 DeviceHandleBuffer
[Index
],
1736 &gEfiDriverBindingProtocolGuid
,
1737 (VOID
**) &DriverBinding
1739 if (EFI_ERROR (Status
)) {
1743 if (DriverBinding
->ImageHandle
!= ImageHandle
) {
1748 // Disconnect the driver specified by ImageHandle from all
1749 // the devices in the handle database.
1751 for (Index2
= 0; Index2
< DeviceHandleCount
; Index2
++) {
1752 Status
= gBS
->DisconnectController (
1753 DeviceHandleBuffer
[Index2
],
1754 DriverBinding
->DriverBindingHandle
,
1760 // Uninstall all the protocols installed in the driver entry point
1762 gBS
->UninstallProtocolInterface (
1763 DriverBinding
->DriverBindingHandle
,
1764 &gEfiDriverBindingProtocolGuid
,
1768 Status
= gBS
->HandleProtocol (
1769 DeviceHandleBuffer
[Index
],
1770 &gEfiComponentNameProtocolGuid
,
1771 (VOID
**) &ComponentName
1773 if (!EFI_ERROR (Status
)) {
1774 gBS
->UninstallProtocolInterface (
1775 DriverBinding
->DriverBindingHandle
,
1776 &gEfiComponentNameProtocolGuid
,
1781 Status
= gBS
->HandleProtocol (
1782 DeviceHandleBuffer
[Index
],
1783 &gEfiComponentName2ProtocolGuid
,
1784 (VOID
**) &ComponentName2
1786 if (!EFI_ERROR (Status
)) {
1787 gBS
->UninstallProtocolInterface (
1788 DriverBinding
->DriverBindingHandle
,
1789 &gEfiComponentName2ProtocolGuid
,
1796 // Free the buffer containing the list of handles from the handle database
1798 if (DeviceHandleBuffer
!= NULL
) {
1799 gBS
->FreePool (DeviceHandleBuffer
);
1808 Create a child of the service that is identified by ServiceBindingGuid.
1810 Get the ServiceBinding Protocol first, then use it to create a child.
1812 If ServiceBindingGuid is NULL, then ASSERT().
1813 If ChildHandle is NULL, then ASSERT().
1815 @param[in] Controller The controller which has the service installed.
1816 @param[in] Image The image handle used to open service.
1817 @param[in] ServiceBindingGuid The service's Guid.
1818 @param[in, out] ChildHandle The handle to receive the create child.
1820 @retval EFI_SUCCESS The child is successfully created.
1821 @retval Others Failed to create the child.
1826 NetLibCreateServiceChild (
1827 IN EFI_HANDLE Controller
,
1828 IN EFI_HANDLE Image
,
1829 IN EFI_GUID
*ServiceBindingGuid
,
1830 IN OUT EFI_HANDLE
*ChildHandle
1834 EFI_SERVICE_BINDING_PROTOCOL
*Service
;
1837 ASSERT ((ServiceBindingGuid
!= NULL
) && (ChildHandle
!= NULL
));
1840 // Get the ServiceBinding Protocol
1842 Status
= gBS
->OpenProtocol (
1848 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1851 if (EFI_ERROR (Status
)) {
1858 Status
= Service
->CreateChild (Service
, ChildHandle
);
1864 Destroy a child of the service that is identified by ServiceBindingGuid.
1866 Get the ServiceBinding Protocol first, then use it to destroy a child.
1868 If ServiceBindingGuid is NULL, then ASSERT().
1870 @param[in] Controller The controller which has the service installed.
1871 @param[in] Image The image handle used to open service.
1872 @param[in] ServiceBindingGuid The service's Guid.
1873 @param[in] ChildHandle The child to destroy.
1875 @retval EFI_SUCCESS The child is successfully destroyed.
1876 @retval Others Failed to destroy the child.
1881 NetLibDestroyServiceChild (
1882 IN EFI_HANDLE Controller
,
1883 IN EFI_HANDLE Image
,
1884 IN EFI_GUID
*ServiceBindingGuid
,
1885 IN EFI_HANDLE ChildHandle
1889 EFI_SERVICE_BINDING_PROTOCOL
*Service
;
1891 ASSERT (ServiceBindingGuid
!= NULL
);
1894 // Get the ServiceBinding Protocol
1896 Status
= gBS
->OpenProtocol (
1902 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1905 if (EFI_ERROR (Status
)) {
1910 // destroy the child
1912 Status
= Service
->DestroyChild (Service
, ChildHandle
);
1917 Get handle with Simple Network Protocol installed on it.
1919 There should be MNP Service Binding Protocol installed on the input ServiceHandle.
1920 If Simple Network Protocol is already installed on the ServiceHandle, the
1921 ServiceHandle will be returned. If SNP is not installed on the ServiceHandle,
1922 try to find its parent handle with SNP installed.
1924 @param[in] ServiceHandle The handle where network service binding protocols are
1926 @param[out] Snp The pointer to store the address of the SNP instance.
1927 This is an optional parameter that may be NULL.
1929 @return The SNP handle, or NULL if not found.
1934 NetLibGetSnpHandle (
1935 IN EFI_HANDLE ServiceHandle
,
1936 OUT EFI_SIMPLE_NETWORK_PROTOCOL
**Snp OPTIONAL
1940 EFI_SIMPLE_NETWORK_PROTOCOL
*SnpInstance
;
1941 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1942 EFI_HANDLE SnpHandle
;
1945 // Try to open SNP from ServiceHandle
1948 Status
= gBS
->HandleProtocol (ServiceHandle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &SnpInstance
);
1949 if (!EFI_ERROR (Status
)) {
1953 return ServiceHandle
;
1957 // Failed to open SNP, try to get SNP handle by LocateDevicePath()
1959 DevicePath
= DevicePathFromHandle (ServiceHandle
);
1960 if (DevicePath
== NULL
) {
1965 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &DevicePath
, &SnpHandle
);
1966 if (EFI_ERROR (Status
)) {
1968 // Failed to find SNP handle
1973 Status
= gBS
->HandleProtocol (SnpHandle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &SnpInstance
);
1974 if (!EFI_ERROR (Status
)) {
1985 Retrieve VLAN ID of a VLAN device handle.
1987 Search VLAN device path node in Device Path of specified ServiceHandle and
1988 return its VLAN ID. If no VLAN device path node found, then this ServiceHandle
1989 is not a VLAN device handle, and 0 will be returned.
1991 @param[in] ServiceHandle The handle where network service binding protocols are
1994 @return VLAN ID of the device handle, or 0 if not a VLAN device.
2000 IN EFI_HANDLE ServiceHandle
2003 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2004 EFI_DEVICE_PATH_PROTOCOL
*Node
;
2006 DevicePath
= DevicePathFromHandle (ServiceHandle
);
2007 if (DevicePath
== NULL
) {
2012 while (!IsDevicePathEnd (Node
)) {
2013 if (Node
->Type
== MESSAGING_DEVICE_PATH
&& Node
->SubType
== MSG_VLAN_DP
) {
2014 return ((VLAN_DEVICE_PATH
*) Node
)->VlanId
;
2016 Node
= NextDevicePathNode (Node
);
2023 Find VLAN device handle with specified VLAN ID.
2025 The VLAN child device handle is created by VLAN Config Protocol on ControllerHandle.
2026 This function will append VLAN device path node to the parent device path,
2027 and then use LocateDevicePath() to find the correct VLAN device handle.
2029 @param[in] ControllerHandle The handle where network service binding protocols are
2031 @param[in] VlanId The configured VLAN ID for the VLAN device.
2033 @return The VLAN device handle, or NULL if not found.
2038 NetLibGetVlanHandle (
2039 IN EFI_HANDLE ControllerHandle
,
2043 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
2044 EFI_DEVICE_PATH_PROTOCOL
*VlanDevicePath
;
2045 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2046 VLAN_DEVICE_PATH VlanNode
;
2049 ParentDevicePath
= DevicePathFromHandle (ControllerHandle
);
2050 if (ParentDevicePath
== NULL
) {
2055 // Construct VLAN device path
2057 CopyMem (&VlanNode
, &mNetVlanDevicePathTemplate
, sizeof (VLAN_DEVICE_PATH
));
2058 VlanNode
.VlanId
= VlanId
;
2059 VlanDevicePath
= AppendDevicePathNode (
2061 (EFI_DEVICE_PATH_PROTOCOL
*) &VlanNode
2063 if (VlanDevicePath
== NULL
) {
2068 // Find VLAN device handle
2071 DevicePath
= VlanDevicePath
;
2072 gBS
->LocateDevicePath (
2073 &gEfiDevicePathProtocolGuid
,
2077 if (!IsDevicePathEnd (DevicePath
)) {
2079 // Device path is not exactly match
2084 FreePool (VlanDevicePath
);
2089 Get MAC address associated with the network service handle.
2091 There should be MNP Service Binding Protocol installed on the input ServiceHandle.
2092 If SNP is installed on the ServiceHandle or its parent handle, MAC address will
2093 be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP.
2095 @param[in] ServiceHandle The handle where network service binding protocols are
2097 @param[out] MacAddress The pointer to store the returned MAC address.
2098 @param[out] AddressSize The length of returned MAC address.
2100 @retval EFI_SUCCESS MAC address is returned successfully.
2101 @retval Others Failed to get SNP mode data.
2106 NetLibGetMacAddress (
2107 IN EFI_HANDLE ServiceHandle
,
2108 OUT EFI_MAC_ADDRESS
*MacAddress
,
2109 OUT UINTN
*AddressSize
2113 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
2114 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
2115 EFI_SIMPLE_NETWORK_MODE SnpModeData
;
2116 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
2117 EFI_SERVICE_BINDING_PROTOCOL
*MnpSb
;
2118 EFI_HANDLE
*SnpHandle
;
2119 EFI_HANDLE MnpChildHandle
;
2121 ASSERT (MacAddress
!= NULL
);
2122 ASSERT (AddressSize
!= NULL
);
2125 // Try to get SNP handle
2128 SnpHandle
= NetLibGetSnpHandle (ServiceHandle
, &Snp
);
2129 if (SnpHandle
!= NULL
) {
2131 // SNP found, use it directly
2133 SnpMode
= Snp
->Mode
;
2136 // Failed to get SNP handle, try to get MAC address from MNP
2138 MnpChildHandle
= NULL
;
2139 Status
= gBS
->HandleProtocol (
2141 &gEfiManagedNetworkServiceBindingProtocolGuid
,
2144 if (EFI_ERROR (Status
)) {
2149 // Create a MNP child
2151 Status
= MnpSb
->CreateChild (MnpSb
, &MnpChildHandle
);
2152 if (EFI_ERROR (Status
)) {
2157 // Open MNP protocol
2159 Status
= gBS
->HandleProtocol (
2161 &gEfiManagedNetworkProtocolGuid
,
2164 if (EFI_ERROR (Status
)) {
2165 MnpSb
->DestroyChild (MnpSb
, MnpChildHandle
);
2170 // Try to get SNP mode from MNP
2172 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpModeData
);
2173 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
2174 MnpSb
->DestroyChild (MnpSb
, MnpChildHandle
);
2177 SnpMode
= &SnpModeData
;
2180 // Destroy the MNP child
2182 MnpSb
->DestroyChild (MnpSb
, MnpChildHandle
);
2185 *AddressSize
= SnpMode
->HwAddressSize
;
2186 CopyMem (MacAddress
->Addr
, SnpMode
->CurrentAddress
.Addr
, SnpMode
->HwAddressSize
);
2192 Convert MAC address of the NIC associated with specified Service Binding Handle
2193 to a unicode string. Callers are responsible for freeing the string storage.
2195 Locate simple network protocol associated with the Service Binding Handle and
2196 get the mac address from SNP. Then convert the mac address into a unicode
2197 string. It takes 2 unicode characters to represent a 1 byte binary buffer.
2198 Plus one unicode character for the null-terminator.
2200 @param[in] ServiceHandle The handle where network service binding protocol is
2202 @param[in] ImageHandle The image handle used to act as the agent handle to
2203 get the simple network protocol. This parameter is
2204 optional and may be NULL.
2205 @param[out] MacString The pointer to store the address of the string
2206 representation of the mac address.
2208 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.
2209 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
2210 @retval Others Failed to open the simple network protocol.
2215 NetLibGetMacString (
2216 IN EFI_HANDLE ServiceHandle
,
2217 IN EFI_HANDLE ImageHandle
, OPTIONAL
2218 OUT CHAR16
**MacString
2222 EFI_MAC_ADDRESS MacAddress
;
2224 UINTN HwAddressSize
;
2230 ASSERT (MacString
!= NULL
);
2233 // Get MAC address of the network device
2235 Status
= NetLibGetMacAddress (ServiceHandle
, &MacAddress
, &HwAddressSize
);
2236 if (EFI_ERROR (Status
)) {
2241 // It takes 2 unicode characters to represent a 1 byte binary buffer.
2242 // If VLAN is configured, it will need extra 5 characters like "\0005".
2243 // Plus one unicode character for the null-terminator.
2245 BufferSize
= (2 * HwAddressSize
+ 5 + 1) * sizeof (CHAR16
);
2246 String
= AllocateZeroPool (BufferSize
);
2247 if (String
== NULL
) {
2248 return EFI_OUT_OF_RESOURCES
;
2250 *MacString
= String
;
2253 // Convert the MAC address into a unicode string.
2255 HwAddress
= &MacAddress
.Addr
[0];
2256 for (Index
= 0; Index
< HwAddressSize
; Index
++) {
2257 UnicodeValueToStringS (
2259 BufferSize
- ((UINTN
)String
- (UINTN
)*MacString
),
2260 PREFIX_ZERO
| RADIX_HEX
,
2264 String
+= StrnLenS (String
, (BufferSize
- ((UINTN
)String
- (UINTN
)*MacString
)) / sizeof (CHAR16
));
2268 // Append VLAN ID if any
2270 VlanId
= NetLibGetVlanId (ServiceHandle
);
2273 UnicodeValueToStringS (
2275 BufferSize
- ((UINTN
)String
- (UINTN
)*MacString
),
2276 PREFIX_ZERO
| RADIX_HEX
,
2280 String
+= StrnLenS (String
, (BufferSize
- ((UINTN
)String
- (UINTN
)*MacString
)) / sizeof (CHAR16
));
2284 // Null terminate the Unicode string
2292 Detect media status for specified network device.
2294 The underlying UNDI driver may or may not support reporting media status from
2295 GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine
2296 will try to invoke Snp->GetStatus() to get the media status: if media already
2297 present, it return directly; if media not present, it will stop SNP and then
2298 restart SNP to get the latest media status, this give chance to get the correct
2299 media status for old UNDI driver which doesn't support reporting media status
2300 from GET_STATUS command.
2301 Note: there will be two limitations for current algorithm:
2302 1) for UNDI with this capability, in case of cable is not attached, there will
2303 be an redundant Stop/Start() process;
2304 2) for UNDI without this capability, in case that network cable is attached when
2305 Snp->Initialize() is invoked while network cable is unattached later,
2306 NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer
2307 apps to wait for timeout time.
2309 @param[in] ServiceHandle The handle where network service binding protocols are
2311 @param[out] MediaPresent The pointer to store the media status.
2313 @retval EFI_SUCCESS Media detection success.
2314 @retval EFI_INVALID_PARAMETER ServiceHandle is not valid network device handle.
2315 @retval EFI_UNSUPPORTED Network device does not support media detection.
2316 @retval EFI_DEVICE_ERROR SNP is in unknown state.
2322 IN EFI_HANDLE ServiceHandle
,
2323 OUT BOOLEAN
*MediaPresent
2327 EFI_HANDLE SnpHandle
;
2328 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
2329 UINT32 InterruptStatus
;
2331 EFI_MAC_ADDRESS
*MCastFilter
;
2332 UINT32 MCastFilterCount
;
2333 UINT32 EnableFilterBits
;
2334 UINT32 DisableFilterBits
;
2335 BOOLEAN ResetMCastFilters
;
2337 ASSERT (MediaPresent
!= NULL
);
2343 SnpHandle
= NetLibGetSnpHandle (ServiceHandle
, &Snp
);
2344 if (SnpHandle
== NULL
) {
2345 return EFI_INVALID_PARAMETER
;
2349 // Check whether SNP support media detection
2351 if (!Snp
->Mode
->MediaPresentSupported
) {
2352 return EFI_UNSUPPORTED
;
2356 // Invoke Snp->GetStatus() to refresh MediaPresent field in SNP mode data
2358 Status
= Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
2359 if (EFI_ERROR (Status
)) {
2363 if (Snp
->Mode
->MediaPresent
) {
2365 // Media is present, return directly
2367 *MediaPresent
= TRUE
;
2372 // Till now, GetStatus() report no media; while, in case UNDI not support
2373 // reporting media status from GetStatus(), this media status may be incorrect.
2374 // So, we will stop SNP and then restart it to get the correct media status.
2376 OldState
= Snp
->Mode
->State
;
2377 if (OldState
>= EfiSimpleNetworkMaxState
) {
2378 return EFI_DEVICE_ERROR
;
2383 if (OldState
== EfiSimpleNetworkInitialized
) {
2385 // SNP is already in use, need Shutdown/Stop and then Start/Initialize
2389 // Backup current SNP receive filter settings
2391 EnableFilterBits
= Snp
->Mode
->ReceiveFilterSetting
;
2392 DisableFilterBits
= Snp
->Mode
->ReceiveFilterMask
^ EnableFilterBits
;
2394 ResetMCastFilters
= TRUE
;
2395 MCastFilterCount
= Snp
->Mode
->MCastFilterCount
;
2396 if (MCastFilterCount
!= 0) {
2397 MCastFilter
= AllocateCopyPool (
2398 MCastFilterCount
* sizeof (EFI_MAC_ADDRESS
),
2399 Snp
->Mode
->MCastFilter
2401 ASSERT (MCastFilter
!= NULL
);
2403 ResetMCastFilters
= FALSE
;
2407 // Shutdown/Stop the simple network
2409 Status
= Snp
->Shutdown (Snp
);
2410 if (!EFI_ERROR (Status
)) {
2411 Status
= Snp
->Stop (Snp
);
2413 if (EFI_ERROR (Status
)) {
2418 // Start/Initialize the simple network
2420 Status
= Snp
->Start (Snp
);
2421 if (!EFI_ERROR (Status
)) {
2422 Status
= Snp
->Initialize (Snp
, 0, 0);
2424 if (EFI_ERROR (Status
)) {
2429 // Here we get the correct media status
2431 *MediaPresent
= Snp
->Mode
->MediaPresent
;
2434 // Restore SNP receive filter settings
2436 Status
= Snp
->ReceiveFilters (
2445 if (MCastFilter
!= NULL
) {
2446 FreePool (MCastFilter
);
2453 // SNP is not in use, it's in state of EfiSimpleNetworkStopped or EfiSimpleNetworkStarted
2455 if (OldState
== EfiSimpleNetworkStopped
) {
2457 // SNP not start yet, start it
2459 Status
= Snp
->Start (Snp
);
2460 if (EFI_ERROR (Status
)) {
2466 // Initialize the simple network
2468 Status
= Snp
->Initialize (Snp
, 0, 0);
2469 if (EFI_ERROR (Status
)) {
2470 Status
= EFI_DEVICE_ERROR
;
2475 // Here we get the correct media status
2477 *MediaPresent
= Snp
->Mode
->MediaPresent
;
2480 // Shut down the simple network
2482 Snp
->Shutdown (Snp
);
2485 if (OldState
== EfiSimpleNetworkStopped
) {
2487 // Original SNP sate is Stopped, restore to original state
2492 if (MCastFilter
!= NULL
) {
2493 FreePool (MCastFilter
);
2500 Check the default address used by the IPv4 driver is static or dynamic (acquired
2503 If the controller handle does not have the EFI_IP4_CONFIG2_PROTOCOL installed, the
2504 default address is static. If failed to get the policy from Ip4 Config2 Protocol,
2505 the default address is static. Otherwise, get the result from Ip4 Config2 Protocol.
2507 @param[in] Controller The controller handle which has the EFI_IP4_CONFIG2_PROTOCOL
2508 relative with the default address to judge.
2510 @retval TRUE If the default address is static.
2511 @retval FALSE If the default address is acquired from DHCP.
2515 NetLibDefaultAddressIsStatic (
2516 IN EFI_HANDLE Controller
2520 EFI_IP4_CONFIG2_PROTOCOL
*Ip4Config2
;
2522 EFI_IP4_CONFIG2_POLICY Policy
;
2527 DataSize
= sizeof (EFI_IP4_CONFIG2_POLICY
);
2532 // Get Ip4Config2 policy.
2534 Status
= gBS
->HandleProtocol (Controller
, &gEfiIp4Config2ProtocolGuid
, (VOID
**) &Ip4Config2
);
2535 if (EFI_ERROR (Status
)) {
2539 Status
= Ip4Config2
->GetData (Ip4Config2
, Ip4Config2DataTypePolicy
, &DataSize
, &Policy
);
2540 if (EFI_ERROR (Status
)) {
2544 IsStatic
= (BOOLEAN
) (Policy
== Ip4Config2PolicyStatic
);
2552 Create an IPv4 device path node.
2554 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.
2555 The header subtype of IPv4 device path node is MSG_IPv4_DP.
2556 Get other info from parameters to make up the whole IPv4 device path node.
2558 @param[in, out] Node Pointer to the IPv4 device path node.
2559 @param[in] Controller The controller handle.
2560 @param[in] LocalIp The local IPv4 address.
2561 @param[in] LocalPort The local port.
2562 @param[in] RemoteIp The remote IPv4 address.
2563 @param[in] RemotePort The remote port.
2564 @param[in] Protocol The protocol type in the IP header.
2565 @param[in] UseDefaultAddress Whether this instance is using default address or not.
2570 NetLibCreateIPv4DPathNode (
2571 IN OUT IPv4_DEVICE_PATH
*Node
,
2572 IN EFI_HANDLE Controller
,
2573 IN IP4_ADDR LocalIp
,
2574 IN UINT16 LocalPort
,
2575 IN IP4_ADDR RemoteIp
,
2576 IN UINT16 RemotePort
,
2578 IN BOOLEAN UseDefaultAddress
2581 Node
->Header
.Type
= MESSAGING_DEVICE_PATH
;
2582 Node
->Header
.SubType
= MSG_IPv4_DP
;
2583 SetDevicePathNodeLength (&Node
->Header
, sizeof (IPv4_DEVICE_PATH
));
2585 CopyMem (&Node
->LocalIpAddress
, &LocalIp
, sizeof (EFI_IPv4_ADDRESS
));
2586 CopyMem (&Node
->RemoteIpAddress
, &RemoteIp
, sizeof (EFI_IPv4_ADDRESS
));
2588 Node
->LocalPort
= LocalPort
;
2589 Node
->RemotePort
= RemotePort
;
2591 Node
->Protocol
= Protocol
;
2593 if (!UseDefaultAddress
) {
2594 Node
->StaticIpAddress
= TRUE
;
2596 Node
->StaticIpAddress
= NetLibDefaultAddressIsStatic (Controller
);
2600 // Set the Gateway IP address to default value 0:0:0:0.
2601 // Set the Subnet mask to default value 255:255:255:0.
2603 ZeroMem (&Node
->GatewayIpAddress
, sizeof (EFI_IPv4_ADDRESS
));
2604 SetMem (&Node
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
), 0xff);
2605 Node
->SubnetMask
.Addr
[3] = 0;
2609 Create an IPv6 device path node.
2611 The header type of IPv6 device path node is MESSAGING_DEVICE_PATH.
2612 The header subtype of IPv6 device path node is MSG_IPv6_DP.
2613 Get other info from parameters to make up the whole IPv6 device path node.
2615 @param[in, out] Node Pointer to the IPv6 device path node.
2616 @param[in] Controller The controller handle.
2617 @param[in] LocalIp The local IPv6 address.
2618 @param[in] LocalPort The local port.
2619 @param[in] RemoteIp The remote IPv6 address.
2620 @param[in] RemotePort The remote port.
2621 @param[in] Protocol The protocol type in the IP header.
2626 NetLibCreateIPv6DPathNode (
2627 IN OUT IPv6_DEVICE_PATH
*Node
,
2628 IN EFI_HANDLE Controller
,
2629 IN EFI_IPv6_ADDRESS
*LocalIp
,
2630 IN UINT16 LocalPort
,
2631 IN EFI_IPv6_ADDRESS
*RemoteIp
,
2632 IN UINT16 RemotePort
,
2636 Node
->Header
.Type
= MESSAGING_DEVICE_PATH
;
2637 Node
->Header
.SubType
= MSG_IPv6_DP
;
2638 SetDevicePathNodeLength (&Node
->Header
, sizeof (IPv6_DEVICE_PATH
));
2640 CopyMem (&Node
->LocalIpAddress
, LocalIp
, sizeof (EFI_IPv6_ADDRESS
));
2641 CopyMem (&Node
->RemoteIpAddress
, RemoteIp
, sizeof (EFI_IPv6_ADDRESS
));
2643 Node
->LocalPort
= LocalPort
;
2644 Node
->RemotePort
= RemotePort
;
2646 Node
->Protocol
= Protocol
;
2649 // Set default value to IPAddressOrigin, PrefixLength.
2650 // Set the Gateway IP address to unspecified address.
2652 Node
->IpAddressOrigin
= 0;
2653 Node
->PrefixLength
= IP6_PREFIX_LENGTH
;
2654 ZeroMem (&Node
->GatewayIpAddress
, sizeof (EFI_IPv6_ADDRESS
));
2658 Find the UNDI/SNP handle from controller and protocol GUID.
2660 For example, IP will open a MNP child to transmit/receive
2661 packets, when MNP is stopped, IP should also be stopped. IP
2662 needs to find its own private data which is related the IP's
2663 service binding instance that is install on UNDI/SNP handle.
2664 Now, the controller is either a MNP or ARP child handle. But
2665 IP opens these handle BY_DRIVER, use that info, we can get the
2668 @param[in] Controller Then protocol handle to check.
2669 @param[in] ProtocolGuid The protocol that is related with the handle.
2671 @return The UNDI/SNP handle or NULL for errors.
2676 NetLibGetNicHandle (
2677 IN EFI_HANDLE Controller
,
2678 IN EFI_GUID
*ProtocolGuid
2681 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenBuffer
;
2687 Status
= gBS
->OpenProtocolInformation (
2694 if (EFI_ERROR (Status
)) {
2700 for (Index
= 0; Index
< OpenCount
; Index
++) {
2701 if ((OpenBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) != 0) {
2702 Handle
= OpenBuffer
[Index
].ControllerHandle
;
2707 gBS
->FreePool (OpenBuffer
);
2712 Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS.
2714 @param[in] String The pointer to the Ascii string.
2715 @param[out] Ip4Address The pointer to the converted IPv4 address.
2717 @retval EFI_SUCCESS Convert to IPv4 address successfully.
2718 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.
2723 NetLibAsciiStrToIp4 (
2724 IN CONST CHAR8
*String
,
2725 OUT EFI_IPv4_ADDRESS
*Ip4Address
2728 RETURN_STATUS Status
;
2731 Status
= AsciiStrToIpv4Address (String
, &EndPointer
, Ip4Address
, NULL
);
2732 if (RETURN_ERROR (Status
) || (*EndPointer
!= '\0')) {
2733 return EFI_INVALID_PARAMETER
;
2741 Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of the
2742 string is defined in RFC 4291 - Text Representation of Addresses.
2744 @param[in] String The pointer to the Ascii string.
2745 @param[out] Ip6Address The pointer to the converted IPv6 address.
2747 @retval EFI_SUCCESS Convert to IPv6 address successfully.
2748 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
2753 NetLibAsciiStrToIp6 (
2754 IN CONST CHAR8
*String
,
2755 OUT EFI_IPv6_ADDRESS
*Ip6Address
2758 RETURN_STATUS Status
;
2761 Status
= AsciiStrToIpv6Address (String
, &EndPointer
, Ip6Address
, NULL
);
2762 if (RETURN_ERROR (Status
) || (*EndPointer
!= '\0')) {
2763 return EFI_INVALID_PARAMETER
;
2771 Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS.
2773 @param[in] String The pointer to the Ascii string.
2774 @param[out] Ip4Address The pointer to the converted IPv4 address.
2776 @retval EFI_SUCCESS Convert to IPv4 address successfully.
2777 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.
2783 IN CONST CHAR16
*String
,
2784 OUT EFI_IPv4_ADDRESS
*Ip4Address
2787 RETURN_STATUS Status
;
2790 Status
= StrToIpv4Address (String
, &EndPointer
, Ip4Address
, NULL
);
2791 if (RETURN_ERROR (Status
) || (*EndPointer
!= L
'\0')) {
2792 return EFI_INVALID_PARAMETER
;
2800 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of
2801 the string is defined in RFC 4291 - Text Representation of Addresses.
2803 @param[in] String The pointer to the Ascii string.
2804 @param[out] Ip6Address The pointer to the converted IPv6 address.
2806 @retval EFI_SUCCESS Convert to IPv6 address successfully.
2807 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
2813 IN CONST CHAR16
*String
,
2814 OUT EFI_IPv6_ADDRESS
*Ip6Address
2817 RETURN_STATUS Status
;
2820 Status
= StrToIpv6Address (String
, &EndPointer
, Ip6Address
, NULL
);
2821 if (RETURN_ERROR (Status
) || (*EndPointer
!= L
'\0')) {
2822 return EFI_INVALID_PARAMETER
;
2829 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length.
2830 The format of the string is defined in RFC 4291 - Text Representation of Addresses
2831 Prefixes: ipv6-address/prefix-length.
2833 @param[in] String The pointer to the Ascii string.
2834 @param[out] Ip6Address The pointer to the converted IPv6 address.
2835 @param[out] PrefixLength The pointer to the converted prefix length.
2837 @retval EFI_SUCCESS Convert to IPv6 address successfully.
2838 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
2843 NetLibStrToIp6andPrefix (
2844 IN CONST CHAR16
*String
,
2845 OUT EFI_IPv6_ADDRESS
*Ip6Address
,
2846 OUT UINT8
*PrefixLength
2849 RETURN_STATUS Status
;
2852 Status
= StrToIpv6Address (String
, &EndPointer
, Ip6Address
, PrefixLength
);
2853 if (RETURN_ERROR (Status
) || (*EndPointer
!= L
'\0')) {
2854 return EFI_INVALID_PARAMETER
;
2862 Convert one EFI_IPv6_ADDRESS to Null-terminated Unicode string.
2863 The text representation of address is defined in RFC 4291.
2865 @param[in] Ip6Address The pointer to the IPv6 address.
2866 @param[out] String The buffer to return the converted string.
2867 @param[in] StringSize The length in bytes of the input String.
2869 @retval EFI_SUCCESS Convert to string successfully.
2870 @retval EFI_INVALID_PARAMETER The input parameter is invalid.
2871 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small for the result. BufferSize has been
2872 updated with the size needed to complete the request.
2877 IN EFI_IPv6_ADDRESS
*Ip6Address
,
2884 UINTN LongestZerosStart
;
2885 UINTN LongestZerosLength
;
2886 UINTN CurrentZerosStart
;
2887 UINTN CurrentZerosLength
;
2888 CHAR16 Buffer
[sizeof"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
2891 if (Ip6Address
== NULL
|| String
== NULL
|| StringSize
== 0) {
2892 return EFI_INVALID_PARAMETER
;
2896 // Convert the UINT8 array to an UINT16 array for easy handling.
2898 ZeroMem (Ip6Addr
, sizeof (Ip6Addr
));
2899 for (Index
= 0; Index
< 16; Index
++) {
2900 Ip6Addr
[Index
/ 2] |= (Ip6Address
->Addr
[Index
] << ((1 - (Index
% 2)) << 3));
2904 // Find the longest zeros and mark it.
2906 CurrentZerosStart
= DEFAULT_ZERO_START
;
2907 CurrentZerosLength
= 0;
2908 LongestZerosStart
= DEFAULT_ZERO_START
;
2909 LongestZerosLength
= 0;
2910 for (Index
= 0; Index
< 8; Index
++) {
2911 if (Ip6Addr
[Index
] == 0) {
2912 if (CurrentZerosStart
== DEFAULT_ZERO_START
) {
2913 CurrentZerosStart
= Index
;
2914 CurrentZerosLength
= 1;
2916 CurrentZerosLength
++;
2919 if (CurrentZerosStart
!= DEFAULT_ZERO_START
) {
2920 if (CurrentZerosLength
> 2 && (LongestZerosStart
== (DEFAULT_ZERO_START
) || CurrentZerosLength
> LongestZerosLength
)) {
2921 LongestZerosStart
= CurrentZerosStart
;
2922 LongestZerosLength
= CurrentZerosLength
;
2924 CurrentZerosStart
= DEFAULT_ZERO_START
;
2925 CurrentZerosLength
= 0;
2930 if (CurrentZerosStart
!= DEFAULT_ZERO_START
&& CurrentZerosLength
> 2) {
2931 if (LongestZerosStart
== DEFAULT_ZERO_START
|| LongestZerosLength
< CurrentZerosLength
) {
2932 LongestZerosStart
= CurrentZerosStart
;
2933 LongestZerosLength
= CurrentZerosLength
;
2938 for (Index
= 0; Index
< 8; Index
++) {
2939 if (LongestZerosStart
!= DEFAULT_ZERO_START
&& Index
>= LongestZerosStart
&& Index
< LongestZerosStart
+ LongestZerosLength
) {
2940 if (Index
== LongestZerosStart
) {
2948 Ptr
+= UnicodeSPrint(Ptr
, 10, L
"%x", Ip6Addr
[Index
]);
2951 if (LongestZerosStart
!= DEFAULT_ZERO_START
&& LongestZerosStart
+ LongestZerosLength
== 8) {
2956 if ((UINTN
)Ptr
- (UINTN
)Buffer
> StringSize
) {
2957 return EFI_BUFFER_TOO_SMALL
;
2960 StrCpyS (String
, StringSize
/ sizeof (CHAR16
), Buffer
);
2966 This function obtains the system guid from the smbios table.
2968 @param[out] SystemGuid The pointer of the returned system guid.
2970 @retval EFI_SUCCESS Successfully obtained the system guid.
2971 @retval EFI_NOT_FOUND Did not find the SMBIOS table.
2976 NetLibGetSystemGuid (
2977 OUT EFI_GUID
*SystemGuid
2981 SMBIOS_TABLE_ENTRY_POINT
*SmbiosTable
;
2982 SMBIOS_TABLE_3_0_ENTRY_POINT
*Smbios30Table
;
2983 SMBIOS_STRUCTURE_POINTER Smbios
;
2984 SMBIOS_STRUCTURE_POINTER SmbiosEnd
;
2988 Status
= EfiGetSystemConfigurationTable (&gEfiSmbios3TableGuid
, (VOID
**) &Smbios30Table
);
2989 if (!(EFI_ERROR (Status
) || Smbios30Table
== NULL
)) {
2990 Smbios
.Hdr
= (SMBIOS_STRUCTURE
*) (UINTN
) Smbios30Table
->TableAddress
;
2991 SmbiosEnd
.Raw
= (UINT8
*) (UINTN
) (Smbios30Table
->TableAddress
+ Smbios30Table
->TableMaximumSize
);
2993 Status
= EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid
, (VOID
**) &SmbiosTable
);
2994 if (EFI_ERROR (Status
) || SmbiosTable
== NULL
) {
2995 return EFI_NOT_FOUND
;
2997 Smbios
.Hdr
= (SMBIOS_STRUCTURE
*) (UINTN
) SmbiosTable
->TableAddress
;
2998 SmbiosEnd
.Raw
= (UINT8
*) ((UINTN
) SmbiosTable
->TableAddress
+ SmbiosTable
->TableLength
);
3002 if (Smbios
.Hdr
->Type
== 1) {
3003 if (Smbios
.Hdr
->Length
< 0x19) {
3005 // Older version did not support UUID.
3007 return EFI_NOT_FOUND
;
3011 // SMBIOS tables are byte packed so we need to do a byte copy to
3012 // prevend alignment faults on Itanium-based platform.
3014 CopyMem (SystemGuid
, &Smbios
.Type1
->Uuid
, sizeof (EFI_GUID
));
3019 // Go to the next SMBIOS structure. Each SMBIOS structure may include 2 parts:
3020 // 1. Formatted section; 2. Unformatted string section. So, 2 steps are needed
3021 // to skip one SMBIOS structure.
3025 // Step 1: Skip over formatted section.
3027 String
= (CHAR8
*) (Smbios
.Raw
+ Smbios
.Hdr
->Length
);
3030 // Step 2: Skip over unformated string section.
3034 // Each string is terminated with a NULL(00h) BYTE and the sets of strings
3035 // is terminated with an additional NULL(00h) BYTE.
3037 for ( ; *String
!= 0; String
++) {
3040 if (*(UINT8
*)++String
== 0) {
3042 // Pointer to the next SMBIOS structure.
3044 Smbios
.Raw
= (UINT8
*)++String
;
3048 } while (Smbios
.Raw
< SmbiosEnd
.Raw
);
3049 return EFI_NOT_FOUND
;
3053 Create Dns QName according the queried domain name.
3054 QName is a domain name represented as a sequence of labels,
3055 where each label consists of a length octet followed by that
3056 number of octets. The QName terminates with the zero
3057 length octet for the null label of the root. Caller should
3058 take responsibility to free the buffer in returned pointer.
3060 @param DomainName The pointer to the queried domain name string.
3062 @retval NULL Failed to fill QName.
3063 @return QName filled successfully.
3068 NetLibCreateDnsQName (
3069 IN CHAR16
*DomainName
3073 UINTN QueryNameSize
;
3085 // One byte for first label length, one byte for terminated length zero.
3087 QueryNameSize
= StrLen (DomainName
) + 2;
3089 if (QueryNameSize
> DNS_MAX_NAME_SIZE
) {
3093 QueryName
= AllocateZeroPool (QueryNameSize
);
3094 if (QueryName
== NULL
) {
3101 for (Index
= 0; DomainName
[Index
] != 0; Index
++) {
3102 *Tail
= (CHAR8
) DomainName
[Index
];
3104 *Header
= (CHAR8
) Len
;
3113 *Header
= (CHAR8
) Len
;