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