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