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