]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
Update to build with 10.8 OS X Frameworks. This is a Mac only change.
[mirror_edk2.git] / MdeModulePkg / Library / DxeNetLib / DxeNetLib.c
CommitLineData
da1d0201 1/** @file\r
3e7104c2 2 Network library.\r
1204fe83 3\r
b00ed21a 4Copyright (c) 2005 - 2012, 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
1157NetIsInHandleBuffer (\r
1158 IN EFI_HANDLE Handle,\r
1159 IN UINTN NumberOfChildren,\r
1160 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL\r
1f7eb561 1161 )\r
216f7970 1162{\r
1163 UINTN Index;\r
1164 \r
1165 if (NumberOfChildren == 0 || ChildHandleBuffer == NULL) {\r
1166 return FALSE;\r
1167 }\r
1168\r
1169 for (Index = 0; Index < NumberOfChildren; Index++) {\r
1170 if (Handle == ChildHandleBuffer[Index]) {\r
1171 return TRUE;\r
1172 }\r
1173 }\r
1174\r
1175 return FALSE;\r
1176}\r
1177\r
da1d0201 1178\r
1179/**\r
1180 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.\r
1204fe83 1181\r
1182 Initialize the forward and backward links of two head nodes donated by Map->Used\r
b9008c87 1183 and Map->Recycled of two doubly linked lists.\r
1184 Initializes the count of the <Key, Value> pairs in the netmap to zero.\r
1204fe83 1185\r
b9008c87 1186 If Map is NULL, then ASSERT().\r
8f5e6151 1187 If the address of Map->Used is NULL, then ASSERT().\r
b9008c87 1188 If the address of Map->Recycled is NULl, then ASSERT().\r
1204fe83 1189\r
3e7104c2 1190 @param[in, out] Map The netmap to initialize.\r
da1d0201 1191\r
1192**/\r
1193VOID\r
7b414b4e 1194EFIAPI\r
da1d0201 1195NetMapInit (\r
3e7104c2 1196 IN OUT NET_MAP *Map\r
da1d0201 1197 )\r
1198{\r
1199 ASSERT (Map != NULL);\r
1200\r
e48e37fc 1201 InitializeListHead (&Map->Used);\r
1202 InitializeListHead (&Map->Recycled);\r
da1d0201 1203 Map->Count = 0;\r
1204}\r
1205\r
1206\r
1207/**\r
1208 To clean up the netmap, that is, release allocated memories.\r
1204fe83 1209\r
b9008c87 1210 Removes all nodes of the Used doubly linked list and free memory of all related netmap items.\r
1211 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.\r
1212 The number of the <Key, Value> pairs in the netmap is set to be zero.\r
1204fe83 1213\r
b9008c87 1214 If Map is NULL, then ASSERT().\r
1204fe83 1215\r
3e7104c2 1216 @param[in, out] Map The netmap to clean up.\r
da1d0201 1217\r
1218**/\r
1219VOID\r
7b414b4e 1220EFIAPI\r
da1d0201 1221NetMapClean (\r
3e7104c2 1222 IN OUT NET_MAP *Map\r
da1d0201 1223 )\r
1224{\r
1225 NET_MAP_ITEM *Item;\r
e48e37fc 1226 LIST_ENTRY *Entry;\r
1227 LIST_ENTRY *Next;\r
da1d0201 1228\r
1229 ASSERT (Map != NULL);\r
1230\r
1231 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Used) {\r
1232 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
1233\r
e48e37fc 1234 RemoveEntryList (&Item->Link);\r
da1d0201 1235 Map->Count--;\r
1236\r
e48e37fc 1237 gBS->FreePool (Item);\r
da1d0201 1238 }\r
1239\r
e48e37fc 1240 ASSERT ((Map->Count == 0) && IsListEmpty (&Map->Used));\r
da1d0201 1241\r
1242 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Recycled) {\r
1243 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
1244\r
e48e37fc 1245 RemoveEntryList (&Item->Link);\r
1246 gBS->FreePool (Item);\r
da1d0201 1247 }\r
1248\r
e48e37fc 1249 ASSERT (IsListEmpty (&Map->Recycled));\r
da1d0201 1250}\r
1251\r
1252\r
1253/**\r
b9008c87 1254 Test whether the netmap is empty and return true if it is.\r
1204fe83 1255\r
b9008c87 1256 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.\r
1204fe83 1257\r
b9008c87 1258 If Map is NULL, then ASSERT().\r
1204fe83 1259\r
1260\r
3e7104c2 1261 @param[in] Map The net map to test.\r
da1d0201 1262\r
1263 @return TRUE if the netmap is empty, otherwise FALSE.\r
1264\r
1265**/\r
1266BOOLEAN\r
7b414b4e 1267EFIAPI\r
da1d0201 1268NetMapIsEmpty (\r
1269 IN NET_MAP *Map\r
1270 )\r
1271{\r
1272 ASSERT (Map != NULL);\r
1273 return (BOOLEAN) (Map->Count == 0);\r
1274}\r
1275\r
1276\r
1277/**\r
1278 Return the number of the <Key, Value> pairs in the netmap.\r
1279\r
3e7104c2 1280 @param[in] Map The netmap to get the entry number.\r
da1d0201 1281\r
1282 @return The entry number in the netmap.\r
1283\r
1284**/\r
1285UINTN\r
7b414b4e 1286EFIAPI\r
da1d0201 1287NetMapGetCount (\r
1288 IN NET_MAP *Map\r
1289 )\r
1290{\r
1291 return Map->Count;\r
1292}\r
1293\r
1294\r
1295/**\r
1204fe83 1296 Return one allocated item.\r
1297\r
1298 If the Recycled doubly linked list of the netmap is empty, it will try to allocate\r
b9008c87 1299 a batch of items if there are enough resources and add corresponding nodes to the begining\r
1300 of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove\r
1301 the fist node entry of the Recycled doubly linked list and return the corresponding item.\r
1204fe83 1302\r
b9008c87 1303 If Map is NULL, then ASSERT().\r
1204fe83 1304\r
3e7104c2 1305 @param[in, out] Map The netmap to allocate item for.\r
da1d0201 1306\r
3e7104c2 1307 @return The allocated item. If NULL, the\r
1308 allocation failed due to resource limit.\r
da1d0201 1309\r
1310**/\r
da1d0201 1311NET_MAP_ITEM *\r
1312NetMapAllocItem (\r
3e7104c2 1313 IN OUT NET_MAP *Map\r
da1d0201 1314 )\r
1315{\r
1316 NET_MAP_ITEM *Item;\r
e48e37fc 1317 LIST_ENTRY *Head;\r
da1d0201 1318 UINTN Index;\r
1319\r
1320 ASSERT (Map != NULL);\r
1321\r
1322 Head = &Map->Recycled;\r
1323\r
e48e37fc 1324 if (IsListEmpty (Head)) {\r
da1d0201 1325 for (Index = 0; Index < NET_MAP_INCREAMENT; Index++) {\r
e48e37fc 1326 Item = AllocatePool (sizeof (NET_MAP_ITEM));\r
da1d0201 1327\r
1328 if (Item == NULL) {\r
1329 if (Index == 0) {\r
1330 return NULL;\r
1331 }\r
1332\r
1333 break;\r
1334 }\r
1335\r
e48e37fc 1336 InsertHeadList (Head, &Item->Link);\r
da1d0201 1337 }\r
1338 }\r
1339\r
1340 Item = NET_LIST_HEAD (Head, NET_MAP_ITEM, Link);\r
1341 NetListRemoveHead (Head);\r
1342\r
1343 return Item;\r
1344}\r
1345\r
1346\r
1347/**\r
1348 Allocate an item to save the <Key, Value> pair to the head of the netmap.\r
1204fe83 1349\r
b9008c87 1350 Allocate an item to save the <Key, Value> pair and add corresponding node entry\r
1204fe83 1351 to the beginning of the Used doubly linked list. The number of the <Key, Value>\r
b9008c87 1352 pairs in the netmap increase by 1.\r
da1d0201 1353\r
b9008c87 1354 If Map is NULL, then ASSERT().\r
1204fe83 1355\r
3e7104c2 1356 @param[in, out] Map The netmap to insert into.\r
1357 @param[in] Key The user's key.\r
1358 @param[in] Value The user's value for the key.\r
da1d0201 1359\r
3e7104c2 1360 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
1361 @retval EFI_SUCCESS The item is inserted to the head.\r
da1d0201 1362\r
1363**/\r
1364EFI_STATUS\r
7b414b4e 1365EFIAPI\r
da1d0201 1366NetMapInsertHead (\r
3e7104c2 1367 IN OUT NET_MAP *Map,\r
da1d0201 1368 IN VOID *Key,\r
1369 IN VOID *Value OPTIONAL\r
1370 )\r
1371{\r
1372 NET_MAP_ITEM *Item;\r
1373\r
1374 ASSERT (Map != NULL);\r
1375\r
1376 Item = NetMapAllocItem (Map);\r
1377\r
1378 if (Item == NULL) {\r
1379 return EFI_OUT_OF_RESOURCES;\r
1380 }\r
1381\r
1382 Item->Key = Key;\r
1383 Item->Value = Value;\r
e48e37fc 1384 InsertHeadList (&Map->Used, &Item->Link);\r
da1d0201 1385\r
1386 Map->Count++;\r
1387 return EFI_SUCCESS;\r
1388}\r
1389\r
1390\r
1391/**\r
1392 Allocate an item to save the <Key, Value> pair to the tail of the netmap.\r
1393\r
b9008c87 1394 Allocate an item to save the <Key, Value> pair and add corresponding node entry\r
1204fe83 1395 to the tail of the Used doubly linked list. The number of the <Key, Value>\r
b9008c87 1396 pairs in the netmap increase by 1.\r
1397\r
1398 If Map is NULL, then ASSERT().\r
1204fe83 1399\r
3e7104c2 1400 @param[in, out] Map The netmap to insert into.\r
1401 @param[in] Key The user's key.\r
1402 @param[in] Value The user's value for the key.\r
da1d0201 1403\r
3e7104c2 1404 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
1405 @retval EFI_SUCCESS The item is inserted to the tail.\r
da1d0201 1406\r
1407**/\r
1408EFI_STATUS\r
7b414b4e 1409EFIAPI\r
da1d0201 1410NetMapInsertTail (\r
3e7104c2 1411 IN OUT NET_MAP *Map,\r
da1d0201 1412 IN VOID *Key,\r
1413 IN VOID *Value OPTIONAL\r
1414 )\r
1415{\r
1416 NET_MAP_ITEM *Item;\r
1417\r
1418 ASSERT (Map != NULL);\r
1419\r
1420 Item = NetMapAllocItem (Map);\r
1421\r
1422 if (Item == NULL) {\r
1423 return EFI_OUT_OF_RESOURCES;\r
1424 }\r
1425\r
1426 Item->Key = Key;\r
1427 Item->Value = Value;\r
e48e37fc 1428 InsertTailList (&Map->Used, &Item->Link);\r
da1d0201 1429\r
1430 Map->Count++;\r
1431\r
1432 return EFI_SUCCESS;\r
1433}\r
1434\r
1435\r
1436/**\r
b9008c87 1437 Check whether the item is in the Map and return TRUE if it is.\r
da1d0201 1438\r
3e7104c2 1439 @param[in] Map The netmap to search within.\r
1440 @param[in] Item The item to search.\r
da1d0201 1441\r
1442 @return TRUE if the item is in the netmap, otherwise FALSE.\r
1443\r
1444**/\r
da1d0201 1445BOOLEAN\r
1446NetItemInMap (\r
1447 IN NET_MAP *Map,\r
1448 IN NET_MAP_ITEM *Item\r
1449 )\r
1450{\r
e48e37fc 1451 LIST_ENTRY *ListEntry;\r
da1d0201 1452\r
1453 NET_LIST_FOR_EACH (ListEntry, &Map->Used) {\r
1454 if (ListEntry == &Item->Link) {\r
1455 return TRUE;\r
1456 }\r
1457 }\r
1458\r
1459 return FALSE;\r
1460}\r
1461\r
1462\r
1463/**\r
b9008c87 1464 Find the key in the netmap and returns the point to the item contains the Key.\r
1204fe83 1465\r
1466 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every\r
b9008c87 1467 item with the key to search. It returns the point to the item contains the Key if found.\r
da1d0201 1468\r
b9008c87 1469 If Map is NULL, then ASSERT().\r
1204fe83 1470\r
3e7104c2 1471 @param[in] Map The netmap to search within.\r
1472 @param[in] Key The key to search.\r
da1d0201 1473\r
1474 @return The point to the item contains the Key, or NULL if Key isn't in the map.\r
1475\r
1476**/\r
1477NET_MAP_ITEM *\r
7b414b4e 1478EFIAPI\r
da1d0201 1479NetMapFindKey (\r
1480 IN NET_MAP *Map,\r
1481 IN VOID *Key\r
1482 )\r
1483{\r
e48e37fc 1484 LIST_ENTRY *Entry;\r
da1d0201 1485 NET_MAP_ITEM *Item;\r
1486\r
1487 ASSERT (Map != NULL);\r
1488\r
1489 NET_LIST_FOR_EACH (Entry, &Map->Used) {\r
1490 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
1491\r
1492 if (Item->Key == Key) {\r
1493 return Item;\r
1494 }\r
1495 }\r
1496\r
1497 return NULL;\r
1498}\r
1499\r
1500\r
1501/**\r
b9008c87 1502 Remove the node entry of the item from the netmap and return the key of the removed item.\r
1204fe83 1503\r
1504 Remove the node entry of the item from the Used doubly linked list of the netmap.\r
1505 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node\r
b9008c87 1506 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,\r
1507 Value will point to the value of the item. It returns the key of the removed item.\r
1204fe83 1508\r
b9008c87 1509 If Map is NULL, then ASSERT().\r
1510 If Item is NULL, then ASSERT().\r
1511 if item in not in the netmap, then ASSERT().\r
1204fe83 1512\r
3e7104c2 1513 @param[in, out] Map The netmap to remove the item from.\r
1514 @param[in, out] Item The item to remove.\r
1515 @param[out] Value The variable to receive the value if not NULL.\r
da1d0201 1516\r
3e7104c2 1517 @return The key of the removed item.\r
da1d0201 1518\r
1519**/\r
1520VOID *\r
7b414b4e 1521EFIAPI\r
da1d0201 1522NetMapRemoveItem (\r
3e7104c2 1523 IN OUT NET_MAP *Map,\r
1524 IN OUT NET_MAP_ITEM *Item,\r
1525 OUT VOID **Value OPTIONAL\r
da1d0201 1526 )\r
1527{\r
1528 ASSERT ((Map != NULL) && (Item != NULL));\r
1529 ASSERT (NetItemInMap (Map, Item));\r
1530\r
e48e37fc 1531 RemoveEntryList (&Item->Link);\r
da1d0201 1532 Map->Count--;\r
e48e37fc 1533 InsertHeadList (&Map->Recycled, &Item->Link);\r
da1d0201 1534\r
1535 if (Value != NULL) {\r
1536 *Value = Item->Value;\r
1537 }\r
1538\r
1539 return Item->Key;\r
1540}\r
1541\r
1542\r
1543/**\r
b9008c87 1544 Remove the first node entry on the netmap and return the key of the removed item.\r
da1d0201 1545\r
1204fe83 1546 Remove the first node entry from the Used doubly linked list of the netmap.\r
1547 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node\r
b9008c87 1548 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r
1549 parameter Value will point to the value of the item. It returns the key of the removed item.\r
1204fe83 1550\r
b9008c87 1551 If Map is NULL, then ASSERT().\r
1552 If the Used doubly linked list is empty, then ASSERT().\r
1204fe83 1553\r
3e7104c2 1554 @param[in, out] Map The netmap to remove the head from.\r
1555 @param[out] Value The variable to receive the value if not NULL.\r
da1d0201 1556\r
3e7104c2 1557 @return The key of the item removed.\r
da1d0201 1558\r
1559**/\r
1560VOID *\r
7b414b4e 1561EFIAPI\r
da1d0201 1562NetMapRemoveHead (\r
3e7104c2 1563 IN OUT NET_MAP *Map,\r
da1d0201 1564 OUT VOID **Value OPTIONAL\r
1565 )\r
1566{\r
1567 NET_MAP_ITEM *Item;\r
1568\r
1569 //\r
1570 // Often, it indicates a programming error to remove\r
1571 // the first entry in an empty list\r
1572 //\r
e48e37fc 1573 ASSERT (Map && !IsListEmpty (&Map->Used));\r
da1d0201 1574\r
1575 Item = NET_LIST_HEAD (&Map->Used, NET_MAP_ITEM, Link);\r
e48e37fc 1576 RemoveEntryList (&Item->Link);\r
da1d0201 1577 Map->Count--;\r
e48e37fc 1578 InsertHeadList (&Map->Recycled, &Item->Link);\r
da1d0201 1579\r
1580 if (Value != NULL) {\r
1581 *Value = Item->Value;\r
1582 }\r
1583\r
1584 return Item->Key;\r
1585}\r
1586\r
1587\r
1588/**\r
b9008c87 1589 Remove the last node entry on the netmap and return the key of the removed item.\r
da1d0201 1590\r
1204fe83 1591 Remove the last node entry from the Used doubly linked list of the netmap.\r
1592 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node\r
b9008c87 1593 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r
1594 parameter Value will point to the value of the item. It returns the key of the removed item.\r
1204fe83 1595\r
b9008c87 1596 If Map is NULL, then ASSERT().\r
1597 If the Used doubly linked list is empty, then ASSERT().\r
1204fe83 1598\r
3e7104c2 1599 @param[in, out] Map The netmap to remove the tail from.\r
1600 @param[out] Value The variable to receive the value if not NULL.\r
da1d0201 1601\r
3e7104c2 1602 @return The key of the item removed.\r
da1d0201 1603\r
1604**/\r
1605VOID *\r
7b414b4e 1606EFIAPI\r
da1d0201 1607NetMapRemoveTail (\r
3e7104c2 1608 IN OUT NET_MAP *Map,\r
da1d0201 1609 OUT VOID **Value OPTIONAL\r
1610 )\r
1611{\r
1612 NET_MAP_ITEM *Item;\r
1613\r
1614 //\r
1615 // Often, it indicates a programming error to remove\r
1616 // the last entry in an empty list\r
1617 //\r
e48e37fc 1618 ASSERT (Map && !IsListEmpty (&Map->Used));\r
da1d0201 1619\r
1620 Item = NET_LIST_TAIL (&Map->Used, NET_MAP_ITEM, Link);\r
e48e37fc 1621 RemoveEntryList (&Item->Link);\r
da1d0201 1622 Map->Count--;\r
e48e37fc 1623 InsertHeadList (&Map->Recycled, &Item->Link);\r
da1d0201 1624\r
1625 if (Value != NULL) {\r
1626 *Value = Item->Value;\r
1627 }\r
1628\r
1629 return Item->Key;\r
1630}\r
1631\r
1632\r
1633/**\r
b9008c87 1634 Iterate through the netmap and call CallBack for each item.\r
1204fe83 1635\r
b9008c87 1636 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break\r
1204fe83 1637 from the loop. It returns the CallBack's last return value. This function is\r
b9008c87 1638 delete safe for the current item.\r
da1d0201 1639\r
b9008c87 1640 If Map is NULL, then ASSERT().\r
1641 If CallBack is NULL, then ASSERT().\r
1204fe83 1642\r
3e7104c2 1643 @param[in] Map The Map to iterate through.\r
1644 @param[in] CallBack The callback function to call for each item.\r
1645 @param[in] Arg The opaque parameter to the callback.\r
da1d0201 1646\r
3e7104c2 1647 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item\r
1648 return EFI_SUCCESS.\r
1649 @retval Others It returns the CallBack's last return value.\r
da1d0201 1650\r
1651**/\r
1652EFI_STATUS\r
7b414b4e 1653EFIAPI\r
da1d0201 1654NetMapIterate (\r
1655 IN NET_MAP *Map,\r
1656 IN NET_MAP_CALLBACK CallBack,\r
e2851998 1657 IN VOID *Arg OPTIONAL\r
da1d0201 1658 )\r
1659{\r
1660\r
e48e37fc 1661 LIST_ENTRY *Entry;\r
1662 LIST_ENTRY *Next;\r
1663 LIST_ENTRY *Head;\r
b9008c87 1664 NET_MAP_ITEM *Item;\r
1665 EFI_STATUS Result;\r
da1d0201 1666\r
1667 ASSERT ((Map != NULL) && (CallBack != NULL));\r
1668\r
1669 Head = &Map->Used;\r
1670\r
e48e37fc 1671 if (IsListEmpty (Head)) {\r
da1d0201 1672 return EFI_SUCCESS;\r
1673 }\r
1674\r
1675 NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {\r
1676 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
1677 Result = CallBack (Map, Item, Arg);\r
1678\r
1679 if (EFI_ERROR (Result)) {\r
1680 return Result;\r
1681 }\r
1682 }\r
1683\r
1684 return EFI_SUCCESS;\r
1685}\r
1686\r
1687\r
1dc1b43f 1688/**\r
1689 Internal function to get the child handle of the NIC handle.\r
1690\r
1691 @param[in] Controller NIC controller handle.\r
1692 @param[out] ChildHandle Returned child handle.\r
1693\r
1694 @retval EFI_SUCCESS Successfully to get child handle.\r
1695 @retval Others Failed to get child handle.\r
1696\r
1697**/\r
1698EFI_STATUS\r
1699NetGetChildHandle (\r
1700 IN EFI_HANDLE Controller,\r
1701 OUT EFI_HANDLE *ChildHandle\r
1702 )\r
1703{\r
1704 EFI_STATUS Status;\r
1705 EFI_HANDLE *Handles;\r
1706 UINTN HandleCount;\r
1707 UINTN Index;\r
1708 EFI_DEVICE_PATH_PROTOCOL *ChildDeviceDevicePath;\r
1709 VENDOR_DEVICE_PATH *VendorDeviceNode;\r
1710\r
1711 //\r
1712 // Locate all EFI Hii Config Access protocols\r
1713 //\r
1714 Status = gBS->LocateHandleBuffer (\r
1715 ByProtocol,\r
1716 &gEfiHiiConfigAccessProtocolGuid,\r
1717 NULL,\r
1718 &HandleCount,\r
1719 &Handles\r
1720 );\r
1721 if (EFI_ERROR (Status) || (HandleCount == 0)) {\r
1722 return Status;\r
1723 }\r
1724\r
1725 Status = EFI_NOT_FOUND;\r
1726\r
1727 for (Index = 0; Index < HandleCount; Index++) {\r
1728\r
1729 Status = EfiTestChildHandle (Controller, Handles[Index], &gEfiManagedNetworkServiceBindingProtocolGuid);\r
1730 if (!EFI_ERROR (Status)) {\r
1731 //\r
1732 // Get device path on the child handle\r
1733 //\r
1734 Status = gBS->HandleProtocol (\r
1735 Handles[Index],\r
1736 &gEfiDevicePathProtocolGuid,\r
1737 (VOID **) &ChildDeviceDevicePath\r
1738 );\r
1739\r
1740 if (!EFI_ERROR (Status)) {\r
1741 while (!IsDevicePathEnd (ChildDeviceDevicePath)) {\r
1742 ChildDeviceDevicePath = NextDevicePathNode (ChildDeviceDevicePath);\r
1743 //\r
1744 // Parse one instance\r
1745 //\r
1746 if (ChildDeviceDevicePath->Type == HARDWARE_DEVICE_PATH &&\r
1747 ChildDeviceDevicePath->SubType == HW_VENDOR_DP) {\r
1748 VendorDeviceNode = (VENDOR_DEVICE_PATH *) ChildDeviceDevicePath;\r
1749 if (CompareMem (&VendorDeviceNode->Guid, &gEfiNicIp4ConfigVariableGuid, sizeof (EFI_GUID)) == 0) {\r
1750 //\r
1751 // Found item matched gEfiNicIp4ConfigVariableGuid\r
1752 //\r
1753 *ChildHandle = Handles[Index];\r
ad108abe 1754 FreePool (Handles);\r
1dc1b43f 1755 return EFI_SUCCESS;\r
1756 }\r
1757 }\r
1758 }\r
1759 }\r
1760 }\r
1761 }\r
1762\r
ad108abe 1763 FreePool (Handles);\r
1dc1b43f 1764 return Status;\r
1765}\r
1766\r
1767\r
da1d0201 1768/**\r
1769 This is the default unload handle for all the network drivers.\r
1770\r
b9008c87 1771 Disconnect the driver specified by ImageHandle from all the devices in the handle database.\r
1772 Uninstall all the protocols installed in the driver entry point.\r
1204fe83 1773\r
3e7104c2 1774 @param[in] ImageHandle The drivers' driver image.\r
da1d0201 1775\r
1776 @retval EFI_SUCCESS The image is unloaded.\r
1777 @retval Others Failed to unload the image.\r
1778\r
1779**/\r
1780EFI_STATUS\r
1781EFIAPI\r
1782NetLibDefaultUnload (\r
1783 IN EFI_HANDLE ImageHandle\r
1784 )\r
1785{\r
1786 EFI_STATUS Status;\r
1787 EFI_HANDLE *DeviceHandleBuffer;\r
1788 UINTN DeviceHandleCount;\r
1789 UINTN Index;\r
1790 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;\r
1791 EFI_COMPONENT_NAME_PROTOCOL *ComponentName;\r
3012ce5c 1792 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r
da1d0201 1793\r
1794 //\r
1795 // Get the list of all the handles in the handle database.\r
1796 // If there is an error getting the list, then the unload\r
1797 // operation fails.\r
1798 //\r
1799 Status = gBS->LocateHandleBuffer (\r
1800 AllHandles,\r
1801 NULL,\r
1802 NULL,\r
1803 &DeviceHandleCount,\r
1804 &DeviceHandleBuffer\r
1805 );\r
1806\r
1807 if (EFI_ERROR (Status)) {\r
1808 return Status;\r
1809 }\r
1810\r
1811 //\r
1812 // Disconnect the driver specified by ImageHandle from all\r
1813 // the devices in the handle database.\r
1814 //\r
1815 for (Index = 0; Index < DeviceHandleCount; Index++) {\r
1816 Status = gBS->DisconnectController (\r
1817 DeviceHandleBuffer[Index],\r
1818 ImageHandle,\r
1819 NULL\r
1820 );\r
1821 }\r
1822\r
1823 //\r
1824 // Uninstall all the protocols installed in the driver entry point\r
1825 //\r
1826 for (Index = 0; Index < DeviceHandleCount; Index++) {\r
1827 Status = gBS->HandleProtocol (\r
1828 DeviceHandleBuffer[Index],\r
1829 &gEfiDriverBindingProtocolGuid,\r
1830 (VOID **) &DriverBinding\r
1831 );\r
1832\r
1833 if (EFI_ERROR (Status)) {\r
1834 continue;\r
1835 }\r
1836\r
1837 if (DriverBinding->ImageHandle != ImageHandle) {\r
1838 continue;\r
1839 }\r
1840\r
1841 gBS->UninstallProtocolInterface (\r
1842 ImageHandle,\r
1843 &gEfiDriverBindingProtocolGuid,\r
1844 DriverBinding\r
1845 );\r
1846 Status = gBS->HandleProtocol (\r
1847 DeviceHandleBuffer[Index],\r
1848 &gEfiComponentNameProtocolGuid,\r
1849 (VOID **) &ComponentName\r
1850 );\r
1851 if (!EFI_ERROR (Status)) {\r
1852 gBS->UninstallProtocolInterface (\r
1853 ImageHandle,\r
1854 &gEfiComponentNameProtocolGuid,\r
1855 ComponentName\r
1856 );\r
1857 }\r
1858\r
1859 Status = gBS->HandleProtocol (\r
1860 DeviceHandleBuffer[Index],\r
3012ce5c 1861 &gEfiComponentName2ProtocolGuid,\r
1862 (VOID **) &ComponentName2\r
da1d0201 1863 );\r
da1d0201 1864 if (!EFI_ERROR (Status)) {\r
1865 gBS->UninstallProtocolInterface (\r
3012ce5c 1866 ImageHandle,\r
1867 &gEfiComponentName2ProtocolGuid,\r
1868 ComponentName2\r
1869 );\r
da1d0201 1870 }\r
1871 }\r
1872\r
1873 //\r
1874 // Free the buffer containing the list of handles from the handle database\r
1875 //\r
1876 if (DeviceHandleBuffer != NULL) {\r
1877 gBS->FreePool (DeviceHandleBuffer);\r
1878 }\r
1879\r
1880 return EFI_SUCCESS;\r
1881}\r
1882\r
1883\r
1884\r
1885/**\r
1886 Create a child of the service that is identified by ServiceBindingGuid.\r
1204fe83 1887\r
b9008c87 1888 Get the ServiceBinding Protocol first, then use it to create a child.\r
da1d0201 1889\r
b9008c87 1890 If ServiceBindingGuid is NULL, then ASSERT().\r
1891 If ChildHandle is NULL, then ASSERT().\r
1204fe83 1892\r
3e7104c2 1893 @param[in] Controller The controller which has the service installed.\r
1894 @param[in] Image The image handle used to open service.\r
1895 @param[in] ServiceBindingGuid The service's Guid.\r
8f5e6151 1896 @param[in, out] ChildHandle The handle to receive the create child.\r
da1d0201 1897\r
1898 @retval EFI_SUCCESS The child is successfully created.\r
1899 @retval Others Failed to create the child.\r
1900\r
1901**/\r
1902EFI_STATUS\r
7b414b4e 1903EFIAPI\r
da1d0201 1904NetLibCreateServiceChild (\r
1905 IN EFI_HANDLE Controller,\r
1906 IN EFI_HANDLE Image,\r
1907 IN EFI_GUID *ServiceBindingGuid,\r
3e7104c2 1908 IN OUT EFI_HANDLE *ChildHandle\r
da1d0201 1909 )\r
1910{\r
1911 EFI_STATUS Status;\r
1912 EFI_SERVICE_BINDING_PROTOCOL *Service;\r
1913\r
1914\r
1915 ASSERT ((ServiceBindingGuid != NULL) && (ChildHandle != NULL));\r
1916\r
1917 //\r
1918 // Get the ServiceBinding Protocol\r
1919 //\r
1920 Status = gBS->OpenProtocol (\r
1921 Controller,\r
1922 ServiceBindingGuid,\r
1923 (VOID **) &Service,\r
1924 Image,\r
1925 Controller,\r
1926 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1927 );\r
1928\r
1929 if (EFI_ERROR (Status)) {\r
1930 return Status;\r
1931 }\r
1932\r
1933 //\r
1934 // Create a child\r
1935 //\r
1936 Status = Service->CreateChild (Service, ChildHandle);\r
1937 return Status;\r
1938}\r
1939\r
1940\r
1941/**\r
75dce340 1942 Destroy a child of the service that is identified by ServiceBindingGuid.\r
1204fe83 1943\r
b9008c87 1944 Get the ServiceBinding Protocol first, then use it to destroy a child.\r
1204fe83 1945\r
b9008c87 1946 If ServiceBindingGuid is NULL, then ASSERT().\r
1204fe83 1947\r
3e7104c2 1948 @param[in] Controller The controller which has the service installed.\r
1949 @param[in] Image The image handle used to open service.\r
1950 @param[in] ServiceBindingGuid The service's Guid.\r
75dce340 1951 @param[in] ChildHandle The child to destroy.\r
da1d0201 1952\r
75dce340 1953 @retval EFI_SUCCESS The child is successfully destroyed.\r
1954 @retval Others Failed to destroy the child.\r
da1d0201 1955\r
1956**/\r
1957EFI_STATUS\r
7b414b4e 1958EFIAPI\r
da1d0201 1959NetLibDestroyServiceChild (\r
1960 IN EFI_HANDLE Controller,\r
1961 IN EFI_HANDLE Image,\r
1962 IN EFI_GUID *ServiceBindingGuid,\r
1963 IN EFI_HANDLE ChildHandle\r
1964 )\r
1965{\r
1966 EFI_STATUS Status;\r
1967 EFI_SERVICE_BINDING_PROTOCOL *Service;\r
1968\r
1969 ASSERT (ServiceBindingGuid != NULL);\r
1970\r
1971 //\r
1972 // Get the ServiceBinding Protocol\r
1973 //\r
1974 Status = gBS->OpenProtocol (\r
1975 Controller,\r
1976 ServiceBindingGuid,\r
1977 (VOID **) &Service,\r
1978 Image,\r
1979 Controller,\r
1980 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1981 );\r
1982\r
1983 if (EFI_ERROR (Status)) {\r
1984 return Status;\r
1985 }\r
1986\r
1987 //\r
75dce340 1988 // destroy the child\r
da1d0201 1989 //\r
1990 Status = Service->DestroyChild (Service, ChildHandle);\r
1991 return Status;\r
1992}\r
1993\r
779ae357 1994/**\r
1995 Get handle with Simple Network Protocol installed on it.\r
1996\r
1997 There should be MNP Service Binding Protocol installed on the input ServiceHandle.\r
1998 If Simple Network Protocol is already installed on the ServiceHandle, the\r
1999 ServiceHandle will be returned. If SNP is not installed on the ServiceHandle,\r
2000 try to find its parent handle with SNP installed.\r
2001\r
2002 @param[in] ServiceHandle The handle where network service binding protocols are\r
2003 installed on.\r
2004 @param[out] Snp The pointer to store the address of the SNP instance.\r
2005 This is an optional parameter that may be NULL.\r
2006\r
2007 @return The SNP handle, or NULL if not found.\r
2008\r
2009**/\r
2010EFI_HANDLE\r
2011EFIAPI\r
2012NetLibGetSnpHandle (\r
2013 IN EFI_HANDLE ServiceHandle,\r
2014 OUT EFI_SIMPLE_NETWORK_PROTOCOL **Snp OPTIONAL\r
2015 )\r
2016{\r
2017 EFI_STATUS Status;\r
2018 EFI_SIMPLE_NETWORK_PROTOCOL *SnpInstance;\r
2019 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2020 EFI_HANDLE SnpHandle;\r
2021\r
2022 //\r
2023 // Try to open SNP from ServiceHandle\r
2024 //\r
2025 SnpInstance = NULL;\r
2026 Status = gBS->HandleProtocol (ServiceHandle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &SnpInstance);\r
2027 if (!EFI_ERROR (Status)) {\r
2028 if (Snp != NULL) {\r
2029 *Snp = SnpInstance;\r
2030 }\r
2031 return ServiceHandle;\r
2032 }\r
2033\r
2034 //\r
2035 // Failed to open SNP, try to get SNP handle by LocateDevicePath()\r
2036 //\r
2037 DevicePath = DevicePathFromHandle (ServiceHandle);\r
2038 if (DevicePath == NULL) {\r
2039 return NULL;\r
2040 }\r
2041\r
2042 SnpHandle = NULL;\r
2043 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &DevicePath, &SnpHandle);\r
2044 if (EFI_ERROR (Status)) {\r
2045 //\r
2046 // Failed to find SNP handle\r
2047 //\r
2048 return NULL;\r
2049 }\r
2050\r
2051 Status = gBS->HandleProtocol (SnpHandle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &SnpInstance);\r
2052 if (!EFI_ERROR (Status)) {\r
2053 if (Snp != NULL) {\r
2054 *Snp = SnpInstance;\r
2055 }\r
2056 return SnpHandle;\r
2057 }\r
2058\r
2059 return NULL;\r
2060}\r
2061\r
2062/**\r
2063 Retrieve VLAN ID of a VLAN device handle.\r
2064\r
2065 Search VLAN device path node in Device Path of specified ServiceHandle and\r
2066 return its VLAN ID. If no VLAN device path node found, then this ServiceHandle\r
2067 is not a VLAN device handle, and 0 will be returned.\r
2068\r
2069 @param[in] ServiceHandle The handle where network service binding protocols are\r
2070 installed on.\r
2071\r
2072 @return VLAN ID of the device handle, or 0 if not a VLAN device.\r
2073\r
2074**/\r
2075UINT16\r
2076EFIAPI\r
2077NetLibGetVlanId (\r
2078 IN EFI_HANDLE ServiceHandle\r
2079 )\r
2080{\r
2081 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2082 EFI_DEVICE_PATH_PROTOCOL *Node;\r
2083\r
2084 DevicePath = DevicePathFromHandle (ServiceHandle);\r
2085 if (DevicePath == NULL) {\r
2086 return 0;\r
2087 }\r
2088\r
2089 Node = DevicePath;\r
2090 while (!IsDevicePathEnd (Node)) {\r
2091 if (Node->Type == MESSAGING_DEVICE_PATH && Node->SubType == MSG_VLAN_DP) {\r
2092 return ((VLAN_DEVICE_PATH *) Node)->VlanId;\r
2093 }\r
2094 Node = NextDevicePathNode (Node);\r
2095 }\r
2096\r
2097 return 0;\r
2098}\r
2099\r
2100/**\r
2101 Find VLAN device handle with specified VLAN ID.\r
2102\r
2103 The VLAN child device handle is created by VLAN Config Protocol on ControllerHandle.\r
2104 This function will append VLAN device path node to the parent device path,\r
2105 and then use LocateDevicePath() to find the correct VLAN device handle.\r
2106\r
e2851998 2107 @param[in] ControllerHandle The handle where network service binding protocols are\r
779ae357 2108 installed on.\r
e2851998 2109 @param[in] VlanId The configured VLAN ID for the VLAN device.\r
779ae357 2110\r
2111 @return The VLAN device handle, or NULL if not found.\r
2112\r
2113**/\r
2114EFI_HANDLE\r
2115EFIAPI\r
2116NetLibGetVlanHandle (\r
2117 IN EFI_HANDLE ControllerHandle,\r
2118 IN UINT16 VlanId\r
2119 )\r
2120{\r
2121 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
2122 EFI_DEVICE_PATH_PROTOCOL *VlanDevicePath;\r
2123 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2124 VLAN_DEVICE_PATH VlanNode;\r
2125 EFI_HANDLE Handle;\r
2126\r
2127 ParentDevicePath = DevicePathFromHandle (ControllerHandle);\r
2128 if (ParentDevicePath == NULL) {\r
2129 return NULL;\r
2130 }\r
2131\r
2132 //\r
2133 // Construct VLAN device path\r
2134 //\r
2135 CopyMem (&VlanNode, &mNetVlanDevicePathTemplate, sizeof (VLAN_DEVICE_PATH));\r
2136 VlanNode.VlanId = VlanId;\r
2137 VlanDevicePath = AppendDevicePathNode (\r
2138 ParentDevicePath,\r
2139 (EFI_DEVICE_PATH_PROTOCOL *) &VlanNode\r
2140 );\r
2141 if (VlanDevicePath == NULL) {\r
2142 return NULL;\r
2143 }\r
2144\r
2145 //\r
2146 // Find VLAN device handle\r
2147 //\r
2148 Handle = NULL;\r
2149 DevicePath = VlanDevicePath;\r
2150 gBS->LocateDevicePath (\r
2151 &gEfiDevicePathProtocolGuid,\r
2152 &DevicePath,\r
2153 &Handle\r
2154 );\r
2155 if (!IsDevicePathEnd (DevicePath)) {\r
2156 //\r
2157 // Device path is not exactly match\r
2158 //\r
2159 Handle = NULL;\r
2160 }\r
2161\r
2162 FreePool (VlanDevicePath);\r
2163 return Handle;\r
2164}\r
da1d0201 2165\r
2166/**\r
779ae357 2167 Get MAC address associated with the network service handle.\r
2168\r
2169 There should be MNP Service Binding Protocol installed on the input ServiceHandle.\r
2170 If SNP is installed on the ServiceHandle or its parent handle, MAC address will\r
2171 be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP.\r
2172\r
2173 @param[in] ServiceHandle The handle where network service binding protocols are\r
2174 installed on.\r
2175 @param[out] MacAddress The pointer to store the returned MAC address.\r
2176 @param[out] AddressSize The length of returned MAC address.\r
2177\r
2178 @retval EFI_SUCCESS MAC address is returned successfully.\r
2179 @retval Others Failed to get SNP mode data.\r
2180\r
2181**/\r
2182EFI_STATUS\r
2183EFIAPI\r
2184NetLibGetMacAddress (\r
2185 IN EFI_HANDLE ServiceHandle,\r
2186 OUT EFI_MAC_ADDRESS *MacAddress,\r
2187 OUT UINTN *AddressSize\r
2188 )\r
2189{\r
2190 EFI_STATUS Status;\r
2191 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
2192 EFI_SIMPLE_NETWORK_MODE *SnpMode;\r
2193 EFI_SIMPLE_NETWORK_MODE SnpModeData;\r
2194 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;\r
2195 EFI_SERVICE_BINDING_PROTOCOL *MnpSb;\r
2196 EFI_HANDLE *SnpHandle;\r
2197 EFI_HANDLE MnpChildHandle;\r
2198\r
2199 ASSERT (MacAddress != NULL);\r
2200 ASSERT (AddressSize != NULL);\r
2201\r
2202 //\r
2203 // Try to get SNP handle\r
2204 //\r
2205 Snp = NULL;\r
2206 SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp);\r
2207 if (SnpHandle != NULL) {\r
2208 //\r
2209 // SNP found, use it directly\r
2210 //\r
2211 SnpMode = Snp->Mode;\r
2212 } else {\r
2213 //\r
2214 // Failed to get SNP handle, try to get MAC address from MNP\r
2215 //\r
2216 MnpChildHandle = NULL;\r
2217 Status = gBS->HandleProtocol (\r
2218 ServiceHandle,\r
2219 &gEfiManagedNetworkServiceBindingProtocolGuid,\r
2220 (VOID **) &MnpSb\r
2221 );\r
2222 if (EFI_ERROR (Status)) {\r
2223 return Status;\r
2224 }\r
2225\r
2226 //\r
2227 // Create a MNP child\r
2228 //\r
2229 Status = MnpSb->CreateChild (MnpSb, &MnpChildHandle);\r
2230 if (EFI_ERROR (Status)) {\r
2231 return Status;\r
2232 }\r
2233\r
2234 //\r
2235 // Open MNP protocol\r
2236 //\r
2237 Status = gBS->HandleProtocol (\r
2238 MnpChildHandle,\r
2239 &gEfiManagedNetworkProtocolGuid,\r
2240 (VOID **) &Mnp\r
2241 );\r
2242 if (EFI_ERROR (Status)) {\r
e20d6513 2243 MnpSb->DestroyChild (MnpSb, MnpChildHandle);\r
779ae357 2244 return Status;\r
2245 }\r
da1d0201 2246\r
779ae357 2247 //\r
2248 // Try to get SNP mode from MNP\r
2249 //\r
2250 Status = Mnp->GetModeData (Mnp, NULL, &SnpModeData);\r
2251 if (EFI_ERROR (Status)) {\r
e20d6513 2252 MnpSb->DestroyChild (MnpSb, MnpChildHandle);\r
779ae357 2253 return Status;\r
2254 }\r
2255 SnpMode = &SnpModeData;\r
b9008c87 2256\r
779ae357 2257 //\r
2258 // Destroy the MNP child\r
2259 //\r
2260 MnpSb->DestroyChild (MnpSb, MnpChildHandle);\r
2261 }\r
b9008c87 2262\r
779ae357 2263 *AddressSize = SnpMode->HwAddressSize;\r
2264 CopyMem (MacAddress->Addr, SnpMode->CurrentAddress.Addr, SnpMode->HwAddressSize);\r
2265\r
2266 return EFI_SUCCESS;\r
2267}\r
2268\r
2269/**\r
2270 Convert MAC address of the NIC associated with specified Service Binding Handle\r
2271 to a unicode string. Callers are responsible for freeing the string storage.\r
2272\r
2273 Locate simple network protocol associated with the Service Binding Handle and\r
2274 get the mac address from SNP. Then convert the mac address into a unicode\r
2275 string. It takes 2 unicode characters to represent a 1 byte binary buffer.\r
2276 Plus one unicode character for the null-terminator.\r
2277\r
2278 @param[in] ServiceHandle The handle where network service binding protocol is\r
3e7104c2 2279 installed on.\r
2280 @param[in] ImageHandle The image handle used to act as the agent handle to\r
b00ed21a 2281 get the simple network protocol. This parameter is\r
2282 optional and may be NULL.\r
3e7104c2 2283 @param[out] MacString The pointer to store the address of the string\r
2284 representation of the mac address.\r
1204fe83 2285\r
3e7104c2 2286 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.\r
da1d0201 2287 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.\r
3e7104c2 2288 @retval Others Failed to open the simple network protocol.\r
da1d0201 2289\r
2290**/\r
2291EFI_STATUS\r
7b414b4e 2292EFIAPI\r
da1d0201 2293NetLibGetMacString (\r
779ae357 2294 IN EFI_HANDLE ServiceHandle,\r
b00ed21a 2295 IN EFI_HANDLE ImageHandle, OPTIONAL\r
3e7104c2 2296 OUT CHAR16 **MacString\r
da1d0201 2297 )\r
2298{\r
2299 EFI_STATUS Status;\r
779ae357 2300 EFI_MAC_ADDRESS MacAddress;\r
1204fe83 2301 UINT8 *HwAddress;\r
779ae357 2302 UINTN HwAddressSize;\r
2303 UINT16 VlanId;\r
2304 CHAR16 *String;\r
da1d0201 2305 UINTN Index;\r
2306\r
779ae357 2307 ASSERT (MacString != NULL);\r
da1d0201 2308\r
2309 //\r
779ae357 2310 // Get MAC address of the network device\r
da1d0201 2311 //\r
779ae357 2312 Status = NetLibGetMacAddress (ServiceHandle, &MacAddress, &HwAddressSize);\r
da1d0201 2313 if (EFI_ERROR (Status)) {\r
2314 return Status;\r
2315 }\r
2316\r
da1d0201 2317 //\r
2318 // It takes 2 unicode characters to represent a 1 byte binary buffer.\r
779ae357 2319 // If VLAN is configured, it will need extra 5 characters like "\0005".\r
da1d0201 2320 // Plus one unicode character for the null-terminator.\r
2321 //\r
779ae357 2322 String = AllocateZeroPool ((2 * HwAddressSize + 5 + 1) * sizeof (CHAR16));\r
2323 if (String == NULL) {\r
da1d0201 2324 return EFI_OUT_OF_RESOURCES;\r
2325 }\r
779ae357 2326 *MacString = String;\r
da1d0201 2327\r
2328 //\r
779ae357 2329 // Convert the MAC address into a unicode string.\r
da1d0201 2330 //\r
779ae357 2331 HwAddress = &MacAddress.Addr[0];\r
2332 for (Index = 0; Index < HwAddressSize; Index++) {\r
2333 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *(HwAddress++), 2);\r
da1d0201 2334 }\r
2335\r
779ae357 2336 //\r
2337 // Append VLAN ID if any\r
2338 //\r
2339 VlanId = NetLibGetVlanId (ServiceHandle);\r
2340 if (VlanId != 0) {\r
2341 *String++ = L'\\';\r
2342 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, VlanId, 4);\r
2343 }\r
da1d0201 2344\r
779ae357 2345 //\r
2346 // Null terminate the Unicode string\r
2347 //\r
2348 *String = L'\0';\r
da1d0201 2349\r
2350 return EFI_SUCCESS;\r
2351}\r
2352\r
dd29f3ed 2353/**\r
2354 Detect media status for specified network device.\r
2355\r
2356 The underlying UNDI driver may or may not support reporting media status from\r
2357 GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine\r
2358 will try to invoke Snp->GetStatus() to get the media status: if media already\r
2359 present, it return directly; if media not present, it will stop SNP and then\r
2360 restart SNP to get the latest media status, this give chance to get the correct\r
2361 media status for old UNDI driver which doesn't support reporting media status\r
2362 from GET_STATUS command.\r
2363 Note: there will be two limitations for current algorithm:\r
2364 1) for UNDI with this capability, in case of cable is not attached, there will\r
2365 be an redundant Stop/Start() process;\r
3b1464d5 2366 2) for UNDI without this capability, in case that network cable is attached when\r
2367 Snp->Initialize() is invoked while network cable is unattached later,\r
2368 NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer\r
2369 apps to wait for timeout time.\r
dd29f3ed 2370\r
2371 @param[in] ServiceHandle The handle where network service binding protocols are\r
2372 installed on.\r
2373 @param[out] MediaPresent The pointer to store the media status.\r
2374\r
2375 @retval EFI_SUCCESS Media detection success.\r
2376 @retval EFI_INVALID_PARAMETER ServiceHandle is not valid network device handle.\r
2377 @retval EFI_UNSUPPORTED Network device does not support media detection.\r
2378 @retval EFI_DEVICE_ERROR SNP is in unknown state.\r
2379\r
2380**/\r
2381EFI_STATUS\r
2382EFIAPI\r
2383NetLibDetectMedia (\r
2384 IN EFI_HANDLE ServiceHandle,\r
2385 OUT BOOLEAN *MediaPresent\r
2386 )\r
2387{\r
2388 EFI_STATUS Status;\r
2389 EFI_HANDLE SnpHandle;\r
2390 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
2391 UINT32 InterruptStatus;\r
2392 UINT32 OldState;\r
2393 EFI_MAC_ADDRESS *MCastFilter;\r
2394 UINT32 MCastFilterCount;\r
2395 UINT32 EnableFilterBits;\r
2396 UINT32 DisableFilterBits;\r
2397 BOOLEAN ResetMCastFilters;\r
2398\r
2399 ASSERT (MediaPresent != NULL);\r
2400\r
2401 //\r
2402 // Get SNP handle\r
2403 //\r
2404 Snp = NULL;\r
2405 SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp);\r
2406 if (SnpHandle == NULL) {\r
2407 return EFI_INVALID_PARAMETER;\r
2408 }\r
2409\r
2410 //\r
2411 // Check whether SNP support media detection\r
2412 //\r
2413 if (!Snp->Mode->MediaPresentSupported) {\r
2414 return EFI_UNSUPPORTED;\r
2415 }\r
2416\r
2417 //\r
2418 // Invoke Snp->GetStatus() to refresh MediaPresent field in SNP mode data\r
2419 //\r
2420 Status = Snp->GetStatus (Snp, &InterruptStatus, NULL);\r
2421 if (EFI_ERROR (Status)) {\r
2422 return Status;\r
2423 }\r
2424\r
2425 if (Snp->Mode->MediaPresent) {\r
2426 //\r
2427 // Media is present, return directly\r
2428 //\r
2429 *MediaPresent = TRUE;\r
2430 return EFI_SUCCESS;\r
2431 }\r
2432\r
2433 //\r
2434 // Till now, GetStatus() report no media; while, in case UNDI not support\r
2435 // reporting media status from GetStatus(), this media status may be incorrect.\r
2436 // So, we will stop SNP and then restart it to get the correct media status.\r
2437 //\r
2438 OldState = Snp->Mode->State;\r
2439 if (OldState >= EfiSimpleNetworkMaxState) {\r
2440 return EFI_DEVICE_ERROR;\r
2441 }\r
2442\r
2443 MCastFilter = NULL;\r
2444\r
2445 if (OldState == EfiSimpleNetworkInitialized) {\r
2446 //\r
2447 // SNP is already in use, need Shutdown/Stop and then Start/Initialize\r
2448 //\r
2449\r
2450 //\r
2451 // Backup current SNP receive filter settings\r
2452 //\r
2453 EnableFilterBits = Snp->Mode->ReceiveFilterSetting;\r
2454 DisableFilterBits = Snp->Mode->ReceiveFilterMask ^ EnableFilterBits;\r
2455\r
2456 ResetMCastFilters = TRUE;\r
2457 MCastFilterCount = Snp->Mode->MCastFilterCount;\r
2458 if (MCastFilterCount != 0) {\r
2459 MCastFilter = AllocateCopyPool (\r
2460 MCastFilterCount * sizeof (EFI_MAC_ADDRESS),\r
2461 Snp->Mode->MCastFilter\r
2462 );\r
2463 ASSERT (MCastFilter != NULL);\r
2464\r
2465 ResetMCastFilters = FALSE;\r
2466 }\r
2467\r
2468 //\r
2469 // Shutdown/Stop the simple network\r
2470 //\r
2471 Status = Snp->Shutdown (Snp);\r
2472 if (!EFI_ERROR (Status)) {\r
2473 Status = Snp->Stop (Snp);\r
2474 }\r
2475 if (EFI_ERROR (Status)) {\r
2476 goto Exit;\r
2477 }\r
2478\r
2479 //\r
2480 // Start/Initialize the simple network\r
2481 //\r
2482 Status = Snp->Start (Snp);\r
2483 if (!EFI_ERROR (Status)) {\r
2484 Status = Snp->Initialize (Snp, 0, 0);\r
2485 }\r
2486 if (EFI_ERROR (Status)) {\r
2487 goto Exit;\r
2488 }\r
2489\r
2490 //\r
2491 // Here we get the correct media status\r
2492 //\r
2493 *MediaPresent = Snp->Mode->MediaPresent;\r
2494\r
2495 //\r
2496 // Restore SNP receive filter settings\r
2497 //\r
2498 Status = Snp->ReceiveFilters (\r
2499 Snp,\r
2500 EnableFilterBits,\r
2501 DisableFilterBits,\r
2502 ResetMCastFilters,\r
2503 MCastFilterCount,\r
2504 MCastFilter\r
2505 );\r
2506\r
2507 if (MCastFilter != NULL) {\r
2508 FreePool (MCastFilter);\r
2509 }\r
2510\r
2511 return Status;\r
2512 }\r
2513\r
2514 //\r
2515 // SNP is not in use, it's in state of EfiSimpleNetworkStopped or EfiSimpleNetworkStarted\r
2516 //\r
2517 if (OldState == EfiSimpleNetworkStopped) {\r
2518 //\r
2519 // SNP not start yet, start it\r
2520 //\r
2521 Status = Snp->Start (Snp);\r
2522 if (EFI_ERROR (Status)) {\r
2523 goto Exit;\r
2524 }\r
2525 }\r
2526\r
2527 //\r
2528 // Initialize the simple network\r
2529 //\r
2530 Status = Snp->Initialize (Snp, 0, 0);\r
2531 if (EFI_ERROR (Status)) {\r
2532 Status = EFI_DEVICE_ERROR;\r
2533 goto Exit;\r
2534 }\r
2535\r
2536 //\r
2537 // Here we get the correct media status\r
2538 //\r
2539 *MediaPresent = Snp->Mode->MediaPresent;\r
2540\r
2541 //\r
2542 // Shut down the simple network\r
2543 //\r
2544 Snp->Shutdown (Snp);\r
2545\r
2546Exit:\r
2547 if (OldState == EfiSimpleNetworkStopped) {\r
2548 //\r
2549 // Original SNP sate is Stopped, restore to original state\r
2550 //\r
2551 Snp->Stop (Snp);\r
2552 }\r
2553\r
2554 if (MCastFilter != NULL) {\r
2555 FreePool (MCastFilter);\r
2556 }\r
2557\r
2558 return Status;\r
2559}\r
2560\r
da1d0201 2561/**\r
2562 Check the default address used by the IPv4 driver is static or dynamic (acquired\r
2563 from DHCP).\r
2564\r
1204fe83 2565 If the controller handle does not have the NIC Ip4 Config Protocol installed, the\r
b9008c87 2566 default address is static. If the EFI variable to save the configuration is not found,\r
1204fe83 2567 the default address is static. Otherwise, get the result from the EFI variable which\r
b9008c87 2568 saving the configuration.\r
1204fe83 2569\r
3e7104c2 2570 @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol\r
2571 relative with the default address to judge.\r
da1d0201 2572\r
2573 @retval TRUE If the default address is static.\r
2574 @retval FALSE If the default address is acquired from DHCP.\r
2575\r
2576**/\r
da1d0201 2577BOOLEAN\r
2578NetLibDefaultAddressIsStatic (\r
2579 IN EFI_HANDLE Controller\r
2580 )\r
2581{\r
63886849 2582 EFI_STATUS Status;\r
2583 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;\r
2584 UINTN Len;\r
2585 NIC_IP4_CONFIG_INFO *ConfigInfo;\r
2586 BOOLEAN IsStatic;\r
2587 EFI_STRING ConfigHdr;\r
2588 EFI_STRING ConfigResp;\r
2589 EFI_STRING AccessProgress;\r
2590 EFI_STRING AccessResults;\r
2591 EFI_STRING String;\r
1dc1b43f 2592 EFI_HANDLE ChildHandle;\r
63886849 2593\r
2594 ConfigInfo = NULL;\r
2595 ConfigHdr = NULL;\r
2596 ConfigResp = NULL;\r
2597 AccessProgress = NULL;\r
2598 AccessResults = NULL;\r
2599 IsStatic = TRUE;\r
2600\r
2601 Status = gBS->LocateProtocol (\r
70b68990 2602 &gEfiHiiConfigRoutingProtocolGuid,\r
2603 NULL,\r
2604 (VOID **) &HiiConfigRouting\r
2605 );\r
da1d0201 2606 if (EFI_ERROR (Status)) {\r
2607 return TRUE;\r
2608 }\r
2609\r
1dc1b43f 2610 Status = NetGetChildHandle (Controller, &ChildHandle);\r
2611 if (EFI_ERROR (Status)) {\r
2612 return TRUE;\r
2613 }\r
2614\r
63886849 2615 //\r
2616 // Construct config request string header\r
2617 //\r
1dc1b43f 2618 ConfigHdr = HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid, EFI_NIC_IP4_CONFIG_VARIABLE, ChildHandle);\r
894d038a 2619 if (ConfigHdr == NULL) {\r
2620 return TRUE;\r
2621 }\r
1204fe83 2622\r
63886849 2623 Len = StrLen (ConfigHdr);\r
ce4106be 2624 ConfigResp = AllocateZeroPool ((Len + NIC_ITEM_CONFIG_SIZE * 2 + 100) * sizeof (CHAR16));\r
63886849 2625 if (ConfigResp == NULL) {\r
2626 goto ON_EXIT;\r
2627 }\r
2628 StrCpy (ConfigResp, ConfigHdr);\r
2629\r
2630 String = ConfigResp + Len;\r
2631 UnicodeSPrint (\r
1204fe83 2632 String,\r
2633 (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16),\r
2634 L"&OFFSET=%04X&WIDTH=%04X",\r
2635 OFFSET_OF (NIC_IP4_CONFIG_INFO, Source),\r
63886849 2636 sizeof (UINT32)\r
2637 );\r
2638\r
2639 Status = HiiConfigRouting->ExtractConfig (\r
2640 HiiConfigRouting,\r
2641 ConfigResp,\r
2642 &AccessProgress,\r
2643 &AccessResults\r
2644 );\r
2645 if (EFI_ERROR (Status)) {\r
2646 goto ON_EXIT;\r
da1d0201 2647 }\r
2648\r
1dc1b43f 2649 ConfigInfo = AllocateZeroPool (NIC_ITEM_CONFIG_SIZE);\r
da1d0201 2650 if (ConfigInfo == NULL) {\r
63886849 2651 goto ON_EXIT;\r
da1d0201 2652 }\r
2653\r
63886849 2654 ConfigInfo->Source = IP4_CONFIG_SOURCE_STATIC;\r
2655 Len = NIC_ITEM_CONFIG_SIZE;\r
2656 Status = HiiConfigRouting->ConfigToBlock (\r
2657 HiiConfigRouting,\r
2658 AccessResults,\r
2659 (UINT8 *) ConfigInfo,\r
2660 &Len,\r
2661 &AccessProgress\r
2662 );\r
da1d0201 2663 if (EFI_ERROR (Status)) {\r
2664 goto ON_EXIT;\r
2665 }\r
2666\r
2667 IsStatic = (BOOLEAN) (ConfigInfo->Source == IP4_CONFIG_SOURCE_STATIC);\r
1204fe83 2668\r
da1d0201 2669ON_EXIT:\r
2670\r
63886849 2671 if (AccessResults != NULL) {\r
2672 FreePool (AccessResults);\r
2673 }\r
2674 if (ConfigInfo != NULL) {\r
2675 FreePool (ConfigInfo);\r
2676 }\r
2677 if (ConfigResp != NULL) {\r
2678 FreePool (ConfigResp);\r
2679 }\r
2680 if (ConfigHdr != NULL) {\r
2681 FreePool (ConfigHdr);\r
2682 }\r
da1d0201 2683\r
2684 return IsStatic;\r
2685}\r
2686\r
2687/**\r
2688 Create an IPv4 device path node.\r
1204fe83 2689\r
b9008c87 2690 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.\r
2691 The header subtype of IPv4 device path node is MSG_IPv4_DP.\r
b9008c87 2692 Get other info from parameters to make up the whole IPv4 device path node.\r
da1d0201 2693\r
3e7104c2 2694 @param[in, out] Node Pointer to the IPv4 device path node.\r
f6b7393c 2695 @param[in] Controller The controller handle.\r
3e7104c2 2696 @param[in] LocalIp The local IPv4 address.\r
2697 @param[in] LocalPort The local port.\r
2698 @param[in] RemoteIp The remote IPv4 address.\r
2699 @param[in] RemotePort The remote port.\r
2700 @param[in] Protocol The protocol type in the IP header.\r
2701 @param[in] UseDefaultAddress Whether this instance is using default address or not.\r
da1d0201 2702\r
da1d0201 2703**/\r
2704VOID\r
7b414b4e 2705EFIAPI\r
da1d0201 2706NetLibCreateIPv4DPathNode (\r
2707 IN OUT IPv4_DEVICE_PATH *Node,\r
2708 IN EFI_HANDLE Controller,\r
2709 IN IP4_ADDR LocalIp,\r
2710 IN UINT16 LocalPort,\r
2711 IN IP4_ADDR RemoteIp,\r
2712 IN UINT16 RemotePort,\r
2713 IN UINT16 Protocol,\r
2714 IN BOOLEAN UseDefaultAddress\r
2715 )\r
2716{\r
2717 Node->Header.Type = MESSAGING_DEVICE_PATH;\r
2718 Node->Header.SubType = MSG_IPv4_DP;\r
501793fa 2719 SetDevicePathNodeLength (&Node->Header, sizeof (IPv4_DEVICE_PATH));\r
da1d0201 2720\r
e48e37fc 2721 CopyMem (&Node->LocalIpAddress, &LocalIp, sizeof (EFI_IPv4_ADDRESS));\r
2722 CopyMem (&Node->RemoteIpAddress, &RemoteIp, sizeof (EFI_IPv4_ADDRESS));\r
da1d0201 2723\r
2724 Node->LocalPort = LocalPort;\r
2725 Node->RemotePort = RemotePort;\r
2726\r
2727 Node->Protocol = Protocol;\r
2728\r
2729 if (!UseDefaultAddress) {\r
2730 Node->StaticIpAddress = TRUE;\r
2731 } else {\r
2732 Node->StaticIpAddress = NetLibDefaultAddressIsStatic (Controller);\r
2733 }\r
501793fa
RN
2734\r
2735 //\r
2736 // Set the Gateway IP address to default value 0:0:0:0.\r
2737 // Set the Subnet mask to default value 255:255:255:0.\r
2738 //\r
2739 ZeroMem (&Node->GatewayIpAddress, sizeof (EFI_IPv4_ADDRESS));\r
2740 SetMem (&Node->SubnetMask, sizeof (EFI_IPv4_ADDRESS), 0xff);\r
2741 Node->SubnetMask.Addr[3] = 0;\r
da1d0201 2742}\r
2743\r
f6b7393c 2744/**\r
2745 Create an IPv6 device path node.\r
1204fe83 2746\r
f6b7393c 2747 The header type of IPv6 device path node is MESSAGING_DEVICE_PATH.\r
2748 The header subtype of IPv6 device path node is MSG_IPv6_DP.\r
2749 Get other info from parameters to make up the whole IPv6 device path node.\r
2750\r
2751 @param[in, out] Node Pointer to the IPv6 device path node.\r
2752 @param[in] Controller The controller handle.\r
2753 @param[in] LocalIp The local IPv6 address.\r
2754 @param[in] LocalPort The local port.\r
2755 @param[in] RemoteIp The remote IPv6 address.\r
2756 @param[in] RemotePort The remote port.\r
2757 @param[in] Protocol The protocol type in the IP header.\r
2758\r
2759**/\r
2760VOID\r
2761EFIAPI\r
2762NetLibCreateIPv6DPathNode (\r
2763 IN OUT IPv6_DEVICE_PATH *Node,\r
2764 IN EFI_HANDLE Controller,\r
2765 IN EFI_IPv6_ADDRESS *LocalIp,\r
2766 IN UINT16 LocalPort,\r
2767 IN EFI_IPv6_ADDRESS *RemoteIp,\r
2768 IN UINT16 RemotePort,\r
2769 IN UINT16 Protocol\r
2770 )\r
2771{\r
2772 Node->Header.Type = MESSAGING_DEVICE_PATH;\r
2773 Node->Header.SubType = MSG_IPv6_DP;\r
2774 SetDevicePathNodeLength (&Node->Header, sizeof (IPv6_DEVICE_PATH));\r
2775\r
2776 CopyMem (&Node->LocalIpAddress, LocalIp, sizeof (EFI_IPv6_ADDRESS));\r
2777 CopyMem (&Node->RemoteIpAddress, RemoteIp, sizeof (EFI_IPv6_ADDRESS));\r
2778\r
2779 Node->LocalPort = LocalPort;\r
2780 Node->RemotePort = RemotePort;\r
2781\r
2782 Node->Protocol = Protocol;\r
501793fa
RN
2783\r
2784 //\r
2785 // Set default value to IPAddressOrigin, PrefixLength.\r
2786 // Set the Gateway IP address to unspecified address.\r
2787 //\r
2788 Node->IpAddressOrigin = 0;\r
2789 Node->PrefixLength = IP6_PREFIX_LENGTH;\r
2790 ZeroMem (&Node->GatewayIpAddress, sizeof (EFI_IPv6_ADDRESS));\r
f6b7393c 2791}\r
da1d0201 2792\r
2793/**\r
2794 Find the UNDI/SNP handle from controller and protocol GUID.\r
1204fe83 2795\r
da1d0201 2796 For example, IP will open a MNP child to transmit/receive\r
2797 packets, when MNP is stopped, IP should also be stopped. IP\r
2798 needs to find its own private data which is related the IP's\r
2799 service binding instance that is install on UNDI/SNP handle.\r
2800 Now, the controller is either a MNP or ARP child handle. But\r
2801 IP opens these handle BY_DRIVER, use that info, we can get the\r
2802 UNDI/SNP handle.\r
2803\r
3e7104c2 2804 @param[in] Controller Then protocol handle to check.\r
2805 @param[in] ProtocolGuid The protocol that is related with the handle.\r
da1d0201 2806\r
3e7104c2 2807 @return The UNDI/SNP handle or NULL for errors.\r
da1d0201 2808\r
2809**/\r
2810EFI_HANDLE\r
7b414b4e 2811EFIAPI\r
da1d0201 2812NetLibGetNicHandle (\r
2813 IN EFI_HANDLE Controller,\r
2814 IN EFI_GUID *ProtocolGuid\r
2815 )\r
2816{\r
2817 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer;\r
2818 EFI_HANDLE Handle;\r
2819 EFI_STATUS Status;\r
2820 UINTN OpenCount;\r
2821 UINTN Index;\r
2822\r
2823 Status = gBS->OpenProtocolInformation (\r
2824 Controller,\r
2825 ProtocolGuid,\r
2826 &OpenBuffer,\r
2827 &OpenCount\r
2828 );\r
2829\r
2830 if (EFI_ERROR (Status)) {\r
2831 return NULL;\r
2832 }\r
2833\r
2834 Handle = NULL;\r
2835\r
2836 for (Index = 0; Index < OpenCount; Index++) {\r
e2851998 2837 if ((OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {\r
da1d0201 2838 Handle = OpenBuffer[Index].ControllerHandle;\r
2839 break;\r
2840 }\r
2841 }\r
2842\r
2843 gBS->FreePool (OpenBuffer);\r
2844 return Handle;\r
2845}\r
e4ef0031 2846\r
2847/**\r
2848 Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS.\r
2849\r
2850 @param[in] String The pointer to the Ascii string.\r
2851 @param[out] Ip4Address The pointer to the converted IPv4 address.\r
2852\r
dd29f3ed 2853 @retval EFI_SUCCESS Convert to IPv4 address successfully.\r
e4ef0031 2854 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.\r
2855\r
2856**/\r
2857EFI_STATUS\r
e798cd87 2858EFIAPI\r
e4ef0031 2859NetLibAsciiStrToIp4 (\r
2860 IN CONST CHAR8 *String,\r
2861 OUT EFI_IPv4_ADDRESS *Ip4Address\r
2862 )\r
2863{\r
2864 UINT8 Index;\r
2865 CHAR8 *Ip4Str;\r
2866 CHAR8 *TempStr;\r
2867 UINTN NodeVal;\r
2868\r
2869 if ((String == NULL) || (Ip4Address == NULL)) {\r
2870 return EFI_INVALID_PARAMETER;\r
2871 }\r
2872\r
2873 Ip4Str = (CHAR8 *) String;\r
2874\r
2875 for (Index = 0; Index < 4; Index++) {\r
2876 TempStr = Ip4Str;\r
2877\r
2878 while ((*Ip4Str != '\0') && (*Ip4Str != '.')) {\r
2879 Ip4Str++;\r
2880 }\r
2881\r
2882 //\r
2883 // The IPv4 address is X.X.X.X\r
2884 //\r
2885 if (*Ip4Str == '.') {\r
2886 if (Index == 3) {\r
2887 return EFI_INVALID_PARAMETER;\r
2888 }\r
2889 } else {\r
2890 if (Index != 3) {\r
2891 return EFI_INVALID_PARAMETER;\r
2892 }\r
2893 }\r
2894\r
9b6f044a 2895 //\r
2896 // Convert the string to IPv4 address. AsciiStrDecimalToUintn stops at the\r
dd29f3ed 2897 // first character that is not a valid decimal character, '.' or '\0' here.\r
9b6f044a 2898 //\r
e4ef0031 2899 NodeVal = AsciiStrDecimalToUintn (TempStr);\r
2900 if (NodeVal > 0xFF) {\r
2901 return EFI_INVALID_PARAMETER;\r
2902 }\r
2903\r
2904 Ip4Address->Addr[Index] = (UINT8) NodeVal;\r
2905\r
2906 Ip4Str++;\r
2907 }\r
2908\r
2909 return EFI_SUCCESS;\r
2910}\r
2911\r
2912\r
2913/**\r
2914 Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of the\r
2915 string is defined in RFC 4291 - Text Pepresentation of Addresses.\r
2916\r
2917 @param[in] String The pointer to the Ascii string.\r
2918 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
2919\r
dd29f3ed 2920 @retval EFI_SUCCESS Convert to IPv6 address successfully.\r
e4ef0031 2921 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.\r
2922\r
2923**/\r
2924EFI_STATUS\r
e798cd87 2925EFIAPI\r
e4ef0031 2926NetLibAsciiStrToIp6 (\r
2927 IN CONST CHAR8 *String,\r
2928 OUT EFI_IPv6_ADDRESS *Ip6Address\r
2929 )\r
2930{\r
2931 UINT8 Index;\r
2932 CHAR8 *Ip6Str;\r
2933 CHAR8 *TempStr;\r
2934 CHAR8 *TempStr2;\r
2935 UINT8 NodeCnt;\r
2936 UINT8 TailNodeCnt;\r
2937 UINT8 AllowedCnt;\r
2938 UINTN NodeVal;\r
2939 BOOLEAN Short;\r
2940 BOOLEAN Update;\r
77545563 2941 BOOLEAN LeadZero;\r
2942 UINT8 LeadZeroCnt;\r
2943 UINT8 Cnt;\r
e4ef0031 2944\r
2945 if ((String == NULL) || (Ip6Address == NULL)) {\r
2946 return EFI_INVALID_PARAMETER;\r
2947 }\r
2948\r
77545563 2949 Ip6Str = (CHAR8 *) String;\r
2950 AllowedCnt = 6;\r
2951 LeadZeroCnt = 0;\r
e4ef0031 2952\r
2953 //\r
2954 // An IPv6 address leading with : looks strange.\r
2955 //\r
2956 if (*Ip6Str == ':') {\r
2957 if (*(Ip6Str + 1) != ':') {\r
2958 return EFI_INVALID_PARAMETER;\r
2959 } else {\r
2960 AllowedCnt = 7;\r
dd29f3ed 2961 }\r
e4ef0031 2962 }\r
2963\r
2964 ZeroMem (Ip6Address, sizeof (EFI_IPv6_ADDRESS));\r
2965\r
2966 NodeCnt = 0;\r
2967 TailNodeCnt = 0;\r
2968 Short = FALSE;\r
2969 Update = FALSE;\r
77545563 2970 LeadZero = FALSE;\r
e4ef0031 2971\r
9b6f044a 2972 for (Index = 0; Index < 15; Index = (UINT8) (Index + 2)) {\r
e4ef0031 2973 TempStr = Ip6Str;\r
2974\r
2975 while ((*Ip6Str != '\0') && (*Ip6Str != ':')) {\r
2976 Ip6Str++;\r
2977 }\r
2978\r
2979 if ((*Ip6Str == '\0') && (Index != 14)) {\r
2980 return EFI_INVALID_PARAMETER;\r
2981 }\r
2982\r
2983 if (*Ip6Str == ':') {\r
2984 if (*(Ip6Str + 1) == ':') {\r
77545563 2985 if ((NodeCnt > 6) || \r
2986 ((*(Ip6Str + 2) != '\0') && (AsciiStrHexToUintn (Ip6Str + 2) == 0))) {\r
e4ef0031 2987 //\r
2988 // ::0 looks strange. report error to user.\r
2989 //\r
2990 return EFI_INVALID_PARAMETER;\r
dd29f3ed 2991 }\r
77545563 2992 if ((NodeCnt == 6) && (*(Ip6Str + 2) != '\0') && \r
2993 (AsciiStrHexToUintn (Ip6Str + 2) != 0)) {\r
2994 return EFI_INVALID_PARAMETER;\r
2995 }\r
e4ef0031 2996\r
2997 //\r
2998 // Skip the abbreviation part of IPv6 address.\r
2999 //\r
3000 TempStr2 = Ip6Str + 2;\r
3001 while ((*TempStr2 != '\0')) {\r
3002 if (*TempStr2 == ':') {\r
3003 if (*(TempStr2 + 1) == ':') {\r
3004 //\r
3005 // :: can only appear once in IPv6 address.\r
3006 //\r
3007 return EFI_INVALID_PARAMETER;\r
3008 }\r
dd29f3ed 3009\r
e4ef0031 3010 TailNodeCnt++;\r
3011 if (TailNodeCnt >= (AllowedCnt - NodeCnt)) {\r
3012 //\r
3013 // :: indicates one or more groups of 16 bits of zeros.\r
3014 //\r
3015 return EFI_INVALID_PARAMETER;\r
3016 }\r
3017 }\r
3018\r
3019 TempStr2++;\r
dd29f3ed 3020 }\r
e4ef0031 3021\r
3022 Short = TRUE;\r
3023 Update = TRUE;\r
3024\r
3025 Ip6Str = Ip6Str + 2;\r
3026 } else {\r
77545563 3027 if (*(Ip6Str + 1) == '\0') {\r
3028 return EFI_INVALID_PARAMETER;\r
3029 }\r
e4ef0031 3030 Ip6Str++;\r
3031 NodeCnt++;\r
3032 if ((Short && (NodeCnt > 6)) || (!Short && (NodeCnt > 7))) {\r
3033 //\r
3034 // There are more than 8 groups of 16 bits of zeros.\r
3035 //\r
3036 return EFI_INVALID_PARAMETER;\r
3037 }\r
dd29f3ed 3038 }\r
3039 }\r
e4ef0031 3040\r
3041 //\r
9b6f044a 3042 // Convert the string to IPv6 address. AsciiStrHexToUintn stops at the first\r
dd29f3ed 3043 // character that is not a valid hexadecimal character, ':' or '\0' here.\r
e4ef0031 3044 //\r
3045 NodeVal = AsciiStrHexToUintn (TempStr);\r
3046 if ((NodeVal > 0xFFFF) || (Index > 14)) {\r
3047 return EFI_INVALID_PARAMETER;\r
3048 }\r
77545563 3049 if (NodeVal != 0) {\r
3050 if ((*TempStr == '0') && \r
3051 ((*(TempStr + 2) == ':') || (*(TempStr + 3) == ':') || \r
3052 (*(TempStr + 2) == '\0') || (*(TempStr + 3) == '\0'))) {\r
3053 return EFI_INVALID_PARAMETER;\r
3054 }\r
3055 if ((*TempStr == '0') && (*(TempStr + 4) != '\0') && \r
3056 (*(TempStr + 4) != ':')) { \r
3057 return EFI_INVALID_PARAMETER;\r
3058 }\r
3059 } else {\r
3060 if (((*TempStr == '0') && (*(TempStr + 1) == '0') && \r
3061 ((*(TempStr + 2) == ':') || (*(TempStr + 2) == '\0'))) ||\r
3062 ((*TempStr == '0') && (*(TempStr + 1) == '0') && (*(TempStr + 2) == '0') && \r
3063 ((*(TempStr + 3) == ':') || (*(TempStr + 3) == '\0')))) {\r
3064 return EFI_INVALID_PARAMETER;\r
3065 }\r
3066 }\r
3067\r
3068 Cnt = 0;\r
3069 while ((TempStr[Cnt] != ':') && (TempStr[Cnt] != '\0')) {\r
3070 Cnt++; \r
3071 }\r
3072 if (LeadZeroCnt == 0) {\r
3073 if ((Cnt == 4) && (*TempStr == '0')) {\r
3074 LeadZero = TRUE;\r
3075 LeadZeroCnt++;\r
3076 }\r
3077 if ((Cnt != 0) && (Cnt < 4)) {\r
3078 LeadZero = FALSE;\r
3079 LeadZeroCnt++;\r
3080 }\r
3081 } else {\r
3af91e23 3082 if ((Cnt == 4) && (*TempStr == '0') && !LeadZero) {\r
77545563 3083 return EFI_INVALID_PARAMETER;\r
3084 }\r
3af91e23 3085 if ((Cnt != 0) && (Cnt < 4) && LeadZero) {\r
77545563 3086 return EFI_INVALID_PARAMETER;\r
3087 }\r
3088 } \r
e4ef0031 3089\r
3090 Ip6Address->Addr[Index] = (UINT8) (NodeVal >> 8);\r
3091 Ip6Address->Addr[Index + 1] = (UINT8) (NodeVal & 0xFF);\r
3092\r
3093 //\r
3094 // Skip the groups of zeros by ::\r
3095 //\r
3096 if (Short && Update) {\r
3097 Index = (UINT8) (16 - (TailNodeCnt + 2) * 2);\r
3098 Update = FALSE;\r
3099 }\r
3100 }\r
3101\r
3102 if ((!Short && Index != 16) || (*Ip6Str != '\0')) {\r
3103 return EFI_INVALID_PARAMETER;\r
3104 }\r
3105\r
3106 return EFI_SUCCESS;\r
3107}\r
3108\r
3109\r
3110/**\r
3111 Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS.\r
3112\r
3113 @param[in] String The pointer to the Ascii string.\r
3114 @param[out] Ip4Address The pointer to the converted IPv4 address.\r
3115\r
dd29f3ed 3116 @retval EFI_SUCCESS Convert to IPv4 address successfully.\r
e4ef0031 3117 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.\r
3118 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.\r
3119\r
3120**/\r
3121EFI_STATUS\r
e798cd87 3122EFIAPI\r
e4ef0031 3123NetLibStrToIp4 (\r
3124 IN CONST CHAR16 *String,\r
3125 OUT EFI_IPv4_ADDRESS *Ip4Address\r
3126 )\r
3127{\r
3128 CHAR8 *Ip4Str;\r
3129 EFI_STATUS Status;\r
dd29f3ed 3130\r
e4ef0031 3131 if ((String == NULL) || (Ip4Address == NULL)) {\r
3132 return EFI_INVALID_PARAMETER;\r
3133 }\r
3134\r
3135 Ip4Str = (CHAR8 *) AllocatePool ((StrLen (String) + 1) * sizeof (CHAR8));\r
3136 if (Ip4Str == NULL) {\r
3137 return EFI_OUT_OF_RESOURCES;\r
3138 }\r
3139\r
3140 UnicodeStrToAsciiStr (String, Ip4Str);\r
3141\r
3142 Status = NetLibAsciiStrToIp4 (Ip4Str, Ip4Address);\r
3143\r
3144 FreePool (Ip4Str);\r
3145\r
3146 return Status;\r
3147}\r
3148\r
3149\r
3150/**\r
3151 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of\r
3152 the string is defined in RFC 4291 - Text Pepresentation of Addresses.\r
3153\r
3154 @param[in] String The pointer to the Ascii string.\r
3155 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
3156\r
dd29f3ed 3157 @retval EFI_SUCCESS Convert to IPv6 address successfully.\r
e4ef0031 3158 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.\r
3159 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.\r
3160\r
3161**/\r
3162EFI_STATUS\r
e798cd87 3163EFIAPI\r
e4ef0031 3164NetLibStrToIp6 (\r
3165 IN CONST CHAR16 *String,\r
3166 OUT EFI_IPv6_ADDRESS *Ip6Address\r
dd29f3ed 3167 )\r
e4ef0031 3168{\r
3169 CHAR8 *Ip6Str;\r
3170 EFI_STATUS Status;\r
dd29f3ed 3171\r
e4ef0031 3172 if ((String == NULL) || (Ip6Address == NULL)) {\r
3173 return EFI_INVALID_PARAMETER;\r
3174 }\r
3175\r
3176 Ip6Str = (CHAR8 *) AllocatePool ((StrLen (String) + 1) * sizeof (CHAR8));\r
3177 if (Ip6Str == NULL) {\r
3178 return EFI_OUT_OF_RESOURCES;\r
3179 }\r
3180\r
3181 UnicodeStrToAsciiStr (String, Ip6Str);\r
3182\r
3183 Status = NetLibAsciiStrToIp6 (Ip6Str, Ip6Address);\r
3184\r
3185 FreePool (Ip6Str);\r
3186\r
3187 return Status;\r
3188}\r
3189\r
3190/**\r
3191 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length.\r
3192 The format of the string is defined in RFC 4291 - Text Pepresentation of Addresses\r
3193 Prefixes: ipv6-address/prefix-length.\r
3194\r
3195 @param[in] String The pointer to the Ascii string.\r
3196 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
3197 @param[out] PrefixLength The pointer to the converted prefix length.\r
3198\r
dd29f3ed 3199 @retval EFI_SUCCESS Convert to IPv6 address successfully.\r
e4ef0031 3200 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.\r
3201 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.\r
3202\r
3203**/\r
3204EFI_STATUS\r
e798cd87 3205EFIAPI\r
e4ef0031 3206NetLibStrToIp6andPrefix (\r
3207 IN CONST CHAR16 *String,\r
3208 OUT EFI_IPv6_ADDRESS *Ip6Address,\r
3209 OUT UINT8 *PrefixLength\r
dd29f3ed 3210 )\r
e4ef0031 3211{\r
dd29f3ed 3212 CHAR8 *Ip6Str;\r
e4ef0031 3213 CHAR8 *PrefixStr;\r
3214 CHAR8 *TempStr;\r
3215 EFI_STATUS Status;\r
3216 UINT8 Length;\r
dd29f3ed 3217\r
e4ef0031 3218 if ((String == NULL) || (Ip6Address == NULL) || (PrefixLength == NULL)) {\r
3219 return EFI_INVALID_PARAMETER;\r
3220 }\r
3221\r
3222 Ip6Str = (CHAR8 *) AllocatePool ((StrLen (String) + 1) * sizeof (CHAR8));\r
3223 if (Ip6Str == NULL) {\r
3224 return EFI_OUT_OF_RESOURCES;\r
3225 }\r
3226\r
3227 UnicodeStrToAsciiStr (String, Ip6Str);\r
3228\r
3229 //\r
3230 // Get the sub string describing prefix length.\r
3231 //\r
3232 TempStr = Ip6Str;\r
3233 while (*TempStr != '\0' && (*TempStr != '/')) {\r
3234 TempStr++;\r
3235 }\r
3236\r
3237 if (*TempStr == '/') {\r
3238 PrefixStr = TempStr + 1;\r
3239 } else {\r
3240 PrefixStr = NULL;\r
3241 }\r
3242\r
3243 //\r
3244 // Get the sub string describing IPv6 address and convert it.\r
3245 //\r
3246 *TempStr = '\0';\r
3247\r
3248 Status = NetLibAsciiStrToIp6 (Ip6Str, Ip6Address);\r
3249 if (EFI_ERROR (Status)) {\r
3250 goto Exit;\r
3251 }\r
3252\r
04bb6788 3253 //\r
3254 // If input string doesn't indicate the prefix length, return 0xff.\r
3255 //\r
3256 Length = 0xFF;\r
1dc1b43f 3257\r
e4ef0031 3258 //\r
3259 // Convert the string to prefix length\r
3260 //\r
e4ef0031 3261 if (PrefixStr != NULL) {\r
3262\r
3263 Status = EFI_INVALID_PARAMETER;\r
04bb6788 3264 Length = 0;\r
e4ef0031 3265 while (*PrefixStr != '\0') {\r
3266 if (NET_IS_DIGIT (*PrefixStr)) {\r
3267 Length = (UINT8) (Length * 10 + (*PrefixStr - '0'));\r
3268 if (Length >= IP6_PREFIX_NUM) {\r
3269 goto Exit;\r
3270 }\r
3271 } else {\r
3272 goto Exit;\r
3273 }\r
3274\r
3275 PrefixStr++;\r
3276 }\r
3277 }\r
3278\r
3279 *PrefixLength = Length;\r
3280 Status = EFI_SUCCESS;\r
3281\r
3282Exit:\r
3283\r
3284 FreePool (Ip6Str);\r
3285 return Status;\r
3286}\r
3287\r
216f7970 3288/**\r
3289\r
3290 Convert one EFI_IPv6_ADDRESS to Null-terminated Unicode string.\r
3291 The text representation of address is defined in RFC 4291.\r
3292 \r
3293 @param[in] Ip6Address The pointer to the IPv6 address.\r
3294 @param[out] String The buffer to return the converted string.\r
3295 @param[in] StringSize The length in bytes of the input String.\r
3296 \r
3297 @retval EFI_SUCCESS Convert to string successfully.\r
3298 @retval EFI_INVALID_PARAMETER The input parameter is invalid.\r
3299 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small for the result. BufferSize has been \r
3300 updated with the size needed to complete the request.\r
3301**/\r
3302EFI_STATUS\r
3303EFIAPI\r
3304NetLibIp6ToStr (\r
3305 IN EFI_IPv6_ADDRESS *Ip6Address,\r
3306 OUT CHAR16 *String,\r
3307 IN UINTN StringSize\r
3308 )\r
3309{\r
3310 UINT16 Ip6Addr[8];\r
3311 UINTN Index;\r
3312 UINTN LongestZerosStart;\r
3313 UINTN LongestZerosLength;\r
3314 UINTN CurrentZerosStart;\r
3315 UINTN CurrentZerosLength;\r
3316 CHAR16 Buffer[sizeof"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];\r
3317 CHAR16 *Ptr;\r
3318\r
3319 if (Ip6Address == NULL || String == NULL || StringSize == 0) {\r
3320 return EFI_INVALID_PARAMETER;\r
3321 }\r
3322\r
3323 //\r
3324 // Convert the UINT8 array to an UINT16 array for easy handling.\r
3325 // \r
3326 ZeroMem (Ip6Addr, sizeof (Ip6Addr));\r
3327 for (Index = 0; Index < 16; Index++) {\r
3328 Ip6Addr[Index / 2] |= (Ip6Address->Addr[Index] << ((1 - (Index % 2)) << 3));\r
3329 }\r
57b301b5 3330\r
216f7970 3331 //\r
3332 // Find the longest zeros and mark it.\r
3333 //\r
3334 CurrentZerosStart = DEFAULT_ZERO_START;\r
3335 CurrentZerosLength = 0;\r
3336 LongestZerosStart = DEFAULT_ZERO_START;\r
3337 LongestZerosLength = 0;\r
3338 for (Index = 0; Index < 8; Index++) {\r
3339 if (Ip6Addr[Index] == 0) {\r
3340 if (CurrentZerosStart == DEFAULT_ZERO_START) {\r
3341 CurrentZerosStart = Index;\r
3342 CurrentZerosLength = 1;\r
3343 } else {\r
3344 CurrentZerosLength++;\r
3345 }\r
3346 } else {\r
3347 if (CurrentZerosStart != DEFAULT_ZERO_START) {\r
3348 if (CurrentZerosLength > 2 && (LongestZerosStart == (DEFAULT_ZERO_START) || CurrentZerosLength > LongestZerosLength)) {\r
3349 LongestZerosStart = CurrentZerosStart;\r
3350 LongestZerosLength = CurrentZerosLength;\r
3351 }\r
3352 CurrentZerosStart = DEFAULT_ZERO_START;\r
3353 CurrentZerosLength = 0;\r
3354 }\r
3355 }\r
3356 }\r
3357 \r
3358 if (CurrentZerosStart != DEFAULT_ZERO_START && CurrentZerosLength > 2) {\r
3359 if (LongestZerosStart == DEFAULT_ZERO_START || LongestZerosLength < CurrentZerosLength) {\r
3360 LongestZerosStart = CurrentZerosStart;\r
3361 LongestZerosLength = CurrentZerosLength;\r
3362 }\r
3363 }\r
3364\r
3365 Ptr = Buffer;\r
3366 for (Index = 0; Index < 8; Index++) {\r
3367 if (LongestZerosStart != DEFAULT_ZERO_START && Index >= LongestZerosStart && Index < LongestZerosStart + LongestZerosLength) {\r
3368 if (Index == LongestZerosStart) {\r
3369 *Ptr++ = L':';\r
3370 }\r
3371 continue;\r
3372 }\r
3373 if (Index != 0) {\r
3374 *Ptr++ = L':';\r
3375 }\r
3376 Ptr += UnicodeSPrint(Ptr, 10, L"%x", Ip6Addr[Index]);\r
3377 }\r
3378 \r
3379 if (LongestZerosStart != DEFAULT_ZERO_START && LongestZerosStart + LongestZerosLength == 8) {\r
3380 *Ptr++ = L':';\r
3381 }\r
3382 *Ptr = L'\0';\r
3383\r
3384 if ((UINTN)Ptr - (UINTN)Buffer > StringSize) {\r
3385 return EFI_BUFFER_TOO_SMALL;\r
3386 }\r
3387\r
3388 StrCpy (String, Buffer);\r
3389\r
3390 return EFI_SUCCESS;\r
3391}\r
57b301b5 3392\r
3393/**\r
3394 This function obtains the system guid from the smbios table.\r
3395\r
3396 @param[out] SystemGuid The pointer of the returned system guid.\r
3397\r
3398 @retval EFI_SUCCESS Successfully obtained the system guid.\r
3399 @retval EFI_NOT_FOUND Did not find the SMBIOS table.\r
3400\r
3401**/\r
3402EFI_STATUS\r
3403EFIAPI\r
3404NetLibGetSystemGuid (\r
3405 OUT EFI_GUID *SystemGuid\r
3406 )\r
3407{\r
3408 EFI_STATUS Status;\r
3409 SMBIOS_TABLE_ENTRY_POINT *SmbiosTable;\r
3410 SMBIOS_STRUCTURE_POINTER Smbios;\r
3411 SMBIOS_STRUCTURE_POINTER SmbiosEnd;\r
3412 CHAR8 *String;\r
3413\r
3414 SmbiosTable = NULL;\r
3415 Status = EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **) &SmbiosTable);\r
3416\r
3417 if (EFI_ERROR (Status) || SmbiosTable == NULL) {\r
3418 return EFI_NOT_FOUND;\r
3419 }\r
3420\r
3421 Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) SmbiosTable->TableAddress;\r
3422 SmbiosEnd.Raw = (UINT8 *) (UINTN) (SmbiosTable->TableAddress + SmbiosTable->TableLength);\r
3423\r
3424 do {\r
3425 if (Smbios.Hdr->Type == 1) {\r
3426 if (Smbios.Hdr->Length < 0x19) {\r
3427 //\r
3428 // Older version did not support UUID.\r
3429 //\r
3430 return EFI_NOT_FOUND;\r
3431 }\r
3432 \r
3433 //\r
3434 // SMBIOS tables are byte packed so we need to do a byte copy to\r
3435 // prevend alignment faults on Itanium-based platform.\r
3436 //\r
3437 CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID));\r
3438 return EFI_SUCCESS;\r
3439 }\r
3440\r
3441 //\r
3442 // Go to the next SMBIOS structure. Each SMBIOS structure may include 2 parts:\r
3443 // 1. Formatted section; 2. Unformatted string section. So, 2 steps are needed\r
3444 // to skip one SMBIOS structure.\r
3445 //\r
3446 \r
3447 //\r
3448 // Step 1: Skip over formatted section.\r
3449 //\r
3450 String = (CHAR8 *) (Smbios.Raw + Smbios.Hdr->Length);\r
3451 \r
3452 //\r
3453 // Step 2: Skip over unformated string section.\r
3454 //\r
3455 do {\r
3456 //\r
3457 // Each string is terminated with a NULL(00h) BYTE and the sets of strings\r
3458 // is terminated with an additional NULL(00h) BYTE.\r
3459 //\r
3460 for ( ; *String != 0; String++) {\r
3461 }\r
3462\r
3463 if (*(UINT8*)++String == 0) {\r
3464 //\r
3465 // Pointer to the next SMBIOS structure.\r
3466 //\r
3467 Smbios.Raw = (UINT8 *)++String;\r
3468 break;\r
3469 } \r
3470 } while (TRUE);\r
3471 } while (Smbios.Raw < SmbiosEnd.Raw);\r
3472 return EFI_NOT_FOUND;\r
3473}\r