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