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