4 Copyright (c) 2005 - 2009, Intel Corporation.<BR>
5 All rights reserved. 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/HiiConfigRouting.h>
20 #include <Protocol/ComponentName.h>
21 #include <Protocol/ComponentName2.h>
23 #include <Guid/NicIp4ConfigNvData.h>
25 #include <Library/NetLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/BaseMemoryLib.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/UefiRuntimeServicesTableLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/DevicePathLib.h>
33 #include <Library/HiiLib.h>
34 #include <Library/PrintLib.h>
36 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mNetLibHexStr
[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
38 #define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE
41 // All the supported IP4 maskes in host byte order.
43 IP4_ADDR gIp4AllMasks
[IP4_MASK_NUM
] = {
82 EFI_IPv4_ADDRESS mZeroIp4Addr
= {{0, 0, 0, 0}};
85 Return the length of the mask.
87 Return the length of the mask, the correct value is from 0 to 32.
88 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.
89 NetMask is in the host byte order.
91 @param[in] NetMask The netmask to get the length from.
93 @return The length of the netmask, IP4_MASK_NUM if the mask is invalid.
104 for (Index
= 0; Index
< IP4_MASK_NUM
; Index
++) {
105 if (NetMask
== gIp4AllMasks
[Index
]) {
116 Return the class of the IP address, such as class A, B, C.
117 Addr is in host byte order.
119 The address of class A starts with 0.
120 If the address belong to class A, return IP4_ADDR_CLASSA.
121 The address of class B starts with 10.
122 If the address belong to class B, return IP4_ADDR_CLASSB.
123 The address of class C starts with 110.
124 If the address belong to class C, return IP4_ADDR_CLASSC.
125 The address of class D starts with 1110.
126 If the address belong to class D, return IP4_ADDR_CLASSD.
127 The address of class E starts with 1111.
128 If the address belong to class E, return IP4_ADDR_CLASSE.
131 @param[in] Addr The address to get the class from.
133 @return IP address class, such as IP4_ADDR_CLASSA.
144 ByteOne
= (UINT8
) (Addr
>> 24);
146 if ((ByteOne
& 0x80) == 0) {
147 return IP4_ADDR_CLASSA
;
149 } else if ((ByteOne
& 0xC0) == 0x80) {
150 return IP4_ADDR_CLASSB
;
152 } else if ((ByteOne
& 0xE0) == 0xC0) {
153 return IP4_ADDR_CLASSC
;
155 } else if ((ByteOne
& 0xF0) == 0xE0) {
156 return IP4_ADDR_CLASSD
;
159 return IP4_ADDR_CLASSE
;
166 Check whether the IP is a valid unicast address according to
167 the netmask. If NetMask is zero, use the IP address's class to get the default mask.
169 If Ip is 0, IP is not a valid unicast address.
170 Class D address is used for multicasting and class E address is reserved for future. If Ip
171 belongs to class D or class E, IP is not a valid unicast address.
172 If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address.
174 @param[in] Ip The IP to check against.
175 @param[in] NetMask The mask of the IP.
177 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.
189 Class
= NetGetIpClass (Ip
);
191 if ((Ip
== 0) || (Class
>= IP4_ADDR_CLASSD
)) {
196 NetMask
= gIp4AllMasks
[Class
<< 3];
199 if (((Ip
&~NetMask
) == ~NetMask
) || ((Ip
&~NetMask
) == 0)) {
207 Check whether the incoming IPv6 address is a valid unicast address.
209 If the address is a multicast address has binary 0xFF at the start, it is not
210 a valid unicast address. If the address is unspecified ::, it is not a valid
211 unicast address to be assigned to any node. If the address is loopback address
212 ::1, it is also not a valid unicast address to be assigned to any physical
215 @param[in] Ip6 The IPv6 address to check against.
217 @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE.
222 IN EFI_IPv6_ADDRESS
*Ip6
228 if (Ip6
->Addr
[0] == 0xFF) {
232 for (i
= 0; i
< 15; i
++) {
233 if (Ip6
->Addr
[i
] != 0) {
240 if (t
== 0x0 || t
== 0x1) {
248 Initialize a random seed using current time.
250 Get current time first. Then initialize a random seed based on some basic
251 mathematics operation on the hour, day, minute, second, nanosecond and year
254 @return The random seed initialized with current time.
266 gRT
->GetTime (&Time
, NULL
);
267 Seed
= (~Time
.Hour
<< 24 | Time
.Day
<< 16 | Time
.Minute
<< 8 | Time
.Second
);
268 Seed
^= Time
.Nanosecond
;
269 Seed
^= Time
.Year
<< 7;
276 Extract a UINT32 from a byte stream.
278 Copy a UINT32 from a byte stream, then converts it from Network
279 byte order to host byte order. Use this function to avoid alignment error.
281 @param[in] Buf The buffer to extract the UINT32.
283 @return The UINT32 extracted.
294 CopyMem (&Value
, Buf
, sizeof (UINT32
));
295 return NTOHL (Value
);
300 Put a UINT32 to the byte stream in network byte order.
302 Converts a UINT32 from host byte order to network byte order. Then copy it to the
305 @param[in, out] Buf The buffer to put the UINT32.
306 @param[in] Data The data to put.
317 CopyMem (Buf
, &Data
, sizeof (UINT32
));
322 Remove the first node entry on the list, and return the removed node entry.
324 Removes the first node Entry from a doubly linked list. It is up to the caller of
325 this function to release the memory used by the first node if that is required. On
326 exit, the removed node is returned.
328 If Head is NULL, then ASSERT().
329 If Head was not initialized, then ASSERT().
330 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
331 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
334 @param[in, out] Head The list header.
336 @return The first node entry that is removed from the list, NULL if the list is empty.
342 IN OUT LIST_ENTRY
*Head
347 ASSERT (Head
!= NULL
);
349 if (IsListEmpty (Head
)) {
353 First
= Head
->ForwardLink
;
354 Head
->ForwardLink
= First
->ForwardLink
;
355 First
->ForwardLink
->BackLink
= Head
;
358 First
->ForwardLink
= (LIST_ENTRY
*) NULL
;
359 First
->BackLink
= (LIST_ENTRY
*) NULL
;
367 Remove the last node entry on the list and and return the removed node entry.
369 Removes the last node entry from a doubly linked list. It is up to the caller of
370 this function to release the memory used by the first node if that is required. On
371 exit, the removed node is returned.
373 If Head is NULL, then ASSERT().
374 If Head was not initialized, then ASSERT().
375 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
376 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
379 @param[in, out] Head The list head.
381 @return The last node entry that is removed from the list, NULL if the list is empty.
387 IN OUT LIST_ENTRY
*Head
392 ASSERT (Head
!= NULL
);
394 if (IsListEmpty (Head
)) {
398 Last
= Head
->BackLink
;
399 Head
->BackLink
= Last
->BackLink
;
400 Last
->BackLink
->ForwardLink
= Head
;
403 Last
->ForwardLink
= (LIST_ENTRY
*) NULL
;
404 Last
->BackLink
= (LIST_ENTRY
*) NULL
;
412 Insert a new node entry after a designated node entry of a doubly linked list.
414 Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry
415 of the doubly linked list.
417 @param[in, out] PrevEntry The previous entry to insert after.
418 @param[in, out] NewEntry The new entry to insert.
424 IN OUT LIST_ENTRY
*PrevEntry
,
425 IN OUT LIST_ENTRY
*NewEntry
428 NewEntry
->BackLink
= PrevEntry
;
429 NewEntry
->ForwardLink
= PrevEntry
->ForwardLink
;
430 PrevEntry
->ForwardLink
->BackLink
= NewEntry
;
431 PrevEntry
->ForwardLink
= NewEntry
;
436 Insert a new node entry before a designated node entry of a doubly linked list.
438 Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry
439 of the doubly linked list.
441 @param[in, out] PostEntry The entry to insert before.
442 @param[in, out] NewEntry The new entry to insert.
447 NetListInsertBefore (
448 IN OUT LIST_ENTRY
*PostEntry
,
449 IN OUT LIST_ENTRY
*NewEntry
452 NewEntry
->ForwardLink
= PostEntry
;
453 NewEntry
->BackLink
= PostEntry
->BackLink
;
454 PostEntry
->BackLink
->ForwardLink
= NewEntry
;
455 PostEntry
->BackLink
= NewEntry
;
460 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
462 Initialize the forward and backward links of two head nodes donated by Map->Used
463 and Map->Recycled of two doubly linked lists.
464 Initializes the count of the <Key, Value> pairs in the netmap to zero.
466 If Map is NULL, then ASSERT().
467 If the address of Map->Used is NULL, then ASSERT().
468 If the address of Map->Recycled is NULl, then ASSERT().
470 @param[in, out] Map The netmap to initialize.
479 ASSERT (Map
!= NULL
);
481 InitializeListHead (&Map
->Used
);
482 InitializeListHead (&Map
->Recycled
);
488 To clean up the netmap, that is, release allocated memories.
490 Removes all nodes of the Used doubly linked list and free memory of all related netmap items.
491 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.
492 The number of the <Key, Value> pairs in the netmap is set to be zero.
494 If Map is NULL, then ASSERT().
496 @param[in, out] Map The netmap to clean up.
509 ASSERT (Map
!= NULL
);
511 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Map
->Used
) {
512 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
514 RemoveEntryList (&Item
->Link
);
517 gBS
->FreePool (Item
);
520 ASSERT ((Map
->Count
== 0) && IsListEmpty (&Map
->Used
));
522 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Map
->Recycled
) {
523 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
525 RemoveEntryList (&Item
->Link
);
526 gBS
->FreePool (Item
);
529 ASSERT (IsListEmpty (&Map
->Recycled
));
534 Test whether the netmap is empty and return true if it is.
536 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.
538 If Map is NULL, then ASSERT().
541 @param[in] Map The net map to test.
543 @return TRUE if the netmap is empty, otherwise FALSE.
552 ASSERT (Map
!= NULL
);
553 return (BOOLEAN
) (Map
->Count
== 0);
558 Return the number of the <Key, Value> pairs in the netmap.
560 @param[in] Map The netmap to get the entry number.
562 @return The entry number in the netmap.
576 Return one allocated item.
578 If the Recycled doubly linked list of the netmap is empty, it will try to allocate
579 a batch of items if there are enough resources and add corresponding nodes to the begining
580 of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove
581 the fist node entry of the Recycled doubly linked list and return the corresponding item.
583 If Map is NULL, then ASSERT().
585 @param[in, out] Map The netmap to allocate item for.
587 @return The allocated item. If NULL, the
588 allocation failed due to resource limit.
600 ASSERT (Map
!= NULL
);
602 Head
= &Map
->Recycled
;
604 if (IsListEmpty (Head
)) {
605 for (Index
= 0; Index
< NET_MAP_INCREAMENT
; Index
++) {
606 Item
= AllocatePool (sizeof (NET_MAP_ITEM
));
616 InsertHeadList (Head
, &Item
->Link
);
620 Item
= NET_LIST_HEAD (Head
, NET_MAP_ITEM
, Link
);
621 NetListRemoveHead (Head
);
628 Allocate an item to save the <Key, Value> pair to the head of the netmap.
630 Allocate an item to save the <Key, Value> pair and add corresponding node entry
631 to the beginning of the Used doubly linked list. The number of the <Key, Value>
632 pairs in the netmap increase by 1.
634 If Map is NULL, then ASSERT().
636 @param[in, out] Map The netmap to insert into.
637 @param[in] Key The user's key.
638 @param[in] Value The user's value for the key.
640 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
641 @retval EFI_SUCCESS The item is inserted to the head.
649 IN VOID
*Value OPTIONAL
654 ASSERT (Map
!= NULL
);
656 Item
= NetMapAllocItem (Map
);
659 return EFI_OUT_OF_RESOURCES
;
664 InsertHeadList (&Map
->Used
, &Item
->Link
);
672 Allocate an item to save the <Key, Value> pair to the tail of the netmap.
674 Allocate an item to save the <Key, Value> pair and add corresponding node entry
675 to the tail of the Used doubly linked list. The number of the <Key, Value>
676 pairs in the netmap increase by 1.
678 If Map is NULL, then ASSERT().
680 @param[in, out] Map The netmap to insert into.
681 @param[in] Key The user's key.
682 @param[in] Value The user's value for the key.
684 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
685 @retval EFI_SUCCESS The item is inserted to the tail.
693 IN VOID
*Value OPTIONAL
698 ASSERT (Map
!= NULL
);
700 Item
= NetMapAllocItem (Map
);
703 return EFI_OUT_OF_RESOURCES
;
708 InsertTailList (&Map
->Used
, &Item
->Link
);
717 Check whether the item is in the Map and return TRUE if it is.
719 @param[in] Map The netmap to search within.
720 @param[in] Item The item to search.
722 @return TRUE if the item is in the netmap, otherwise FALSE.
728 IN NET_MAP_ITEM
*Item
731 LIST_ENTRY
*ListEntry
;
733 NET_LIST_FOR_EACH (ListEntry
, &Map
->Used
) {
734 if (ListEntry
== &Item
->Link
) {
744 Find the key in the netmap and returns the point to the item contains the Key.
746 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every
747 item with the key to search. It returns the point to the item contains the Key if found.
749 If Map is NULL, then ASSERT().
751 @param[in] Map The netmap to search within.
752 @param[in] Key The key to search.
754 @return The point to the item contains the Key, or NULL if Key isn't in the map.
767 ASSERT (Map
!= NULL
);
769 NET_LIST_FOR_EACH (Entry
, &Map
->Used
) {
770 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
772 if (Item
->Key
== Key
) {
782 Remove the node entry of the item from the netmap and return the key of the removed item.
784 Remove the node entry of the item from the Used doubly linked list of the netmap.
785 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
786 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,
787 Value will point to the value of the item. It returns the key of the removed item.
789 If Map is NULL, then ASSERT().
790 If Item is NULL, then ASSERT().
791 if item in not in the netmap, then ASSERT().
793 @param[in, out] Map The netmap to remove the item from.
794 @param[in, out] Item The item to remove.
795 @param[out] Value The variable to receive the value if not NULL.
797 @return The key of the removed item.
804 IN OUT NET_MAP_ITEM
*Item
,
805 OUT VOID
**Value OPTIONAL
808 ASSERT ((Map
!= NULL
) && (Item
!= NULL
));
809 ASSERT (NetItemInMap (Map
, Item
));
811 RemoveEntryList (&Item
->Link
);
813 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
816 *Value
= Item
->Value
;
824 Remove the first node entry on the netmap and return the key of the removed item.
826 Remove the first node entry from the Used doubly linked list of the netmap.
827 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
828 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
829 parameter Value will point to the value of the item. It returns the key of the removed item.
831 If Map is NULL, then ASSERT().
832 If the Used doubly linked list is empty, then ASSERT().
834 @param[in, out] Map The netmap to remove the head from.
835 @param[out] Value The variable to receive the value if not NULL.
837 @return The key of the item removed.
844 OUT VOID
**Value OPTIONAL
850 // Often, it indicates a programming error to remove
851 // the first entry in an empty list
853 ASSERT (Map
&& !IsListEmpty (&Map
->Used
));
855 Item
= NET_LIST_HEAD (&Map
->Used
, NET_MAP_ITEM
, Link
);
856 RemoveEntryList (&Item
->Link
);
858 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
861 *Value
= Item
->Value
;
869 Remove the last node entry on the netmap and return the key of the removed item.
871 Remove the last node entry from the Used doubly linked list of the netmap.
872 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
873 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
874 parameter Value will point to the value of the item. It returns the key of the removed item.
876 If Map is NULL, then ASSERT().
877 If the Used doubly linked list is empty, then ASSERT().
879 @param[in, out] Map The netmap to remove the tail from.
880 @param[out] Value The variable to receive the value if not NULL.
882 @return The key of the item removed.
889 OUT VOID
**Value OPTIONAL
895 // Often, it indicates a programming error to remove
896 // the last entry in an empty list
898 ASSERT (Map
&& !IsListEmpty (&Map
->Used
));
900 Item
= NET_LIST_TAIL (&Map
->Used
, NET_MAP_ITEM
, Link
);
901 RemoveEntryList (&Item
->Link
);
903 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
906 *Value
= Item
->Value
;
914 Iterate through the netmap and call CallBack for each item.
916 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break
917 from the loop. It returns the CallBack's last return value. This function is
918 delete safe for the current item.
920 If Map is NULL, then ASSERT().
921 If CallBack is NULL, then ASSERT().
923 @param[in] Map The Map to iterate through.
924 @param[in] CallBack The callback function to call for each item.
925 @param[in] Arg The opaque parameter to the callback.
927 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item
929 @retval Others It returns the CallBack's last return value.
936 IN NET_MAP_CALLBACK CallBack
,
947 ASSERT ((Map
!= NULL
) && (CallBack
!= NULL
));
951 if (IsListEmpty (Head
)) {
955 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Head
) {
956 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
957 Result
= CallBack (Map
, Item
, Arg
);
959 if (EFI_ERROR (Result
)) {
969 This is the default unload handle for all the network drivers.
971 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
972 Uninstall all the protocols installed in the driver entry point.
974 @param[in] ImageHandle The drivers' driver image.
976 @retval EFI_SUCCESS The image is unloaded.
977 @retval Others Failed to unload the image.
982 NetLibDefaultUnload (
983 IN EFI_HANDLE ImageHandle
987 EFI_HANDLE
*DeviceHandleBuffer
;
988 UINTN DeviceHandleCount
;
990 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
991 EFI_COMPONENT_NAME_PROTOCOL
*ComponentName
;
992 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
995 // Get the list of all the handles in the handle database.
996 // If there is an error getting the list, then the unload
999 Status
= gBS
->LocateHandleBuffer (
1007 if (EFI_ERROR (Status
)) {
1012 // Disconnect the driver specified by ImageHandle from all
1013 // the devices in the handle database.
1015 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1016 Status
= gBS
->DisconnectController (
1017 DeviceHandleBuffer
[Index
],
1024 // Uninstall all the protocols installed in the driver entry point
1026 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1027 Status
= gBS
->HandleProtocol (
1028 DeviceHandleBuffer
[Index
],
1029 &gEfiDriverBindingProtocolGuid
,
1030 (VOID
**) &DriverBinding
1033 if (EFI_ERROR (Status
)) {
1037 if (DriverBinding
->ImageHandle
!= ImageHandle
) {
1041 gBS
->UninstallProtocolInterface (
1043 &gEfiDriverBindingProtocolGuid
,
1046 Status
= gBS
->HandleProtocol (
1047 DeviceHandleBuffer
[Index
],
1048 &gEfiComponentNameProtocolGuid
,
1049 (VOID
**) &ComponentName
1051 if (!EFI_ERROR (Status
)) {
1052 gBS
->UninstallProtocolInterface (
1054 &gEfiComponentNameProtocolGuid
,
1059 Status
= gBS
->HandleProtocol (
1060 DeviceHandleBuffer
[Index
],
1061 &gEfiComponentName2ProtocolGuid
,
1062 (VOID
**) &ComponentName2
1064 if (!EFI_ERROR (Status
)) {
1065 gBS
->UninstallProtocolInterface (
1067 &gEfiComponentName2ProtocolGuid
,
1074 // Free the buffer containing the list of handles from the handle database
1076 if (DeviceHandleBuffer
!= NULL
) {
1077 gBS
->FreePool (DeviceHandleBuffer
);
1086 Create a child of the service that is identified by ServiceBindingGuid.
1088 Get the ServiceBinding Protocol first, then use it to create a child.
1090 If ServiceBindingGuid is NULL, then ASSERT().
1091 If ChildHandle is NULL, then ASSERT().
1093 @param[in] Controller The controller which has the service installed.
1094 @param[in] Image The image handle used to open service.
1095 @param[in] ServiceBindingGuid The service's Guid.
1096 @param[in, out] ChildHandle The handle to receive the create child.
1098 @retval EFI_SUCCESS The child is successfully created.
1099 @retval Others Failed to create the child.
1104 NetLibCreateServiceChild (
1105 IN EFI_HANDLE Controller
,
1106 IN EFI_HANDLE Image
,
1107 IN EFI_GUID
*ServiceBindingGuid
,
1108 IN OUT EFI_HANDLE
*ChildHandle
1112 EFI_SERVICE_BINDING_PROTOCOL
*Service
;
1115 ASSERT ((ServiceBindingGuid
!= NULL
) && (ChildHandle
!= NULL
));
1118 // Get the ServiceBinding Protocol
1120 Status
= gBS
->OpenProtocol (
1126 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1129 if (EFI_ERROR (Status
)) {
1136 Status
= Service
->CreateChild (Service
, ChildHandle
);
1142 Destory a child of the service that is identified by ServiceBindingGuid.
1144 Get the ServiceBinding Protocol first, then use it to destroy a child.
1146 If ServiceBindingGuid is NULL, then ASSERT().
1148 @param[in] Controller The controller which has the service installed.
1149 @param[in] Image The image handle used to open service.
1150 @param[in] ServiceBindingGuid The service's Guid.
1151 @param[in] ChildHandle The child to destory.
1153 @retval EFI_SUCCESS The child is successfully destoried.
1154 @retval Others Failed to destory the child.
1159 NetLibDestroyServiceChild (
1160 IN EFI_HANDLE Controller
,
1161 IN EFI_HANDLE Image
,
1162 IN EFI_GUID
*ServiceBindingGuid
,
1163 IN EFI_HANDLE ChildHandle
1167 EFI_SERVICE_BINDING_PROTOCOL
*Service
;
1169 ASSERT (ServiceBindingGuid
!= NULL
);
1172 // Get the ServiceBinding Protocol
1174 Status
= gBS
->OpenProtocol (
1180 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1183 if (EFI_ERROR (Status
)) {
1188 // destory the child
1190 Status
= Service
->DestroyChild (Service
, ChildHandle
);
1196 Convert the mac address of the simple network protocol installed on
1197 SnpHandle to a unicode string. Callers are responsible for freeing the
1200 Get the mac address of the Simple Network protocol from the SnpHandle. Then convert
1201 the mac address into a unicode string. It takes 2 unicode characters to represent
1202 a 1 byte binary buffer. Plus one unicode character for the null-terminator.
1205 @param[in] SnpHandle The handle where the simple network protocol is
1207 @param[in] ImageHandle The image handle used to act as the agent handle to
1208 get the simple network protocol.
1209 @param[out] MacString The pointer to store the address of the string
1210 representation of the mac address.
1212 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.
1213 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
1214 @retval Others Failed to open the simple network protocol.
1219 NetLibGetMacString (
1220 IN EFI_HANDLE SnpHandle
,
1221 IN EFI_HANDLE ImageHandle
,
1222 OUT CHAR16
**MacString
1226 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1227 EFI_SIMPLE_NETWORK_MODE
*Mode
;
1234 // Get the Simple Network protocol from the SnpHandle.
1236 Status
= gBS
->OpenProtocol (
1238 &gEfiSimpleNetworkProtocolGuid
,
1242 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1244 if (EFI_ERROR (Status
)) {
1251 // It takes 2 unicode characters to represent a 1 byte binary buffer.
1252 // Plus one unicode character for the null-terminator.
1254 MacAddress
= AllocatePool ((2 * Mode
->HwAddressSize
+ 1) * sizeof (CHAR16
));
1255 if (MacAddress
== NULL
) {
1256 return EFI_OUT_OF_RESOURCES
;
1260 // Convert the mac address into a unicode string.
1262 for (Index
= 0; Index
< Mode
->HwAddressSize
; Index
++) {
1263 MacAddress
[Index
* 2] = (CHAR16
) mNetLibHexStr
[(Mode
->CurrentAddress
.Addr
[Index
] >> 4) & 0x0F];
1264 MacAddress
[Index
* 2 + 1] = (CHAR16
) mNetLibHexStr
[Mode
->CurrentAddress
.Addr
[Index
] & 0x0F];
1267 MacAddress
[Mode
->HwAddressSize
* 2] = L
'\0';
1269 *MacString
= MacAddress
;
1275 Check the default address used by the IPv4 driver is static or dynamic (acquired
1278 If the controller handle does not have the NIC Ip4 Config Protocol installed, the
1279 default address is static. If the EFI variable to save the configuration is not found,
1280 the default address is static. Otherwise, get the result from the EFI variable which
1281 saving the configuration.
1283 @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol
1284 relative with the default address to judge.
1286 @retval TRUE If the default address is static.
1287 @retval FALSE If the default address is acquired from DHCP.
1291 NetLibDefaultAddressIsStatic (
1292 IN EFI_HANDLE Controller
1296 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
1298 NIC_IP4_CONFIG_INFO
*ConfigInfo
;
1300 EFI_STRING ConfigHdr
;
1301 EFI_STRING ConfigResp
;
1302 EFI_STRING AccessProgress
;
1303 EFI_STRING AccessResults
;
1309 AccessProgress
= NULL
;
1310 AccessResults
= NULL
;
1313 Status
= gBS
->LocateProtocol (
1314 &gEfiHiiConfigRoutingProtocolGuid
,
1316 (VOID
**) &HiiConfigRouting
1318 if (EFI_ERROR (Status
)) {
1323 // Construct config request string header
1325 ConfigHdr
= HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid
, EFI_NIC_IP4_CONFIG_VARIABLE
, Controller
);
1326 if (ConfigHdr
== NULL
) {
1330 Len
= StrLen (ConfigHdr
);
1331 ConfigResp
= AllocateZeroPool ((Len
+ NIC_ITEM_CONFIG_SIZE
* 2 + 100) * sizeof (CHAR16
));
1332 if (ConfigResp
== NULL
) {
1335 StrCpy (ConfigResp
, ConfigHdr
);
1337 String
= ConfigResp
+ Len
;
1340 (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16
),
1341 L
"&OFFSET=%04X&WIDTH=%04X",
1342 OFFSET_OF (NIC_IP4_CONFIG_INFO
, Source
),
1346 Status
= HiiConfigRouting
->ExtractConfig (
1352 if (EFI_ERROR (Status
)) {
1356 ConfigInfo
= AllocateZeroPool (sizeof (NIC_ITEM_CONFIG_SIZE
));
1357 if (ConfigInfo
== NULL
) {
1361 ConfigInfo
->Source
= IP4_CONFIG_SOURCE_STATIC
;
1362 Len
= NIC_ITEM_CONFIG_SIZE
;
1363 Status
= HiiConfigRouting
->ConfigToBlock (
1366 (UINT8
*) ConfigInfo
,
1370 if (EFI_ERROR (Status
)) {
1374 IsStatic
= (BOOLEAN
) (ConfigInfo
->Source
== IP4_CONFIG_SOURCE_STATIC
);
1378 if (AccessResults
!= NULL
) {
1379 FreePool (AccessResults
);
1381 if (ConfigInfo
!= NULL
) {
1382 FreePool (ConfigInfo
);
1384 if (ConfigResp
!= NULL
) {
1385 FreePool (ConfigResp
);
1387 if (ConfigHdr
!= NULL
) {
1388 FreePool (ConfigHdr
);
1395 Create an IPv4 device path node.
1397 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.
1398 The header subtype of IPv4 device path node is MSG_IPv4_DP.
1399 The length of the IPv4 device path node in bytes is 19.
1400 Get other info from parameters to make up the whole IPv4 device path node.
1402 @param[in, out] Node Pointer to the IPv4 device path node.
1403 @param[in] Controller The handle where the NIC IP4 config protocol resides.
1404 @param[in] LocalIp The local IPv4 address.
1405 @param[in] LocalPort The local port.
1406 @param[in] RemoteIp The remote IPv4 address.
1407 @param[in] RemotePort The remote port.
1408 @param[in] Protocol The protocol type in the IP header.
1409 @param[in] UseDefaultAddress Whether this instance is using default address or not.
1414 NetLibCreateIPv4DPathNode (
1415 IN OUT IPv4_DEVICE_PATH
*Node
,
1416 IN EFI_HANDLE Controller
,
1417 IN IP4_ADDR LocalIp
,
1418 IN UINT16 LocalPort
,
1419 IN IP4_ADDR RemoteIp
,
1420 IN UINT16 RemotePort
,
1422 IN BOOLEAN UseDefaultAddress
1425 Node
->Header
.Type
= MESSAGING_DEVICE_PATH
;
1426 Node
->Header
.SubType
= MSG_IPv4_DP
;
1427 SetDevicePathNodeLength (&Node
->Header
, 19);
1429 CopyMem (&Node
->LocalIpAddress
, &LocalIp
, sizeof (EFI_IPv4_ADDRESS
));
1430 CopyMem (&Node
->RemoteIpAddress
, &RemoteIp
, sizeof (EFI_IPv4_ADDRESS
));
1432 Node
->LocalPort
= LocalPort
;
1433 Node
->RemotePort
= RemotePort
;
1435 Node
->Protocol
= Protocol
;
1437 if (!UseDefaultAddress
) {
1438 Node
->StaticIpAddress
= TRUE
;
1440 Node
->StaticIpAddress
= NetLibDefaultAddressIsStatic (Controller
);
1446 Find the UNDI/SNP handle from controller and protocol GUID.
1448 For example, IP will open a MNP child to transmit/receive
1449 packets, when MNP is stopped, IP should also be stopped. IP
1450 needs to find its own private data which is related the IP's
1451 service binding instance that is install on UNDI/SNP handle.
1452 Now, the controller is either a MNP or ARP child handle. But
1453 IP opens these handle BY_DRIVER, use that info, we can get the
1456 @param[in] Controller Then protocol handle to check.
1457 @param[in] ProtocolGuid The protocol that is related with the handle.
1459 @return The UNDI/SNP handle or NULL for errors.
1464 NetLibGetNicHandle (
1465 IN EFI_HANDLE Controller
,
1466 IN EFI_GUID
*ProtocolGuid
1469 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenBuffer
;
1475 Status
= gBS
->OpenProtocolInformation (
1482 if (EFI_ERROR (Status
)) {
1488 for (Index
= 0; Index
< OpenCount
; Index
++) {
1489 if (OpenBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) {
1490 Handle
= OpenBuffer
[Index
].ControllerHandle
;
1495 gBS
->FreePool (OpenBuffer
);