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