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