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