]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
Patch to remove STATIC modifier. This is on longer recommended by EFI Framework codin...
[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
2a86ff1c 46IP4_ADDR gIp4AllMasks[IP4_MASK_NUM] = {\r
da1d0201 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
da1d0201 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
7b414b4e 99EFIAPI\r
da1d0201 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
2a86ff1c 107 if (NetMask == gIp4AllMasks[Index]) {\r
da1d0201 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
7b414b4e 127EFIAPI\r
da1d0201 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
7b414b4e 167EFIAPI\r
da1d0201 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
2a86ff1c 182 NetMask = gIp4AllMasks[Class << 3];\r
da1d0201 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
7b414b4e 202EFIAPI\r
da1d0201 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
36ee91ca 211 Seed = (~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second);\r
da1d0201 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
7b414b4e 229EFIAPI\r
da1d0201 230NetGetUint32 (\r
231 IN UINT8 *Buf\r
232 )\r
233{\r
234 UINT32 Value;\r
235\r
e48e37fc 236 CopyMem (&Value, Buf, sizeof (UINT32));\r
da1d0201 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
7b414b4e 252EFIAPI\r
da1d0201 253NetPutUint32 (\r
254 IN UINT8 *Buf,\r
255 IN UINT32 Data\r
256 )\r
257{\r
258 Data = HTONL (Data);\r
e48e37fc 259 CopyMem (Buf, &Data, sizeof (UINT32));\r
da1d0201 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
e48e37fc 271LIST_ENTRY *\r
7b414b4e 272EFIAPI\r
da1d0201 273NetListRemoveHead (\r
e48e37fc 274 LIST_ENTRY *Head\r
da1d0201 275 )\r
276{\r
e48e37fc 277 LIST_ENTRY *First;\r
da1d0201 278\r
279 ASSERT (Head != NULL);\r
280\r
e48e37fc 281 if (IsListEmpty (Head)) {\r
da1d0201 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
e48e37fc 290 First->ForwardLink = (LIST_ENTRY *) NULL;\r
291 First->BackLink = (LIST_ENTRY *) NULL;\r
da1d0201 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
e48e37fc 306LIST_ENTRY *\r
7b414b4e 307EFIAPI\r
da1d0201 308NetListRemoveTail (\r
e48e37fc 309 LIST_ENTRY *Head\r
da1d0201 310 )\r
311{\r
e48e37fc 312 LIST_ENTRY *Last;\r
da1d0201 313\r
314 ASSERT (Head != NULL);\r
315\r
e48e37fc 316 if (IsListEmpty (Head)) {\r
da1d0201 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
e48e37fc 325 Last->ForwardLink = (LIST_ENTRY *) NULL;\r
326 Last->BackLink = (LIST_ENTRY *) NULL;\r
da1d0201 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
7b414b4e 343EFIAPI\r
da1d0201 344NetListInsertAfter (\r
e48e37fc 345 IN LIST_ENTRY *PrevEntry,\r
346 IN LIST_ENTRY *NewEntry\r
da1d0201 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
7b414b4e 366EFIAPI\r
da1d0201 367NetListInsertBefore (\r
e48e37fc 368 IN LIST_ENTRY *PostEntry,\r
369 IN LIST_ENTRY *NewEntry\r
da1d0201 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
7b414b4e 388EFIAPI\r
da1d0201 389NetMapInit (\r
390 IN NET_MAP *Map\r
391 )\r
392{\r
393 ASSERT (Map != NULL);\r
394\r
e48e37fc 395 InitializeListHead (&Map->Used);\r
396 InitializeListHead (&Map->Recycled);\r
da1d0201 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
7b414b4e 410EFIAPI\r
da1d0201 411NetMapClean (\r
412 IN NET_MAP *Map\r
413 )\r
414{\r
415 NET_MAP_ITEM *Item;\r
e48e37fc 416 LIST_ENTRY *Entry;\r
417 LIST_ENTRY *Next;\r
da1d0201 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
e48e37fc 424 RemoveEntryList (&Item->Link);\r
da1d0201 425 Map->Count--;\r
426\r
e48e37fc 427 gBS->FreePool (Item);\r
da1d0201 428 }\r
429\r
e48e37fc 430 ASSERT ((Map->Count == 0) && IsListEmpty (&Map->Used));\r
da1d0201 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
e48e37fc 435 RemoveEntryList (&Item->Link);\r
436 gBS->FreePool (Item);\r
da1d0201 437 }\r
438\r
e48e37fc 439 ASSERT (IsListEmpty (&Map->Recycled));\r
da1d0201 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
7b414b4e 452EFIAPI\r
da1d0201 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
7b414b4e 471EFIAPI\r
da1d0201 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
da1d0201 489NET_MAP_ITEM *\r
490NetMapAllocItem (\r
491 IN NET_MAP *Map\r
492 )\r
493{\r
494 NET_MAP_ITEM *Item;\r
e48e37fc 495 LIST_ENTRY *Head;\r
da1d0201 496 UINTN Index;\r
497\r
498 ASSERT (Map != NULL);\r
499\r
500 Head = &Map->Recycled;\r
501\r
e48e37fc 502 if (IsListEmpty (Head)) {\r
da1d0201 503 for (Index = 0; Index < NET_MAP_INCREAMENT; Index++) {\r
e48e37fc 504 Item = AllocatePool (sizeof (NET_MAP_ITEM));\r
da1d0201 505\r
506 if (Item == NULL) {\r
507 if (Index == 0) {\r
508 return NULL;\r
509 }\r
510\r
511 break;\r
512 }\r
513\r
e48e37fc 514 InsertHeadList (Head, &Item->Link);\r
da1d0201 515 }\r
516 }\r
517\r
518 Item = NET_LIST_HEAD (Head, NET_MAP_ITEM, Link);\r
519 NetListRemoveHead (Head);\r
520\r
521 return Item;\r
522}\r
523\r
524\r
525/**\r
526 Allocate an item to save the <Key, Value> pair to the head of the netmap.\r
527\r
528 @param Map The netmap to insert into\r
529 @param Key The user's key\r
530 @param Value The user's value for the key\r
531\r
532 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item\r
533 @retval EFI_SUCCESS The item is inserted to the head\r
534\r
535**/\r
536EFI_STATUS\r
7b414b4e 537EFIAPI\r
da1d0201 538NetMapInsertHead (\r
539 IN NET_MAP *Map,\r
540 IN VOID *Key,\r
541 IN VOID *Value OPTIONAL\r
542 )\r
543{\r
544 NET_MAP_ITEM *Item;\r
545\r
546 ASSERT (Map != NULL);\r
547\r
548 Item = NetMapAllocItem (Map);\r
549\r
550 if (Item == NULL) {\r
551 return EFI_OUT_OF_RESOURCES;\r
552 }\r
553\r
554 Item->Key = Key;\r
555 Item->Value = Value;\r
e48e37fc 556 InsertHeadList (&Map->Used, &Item->Link);\r
da1d0201 557\r
558 Map->Count++;\r
559 return EFI_SUCCESS;\r
560}\r
561\r
562\r
563/**\r
564 Allocate an item to save the <Key, Value> pair to the tail of the netmap.\r
565\r
566 @param Map The netmap to insert into\r
567 @param Key The user's key\r
568 @param Value The user's value for the key\r
569\r
570 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item\r
571 @retval EFI_SUCCESS The item is inserted to the tail\r
572\r
573**/\r
574EFI_STATUS\r
7b414b4e 575EFIAPI\r
da1d0201 576NetMapInsertTail (\r
577 IN NET_MAP *Map,\r
578 IN VOID *Key,\r
579 IN VOID *Value OPTIONAL\r
580 )\r
581{\r
582 NET_MAP_ITEM *Item;\r
583\r
584 ASSERT (Map != NULL);\r
585\r
586 Item = NetMapAllocItem (Map);\r
587\r
588 if (Item == NULL) {\r
589 return EFI_OUT_OF_RESOURCES;\r
590 }\r
591\r
592 Item->Key = Key;\r
593 Item->Value = Value;\r
e48e37fc 594 InsertTailList (&Map->Used, &Item->Link);\r
da1d0201 595\r
596 Map->Count++;\r
597\r
598 return EFI_SUCCESS;\r
599}\r
600\r
601\r
602/**\r
603 Check whther the item is in the Map\r
604\r
605 @param Map The netmap to search within\r
606 @param Item The item to search\r
607\r
608 @return TRUE if the item is in the netmap, otherwise FALSE.\r
609\r
610**/\r
da1d0201 611BOOLEAN\r
612NetItemInMap (\r
613 IN NET_MAP *Map,\r
614 IN NET_MAP_ITEM *Item\r
615 )\r
616{\r
e48e37fc 617 LIST_ENTRY *ListEntry;\r
da1d0201 618\r
619 NET_LIST_FOR_EACH (ListEntry, &Map->Used) {\r
620 if (ListEntry == &Item->Link) {\r
621 return TRUE;\r
622 }\r
623 }\r
624\r
625 return FALSE;\r
626}\r
627\r
628\r
629/**\r
630 Find the key in the netmap\r
631\r
632 @param Map The netmap to search within\r
633 @param Key The key to search\r
634\r
635 @return The point to the item contains the Key, or NULL if Key isn't in the map.\r
636\r
637**/\r
638NET_MAP_ITEM *\r
7b414b4e 639EFIAPI\r
da1d0201 640NetMapFindKey (\r
641 IN NET_MAP *Map,\r
642 IN VOID *Key\r
643 )\r
644{\r
e48e37fc 645 LIST_ENTRY *Entry;\r
da1d0201 646 NET_MAP_ITEM *Item;\r
647\r
648 ASSERT (Map != NULL);\r
649\r
650 NET_LIST_FOR_EACH (Entry, &Map->Used) {\r
651 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
652\r
653 if (Item->Key == Key) {\r
654 return Item;\r
655 }\r
656 }\r
657\r
658 return NULL;\r
659}\r
660\r
661\r
662/**\r
663 Remove the item from the netmap\r
664\r
665 @param Map The netmap to remove the item from\r
666 @param Item The item to remove\r
667 @param Value The variable to receive the value if not NULL\r
668\r
669 @return The key of the removed item.\r
670\r
671**/\r
672VOID *\r
7b414b4e 673EFIAPI\r
da1d0201 674NetMapRemoveItem (\r
675 IN NET_MAP *Map,\r
676 IN NET_MAP_ITEM *Item,\r
677 OUT VOID **Value OPTIONAL\r
678 )\r
679{\r
680 ASSERT ((Map != NULL) && (Item != NULL));\r
681 ASSERT (NetItemInMap (Map, Item));\r
682\r
e48e37fc 683 RemoveEntryList (&Item->Link);\r
da1d0201 684 Map->Count--;\r
e48e37fc 685 InsertHeadList (&Map->Recycled, &Item->Link);\r
da1d0201 686\r
687 if (Value != NULL) {\r
688 *Value = Item->Value;\r
689 }\r
690\r
691 return Item->Key;\r
692}\r
693\r
694\r
695/**\r
696 Remove the first entry on the netmap\r
697\r
698 @param Map The netmap to remove the head from\r
699 @param Value The variable to receive the value if not NULL\r
700\r
701 @return The key of the item removed\r
702\r
703**/\r
704VOID *\r
7b414b4e 705EFIAPI\r
da1d0201 706NetMapRemoveHead (\r
707 IN NET_MAP *Map,\r
708 OUT VOID **Value OPTIONAL\r
709 )\r
710{\r
711 NET_MAP_ITEM *Item;\r
712\r
713 //\r
714 // Often, it indicates a programming error to remove\r
715 // the first entry in an empty list\r
716 //\r
e48e37fc 717 ASSERT (Map && !IsListEmpty (&Map->Used));\r
da1d0201 718\r
719 Item = NET_LIST_HEAD (&Map->Used, NET_MAP_ITEM, Link);\r
e48e37fc 720 RemoveEntryList (&Item->Link);\r
da1d0201 721 Map->Count--;\r
e48e37fc 722 InsertHeadList (&Map->Recycled, &Item->Link);\r
da1d0201 723\r
724 if (Value != NULL) {\r
725 *Value = Item->Value;\r
726 }\r
727\r
728 return Item->Key;\r
729}\r
730\r
731\r
732/**\r
733 Remove the last entry on the netmap\r
734\r
735 @param Map The netmap to remove the tail from\r
736 @param Value The variable to receive the value if not NULL\r
737\r
738 @return The key of the item removed\r
739\r
740**/\r
741VOID *\r
7b414b4e 742EFIAPI\r
da1d0201 743NetMapRemoveTail (\r
744 IN NET_MAP *Map,\r
745 OUT VOID **Value OPTIONAL\r
746 )\r
747{\r
748 NET_MAP_ITEM *Item;\r
749\r
750 //\r
751 // Often, it indicates a programming error to remove\r
752 // the last entry in an empty list\r
753 //\r
e48e37fc 754 ASSERT (Map && !IsListEmpty (&Map->Used));\r
da1d0201 755\r
756 Item = NET_LIST_TAIL (&Map->Used, NET_MAP_ITEM, Link);\r
e48e37fc 757 RemoveEntryList (&Item->Link);\r
da1d0201 758 Map->Count--;\r
e48e37fc 759 InsertHeadList (&Map->Recycled, &Item->Link);\r
da1d0201 760\r
761 if (Value != NULL) {\r
762 *Value = Item->Value;\r
763 }\r
764\r
765 return Item->Key;\r
766}\r
767\r
768\r
769/**\r
770 Iterate through the netmap and call CallBack for each item. It will\r
771 contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break\r
772 from the loop. It returns the CallBack's last return value. This\r
773 function is delete safe for the current item.\r
774\r
775 @param Map The Map to iterate through\r
776 @param CallBack The callback function to call for each item.\r
777 @param Arg The opaque parameter to the callback\r
778\r
779 @return It returns the CallBack's last return value.\r
780\r
781**/\r
782EFI_STATUS\r
7b414b4e 783EFIAPI\r
da1d0201 784NetMapIterate (\r
785 IN NET_MAP *Map,\r
786 IN NET_MAP_CALLBACK CallBack,\r
787 IN VOID *Arg\r
788 )\r
789{\r
790\r
e48e37fc 791 LIST_ENTRY *Entry;\r
792 LIST_ENTRY *Next;\r
793 LIST_ENTRY *Head;\r
da1d0201 794 NET_MAP_ITEM *Item;\r
795 EFI_STATUS Result;\r
796\r
797 ASSERT ((Map != NULL) && (CallBack != NULL));\r
798\r
799 Head = &Map->Used;\r
800\r
e48e37fc 801 if (IsListEmpty (Head)) {\r
da1d0201 802 return EFI_SUCCESS;\r
803 }\r
804\r
805 NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {\r
806 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
807 Result = CallBack (Map, Item, Arg);\r
808\r
809 if (EFI_ERROR (Result)) {\r
810 return Result;\r
811 }\r
812 }\r
813\r
814 return EFI_SUCCESS;\r
815}\r
816\r
817\r
818/**\r
819 This is the default unload handle for all the network drivers.\r
820\r
821 @param ImageHandle The drivers' driver image.\r
822\r
823 @retval EFI_SUCCESS The image is unloaded.\r
824 @retval Others Failed to unload the image.\r
825\r
826**/\r
827EFI_STATUS\r
828EFIAPI\r
829NetLibDefaultUnload (\r
830 IN EFI_HANDLE ImageHandle\r
831 )\r
832{\r
833 EFI_STATUS Status;\r
834 EFI_HANDLE *DeviceHandleBuffer;\r
835 UINTN DeviceHandleCount;\r
836 UINTN Index;\r
837 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;\r
838 EFI_COMPONENT_NAME_PROTOCOL *ComponentName;\r
3012ce5c 839 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r
da1d0201 840\r
841 //\r
842 // Get the list of all the handles in the handle database.\r
843 // If there is an error getting the list, then the unload\r
844 // operation fails.\r
845 //\r
846 Status = gBS->LocateHandleBuffer (\r
847 AllHandles,\r
848 NULL,\r
849 NULL,\r
850 &DeviceHandleCount,\r
851 &DeviceHandleBuffer\r
852 );\r
853\r
854 if (EFI_ERROR (Status)) {\r
855 return Status;\r
856 }\r
857\r
858 //\r
859 // Disconnect the driver specified by ImageHandle from all\r
860 // the devices in the handle database.\r
861 //\r
862 for (Index = 0; Index < DeviceHandleCount; Index++) {\r
863 Status = gBS->DisconnectController (\r
864 DeviceHandleBuffer[Index],\r
865 ImageHandle,\r
866 NULL\r
867 );\r
868 }\r
869\r
870 //\r
871 // Uninstall all the protocols installed in the driver entry point\r
872 //\r
873 for (Index = 0; Index < DeviceHandleCount; Index++) {\r
874 Status = gBS->HandleProtocol (\r
875 DeviceHandleBuffer[Index],\r
876 &gEfiDriverBindingProtocolGuid,\r
877 (VOID **) &DriverBinding\r
878 );\r
879\r
880 if (EFI_ERROR (Status)) {\r
881 continue;\r
882 }\r
883\r
884 if (DriverBinding->ImageHandle != ImageHandle) {\r
885 continue;\r
886 }\r
887\r
888 gBS->UninstallProtocolInterface (\r
889 ImageHandle,\r
890 &gEfiDriverBindingProtocolGuid,\r
891 DriverBinding\r
892 );\r
893 Status = gBS->HandleProtocol (\r
894 DeviceHandleBuffer[Index],\r
895 &gEfiComponentNameProtocolGuid,\r
896 (VOID **) &ComponentName\r
897 );\r
898 if (!EFI_ERROR (Status)) {\r
899 gBS->UninstallProtocolInterface (\r
900 ImageHandle,\r
901 &gEfiComponentNameProtocolGuid,\r
902 ComponentName\r
903 );\r
904 }\r
905\r
906 Status = gBS->HandleProtocol (\r
907 DeviceHandleBuffer[Index],\r
3012ce5c 908 &gEfiComponentName2ProtocolGuid,\r
909 (VOID **) &ComponentName2\r
da1d0201 910 );\r
da1d0201 911 if (!EFI_ERROR (Status)) {\r
912 gBS->UninstallProtocolInterface (\r
3012ce5c 913 ImageHandle,\r
914 &gEfiComponentName2ProtocolGuid,\r
915 ComponentName2\r
916 );\r
da1d0201 917 }\r
918 }\r
919\r
920 //\r
921 // Free the buffer containing the list of handles from the handle database\r
922 //\r
923 if (DeviceHandleBuffer != NULL) {\r
924 gBS->FreePool (DeviceHandleBuffer);\r
925 }\r
926\r
927 return EFI_SUCCESS;\r
928}\r
929\r
930\r
931\r
932/**\r
933 Create a child of the service that is identified by ServiceBindingGuid.\r
934\r
935 @param Controller The controller which has the service installed.\r
936 @param Image The image handle used to open service.\r
937 @param ServiceBindingGuid The service's Guid.\r
938 @param ChildHandle The handle to receive the create child\r
939\r
940 @retval EFI_SUCCESS The child is successfully created.\r
941 @retval Others Failed to create the child.\r
942\r
943**/\r
944EFI_STATUS\r
7b414b4e 945EFIAPI\r
da1d0201 946NetLibCreateServiceChild (\r
947 IN EFI_HANDLE Controller,\r
948 IN EFI_HANDLE Image,\r
949 IN EFI_GUID *ServiceBindingGuid,\r
950 OUT EFI_HANDLE *ChildHandle\r
951 )\r
952{\r
953 EFI_STATUS Status;\r
954 EFI_SERVICE_BINDING_PROTOCOL *Service;\r
955\r
956\r
957 ASSERT ((ServiceBindingGuid != NULL) && (ChildHandle != NULL));\r
958\r
959 //\r
960 // Get the ServiceBinding Protocol\r
961 //\r
962 Status = gBS->OpenProtocol (\r
963 Controller,\r
964 ServiceBindingGuid,\r
965 (VOID **) &Service,\r
966 Image,\r
967 Controller,\r
968 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
969 );\r
970\r
971 if (EFI_ERROR (Status)) {\r
972 return Status;\r
973 }\r
974\r
975 //\r
976 // Create a child\r
977 //\r
978 Status = Service->CreateChild (Service, ChildHandle);\r
979 return Status;\r
980}\r
981\r
982\r
983/**\r
984 Destory a child of the service that is identified by ServiceBindingGuid.\r
985\r
986 @param Controller The controller which has the service installed.\r
987 @param Image The image handle used to open service.\r
988 @param ServiceBindingGuid The service's Guid.\r
989 @param ChildHandle The child to destory\r
990\r
991 @retval EFI_SUCCESS The child is successfully destoried.\r
992 @retval Others Failed to destory the child.\r
993\r
994**/\r
995EFI_STATUS\r
7b414b4e 996EFIAPI\r
da1d0201 997NetLibDestroyServiceChild (\r
998 IN EFI_HANDLE Controller,\r
999 IN EFI_HANDLE Image,\r
1000 IN EFI_GUID *ServiceBindingGuid,\r
1001 IN EFI_HANDLE ChildHandle\r
1002 )\r
1003{\r
1004 EFI_STATUS Status;\r
1005 EFI_SERVICE_BINDING_PROTOCOL *Service;\r
1006\r
1007 ASSERT (ServiceBindingGuid != NULL);\r
1008\r
1009 //\r
1010 // Get the ServiceBinding Protocol\r
1011 //\r
1012 Status = gBS->OpenProtocol (\r
1013 Controller,\r
1014 ServiceBindingGuid,\r
1015 (VOID **) &Service,\r
1016 Image,\r
1017 Controller,\r
1018 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1019 );\r
1020\r
1021 if (EFI_ERROR (Status)) {\r
1022 return Status;\r
1023 }\r
1024\r
1025 //\r
1026 // destory the child\r
1027 //\r
1028 Status = Service->DestroyChild (Service, ChildHandle);\r
1029 return Status;\r
1030}\r
1031\r
1032\r
1033/**\r
1034 Convert the mac address of the simple network protocol installed on\r
1035 SnpHandle to a unicode string. Callers are responsible for freeing the\r
1036 string storage.\r
1037\r
1038 @param SnpHandle The handle where the simple network protocol is\r
1039 installed on.\r
1040 @param ImageHandle The image handle used to act as the agent handle to\r
1041 get the simple network protocol.\r
1042 @param MacString The pointer to store the address of the string\r
1043 representation of the mac address.\r
1044\r
1045 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.\r
1046 @retval other Failed to open the simple network protocol.\r
1047\r
1048**/\r
1049EFI_STATUS\r
7b414b4e 1050EFIAPI\r
da1d0201 1051NetLibGetMacString (\r
1052 IN EFI_HANDLE SnpHandle,\r
1053 IN EFI_HANDLE ImageHandle,\r
1054 IN OUT CHAR16 **MacString\r
1055 )\r
1056{\r
1057 EFI_STATUS Status;\r
1058 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
1059 EFI_SIMPLE_NETWORK_MODE *Mode;\r
1060 CHAR16 *MacAddress;\r
1061 UINTN Index;\r
1062\r
1063 *MacString = NULL;\r
1064\r
1065 //\r
1066 // Get the Simple Network protocol from the SnpHandle.\r
1067 //\r
1068 Status = gBS->OpenProtocol (\r
1069 SnpHandle,\r
1070 &gEfiSimpleNetworkProtocolGuid,\r
1071 (VOID **) &Snp,\r
1072 ImageHandle,\r
1073 SnpHandle,\r
1074 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1075 );\r
1076 if (EFI_ERROR (Status)) {\r
1077 return Status;\r
1078 }\r
1079\r
1080 Mode = Snp->Mode;\r
1081\r
1082 //\r
1083 // It takes 2 unicode characters to represent a 1 byte binary buffer.\r
1084 // Plus one unicode character for the null-terminator.\r
1085 //\r
e48e37fc 1086 MacAddress = AllocatePool ((2 * Mode->HwAddressSize + 1) * sizeof (CHAR16));\r
da1d0201 1087 if (MacAddress == NULL) {\r
1088 return EFI_OUT_OF_RESOURCES;\r
1089 }\r
1090\r
1091 //\r
1092 // Convert the mac address into a unicode string.\r
1093 //\r
1094 for (Index = 0; Index < Mode->HwAddressSize; Index++) {\r
1095 MacAddress[Index * 2] = NibbleToHexChar ((UINT8) (Mode->CurrentAddress.Addr[Index] >> 4));\r
1096 MacAddress[Index * 2 + 1] = NibbleToHexChar (Mode->CurrentAddress.Addr[Index]);\r
1097 }\r
1098\r
1099 MacAddress[Mode->HwAddressSize * 2] = L'\0';\r
1100\r
1101 *MacString = MacAddress;\r
1102\r
1103 return EFI_SUCCESS;\r
1104}\r
1105\r
1106/**\r
1107 Check the default address used by the IPv4 driver is static or dynamic (acquired\r
1108 from DHCP).\r
1109\r
1110 @param Controller The controller handle which has the NIC Ip4 Config Protocol\r
1111 relative with the default address to judge.\r
1112\r
1113 @retval TRUE If the default address is static.\r
1114 @retval FALSE If the default address is acquired from DHCP.\r
1115\r
1116**/\r
da1d0201 1117BOOLEAN\r
1118NetLibDefaultAddressIsStatic (\r
1119 IN EFI_HANDLE Controller\r
1120 )\r
1121{\r
1122 EFI_STATUS Status;\r
1123 EFI_NIC_IP4_CONFIG_PROTOCOL *NicIp4;\r
1124 UINTN Len;\r
1125 NIC_IP4_CONFIG_INFO *ConfigInfo;\r
1126 BOOLEAN IsStatic;\r
1127\r
1128 Status = gBS->HandleProtocol (\r
1129 Controller,\r
1130 &gEfiNicIp4ConfigProtocolGuid,\r
1131 (VOID **) &NicIp4\r
1132 );\r
1133 if (EFI_ERROR (Status)) {\r
1134 return TRUE;\r
1135 }\r
1136\r
1137 Len = 0;\r
1138 Status = NicIp4->GetInfo (NicIp4, &Len, NULL);\r
1139 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1140 return TRUE;\r
1141 }\r
1142\r
e48e37fc 1143 ConfigInfo = AllocatePool (Len);\r
da1d0201 1144 if (ConfigInfo == NULL) {\r
1145 return TRUE;\r
1146 }\r
1147\r
1148 IsStatic = TRUE;\r
1149 Status = NicIp4->GetInfo (NicIp4, &Len, ConfigInfo);\r
1150 if (EFI_ERROR (Status)) {\r
1151 goto ON_EXIT;\r
1152 }\r
1153\r
1154 IsStatic = (BOOLEAN) (ConfigInfo->Source == IP4_CONFIG_SOURCE_STATIC);\r
1155\r
1156ON_EXIT:\r
1157\r
e48e37fc 1158 gBS->FreePool (ConfigInfo);\r
da1d0201 1159\r
1160 return IsStatic;\r
1161}\r
1162\r
1163/**\r
1164 Create an IPv4 device path node.\r
1165\r
1166 @param Node Pointer to the IPv4 device path node.\r
1167 @param Controller The handle where the NIC IP4 config protocol resides.\r
1168 @param LocalIp The local IPv4 address.\r
1169 @param LocalPort The local port.\r
1170 @param RemoteIp The remote IPv4 address.\r
1171 @param RemotePort The remote port.\r
1172 @param Protocol The protocol type in the IP header.\r
1173 @param UseDefaultAddress Whether this instance is using default address or not.\r
1174\r
1175 @retval None\r
1176**/\r
1177VOID\r
7b414b4e 1178EFIAPI\r
da1d0201 1179NetLibCreateIPv4DPathNode (\r
1180 IN OUT IPv4_DEVICE_PATH *Node,\r
1181 IN EFI_HANDLE Controller,\r
1182 IN IP4_ADDR LocalIp,\r
1183 IN UINT16 LocalPort,\r
1184 IN IP4_ADDR RemoteIp,\r
1185 IN UINT16 RemotePort,\r
1186 IN UINT16 Protocol,\r
1187 IN BOOLEAN UseDefaultAddress\r
1188 )\r
1189{\r
1190 Node->Header.Type = MESSAGING_DEVICE_PATH;\r
1191 Node->Header.SubType = MSG_IPv4_DP;\r
1192 SetDevicePathNodeLength (&Node->Header, 19);\r
1193\r
e48e37fc 1194 CopyMem (&Node->LocalIpAddress, &LocalIp, sizeof (EFI_IPv4_ADDRESS));\r
1195 CopyMem (&Node->RemoteIpAddress, &RemoteIp, sizeof (EFI_IPv4_ADDRESS));\r
da1d0201 1196\r
1197 Node->LocalPort = LocalPort;\r
1198 Node->RemotePort = RemotePort;\r
1199\r
1200 Node->Protocol = Protocol;\r
1201\r
1202 if (!UseDefaultAddress) {\r
1203 Node->StaticIpAddress = TRUE;\r
1204 } else {\r
1205 Node->StaticIpAddress = NetLibDefaultAddressIsStatic (Controller);\r
1206 }\r
1207}\r
1208\r
1209\r
1210/**\r
1211 Find the UNDI/SNP handle from controller and protocol GUID.\r
1212 For example, IP will open a MNP child to transmit/receive\r
1213 packets, when MNP is stopped, IP should also be stopped. IP\r
1214 needs to find its own private data which is related the IP's\r
1215 service binding instance that is install on UNDI/SNP handle.\r
1216 Now, the controller is either a MNP or ARP child handle. But\r
1217 IP opens these handle BY_DRIVER, use that info, we can get the\r
1218 UNDI/SNP handle.\r
1219\r
1220 @param Controller Then protocol handle to check\r
1221 @param ProtocolGuid The protocol that is related with the handle.\r
1222\r
1223 @return The UNDI/SNP handle or NULL.\r
1224\r
1225**/\r
1226EFI_HANDLE\r
7b414b4e 1227EFIAPI\r
da1d0201 1228NetLibGetNicHandle (\r
1229 IN EFI_HANDLE Controller,\r
1230 IN EFI_GUID *ProtocolGuid\r
1231 )\r
1232{\r
1233 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer;\r
1234 EFI_HANDLE Handle;\r
1235 EFI_STATUS Status;\r
1236 UINTN OpenCount;\r
1237 UINTN Index;\r
1238\r
1239 Status = gBS->OpenProtocolInformation (\r
1240 Controller,\r
1241 ProtocolGuid,\r
1242 &OpenBuffer,\r
1243 &OpenCount\r
1244 );\r
1245\r
1246 if (EFI_ERROR (Status)) {\r
1247 return NULL;\r
1248 }\r
1249\r
1250 Handle = NULL;\r
1251\r
1252 for (Index = 0; Index < OpenCount; Index++) {\r
1253 if (OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {\r
1254 Handle = OpenBuffer[Index].ControllerHandle;\r
1255 break;\r
1256 }\r
1257 }\r
1258\r
1259 gBS->FreePool (OpenBuffer);\r
1260 return Handle;\r
1261}\r
1262\r
36ee91ca 1263/**\r
1264 Add a Deferred Procedure Call to the end of the DPC queue.\r
1265\r
1266 @DpcTpl The EFI_TPL that the DPC should be invoked.\r
1267 @DpcProcedure Pointer to the DPC's function.\r
1268 @DpcContext Pointer to the DPC's context. Passed to DpcProcedure\r
1269 when DpcProcedure is invoked.\r
1270\r
1271 @retval EFI_SUCCESS The DPC was queued.\r
1272 @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL.\r
1273 DpcProcedure is NULL.\r
1274 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r
1275 add the DPC to the queue.\r
1276\r
1277**/\r
1278EFI_STATUS\r
7b414b4e 1279EFIAPI\r
36ee91ca 1280NetLibQueueDpc (\r
1281 IN EFI_TPL DpcTpl,\r
1282 IN EFI_DPC_PROCEDURE DpcProcedure,\r
1283 IN VOID *DpcContext OPTIONAL\r
1284 )\r
1285{\r
1286 return mDpc->QueueDpc (mDpc, DpcTpl, DpcProcedure, DpcContext);\r
1287}\r
1288\r
1289/**\r
1290 Add a Deferred Procedure Call to the end of the DPC queue.\r
1291\r
1292 @retval EFI_SUCCESS One or more DPCs were invoked.\r
1293 @retval EFI_NOT_FOUND No DPCs were invoked.\r
1294\r
1295**/\r
1296EFI_STATUS\r
7b414b4e 1297EFIAPI\r
36ee91ca 1298NetLibDispatchDpc (\r
1299 VOID\r
1300 )\r
1301{\r
1302 return mDpc->DispatchDpc(mDpc);\r
1303}\r
1304\r
1305\r
1306/**\r
1307 The constructor function caches the pointer to DPC protocol.\r
1308\r
1309 The constructor function locates DPC protocol from protocol database.\r
1310 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.\r
1311\r
1312 @param ImageHandle The firmware allocated handle for the EFI image.\r
1313 @param SystemTable A pointer to the EFI System Table.\r
1314\r
1315 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
1316\r
1317**/\r
1318EFI_STATUS\r
1319EFIAPI\r
1320NetLibConstructor (\r
1321 IN EFI_HANDLE ImageHandle,\r
1322 IN EFI_SYSTEM_TABLE *SystemTable\r
1323 )\r
1324{\r
1325 EFI_STATUS Status;\r
1326\r
1327 Status = gBS->LocateProtocol (&gEfiDpcProtocolGuid, NULL, (VOID**) &mDpc);\r
1328 ASSERT_EFI_ERROR (Status);\r
1329 ASSERT (mDpc != NULL);\r
1330\r
1331 return Status;\r
1332}\r