]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
MdeModulePkg: Refine casting expression result to bigger size
[mirror_edk2.git] / MdeModulePkg / Library / DxeNetLib / DxeNetLib.c
CommitLineData
da1d0201 1/** @file\r
3e7104c2 2 Network library.\r
1204fe83 3\r
9f5ca5ef 4Copyright (c) 2005 - 2017, 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
9f4048f7 2228 UINTN BufferSize;\r
da1d0201 2229\r
779ae357 2230 ASSERT (MacString != NULL);\r
da1d0201 2231\r
2232 //\r
779ae357 2233 // Get MAC address of the network device\r
da1d0201 2234 //\r
779ae357 2235 Status = NetLibGetMacAddress (ServiceHandle, &MacAddress, &HwAddressSize);\r
da1d0201 2236 if (EFI_ERROR (Status)) {\r
2237 return Status;\r
2238 }\r
2239\r
da1d0201 2240 //\r
2241 // It takes 2 unicode characters to represent a 1 byte binary buffer.\r
779ae357 2242 // If VLAN is configured, it will need extra 5 characters like "\0005".\r
da1d0201 2243 // Plus one unicode character for the null-terminator.\r
2244 //\r
9f4048f7
HW
2245 BufferSize = (2 * HwAddressSize + 5 + 1) * sizeof (CHAR16);\r
2246 String = AllocateZeroPool (BufferSize);\r
779ae357 2247 if (String == NULL) {\r
da1d0201 2248 return EFI_OUT_OF_RESOURCES;\r
2249 }\r
779ae357 2250 *MacString = String;\r
da1d0201 2251\r
2252 //\r
779ae357 2253 // Convert the MAC address into a unicode string.\r
da1d0201 2254 //\r
779ae357 2255 HwAddress = &MacAddress.Addr[0];\r
2256 for (Index = 0; Index < HwAddressSize; Index++) {\r
9f4048f7
HW
2257 UnicodeValueToStringS (\r
2258 String,\r
2259 BufferSize - ((UINTN)String - (UINTN)*MacString),\r
2260 PREFIX_ZERO | RADIX_HEX,\r
2261 *(HwAddress++),\r
2262 2\r
2263 );\r
2264 String += StrnLenS (String, (BufferSize - ((UINTN)String - (UINTN)*MacString)) / sizeof (CHAR16));\r
da1d0201 2265 }\r
2266\r
779ae357 2267 //\r
2268 // Append VLAN ID if any\r
2269 //\r
2270 VlanId = NetLibGetVlanId (ServiceHandle);\r
2271 if (VlanId != 0) {\r
2272 *String++ = L'\\';\r
9f4048f7
HW
2273 UnicodeValueToStringS (\r
2274 String,\r
2275 BufferSize - ((UINTN)String - (UINTN)*MacString),\r
2276 PREFIX_ZERO | RADIX_HEX,\r
2277 VlanId,\r
2278 4\r
2279 );\r
2280 String += StrnLenS (String, (BufferSize - ((UINTN)String - (UINTN)*MacString)) / sizeof (CHAR16));\r
779ae357 2281 }\r
da1d0201 2282\r
779ae357 2283 //\r
2284 // Null terminate the Unicode string\r
2285 //\r
2286 *String = L'\0';\r
da1d0201 2287\r
2288 return EFI_SUCCESS;\r
2289}\r
2290\r
dd29f3ed 2291/**\r
2292 Detect media status for specified network device.\r
2293\r
2294 The underlying UNDI driver may or may not support reporting media status from\r
2295 GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine\r
2296 will try to invoke Snp->GetStatus() to get the media status: if media already\r
2297 present, it return directly; if media not present, it will stop SNP and then\r
2298 restart SNP to get the latest media status, this give chance to get the correct\r
2299 media status for old UNDI driver which doesn't support reporting media status\r
2300 from GET_STATUS command.\r
2301 Note: there will be two limitations for current algorithm:\r
2302 1) for UNDI with this capability, in case of cable is not attached, there will\r
2303 be an redundant Stop/Start() process;\r
3b1464d5 2304 2) for UNDI without this capability, in case that network cable is attached when\r
2305 Snp->Initialize() is invoked while network cable is unattached later,\r
2306 NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer\r
2307 apps to wait for timeout time.\r
dd29f3ed 2308\r
2309 @param[in] ServiceHandle The handle where network service binding protocols are\r
2310 installed on.\r
2311 @param[out] MediaPresent The pointer to store the media status.\r
2312\r
2313 @retval EFI_SUCCESS Media detection success.\r
2314 @retval EFI_INVALID_PARAMETER ServiceHandle is not valid network device handle.\r
2315 @retval EFI_UNSUPPORTED Network device does not support media detection.\r
2316 @retval EFI_DEVICE_ERROR SNP is in unknown state.\r
2317\r
2318**/\r
2319EFI_STATUS\r
2320EFIAPI\r
2321NetLibDetectMedia (\r
2322 IN EFI_HANDLE ServiceHandle,\r
2323 OUT BOOLEAN *MediaPresent\r
2324 )\r
2325{\r
2326 EFI_STATUS Status;\r
2327 EFI_HANDLE SnpHandle;\r
2328 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
2329 UINT32 InterruptStatus;\r
2330 UINT32 OldState;\r
2331 EFI_MAC_ADDRESS *MCastFilter;\r
2332 UINT32 MCastFilterCount;\r
2333 UINT32 EnableFilterBits;\r
2334 UINT32 DisableFilterBits;\r
2335 BOOLEAN ResetMCastFilters;\r
2336\r
2337 ASSERT (MediaPresent != NULL);\r
2338\r
2339 //\r
2340 // Get SNP handle\r
2341 //\r
2342 Snp = NULL;\r
2343 SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp);\r
2344 if (SnpHandle == NULL) {\r
2345 return EFI_INVALID_PARAMETER;\r
2346 }\r
2347\r
2348 //\r
2349 // Check whether SNP support media detection\r
2350 //\r
2351 if (!Snp->Mode->MediaPresentSupported) {\r
2352 return EFI_UNSUPPORTED;\r
2353 }\r
2354\r
2355 //\r
2356 // Invoke Snp->GetStatus() to refresh MediaPresent field in SNP mode data\r
2357 //\r
2358 Status = Snp->GetStatus (Snp, &InterruptStatus, NULL);\r
2359 if (EFI_ERROR (Status)) {\r
2360 return Status;\r
2361 }\r
2362\r
2363 if (Snp->Mode->MediaPresent) {\r
2364 //\r
2365 // Media is present, return directly\r
2366 //\r
2367 *MediaPresent = TRUE;\r
2368 return EFI_SUCCESS;\r
2369 }\r
2370\r
2371 //\r
2372 // Till now, GetStatus() report no media; while, in case UNDI not support\r
2373 // reporting media status from GetStatus(), this media status may be incorrect.\r
2374 // So, we will stop SNP and then restart it to get the correct media status.\r
2375 //\r
2376 OldState = Snp->Mode->State;\r
2377 if (OldState >= EfiSimpleNetworkMaxState) {\r
2378 return EFI_DEVICE_ERROR;\r
2379 }\r
2380\r
2381 MCastFilter = NULL;\r
2382\r
2383 if (OldState == EfiSimpleNetworkInitialized) {\r
2384 //\r
2385 // SNP is already in use, need Shutdown/Stop and then Start/Initialize\r
2386 //\r
2387\r
2388 //\r
2389 // Backup current SNP receive filter settings\r
2390 //\r
2391 EnableFilterBits = Snp->Mode->ReceiveFilterSetting;\r
2392 DisableFilterBits = Snp->Mode->ReceiveFilterMask ^ EnableFilterBits;\r
2393\r
2394 ResetMCastFilters = TRUE;\r
2395 MCastFilterCount = Snp->Mode->MCastFilterCount;\r
2396 if (MCastFilterCount != 0) {\r
2397 MCastFilter = AllocateCopyPool (\r
2398 MCastFilterCount * sizeof (EFI_MAC_ADDRESS),\r
2399 Snp->Mode->MCastFilter\r
2400 );\r
2401 ASSERT (MCastFilter != NULL);\r
2402\r
2403 ResetMCastFilters = FALSE;\r
2404 }\r
2405\r
2406 //\r
2407 // Shutdown/Stop the simple network\r
2408 //\r
2409 Status = Snp->Shutdown (Snp);\r
2410 if (!EFI_ERROR (Status)) {\r
2411 Status = Snp->Stop (Snp);\r
2412 }\r
2413 if (EFI_ERROR (Status)) {\r
2414 goto Exit;\r
2415 }\r
2416\r
2417 //\r
2418 // Start/Initialize the simple network\r
2419 //\r
2420 Status = Snp->Start (Snp);\r
2421 if (!EFI_ERROR (Status)) {\r
2422 Status = Snp->Initialize (Snp, 0, 0);\r
2423 }\r
2424 if (EFI_ERROR (Status)) {\r
2425 goto Exit;\r
2426 }\r
2427\r
2428 //\r
2429 // Here we get the correct media status\r
2430 //\r
2431 *MediaPresent = Snp->Mode->MediaPresent;\r
2432\r
2433 //\r
2434 // Restore SNP receive filter settings\r
2435 //\r
2436 Status = Snp->ReceiveFilters (\r
2437 Snp,\r
2438 EnableFilterBits,\r
2439 DisableFilterBits,\r
2440 ResetMCastFilters,\r
2441 MCastFilterCount,\r
2442 MCastFilter\r
2443 );\r
2444\r
2445 if (MCastFilter != NULL) {\r
2446 FreePool (MCastFilter);\r
2447 }\r
2448\r
2449 return Status;\r
2450 }\r
2451\r
2452 //\r
2453 // SNP is not in use, it's in state of EfiSimpleNetworkStopped or EfiSimpleNetworkStarted\r
2454 //\r
2455 if (OldState == EfiSimpleNetworkStopped) {\r
2456 //\r
2457 // SNP not start yet, start it\r
2458 //\r
2459 Status = Snp->Start (Snp);\r
2460 if (EFI_ERROR (Status)) {\r
2461 goto Exit;\r
2462 }\r
2463 }\r
2464\r
2465 //\r
2466 // Initialize the simple network\r
2467 //\r
2468 Status = Snp->Initialize (Snp, 0, 0);\r
2469 if (EFI_ERROR (Status)) {\r
2470 Status = EFI_DEVICE_ERROR;\r
2471 goto Exit;\r
2472 }\r
2473\r
2474 //\r
2475 // Here we get the correct media status\r
2476 //\r
2477 *MediaPresent = Snp->Mode->MediaPresent;\r
2478\r
2479 //\r
2480 // Shut down the simple network\r
2481 //\r
2482 Snp->Shutdown (Snp);\r
2483\r
2484Exit:\r
2485 if (OldState == EfiSimpleNetworkStopped) {\r
2486 //\r
2487 // Original SNP sate is Stopped, restore to original state\r
2488 //\r
2489 Snp->Stop (Snp);\r
2490 }\r
2491\r
2492 if (MCastFilter != NULL) {\r
2493 FreePool (MCastFilter);\r
2494 }\r
2495\r
2496 return Status;\r
2497}\r
2498\r
da1d0201 2499/**\r
2500 Check the default address used by the IPv4 driver is static or dynamic (acquired\r
2501 from DHCP).\r
2502\r
6c5c70d6 2503 If the controller handle does not have the EFI_IP4_CONFIG2_PROTOCOL installed, the\r
2504 default address is static. If failed to get the policy from Ip4 Config2 Protocol, \r
2505 the default address is static. Otherwise, get the result from Ip4 Config2 Protocol.\r
1204fe83 2506\r
6c5c70d6 2507 @param[in] Controller The controller handle which has the EFI_IP4_CONFIG2_PROTOCOL \r
3e7104c2 2508 relative with the default address to judge.\r
da1d0201 2509\r
2510 @retval TRUE If the default address is static.\r
2511 @retval FALSE If the default address is acquired from DHCP.\r
2512\r
2513**/\r
da1d0201 2514BOOLEAN\r
2515NetLibDefaultAddressIsStatic (\r
2516 IN EFI_HANDLE Controller\r
2517 )\r
2518{\r
63886849 2519 EFI_STATUS Status;\r
6c5c70d6 2520 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;\r
2521 UINTN DataSize; \r
2522 EFI_IP4_CONFIG2_POLICY Policy;\r
63886849 2523 BOOLEAN IsStatic;\r
da1d0201 2524\r
6c5c70d6 2525 Ip4Config2 = NULL;\r
2526 \r
2527 DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);\r
2528\r
2529 IsStatic = TRUE;\r
1dc1b43f 2530\r
63886849 2531 //\r
6c5c70d6 2532 // Get Ip4Config2 policy.\r
63886849 2533 //\r
6c5c70d6 2534 Status = gBS->HandleProtocol (Controller, &gEfiIp4Config2ProtocolGuid, (VOID **) &Ip4Config2);\r
63886849 2535 if (EFI_ERROR (Status)) {\r
2536 goto ON_EXIT;\r
da1d0201 2537 }\r
2538\r
6c5c70d6 2539 Status = Ip4Config2->GetData (Ip4Config2, Ip4Config2DataTypePolicy, &DataSize, &Policy);\r
da1d0201 2540 if (EFI_ERROR (Status)) {\r
2541 goto ON_EXIT;\r
2542 }\r
6c5c70d6 2543 \r
2544 IsStatic = (BOOLEAN) (Policy == Ip4Config2PolicyStatic);\r
1204fe83 2545\r
da1d0201 2546ON_EXIT:\r
6c5c70d6 2547 \r
da1d0201 2548 return IsStatic;\r
2549}\r
2550\r
2551/**\r
2552 Create an IPv4 device path node.\r
1204fe83 2553\r
b9008c87 2554 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.\r
2555 The header subtype of IPv4 device path node is MSG_IPv4_DP.\r
b9008c87 2556 Get other info from parameters to make up the whole IPv4 device path node.\r
da1d0201 2557\r
3e7104c2 2558 @param[in, out] Node Pointer to the IPv4 device path node.\r
f6b7393c 2559 @param[in] Controller The controller handle.\r
3e7104c2 2560 @param[in] LocalIp The local IPv4 address.\r
2561 @param[in] LocalPort The local port.\r
2562 @param[in] RemoteIp The remote IPv4 address.\r
2563 @param[in] RemotePort The remote port.\r
2564 @param[in] Protocol The protocol type in the IP header.\r
2565 @param[in] UseDefaultAddress Whether this instance is using default address or not.\r
da1d0201 2566\r
da1d0201 2567**/\r
2568VOID\r
7b414b4e 2569EFIAPI\r
da1d0201 2570NetLibCreateIPv4DPathNode (\r
2571 IN OUT IPv4_DEVICE_PATH *Node,\r
2572 IN EFI_HANDLE Controller,\r
2573 IN IP4_ADDR LocalIp,\r
2574 IN UINT16 LocalPort,\r
2575 IN IP4_ADDR RemoteIp,\r
2576 IN UINT16 RemotePort,\r
2577 IN UINT16 Protocol,\r
2578 IN BOOLEAN UseDefaultAddress\r
2579 )\r
2580{\r
2581 Node->Header.Type = MESSAGING_DEVICE_PATH;\r
2582 Node->Header.SubType = MSG_IPv4_DP;\r
501793fa 2583 SetDevicePathNodeLength (&Node->Header, sizeof (IPv4_DEVICE_PATH));\r
da1d0201 2584\r
e48e37fc 2585 CopyMem (&Node->LocalIpAddress, &LocalIp, sizeof (EFI_IPv4_ADDRESS));\r
2586 CopyMem (&Node->RemoteIpAddress, &RemoteIp, sizeof (EFI_IPv4_ADDRESS));\r
da1d0201 2587\r
2588 Node->LocalPort = LocalPort;\r
2589 Node->RemotePort = RemotePort;\r
2590\r
2591 Node->Protocol = Protocol;\r
2592\r
2593 if (!UseDefaultAddress) {\r
2594 Node->StaticIpAddress = TRUE;\r
2595 } else {\r
2596 Node->StaticIpAddress = NetLibDefaultAddressIsStatic (Controller);\r
2597 }\r
501793fa
RN
2598\r
2599 //\r
2600 // Set the Gateway IP address to default value 0:0:0:0.\r
2601 // Set the Subnet mask to default value 255:255:255:0.\r
2602 //\r
2603 ZeroMem (&Node->GatewayIpAddress, sizeof (EFI_IPv4_ADDRESS));\r
2604 SetMem (&Node->SubnetMask, sizeof (EFI_IPv4_ADDRESS), 0xff);\r
2605 Node->SubnetMask.Addr[3] = 0;\r
da1d0201 2606}\r
2607\r
f6b7393c 2608/**\r
2609 Create an IPv6 device path node.\r
1204fe83 2610\r
f6b7393c 2611 The header type of IPv6 device path node is MESSAGING_DEVICE_PATH.\r
2612 The header subtype of IPv6 device path node is MSG_IPv6_DP.\r
2613 Get other info from parameters to make up the whole IPv6 device path node.\r
2614\r
2615 @param[in, out] Node Pointer to the IPv6 device path node.\r
2616 @param[in] Controller The controller handle.\r
2617 @param[in] LocalIp The local IPv6 address.\r
2618 @param[in] LocalPort The local port.\r
2619 @param[in] RemoteIp The remote IPv6 address.\r
2620 @param[in] RemotePort The remote port.\r
2621 @param[in] Protocol The protocol type in the IP header.\r
2622\r
2623**/\r
2624VOID\r
2625EFIAPI\r
2626NetLibCreateIPv6DPathNode (\r
2627 IN OUT IPv6_DEVICE_PATH *Node,\r
2628 IN EFI_HANDLE Controller,\r
2629 IN EFI_IPv6_ADDRESS *LocalIp,\r
2630 IN UINT16 LocalPort,\r
2631 IN EFI_IPv6_ADDRESS *RemoteIp,\r
2632 IN UINT16 RemotePort,\r
2633 IN UINT16 Protocol\r
2634 )\r
2635{\r
2636 Node->Header.Type = MESSAGING_DEVICE_PATH;\r
2637 Node->Header.SubType = MSG_IPv6_DP;\r
2638 SetDevicePathNodeLength (&Node->Header, sizeof (IPv6_DEVICE_PATH));\r
2639\r
2640 CopyMem (&Node->LocalIpAddress, LocalIp, sizeof (EFI_IPv6_ADDRESS));\r
2641 CopyMem (&Node->RemoteIpAddress, RemoteIp, sizeof (EFI_IPv6_ADDRESS));\r
2642\r
2643 Node->LocalPort = LocalPort;\r
2644 Node->RemotePort = RemotePort;\r
2645\r
2646 Node->Protocol = Protocol;\r
501793fa
RN
2647\r
2648 //\r
2649 // Set default value to IPAddressOrigin, PrefixLength.\r
2650 // Set the Gateway IP address to unspecified address.\r
2651 //\r
2652 Node->IpAddressOrigin = 0;\r
2653 Node->PrefixLength = IP6_PREFIX_LENGTH;\r
2654 ZeroMem (&Node->GatewayIpAddress, sizeof (EFI_IPv6_ADDRESS));\r
f6b7393c 2655}\r
da1d0201 2656\r
2657/**\r
2658 Find the UNDI/SNP handle from controller and protocol GUID.\r
1204fe83 2659\r
da1d0201 2660 For example, IP will open a MNP child to transmit/receive\r
2661 packets, when MNP is stopped, IP should also be stopped. IP\r
2662 needs to find its own private data which is related the IP's\r
2663 service binding instance that is install on UNDI/SNP handle.\r
2664 Now, the controller is either a MNP or ARP child handle. But\r
2665 IP opens these handle BY_DRIVER, use that info, we can get the\r
2666 UNDI/SNP handle.\r
2667\r
3e7104c2 2668 @param[in] Controller Then protocol handle to check.\r
2669 @param[in] ProtocolGuid The protocol that is related with the handle.\r
da1d0201 2670\r
3e7104c2 2671 @return The UNDI/SNP handle or NULL for errors.\r
da1d0201 2672\r
2673**/\r
2674EFI_HANDLE\r
7b414b4e 2675EFIAPI\r
da1d0201 2676NetLibGetNicHandle (\r
2677 IN EFI_HANDLE Controller,\r
2678 IN EFI_GUID *ProtocolGuid\r
2679 )\r
2680{\r
2681 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer;\r
2682 EFI_HANDLE Handle;\r
2683 EFI_STATUS Status;\r
2684 UINTN OpenCount;\r
2685 UINTN Index;\r
2686\r
2687 Status = gBS->OpenProtocolInformation (\r
2688 Controller,\r
2689 ProtocolGuid,\r
2690 &OpenBuffer,\r
2691 &OpenCount\r
2692 );\r
2693\r
2694 if (EFI_ERROR (Status)) {\r
2695 return NULL;\r
2696 }\r
2697\r
2698 Handle = NULL;\r
2699\r
2700 for (Index = 0; Index < OpenCount; Index++) {\r
e2851998 2701 if ((OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {\r
da1d0201 2702 Handle = OpenBuffer[Index].ControllerHandle;\r
2703 break;\r
2704 }\r
2705 }\r
2706\r
2707 gBS->FreePool (OpenBuffer);\r
2708 return Handle;\r
2709}\r
e4ef0031 2710\r
2711/**\r
2712 Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS.\r
2713\r
2714 @param[in] String The pointer to the Ascii string.\r
2715 @param[out] Ip4Address The pointer to the converted IPv4 address.\r
2716\r
dd29f3ed 2717 @retval EFI_SUCCESS Convert to IPv4 address successfully.\r
e4ef0031 2718 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.\r
2719\r
2720**/\r
2721EFI_STATUS\r
e798cd87 2722EFIAPI\r
e4ef0031 2723NetLibAsciiStrToIp4 (\r
2724 IN CONST CHAR8 *String,\r
2725 OUT EFI_IPv4_ADDRESS *Ip4Address\r
2726 )\r
2727{\r
9f5ca5ef
RN
2728 RETURN_STATUS Status;\r
2729 CHAR8 *EndPointer;\r
e4ef0031 2730\r
9f5ca5ef
RN
2731 Status = AsciiStrToIpv4Address (String, &EndPointer, Ip4Address, NULL);\r
2732 if (RETURN_ERROR (Status) || (*EndPointer != '\0')) {\r
e4ef0031 2733 return EFI_INVALID_PARAMETER;\r
9f5ca5ef
RN
2734 } else {\r
2735 return EFI_SUCCESS;\r
e4ef0031 2736 }\r
e4ef0031 2737}\r
2738\r
2739\r
2740/**\r
2741 Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of the\r
3b28e744 2742 string is defined in RFC 4291 - Text Representation of Addresses.\r
e4ef0031 2743\r
2744 @param[in] String The pointer to the Ascii string.\r
2745 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
2746\r
dd29f3ed 2747 @retval EFI_SUCCESS Convert to IPv6 address successfully.\r
e4ef0031 2748 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.\r
2749\r
2750**/\r
2751EFI_STATUS\r
e798cd87 2752EFIAPI\r
e4ef0031 2753NetLibAsciiStrToIp6 (\r
2754 IN CONST CHAR8 *String,\r
2755 OUT EFI_IPv6_ADDRESS *Ip6Address\r
2756 )\r
2757{\r
9f5ca5ef
RN
2758 RETURN_STATUS Status;\r
2759 CHAR8 *EndPointer;\r
e4ef0031 2760\r
9f5ca5ef
RN
2761 Status = AsciiStrToIpv6Address (String, &EndPointer, Ip6Address, NULL);\r
2762 if (RETURN_ERROR (Status) || (*EndPointer != '\0')) {\r
e4ef0031 2763 return EFI_INVALID_PARAMETER;\r
9f5ca5ef
RN
2764 } else {\r
2765 return EFI_SUCCESS;\r
e4ef0031 2766 }\r
e4ef0031 2767}\r
2768\r
2769\r
2770/**\r
2771 Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS.\r
2772\r
2773 @param[in] String The pointer to the Ascii string.\r
2774 @param[out] Ip4Address The pointer to the converted IPv4 address.\r
2775\r
dd29f3ed 2776 @retval EFI_SUCCESS Convert to IPv4 address successfully.\r
e4ef0031 2777 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.\r
e4ef0031 2778\r
2779**/\r
2780EFI_STATUS\r
e798cd87 2781EFIAPI\r
e4ef0031 2782NetLibStrToIp4 (\r
2783 IN CONST CHAR16 *String,\r
2784 OUT EFI_IPv4_ADDRESS *Ip4Address\r
2785 )\r
2786{\r
9f5ca5ef
RN
2787 RETURN_STATUS Status;\r
2788 CHAR16 *EndPointer;\r
dd29f3ed 2789\r
9f5ca5ef
RN
2790 Status = StrToIpv4Address (String, &EndPointer, Ip4Address, NULL);\r
2791 if (RETURN_ERROR (Status) || (*EndPointer != L'\0')) {\r
e4ef0031 2792 return EFI_INVALID_PARAMETER;\r
9f5ca5ef
RN
2793 } else {\r
2794 return EFI_SUCCESS;\r
e4ef0031 2795 }\r
e4ef0031 2796}\r
2797\r
2798\r
2799/**\r
2800 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of\r
3b28e744 2801 the string is defined in RFC 4291 - Text Representation of Addresses.\r
e4ef0031 2802\r
2803 @param[in] String The pointer to the Ascii string.\r
2804 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
2805\r
dd29f3ed 2806 @retval EFI_SUCCESS Convert to IPv6 address successfully.\r
e4ef0031 2807 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.\r
e4ef0031 2808\r
2809**/\r
2810EFI_STATUS\r
e798cd87 2811EFIAPI\r
e4ef0031 2812NetLibStrToIp6 (\r
2813 IN CONST CHAR16 *String,\r
2814 OUT EFI_IPv6_ADDRESS *Ip6Address\r
dd29f3ed 2815 )\r
e4ef0031 2816{\r
9f5ca5ef
RN
2817 RETURN_STATUS Status;\r
2818 CHAR16 *EndPointer;\r
dd29f3ed 2819\r
9f5ca5ef
RN
2820 Status = StrToIpv6Address (String, &EndPointer, Ip6Address, NULL);\r
2821 if (RETURN_ERROR (Status) || (*EndPointer != L'\0')) {\r
e4ef0031 2822 return EFI_INVALID_PARAMETER;\r
9f5ca5ef
RN
2823 } else {\r
2824 return EFI_SUCCESS;\r
e4ef0031 2825 }\r
e4ef0031 2826}\r
2827\r
2828/**\r
2829 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length.\r
3b28e744 2830 The format of the string is defined in RFC 4291 - Text Representation of Addresses\r
e4ef0031 2831 Prefixes: ipv6-address/prefix-length.\r
2832\r
2833 @param[in] String The pointer to the Ascii string.\r
2834 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
2835 @param[out] PrefixLength The pointer to the converted prefix length.\r
2836\r
dd29f3ed 2837 @retval EFI_SUCCESS Convert to IPv6 address successfully.\r
e4ef0031 2838 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.\r
e4ef0031 2839\r
2840**/\r
2841EFI_STATUS\r
e798cd87 2842EFIAPI\r
e4ef0031 2843NetLibStrToIp6andPrefix (\r
2844 IN CONST CHAR16 *String,\r
2845 OUT EFI_IPv6_ADDRESS *Ip6Address,\r
2846 OUT UINT8 *PrefixLength\r
dd29f3ed 2847 )\r
e4ef0031 2848{\r
9f5ca5ef
RN
2849 RETURN_STATUS Status;\r
2850 CHAR16 *EndPointer;\r
e4ef0031 2851\r
9f5ca5ef
RN
2852 Status = StrToIpv6Address (String, &EndPointer, Ip6Address, PrefixLength);\r
2853 if (RETURN_ERROR (Status) || (*EndPointer != L'\0')) {\r
2854 return EFI_INVALID_PARAMETER;\r
e4ef0031 2855 } else {\r
9f5ca5ef 2856 return EFI_SUCCESS;\r
e4ef0031 2857 }\r
e4ef0031 2858}\r
2859\r
216f7970 2860/**\r
2861\r
2862 Convert one EFI_IPv6_ADDRESS to Null-terminated Unicode string.\r
2863 The text representation of address is defined in RFC 4291.\r
2864 \r
2865 @param[in] Ip6Address The pointer to the IPv6 address.\r
2866 @param[out] String The buffer to return the converted string.\r
2867 @param[in] StringSize The length in bytes of the input String.\r
2868 \r
2869 @retval EFI_SUCCESS Convert to string successfully.\r
2870 @retval EFI_INVALID_PARAMETER The input parameter is invalid.\r
2871 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small for the result. BufferSize has been \r
2872 updated with the size needed to complete the request.\r
2873**/\r
2874EFI_STATUS\r
2875EFIAPI\r
2876NetLibIp6ToStr (\r
2877 IN EFI_IPv6_ADDRESS *Ip6Address,\r
2878 OUT CHAR16 *String,\r
2879 IN UINTN StringSize\r
2880 )\r
2881{\r
2882 UINT16 Ip6Addr[8];\r
2883 UINTN Index;\r
2884 UINTN LongestZerosStart;\r
2885 UINTN LongestZerosLength;\r
2886 UINTN CurrentZerosStart;\r
2887 UINTN CurrentZerosLength;\r
2888 CHAR16 Buffer[sizeof"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];\r
2889 CHAR16 *Ptr;\r
2890\r
2891 if (Ip6Address == NULL || String == NULL || StringSize == 0) {\r
2892 return EFI_INVALID_PARAMETER;\r
2893 }\r
2894\r
2895 //\r
2896 // Convert the UINT8 array to an UINT16 array for easy handling.\r
2897 // \r
2898 ZeroMem (Ip6Addr, sizeof (Ip6Addr));\r
2899 for (Index = 0; Index < 16; Index++) {\r
2900 Ip6Addr[Index / 2] |= (Ip6Address->Addr[Index] << ((1 - (Index % 2)) << 3));\r
2901 }\r
57b301b5 2902\r
216f7970 2903 //\r
2904 // Find the longest zeros and mark it.\r
2905 //\r
2906 CurrentZerosStart = DEFAULT_ZERO_START;\r
2907 CurrentZerosLength = 0;\r
2908 LongestZerosStart = DEFAULT_ZERO_START;\r
2909 LongestZerosLength = 0;\r
2910 for (Index = 0; Index < 8; Index++) {\r
2911 if (Ip6Addr[Index] == 0) {\r
2912 if (CurrentZerosStart == DEFAULT_ZERO_START) {\r
2913 CurrentZerosStart = Index;\r
2914 CurrentZerosLength = 1;\r
2915 } else {\r
2916 CurrentZerosLength++;\r
2917 }\r
2918 } else {\r
2919 if (CurrentZerosStart != DEFAULT_ZERO_START) {\r
2920 if (CurrentZerosLength > 2 && (LongestZerosStart == (DEFAULT_ZERO_START) || CurrentZerosLength > LongestZerosLength)) {\r
2921 LongestZerosStart = CurrentZerosStart;\r
2922 LongestZerosLength = CurrentZerosLength;\r
2923 }\r
2924 CurrentZerosStart = DEFAULT_ZERO_START;\r
2925 CurrentZerosLength = 0;\r
2926 }\r
2927 }\r
2928 }\r
2929 \r
2930 if (CurrentZerosStart != DEFAULT_ZERO_START && CurrentZerosLength > 2) {\r
2931 if (LongestZerosStart == DEFAULT_ZERO_START || LongestZerosLength < CurrentZerosLength) {\r
2932 LongestZerosStart = CurrentZerosStart;\r
2933 LongestZerosLength = CurrentZerosLength;\r
2934 }\r
2935 }\r
2936\r
2937 Ptr = Buffer;\r
2938 for (Index = 0; Index < 8; Index++) {\r
2939 if (LongestZerosStart != DEFAULT_ZERO_START && Index >= LongestZerosStart && Index < LongestZerosStart + LongestZerosLength) {\r
2940 if (Index == LongestZerosStart) {\r
2941 *Ptr++ = L':';\r
2942 }\r
2943 continue;\r
2944 }\r
2945 if (Index != 0) {\r
2946 *Ptr++ = L':';\r
2947 }\r
2948 Ptr += UnicodeSPrint(Ptr, 10, L"%x", Ip6Addr[Index]);\r
2949 }\r
2950 \r
2951 if (LongestZerosStart != DEFAULT_ZERO_START && LongestZerosStart + LongestZerosLength == 8) {\r
2952 *Ptr++ = L':';\r
2953 }\r
2954 *Ptr = L'\0';\r
2955\r
2956 if ((UINTN)Ptr - (UINTN)Buffer > StringSize) {\r
2957 return EFI_BUFFER_TOO_SMALL;\r
2958 }\r
2959\r
206b5f51 2960 StrCpyS (String, StringSize / sizeof (CHAR16), Buffer);\r
216f7970 2961\r
2962 return EFI_SUCCESS;\r
2963}\r
57b301b5 2964\r
2965/**\r
2966 This function obtains the system guid from the smbios table.\r
2967\r
2968 @param[out] SystemGuid The pointer of the returned system guid.\r
2969\r
2970 @retval EFI_SUCCESS Successfully obtained the system guid.\r
2971 @retval EFI_NOT_FOUND Did not find the SMBIOS table.\r
2972\r
2973**/\r
2974EFI_STATUS\r
2975EFIAPI\r
2976NetLibGetSystemGuid (\r
2977 OUT EFI_GUID *SystemGuid\r
2978 )\r
2979{\r
33ecfa8a
SEHM
2980 EFI_STATUS Status;\r
2981 SMBIOS_TABLE_ENTRY_POINT *SmbiosTable;\r
2982 SMBIOS_TABLE_3_0_ENTRY_POINT *Smbios30Table;\r
2983 SMBIOS_STRUCTURE_POINTER Smbios;\r
2984 SMBIOS_STRUCTURE_POINTER SmbiosEnd;\r
2985 CHAR8 *String;\r
57b301b5 2986\r
2987 SmbiosTable = NULL;\r
33ecfa8a
SEHM
2988 Status = EfiGetSystemConfigurationTable (&gEfiSmbios3TableGuid, (VOID **) &Smbios30Table);\r
2989 if (!(EFI_ERROR (Status) || Smbios30Table == NULL)) {\r
2990 Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) Smbios30Table->TableAddress;\r
2991 SmbiosEnd.Raw = (UINT8 *) (UINTN) (Smbios30Table->TableAddress + Smbios30Table->TableMaximumSize);\r
2992 } else {\r
2993 Status = EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **) &SmbiosTable);\r
2994 if (EFI_ERROR (Status) || SmbiosTable == NULL) {\r
2995 return EFI_NOT_FOUND;\r
2996 }\r
2997 Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) SmbiosTable->TableAddress;\r
16f69227 2998 SmbiosEnd.Raw = (UINT8 *) ((UINTN) SmbiosTable->TableAddress + SmbiosTable->TableLength);\r
57b301b5 2999 }\r
3000\r
57b301b5 3001 do {\r
3002 if (Smbios.Hdr->Type == 1) {\r
3003 if (Smbios.Hdr->Length < 0x19) {\r
3004 //\r
3005 // Older version did not support UUID.\r
3006 //\r
3007 return EFI_NOT_FOUND;\r
3008 }\r
3009 \r
3010 //\r
3011 // SMBIOS tables are byte packed so we need to do a byte copy to\r
3012 // prevend alignment faults on Itanium-based platform.\r
3013 //\r
3014 CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID));\r
3015 return EFI_SUCCESS;\r
3016 }\r
3017\r
3018 //\r
3019 // Go to the next SMBIOS structure. Each SMBIOS structure may include 2 parts:\r
3020 // 1. Formatted section; 2. Unformatted string section. So, 2 steps are needed\r
3021 // to skip one SMBIOS structure.\r
3022 //\r
3023 \r
3024 //\r
3025 // Step 1: Skip over formatted section.\r
3026 //\r
3027 String = (CHAR8 *) (Smbios.Raw + Smbios.Hdr->Length);\r
3028 \r
3029 //\r
3030 // Step 2: Skip over unformated string section.\r
3031 //\r
3032 do {\r
3033 //\r
3034 // Each string is terminated with a NULL(00h) BYTE and the sets of strings\r
3035 // is terminated with an additional NULL(00h) BYTE.\r
3036 //\r
3037 for ( ; *String != 0; String++) {\r
3038 }\r
3039\r
3040 if (*(UINT8*)++String == 0) {\r
3041 //\r
3042 // Pointer to the next SMBIOS structure.\r
3043 //\r
3044 Smbios.Raw = (UINT8 *)++String;\r
3045 break;\r
3046 } \r
3047 } while (TRUE);\r
3048 } while (Smbios.Raw < SmbiosEnd.Raw);\r
3049 return EFI_NOT_FOUND;\r
3050}\r
dba6e9a9
JW
3051\r
3052/**\r
3053 Create Dns QName according the queried domain name. \r
3054 QName is a domain name represented as a sequence of labels, \r
3055 where each label consists of a length octet followed by that \r
3056 number of octets. The QName terminates with the zero \r
3057 length octet for the null label of the root. Caller should \r
3058 take responsibility to free the buffer in returned pointer.\r
3059\r
3060 @param DomainName The pointer to the queried domain name string. \r
3061\r
3062 @retval NULL Failed to fill QName.\r
3063 @return QName filled successfully.\r
3064 \r
3065**/ \r
3066CHAR8 *\r
3067EFIAPI\r
3068NetLibCreateDnsQName (\r
3069 IN CHAR16 *DomainName\r
3070 )\r
3071{\r
3072 CHAR8 *QueryName;\r
3073 UINTN QueryNameSize;\r
3074 CHAR8 *Header;\r
3075 CHAR8 *Tail;\r
3076 UINTN Len;\r
3077 UINTN Index;\r
3078\r
3079 QueryName = NULL;\r
3080 QueryNameSize = 0;\r
3081 Header = NULL;\r
3082 Tail = NULL;\r
3083\r
3084 //\r
3085 // One byte for first label length, one byte for terminated length zero. \r
3086 //\r
3087 QueryNameSize = StrLen (DomainName) + 2;\r
3088 \r
3089 if (QueryNameSize > DNS_MAX_NAME_SIZE) {\r
3090 return NULL;\r
3091 }\r
3092\r
3093 QueryName = AllocateZeroPool (QueryNameSize);\r
3094 if (QueryName == NULL) {\r
3095 return NULL;\r
3096 }\r
3097 \r
3098 Header = QueryName;\r
3099 Tail = Header + 1;\r
3100 Len = 0;\r
3101 for (Index = 0; DomainName[Index] != 0; Index++) {\r
3102 *Tail = (CHAR8) DomainName[Index];\r
3103 if (*Tail == '.') {\r
3104 *Header = (CHAR8) Len;\r
3105 Header = Tail;\r
3106 Tail ++;\r
3107 Len = 0;\r
3108 } else {\r
3109 Tail++;\r
3110 Len++;\r
3111 }\r
3112 }\r
3113 *Header = (CHAR8) Len;\r
3114 *Tail = 0;\r
3115\r
3116 return QueryName;\r
3b28e744 3117}\r