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