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 (Index
= 0; Index
< 15; Index
++) {
233 if (Ip6
->Addr
[Index
] != 0) {
238 Byte
= Ip6
->Addr
[Index
];
240 if (Byte
== 0x0 || Byte
== 0x1) {
248 Switches the endianess of an IPv6 address
250 This function swaps the bytes in a 128-bit IPv6 address to switch the value
251 from little endian to big endian or vice versa. The byte swapped value is
254 @param Ip6 Points to an IPv6 address
256 @return The byte swapped IPv6 address.
261 EFI_IPv6_ADDRESS
*Ip6
267 CopyMem (&High
, Ip6
, sizeof (UINT64
));
268 CopyMem (&Low
, &Ip6
->Addr
[8], sizeof (UINT64
));
270 High
= SwapBytes64 (High
);
271 Low
= SwapBytes64 (Low
);
273 CopyMem (Ip6
, &Low
, sizeof (UINT64
));
274 CopyMem (&Ip6
->Addr
[8], &High
, sizeof (UINT64
));
280 Initialize a random seed using current time.
282 Get current time first. Then initialize a random seed based on some basic
283 mathematics operation on the hour, day, minute, second, nanosecond and year
286 @return The random seed initialized with current time.
298 gRT
->GetTime (&Time
, NULL
);
299 Seed
= (~Time
.Hour
<< 24 | Time
.Day
<< 16 | Time
.Minute
<< 8 | Time
.Second
);
300 Seed
^= Time
.Nanosecond
;
301 Seed
^= Time
.Year
<< 7;
308 Extract a UINT32 from a byte stream.
310 Copy a UINT32 from a byte stream, then converts it from Network
311 byte order to host byte order. Use this function to avoid alignment error.
313 @param[in] Buf The buffer to extract the UINT32.
315 @return The UINT32 extracted.
326 CopyMem (&Value
, Buf
, sizeof (UINT32
));
327 return NTOHL (Value
);
332 Put a UINT32 to the byte stream in network byte order.
334 Converts a UINT32 from host byte order to network byte order. Then copy it to the
337 @param[in, out] Buf The buffer to put the UINT32.
338 @param[in] Data The data to put.
349 CopyMem (Buf
, &Data
, sizeof (UINT32
));
354 Remove the first node entry on the list, and return the removed node entry.
356 Removes the first node Entry from a doubly linked list. It is up to the caller of
357 this function to release the memory used by the first node if that is required. On
358 exit, the removed node is returned.
360 If Head is NULL, then ASSERT().
361 If Head was not initialized, then ASSERT().
362 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
363 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
366 @param[in, out] Head The list header.
368 @return The first node entry that is removed from the list, NULL if the list is empty.
374 IN OUT LIST_ENTRY
*Head
379 ASSERT (Head
!= NULL
);
381 if (IsListEmpty (Head
)) {
385 First
= Head
->ForwardLink
;
386 Head
->ForwardLink
= First
->ForwardLink
;
387 First
->ForwardLink
->BackLink
= Head
;
390 First
->ForwardLink
= (LIST_ENTRY
*) NULL
;
391 First
->BackLink
= (LIST_ENTRY
*) NULL
;
399 Remove the last node entry on the list and and return the removed node entry.
401 Removes the last node entry from a doubly linked list. It is up to the caller of
402 this function to release the memory used by the first node if that is required. On
403 exit, the removed node is returned.
405 If Head is NULL, then ASSERT().
406 If Head was not initialized, then ASSERT().
407 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
408 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
411 @param[in, out] Head The list head.
413 @return The last node entry that is removed from the list, NULL if the list is empty.
419 IN OUT LIST_ENTRY
*Head
424 ASSERT (Head
!= NULL
);
426 if (IsListEmpty (Head
)) {
430 Last
= Head
->BackLink
;
431 Head
->BackLink
= Last
->BackLink
;
432 Last
->BackLink
->ForwardLink
= Head
;
435 Last
->ForwardLink
= (LIST_ENTRY
*) NULL
;
436 Last
->BackLink
= (LIST_ENTRY
*) NULL
;
444 Insert a new node entry after a designated node entry of a doubly linked list.
446 Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry
447 of the doubly linked list.
449 @param[in, out] PrevEntry The previous entry to insert after.
450 @param[in, out] NewEntry The new entry to insert.
456 IN OUT LIST_ENTRY
*PrevEntry
,
457 IN OUT LIST_ENTRY
*NewEntry
460 NewEntry
->BackLink
= PrevEntry
;
461 NewEntry
->ForwardLink
= PrevEntry
->ForwardLink
;
462 PrevEntry
->ForwardLink
->BackLink
= NewEntry
;
463 PrevEntry
->ForwardLink
= NewEntry
;
468 Insert a new node entry before a designated node entry of a doubly linked list.
470 Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry
471 of the doubly linked list.
473 @param[in, out] PostEntry The entry to insert before.
474 @param[in, out] NewEntry The new entry to insert.
479 NetListInsertBefore (
480 IN OUT LIST_ENTRY
*PostEntry
,
481 IN OUT LIST_ENTRY
*NewEntry
484 NewEntry
->ForwardLink
= PostEntry
;
485 NewEntry
->BackLink
= PostEntry
->BackLink
;
486 PostEntry
->BackLink
->ForwardLink
= NewEntry
;
487 PostEntry
->BackLink
= NewEntry
;
492 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
494 Initialize the forward and backward links of two head nodes donated by Map->Used
495 and Map->Recycled of two doubly linked lists.
496 Initializes the count of the <Key, Value> pairs in the netmap to zero.
498 If Map is NULL, then ASSERT().
499 If the address of Map->Used is NULL, then ASSERT().
500 If the address of Map->Recycled is NULl, then ASSERT().
502 @param[in, out] Map The netmap to initialize.
511 ASSERT (Map
!= NULL
);
513 InitializeListHead (&Map
->Used
);
514 InitializeListHead (&Map
->Recycled
);
520 To clean up the netmap, that is, release allocated memories.
522 Removes all nodes of the Used doubly linked list and free memory of all related netmap items.
523 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.
524 The number of the <Key, Value> pairs in the netmap is set to be zero.
526 If Map is NULL, then ASSERT().
528 @param[in, out] Map The netmap to clean up.
541 ASSERT (Map
!= NULL
);
543 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Map
->Used
) {
544 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
546 RemoveEntryList (&Item
->Link
);
549 gBS
->FreePool (Item
);
552 ASSERT ((Map
->Count
== 0) && IsListEmpty (&Map
->Used
));
554 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Map
->Recycled
) {
555 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
557 RemoveEntryList (&Item
->Link
);
558 gBS
->FreePool (Item
);
561 ASSERT (IsListEmpty (&Map
->Recycled
));
566 Test whether the netmap is empty and return true if it is.
568 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.
570 If Map is NULL, then ASSERT().
573 @param[in] Map The net map to test.
575 @return TRUE if the netmap is empty, otherwise FALSE.
584 ASSERT (Map
!= NULL
);
585 return (BOOLEAN
) (Map
->Count
== 0);
590 Return the number of the <Key, Value> pairs in the netmap.
592 @param[in] Map The netmap to get the entry number.
594 @return The entry number in the netmap.
608 Return one allocated item.
610 If the Recycled doubly linked list of the netmap is empty, it will try to allocate
611 a batch of items if there are enough resources and add corresponding nodes to the begining
612 of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove
613 the fist node entry of the Recycled doubly linked list and return the corresponding item.
615 If Map is NULL, then ASSERT().
617 @param[in, out] Map The netmap to allocate item for.
619 @return The allocated item. If NULL, the
620 allocation failed due to resource limit.
632 ASSERT (Map
!= NULL
);
634 Head
= &Map
->Recycled
;
636 if (IsListEmpty (Head
)) {
637 for (Index
= 0; Index
< NET_MAP_INCREAMENT
; Index
++) {
638 Item
= AllocatePool (sizeof (NET_MAP_ITEM
));
648 InsertHeadList (Head
, &Item
->Link
);
652 Item
= NET_LIST_HEAD (Head
, NET_MAP_ITEM
, Link
);
653 NetListRemoveHead (Head
);
660 Allocate an item to save the <Key, Value> pair to the head of the netmap.
662 Allocate an item to save the <Key, Value> pair and add corresponding node entry
663 to the beginning of the Used doubly linked list. The number of the <Key, Value>
664 pairs in the netmap increase by 1.
666 If Map is NULL, then ASSERT().
668 @param[in, out] Map The netmap to insert into.
669 @param[in] Key The user's key.
670 @param[in] Value The user's value for the key.
672 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
673 @retval EFI_SUCCESS The item is inserted to the head.
681 IN VOID
*Value OPTIONAL
686 ASSERT (Map
!= NULL
);
688 Item
= NetMapAllocItem (Map
);
691 return EFI_OUT_OF_RESOURCES
;
696 InsertHeadList (&Map
->Used
, &Item
->Link
);
704 Allocate an item to save the <Key, Value> pair to the tail of the netmap.
706 Allocate an item to save the <Key, Value> pair and add corresponding node entry
707 to the tail of the Used doubly linked list. The number of the <Key, Value>
708 pairs in the netmap increase by 1.
710 If Map is NULL, then ASSERT().
712 @param[in, out] Map The netmap to insert into.
713 @param[in] Key The user's key.
714 @param[in] Value The user's value for the key.
716 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
717 @retval EFI_SUCCESS The item is inserted to the tail.
725 IN VOID
*Value OPTIONAL
730 ASSERT (Map
!= NULL
);
732 Item
= NetMapAllocItem (Map
);
735 return EFI_OUT_OF_RESOURCES
;
740 InsertTailList (&Map
->Used
, &Item
->Link
);
749 Check whether the item is in the Map and return TRUE if it is.
751 @param[in] Map The netmap to search within.
752 @param[in] Item The item to search.
754 @return TRUE if the item is in the netmap, otherwise FALSE.
760 IN NET_MAP_ITEM
*Item
763 LIST_ENTRY
*ListEntry
;
765 NET_LIST_FOR_EACH (ListEntry
, &Map
->Used
) {
766 if (ListEntry
== &Item
->Link
) {
776 Find the key in the netmap and returns the point to the item contains the Key.
778 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every
779 item with the key to search. It returns the point to the item contains the Key if found.
781 If Map is NULL, then ASSERT().
783 @param[in] Map The netmap to search within.
784 @param[in] Key The key to search.
786 @return The point to the item contains the Key, or NULL if Key isn't in the map.
799 ASSERT (Map
!= NULL
);
801 NET_LIST_FOR_EACH (Entry
, &Map
->Used
) {
802 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
804 if (Item
->Key
== Key
) {
814 Remove the node entry of the item from the netmap and return the key of the removed item.
816 Remove the node entry of the item from the Used doubly linked list of the netmap.
817 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
818 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,
819 Value will point to the value of the item. It returns the key of the removed item.
821 If Map is NULL, then ASSERT().
822 If Item is NULL, then ASSERT().
823 if item in not in the netmap, then ASSERT().
825 @param[in, out] Map The netmap to remove the item from.
826 @param[in, out] Item The item to remove.
827 @param[out] Value The variable to receive the value if not NULL.
829 @return The key of the removed item.
836 IN OUT NET_MAP_ITEM
*Item
,
837 OUT VOID
**Value OPTIONAL
840 ASSERT ((Map
!= NULL
) && (Item
!= NULL
));
841 ASSERT (NetItemInMap (Map
, Item
));
843 RemoveEntryList (&Item
->Link
);
845 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
848 *Value
= Item
->Value
;
856 Remove the first node entry on the netmap and return the key of the removed item.
858 Remove the first node entry from the Used doubly linked list of the netmap.
859 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
860 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
861 parameter Value will point to the value of the item. It returns the key of the removed item.
863 If Map is NULL, then ASSERT().
864 If the Used doubly linked list is empty, then ASSERT().
866 @param[in, out] Map The netmap to remove the head from.
867 @param[out] Value The variable to receive the value if not NULL.
869 @return The key of the item removed.
876 OUT VOID
**Value OPTIONAL
882 // Often, it indicates a programming error to remove
883 // the first entry in an empty list
885 ASSERT (Map
&& !IsListEmpty (&Map
->Used
));
887 Item
= NET_LIST_HEAD (&Map
->Used
, NET_MAP_ITEM
, Link
);
888 RemoveEntryList (&Item
->Link
);
890 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
893 *Value
= Item
->Value
;
901 Remove the last node entry on the netmap and return the key of the removed item.
903 Remove the last node entry from the Used doubly linked list of the netmap.
904 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
905 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
906 parameter Value will point to the value of the item. It returns the key of the removed item.
908 If Map is NULL, then ASSERT().
909 If the Used doubly linked list is empty, then ASSERT().
911 @param[in, out] Map The netmap to remove the tail from.
912 @param[out] Value The variable to receive the value if not NULL.
914 @return The key of the item removed.
921 OUT VOID
**Value OPTIONAL
927 // Often, it indicates a programming error to remove
928 // the last entry in an empty list
930 ASSERT (Map
&& !IsListEmpty (&Map
->Used
));
932 Item
= NET_LIST_TAIL (&Map
->Used
, NET_MAP_ITEM
, Link
);
933 RemoveEntryList (&Item
->Link
);
935 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
938 *Value
= Item
->Value
;
946 Iterate through the netmap and call CallBack for each item.
948 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break
949 from the loop. It returns the CallBack's last return value. This function is
950 delete safe for the current item.
952 If Map is NULL, then ASSERT().
953 If CallBack is NULL, then ASSERT().
955 @param[in] Map The Map to iterate through.
956 @param[in] CallBack The callback function to call for each item.
957 @param[in] Arg The opaque parameter to the callback.
959 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item
961 @retval Others It returns the CallBack's last return value.
968 IN NET_MAP_CALLBACK CallBack
,
979 ASSERT ((Map
!= NULL
) && (CallBack
!= NULL
));
983 if (IsListEmpty (Head
)) {
987 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Head
) {
988 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
989 Result
= CallBack (Map
, Item
, Arg
);
991 if (EFI_ERROR (Result
)) {
1001 This is the default unload handle for all the network drivers.
1003 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
1004 Uninstall all the protocols installed in the driver entry point.
1006 @param[in] ImageHandle The drivers' driver image.
1008 @retval EFI_SUCCESS The image is unloaded.
1009 @retval Others Failed to unload the image.
1014 NetLibDefaultUnload (
1015 IN EFI_HANDLE ImageHandle
1019 EFI_HANDLE
*DeviceHandleBuffer
;
1020 UINTN DeviceHandleCount
;
1022 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
1023 EFI_COMPONENT_NAME_PROTOCOL
*ComponentName
;
1024 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
1027 // Get the list of all the handles in the handle database.
1028 // If there is an error getting the list, then the unload
1031 Status
= gBS
->LocateHandleBuffer (
1039 if (EFI_ERROR (Status
)) {
1044 // Disconnect the driver specified by ImageHandle from all
1045 // the devices in the handle database.
1047 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1048 Status
= gBS
->DisconnectController (
1049 DeviceHandleBuffer
[Index
],
1056 // Uninstall all the protocols installed in the driver entry point
1058 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1059 Status
= gBS
->HandleProtocol (
1060 DeviceHandleBuffer
[Index
],
1061 &gEfiDriverBindingProtocolGuid
,
1062 (VOID
**) &DriverBinding
1065 if (EFI_ERROR (Status
)) {
1069 if (DriverBinding
->ImageHandle
!= ImageHandle
) {
1073 gBS
->UninstallProtocolInterface (
1075 &gEfiDriverBindingProtocolGuid
,
1078 Status
= gBS
->HandleProtocol (
1079 DeviceHandleBuffer
[Index
],
1080 &gEfiComponentNameProtocolGuid
,
1081 (VOID
**) &ComponentName
1083 if (!EFI_ERROR (Status
)) {
1084 gBS
->UninstallProtocolInterface (
1086 &gEfiComponentNameProtocolGuid
,
1091 Status
= gBS
->HandleProtocol (
1092 DeviceHandleBuffer
[Index
],
1093 &gEfiComponentName2ProtocolGuid
,
1094 (VOID
**) &ComponentName2
1096 if (!EFI_ERROR (Status
)) {
1097 gBS
->UninstallProtocolInterface (
1099 &gEfiComponentName2ProtocolGuid
,
1106 // Free the buffer containing the list of handles from the handle database
1108 if (DeviceHandleBuffer
!= NULL
) {
1109 gBS
->FreePool (DeviceHandleBuffer
);
1118 Create a child of the service that is identified by ServiceBindingGuid.
1120 Get the ServiceBinding Protocol first, then use it to create a child.
1122 If ServiceBindingGuid is NULL, then ASSERT().
1123 If ChildHandle is NULL, then ASSERT().
1125 @param[in] Controller The controller which has the service installed.
1126 @param[in] Image The image handle used to open service.
1127 @param[in] ServiceBindingGuid The service's Guid.
1128 @param[in, out] ChildHandle The handle to receive the create child.
1130 @retval EFI_SUCCESS The child is successfully created.
1131 @retval Others Failed to create the child.
1136 NetLibCreateServiceChild (
1137 IN EFI_HANDLE Controller
,
1138 IN EFI_HANDLE Image
,
1139 IN EFI_GUID
*ServiceBindingGuid
,
1140 IN OUT EFI_HANDLE
*ChildHandle
1144 EFI_SERVICE_BINDING_PROTOCOL
*Service
;
1147 ASSERT ((ServiceBindingGuid
!= NULL
) && (ChildHandle
!= NULL
));
1150 // Get the ServiceBinding Protocol
1152 Status
= gBS
->OpenProtocol (
1158 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1161 if (EFI_ERROR (Status
)) {
1168 Status
= Service
->CreateChild (Service
, ChildHandle
);
1174 Destory a child of the service that is identified by ServiceBindingGuid.
1176 Get the ServiceBinding Protocol first, then use it to destroy a child.
1178 If ServiceBindingGuid is NULL, then ASSERT().
1180 @param[in] Controller The controller which has the service installed.
1181 @param[in] Image The image handle used to open service.
1182 @param[in] ServiceBindingGuid The service's Guid.
1183 @param[in] ChildHandle The child to destory.
1185 @retval EFI_SUCCESS The child is successfully destoried.
1186 @retval Others Failed to destory the child.
1191 NetLibDestroyServiceChild (
1192 IN EFI_HANDLE Controller
,
1193 IN EFI_HANDLE Image
,
1194 IN EFI_GUID
*ServiceBindingGuid
,
1195 IN EFI_HANDLE ChildHandle
1199 EFI_SERVICE_BINDING_PROTOCOL
*Service
;
1201 ASSERT (ServiceBindingGuid
!= NULL
);
1204 // Get the ServiceBinding Protocol
1206 Status
= gBS
->OpenProtocol (
1212 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1215 if (EFI_ERROR (Status
)) {
1220 // destory the child
1222 Status
= Service
->DestroyChild (Service
, ChildHandle
);
1228 Convert the mac address of the simple network protocol installed on
1229 SnpHandle to a unicode string. Callers are responsible for freeing the
1232 Get the mac address of the Simple Network protocol from the SnpHandle. Then convert
1233 the mac address into a unicode string. It takes 2 unicode characters to represent
1234 a 1 byte binary buffer. Plus one unicode character for the null-terminator.
1237 @param[in] SnpHandle The handle where the simple network protocol is
1239 @param[in] ImageHandle The image handle used to act as the agent handle to
1240 get the simple network protocol.
1241 @param[out] MacString The pointer to store the address of the string
1242 representation of the mac address.
1244 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.
1245 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
1246 @retval Others Failed to open the simple network protocol.
1251 NetLibGetMacString (
1252 IN EFI_HANDLE SnpHandle
,
1253 IN EFI_HANDLE ImageHandle
,
1254 OUT CHAR16
**MacString
1258 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1259 EFI_SIMPLE_NETWORK_MODE
*Mode
;
1266 // Get the Simple Network protocol from the SnpHandle.
1268 Status
= gBS
->OpenProtocol (
1270 &gEfiSimpleNetworkProtocolGuid
,
1274 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1276 if (EFI_ERROR (Status
)) {
1283 // It takes 2 unicode characters to represent a 1 byte binary buffer.
1284 // Plus one unicode character for the null-terminator.
1286 MacAddress
= AllocatePool ((2 * Mode
->HwAddressSize
+ 1) * sizeof (CHAR16
));
1287 if (MacAddress
== NULL
) {
1288 return EFI_OUT_OF_RESOURCES
;
1292 // Convert the mac address into a unicode string.
1294 for (Index
= 0; Index
< Mode
->HwAddressSize
; Index
++) {
1295 MacAddress
[Index
* 2] = (CHAR16
) mNetLibHexStr
[(Mode
->CurrentAddress
.Addr
[Index
] >> 4) & 0x0F];
1296 MacAddress
[Index
* 2 + 1] = (CHAR16
) mNetLibHexStr
[Mode
->CurrentAddress
.Addr
[Index
] & 0x0F];
1299 MacAddress
[Mode
->HwAddressSize
* 2] = L
'\0';
1301 *MacString
= MacAddress
;
1307 Check the default address used by the IPv4 driver is static or dynamic (acquired
1310 If the controller handle does not have the NIC Ip4 Config Protocol installed, the
1311 default address is static. If the EFI variable to save the configuration is not found,
1312 the default address is static. Otherwise, get the result from the EFI variable which
1313 saving the configuration.
1315 @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol
1316 relative with the default address to judge.
1318 @retval TRUE If the default address is static.
1319 @retval FALSE If the default address is acquired from DHCP.
1323 NetLibDefaultAddressIsStatic (
1324 IN EFI_HANDLE Controller
1328 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
1330 NIC_IP4_CONFIG_INFO
*ConfigInfo
;
1332 EFI_STRING ConfigHdr
;
1333 EFI_STRING ConfigResp
;
1334 EFI_STRING AccessProgress
;
1335 EFI_STRING AccessResults
;
1341 AccessProgress
= NULL
;
1342 AccessResults
= NULL
;
1345 Status
= gBS
->LocateProtocol (
1346 &gEfiHiiConfigRoutingProtocolGuid
,
1348 (VOID
**) &HiiConfigRouting
1350 if (EFI_ERROR (Status
)) {
1355 // Construct config request string header
1357 ConfigHdr
= HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid
, EFI_NIC_IP4_CONFIG_VARIABLE
, Controller
);
1358 if (ConfigHdr
== NULL
) {
1362 Len
= StrLen (ConfigHdr
);
1363 ConfigResp
= AllocateZeroPool ((Len
+ NIC_ITEM_CONFIG_SIZE
* 2 + 100) * sizeof (CHAR16
));
1364 if (ConfigResp
== NULL
) {
1367 StrCpy (ConfigResp
, ConfigHdr
);
1369 String
= ConfigResp
+ Len
;
1372 (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16
),
1373 L
"&OFFSET=%04X&WIDTH=%04X",
1374 OFFSET_OF (NIC_IP4_CONFIG_INFO
, Source
),
1378 Status
= HiiConfigRouting
->ExtractConfig (
1384 if (EFI_ERROR (Status
)) {
1388 ConfigInfo
= AllocateZeroPool (sizeof (NIC_ITEM_CONFIG_SIZE
));
1389 if (ConfigInfo
== NULL
) {
1393 ConfigInfo
->Source
= IP4_CONFIG_SOURCE_STATIC
;
1394 Len
= NIC_ITEM_CONFIG_SIZE
;
1395 Status
= HiiConfigRouting
->ConfigToBlock (
1398 (UINT8
*) ConfigInfo
,
1402 if (EFI_ERROR (Status
)) {
1406 IsStatic
= (BOOLEAN
) (ConfigInfo
->Source
== IP4_CONFIG_SOURCE_STATIC
);
1410 if (AccessResults
!= NULL
) {
1411 FreePool (AccessResults
);
1413 if (ConfigInfo
!= NULL
) {
1414 FreePool (ConfigInfo
);
1416 if (ConfigResp
!= NULL
) {
1417 FreePool (ConfigResp
);
1419 if (ConfigHdr
!= NULL
) {
1420 FreePool (ConfigHdr
);
1427 Create an IPv4 device path node.
1429 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.
1430 The header subtype of IPv4 device path node is MSG_IPv4_DP.
1431 The length of the IPv4 device path node in bytes is 19.
1432 Get other info from parameters to make up the whole IPv4 device path node.
1434 @param[in, out] Node Pointer to the IPv4 device path node.
1435 @param[in] Controller The handle where the NIC IP4 config protocol resides.
1436 @param[in] LocalIp The local IPv4 address.
1437 @param[in] LocalPort The local port.
1438 @param[in] RemoteIp The remote IPv4 address.
1439 @param[in] RemotePort The remote port.
1440 @param[in] Protocol The protocol type in the IP header.
1441 @param[in] UseDefaultAddress Whether this instance is using default address or not.
1446 NetLibCreateIPv4DPathNode (
1447 IN OUT IPv4_DEVICE_PATH
*Node
,
1448 IN EFI_HANDLE Controller
,
1449 IN IP4_ADDR LocalIp
,
1450 IN UINT16 LocalPort
,
1451 IN IP4_ADDR RemoteIp
,
1452 IN UINT16 RemotePort
,
1454 IN BOOLEAN UseDefaultAddress
1457 Node
->Header
.Type
= MESSAGING_DEVICE_PATH
;
1458 Node
->Header
.SubType
= MSG_IPv4_DP
;
1459 SetDevicePathNodeLength (&Node
->Header
, 19);
1461 CopyMem (&Node
->LocalIpAddress
, &LocalIp
, sizeof (EFI_IPv4_ADDRESS
));
1462 CopyMem (&Node
->RemoteIpAddress
, &RemoteIp
, sizeof (EFI_IPv4_ADDRESS
));
1464 Node
->LocalPort
= LocalPort
;
1465 Node
->RemotePort
= RemotePort
;
1467 Node
->Protocol
= Protocol
;
1469 if (!UseDefaultAddress
) {
1470 Node
->StaticIpAddress
= TRUE
;
1472 Node
->StaticIpAddress
= NetLibDefaultAddressIsStatic (Controller
);
1478 Find the UNDI/SNP handle from controller and protocol GUID.
1480 For example, IP will open a MNP child to transmit/receive
1481 packets, when MNP is stopped, IP should also be stopped. IP
1482 needs to find its own private data which is related the IP's
1483 service binding instance that is install on UNDI/SNP handle.
1484 Now, the controller is either a MNP or ARP child handle. But
1485 IP opens these handle BY_DRIVER, use that info, we can get the
1488 @param[in] Controller Then protocol handle to check.
1489 @param[in] ProtocolGuid The protocol that is related with the handle.
1491 @return The UNDI/SNP handle or NULL for errors.
1496 NetLibGetNicHandle (
1497 IN EFI_HANDLE Controller
,
1498 IN EFI_GUID
*ProtocolGuid
1501 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenBuffer
;
1507 Status
= gBS
->OpenProtocolInformation (
1514 if (EFI_ERROR (Status
)) {
1520 for (Index
= 0; Index
< OpenCount
; Index
++) {
1521 if (OpenBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) {
1522 Handle
= OpenBuffer
[Index
].ControllerHandle
;
1527 gBS
->FreePool (OpenBuffer
);