4 Copyright (c) 2005 - 2011, 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 <IndustryStandard/SmBios.h>
18 #include <Protocol/DriverBinding.h>
19 #include <Protocol/ServiceBinding.h>
20 #include <Protocol/SimpleNetwork.h>
21 #include <Protocol/ManagedNetwork.h>
22 #include <Protocol/HiiConfigRouting.h>
23 #include <Protocol/ComponentName.h>
24 #include <Protocol/ComponentName2.h>
25 #include <Protocol/HiiConfigAccess.h>
27 #include <Guid/NicIp4ConfigNvData.h>
28 #include <Guid/SmBios.h>
30 #include <Library/NetLib.h>
31 #include <Library/BaseLib.h>
32 #include <Library/DebugLib.h>
33 #include <Library/BaseMemoryLib.h>
34 #include <Library/UefiBootServicesTableLib.h>
35 #include <Library/UefiRuntimeServicesTableLib.h>
36 #include <Library/MemoryAllocationLib.h>
37 #include <Library/DevicePathLib.h>
38 #include <Library/HiiLib.h>
39 #include <Library/PrintLib.h>
40 #include <Library/UefiLib.h>
42 #define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE
45 // All the supported IP4 maskes in host byte order.
47 GLOBAL_REMOVE_IF_UNREFERENCED IP4_ADDR gIp4AllMasks
[IP4_MASK_NUM
] = {
86 GLOBAL_REMOVE_IF_UNREFERENCED EFI_IPv4_ADDRESS mZeroIp4Addr
= {{0, 0, 0, 0}};
89 // Any error level digitally larger than mNetDebugLevelMax
90 // will be silently discarded.
92 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mNetDebugLevelMax
= NETDEBUG_LEVEL_ERROR
;
93 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogPacketSeq
= 0xDEADBEEF;
96 // You can change mSyslogDstMac mSyslogDstIp and mSyslogSrcIp
97 // here to direct the syslog packets to the syslog deamon. The
98 // default is broadcast to both the ethernet and IP.
100 GLOBAL_REMOVE_IF_UNREFERENCED UINT8 mSyslogDstMac
[NET_ETHER_ADDR_LEN
] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
101 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogDstIp
= 0xffffffff;
102 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogSrcIp
= 0;
104 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8
*mMonthName
[] = {
120 // VLAN device path node template
122 GLOBAL_REMOVE_IF_UNREFERENCED VLAN_DEVICE_PATH mNetVlanDevicePathTemplate
= {
124 MESSAGING_DEVICE_PATH
,
127 (UINT8
) (sizeof (VLAN_DEVICE_PATH
)),
128 (UINT8
) ((sizeof (VLAN_DEVICE_PATH
)) >> 8)
135 Locate the handles that support SNP, then open one of them
136 to send the syslog packets. The caller isn't required to close
137 the SNP after use because the SNP is opened by HandleProtocol.
139 @return The point to SNP if one is properly openned. Otherwise NULL
142 EFI_SIMPLE_NETWORK_PROTOCOL
*
147 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
154 // Locate the handles which has SNP installed.
157 Status
= gBS
->LocateHandleBuffer (
159 &gEfiSimpleNetworkProtocolGuid
,
165 if (EFI_ERROR (Status
) || (HandleCount
== 0)) {
170 // Try to open one of the ethernet SNP protocol to send packet
174 for (Index
= 0; Index
< HandleCount
; Index
++) {
175 Status
= gBS
->HandleProtocol (
177 &gEfiSimpleNetworkProtocolGuid
,
181 if ((Status
== EFI_SUCCESS
) && (Snp
!= NULL
) &&
182 (Snp
->Mode
->IfType
== NET_IFTYPE_ETHERNET
) &&
183 (Snp
->Mode
->MaxPacketSize
>= NET_SYSLOG_PACKET_LEN
)) {
196 Transmit a syslog packet synchronously through SNP. The Packet
197 already has the ethernet header prepended. This function should
198 fill in the source MAC because it will try to locate a SNP each
199 time it is called to avoid the problem if SNP is unloaded.
200 This code snip is copied from MNP.
202 @param[in] Packet The Syslog packet
203 @param[in] Length The length of the packet
205 @retval EFI_DEVICE_ERROR Failed to locate a usable SNP protocol
206 @retval EFI_TIMEOUT Timeout happened to send the packet.
207 @retval EFI_SUCCESS Packet is sent.
216 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
219 EFI_EVENT TimeoutEvent
;
222 Snp
= SyslogLocateSnp ();
225 return EFI_DEVICE_ERROR
;
228 Ether
= (ETHER_HEAD
*) Packet
;
229 CopyMem (Ether
->SrcMac
, Snp
->Mode
->CurrentAddress
.Addr
, NET_ETHER_ADDR_LEN
);
232 // Start the timeout event.
234 Status
= gBS
->CreateEvent (
242 if (EFI_ERROR (Status
)) {
246 Status
= gBS
->SetTimer (TimeoutEvent
, TimerRelative
, NET_SYSLOG_TX_TIMEOUT
);
248 if (EFI_ERROR (Status
)) {
254 // Transmit the packet through SNP.
256 Status
= Snp
->Transmit (Snp
, 0, Length
, Packet
, NULL
, NULL
, NULL
);
258 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_READY
)) {
259 Status
= EFI_DEVICE_ERROR
;
264 // If Status is EFI_SUCCESS, the packet is put in the transmit queue.
265 // if Status is EFI_NOT_READY, the transmit engine of the network
266 // interface is busy. Both need to sync SNP.
272 // Get the recycled transmit buffer status.
274 Snp
->GetStatus (Snp
, NULL
, (VOID
**) &TxBuf
);
276 if (!EFI_ERROR (gBS
->CheckEvent (TimeoutEvent
))) {
277 Status
= EFI_TIMEOUT
;
281 } while (TxBuf
== NULL
);
283 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_TIMEOUT
)) {
288 // Status is EFI_NOT_READY. Restart the timer event and
289 // call Snp->Transmit again.
291 gBS
->SetTimer (TimeoutEvent
, TimerRelative
, NET_SYSLOG_TX_TIMEOUT
);
294 gBS
->SetTimer (TimeoutEvent
, TimerCancel
, 0);
297 gBS
->CloseEvent (TimeoutEvent
);
302 Build a syslog packet, including the Ethernet/Ip/Udp headers
305 @param[in] Level Syslog servity level
306 @param[in] Module The module that generates the log
307 @param[in] File The file that contains the current log
308 @param[in] Line The line of code in the File that contains the current log
309 @param[in] Message The log message
310 @param[in] BufLen The lenght of the Buf
311 @param[out] Buf The buffer to put the packet data
313 @return The length of the syslog packet built.
329 EFI_UDP_HEADER
*Udp4
;
335 // Fill in the Ethernet header. Leave alone the source MAC.
336 // SyslogSendPacket will fill in the address for us.
338 Ether
= (ETHER_HEAD
*) Buf
;
339 CopyMem (Ether
->DstMac
, mSyslogDstMac
, NET_ETHER_ADDR_LEN
);
340 ZeroMem (Ether
->SrcMac
, NET_ETHER_ADDR_LEN
);
342 Ether
->EtherType
= HTONS (0x0800); // IPv4 protocol
344 Buf
+= sizeof (ETHER_HEAD
);
345 BufLen
-= sizeof (ETHER_HEAD
);
348 // Fill in the IP header
350 Ip4
= (IP4_HEAD
*) Buf
;
355 Ip4
->Id
= (UINT16
) mSyslogPacketSeq
;
358 Ip4
->Protocol
= 0x11;
360 Ip4
->Src
= mSyslogSrcIp
;
361 Ip4
->Dst
= mSyslogDstIp
;
363 Buf
+= sizeof (IP4_HEAD
);
364 BufLen
-= sizeof (IP4_HEAD
);
367 // Fill in the UDP header, Udp checksum is optional. Leave it zero.
369 Udp4
= (EFI_UDP_HEADER
*) Buf
;
370 Udp4
->SrcPort
= HTONS (514);
371 Udp4
->DstPort
= HTONS (514);
375 Buf
+= sizeof (EFI_UDP_HEADER
);
376 BufLen
-= sizeof (EFI_UDP_HEADER
);
379 // Build the syslog message body with <PRI> Timestamp machine module Message
381 Pri
= ((NET_SYSLOG_FACILITY
& 31) << 3) | (Level
& 7);
382 gRT
->GetTime (&Time
, NULL
);
383 ASSERT ((Time
.Month
<= 12) && (Time
.Month
>= 1));
386 // Use %a to format the ASCII strings, %s to format UNICODE strings
389 Len
+= (UINT32
) AsciiSPrint (
392 "<%d> %a %d %d:%d:%d ",
394 mMonthName
[Time
.Month
-1],
402 Len
+= (UINT32
) AsciiSPrint (
405 "Tiano %a: %a (Line: %d File: %a)",
414 // OK, patch the IP length/checksum and UDP length fields.
416 Len
+= sizeof (EFI_UDP_HEADER
);
417 Udp4
->Length
= HTONS ((UINT16
) Len
);
419 Len
+= sizeof (IP4_HEAD
);
420 Ip4
->TotalLen
= HTONS ((UINT16
) Len
);
421 Ip4
->Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Ip4
, sizeof (IP4_HEAD
)));
423 return Len
+ sizeof (ETHER_HEAD
);
427 Allocate a buffer, then format the message to it. This is a
428 help function for the NET_DEBUG_XXX macros. The PrintArg of
429 these macros treats the variable length print parameters as a
430 single parameter, and pass it to the NetDebugASPrint. For
431 example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name))
435 NETDEBUG_LEVEL_TRACE,
439 NetDebugASPrint ("State transit to %a\n", Name)
442 @param Format The ASCII format string.
443 @param ... The variable length parameter whose format is determined
444 by the Format string.
446 @return The buffer containing the formatted message,
447 or NULL if failed to allocate memory.
460 Buf
= (CHAR8
*) AllocatePool (NET_DEBUG_MSG_LEN
);
466 VA_START (Marker
, Format
);
467 AsciiVSPrint (Buf
, NET_DEBUG_MSG_LEN
, Format
, Marker
);
474 Builds an UDP4 syslog packet and send it using SNP.
476 This function will locate a instance of SNP then send the message through it.
477 Because it isn't open the SNP BY_DRIVER, apply caution when using it.
479 @param Level The servity level of the message.
480 @param Module The Moudle that generates the log.
481 @param File The file that contains the log.
482 @param Line The exact line that contains the log.
483 @param Message The user message to log.
485 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
486 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet
487 @retval EFI_SUCCESS The log is discard because that it is more verbose
488 than the mNetDebugLevelMax. Or, it has been sent out.
505 // Check whether the message should be sent out
507 if (Message
== NULL
) {
508 return EFI_INVALID_PARAMETER
;
511 if (Level
> mNetDebugLevelMax
) {
512 Status
= EFI_SUCCESS
;
517 // Allocate a maxium of 1024 bytes, the caller should ensure
518 // that the message plus the ethernet/ip/udp header is shorter
521 Packet
= (CHAR8
*) AllocatePool (NET_SYSLOG_PACKET_LEN
);
523 if (Packet
== NULL
) {
524 Status
= EFI_OUT_OF_RESOURCES
;
529 // Build the message: Ethernet header + IP header + Udp Header + user data
531 Len
= SyslogBuildPacket (
537 NET_SYSLOG_PACKET_LEN
,
542 Status
= SyslogSendPacket (Packet
, Len
);
550 Return the length of the mask.
552 Return the length of the mask, the correct value is from 0 to 32.
553 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.
554 NetMask is in the host byte order.
556 @param[in] NetMask The netmask to get the length from.
558 @return The length of the netmask, IP4_MASK_NUM if the mask is invalid.
569 for (Index
= 0; Index
< IP4_MASK_NUM
; Index
++) {
570 if (NetMask
== gIp4AllMasks
[Index
]) {
581 Return the class of the IP address, such as class A, B, C.
582 Addr is in host byte order.
584 The address of class A starts with 0.
585 If the address belong to class A, return IP4_ADDR_CLASSA.
586 The address of class B starts with 10.
587 If the address belong to class B, return IP4_ADDR_CLASSB.
588 The address of class C starts with 110.
589 If the address belong to class C, return IP4_ADDR_CLASSC.
590 The address of class D starts with 1110.
591 If the address belong to class D, return IP4_ADDR_CLASSD.
592 The address of class E starts with 1111.
593 If the address belong to class E, return IP4_ADDR_CLASSE.
596 @param[in] Addr The address to get the class from.
598 @return IP address class, such as IP4_ADDR_CLASSA.
609 ByteOne
= (UINT8
) (Addr
>> 24);
611 if ((ByteOne
& 0x80) == 0) {
612 return IP4_ADDR_CLASSA
;
614 } else if ((ByteOne
& 0xC0) == 0x80) {
615 return IP4_ADDR_CLASSB
;
617 } else if ((ByteOne
& 0xE0) == 0xC0) {
618 return IP4_ADDR_CLASSC
;
620 } else if ((ByteOne
& 0xF0) == 0xE0) {
621 return IP4_ADDR_CLASSD
;
624 return IP4_ADDR_CLASSE
;
631 Check whether the IP is a valid unicast address according to
632 the netmask. If NetMask is zero, use the IP address's class to get the default mask.
634 If Ip is 0, IP is not a valid unicast address.
635 Class D address is used for multicasting and class E address is reserved for future. If Ip
636 belongs to class D or class E, IP is not a valid unicast address.
637 If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address.
639 @param[in] Ip The IP to check against.
640 @param[in] NetMask The mask of the IP.
642 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.
654 Class
= NetGetIpClass (Ip
);
656 if ((Ip
== 0) || (Class
>= IP4_ADDR_CLASSD
)) {
661 NetMask
= gIp4AllMasks
[Class
<< 3];
664 if (((Ip
&~NetMask
) == ~NetMask
) || ((Ip
&~NetMask
) == 0)) {
672 Check whether the incoming IPv6 address is a valid unicast address.
674 If the address is a multicast address has binary 0xFF at the start, it is not
675 a valid unicast address. If the address is unspecified ::, it is not a valid
676 unicast address to be assigned to any node. If the address is loopback address
677 ::1, it is also not a valid unicast address to be assigned to any physical
680 @param[in] Ip6 The IPv6 address to check against.
682 @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE.
687 NetIp6IsValidUnicast (
688 IN EFI_IPv6_ADDRESS
*Ip6
694 if (Ip6
->Addr
[0] == 0xFF) {
698 for (Index
= 0; Index
< 15; Index
++) {
699 if (Ip6
->Addr
[Index
] != 0) {
704 Byte
= Ip6
->Addr
[Index
];
706 if (Byte
== 0x0 || Byte
== 0x1) {
714 Check whether the incoming Ipv6 address is the unspecified address or not.
716 @param[in] Ip6 - Ip6 address, in network order.
718 @retval TRUE - Yes, unspecified
724 NetIp6IsUnspecifiedAddr (
725 IN EFI_IPv6_ADDRESS
*Ip6
730 for (Index
= 0; Index
< 16; Index
++) {
731 if (Ip6
->Addr
[Index
] != 0) {
740 Check whether the incoming Ipv6 address is a link-local address.
742 @param[in] Ip6 - Ip6 address, in network order.
744 @retval TRUE - Yes, link-local address
750 NetIp6IsLinkLocalAddr (
751 IN EFI_IPv6_ADDRESS
*Ip6
756 ASSERT (Ip6
!= NULL
);
758 if (Ip6
->Addr
[0] != 0xFE) {
762 if (Ip6
->Addr
[1] != 0x80) {
766 for (Index
= 2; Index
< 8; Index
++) {
767 if (Ip6
->Addr
[Index
] != 0) {
776 Check whether the Ipv6 address1 and address2 are on the connected network.
778 @param[in] Ip1 - Ip6 address1, in network order.
779 @param[in] Ip2 - Ip6 address2, in network order.
780 @param[in] PrefixLength - The prefix length of the checking net.
782 @retval TRUE - Yes, connected.
789 EFI_IPv6_ADDRESS
*Ip1
,
790 EFI_IPv6_ADDRESS
*Ip2
,
798 ASSERT ((Ip1
!= NULL
) && (Ip2
!= NULL
) && (PrefixLength
< IP6_PREFIX_NUM
));
800 if (PrefixLength
== 0) {
804 Byte
= (UINT8
) (PrefixLength
/ 8);
805 Bit
= (UINT8
) (PrefixLength
% 8);
807 if (CompareMem (Ip1
, Ip2
, Byte
) != 0) {
812 Mask
= (UINT8
) (0xFF << (8 - Bit
));
815 if ((Ip1
->Addr
[Byte
] & Mask
) != (Ip2
->Addr
[Byte
] & Mask
)) {
825 Switches the endianess of an IPv6 address
827 This function swaps the bytes in a 128-bit IPv6 address to switch the value
828 from little endian to big endian or vice versa. The byte swapped value is
831 @param Ip6 Points to an IPv6 address
833 @return The byte swapped IPv6 address.
839 EFI_IPv6_ADDRESS
*Ip6
845 CopyMem (&High
, Ip6
, sizeof (UINT64
));
846 CopyMem (&Low
, &Ip6
->Addr
[8], sizeof (UINT64
));
848 High
= SwapBytes64 (High
);
849 Low
= SwapBytes64 (Low
);
851 CopyMem (Ip6
, &Low
, sizeof (UINT64
));
852 CopyMem (&Ip6
->Addr
[8], &High
, sizeof (UINT64
));
858 Initialize a random seed using current time.
860 Get current time first. Then initialize a random seed based on some basic
861 mathematics operation on the hour, day, minute, second, nanosecond and year
864 @return The random seed initialized with current time.
876 gRT
->GetTime (&Time
, NULL
);
877 Seed
= (~Time
.Hour
<< 24 | Time
.Day
<< 16 | Time
.Minute
<< 8 | Time
.Second
);
878 Seed
^= Time
.Nanosecond
;
879 Seed
^= Time
.Year
<< 7;
886 Extract a UINT32 from a byte stream.
888 Copy a UINT32 from a byte stream, then converts it from Network
889 byte order to host byte order. Use this function to avoid alignment error.
891 @param[in] Buf The buffer to extract the UINT32.
893 @return The UINT32 extracted.
904 CopyMem (&Value
, Buf
, sizeof (UINT32
));
905 return NTOHL (Value
);
910 Put a UINT32 to the byte stream in network byte order.
912 Converts a UINT32 from host byte order to network byte order. Then copy it to the
915 @param[in, out] Buf The buffer to put the UINT32.
916 @param[in] Data The data to be converted and put into the byte stream.
927 CopyMem (Buf
, &Data
, sizeof (UINT32
));
932 Remove the first node entry on the list, and return the removed node entry.
934 Removes the first node Entry from a doubly linked list. It is up to the caller of
935 this function to release the memory used by the first node if that is required. On
936 exit, the removed node is returned.
938 If Head is NULL, then ASSERT().
939 If Head was not initialized, then ASSERT().
940 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
941 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
944 @param[in, out] Head The list header.
946 @return The first node entry that is removed from the list, NULL if the list is empty.
952 IN OUT LIST_ENTRY
*Head
957 ASSERT (Head
!= NULL
);
959 if (IsListEmpty (Head
)) {
963 First
= Head
->ForwardLink
;
964 Head
->ForwardLink
= First
->ForwardLink
;
965 First
->ForwardLink
->BackLink
= Head
;
968 First
->ForwardLink
= (LIST_ENTRY
*) NULL
;
969 First
->BackLink
= (LIST_ENTRY
*) NULL
;
977 Remove the last node entry on the list and and return the removed node entry.
979 Removes the last node entry from a doubly linked list. It is up to the caller of
980 this function to release the memory used by the first node if that is required. On
981 exit, the removed node is returned.
983 If Head is NULL, then ASSERT().
984 If Head was not initialized, then ASSERT().
985 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
986 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
989 @param[in, out] Head The list head.
991 @return The last node entry that is removed from the list, NULL if the list is empty.
997 IN OUT LIST_ENTRY
*Head
1002 ASSERT (Head
!= NULL
);
1004 if (IsListEmpty (Head
)) {
1008 Last
= Head
->BackLink
;
1009 Head
->BackLink
= Last
->BackLink
;
1010 Last
->BackLink
->ForwardLink
= Head
;
1013 Last
->ForwardLink
= (LIST_ENTRY
*) NULL
;
1014 Last
->BackLink
= (LIST_ENTRY
*) NULL
;
1022 Insert a new node entry after a designated node entry of a doubly linked list.
1024 Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry
1025 of the doubly linked list.
1027 @param[in, out] PrevEntry The previous entry to insert after.
1028 @param[in, out] NewEntry The new entry to insert.
1033 NetListInsertAfter (
1034 IN OUT LIST_ENTRY
*PrevEntry
,
1035 IN OUT LIST_ENTRY
*NewEntry
1038 NewEntry
->BackLink
= PrevEntry
;
1039 NewEntry
->ForwardLink
= PrevEntry
->ForwardLink
;
1040 PrevEntry
->ForwardLink
->BackLink
= NewEntry
;
1041 PrevEntry
->ForwardLink
= NewEntry
;
1046 Insert a new node entry before a designated node entry of a doubly linked list.
1048 Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry
1049 of the doubly linked list.
1051 @param[in, out] PostEntry The entry to insert before.
1052 @param[in, out] NewEntry The new entry to insert.
1057 NetListInsertBefore (
1058 IN OUT LIST_ENTRY
*PostEntry
,
1059 IN OUT LIST_ENTRY
*NewEntry
1062 NewEntry
->ForwardLink
= PostEntry
;
1063 NewEntry
->BackLink
= PostEntry
->BackLink
;
1064 PostEntry
->BackLink
->ForwardLink
= NewEntry
;
1065 PostEntry
->BackLink
= NewEntry
;
1070 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
1072 Initialize the forward and backward links of two head nodes donated by Map->Used
1073 and Map->Recycled of two doubly linked lists.
1074 Initializes the count of the <Key, Value> pairs in the netmap to zero.
1076 If Map is NULL, then ASSERT().
1077 If the address of Map->Used is NULL, then ASSERT().
1078 If the address of Map->Recycled is NULl, then ASSERT().
1080 @param[in, out] Map The netmap to initialize.
1089 ASSERT (Map
!= NULL
);
1091 InitializeListHead (&Map
->Used
);
1092 InitializeListHead (&Map
->Recycled
);
1098 To clean up the netmap, that is, release allocated memories.
1100 Removes all nodes of the Used doubly linked list and free memory of all related netmap items.
1101 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.
1102 The number of the <Key, Value> pairs in the netmap is set to be zero.
1104 If Map is NULL, then ASSERT().
1106 @param[in, out] Map The netmap to clean up.
1119 ASSERT (Map
!= NULL
);
1121 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Map
->Used
) {
1122 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1124 RemoveEntryList (&Item
->Link
);
1127 gBS
->FreePool (Item
);
1130 ASSERT ((Map
->Count
== 0) && IsListEmpty (&Map
->Used
));
1132 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Map
->Recycled
) {
1133 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1135 RemoveEntryList (&Item
->Link
);
1136 gBS
->FreePool (Item
);
1139 ASSERT (IsListEmpty (&Map
->Recycled
));
1144 Test whether the netmap is empty and return true if it is.
1146 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.
1148 If Map is NULL, then ASSERT().
1151 @param[in] Map The net map to test.
1153 @return TRUE if the netmap is empty, otherwise FALSE.
1162 ASSERT (Map
!= NULL
);
1163 return (BOOLEAN
) (Map
->Count
== 0);
1168 Return the number of the <Key, Value> pairs in the netmap.
1170 @param[in] Map The netmap to get the entry number.
1172 @return The entry number in the netmap.
1186 Return one allocated item.
1188 If the Recycled doubly linked list of the netmap is empty, it will try to allocate
1189 a batch of items if there are enough resources and add corresponding nodes to the begining
1190 of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove
1191 the fist node entry of the Recycled doubly linked list and return the corresponding item.
1193 If Map is NULL, then ASSERT().
1195 @param[in, out] Map The netmap to allocate item for.
1197 @return The allocated item. If NULL, the
1198 allocation failed due to resource limit.
1210 ASSERT (Map
!= NULL
);
1212 Head
= &Map
->Recycled
;
1214 if (IsListEmpty (Head
)) {
1215 for (Index
= 0; Index
< NET_MAP_INCREAMENT
; Index
++) {
1216 Item
= AllocatePool (sizeof (NET_MAP_ITEM
));
1226 InsertHeadList (Head
, &Item
->Link
);
1230 Item
= NET_LIST_HEAD (Head
, NET_MAP_ITEM
, Link
);
1231 NetListRemoveHead (Head
);
1238 Allocate an item to save the <Key, Value> pair to the head of the netmap.
1240 Allocate an item to save the <Key, Value> pair and add corresponding node entry
1241 to the beginning of the Used doubly linked list. The number of the <Key, Value>
1242 pairs in the netmap increase by 1.
1244 If Map is NULL, then ASSERT().
1246 @param[in, out] Map The netmap to insert into.
1247 @param[in] Key The user's key.
1248 @param[in] Value The user's value for the key.
1250 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
1251 @retval EFI_SUCCESS The item is inserted to the head.
1257 IN OUT NET_MAP
*Map
,
1259 IN VOID
*Value OPTIONAL
1264 ASSERT (Map
!= NULL
);
1266 Item
= NetMapAllocItem (Map
);
1269 return EFI_OUT_OF_RESOURCES
;
1273 Item
->Value
= Value
;
1274 InsertHeadList (&Map
->Used
, &Item
->Link
);
1282 Allocate an item to save the <Key, Value> pair to the tail of the netmap.
1284 Allocate an item to save the <Key, Value> pair and add corresponding node entry
1285 to the tail of the Used doubly linked list. The number of the <Key, Value>
1286 pairs in the netmap increase by 1.
1288 If Map is NULL, then ASSERT().
1290 @param[in, out] Map The netmap to insert into.
1291 @param[in] Key The user's key.
1292 @param[in] Value The user's value for the key.
1294 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
1295 @retval EFI_SUCCESS The item is inserted to the tail.
1301 IN OUT NET_MAP
*Map
,
1303 IN VOID
*Value OPTIONAL
1308 ASSERT (Map
!= NULL
);
1310 Item
= NetMapAllocItem (Map
);
1313 return EFI_OUT_OF_RESOURCES
;
1317 Item
->Value
= Value
;
1318 InsertTailList (&Map
->Used
, &Item
->Link
);
1327 Check whether the item is in the Map and return TRUE if it is.
1329 @param[in] Map The netmap to search within.
1330 @param[in] Item The item to search.
1332 @return TRUE if the item is in the netmap, otherwise FALSE.
1338 IN NET_MAP_ITEM
*Item
1341 LIST_ENTRY
*ListEntry
;
1343 NET_LIST_FOR_EACH (ListEntry
, &Map
->Used
) {
1344 if (ListEntry
== &Item
->Link
) {
1354 Find the key in the netmap and returns the point to the item contains the Key.
1356 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every
1357 item with the key to search. It returns the point to the item contains the Key if found.
1359 If Map is NULL, then ASSERT().
1361 @param[in] Map The netmap to search within.
1362 @param[in] Key The key to search.
1364 @return The point to the item contains the Key, or NULL if Key isn't in the map.
1377 ASSERT (Map
!= NULL
);
1379 NET_LIST_FOR_EACH (Entry
, &Map
->Used
) {
1380 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1382 if (Item
->Key
== Key
) {
1392 Remove the node entry of the item from the netmap and return the key of the removed item.
1394 Remove the node entry of the item from the Used doubly linked list of the netmap.
1395 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1396 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,
1397 Value will point to the value of the item. It returns the key of the removed item.
1399 If Map is NULL, then ASSERT().
1400 If Item is NULL, then ASSERT().
1401 if item in not in the netmap, then ASSERT().
1403 @param[in, out] Map The netmap to remove the item from.
1404 @param[in, out] Item The item to remove.
1405 @param[out] Value The variable to receive the value if not NULL.
1407 @return The key of the removed item.
1413 IN OUT NET_MAP
*Map
,
1414 IN OUT NET_MAP_ITEM
*Item
,
1415 OUT VOID
**Value OPTIONAL
1418 ASSERT ((Map
!= NULL
) && (Item
!= NULL
));
1419 ASSERT (NetItemInMap (Map
, Item
));
1421 RemoveEntryList (&Item
->Link
);
1423 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
1425 if (Value
!= NULL
) {
1426 *Value
= Item
->Value
;
1434 Remove the first node entry on the netmap and return the key of the removed item.
1436 Remove the first node entry from the Used doubly linked list of the netmap.
1437 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1438 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
1439 parameter Value will point to the value of the item. It returns the key of the removed item.
1441 If Map is NULL, then ASSERT().
1442 If the Used doubly linked list is empty, then ASSERT().
1444 @param[in, out] Map The netmap to remove the head from.
1445 @param[out] Value The variable to receive the value if not NULL.
1447 @return The key of the item removed.
1453 IN OUT NET_MAP
*Map
,
1454 OUT VOID
**Value OPTIONAL
1460 // Often, it indicates a programming error to remove
1461 // the first entry in an empty list
1463 ASSERT (Map
&& !IsListEmpty (&Map
->Used
));
1465 Item
= NET_LIST_HEAD (&Map
->Used
, NET_MAP_ITEM
, Link
);
1466 RemoveEntryList (&Item
->Link
);
1468 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
1470 if (Value
!= NULL
) {
1471 *Value
= Item
->Value
;
1479 Remove the last node entry on the netmap and return the key of the removed item.
1481 Remove the last node entry from the Used doubly linked list of the netmap.
1482 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1483 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
1484 parameter Value will point to the value of the item. It returns the key of the removed item.
1486 If Map is NULL, then ASSERT().
1487 If the Used doubly linked list is empty, then ASSERT().
1489 @param[in, out] Map The netmap to remove the tail from.
1490 @param[out] Value The variable to receive the value if not NULL.
1492 @return The key of the item removed.
1498 IN OUT NET_MAP
*Map
,
1499 OUT VOID
**Value OPTIONAL
1505 // Often, it indicates a programming error to remove
1506 // the last entry in an empty list
1508 ASSERT (Map
&& !IsListEmpty (&Map
->Used
));
1510 Item
= NET_LIST_TAIL (&Map
->Used
, NET_MAP_ITEM
, Link
);
1511 RemoveEntryList (&Item
->Link
);
1513 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
1515 if (Value
!= NULL
) {
1516 *Value
= Item
->Value
;
1524 Iterate through the netmap and call CallBack for each item.
1526 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break
1527 from the loop. It returns the CallBack's last return value. This function is
1528 delete safe for the current item.
1530 If Map is NULL, then ASSERT().
1531 If CallBack is NULL, then ASSERT().
1533 @param[in] Map The Map to iterate through.
1534 @param[in] CallBack The callback function to call for each item.
1535 @param[in] Arg The opaque parameter to the callback.
1537 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item
1539 @retval Others It returns the CallBack's last return value.
1546 IN NET_MAP_CALLBACK CallBack
,
1547 IN VOID
*Arg OPTIONAL
1557 ASSERT ((Map
!= NULL
) && (CallBack
!= NULL
));
1561 if (IsListEmpty (Head
)) {
1565 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Head
) {
1566 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1567 Result
= CallBack (Map
, Item
, Arg
);
1569 if (EFI_ERROR (Result
)) {
1579 Internal function to get the child handle of the NIC handle.
1581 @param[in] Controller NIC controller handle.
1582 @param[out] ChildHandle Returned child handle.
1584 @retval EFI_SUCCESS Successfully to get child handle.
1585 @retval Others Failed to get child handle.
1590 IN EFI_HANDLE Controller
,
1591 OUT EFI_HANDLE
*ChildHandle
1595 EFI_HANDLE
*Handles
;
1598 EFI_DEVICE_PATH_PROTOCOL
*ChildDeviceDevicePath
;
1599 VENDOR_DEVICE_PATH
*VendorDeviceNode
;
1602 // Locate all EFI Hii Config Access protocols
1604 Status
= gBS
->LocateHandleBuffer (
1606 &gEfiHiiConfigAccessProtocolGuid
,
1611 if (EFI_ERROR (Status
) || (HandleCount
== 0)) {
1615 Status
= EFI_NOT_FOUND
;
1617 for (Index
= 0; Index
< HandleCount
; Index
++) {
1619 Status
= EfiTestChildHandle (Controller
, Handles
[Index
], &gEfiManagedNetworkServiceBindingProtocolGuid
);
1620 if (!EFI_ERROR (Status
)) {
1622 // Get device path on the child handle
1624 Status
= gBS
->HandleProtocol (
1626 &gEfiDevicePathProtocolGuid
,
1627 (VOID
**) &ChildDeviceDevicePath
1630 if (!EFI_ERROR (Status
)) {
1631 while (!IsDevicePathEnd (ChildDeviceDevicePath
)) {
1632 ChildDeviceDevicePath
= NextDevicePathNode (ChildDeviceDevicePath
);
1634 // Parse one instance
1636 if (ChildDeviceDevicePath
->Type
== HARDWARE_DEVICE_PATH
&&
1637 ChildDeviceDevicePath
->SubType
== HW_VENDOR_DP
) {
1638 VendorDeviceNode
= (VENDOR_DEVICE_PATH
*) ChildDeviceDevicePath
;
1639 if (CompareMem (&VendorDeviceNode
->Guid
, &gEfiNicIp4ConfigVariableGuid
, sizeof (EFI_GUID
)) == 0) {
1641 // Found item matched gEfiNicIp4ConfigVariableGuid
1643 *ChildHandle
= Handles
[Index
];
1659 This is the default unload handle for all the network drivers.
1661 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
1662 Uninstall all the protocols installed in the driver entry point.
1664 @param[in] ImageHandle The drivers' driver image.
1666 @retval EFI_SUCCESS The image is unloaded.
1667 @retval Others Failed to unload the image.
1672 NetLibDefaultUnload (
1673 IN EFI_HANDLE ImageHandle
1677 EFI_HANDLE
*DeviceHandleBuffer
;
1678 UINTN DeviceHandleCount
;
1680 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
1681 EFI_COMPONENT_NAME_PROTOCOL
*ComponentName
;
1682 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
1685 // Get the list of all the handles in the handle database.
1686 // If there is an error getting the list, then the unload
1689 Status
= gBS
->LocateHandleBuffer (
1697 if (EFI_ERROR (Status
)) {
1702 // Disconnect the driver specified by ImageHandle from all
1703 // the devices in the handle database.
1705 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1706 Status
= gBS
->DisconnectController (
1707 DeviceHandleBuffer
[Index
],
1714 // Uninstall all the protocols installed in the driver entry point
1716 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1717 Status
= gBS
->HandleProtocol (
1718 DeviceHandleBuffer
[Index
],
1719 &gEfiDriverBindingProtocolGuid
,
1720 (VOID
**) &DriverBinding
1723 if (EFI_ERROR (Status
)) {
1727 if (DriverBinding
->ImageHandle
!= ImageHandle
) {
1731 gBS
->UninstallProtocolInterface (
1733 &gEfiDriverBindingProtocolGuid
,
1736 Status
= gBS
->HandleProtocol (
1737 DeviceHandleBuffer
[Index
],
1738 &gEfiComponentNameProtocolGuid
,
1739 (VOID
**) &ComponentName
1741 if (!EFI_ERROR (Status
)) {
1742 gBS
->UninstallProtocolInterface (
1744 &gEfiComponentNameProtocolGuid
,
1749 Status
= gBS
->HandleProtocol (
1750 DeviceHandleBuffer
[Index
],
1751 &gEfiComponentName2ProtocolGuid
,
1752 (VOID
**) &ComponentName2
1754 if (!EFI_ERROR (Status
)) {
1755 gBS
->UninstallProtocolInterface (
1757 &gEfiComponentName2ProtocolGuid
,
1764 // Free the buffer containing the list of handles from the handle database
1766 if (DeviceHandleBuffer
!= NULL
) {
1767 gBS
->FreePool (DeviceHandleBuffer
);
1776 Create a child of the service that is identified by ServiceBindingGuid.
1778 Get the ServiceBinding Protocol first, then use it to create a child.
1780 If ServiceBindingGuid is NULL, then ASSERT().
1781 If ChildHandle is NULL, then ASSERT().
1783 @param[in] Controller The controller which has the service installed.
1784 @param[in] Image The image handle used to open service.
1785 @param[in] ServiceBindingGuid The service's Guid.
1786 @param[in, out] ChildHandle The handle to receive the create child.
1788 @retval EFI_SUCCESS The child is successfully created.
1789 @retval Others Failed to create the child.
1794 NetLibCreateServiceChild (
1795 IN EFI_HANDLE Controller
,
1796 IN EFI_HANDLE Image
,
1797 IN EFI_GUID
*ServiceBindingGuid
,
1798 IN OUT EFI_HANDLE
*ChildHandle
1802 EFI_SERVICE_BINDING_PROTOCOL
*Service
;
1805 ASSERT ((ServiceBindingGuid
!= NULL
) && (ChildHandle
!= NULL
));
1808 // Get the ServiceBinding Protocol
1810 Status
= gBS
->OpenProtocol (
1816 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1819 if (EFI_ERROR (Status
)) {
1826 Status
= Service
->CreateChild (Service
, ChildHandle
);
1832 Destory a child of the service that is identified by ServiceBindingGuid.
1834 Get the ServiceBinding Protocol first, then use it to destroy a child.
1836 If ServiceBindingGuid is NULL, then ASSERT().
1838 @param[in] Controller The controller which has the service installed.
1839 @param[in] Image The image handle used to open service.
1840 @param[in] ServiceBindingGuid The service's Guid.
1841 @param[in] ChildHandle The child to destory.
1843 @retval EFI_SUCCESS The child is successfully destoried.
1844 @retval Others Failed to destory the child.
1849 NetLibDestroyServiceChild (
1850 IN EFI_HANDLE Controller
,
1851 IN EFI_HANDLE Image
,
1852 IN EFI_GUID
*ServiceBindingGuid
,
1853 IN EFI_HANDLE ChildHandle
1857 EFI_SERVICE_BINDING_PROTOCOL
*Service
;
1859 ASSERT (ServiceBindingGuid
!= NULL
);
1862 // Get the ServiceBinding Protocol
1864 Status
= gBS
->OpenProtocol (
1870 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1873 if (EFI_ERROR (Status
)) {
1878 // destory the child
1880 Status
= Service
->DestroyChild (Service
, ChildHandle
);
1885 Get handle with Simple Network Protocol installed on it.
1887 There should be MNP Service Binding Protocol installed on the input ServiceHandle.
1888 If Simple Network Protocol is already installed on the ServiceHandle, the
1889 ServiceHandle will be returned. If SNP is not installed on the ServiceHandle,
1890 try to find its parent handle with SNP installed.
1892 @param[in] ServiceHandle The handle where network service binding protocols are
1894 @param[out] Snp The pointer to store the address of the SNP instance.
1895 This is an optional parameter that may be NULL.
1897 @return The SNP handle, or NULL if not found.
1902 NetLibGetSnpHandle (
1903 IN EFI_HANDLE ServiceHandle
,
1904 OUT EFI_SIMPLE_NETWORK_PROTOCOL
**Snp OPTIONAL
1908 EFI_SIMPLE_NETWORK_PROTOCOL
*SnpInstance
;
1909 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1910 EFI_HANDLE SnpHandle
;
1913 // Try to open SNP from ServiceHandle
1916 Status
= gBS
->HandleProtocol (ServiceHandle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &SnpInstance
);
1917 if (!EFI_ERROR (Status
)) {
1921 return ServiceHandle
;
1925 // Failed to open SNP, try to get SNP handle by LocateDevicePath()
1927 DevicePath
= DevicePathFromHandle (ServiceHandle
);
1928 if (DevicePath
== NULL
) {
1933 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &DevicePath
, &SnpHandle
);
1934 if (EFI_ERROR (Status
)) {
1936 // Failed to find SNP handle
1941 Status
= gBS
->HandleProtocol (SnpHandle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &SnpInstance
);
1942 if (!EFI_ERROR (Status
)) {
1953 Retrieve VLAN ID of a VLAN device handle.
1955 Search VLAN device path node in Device Path of specified ServiceHandle and
1956 return its VLAN ID. If no VLAN device path node found, then this ServiceHandle
1957 is not a VLAN device handle, and 0 will be returned.
1959 @param[in] ServiceHandle The handle where network service binding protocols are
1962 @return VLAN ID of the device handle, or 0 if not a VLAN device.
1968 IN EFI_HANDLE ServiceHandle
1971 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1972 EFI_DEVICE_PATH_PROTOCOL
*Node
;
1974 DevicePath
= DevicePathFromHandle (ServiceHandle
);
1975 if (DevicePath
== NULL
) {
1980 while (!IsDevicePathEnd (Node
)) {
1981 if (Node
->Type
== MESSAGING_DEVICE_PATH
&& Node
->SubType
== MSG_VLAN_DP
) {
1982 return ((VLAN_DEVICE_PATH
*) Node
)->VlanId
;
1984 Node
= NextDevicePathNode (Node
);
1991 Find VLAN device handle with specified VLAN ID.
1993 The VLAN child device handle is created by VLAN Config Protocol on ControllerHandle.
1994 This function will append VLAN device path node to the parent device path,
1995 and then use LocateDevicePath() to find the correct VLAN device handle.
1997 @param[in] ControllerHandle The handle where network service binding protocols are
1999 @param[in] VlanId The configured VLAN ID for the VLAN device.
2001 @return The VLAN device handle, or NULL if not found.
2006 NetLibGetVlanHandle (
2007 IN EFI_HANDLE ControllerHandle
,
2011 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
2012 EFI_DEVICE_PATH_PROTOCOL
*VlanDevicePath
;
2013 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2014 VLAN_DEVICE_PATH VlanNode
;
2017 ParentDevicePath
= DevicePathFromHandle (ControllerHandle
);
2018 if (ParentDevicePath
== NULL
) {
2023 // Construct VLAN device path
2025 CopyMem (&VlanNode
, &mNetVlanDevicePathTemplate
, sizeof (VLAN_DEVICE_PATH
));
2026 VlanNode
.VlanId
= VlanId
;
2027 VlanDevicePath
= AppendDevicePathNode (
2029 (EFI_DEVICE_PATH_PROTOCOL
*) &VlanNode
2031 if (VlanDevicePath
== NULL
) {
2036 // Find VLAN device handle
2039 DevicePath
= VlanDevicePath
;
2040 gBS
->LocateDevicePath (
2041 &gEfiDevicePathProtocolGuid
,
2045 if (!IsDevicePathEnd (DevicePath
)) {
2047 // Device path is not exactly match
2052 FreePool (VlanDevicePath
);
2057 Get MAC address associated with the network service handle.
2059 There should be MNP Service Binding Protocol installed on the input ServiceHandle.
2060 If SNP is installed on the ServiceHandle or its parent handle, MAC address will
2061 be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP.
2063 @param[in] ServiceHandle The handle where network service binding protocols are
2065 @param[out] MacAddress The pointer to store the returned MAC address.
2066 @param[out] AddressSize The length of returned MAC address.
2068 @retval EFI_SUCCESS MAC address is returned successfully.
2069 @retval Others Failed to get SNP mode data.
2074 NetLibGetMacAddress (
2075 IN EFI_HANDLE ServiceHandle
,
2076 OUT EFI_MAC_ADDRESS
*MacAddress
,
2077 OUT UINTN
*AddressSize
2081 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
2082 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
2083 EFI_SIMPLE_NETWORK_MODE SnpModeData
;
2084 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
2085 EFI_SERVICE_BINDING_PROTOCOL
*MnpSb
;
2086 EFI_HANDLE
*SnpHandle
;
2087 EFI_HANDLE MnpChildHandle
;
2089 ASSERT (MacAddress
!= NULL
);
2090 ASSERT (AddressSize
!= NULL
);
2093 // Try to get SNP handle
2096 SnpHandle
= NetLibGetSnpHandle (ServiceHandle
, &Snp
);
2097 if (SnpHandle
!= NULL
) {
2099 // SNP found, use it directly
2101 SnpMode
= Snp
->Mode
;
2104 // Failed to get SNP handle, try to get MAC address from MNP
2106 MnpChildHandle
= NULL
;
2107 Status
= gBS
->HandleProtocol (
2109 &gEfiManagedNetworkServiceBindingProtocolGuid
,
2112 if (EFI_ERROR (Status
)) {
2117 // Create a MNP child
2119 Status
= MnpSb
->CreateChild (MnpSb
, &MnpChildHandle
);
2120 if (EFI_ERROR (Status
)) {
2125 // Open MNP protocol
2127 Status
= gBS
->HandleProtocol (
2129 &gEfiManagedNetworkProtocolGuid
,
2132 if (EFI_ERROR (Status
)) {
2133 MnpSb
->DestroyChild (MnpSb
, MnpChildHandle
);
2138 // Try to get SNP mode from MNP
2140 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpModeData
);
2141 if (EFI_ERROR (Status
)) {
2142 MnpSb
->DestroyChild (MnpSb
, MnpChildHandle
);
2145 SnpMode
= &SnpModeData
;
2148 // Destroy the MNP child
2150 MnpSb
->DestroyChild (MnpSb
, MnpChildHandle
);
2153 *AddressSize
= SnpMode
->HwAddressSize
;
2154 CopyMem (MacAddress
->Addr
, SnpMode
->CurrentAddress
.Addr
, SnpMode
->HwAddressSize
);
2160 Convert MAC address of the NIC associated with specified Service Binding Handle
2161 to a unicode string. Callers are responsible for freeing the string storage.
2163 Locate simple network protocol associated with the Service Binding Handle and
2164 get the mac address from SNP. Then convert the mac address into a unicode
2165 string. It takes 2 unicode characters to represent a 1 byte binary buffer.
2166 Plus one unicode character for the null-terminator.
2168 @param[in] ServiceHandle The handle where network service binding protocol is
2170 @param[in] ImageHandle The image handle used to act as the agent handle to
2171 get the simple network protocol.
2172 @param[out] MacString The pointer to store the address of the string
2173 representation of the mac address.
2175 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.
2176 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
2177 @retval Others Failed to open the simple network protocol.
2182 NetLibGetMacString (
2183 IN EFI_HANDLE ServiceHandle
,
2184 IN EFI_HANDLE ImageHandle
,
2185 OUT CHAR16
**MacString
2189 EFI_MAC_ADDRESS MacAddress
;
2191 UINTN HwAddressSize
;
2196 ASSERT (MacString
!= NULL
);
2199 // Get MAC address of the network device
2201 Status
= NetLibGetMacAddress (ServiceHandle
, &MacAddress
, &HwAddressSize
);
2202 if (EFI_ERROR (Status
)) {
2207 // It takes 2 unicode characters to represent a 1 byte binary buffer.
2208 // If VLAN is configured, it will need extra 5 characters like "\0005".
2209 // Plus one unicode character for the null-terminator.
2211 String
= AllocateZeroPool ((2 * HwAddressSize
+ 5 + 1) * sizeof (CHAR16
));
2212 if (String
== NULL
) {
2213 return EFI_OUT_OF_RESOURCES
;
2215 *MacString
= String
;
2218 // Convert the MAC address into a unicode string.
2220 HwAddress
= &MacAddress
.Addr
[0];
2221 for (Index
= 0; Index
< HwAddressSize
; Index
++) {
2222 String
+= UnicodeValueToString (String
, PREFIX_ZERO
| RADIX_HEX
, *(HwAddress
++), 2);
2226 // Append VLAN ID if any
2228 VlanId
= NetLibGetVlanId (ServiceHandle
);
2231 String
+= UnicodeValueToString (String
, PREFIX_ZERO
| RADIX_HEX
, VlanId
, 4);
2235 // Null terminate the Unicode string
2243 Detect media status for specified network device.
2245 The underlying UNDI driver may or may not support reporting media status from
2246 GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine
2247 will try to invoke Snp->GetStatus() to get the media status: if media already
2248 present, it return directly; if media not present, it will stop SNP and then
2249 restart SNP to get the latest media status, this give chance to get the correct
2250 media status for old UNDI driver which doesn't support reporting media status
2251 from GET_STATUS command.
2252 Note: there will be two limitations for current algorithm:
2253 1) for UNDI with this capability, in case of cable is not attached, there will
2254 be an redundant Stop/Start() process;
2255 2) for UNDI without this capability, in case that network cable is attached when
2256 Snp->Initialize() is invoked while network cable is unattached later,
2257 NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer
2258 apps to wait for timeout time.
2260 @param[in] ServiceHandle The handle where network service binding protocols are
2262 @param[out] MediaPresent The pointer to store the media status.
2264 @retval EFI_SUCCESS Media detection success.
2265 @retval EFI_INVALID_PARAMETER ServiceHandle is not valid network device handle.
2266 @retval EFI_UNSUPPORTED Network device does not support media detection.
2267 @retval EFI_DEVICE_ERROR SNP is in unknown state.
2273 IN EFI_HANDLE ServiceHandle
,
2274 OUT BOOLEAN
*MediaPresent
2278 EFI_HANDLE SnpHandle
;
2279 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
2280 UINT32 InterruptStatus
;
2282 EFI_MAC_ADDRESS
*MCastFilter
;
2283 UINT32 MCastFilterCount
;
2284 UINT32 EnableFilterBits
;
2285 UINT32 DisableFilterBits
;
2286 BOOLEAN ResetMCastFilters
;
2288 ASSERT (MediaPresent
!= NULL
);
2294 SnpHandle
= NetLibGetSnpHandle (ServiceHandle
, &Snp
);
2295 if (SnpHandle
== NULL
) {
2296 return EFI_INVALID_PARAMETER
;
2300 // Check whether SNP support media detection
2302 if (!Snp
->Mode
->MediaPresentSupported
) {
2303 return EFI_UNSUPPORTED
;
2307 // Invoke Snp->GetStatus() to refresh MediaPresent field in SNP mode data
2309 Status
= Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
2310 if (EFI_ERROR (Status
)) {
2314 if (Snp
->Mode
->MediaPresent
) {
2316 // Media is present, return directly
2318 *MediaPresent
= TRUE
;
2323 // Till now, GetStatus() report no media; while, in case UNDI not support
2324 // reporting media status from GetStatus(), this media status may be incorrect.
2325 // So, we will stop SNP and then restart it to get the correct media status.
2327 OldState
= Snp
->Mode
->State
;
2328 if (OldState
>= EfiSimpleNetworkMaxState
) {
2329 return EFI_DEVICE_ERROR
;
2334 if (OldState
== EfiSimpleNetworkInitialized
) {
2336 // SNP is already in use, need Shutdown/Stop and then Start/Initialize
2340 // Backup current SNP receive filter settings
2342 EnableFilterBits
= Snp
->Mode
->ReceiveFilterSetting
;
2343 DisableFilterBits
= Snp
->Mode
->ReceiveFilterMask
^ EnableFilterBits
;
2345 ResetMCastFilters
= TRUE
;
2346 MCastFilterCount
= Snp
->Mode
->MCastFilterCount
;
2347 if (MCastFilterCount
!= 0) {
2348 MCastFilter
= AllocateCopyPool (
2349 MCastFilterCount
* sizeof (EFI_MAC_ADDRESS
),
2350 Snp
->Mode
->MCastFilter
2352 ASSERT (MCastFilter
!= NULL
);
2354 ResetMCastFilters
= FALSE
;
2358 // Shutdown/Stop the simple network
2360 Status
= Snp
->Shutdown (Snp
);
2361 if (!EFI_ERROR (Status
)) {
2362 Status
= Snp
->Stop (Snp
);
2364 if (EFI_ERROR (Status
)) {
2369 // Start/Initialize the simple network
2371 Status
= Snp
->Start (Snp
);
2372 if (!EFI_ERROR (Status
)) {
2373 Status
= Snp
->Initialize (Snp
, 0, 0);
2375 if (EFI_ERROR (Status
)) {
2380 // Here we get the correct media status
2382 *MediaPresent
= Snp
->Mode
->MediaPresent
;
2385 // Restore SNP receive filter settings
2387 Status
= Snp
->ReceiveFilters (
2396 if (MCastFilter
!= NULL
) {
2397 FreePool (MCastFilter
);
2404 // SNP is not in use, it's in state of EfiSimpleNetworkStopped or EfiSimpleNetworkStarted
2406 if (OldState
== EfiSimpleNetworkStopped
) {
2408 // SNP not start yet, start it
2410 Status
= Snp
->Start (Snp
);
2411 if (EFI_ERROR (Status
)) {
2417 // Initialize the simple network
2419 Status
= Snp
->Initialize (Snp
, 0, 0);
2420 if (EFI_ERROR (Status
)) {
2421 Status
= EFI_DEVICE_ERROR
;
2426 // Here we get the correct media status
2428 *MediaPresent
= Snp
->Mode
->MediaPresent
;
2431 // Shut down the simple network
2433 Snp
->Shutdown (Snp
);
2436 if (OldState
== EfiSimpleNetworkStopped
) {
2438 // Original SNP sate is Stopped, restore to original state
2443 if (MCastFilter
!= NULL
) {
2444 FreePool (MCastFilter
);
2451 Check the default address used by the IPv4 driver is static or dynamic (acquired
2454 If the controller handle does not have the NIC Ip4 Config Protocol installed, the
2455 default address is static. If the EFI variable to save the configuration is not found,
2456 the default address is static. Otherwise, get the result from the EFI variable which
2457 saving the configuration.
2459 @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol
2460 relative with the default address to judge.
2462 @retval TRUE If the default address is static.
2463 @retval FALSE If the default address is acquired from DHCP.
2467 NetLibDefaultAddressIsStatic (
2468 IN EFI_HANDLE Controller
2472 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
2474 NIC_IP4_CONFIG_INFO
*ConfigInfo
;
2476 EFI_STRING ConfigHdr
;
2477 EFI_STRING ConfigResp
;
2478 EFI_STRING AccessProgress
;
2479 EFI_STRING AccessResults
;
2481 EFI_HANDLE ChildHandle
;
2486 AccessProgress
= NULL
;
2487 AccessResults
= NULL
;
2490 Status
= gBS
->LocateProtocol (
2491 &gEfiHiiConfigRoutingProtocolGuid
,
2493 (VOID
**) &HiiConfigRouting
2495 if (EFI_ERROR (Status
)) {
2499 Status
= NetGetChildHandle (Controller
, &ChildHandle
);
2500 if (EFI_ERROR (Status
)) {
2505 // Construct config request string header
2507 ConfigHdr
= HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid
, EFI_NIC_IP4_CONFIG_VARIABLE
, ChildHandle
);
2508 if (ConfigHdr
== NULL
) {
2512 Len
= StrLen (ConfigHdr
);
2513 ConfigResp
= AllocateZeroPool ((Len
+ NIC_ITEM_CONFIG_SIZE
* 2 + 100) * sizeof (CHAR16
));
2514 if (ConfigResp
== NULL
) {
2517 StrCpy (ConfigResp
, ConfigHdr
);
2519 String
= ConfigResp
+ Len
;
2522 (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16
),
2523 L
"&OFFSET=%04X&WIDTH=%04X",
2524 OFFSET_OF (NIC_IP4_CONFIG_INFO
, Source
),
2528 Status
= HiiConfigRouting
->ExtractConfig (
2534 if (EFI_ERROR (Status
)) {
2538 ConfigInfo
= AllocateZeroPool (NIC_ITEM_CONFIG_SIZE
);
2539 if (ConfigInfo
== NULL
) {
2543 ConfigInfo
->Source
= IP4_CONFIG_SOURCE_STATIC
;
2544 Len
= NIC_ITEM_CONFIG_SIZE
;
2545 Status
= HiiConfigRouting
->ConfigToBlock (
2548 (UINT8
*) ConfigInfo
,
2552 if (EFI_ERROR (Status
)) {
2556 IsStatic
= (BOOLEAN
) (ConfigInfo
->Source
== IP4_CONFIG_SOURCE_STATIC
);
2560 if (AccessResults
!= NULL
) {
2561 FreePool (AccessResults
);
2563 if (ConfigInfo
!= NULL
) {
2564 FreePool (ConfigInfo
);
2566 if (ConfigResp
!= NULL
) {
2567 FreePool (ConfigResp
);
2569 if (ConfigHdr
!= NULL
) {
2570 FreePool (ConfigHdr
);
2577 Create an IPv4 device path node.
2579 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.
2580 The header subtype of IPv4 device path node is MSG_IPv4_DP.
2581 Get other info from parameters to make up the whole IPv4 device path node.
2583 @param[in, out] Node Pointer to the IPv4 device path node.
2584 @param[in] Controller The controller handle.
2585 @param[in] LocalIp The local IPv4 address.
2586 @param[in] LocalPort The local port.
2587 @param[in] RemoteIp The remote IPv4 address.
2588 @param[in] RemotePort The remote port.
2589 @param[in] Protocol The protocol type in the IP header.
2590 @param[in] UseDefaultAddress Whether this instance is using default address or not.
2595 NetLibCreateIPv4DPathNode (
2596 IN OUT IPv4_DEVICE_PATH
*Node
,
2597 IN EFI_HANDLE Controller
,
2598 IN IP4_ADDR LocalIp
,
2599 IN UINT16 LocalPort
,
2600 IN IP4_ADDR RemoteIp
,
2601 IN UINT16 RemotePort
,
2603 IN BOOLEAN UseDefaultAddress
2606 Node
->Header
.Type
= MESSAGING_DEVICE_PATH
;
2607 Node
->Header
.SubType
= MSG_IPv4_DP
;
2608 SetDevicePathNodeLength (&Node
->Header
, sizeof (IPv4_DEVICE_PATH
));
2610 CopyMem (&Node
->LocalIpAddress
, &LocalIp
, sizeof (EFI_IPv4_ADDRESS
));
2611 CopyMem (&Node
->RemoteIpAddress
, &RemoteIp
, sizeof (EFI_IPv4_ADDRESS
));
2613 Node
->LocalPort
= LocalPort
;
2614 Node
->RemotePort
= RemotePort
;
2616 Node
->Protocol
= Protocol
;
2618 if (!UseDefaultAddress
) {
2619 Node
->StaticIpAddress
= TRUE
;
2621 Node
->StaticIpAddress
= NetLibDefaultAddressIsStatic (Controller
);
2625 // Set the Gateway IP address to default value 0:0:0:0.
2626 // Set the Subnet mask to default value 255:255:255:0.
2628 ZeroMem (&Node
->GatewayIpAddress
, sizeof (EFI_IPv4_ADDRESS
));
2629 SetMem (&Node
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
), 0xff);
2630 Node
->SubnetMask
.Addr
[3] = 0;
2634 Create an IPv6 device path node.
2636 The header type of IPv6 device path node is MESSAGING_DEVICE_PATH.
2637 The header subtype of IPv6 device path node is MSG_IPv6_DP.
2638 Get other info from parameters to make up the whole IPv6 device path node.
2640 @param[in, out] Node Pointer to the IPv6 device path node.
2641 @param[in] Controller The controller handle.
2642 @param[in] LocalIp The local IPv6 address.
2643 @param[in] LocalPort The local port.
2644 @param[in] RemoteIp The remote IPv6 address.
2645 @param[in] RemotePort The remote port.
2646 @param[in] Protocol The protocol type in the IP header.
2651 NetLibCreateIPv6DPathNode (
2652 IN OUT IPv6_DEVICE_PATH
*Node
,
2653 IN EFI_HANDLE Controller
,
2654 IN EFI_IPv6_ADDRESS
*LocalIp
,
2655 IN UINT16 LocalPort
,
2656 IN EFI_IPv6_ADDRESS
*RemoteIp
,
2657 IN UINT16 RemotePort
,
2661 Node
->Header
.Type
= MESSAGING_DEVICE_PATH
;
2662 Node
->Header
.SubType
= MSG_IPv6_DP
;
2663 SetDevicePathNodeLength (&Node
->Header
, sizeof (IPv6_DEVICE_PATH
));
2665 CopyMem (&Node
->LocalIpAddress
, LocalIp
, sizeof (EFI_IPv6_ADDRESS
));
2666 CopyMem (&Node
->RemoteIpAddress
, RemoteIp
, sizeof (EFI_IPv6_ADDRESS
));
2668 Node
->LocalPort
= LocalPort
;
2669 Node
->RemotePort
= RemotePort
;
2671 Node
->Protocol
= Protocol
;
2674 // Set default value to IPAddressOrigin, PrefixLength.
2675 // Set the Gateway IP address to unspecified address.
2677 Node
->IpAddressOrigin
= 0;
2678 Node
->PrefixLength
= IP6_PREFIX_LENGTH
;
2679 ZeroMem (&Node
->GatewayIpAddress
, sizeof (EFI_IPv6_ADDRESS
));
2683 Find the UNDI/SNP handle from controller and protocol GUID.
2685 For example, IP will open a MNP child to transmit/receive
2686 packets, when MNP is stopped, IP should also be stopped. IP
2687 needs to find its own private data which is related the IP's
2688 service binding instance that is install on UNDI/SNP handle.
2689 Now, the controller is either a MNP or ARP child handle. But
2690 IP opens these handle BY_DRIVER, use that info, we can get the
2693 @param[in] Controller Then protocol handle to check.
2694 @param[in] ProtocolGuid The protocol that is related with the handle.
2696 @return The UNDI/SNP handle or NULL for errors.
2701 NetLibGetNicHandle (
2702 IN EFI_HANDLE Controller
,
2703 IN EFI_GUID
*ProtocolGuid
2706 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenBuffer
;
2712 Status
= gBS
->OpenProtocolInformation (
2719 if (EFI_ERROR (Status
)) {
2725 for (Index
= 0; Index
< OpenCount
; Index
++) {
2726 if ((OpenBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) != 0) {
2727 Handle
= OpenBuffer
[Index
].ControllerHandle
;
2732 gBS
->FreePool (OpenBuffer
);
2737 Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS.
2739 @param[in] String The pointer to the Ascii string.
2740 @param[out] Ip4Address The pointer to the converted IPv4 address.
2742 @retval EFI_SUCCESS Convert to IPv4 address successfully.
2743 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.
2748 NetLibAsciiStrToIp4 (
2749 IN CONST CHAR8
*String
,
2750 OUT EFI_IPv4_ADDRESS
*Ip4Address
2758 if ((String
== NULL
) || (Ip4Address
== NULL
)) {
2759 return EFI_INVALID_PARAMETER
;
2762 Ip4Str
= (CHAR8
*) String
;
2764 for (Index
= 0; Index
< 4; Index
++) {
2767 while ((*Ip4Str
!= '\0') && (*Ip4Str
!= '.')) {
2772 // The IPv4 address is X.X.X.X
2774 if (*Ip4Str
== '.') {
2776 return EFI_INVALID_PARAMETER
;
2780 return EFI_INVALID_PARAMETER
;
2785 // Convert the string to IPv4 address. AsciiStrDecimalToUintn stops at the
2786 // first character that is not a valid decimal character, '.' or '\0' here.
2788 NodeVal
= AsciiStrDecimalToUintn (TempStr
);
2789 if (NodeVal
> 0xFF) {
2790 return EFI_INVALID_PARAMETER
;
2793 Ip4Address
->Addr
[Index
] = (UINT8
) NodeVal
;
2803 Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of the
2804 string is defined in RFC 4291 - Text Pepresentation of Addresses.
2806 @param[in] String The pointer to the Ascii string.
2807 @param[out] Ip6Address The pointer to the converted IPv6 address.
2809 @retval EFI_SUCCESS Convert to IPv6 address successfully.
2810 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
2815 NetLibAsciiStrToIp6 (
2816 IN CONST CHAR8
*String
,
2817 OUT EFI_IPv6_ADDRESS
*Ip6Address
2834 if ((String
== NULL
) || (Ip6Address
== NULL
)) {
2835 return EFI_INVALID_PARAMETER
;
2838 Ip6Str
= (CHAR8
*) String
;
2843 // An IPv6 address leading with : looks strange.
2845 if (*Ip6Str
== ':') {
2846 if (*(Ip6Str
+ 1) != ':') {
2847 return EFI_INVALID_PARAMETER
;
2853 ZeroMem (Ip6Address
, sizeof (EFI_IPv6_ADDRESS
));
2861 for (Index
= 0; Index
< 15; Index
= (UINT8
) (Index
+ 2)) {
2864 while ((*Ip6Str
!= '\0') && (*Ip6Str
!= ':')) {
2868 if ((*Ip6Str
== '\0') && (Index
!= 14)) {
2869 return EFI_INVALID_PARAMETER
;
2872 if (*Ip6Str
== ':') {
2873 if (*(Ip6Str
+ 1) == ':') {
2874 if ((NodeCnt
> 6) ||
2875 ((*(Ip6Str
+ 2) != '\0') && (AsciiStrHexToUintn (Ip6Str
+ 2) == 0))) {
2877 // ::0 looks strange. report error to user.
2879 return EFI_INVALID_PARAMETER
;
2881 if ((NodeCnt
== 6) && (*(Ip6Str
+ 2) != '\0') &&
2882 (AsciiStrHexToUintn (Ip6Str
+ 2) != 0)) {
2883 return EFI_INVALID_PARAMETER
;
2887 // Skip the abbreviation part of IPv6 address.
2889 TempStr2
= Ip6Str
+ 2;
2890 while ((*TempStr2
!= '\0')) {
2891 if (*TempStr2
== ':') {
2892 if (*(TempStr2
+ 1) == ':') {
2894 // :: can only appear once in IPv6 address.
2896 return EFI_INVALID_PARAMETER
;
2900 if (TailNodeCnt
>= (AllowedCnt
- NodeCnt
)) {
2902 // :: indicates one or more groups of 16 bits of zeros.
2904 return EFI_INVALID_PARAMETER
;
2914 Ip6Str
= Ip6Str
+ 2;
2916 if (*(Ip6Str
+ 1) == '\0') {
2917 return EFI_INVALID_PARAMETER
;
2921 if ((Short
&& (NodeCnt
> 6)) || (!Short
&& (NodeCnt
> 7))) {
2923 // There are more than 8 groups of 16 bits of zeros.
2925 return EFI_INVALID_PARAMETER
;
2931 // Convert the string to IPv6 address. AsciiStrHexToUintn stops at the first
2932 // character that is not a valid hexadecimal character, ':' or '\0' here.
2934 NodeVal
= AsciiStrHexToUintn (TempStr
);
2935 if ((NodeVal
> 0xFFFF) || (Index
> 14)) {
2936 return EFI_INVALID_PARAMETER
;
2939 if ((*TempStr
== '0') &&
2940 ((*(TempStr
+ 2) == ':') || (*(TempStr
+ 3) == ':') ||
2941 (*(TempStr
+ 2) == '\0') || (*(TempStr
+ 3) == '\0'))) {
2942 return EFI_INVALID_PARAMETER
;
2944 if ((*TempStr
== '0') && (*(TempStr
+ 4) != '\0') &&
2945 (*(TempStr
+ 4) != ':')) {
2946 return EFI_INVALID_PARAMETER
;
2949 if (((*TempStr
== '0') && (*(TempStr
+ 1) == '0') &&
2950 ((*(TempStr
+ 2) == ':') || (*(TempStr
+ 2) == '\0'))) ||
2951 ((*TempStr
== '0') && (*(TempStr
+ 1) == '0') && (*(TempStr
+ 2) == '0') &&
2952 ((*(TempStr
+ 3) == ':') || (*(TempStr
+ 3) == '\0')))) {
2953 return EFI_INVALID_PARAMETER
;
2958 while ((TempStr
[Cnt
] != ':') && (TempStr
[Cnt
] != '\0')) {
2961 if (LeadZeroCnt
== 0) {
2962 if ((Cnt
== 4) && (*TempStr
== '0')) {
2966 if ((Cnt
!= 0) && (Cnt
< 4)) {
2971 if ((Cnt
== 4) && (*TempStr
== '0') && !LeadZero
) {
2972 return EFI_INVALID_PARAMETER
;
2974 if ((Cnt
!= 0) && (Cnt
< 4) && LeadZero
) {
2975 return EFI_INVALID_PARAMETER
;
2979 Ip6Address
->Addr
[Index
] = (UINT8
) (NodeVal
>> 8);
2980 Ip6Address
->Addr
[Index
+ 1] = (UINT8
) (NodeVal
& 0xFF);
2983 // Skip the groups of zeros by ::
2985 if (Short
&& Update
) {
2986 Index
= (UINT8
) (16 - (TailNodeCnt
+ 2) * 2);
2991 if ((!Short
&& Index
!= 16) || (*Ip6Str
!= '\0')) {
2992 return EFI_INVALID_PARAMETER
;
3000 Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS.
3002 @param[in] String The pointer to the Ascii string.
3003 @param[out] Ip4Address The pointer to the converted IPv4 address.
3005 @retval EFI_SUCCESS Convert to IPv4 address successfully.
3006 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.
3007 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.
3013 IN CONST CHAR16
*String
,
3014 OUT EFI_IPv4_ADDRESS
*Ip4Address
3020 if ((String
== NULL
) || (Ip4Address
== NULL
)) {
3021 return EFI_INVALID_PARAMETER
;
3024 Ip4Str
= (CHAR8
*) AllocatePool ((StrLen (String
) + 1) * sizeof (CHAR8
));
3025 if (Ip4Str
== NULL
) {
3026 return EFI_OUT_OF_RESOURCES
;
3029 UnicodeStrToAsciiStr (String
, Ip4Str
);
3031 Status
= NetLibAsciiStrToIp4 (Ip4Str
, Ip4Address
);
3040 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of
3041 the string is defined in RFC 4291 - Text Pepresentation of Addresses.
3043 @param[in] String The pointer to the Ascii string.
3044 @param[out] Ip6Address The pointer to the converted IPv6 address.
3046 @retval EFI_SUCCESS Convert to IPv6 address successfully.
3047 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
3048 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.
3054 IN CONST CHAR16
*String
,
3055 OUT EFI_IPv6_ADDRESS
*Ip6Address
3061 if ((String
== NULL
) || (Ip6Address
== NULL
)) {
3062 return EFI_INVALID_PARAMETER
;
3065 Ip6Str
= (CHAR8
*) AllocatePool ((StrLen (String
) + 1) * sizeof (CHAR8
));
3066 if (Ip6Str
== NULL
) {
3067 return EFI_OUT_OF_RESOURCES
;
3070 UnicodeStrToAsciiStr (String
, Ip6Str
);
3072 Status
= NetLibAsciiStrToIp6 (Ip6Str
, Ip6Address
);
3080 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length.
3081 The format of the string is defined in RFC 4291 - Text Pepresentation of Addresses
3082 Prefixes: ipv6-address/prefix-length.
3084 @param[in] String The pointer to the Ascii string.
3085 @param[out] Ip6Address The pointer to the converted IPv6 address.
3086 @param[out] PrefixLength The pointer to the converted prefix length.
3088 @retval EFI_SUCCESS Convert to IPv6 address successfully.
3089 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
3090 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.
3095 NetLibStrToIp6andPrefix (
3096 IN CONST CHAR16
*String
,
3097 OUT EFI_IPv6_ADDRESS
*Ip6Address
,
3098 OUT UINT8
*PrefixLength
3107 if ((String
== NULL
) || (Ip6Address
== NULL
) || (PrefixLength
== NULL
)) {
3108 return EFI_INVALID_PARAMETER
;
3111 Ip6Str
= (CHAR8
*) AllocatePool ((StrLen (String
) + 1) * sizeof (CHAR8
));
3112 if (Ip6Str
== NULL
) {
3113 return EFI_OUT_OF_RESOURCES
;
3116 UnicodeStrToAsciiStr (String
, Ip6Str
);
3119 // Get the sub string describing prefix length.
3122 while (*TempStr
!= '\0' && (*TempStr
!= '/')) {
3126 if (*TempStr
== '/') {
3127 PrefixStr
= TempStr
+ 1;
3133 // Get the sub string describing IPv6 address and convert it.
3137 Status
= NetLibAsciiStrToIp6 (Ip6Str
, Ip6Address
);
3138 if (EFI_ERROR (Status
)) {
3143 // If input string doesn't indicate the prefix length, return 0xff.
3148 // Convert the string to prefix length
3150 if (PrefixStr
!= NULL
) {
3152 Status
= EFI_INVALID_PARAMETER
;
3154 while (*PrefixStr
!= '\0') {
3155 if (NET_IS_DIGIT (*PrefixStr
)) {
3156 Length
= (UINT8
) (Length
* 10 + (*PrefixStr
- '0'));
3157 if (Length
>= IP6_PREFIX_NUM
) {
3168 *PrefixLength
= Length
;
3169 Status
= EFI_SUCCESS
;
3180 This function obtains the system guid from the smbios table.
3182 @param[out] SystemGuid The pointer of the returned system guid.
3184 @retval EFI_SUCCESS Successfully obtained the system guid.
3185 @retval EFI_NOT_FOUND Did not find the SMBIOS table.
3190 NetLibGetSystemGuid (
3191 OUT EFI_GUID
*SystemGuid
3195 SMBIOS_TABLE_ENTRY_POINT
*SmbiosTable
;
3196 SMBIOS_STRUCTURE_POINTER Smbios
;
3197 SMBIOS_STRUCTURE_POINTER SmbiosEnd
;
3201 Status
= EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid
, (VOID
**) &SmbiosTable
);
3203 if (EFI_ERROR (Status
) || SmbiosTable
== NULL
) {
3204 return EFI_NOT_FOUND
;
3207 Smbios
.Hdr
= (SMBIOS_STRUCTURE
*) (UINTN
) SmbiosTable
->TableAddress
;
3208 SmbiosEnd
.Raw
= (UINT8
*) (UINTN
) (SmbiosTable
->TableAddress
+ SmbiosTable
->TableLength
);
3211 if (Smbios
.Hdr
->Type
== 1) {
3212 if (Smbios
.Hdr
->Length
< 0x19) {
3214 // Older version did not support UUID.
3216 return EFI_NOT_FOUND
;
3220 // SMBIOS tables are byte packed so we need to do a byte copy to
3221 // prevend alignment faults on Itanium-based platform.
3223 CopyMem (SystemGuid
, &Smbios
.Type1
->Uuid
, sizeof (EFI_GUID
));
3228 // Go to the next SMBIOS structure. Each SMBIOS structure may include 2 parts:
3229 // 1. Formatted section; 2. Unformatted string section. So, 2 steps are needed
3230 // to skip one SMBIOS structure.
3234 // Step 1: Skip over formatted section.
3236 String
= (CHAR8
*) (Smbios
.Raw
+ Smbios
.Hdr
->Length
);
3239 // Step 2: Skip over unformated string section.
3243 // Each string is terminated with a NULL(00h) BYTE and the sets of strings
3244 // is terminated with an additional NULL(00h) BYTE.
3246 for ( ; *String
!= 0; String
++) {
3249 if (*(UINT8
*)++String
== 0) {
3251 // Pointer to the next SMBIOS structure.
3253 Smbios
.Raw
= (UINT8
*)++String
;
3257 } while (Smbios
.Raw
< SmbiosEnd
.Raw
);
3258 return EFI_NOT_FOUND
;