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