]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
add back CAPSULE_HOB_INFO definition for back-compatible.
[mirror_edk2.git] / MdeModulePkg / Library / DxeNetLib / DxeNetLib.c
... / ...
CommitLineData
1/** @file\r
2 Network library.\r
3 \r
4Copyright (c) 2005 - 2009, Intel Corporation.<BR>\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12**/\r
13\r
14#include <Uefi.h>\r
15\r
16#include <Protocol/DriverBinding.h>\r
17#include <Protocol/ServiceBinding.h>\r
18#include <Protocol/SimpleNetwork.h>\r
19#include <Protocol/HiiConfigRouting.h>\r
20#include <Protocol/ComponentName.h>\r
21#include <Protocol/ComponentName2.h>\r
22\r
23#include <Guid/NicIp4ConfigNvData.h>\r
24\r
25#include <Library/NetLib.h>\r
26#include <Library/BaseLib.h>\r
27#include <Library/DebugLib.h>\r
28#include <Library/BaseMemoryLib.h>\r
29#include <Library/UefiBootServicesTableLib.h>\r
30#include <Library/UefiRuntimeServicesTableLib.h>\r
31#include <Library/MemoryAllocationLib.h>\r
32#include <Library/DevicePathLib.h>\r
33#include <Library/HiiLib.h>\r
34#include <Library/PrintLib.h>\r
35\r
36GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mNetLibHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r
37\r
38#define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE\r
39\r
40//\r
41// All the supported IP4 maskes in host byte order.\r
42//\r
43IP4_ADDR gIp4AllMasks[IP4_MASK_NUM] = {\r
44 0x00000000,\r
45 0x80000000,\r
46 0xC0000000,\r
47 0xE0000000,\r
48 0xF0000000,\r
49 0xF8000000,\r
50 0xFC000000,\r
51 0xFE000000,\r
52\r
53 0xFF000000,\r
54 0xFF800000,\r
55 0xFFC00000,\r
56 0xFFE00000,\r
57 0xFFF00000,\r
58 0xFFF80000,\r
59 0xFFFC0000,\r
60 0xFFFE0000,\r
61\r
62 0xFFFF0000,\r
63 0xFFFF8000,\r
64 0xFFFFC000,\r
65 0xFFFFE000,\r
66 0xFFFFF000,\r
67 0xFFFFF800,\r
68 0xFFFFFC00,\r
69 0xFFFFFE00,\r
70\r
71 0xFFFFFF00,\r
72 0xFFFFFF80,\r
73 0xFFFFFFC0,\r
74 0xFFFFFFE0,\r
75 0xFFFFFFF0,\r
76 0xFFFFFFF8,\r
77 0xFFFFFFFC,\r
78 0xFFFFFFFE,\r
79 0xFFFFFFFF,\r
80};\r
81\r
82EFI_IPv4_ADDRESS mZeroIp4Addr = {{0, 0, 0, 0}};\r
83\r
84/**\r
85 Return the length of the mask. \r
86 \r
87 Return the length of the mask, the correct value is from 0 to 32.\r
88 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.\r
89 NetMask is in the host byte order.\r
90\r
91 @param[in] NetMask The netmask to get the length from.\r
92\r
93 @return The length of the netmask, IP4_MASK_NUM if the mask is invalid.\r
94 \r
95**/\r
96INTN\r
97EFIAPI\r
98NetGetMaskLength (\r
99 IN IP4_ADDR NetMask\r
100 )\r
101{\r
102 INTN Index;\r
103\r
104 for (Index = 0; Index < IP4_MASK_NUM; Index++) {\r
105 if (NetMask == gIp4AllMasks[Index]) {\r
106 break;\r
107 }\r
108 }\r
109\r
110 return Index;\r
111}\r
112\r
113\r
114\r
115/**\r
116 Return the class of the IP address, such as class A, B, C.\r
117 Addr is in host byte order.\r
118 \r
119 The address of class A starts with 0.\r
120 If the address belong to class A, return IP4_ADDR_CLASSA.\r
121 The address of class B starts with 10. \r
122 If the address belong to class B, return IP4_ADDR_CLASSB.\r
123 The address of class C starts with 110. \r
124 If the address belong to class C, return IP4_ADDR_CLASSC.\r
125 The address of class D starts with 1110. \r
126 If the address belong to class D, return IP4_ADDR_CLASSD.\r
127 The address of class E starts with 1111.\r
128 If the address belong to class E, return IP4_ADDR_CLASSE.\r
129\r
130 \r
131 @param[in] Addr The address to get the class from.\r
132\r
133 @return IP address class, such as IP4_ADDR_CLASSA.\r
134\r
135**/\r
136INTN\r
137EFIAPI\r
138NetGetIpClass (\r
139 IN IP4_ADDR Addr\r
140 )\r
141{\r
142 UINT8 ByteOne;\r
143\r
144 ByteOne = (UINT8) (Addr >> 24);\r
145\r
146 if ((ByteOne & 0x80) == 0) {\r
147 return IP4_ADDR_CLASSA;\r
148\r
149 } else if ((ByteOne & 0xC0) == 0x80) {\r
150 return IP4_ADDR_CLASSB;\r
151\r
152 } else if ((ByteOne & 0xE0) == 0xC0) {\r
153 return IP4_ADDR_CLASSC;\r
154\r
155 } else if ((ByteOne & 0xF0) == 0xE0) {\r
156 return IP4_ADDR_CLASSD;\r
157\r
158 } else {\r
159 return IP4_ADDR_CLASSE;\r
160\r
161 }\r
162}\r
163\r
164\r
165/**\r
166 Check whether the IP is a valid unicast address according to\r
167 the netmask. If NetMask is zero, use the IP address's class to get the default mask.\r
168 \r
169 If Ip is 0, IP is not a valid unicast address.\r
170 Class D address is used for multicasting and class E address is reserved for future. If Ip\r
171 belongs to class D or class E, IP is not a valid unicast address. \r
172 If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address.\r
173\r
174 @param[in] Ip The IP to check against.\r
175 @param[in] NetMask The mask of the IP.\r
176\r
177 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.\r
178\r
179**/\r
180BOOLEAN\r
181EFIAPI\r
182Ip4IsUnicast (\r
183 IN IP4_ADDR Ip,\r
184 IN IP4_ADDR NetMask\r
185 )\r
186{\r
187 INTN Class;\r
188\r
189 Class = NetGetIpClass (Ip);\r
190\r
191 if ((Ip == 0) || (Class >= IP4_ADDR_CLASSD)) {\r
192 return FALSE;\r
193 }\r
194\r
195 if (NetMask == 0) {\r
196 NetMask = gIp4AllMasks[Class << 3];\r
197 }\r
198\r
199 if (((Ip &~NetMask) == ~NetMask) || ((Ip &~NetMask) == 0)) {\r
200 return FALSE;\r
201 }\r
202\r
203 return TRUE;\r
204}\r
205\r
206/**\r
207 Check whether the incoming IPv6 address is a valid unicast address.\r
208\r
209 If the address is a multicast address has binary 0xFF at the start, it is not\r
210 a valid unicast address. If the address is unspecified ::, it is not a valid\r
211 unicast address to be assigned to any node. If the address is loopback address\r
212 ::1, it is also not a valid unicast address to be assigned to any physical\r
213 interface. \r
214\r
215 @param[in] Ip6 The IPv6 address to check against.\r
216\r
217 @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE.\r
218\r
219**/ \r
220BOOLEAN\r
221Ip6IsValidUnicast (\r
222 IN EFI_IPv6_ADDRESS *Ip6\r
223 ) \r
224{\r
225 UINT8 t;\r
226 UINT8 i;\r
227 \r
228 if (Ip6->Addr[0] == 0xFF) {\r
229 return FALSE;\r
230 }\r
231\r
232 for (i = 0; i < 15; i++) {\r
233 if (Ip6->Addr[i] != 0) {\r
234 return TRUE;\r
235 }\r
236 }\r
237\r
238 t = Ip6->Addr[i];\r
239\r
240 if (t == 0x0 || t == 0x1) {\r
241 return FALSE;\r
242 }\r
243\r
244 return TRUE; \r
245}\r
246\r
247/**\r
248 Initialize a random seed using current time.\r
249 \r
250 Get current time first. Then initialize a random seed based on some basic \r
251 mathematics operation on the hour, day, minute, second, nanosecond and year \r
252 of the current time.\r
253 \r
254 @return The random seed initialized with current time.\r
255\r
256**/\r
257UINT32\r
258EFIAPI\r
259NetRandomInitSeed (\r
260 VOID\r
261 )\r
262{\r
263 EFI_TIME Time;\r
264 UINT32 Seed;\r
265\r
266 gRT->GetTime (&Time, NULL);\r
267 Seed = (~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second);\r
268 Seed ^= Time.Nanosecond;\r
269 Seed ^= Time.Year << 7;\r
270\r
271 return Seed;\r
272}\r
273\r
274\r
275/**\r
276 Extract a UINT32 from a byte stream.\r
277 \r
278 Copy a UINT32 from a byte stream, then converts it from Network \r
279 byte order to host byte order. Use this function to avoid alignment error.\r
280\r
281 @param[in] Buf The buffer to extract the UINT32.\r
282\r
283 @return The UINT32 extracted.\r
284\r
285**/\r
286UINT32\r
287EFIAPI\r
288NetGetUint32 (\r
289 IN UINT8 *Buf\r
290 )\r
291{\r
292 UINT32 Value;\r
293\r
294 CopyMem (&Value, Buf, sizeof (UINT32));\r
295 return NTOHL (Value);\r
296}\r
297\r
298\r
299/**\r
300 Put a UINT32 to the byte stream in network byte order. \r
301 \r
302 Converts a UINT32 from host byte order to network byte order. Then copy it to the \r
303 byte stream.\r
304\r
305 @param[in, out] Buf The buffer to put the UINT32.\r
306 @param[in] Data The data to put.\r
307 \r
308**/\r
309VOID\r
310EFIAPI\r
311NetPutUint32 (\r
312 IN OUT UINT8 *Buf,\r
313 IN UINT32 Data\r
314 )\r
315{\r
316 Data = HTONL (Data);\r
317 CopyMem (Buf, &Data, sizeof (UINT32));\r
318}\r
319\r
320\r
321/**\r
322 Remove the first node entry on the list, and return the removed node entry.\r
323 \r
324 Removes the first node Entry from a doubly linked list. It is up to the caller of\r
325 this function to release the memory used by the first node if that is required. On\r
326 exit, the removed node is returned. \r
327\r
328 If Head is NULL, then ASSERT().\r
329 If Head was not initialized, then ASSERT().\r
330 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
331 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,\r
332 then ASSERT(). \r
333\r
334 @param[in, out] Head The list header.\r
335\r
336 @return The first node entry that is removed from the list, NULL if the list is empty.\r
337\r
338**/\r
339LIST_ENTRY *\r
340EFIAPI\r
341NetListRemoveHead (\r
342 IN OUT LIST_ENTRY *Head\r
343 )\r
344{\r
345 LIST_ENTRY *First;\r
346\r
347 ASSERT (Head != NULL);\r
348\r
349 if (IsListEmpty (Head)) {\r
350 return NULL;\r
351 }\r
352\r
353 First = Head->ForwardLink;\r
354 Head->ForwardLink = First->ForwardLink;\r
355 First->ForwardLink->BackLink = Head;\r
356\r
357 DEBUG_CODE (\r
358 First->ForwardLink = (LIST_ENTRY *) NULL;\r
359 First->BackLink = (LIST_ENTRY *) NULL;\r
360 );\r
361\r
362 return First;\r
363}\r
364\r
365\r
366/**\r
367 Remove the last node entry on the list and and return the removed node entry.\r
368\r
369 Removes the last node entry from a doubly linked list. It is up to the caller of\r
370 this function to release the memory used by the first node if that is required. On\r
371 exit, the removed node is returned. \r
372\r
373 If Head is NULL, then ASSERT().\r
374 If Head was not initialized, then ASSERT().\r
375 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
376 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,\r
377 then ASSERT(). \r
378 \r
379 @param[in, out] Head The list head.\r
380\r
381 @return The last node entry that is removed from the list, NULL if the list is empty.\r
382\r
383**/\r
384LIST_ENTRY *\r
385EFIAPI\r
386NetListRemoveTail (\r
387 IN OUT LIST_ENTRY *Head\r
388 )\r
389{\r
390 LIST_ENTRY *Last;\r
391\r
392 ASSERT (Head != NULL);\r
393\r
394 if (IsListEmpty (Head)) {\r
395 return NULL;\r
396 }\r
397\r
398 Last = Head->BackLink;\r
399 Head->BackLink = Last->BackLink;\r
400 Last->BackLink->ForwardLink = Head;\r
401\r
402 DEBUG_CODE (\r
403 Last->ForwardLink = (LIST_ENTRY *) NULL;\r
404 Last->BackLink = (LIST_ENTRY *) NULL;\r
405 );\r
406\r
407 return Last;\r
408}\r
409\r
410\r
411/**\r
412 Insert a new node entry after a designated node entry of a doubly linked list.\r
413 \r
414 Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry\r
415 of the doubly linked list.\r
416 \r
417 @param[in, out] PrevEntry The previous entry to insert after.\r
418 @param[in, out] NewEntry The new entry to insert.\r
419\r
420**/\r
421VOID\r
422EFIAPI\r
423NetListInsertAfter (\r
424 IN OUT LIST_ENTRY *PrevEntry,\r
425 IN OUT LIST_ENTRY *NewEntry\r
426 )\r
427{\r
428 NewEntry->BackLink = PrevEntry;\r
429 NewEntry->ForwardLink = PrevEntry->ForwardLink;\r
430 PrevEntry->ForwardLink->BackLink = NewEntry;\r
431 PrevEntry->ForwardLink = NewEntry;\r
432}\r
433\r
434\r
435/**\r
436 Insert a new node entry before a designated node entry of a doubly linked list.\r
437 \r
438 Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry\r
439 of the doubly linked list.\r
440 \r
441 @param[in, out] PostEntry The entry to insert before.\r
442 @param[in, out] NewEntry The new entry to insert.\r
443\r
444**/\r
445VOID\r
446EFIAPI\r
447NetListInsertBefore (\r
448 IN OUT LIST_ENTRY *PostEntry,\r
449 IN OUT LIST_ENTRY *NewEntry\r
450 )\r
451{\r
452 NewEntry->ForwardLink = PostEntry;\r
453 NewEntry->BackLink = PostEntry->BackLink;\r
454 PostEntry->BackLink->ForwardLink = NewEntry;\r
455 PostEntry->BackLink = NewEntry;\r
456}\r
457\r
458\r
459/**\r
460 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.\r
461 \r
462 Initialize the forward and backward links of two head nodes donated by Map->Used \r
463 and Map->Recycled of two doubly linked lists.\r
464 Initializes the count of the <Key, Value> pairs in the netmap to zero.\r
465 \r
466 If Map is NULL, then ASSERT().\r
467 If the address of Map->Used is NULL, then ASSERT().\r
468 If the address of Map->Recycled is NULl, then ASSERT().\r
469 \r
470 @param[in, out] Map The netmap to initialize.\r
471\r
472**/\r
473VOID\r
474EFIAPI\r
475NetMapInit (\r
476 IN OUT NET_MAP *Map\r
477 )\r
478{\r
479 ASSERT (Map != NULL);\r
480\r
481 InitializeListHead (&Map->Used);\r
482 InitializeListHead (&Map->Recycled);\r
483 Map->Count = 0;\r
484}\r
485\r
486\r
487/**\r
488 To clean up the netmap, that is, release allocated memories.\r
489 \r
490 Removes all nodes of the Used doubly linked list and free memory of all related netmap items.\r
491 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.\r
492 The number of the <Key, Value> pairs in the netmap is set to be zero.\r
493 \r
494 If Map is NULL, then ASSERT().\r
495 \r
496 @param[in, out] Map The netmap to clean up.\r
497\r
498**/\r
499VOID\r
500EFIAPI\r
501NetMapClean (\r
502 IN OUT NET_MAP *Map\r
503 )\r
504{\r
505 NET_MAP_ITEM *Item;\r
506 LIST_ENTRY *Entry;\r
507 LIST_ENTRY *Next;\r
508\r
509 ASSERT (Map != NULL);\r
510\r
511 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Used) {\r
512 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
513\r
514 RemoveEntryList (&Item->Link);\r
515 Map->Count--;\r
516\r
517 gBS->FreePool (Item);\r
518 }\r
519\r
520 ASSERT ((Map->Count == 0) && IsListEmpty (&Map->Used));\r
521\r
522 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Recycled) {\r
523 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
524\r
525 RemoveEntryList (&Item->Link);\r
526 gBS->FreePool (Item);\r
527 }\r
528\r
529 ASSERT (IsListEmpty (&Map->Recycled));\r
530}\r
531\r
532\r
533/**\r
534 Test whether the netmap is empty and return true if it is.\r
535 \r
536 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.\r
537 \r
538 If Map is NULL, then ASSERT().\r
539 \r
540 \r
541 @param[in] Map The net map to test.\r
542\r
543 @return TRUE if the netmap is empty, otherwise FALSE.\r
544\r
545**/\r
546BOOLEAN\r
547EFIAPI\r
548NetMapIsEmpty (\r
549 IN NET_MAP *Map\r
550 )\r
551{\r
552 ASSERT (Map != NULL);\r
553 return (BOOLEAN) (Map->Count == 0);\r
554}\r
555\r
556\r
557/**\r
558 Return the number of the <Key, Value> pairs in the netmap.\r
559\r
560 @param[in] Map The netmap to get the entry number.\r
561\r
562 @return The entry number in the netmap.\r
563\r
564**/\r
565UINTN\r
566EFIAPI\r
567NetMapGetCount (\r
568 IN NET_MAP *Map\r
569 )\r
570{\r
571 return Map->Count;\r
572}\r
573\r
574\r
575/**\r
576 Return one allocated item. \r
577 \r
578 If the Recycled doubly linked list of the netmap is empty, it will try to allocate \r
579 a batch of items if there are enough resources and add corresponding nodes to the begining\r
580 of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove\r
581 the fist node entry of the Recycled doubly linked list and return the corresponding item.\r
582 \r
583 If Map is NULL, then ASSERT().\r
584 \r
585 @param[in, out] Map The netmap to allocate item for.\r
586\r
587 @return The allocated item. If NULL, the\r
588 allocation failed due to resource limit.\r
589\r
590**/\r
591NET_MAP_ITEM *\r
592NetMapAllocItem (\r
593 IN OUT NET_MAP *Map\r
594 )\r
595{\r
596 NET_MAP_ITEM *Item;\r
597 LIST_ENTRY *Head;\r
598 UINTN Index;\r
599\r
600 ASSERT (Map != NULL);\r
601\r
602 Head = &Map->Recycled;\r
603\r
604 if (IsListEmpty (Head)) {\r
605 for (Index = 0; Index < NET_MAP_INCREAMENT; Index++) {\r
606 Item = AllocatePool (sizeof (NET_MAP_ITEM));\r
607\r
608 if (Item == NULL) {\r
609 if (Index == 0) {\r
610 return NULL;\r
611 }\r
612\r
613 break;\r
614 }\r
615\r
616 InsertHeadList (Head, &Item->Link);\r
617 }\r
618 }\r
619\r
620 Item = NET_LIST_HEAD (Head, NET_MAP_ITEM, Link);\r
621 NetListRemoveHead (Head);\r
622\r
623 return Item;\r
624}\r
625\r
626\r
627/**\r
628 Allocate an item to save the <Key, Value> pair to the head of the netmap.\r
629 \r
630 Allocate an item to save the <Key, Value> pair and add corresponding node entry\r
631 to the beginning of the Used doubly linked list. The number of the <Key, Value> \r
632 pairs in the netmap increase by 1.\r
633\r
634 If Map is NULL, then ASSERT().\r
635 \r
636 @param[in, out] Map The netmap to insert into.\r
637 @param[in] Key The user's key.\r
638 @param[in] Value The user's value for the key.\r
639\r
640 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
641 @retval EFI_SUCCESS The item is inserted to the head.\r
642\r
643**/\r
644EFI_STATUS\r
645EFIAPI\r
646NetMapInsertHead (\r
647 IN OUT NET_MAP *Map,\r
648 IN VOID *Key,\r
649 IN VOID *Value OPTIONAL\r
650 )\r
651{\r
652 NET_MAP_ITEM *Item;\r
653\r
654 ASSERT (Map != NULL);\r
655\r
656 Item = NetMapAllocItem (Map);\r
657\r
658 if (Item == NULL) {\r
659 return EFI_OUT_OF_RESOURCES;\r
660 }\r
661\r
662 Item->Key = Key;\r
663 Item->Value = Value;\r
664 InsertHeadList (&Map->Used, &Item->Link);\r
665\r
666 Map->Count++;\r
667 return EFI_SUCCESS;\r
668}\r
669\r
670\r
671/**\r
672 Allocate an item to save the <Key, Value> pair to the tail of the netmap.\r
673\r
674 Allocate an item to save the <Key, Value> pair and add corresponding node entry\r
675 to the tail of the Used doubly linked list. The number of the <Key, Value> \r
676 pairs in the netmap increase by 1.\r
677\r
678 If Map is NULL, then ASSERT().\r
679 \r
680 @param[in, out] Map The netmap to insert into.\r
681 @param[in] Key The user's key.\r
682 @param[in] Value The user's value for the key.\r
683\r
684 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
685 @retval EFI_SUCCESS The item is inserted to the tail.\r
686\r
687**/\r
688EFI_STATUS\r
689EFIAPI\r
690NetMapInsertTail (\r
691 IN OUT NET_MAP *Map,\r
692 IN VOID *Key,\r
693 IN VOID *Value OPTIONAL\r
694 )\r
695{\r
696 NET_MAP_ITEM *Item;\r
697\r
698 ASSERT (Map != NULL);\r
699\r
700 Item = NetMapAllocItem (Map);\r
701\r
702 if (Item == NULL) {\r
703 return EFI_OUT_OF_RESOURCES;\r
704 }\r
705\r
706 Item->Key = Key;\r
707 Item->Value = Value;\r
708 InsertTailList (&Map->Used, &Item->Link);\r
709\r
710 Map->Count++;\r
711\r
712 return EFI_SUCCESS;\r
713}\r
714\r
715\r
716/**\r
717 Check whether the item is in the Map and return TRUE if it is.\r
718\r
719 @param[in] Map The netmap to search within.\r
720 @param[in] Item The item to search.\r
721\r
722 @return TRUE if the item is in the netmap, otherwise FALSE.\r
723\r
724**/\r
725BOOLEAN\r
726NetItemInMap (\r
727 IN NET_MAP *Map,\r
728 IN NET_MAP_ITEM *Item\r
729 )\r
730{\r
731 LIST_ENTRY *ListEntry;\r
732\r
733 NET_LIST_FOR_EACH (ListEntry, &Map->Used) {\r
734 if (ListEntry == &Item->Link) {\r
735 return TRUE;\r
736 }\r
737 }\r
738\r
739 return FALSE;\r
740}\r
741\r
742\r
743/**\r
744 Find the key in the netmap and returns the point to the item contains the Key.\r
745 \r
746 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every \r
747 item with the key to search. It returns the point to the item contains the Key if found.\r
748\r
749 If Map is NULL, then ASSERT().\r
750 \r
751 @param[in] Map The netmap to search within.\r
752 @param[in] Key The key to search.\r
753\r
754 @return The point to the item contains the Key, or NULL if Key isn't in the map.\r
755\r
756**/\r
757NET_MAP_ITEM *\r
758EFIAPI\r
759NetMapFindKey (\r
760 IN NET_MAP *Map,\r
761 IN VOID *Key\r
762 )\r
763{\r
764 LIST_ENTRY *Entry;\r
765 NET_MAP_ITEM *Item;\r
766\r
767 ASSERT (Map != NULL);\r
768\r
769 NET_LIST_FOR_EACH (Entry, &Map->Used) {\r
770 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
771\r
772 if (Item->Key == Key) {\r
773 return Item;\r
774 }\r
775 }\r
776\r
777 return NULL;\r
778}\r
779\r
780\r
781/**\r
782 Remove the node entry of the item from the netmap and return the key of the removed item.\r
783 \r
784 Remove the node entry of the item from the Used doubly linked list of the netmap. \r
785 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node \r
786 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,\r
787 Value will point to the value of the item. It returns the key of the removed item.\r
788 \r
789 If Map is NULL, then ASSERT().\r
790 If Item is NULL, then ASSERT().\r
791 if item in not in the netmap, then ASSERT().\r
792 \r
793 @param[in, out] Map The netmap to remove the item from.\r
794 @param[in, out] Item The item to remove.\r
795 @param[out] Value The variable to receive the value if not NULL.\r
796\r
797 @return The key of the removed item.\r
798\r
799**/\r
800VOID *\r
801EFIAPI\r
802NetMapRemoveItem (\r
803 IN OUT NET_MAP *Map,\r
804 IN OUT NET_MAP_ITEM *Item,\r
805 OUT VOID **Value OPTIONAL\r
806 )\r
807{\r
808 ASSERT ((Map != NULL) && (Item != NULL));\r
809 ASSERT (NetItemInMap (Map, Item));\r
810\r
811 RemoveEntryList (&Item->Link);\r
812 Map->Count--;\r
813 InsertHeadList (&Map->Recycled, &Item->Link);\r
814\r
815 if (Value != NULL) {\r
816 *Value = Item->Value;\r
817 }\r
818\r
819 return Item->Key;\r
820}\r
821\r
822\r
823/**\r
824 Remove the first node entry on the netmap and return the key of the removed item.\r
825\r
826 Remove the first node entry from the Used doubly linked list of the netmap. \r
827 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node \r
828 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r
829 parameter Value will point to the value of the item. It returns the key of the removed item.\r
830 \r
831 If Map is NULL, then ASSERT().\r
832 If the Used doubly linked list is empty, then ASSERT().\r
833 \r
834 @param[in, out] Map The netmap to remove the head from.\r
835 @param[out] Value The variable to receive the value if not NULL.\r
836\r
837 @return The key of the item removed.\r
838\r
839**/\r
840VOID *\r
841EFIAPI\r
842NetMapRemoveHead (\r
843 IN OUT NET_MAP *Map,\r
844 OUT VOID **Value OPTIONAL\r
845 )\r
846{\r
847 NET_MAP_ITEM *Item;\r
848\r
849 //\r
850 // Often, it indicates a programming error to remove\r
851 // the first entry in an empty list\r
852 //\r
853 ASSERT (Map && !IsListEmpty (&Map->Used));\r
854\r
855 Item = NET_LIST_HEAD (&Map->Used, NET_MAP_ITEM, Link);\r
856 RemoveEntryList (&Item->Link);\r
857 Map->Count--;\r
858 InsertHeadList (&Map->Recycled, &Item->Link);\r
859\r
860 if (Value != NULL) {\r
861 *Value = Item->Value;\r
862 }\r
863\r
864 return Item->Key;\r
865}\r
866\r
867\r
868/**\r
869 Remove the last node entry on the netmap and return the key of the removed item.\r
870\r
871 Remove the last node entry from the Used doubly linked list of the netmap. \r
872 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node \r
873 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r
874 parameter Value will point to the value of the item. It returns the key of the removed item.\r
875 \r
876 If Map is NULL, then ASSERT().\r
877 If the Used doubly linked list is empty, then ASSERT().\r
878 \r
879 @param[in, out] Map The netmap to remove the tail from.\r
880 @param[out] Value The variable to receive the value if not NULL.\r
881\r
882 @return The key of the item removed.\r
883\r
884**/\r
885VOID *\r
886EFIAPI\r
887NetMapRemoveTail (\r
888 IN OUT NET_MAP *Map,\r
889 OUT VOID **Value OPTIONAL\r
890 )\r
891{\r
892 NET_MAP_ITEM *Item;\r
893\r
894 //\r
895 // Often, it indicates a programming error to remove\r
896 // the last entry in an empty list\r
897 //\r
898 ASSERT (Map && !IsListEmpty (&Map->Used));\r
899\r
900 Item = NET_LIST_TAIL (&Map->Used, NET_MAP_ITEM, Link);\r
901 RemoveEntryList (&Item->Link);\r
902 Map->Count--;\r
903 InsertHeadList (&Map->Recycled, &Item->Link);\r
904\r
905 if (Value != NULL) {\r
906 *Value = Item->Value;\r
907 }\r
908\r
909 return Item->Key;\r
910}\r
911\r
912\r
913/**\r
914 Iterate through the netmap and call CallBack for each item.\r
915 \r
916 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break\r
917 from the loop. It returns the CallBack's last return value. This function is \r
918 delete safe for the current item.\r
919\r
920 If Map is NULL, then ASSERT().\r
921 If CallBack is NULL, then ASSERT().\r
922 \r
923 @param[in] Map The Map to iterate through.\r
924 @param[in] CallBack The callback function to call for each item.\r
925 @param[in] Arg The opaque parameter to the callback.\r
926\r
927 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item\r
928 return EFI_SUCCESS.\r
929 @retval Others It returns the CallBack's last return value.\r
930\r
931**/\r
932EFI_STATUS\r
933EFIAPI\r
934NetMapIterate (\r
935 IN NET_MAP *Map,\r
936 IN NET_MAP_CALLBACK CallBack,\r
937 IN VOID *Arg\r
938 )\r
939{\r
940\r
941 LIST_ENTRY *Entry;\r
942 LIST_ENTRY *Next;\r
943 LIST_ENTRY *Head;\r
944 NET_MAP_ITEM *Item;\r
945 EFI_STATUS Result;\r
946\r
947 ASSERT ((Map != NULL) && (CallBack != NULL));\r
948\r
949 Head = &Map->Used;\r
950\r
951 if (IsListEmpty (Head)) {\r
952 return EFI_SUCCESS;\r
953 }\r
954\r
955 NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {\r
956 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
957 Result = CallBack (Map, Item, Arg);\r
958\r
959 if (EFI_ERROR (Result)) {\r
960 return Result;\r
961 }\r
962 }\r
963\r
964 return EFI_SUCCESS;\r
965}\r
966\r
967\r
968/**\r
969 This is the default unload handle for all the network drivers.\r
970\r
971 Disconnect the driver specified by ImageHandle from all the devices in the handle database.\r
972 Uninstall all the protocols installed in the driver entry point.\r
973 \r
974 @param[in] ImageHandle The drivers' driver image.\r
975\r
976 @retval EFI_SUCCESS The image is unloaded.\r
977 @retval Others Failed to unload the image.\r
978\r
979**/\r
980EFI_STATUS\r
981EFIAPI\r
982NetLibDefaultUnload (\r
983 IN EFI_HANDLE ImageHandle\r
984 )\r
985{\r
986 EFI_STATUS Status;\r
987 EFI_HANDLE *DeviceHandleBuffer;\r
988 UINTN DeviceHandleCount;\r
989 UINTN Index;\r
990 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;\r
991 EFI_COMPONENT_NAME_PROTOCOL *ComponentName;\r
992 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r
993\r
994 //\r
995 // Get the list of all the handles in the handle database.\r
996 // If there is an error getting the list, then the unload\r
997 // operation fails.\r
998 //\r
999 Status = gBS->LocateHandleBuffer (\r
1000 AllHandles,\r
1001 NULL,\r
1002 NULL,\r
1003 &DeviceHandleCount,\r
1004 &DeviceHandleBuffer\r
1005 );\r
1006\r
1007 if (EFI_ERROR (Status)) {\r
1008 return Status;\r
1009 }\r
1010\r
1011 //\r
1012 // Disconnect the driver specified by ImageHandle from all\r
1013 // the devices in the handle database.\r
1014 //\r
1015 for (Index = 0; Index < DeviceHandleCount; Index++) {\r
1016 Status = gBS->DisconnectController (\r
1017 DeviceHandleBuffer[Index],\r
1018 ImageHandle,\r
1019 NULL\r
1020 );\r
1021 }\r
1022\r
1023 //\r
1024 // Uninstall all the protocols installed in the driver entry point\r
1025 //\r
1026 for (Index = 0; Index < DeviceHandleCount; Index++) {\r
1027 Status = gBS->HandleProtocol (\r
1028 DeviceHandleBuffer[Index],\r
1029 &gEfiDriverBindingProtocolGuid,\r
1030 (VOID **) &DriverBinding\r
1031 );\r
1032\r
1033 if (EFI_ERROR (Status)) {\r
1034 continue;\r
1035 }\r
1036\r
1037 if (DriverBinding->ImageHandle != ImageHandle) {\r
1038 continue;\r
1039 }\r
1040\r
1041 gBS->UninstallProtocolInterface (\r
1042 ImageHandle,\r
1043 &gEfiDriverBindingProtocolGuid,\r
1044 DriverBinding\r
1045 );\r
1046 Status = gBS->HandleProtocol (\r
1047 DeviceHandleBuffer[Index],\r
1048 &gEfiComponentNameProtocolGuid,\r
1049 (VOID **) &ComponentName\r
1050 );\r
1051 if (!EFI_ERROR (Status)) {\r
1052 gBS->UninstallProtocolInterface (\r
1053 ImageHandle,\r
1054 &gEfiComponentNameProtocolGuid,\r
1055 ComponentName\r
1056 );\r
1057 }\r
1058\r
1059 Status = gBS->HandleProtocol (\r
1060 DeviceHandleBuffer[Index],\r
1061 &gEfiComponentName2ProtocolGuid,\r
1062 (VOID **) &ComponentName2\r
1063 );\r
1064 if (!EFI_ERROR (Status)) {\r
1065 gBS->UninstallProtocolInterface (\r
1066 ImageHandle,\r
1067 &gEfiComponentName2ProtocolGuid,\r
1068 ComponentName2\r
1069 );\r
1070 }\r
1071 }\r
1072\r
1073 //\r
1074 // Free the buffer containing the list of handles from the handle database\r
1075 //\r
1076 if (DeviceHandleBuffer != NULL) {\r
1077 gBS->FreePool (DeviceHandleBuffer);\r
1078 }\r
1079\r
1080 return EFI_SUCCESS;\r
1081}\r
1082\r
1083\r
1084\r
1085/**\r
1086 Create a child of the service that is identified by ServiceBindingGuid.\r
1087 \r
1088 Get the ServiceBinding Protocol first, then use it to create a child.\r
1089\r
1090 If ServiceBindingGuid is NULL, then ASSERT().\r
1091 If ChildHandle is NULL, then ASSERT().\r
1092 \r
1093 @param[in] Controller The controller which has the service installed.\r
1094 @param[in] Image The image handle used to open service.\r
1095 @param[in] ServiceBindingGuid The service's Guid.\r
1096 @param[in, out] ChildHandle The handle to receive the create child.\r
1097\r
1098 @retval EFI_SUCCESS The child is successfully created.\r
1099 @retval Others Failed to create the child.\r
1100\r
1101**/\r
1102EFI_STATUS\r
1103EFIAPI\r
1104NetLibCreateServiceChild (\r
1105 IN EFI_HANDLE Controller,\r
1106 IN EFI_HANDLE Image,\r
1107 IN EFI_GUID *ServiceBindingGuid,\r
1108 IN OUT EFI_HANDLE *ChildHandle\r
1109 )\r
1110{\r
1111 EFI_STATUS Status;\r
1112 EFI_SERVICE_BINDING_PROTOCOL *Service;\r
1113\r
1114\r
1115 ASSERT ((ServiceBindingGuid != NULL) && (ChildHandle != NULL));\r
1116\r
1117 //\r
1118 // Get the ServiceBinding Protocol\r
1119 //\r
1120 Status = gBS->OpenProtocol (\r
1121 Controller,\r
1122 ServiceBindingGuid,\r
1123 (VOID **) &Service,\r
1124 Image,\r
1125 Controller,\r
1126 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1127 );\r
1128\r
1129 if (EFI_ERROR (Status)) {\r
1130 return Status;\r
1131 }\r
1132\r
1133 //\r
1134 // Create a child\r
1135 //\r
1136 Status = Service->CreateChild (Service, ChildHandle);\r
1137 return Status;\r
1138}\r
1139\r
1140\r
1141/**\r
1142 Destory a child of the service that is identified by ServiceBindingGuid.\r
1143 \r
1144 Get the ServiceBinding Protocol first, then use it to destroy a child.\r
1145 \r
1146 If ServiceBindingGuid is NULL, then ASSERT().\r
1147 \r
1148 @param[in] Controller The controller which has the service installed.\r
1149 @param[in] Image The image handle used to open service.\r
1150 @param[in] ServiceBindingGuid The service's Guid.\r
1151 @param[in] ChildHandle The child to destory.\r
1152\r
1153 @retval EFI_SUCCESS The child is successfully destoried.\r
1154 @retval Others Failed to destory the child.\r
1155\r
1156**/\r
1157EFI_STATUS\r
1158EFIAPI\r
1159NetLibDestroyServiceChild (\r
1160 IN EFI_HANDLE Controller,\r
1161 IN EFI_HANDLE Image,\r
1162 IN EFI_GUID *ServiceBindingGuid,\r
1163 IN EFI_HANDLE ChildHandle\r
1164 )\r
1165{\r
1166 EFI_STATUS Status;\r
1167 EFI_SERVICE_BINDING_PROTOCOL *Service;\r
1168\r
1169 ASSERT (ServiceBindingGuid != NULL);\r
1170\r
1171 //\r
1172 // Get the ServiceBinding Protocol\r
1173 //\r
1174 Status = gBS->OpenProtocol (\r
1175 Controller,\r
1176 ServiceBindingGuid,\r
1177 (VOID **) &Service,\r
1178 Image,\r
1179 Controller,\r
1180 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1181 );\r
1182\r
1183 if (EFI_ERROR (Status)) {\r
1184 return Status;\r
1185 }\r
1186\r
1187 //\r
1188 // destory the child\r
1189 //\r
1190 Status = Service->DestroyChild (Service, ChildHandle);\r
1191 return Status;\r
1192}\r
1193\r
1194\r
1195/**\r
1196 Convert the mac address of the simple network protocol installed on\r
1197 SnpHandle to a unicode string. Callers are responsible for freeing the\r
1198 string storage.\r
1199\r
1200 Get the mac address of the Simple Network protocol from the SnpHandle. Then convert\r
1201 the mac address into a unicode string. It takes 2 unicode characters to represent \r
1202 a 1 byte binary buffer. Plus one unicode character for the null-terminator.\r
1203\r
1204\r
1205 @param[in] SnpHandle The handle where the simple network protocol is\r
1206 installed on.\r
1207 @param[in] ImageHandle The image handle used to act as the agent handle to\r
1208 get the simple network protocol.\r
1209 @param[out] MacString The pointer to store the address of the string\r
1210 representation of the mac address.\r
1211 \r
1212 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.\r
1213 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.\r
1214 @retval Others Failed to open the simple network protocol.\r
1215\r
1216**/\r
1217EFI_STATUS\r
1218EFIAPI\r
1219NetLibGetMacString (\r
1220 IN EFI_HANDLE SnpHandle,\r
1221 IN EFI_HANDLE ImageHandle,\r
1222 OUT CHAR16 **MacString\r
1223 )\r
1224{\r
1225 EFI_STATUS Status;\r
1226 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
1227 EFI_SIMPLE_NETWORK_MODE *Mode;\r
1228 CHAR16 *MacAddress;\r
1229 UINTN Index;\r
1230\r
1231 *MacString = NULL;\r
1232\r
1233 //\r
1234 // Get the Simple Network protocol from the SnpHandle.\r
1235 //\r
1236 Status = gBS->OpenProtocol (\r
1237 SnpHandle,\r
1238 &gEfiSimpleNetworkProtocolGuid,\r
1239 (VOID **) &Snp,\r
1240 ImageHandle,\r
1241 SnpHandle,\r
1242 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1243 );\r
1244 if (EFI_ERROR (Status)) {\r
1245 return Status;\r
1246 }\r
1247\r
1248 Mode = Snp->Mode;\r
1249\r
1250 //\r
1251 // It takes 2 unicode characters to represent a 1 byte binary buffer.\r
1252 // Plus one unicode character for the null-terminator.\r
1253 //\r
1254 MacAddress = AllocatePool ((2 * Mode->HwAddressSize + 1) * sizeof (CHAR16));\r
1255 if (MacAddress == NULL) {\r
1256 return EFI_OUT_OF_RESOURCES;\r
1257 }\r
1258\r
1259 //\r
1260 // Convert the mac address into a unicode string.\r
1261 //\r
1262 for (Index = 0; Index < Mode->HwAddressSize; Index++) {\r
1263 MacAddress[Index * 2] = (CHAR16) mNetLibHexStr[(Mode->CurrentAddress.Addr[Index] >> 4) & 0x0F];\r
1264 MacAddress[Index * 2 + 1] = (CHAR16) mNetLibHexStr[Mode->CurrentAddress.Addr[Index] & 0x0F];\r
1265 }\r
1266\r
1267 MacAddress[Mode->HwAddressSize * 2] = L'\0';\r
1268\r
1269 *MacString = MacAddress;\r
1270\r
1271 return EFI_SUCCESS;\r
1272}\r
1273\r
1274/**\r
1275 Check the default address used by the IPv4 driver is static or dynamic (acquired\r
1276 from DHCP).\r
1277\r
1278 If the controller handle does not have the NIC Ip4 Config Protocol installed, the \r
1279 default address is static. If the EFI variable to save the configuration is not found,\r
1280 the default address is static. Otherwise, get the result from the EFI variable which \r
1281 saving the configuration.\r
1282 \r
1283 @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol\r
1284 relative with the default address to judge.\r
1285\r
1286 @retval TRUE If the default address is static.\r
1287 @retval FALSE If the default address is acquired from DHCP.\r
1288\r
1289**/\r
1290BOOLEAN\r
1291NetLibDefaultAddressIsStatic (\r
1292 IN EFI_HANDLE Controller\r
1293 )\r
1294{\r
1295 EFI_STATUS Status;\r
1296 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;\r
1297 UINTN Len;\r
1298 NIC_IP4_CONFIG_INFO *ConfigInfo;\r
1299 BOOLEAN IsStatic;\r
1300 EFI_STRING ConfigHdr;\r
1301 EFI_STRING ConfigResp;\r
1302 EFI_STRING AccessProgress;\r
1303 EFI_STRING AccessResults;\r
1304 EFI_STRING String;\r
1305\r
1306 ConfigInfo = NULL;\r
1307 ConfigHdr = NULL;\r
1308 ConfigResp = NULL;\r
1309 AccessProgress = NULL;\r
1310 AccessResults = NULL;\r
1311 IsStatic = TRUE;\r
1312\r
1313 Status = gBS->LocateProtocol (\r
1314 &gEfiHiiConfigRoutingProtocolGuid,\r
1315 NULL,\r
1316 (VOID **) &HiiConfigRouting\r
1317 );\r
1318 if (EFI_ERROR (Status)) {\r
1319 return TRUE;\r
1320 }\r
1321\r
1322 //\r
1323 // Construct config request string header\r
1324 //\r
1325 ConfigHdr = HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid, EFI_NIC_IP4_CONFIG_VARIABLE, Controller);\r
1326 if (ConfigHdr == NULL) {\r
1327 return TRUE;\r
1328 }\r
1329 \r
1330 Len = StrLen (ConfigHdr);\r
1331 ConfigResp = AllocateZeroPool ((Len + NIC_ITEM_CONFIG_SIZE * 2 + 100) * sizeof (CHAR16));\r
1332 if (ConfigResp == NULL) {\r
1333 goto ON_EXIT;\r
1334 }\r
1335 StrCpy (ConfigResp, ConfigHdr);\r
1336\r
1337 String = ConfigResp + Len;\r
1338 UnicodeSPrint (\r
1339 String, \r
1340 (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16), \r
1341 L"&OFFSET=%04X&WIDTH=%04X", \r
1342 OFFSET_OF (NIC_IP4_CONFIG_INFO, Source), \r
1343 sizeof (UINT32)\r
1344 );\r
1345\r
1346 Status = HiiConfigRouting->ExtractConfig (\r
1347 HiiConfigRouting,\r
1348 ConfigResp,\r
1349 &AccessProgress,\r
1350 &AccessResults\r
1351 );\r
1352 if (EFI_ERROR (Status)) {\r
1353 goto ON_EXIT;\r
1354 }\r
1355\r
1356 ConfigInfo = AllocateZeroPool (sizeof (NIC_ITEM_CONFIG_SIZE));\r
1357 if (ConfigInfo == NULL) {\r
1358 goto ON_EXIT;\r
1359 }\r
1360\r
1361 ConfigInfo->Source = IP4_CONFIG_SOURCE_STATIC;\r
1362 Len = NIC_ITEM_CONFIG_SIZE;\r
1363 Status = HiiConfigRouting->ConfigToBlock (\r
1364 HiiConfigRouting,\r
1365 AccessResults,\r
1366 (UINT8 *) ConfigInfo,\r
1367 &Len,\r
1368 &AccessProgress\r
1369 );\r
1370 if (EFI_ERROR (Status)) {\r
1371 goto ON_EXIT;\r
1372 }\r
1373\r
1374 IsStatic = (BOOLEAN) (ConfigInfo->Source == IP4_CONFIG_SOURCE_STATIC);\r
1375 \r
1376ON_EXIT:\r
1377\r
1378 if (AccessResults != NULL) {\r
1379 FreePool (AccessResults);\r
1380 }\r
1381 if (ConfigInfo != NULL) {\r
1382 FreePool (ConfigInfo);\r
1383 }\r
1384 if (ConfigResp != NULL) {\r
1385 FreePool (ConfigResp);\r
1386 }\r
1387 if (ConfigHdr != NULL) {\r
1388 FreePool (ConfigHdr);\r
1389 }\r
1390\r
1391 return IsStatic;\r
1392}\r
1393\r
1394/**\r
1395 Create an IPv4 device path node.\r
1396 \r
1397 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.\r
1398 The header subtype of IPv4 device path node is MSG_IPv4_DP.\r
1399 The length of the IPv4 device path node in bytes is 19.\r
1400 Get other info from parameters to make up the whole IPv4 device path node.\r
1401\r
1402 @param[in, out] Node Pointer to the IPv4 device path node.\r
1403 @param[in] Controller The handle where the NIC IP4 config protocol resides.\r
1404 @param[in] LocalIp The local IPv4 address.\r
1405 @param[in] LocalPort The local port.\r
1406 @param[in] RemoteIp The remote IPv4 address.\r
1407 @param[in] RemotePort The remote port.\r
1408 @param[in] Protocol The protocol type in the IP header.\r
1409 @param[in] UseDefaultAddress Whether this instance is using default address or not.\r
1410\r
1411**/\r
1412VOID\r
1413EFIAPI\r
1414NetLibCreateIPv4DPathNode (\r
1415 IN OUT IPv4_DEVICE_PATH *Node,\r
1416 IN EFI_HANDLE Controller,\r
1417 IN IP4_ADDR LocalIp,\r
1418 IN UINT16 LocalPort,\r
1419 IN IP4_ADDR RemoteIp,\r
1420 IN UINT16 RemotePort,\r
1421 IN UINT16 Protocol,\r
1422 IN BOOLEAN UseDefaultAddress\r
1423 )\r
1424{\r
1425 Node->Header.Type = MESSAGING_DEVICE_PATH;\r
1426 Node->Header.SubType = MSG_IPv4_DP;\r
1427 SetDevicePathNodeLength (&Node->Header, 19);\r
1428\r
1429 CopyMem (&Node->LocalIpAddress, &LocalIp, sizeof (EFI_IPv4_ADDRESS));\r
1430 CopyMem (&Node->RemoteIpAddress, &RemoteIp, sizeof (EFI_IPv4_ADDRESS));\r
1431\r
1432 Node->LocalPort = LocalPort;\r
1433 Node->RemotePort = RemotePort;\r
1434\r
1435 Node->Protocol = Protocol;\r
1436\r
1437 if (!UseDefaultAddress) {\r
1438 Node->StaticIpAddress = TRUE;\r
1439 } else {\r
1440 Node->StaticIpAddress = NetLibDefaultAddressIsStatic (Controller);\r
1441 }\r
1442}\r
1443\r
1444\r
1445/**\r
1446 Find the UNDI/SNP handle from controller and protocol GUID.\r
1447 \r
1448 For example, IP will open a MNP child to transmit/receive\r
1449 packets, when MNP is stopped, IP should also be stopped. IP\r
1450 needs to find its own private data which is related the IP's\r
1451 service binding instance that is install on UNDI/SNP handle.\r
1452 Now, the controller is either a MNP or ARP child handle. But\r
1453 IP opens these handle BY_DRIVER, use that info, we can get the\r
1454 UNDI/SNP handle.\r
1455\r
1456 @param[in] Controller Then protocol handle to check.\r
1457 @param[in] ProtocolGuid The protocol that is related with the handle.\r
1458\r
1459 @return The UNDI/SNP handle or NULL for errors.\r
1460\r
1461**/\r
1462EFI_HANDLE\r
1463EFIAPI\r
1464NetLibGetNicHandle (\r
1465 IN EFI_HANDLE Controller,\r
1466 IN EFI_GUID *ProtocolGuid\r
1467 )\r
1468{\r
1469 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer;\r
1470 EFI_HANDLE Handle;\r
1471 EFI_STATUS Status;\r
1472 UINTN OpenCount;\r
1473 UINTN Index;\r
1474\r
1475 Status = gBS->OpenProtocolInformation (\r
1476 Controller,\r
1477 ProtocolGuid,\r
1478 &OpenBuffer,\r
1479 &OpenCount\r
1480 );\r
1481\r
1482 if (EFI_ERROR (Status)) {\r
1483 return NULL;\r
1484 }\r
1485\r
1486 Handle = NULL;\r
1487\r
1488 for (Index = 0; Index < OpenCount; Index++) {\r
1489 if (OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {\r
1490 Handle = OpenBuffer[Index].ControllerHandle;\r
1491 break;\r
1492 }\r
1493 }\r
1494\r
1495 gBS->FreePool (OpenBuffer);\r
1496 return Handle;\r
1497}\r