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/ServiceBinding.h>
17 #include <Protocol/SimpleNetwork.h>
18 #include <Protocol/HiiConfigRouting.h>
19 #include <Protocol/ComponentName.h>
20 #include <Protocol/ComponentName2.h>
21 #include <Protocol/Dpc.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 EFI_DPC_PROTOCOL
*mDpc
= NULL
;
38 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mNetLibHexStr
[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
40 #define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE
43 // All the supported IP4 maskes in host byte order.
45 IP4_ADDR gIp4AllMasks
[IP4_MASK_NUM
] = {
84 EFI_IPv4_ADDRESS mZeroIp4Addr
= {{0, 0, 0, 0}};
87 Return the length of the mask.
89 Return the length of the mask, the correct value is from 0 to 32.
90 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.
91 NetMask is in the host byte order.
93 @param[in] NetMask The netmask to get the length from.
95 @return The length of the netmask, IP4_MASK_NUM if the mask is invalid.
106 for (Index
= 0; Index
< IP4_MASK_NUM
; Index
++) {
107 if (NetMask
== gIp4AllMasks
[Index
]) {
118 Return the class of the IP address, such as class A, B, C.
119 Addr is in host byte order.
121 The address of class A starts with 0.
122 If the address belong to class A, return IP4_ADDR_CLASSA.
123 The address of class B starts with 10.
124 If the address belong to class B, return IP4_ADDR_CLASSB.
125 The address of class C starts with 110.
126 If the address belong to class C, return IP4_ADDR_CLASSC.
127 The address of class D starts with 1110.
128 If the address belong to class D, return IP4_ADDR_CLASSD.
129 The address of class E starts with 1111.
130 If the address belong to class E, return IP4_ADDR_CLASSE.
133 @param[in] Addr The address to get the class from.
135 @return IP address class, such as IP4_ADDR_CLASSA.
146 ByteOne
= (UINT8
) (Addr
>> 24);
148 if ((ByteOne
& 0x80) == 0) {
149 return IP4_ADDR_CLASSA
;
151 } else if ((ByteOne
& 0xC0) == 0x80) {
152 return IP4_ADDR_CLASSB
;
154 } else if ((ByteOne
& 0xE0) == 0xC0) {
155 return IP4_ADDR_CLASSC
;
157 } else if ((ByteOne
& 0xF0) == 0xE0) {
158 return IP4_ADDR_CLASSD
;
161 return IP4_ADDR_CLASSE
;
168 Check whether the IP is a valid unicast address according to
169 the netmask. If NetMask is zero, use the IP address's class to get the default mask.
171 If Ip is 0, IP is not a valid unicast address.
172 Class D address is used for multicasting and class E address is reserved for future. If Ip
173 belongs to class D or class E, IP is not a valid unicast address.
174 If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address.
176 @param[in] Ip The IP to check against.
177 @param[in] NetMask The mask of the IP.
179 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.
191 Class
= NetGetIpClass (Ip
);
193 if ((Ip
== 0) || (Class
>= IP4_ADDR_CLASSD
)) {
198 NetMask
= gIp4AllMasks
[Class
<< 3];
201 if (((Ip
&~NetMask
) == ~NetMask
) || ((Ip
&~NetMask
) == 0)) {
210 Initialize a random seed using current time.
212 Get current time first. Then initialize a random seed based on some basic
213 mathematics operation on the hour, day, minute, second, nanosecond and year
216 @return The random seed initialized with current time.
228 gRT
->GetTime (&Time
, NULL
);
229 Seed
= (~Time
.Hour
<< 24 | Time
.Day
<< 16 | Time
.Minute
<< 8 | Time
.Second
);
230 Seed
^= Time
.Nanosecond
;
231 Seed
^= Time
.Year
<< 7;
238 Extract a UINT32 from a byte stream.
240 Copy a UINT32 from a byte stream, then converts it from Network
241 byte order to host byte order. Use this function to avoid alignment error.
243 @param[in] Buf The buffer to extract the UINT32.
245 @return The UINT32 extracted.
256 CopyMem (&Value
, Buf
, sizeof (UINT32
));
257 return NTOHL (Value
);
262 Put a UINT32 to the byte stream in network byte order.
264 Converts a UINT32 from host byte order to network byte order. Then copy it to the
267 @param[in, out] Buf The buffer to put the UINT32.
268 @param[in] Data The data to put.
279 CopyMem (Buf
, &Data
, sizeof (UINT32
));
284 Remove the first node entry on the list, and return the removed node entry.
286 Removes the first node Entry from a doubly linked list. It is up to the caller of
287 this function to release the memory used by the first node if that is required. On
288 exit, the removed node is returned.
290 If Head is NULL, then ASSERT().
291 If Head was not initialized, then ASSERT().
292 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
293 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
296 @param[in, out] Head The list header.
298 @return The first node entry that is removed from the list, NULL if the list is empty.
304 IN OUT LIST_ENTRY
*Head
309 ASSERT (Head
!= NULL
);
311 if (IsListEmpty (Head
)) {
315 First
= Head
->ForwardLink
;
316 Head
->ForwardLink
= First
->ForwardLink
;
317 First
->ForwardLink
->BackLink
= Head
;
320 First
->ForwardLink
= (LIST_ENTRY
*) NULL
;
321 First
->BackLink
= (LIST_ENTRY
*) NULL
;
329 Remove the last node entry on the list and and return the removed node entry.
331 Removes the last node entry from a doubly linked list. It is up to the caller of
332 this function to release the memory used by the first node if that is required. On
333 exit, the removed node is returned.
335 If Head is NULL, then ASSERT().
336 If Head was not initialized, then ASSERT().
337 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
338 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
341 @param[in, out] Head The list head.
343 @return The last node entry that is removed from the list, NULL if the list is empty.
349 IN OUT LIST_ENTRY
*Head
354 ASSERT (Head
!= NULL
);
356 if (IsListEmpty (Head
)) {
360 Last
= Head
->BackLink
;
361 Head
->BackLink
= Last
->BackLink
;
362 Last
->BackLink
->ForwardLink
= Head
;
365 Last
->ForwardLink
= (LIST_ENTRY
*) NULL
;
366 Last
->BackLink
= (LIST_ENTRY
*) NULL
;
374 Insert a new node entry after a designated node entry of a doubly linked list.
376 Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry
377 of the doubly linked list.
379 @param[in, out] PrevEntry The previous entry to insert after.
380 @param[in, out] NewEntry The new entry to insert.
386 IN OUT LIST_ENTRY
*PrevEntry
,
387 IN OUT LIST_ENTRY
*NewEntry
390 NewEntry
->BackLink
= PrevEntry
;
391 NewEntry
->ForwardLink
= PrevEntry
->ForwardLink
;
392 PrevEntry
->ForwardLink
->BackLink
= NewEntry
;
393 PrevEntry
->ForwardLink
= NewEntry
;
398 Insert a new node entry before a designated node entry of a doubly linked list.
400 Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry
401 of the doubly linked list.
403 @param[in, out] PostEntry The entry to insert before.
404 @param[in, out] NewEntry The new entry to insert.
409 NetListInsertBefore (
410 IN OUT LIST_ENTRY
*PostEntry
,
411 IN OUT LIST_ENTRY
*NewEntry
414 NewEntry
->ForwardLink
= PostEntry
;
415 NewEntry
->BackLink
= PostEntry
->BackLink
;
416 PostEntry
->BackLink
->ForwardLink
= NewEntry
;
417 PostEntry
->BackLink
= NewEntry
;
422 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
424 Initialize the forward and backward links of two head nodes donated by Map->Used
425 and Map->Recycled of two doubly linked lists.
426 Initializes the count of the <Key, Value> pairs in the netmap to zero.
428 If Map is NULL, then ASSERT().
429 If the address of Map->Used is NULL, then ASSERT().
430 If the address of Map->Recycled is NULl, then ASSERT().
432 @param[in, out] Map The netmap to initialize.
441 ASSERT (Map
!= NULL
);
443 InitializeListHead (&Map
->Used
);
444 InitializeListHead (&Map
->Recycled
);
450 To clean up the netmap, that is, release allocated memories.
452 Removes all nodes of the Used doubly linked list and free memory of all related netmap items.
453 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.
454 The number of the <Key, Value> pairs in the netmap is set to be zero.
456 If Map is NULL, then ASSERT().
458 @param[in, out] Map The netmap to clean up.
471 ASSERT (Map
!= NULL
);
473 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Map
->Used
) {
474 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
476 RemoveEntryList (&Item
->Link
);
479 gBS
->FreePool (Item
);
482 ASSERT ((Map
->Count
== 0) && IsListEmpty (&Map
->Used
));
484 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Map
->Recycled
) {
485 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
487 RemoveEntryList (&Item
->Link
);
488 gBS
->FreePool (Item
);
491 ASSERT (IsListEmpty (&Map
->Recycled
));
496 Test whether the netmap is empty and return true if it is.
498 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.
500 If Map is NULL, then ASSERT().
503 @param[in] Map The net map to test.
505 @return TRUE if the netmap is empty, otherwise FALSE.
514 ASSERT (Map
!= NULL
);
515 return (BOOLEAN
) (Map
->Count
== 0);
520 Return the number of the <Key, Value> pairs in the netmap.
522 @param[in] Map The netmap to get the entry number.
524 @return The entry number in the netmap.
538 Return one allocated item.
540 If the Recycled doubly linked list of the netmap is empty, it will try to allocate
541 a batch of items if there are enough resources and add corresponding nodes to the begining
542 of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove
543 the fist node entry of the Recycled doubly linked list and return the corresponding item.
545 If Map is NULL, then ASSERT().
547 @param[in, out] Map The netmap to allocate item for.
549 @return The allocated item. If NULL, the
550 allocation failed due to resource limit.
562 ASSERT (Map
!= NULL
);
564 Head
= &Map
->Recycled
;
566 if (IsListEmpty (Head
)) {
567 for (Index
= 0; Index
< NET_MAP_INCREAMENT
; Index
++) {
568 Item
= AllocatePool (sizeof (NET_MAP_ITEM
));
578 InsertHeadList (Head
, &Item
->Link
);
582 Item
= NET_LIST_HEAD (Head
, NET_MAP_ITEM
, Link
);
583 NetListRemoveHead (Head
);
590 Allocate an item to save the <Key, Value> pair to the head of the netmap.
592 Allocate an item to save the <Key, Value> pair and add corresponding node entry
593 to the beginning of the Used doubly linked list. The number of the <Key, Value>
594 pairs in the netmap increase by 1.
596 If Map is NULL, then ASSERT().
598 @param[in, out] Map The netmap to insert into.
599 @param[in] Key The user's key.
600 @param[in] Value The user's value for the key.
602 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
603 @retval EFI_SUCCESS The item is inserted to the head.
611 IN VOID
*Value OPTIONAL
616 ASSERT (Map
!= NULL
);
618 Item
= NetMapAllocItem (Map
);
621 return EFI_OUT_OF_RESOURCES
;
626 InsertHeadList (&Map
->Used
, &Item
->Link
);
634 Allocate an item to save the <Key, Value> pair to the tail of the netmap.
636 Allocate an item to save the <Key, Value> pair and add corresponding node entry
637 to the tail of the Used doubly linked list. The number of the <Key, Value>
638 pairs in the netmap increase by 1.
640 If Map is NULL, then ASSERT().
642 @param[in, out] Map The netmap to insert into.
643 @param[in] Key The user's key.
644 @param[in] Value The user's value for the key.
646 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
647 @retval EFI_SUCCESS The item is inserted to the tail.
655 IN VOID
*Value OPTIONAL
660 ASSERT (Map
!= NULL
);
662 Item
= NetMapAllocItem (Map
);
665 return EFI_OUT_OF_RESOURCES
;
670 InsertTailList (&Map
->Used
, &Item
->Link
);
679 Check whether the item is in the Map and return TRUE if it is.
681 @param[in] Map The netmap to search within.
682 @param[in] Item The item to search.
684 @return TRUE if the item is in the netmap, otherwise FALSE.
690 IN NET_MAP_ITEM
*Item
693 LIST_ENTRY
*ListEntry
;
695 NET_LIST_FOR_EACH (ListEntry
, &Map
->Used
) {
696 if (ListEntry
== &Item
->Link
) {
706 Find the key in the netmap and returns the point to the item contains the Key.
708 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every
709 item with the key to search. It returns the point to the item contains the Key if found.
711 If Map is NULL, then ASSERT().
713 @param[in] Map The netmap to search within.
714 @param[in] Key The key to search.
716 @return The point to the item contains the Key, or NULL if Key isn't in the map.
729 ASSERT (Map
!= NULL
);
731 NET_LIST_FOR_EACH (Entry
, &Map
->Used
) {
732 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
734 if (Item
->Key
== Key
) {
744 Remove the node entry of the item from the netmap and return the key of the removed item.
746 Remove the node entry of the item from the Used doubly linked list of the netmap.
747 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
748 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,
749 Value will point to the value of the item. It returns the key of the removed item.
751 If Map is NULL, then ASSERT().
752 If Item is NULL, then ASSERT().
753 if item in not in the netmap, then ASSERT().
755 @param[in, out] Map The netmap to remove the item from.
756 @param[in, out] Item The item to remove.
757 @param[out] Value The variable to receive the value if not NULL.
759 @return The key of the removed item.
766 IN OUT NET_MAP_ITEM
*Item
,
767 OUT VOID
**Value OPTIONAL
770 ASSERT ((Map
!= NULL
) && (Item
!= NULL
));
771 ASSERT (NetItemInMap (Map
, Item
));
773 RemoveEntryList (&Item
->Link
);
775 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
778 *Value
= Item
->Value
;
786 Remove the first node entry on the netmap and return the key of the removed item.
788 Remove the first node entry from the Used doubly linked list of the netmap.
789 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
790 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
791 parameter Value will point to the value of the item. It returns the key of the removed item.
793 If Map is NULL, then ASSERT().
794 If the Used doubly linked list is empty, then ASSERT().
796 @param[in, out] Map The netmap to remove the head from.
797 @param[out] Value The variable to receive the value if not NULL.
799 @return The key of the item removed.
806 OUT VOID
**Value OPTIONAL
812 // Often, it indicates a programming error to remove
813 // the first entry in an empty list
815 ASSERT (Map
&& !IsListEmpty (&Map
->Used
));
817 Item
= NET_LIST_HEAD (&Map
->Used
, NET_MAP_ITEM
, Link
);
818 RemoveEntryList (&Item
->Link
);
820 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
823 *Value
= Item
->Value
;
831 Remove the last node entry on the netmap and return the key of the removed item.
833 Remove the last node entry from the Used doubly linked list of the netmap.
834 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
835 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
836 parameter Value will point to the value of the item. It returns the key of the removed item.
838 If Map is NULL, then ASSERT().
839 If the Used doubly linked list is empty, then ASSERT().
841 @param[in, out] Map The netmap to remove the tail from.
842 @param[out] Value The variable to receive the value if not NULL.
844 @return The key of the item removed.
851 OUT VOID
**Value OPTIONAL
857 // Often, it indicates a programming error to remove
858 // the last entry in an empty list
860 ASSERT (Map
&& !IsListEmpty (&Map
->Used
));
862 Item
= NET_LIST_TAIL (&Map
->Used
, NET_MAP_ITEM
, Link
);
863 RemoveEntryList (&Item
->Link
);
865 InsertHeadList (&Map
->Recycled
, &Item
->Link
);
868 *Value
= Item
->Value
;
876 Iterate through the netmap and call CallBack for each item.
878 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break
879 from the loop. It returns the CallBack's last return value. This function is
880 delete safe for the current item.
882 If Map is NULL, then ASSERT().
883 If CallBack is NULL, then ASSERT().
885 @param[in] Map The Map to iterate through.
886 @param[in] CallBack The callback function to call for each item.
887 @param[in] Arg The opaque parameter to the callback.
889 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item
891 @retval Others It returns the CallBack's last return value.
898 IN NET_MAP_CALLBACK CallBack
,
909 ASSERT ((Map
!= NULL
) && (CallBack
!= NULL
));
913 if (IsListEmpty (Head
)) {
917 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Head
) {
918 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
919 Result
= CallBack (Map
, Item
, Arg
);
921 if (EFI_ERROR (Result
)) {
931 This is the default unload handle for all the network drivers.
933 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
934 Uninstall all the protocols installed in the driver entry point.
936 @param[in] ImageHandle The drivers' driver image.
938 @retval EFI_SUCCESS The image is unloaded.
939 @retval Others Failed to unload the image.
944 NetLibDefaultUnload (
945 IN EFI_HANDLE ImageHandle
949 EFI_HANDLE
*DeviceHandleBuffer
;
950 UINTN DeviceHandleCount
;
952 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
953 EFI_COMPONENT_NAME_PROTOCOL
*ComponentName
;
954 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
957 // Get the list of all the handles in the handle database.
958 // If there is an error getting the list, then the unload
961 Status
= gBS
->LocateHandleBuffer (
969 if (EFI_ERROR (Status
)) {
974 // Disconnect the driver specified by ImageHandle from all
975 // the devices in the handle database.
977 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
978 Status
= gBS
->DisconnectController (
979 DeviceHandleBuffer
[Index
],
986 // Uninstall all the protocols installed in the driver entry point
988 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
989 Status
= gBS
->HandleProtocol (
990 DeviceHandleBuffer
[Index
],
991 &gEfiDriverBindingProtocolGuid
,
992 (VOID
**) &DriverBinding
995 if (EFI_ERROR (Status
)) {
999 if (DriverBinding
->ImageHandle
!= ImageHandle
) {
1003 gBS
->UninstallProtocolInterface (
1005 &gEfiDriverBindingProtocolGuid
,
1008 Status
= gBS
->HandleProtocol (
1009 DeviceHandleBuffer
[Index
],
1010 &gEfiComponentNameProtocolGuid
,
1011 (VOID
**) &ComponentName
1013 if (!EFI_ERROR (Status
)) {
1014 gBS
->UninstallProtocolInterface (
1016 &gEfiComponentNameProtocolGuid
,
1021 Status
= gBS
->HandleProtocol (
1022 DeviceHandleBuffer
[Index
],
1023 &gEfiComponentName2ProtocolGuid
,
1024 (VOID
**) &ComponentName2
1026 if (!EFI_ERROR (Status
)) {
1027 gBS
->UninstallProtocolInterface (
1029 &gEfiComponentName2ProtocolGuid
,
1036 // Free the buffer containing the list of handles from the handle database
1038 if (DeviceHandleBuffer
!= NULL
) {
1039 gBS
->FreePool (DeviceHandleBuffer
);
1048 Create a child of the service that is identified by ServiceBindingGuid.
1050 Get the ServiceBinding Protocol first, then use it to create a child.
1052 If ServiceBindingGuid is NULL, then ASSERT().
1053 If ChildHandle is NULL, then ASSERT().
1055 @param[in] Controller The controller which has the service installed.
1056 @param[in] Image The image handle used to open service.
1057 @param[in] ServiceBindingGuid The service's Guid.
1058 @param[in, out] ChildHandle The handle to receive the create child.
1060 @retval EFI_SUCCESS The child is successfully created.
1061 @retval Others Failed to create the child.
1066 NetLibCreateServiceChild (
1067 IN EFI_HANDLE Controller
,
1068 IN EFI_HANDLE Image
,
1069 IN EFI_GUID
*ServiceBindingGuid
,
1070 IN OUT EFI_HANDLE
*ChildHandle
1074 EFI_SERVICE_BINDING_PROTOCOL
*Service
;
1077 ASSERT ((ServiceBindingGuid
!= NULL
) && (ChildHandle
!= NULL
));
1080 // Get the ServiceBinding Protocol
1082 Status
= gBS
->OpenProtocol (
1088 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1091 if (EFI_ERROR (Status
)) {
1098 Status
= Service
->CreateChild (Service
, ChildHandle
);
1104 Destory a child of the service that is identified by ServiceBindingGuid.
1106 Get the ServiceBinding Protocol first, then use it to destroy a child.
1108 If ServiceBindingGuid is NULL, then ASSERT().
1110 @param[in] Controller The controller which has the service installed.
1111 @param[in] Image The image handle used to open service.
1112 @param[in] ServiceBindingGuid The service's Guid.
1113 @param[in] ChildHandle The child to destory.
1115 @retval EFI_SUCCESS The child is successfully destoried.
1116 @retval Others Failed to destory the child.
1121 NetLibDestroyServiceChild (
1122 IN EFI_HANDLE Controller
,
1123 IN EFI_HANDLE Image
,
1124 IN EFI_GUID
*ServiceBindingGuid
,
1125 IN EFI_HANDLE ChildHandle
1129 EFI_SERVICE_BINDING_PROTOCOL
*Service
;
1131 ASSERT (ServiceBindingGuid
!= NULL
);
1134 // Get the ServiceBinding Protocol
1136 Status
= gBS
->OpenProtocol (
1142 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1145 if (EFI_ERROR (Status
)) {
1150 // destory the child
1152 Status
= Service
->DestroyChild (Service
, ChildHandle
);
1158 Convert the mac address of the simple network protocol installed on
1159 SnpHandle to a unicode string. Callers are responsible for freeing the
1162 Get the mac address of the Simple Network protocol from the SnpHandle. Then convert
1163 the mac address into a unicode string. It takes 2 unicode characters to represent
1164 a 1 byte binary buffer. Plus one unicode character for the null-terminator.
1167 @param[in] SnpHandle The handle where the simple network protocol is
1169 @param[in] ImageHandle The image handle used to act as the agent handle to
1170 get the simple network protocol.
1171 @param[out] MacString The pointer to store the address of the string
1172 representation of the mac address.
1174 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.
1175 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
1176 @retval Others Failed to open the simple network protocol.
1181 NetLibGetMacString (
1182 IN EFI_HANDLE SnpHandle
,
1183 IN EFI_HANDLE ImageHandle
,
1184 OUT CHAR16
**MacString
1188 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1189 EFI_SIMPLE_NETWORK_MODE
*Mode
;
1196 // Get the Simple Network protocol from the SnpHandle.
1198 Status
= gBS
->OpenProtocol (
1200 &gEfiSimpleNetworkProtocolGuid
,
1204 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1206 if (EFI_ERROR (Status
)) {
1213 // It takes 2 unicode characters to represent a 1 byte binary buffer.
1214 // Plus one unicode character for the null-terminator.
1216 MacAddress
= AllocatePool ((2 * Mode
->HwAddressSize
+ 1) * sizeof (CHAR16
));
1217 if (MacAddress
== NULL
) {
1218 return EFI_OUT_OF_RESOURCES
;
1222 // Convert the mac address into a unicode string.
1224 for (Index
= 0; Index
< Mode
->HwAddressSize
; Index
++) {
1225 MacAddress
[Index
* 2] = (CHAR16
) mNetLibHexStr
[(Mode
->CurrentAddress
.Addr
[Index
] >> 4) & 0x0F];
1226 MacAddress
[Index
* 2 + 1] = (CHAR16
) mNetLibHexStr
[Mode
->CurrentAddress
.Addr
[Index
] & 0x0F];
1229 MacAddress
[Mode
->HwAddressSize
* 2] = L
'\0';
1231 *MacString
= MacAddress
;
1237 Check the default address used by the IPv4 driver is static or dynamic (acquired
1240 If the controller handle does not have the NIC Ip4 Config Protocol installed, the
1241 default address is static. If the EFI variable to save the configuration is not found,
1242 the default address is static. Otherwise, get the result from the EFI variable which
1243 saving the configuration.
1245 @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol
1246 relative with the default address to judge.
1248 @retval TRUE If the default address is static.
1249 @retval FALSE If the default address is acquired from DHCP.
1253 NetLibDefaultAddressIsStatic (
1254 IN EFI_HANDLE Controller
1258 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
1260 NIC_IP4_CONFIG_INFO
*ConfigInfo
;
1262 EFI_STRING ConfigHdr
;
1263 EFI_STRING ConfigResp
;
1264 EFI_STRING AccessProgress
;
1265 EFI_STRING AccessResults
;
1271 AccessProgress
= NULL
;
1272 AccessResults
= NULL
;
1275 Status
= gBS
->LocateProtocol (
1276 &gEfiHiiConfigRoutingProtocolGuid
,
1278 (VOID
**) &HiiConfigRouting
1280 if (EFI_ERROR (Status
)) {
1285 // Construct config request string header
1287 ConfigHdr
= HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid
, EFI_NIC_IP4_CONFIG_VARIABLE
, Controller
);
1289 Len
= StrLen (ConfigHdr
);
1290 ConfigResp
= AllocateZeroPool ((Len
+ NIC_ITEM_CONFIG_SIZE
* 2 + 100) * sizeof (CHAR16
));
1291 if (ConfigResp
== NULL
) {
1294 StrCpy (ConfigResp
, ConfigHdr
);
1296 String
= ConfigResp
+ Len
;
1299 (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16
),
1300 L
"&OFFSET=%04X&WIDTH=%04X",
1301 OFFSET_OF (NIC_IP4_CONFIG_INFO
, Source
),
1305 Status
= HiiConfigRouting
->ExtractConfig (
1311 if (EFI_ERROR (Status
)) {
1315 ConfigInfo
= AllocateZeroPool (sizeof (NIC_ITEM_CONFIG_SIZE
));
1316 if (ConfigInfo
== NULL
) {
1320 ConfigInfo
->Source
= IP4_CONFIG_SOURCE_STATIC
;
1321 Len
= NIC_ITEM_CONFIG_SIZE
;
1322 Status
= HiiConfigRouting
->ConfigToBlock (
1325 (UINT8
*) ConfigInfo
,
1329 if (EFI_ERROR (Status
)) {
1333 IsStatic
= (BOOLEAN
) (ConfigInfo
->Source
== IP4_CONFIG_SOURCE_STATIC
);
1337 if (AccessResults
!= NULL
) {
1338 FreePool (AccessResults
);
1340 if (ConfigInfo
!= NULL
) {
1341 FreePool (ConfigInfo
);
1343 if (ConfigResp
!= NULL
) {
1344 FreePool (ConfigResp
);
1346 if (ConfigHdr
!= NULL
) {
1347 FreePool (ConfigHdr
);
1354 Create an IPv4 device path node.
1356 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.
1357 The header subtype of IPv4 device path node is MSG_IPv4_DP.
1358 The length of the IPv4 device path node in bytes is 19.
1359 Get other info from parameters to make up the whole IPv4 device path node.
1361 @param[in, out] Node Pointer to the IPv4 device path node.
1362 @param[in] Controller The handle where the NIC IP4 config protocol resides.
1363 @param[in] LocalIp The local IPv4 address.
1364 @param[in] LocalPort The local port.
1365 @param[in] RemoteIp The remote IPv4 address.
1366 @param[in] RemotePort The remote port.
1367 @param[in] Protocol The protocol type in the IP header.
1368 @param[in] UseDefaultAddress Whether this instance is using default address or not.
1373 NetLibCreateIPv4DPathNode (
1374 IN OUT IPv4_DEVICE_PATH
*Node
,
1375 IN EFI_HANDLE Controller
,
1376 IN IP4_ADDR LocalIp
,
1377 IN UINT16 LocalPort
,
1378 IN IP4_ADDR RemoteIp
,
1379 IN UINT16 RemotePort
,
1381 IN BOOLEAN UseDefaultAddress
1384 Node
->Header
.Type
= MESSAGING_DEVICE_PATH
;
1385 Node
->Header
.SubType
= MSG_IPv4_DP
;
1386 SetDevicePathNodeLength (&Node
->Header
, 19);
1388 CopyMem (&Node
->LocalIpAddress
, &LocalIp
, sizeof (EFI_IPv4_ADDRESS
));
1389 CopyMem (&Node
->RemoteIpAddress
, &RemoteIp
, sizeof (EFI_IPv4_ADDRESS
));
1391 Node
->LocalPort
= LocalPort
;
1392 Node
->RemotePort
= RemotePort
;
1394 Node
->Protocol
= Protocol
;
1396 if (!UseDefaultAddress
) {
1397 Node
->StaticIpAddress
= TRUE
;
1399 Node
->StaticIpAddress
= NetLibDefaultAddressIsStatic (Controller
);
1405 Find the UNDI/SNP handle from controller and protocol GUID.
1407 For example, IP will open a MNP child to transmit/receive
1408 packets, when MNP is stopped, IP should also be stopped. IP
1409 needs to find its own private data which is related the IP's
1410 service binding instance that is install on UNDI/SNP handle.
1411 Now, the controller is either a MNP or ARP child handle. But
1412 IP opens these handle BY_DRIVER, use that info, we can get the
1415 @param[in] Controller Then protocol handle to check.
1416 @param[in] ProtocolGuid The protocol that is related with the handle.
1418 @return The UNDI/SNP handle or NULL for errors.
1423 NetLibGetNicHandle (
1424 IN EFI_HANDLE Controller
,
1425 IN EFI_GUID
*ProtocolGuid
1428 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenBuffer
;
1434 Status
= gBS
->OpenProtocolInformation (
1441 if (EFI_ERROR (Status
)) {
1447 for (Index
= 0; Index
< OpenCount
; Index
++) {
1448 if (OpenBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) {
1449 Handle
= OpenBuffer
[Index
].ControllerHandle
;
1454 gBS
->FreePool (OpenBuffer
);
1459 Add a Deferred Procedure Call to the end of the DPC queue.
1461 @param[in] DpcTpl The EFI_TPL that the DPC should be invoked.
1462 @param[in] DpcProcedure Pointer to the DPC's function.
1463 @param[in] DpcContext Pointer to the DPC's context. Passed to DpcProcedure
1464 when DpcProcedure is invoked.
1466 @retval EFI_SUCCESS The DPC was queued.
1467 @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL, or DpcProcedure
1469 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
1470 add the DPC to the queue.
1477 IN EFI_DPC_PROCEDURE DpcProcedure
,
1478 IN VOID
*DpcContext OPTIONAL
1481 return mDpc
->QueueDpc (mDpc
, DpcTpl
, DpcProcedure
, DpcContext
);
1485 Dispatch the queue of DPCs. ALL DPCs that have been queued with a DpcTpl
1486 value greater than or equal to the current TPL are invoked in the order that
1487 they were queued. DPCs with higher DpcTpl values are invoked before DPCs with
1488 lower DpcTpl values.
1490 @retval EFI_SUCCESS One or more DPCs were invoked.
1491 @retval EFI_NOT_FOUND No DPCs were invoked.
1500 return mDpc
->DispatchDpc(mDpc
);
1504 The constructor function caches the pointer to DPC protocol.
1506 The constructor function locates DPC protocol from protocol database.
1507 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
1509 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1510 @param[in] SystemTable A pointer to the EFI System Table.
1512 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1518 IN EFI_HANDLE ImageHandle
,
1519 IN EFI_SYSTEM_TABLE
*SystemTable
1524 Status
= gBS
->LocateProtocol (&gEfiDpcProtocolGuid
, NULL
, (VOID
**) &mDpc
);
1525 ASSERT_EFI_ERROR (Status
);
1526 ASSERT (mDpc
!= NULL
);