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