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