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