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