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