4 Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Protocol/DriverBinding.h>
17 #include <Protocol/ServiceBinding.h>
18 #include <Protocol/SimpleNetwork.h>
19 #include <Protocol/ManagedNetwork.h>
20 #include <Protocol/HiiConfigRouting.h>
21 #include <Protocol/ComponentName.h>
22 #include <Protocol/ComponentName2.h>
23 #include <Protocol/HiiConfigAccess.h>
25 #include <Guid/NicIp4ConfigNvData.h>
27 #include <Library/NetLib.h>
28 #include <Library/BaseLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/UefiBootServicesTableLib.h>
32 #include <Library/UefiRuntimeServicesTableLib.h>
33 #include <Library/MemoryAllocationLib.h>
34 #include <Library/DevicePathLib.h>
35 #include <Library/HiiLib.h>
36 #include <Library/PrintLib.h>
37 #include <Library/UefiLib.h>
39 #define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE
42 // All the supported IP4 maskes in host byte order.
44 GLOBAL_REMOVE_IF_UNREFERENCED IP4_ADDR gIp4AllMasks
[IP4_MASK_NUM
] = {
83 GLOBAL_REMOVE_IF_UNREFERENCED EFI_IPv4_ADDRESS mZeroIp4Addr
= {{0, 0, 0, 0}};
86 // Any error level digitally larger than mNetDebugLevelMax
87 // will be silently discarded.
89 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mNetDebugLevelMax
= NETDEBUG_LEVEL_ERROR
;
90 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogPacketSeq
= 0xDEADBEEF;
93 // You can change mSyslogDstMac mSyslogDstIp and mSyslogSrcIp
94 // here to direct the syslog packets to the syslog deamon. The
95 // default is broadcast to both the ethernet and IP.
97 GLOBAL_REMOVE_IF_UNREFERENCED UINT8 mSyslogDstMac
[NET_ETHER_ADDR_LEN
] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
98 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogDstIp
= 0xffffffff;
99 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogSrcIp
= 0;
101 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8
*mMonthName
[] = {
117 // VLAN device path node template
119 GLOBAL_REMOVE_IF_UNREFERENCED VLAN_DEVICE_PATH mNetVlanDevicePathTemplate
= {
121 MESSAGING_DEVICE_PATH
,
124 (UINT8
) (sizeof (VLAN_DEVICE_PATH
)),
125 (UINT8
) ((sizeof (VLAN_DEVICE_PATH
)) >> 8)
132 Locate the handles that support SNP, then open one of them
133 to send the syslog packets. The caller isn't required to close
134 the SNP after use because the SNP is opened by HandleProtocol.
136 @return The point to SNP if one is properly openned. Otherwise NULL
139 EFI_SIMPLE_NETWORK_PROTOCOL
*
144 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
151 // Locate the handles which has SNP installed.
154 Status
= gBS
->LocateHandleBuffer (
156 &gEfiSimpleNetworkProtocolGuid
,
162 if (EFI_ERROR (Status
) || (HandleCount
== 0)) {
167 // Try to open one of the ethernet SNP protocol to send packet
171 for (Index
= 0; Index
< HandleCount
; Index
++) {
172 Status
= gBS
->HandleProtocol (
174 &gEfiSimpleNetworkProtocolGuid
,
178 if ((Status
== EFI_SUCCESS
) && (Snp
!= NULL
) &&
179 (Snp
->Mode
->IfType
== NET_IFTYPE_ETHERNET
) &&
180 (Snp
->Mode
->MaxPacketSize
>= NET_SYSLOG_PACKET_LEN
)) {
193 Transmit a syslog packet synchronously through SNP. The Packet
194 already has the ethernet header prepended. This function should
195 fill in the source MAC because it will try to locate a SNP each
196 time it is called to avoid the problem if SNP is unloaded.
197 This code snip is copied from MNP.
199 @param[in] Packet The Syslog packet
200 @param[in] Length The length of the packet
202 @retval EFI_DEVICE_ERROR Failed to locate a usable SNP protocol
203 @retval EFI_TIMEOUT Timeout happened to send the packet.
204 @retval EFI_SUCCESS Packet is sent.
213 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
216 EFI_EVENT TimeoutEvent
;
219 Snp
= SyslogLocateSnp ();
222 return EFI_DEVICE_ERROR
;
225 Ether
= (ETHER_HEAD
*) Packet
;
226 CopyMem (Ether
->SrcMac
, Snp
->Mode
->CurrentAddress
.Addr
, NET_ETHER_ADDR_LEN
);
229 // Start the timeout event.
231 Status
= gBS
->CreateEvent (
239 if (EFI_ERROR (Status
)) {
243 Status
= gBS
->SetTimer (TimeoutEvent
, TimerRelative
, NET_SYSLOG_TX_TIMEOUT
);
245 if (EFI_ERROR (Status
)) {
251 // Transmit the packet through SNP.
253 Status
= Snp
->Transmit (Snp
, 0, Length
, Packet
, NULL
, NULL
, NULL
);
255 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_READY
)) {
256 Status
= EFI_DEVICE_ERROR
;
261 // If Status is EFI_SUCCESS, the packet is put in the transmit queue.
262 // if Status is EFI_NOT_READY, the transmit engine of the network
263 // interface is busy. Both need to sync SNP.
269 // Get the recycled transmit buffer status.
271 Snp
->GetStatus (Snp
, NULL
, (VOID
**) &TxBuf
);
273 if (!EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
274 Status
= EFI_TIMEOUT
;
278 } while (TxBuf
== NULL
);
280 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_TIMEOUT
)) {
285 // Status is EFI_NOT_READY. Restart the timer event and
286 // call Snp->Transmit again.
288 gBS
->SetTimer (TimeoutEvent
, TimerRelative
, NET_SYSLOG_TX_TIMEOUT
);
291 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
294 gBS
->CloseEvent (TimeoutEvent
);
299 Build a syslog packet, including the Ethernet/Ip/Udp headers
302 @param[in] Level Syslog servity level
303 @param[in] Module The module that generates the log
304 @param[in] File The file that contains the current log
305 @param[in] Line The line of code in the File that contains the current log
306 @param[in] Message The log message
307 @param[in] BufLen The lenght of the Buf
308 @param[out] Buf The buffer to put the packet data
310 @return The length of the syslog packet built.
326 EFI_UDP_HEADER
*Udp4
;
332 // Fill in the Ethernet header. Leave alone the source MAC.
333 // SyslogSendPacket will fill in the address for us.
335 Ether
= (ETHER_HEAD
*) Buf
;
336 CopyMem (Ether
->DstMac
, mSyslogDstMac
, NET_ETHER_ADDR_LEN
);
337 ZeroMem (Ether
->SrcMac
, NET_ETHER_ADDR_LEN
);
339 Ether
->EtherType
= HTONS (0x0800); // IPv4 protocol
341 Buf
+= sizeof (ETHER_HEAD
);
342 BufLen
-= sizeof (ETHER_HEAD
);
345 // Fill in the IP header
347 Ip4
= (IP4_HEAD
*) Buf
;
352 Ip4
->Id
= (UINT16
) mSyslogPacketSeq
;
355 Ip4
->Protocol
= 0x11;
357 Ip4
->Src
= mSyslogSrcIp
;
358 Ip4
->Dst
= mSyslogDstIp
;
360 Buf
+= sizeof (IP4_HEAD
);
361 BufLen
-= sizeof (IP4_HEAD
);
364 // Fill in the UDP header, Udp checksum is optional. Leave it zero.
366 Udp4
= (EFI_UDP_HEADER
*) Buf
;
367 Udp4
->SrcPort
= HTONS (514);
368 Udp4
->DstPort
= HTONS (514);
372 Buf
+= sizeof (EFI_UDP_HEADER
);
373 BufLen
-= sizeof (EFI_UDP_HEADER
);
376 // Build the syslog message body with <PRI> Timestamp machine module Message
378 Pri
= ((NET_SYSLOG_FACILITY
& 31) << 3) | (Level
& 7);
379 gRT
->GetTime (&Time
, NULL
);
380 ASSERT ((Time
.Month
<= 12) && (Time
.Month
>= 1));
383 // Use %a to format the ASCII strings, %s to format UNICODE strings
386 Len
+= (UINT32
) AsciiSPrint (
389 "<%d> %a %d %d:%d:%d ",
391 mMonthName
[Time
.Month
-1],
399 Len
+= (UINT32
) AsciiSPrint (
402 "Tiano %a: %a (Line: %d File: %a)",
411 // OK, patch the IP length/checksum and UDP length fields.
413 Len
+= sizeof (EFI_UDP_HEADER
);
414 Udp4
->Length
= HTONS ((UINT16
) Len
);
416 Len
+= sizeof (IP4_HEAD
);
417 Ip4
->TotalLen
= HTONS ((UINT16
) Len
);
418 Ip4
->Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Ip4
, sizeof (IP4_HEAD
)));
420 return Len
+ sizeof (ETHER_HEAD
);
424 Allocate a buffer, then format the message to it. This is a
425 help function for the NET_DEBUG_XXX macros. The PrintArg of
426 these macros treats the variable length print parameters as a
427 single parameter, and pass it to the NetDebugASPrint. For
428 example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name))
432 NETDEBUG_LEVEL_TRACE,
436 NetDebugASPrint ("State transit to %a\n", Name)
439 @param Format The ASCII format string.
440 @param ... The variable length parameter whose format is determined
441 by the Format string.
443 @return The buffer containing the formatted message,
444 or NULL if failed to allocate memory.
457 Buf
= (CHAR8
*) AllocatePool (NET_DEBUG_MSG_LEN
);
463 VA_START (Marker
, Format
);
464 AsciiVSPrint (Buf
, NET_DEBUG_MSG_LEN
, Format
, Marker
);
471 Builds an UDP4 syslog packet and send it using SNP.
473 This function will locate a instance of SNP then send the message through it.
474 Because it isn't open the SNP BY_DRIVER, apply caution when using it.
476 @param Level The servity level of the message.
477 @param Module The Moudle that generates the log.
478 @param File The file that contains the log.
479 @param Line The exact line that contains the log.
480 @param Message The user message to log.
482 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
483 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet
484 @retval EFI_SUCCESS The log is discard because that it is more verbose
485 than the mNetDebugLevelMax. Or, it has been sent out.
502 // Check whether the message should be sent out
504 if (Message
== NULL
) {
505 return EFI_INVALID_PARAMETER
;
508 if (Level
> mNetDebugLevelMax
) {
509 Status
= EFI_SUCCESS
;
514 // Allocate a maxium of 1024 bytes, the caller should ensure
515 // that the message plus the ethernet/ip/udp header is shorter
518 Packet
= (CHAR8
*) AllocatePool (NET_SYSLOG_PACKET_LEN
);
520 if (Packet
== NULL
) {
521 Status
= EFI_OUT_OF_RESOURCES
;
526 // Build the message: Ethernet header + IP header + Udp Header + user data
528 Len
= SyslogBuildPacket (
534 NET_SYSLOG_PACKET_LEN
,
539 Status
= SyslogSendPacket (Packet
, Len
);
547 Return the length of the mask.
549 Return the length of the mask, the correct value is from 0 to 32.
550 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.
551 NetMask is in the host byte order.
553 @param[in] NetMask The netmask to get the length from.
555 @return The length of the netmask, IP4_MASK_NUM if the mask is invalid.
566 for (Index
= 0; Index
< IP4_MASK_NUM
; Index
++) {
567 if (NetMask
== gIp4AllMasks
[Index
]) {
578 Return the class of the IP address, such as class A, B, C.
579 Addr is in host byte order.
581 The address of class A starts with 0.
582 If the address belong to class A, return IP4_ADDR_CLASSA.
583 The address of class B starts with 10.
584 If the address belong to class B, return IP4_ADDR_CLASSB.
585 The address of class C starts with 110.
586 If the address belong to class C, return IP4_ADDR_CLASSC.
587 The address of class D starts with 1110.
588 If the address belong to class D, return IP4_ADDR_CLASSD.
589 The address of class E starts with 1111.
590 If the address belong to class E, return IP4_ADDR_CLASSE.
593 @param[in] Addr The address to get the class from.
595 @return IP address class, such as IP4_ADDR_CLASSA.
606 ByteOne
= (UINT8
) (Addr
>> 24);
608 if ((ByteOne
& 0x80) == 0) {
609 return IP4_ADDR_CLASSA
;
611 } else if ((ByteOne
& 0xC0) == 0x80) {
612 return IP4_ADDR_CLASSB
;
614 } else if ((ByteOne
& 0xE0) == 0xC0) {
615 return IP4_ADDR_CLASSC
;
617 } else if ((ByteOne
& 0xF0) == 0xE0) {
618 return IP4_ADDR_CLASSD
;
621 return IP4_ADDR_CLASSE
;
628 Check whether the IP is a valid unicast address according to
629 the netmask. If NetMask is zero, use the IP address's class to get the default mask.
631 If Ip is 0, IP is not a valid unicast address.
632 Class D address is used for multicasting and class E address is reserved for future. If Ip
633 belongs to class D or class E, IP is not a valid unicast address.
634 If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address.
636 @param[in] Ip The IP to check against.
637 @param[in] NetMask The mask of the IP.
639 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.
651 Class
= NetGetIpClass (Ip
);
653 if ((Ip
== 0) || (Class
>= IP4_ADDR_CLASSD
)) {
658 NetMask
= gIp4AllMasks
[Class
<< 3];
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_NUM
));
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.
857 Get current time first. Then initialize a random seed based on some basic
858 mathematics operation on the hour, day, minute, second, nanosecond and year
861 @return The random seed initialized with current time.
873 gRT
->GetTime (&Time
, NULL
);
874 Seed
= (~Time
.Hour
<< 24 | Time
.Day
<< 16 | Time
.Minute
<< 8 | Time
.Second
);
875 Seed
^= Time
.Nanosecond
;
876 Seed
^= Time
.Year
<< 7;
883 Extract a UINT32 from a byte stream.
885 Copy a UINT32 from a byte stream, then converts it from Network
886 byte order to host byte order. Use this function to avoid alignment error.
888 @param[in] Buf The buffer to extract the UINT32.
890 @return The UINT32 extracted.
901 CopyMem (&Value
, Buf
, sizeof (UINT32
));
902 return NTOHL (Value
);
907 Put a UINT32 to the byte stream in network byte order.
909 Converts a UINT32 from host byte order to network byte order. Then copy it to the
912 @param[in, out] Buf The buffer to put the UINT32.
913 @param[in] Data The data to be converted and put into the byte stream.
924 CopyMem (Buf
, &Data
, sizeof (UINT32
));
929 Remove the first node entry on the list, and return the removed node entry.
931 Removes the first node Entry from a doubly linked list. It is up to the caller of
932 this function to release the memory used by the first node if that is required. On
933 exit, the removed node is returned.
935 If Head is NULL, then ASSERT().
936 If Head was not initialized, then ASSERT().
937 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
938 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
941 @param[in, out] Head The list header.
943 @return The first node entry that is removed from the list, NULL if the list is empty.
949 IN OUT LIST_ENTRY
*Head
954 ASSERT (Head
!= NULL
);
956 if (IsListEmpty (Head
)) {
960 First
= Head
->ForwardLink
;
961 Head
->ForwardLink
= First
->ForwardLink
;
962 First
->ForwardLink
->BackLink
= Head
;
965 First
->ForwardLink
= (LIST_ENTRY
*) NULL
;
966 First
->BackLink
= (LIST_ENTRY
*) NULL
;
974 Remove the last node entry on the list and and return the removed node entry.
976 Removes the last node entry from a doubly linked list. It is up to the caller of
977 this function to release the memory used by the first node if that is required. On
978 exit, the removed node is returned.
980 If Head is NULL, then ASSERT().
981 If Head was not initialized, then ASSERT().
982 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
983 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
986 @param[in, out] Head The list head.
988 @return The last node entry that is removed from the list, NULL if the list is empty.
994 IN OUT LIST_ENTRY
*Head
999 ASSERT (Head
!= NULL
);
1001 if (IsListEmpty (Head
)) {
1005 Last
= Head
->BackLink
;
1006 Head
->BackLink
= Last
->BackLink
;
1007 Last
->BackLink
->ForwardLink
= Head
;
1010 Last
->ForwardLink
= (LIST_ENTRY
*) NULL
;
1011 Last
->BackLink
= (LIST_ENTRY
*) NULL
;
1019 Insert a new node entry after a designated node entry of a doubly linked list.
1021 Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry
1022 of the doubly linked list.
1024 @param[in, out] PrevEntry The previous entry to insert after.
1025 @param[in, out] NewEntry The new entry to insert.
1030 NetListInsertAfter (
1031 IN OUT LIST_ENTRY
*PrevEntry
,
1032 IN OUT LIST_ENTRY
*NewEntry
1035 NewEntry
->BackLink
= PrevEntry
;
1036 NewEntry
->ForwardLink
= PrevEntry
->ForwardLink
;
1037 PrevEntry
->ForwardLink
->BackLink
= NewEntry
;
1038 PrevEntry
->ForwardLink
= NewEntry
;
1043 Insert a new node entry before a designated node entry of a doubly linked list.
1045 Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry
1046 of the doubly linked list.
1048 @param[in, out] PostEntry The entry to insert before.
1049 @param[in, out] NewEntry The new entry to insert.
1054 NetListInsertBefore (
1055 IN OUT LIST_ENTRY
*PostEntry
,
1056 IN OUT LIST_ENTRY
*NewEntry
1059 NewEntry
->ForwardLink
= PostEntry
;
1060 NewEntry
->BackLink
= PostEntry
->BackLink
;
1061 PostEntry
->BackLink
->ForwardLink
= NewEntry
;
1062 PostEntry
->BackLink
= NewEntry
;
1067 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
1069 Initialize the forward and backward links of two head nodes donated by Map->Used
1070 and Map->Recycled of two doubly linked lists.
1071 Initializes the count of the <Key, Value> pairs in the netmap to zero.
1073 If Map is NULL, then ASSERT().
1074 If the address of Map->Used is NULL, then ASSERT().
1075 If the address of Map->Recycled is NULl, then ASSERT().
1077 @param[in, out] Map The netmap to initialize.
1086 ASSERT (Map
!= NULL
);
1088 InitializeListHead (&Map
->Used
);
1089 InitializeListHead (&Map
->Recycled
);
1095 To clean up the netmap, that is, release allocated memories.
1097 Removes all nodes of the Used doubly linked list and free memory of all related netmap items.
1098 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.
1099 The number of the <Key, Value> pairs in the netmap is set to be zero.
1101 If Map is NULL, then ASSERT().
1103 @param[in, out] Map The netmap to clean up.
1116 ASSERT (Map
!= NULL
);
1118 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Map
->Used
) {
1119 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1121 RemoveEntryList (&Item
->Link
);
1124 gBS
->FreePool (Item
);
1127 ASSERT ((Map
->Count
== 0) && IsListEmpty (&Map
->Used
));
1129 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Map
->Recycled
) {
1130 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1132 RemoveEntryList (&Item
->Link
);
1133 gBS
->FreePool (Item
);
1136 ASSERT (IsListEmpty (&Map
->Recycled
));
1141 Test whether the netmap is empty and return true if it is.
1143 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.
1145 If Map is NULL, then ASSERT().
1148 @param[in] Map The net map to test.
1150 @return TRUE if the netmap is empty, otherwise FALSE.
1159 ASSERT (Map
!= NULL
);
1160 return (BOOLEAN
) (Map
->Count
== 0);
1165 Return the number of the <Key, Value> pairs in the netmap.
1167 @param[in] Map The netmap to get the entry number.
1169 @return The entry number in the netmap.
1183 Return one allocated item.
1185 If the Recycled doubly linked list of the netmap is empty, it will try to allocate
1186 a batch of items if there are enough resources and add corresponding nodes to the begining
1187 of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove
1188 the fist node entry of the Recycled doubly linked list and return the corresponding item.
1190 If Map is NULL, then ASSERT().
1192 @param[in, out] Map The netmap to allocate item for.
1194 @return The allocated item. If NULL, the
1195 allocation failed due to resource limit.
1207 ASSERT (Map
!= NULL
);
1209 Head
= &Map
->Recycled
;
1211 if (IsListEmpty (Head
)) {
1212 for (Index
= 0; Index
< NET_MAP_INCREAMENT
; Index
++) {
1213 Item
= AllocatePool (sizeof (NET_MAP_ITEM
));
1223 InsertHeadList (Head
, &Item
->Link
);
1227 Item
= NET_LIST_HEAD (Head
, NET_MAP_ITEM
, Link
);
1228 NetListRemoveHead (Head
);
1235 Allocate an item to save the <Key, Value> pair to the head of the netmap.
1237 Allocate an item to save the <Key, Value> pair and add corresponding node entry
1238 to the beginning of the Used doubly linked list. The number of the <Key, Value>
1239 pairs in the netmap increase by 1.
1241 If Map is NULL, then ASSERT().
1243 @param[in, out] Map The netmap to insert into.
1244 @param[in] Key The user's key.
1245 @param[in] Value The user's value for the key.
1247 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
1248 @retval EFI_SUCCESS The item is inserted to the head.
1254 IN OUT NET_MAP
*Map
,
1256 IN VOID
*Value OPTIONAL
1261 ASSERT (Map
!= NULL
);
1263 Item
= NetMapAllocItem (Map
);
1266 return EFI_OUT_OF_RESOURCES
;
1270 Item
->Value
= Value
;
1271 InsertHeadList (&Map
->Used
, &Item
->Link
);
1279 Allocate an item to save the <Key, Value> pair to the tail of the netmap.
1281 Allocate an item to save the <Key, Value> pair and add corresponding node entry
1282 to the tail of the Used doubly linked list. The number of the <Key, Value>
1283 pairs in the netmap increase by 1.
1285 If Map is NULL, then ASSERT().
1287 @param[in, out] Map The netmap to insert into.
1288 @param[in] Key The user's key.
1289 @param[in] Value The user's value for the key.
1291 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
1292 @retval EFI_SUCCESS The item is inserted to the tail.
1298 IN OUT NET_MAP
*Map
,
1300 IN VOID
*Value OPTIONAL
1305 ASSERT (Map
!= NULL
);
1307 Item
= NetMapAllocItem (Map
);
1310 return EFI_OUT_OF_RESOURCES
;
1314 Item
->Value
= Value
;
1315 InsertTailList (&Map
->Used
, &Item
->Link
);
1324 Check whether the item is in the Map and return TRUE if it is.
1326 @param[in] Map The netmap to search within.
1327 @param[in] Item The item to search.
1329 @return TRUE if the item is in the netmap, otherwise FALSE.
1335 IN NET_MAP_ITEM
*Item
1338 LIST_ENTRY
*ListEntry
;
1340 NET_LIST_FOR_EACH (ListEntry
, &Map
->Used
) {
1341 if (ListEntry
== &Item
->Link
) {
1351 Find the key in the netmap and returns the point to the item contains the Key.
1353 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every
1354 item with the key to search. It returns the point to the item contains the Key if found.
1356 If Map is NULL, then ASSERT().
1358 @param[in] Map The netmap to search within.
1359 @param[in] Key The key to search.
1361 @return The point to the item contains the Key, or NULL if Key isn't in the map.
1374 ASSERT (Map
!= NULL
);
1376 NET_LIST_FOR_EACH (Entry
, &Map
->Used
) {
1377 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1379 if (Item
->Key
== Key
) {
1389 Remove the node entry of the item from the netmap and return the key of the removed item.
1391 Remove the node entry of the item from the Used doubly linked list of the netmap.
1392 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1393 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,
1394 Value will point to the value of the item. It returns the key of the removed item.
1396 If Map is NULL, then ASSERT().
1397 If Item is NULL, then ASSERT().
1398 if item in not in the netmap, then ASSERT().
1400 @param[in, out] Map The netmap to remove the item from.
1401 @param[in, out] Item The item to remove.
1402 @param[out] Value The variable to receive the value if not NULL.
1404 @return The key of the removed item.
1410 IN OUT NET_MAP
*Map
,
1411 IN OUT NET_MAP_ITEM
*Item
,
1412 OUT VOID
**Value OPTIONAL
1415 ASSERT ((Map
!= NULL
) && (Item
!= NULL
));
1416 ASSERT (NetItemInMap (Map
, Item
));
1418 RemoveEntryList (&Item
->Link
);
1420 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
1422 if (Value
!= NULL
) {
1423 *Value
= Item
->Value
;
1431 Remove the first node entry on the netmap and return the key of the removed item.
1433 Remove the first node entry from the Used doubly linked list of the netmap.
1434 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1435 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
1436 parameter Value will point to the value of the item. It returns the key of the removed item.
1438 If Map is NULL, then ASSERT().
1439 If the Used doubly linked list is empty, then ASSERT().
1441 @param[in, out] Map The netmap to remove the head from.
1442 @param[out] Value The variable to receive the value if not NULL.
1444 @return The key of the item removed.
1450 IN OUT NET_MAP
*Map
,
1451 OUT VOID
**Value OPTIONAL
1457 // Often, it indicates a programming error to remove
1458 // the first entry in an empty list
1460 ASSERT (Map
&& !IsListEmpty (&Map
->Used
));
1462 Item
= NET_LIST_HEAD (&Map
->Used
, NET_MAP_ITEM
, Link
);
1463 RemoveEntryList (&Item
->Link
);
1465 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
1467 if (Value
!= NULL
) {
1468 *Value
= Item
->Value
;
1476 Remove the last node entry on the netmap and return the key of the removed item.
1478 Remove the last node entry from the Used doubly linked list of the netmap.
1479 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1480 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
1481 parameter Value will point to the value of the item. It returns the key of the removed item.
1483 If Map is NULL, then ASSERT().
1484 If the Used doubly linked list is empty, then ASSERT().
1486 @param[in, out] Map The netmap to remove the tail from.
1487 @param[out] Value The variable to receive the value if not NULL.
1489 @return The key of the item removed.
1495 IN OUT NET_MAP
*Map
,
1496 OUT VOID
**Value OPTIONAL
1502 // Often, it indicates a programming error to remove
1503 // the last entry in an empty list
1505 ASSERT (Map
&& !IsListEmpty (&Map
->Used
));
1507 Item
= NET_LIST_TAIL (&Map
->Used
, NET_MAP_ITEM
, Link
);
1508 RemoveEntryList (&Item
->Link
);
1510 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
1512 if (Value
!= NULL
) {
1513 *Value
= Item
->Value
;
1521 Iterate through the netmap and call CallBack for each item.
1523 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break
1524 from the loop. It returns the CallBack's last return value. This function is
1525 delete safe for the current item.
1527 If Map is NULL, then ASSERT().
1528 If CallBack is NULL, then ASSERT().
1530 @param[in] Map The Map to iterate through.
1531 @param[in] CallBack The callback function to call for each item.
1532 @param[in] Arg The opaque parameter to the callback.
1534 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item
1536 @retval Others It returns the CallBack's last return value.
1543 IN NET_MAP_CALLBACK CallBack
,
1544 IN VOID
*Arg OPTIONAL
1554 ASSERT ((Map
!= NULL
) && (CallBack
!= NULL
));
1558 if (IsListEmpty (Head
)) {
1562 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Head
) {
1563 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1564 Result
= CallBack (Map
, Item
, Arg
);
1566 if (EFI_ERROR (Result
)) {
1576 Internal function to get the child handle of the NIC handle.
1578 @param[in] Controller NIC controller handle.
1579 @param[out] ChildHandle Returned child handle.
1581 @retval EFI_SUCCESS Successfully to get child handle.
1582 @retval Others Failed to get child handle.
1587 IN EFI_HANDLE Controller
,
1588 OUT EFI_HANDLE
*ChildHandle
1592 EFI_HANDLE
*Handles
;
1595 EFI_DEVICE_PATH_PROTOCOL
*ChildDeviceDevicePath
;
1596 VENDOR_DEVICE_PATH
*VendorDeviceNode
;
1599 // Locate all EFI Hii Config Access protocols
1601 Status
= gBS
->LocateHandleBuffer (
1603 &gEfiHiiConfigAccessProtocolGuid
,
1608 if (EFI_ERROR (Status
) || (HandleCount
== 0)) {
1612 Status
= EFI_NOT_FOUND
;
1614 for (Index
= 0; Index
< HandleCount
; Index
++) {
1616 Status
= EfiTestChildHandle (Controller
, Handles
[Index
], &gEfiManagedNetworkServiceBindingProtocolGuid
);
1617 if (!EFI_ERROR (Status
)) {
1619 // Get device path on the child handle
1621 Status
= gBS
->HandleProtocol (
1623 &gEfiDevicePathProtocolGuid
,
1624 (VOID
**) &ChildDeviceDevicePath
1627 if (!EFI_ERROR (Status
)) {
1628 while (!IsDevicePathEnd (ChildDeviceDevicePath
)) {
1629 ChildDeviceDevicePath
= NextDevicePathNode (ChildDeviceDevicePath
);
1631 // Parse one instance
1633 if (ChildDeviceDevicePath
->Type
== HARDWARE_DEVICE_PATH
&&
1634 ChildDeviceDevicePath
->SubType
== HW_VENDOR_DP
) {
1635 VendorDeviceNode
= (VENDOR_DEVICE_PATH
*) ChildDeviceDevicePath
;
1636 if (CompareMem (&VendorDeviceNode
->Guid
, &gEfiNicIp4ConfigVariableGuid
, sizeof (EFI_GUID
)) == 0) {
1638 // Found item matched gEfiNicIp4ConfigVariableGuid
1640 *ChildHandle
= Handles
[Index
];
1656 This is the default unload handle for all the network drivers.
1658 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
1659 Uninstall all the protocols installed in the driver entry point.
1661 @param[in] ImageHandle The drivers' driver image.
1663 @retval EFI_SUCCESS The image is unloaded.
1664 @retval Others Failed to unload the image.
1669 NetLibDefaultUnload (
1670 IN EFI_HANDLE ImageHandle
1674 EFI_HANDLE
*DeviceHandleBuffer
;
1675 UINTN DeviceHandleCount
;
1677 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
1678 EFI_COMPONENT_NAME_PROTOCOL
*ComponentName
;
1679 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
1682 // Get the list of all the handles in the handle database.
1683 // If there is an error getting the list, then the unload
1686 Status
= gBS
->LocateHandleBuffer (
1694 if (EFI_ERROR (Status
)) {
1699 // Disconnect the driver specified by ImageHandle from all
1700 // the devices in the handle database.
1702 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1703 Status
= gBS
->DisconnectController (
1704 DeviceHandleBuffer
[Index
],
1711 // Uninstall all the protocols installed in the driver entry point
1713 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1714 Status
= gBS
->HandleProtocol (
1715 DeviceHandleBuffer
[Index
],
1716 &gEfiDriverBindingProtocolGuid
,
1717 (VOID
**) &DriverBinding
1720 if (EFI_ERROR (Status
)) {
1724 if (DriverBinding
->ImageHandle
!= ImageHandle
) {
1728 gBS
->UninstallProtocolInterface (
1730 &gEfiDriverBindingProtocolGuid
,
1733 Status
= gBS
->HandleProtocol (
1734 DeviceHandleBuffer
[Index
],
1735 &gEfiComponentNameProtocolGuid
,
1736 (VOID
**) &ComponentName
1738 if (!EFI_ERROR (Status
)) {
1739 gBS
->UninstallProtocolInterface (
1741 &gEfiComponentNameProtocolGuid
,
1746 Status
= gBS
->HandleProtocol (
1747 DeviceHandleBuffer
[Index
],
1748 &gEfiComponentName2ProtocolGuid
,
1749 (VOID
**) &ComponentName2
1751 if (!EFI_ERROR (Status
)) {
1752 gBS
->UninstallProtocolInterface (
1754 &gEfiComponentName2ProtocolGuid
,
1761 // Free the buffer containing the list of handles from the handle database
1763 if (DeviceHandleBuffer
!= NULL
) {
1764 gBS
->FreePool (DeviceHandleBuffer
);
1773 Create a child of the service that is identified by ServiceBindingGuid.
1775 Get the ServiceBinding Protocol first, then use it to create a child.
1777 If ServiceBindingGuid is NULL, then ASSERT().
1778 If ChildHandle is NULL, then ASSERT().
1780 @param[in] Controller The controller which has the service installed.
1781 @param[in] Image The image handle used to open service.
1782 @param[in] ServiceBindingGuid The service's Guid.
1783 @param[in, out] ChildHandle The handle to receive the create child.
1785 @retval EFI_SUCCESS The child is successfully created.
1786 @retval Others Failed to create the child.
1791 NetLibCreateServiceChild (
1792 IN EFI_HANDLE Controller
,
1793 IN EFI_HANDLE Image
,
1794 IN EFI_GUID
*ServiceBindingGuid
,
1795 IN OUT EFI_HANDLE
*ChildHandle
1799 EFI_SERVICE_BINDING_PROTOCOL
*Service
;
1802 ASSERT ((ServiceBindingGuid
!= NULL
) && (ChildHandle
!= NULL
));
1805 // Get the ServiceBinding Protocol
1807 Status
= gBS
->OpenProtocol (
1813 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1816 if (EFI_ERROR (Status
)) {
1823 Status
= Service
->CreateChild (Service
, ChildHandle
);
1829 Destory a child of the service that is identified by ServiceBindingGuid.
1831 Get the ServiceBinding Protocol first, then use it to destroy a child.
1833 If ServiceBindingGuid is NULL, then ASSERT().
1835 @param[in] Controller The controller which has the service installed.
1836 @param[in] Image The image handle used to open service.
1837 @param[in] ServiceBindingGuid The service's Guid.
1838 @param[in] ChildHandle The child to destory.
1840 @retval EFI_SUCCESS The child is successfully destoried.
1841 @retval Others Failed to destory the child.
1846 NetLibDestroyServiceChild (
1847 IN EFI_HANDLE Controller
,
1848 IN EFI_HANDLE Image
,
1849 IN EFI_GUID
*ServiceBindingGuid
,
1850 IN EFI_HANDLE ChildHandle
1854 EFI_SERVICE_BINDING_PROTOCOL
*Service
;
1856 ASSERT (ServiceBindingGuid
!= NULL
);
1859 // Get the ServiceBinding Protocol
1861 Status
= gBS
->OpenProtocol (
1867 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1870 if (EFI_ERROR (Status
)) {
1875 // destory the child
1877 Status
= Service
->DestroyChild (Service
, ChildHandle
);
1882 Get handle with Simple Network Protocol installed on it.
1884 There should be MNP Service Binding Protocol installed on the input ServiceHandle.
1885 If Simple Network Protocol is already installed on the ServiceHandle, the
1886 ServiceHandle will be returned. If SNP is not installed on the ServiceHandle,
1887 try to find its parent handle with SNP installed.
1889 @param[in] ServiceHandle The handle where network service binding protocols are
1891 @param[out] Snp The pointer to store the address of the SNP instance.
1892 This is an optional parameter that may be NULL.
1894 @return The SNP handle, or NULL if not found.
1899 NetLibGetSnpHandle (
1900 IN EFI_HANDLE ServiceHandle
,
1901 OUT EFI_SIMPLE_NETWORK_PROTOCOL
**Snp OPTIONAL
1905 EFI_SIMPLE_NETWORK_PROTOCOL
*SnpInstance
;
1906 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1907 EFI_HANDLE SnpHandle
;
1910 // Try to open SNP from ServiceHandle
1913 Status
= gBS
->HandleProtocol (ServiceHandle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &SnpInstance
);
1914 if (!EFI_ERROR (Status
)) {
1918 return ServiceHandle
;
1922 // Failed to open SNP, try to get SNP handle by LocateDevicePath()
1924 DevicePath
= DevicePathFromHandle (ServiceHandle
);
1925 if (DevicePath
== NULL
) {
1930 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &DevicePath
, &SnpHandle
);
1931 if (EFI_ERROR (Status
)) {
1933 // Failed to find SNP handle
1938 Status
= gBS
->HandleProtocol (SnpHandle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &SnpInstance
);
1939 if (!EFI_ERROR (Status
)) {
1950 Retrieve VLAN ID of a VLAN device handle.
1952 Search VLAN device path node in Device Path of specified ServiceHandle and
1953 return its VLAN ID. If no VLAN device path node found, then this ServiceHandle
1954 is not a VLAN device handle, and 0 will be returned.
1956 @param[in] ServiceHandle The handle where network service binding protocols are
1959 @return VLAN ID of the device handle, or 0 if not a VLAN device.
1965 IN EFI_HANDLE ServiceHandle
1968 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1969 EFI_DEVICE_PATH_PROTOCOL
*Node
;
1971 DevicePath
= DevicePathFromHandle (ServiceHandle
);
1972 if (DevicePath
== NULL
) {
1977 while (!IsDevicePathEnd (Node
)) {
1978 if (Node
->Type
== MESSAGING_DEVICE_PATH
&& Node
->SubType
== MSG_VLAN_DP
) {
1979 return ((VLAN_DEVICE_PATH
*) Node
)->VlanId
;
1981 Node
= NextDevicePathNode (Node
);
1988 Find VLAN device handle with specified VLAN ID.
1990 The VLAN child device handle is created by VLAN Config Protocol on ControllerHandle.
1991 This function will append VLAN device path node to the parent device path,
1992 and then use LocateDevicePath() to find the correct VLAN device handle.
1994 @param[in] ControllerHandle The handle where network service binding protocols are
1996 @param[in] VlanId The configured VLAN ID for the VLAN device.
1998 @return The VLAN device handle, or NULL if not found.
2003 NetLibGetVlanHandle (
2004 IN EFI_HANDLE ControllerHandle
,
2008 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
2009 EFI_DEVICE_PATH_PROTOCOL
*VlanDevicePath
;
2010 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2011 VLAN_DEVICE_PATH VlanNode
;
2014 ParentDevicePath
= DevicePathFromHandle (ControllerHandle
);
2015 if (ParentDevicePath
== NULL
) {
2020 // Construct VLAN device path
2022 CopyMem (&VlanNode
, &mNetVlanDevicePathTemplate
, sizeof (VLAN_DEVICE_PATH
));
2023 VlanNode
.VlanId
= VlanId
;
2024 VlanDevicePath
= AppendDevicePathNode (
2026 (EFI_DEVICE_PATH_PROTOCOL
*) &VlanNode
2028 if (VlanDevicePath
== NULL
) {
2033 // Find VLAN device handle
2036 DevicePath
= VlanDevicePath
;
2037 gBS
->LocateDevicePath (
2038 &gEfiDevicePathProtocolGuid
,
2042 if (!IsDevicePathEnd (DevicePath
)) {
2044 // Device path is not exactly match
2049 FreePool (VlanDevicePath
);
2054 Get MAC address associated with the network service handle.
2056 There should be MNP Service Binding Protocol installed on the input ServiceHandle.
2057 If SNP is installed on the ServiceHandle or its parent handle, MAC address will
2058 be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP.
2060 @param[in] ServiceHandle The handle where network service binding protocols are
2062 @param[out] MacAddress The pointer to store the returned MAC address.
2063 @param[out] AddressSize The length of returned MAC address.
2065 @retval EFI_SUCCESS MAC address is returned successfully.
2066 @retval Others Failed to get SNP mode data.
2071 NetLibGetMacAddress (
2072 IN EFI_HANDLE ServiceHandle
,
2073 OUT EFI_MAC_ADDRESS
*MacAddress
,
2074 OUT UINTN
*AddressSize
2078 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
2079 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
2080 EFI_SIMPLE_NETWORK_MODE SnpModeData
;
2081 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
2082 EFI_SERVICE_BINDING_PROTOCOL
*MnpSb
;
2083 EFI_HANDLE
*SnpHandle
;
2084 EFI_HANDLE MnpChildHandle
;
2086 ASSERT (MacAddress
!= NULL
);
2087 ASSERT (AddressSize
!= NULL
);
2090 // Try to get SNP handle
2093 SnpHandle
= NetLibGetSnpHandle (ServiceHandle
, &Snp
);
2094 if (SnpHandle
!= NULL
) {
2096 // SNP found, use it directly
2098 SnpMode
= Snp
->Mode
;
2101 // Failed to get SNP handle, try to get MAC address from MNP
2103 MnpChildHandle
= NULL
;
2104 Status
= gBS
->HandleProtocol (
2106 &gEfiManagedNetworkServiceBindingProtocolGuid
,
2109 if (EFI_ERROR (Status
)) {
2114 // Create a MNP child
2116 Status
= MnpSb
->CreateChild (MnpSb
, &MnpChildHandle
);
2117 if (EFI_ERROR (Status
)) {
2122 // Open MNP protocol
2124 Status
= gBS
->HandleProtocol (
2126 &gEfiManagedNetworkProtocolGuid
,
2129 if (EFI_ERROR (Status
)) {
2134 // Try to get SNP mode from MNP
2136 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpModeData
);
2137 if (EFI_ERROR (Status
)) {
2140 SnpMode
= &SnpModeData
;
2143 // Destroy the MNP child
2145 MnpSb
->DestroyChild (MnpSb
, MnpChildHandle
);
2148 *AddressSize
= SnpMode
->HwAddressSize
;
2149 CopyMem (MacAddress
->Addr
, SnpMode
->CurrentAddress
.Addr
, SnpMode
->HwAddressSize
);
2155 Convert MAC address of the NIC associated with specified Service Binding Handle
2156 to a unicode string. Callers are responsible for freeing the string storage.
2158 Locate simple network protocol associated with the Service Binding Handle and
2159 get the mac address from SNP. Then convert the mac address into a unicode
2160 string. It takes 2 unicode characters to represent a 1 byte binary buffer.
2161 Plus one unicode character for the null-terminator.
2163 @param[in] ServiceHandle The handle where network service binding protocol is
2165 @param[in] ImageHandle The image handle used to act as the agent handle to
2166 get the simple network protocol.
2167 @param[out] MacString The pointer to store the address of the string
2168 representation of the mac address.
2170 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.
2171 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
2172 @retval Others Failed to open the simple network protocol.
2177 NetLibGetMacString (
2178 IN EFI_HANDLE ServiceHandle
,
2179 IN EFI_HANDLE ImageHandle
,
2180 OUT CHAR16
**MacString
2184 EFI_MAC_ADDRESS MacAddress
;
2186 UINTN HwAddressSize
;
2191 ASSERT (MacString
!= NULL
);
2194 // Get MAC address of the network device
2196 Status
= NetLibGetMacAddress (ServiceHandle
, &MacAddress
, &HwAddressSize
);
2197 if (EFI_ERROR (Status
)) {
2202 // It takes 2 unicode characters to represent a 1 byte binary buffer.
2203 // If VLAN is configured, it will need extra 5 characters like "\0005".
2204 // Plus one unicode character for the null-terminator.
2206 String
= AllocateZeroPool ((2 * HwAddressSize
+ 5 + 1) * sizeof (CHAR16
));
2207 if (String
== NULL
) {
2208 return EFI_OUT_OF_RESOURCES
;
2210 *MacString
= String
;
2213 // Convert the MAC address into a unicode string.
2215 HwAddress
= &MacAddress
.Addr
[0];
2216 for (Index
= 0; Index
< HwAddressSize
; Index
++) {
2217 String
+= UnicodeValueToString (String
, PREFIX_ZERO
| RADIX_HEX
, *(HwAddress
++), 2);
2221 // Append VLAN ID if any
2223 VlanId
= NetLibGetVlanId (ServiceHandle
);
2226 String
+= UnicodeValueToString (String
, PREFIX_ZERO
| RADIX_HEX
, VlanId
, 4);
2230 // Null terminate the Unicode string
2238 Detect media status for specified network device.
2240 The underlying UNDI driver may or may not support reporting media status from
2241 GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine
2242 will try to invoke Snp->GetStatus() to get the media status: if media already
2243 present, it return directly; if media not present, it will stop SNP and then
2244 restart SNP to get the latest media status, this give chance to get the correct
2245 media status for old UNDI driver which doesn't support reporting media status
2246 from GET_STATUS command.
2247 Note: there will be two limitations for current algorithm:
2248 1) for UNDI with this capability, in case of cable is not attached, there will
2249 be an redundant Stop/Start() process;
2250 2) for UNDI without this capability, in case that network cable is attached when
2251 Snp->Initialize() is invoked while network cable is unattached later,
2252 NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer
2253 apps to wait for timeout time.
2255 @param[in] ServiceHandle The handle where network service binding protocols are
2257 @param[out] MediaPresent The pointer to store the media status.
2259 @retval EFI_SUCCESS Media detection success.
2260 @retval EFI_INVALID_PARAMETER ServiceHandle is not valid network device handle.
2261 @retval EFI_UNSUPPORTED Network device does not support media detection.
2262 @retval EFI_DEVICE_ERROR SNP is in unknown state.
2268 IN EFI_HANDLE ServiceHandle
,
2269 OUT BOOLEAN
*MediaPresent
2273 EFI_HANDLE SnpHandle
;
2274 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
2275 UINT32 InterruptStatus
;
2277 EFI_MAC_ADDRESS
*MCastFilter
;
2278 UINT32 MCastFilterCount
;
2279 UINT32 EnableFilterBits
;
2280 UINT32 DisableFilterBits
;
2281 BOOLEAN ResetMCastFilters
;
2283 ASSERT (MediaPresent
!= NULL
);
2289 SnpHandle
= NetLibGetSnpHandle (ServiceHandle
, &Snp
);
2290 if (SnpHandle
== NULL
) {
2291 return EFI_INVALID_PARAMETER
;
2295 // Check whether SNP support media detection
2297 if (!Snp
->Mode
->MediaPresentSupported
) {
2298 return EFI_UNSUPPORTED
;
2302 // Invoke Snp->GetStatus() to refresh MediaPresent field in SNP mode data
2304 Status
= Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
2305 if (EFI_ERROR (Status
)) {
2309 if (Snp
->Mode
->MediaPresent
) {
2311 // Media is present, return directly
2313 *MediaPresent
= TRUE
;
2318 // Till now, GetStatus() report no media; while, in case UNDI not support
2319 // reporting media status from GetStatus(), this media status may be incorrect.
2320 // So, we will stop SNP and then restart it to get the correct media status.
2322 OldState
= Snp
->Mode
->State
;
2323 if (OldState
>= EfiSimpleNetworkMaxState
) {
2324 return EFI_DEVICE_ERROR
;
2329 if (OldState
== EfiSimpleNetworkInitialized
) {
2331 // SNP is already in use, need Shutdown/Stop and then Start/Initialize
2335 // Backup current SNP receive filter settings
2337 EnableFilterBits
= Snp
->Mode
->ReceiveFilterSetting
;
2338 DisableFilterBits
= Snp
->Mode
->ReceiveFilterMask
^ EnableFilterBits
;
2340 ResetMCastFilters
= TRUE
;
2341 MCastFilterCount
= Snp
->Mode
->MCastFilterCount
;
2342 if (MCastFilterCount
!= 0) {
2343 MCastFilter
= AllocateCopyPool (
2344 MCastFilterCount
* sizeof (EFI_MAC_ADDRESS
),
2345 Snp
->Mode
->MCastFilter
2347 ASSERT (MCastFilter
!= NULL
);
2349 ResetMCastFilters
= FALSE
;
2353 // Shutdown/Stop the simple network
2355 Status
= Snp
->Shutdown (Snp
);
2356 if (!EFI_ERROR (Status
)) {
2357 Status
= Snp
->Stop (Snp
);
2359 if (EFI_ERROR (Status
)) {
2364 // Start/Initialize the simple network
2366 Status
= Snp
->Start (Snp
);
2367 if (!EFI_ERROR (Status
)) {
2368 Status
= Snp
->Initialize (Snp
, 0, 0);
2370 if (EFI_ERROR (Status
)) {
2375 // Here we get the correct media status
2377 *MediaPresent
= Snp
->Mode
->MediaPresent
;
2380 // Restore SNP receive filter settings
2382 Status
= Snp
->ReceiveFilters (
2391 if (MCastFilter
!= NULL
) {
2392 FreePool (MCastFilter
);
2399 // SNP is not in use, it's in state of EfiSimpleNetworkStopped or EfiSimpleNetworkStarted
2401 if (OldState
== EfiSimpleNetworkStopped
) {
2403 // SNP not start yet, start it
2405 Status
= Snp
->Start (Snp
);
2406 if (EFI_ERROR (Status
)) {
2412 // Initialize the simple network
2414 Status
= Snp
->Initialize (Snp
, 0, 0);
2415 if (EFI_ERROR (Status
)) {
2416 Status
= EFI_DEVICE_ERROR
;
2421 // Here we get the correct media status
2423 *MediaPresent
= Snp
->Mode
->MediaPresent
;
2426 // Shut down the simple network
2428 Snp
->Shutdown (Snp
);
2431 if (OldState
== EfiSimpleNetworkStopped
) {
2433 // Original SNP sate is Stopped, restore to original state
2438 if (MCastFilter
!= NULL
) {
2439 FreePool (MCastFilter
);
2446 Check the default address used by the IPv4 driver is static or dynamic (acquired
2449 If the controller handle does not have the NIC Ip4 Config Protocol installed, the
2450 default address is static. If the EFI variable to save the configuration is not found,
2451 the default address is static. Otherwise, get the result from the EFI variable which
2452 saving the configuration.
2454 @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol
2455 relative with the default address to judge.
2457 @retval TRUE If the default address is static.
2458 @retval FALSE If the default address is acquired from DHCP.
2462 NetLibDefaultAddressIsStatic (
2463 IN EFI_HANDLE Controller
2467 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
2469 NIC_IP4_CONFIG_INFO
*ConfigInfo
;
2471 EFI_STRING ConfigHdr
;
2472 EFI_STRING ConfigResp
;
2473 EFI_STRING AccessProgress
;
2474 EFI_STRING AccessResults
;
2476 EFI_HANDLE ChildHandle
;
2481 AccessProgress
= NULL
;
2482 AccessResults
= NULL
;
2485 Status
= gBS
->LocateProtocol (
2486 &gEfiHiiConfigRoutingProtocolGuid
,
2488 (VOID
**) &HiiConfigRouting
2490 if (EFI_ERROR (Status
)) {
2494 Status
= NetGetChildHandle (Controller
, &ChildHandle
);
2495 if (EFI_ERROR (Status
)) {
2500 // Construct config request string header
2502 ConfigHdr
= HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid
, EFI_NIC_IP4_CONFIG_VARIABLE
, ChildHandle
);
2503 if (ConfigHdr
== NULL
) {
2507 Len
= StrLen (ConfigHdr
);
2508 ConfigResp
= AllocateZeroPool ((Len
+ NIC_ITEM_CONFIG_SIZE
* 2 + 100) * sizeof (CHAR16
));
2509 if (ConfigResp
== NULL
) {
2512 StrCpy (ConfigResp
, ConfigHdr
);
2514 String
= ConfigResp
+ Len
;
2517 (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16
),
2518 L
"&OFFSET=%04X&WIDTH=%04X",
2519 OFFSET_OF (NIC_IP4_CONFIG_INFO
, Source
),
2523 Status
= HiiConfigRouting
->ExtractConfig (
2529 if (EFI_ERROR (Status
)) {
2533 ConfigInfo
= AllocateZeroPool (NIC_ITEM_CONFIG_SIZE
);
2534 if (ConfigInfo
== NULL
) {
2538 ConfigInfo
->Source
= IP4_CONFIG_SOURCE_STATIC
;
2539 Len
= NIC_ITEM_CONFIG_SIZE
;
2540 Status
= HiiConfigRouting
->ConfigToBlock (
2543 (UINT8
*) ConfigInfo
,
2547 if (EFI_ERROR (Status
)) {
2551 IsStatic
= (BOOLEAN
) (ConfigInfo
->Source
== IP4_CONFIG_SOURCE_STATIC
);
2555 if (AccessResults
!= NULL
) {
2556 FreePool (AccessResults
);
2558 if (ConfigInfo
!= NULL
) {
2559 FreePool (ConfigInfo
);
2561 if (ConfigResp
!= NULL
) {
2562 FreePool (ConfigResp
);
2564 if (ConfigHdr
!= NULL
) {
2565 FreePool (ConfigHdr
);
2572 Create an IPv4 device path node.
2574 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.
2575 The header subtype of IPv4 device path node is MSG_IPv4_DP.
2576 The length of the IPv4 device path node in bytes is 19.
2577 Get other info from parameters to make up the whole IPv4 device path node.
2579 @param[in, out] Node Pointer to the IPv4 device path node.
2580 @param[in] Controller The controller handle.
2581 @param[in] LocalIp The local IPv4 address.
2582 @param[in] LocalPort The local port.
2583 @param[in] RemoteIp The remote IPv4 address.
2584 @param[in] RemotePort The remote port.
2585 @param[in] Protocol The protocol type in the IP header.
2586 @param[in] UseDefaultAddress Whether this instance is using default address or not.
2591 NetLibCreateIPv4DPathNode (
2592 IN OUT IPv4_DEVICE_PATH
*Node
,
2593 IN EFI_HANDLE Controller
,
2594 IN IP4_ADDR LocalIp
,
2595 IN UINT16 LocalPort
,
2596 IN IP4_ADDR RemoteIp
,
2597 IN UINT16 RemotePort
,
2599 IN BOOLEAN UseDefaultAddress
2602 Node
->Header
.Type
= MESSAGING_DEVICE_PATH
;
2603 Node
->Header
.SubType
= MSG_IPv4_DP
;
2604 SetDevicePathNodeLength (&Node
->Header
, 19);
2606 CopyMem (&Node
->LocalIpAddress
, &LocalIp
, sizeof (EFI_IPv4_ADDRESS
));
2607 CopyMem (&Node
->RemoteIpAddress
, &RemoteIp
, sizeof (EFI_IPv4_ADDRESS
));
2609 Node
->LocalPort
= LocalPort
;
2610 Node
->RemotePort
= RemotePort
;
2612 Node
->Protocol
= Protocol
;
2614 if (!UseDefaultAddress
) {
2615 Node
->StaticIpAddress
= TRUE
;
2617 Node
->StaticIpAddress
= NetLibDefaultAddressIsStatic (Controller
);
2622 Create an IPv6 device path node.
2624 The header type of IPv6 device path node is MESSAGING_DEVICE_PATH.
2625 The header subtype of IPv6 device path node is MSG_IPv6_DP.
2626 Get other info from parameters to make up the whole IPv6 device path node.
2628 @param[in, out] Node Pointer to the IPv6 device path node.
2629 @param[in] Controller The controller handle.
2630 @param[in] LocalIp The local IPv6 address.
2631 @param[in] LocalPort The local port.
2632 @param[in] RemoteIp The remote IPv6 address.
2633 @param[in] RemotePort The remote port.
2634 @param[in] Protocol The protocol type in the IP header.
2639 NetLibCreateIPv6DPathNode (
2640 IN OUT IPv6_DEVICE_PATH
*Node
,
2641 IN EFI_HANDLE Controller
,
2642 IN EFI_IPv6_ADDRESS
*LocalIp
,
2643 IN UINT16 LocalPort
,
2644 IN EFI_IPv6_ADDRESS
*RemoteIp
,
2645 IN UINT16 RemotePort
,
2649 Node
->Header
.Type
= MESSAGING_DEVICE_PATH
;
2650 Node
->Header
.SubType
= MSG_IPv6_DP
;
2651 SetDevicePathNodeLength (&Node
->Header
, sizeof (IPv6_DEVICE_PATH
));
2653 CopyMem (&Node
->LocalIpAddress
, LocalIp
, sizeof (EFI_IPv6_ADDRESS
));
2654 CopyMem (&Node
->RemoteIpAddress
, RemoteIp
, sizeof (EFI_IPv6_ADDRESS
));
2656 Node
->LocalPort
= LocalPort
;
2657 Node
->RemotePort
= RemotePort
;
2659 Node
->Protocol
= Protocol
;
2660 Node
->StaticIpAddress
= FALSE
;
2664 Find the UNDI/SNP handle from controller and protocol GUID.
2666 For example, IP will open a MNP child to transmit/receive
2667 packets, when MNP is stopped, IP should also be stopped. IP
2668 needs to find its own private data which is related the IP's
2669 service binding instance that is install on UNDI/SNP handle.
2670 Now, the controller is either a MNP or ARP child handle. But
2671 IP opens these handle BY_DRIVER, use that info, we can get the
2674 @param[in] Controller Then protocol handle to check.
2675 @param[in] ProtocolGuid The protocol that is related with the handle.
2677 @return The UNDI/SNP handle or NULL for errors.
2682 NetLibGetNicHandle (
2683 IN EFI_HANDLE Controller
,
2684 IN EFI_GUID
*ProtocolGuid
2687 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenBuffer
;
2693 Status
= gBS
->OpenProtocolInformation (
2700 if (EFI_ERROR (Status
)) {
2706 for (Index
= 0; Index
< OpenCount
; Index
++) {
2707 if ((OpenBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) != 0) {
2708 Handle
= OpenBuffer
[Index
].ControllerHandle
;
2713 gBS
->FreePool (OpenBuffer
);
2718 Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS.
2720 @param[in] String The pointer to the Ascii string.
2721 @param[out] Ip4Address The pointer to the converted IPv4 address.
2723 @retval EFI_SUCCESS Convert to IPv4 address successfully.
2724 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.
2729 NetLibAsciiStrToIp4 (
2730 IN CONST CHAR8
*String
,
2731 OUT EFI_IPv4_ADDRESS
*Ip4Address
2739 if ((String
== NULL
) || (Ip4Address
== NULL
)) {
2740 return EFI_INVALID_PARAMETER
;
2743 Ip4Str
= (CHAR8
*) String
;
2745 for (Index
= 0; Index
< 4; Index
++) {
2748 while ((*Ip4Str
!= '\0') && (*Ip4Str
!= '.')) {
2753 // The IPv4 address is X.X.X.X
2755 if (*Ip4Str
== '.') {
2757 return EFI_INVALID_PARAMETER
;
2761 return EFI_INVALID_PARAMETER
;
2766 // Convert the string to IPv4 address. AsciiStrDecimalToUintn stops at the
2767 // first character that is not a valid decimal character, '.' or '\0' here.
2769 NodeVal
= AsciiStrDecimalToUintn (TempStr
);
2770 if (NodeVal
> 0xFF) {
2771 return EFI_INVALID_PARAMETER
;
2774 Ip4Address
->Addr
[Index
] = (UINT8
) NodeVal
;
2784 Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of the
2785 string is defined in RFC 4291 - Text Pepresentation of Addresses.
2787 @param[in] String The pointer to the Ascii string.
2788 @param[out] Ip6Address The pointer to the converted IPv6 address.
2790 @retval EFI_SUCCESS Convert to IPv6 address successfully.
2791 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
2796 NetLibAsciiStrToIp6 (
2797 IN CONST CHAR8
*String
,
2798 OUT EFI_IPv6_ADDRESS
*Ip6Address
2815 if ((String
== NULL
) || (Ip6Address
== NULL
)) {
2816 return EFI_INVALID_PARAMETER
;
2819 Ip6Str
= (CHAR8
*) String
;
2824 // An IPv6 address leading with : looks strange.
2826 if (*Ip6Str
== ':') {
2827 if (*(Ip6Str
+ 1) != ':') {
2828 return EFI_INVALID_PARAMETER
;
2834 ZeroMem (Ip6Address
, sizeof (EFI_IPv6_ADDRESS
));
2842 for (Index
= 0; Index
< 15; Index
= (UINT8
) (Index
+ 2)) {
2845 while ((*Ip6Str
!= '\0') && (*Ip6Str
!= ':')) {
2849 if ((*Ip6Str
== '\0') && (Index
!= 14)) {
2850 return EFI_INVALID_PARAMETER
;
2853 if (*Ip6Str
== ':') {
2854 if (*(Ip6Str
+ 1) == ':') {
2855 if ((NodeCnt
> 6) ||
2856 ((*(Ip6Str
+ 2) != '\0') && (AsciiStrHexToUintn (Ip6Str
+ 2) == 0))) {
2858 // ::0 looks strange. report error to user.
2860 return EFI_INVALID_PARAMETER
;
2862 if ((NodeCnt
== 6) && (*(Ip6Str
+ 2) != '\0') &&
2863 (AsciiStrHexToUintn (Ip6Str
+ 2) != 0)) {
2864 return EFI_INVALID_PARAMETER
;
2868 // Skip the abbreviation part of IPv6 address.
2870 TempStr2
= Ip6Str
+ 2;
2871 while ((*TempStr2
!= '\0')) {
2872 if (*TempStr2
== ':') {
2873 if (*(TempStr2
+ 1) == ':') {
2875 // :: can only appear once in IPv6 address.
2877 return EFI_INVALID_PARAMETER
;
2881 if (TailNodeCnt
>= (AllowedCnt
- NodeCnt
)) {
2883 // :: indicates one or more groups of 16 bits of zeros.
2885 return EFI_INVALID_PARAMETER
;
2895 Ip6Str
= Ip6Str
+ 2;
2897 if (*(Ip6Str
+ 1) == '\0') {
2898 return EFI_INVALID_PARAMETER
;
2902 if ((Short
&& (NodeCnt
> 6)) || (!Short
&& (NodeCnt
> 7))) {
2904 // There are more than 8 groups of 16 bits of zeros.
2906 return EFI_INVALID_PARAMETER
;
2912 // Convert the string to IPv6 address. AsciiStrHexToUintn stops at the first
2913 // character that is not a valid hexadecimal character, ':' or '\0' here.
2915 NodeVal
= AsciiStrHexToUintn (TempStr
);
2916 if ((NodeVal
> 0xFFFF) || (Index
> 14)) {
2917 return EFI_INVALID_PARAMETER
;
2920 if ((*TempStr
== '0') &&
2921 ((*(TempStr
+ 2) == ':') || (*(TempStr
+ 3) == ':') ||
2922 (*(TempStr
+ 2) == '\0') || (*(TempStr
+ 3) == '\0'))) {
2923 return EFI_INVALID_PARAMETER
;
2925 if ((*TempStr
== '0') && (*(TempStr
+ 4) != '\0') &&
2926 (*(TempStr
+ 4) != ':')) {
2927 return EFI_INVALID_PARAMETER
;
2930 if (((*TempStr
== '0') && (*(TempStr
+ 1) == '0') &&
2931 ((*(TempStr
+ 2) == ':') || (*(TempStr
+ 2) == '\0'))) ||
2932 ((*TempStr
== '0') && (*(TempStr
+ 1) == '0') && (*(TempStr
+ 2) == '0') &&
2933 ((*(TempStr
+ 3) == ':') || (*(TempStr
+ 3) == '\0')))) {
2934 return EFI_INVALID_PARAMETER
;
2939 while ((TempStr
[Cnt
] != ':') && (TempStr
[Cnt
] != '\0')) {
2942 if (LeadZeroCnt
== 0) {
2943 if ((Cnt
== 4) && (*TempStr
== '0')) {
2947 if ((Cnt
!= 0) && (Cnt
< 4)) {
2952 if ((Cnt
== 4) && (*TempStr
== '0') && (LeadZero
== FALSE
)) {
2953 return EFI_INVALID_PARAMETER
;
2955 if ((Cnt
!= 0) && (Cnt
< 4) && (LeadZero
== TRUE
)) {
2956 return EFI_INVALID_PARAMETER
;
2960 Ip6Address
->Addr
[Index
] = (UINT8
) (NodeVal
>> 8);
2961 Ip6Address
->Addr
[Index
+ 1] = (UINT8
) (NodeVal
& 0xFF);
2964 // Skip the groups of zeros by ::
2966 if (Short
&& Update
) {
2967 Index
= (UINT8
) (16 - (TailNodeCnt
+ 2) * 2);
2972 if ((!Short
&& Index
!= 16) || (*Ip6Str
!= '\0')) {
2973 return EFI_INVALID_PARAMETER
;
2981 Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS.
2983 @param[in] String The pointer to the Ascii string.
2984 @param[out] Ip4Address The pointer to the converted IPv4 address.
2986 @retval EFI_SUCCESS Convert to IPv4 address successfully.
2987 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.
2988 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.
2994 IN CONST CHAR16
*String
,
2995 OUT EFI_IPv4_ADDRESS
*Ip4Address
3001 if ((String
== NULL
) || (Ip4Address
== NULL
)) {
3002 return EFI_INVALID_PARAMETER
;
3005 Ip4Str
= (CHAR8
*) AllocatePool ((StrLen (String
) + 1) * sizeof (CHAR8
));
3006 if (Ip4Str
== NULL
) {
3007 return EFI_OUT_OF_RESOURCES
;
3010 UnicodeStrToAsciiStr (String
, Ip4Str
);
3012 Status
= NetLibAsciiStrToIp4 (Ip4Str
, Ip4Address
);
3021 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of
3022 the string is defined in RFC 4291 - Text Pepresentation of Addresses.
3024 @param[in] String The pointer to the Ascii string.
3025 @param[out] Ip6Address The pointer to the converted IPv6 address.
3027 @retval EFI_SUCCESS Convert to IPv6 address successfully.
3028 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
3029 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.
3035 IN CONST CHAR16
*String
,
3036 OUT EFI_IPv6_ADDRESS
*Ip6Address
3042 if ((String
== NULL
) || (Ip6Address
== NULL
)) {
3043 return EFI_INVALID_PARAMETER
;
3046 Ip6Str
= (CHAR8
*) AllocatePool ((StrLen (String
) + 1) * sizeof (CHAR8
));
3047 if (Ip6Str
== NULL
) {
3048 return EFI_OUT_OF_RESOURCES
;
3051 UnicodeStrToAsciiStr (String
, Ip6Str
);
3053 Status
= NetLibAsciiStrToIp6 (Ip6Str
, Ip6Address
);
3061 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length.
3062 The format of the string is defined in RFC 4291 - Text Pepresentation of Addresses
3063 Prefixes: ipv6-address/prefix-length.
3065 @param[in] String The pointer to the Ascii string.
3066 @param[out] Ip6Address The pointer to the converted IPv6 address.
3067 @param[out] PrefixLength The pointer to the converted prefix length.
3069 @retval EFI_SUCCESS Convert to IPv6 address successfully.
3070 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
3071 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.
3076 NetLibStrToIp6andPrefix (
3077 IN CONST CHAR16
*String
,
3078 OUT EFI_IPv6_ADDRESS
*Ip6Address
,
3079 OUT UINT8
*PrefixLength
3088 if ((String
== NULL
) || (Ip6Address
== NULL
) || (PrefixLength
== NULL
)) {
3089 return EFI_INVALID_PARAMETER
;
3092 Ip6Str
= (CHAR8
*) AllocatePool ((StrLen (String
) + 1) * sizeof (CHAR8
));
3093 if (Ip6Str
== NULL
) {
3094 return EFI_OUT_OF_RESOURCES
;
3097 UnicodeStrToAsciiStr (String
, Ip6Str
);
3100 // Get the sub string describing prefix length.
3103 while (*TempStr
!= '\0' && (*TempStr
!= '/')) {
3107 if (*TempStr
== '/') {
3108 PrefixStr
= TempStr
+ 1;
3114 // Get the sub string describing IPv6 address and convert it.
3118 Status
= NetLibAsciiStrToIp6 (Ip6Str
, Ip6Address
);
3119 if (EFI_ERROR (Status
)) {
3124 // If input string doesn't indicate the prefix length, return 0xff.
3129 // Convert the string to prefix length
3131 if (PrefixStr
!= NULL
) {
3133 Status
= EFI_INVALID_PARAMETER
;
3135 while (*PrefixStr
!= '\0') {
3136 if (NET_IS_DIGIT (*PrefixStr
)) {
3137 Length
= (UINT8
) (Length
* 10 + (*PrefixStr
- '0'));
3138 if (Length
>= IP6_PREFIX_NUM
) {
3149 *PrefixLength
= Length
;
3150 Status
= EFI_SUCCESS
;