]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
MdeModulePkg: Remove trailing white space
[mirror_edk2.git] / MdeModulePkg / Library / DxeNetLib / DxeNetLib.c
1 /** @file
2 Network library.
3
4 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 **/
14
15 #include <Uefi.h>
16
17 #include <IndustryStandard/SmBios.h>
18
19 #include <Protocol/DriverBinding.h>
20 #include <Protocol/ServiceBinding.h>
21 #include <Protocol/SimpleNetwork.h>
22 #include <Protocol/AdapterInformation.h>
23 #include <Protocol/ManagedNetwork.h>
24 #include <Protocol/Ip4Config2.h>
25 #include <Protocol/ComponentName.h>
26 #include <Protocol/ComponentName2.h>
27
28 #include <Guid/SmBios.h>
29
30 #include <Library/NetLib.h>
31 #include <Library/BaseLib.h>
32 #include <Library/DebugLib.h>
33 #include <Library/BaseMemoryLib.h>
34 #include <Library/UefiBootServicesTableLib.h>
35 #include <Library/UefiRuntimeServicesTableLib.h>
36 #include <Library/MemoryAllocationLib.h>
37 #include <Library/DevicePathLib.h>
38 #include <Library/PrintLib.h>
39 #include <Library/UefiLib.h>
40
41 #define NIC_ITEM_CONFIG_SIZE (sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE)
42 #define DEFAULT_ZERO_START ((UINTN) ~0)
43
44 //
45 // All the supported IP4 maskes in host byte order.
46 //
47 GLOBAL_REMOVE_IF_UNREFERENCED IP4_ADDR gIp4AllMasks[IP4_MASK_NUM] = {
48 0x00000000,
49 0x80000000,
50 0xC0000000,
51 0xE0000000,
52 0xF0000000,
53 0xF8000000,
54 0xFC000000,
55 0xFE000000,
56
57 0xFF000000,
58 0xFF800000,
59 0xFFC00000,
60 0xFFE00000,
61 0xFFF00000,
62 0xFFF80000,
63 0xFFFC0000,
64 0xFFFE0000,
65
66 0xFFFF0000,
67 0xFFFF8000,
68 0xFFFFC000,
69 0xFFFFE000,
70 0xFFFFF000,
71 0xFFFFF800,
72 0xFFFFFC00,
73 0xFFFFFE00,
74
75 0xFFFFFF00,
76 0xFFFFFF80,
77 0xFFFFFFC0,
78 0xFFFFFFE0,
79 0xFFFFFFF0,
80 0xFFFFFFF8,
81 0xFFFFFFFC,
82 0xFFFFFFFE,
83 0xFFFFFFFF,
84 };
85
86 GLOBAL_REMOVE_IF_UNREFERENCED EFI_IPv4_ADDRESS mZeroIp4Addr = {{0, 0, 0, 0}};
87
88 //
89 // Any error level digitally larger than mNetDebugLevelMax
90 // will be silently discarded.
91 //
92 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mNetDebugLevelMax = NETDEBUG_LEVEL_ERROR;
93 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogPacketSeq = 0xDEADBEEF;
94
95 //
96 // You can change mSyslogDstMac mSyslogDstIp and mSyslogSrcIp
97 // here to direct the syslog packets to the syslog deamon. The
98 // default is broadcast to both the ethernet and IP.
99 //
100 GLOBAL_REMOVE_IF_UNREFERENCED UINT8 mSyslogDstMac[NET_ETHER_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
101 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogDstIp = 0xffffffff;
102 GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogSrcIp = 0;
103
104 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 *mMonthName[] = {
105 "Jan",
106 "Feb",
107 "Mar",
108 "Apr",
109 "May",
110 "Jun",
111 "Jul",
112 "Aug",
113 "Sep",
114 "Oct",
115 "Nov",
116 "Dec"
117 };
118
119 //
120 // VLAN device path node template
121 //
122 GLOBAL_REMOVE_IF_UNREFERENCED VLAN_DEVICE_PATH mNetVlanDevicePathTemplate = {
123 {
124 MESSAGING_DEVICE_PATH,
125 MSG_VLAN_DP,
126 {
127 (UINT8) (sizeof (VLAN_DEVICE_PATH)),
128 (UINT8) ((sizeof (VLAN_DEVICE_PATH)) >> 8)
129 }
130 },
131 0
132 };
133
134 /**
135 Locate the handles that support SNP, then open one of them
136 to send the syslog packets. The caller isn't required to close
137 the SNP after use because the SNP is opened by HandleProtocol.
138
139 @return The point to SNP if one is properly openned. Otherwise NULL
140
141 **/
142 EFI_SIMPLE_NETWORK_PROTOCOL *
143 SyslogLocateSnp (
144 VOID
145 )
146 {
147 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
148 EFI_STATUS Status;
149 EFI_HANDLE *Handles;
150 UINTN HandleCount;
151 UINTN Index;
152
153 //
154 // Locate the handles which has SNP installed.
155 //
156 Handles = NULL;
157 Status = gBS->LocateHandleBuffer (
158 ByProtocol,
159 &gEfiSimpleNetworkProtocolGuid,
160 NULL,
161 &HandleCount,
162 &Handles
163 );
164
165 if (EFI_ERROR (Status) || (HandleCount == 0)) {
166 return NULL;
167 }
168
169 //
170 // Try to open one of the ethernet SNP protocol to send packet
171 //
172 Snp = NULL;
173
174 for (Index = 0; Index < HandleCount; Index++) {
175 Status = gBS->HandleProtocol (
176 Handles[Index],
177 &gEfiSimpleNetworkProtocolGuid,
178 (VOID **) &Snp
179 );
180
181 if ((Status == EFI_SUCCESS) && (Snp != NULL) &&
182 (Snp->Mode->IfType == NET_IFTYPE_ETHERNET) &&
183 (Snp->Mode->MaxPacketSize >= NET_SYSLOG_PACKET_LEN)) {
184
185 break;
186 }
187
188 Snp = NULL;
189 }
190
191 FreePool (Handles);
192 return Snp;
193 }
194
195 /**
196 Transmit a syslog packet synchronously through SNP. The Packet
197 already has the ethernet header prepended. This function should
198 fill in the source MAC because it will try to locate a SNP each
199 time it is called to avoid the problem if SNP is unloaded.
200 This code snip is copied from MNP.
201 If Packet is NULL, then ASSERT().
202
203 @param[in] Packet The Syslog packet
204 @param[in] Length The length of the packet
205
206 @retval EFI_DEVICE_ERROR Failed to locate a usable SNP protocol
207 @retval EFI_TIMEOUT Timeout happened to send the packet.
208 @retval EFI_SUCCESS Packet is sent.
209
210 **/
211 EFI_STATUS
212 SyslogSendPacket (
213 IN CHAR8 *Packet,
214 IN UINT32 Length
215 )
216 {
217 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
218 ETHER_HEAD *Ether;
219 EFI_STATUS Status;
220 EFI_EVENT TimeoutEvent;
221 UINT8 *TxBuf;
222
223 ASSERT (Packet != NULL);
224
225 Snp = SyslogLocateSnp ();
226
227 if (Snp == NULL) {
228 return EFI_DEVICE_ERROR;
229 }
230
231 Ether = (ETHER_HEAD *) Packet;
232 CopyMem (Ether->SrcMac, Snp->Mode->CurrentAddress.Addr, NET_ETHER_ADDR_LEN);
233
234 //
235 // Start the timeout event.
236 //
237 Status = gBS->CreateEvent (
238 EVT_TIMER,
239 TPL_NOTIFY,
240 NULL,
241 NULL,
242 &TimeoutEvent
243 );
244
245 if (EFI_ERROR (Status)) {
246 return Status;
247 }
248
249 Status = gBS->SetTimer (TimeoutEvent, TimerRelative, NET_SYSLOG_TX_TIMEOUT);
250
251 if (EFI_ERROR (Status)) {
252 goto ON_EXIT;
253 }
254
255 for (;;) {
256 //
257 // Transmit the packet through SNP.
258 //
259 Status = Snp->Transmit (Snp, 0, Length, Packet, NULL, NULL, NULL);
260
261 if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_READY)) {
262 Status = EFI_DEVICE_ERROR;
263 break;
264 }
265
266 //
267 // If Status is EFI_SUCCESS, the packet is put in the transmit queue.
268 // if Status is EFI_NOT_READY, the transmit engine of the network
269 // interface is busy. Both need to sync SNP.
270 //
271 TxBuf = NULL;
272
273 do {
274 //
275 // Get the recycled transmit buffer status.
276 //
277 Snp->GetStatus (Snp, NULL, (VOID **) &TxBuf);
278
279 if (!EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) {
280 Status = EFI_TIMEOUT;
281 break;
282 }
283
284 } while (TxBuf == NULL);
285
286 if ((Status == EFI_SUCCESS) || (Status == EFI_TIMEOUT)) {
287 break;
288 }
289
290 //
291 // Status is EFI_NOT_READY. Restart the timer event and
292 // call Snp->Transmit again.
293 //
294 gBS->SetTimer (TimeoutEvent, TimerRelative, NET_SYSLOG_TX_TIMEOUT);
295 }
296
297 gBS->SetTimer (TimeoutEvent, TimerCancel, 0);
298
299 ON_EXIT:
300 gBS->CloseEvent (TimeoutEvent);
301 return Status;
302 }
303
304 /**
305 Build a syslog packet, including the Ethernet/Ip/Udp headers
306 and user's message.
307
308 @param[in] Level Syslog severity level
309 @param[in] Module The module that generates the log
310 @param[in] File The file that contains the current log
311 @param[in] Line The line of code in the File that contains the current log
312 @param[in] Message The log message
313 @param[in] BufLen The lenght of the Buf
314 @param[out] Buf The buffer to put the packet data
315
316 @return The length of the syslog packet built, 0 represents no packet is built.
317
318 **/
319 UINT32
320 SyslogBuildPacket (
321 IN UINT32 Level,
322 IN UINT8 *Module,
323 IN UINT8 *File,
324 IN UINT32 Line,
325 IN UINT8 *Message,
326 IN UINT32 BufLen,
327 OUT CHAR8 *Buf
328 )
329 {
330 EFI_STATUS Status;
331 ETHER_HEAD *Ether;
332 IP4_HEAD *Ip4;
333 EFI_UDP_HEADER *Udp4;
334 EFI_TIME Time;
335 UINT32 Pri;
336 UINT32 Len;
337
338 //
339 // Fill in the Ethernet header. Leave alone the source MAC.
340 // SyslogSendPacket will fill in the address for us.
341 //
342 Ether = (ETHER_HEAD *) Buf;
343 CopyMem (Ether->DstMac, mSyslogDstMac, NET_ETHER_ADDR_LEN);
344 ZeroMem (Ether->SrcMac, NET_ETHER_ADDR_LEN);
345
346 Ether->EtherType = HTONS (0x0800); // IPv4 protocol
347
348 Buf += sizeof (ETHER_HEAD);
349 BufLen -= sizeof (ETHER_HEAD);
350
351 //
352 // Fill in the IP header
353 //
354 Ip4 = (IP4_HEAD *) Buf;
355 Ip4->HeadLen = 5;
356 Ip4->Ver = 4;
357 Ip4->Tos = 0;
358 Ip4->TotalLen = 0;
359 Ip4->Id = (UINT16) mSyslogPacketSeq;
360 Ip4->Fragment = 0;
361 Ip4->Ttl = 16;
362 Ip4->Protocol = 0x11;
363 Ip4->Checksum = 0;
364 Ip4->Src = mSyslogSrcIp;
365 Ip4->Dst = mSyslogDstIp;
366
367 Buf += sizeof (IP4_HEAD);
368 BufLen -= sizeof (IP4_HEAD);
369
370 //
371 // Fill in the UDP header, Udp checksum is optional. Leave it zero.
372 //
373 Udp4 = (EFI_UDP_HEADER *) Buf;
374 Udp4->SrcPort = HTONS (514);
375 Udp4->DstPort = HTONS (514);
376 Udp4->Length = 0;
377 Udp4->Checksum = 0;
378
379 Buf += sizeof (EFI_UDP_HEADER);
380 BufLen -= sizeof (EFI_UDP_HEADER);
381
382 //
383 // Build the syslog message body with <PRI> Timestamp machine module Message
384 //
385 Pri = ((NET_SYSLOG_FACILITY & 31) << 3) | (Level & 7);
386 Status = gRT->GetTime (&Time, NULL);
387 if (EFI_ERROR (Status)) {
388 return 0;
389 }
390
391 //
392 // Use %a to format the ASCII strings, %s to format UNICODE strings
393 //
394 Len = 0;
395 Len += (UINT32) AsciiSPrint (
396 Buf,
397 BufLen,
398 "<%d> %a %d %d:%d:%d ",
399 Pri,
400 mMonthName [Time.Month-1],
401 Time.Day,
402 Time.Hour,
403 Time.Minute,
404 Time.Second
405 );
406
407 Len += (UINT32) AsciiSPrint (
408 Buf + Len,
409 BufLen - Len,
410 "Tiano %a: %a (Line: %d File: %a)",
411 Module,
412 Message,
413 Line,
414 File
415 );
416 Len ++;
417
418 //
419 // OK, patch the IP length/checksum and UDP length fields.
420 //
421 Len += sizeof (EFI_UDP_HEADER);
422 Udp4->Length = HTONS ((UINT16) Len);
423
424 Len += sizeof (IP4_HEAD);
425 Ip4->TotalLen = HTONS ((UINT16) Len);
426 Ip4->Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Ip4, sizeof (IP4_HEAD)));
427
428 return Len + sizeof (ETHER_HEAD);
429 }
430
431 /**
432 Allocate a buffer, then format the message to it. This is a
433 help function for the NET_DEBUG_XXX macros. The PrintArg of
434 these macros treats the variable length print parameters as a
435 single parameter, and pass it to the NetDebugASPrint. For
436 example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name))
437 if extracted to:
438
439 NetDebugOutput (
440 NETDEBUG_LEVEL_TRACE,
441 "Tcp",
442 __FILE__,
443 __LINE__,
444 NetDebugASPrint ("State transit to %a\n", Name)
445 )
446
447 If Format is NULL, then ASSERT().
448
449 @param Format The ASCII format string.
450 @param ... The variable length parameter whose format is determined
451 by the Format string.
452
453 @return The buffer containing the formatted message,
454 or NULL if failed to allocate memory.
455
456 **/
457 CHAR8 *
458 EFIAPI
459 NetDebugASPrint (
460 IN CHAR8 *Format,
461 ...
462 )
463 {
464 VA_LIST Marker;
465 CHAR8 *Buf;
466
467 ASSERT (Format != NULL);
468
469 Buf = (CHAR8 *) AllocatePool (NET_DEBUG_MSG_LEN);
470
471 if (Buf == NULL) {
472 return NULL;
473 }
474
475 VA_START (Marker, Format);
476 AsciiVSPrint (Buf, NET_DEBUG_MSG_LEN, Format, Marker);
477 VA_END (Marker);
478
479 return Buf;
480 }
481
482 /**
483 Builds an UDP4 syslog packet and send it using SNP.
484
485 This function will locate a instance of SNP then send the message through it.
486 Because it isn't open the SNP BY_DRIVER, apply caution when using it.
487
488 @param Level The severity level of the message.
489 @param Module The Moudle that generates the log.
490 @param File The file that contains the log.
491 @param Line The exact line that contains the log.
492 @param Message The user message to log.
493
494 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
495 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet.
496 @retval EFI_DEVICE_ERROR Device error occurs.
497 @retval EFI_SUCCESS The log is discard because that it is more verbose
498 than the mNetDebugLevelMax. Or, it has been sent out.
499 **/
500 EFI_STATUS
501 EFIAPI
502 NetDebugOutput (
503 IN UINT32 Level,
504 IN UINT8 *Module,
505 IN UINT8 *File,
506 IN UINT32 Line,
507 IN UINT8 *Message
508 )
509 {
510 CHAR8 *Packet;
511 UINT32 Len;
512 EFI_STATUS Status;
513
514 //
515 // Check whether the message should be sent out
516 //
517 if (Message == NULL || File == NULL || Module == NULL) {
518 return EFI_INVALID_PARAMETER;
519 }
520
521 if (Level > mNetDebugLevelMax) {
522 Status = EFI_SUCCESS;
523 goto ON_EXIT;
524 }
525
526 //
527 // Allocate a maxium of 1024 bytes, the caller should ensure
528 // that the message plus the ethernet/ip/udp header is shorter
529 // than this
530 //
531 Packet = (CHAR8 *) AllocatePool (NET_SYSLOG_PACKET_LEN);
532
533 if (Packet == NULL) {
534 Status = EFI_OUT_OF_RESOURCES;
535 goto ON_EXIT;
536 }
537
538 //
539 // Build the message: Ethernet header + IP header + Udp Header + user data
540 //
541 Len = SyslogBuildPacket (
542 Level,
543 Module,
544 File,
545 Line,
546 Message,
547 NET_SYSLOG_PACKET_LEN,
548 Packet
549 );
550 if (Len == 0) {
551 Status = EFI_DEVICE_ERROR;
552 } else {
553 mSyslogPacketSeq++;
554 Status = SyslogSendPacket (Packet, Len);
555 }
556
557 FreePool (Packet);
558
559 ON_EXIT:
560 FreePool (Message);
561 return Status;
562 }
563 /**
564 Return the length of the mask.
565
566 Return the length of the mask, the correct value is from 0 to 32.
567 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.
568 NetMask is in the host byte order.
569
570 @param[in] NetMask The netmask to get the length from.
571
572 @return The length of the netmask, IP4_MASK_NUM if the mask is invalid.
573
574 **/
575 INTN
576 EFIAPI
577 NetGetMaskLength (
578 IN IP4_ADDR NetMask
579 )
580 {
581 INTN Index;
582
583 for (Index = 0; Index <= IP4_MASK_MAX; Index++) {
584 if (NetMask == gIp4AllMasks[Index]) {
585 break;
586 }
587 }
588
589 return Index;
590 }
591
592
593
594 /**
595 Return the class of the IP address, such as class A, B, C.
596 Addr is in host byte order.
597
598 [ATTENTION]
599 Classful addressing (IP class A/B/C) has been deprecated according to RFC4632.
600 Caller of this function could only check the returned value against
601 IP4_ADDR_CLASSD (multicast) or IP4_ADDR_CLASSE (reserved) now.
602
603 The address of class A starts with 0.
604 If the address belong to class A, return IP4_ADDR_CLASSA.
605 The address of class B starts with 10.
606 If the address belong to class B, return IP4_ADDR_CLASSB.
607 The address of class C starts with 110.
608 If the address belong to class C, return IP4_ADDR_CLASSC.
609 The address of class D starts with 1110.
610 If the address belong to class D, return IP4_ADDR_CLASSD.
611 The address of class E starts with 1111.
612 If the address belong to class E, return IP4_ADDR_CLASSE.
613
614
615 @param[in] Addr The address to get the class from.
616
617 @return IP address class, such as IP4_ADDR_CLASSA.
618
619 **/
620 INTN
621 EFIAPI
622 NetGetIpClass (
623 IN IP4_ADDR Addr
624 )
625 {
626 UINT8 ByteOne;
627
628 ByteOne = (UINT8) (Addr >> 24);
629
630 if ((ByteOne & 0x80) == 0) {
631 return IP4_ADDR_CLASSA;
632
633 } else if ((ByteOne & 0xC0) == 0x80) {
634 return IP4_ADDR_CLASSB;
635
636 } else if ((ByteOne & 0xE0) == 0xC0) {
637 return IP4_ADDR_CLASSC;
638
639 } else if ((ByteOne & 0xF0) == 0xE0) {
640 return IP4_ADDR_CLASSD;
641
642 } else {
643 return IP4_ADDR_CLASSE;
644
645 }
646 }
647
648
649 /**
650 Check whether the IP is a valid unicast address according to
651 the netmask.
652
653 ASSERT if NetMask is zero.
654
655 If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address,
656 except when the originator is one of the endpoints of a point-to-point link with a 31-bit
657 mask (RFC3021), or a 32bit NetMask (all 0xFF) is used for special network environment (e.g.
658 PPP link).
659
660 @param[in] Ip The IP to check against.
661 @param[in] NetMask The mask of the IP.
662
663 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.
664
665 **/
666 BOOLEAN
667 EFIAPI
668 NetIp4IsUnicast (
669 IN IP4_ADDR Ip,
670 IN IP4_ADDR NetMask
671 )
672 {
673 INTN MaskLength;
674
675 ASSERT (NetMask != 0);
676
677 if (Ip == 0 || IP4_IS_LOCAL_BROADCAST (Ip)) {
678 return FALSE;
679 }
680
681 MaskLength = NetGetMaskLength (NetMask);
682 ASSERT ((MaskLength >= 0) && (MaskLength <= IP4_MASK_NUM));
683 if (MaskLength < 31) {
684 if (((Ip &~NetMask) == ~NetMask) || ((Ip &~NetMask) == 0)) {
685 return FALSE;
686 }
687 }
688
689 return TRUE;
690 }
691
692 /**
693 Check whether the incoming IPv6 address is a valid unicast address.
694
695 ASSERT if Ip6 is NULL.
696
697 If the address is a multicast address has binary 0xFF at the start, it is not
698 a valid unicast address. If the address is unspecified ::, it is not a valid
699 unicast address to be assigned to any node. If the address is loopback address
700 ::1, it is also not a valid unicast address to be assigned to any physical
701 interface.
702
703 @param[in] Ip6 The IPv6 address to check against.
704
705 @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE.
706
707 **/
708 BOOLEAN
709 EFIAPI
710 NetIp6IsValidUnicast (
711 IN EFI_IPv6_ADDRESS *Ip6
712 )
713 {
714 UINT8 Byte;
715 UINT8 Index;
716
717 ASSERT (Ip6 != NULL);
718
719 if (Ip6->Addr[0] == 0xFF) {
720 return FALSE;
721 }
722
723 for (Index = 0; Index < 15; Index++) {
724 if (Ip6->Addr[Index] != 0) {
725 return TRUE;
726 }
727 }
728
729 Byte = Ip6->Addr[Index];
730
731 if (Byte == 0x0 || Byte == 0x1) {
732 return FALSE;
733 }
734
735 return TRUE;
736 }
737
738 /**
739 Check whether the incoming Ipv6 address is the unspecified address or not.
740
741 ASSERT if Ip6 is NULL.
742
743 @param[in] Ip6 - Ip6 address, in network order.
744
745 @retval TRUE - Yes, unspecified
746 @retval FALSE - No
747
748 **/
749 BOOLEAN
750 EFIAPI
751 NetIp6IsUnspecifiedAddr (
752 IN EFI_IPv6_ADDRESS *Ip6
753 )
754 {
755 UINT8 Index;
756
757 ASSERT (Ip6 != NULL);
758
759 for (Index = 0; Index < 16; Index++) {
760 if (Ip6->Addr[Index] != 0) {
761 return FALSE;
762 }
763 }
764
765 return TRUE;
766 }
767
768 /**
769 Check whether the incoming Ipv6 address is a link-local address.
770
771 ASSERT if Ip6 is NULL.
772
773 @param[in] Ip6 - Ip6 address, in network order.
774
775 @retval TRUE - Yes, link-local address
776 @retval FALSE - No
777
778 **/
779 BOOLEAN
780 EFIAPI
781 NetIp6IsLinkLocalAddr (
782 IN EFI_IPv6_ADDRESS *Ip6
783 )
784 {
785 UINT8 Index;
786
787 ASSERT (Ip6 != NULL);
788
789 if (Ip6->Addr[0] != 0xFE) {
790 return FALSE;
791 }
792
793 if (Ip6->Addr[1] != 0x80) {
794 return FALSE;
795 }
796
797 for (Index = 2; Index < 8; Index++) {
798 if (Ip6->Addr[Index] != 0) {
799 return FALSE;
800 }
801 }
802
803 return TRUE;
804 }
805
806 /**
807 Check whether the Ipv6 address1 and address2 are on the connected network.
808
809 ASSERT if Ip1 or Ip2 is NULL.
810 ASSERT if PrefixLength exceeds or equals to IP6_PREFIX_MAX.
811
812 @param[in] Ip1 - Ip6 address1, in network order.
813 @param[in] Ip2 - Ip6 address2, in network order.
814 @param[in] PrefixLength - The prefix length of the checking net.
815
816 @retval TRUE - Yes, connected.
817 @retval FALSE - No.
818
819 **/
820 BOOLEAN
821 EFIAPI
822 NetIp6IsNetEqual (
823 EFI_IPv6_ADDRESS *Ip1,
824 EFI_IPv6_ADDRESS *Ip2,
825 UINT8 PrefixLength
826 )
827 {
828 UINT8 Byte;
829 UINT8 Bit;
830 UINT8 Mask;
831
832 ASSERT ((Ip1 != NULL) && (Ip2 != NULL) && (PrefixLength < IP6_PREFIX_MAX));
833
834 if (PrefixLength == 0) {
835 return TRUE;
836 }
837
838 Byte = (UINT8) (PrefixLength / 8);
839 Bit = (UINT8) (PrefixLength % 8);
840
841 if (CompareMem (Ip1, Ip2, Byte) != 0) {
842 return FALSE;
843 }
844
845 if (Bit > 0) {
846 Mask = (UINT8) (0xFF << (8 - Bit));
847
848 ASSERT (Byte < 16);
849 if (Byte >= 16) {
850 return FALSE;
851 }
852 if ((Ip1->Addr[Byte] & Mask) != (Ip2->Addr[Byte] & Mask)) {
853 return FALSE;
854 }
855 }
856
857 return TRUE;
858 }
859
860
861 /**
862 Switches the endianess of an IPv6 address
863
864 ASSERT if Ip6 is NULL.
865
866 This function swaps the bytes in a 128-bit IPv6 address to switch the value
867 from little endian to big endian or vice versa. The byte swapped value is
868 returned.
869
870 @param Ip6 Points to an IPv6 address
871
872 @return The byte swapped IPv6 address.
873
874 **/
875 EFI_IPv6_ADDRESS *
876 EFIAPI
877 Ip6Swap128 (
878 EFI_IPv6_ADDRESS *Ip6
879 )
880 {
881 UINT64 High;
882 UINT64 Low;
883
884 ASSERT (Ip6 != NULL);
885
886 CopyMem (&High, Ip6, sizeof (UINT64));
887 CopyMem (&Low, &Ip6->Addr[8], sizeof (UINT64));
888
889 High = SwapBytes64 (High);
890 Low = SwapBytes64 (Low);
891
892 CopyMem (Ip6, &Low, sizeof (UINT64));
893 CopyMem (&Ip6->Addr[8], &High, sizeof (UINT64));
894
895 return Ip6;
896 }
897
898 /**
899 Initialize a random seed using current time and monotonic count.
900
901 Get current time and monotonic count first. Then initialize a random seed
902 based on some basic mathematics operation on the hour, day, minute, second,
903 nanosecond and year of the current time and the monotonic count value.
904
905 @return The random seed initialized with current time.
906
907 **/
908 UINT32
909 EFIAPI
910 NetRandomInitSeed (
911 VOID
912 )
913 {
914 EFI_TIME Time;
915 UINT32 Seed;
916 UINT64 MonotonicCount;
917
918 gRT->GetTime (&Time, NULL);
919 Seed = (Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second);
920 Seed ^= Time.Nanosecond;
921 Seed ^= Time.Year << 7;
922
923 gBS->GetNextMonotonicCount (&MonotonicCount);
924 Seed += (UINT32) MonotonicCount;
925
926 return Seed;
927 }
928
929
930 /**
931 Extract a UINT32 from a byte stream.
932
933 ASSERT if Buf is NULL.
934
935 Copy a UINT32 from a byte stream, then converts it from Network
936 byte order to host byte order. Use this function to avoid alignment error.
937
938 @param[in] Buf The buffer to extract the UINT32.
939
940 @return The UINT32 extracted.
941
942 **/
943 UINT32
944 EFIAPI
945 NetGetUint32 (
946 IN UINT8 *Buf
947 )
948 {
949 UINT32 Value;
950
951 ASSERT (Buf != NULL);
952
953 CopyMem (&Value, Buf, sizeof (UINT32));
954 return NTOHL (Value);
955 }
956
957
958 /**
959 Put a UINT32 to the byte stream in network byte order.
960
961 ASSERT if Buf is NULL.
962
963 Converts a UINT32 from host byte order to network byte order. Then copy it to the
964 byte stream.
965
966 @param[in, out] Buf The buffer to put the UINT32.
967 @param[in] Data The data to be converted and put into the byte stream.
968
969 **/
970 VOID
971 EFIAPI
972 NetPutUint32 (
973 IN OUT UINT8 *Buf,
974 IN UINT32 Data
975 )
976 {
977 ASSERT (Buf != NULL);
978
979 Data = HTONL (Data);
980 CopyMem (Buf, &Data, sizeof (UINT32));
981 }
982
983
984 /**
985 Remove the first node entry on the list, and return the removed node entry.
986
987 Removes the first node Entry from a doubly linked list. It is up to the caller of
988 this function to release the memory used by the first node if that is required. On
989 exit, the removed node is returned.
990
991 If Head is NULL, then ASSERT().
992 If Head was not initialized, then ASSERT().
993 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
994 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
995 then ASSERT().
996
997 @param[in, out] Head The list header.
998
999 @return The first node entry that is removed from the list, NULL if the list is empty.
1000
1001 **/
1002 LIST_ENTRY *
1003 EFIAPI
1004 NetListRemoveHead (
1005 IN OUT LIST_ENTRY *Head
1006 )
1007 {
1008 LIST_ENTRY *First;
1009
1010 ASSERT (Head != NULL);
1011
1012 if (IsListEmpty (Head)) {
1013 return NULL;
1014 }
1015
1016 First = Head->ForwardLink;
1017 Head->ForwardLink = First->ForwardLink;
1018 First->ForwardLink->BackLink = Head;
1019
1020 DEBUG_CODE (
1021 First->ForwardLink = (LIST_ENTRY *) NULL;
1022 First->BackLink = (LIST_ENTRY *) NULL;
1023 );
1024
1025 return First;
1026 }
1027
1028
1029 /**
1030 Remove the last node entry on the list and and return the removed node entry.
1031
1032 Removes the last node entry from a doubly linked list. It is up to the caller of
1033 this function to release the memory used by the first node if that is required. On
1034 exit, the removed node is returned.
1035
1036 If Head is NULL, then ASSERT().
1037 If Head was not initialized, then ASSERT().
1038 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
1039 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
1040 then ASSERT().
1041
1042 @param[in, out] Head The list head.
1043
1044 @return The last node entry that is removed from the list, NULL if the list is empty.
1045
1046 **/
1047 LIST_ENTRY *
1048 EFIAPI
1049 NetListRemoveTail (
1050 IN OUT LIST_ENTRY *Head
1051 )
1052 {
1053 LIST_ENTRY *Last;
1054
1055 ASSERT (Head != NULL);
1056
1057 if (IsListEmpty (Head)) {
1058 return NULL;
1059 }
1060
1061 Last = Head->BackLink;
1062 Head->BackLink = Last->BackLink;
1063 Last->BackLink->ForwardLink = Head;
1064
1065 DEBUG_CODE (
1066 Last->ForwardLink = (LIST_ENTRY *) NULL;
1067 Last->BackLink = (LIST_ENTRY *) NULL;
1068 );
1069
1070 return Last;
1071 }
1072
1073
1074 /**
1075 Insert a new node entry after a designated node entry of a doubly linked list.
1076
1077 ASSERT if PrevEntry or NewEntry is NULL.
1078
1079 Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry
1080 of the doubly linked list.
1081
1082 @param[in, out] PrevEntry The previous entry to insert after.
1083 @param[in, out] NewEntry The new entry to insert.
1084
1085 **/
1086 VOID
1087 EFIAPI
1088 NetListInsertAfter (
1089 IN OUT LIST_ENTRY *PrevEntry,
1090 IN OUT LIST_ENTRY *NewEntry
1091 )
1092 {
1093 ASSERT (PrevEntry != NULL && NewEntry != NULL);
1094
1095 NewEntry->BackLink = PrevEntry;
1096 NewEntry->ForwardLink = PrevEntry->ForwardLink;
1097 PrevEntry->ForwardLink->BackLink = NewEntry;
1098 PrevEntry->ForwardLink = NewEntry;
1099 }
1100
1101
1102 /**
1103 Insert a new node entry before a designated node entry of a doubly linked list.
1104
1105 ASSERT if PostEntry or NewEntry is NULL.
1106
1107 Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry
1108 of the doubly linked list.
1109
1110 @param[in, out] PostEntry The entry to insert before.
1111 @param[in, out] NewEntry The new entry to insert.
1112
1113 **/
1114 VOID
1115 EFIAPI
1116 NetListInsertBefore (
1117 IN OUT LIST_ENTRY *PostEntry,
1118 IN OUT LIST_ENTRY *NewEntry
1119 )
1120 {
1121 ASSERT (PostEntry != NULL && NewEntry != NULL);
1122
1123 NewEntry->ForwardLink = PostEntry;
1124 NewEntry->BackLink = PostEntry->BackLink;
1125 PostEntry->BackLink->ForwardLink = NewEntry;
1126 PostEntry->BackLink = NewEntry;
1127 }
1128
1129 /**
1130 Safe destroy nodes in a linked list, and return the length of the list after all possible operations finished.
1131
1132 Destroy network child instance list by list traversals is not safe due to graph dependencies between nodes.
1133 This function performs a safe traversal to destroy these nodes by checking to see if the node being destroyed
1134 has been removed from the list or not.
1135 If it has been removed, then restart the traversal from the head.
1136 If it hasn't been removed, then continue with the next node directly.
1137 This function will end the iterate and return the CallBack's last return value if error happens,
1138 or retrun EFI_SUCCESS if 2 complete passes are made with no changes in the number of children in the list.
1139
1140 @param[in] List The head of the list.
1141 @param[in] CallBack Pointer to the callback function to destroy one node in the list.
1142 @param[in] Context Pointer to the callback function's context: corresponds to the
1143 parameter Context in NET_DESTROY_LINK_LIST_CALLBACK.
1144 @param[out] ListLength The length of the link list if the function returns successfully.
1145
1146 @retval EFI_SUCCESS Two complete passes are made with no changes in the number of children.
1147 @retval EFI_INVALID_PARAMETER The input parameter is invalid.
1148 @retval Others Return the CallBack's last return value.
1149
1150 **/
1151 EFI_STATUS
1152 EFIAPI
1153 NetDestroyLinkList (
1154 IN LIST_ENTRY *List,
1155 IN NET_DESTROY_LINK_LIST_CALLBACK CallBack,
1156 IN VOID *Context, OPTIONAL
1157 OUT UINTN *ListLength OPTIONAL
1158 )
1159 {
1160 UINTN PreviousLength;
1161 LIST_ENTRY *Entry;
1162 LIST_ENTRY *Ptr;
1163 UINTN Length;
1164 EFI_STATUS Status;
1165
1166 if (List == NULL || CallBack == NULL) {
1167 return EFI_INVALID_PARAMETER;
1168 }
1169
1170 Length = 0;
1171 do {
1172 PreviousLength = Length;
1173 Entry = GetFirstNode (List);
1174 while (!IsNull (List, Entry)) {
1175 Status = CallBack (Entry, Context);
1176 if (EFI_ERROR (Status)) {
1177 return Status;
1178 }
1179 //
1180 // Walk through the list to see whether the Entry has been removed or not.
1181 // If the Entry still exists, just try to destroy the next one.
1182 // If not, go back to the start point to iterate the list again.
1183 //
1184 for (Ptr = List->ForwardLink; Ptr != List; Ptr = Ptr->ForwardLink) {
1185 if (Ptr == Entry) {
1186 break;
1187 }
1188 }
1189 if (Ptr == Entry) {
1190 Entry = GetNextNode (List, Entry);
1191 } else {
1192 Entry = GetFirstNode (List);
1193 }
1194 }
1195 for (Length = 0, Ptr = List->ForwardLink; Ptr != List; Length++, Ptr = Ptr->ForwardLink);
1196 } while (Length != PreviousLength);
1197
1198 if (ListLength != NULL) {
1199 *ListLength = Length;
1200 }
1201 return EFI_SUCCESS;
1202 }
1203
1204 /**
1205 This function checks the input Handle to see if it's one of these handles in ChildHandleBuffer.
1206
1207 @param[in] Handle Handle to be checked.
1208 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer.
1209 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
1210 if NumberOfChildren is 0.
1211
1212 @retval TRUE Found the input Handle in ChildHandleBuffer.
1213 @retval FALSE Can't find the input Handle in ChildHandleBuffer.
1214
1215 **/
1216 BOOLEAN
1217 EFIAPI
1218 NetIsInHandleBuffer (
1219 IN EFI_HANDLE Handle,
1220 IN UINTN NumberOfChildren,
1221 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
1222 )
1223 {
1224 UINTN Index;
1225
1226 if (NumberOfChildren == 0 || ChildHandleBuffer == NULL) {
1227 return FALSE;
1228 }
1229
1230 for (Index = 0; Index < NumberOfChildren; Index++) {
1231 if (Handle == ChildHandleBuffer[Index]) {
1232 return TRUE;
1233 }
1234 }
1235
1236 return FALSE;
1237 }
1238
1239
1240 /**
1241 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
1242
1243 Initialize the forward and backward links of two head nodes donated by Map->Used
1244 and Map->Recycled of two doubly linked lists.
1245 Initializes the count of the <Key, Value> pairs in the netmap to zero.
1246
1247 If Map is NULL, then ASSERT().
1248 If the address of Map->Used is NULL, then ASSERT().
1249 If the address of Map->Recycled is NULl, then ASSERT().
1250
1251 @param[in, out] Map The netmap to initialize.
1252
1253 **/
1254 VOID
1255 EFIAPI
1256 NetMapInit (
1257 IN OUT NET_MAP *Map
1258 )
1259 {
1260 ASSERT (Map != NULL);
1261
1262 InitializeListHead (&Map->Used);
1263 InitializeListHead (&Map->Recycled);
1264 Map->Count = 0;
1265 }
1266
1267
1268 /**
1269 To clean up the netmap, that is, release allocated memories.
1270
1271 Removes all nodes of the Used doubly linked list and free memory of all related netmap items.
1272 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.
1273 The number of the <Key, Value> pairs in the netmap is set to be zero.
1274
1275 If Map is NULL, then ASSERT().
1276
1277 @param[in, out] Map The netmap to clean up.
1278
1279 **/
1280 VOID
1281 EFIAPI
1282 NetMapClean (
1283 IN OUT NET_MAP *Map
1284 )
1285 {
1286 NET_MAP_ITEM *Item;
1287 LIST_ENTRY *Entry;
1288 LIST_ENTRY *Next;
1289
1290 ASSERT (Map != NULL);
1291
1292 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Used) {
1293 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
1294
1295 RemoveEntryList (&Item->Link);
1296 Map->Count--;
1297
1298 gBS->FreePool (Item);
1299 }
1300
1301 ASSERT ((Map->Count == 0) && IsListEmpty (&Map->Used));
1302
1303 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Recycled) {
1304 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
1305
1306 RemoveEntryList (&Item->Link);
1307 gBS->FreePool (Item);
1308 }
1309
1310 ASSERT (IsListEmpty (&Map->Recycled));
1311 }
1312
1313
1314 /**
1315 Test whether the netmap is empty and return true if it is.
1316
1317 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.
1318
1319 If Map is NULL, then ASSERT().
1320
1321 @param[in] Map The net map to test.
1322
1323 @return TRUE if the netmap is empty, otherwise FALSE.
1324
1325 **/
1326 BOOLEAN
1327 EFIAPI
1328 NetMapIsEmpty (
1329 IN NET_MAP *Map
1330 )
1331 {
1332 ASSERT (Map != NULL);
1333 return (BOOLEAN) (Map->Count == 0);
1334 }
1335
1336
1337 /**
1338 Return the number of the <Key, Value> pairs in the netmap.
1339
1340 If Map is NULL, then ASSERT().
1341
1342 @param[in] Map The netmap to get the entry number.
1343
1344 @return The entry number in the netmap.
1345
1346 **/
1347 UINTN
1348 EFIAPI
1349 NetMapGetCount (
1350 IN NET_MAP *Map
1351 )
1352 {
1353 ASSERT (Map != NULL);
1354 return Map->Count;
1355 }
1356
1357
1358 /**
1359 Return one allocated item.
1360
1361 If the Recycled doubly linked list of the netmap is empty, it will try to allocate
1362 a batch of items if there are enough resources and add corresponding nodes to the begining
1363 of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove
1364 the fist node entry of the Recycled doubly linked list and return the corresponding item.
1365
1366 If Map is NULL, then ASSERT().
1367
1368 @param[in, out] Map The netmap to allocate item for.
1369
1370 @return The allocated item. If NULL, the
1371 allocation failed due to resource limit.
1372
1373 **/
1374 NET_MAP_ITEM *
1375 NetMapAllocItem (
1376 IN OUT NET_MAP *Map
1377 )
1378 {
1379 NET_MAP_ITEM *Item;
1380 LIST_ENTRY *Head;
1381 UINTN Index;
1382
1383 ASSERT (Map != NULL);
1384
1385 Head = &Map->Recycled;
1386
1387 if (IsListEmpty (Head)) {
1388 for (Index = 0; Index < NET_MAP_INCREAMENT; Index++) {
1389 Item = AllocatePool (sizeof (NET_MAP_ITEM));
1390
1391 if (Item == NULL) {
1392 if (Index == 0) {
1393 return NULL;
1394 }
1395
1396 break;
1397 }
1398
1399 InsertHeadList (Head, &Item->Link);
1400 }
1401 }
1402
1403 Item = NET_LIST_HEAD (Head, NET_MAP_ITEM, Link);
1404 NetListRemoveHead (Head);
1405
1406 return Item;
1407 }
1408
1409
1410 /**
1411 Allocate an item to save the <Key, Value> pair to the head of the netmap.
1412
1413 Allocate an item to save the <Key, Value> pair and add corresponding node entry
1414 to the beginning of the Used doubly linked list. The number of the <Key, Value>
1415 pairs in the netmap increase by 1.
1416
1417 If Map is NULL, then ASSERT().
1418 If Key is NULL, then ASSERT().
1419
1420 @param[in, out] Map The netmap to insert into.
1421 @param[in] Key The user's key.
1422 @param[in] Value The user's value for the key.
1423
1424 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
1425 @retval EFI_SUCCESS The item is inserted to the head.
1426
1427 **/
1428 EFI_STATUS
1429 EFIAPI
1430 NetMapInsertHead (
1431 IN OUT NET_MAP *Map,
1432 IN VOID *Key,
1433 IN VOID *Value OPTIONAL
1434 )
1435 {
1436 NET_MAP_ITEM *Item;
1437
1438 ASSERT (Map != NULL && Key != NULL);
1439
1440 Item = NetMapAllocItem (Map);
1441
1442 if (Item == NULL) {
1443 return EFI_OUT_OF_RESOURCES;
1444 }
1445
1446 Item->Key = Key;
1447 Item->Value = Value;
1448 InsertHeadList (&Map->Used, &Item->Link);
1449
1450 Map->Count++;
1451 return EFI_SUCCESS;
1452 }
1453
1454
1455 /**
1456 Allocate an item to save the <Key, Value> pair to the tail of the netmap.
1457
1458 Allocate an item to save the <Key, Value> pair and add corresponding node entry
1459 to the tail of the Used doubly linked list. The number of the <Key, Value>
1460 pairs in the netmap increase by 1.
1461
1462 If Map is NULL, then ASSERT().
1463 If Key is NULL, then ASSERT().
1464
1465 @param[in, out] Map The netmap to insert into.
1466 @param[in] Key The user's key.
1467 @param[in] Value The user's value for the key.
1468
1469 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
1470 @retval EFI_SUCCESS The item is inserted to the tail.
1471
1472 **/
1473 EFI_STATUS
1474 EFIAPI
1475 NetMapInsertTail (
1476 IN OUT NET_MAP *Map,
1477 IN VOID *Key,
1478 IN VOID *Value OPTIONAL
1479 )
1480 {
1481 NET_MAP_ITEM *Item;
1482
1483 ASSERT (Map != NULL && Key != NULL);
1484
1485 Item = NetMapAllocItem (Map);
1486
1487 if (Item == NULL) {
1488 return EFI_OUT_OF_RESOURCES;
1489 }
1490
1491 Item->Key = Key;
1492 Item->Value = Value;
1493 InsertTailList (&Map->Used, &Item->Link);
1494
1495 Map->Count++;
1496
1497 return EFI_SUCCESS;
1498 }
1499
1500
1501 /**
1502 Check whether the item is in the Map and return TRUE if it is.
1503
1504 If Map is NULL, then ASSERT().
1505 If Item is NULL, then ASSERT().
1506
1507 @param[in] Map The netmap to search within.
1508 @param[in] Item The item to search.
1509
1510 @return TRUE if the item is in the netmap, otherwise FALSE.
1511
1512 **/
1513 BOOLEAN
1514 NetItemInMap (
1515 IN NET_MAP *Map,
1516 IN NET_MAP_ITEM *Item
1517 )
1518 {
1519 LIST_ENTRY *ListEntry;
1520
1521 ASSERT (Map != NULL && Item != NULL);
1522
1523 NET_LIST_FOR_EACH (ListEntry, &Map->Used) {
1524 if (ListEntry == &Item->Link) {
1525 return TRUE;
1526 }
1527 }
1528
1529 return FALSE;
1530 }
1531
1532
1533 /**
1534 Find the key in the netmap and returns the point to the item contains the Key.
1535
1536 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every
1537 item with the key to search. It returns the point to the item contains the Key if found.
1538
1539 If Map is NULL, then ASSERT().
1540 If Key is NULL, then ASSERT().
1541
1542 @param[in] Map The netmap to search within.
1543 @param[in] Key The key to search.
1544
1545 @return The point to the item contains the Key, or NULL if Key isn't in the map.
1546
1547 **/
1548 NET_MAP_ITEM *
1549 EFIAPI
1550 NetMapFindKey (
1551 IN NET_MAP *Map,
1552 IN VOID *Key
1553 )
1554 {
1555 LIST_ENTRY *Entry;
1556 NET_MAP_ITEM *Item;
1557
1558 ASSERT (Map != NULL && Key != NULL);
1559
1560 NET_LIST_FOR_EACH (Entry, &Map->Used) {
1561 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
1562
1563 if (Item->Key == Key) {
1564 return Item;
1565 }
1566 }
1567
1568 return NULL;
1569 }
1570
1571
1572 /**
1573 Remove the node entry of the item from the netmap and return the key of the removed item.
1574
1575 Remove the node entry of the item from the Used doubly linked list of the netmap.
1576 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1577 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,
1578 Value will point to the value of the item. It returns the key of the removed item.
1579
1580 If Map is NULL, then ASSERT().
1581 If Item is NULL, then ASSERT().
1582 if item in not in the netmap, then ASSERT().
1583
1584 @param[in, out] Map The netmap to remove the item from.
1585 @param[in, out] Item The item to remove.
1586 @param[out] Value The variable to receive the value if not NULL.
1587
1588 @return The key of the removed item.
1589
1590 **/
1591 VOID *
1592 EFIAPI
1593 NetMapRemoveItem (
1594 IN OUT NET_MAP *Map,
1595 IN OUT NET_MAP_ITEM *Item,
1596 OUT VOID **Value OPTIONAL
1597 )
1598 {
1599 ASSERT ((Map != NULL) && (Item != NULL));
1600 ASSERT (NetItemInMap (Map, Item));
1601
1602 RemoveEntryList (&Item->Link);
1603 Map->Count--;
1604 InsertHeadList (&Map->Recycled, &Item->Link);
1605
1606 if (Value != NULL) {
1607 *Value = Item->Value;
1608 }
1609
1610 return Item->Key;
1611 }
1612
1613
1614 /**
1615 Remove the first node entry on the netmap and return the key of the removed item.
1616
1617 Remove the first node entry from the Used doubly linked list of the netmap.
1618 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1619 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
1620 parameter Value will point to the value of the item. It returns the key of the removed item.
1621
1622 If Map is NULL, then ASSERT().
1623 If the Used doubly linked list is empty, then ASSERT().
1624
1625 @param[in, out] Map The netmap to remove the head from.
1626 @param[out] Value The variable to receive the value if not NULL.
1627
1628 @return The key of the item removed.
1629
1630 **/
1631 VOID *
1632 EFIAPI
1633 NetMapRemoveHead (
1634 IN OUT NET_MAP *Map,
1635 OUT VOID **Value OPTIONAL
1636 )
1637 {
1638 NET_MAP_ITEM *Item;
1639
1640 //
1641 // Often, it indicates a programming error to remove
1642 // the first entry in an empty list
1643 //
1644 ASSERT (Map && !IsListEmpty (&Map->Used));
1645
1646 Item = NET_LIST_HEAD (&Map->Used, NET_MAP_ITEM, Link);
1647 RemoveEntryList (&Item->Link);
1648 Map->Count--;
1649 InsertHeadList (&Map->Recycled, &Item->Link);
1650
1651 if (Value != NULL) {
1652 *Value = Item->Value;
1653 }
1654
1655 return Item->Key;
1656 }
1657
1658
1659 /**
1660 Remove the last node entry on the netmap and return the key of the removed item.
1661
1662 Remove the last node entry from the Used doubly linked list of the netmap.
1663 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
1664 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
1665 parameter Value will point to the value of the item. It returns the key of the removed item.
1666
1667 If Map is NULL, then ASSERT().
1668 If the Used doubly linked list is empty, then ASSERT().
1669
1670 @param[in, out] Map The netmap to remove the tail from.
1671 @param[out] Value The variable to receive the value if not NULL.
1672
1673 @return The key of the item removed.
1674
1675 **/
1676 VOID *
1677 EFIAPI
1678 NetMapRemoveTail (
1679 IN OUT NET_MAP *Map,
1680 OUT VOID **Value OPTIONAL
1681 )
1682 {
1683 NET_MAP_ITEM *Item;
1684
1685 //
1686 // Often, it indicates a programming error to remove
1687 // the last entry in an empty list
1688 //
1689 ASSERT (Map && !IsListEmpty (&Map->Used));
1690
1691 Item = NET_LIST_TAIL (&Map->Used, NET_MAP_ITEM, Link);
1692 RemoveEntryList (&Item->Link);
1693 Map->Count--;
1694 InsertHeadList (&Map->Recycled, &Item->Link);
1695
1696 if (Value != NULL) {
1697 *Value = Item->Value;
1698 }
1699
1700 return Item->Key;
1701 }
1702
1703
1704 /**
1705 Iterate through the netmap and call CallBack for each item.
1706
1707 It will continue the traverse if CallBack returns EFI_SUCCESS, otherwise, break
1708 from the loop. It returns the CallBack's last return value. This function is
1709 delete safe for the current item.
1710
1711 If Map is NULL, then ASSERT().
1712 If CallBack is NULL, then ASSERT().
1713
1714 @param[in] Map The Map to iterate through.
1715 @param[in] CallBack The callback function to call for each item.
1716 @param[in] Arg The opaque parameter to the callback.
1717
1718 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item
1719 return EFI_SUCCESS.
1720 @retval Others It returns the CallBack's last return value.
1721
1722 **/
1723 EFI_STATUS
1724 EFIAPI
1725 NetMapIterate (
1726 IN NET_MAP *Map,
1727 IN NET_MAP_CALLBACK CallBack,
1728 IN VOID *Arg OPTIONAL
1729 )
1730 {
1731
1732 LIST_ENTRY *Entry;
1733 LIST_ENTRY *Next;
1734 LIST_ENTRY *Head;
1735 NET_MAP_ITEM *Item;
1736 EFI_STATUS Result;
1737
1738 ASSERT ((Map != NULL) && (CallBack != NULL));
1739
1740 Head = &Map->Used;
1741
1742 if (IsListEmpty (Head)) {
1743 return EFI_SUCCESS;
1744 }
1745
1746 NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {
1747 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);
1748 Result = CallBack (Map, Item, Arg);
1749
1750 if (EFI_ERROR (Result)) {
1751 return Result;
1752 }
1753 }
1754
1755 return EFI_SUCCESS;
1756 }
1757
1758
1759 /**
1760 This is the default unload handle for all the network drivers.
1761
1762 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
1763 Uninstall all the protocols installed in the driver entry point.
1764
1765 @param[in] ImageHandle The drivers' driver image.
1766
1767 @retval EFI_SUCCESS The image is unloaded.
1768 @retval Others Failed to unload the image.
1769
1770 **/
1771 EFI_STATUS
1772 EFIAPI
1773 NetLibDefaultUnload (
1774 IN EFI_HANDLE ImageHandle
1775 )
1776 {
1777 EFI_STATUS Status;
1778 EFI_HANDLE *DeviceHandleBuffer;
1779 UINTN DeviceHandleCount;
1780 UINTN Index;
1781 UINTN Index2;
1782 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
1783 EFI_COMPONENT_NAME_PROTOCOL *ComponentName;
1784 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
1785
1786 //
1787 // Get the list of all the handles in the handle database.
1788 // If there is an error getting the list, then the unload
1789 // operation fails.
1790 //
1791 Status = gBS->LocateHandleBuffer (
1792 AllHandles,
1793 NULL,
1794 NULL,
1795 &DeviceHandleCount,
1796 &DeviceHandleBuffer
1797 );
1798
1799 if (EFI_ERROR (Status)) {
1800 return Status;
1801 }
1802
1803 for (Index = 0; Index < DeviceHandleCount; Index++) {
1804 Status = gBS->HandleProtocol (
1805 DeviceHandleBuffer[Index],
1806 &gEfiDriverBindingProtocolGuid,
1807 (VOID **) &DriverBinding
1808 );
1809 if (EFI_ERROR (Status)) {
1810 continue;
1811 }
1812
1813 if (DriverBinding->ImageHandle != ImageHandle) {
1814 continue;
1815 }
1816
1817 //
1818 // Disconnect the driver specified by ImageHandle from all
1819 // the devices in the handle database.
1820 //
1821 for (Index2 = 0; Index2 < DeviceHandleCount; Index2++) {
1822 Status = gBS->DisconnectController (
1823 DeviceHandleBuffer[Index2],
1824 DriverBinding->DriverBindingHandle,
1825 NULL
1826 );
1827 }
1828
1829 //
1830 // Uninstall all the protocols installed in the driver entry point
1831 //
1832 gBS->UninstallProtocolInterface (
1833 DriverBinding->DriverBindingHandle,
1834 &gEfiDriverBindingProtocolGuid,
1835 DriverBinding
1836 );
1837
1838 Status = gBS->HandleProtocol (
1839 DeviceHandleBuffer[Index],
1840 &gEfiComponentNameProtocolGuid,
1841 (VOID **) &ComponentName
1842 );
1843 if (!EFI_ERROR (Status)) {
1844 gBS->UninstallProtocolInterface (
1845 DriverBinding->DriverBindingHandle,
1846 &gEfiComponentNameProtocolGuid,
1847 ComponentName
1848 );
1849 }
1850
1851 Status = gBS->HandleProtocol (
1852 DeviceHandleBuffer[Index],
1853 &gEfiComponentName2ProtocolGuid,
1854 (VOID **) &ComponentName2
1855 );
1856 if (!EFI_ERROR (Status)) {
1857 gBS->UninstallProtocolInterface (
1858 DriverBinding->DriverBindingHandle,
1859 &gEfiComponentName2ProtocolGuid,
1860 ComponentName2
1861 );
1862 }
1863 }
1864
1865 //
1866 // Free the buffer containing the list of handles from the handle database
1867 //
1868 if (DeviceHandleBuffer != NULL) {
1869 gBS->FreePool (DeviceHandleBuffer);
1870 }
1871
1872 return EFI_SUCCESS;
1873 }
1874
1875
1876
1877 /**
1878 Create a child of the service that is identified by ServiceBindingGuid.
1879
1880 Get the ServiceBinding Protocol first, then use it to create a child.
1881
1882 If ServiceBindingGuid is NULL, then ASSERT().
1883 If ChildHandle is NULL, then ASSERT().
1884
1885 @param[in] Controller The controller which has the service installed.
1886 @param[in] Image The image handle used to open service.
1887 @param[in] ServiceBindingGuid The service's Guid.
1888 @param[in, out] ChildHandle The handle to receive the create child.
1889
1890 @retval EFI_SUCCESS The child is successfully created.
1891 @retval Others Failed to create the child.
1892
1893 **/
1894 EFI_STATUS
1895 EFIAPI
1896 NetLibCreateServiceChild (
1897 IN EFI_HANDLE Controller,
1898 IN EFI_HANDLE Image,
1899 IN EFI_GUID *ServiceBindingGuid,
1900 IN OUT EFI_HANDLE *ChildHandle
1901 )
1902 {
1903 EFI_STATUS Status;
1904 EFI_SERVICE_BINDING_PROTOCOL *Service;
1905
1906
1907 ASSERT ((ServiceBindingGuid != NULL) && (ChildHandle != NULL));
1908
1909 //
1910 // Get the ServiceBinding Protocol
1911 //
1912 Status = gBS->OpenProtocol (
1913 Controller,
1914 ServiceBindingGuid,
1915 (VOID **) &Service,
1916 Image,
1917 Controller,
1918 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1919 );
1920
1921 if (EFI_ERROR (Status)) {
1922 return Status;
1923 }
1924
1925 //
1926 // Create a child
1927 //
1928 Status = Service->CreateChild (Service, ChildHandle);
1929 return Status;
1930 }
1931
1932
1933 /**
1934 Destroy a child of the service that is identified by ServiceBindingGuid.
1935
1936 Get the ServiceBinding Protocol first, then use it to destroy a child.
1937
1938 If ServiceBindingGuid is NULL, then ASSERT().
1939
1940 @param[in] Controller The controller which has the service installed.
1941 @param[in] Image The image handle used to open service.
1942 @param[in] ServiceBindingGuid The service's Guid.
1943 @param[in] ChildHandle The child to destroy.
1944
1945 @retval EFI_SUCCESS The child is successfully destroyed.
1946 @retval Others Failed to destroy the child.
1947
1948 **/
1949 EFI_STATUS
1950 EFIAPI
1951 NetLibDestroyServiceChild (
1952 IN EFI_HANDLE Controller,
1953 IN EFI_HANDLE Image,
1954 IN EFI_GUID *ServiceBindingGuid,
1955 IN EFI_HANDLE ChildHandle
1956 )
1957 {
1958 EFI_STATUS Status;
1959 EFI_SERVICE_BINDING_PROTOCOL *Service;
1960
1961 ASSERT (ServiceBindingGuid != NULL);
1962
1963 //
1964 // Get the ServiceBinding Protocol
1965 //
1966 Status = gBS->OpenProtocol (
1967 Controller,
1968 ServiceBindingGuid,
1969 (VOID **) &Service,
1970 Image,
1971 Controller,
1972 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1973 );
1974
1975 if (EFI_ERROR (Status)) {
1976 return Status;
1977 }
1978
1979 //
1980 // destroy the child
1981 //
1982 Status = Service->DestroyChild (Service, ChildHandle);
1983 return Status;
1984 }
1985
1986 /**
1987 Get handle with Simple Network Protocol installed on it.
1988
1989 There should be MNP Service Binding Protocol installed on the input ServiceHandle.
1990 If Simple Network Protocol is already installed on the ServiceHandle, the
1991 ServiceHandle will be returned. If SNP is not installed on the ServiceHandle,
1992 try to find its parent handle with SNP installed.
1993
1994 @param[in] ServiceHandle The handle where network service binding protocols are
1995 installed on.
1996 @param[out] Snp The pointer to store the address of the SNP instance.
1997 This is an optional parameter that may be NULL.
1998
1999 @return The SNP handle, or NULL if not found.
2000
2001 **/
2002 EFI_HANDLE
2003 EFIAPI
2004 NetLibGetSnpHandle (
2005 IN EFI_HANDLE ServiceHandle,
2006 OUT EFI_SIMPLE_NETWORK_PROTOCOL **Snp OPTIONAL
2007 )
2008 {
2009 EFI_STATUS Status;
2010 EFI_SIMPLE_NETWORK_PROTOCOL *SnpInstance;
2011 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
2012 EFI_HANDLE SnpHandle;
2013
2014 //
2015 // Try to open SNP from ServiceHandle
2016 //
2017 SnpInstance = NULL;
2018 Status = gBS->HandleProtocol (ServiceHandle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &SnpInstance);
2019 if (!EFI_ERROR (Status)) {
2020 if (Snp != NULL) {
2021 *Snp = SnpInstance;
2022 }
2023 return ServiceHandle;
2024 }
2025
2026 //
2027 // Failed to open SNP, try to get SNP handle by LocateDevicePath()
2028 //
2029 DevicePath = DevicePathFromHandle (ServiceHandle);
2030 if (DevicePath == NULL) {
2031 return NULL;
2032 }
2033
2034 SnpHandle = NULL;
2035 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &DevicePath, &SnpHandle);
2036 if (EFI_ERROR (Status)) {
2037 //
2038 // Failed to find SNP handle
2039 //
2040 return NULL;
2041 }
2042
2043 Status = gBS->HandleProtocol (SnpHandle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &SnpInstance);
2044 if (!EFI_ERROR (Status)) {
2045 if (Snp != NULL) {
2046 *Snp = SnpInstance;
2047 }
2048 return SnpHandle;
2049 }
2050
2051 return NULL;
2052 }
2053
2054 /**
2055 Retrieve VLAN ID of a VLAN device handle.
2056
2057 Search VLAN device path node in Device Path of specified ServiceHandle and
2058 return its VLAN ID. If no VLAN device path node found, then this ServiceHandle
2059 is not a VLAN device handle, and 0 will be returned.
2060
2061 @param[in] ServiceHandle The handle where network service binding protocols are
2062 installed on.
2063
2064 @return VLAN ID of the device handle, or 0 if not a VLAN device.
2065
2066 **/
2067 UINT16
2068 EFIAPI
2069 NetLibGetVlanId (
2070 IN EFI_HANDLE ServiceHandle
2071 )
2072 {
2073 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
2074 EFI_DEVICE_PATH_PROTOCOL *Node;
2075
2076 DevicePath = DevicePathFromHandle (ServiceHandle);
2077 if (DevicePath == NULL) {
2078 return 0;
2079 }
2080
2081 Node = DevicePath;
2082 while (!IsDevicePathEnd (Node)) {
2083 if (Node->Type == MESSAGING_DEVICE_PATH && Node->SubType == MSG_VLAN_DP) {
2084 return ((VLAN_DEVICE_PATH *) Node)->VlanId;
2085 }
2086 Node = NextDevicePathNode (Node);
2087 }
2088
2089 return 0;
2090 }
2091
2092 /**
2093 Find VLAN device handle with specified VLAN ID.
2094
2095 The VLAN child device handle is created by VLAN Config Protocol on ControllerHandle.
2096 This function will append VLAN device path node to the parent device path,
2097 and then use LocateDevicePath() to find the correct VLAN device handle.
2098
2099 @param[in] ControllerHandle The handle where network service binding protocols are
2100 installed on.
2101 @param[in] VlanId The configured VLAN ID for the VLAN device.
2102
2103 @return The VLAN device handle, or NULL if not found.
2104
2105 **/
2106 EFI_HANDLE
2107 EFIAPI
2108 NetLibGetVlanHandle (
2109 IN EFI_HANDLE ControllerHandle,
2110 IN UINT16 VlanId
2111 )
2112 {
2113 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
2114 EFI_DEVICE_PATH_PROTOCOL *VlanDevicePath;
2115 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
2116 VLAN_DEVICE_PATH VlanNode;
2117 EFI_HANDLE Handle;
2118
2119 ParentDevicePath = DevicePathFromHandle (ControllerHandle);
2120 if (ParentDevicePath == NULL) {
2121 return NULL;
2122 }
2123
2124 //
2125 // Construct VLAN device path
2126 //
2127 CopyMem (&VlanNode, &mNetVlanDevicePathTemplate, sizeof (VLAN_DEVICE_PATH));
2128 VlanNode.VlanId = VlanId;
2129 VlanDevicePath = AppendDevicePathNode (
2130 ParentDevicePath,
2131 (EFI_DEVICE_PATH_PROTOCOL *) &VlanNode
2132 );
2133 if (VlanDevicePath == NULL) {
2134 return NULL;
2135 }
2136
2137 //
2138 // Find VLAN device handle
2139 //
2140 Handle = NULL;
2141 DevicePath = VlanDevicePath;
2142 gBS->LocateDevicePath (
2143 &gEfiDevicePathProtocolGuid,
2144 &DevicePath,
2145 &Handle
2146 );
2147 if (!IsDevicePathEnd (DevicePath)) {
2148 //
2149 // Device path is not exactly match
2150 //
2151 Handle = NULL;
2152 }
2153
2154 FreePool (VlanDevicePath);
2155 return Handle;
2156 }
2157
2158 /**
2159 Get MAC address associated with the network service handle.
2160
2161 If MacAddress is NULL, then ASSERT().
2162 If AddressSize is NULL, then ASSERT().
2163
2164 There should be MNP Service Binding Protocol installed on the input ServiceHandle.
2165 If SNP is installed on the ServiceHandle or its parent handle, MAC address will
2166 be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP.
2167
2168 @param[in] ServiceHandle The handle where network service binding protocols are
2169 installed on.
2170 @param[out] MacAddress The pointer to store the returned MAC address.
2171 @param[out] AddressSize The length of returned MAC address.
2172
2173 @retval EFI_SUCCESS MAC address is returned successfully.
2174 @retval Others Failed to get SNP mode data.
2175
2176 **/
2177 EFI_STATUS
2178 EFIAPI
2179 NetLibGetMacAddress (
2180 IN EFI_HANDLE ServiceHandle,
2181 OUT EFI_MAC_ADDRESS *MacAddress,
2182 OUT UINTN *AddressSize
2183 )
2184 {
2185 EFI_STATUS Status;
2186 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
2187 EFI_SIMPLE_NETWORK_MODE *SnpMode;
2188 EFI_SIMPLE_NETWORK_MODE SnpModeData;
2189 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;
2190 EFI_SERVICE_BINDING_PROTOCOL *MnpSb;
2191 EFI_HANDLE *SnpHandle;
2192 EFI_HANDLE MnpChildHandle;
2193
2194 ASSERT (MacAddress != NULL);
2195 ASSERT (AddressSize != NULL);
2196
2197 //
2198 // Try to get SNP handle
2199 //
2200 Snp = NULL;
2201 SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp);
2202 if (SnpHandle != NULL) {
2203 //
2204 // SNP found, use it directly
2205 //
2206 SnpMode = Snp->Mode;
2207 } else {
2208 //
2209 // Failed to get SNP handle, try to get MAC address from MNP
2210 //
2211 MnpChildHandle = NULL;
2212 Status = gBS->HandleProtocol (
2213 ServiceHandle,
2214 &gEfiManagedNetworkServiceBindingProtocolGuid,
2215 (VOID **) &MnpSb
2216 );
2217 if (EFI_ERROR (Status)) {
2218 return Status;
2219 }
2220
2221 //
2222 // Create a MNP child
2223 //
2224 Status = MnpSb->CreateChild (MnpSb, &MnpChildHandle);
2225 if (EFI_ERROR (Status)) {
2226 return Status;
2227 }
2228
2229 //
2230 // Open MNP protocol
2231 //
2232 Status = gBS->HandleProtocol (
2233 MnpChildHandle,
2234 &gEfiManagedNetworkProtocolGuid,
2235 (VOID **) &Mnp
2236 );
2237 if (EFI_ERROR (Status)) {
2238 MnpSb->DestroyChild (MnpSb, MnpChildHandle);
2239 return Status;
2240 }
2241
2242 //
2243 // Try to get SNP mode from MNP
2244 //
2245 Status = Mnp->GetModeData (Mnp, NULL, &SnpModeData);
2246 if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {
2247 MnpSb->DestroyChild (MnpSb, MnpChildHandle);
2248 return Status;
2249 }
2250 SnpMode = &SnpModeData;
2251
2252 //
2253 // Destroy the MNP child
2254 //
2255 MnpSb->DestroyChild (MnpSb, MnpChildHandle);
2256 }
2257
2258 *AddressSize = SnpMode->HwAddressSize;
2259 CopyMem (MacAddress->Addr, SnpMode->CurrentAddress.Addr, SnpMode->HwAddressSize);
2260
2261 return EFI_SUCCESS;
2262 }
2263
2264 /**
2265 Convert MAC address of the NIC associated with specified Service Binding Handle
2266 to a unicode string. Callers are responsible for freeing the string storage.
2267
2268 If MacString is NULL, then ASSERT().
2269
2270 Locate simple network protocol associated with the Service Binding Handle and
2271 get the mac address from SNP. Then convert the mac address into a unicode
2272 string. It takes 2 unicode characters to represent a 1 byte binary buffer.
2273 Plus one unicode character for the null-terminator.
2274
2275 @param[in] ServiceHandle The handle where network service binding protocol is
2276 installed on.
2277 @param[in] ImageHandle The image handle used to act as the agent handle to
2278 get the simple network protocol. This parameter is
2279 optional and may be NULL.
2280 @param[out] MacString The pointer to store the address of the string
2281 representation of the mac address.
2282
2283 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.
2284 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
2285 @retval Others Failed to open the simple network protocol.
2286
2287 **/
2288 EFI_STATUS
2289 EFIAPI
2290 NetLibGetMacString (
2291 IN EFI_HANDLE ServiceHandle,
2292 IN EFI_HANDLE ImageHandle, OPTIONAL
2293 OUT CHAR16 **MacString
2294 )
2295 {
2296 EFI_STATUS Status;
2297 EFI_MAC_ADDRESS MacAddress;
2298 UINT8 *HwAddress;
2299 UINTN HwAddressSize;
2300 UINT16 VlanId;
2301 CHAR16 *String;
2302 UINTN Index;
2303 UINTN BufferSize;
2304
2305 ASSERT (MacString != NULL);
2306
2307 //
2308 // Get MAC address of the network device
2309 //
2310 Status = NetLibGetMacAddress (ServiceHandle, &MacAddress, &HwAddressSize);
2311 if (EFI_ERROR (Status)) {
2312 return Status;
2313 }
2314
2315 //
2316 // It takes 2 unicode characters to represent a 1 byte binary buffer.
2317 // If VLAN is configured, it will need extra 5 characters like "\0005".
2318 // Plus one unicode character for the null-terminator.
2319 //
2320 BufferSize = (2 * HwAddressSize + 5 + 1) * sizeof (CHAR16);
2321 String = AllocateZeroPool (BufferSize);
2322 if (String == NULL) {
2323 return EFI_OUT_OF_RESOURCES;
2324 }
2325 *MacString = String;
2326
2327 //
2328 // Convert the MAC address into a unicode string.
2329 //
2330 HwAddress = &MacAddress.Addr[0];
2331 for (Index = 0; Index < HwAddressSize; Index++) {
2332 UnicodeValueToStringS (
2333 String,
2334 BufferSize - ((UINTN)String - (UINTN)*MacString),
2335 PREFIX_ZERO | RADIX_HEX,
2336 *(HwAddress++),
2337 2
2338 );
2339 String += StrnLenS (String, (BufferSize - ((UINTN)String - (UINTN)*MacString)) / sizeof (CHAR16));
2340 }
2341
2342 //
2343 // Append VLAN ID if any
2344 //
2345 VlanId = NetLibGetVlanId (ServiceHandle);
2346 if (VlanId != 0) {
2347 *String++ = L'\\';
2348 UnicodeValueToStringS (
2349 String,
2350 BufferSize - ((UINTN)String - (UINTN)*MacString),
2351 PREFIX_ZERO | RADIX_HEX,
2352 VlanId,
2353 4
2354 );
2355 String += StrnLenS (String, (BufferSize - ((UINTN)String - (UINTN)*MacString)) / sizeof (CHAR16));
2356 }
2357
2358 //
2359 // Null terminate the Unicode string
2360 //
2361 *String = L'\0';
2362
2363 return EFI_SUCCESS;
2364 }
2365
2366 /**
2367 Detect media status for specified network device.
2368
2369 If MediaPresent is NULL, then ASSERT().
2370
2371 The underlying UNDI driver may or may not support reporting media status from
2372 GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine
2373 will try to invoke Snp->GetStatus() to get the media status: if media already
2374 present, it return directly; if media not present, it will stop SNP and then
2375 restart SNP to get the latest media status, this give chance to get the correct
2376 media status for old UNDI driver which doesn't support reporting media status
2377 from GET_STATUS command.
2378 Note: there will be two limitations for current algorithm:
2379 1) for UNDI with this capability, in case of cable is not attached, there will
2380 be an redundant Stop/Start() process;
2381 2) for UNDI without this capability, in case that network cable is attached when
2382 Snp->Initialize() is invoked while network cable is unattached later,
2383 NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer
2384 apps to wait for timeout time.
2385
2386 @param[in] ServiceHandle The handle where network service binding protocols are
2387 installed on.
2388 @param[out] MediaPresent The pointer to store the media status.
2389
2390 @retval EFI_SUCCESS Media detection success.
2391 @retval EFI_INVALID_PARAMETER ServiceHandle is not valid network device handle.
2392 @retval EFI_UNSUPPORTED Network device does not support media detection.
2393 @retval EFI_DEVICE_ERROR SNP is in unknown state.
2394
2395 **/
2396 EFI_STATUS
2397 EFIAPI
2398 NetLibDetectMedia (
2399 IN EFI_HANDLE ServiceHandle,
2400 OUT BOOLEAN *MediaPresent
2401 )
2402 {
2403 EFI_STATUS Status;
2404 EFI_HANDLE SnpHandle;
2405 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
2406 UINT32 InterruptStatus;
2407 UINT32 OldState;
2408 EFI_MAC_ADDRESS *MCastFilter;
2409 UINT32 MCastFilterCount;
2410 UINT32 EnableFilterBits;
2411 UINT32 DisableFilterBits;
2412 BOOLEAN ResetMCastFilters;
2413
2414 ASSERT (MediaPresent != NULL);
2415
2416 //
2417 // Get SNP handle
2418 //
2419 Snp = NULL;
2420 SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp);
2421 if (SnpHandle == NULL) {
2422 return EFI_INVALID_PARAMETER;
2423 }
2424
2425 //
2426 // Check whether SNP support media detection
2427 //
2428 if (!Snp->Mode->MediaPresentSupported) {
2429 return EFI_UNSUPPORTED;
2430 }
2431
2432 //
2433 // Invoke Snp->GetStatus() to refresh MediaPresent field in SNP mode data
2434 //
2435 Status = Snp->GetStatus (Snp, &InterruptStatus, NULL);
2436 if (EFI_ERROR (Status)) {
2437 return Status;
2438 }
2439
2440 if (Snp->Mode->MediaPresent) {
2441 //
2442 // Media is present, return directly
2443 //
2444 *MediaPresent = TRUE;
2445 return EFI_SUCCESS;
2446 }
2447
2448 //
2449 // Till now, GetStatus() report no media; while, in case UNDI not support
2450 // reporting media status from GetStatus(), this media status may be incorrect.
2451 // So, we will stop SNP and then restart it to get the correct media status.
2452 //
2453 OldState = Snp->Mode->State;
2454 if (OldState >= EfiSimpleNetworkMaxState) {
2455 return EFI_DEVICE_ERROR;
2456 }
2457
2458 MCastFilter = NULL;
2459
2460 if (OldState == EfiSimpleNetworkInitialized) {
2461 //
2462 // SNP is already in use, need Shutdown/Stop and then Start/Initialize
2463 //
2464
2465 //
2466 // Backup current SNP receive filter settings
2467 //
2468 EnableFilterBits = Snp->Mode->ReceiveFilterSetting;
2469 DisableFilterBits = Snp->Mode->ReceiveFilterMask ^ EnableFilterBits;
2470
2471 ResetMCastFilters = TRUE;
2472 MCastFilterCount = Snp->Mode->MCastFilterCount;
2473 if (MCastFilterCount != 0) {
2474 MCastFilter = AllocateCopyPool (
2475 MCastFilterCount * sizeof (EFI_MAC_ADDRESS),
2476 Snp->Mode->MCastFilter
2477 );
2478 ASSERT (MCastFilter != NULL);
2479 if (MCastFilter == NULL) {
2480 Status = EFI_OUT_OF_RESOURCES;
2481 goto Exit;
2482 }
2483
2484 ResetMCastFilters = FALSE;
2485 }
2486
2487 //
2488 // Shutdown/Stop the simple network
2489 //
2490 Status = Snp->Shutdown (Snp);
2491 if (!EFI_ERROR (Status)) {
2492 Status = Snp->Stop (Snp);
2493 }
2494 if (EFI_ERROR (Status)) {
2495 goto Exit;
2496 }
2497
2498 //
2499 // Start/Initialize the simple network
2500 //
2501 Status = Snp->Start (Snp);
2502 if (!EFI_ERROR (Status)) {
2503 Status = Snp->Initialize (Snp, 0, 0);
2504 }
2505 if (EFI_ERROR (Status)) {
2506 goto Exit;
2507 }
2508
2509 //
2510 // Here we get the correct media status
2511 //
2512 *MediaPresent = Snp->Mode->MediaPresent;
2513
2514 //
2515 // Restore SNP receive filter settings
2516 //
2517 Status = Snp->ReceiveFilters (
2518 Snp,
2519 EnableFilterBits,
2520 DisableFilterBits,
2521 ResetMCastFilters,
2522 MCastFilterCount,
2523 MCastFilter
2524 );
2525
2526 if (MCastFilter != NULL) {
2527 FreePool (MCastFilter);
2528 }
2529
2530 return Status;
2531 }
2532
2533 //
2534 // SNP is not in use, it's in state of EfiSimpleNetworkStopped or EfiSimpleNetworkStarted
2535 //
2536 if (OldState == EfiSimpleNetworkStopped) {
2537 //
2538 // SNP not start yet, start it
2539 //
2540 Status = Snp->Start (Snp);
2541 if (EFI_ERROR (Status)) {
2542 goto Exit;
2543 }
2544 }
2545
2546 //
2547 // Initialize the simple network
2548 //
2549 Status = Snp->Initialize (Snp, 0, 0);
2550 if (EFI_ERROR (Status)) {
2551 Status = EFI_DEVICE_ERROR;
2552 goto Exit;
2553 }
2554
2555 //
2556 // Here we get the correct media status
2557 //
2558 *MediaPresent = Snp->Mode->MediaPresent;
2559
2560 //
2561 // Shut down the simple network
2562 //
2563 Snp->Shutdown (Snp);
2564
2565 Exit:
2566 if (OldState == EfiSimpleNetworkStopped) {
2567 //
2568 // Original SNP sate is Stopped, restore to original state
2569 //
2570 Snp->Stop (Snp);
2571 }
2572
2573 if (MCastFilter != NULL) {
2574 FreePool (MCastFilter);
2575 }
2576
2577 return Status;
2578 }
2579
2580 /**
2581
2582 Detect media state for a network device. This routine will wait for a period of time at
2583 a specified checking interval when a certain network is under connecting until connection
2584 process finishs or timeout. If Aip protocol is supported by low layer drivers, three kinds
2585 of media states can be detected: EFI_SUCCESS, EFI_NOT_READY and EFI_NO_MEDIA, represents
2586 connected state, connecting state and no media state respectively. When function detects
2587 the current state is EFI_NOT_READY, it will loop to wait for next time's check until state
2588 turns to be EFI_SUCCESS or EFI_NO_MEDIA. If Aip protocol is not supported, function will
2589 call NetLibDetectMedia() and return state directly.
2590
2591 @param[in] ServiceHandle The handle where network service binding protocols are
2592 installed on.
2593 @param[in] Timeout The maximum number of 100ns units to wait when network
2594 is connecting. Zero value means detect once and return
2595 immediately.
2596 @param[out] MediaState The pointer to the detected media state.
2597
2598 @retval EFI_SUCCESS Media detection success.
2599 @retval EFI_INVALID_PARAMETER ServiceHandle is not a valid network device handle or
2600 MediaState pointer is NULL.
2601 @retval EFI_DEVICE_ERROR A device error occurred.
2602 @retval EFI_TIMEOUT Network is connecting but timeout.
2603
2604 **/
2605 EFI_STATUS
2606 EFIAPI
2607 NetLibDetectMediaWaitTimeout (
2608 IN EFI_HANDLE ServiceHandle,
2609 IN UINT64 Timeout,
2610 OUT EFI_STATUS *MediaState
2611 )
2612 {
2613 EFI_STATUS Status;
2614 EFI_HANDLE SnpHandle;
2615 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
2616 EFI_ADAPTER_INFORMATION_PROTOCOL *Aip;
2617 EFI_ADAPTER_INFO_MEDIA_STATE *MediaInfo;
2618 BOOLEAN MediaPresent;
2619 UINTN DataSize;
2620 EFI_STATUS TimerStatus;
2621 EFI_EVENT Timer;
2622 UINT64 TimeRemained;
2623
2624 if (MediaState == NULL) {
2625 return EFI_INVALID_PARAMETER;
2626 }
2627 *MediaState = EFI_SUCCESS;
2628 MediaInfo = NULL;
2629
2630 //
2631 // Get SNP handle
2632 //
2633 Snp = NULL;
2634 SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp);
2635 if (SnpHandle == NULL) {
2636 return EFI_INVALID_PARAMETER;
2637 }
2638
2639 Status = gBS->HandleProtocol (
2640 SnpHandle,
2641 &gEfiAdapterInformationProtocolGuid,
2642 (VOID *) &Aip
2643 );
2644 if (EFI_ERROR (Status)) {
2645
2646 MediaPresent = TRUE;
2647 Status = NetLibDetectMedia (ServiceHandle, &MediaPresent);
2648 if (!EFI_ERROR (Status)) {
2649 if (MediaPresent) {
2650 *MediaState = EFI_SUCCESS;
2651 } else {
2652 *MediaState = EFI_NO_MEDIA;
2653 }
2654 }
2655
2656 //
2657 // NetLibDetectMedia doesn't support EFI_NOT_READY status, return now!
2658 //
2659 return Status;
2660 }
2661
2662 Status = Aip->GetInformation (
2663 Aip,
2664 &gEfiAdapterInfoMediaStateGuid,
2665 (VOID **) &MediaInfo,
2666 &DataSize
2667 );
2668 if (!EFI_ERROR (Status)) {
2669
2670 *MediaState = MediaInfo->MediaState;
2671 FreePool (MediaInfo);
2672 if (*MediaState != EFI_NOT_READY || Timeout < MEDIA_STATE_DETECT_TIME_INTERVAL) {
2673
2674 return EFI_SUCCESS;
2675 }
2676 } else {
2677
2678 if (MediaInfo != NULL) {
2679 FreePool (MediaInfo);
2680 }
2681
2682 if (Status == EFI_UNSUPPORTED) {
2683
2684 //
2685 // If gEfiAdapterInfoMediaStateGuid is not supported, call NetLibDetectMedia to get media state!
2686 //
2687 MediaPresent = TRUE;
2688 Status = NetLibDetectMedia (ServiceHandle, &MediaPresent);
2689 if (!EFI_ERROR (Status)) {
2690 if (MediaPresent) {
2691 *MediaState = EFI_SUCCESS;
2692 } else {
2693 *MediaState = EFI_NO_MEDIA;
2694 }
2695 }
2696 return Status;
2697 }
2698
2699 return Status;
2700 }
2701
2702 //
2703 // Loop to check media state
2704 //
2705
2706 Timer = NULL;
2707 TimeRemained = Timeout;
2708 Status = gBS->CreateEvent (EVT_TIMER, TPL_CALLBACK, NULL, NULL, &Timer);
2709 if (EFI_ERROR (Status)) {
2710 return EFI_DEVICE_ERROR;
2711 }
2712
2713 do {
2714 Status = gBS->SetTimer (
2715 Timer,
2716 TimerRelative,
2717 MEDIA_STATE_DETECT_TIME_INTERVAL
2718 );
2719 if (EFI_ERROR (Status)) {
2720 gBS->CloseEvent(Timer);
2721 return EFI_DEVICE_ERROR;
2722 }
2723
2724 do {
2725 TimerStatus = gBS->CheckEvent (Timer);
2726 if (!EFI_ERROR (TimerStatus)) {
2727
2728 TimeRemained -= MEDIA_STATE_DETECT_TIME_INTERVAL;
2729 Status = Aip->GetInformation (
2730 Aip,
2731 &gEfiAdapterInfoMediaStateGuid,
2732 (VOID **) &MediaInfo,
2733 &DataSize
2734 );
2735 if (!EFI_ERROR (Status)) {
2736
2737 *MediaState = MediaInfo->MediaState;
2738 FreePool (MediaInfo);
2739 } else {
2740
2741 if (MediaInfo != NULL) {
2742 FreePool (MediaInfo);
2743 }
2744 gBS->CloseEvent(Timer);
2745 return Status;
2746 }
2747 }
2748 } while (TimerStatus == EFI_NOT_READY);
2749 } while (*MediaState == EFI_NOT_READY && TimeRemained >= MEDIA_STATE_DETECT_TIME_INTERVAL);
2750
2751 gBS->CloseEvent(Timer);
2752 if (*MediaState == EFI_NOT_READY && TimeRemained < MEDIA_STATE_DETECT_TIME_INTERVAL) {
2753 return EFI_TIMEOUT;
2754 } else {
2755 return EFI_SUCCESS;
2756 }
2757 }
2758
2759 /**
2760 Check the default address used by the IPv4 driver is static or dynamic (acquired
2761 from DHCP).
2762
2763 If the controller handle does not have the EFI_IP4_CONFIG2_PROTOCOL installed, the
2764 default address is static. If failed to get the policy from Ip4 Config2 Protocol,
2765 the default address is static. Otherwise, get the result from Ip4 Config2 Protocol.
2766
2767 @param[in] Controller The controller handle which has the EFI_IP4_CONFIG2_PROTOCOL
2768 relative with the default address to judge.
2769
2770 @retval TRUE If the default address is static.
2771 @retval FALSE If the default address is acquired from DHCP.
2772
2773 **/
2774 BOOLEAN
2775 NetLibDefaultAddressIsStatic (
2776 IN EFI_HANDLE Controller
2777 )
2778 {
2779 EFI_STATUS Status;
2780 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
2781 UINTN DataSize;
2782 EFI_IP4_CONFIG2_POLICY Policy;
2783 BOOLEAN IsStatic;
2784
2785 Ip4Config2 = NULL;
2786
2787 DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);
2788
2789 IsStatic = TRUE;
2790
2791 //
2792 // Get Ip4Config2 policy.
2793 //
2794 Status = gBS->HandleProtocol (Controller, &gEfiIp4Config2ProtocolGuid, (VOID **) &Ip4Config2);
2795 if (EFI_ERROR (Status)) {
2796 goto ON_EXIT;
2797 }
2798
2799 Status = Ip4Config2->GetData (Ip4Config2, Ip4Config2DataTypePolicy, &DataSize, &Policy);
2800 if (EFI_ERROR (Status)) {
2801 goto ON_EXIT;
2802 }
2803
2804 IsStatic = (BOOLEAN) (Policy == Ip4Config2PolicyStatic);
2805
2806 ON_EXIT:
2807
2808 return IsStatic;
2809 }
2810
2811 /**
2812 Create an IPv4 device path node.
2813
2814 If Node is NULL, then ASSERT().
2815
2816 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.
2817 The header subtype of IPv4 device path node is MSG_IPv4_DP.
2818 Get other info from parameters to make up the whole IPv4 device path node.
2819
2820 @param[in, out] Node Pointer to the IPv4 device path node.
2821 @param[in] Controller The controller handle.
2822 @param[in] LocalIp The local IPv4 address.
2823 @param[in] LocalPort The local port.
2824 @param[in] RemoteIp The remote IPv4 address.
2825 @param[in] RemotePort The remote port.
2826 @param[in] Protocol The protocol type in the IP header.
2827 @param[in] UseDefaultAddress Whether this instance is using default address or not.
2828
2829 **/
2830 VOID
2831 EFIAPI
2832 NetLibCreateIPv4DPathNode (
2833 IN OUT IPv4_DEVICE_PATH *Node,
2834 IN EFI_HANDLE Controller,
2835 IN IP4_ADDR LocalIp,
2836 IN UINT16 LocalPort,
2837 IN IP4_ADDR RemoteIp,
2838 IN UINT16 RemotePort,
2839 IN UINT16 Protocol,
2840 IN BOOLEAN UseDefaultAddress
2841 )
2842 {
2843 ASSERT (Node != NULL);
2844
2845 Node->Header.Type = MESSAGING_DEVICE_PATH;
2846 Node->Header.SubType = MSG_IPv4_DP;
2847 SetDevicePathNodeLength (&Node->Header, sizeof (IPv4_DEVICE_PATH));
2848
2849 CopyMem (&Node->LocalIpAddress, &LocalIp, sizeof (EFI_IPv4_ADDRESS));
2850 CopyMem (&Node->RemoteIpAddress, &RemoteIp, sizeof (EFI_IPv4_ADDRESS));
2851
2852 Node->LocalPort = LocalPort;
2853 Node->RemotePort = RemotePort;
2854
2855 Node->Protocol = Protocol;
2856
2857 if (!UseDefaultAddress) {
2858 Node->StaticIpAddress = TRUE;
2859 } else {
2860 Node->StaticIpAddress = NetLibDefaultAddressIsStatic (Controller);
2861 }
2862
2863 //
2864 // Set the Gateway IP address to default value 0:0:0:0.
2865 // Set the Subnet mask to default value 255:255:255:0.
2866 //
2867 ZeroMem (&Node->GatewayIpAddress, sizeof (EFI_IPv4_ADDRESS));
2868 SetMem (&Node->SubnetMask, sizeof (EFI_IPv4_ADDRESS), 0xff);
2869 Node->SubnetMask.Addr[3] = 0;
2870 }
2871
2872 /**
2873 Create an IPv6 device path node.
2874
2875 If Node is NULL, then ASSERT().
2876 If LocalIp is NULL, then ASSERT().
2877 If RemoteIp is NULL, then ASSERT().
2878
2879 The header type of IPv6 device path node is MESSAGING_DEVICE_PATH.
2880 The header subtype of IPv6 device path node is MSG_IPv6_DP.
2881 Get other info from parameters to make up the whole IPv6 device path node.
2882
2883 @param[in, out] Node Pointer to the IPv6 device path node.
2884 @param[in] Controller The controller handle.
2885 @param[in] LocalIp The local IPv6 address.
2886 @param[in] LocalPort The local port.
2887 @param[in] RemoteIp The remote IPv6 address.
2888 @param[in] RemotePort The remote port.
2889 @param[in] Protocol The protocol type in the IP header.
2890
2891 **/
2892 VOID
2893 EFIAPI
2894 NetLibCreateIPv6DPathNode (
2895 IN OUT IPv6_DEVICE_PATH *Node,
2896 IN EFI_HANDLE Controller,
2897 IN EFI_IPv6_ADDRESS *LocalIp,
2898 IN UINT16 LocalPort,
2899 IN EFI_IPv6_ADDRESS *RemoteIp,
2900 IN UINT16 RemotePort,
2901 IN UINT16 Protocol
2902 )
2903 {
2904 ASSERT (Node != NULL && LocalIp != NULL && RemoteIp != NULL);
2905
2906 Node->Header.Type = MESSAGING_DEVICE_PATH;
2907 Node->Header.SubType = MSG_IPv6_DP;
2908 SetDevicePathNodeLength (&Node->Header, sizeof (IPv6_DEVICE_PATH));
2909
2910 CopyMem (&Node->LocalIpAddress, LocalIp, sizeof (EFI_IPv6_ADDRESS));
2911 CopyMem (&Node->RemoteIpAddress, RemoteIp, sizeof (EFI_IPv6_ADDRESS));
2912
2913 Node->LocalPort = LocalPort;
2914 Node->RemotePort = RemotePort;
2915
2916 Node->Protocol = Protocol;
2917
2918 //
2919 // Set default value to IPAddressOrigin, PrefixLength.
2920 // Set the Gateway IP address to unspecified address.
2921 //
2922 Node->IpAddressOrigin = 0;
2923 Node->PrefixLength = IP6_PREFIX_LENGTH;
2924 ZeroMem (&Node->GatewayIpAddress, sizeof (EFI_IPv6_ADDRESS));
2925 }
2926
2927 /**
2928 Find the UNDI/SNP handle from controller and protocol GUID.
2929
2930 If ProtocolGuid is NULL, then ASSERT().
2931
2932 For example, IP will open a MNP child to transmit/receive
2933 packets, when MNP is stopped, IP should also be stopped. IP
2934 needs to find its own private data which is related the IP's
2935 service binding instance that is install on UNDI/SNP handle.
2936 Now, the controller is either a MNP or ARP child handle. But
2937 IP opens these handle BY_DRIVER, use that info, we can get the
2938 UNDI/SNP handle.
2939
2940 @param[in] Controller Then protocol handle to check.
2941 @param[in] ProtocolGuid The protocol that is related with the handle.
2942
2943 @return The UNDI/SNP handle or NULL for errors.
2944
2945 **/
2946 EFI_HANDLE
2947 EFIAPI
2948 NetLibGetNicHandle (
2949 IN EFI_HANDLE Controller,
2950 IN EFI_GUID *ProtocolGuid
2951 )
2952 {
2953 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer;
2954 EFI_HANDLE Handle;
2955 EFI_STATUS Status;
2956 UINTN OpenCount;
2957 UINTN Index;
2958
2959 ASSERT (ProtocolGuid != NULL);
2960
2961 Status = gBS->OpenProtocolInformation (
2962 Controller,
2963 ProtocolGuid,
2964 &OpenBuffer,
2965 &OpenCount
2966 );
2967
2968 if (EFI_ERROR (Status)) {
2969 return NULL;
2970 }
2971
2972 Handle = NULL;
2973
2974 for (Index = 0; Index < OpenCount; Index++) {
2975 if ((OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {
2976 Handle = OpenBuffer[Index].ControllerHandle;
2977 break;
2978 }
2979 }
2980
2981 gBS->FreePool (OpenBuffer);
2982 return Handle;
2983 }
2984
2985 /**
2986 Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS.
2987
2988 @param[in] String The pointer to the Ascii string.
2989 @param[out] Ip4Address The pointer to the converted IPv4 address.
2990
2991 @retval EFI_SUCCESS Convert to IPv4 address successfully.
2992 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.
2993
2994 **/
2995 EFI_STATUS
2996 EFIAPI
2997 NetLibAsciiStrToIp4 (
2998 IN CONST CHAR8 *String,
2999 OUT EFI_IPv4_ADDRESS *Ip4Address
3000 )
3001 {
3002 RETURN_STATUS Status;
3003 CHAR8 *EndPointer;
3004
3005 Status = AsciiStrToIpv4Address (String, &EndPointer, Ip4Address, NULL);
3006 if (RETURN_ERROR (Status) || (*EndPointer != '\0')) {
3007 return EFI_INVALID_PARAMETER;
3008 } else {
3009 return EFI_SUCCESS;
3010 }
3011 }
3012
3013
3014 /**
3015 Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of the
3016 string is defined in RFC 4291 - Text Representation of Addresses.
3017
3018 @param[in] String The pointer to the Ascii string.
3019 @param[out] Ip6Address The pointer to the converted IPv6 address.
3020
3021 @retval EFI_SUCCESS Convert to IPv6 address successfully.
3022 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
3023
3024 **/
3025 EFI_STATUS
3026 EFIAPI
3027 NetLibAsciiStrToIp6 (
3028 IN CONST CHAR8 *String,
3029 OUT EFI_IPv6_ADDRESS *Ip6Address
3030 )
3031 {
3032 RETURN_STATUS Status;
3033 CHAR8 *EndPointer;
3034
3035 Status = AsciiStrToIpv6Address (String, &EndPointer, Ip6Address, NULL);
3036 if (RETURN_ERROR (Status) || (*EndPointer != '\0')) {
3037 return EFI_INVALID_PARAMETER;
3038 } else {
3039 return EFI_SUCCESS;
3040 }
3041 }
3042
3043
3044 /**
3045 Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS.
3046
3047 @param[in] String The pointer to the Ascii string.
3048 @param[out] Ip4Address The pointer to the converted IPv4 address.
3049
3050 @retval EFI_SUCCESS Convert to IPv4 address successfully.
3051 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.
3052
3053 **/
3054 EFI_STATUS
3055 EFIAPI
3056 NetLibStrToIp4 (
3057 IN CONST CHAR16 *String,
3058 OUT EFI_IPv4_ADDRESS *Ip4Address
3059 )
3060 {
3061 RETURN_STATUS Status;
3062 CHAR16 *EndPointer;
3063
3064 Status = StrToIpv4Address (String, &EndPointer, Ip4Address, NULL);
3065 if (RETURN_ERROR (Status) || (*EndPointer != L'\0')) {
3066 return EFI_INVALID_PARAMETER;
3067 } else {
3068 return EFI_SUCCESS;
3069 }
3070 }
3071
3072
3073 /**
3074 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of
3075 the string is defined in RFC 4291 - Text Representation of Addresses.
3076
3077 @param[in] String The pointer to the Ascii string.
3078 @param[out] Ip6Address The pointer to the converted IPv6 address.
3079
3080 @retval EFI_SUCCESS Convert to IPv6 address successfully.
3081 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
3082
3083 **/
3084 EFI_STATUS
3085 EFIAPI
3086 NetLibStrToIp6 (
3087 IN CONST CHAR16 *String,
3088 OUT EFI_IPv6_ADDRESS *Ip6Address
3089 )
3090 {
3091 RETURN_STATUS Status;
3092 CHAR16 *EndPointer;
3093
3094 Status = StrToIpv6Address (String, &EndPointer, Ip6Address, NULL);
3095 if (RETURN_ERROR (Status) || (*EndPointer != L'\0')) {
3096 return EFI_INVALID_PARAMETER;
3097 } else {
3098 return EFI_SUCCESS;
3099 }
3100 }
3101
3102 /**
3103 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length.
3104 The format of the string is defined in RFC 4291 - Text Representation of Addresses
3105 Prefixes: ipv6-address/prefix-length.
3106
3107 @param[in] String The pointer to the Ascii string.
3108 @param[out] Ip6Address The pointer to the converted IPv6 address.
3109 @param[out] PrefixLength The pointer to the converted prefix length.
3110
3111 @retval EFI_SUCCESS Convert to IPv6 address successfully.
3112 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.
3113
3114 **/
3115 EFI_STATUS
3116 EFIAPI
3117 NetLibStrToIp6andPrefix (
3118 IN CONST CHAR16 *String,
3119 OUT EFI_IPv6_ADDRESS *Ip6Address,
3120 OUT UINT8 *PrefixLength
3121 )
3122 {
3123 RETURN_STATUS Status;
3124 CHAR16 *EndPointer;
3125
3126 Status = StrToIpv6Address (String, &EndPointer, Ip6Address, PrefixLength);
3127 if (RETURN_ERROR (Status) || (*EndPointer != L'\0')) {
3128 return EFI_INVALID_PARAMETER;
3129 } else {
3130 return EFI_SUCCESS;
3131 }
3132 }
3133
3134 /**
3135
3136 Convert one EFI_IPv6_ADDRESS to Null-terminated Unicode string.
3137 The text representation of address is defined in RFC 4291.
3138
3139 @param[in] Ip6Address The pointer to the IPv6 address.
3140 @param[out] String The buffer to return the converted string.
3141 @param[in] StringSize The length in bytes of the input String.
3142
3143 @retval EFI_SUCCESS Convert to string successfully.
3144 @retval EFI_INVALID_PARAMETER The input parameter is invalid.
3145 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small for the result. BufferSize has been
3146 updated with the size needed to complete the request.
3147 **/
3148 EFI_STATUS
3149 EFIAPI
3150 NetLibIp6ToStr (
3151 IN EFI_IPv6_ADDRESS *Ip6Address,
3152 OUT CHAR16 *String,
3153 IN UINTN StringSize
3154 )
3155 {
3156 UINT16 Ip6Addr[8];
3157 UINTN Index;
3158 UINTN LongestZerosStart;
3159 UINTN LongestZerosLength;
3160 UINTN CurrentZerosStart;
3161 UINTN CurrentZerosLength;
3162 CHAR16 Buffer[sizeof"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
3163 CHAR16 *Ptr;
3164
3165 if (Ip6Address == NULL || String == NULL || StringSize == 0) {
3166 return EFI_INVALID_PARAMETER;
3167 }
3168
3169 //
3170 // Convert the UINT8 array to an UINT16 array for easy handling.
3171 //
3172 ZeroMem (Ip6Addr, sizeof (Ip6Addr));
3173 for (Index = 0; Index < 16; Index++) {
3174 Ip6Addr[Index / 2] |= (Ip6Address->Addr[Index] << ((1 - (Index % 2)) << 3));
3175 }
3176
3177 //
3178 // Find the longest zeros and mark it.
3179 //
3180 CurrentZerosStart = DEFAULT_ZERO_START;
3181 CurrentZerosLength = 0;
3182 LongestZerosStart = DEFAULT_ZERO_START;
3183 LongestZerosLength = 0;
3184 for (Index = 0; Index < 8; Index++) {
3185 if (Ip6Addr[Index] == 0) {
3186 if (CurrentZerosStart == DEFAULT_ZERO_START) {
3187 CurrentZerosStart = Index;
3188 CurrentZerosLength = 1;
3189 } else {
3190 CurrentZerosLength++;
3191 }
3192 } else {
3193 if (CurrentZerosStart != DEFAULT_ZERO_START) {
3194 if (CurrentZerosLength > 2 && (LongestZerosStart == (DEFAULT_ZERO_START) || CurrentZerosLength > LongestZerosLength)) {
3195 LongestZerosStart = CurrentZerosStart;
3196 LongestZerosLength = CurrentZerosLength;
3197 }
3198 CurrentZerosStart = DEFAULT_ZERO_START;
3199 CurrentZerosLength = 0;
3200 }
3201 }
3202 }
3203
3204 if (CurrentZerosStart != DEFAULT_ZERO_START && CurrentZerosLength > 2) {
3205 if (LongestZerosStart == DEFAULT_ZERO_START || LongestZerosLength < CurrentZerosLength) {
3206 LongestZerosStart = CurrentZerosStart;
3207 LongestZerosLength = CurrentZerosLength;
3208 }
3209 }
3210
3211 Ptr = Buffer;
3212 for (Index = 0; Index < 8; Index++) {
3213 if (LongestZerosStart != DEFAULT_ZERO_START && Index >= LongestZerosStart && Index < LongestZerosStart + LongestZerosLength) {
3214 if (Index == LongestZerosStart) {
3215 *Ptr++ = L':';
3216 }
3217 continue;
3218 }
3219 if (Index != 0) {
3220 *Ptr++ = L':';
3221 }
3222 Ptr += UnicodeSPrint(Ptr, 10, L"%x", Ip6Addr[Index]);
3223 }
3224
3225 if (LongestZerosStart != DEFAULT_ZERO_START && LongestZerosStart + LongestZerosLength == 8) {
3226 *Ptr++ = L':';
3227 }
3228 *Ptr = L'\0';
3229
3230 if ((UINTN)Ptr - (UINTN)Buffer > StringSize) {
3231 return EFI_BUFFER_TOO_SMALL;
3232 }
3233
3234 StrCpyS (String, StringSize / sizeof (CHAR16), Buffer);
3235
3236 return EFI_SUCCESS;
3237 }
3238
3239 /**
3240 This function obtains the system guid from the smbios table.
3241
3242 If SystemGuid is NULL, then ASSERT().
3243
3244 @param[out] SystemGuid The pointer of the returned system guid.
3245
3246 @retval EFI_SUCCESS Successfully obtained the system guid.
3247 @retval EFI_NOT_FOUND Did not find the SMBIOS table.
3248
3249 **/
3250 EFI_STATUS
3251 EFIAPI
3252 NetLibGetSystemGuid (
3253 OUT EFI_GUID *SystemGuid
3254 )
3255 {
3256 EFI_STATUS Status;
3257 SMBIOS_TABLE_ENTRY_POINT *SmbiosTable;
3258 SMBIOS_TABLE_3_0_ENTRY_POINT *Smbios30Table;
3259 SMBIOS_STRUCTURE_POINTER Smbios;
3260 SMBIOS_STRUCTURE_POINTER SmbiosEnd;
3261 CHAR8 *String;
3262
3263 ASSERT (SystemGuid != NULL);
3264
3265 SmbiosTable = NULL;
3266 Status = EfiGetSystemConfigurationTable (&gEfiSmbios3TableGuid, (VOID **) &Smbios30Table);
3267 if (!(EFI_ERROR (Status) || Smbios30Table == NULL)) {
3268 Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) Smbios30Table->TableAddress;
3269 SmbiosEnd.Raw = (UINT8 *) (UINTN) (Smbios30Table->TableAddress + Smbios30Table->TableMaximumSize);
3270 } else {
3271 Status = EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **) &SmbiosTable);
3272 if (EFI_ERROR (Status) || SmbiosTable == NULL) {
3273 return EFI_NOT_FOUND;
3274 }
3275 Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) SmbiosTable->TableAddress;
3276 SmbiosEnd.Raw = (UINT8 *) ((UINTN) SmbiosTable->TableAddress + SmbiosTable->TableLength);
3277 }
3278
3279 do {
3280 if (Smbios.Hdr->Type == 1) {
3281 if (Smbios.Hdr->Length < 0x19) {
3282 //
3283 // Older version did not support UUID.
3284 //
3285 return EFI_NOT_FOUND;
3286 }
3287
3288 //
3289 // SMBIOS tables are byte packed so we need to do a byte copy to
3290 // prevend alignment faults on Itanium-based platform.
3291 //
3292 CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID));
3293 return EFI_SUCCESS;
3294 }
3295
3296 //
3297 // Go to the next SMBIOS structure. Each SMBIOS structure may include 2 parts:
3298 // 1. Formatted section; 2. Unformatted string section. So, 2 steps are needed
3299 // to skip one SMBIOS structure.
3300 //
3301
3302 //
3303 // Step 1: Skip over formatted section.
3304 //
3305 String = (CHAR8 *) (Smbios.Raw + Smbios.Hdr->Length);
3306
3307 //
3308 // Step 2: Skip over unformated string section.
3309 //
3310 do {
3311 //
3312 // Each string is terminated with a NULL(00h) BYTE and the sets of strings
3313 // is terminated with an additional NULL(00h) BYTE.
3314 //
3315 for ( ; *String != 0; String++) {
3316 }
3317
3318 if (*(UINT8*)++String == 0) {
3319 //
3320 // Pointer to the next SMBIOS structure.
3321 //
3322 Smbios.Raw = (UINT8 *)++String;
3323 break;
3324 }
3325 } while (TRUE);
3326 } while (Smbios.Raw < SmbiosEnd.Raw);
3327 return EFI_NOT_FOUND;
3328 }
3329
3330 /**
3331 Create Dns QName according the queried domain name.
3332
3333 If DomainName is NULL, then ASSERT().
3334
3335 QName is a domain name represented as a sequence of labels,
3336 where each label consists of a length octet followed by that
3337 number of octets. The QName terminates with the zero
3338 length octet for the null label of the root. Caller should
3339 take responsibility to free the buffer in returned pointer.
3340
3341 @param DomainName The pointer to the queried domain name string.
3342
3343 @retval NULL Failed to fill QName.
3344 @return QName filled successfully.
3345
3346 **/
3347 CHAR8 *
3348 EFIAPI
3349 NetLibCreateDnsQName (
3350 IN CHAR16 *DomainName
3351 )
3352 {
3353 CHAR8 *QueryName;
3354 UINTN QueryNameSize;
3355 CHAR8 *Header;
3356 CHAR8 *Tail;
3357 UINTN Len;
3358 UINTN Index;
3359
3360 ASSERT (DomainName != NULL);
3361
3362 QueryName = NULL;
3363 QueryNameSize = 0;
3364 Header = NULL;
3365 Tail = NULL;
3366
3367 //
3368 // One byte for first label length, one byte for terminated length zero.
3369 //
3370 QueryNameSize = StrLen (DomainName) + 2;
3371
3372 if (QueryNameSize > DNS_MAX_NAME_SIZE) {
3373 return NULL;
3374 }
3375
3376 QueryName = AllocateZeroPool (QueryNameSize);
3377 if (QueryName == NULL) {
3378 return NULL;
3379 }
3380
3381 Header = QueryName;
3382 Tail = Header + 1;
3383 Len = 0;
3384 for (Index = 0; DomainName[Index] != 0; Index++) {
3385 *Tail = (CHAR8) DomainName[Index];
3386 if (*Tail == '.') {
3387 *Header = (CHAR8) Len;
3388 Header = Tail;
3389 Tail ++;
3390 Len = 0;
3391 } else {
3392 Tail++;
3393 Len++;
3394 }
3395 }
3396 *Header = (CHAR8) Len;
3397 *Tail = 0;
3398
3399 return QueryName;
3400 }