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