]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
1. retired NicIp4ConfigProtocolGuid
[mirror_edk2.git] / MdeModulePkg / Library / DxeNetLib / DxeNetLib.c
1 /** @file
2 Network library.
3
4 Copyright (c) 2005 - 2007, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 **/
13
14 #include <Uefi.h>
15
16 #include <Protocol/ServiceBinding.h>
17 #include <Protocol/SimpleNetwork.h>
18 #include <Protocol/HiiConfigRouting.h>
19 #include <Protocol/ComponentName.h>
20 #include <Protocol/ComponentName2.h>
21 #include <Protocol/Dpc.h>
22
23 #include <Guid/NicIp4ConfigNvData.h>
24
25 #include <Library/NetLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/BaseMemoryLib.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/UefiRuntimeServicesTableLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/DevicePathLib.h>
33 #include <Library/HiiLib.h>
34 #include <Library/PrintLib.h>
35
36 EFI_DPC_PROTOCOL *mDpc = NULL;
37
38 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mNetLibHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
39
40 #define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * 2
41
42 //
43 // All the supported IP4 maskes in host byte order.
44 //
45 IP4_ADDR gIp4AllMasks[IP4_MASK_NUM] = {
46 0x00000000,
47 0x80000000,
48 0xC0000000,
49 0xE0000000,
50 0xF0000000,
51 0xF8000000,
52 0xFC000000,
53 0xFE000000,
54
55 0xFF000000,
56 0xFF800000,
57 0xFFC00000,
58 0xFFE00000,
59 0xFFF00000,
60 0xFFF80000,
61 0xFFFC0000,
62 0xFFFE0000,
63
64 0xFFFF0000,
65 0xFFFF8000,
66 0xFFFFC000,
67 0xFFFFE000,
68 0xFFFFF000,
69 0xFFFFF800,
70 0xFFFFFC00,
71 0xFFFFFE00,
72
73 0xFFFFFF00,
74 0xFFFFFF80,
75 0xFFFFFFC0,
76 0xFFFFFFE0,
77 0xFFFFFFF0,
78 0xFFFFFFF8,
79 0xFFFFFFFC,
80 0xFFFFFFFE,
81 0xFFFFFFFF,
82 };
83
84 EFI_IPv4_ADDRESS mZeroIp4Addr = {{0, 0, 0, 0}};
85
86 /**
87 Return the length of the mask.
88
89 Return the length of the mask, the correct value is from 0 to 32.
90 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.
91 NetMask is in the host byte order.
92
93 @param[in] NetMask The netmask to get the length from.
94
95 @return The length of the netmask, IP4_MASK_NUM if the mask is invalid.
96
97 **/
98 INTN
99 EFIAPI
100 NetGetMaskLength (
101 IN IP4_ADDR NetMask
102 )
103 {
104 INTN Index;
105
106 for (Index = 0; Index < IP4_MASK_NUM; Index++) {
107 if (NetMask == gIp4AllMasks[Index]) {
108 break;
109 }
110 }
111
112 return Index;
113 }
114
115
116
117 /**
118 Return the class of the IP address, such as class A, B, C.
119 Addr is in host byte order.
120
121 The address of class A starts with 0.
122 If the address belong to class A, return IP4_ADDR_CLASSA.
123 The address of class B starts with 10.
124 If the address belong to class B, return IP4_ADDR_CLASSB.
125 The address of class C starts with 110.
126 If the address belong to class C, return IP4_ADDR_CLASSC.
127 The address of class D starts with 1110.
128 If the address belong to class D, return IP4_ADDR_CLASSD.
129 The address of class E starts with 1111.
130 If the address belong to class E, return IP4_ADDR_CLASSE.
131
132
133 @param[in] Addr The address to get the class from.
134
135 @return IP address class, such as IP4_ADDR_CLASSA.
136
137 **/
138 INTN
139 EFIAPI
140 NetGetIpClass (
141 IN IP4_ADDR Addr
142 )
143 {
144 UINT8 ByteOne;
145
146 ByteOne = (UINT8) (Addr >> 24);
147
148 if ((ByteOne & 0x80) == 0) {
149 return IP4_ADDR_CLASSA;
150
151 } else if ((ByteOne & 0xC0) == 0x80) {
152 return IP4_ADDR_CLASSB;
153
154 } else if ((ByteOne & 0xE0) == 0xC0) {
155 return IP4_ADDR_CLASSC;
156
157 } else if ((ByteOne & 0xF0) == 0xE0) {
158 return IP4_ADDR_CLASSD;
159
160 } else {
161 return IP4_ADDR_CLASSE;
162
163 }
164 }
165
166
167 /**
168 Check whether the IP is a valid unicast address according to
169 the netmask. If NetMask is zero, use the IP address's class to get the default mask.
170
171 If Ip is 0, IP is not a valid unicast address.
172 Class D address is used for multicasting and class E address is reserved for future. If Ip
173 belongs to class D or class E, IP is not a valid unicast address.
174 If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address.
175
176 @param[in] Ip The IP to check against.
177 @param[in] NetMask The mask of the IP.
178
179 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.
180
181 **/
182 BOOLEAN
183 EFIAPI
184 Ip4IsUnicast (
185 IN IP4_ADDR Ip,
186 IN IP4_ADDR NetMask
187 )
188 {
189 INTN Class;
190
191 Class = NetGetIpClass (Ip);
192
193 if ((Ip == 0) || (Class >= IP4_ADDR_CLASSD)) {
194 return FALSE;
195 }
196
197 if (NetMask == 0) {
198 NetMask = gIp4AllMasks[Class << 3];
199 }
200
201 if (((Ip &~NetMask) == ~NetMask) || ((Ip &~NetMask) == 0)) {
202 return FALSE;
203 }
204
205 return TRUE;
206 }
207
208
209 /**
210 Initialize a random seed using current time.
211
212 Get current time first. Then initialize a random seed based on some basic
213 mathematics operation on the hour, day, minute, second, nanosecond and year
214 of the current time.
215
216 @return The random seed initialized with current time.
217
218 **/
219 UINT32
220 EFIAPI
221 NetRandomInitSeed (
222 VOID
223 )
224 {
225 EFI_TIME Time;
226 UINT32 Seed;
227
228 gRT->GetTime (&Time, NULL);
229 Seed = (~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second);
230 Seed ^= Time.Nanosecond;
231 Seed ^= Time.Year << 7;
232
233 return Seed;
234 }
235
236
237 /**
238 Extract a UINT32 from a byte stream.
239
240 Copy a UINT32 from a byte stream, then converts it from Network
241 byte order to host byte order. Use this function to avoid alignment error.
242
243 @param[in] Buf The buffer to extract the UINT32.
244
245 @return The UINT32 extracted.
246
247 **/
248 UINT32
249 EFIAPI
250 NetGetUint32 (
251 IN UINT8 *Buf
252 )
253 {
254 UINT32 Value;
255
256 CopyMem (&Value, Buf, sizeof (UINT32));
257 return NTOHL (Value);
258 }
259
260
261 /**
262 Put a UINT32 to the byte stream in network byte order.
263
264 Converts a UINT32 from host byte order to network byte order. Then copy it to the
265 byte stream.
266
267 @param[in, out] Buf The buffer to put the UINT32.
268 @param[in] Data The data to put.
269
270 **/
271 VOID
272 EFIAPI
273 NetPutUint32 (
274 IN OUT UINT8 *Buf,
275 IN UINT32 Data
276 )
277 {
278 Data = HTONL (Data);
279 CopyMem (Buf, &Data, sizeof (UINT32));
280 }
281
282
283 /**
284 Remove the first node entry on the list, and return the removed node entry.
285
286 Removes the first node Entry from a doubly linked list. It is up to the caller of
287 this function to release the memory used by the first node if that is required. On
288 exit, the removed node is returned.
289
290 If Head is NULL, then ASSERT().
291 If Head was not initialized, then ASSERT().
292 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
293 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
294 then ASSERT().
295
296 @param[in, out] Head The list header.
297
298 @return The first node entry that is removed from the list, NULL if the list is empty.
299
300 **/
301 LIST_ENTRY *
302 EFIAPI
303 NetListRemoveHead (
304 IN OUT LIST_ENTRY *Head
305 )
306 {
307 LIST_ENTRY *First;
308
309 ASSERT (Head != NULL);
310
311 if (IsListEmpty (Head)) {
312 return NULL;
313 }
314
315 First = Head->ForwardLink;
316 Head->ForwardLink = First->ForwardLink;
317 First->ForwardLink->BackLink = Head;
318
319 DEBUG_CODE (
320 First->ForwardLink = (LIST_ENTRY *) NULL;
321 First->BackLink = (LIST_ENTRY *) NULL;
322 );
323
324 return First;
325 }
326
327
328 /**
329 Remove the last node entry on the list and and return the removed node entry.
330
331 Removes the last node entry from a doubly linked list. It is up to the caller of
332 this function to release the memory used by the first node if that is required. On
333 exit, the removed node is returned.
334
335 If Head is NULL, then ASSERT().
336 If Head was not initialized, then ASSERT().
337 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
338 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
339 then ASSERT().
340
341 @param[in, out] Head The list head.
342
343 @return The last node entry that is removed from the list, NULL if the list is empty.
344
345 **/
346 LIST_ENTRY *
347 EFIAPI
348 NetListRemoveTail (
349 IN OUT LIST_ENTRY *Head
350 )
351 {
352 LIST_ENTRY *Last;
353
354 ASSERT (Head != NULL);
355
356 if (IsListEmpty (Head)) {
357 return NULL;
358 }
359
360 Last = Head->BackLink;
361 Head->BackLink = Last->BackLink;
362 Last->BackLink->ForwardLink = Head;
363
364 DEBUG_CODE (
365 Last->ForwardLink = (LIST_ENTRY *) NULL;
366 Last->BackLink = (LIST_ENTRY *) NULL;
367 );
368
369 return Last;
370 }
371
372
373 /**
374 Insert a new node entry after a designated node entry of a doubly linked list.
375
376 Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry
377 of the doubly linked list.
378
379 @param[in, out] PrevEntry The previous entry to insert after.
380 @param[in, out] NewEntry The new entry to insert.
381
382 **/
383 VOID
384 EFIAPI
385 NetListInsertAfter (
386 IN OUT LIST_ENTRY *PrevEntry,
387 IN OUT LIST_ENTRY *NewEntry
388 )
389 {
390 NewEntry->BackLink = PrevEntry;
391 NewEntry->ForwardLink = PrevEntry->ForwardLink;
392 PrevEntry->ForwardLink->BackLink = NewEntry;
393 PrevEntry->ForwardLink = NewEntry;
394 }
395
396
397 /**
398 Insert a new node entry before a designated node entry of a doubly linked list.
399
400 Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry
401 of the doubly linked list.
402
403 @param[in, out] PostEntry The entry to insert before.
404 @param[in, out] NewEntry The new entry to insert.
405
406 **/
407 VOID
408 EFIAPI
409 NetListInsertBefore (
410 IN OUT LIST_ENTRY *PostEntry,
411 IN OUT LIST_ENTRY *NewEntry
412 )
413 {
414 NewEntry->ForwardLink = PostEntry;
415 NewEntry->BackLink = PostEntry->BackLink;
416 PostEntry->BackLink->ForwardLink = NewEntry;
417 PostEntry->BackLink = NewEntry;
418 }
419
420
421 /**
422 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
423
424 Initialize the forward and backward links of two head nodes donated by Map->Used
425 and Map->Recycled of two doubly linked lists.
426 Initializes the count of the <Key, Value> pairs in the netmap to zero.
427
428 If Map is NULL, then ASSERT().
429 If the address of Map->Used is NULL, then ASSERT().
430 If the address of Map->Recycled is NULl, then ASSERT().
431
432 @param[in, out] Map The netmap to initialize.
433
434 **/
435 VOID
436 EFIAPI
437 NetMapInit (
438 IN OUT NET_MAP *Map
439 )
440 {
441 ASSERT (Map != NULL);
442
443 InitializeListHead (&Map->Used);
444 InitializeListHead (&Map->Recycled);
445 Map->Count = 0;
446 }
447
448
449 /**
450 To clean up the netmap, that is, release allocated memories.
451
452 Removes all nodes of the Used doubly linked list and free memory of all related netmap items.
453 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.
454 The number of the <Key, Value> pairs in the netmap is set to be zero.
455
456 If Map is NULL, then ASSERT().
457
458 @param[in, out] Map The netmap to clean up.
459
460 **/
461 VOID
462 EFIAPI
463 NetMapClean (
464 IN OUT NET_MAP *Map
465 )
466 {
467 NET_MAP_ITEM *Item;
468 LIST_ENTRY *Entry;
469 LIST_ENTRY *Next;
470
471 ASSERT (Map != NULL);
472
473 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Used) {
474 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
475
476 RemoveEntryList (&Item->Link);
477 Map->Count--;
478
479 gBS->FreePool (Item);
480 }
481
482 ASSERT ((Map->Count == 0) && IsListEmpty (&Map->Used));
483
484 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Recycled) {
485 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
486
487 RemoveEntryList (&Item->Link);
488 gBS->FreePool (Item);
489 }
490
491 ASSERT (IsListEmpty (&Map->Recycled));
492 }
493
494
495 /**
496 Test whether the netmap is empty and return true if it is.
497
498 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.
499
500 If Map is NULL, then ASSERT().
501
502
503 @param[in] Map The net map to test.
504
505 @return TRUE if the netmap is empty, otherwise FALSE.
506
507 **/
508 BOOLEAN
509 EFIAPI
510 NetMapIsEmpty (
511 IN NET_MAP *Map
512 )
513 {
514 ASSERT (Map != NULL);
515 return (BOOLEAN) (Map->Count == 0);
516 }
517
518
519 /**
520 Return the number of the <Key, Value> pairs in the netmap.
521
522 @param[in] Map The netmap to get the entry number.
523
524 @return The entry number in the netmap.
525
526 **/
527 UINTN
528 EFIAPI
529 NetMapGetCount (
530 IN NET_MAP *Map
531 )
532 {
533 return Map->Count;
534 }
535
536
537 /**
538 Return one allocated item.
539
540 If the Recycled doubly linked list of the netmap is empty, it will try to allocate
541 a batch of items if there are enough resources and add corresponding nodes to the begining
542 of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove
543 the fist node entry of the Recycled doubly linked list and return the corresponding item.
544
545 If Map is NULL, then ASSERT().
546
547 @param[in, out] Map The netmap to allocate item for.
548
549 @return The allocated item. If NULL, the
550 allocation failed due to resource limit.
551
552 **/
553 NET_MAP_ITEM *
554 NetMapAllocItem (
555 IN OUT NET_MAP *Map
556 )
557 {
558 NET_MAP_ITEM *Item;
559 LIST_ENTRY *Head;
560 UINTN Index;
561
562 ASSERT (Map != NULL);
563
564 Head = &Map->Recycled;
565
566 if (IsListEmpty (Head)) {
567 for (Index = 0; Index < NET_MAP_INCREAMENT; Index++) {
568 Item = AllocatePool (sizeof (NET_MAP_ITEM));
569
570 if (Item == NULL) {
571 if (Index == 0) {
572 return NULL;
573 }
574
575 break;
576 }
577
578 InsertHeadList (Head, &Item->Link);
579 }
580 }
581
582 Item = NET_LIST_HEAD (Head, NET_MAP_ITEM, Link);
583 NetListRemoveHead (Head);
584
585 return Item;
586 }
587
588
589 /**
590 Allocate an item to save the <Key, Value> pair to the head of the netmap.
591
592 Allocate an item to save the <Key, Value> pair and add corresponding node entry
593 to the beginning of the Used doubly linked list. The number of the <Key, Value>
594 pairs in the netmap increase by 1.
595
596 If Map is NULL, then ASSERT().
597
598 @param[in, out] Map The netmap to insert into.
599 @param[in] Key The user's key.
600 @param[in] Value The user's value for the key.
601
602 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
603 @retval EFI_SUCCESS The item is inserted to the head.
604
605 **/
606 EFI_STATUS
607 EFIAPI
608 NetMapInsertHead (
609 IN OUT NET_MAP *Map,
610 IN VOID *Key,
611 IN VOID *Value OPTIONAL
612 )
613 {
614 NET_MAP_ITEM *Item;
615
616 ASSERT (Map != NULL);
617
618 Item = NetMapAllocItem (Map);
619
620 if (Item == NULL) {
621 return EFI_OUT_OF_RESOURCES;
622 }
623
624 Item->Key = Key;
625 Item->Value = Value;
626 InsertHeadList (&Map->Used, &Item->Link);
627
628 Map->Count++;
629 return EFI_SUCCESS;
630 }
631
632
633 /**
634 Allocate an item to save the <Key, Value> pair to the tail of the netmap.
635
636 Allocate an item to save the <Key, Value> pair and add corresponding node entry
637 to the tail of the Used doubly linked list. The number of the <Key, Value>
638 pairs in the netmap increase by 1.
639
640 If Map is NULL, then ASSERT().
641
642 @param[in, out] Map The netmap to insert into.
643 @param[in] Key The user's key.
644 @param[in] Value The user's value for the key.
645
646 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
647 @retval EFI_SUCCESS The item is inserted to the tail.
648
649 **/
650 EFI_STATUS
651 EFIAPI
652 NetMapInsertTail (
653 IN OUT NET_MAP *Map,
654 IN VOID *Key,
655 IN VOID *Value OPTIONAL
656 )
657 {
658 NET_MAP_ITEM *Item;
659
660 ASSERT (Map != NULL);
661
662 Item = NetMapAllocItem (Map);
663
664 if (Item == NULL) {
665 return EFI_OUT_OF_RESOURCES;
666 }
667
668 Item->Key = Key;
669 Item->Value = Value;
670 InsertTailList (&Map->Used, &Item->Link);
671
672 Map->Count++;
673
674 return EFI_SUCCESS;
675 }
676
677
678 /**
679 Check whether the item is in the Map and return TRUE if it is.
680
681 @param[in] Map The netmap to search within.
682 @param[in] Item The item to search.
683
684 @return TRUE if the item is in the netmap, otherwise FALSE.
685
686 **/
687 BOOLEAN
688 NetItemInMap (
689 IN NET_MAP *Map,
690 IN NET_MAP_ITEM *Item
691 )
692 {
693 LIST_ENTRY *ListEntry;
694
695 NET_LIST_FOR_EACH (ListEntry, &Map->Used) {
696 if (ListEntry == &Item->Link) {
697 return TRUE;
698 }
699 }
700
701 return FALSE;
702 }
703
704
705 /**
706 Find the key in the netmap and returns the point to the item contains the Key.
707
708 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every
709 item with the key to search. It returns the point to the item contains the Key if found.
710
711 If Map is NULL, then ASSERT().
712
713 @param[in] Map The netmap to search within.
714 @param[in] Key The key to search.
715
716 @return The point to the item contains the Key, or NULL if Key isn't in the map.
717
718 **/
719 NET_MAP_ITEM *
720 EFIAPI
721 NetMapFindKey (
722 IN NET_MAP *Map,
723 IN VOID *Key
724 )
725 {
726 LIST_ENTRY *Entry;
727 NET_MAP_ITEM *Item;
728
729 ASSERT (Map != NULL);
730
731 NET_LIST_FOR_EACH (Entry, &Map->Used) {
732 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
733
734 if (Item->Key == Key) {
735 return Item;
736 }
737 }
738
739 return NULL;
740 }
741
742
743 /**
744 Remove the node entry of the item from the netmap and return the key of the removed item.
745
746 Remove the node entry of the item from the Used doubly linked list of the netmap.
747 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
748 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,
749 Value will point to the value of the item. It returns the key of the removed item.
750
751 If Map is NULL, then ASSERT().
752 If Item is NULL, then ASSERT().
753 if item in not in the netmap, then ASSERT().
754
755 @param[in, out] Map The netmap to remove the item from.
756 @param[in, out] Item The item to remove.
757 @param[out] Value The variable to receive the value if not NULL.
758
759 @return The key of the removed item.
760
761 **/
762 VOID *
763 EFIAPI
764 NetMapRemoveItem (
765 IN OUT NET_MAP *Map,
766 IN OUT NET_MAP_ITEM *Item,
767 OUT VOID **Value OPTIONAL
768 )
769 {
770 ASSERT ((Map != NULL) && (Item != NULL));
771 ASSERT (NetItemInMap (Map, Item));
772
773 RemoveEntryList (&Item->Link);
774 Map->Count--;
775 InsertHeadList (&Map->Recycled, &Item->Link);
776
777 if (Value != NULL) {
778 *Value = Item->Value;
779 }
780
781 return Item->Key;
782 }
783
784
785 /**
786 Remove the first node entry on the netmap and return the key of the removed item.
787
788 Remove the first node entry from the Used doubly linked list of the netmap.
789 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
790 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
791 parameter Value will point to the value of the item. It returns the key of the removed item.
792
793 If Map is NULL, then ASSERT().
794 If the Used doubly linked list is empty, then ASSERT().
795
796 @param[in, out] Map The netmap to remove the head from.
797 @param[out] Value The variable to receive the value if not NULL.
798
799 @return The key of the item removed.
800
801 **/
802 VOID *
803 EFIAPI
804 NetMapRemoveHead (
805 IN OUT NET_MAP *Map,
806 OUT VOID **Value OPTIONAL
807 )
808 {
809 NET_MAP_ITEM *Item;
810
811 //
812 // Often, it indicates a programming error to remove
813 // the first entry in an empty list
814 //
815 ASSERT (Map && !IsListEmpty (&Map->Used));
816
817 Item = NET_LIST_HEAD (&Map->Used, NET_MAP_ITEM, Link);
818 RemoveEntryList (&Item->Link);
819 Map->Count--;
820 InsertHeadList (&Map->Recycled, &Item->Link);
821
822 if (Value != NULL) {
823 *Value = Item->Value;
824 }
825
826 return Item->Key;
827 }
828
829
830 /**
831 Remove the last node entry on the netmap and return the key of the removed item.
832
833 Remove the last node entry from the Used doubly linked list of the netmap.
834 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
835 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
836 parameter Value will point to the value of the item. It returns the key of the removed item.
837
838 If Map is NULL, then ASSERT().
839 If the Used doubly linked list is empty, then ASSERT().
840
841 @param[in, out] Map The netmap to remove the tail from.
842 @param[out] Value The variable to receive the value if not NULL.
843
844 @return The key of the item removed.
845
846 **/
847 VOID *
848 EFIAPI
849 NetMapRemoveTail (
850 IN OUT NET_MAP *Map,
851 OUT VOID **Value OPTIONAL
852 )
853 {
854 NET_MAP_ITEM *Item;
855
856 //
857 // Often, it indicates a programming error to remove
858 // the last entry in an empty list
859 //
860 ASSERT (Map && !IsListEmpty (&Map->Used));
861
862 Item = NET_LIST_TAIL (&Map->Used, NET_MAP_ITEM, Link);
863 RemoveEntryList (&Item->Link);
864 Map->Count--;
865 InsertHeadList (&Map->Recycled, &Item->Link);
866
867 if (Value != NULL) {
868 *Value = Item->Value;
869 }
870
871 return Item->Key;
872 }
873
874
875 /**
876 Iterate through the netmap and call CallBack for each item.
877
878 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break
879 from the loop. It returns the CallBack's last return value. This function is
880 delete safe for the current item.
881
882 If Map is NULL, then ASSERT().
883 If CallBack is NULL, then ASSERT().
884
885 @param[in] Map The Map to iterate through.
886 @param[in] CallBack The callback function to call for each item.
887 @param[in] Arg The opaque parameter to the callback.
888
889 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item
890 return EFI_SUCCESS.
891 @retval Others It returns the CallBack's last return value.
892
893 **/
894 EFI_STATUS
895 EFIAPI
896 NetMapIterate (
897 IN NET_MAP *Map,
898 IN NET_MAP_CALLBACK CallBack,
899 IN VOID *Arg
900 )
901 {
902
903 LIST_ENTRY *Entry;
904 LIST_ENTRY *Next;
905 LIST_ENTRY *Head;
906 NET_MAP_ITEM *Item;
907 EFI_STATUS Result;
908
909 ASSERT ((Map != NULL) && (CallBack != NULL));
910
911 Head = &Map->Used;
912
913 if (IsListEmpty (Head)) {
914 return EFI_SUCCESS;
915 }
916
917 NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {
918 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
919 Result = CallBack (Map, Item, Arg);
920
921 if (EFI_ERROR (Result)) {
922 return Result;
923 }
924 }
925
926 return EFI_SUCCESS;
927 }
928
929
930 /**
931 This is the default unload handle for all the network drivers.
932
933 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
934 Uninstall all the protocols installed in the driver entry point.
935
936 @param[in] ImageHandle The drivers' driver image.
937
938 @retval EFI_SUCCESS The image is unloaded.
939 @retval Others Failed to unload the image.
940
941 **/
942 EFI_STATUS
943 EFIAPI
944 NetLibDefaultUnload (
945 IN EFI_HANDLE ImageHandle
946 )
947 {
948 EFI_STATUS Status;
949 EFI_HANDLE *DeviceHandleBuffer;
950 UINTN DeviceHandleCount;
951 UINTN Index;
952 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
953 EFI_COMPONENT_NAME_PROTOCOL *ComponentName;
954 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
955
956 //
957 // Get the list of all the handles in the handle database.
958 // If there is an error getting the list, then the unload
959 // operation fails.
960 //
961 Status = gBS->LocateHandleBuffer (
962 AllHandles,
963 NULL,
964 NULL,
965 &DeviceHandleCount,
966 &DeviceHandleBuffer
967 );
968
969 if (EFI_ERROR (Status)) {
970 return Status;
971 }
972
973 //
974 // Disconnect the driver specified by ImageHandle from all
975 // the devices in the handle database.
976 //
977 for (Index = 0; Index < DeviceHandleCount; Index++) {
978 Status = gBS->DisconnectController (
979 DeviceHandleBuffer[Index],
980 ImageHandle,
981 NULL
982 );
983 }
984
985 //
986 // Uninstall all the protocols installed in the driver entry point
987 //
988 for (Index = 0; Index < DeviceHandleCount; Index++) {
989 Status = gBS->HandleProtocol (
990 DeviceHandleBuffer[Index],
991 &gEfiDriverBindingProtocolGuid,
992 (VOID **) &DriverBinding
993 );
994
995 if (EFI_ERROR (Status)) {
996 continue;
997 }
998
999 if (DriverBinding->ImageHandle != ImageHandle) {
1000 continue;
1001 }
1002
1003 gBS->UninstallProtocolInterface (
1004 ImageHandle,
1005 &gEfiDriverBindingProtocolGuid,
1006 DriverBinding
1007 );
1008 Status = gBS->HandleProtocol (
1009 DeviceHandleBuffer[Index],
1010 &gEfiComponentNameProtocolGuid,
1011 (VOID **) &ComponentName
1012 );
1013 if (!EFI_ERROR (Status)) {
1014 gBS->UninstallProtocolInterface (
1015 ImageHandle,
1016 &gEfiComponentNameProtocolGuid,
1017 ComponentName
1018 );
1019 }
1020
1021 Status = gBS->HandleProtocol (
1022 DeviceHandleBuffer[Index],
1023 &gEfiComponentName2ProtocolGuid,
1024 (VOID **) &ComponentName2
1025 );
1026 if (!EFI_ERROR (Status)) {
1027 gBS->UninstallProtocolInterface (
1028 ImageHandle,
1029 &gEfiComponentName2ProtocolGuid,
1030 ComponentName2
1031 );
1032 }
1033 }
1034
1035 //
1036 // Free the buffer containing the list of handles from the handle database
1037 //
1038 if (DeviceHandleBuffer != NULL) {
1039 gBS->FreePool (DeviceHandleBuffer);
1040 }
1041
1042 return EFI_SUCCESS;
1043 }
1044
1045
1046
1047 /**
1048 Create a child of the service that is identified by ServiceBindingGuid.
1049
1050 Get the ServiceBinding Protocol first, then use it to create a child.
1051
1052 If ServiceBindingGuid is NULL, then ASSERT().
1053 If ChildHandle is NULL, then ASSERT().
1054
1055 @param[in] Controller The controller which has the service installed.
1056 @param[in] Image The image handle used to open service.
1057 @param[in] ServiceBindingGuid The service's Guid.
1058 @param[in, out] ChildHandle The handle to receive the create child.
1059
1060 @retval EFI_SUCCESS The child is successfully created.
1061 @retval Others Failed to create the child.
1062
1063 **/
1064 EFI_STATUS
1065 EFIAPI
1066 NetLibCreateServiceChild (
1067 IN EFI_HANDLE Controller,
1068 IN EFI_HANDLE Image,
1069 IN EFI_GUID *ServiceBindingGuid,
1070 IN OUT EFI_HANDLE *ChildHandle
1071 )
1072 {
1073 EFI_STATUS Status;
1074 EFI_SERVICE_BINDING_PROTOCOL *Service;
1075
1076
1077 ASSERT ((ServiceBindingGuid != NULL) && (ChildHandle != NULL));
1078
1079 //
1080 // Get the ServiceBinding Protocol
1081 //
1082 Status = gBS->OpenProtocol (
1083 Controller,
1084 ServiceBindingGuid,
1085 (VOID **) &Service,
1086 Image,
1087 Controller,
1088 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1089 );
1090
1091 if (EFI_ERROR (Status)) {
1092 return Status;
1093 }
1094
1095 //
1096 // Create a child
1097 //
1098 Status = Service->CreateChild (Service, ChildHandle);
1099 return Status;
1100 }
1101
1102
1103 /**
1104 Destory a child of the service that is identified by ServiceBindingGuid.
1105
1106 Get the ServiceBinding Protocol first, then use it to destroy a child.
1107
1108 If ServiceBindingGuid is NULL, then ASSERT().
1109
1110 @param[in] Controller The controller which has the service installed.
1111 @param[in] Image The image handle used to open service.
1112 @param[in] ServiceBindingGuid The service's Guid.
1113 @param[in] ChildHandle The child to destory.
1114
1115 @retval EFI_SUCCESS The child is successfully destoried.
1116 @retval Others Failed to destory the child.
1117
1118 **/
1119 EFI_STATUS
1120 EFIAPI
1121 NetLibDestroyServiceChild (
1122 IN EFI_HANDLE Controller,
1123 IN EFI_HANDLE Image,
1124 IN EFI_GUID *ServiceBindingGuid,
1125 IN EFI_HANDLE ChildHandle
1126 )
1127 {
1128 EFI_STATUS Status;
1129 EFI_SERVICE_BINDING_PROTOCOL *Service;
1130
1131 ASSERT (ServiceBindingGuid != NULL);
1132
1133 //
1134 // Get the ServiceBinding Protocol
1135 //
1136 Status = gBS->OpenProtocol (
1137 Controller,
1138 ServiceBindingGuid,
1139 (VOID **) &Service,
1140 Image,
1141 Controller,
1142 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1143 );
1144
1145 if (EFI_ERROR (Status)) {
1146 return Status;
1147 }
1148
1149 //
1150 // destory the child
1151 //
1152 Status = Service->DestroyChild (Service, ChildHandle);
1153 return Status;
1154 }
1155
1156
1157 /**
1158 Convert the mac address of the simple network protocol installed on
1159 SnpHandle to a unicode string. Callers are responsible for freeing the
1160 string storage.
1161
1162 Get the mac address of the Simple Network protocol from the SnpHandle. Then convert
1163 the mac address into a unicode string. It takes 2 unicode characters to represent
1164 a 1 byte binary buffer. Plus one unicode character for the null-terminator.
1165
1166
1167 @param[in] SnpHandle The handle where the simple network protocol is
1168 installed on.
1169 @param[in] ImageHandle The image handle used to act as the agent handle to
1170 get the simple network protocol.
1171 @param[out] MacString The pointer to store the address of the string
1172 representation of the mac address.
1173
1174 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.
1175 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
1176 @retval Others Failed to open the simple network protocol.
1177
1178 **/
1179 EFI_STATUS
1180 EFIAPI
1181 NetLibGetMacString (
1182 IN EFI_HANDLE SnpHandle,
1183 IN EFI_HANDLE ImageHandle,
1184 OUT CHAR16 **MacString
1185 )
1186 {
1187 EFI_STATUS Status;
1188 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
1189 EFI_SIMPLE_NETWORK_MODE *Mode;
1190 CHAR16 *MacAddress;
1191 UINTN Index;
1192
1193 *MacString = NULL;
1194
1195 //
1196 // Get the Simple Network protocol from the SnpHandle.
1197 //
1198 Status = gBS->OpenProtocol (
1199 SnpHandle,
1200 &gEfiSimpleNetworkProtocolGuid,
1201 (VOID **) &Snp,
1202 ImageHandle,
1203 SnpHandle,
1204 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1205 );
1206 if (EFI_ERROR (Status)) {
1207 return Status;
1208 }
1209
1210 Mode = Snp->Mode;
1211
1212 //
1213 // It takes 2 unicode characters to represent a 1 byte binary buffer.
1214 // Plus one unicode character for the null-terminator.
1215 //
1216 MacAddress = AllocatePool ((2 * Mode->HwAddressSize + 1) * sizeof (CHAR16));
1217 if (MacAddress == NULL) {
1218 return EFI_OUT_OF_RESOURCES;
1219 }
1220
1221 //
1222 // Convert the mac address into a unicode string.
1223 //
1224 for (Index = 0; Index < Mode->HwAddressSize; Index++) {
1225 MacAddress[Index * 2] = (CHAR16) mNetLibHexStr[(Mode->CurrentAddress.Addr[Index] >> 4) & 0x0F];
1226 MacAddress[Index * 2 + 1] = (CHAR16) mNetLibHexStr[Mode->CurrentAddress.Addr[Index] & 0x0F];
1227 }
1228
1229 MacAddress[Mode->HwAddressSize * 2] = L'\0';
1230
1231 *MacString = MacAddress;
1232
1233 return EFI_SUCCESS;
1234 }
1235
1236 /**
1237 Check the default address used by the IPv4 driver is static or dynamic (acquired
1238 from DHCP).
1239
1240 If the controller handle does not have the NIC Ip4 Config Protocol installed, the
1241 default address is static. If the EFI variable to save the configuration is not found,
1242 the default address is static. Otherwise, get the result from the EFI variable which
1243 saving the configuration.
1244
1245 @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol
1246 relative with the default address to judge.
1247
1248 @retval TRUE If the default address is static.
1249 @retval FALSE If the default address is acquired from DHCP.
1250
1251 **/
1252 BOOLEAN
1253 NetLibDefaultAddressIsStatic (
1254 IN EFI_HANDLE Controller
1255 )
1256 {
1257 EFI_STATUS Status;
1258 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;
1259 UINTN Len;
1260 NIC_IP4_CONFIG_INFO *ConfigInfo;
1261 BOOLEAN IsStatic;
1262 EFI_STRING ConfigHdr;
1263 EFI_STRING ConfigResp;
1264 EFI_STRING AccessProgress;
1265 EFI_STRING AccessResults;
1266 EFI_STRING String;
1267
1268 ConfigInfo = NULL;
1269 ConfigHdr = NULL;
1270 ConfigResp = NULL;
1271 AccessProgress = NULL;
1272 AccessResults = NULL;
1273 IsStatic = TRUE;
1274
1275 Status = gBS->LocateProtocol (
1276 &gEfiHiiConfigRoutingProtocolGuid,
1277 NULL,
1278 (VOID **) &HiiConfigRouting
1279 );
1280 if (EFI_ERROR (Status)) {
1281 return TRUE;
1282 }
1283
1284 //
1285 // Construct config request string header
1286 //
1287 ConfigHdr = HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid, EFI_NIC_IP4_CONFIG_VARIABLE, Controller);
1288
1289 Len = StrLen (ConfigHdr);
1290 ConfigResp = AllocateZeroPool (Len + NIC_ITEM_CONFIG_SIZE * 2 + 200);
1291 if (ConfigResp == NULL) {
1292 goto ON_EXIT;
1293 }
1294 StrCpy (ConfigResp, ConfigHdr);
1295
1296 String = ConfigResp + Len;
1297 UnicodeSPrint (
1298 String,
1299 (8 + 4 + 7 + 4) * sizeof (CHAR16),
1300 L"&OFFSET=%04X&WIDTH=%04X",
1301 OFFSET_OF (NIC_IP4_CONFIG_INFO, Source),
1302 sizeof (UINT32)
1303 );
1304
1305 Status = HiiConfigRouting->ExtractConfig (
1306 HiiConfigRouting,
1307 ConfigResp,
1308 &AccessProgress,
1309 &AccessResults
1310 );
1311 if (EFI_ERROR (Status)) {
1312 goto ON_EXIT;
1313 }
1314
1315 ConfigInfo = AllocateZeroPool (sizeof (NIC_IP4_CONFIG_INFO));
1316 if (ConfigInfo == NULL) {
1317 goto ON_EXIT;
1318 }
1319
1320 ConfigInfo->Source = IP4_CONFIG_SOURCE_STATIC;
1321 Len = NIC_ITEM_CONFIG_SIZE;
1322 Status = HiiConfigRouting->ConfigToBlock (
1323 HiiConfigRouting,
1324 AccessResults,
1325 (UINT8 *) ConfigInfo,
1326 &Len,
1327 &AccessProgress
1328 );
1329 if (EFI_ERROR (Status)) {
1330 goto ON_EXIT;
1331 }
1332
1333 IsStatic = (BOOLEAN) (ConfigInfo->Source == IP4_CONFIG_SOURCE_STATIC);
1334
1335 ON_EXIT:
1336
1337 if (AccessResults != NULL) {
1338 FreePool (AccessResults);
1339 }
1340 if (ConfigInfo != NULL) {
1341 FreePool (ConfigInfo);
1342 }
1343 if (ConfigResp != NULL) {
1344 FreePool (ConfigResp);
1345 }
1346 if (ConfigHdr != NULL) {
1347 FreePool (ConfigHdr);
1348 }
1349
1350 return IsStatic;
1351 }
1352
1353 /**
1354 Create an IPv4 device path node.
1355
1356 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.
1357 The header subtype of IPv4 device path node is MSG_IPv4_DP.
1358 The length of the IPv4 device path node in bytes is 19.
1359 Get other info from parameters to make up the whole IPv4 device path node.
1360
1361 @param[in, out] Node Pointer to the IPv4 device path node.
1362 @param[in] Controller The handle where the NIC IP4 config protocol resides.
1363 @param[in] LocalIp The local IPv4 address.
1364 @param[in] LocalPort The local port.
1365 @param[in] RemoteIp The remote IPv4 address.
1366 @param[in] RemotePort The remote port.
1367 @param[in] Protocol The protocol type in the IP header.
1368 @param[in] UseDefaultAddress Whether this instance is using default address or not.
1369
1370 **/
1371 VOID
1372 EFIAPI
1373 NetLibCreateIPv4DPathNode (
1374 IN OUT IPv4_DEVICE_PATH *Node,
1375 IN EFI_HANDLE Controller,
1376 IN IP4_ADDR LocalIp,
1377 IN UINT16 LocalPort,
1378 IN IP4_ADDR RemoteIp,
1379 IN UINT16 RemotePort,
1380 IN UINT16 Protocol,
1381 IN BOOLEAN UseDefaultAddress
1382 )
1383 {
1384 Node->Header.Type = MESSAGING_DEVICE_PATH;
1385 Node->Header.SubType = MSG_IPv4_DP;
1386 SetDevicePathNodeLength (&Node->Header, 19);
1387
1388 CopyMem (&Node->LocalIpAddress, &LocalIp, sizeof (EFI_IPv4_ADDRESS));
1389 CopyMem (&Node->RemoteIpAddress, &RemoteIp, sizeof (EFI_IPv4_ADDRESS));
1390
1391 Node->LocalPort = LocalPort;
1392 Node->RemotePort = RemotePort;
1393
1394 Node->Protocol = Protocol;
1395
1396 if (!UseDefaultAddress) {
1397 Node->StaticIpAddress = TRUE;
1398 } else {
1399 Node->StaticIpAddress = NetLibDefaultAddressIsStatic (Controller);
1400 }
1401 }
1402
1403
1404 /**
1405 Find the UNDI/SNP handle from controller and protocol GUID.
1406
1407 For example, IP will open a MNP child to transmit/receive
1408 packets, when MNP is stopped, IP should also be stopped. IP
1409 needs to find its own private data which is related the IP's
1410 service binding instance that is install on UNDI/SNP handle.
1411 Now, the controller is either a MNP or ARP child handle. But
1412 IP opens these handle BY_DRIVER, use that info, we can get the
1413 UNDI/SNP handle.
1414
1415 @param[in] Controller Then protocol handle to check.
1416 @param[in] ProtocolGuid The protocol that is related with the handle.
1417
1418 @return The UNDI/SNP handle or NULL for errors.
1419
1420 **/
1421 EFI_HANDLE
1422 EFIAPI
1423 NetLibGetNicHandle (
1424 IN EFI_HANDLE Controller,
1425 IN EFI_GUID *ProtocolGuid
1426 )
1427 {
1428 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer;
1429 EFI_HANDLE Handle;
1430 EFI_STATUS Status;
1431 UINTN OpenCount;
1432 UINTN Index;
1433
1434 Status = gBS->OpenProtocolInformation (
1435 Controller,
1436 ProtocolGuid,
1437 &OpenBuffer,
1438 &OpenCount
1439 );
1440
1441 if (EFI_ERROR (Status)) {
1442 return NULL;
1443 }
1444
1445 Handle = NULL;
1446
1447 for (Index = 0; Index < OpenCount; Index++) {
1448 if (OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
1449 Handle = OpenBuffer[Index].ControllerHandle;
1450 break;
1451 }
1452 }
1453
1454 gBS->FreePool (OpenBuffer);
1455 return Handle;
1456 }
1457
1458 /**
1459 Add a Deferred Procedure Call to the end of the DPC queue.
1460
1461 @param[in] DpcTpl The EFI_TPL that the DPC should be invoked.
1462 @param[in] DpcProcedure Pointer to the DPC's function.
1463 @param[in] DpcContext Pointer to the DPC's context. Passed to DpcProcedure
1464 when DpcProcedure is invoked.
1465
1466 @retval EFI_SUCCESS The DPC was queued.
1467 @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL, or DpcProcedure
1468 is NULL.
1469 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
1470 add the DPC to the queue.
1471
1472 **/
1473 EFI_STATUS
1474 EFIAPI
1475 NetLibQueueDpc (
1476 IN EFI_TPL DpcTpl,
1477 IN EFI_DPC_PROCEDURE DpcProcedure,
1478 IN VOID *DpcContext OPTIONAL
1479 )
1480 {
1481 return mDpc->QueueDpc (mDpc, DpcTpl, DpcProcedure, DpcContext);
1482 }
1483
1484 /**
1485 Dispatch the queue of DPCs. ALL DPCs that have been queued with a DpcTpl
1486 value greater than or equal to the current TPL are invoked in the order that
1487 they were queued. DPCs with higher DpcTpl values are invoked before DPCs with
1488 lower DpcTpl values.
1489
1490 @retval EFI_SUCCESS One or more DPCs were invoked.
1491 @retval EFI_NOT_FOUND No DPCs were invoked.
1492
1493 **/
1494 EFI_STATUS
1495 EFIAPI
1496 NetLibDispatchDpc (
1497 VOID
1498 )
1499 {
1500 return mDpc->DispatchDpc(mDpc);
1501 }
1502
1503 /**
1504 The constructor function caches the pointer to DPC protocol.
1505
1506 The constructor function locates DPC protocol from protocol database.
1507 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
1508
1509 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1510 @param[in] SystemTable A pointer to the EFI System Table.
1511
1512 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1513
1514 **/
1515 EFI_STATUS
1516 EFIAPI
1517 NetLibConstructor (
1518 IN EFI_HANDLE ImageHandle,
1519 IN EFI_SYSTEM_TABLE *SystemTable
1520 )
1521 {
1522 EFI_STATUS Status;
1523
1524 Status = gBS->LocateProtocol (&gEfiDpcProtocolGuid, NULL, (VOID**) &mDpc);
1525 ASSERT_EFI_ERROR (Status);
1526 ASSERT (mDpc != NULL);
1527
1528 return Status;
1529 }