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