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