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