]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
Use GetFirstGuidHob() instead of GetNextGuidHob() when only the first GUID instance...
[mirror_edk2.git] / MdeModulePkg / Library / DxeNetLib / DxeNetLib.c
1 /** @file
2 Network library.
3
4 Copyright (c) 2005 - 2010, 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/DriverBinding.h>
17 #include <Protocol/ServiceBinding.h>
18 #include <Protocol/SimpleNetwork.h>
19 #include <Protocol/ManagedNetwork.h>
20 #include <Protocol/HiiConfigRouting.h>
21 #include <Protocol/ComponentName.h>
22 #include <Protocol/ComponentName2.h>
23
24 #include <Guid/NicIp4ConfigNvData.h>
25
26 #include <Library/NetLib.h>
27 #include <Library/BaseLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/UefiRuntimeServicesTableLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/DevicePathLib.h>
34 #include <Library/HiiLib.h>
35 #include <Library/PrintLib.h>
36
37 #define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE
38
39 //
40 // All the supported IP4 maskes in host byte order.
41 //
42 GLOBAL_REMOVE_IF_UNREFERENCED IP4_ADDR gIp4AllMasks[IP4_MASK_NUM] = {
43 0x00000000,
44 0x80000000,
45 0xC0000000,
46 0xE0000000,
47 0xF0000000,
48 0xF8000000,
49 0xFC000000,
50 0xFE000000,
51
52 0xFF000000,
53 0xFF800000,
54 0xFFC00000,
55 0xFFE00000,
56 0xFFF00000,
57 0xFFF80000,
58 0xFFFC0000,
59 0xFFFE0000,
60
61 0xFFFF0000,
62 0xFFFF8000,
63 0xFFFFC000,
64 0xFFFFE000,
65 0xFFFFF000,
66 0xFFFFF800,
67 0xFFFFFC00,
68 0xFFFFFE00,
69
70 0xFFFFFF00,
71 0xFFFFFF80,
72 0xFFFFFFC0,
73 0xFFFFFFE0,
74 0xFFFFFFF0,
75 0xFFFFFFF8,
76 0xFFFFFFFC,
77 0xFFFFFFFE,
78 0xFFFFFFFF,
79 };
80
81 GLOBAL_REMOVE_IF_UNREFERENCED EFI_IPv4_ADDRESS mZeroIp4Addr = {{0, 0, 0, 0}};
82
83 //
84 // Any error level digitally larger than mNetDebugLevelMax
85 // will be silently discarded.
86 //
87 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mNetDebugLevelMax = NETDEBUG_LEVEL_ERROR;
88 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogPacketSeq = 0xDEADBEEF;
89
90 //
91 // You can change mSyslogDstMac mSyslogDstIp and mSyslogSrcIp
92 // here to direct the syslog packets to the syslog deamon. The
93 // default is broadcast to both the ethernet and IP.
94 //
95 GLOBAL_REMOVE_IF_UNREFERENCED UINT8 mSyslogDstMac[NET_ETHER_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
96 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogDstIp = 0xffffffff;
97 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogSrcIp = 0;
98
99 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 *mMonthName[] = {
100 "Jan",
101 "Feb",
102 "Mar",
103 "Apr",
104 "May",
105 "Jun",
106 "Jul",
107 "Aug",
108 "Sep",
109 "Oct",
110 "Nov",
111 "Dec"
112 };
113
114 //
115 // VLAN device path node template
116 //
117 GLOBAL_REMOVE_IF_UNREFERENCED VLAN_DEVICE_PATH mNetVlanDevicePathTemplate = {
118 {
119 MESSAGING_DEVICE_PATH,
120 MSG_VLAN_DP,
121 {
122 (UINT8) (sizeof (VLAN_DEVICE_PATH)),
123 (UINT8) ((sizeof (VLAN_DEVICE_PATH)) >> 8)
124 }
125 },
126 0
127 };
128
129 /**
130 Locate the handles that support SNP, then open one of them
131 to send the syslog packets. The caller isn't required to close
132 the SNP after use because the SNP is opened by HandleProtocol.
133
134 @return The point to SNP if one is properly openned. Otherwise NULL
135
136 **/
137 EFI_SIMPLE_NETWORK_PROTOCOL *
138 SyslogLocateSnp (
139 VOID
140 )
141 {
142 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
143 EFI_STATUS Status;
144 EFI_HANDLE *Handles;
145 UINTN HandleCount;
146 UINTN Index;
147
148 //
149 // Locate the handles which has SNP installed.
150 //
151 Handles = NULL;
152 Status = gBS->LocateHandleBuffer (
153 ByProtocol,
154 &gEfiSimpleNetworkProtocolGuid,
155 NULL,
156 &HandleCount,
157 &Handles
158 );
159
160 if (EFI_ERROR (Status) || (HandleCount == 0)) {
161 return NULL;
162 }
163
164 //
165 // Try to open one of the ethernet SNP protocol to send packet
166 //
167 Snp = NULL;
168
169 for (Index = 0; Index < HandleCount; Index++) {
170 Status = gBS->HandleProtocol (
171 Handles[Index],
172 &gEfiSimpleNetworkProtocolGuid,
173 (VOID **) &Snp
174 );
175
176 if ((Status == EFI_SUCCESS) && (Snp != NULL) &&
177 (Snp->Mode->IfType == NET_IFTYPE_ETHERNET) &&
178 (Snp->Mode->MaxPacketSize >= NET_SYSLOG_PACKET_LEN)) {
179
180 break;
181 }
182
183 Snp = NULL;
184 }
185
186 FreePool (Handles);
187 return Snp;
188 }
189
190 /**
191 Transmit a syslog packet synchronously through SNP. The Packet
192 already has the ethernet header prepended. This function should
193 fill in the source MAC because it will try to locate a SNP each
194 time it is called to avoid the problem if SNP is unloaded.
195 This code snip is copied from MNP.
196
197 @param[in] Packet The Syslog packet
198 @param[in] Length The length of the packet
199
200 @retval EFI_DEVICE_ERROR Failed to locate a usable SNP protocol
201 @retval EFI_TIMEOUT Timeout happened to send the packet.
202 @retval EFI_SUCCESS Packet is sent.
203
204 **/
205 EFI_STATUS
206 SyslogSendPacket (
207 IN CHAR8 *Packet,
208 IN UINT32 Length
209 )
210 {
211 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
212 ETHER_HEAD *Ether;
213 EFI_STATUS Status;
214 EFI_EVENT TimeoutEvent;
215 UINT8 *TxBuf;
216
217 Snp = SyslogLocateSnp ();
218
219 if (Snp == NULL) {
220 return EFI_DEVICE_ERROR;
221 }
222
223 Ether = (ETHER_HEAD *) Packet;
224 CopyMem (Ether->SrcMac, Snp->Mode->CurrentAddress.Addr, NET_ETHER_ADDR_LEN);
225
226 //
227 // Start the timeout event.
228 //
229 Status = gBS->CreateEvent (
230 EVT_TIMER,
231 TPL_NOTIFY,
232 NULL,
233 NULL,
234 &TimeoutEvent
235 );
236
237 if (EFI_ERROR (Status)) {
238 return Status;
239 }
240
241 Status = gBS->SetTimer (TimeoutEvent, TimerRelative, NET_SYSLOG_TX_TIMEOUT);
242
243 if (EFI_ERROR (Status)) {
244 goto ON_EXIT;
245 }
246
247 for (;;) {
248 //
249 // Transmit the packet through SNP.
250 //
251 Status = Snp->Transmit (Snp, 0, Length, Packet, NULL, NULL, NULL);
252
253 if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_READY)) {
254 Status = EFI_DEVICE_ERROR;
255 break;
256 }
257
258 //
259 // If Status is EFI_SUCCESS, the packet is put in the transmit queue.
260 // if Status is EFI_NOT_READY, the transmit engine of the network
261 // interface is busy. Both need to sync SNP.
262 //
263 TxBuf = NULL;
264
265 do {
266 //
267 // Get the recycled transmit buffer status.
268 //
269 Snp->GetStatus (Snp, NULL, (VOID **) &TxBuf);
270
271 if (!EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) {
272 Status = EFI_TIMEOUT;
273 break;
274 }
275
276 } while (TxBuf == NULL);
277
278 if ((Status == EFI_SUCCESS) || (Status == EFI_TIMEOUT)) {
279 break;
280 }
281
282 //
283 // Status is EFI_NOT_READY. Restart the timer event and
284 // call Snp->Transmit again.
285 //
286 gBS->SetTimer (TimeoutEvent, TimerRelative, NET_SYSLOG_TX_TIMEOUT);
287 }
288
289 gBS->SetTimer (TimeoutEvent, TimerCancel, 0);
290
291 ON_EXIT:
292 gBS->CloseEvent (TimeoutEvent);
293 return Status;
294 }
295
296 /**
297 Build a syslog packet, including the Ethernet/Ip/Udp headers
298 and user's message.
299
300 @param[in] Level Syslog servity level
301 @param[in] Module The module that generates the log
302 @param[in] File The file that contains the current log
303 @param[in] Line The line of code in the File that contains the current log
304 @param[in] Message The log message
305 @param[in] BufLen The lenght of the Buf
306 @param[out] Buf The buffer to put the packet data
307
308 @return The length of the syslog packet built.
309
310 **/
311 UINT32
312 SyslogBuildPacket (
313 IN UINT32 Level,
314 IN UINT8 *Module,
315 IN UINT8 *File,
316 IN UINT32 Line,
317 IN UINT8 *Message,
318 IN UINT32 BufLen,
319 OUT CHAR8 *Buf
320 )
321 {
322 ETHER_HEAD *Ether;
323 IP4_HEAD *Ip4;
324 EFI_UDP_HEADER *Udp4;
325 EFI_TIME Time;
326 UINT32 Pri;
327 UINT32 Len;
328
329 //
330 // Fill in the Ethernet header. Leave alone the source MAC.
331 // SyslogSendPacket will fill in the address for us.
332 //
333 Ether = (ETHER_HEAD *) Buf;
334 CopyMem (Ether->DstMac, mSyslogDstMac, NET_ETHER_ADDR_LEN);
335 ZeroMem (Ether->SrcMac, NET_ETHER_ADDR_LEN);
336
337 Ether->EtherType = HTONS (0x0800); // IPv4 protocol
338
339 Buf += sizeof (ETHER_HEAD);
340 BufLen -= sizeof (ETHER_HEAD);
341
342 //
343 // Fill in the IP header
344 //
345 Ip4 = (IP4_HEAD *) Buf;
346 Ip4->HeadLen = 5;
347 Ip4->Ver = 4;
348 Ip4->Tos = 0;
349 Ip4->TotalLen = 0;
350 Ip4->Id = (UINT16) mSyslogPacketSeq;
351 Ip4->Fragment = 0;
352 Ip4->Ttl = 16;
353 Ip4->Protocol = 0x11;
354 Ip4->Checksum = 0;
355 Ip4->Src = mSyslogSrcIp;
356 Ip4->Dst = mSyslogDstIp;
357
358 Buf += sizeof (IP4_HEAD);
359 BufLen -= sizeof (IP4_HEAD);
360
361 //
362 // Fill in the UDP header, Udp checksum is optional. Leave it zero.
363 //
364 Udp4 = (EFI_UDP_HEADER *) Buf;
365 Udp4->SrcPort = HTONS (514);
366 Udp4->DstPort = HTONS (514);
367 Udp4->Length = 0;
368 Udp4->Checksum = 0;
369
370 Buf += sizeof (EFI_UDP_HEADER);
371 BufLen -= sizeof (EFI_UDP_HEADER);
372
373 //
374 // Build the syslog message body with <PRI> Timestamp machine module Message
375 //
376 Pri = ((NET_SYSLOG_FACILITY & 31) << 3) | (Level & 7);
377 gRT->GetTime (&Time, NULL);
378 ASSERT ((Time.Month <= 12) && (Time.Month >= 1));
379
380 //
381 // Use %a to format the ASCII strings, %s to format UNICODE strings
382 //
383 Len = 0;
384 Len += (UINT32) AsciiSPrint (
385 Buf,
386 BufLen,
387 "<%d> %a %d %d:%d:%d ",
388 Pri,
389 mMonthName [Time.Month-1],
390 Time.Day,
391 Time.Hour,
392 Time.Minute,
393 Time.Second
394 );
395 Len--;
396
397 Len += (UINT32) AsciiSPrint (
398 Buf + Len,
399 BufLen - Len,
400 "Tiano %a: %a (Line: %d File: %a)",
401 Module,
402 Message,
403 Line,
404 File
405 );
406 Len--;
407
408 //
409 // OK, patch the IP length/checksum and UDP length fields.
410 //
411 Len += sizeof (EFI_UDP_HEADER);
412 Udp4->Length = HTONS ((UINT16) Len);
413
414 Len += sizeof (IP4_HEAD);
415 Ip4->TotalLen = HTONS ((UINT16) Len);
416 Ip4->Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Ip4, sizeof (IP4_HEAD)));
417
418 return Len + sizeof (ETHER_HEAD);
419 }
420
421 /**
422 Allocate a buffer, then format the message to it. This is a
423 help function for the NET_DEBUG_XXX macros. The PrintArg of
424 these macros treats the variable length print parameters as a
425 single parameter, and pass it to the NetDebugASPrint. For
426 example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name))
427 if extracted to:
428
429 NetDebugOutput (
430 NETDEBUG_LEVEL_TRACE,
431 "Tcp",
432 __FILE__,
433 __LINE__,
434 NetDebugASPrint ("State transit to %a\n", Name)
435 )
436
437 @param Format The ASCII format string.
438 @param ... The variable length parameter whose format is determined
439 by the Format string.
440
441 @return The buffer containing the formatted message,
442 or NULL if failed to allocate memory.
443
444 **/
445 CHAR8 *
446 NetDebugASPrint (
447 IN CHAR8 *Format,
448 ...
449 )
450 {
451 VA_LIST Marker;
452 CHAR8 *Buf;
453
454 Buf = (CHAR8 *) AllocatePool (NET_DEBUG_MSG_LEN);
455
456 if (Buf == NULL) {
457 return NULL;
458 }
459
460 VA_START (Marker, Format);
461 AsciiVSPrint (Buf, NET_DEBUG_MSG_LEN, Format, Marker);
462 VA_END (Marker);
463
464 return Buf;
465 }
466
467 /**
468 Builds an UDP4 syslog packet and send it using SNP.
469
470 This function will locate a instance of SNP then send the message through it.
471 Because it isn't open the SNP BY_DRIVER, apply caution when using it.
472
473 @param Level The servity level of the message.
474 @param Module The Moudle that generates the log.
475 @param File The file that contains the log.
476 @param Line The exact line that contains the log.
477 @param Message The user message to log.
478
479 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
480 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet
481 @retval EFI_SUCCESS The log is discard because that it is more verbose
482 than the mNetDebugLevelMax. Or, it has been sent out.
483 **/
484 EFI_STATUS
485 NetDebugOutput (
486 IN UINT32 Level,
487 IN UINT8 *Module,
488 IN UINT8 *File,
489 IN UINT32 Line,
490 IN UINT8 *Message
491 )
492 {
493 CHAR8 *Packet;
494 UINT32 Len;
495 EFI_STATUS Status;
496
497 //
498 // Check whether the message should be sent out
499 //
500 if (Message == NULL) {
501 return EFI_INVALID_PARAMETER;
502 }
503
504 if (Level > mNetDebugLevelMax) {
505 Status = EFI_SUCCESS;
506 goto ON_EXIT;
507 }
508
509 //
510 // Allocate a maxium of 1024 bytes, the caller should ensure
511 // that the message plus the ethernet/ip/udp header is shorter
512 // than this
513 //
514 Packet = (CHAR8 *) AllocatePool (NET_SYSLOG_PACKET_LEN);
515
516 if (Packet == NULL) {
517 Status = EFI_OUT_OF_RESOURCES;
518 goto ON_EXIT;
519 }
520
521 //
522 // Build the message: Ethernet header + IP header + Udp Header + user data
523 //
524 Len = SyslogBuildPacket (
525 Level,
526 Module,
527 File,
528 Line,
529 Message,
530 NET_SYSLOG_PACKET_LEN,
531 Packet
532 );
533
534 mSyslogPacketSeq++;
535 Status = SyslogSendPacket (Packet, Len);
536 FreePool (Packet);
537
538 ON_EXIT:
539 FreePool (Message);
540 return Status;
541 }
542 /**
543 Return the length of the mask.
544
545 Return the length of the mask, the correct value is from 0 to 32.
546 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.
547 NetMask is in the host byte order.
548
549 @param[in] NetMask The netmask to get the length from.
550
551 @return The length of the netmask, IP4_MASK_NUM if the mask is invalid.
552
553 **/
554 INTN
555 EFIAPI
556 NetGetMaskLength (
557 IN IP4_ADDR NetMask
558 )
559 {
560 INTN Index;
561
562 for (Index = 0; Index < IP4_MASK_NUM; Index++) {
563 if (NetMask == gIp4AllMasks[Index]) {
564 break;
565 }
566 }
567
568 return Index;
569 }
570
571
572
573 /**
574 Return the class of the IP address, such as class A, B, C.
575 Addr is in host byte order.
576
577 The address of class A starts with 0.
578 If the address belong to class A, return IP4_ADDR_CLASSA.
579 The address of class B starts with 10.
580 If the address belong to class B, return IP4_ADDR_CLASSB.
581 The address of class C starts with 110.
582 If the address belong to class C, return IP4_ADDR_CLASSC.
583 The address of class D starts with 1110.
584 If the address belong to class D, return IP4_ADDR_CLASSD.
585 The address of class E starts with 1111.
586 If the address belong to class E, return IP4_ADDR_CLASSE.
587
588
589 @param[in] Addr The address to get the class from.
590
591 @return IP address class, such as IP4_ADDR_CLASSA.
592
593 **/
594 INTN
595 EFIAPI
596 NetGetIpClass (
597 IN IP4_ADDR Addr
598 )
599 {
600 UINT8 ByteOne;
601
602 ByteOne = (UINT8) (Addr >> 24);
603
604 if ((ByteOne & 0x80) == 0) {
605 return IP4_ADDR_CLASSA;
606
607 } else if ((ByteOne & 0xC0) == 0x80) {
608 return IP4_ADDR_CLASSB;
609
610 } else if ((ByteOne & 0xE0) == 0xC0) {
611 return IP4_ADDR_CLASSC;
612
613 } else if ((ByteOne & 0xF0) == 0xE0) {
614 return IP4_ADDR_CLASSD;
615
616 } else {
617 return IP4_ADDR_CLASSE;
618
619 }
620 }
621
622
623 /**
624 Check whether the IP is a valid unicast address according to
625 the netmask. If NetMask is zero, use the IP address's class to get the default mask.
626
627 If Ip is 0, IP is not a valid unicast address.
628 Class D address is used for multicasting and class E address is reserved for future. If Ip
629 belongs to class D or class E, IP is not a valid unicast address.
630 If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address.
631
632 @param[in] Ip The IP to check against.
633 @param[in] NetMask The mask of the IP.
634
635 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.
636
637 **/
638 BOOLEAN
639 EFIAPI
640 NetIp4IsUnicast (
641 IN IP4_ADDR Ip,
642 IN IP4_ADDR NetMask
643 )
644 {
645 INTN Class;
646
647 Class = NetGetIpClass (Ip);
648
649 if ((Ip == 0) || (Class >= IP4_ADDR_CLASSD)) {
650 return FALSE;
651 }
652
653 if (NetMask == 0) {
654 NetMask = gIp4AllMasks[Class << 3];
655 }
656
657 if (((Ip &~NetMask) == ~NetMask) || ((Ip &~NetMask) == 0)) {
658 return FALSE;
659 }
660
661 return TRUE;
662 }
663
664 /**
665 Check whether the incoming IPv6 address is a valid unicast address.
666
667 If the address is a multicast address has binary 0xFF at the start, it is not
668 a valid unicast address. If the address is unspecified ::, it is not a valid
669 unicast address to be assigned to any node. If the address is loopback address
670 ::1, it is also not a valid unicast address to be assigned to any physical
671 interface.
672
673 @param[in] Ip6 The IPv6 address to check against.
674
675 @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE.
676
677 **/
678 BOOLEAN
679 NetIp6IsValidUnicast (
680 IN EFI_IPv6_ADDRESS *Ip6
681 )
682 {
683 UINT8 Byte;
684 UINT8 Index;
685
686 if (Ip6->Addr[0] == 0xFF) {
687 return FALSE;
688 }
689
690 for (Index = 0; Index < 15; Index++) {
691 if (Ip6->Addr[Index] != 0) {
692 return TRUE;
693 }
694 }
695
696 Byte = Ip6->Addr[Index];
697
698 if (Byte == 0x0 || Byte == 0x1) {
699 return FALSE;
700 }
701
702 return TRUE;
703 }
704
705 /**
706 Check whether the incoming Ipv6 address is the unspecified address or not.
707
708 @param[in] Ip6 - Ip6 address, in network order.
709
710 @retval TRUE - Yes, unspecified
711 @retval FALSE - No
712
713 **/
714 BOOLEAN
715 NetIp6IsUnspecifiedAddr (
716 IN EFI_IPv6_ADDRESS *Ip6
717 )
718 {
719 UINT8 Index;
720
721 for (Index = 0; Index < 16; Index++) {
722 if (Ip6->Addr[Index] != 0) {
723 return FALSE;
724 }
725 }
726
727 return TRUE;
728 }
729
730 /**
731 Check whether the incoming Ipv6 address is a link-local address.
732
733 @param[in] Ip6 - Ip6 address, in network order.
734
735 @retval TRUE - Yes, link-local address
736 @retval FALSE - No
737
738 **/
739 BOOLEAN
740 NetIp6IsLinkLocalAddr (
741 IN EFI_IPv6_ADDRESS *Ip6
742 )
743 {
744 UINT8 Index;
745
746 ASSERT (Ip6 != NULL);
747
748 if (Ip6->Addr[0] != 0xFE) {
749 return FALSE;
750 }
751
752 if (Ip6->Addr[1] != 0x80) {
753 return FALSE;
754 }
755
756 for (Index = 2; Index < 8; Index++) {
757 if (Ip6->Addr[Index] != 0) {
758 return FALSE;
759 }
760 }
761
762 return TRUE;
763 }
764
765 /**
766 Check whether the Ipv6 address1 and address2 are on the connected network.
767
768 @param[in] Ip1 - Ip6 address1, in network order.
769 @param[in] Ip2 - Ip6 address2, in network order.
770 @param[in] PrefixLength - The prefix length of the checking net.
771
772 @retval TRUE - Yes, connected.
773 @retval FALSE - No.
774
775 **/
776 BOOLEAN
777 NetIp6IsNetEqual (
778 EFI_IPv6_ADDRESS *Ip1,
779 EFI_IPv6_ADDRESS *Ip2,
780 UINT8 PrefixLength
781 )
782 {
783 UINT8 Byte;
784 UINT8 Bit;
785 UINT8 Mask;
786
787 ASSERT ((Ip1 != NULL) && (Ip2 != NULL) && (PrefixLength < IP6_PREFIX_NUM));
788
789 if (PrefixLength == 0) {
790 return TRUE;
791 }
792
793 Byte = (UINT8) (PrefixLength / 8);
794 Bit = (UINT8) (PrefixLength % 8);
795
796 if (CompareMem (Ip1, Ip2, Byte) != 0) {
797 return FALSE;
798 }
799
800 if (Bit > 0) {
801 Mask = (UINT8) (0xFF << (8 - Bit));
802
803 ASSERT (Byte < 16);
804 if ((Ip1->Addr[Byte] & Mask) != (Ip2->Addr[Byte] & Mask)) {
805 return FALSE;
806 }
807 }
808
809 return TRUE;
810 }
811
812
813 /**
814 Switches the endianess of an IPv6 address
815
816 This function swaps the bytes in a 128-bit IPv6 address to switch the value
817 from little endian to big endian or vice versa. The byte swapped value is
818 returned.
819
820 @param Ip6 Points to an IPv6 address
821
822 @return The byte swapped IPv6 address.
823
824 **/
825 EFI_IPv6_ADDRESS *
826 Ip6Swap128 (
827 EFI_IPv6_ADDRESS *Ip6
828 )
829 {
830 UINT64 High;
831 UINT64 Low;
832
833 CopyMem (&High, Ip6, sizeof (UINT64));
834 CopyMem (&Low, &Ip6->Addr[8], sizeof (UINT64));
835
836 High = SwapBytes64 (High);
837 Low = SwapBytes64 (Low);
838
839 CopyMem (Ip6, &Low, sizeof (UINT64));
840 CopyMem (&Ip6->Addr[8], &High, sizeof (UINT64));
841
842 return Ip6;
843 }
844
845 /**
846 Initialize a random seed using current time.
847
848 Get current time first. Then initialize a random seed based on some basic
849 mathematics operation on the hour, day, minute, second, nanosecond and year
850 of the current time.
851
852 @return The random seed initialized with current time.
853
854 **/
855 UINT32
856 EFIAPI
857 NetRandomInitSeed (
858 VOID
859 )
860 {
861 EFI_TIME Time;
862 UINT32 Seed;
863
864 gRT->GetTime (&Time, NULL);
865 Seed = (~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second);
866 Seed ^= Time.Nanosecond;
867 Seed ^= Time.Year << 7;
868
869 return Seed;
870 }
871
872
873 /**
874 Extract a UINT32 from a byte stream.
875
876 Copy a UINT32 from a byte stream, then converts it from Network
877 byte order to host byte order. Use this function to avoid alignment error.
878
879 @param[in] Buf The buffer to extract the UINT32.
880
881 @return The UINT32 extracted.
882
883 **/
884 UINT32
885 EFIAPI
886 NetGetUint32 (
887 IN UINT8 *Buf
888 )
889 {
890 UINT32 Value;
891
892 CopyMem (&Value, Buf, sizeof (UINT32));
893 return NTOHL (Value);
894 }
895
896
897 /**
898 Put a UINT32 to the byte stream in network byte order.
899
900 Converts a UINT32 from host byte order to network byte order. Then copy it to the
901 byte stream.
902
903 @param[in, out] Buf The buffer to put the UINT32.
904 @param[in] Data The data to put.
905
906 **/
907 VOID
908 EFIAPI
909 NetPutUint32 (
910 IN OUT UINT8 *Buf,
911 IN UINT32 Data
912 )
913 {
914 Data = HTONL (Data);
915 CopyMem (Buf, &Data, sizeof (UINT32));
916 }
917
918
919 /**
920 Remove the first node entry on the list, and return the removed node entry.
921
922 Removes the first node Entry from a doubly linked list. It is up to the caller of
923 this function to release the memory used by the first node if that is required. On
924 exit, the removed node is returned.
925
926 If Head is NULL, then ASSERT().
927 If Head was not initialized, then ASSERT().
928 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
929 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
930 then ASSERT().
931
932 @param[in, out] Head The list header.
933
934 @return The first node entry that is removed from the list, NULL if the list is empty.
935
936 **/
937 LIST_ENTRY *
938 EFIAPI
939 NetListRemoveHead (
940 IN OUT LIST_ENTRY *Head
941 )
942 {
943 LIST_ENTRY *First;
944
945 ASSERT (Head != NULL);
946
947 if (IsListEmpty (Head)) {
948 return NULL;
949 }
950
951 First = Head->ForwardLink;
952 Head->ForwardLink = First->ForwardLink;
953 First->ForwardLink->BackLink = Head;
954
955 DEBUG_CODE (
956 First->ForwardLink = (LIST_ENTRY *) NULL;
957 First->BackLink = (LIST_ENTRY *) NULL;
958 );
959
960 return First;
961 }
962
963
964 /**
965 Remove the last node entry on the list and and return the removed node entry.
966
967 Removes the last node entry from a doubly linked list. It is up to the caller of
968 this function to release the memory used by the first node if that is required. On
969 exit, the removed node is returned.
970
971 If Head is NULL, then ASSERT().
972 If Head was not initialized, then ASSERT().
973 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
974 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
975 then ASSERT().
976
977 @param[in, out] Head The list head.
978
979 @return The last node entry that is removed from the list, NULL if the list is empty.
980
981 **/
982 LIST_ENTRY *
983 EFIAPI
984 NetListRemoveTail (
985 IN OUT LIST_ENTRY *Head
986 )
987 {
988 LIST_ENTRY *Last;
989
990 ASSERT (Head != NULL);
991
992 if (IsListEmpty (Head)) {
993 return NULL;
994 }
995
996 Last = Head->BackLink;
997 Head->BackLink = Last->BackLink;
998 Last->BackLink->ForwardLink = Head;
999
1000 DEBUG_CODE (
1001 Last->ForwardLink = (LIST_ENTRY *) NULL;
1002 Last->BackLink = (LIST_ENTRY *) NULL;
1003 );
1004
1005 return Last;
1006 }
1007
1008
1009 /**
1010 Insert a new node entry after a designated node entry of a doubly linked list.
1011
1012 Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry
1013 of the doubly linked list.
1014
1015 @param[in, out] PrevEntry The previous entry to insert after.
1016 @param[in, out] NewEntry The new entry to insert.
1017
1018 **/
1019 VOID
1020 EFIAPI
1021 NetListInsertAfter (
1022 IN OUT LIST_ENTRY *PrevEntry,
1023 IN OUT LIST_ENTRY *NewEntry
1024 )
1025 {
1026 NewEntry->BackLink = PrevEntry;
1027 NewEntry->ForwardLink = PrevEntry->ForwardLink;
1028 PrevEntry->ForwardLink->BackLink = NewEntry;
1029 PrevEntry->ForwardLink = NewEntry;
1030 }
1031
1032
1033 /**
1034 Insert a new node entry before a designated node entry of a doubly linked list.
1035
1036 Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry
1037 of the doubly linked list.
1038
1039 @param[in, out] PostEntry The entry to insert before.
1040 @param[in, out] NewEntry The new entry to insert.
1041
1042 **/
1043 VOID
1044 EFIAPI
1045 NetListInsertBefore (
1046 IN OUT LIST_ENTRY *PostEntry,
1047 IN OUT LIST_ENTRY *NewEntry
1048 )
1049 {
1050 NewEntry->ForwardLink = PostEntry;
1051 NewEntry->BackLink = PostEntry->BackLink;
1052 PostEntry->BackLink->ForwardLink = NewEntry;
1053 PostEntry->BackLink = NewEntry;
1054 }
1055
1056
1057 /**
1058 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
1059
1060 Initialize the forward and backward links of two head nodes donated by Map->Used
1061 and Map->Recycled of two doubly linked lists.
1062 Initializes the count of the <Key, Value> pairs in the netmap to zero.
1063
1064 If Map is NULL, then ASSERT().
1065 If the address of Map->Used is NULL, then ASSERT().
1066 If the address of Map->Recycled is NULl, then ASSERT().
1067
1068 @param[in, out] Map The netmap to initialize.
1069
1070 **/
1071 VOID
1072 EFIAPI
1073 NetMapInit (
1074 IN OUT NET_MAP *Map
1075 )
1076 {
1077 ASSERT (Map != NULL);
1078
1079 InitializeListHead (&Map->Used);
1080 InitializeListHead (&Map->Recycled);
1081 Map->Count = 0;
1082 }
1083
1084
1085 /**
1086 To clean up the netmap, that is, release allocated memories.
1087
1088 Removes all nodes of the Used doubly linked list and free memory of all related netmap items.
1089 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.
1090 The number of the <Key, Value> pairs in the netmap is set to be zero.
1091
1092 If Map is NULL, then ASSERT().
1093
1094 @param[in, out] Map The netmap to clean up.
1095
1096 **/
1097 VOID
1098 EFIAPI
1099 NetMapClean (
1100 IN OUT NET_MAP *Map
1101 )
1102 {
1103 NET_MAP_ITEM *Item;
1104 LIST_ENTRY *Entry;
1105 LIST_ENTRY *Next;
1106
1107 ASSERT (Map != NULL);
1108
1109 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Used) {
1110 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
1111
1112 RemoveEntryList (&Item->Link);
1113 Map->Count--;
1114
1115 gBS->FreePool (Item);
1116 }
1117
1118 ASSERT ((Map->Count == 0) && IsListEmpty (&Map->Used));
1119
1120 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Recycled) {
1121 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
1122
1123 RemoveEntryList (&Item->Link);
1124 gBS->FreePool (Item);
1125 }
1126
1127 ASSERT (IsListEmpty (&Map->Recycled));
1128 }
1129
1130
1131 /**
1132 Test whether the netmap is empty and return true if it is.
1133
1134 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.
1135
1136 If Map is NULL, then ASSERT().
1137
1138
1139 @param[in] Map The net map to test.
1140
1141 @return TRUE if the netmap is empty, otherwise FALSE.
1142
1143 **/
1144 BOOLEAN
1145 EFIAPI
1146 NetMapIsEmpty (
1147 IN NET_MAP *Map
1148 )
1149 {
1150 ASSERT (Map != NULL);
1151 return (BOOLEAN) (Map->Count == 0);
1152 }
1153
1154
1155 /**
1156 Return the number of the <Key, Value> pairs in the netmap.
1157
1158 @param[in] Map The netmap to get the entry number.
1159
1160 @return The entry number in the netmap.
1161
1162 **/
1163 UINTN
1164 EFIAPI
1165 NetMapGetCount (
1166 IN NET_MAP *Map
1167 )
1168 {
1169 return Map->Count;
1170 }
1171
1172
1173 /**
1174 Return one allocated item.
1175
1176 If the Recycled doubly linked list of the netmap is empty, it will try to allocate
1177 a batch of items if there are enough resources and add corresponding nodes to the begining
1178 of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove
1179 the fist node entry of the Recycled doubly linked list and return the corresponding item.
1180
1181 If Map is NULL, then ASSERT().
1182
1183 @param[in, out] Map The netmap to allocate item for.
1184
1185 @return The allocated item. If NULL, the
1186 allocation failed due to resource limit.
1187
1188 **/
1189 NET_MAP_ITEM *
1190 NetMapAllocItem (
1191 IN OUT NET_MAP *Map
1192 )
1193 {
1194 NET_MAP_ITEM *Item;
1195 LIST_ENTRY *Head;
1196 UINTN Index;
1197
1198 ASSERT (Map != NULL);
1199
1200 Head = &Map->Recycled;
1201
1202 if (IsListEmpty (Head)) {
1203 for (Index = 0; Index < NET_MAP_INCREAMENT; Index++) {
1204 Item = AllocatePool (sizeof (NET_MAP_ITEM));
1205
1206 if (Item == NULL) {
1207 if (Index == 0) {
1208 return NULL;
1209 }
1210
1211 break;
1212 }
1213
1214 InsertHeadList (Head, &Item->Link);
1215 }
1216 }
1217
1218 Item = NET_LIST_HEAD (Head, NET_MAP_ITEM, Link);
1219 NetListRemoveHead (Head);
1220
1221 return Item;
1222 }
1223
1224
1225 /**
1226 Allocate an item to save the <Key, Value> pair to the head of the netmap.
1227
1228 Allocate an item to save the <Key, Value> pair and add corresponding node entry
1229 to the beginning of the Used doubly linked list. The number of the <Key, Value>
1230 pairs in the netmap increase by 1.
1231
1232 If Map is NULL, then ASSERT().
1233
1234 @param[in, out] Map The netmap to insert into.
1235 @param[in] Key The user's key.
1236 @param[in] Value The user's value for the key.
1237
1238 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
1239 @retval EFI_SUCCESS The item is inserted to the head.
1240
1241 **/
1242 EFI_STATUS
1243 EFIAPI
1244 NetMapInsertHead (
1245 IN OUT NET_MAP *Map,
1246 IN VOID *Key,
1247 IN VOID *Value OPTIONAL
1248 )
1249 {
1250 NET_MAP_ITEM *Item;
1251
1252 ASSERT (Map != NULL);
1253
1254 Item = NetMapAllocItem (Map);
1255
1256 if (Item == NULL) {
1257 return EFI_OUT_OF_RESOURCES;
1258 }
1259
1260 Item->Key = Key;
1261 Item->Value = Value;
1262 InsertHeadList (&Map->Used, &Item->Link);
1263
1264 Map->Count++;
1265 return EFI_SUCCESS;
1266 }
1267
1268
1269 /**
1270 Allocate an item to save the <Key, Value> pair to the tail of the netmap.
1271
1272 Allocate an item to save the <Key, Value> pair and add corresponding node entry
1273 to the tail of the Used doubly linked list. The number of the <Key, Value>
1274 pairs in the netmap increase by 1.
1275
1276 If Map is NULL, then ASSERT().
1277
1278 @param[in, out] Map The netmap to insert into.
1279 @param[in] Key The user's key.
1280 @param[in] Value The user's value for the key.
1281
1282 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
1283 @retval EFI_SUCCESS The item is inserted to the tail.
1284
1285 **/
1286 EFI_STATUS
1287 EFIAPI
1288 NetMapInsertTail (
1289 IN OUT NET_MAP *Map,
1290 IN VOID *Key,
1291 IN VOID *Value OPTIONAL
1292 )
1293 {
1294 NET_MAP_ITEM *Item;
1295
1296 ASSERT (Map != NULL);
1297
1298 Item = NetMapAllocItem (Map);
1299
1300 if (Item == NULL) {
1301 return EFI_OUT_OF_RESOURCES;
1302 }
1303
1304 Item->Key = Key;
1305 Item->Value = Value;
1306 InsertTailList (&Map->Used, &Item->Link);
1307
1308 Map->Count++;
1309
1310 return EFI_SUCCESS;
1311 }
1312
1313
1314 /**
1315 Check whether the item is in the Map and return TRUE if it is.
1316
1317 @param[in] Map The netmap to search within.
1318 @param[in] Item The item to search.
1319
1320 @return TRUE if the item is in the netmap, otherwise FALSE.
1321
1322 **/
1323 BOOLEAN
1324 NetItemInMap (
1325 IN NET_MAP *Map,
1326 IN NET_MAP_ITEM *Item
1327 )
1328 {
1329 LIST_ENTRY *ListEntry;
1330
1331 NET_LIST_FOR_EACH (ListEntry, &Map->Used) {
1332 if (ListEntry == &Item->Link) {
1333 return TRUE;
1334 }
1335 }
1336
1337 return FALSE;
1338 }
1339
1340
1341 /**
1342 Find the key in the netmap and returns the point to the item contains the Key.
1343
1344 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every
1345 item with the key to search. It returns the point to the item contains the Key if found.
1346
1347 If Map is NULL, then ASSERT().
1348
1349 @param[in] Map The netmap to search within.
1350 @param[in] Key The key to search.
1351
1352 @return The point to the item contains the Key, or NULL if Key isn't in the map.
1353
1354 **/
1355 NET_MAP_ITEM *
1356 EFIAPI
1357 NetMapFindKey (
1358 IN NET_MAP *Map,
1359 IN VOID *Key
1360 )
1361 {
1362 LIST_ENTRY *Entry;
1363 NET_MAP_ITEM *Item;
1364
1365 ASSERT (Map != NULL);
1366
1367 NET_LIST_FOR_EACH (Entry, &Map->Used) {
1368 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
1369
1370 if (Item->Key == Key) {
1371 return Item;
1372 }
1373 }
1374
1375 return NULL;
1376 }
1377
1378
1379 /**
1380 Remove the node entry of the item from the netmap and return the key of the removed item.
1381
1382 Remove the node entry of the item from the Used doubly linked list of the netmap.
1383 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1384 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,
1385 Value will point to the value of the item. It returns the key of the removed item.
1386
1387 If Map is NULL, then ASSERT().
1388 If Item is NULL, then ASSERT().
1389 if item in not in the netmap, then ASSERT().
1390
1391 @param[in, out] Map The netmap to remove the item from.
1392 @param[in, out] Item The item to remove.
1393 @param[out] Value The variable to receive the value if not NULL.
1394
1395 @return The key of the removed item.
1396
1397 **/
1398 VOID *
1399 EFIAPI
1400 NetMapRemoveItem (
1401 IN OUT NET_MAP *Map,
1402 IN OUT NET_MAP_ITEM *Item,
1403 OUT VOID **Value OPTIONAL
1404 )
1405 {
1406 ASSERT ((Map != NULL) && (Item != NULL));
1407 ASSERT (NetItemInMap (Map, Item));
1408
1409 RemoveEntryList (&Item->Link);
1410 Map->Count--;
1411 InsertHeadList (&Map->Recycled, &Item->Link);
1412
1413 if (Value != NULL) {
1414 *Value = Item->Value;
1415 }
1416
1417 return Item->Key;
1418 }
1419
1420
1421 /**
1422 Remove the first node entry on the netmap and return the key of the removed item.
1423
1424 Remove the first node entry from the Used doubly linked list of the netmap.
1425 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1426 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
1427 parameter Value will point to the value of the item. It returns the key of the removed item.
1428
1429 If Map is NULL, then ASSERT().
1430 If the Used doubly linked list is empty, then ASSERT().
1431
1432 @param[in, out] Map The netmap to remove the head from.
1433 @param[out] Value The variable to receive the value if not NULL.
1434
1435 @return The key of the item removed.
1436
1437 **/
1438 VOID *
1439 EFIAPI
1440 NetMapRemoveHead (
1441 IN OUT NET_MAP *Map,
1442 OUT VOID **Value OPTIONAL
1443 )
1444 {
1445 NET_MAP_ITEM *Item;
1446
1447 //
1448 // Often, it indicates a programming error to remove
1449 // the first entry in an empty list
1450 //
1451 ASSERT (Map && !IsListEmpty (&Map->Used));
1452
1453 Item = NET_LIST_HEAD (&Map->Used, NET_MAP_ITEM, Link);
1454 RemoveEntryList (&Item->Link);
1455 Map->Count--;
1456 InsertHeadList (&Map->Recycled, &Item->Link);
1457
1458 if (Value != NULL) {
1459 *Value = Item->Value;
1460 }
1461
1462 return Item->Key;
1463 }
1464
1465
1466 /**
1467 Remove the last node entry on the netmap and return the key of the removed item.
1468
1469 Remove the last node entry from the Used doubly linked list of the netmap.
1470 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1471 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
1472 parameter Value will point to the value of the item. It returns the key of the removed item.
1473
1474 If Map is NULL, then ASSERT().
1475 If the Used doubly linked list is empty, then ASSERT().
1476
1477 @param[in, out] Map The netmap to remove the tail from.
1478 @param[out] Value The variable to receive the value if not NULL.
1479
1480 @return The key of the item removed.
1481
1482 **/
1483 VOID *
1484 EFIAPI
1485 NetMapRemoveTail (
1486 IN OUT NET_MAP *Map,
1487 OUT VOID **Value OPTIONAL
1488 )
1489 {
1490 NET_MAP_ITEM *Item;
1491
1492 //
1493 // Often, it indicates a programming error to remove
1494 // the last entry in an empty list
1495 //
1496 ASSERT (Map && !IsListEmpty (&Map->Used));
1497
1498 Item = NET_LIST_TAIL (&Map->Used, NET_MAP_ITEM, Link);
1499 RemoveEntryList (&Item->Link);
1500 Map->Count--;
1501 InsertHeadList (&Map->Recycled, &Item->Link);
1502
1503 if (Value != NULL) {
1504 *Value = Item->Value;
1505 }
1506
1507 return Item->Key;
1508 }
1509
1510
1511 /**
1512 Iterate through the netmap and call CallBack for each item.
1513
1514 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break
1515 from the loop. It returns the CallBack's last return value. This function is
1516 delete safe for the current item.
1517
1518 If Map is NULL, then ASSERT().
1519 If CallBack is NULL, then ASSERT().
1520
1521 @param[in] Map The Map to iterate through.
1522 @param[in] CallBack The callback function to call for each item.
1523 @param[in] Arg The opaque parameter to the callback.
1524
1525 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item
1526 return EFI_SUCCESS.
1527 @retval Others It returns the CallBack's last return value.
1528
1529 **/
1530 EFI_STATUS
1531 EFIAPI
1532 NetMapIterate (
1533 IN NET_MAP *Map,
1534 IN NET_MAP_CALLBACK CallBack,
1535 IN VOID *Arg OPTIONAL
1536 )
1537 {
1538
1539 LIST_ENTRY *Entry;
1540 LIST_ENTRY *Next;
1541 LIST_ENTRY *Head;
1542 NET_MAP_ITEM *Item;
1543 EFI_STATUS Result;
1544
1545 ASSERT ((Map != NULL) && (CallBack != NULL));
1546
1547 Head = &Map->Used;
1548
1549 if (IsListEmpty (Head)) {
1550 return EFI_SUCCESS;
1551 }
1552
1553 NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {
1554 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
1555 Result = CallBack (Map, Item, Arg);
1556
1557 if (EFI_ERROR (Result)) {
1558 return Result;
1559 }
1560 }
1561
1562 return EFI_SUCCESS;
1563 }
1564
1565
1566 /**
1567 This is the default unload handle for all the network drivers.
1568
1569 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
1570 Uninstall all the protocols installed in the driver entry point.
1571
1572 @param[in] ImageHandle The drivers' driver image.
1573
1574 @retval EFI_SUCCESS The image is unloaded.
1575 @retval Others Failed to unload the image.
1576
1577 **/
1578 EFI_STATUS
1579 EFIAPI
1580 NetLibDefaultUnload (
1581 IN EFI_HANDLE ImageHandle
1582 )
1583 {
1584 EFI_STATUS Status;
1585 EFI_HANDLE *DeviceHandleBuffer;
1586 UINTN DeviceHandleCount;
1587 UINTN Index;
1588 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
1589 EFI_COMPONENT_NAME_PROTOCOL *ComponentName;
1590 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
1591
1592 //
1593 // Get the list of all the handles in the handle database.
1594 // If there is an error getting the list, then the unload
1595 // operation fails.
1596 //
1597 Status = gBS->LocateHandleBuffer (
1598 AllHandles,
1599 NULL,
1600 NULL,
1601 &DeviceHandleCount,
1602 &DeviceHandleBuffer
1603 );
1604
1605 if (EFI_ERROR (Status)) {
1606 return Status;
1607 }
1608
1609 //
1610 // Disconnect the driver specified by ImageHandle from all
1611 // the devices in the handle database.
1612 //
1613 for (Index = 0; Index < DeviceHandleCount; Index++) {
1614 Status = gBS->DisconnectController (
1615 DeviceHandleBuffer[Index],
1616 ImageHandle,
1617 NULL
1618 );
1619 }
1620
1621 //
1622 // Uninstall all the protocols installed in the driver entry point
1623 //
1624 for (Index = 0; Index < DeviceHandleCount; Index++) {
1625 Status = gBS->HandleProtocol (
1626 DeviceHandleBuffer[Index],
1627 &gEfiDriverBindingProtocolGuid,
1628 (VOID **) &DriverBinding
1629 );
1630
1631 if (EFI_ERROR (Status)) {
1632 continue;
1633 }
1634
1635 if (DriverBinding->ImageHandle != ImageHandle) {
1636 continue;
1637 }
1638
1639 gBS->UninstallProtocolInterface (
1640 ImageHandle,
1641 &gEfiDriverBindingProtocolGuid,
1642 DriverBinding
1643 );
1644 Status = gBS->HandleProtocol (
1645 DeviceHandleBuffer[Index],
1646 &gEfiComponentNameProtocolGuid,
1647 (VOID **) &ComponentName
1648 );
1649 if (!EFI_ERROR (Status)) {
1650 gBS->UninstallProtocolInterface (
1651 ImageHandle,
1652 &gEfiComponentNameProtocolGuid,
1653 ComponentName
1654 );
1655 }
1656
1657 Status = gBS->HandleProtocol (
1658 DeviceHandleBuffer[Index],
1659 &gEfiComponentName2ProtocolGuid,
1660 (VOID **) &ComponentName2
1661 );
1662 if (!EFI_ERROR (Status)) {
1663 gBS->UninstallProtocolInterface (
1664 ImageHandle,
1665 &gEfiComponentName2ProtocolGuid,
1666 ComponentName2
1667 );
1668 }
1669 }
1670
1671 //
1672 // Free the buffer containing the list of handles from the handle database
1673 //
1674 if (DeviceHandleBuffer != NULL) {
1675 gBS->FreePool (DeviceHandleBuffer);
1676 }
1677
1678 return EFI_SUCCESS;
1679 }
1680
1681
1682
1683 /**
1684 Create a child of the service that is identified by ServiceBindingGuid.
1685
1686 Get the ServiceBinding Protocol first, then use it to create a child.
1687
1688 If ServiceBindingGuid is NULL, then ASSERT().
1689 If ChildHandle is NULL, then ASSERT().
1690
1691 @param[in] Controller The controller which has the service installed.
1692 @param[in] Image The image handle used to open service.
1693 @param[in] ServiceBindingGuid The service's Guid.
1694 @param[in, out] ChildHandle The handle to receive the create child.
1695
1696 @retval EFI_SUCCESS The child is successfully created.
1697 @retval Others Failed to create the child.
1698
1699 **/
1700 EFI_STATUS
1701 EFIAPI
1702 NetLibCreateServiceChild (
1703 IN EFI_HANDLE Controller,
1704 IN EFI_HANDLE Image,
1705 IN EFI_GUID *ServiceBindingGuid,
1706 IN OUT EFI_HANDLE *ChildHandle
1707 )
1708 {
1709 EFI_STATUS Status;
1710 EFI_SERVICE_BINDING_PROTOCOL *Service;
1711
1712
1713 ASSERT ((ServiceBindingGuid != NULL) && (ChildHandle != NULL));
1714
1715 //
1716 // Get the ServiceBinding Protocol
1717 //
1718 Status = gBS->OpenProtocol (
1719 Controller,
1720 ServiceBindingGuid,
1721 (VOID **) &Service,
1722 Image,
1723 Controller,
1724 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1725 );
1726
1727 if (EFI_ERROR (Status)) {
1728 return Status;
1729 }
1730
1731 //
1732 // Create a child
1733 //
1734 Status = Service->CreateChild (Service, ChildHandle);
1735 return Status;
1736 }
1737
1738
1739 /**
1740 Destory a child of the service that is identified by ServiceBindingGuid.
1741
1742 Get the ServiceBinding Protocol first, then use it to destroy a child.
1743
1744 If ServiceBindingGuid is NULL, then ASSERT().
1745
1746 @param[in] Controller The controller which has the service installed.
1747 @param[in] Image The image handle used to open service.
1748 @param[in] ServiceBindingGuid The service's Guid.
1749 @param[in] ChildHandle The child to destory.
1750
1751 @retval EFI_SUCCESS The child is successfully destoried.
1752 @retval Others Failed to destory the child.
1753
1754 **/
1755 EFI_STATUS
1756 EFIAPI
1757 NetLibDestroyServiceChild (
1758 IN EFI_HANDLE Controller,
1759 IN EFI_HANDLE Image,
1760 IN EFI_GUID *ServiceBindingGuid,
1761 IN EFI_HANDLE ChildHandle
1762 )
1763 {
1764 EFI_STATUS Status;
1765 EFI_SERVICE_BINDING_PROTOCOL *Service;
1766
1767 ASSERT (ServiceBindingGuid != NULL);
1768
1769 //
1770 // Get the ServiceBinding Protocol
1771 //
1772 Status = gBS->OpenProtocol (
1773 Controller,
1774 ServiceBindingGuid,
1775 (VOID **) &Service,
1776 Image,
1777 Controller,
1778 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1779 );
1780
1781 if (EFI_ERROR (Status)) {
1782 return Status;
1783 }
1784
1785 //
1786 // destory the child
1787 //
1788 Status = Service->DestroyChild (Service, ChildHandle);
1789 return Status;
1790 }
1791
1792 /**
1793 Get handle with Simple Network Protocol installed on it.
1794
1795 There should be MNP Service Binding Protocol installed on the input ServiceHandle.
1796 If Simple Network Protocol is already installed on the ServiceHandle, the
1797 ServiceHandle will be returned. If SNP is not installed on the ServiceHandle,
1798 try to find its parent handle with SNP installed.
1799
1800 @param[in] ServiceHandle The handle where network service binding protocols are
1801 installed on.
1802 @param[out] Snp The pointer to store the address of the SNP instance.
1803 This is an optional parameter that may be NULL.
1804
1805 @return The SNP handle, or NULL if not found.
1806
1807 **/
1808 EFI_HANDLE
1809 EFIAPI
1810 NetLibGetSnpHandle (
1811 IN EFI_HANDLE ServiceHandle,
1812 OUT EFI_SIMPLE_NETWORK_PROTOCOL **Snp OPTIONAL
1813 )
1814 {
1815 EFI_STATUS Status;
1816 EFI_SIMPLE_NETWORK_PROTOCOL *SnpInstance;
1817 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1818 EFI_HANDLE SnpHandle;
1819
1820 //
1821 // Try to open SNP from ServiceHandle
1822 //
1823 SnpInstance = NULL;
1824 Status = gBS->HandleProtocol (ServiceHandle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &SnpInstance);
1825 if (!EFI_ERROR (Status)) {
1826 if (Snp != NULL) {
1827 *Snp = SnpInstance;
1828 }
1829 return ServiceHandle;
1830 }
1831
1832 //
1833 // Failed to open SNP, try to get SNP handle by LocateDevicePath()
1834 //
1835 DevicePath = DevicePathFromHandle (ServiceHandle);
1836 if (DevicePath == NULL) {
1837 return NULL;
1838 }
1839
1840 SnpHandle = NULL;
1841 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &DevicePath, &SnpHandle);
1842 if (EFI_ERROR (Status)) {
1843 //
1844 // Failed to find SNP handle
1845 //
1846 return NULL;
1847 }
1848
1849 Status = gBS->HandleProtocol (SnpHandle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &SnpInstance);
1850 if (!EFI_ERROR (Status)) {
1851 if (Snp != NULL) {
1852 *Snp = SnpInstance;
1853 }
1854 return SnpHandle;
1855 }
1856
1857 return NULL;
1858 }
1859
1860 /**
1861 Retrieve VLAN ID of a VLAN device handle.
1862
1863 Search VLAN device path node in Device Path of specified ServiceHandle and
1864 return its VLAN ID. If no VLAN device path node found, then this ServiceHandle
1865 is not a VLAN device handle, and 0 will be returned.
1866
1867 @param[in] ServiceHandle The handle where network service binding protocols are
1868 installed on.
1869
1870 @return VLAN ID of the device handle, or 0 if not a VLAN device.
1871
1872 **/
1873 UINT16
1874 EFIAPI
1875 NetLibGetVlanId (
1876 IN EFI_HANDLE ServiceHandle
1877 )
1878 {
1879 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1880 EFI_DEVICE_PATH_PROTOCOL *Node;
1881
1882 DevicePath = DevicePathFromHandle (ServiceHandle);
1883 if (DevicePath == NULL) {
1884 return 0;
1885 }
1886
1887 Node = DevicePath;
1888 while (!IsDevicePathEnd (Node)) {
1889 if (Node->Type == MESSAGING_DEVICE_PATH && Node->SubType == MSG_VLAN_DP) {
1890 return ((VLAN_DEVICE_PATH *) Node)->VlanId;
1891 }
1892 Node = NextDevicePathNode (Node);
1893 }
1894
1895 return 0;
1896 }
1897
1898 /**
1899 Find VLAN device handle with specified VLAN ID.
1900
1901 The VLAN child device handle is created by VLAN Config Protocol on ControllerHandle.
1902 This function will append VLAN device path node to the parent device path,
1903 and then use LocateDevicePath() to find the correct VLAN device handle.
1904
1905 @param[in] ControllerHandle The handle where network service binding protocols are
1906 installed on.
1907 @param[in] VlanId The configured VLAN ID for the VLAN device.
1908
1909 @return The VLAN device handle, or NULL if not found.
1910
1911 **/
1912 EFI_HANDLE
1913 EFIAPI
1914 NetLibGetVlanHandle (
1915 IN EFI_HANDLE ControllerHandle,
1916 IN UINT16 VlanId
1917 )
1918 {
1919 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
1920 EFI_DEVICE_PATH_PROTOCOL *VlanDevicePath;
1921 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1922 VLAN_DEVICE_PATH VlanNode;
1923 EFI_HANDLE Handle;
1924
1925 ParentDevicePath = DevicePathFromHandle (ControllerHandle);
1926 if (ParentDevicePath == NULL) {
1927 return NULL;
1928 }
1929
1930 //
1931 // Construct VLAN device path
1932 //
1933 CopyMem (&VlanNode, &mNetVlanDevicePathTemplate, sizeof (VLAN_DEVICE_PATH));
1934 VlanNode.VlanId = VlanId;
1935 VlanDevicePath = AppendDevicePathNode (
1936 ParentDevicePath,
1937 (EFI_DEVICE_PATH_PROTOCOL *) &VlanNode
1938 );
1939 if (VlanDevicePath == NULL) {
1940 return NULL;
1941 }
1942
1943 //
1944 // Find VLAN device handle
1945 //
1946 Handle = NULL;
1947 DevicePath = VlanDevicePath;
1948 gBS->LocateDevicePath (
1949 &gEfiDevicePathProtocolGuid,
1950 &DevicePath,
1951 &Handle
1952 );
1953 if (!IsDevicePathEnd (DevicePath)) {
1954 //
1955 // Device path is not exactly match
1956 //
1957 Handle = NULL;
1958 }
1959
1960 FreePool (VlanDevicePath);
1961 return Handle;
1962 }
1963
1964 /**
1965 Get MAC address associated with the network service handle.
1966
1967 There should be MNP Service Binding Protocol installed on the input ServiceHandle.
1968 If SNP is installed on the ServiceHandle or its parent handle, MAC address will
1969 be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP.
1970
1971 @param[in] ServiceHandle The handle where network service binding protocols are
1972 installed on.
1973 @param[out] MacAddress The pointer to store the returned MAC address.
1974 @param[out] AddressSize The length of returned MAC address.
1975
1976 @retval EFI_SUCCESS MAC address is returned successfully.
1977 @retval Others Failed to get SNP mode data.
1978
1979 **/
1980 EFI_STATUS
1981 EFIAPI
1982 NetLibGetMacAddress (
1983 IN EFI_HANDLE ServiceHandle,
1984 OUT EFI_MAC_ADDRESS *MacAddress,
1985 OUT UINTN *AddressSize
1986 )
1987 {
1988 EFI_STATUS Status;
1989 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
1990 EFI_SIMPLE_NETWORK_MODE *SnpMode;
1991 EFI_SIMPLE_NETWORK_MODE SnpModeData;
1992 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;
1993 EFI_SERVICE_BINDING_PROTOCOL *MnpSb;
1994 EFI_HANDLE *SnpHandle;
1995 EFI_HANDLE MnpChildHandle;
1996
1997 ASSERT (MacAddress != NULL);
1998 ASSERT (AddressSize != NULL);
1999
2000 //
2001 // Try to get SNP handle
2002 //
2003 Snp = NULL;
2004 SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp);
2005 if (SnpHandle != NULL) {
2006 //
2007 // SNP found, use it directly
2008 //
2009 SnpMode = Snp->Mode;
2010 } else {
2011 //
2012 // Failed to get SNP handle, try to get MAC address from MNP
2013 //
2014 MnpChildHandle = NULL;
2015 Status = gBS->HandleProtocol (
2016 ServiceHandle,
2017 &gEfiManagedNetworkServiceBindingProtocolGuid,
2018 (VOID **) &MnpSb
2019 );
2020 if (EFI_ERROR (Status)) {
2021 return Status;
2022 }
2023
2024 //
2025 // Create a MNP child
2026 //
2027 Status = MnpSb->CreateChild (MnpSb, &MnpChildHandle);
2028 if (EFI_ERROR (Status)) {
2029 return Status;
2030 }
2031
2032 //
2033 // Open MNP protocol
2034 //
2035 Status = gBS->HandleProtocol (
2036 MnpChildHandle,
2037 &gEfiManagedNetworkProtocolGuid,
2038 (VOID **) &Mnp
2039 );
2040 if (EFI_ERROR (Status)) {
2041 return Status;
2042 }
2043
2044 //
2045 // Try to get SNP mode from MNP
2046 //
2047 Status = Mnp->GetModeData (Mnp, NULL, &SnpModeData);
2048 if (EFI_ERROR (Status)) {
2049 return Status;
2050 }
2051 SnpMode = &SnpModeData;
2052
2053 //
2054 // Destroy the MNP child
2055 //
2056 MnpSb->DestroyChild (MnpSb, MnpChildHandle);
2057 }
2058
2059 *AddressSize = SnpMode->HwAddressSize;
2060 CopyMem (MacAddress->Addr, SnpMode->CurrentAddress.Addr, SnpMode->HwAddressSize);
2061
2062 return EFI_SUCCESS;
2063 }
2064
2065 /**
2066 Convert MAC address of the NIC associated with specified Service Binding Handle
2067 to a unicode string. Callers are responsible for freeing the string storage.
2068
2069 Locate simple network protocol associated with the Service Binding Handle and
2070 get the mac address from SNP. Then convert the mac address into a unicode
2071 string. It takes 2 unicode characters to represent a 1 byte binary buffer.
2072 Plus one unicode character for the null-terminator.
2073
2074 @param[in] ServiceHandle The handle where network service binding protocol is
2075 installed on.
2076 @param[in] ImageHandle The image handle used to act as the agent handle to
2077 get the simple network protocol.
2078 @param[out] MacString The pointer to store the address of the string
2079 representation of the mac address.
2080
2081 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.
2082 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
2083 @retval Others Failed to open the simple network protocol.
2084
2085 **/
2086 EFI_STATUS
2087 EFIAPI
2088 NetLibGetMacString (
2089 IN EFI_HANDLE ServiceHandle,
2090 IN EFI_HANDLE ImageHandle,
2091 OUT CHAR16 **MacString
2092 )
2093 {
2094 EFI_STATUS Status;
2095 EFI_MAC_ADDRESS MacAddress;
2096 UINT8 *HwAddress;
2097 UINTN HwAddressSize;
2098 UINT16 VlanId;
2099 CHAR16 *String;
2100 UINTN Index;
2101
2102 ASSERT (MacString != NULL);
2103
2104 //
2105 // Get MAC address of the network device
2106 //
2107 Status = NetLibGetMacAddress (ServiceHandle, &MacAddress, &HwAddressSize);
2108 if (EFI_ERROR (Status)) {
2109 return Status;
2110 }
2111
2112 //
2113 // It takes 2 unicode characters to represent a 1 byte binary buffer.
2114 // If VLAN is configured, it will need extra 5 characters like "\0005".
2115 // Plus one unicode character for the null-terminator.
2116 //
2117 String = AllocateZeroPool ((2 * HwAddressSize + 5 + 1) * sizeof (CHAR16));
2118 if (String == NULL) {
2119 return EFI_OUT_OF_RESOURCES;
2120 }
2121 *MacString = String;
2122
2123 //
2124 // Convert the MAC address into a unicode string.
2125 //
2126 HwAddress = &MacAddress.Addr[0];
2127 for (Index = 0; Index < HwAddressSize; Index++) {
2128 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *(HwAddress++), 2);
2129 }
2130
2131 //
2132 // Append VLAN ID if any
2133 //
2134 VlanId = NetLibGetVlanId (ServiceHandle);
2135 if (VlanId != 0) {
2136 *String++ = L'\\';
2137 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, VlanId, 4);
2138 }
2139
2140 //
2141 // Null terminate the Unicode string
2142 //
2143 *String = L'\0';
2144
2145 return EFI_SUCCESS;
2146 }
2147
2148 /**
2149 Check the default address used by the IPv4 driver is static or dynamic (acquired
2150 from DHCP).
2151
2152 If the controller handle does not have the NIC Ip4 Config Protocol installed, the
2153 default address is static. If the EFI variable to save the configuration is not found,
2154 the default address is static. Otherwise, get the result from the EFI variable which
2155 saving the configuration.
2156
2157 @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol
2158 relative with the default address to judge.
2159
2160 @retval TRUE If the default address is static.
2161 @retval FALSE If the default address is acquired from DHCP.
2162
2163 **/
2164 BOOLEAN
2165 NetLibDefaultAddressIsStatic (
2166 IN EFI_HANDLE Controller
2167 )
2168 {
2169 EFI_STATUS Status;
2170 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;
2171 UINTN Len;
2172 NIC_IP4_CONFIG_INFO *ConfigInfo;
2173 BOOLEAN IsStatic;
2174 EFI_STRING ConfigHdr;
2175 EFI_STRING ConfigResp;
2176 EFI_STRING AccessProgress;
2177 EFI_STRING AccessResults;
2178 EFI_STRING String;
2179
2180 ConfigInfo = NULL;
2181 ConfigHdr = NULL;
2182 ConfigResp = NULL;
2183 AccessProgress = NULL;
2184 AccessResults = NULL;
2185 IsStatic = TRUE;
2186
2187 Status = gBS->LocateProtocol (
2188 &gEfiHiiConfigRoutingProtocolGuid,
2189 NULL,
2190 (VOID **) &HiiConfigRouting
2191 );
2192 if (EFI_ERROR (Status)) {
2193 return TRUE;
2194 }
2195
2196 //
2197 // Construct config request string header
2198 //
2199 ConfigHdr = HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid, EFI_NIC_IP4_CONFIG_VARIABLE, Controller);
2200 if (ConfigHdr == NULL) {
2201 return TRUE;
2202 }
2203
2204 Len = StrLen (ConfigHdr);
2205 ConfigResp = AllocateZeroPool ((Len + NIC_ITEM_CONFIG_SIZE * 2 + 100) * sizeof (CHAR16));
2206 if (ConfigResp == NULL) {
2207 goto ON_EXIT;
2208 }
2209 StrCpy (ConfigResp, ConfigHdr);
2210
2211 String = ConfigResp + Len;
2212 UnicodeSPrint (
2213 String,
2214 (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16),
2215 L"&OFFSET=%04X&WIDTH=%04X",
2216 OFFSET_OF (NIC_IP4_CONFIG_INFO, Source),
2217 sizeof (UINT32)
2218 );
2219
2220 Status = HiiConfigRouting->ExtractConfig (
2221 HiiConfigRouting,
2222 ConfigResp,
2223 &AccessProgress,
2224 &AccessResults
2225 );
2226 if (EFI_ERROR (Status)) {
2227 goto ON_EXIT;
2228 }
2229
2230 ConfigInfo = AllocateZeroPool (sizeof (NIC_ITEM_CONFIG_SIZE));
2231 if (ConfigInfo == NULL) {
2232 goto ON_EXIT;
2233 }
2234
2235 ConfigInfo->Source = IP4_CONFIG_SOURCE_STATIC;
2236 Len = NIC_ITEM_CONFIG_SIZE;
2237 Status = HiiConfigRouting->ConfigToBlock (
2238 HiiConfigRouting,
2239 AccessResults,
2240 (UINT8 *) ConfigInfo,
2241 &Len,
2242 &AccessProgress
2243 );
2244 if (EFI_ERROR (Status)) {
2245 goto ON_EXIT;
2246 }
2247
2248 IsStatic = (BOOLEAN) (ConfigInfo->Source == IP4_CONFIG_SOURCE_STATIC);
2249
2250 ON_EXIT:
2251
2252 if (AccessResults != NULL) {
2253 FreePool (AccessResults);
2254 }
2255 if (ConfigInfo != NULL) {
2256 FreePool (ConfigInfo);
2257 }
2258 if (ConfigResp != NULL) {
2259 FreePool (ConfigResp);
2260 }
2261 if (ConfigHdr != NULL) {
2262 FreePool (ConfigHdr);
2263 }
2264
2265 return IsStatic;
2266 }
2267
2268 /**
2269 Create an IPv4 device path node.
2270
2271 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.
2272 The header subtype of IPv4 device path node is MSG_IPv4_DP.
2273 The length of the IPv4 device path node in bytes is 19.
2274 Get other info from parameters to make up the whole IPv4 device path node.
2275
2276 @param[in, out] Node Pointer to the IPv4 device path node.
2277 @param[in] Controller The controller handle.
2278 @param[in] LocalIp The local IPv4 address.
2279 @param[in] LocalPort The local port.
2280 @param[in] RemoteIp The remote IPv4 address.
2281 @param[in] RemotePort The remote port.
2282 @param[in] Protocol The protocol type in the IP header.
2283 @param[in] UseDefaultAddress Whether this instance is using default address or not.
2284
2285 **/
2286 VOID
2287 EFIAPI
2288 NetLibCreateIPv4DPathNode (
2289 IN OUT IPv4_DEVICE_PATH *Node,
2290 IN EFI_HANDLE Controller,
2291 IN IP4_ADDR LocalIp,
2292 IN UINT16 LocalPort,
2293 IN IP4_ADDR RemoteIp,
2294 IN UINT16 RemotePort,
2295 IN UINT16 Protocol,
2296 IN BOOLEAN UseDefaultAddress
2297 )
2298 {
2299 Node->Header.Type = MESSAGING_DEVICE_PATH;
2300 Node->Header.SubType = MSG_IPv4_DP;
2301 SetDevicePathNodeLength (&Node->Header, 19);
2302
2303 CopyMem (&Node->LocalIpAddress, &LocalIp, sizeof (EFI_IPv4_ADDRESS));
2304 CopyMem (&Node->RemoteIpAddress, &RemoteIp, sizeof (EFI_IPv4_ADDRESS));
2305
2306 Node->LocalPort = LocalPort;
2307 Node->RemotePort = RemotePort;
2308
2309 Node->Protocol = Protocol;
2310
2311 if (!UseDefaultAddress) {
2312 Node->StaticIpAddress = TRUE;
2313 } else {
2314 Node->StaticIpAddress = NetLibDefaultAddressIsStatic (Controller);
2315 }
2316 }
2317
2318 /**
2319 Create an IPv6 device path node.
2320
2321 The header type of IPv6 device path node is MESSAGING_DEVICE_PATH.
2322 The header subtype of IPv6 device path node is MSG_IPv6_DP.
2323 Get other info from parameters to make up the whole IPv6 device path node.
2324
2325 @param[in, out] Node Pointer to the IPv6 device path node.
2326 @param[in] Controller The controller handle.
2327 @param[in] LocalIp The local IPv6 address.
2328 @param[in] LocalPort The local port.
2329 @param[in] RemoteIp The remote IPv6 address.
2330 @param[in] RemotePort The remote port.
2331 @param[in] Protocol The protocol type in the IP header.
2332
2333 **/
2334 VOID
2335 EFIAPI
2336 NetLibCreateIPv6DPathNode (
2337 IN OUT IPv6_DEVICE_PATH *Node,
2338 IN EFI_HANDLE Controller,
2339 IN EFI_IPv6_ADDRESS *LocalIp,
2340 IN UINT16 LocalPort,
2341 IN EFI_IPv6_ADDRESS *RemoteIp,
2342 IN UINT16 RemotePort,
2343 IN UINT16 Protocol
2344 )
2345 {
2346 Node->Header.Type = MESSAGING_DEVICE_PATH;
2347 Node->Header.SubType = MSG_IPv6_DP;
2348 SetDevicePathNodeLength (&Node->Header, sizeof (IPv6_DEVICE_PATH));
2349
2350 CopyMem (&Node->LocalIpAddress, LocalIp, sizeof (EFI_IPv6_ADDRESS));
2351 CopyMem (&Node->RemoteIpAddress, RemoteIp, sizeof (EFI_IPv6_ADDRESS));
2352
2353 Node->LocalPort = LocalPort;
2354 Node->RemotePort = RemotePort;
2355
2356 Node->Protocol = Protocol;
2357 Node->StaticIpAddress = FALSE;
2358 }
2359
2360 /**
2361 Find the UNDI/SNP handle from controller and protocol GUID.
2362
2363 For example, IP will open a MNP child to transmit/receive
2364 packets, when MNP is stopped, IP should also be stopped. IP
2365 needs to find its own private data which is related the IP's
2366 service binding instance that is install on UNDI/SNP handle.
2367 Now, the controller is either a MNP or ARP child handle. But
2368 IP opens these handle BY_DRIVER, use that info, we can get the
2369 UNDI/SNP handle.
2370
2371 @param[in] Controller Then protocol handle to check.
2372 @param[in] ProtocolGuid The protocol that is related with the handle.
2373
2374 @return The UNDI/SNP handle or NULL for errors.
2375
2376 **/
2377 EFI_HANDLE
2378 EFIAPI
2379 NetLibGetNicHandle (
2380 IN EFI_HANDLE Controller,
2381 IN EFI_GUID *ProtocolGuid
2382 )
2383 {
2384 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer;
2385 EFI_HANDLE Handle;
2386 EFI_STATUS Status;
2387 UINTN OpenCount;
2388 UINTN Index;
2389
2390 Status = gBS->OpenProtocolInformation (
2391 Controller,
2392 ProtocolGuid,
2393 &OpenBuffer,
2394 &OpenCount
2395 );
2396
2397 if (EFI_ERROR (Status)) {
2398 return NULL;
2399 }
2400
2401 Handle = NULL;
2402
2403 for (Index = 0; Index < OpenCount; Index++) {
2404 if ((OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {
2405 Handle = OpenBuffer[Index].ControllerHandle;
2406 break;
2407 }
2408 }
2409
2410 gBS->FreePool (OpenBuffer);
2411 return Handle;
2412 }