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