]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
1. Fixed issue when calculating timeout value in timeout function in MNP module.
[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
347cacce 30#include <Protocol/Ip4.h>\r
e1a590c3 31#include <Protocol/Dpc.h>\r
da1d0201 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
cbf316f2 39#include <Library/UefiLib.h>\r
da1d0201 40#include <Library/MemoryAllocationLib.h>\r
1232b214 41#include <Library/DevicePathLib.h>\r
da1d0201 42\r
36ee91ca 43EFI_DPC_PROTOCOL *mDpc = NULL;\r
44\r
da1d0201 45//\r
46// All the supported IP4 maskes in host byte order.\r
47//\r
2a86ff1c 48IP4_ADDR gIp4AllMasks[IP4_MASK_NUM] = {\r
da1d0201 49 0x00000000,\r
50 0x80000000,\r
51 0xC0000000,\r
52 0xE0000000,\r
53 0xF0000000,\r
54 0xF8000000,\r
55 0xFC000000,\r
56 0xFE000000,\r
57\r
58 0xFF000000,\r
59 0xFF800000,\r
60 0xFFC00000,\r
61 0xFFE00000,\r
62 0xFFF00000,\r
63 0xFFF80000,\r
64 0xFFFC0000,\r
65 0xFFFE0000,\r
66\r
67 0xFFFF0000,\r
68 0xFFFF8000,\r
69 0xFFFFC000,\r
70 0xFFFFE000,\r
71 0xFFFFF000,\r
72 0xFFFFF800,\r
73 0xFFFFFC00,\r
74 0xFFFFFE00,\r
75\r
76 0xFFFFFF00,\r
77 0xFFFFFF80,\r
78 0xFFFFFFC0,\r
79 0xFFFFFFE0,\r
80 0xFFFFFFF0,\r
81 0xFFFFFFF8,\r
82 0xFFFFFFFC,\r
83 0xFFFFFFFE,\r
84 0xFFFFFFFF,\r
85};\r
86\r
87EFI_IPv4_ADDRESS mZeroIp4Addr = {{0, 0, 0, 0}};\r
88\r
da1d0201 89/**\r
90 Return the length of the mask. If the mask is invalid,\r
91 return the invalid length 33, which is IP4_MASK_NUM.\r
92 NetMask is in the host byte order.\r
93\r
94 @param NetMask The netmask to get the length from\r
95\r
96 @return The length of the netmask, IP4_MASK_NUM if the mask isn't\r
97 @return supported.\r
98\r
99**/\r
100INTN\r
7b414b4e 101EFIAPI\r
da1d0201 102NetGetMaskLength (\r
103 IN IP4_ADDR NetMask\r
104 )\r
105{\r
106 INTN Index;\r
107\r
108 for (Index = 0; Index < IP4_MASK_NUM; Index++) {\r
2a86ff1c 109 if (NetMask == gIp4AllMasks[Index]) {\r
da1d0201 110 break;\r
111 }\r
112 }\r
113\r
114 return Index;\r
115}\r
116\r
117\r
118\r
119/**\r
120 Return the class of the address, such as class a, b, c.\r
121 Addr is in host byte order.\r
122\r
123 @param Addr The address to get the class from\r
124\r
125 @return IP address class, such as IP4_ADDR_CLASSA\r
126\r
127**/\r
128INTN\r
7b414b4e 129EFIAPI\r
da1d0201 130NetGetIpClass (\r
131 IN IP4_ADDR Addr\r
132 )\r
133{\r
134 UINT8 ByteOne;\r
135\r
136 ByteOne = (UINT8) (Addr >> 24);\r
137\r
138 if ((ByteOne & 0x80) == 0) {\r
139 return IP4_ADDR_CLASSA;\r
140\r
141 } else if ((ByteOne & 0xC0) == 0x80) {\r
142 return IP4_ADDR_CLASSB;\r
143\r
144 } else if ((ByteOne & 0xE0) == 0xC0) {\r
145 return IP4_ADDR_CLASSC;\r
146\r
147 } else if ((ByteOne & 0xF0) == 0xE0) {\r
148 return IP4_ADDR_CLASSD;\r
149\r
150 } else {\r
151 return IP4_ADDR_CLASSE;\r
152\r
153 }\r
154}\r
155\r
156\r
157/**\r
158 Check whether the IP is a valid unicast address according to\r
159 the netmask. If NetMask is zero, use the IP address's class to\r
160 get the default mask.\r
161\r
162 @param Ip The IP to check againist\r
163 @param NetMask The mask of the IP\r
164\r
165 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE\r
166\r
167**/\r
168BOOLEAN\r
7b414b4e 169EFIAPI\r
da1d0201 170Ip4IsUnicast (\r
171 IN IP4_ADDR Ip,\r
172 IN IP4_ADDR NetMask\r
173 )\r
174{\r
175 INTN Class;\r
176\r
177 Class = NetGetIpClass (Ip);\r
178\r
179 if ((Ip == 0) || (Class >= IP4_ADDR_CLASSD)) {\r
180 return FALSE;\r
181 }\r
182\r
183 if (NetMask == 0) {\r
2a86ff1c 184 NetMask = gIp4AllMasks[Class << 3];\r
da1d0201 185 }\r
186\r
187 if (((Ip &~NetMask) == ~NetMask) || ((Ip &~NetMask) == 0)) {\r
188 return FALSE;\r
189 }\r
190\r
191 return TRUE;\r
192}\r
193\r
194\r
195/**\r
196 Initialize a random seed using current time.\r
197\r
198 None\r
199\r
200 @return The random seed initialized with current time.\r
201\r
202**/\r
203UINT32\r
7b414b4e 204EFIAPI\r
da1d0201 205NetRandomInitSeed (\r
206 VOID\r
207 )\r
208{\r
209 EFI_TIME Time;\r
210 UINT32 Seed;\r
211\r
212 gRT->GetTime (&Time, NULL);\r
36ee91ca 213 Seed = (~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second);\r
da1d0201 214 Seed ^= Time.Nanosecond;\r
215 Seed ^= Time.Year << 7;\r
216\r
217 return Seed;\r
218}\r
219\r
220\r
221/**\r
222 Extract a UINT32 from a byte stream, then convert it to host\r
223 byte order. Use this function to avoid alignment error.\r
224\r
225 @param Buf The buffer to extract the UINT32.\r
226\r
227 @return The UINT32 extracted.\r
228\r
229**/\r
230UINT32\r
7b414b4e 231EFIAPI\r
da1d0201 232NetGetUint32 (\r
233 IN UINT8 *Buf\r
234 )\r
235{\r
236 UINT32 Value;\r
237\r
e48e37fc 238 CopyMem (&Value, Buf, sizeof (UINT32));\r
da1d0201 239 return NTOHL (Value);\r
240}\r
241\r
242\r
243/**\r
244 Put a UINT32 to the byte stream. Convert it from host byte order\r
245 to network byte order before putting.\r
246\r
247 @param Buf The buffer to put the UINT32\r
248 @param Data The data to put\r
249\r
250 @return None\r
251\r
252**/\r
253VOID\r
7b414b4e 254EFIAPI\r
da1d0201 255NetPutUint32 (\r
256 IN UINT8 *Buf,\r
257 IN UINT32 Data\r
258 )\r
259{\r
260 Data = HTONL (Data);\r
e48e37fc 261 CopyMem (Buf, &Data, sizeof (UINT32));\r
da1d0201 262}\r
263\r
264\r
265/**\r
266 Remove the first entry on the list\r
267\r
268 @param Head The list header\r
269\r
270 @return The entry that is removed from the list, NULL if the list is empty.\r
271\r
272**/\r
e48e37fc 273LIST_ENTRY *\r
7b414b4e 274EFIAPI\r
da1d0201 275NetListRemoveHead (\r
e48e37fc 276 LIST_ENTRY *Head\r
da1d0201 277 )\r
278{\r
e48e37fc 279 LIST_ENTRY *First;\r
da1d0201 280\r
281 ASSERT (Head != NULL);\r
282\r
e48e37fc 283 if (IsListEmpty (Head)) {\r
da1d0201 284 return NULL;\r
285 }\r
286\r
287 First = Head->ForwardLink;\r
288 Head->ForwardLink = First->ForwardLink;\r
289 First->ForwardLink->BackLink = Head;\r
290\r
291 DEBUG_CODE (\r
e48e37fc 292 First->ForwardLink = (LIST_ENTRY *) NULL;\r
293 First->BackLink = (LIST_ENTRY *) NULL;\r
da1d0201 294 );\r
295\r
296 return First;\r
297}\r
298\r
299\r
300/**\r
301 Remove the last entry on the list\r
302\r
303 @param Head The list head\r
304\r
305 @return The entry that is removed from the list, NULL if the list is empty.\r
306\r
307**/\r
e48e37fc 308LIST_ENTRY *\r
7b414b4e 309EFIAPI\r
da1d0201 310NetListRemoveTail (\r
e48e37fc 311 LIST_ENTRY *Head\r
da1d0201 312 )\r
313{\r
e48e37fc 314 LIST_ENTRY *Last;\r
da1d0201 315\r
316 ASSERT (Head != NULL);\r
317\r
e48e37fc 318 if (IsListEmpty (Head)) {\r
da1d0201 319 return NULL;\r
320 }\r
321\r
322 Last = Head->BackLink;\r
323 Head->BackLink = Last->BackLink;\r
324 Last->BackLink->ForwardLink = Head;\r
325\r
326 DEBUG_CODE (\r
e48e37fc 327 Last->ForwardLink = (LIST_ENTRY *) NULL;\r
328 Last->BackLink = (LIST_ENTRY *) NULL;\r
da1d0201 329 );\r
330\r
331 return Last;\r
332}\r
333\r
334\r
335/**\r
336 Insert the NewEntry after the PrevEntry\r
337\r
338 @param PrevEntry The previous entry to insert after\r
339 @param NewEntry The new entry to insert\r
340\r
341 @return None\r
342\r
343**/\r
344VOID\r
7b414b4e 345EFIAPI\r
da1d0201 346NetListInsertAfter (\r
e48e37fc 347 IN LIST_ENTRY *PrevEntry,\r
348 IN LIST_ENTRY *NewEntry\r
da1d0201 349 )\r
350{\r
351 NewEntry->BackLink = PrevEntry;\r
352 NewEntry->ForwardLink = PrevEntry->ForwardLink;\r
353 PrevEntry->ForwardLink->BackLink = NewEntry;\r
354 PrevEntry->ForwardLink = NewEntry;\r
355}\r
356\r
357\r
358/**\r
359 Insert the NewEntry before the PostEntry\r
360\r
361 @param PostEntry The entry to insert before\r
362 @param NewEntry The new entry to insert\r
363\r
364 @return None\r
365\r
366**/\r
367VOID\r
7b414b4e 368EFIAPI\r
da1d0201 369NetListInsertBefore (\r
e48e37fc 370 IN LIST_ENTRY *PostEntry,\r
371 IN LIST_ENTRY *NewEntry\r
da1d0201 372 )\r
373{\r
374 NewEntry->ForwardLink = PostEntry;\r
375 NewEntry->BackLink = PostEntry->BackLink;\r
376 PostEntry->BackLink->ForwardLink = NewEntry;\r
377 PostEntry->BackLink = NewEntry;\r
378}\r
379\r
380\r
381/**\r
382 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.\r
383\r
384 @param Map The netmap to initialize\r
385\r
386 @return None\r
387\r
388**/\r
389VOID\r
7b414b4e 390EFIAPI\r
da1d0201 391NetMapInit (\r
392 IN NET_MAP *Map\r
393 )\r
394{\r
395 ASSERT (Map != NULL);\r
396\r
e48e37fc 397 InitializeListHead (&Map->Used);\r
398 InitializeListHead (&Map->Recycled);\r
da1d0201 399 Map->Count = 0;\r
400}\r
401\r
402\r
403/**\r
404 To clean up the netmap, that is, release allocated memories.\r
405\r
406 @param Map The netmap to clean up.\r
407\r
408 @return None\r
409\r
410**/\r
411VOID\r
7b414b4e 412EFIAPI\r
da1d0201 413NetMapClean (\r
414 IN NET_MAP *Map\r
415 )\r
416{\r
417 NET_MAP_ITEM *Item;\r
e48e37fc 418 LIST_ENTRY *Entry;\r
419 LIST_ENTRY *Next;\r
da1d0201 420\r
421 ASSERT (Map != NULL);\r
422\r
423 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Used) {\r
424 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
425\r
e48e37fc 426 RemoveEntryList (&Item->Link);\r
da1d0201 427 Map->Count--;\r
428\r
e48e37fc 429 gBS->FreePool (Item);\r
da1d0201 430 }\r
431\r
e48e37fc 432 ASSERT ((Map->Count == 0) && IsListEmpty (&Map->Used));\r
da1d0201 433\r
434 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Recycled) {\r
435 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
436\r
e48e37fc 437 RemoveEntryList (&Item->Link);\r
438 gBS->FreePool (Item);\r
da1d0201 439 }\r
440\r
e48e37fc 441 ASSERT (IsListEmpty (&Map->Recycled));\r
da1d0201 442}\r
443\r
444\r
445/**\r
446 Test whether the netmap is empty\r
447\r
448 @param Map The net map to test\r
449\r
450 @return TRUE if the netmap is empty, otherwise FALSE.\r
451\r
452**/\r
453BOOLEAN\r
7b414b4e 454EFIAPI\r
da1d0201 455NetMapIsEmpty (\r
456 IN NET_MAP *Map\r
457 )\r
458{\r
459 ASSERT (Map != NULL);\r
460 return (BOOLEAN) (Map->Count == 0);\r
461}\r
462\r
463\r
464/**\r
465 Return the number of the <Key, Value> pairs in the netmap.\r
466\r
467 @param Map The netmap to get the entry number\r
468\r
469 @return The entry number in the netmap.\r
470\r
471**/\r
472UINTN\r
7b414b4e 473EFIAPI\r
da1d0201 474NetMapGetCount (\r
475 IN NET_MAP *Map\r
476 )\r
477{\r
478 return Map->Count;\r
479}\r
480\r
481\r
482/**\r
483 Allocate an item for the netmap. It will try to allocate\r
484 a batch of items and return one.\r
485\r
486 @param Map The netmap to allocate item for\r
487\r
488 @return The allocated item or NULL\r
489\r
490**/\r
da1d0201 491NET_MAP_ITEM *\r
492NetMapAllocItem (\r
493 IN NET_MAP *Map\r
494 )\r
495{\r
496 NET_MAP_ITEM *Item;\r
e48e37fc 497 LIST_ENTRY *Head;\r
da1d0201 498 UINTN Index;\r
499\r
500 ASSERT (Map != NULL);\r
501\r
502 Head = &Map->Recycled;\r
503\r
e48e37fc 504 if (IsListEmpty (Head)) {\r
da1d0201 505 for (Index = 0; Index < NET_MAP_INCREAMENT; Index++) {\r
e48e37fc 506 Item = AllocatePool (sizeof (NET_MAP_ITEM));\r
da1d0201 507\r
508 if (Item == NULL) {\r
509 if (Index == 0) {\r
510 return NULL;\r
511 }\r
512\r
513 break;\r
514 }\r
515\r
e48e37fc 516 InsertHeadList (Head, &Item->Link);\r
da1d0201 517 }\r
518 }\r
519\r
520 Item = NET_LIST_HEAD (Head, NET_MAP_ITEM, Link);\r
521 NetListRemoveHead (Head);\r
522\r
523 return Item;\r
524}\r
525\r
526\r
527/**\r
528 Allocate an item to save the <Key, Value> pair to the head of the netmap.\r
529\r
530 @param Map The netmap to insert into\r
531 @param Key The user's key\r
532 @param Value The user's value for the key\r
533\r
534 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item\r
535 @retval EFI_SUCCESS The item is inserted to the head\r
536\r
537**/\r
538EFI_STATUS\r
7b414b4e 539EFIAPI\r
da1d0201 540NetMapInsertHead (\r
541 IN NET_MAP *Map,\r
542 IN VOID *Key,\r
543 IN VOID *Value OPTIONAL\r
544 )\r
545{\r
546 NET_MAP_ITEM *Item;\r
547\r
548 ASSERT (Map != NULL);\r
549\r
550 Item = NetMapAllocItem (Map);\r
551\r
552 if (Item == NULL) {\r
553 return EFI_OUT_OF_RESOURCES;\r
554 }\r
555\r
556 Item->Key = Key;\r
557 Item->Value = Value;\r
e48e37fc 558 InsertHeadList (&Map->Used, &Item->Link);\r
da1d0201 559\r
560 Map->Count++;\r
561 return EFI_SUCCESS;\r
562}\r
563\r
564\r
565/**\r
566 Allocate an item to save the <Key, Value> pair to the tail of the netmap.\r
567\r
568 @param Map The netmap to insert into\r
569 @param Key The user's key\r
570 @param Value The user's value for the key\r
571\r
572 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item\r
573 @retval EFI_SUCCESS The item is inserted to the tail\r
574\r
575**/\r
576EFI_STATUS\r
7b414b4e 577EFIAPI\r
da1d0201 578NetMapInsertTail (\r
579 IN NET_MAP *Map,\r
580 IN VOID *Key,\r
581 IN VOID *Value OPTIONAL\r
582 )\r
583{\r
584 NET_MAP_ITEM *Item;\r
585\r
586 ASSERT (Map != NULL);\r
587\r
588 Item = NetMapAllocItem (Map);\r
589\r
590 if (Item == NULL) {\r
591 return EFI_OUT_OF_RESOURCES;\r
592 }\r
593\r
594 Item->Key = Key;\r
595 Item->Value = Value;\r
e48e37fc 596 InsertTailList (&Map->Used, &Item->Link);\r
da1d0201 597\r
598 Map->Count++;\r
599\r
600 return EFI_SUCCESS;\r
601}\r
602\r
603\r
604/**\r
605 Check whther the item is in the Map\r
606\r
607 @param Map The netmap to search within\r
608 @param Item The item to search\r
609\r
610 @return TRUE if the item is in the netmap, otherwise FALSE.\r
611\r
612**/\r
da1d0201 613BOOLEAN\r
614NetItemInMap (\r
615 IN NET_MAP *Map,\r
616 IN NET_MAP_ITEM *Item\r
617 )\r
618{\r
e48e37fc 619 LIST_ENTRY *ListEntry;\r
da1d0201 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
7b414b4e 641EFIAPI\r
da1d0201 642NetMapFindKey (\r
643 IN NET_MAP *Map,\r
644 IN VOID *Key\r
645 )\r
646{\r
e48e37fc 647 LIST_ENTRY *Entry;\r
da1d0201 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
7b414b4e 675EFIAPI\r
da1d0201 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
e48e37fc 685 RemoveEntryList (&Item->Link);\r
da1d0201 686 Map->Count--;\r
e48e37fc 687 InsertHeadList (&Map->Recycled, &Item->Link);\r
da1d0201 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
7b414b4e 707EFIAPI\r
da1d0201 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
e48e37fc 719 ASSERT (Map && !IsListEmpty (&Map->Used));\r
da1d0201 720\r
721 Item = NET_LIST_HEAD (&Map->Used, NET_MAP_ITEM, Link);\r
e48e37fc 722 RemoveEntryList (&Item->Link);\r
da1d0201 723 Map->Count--;\r
e48e37fc 724 InsertHeadList (&Map->Recycled, &Item->Link);\r
da1d0201 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
7b414b4e 744EFIAPI\r
da1d0201 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
e48e37fc 756 ASSERT (Map && !IsListEmpty (&Map->Used));\r
da1d0201 757\r
758 Item = NET_LIST_TAIL (&Map->Used, NET_MAP_ITEM, Link);\r
e48e37fc 759 RemoveEntryList (&Item->Link);\r
da1d0201 760 Map->Count--;\r
e48e37fc 761 InsertHeadList (&Map->Recycled, &Item->Link);\r
da1d0201 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
7b414b4e 785EFIAPI\r
da1d0201 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
e48e37fc 793 LIST_ENTRY *Entry;\r
794 LIST_ENTRY *Next;\r
795 LIST_ENTRY *Head;\r
da1d0201 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
e48e37fc 803 if (IsListEmpty (Head)) {\r
da1d0201 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
3012ce5c 841 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r
da1d0201 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
3012ce5c 910 &gEfiComponentName2ProtocolGuid,\r
911 (VOID **) &ComponentName2\r
da1d0201 912 );\r
da1d0201 913 if (!EFI_ERROR (Status)) {\r
914 gBS->UninstallProtocolInterface (\r
3012ce5c 915 ImageHandle,\r
916 &gEfiComponentName2ProtocolGuid,\r
917 ComponentName2\r
918 );\r
da1d0201 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
7b414b4e 947EFIAPI\r
da1d0201 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
7b414b4e 998EFIAPI\r
da1d0201 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
7b414b4e 1052EFIAPI\r
da1d0201 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
e48e37fc 1088 MacAddress = AllocatePool ((2 * Mode->HwAddressSize + 1) * sizeof (CHAR16));\r
da1d0201 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
da1d0201 1119BOOLEAN\r
1120NetLibDefaultAddressIsStatic (\r
1121 IN EFI_HANDLE Controller\r
1122 )\r
1123{\r
1124 EFI_STATUS Status;\r
1125 EFI_NIC_IP4_CONFIG_PROTOCOL *NicIp4;\r
1126 UINTN Len;\r
1127 NIC_IP4_CONFIG_INFO *ConfigInfo;\r
1128 BOOLEAN IsStatic;\r
1129\r
1130 Status = gBS->HandleProtocol (\r
1131 Controller,\r
1132 &gEfiNicIp4ConfigProtocolGuid,\r
1133 (VOID **) &NicIp4\r
1134 );\r
1135 if (EFI_ERROR (Status)) {\r
1136 return TRUE;\r
1137 }\r
1138\r
1139 Len = 0;\r
1140 Status = NicIp4->GetInfo (NicIp4, &Len, NULL);\r
1141 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1142 return TRUE;\r
1143 }\r
1144\r
e48e37fc 1145 ConfigInfo = AllocatePool (Len);\r
da1d0201 1146 if (ConfigInfo == NULL) {\r
1147 return TRUE;\r
1148 }\r
1149\r
1150 IsStatic = TRUE;\r
1151 Status = NicIp4->GetInfo (NicIp4, &Len, ConfigInfo);\r
1152 if (EFI_ERROR (Status)) {\r
1153 goto ON_EXIT;\r
1154 }\r
1155\r
1156 IsStatic = (BOOLEAN) (ConfigInfo->Source == IP4_CONFIG_SOURCE_STATIC);\r
1157\r
1158ON_EXIT:\r
1159\r
e48e37fc 1160 gBS->FreePool (ConfigInfo);\r
da1d0201 1161\r
1162 return IsStatic;\r
1163}\r
1164\r
1165/**\r
1166 Create an IPv4 device path node.\r
1167\r
1168 @param Node Pointer to the IPv4 device path node.\r
1169 @param Controller The handle where the NIC IP4 config protocol resides.\r
1170 @param LocalIp The local IPv4 address.\r
1171 @param LocalPort The local port.\r
1172 @param RemoteIp The remote IPv4 address.\r
1173 @param RemotePort The remote port.\r
1174 @param Protocol The protocol type in the IP header.\r
1175 @param UseDefaultAddress Whether this instance is using default address or not.\r
1176\r
1177 @retval None\r
1178**/\r
1179VOID\r
7b414b4e 1180EFIAPI\r
da1d0201 1181NetLibCreateIPv4DPathNode (\r
1182 IN OUT IPv4_DEVICE_PATH *Node,\r
1183 IN EFI_HANDLE Controller,\r
1184 IN IP4_ADDR LocalIp,\r
1185 IN UINT16 LocalPort,\r
1186 IN IP4_ADDR RemoteIp,\r
1187 IN UINT16 RemotePort,\r
1188 IN UINT16 Protocol,\r
1189 IN BOOLEAN UseDefaultAddress\r
1190 )\r
1191{\r
1192 Node->Header.Type = MESSAGING_DEVICE_PATH;\r
1193 Node->Header.SubType = MSG_IPv4_DP;\r
1194 SetDevicePathNodeLength (&Node->Header, 19);\r
1195\r
e48e37fc 1196 CopyMem (&Node->LocalIpAddress, &LocalIp, sizeof (EFI_IPv4_ADDRESS));\r
1197 CopyMem (&Node->RemoteIpAddress, &RemoteIp, sizeof (EFI_IPv4_ADDRESS));\r
da1d0201 1198\r
1199 Node->LocalPort = LocalPort;\r
1200 Node->RemotePort = RemotePort;\r
1201\r
1202 Node->Protocol = Protocol;\r
1203\r
1204 if (!UseDefaultAddress) {\r
1205 Node->StaticIpAddress = TRUE;\r
1206 } else {\r
1207 Node->StaticIpAddress = NetLibDefaultAddressIsStatic (Controller);\r
1208 }\r
1209}\r
1210\r
1211\r
1212/**\r
1213 Find the UNDI/SNP handle from controller and protocol GUID.\r
1214 For example, IP will open a MNP child to transmit/receive\r
1215 packets, when MNP is stopped, IP should also be stopped. IP\r
1216 needs to find its own private data which is related the IP's\r
1217 service binding instance that is install on UNDI/SNP handle.\r
1218 Now, the controller is either a MNP or ARP child handle. But\r
1219 IP opens these handle BY_DRIVER, use that info, we can get the\r
1220 UNDI/SNP handle.\r
1221\r
1222 @param Controller Then protocol handle to check\r
1223 @param ProtocolGuid The protocol that is related with the handle.\r
1224\r
1225 @return The UNDI/SNP handle or NULL.\r
1226\r
1227**/\r
1228EFI_HANDLE\r
7b414b4e 1229EFIAPI\r
da1d0201 1230NetLibGetNicHandle (\r
1231 IN EFI_HANDLE Controller,\r
1232 IN EFI_GUID *ProtocolGuid\r
1233 )\r
1234{\r
1235 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer;\r
1236 EFI_HANDLE Handle;\r
1237 EFI_STATUS Status;\r
1238 UINTN OpenCount;\r
1239 UINTN Index;\r
1240\r
1241 Status = gBS->OpenProtocolInformation (\r
1242 Controller,\r
1243 ProtocolGuid,\r
1244 &OpenBuffer,\r
1245 &OpenCount\r
1246 );\r
1247\r
1248 if (EFI_ERROR (Status)) {\r
1249 return NULL;\r
1250 }\r
1251\r
1252 Handle = NULL;\r
1253\r
1254 for (Index = 0; Index < OpenCount; Index++) {\r
1255 if (OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {\r
1256 Handle = OpenBuffer[Index].ControllerHandle;\r
1257 break;\r
1258 }\r
1259 }\r
1260\r
1261 gBS->FreePool (OpenBuffer);\r
1262 return Handle;\r
1263}\r
1264\r
36ee91ca 1265/**\r
1266 Add a Deferred Procedure Call to the end of the DPC queue.\r
1267\r
1268 @DpcTpl The EFI_TPL that the DPC should be invoked.\r
1269 @DpcProcedure Pointer to the DPC's function.\r
1270 @DpcContext Pointer to the DPC's context. Passed to DpcProcedure\r
1271 when DpcProcedure is invoked.\r
1272\r
1273 @retval EFI_SUCCESS The DPC was queued.\r
1274 @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL.\r
1275 DpcProcedure is NULL.\r
1276 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r
1277 add the DPC to the queue.\r
1278\r
1279**/\r
1280EFI_STATUS\r
7b414b4e 1281EFIAPI\r
36ee91ca 1282NetLibQueueDpc (\r
1283 IN EFI_TPL DpcTpl,\r
1284 IN EFI_DPC_PROCEDURE DpcProcedure,\r
1285 IN VOID *DpcContext OPTIONAL\r
1286 )\r
1287{\r
1288 return mDpc->QueueDpc (mDpc, DpcTpl, DpcProcedure, DpcContext);\r
1289}\r
1290\r
1291/**\r
1292 Add a Deferred Procedure Call to the end of the DPC queue.\r
1293\r
1294 @retval EFI_SUCCESS One or more DPCs were invoked.\r
1295 @retval EFI_NOT_FOUND No DPCs were invoked.\r
1296\r
1297**/\r
1298EFI_STATUS\r
7b414b4e 1299EFIAPI\r
36ee91ca 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