]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
Removing IsaSerailDxe driver instead of removing the TerminalDxe driver when source...
[mirror_edk2.git] / MdeModulePkg / Library / DxeNetLib / DxeNetLib.c
CommitLineData
da1d0201 1/** @file\r
3e7104c2 2 Network library.\r
1204fe83 3\r
57b301b5 4Copyright (c) 2005 - 2011, 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
1832 Destory 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
8f5e6151 1841 @param[in] ChildHandle The child to destory.\r
da1d0201 1842\r
1843 @retval EFI_SUCCESS The child is successfully destoried.\r
1844 @retval Others Failed to destory the child.\r
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
1878 // destory the child\r
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
2171 get the simple network protocol.\r
2172 @param[out] MacString The pointer to store the address of the string\r
2173 representation of the mac address.\r
1204fe83 2174\r
3e7104c2 2175 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.\r
da1d0201 2176 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.\r
3e7104c2 2177 @retval Others Failed to open the simple network protocol.\r
da1d0201 2178\r
2179**/\r
2180EFI_STATUS\r
7b414b4e 2181EFIAPI\r
da1d0201 2182NetLibGetMacString (\r
779ae357 2183 IN EFI_HANDLE ServiceHandle,\r
3e7104c2 2184 IN EFI_HANDLE ImageHandle,\r
2185 OUT CHAR16 **MacString\r
da1d0201 2186 )\r
2187{\r
2188 EFI_STATUS Status;\r
779ae357 2189 EFI_MAC_ADDRESS MacAddress;\r
1204fe83 2190 UINT8 *HwAddress;\r
779ae357 2191 UINTN HwAddressSize;\r
2192 UINT16 VlanId;\r
2193 CHAR16 *String;\r
da1d0201 2194 UINTN Index;\r
2195\r
779ae357 2196 ASSERT (MacString != NULL);\r
da1d0201 2197\r
2198 //\r
779ae357 2199 // Get MAC address of the network device\r
da1d0201 2200 //\r
779ae357 2201 Status = NetLibGetMacAddress (ServiceHandle, &MacAddress, &HwAddressSize);\r
da1d0201 2202 if (EFI_ERROR (Status)) {\r
2203 return Status;\r
2204 }\r
2205\r
da1d0201 2206 //\r
2207 // It takes 2 unicode characters to represent a 1 byte binary buffer.\r
779ae357 2208 // If VLAN is configured, it will need extra 5 characters like "\0005".\r
da1d0201 2209 // Plus one unicode character for the null-terminator.\r
2210 //\r
779ae357 2211 String = AllocateZeroPool ((2 * HwAddressSize + 5 + 1) * sizeof (CHAR16));\r
2212 if (String == NULL) {\r
da1d0201 2213 return EFI_OUT_OF_RESOURCES;\r
2214 }\r
779ae357 2215 *MacString = String;\r
da1d0201 2216\r
2217 //\r
779ae357 2218 // Convert the MAC address into a unicode string.\r
da1d0201 2219 //\r
779ae357 2220 HwAddress = &MacAddress.Addr[0];\r
2221 for (Index = 0; Index < HwAddressSize; Index++) {\r
2222 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *(HwAddress++), 2);\r
da1d0201 2223 }\r
2224\r
779ae357 2225 //\r
2226 // Append VLAN ID if any\r
2227 //\r
2228 VlanId = NetLibGetVlanId (ServiceHandle);\r
2229 if (VlanId != 0) {\r
2230 *String++ = L'\\';\r
2231 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, VlanId, 4);\r
2232 }\r
da1d0201 2233\r
779ae357 2234 //\r
2235 // Null terminate the Unicode string\r
2236 //\r
2237 *String = L'\0';\r
da1d0201 2238\r
2239 return EFI_SUCCESS;\r
2240}\r
2241\r
dd29f3ed 2242/**\r
2243 Detect media status for specified network device.\r
2244\r
2245 The underlying UNDI driver may or may not support reporting media status from\r
2246 GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine\r
2247 will try to invoke Snp->GetStatus() to get the media status: if media already\r
2248 present, it return directly; if media not present, it will stop SNP and then\r
2249 restart SNP to get the latest media status, this give chance to get the correct\r
2250 media status for old UNDI driver which doesn't support reporting media status\r
2251 from GET_STATUS command.\r
2252 Note: there will be two limitations for current algorithm:\r
2253 1) for UNDI with this capability, in case of cable is not attached, there will\r
2254 be an redundant Stop/Start() process;\r
3b1464d5 2255 2) for UNDI without this capability, in case that network cable is attached when\r
2256 Snp->Initialize() is invoked while network cable is unattached later,\r
2257 NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer\r
2258 apps to wait for timeout time.\r
dd29f3ed 2259\r
2260 @param[in] ServiceHandle The handle where network service binding protocols are\r
2261 installed on.\r
2262 @param[out] MediaPresent The pointer to store the media status.\r
2263\r
2264 @retval EFI_SUCCESS Media detection success.\r
2265 @retval EFI_INVALID_PARAMETER ServiceHandle is not valid network device handle.\r
2266 @retval EFI_UNSUPPORTED Network device does not support media detection.\r
2267 @retval EFI_DEVICE_ERROR SNP is in unknown state.\r
2268\r
2269**/\r
2270EFI_STATUS\r
2271EFIAPI\r
2272NetLibDetectMedia (\r
2273 IN EFI_HANDLE ServiceHandle,\r
2274 OUT BOOLEAN *MediaPresent\r
2275 )\r
2276{\r
2277 EFI_STATUS Status;\r
2278 EFI_HANDLE SnpHandle;\r
2279 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
2280 UINT32 InterruptStatus;\r
2281 UINT32 OldState;\r
2282 EFI_MAC_ADDRESS *MCastFilter;\r
2283 UINT32 MCastFilterCount;\r
2284 UINT32 EnableFilterBits;\r
2285 UINT32 DisableFilterBits;\r
2286 BOOLEAN ResetMCastFilters;\r
2287\r
2288 ASSERT (MediaPresent != NULL);\r
2289\r
2290 //\r
2291 // Get SNP handle\r
2292 //\r
2293 Snp = NULL;\r
2294 SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp);\r
2295 if (SnpHandle == NULL) {\r
2296 return EFI_INVALID_PARAMETER;\r
2297 }\r
2298\r
2299 //\r
2300 // Check whether SNP support media detection\r
2301 //\r
2302 if (!Snp->Mode->MediaPresentSupported) {\r
2303 return EFI_UNSUPPORTED;\r
2304 }\r
2305\r
2306 //\r
2307 // Invoke Snp->GetStatus() to refresh MediaPresent field in SNP mode data\r
2308 //\r
2309 Status = Snp->GetStatus (Snp, &InterruptStatus, NULL);\r
2310 if (EFI_ERROR (Status)) {\r
2311 return Status;\r
2312 }\r
2313\r
2314 if (Snp->Mode->MediaPresent) {\r
2315 //\r
2316 // Media is present, return directly\r
2317 //\r
2318 *MediaPresent = TRUE;\r
2319 return EFI_SUCCESS;\r
2320 }\r
2321\r
2322 //\r
2323 // Till now, GetStatus() report no media; while, in case UNDI not support\r
2324 // reporting media status from GetStatus(), this media status may be incorrect.\r
2325 // So, we will stop SNP and then restart it to get the correct media status.\r
2326 //\r
2327 OldState = Snp->Mode->State;\r
2328 if (OldState >= EfiSimpleNetworkMaxState) {\r
2329 return EFI_DEVICE_ERROR;\r
2330 }\r
2331\r
2332 MCastFilter = NULL;\r
2333\r
2334 if (OldState == EfiSimpleNetworkInitialized) {\r
2335 //\r
2336 // SNP is already in use, need Shutdown/Stop and then Start/Initialize\r
2337 //\r
2338\r
2339 //\r
2340 // Backup current SNP receive filter settings\r
2341 //\r
2342 EnableFilterBits = Snp->Mode->ReceiveFilterSetting;\r
2343 DisableFilterBits = Snp->Mode->ReceiveFilterMask ^ EnableFilterBits;\r
2344\r
2345 ResetMCastFilters = TRUE;\r
2346 MCastFilterCount = Snp->Mode->MCastFilterCount;\r
2347 if (MCastFilterCount != 0) {\r
2348 MCastFilter = AllocateCopyPool (\r
2349 MCastFilterCount * sizeof (EFI_MAC_ADDRESS),\r
2350 Snp->Mode->MCastFilter\r
2351 );\r
2352 ASSERT (MCastFilter != NULL);\r
2353\r
2354 ResetMCastFilters = FALSE;\r
2355 }\r
2356\r
2357 //\r
2358 // Shutdown/Stop the simple network\r
2359 //\r
2360 Status = Snp->Shutdown (Snp);\r
2361 if (!EFI_ERROR (Status)) {\r
2362 Status = Snp->Stop (Snp);\r
2363 }\r
2364 if (EFI_ERROR (Status)) {\r
2365 goto Exit;\r
2366 }\r
2367\r
2368 //\r
2369 // Start/Initialize the simple network\r
2370 //\r
2371 Status = Snp->Start (Snp);\r
2372 if (!EFI_ERROR (Status)) {\r
2373 Status = Snp->Initialize (Snp, 0, 0);\r
2374 }\r
2375 if (EFI_ERROR (Status)) {\r
2376 goto Exit;\r
2377 }\r
2378\r
2379 //\r
2380 // Here we get the correct media status\r
2381 //\r
2382 *MediaPresent = Snp->Mode->MediaPresent;\r
2383\r
2384 //\r
2385 // Restore SNP receive filter settings\r
2386 //\r
2387 Status = Snp->ReceiveFilters (\r
2388 Snp,\r
2389 EnableFilterBits,\r
2390 DisableFilterBits,\r
2391 ResetMCastFilters,\r
2392 MCastFilterCount,\r
2393 MCastFilter\r
2394 );\r
2395\r
2396 if (MCastFilter != NULL) {\r
2397 FreePool (MCastFilter);\r
2398 }\r
2399\r
2400 return Status;\r
2401 }\r
2402\r
2403 //\r
2404 // SNP is not in use, it's in state of EfiSimpleNetworkStopped or EfiSimpleNetworkStarted\r
2405 //\r
2406 if (OldState == EfiSimpleNetworkStopped) {\r
2407 //\r
2408 // SNP not start yet, start it\r
2409 //\r
2410 Status = Snp->Start (Snp);\r
2411 if (EFI_ERROR (Status)) {\r
2412 goto Exit;\r
2413 }\r
2414 }\r
2415\r
2416 //\r
2417 // Initialize the simple network\r
2418 //\r
2419 Status = Snp->Initialize (Snp, 0, 0);\r
2420 if (EFI_ERROR (Status)) {\r
2421 Status = EFI_DEVICE_ERROR;\r
2422 goto Exit;\r
2423 }\r
2424\r
2425 //\r
2426 // Here we get the correct media status\r
2427 //\r
2428 *MediaPresent = Snp->Mode->MediaPresent;\r
2429\r
2430 //\r
2431 // Shut down the simple network\r
2432 //\r
2433 Snp->Shutdown (Snp);\r
2434\r
2435Exit:\r
2436 if (OldState == EfiSimpleNetworkStopped) {\r
2437 //\r
2438 // Original SNP sate is Stopped, restore to original state\r
2439 //\r
2440 Snp->Stop (Snp);\r
2441 }\r
2442\r
2443 if (MCastFilter != NULL) {\r
2444 FreePool (MCastFilter);\r
2445 }\r
2446\r
2447 return Status;\r
2448}\r
2449\r
da1d0201 2450/**\r
2451 Check the default address used by the IPv4 driver is static or dynamic (acquired\r
2452 from DHCP).\r
2453\r
1204fe83 2454 If the controller handle does not have the NIC Ip4 Config Protocol installed, the\r
b9008c87 2455 default address is static. If the EFI variable to save the configuration is not found,\r
1204fe83 2456 the default address is static. Otherwise, get the result from the EFI variable which\r
b9008c87 2457 saving the configuration.\r
1204fe83 2458\r
3e7104c2 2459 @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol\r
2460 relative with the default address to judge.\r
da1d0201 2461\r
2462 @retval TRUE If the default address is static.\r
2463 @retval FALSE If the default address is acquired from DHCP.\r
2464\r
2465**/\r
da1d0201 2466BOOLEAN\r
2467NetLibDefaultAddressIsStatic (\r
2468 IN EFI_HANDLE Controller\r
2469 )\r
2470{\r
63886849 2471 EFI_STATUS Status;\r
2472 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;\r
2473 UINTN Len;\r
2474 NIC_IP4_CONFIG_INFO *ConfigInfo;\r
2475 BOOLEAN IsStatic;\r
2476 EFI_STRING ConfigHdr;\r
2477 EFI_STRING ConfigResp;\r
2478 EFI_STRING AccessProgress;\r
2479 EFI_STRING AccessResults;\r
2480 EFI_STRING String;\r
1dc1b43f 2481 EFI_HANDLE ChildHandle;\r
63886849 2482\r
2483 ConfigInfo = NULL;\r
2484 ConfigHdr = NULL;\r
2485 ConfigResp = NULL;\r
2486 AccessProgress = NULL;\r
2487 AccessResults = NULL;\r
2488 IsStatic = TRUE;\r
2489\r
2490 Status = gBS->LocateProtocol (\r
70b68990 2491 &gEfiHiiConfigRoutingProtocolGuid,\r
2492 NULL,\r
2493 (VOID **) &HiiConfigRouting\r
2494 );\r
da1d0201 2495 if (EFI_ERROR (Status)) {\r
2496 return TRUE;\r
2497 }\r
2498\r
1dc1b43f 2499 Status = NetGetChildHandle (Controller, &ChildHandle);\r
2500 if (EFI_ERROR (Status)) {\r
2501 return TRUE;\r
2502 }\r
2503\r
63886849 2504 //\r
2505 // Construct config request string header\r
2506 //\r
1dc1b43f 2507 ConfigHdr = HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid, EFI_NIC_IP4_CONFIG_VARIABLE, ChildHandle);\r
894d038a 2508 if (ConfigHdr == NULL) {\r
2509 return TRUE;\r
2510 }\r
1204fe83 2511\r
63886849 2512 Len = StrLen (ConfigHdr);\r
ce4106be 2513 ConfigResp = AllocateZeroPool ((Len + NIC_ITEM_CONFIG_SIZE * 2 + 100) * sizeof (CHAR16));\r
63886849 2514 if (ConfigResp == NULL) {\r
2515 goto ON_EXIT;\r
2516 }\r
2517 StrCpy (ConfigResp, ConfigHdr);\r
2518\r
2519 String = ConfigResp + Len;\r
2520 UnicodeSPrint (\r
1204fe83 2521 String,\r
2522 (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16),\r
2523 L"&OFFSET=%04X&WIDTH=%04X",\r
2524 OFFSET_OF (NIC_IP4_CONFIG_INFO, Source),\r
63886849 2525 sizeof (UINT32)\r
2526 );\r
2527\r
2528 Status = HiiConfigRouting->ExtractConfig (\r
2529 HiiConfigRouting,\r
2530 ConfigResp,\r
2531 &AccessProgress,\r
2532 &AccessResults\r
2533 );\r
2534 if (EFI_ERROR (Status)) {\r
2535 goto ON_EXIT;\r
da1d0201 2536 }\r
2537\r
1dc1b43f 2538 ConfigInfo = AllocateZeroPool (NIC_ITEM_CONFIG_SIZE);\r
da1d0201 2539 if (ConfigInfo == NULL) {\r
63886849 2540 goto ON_EXIT;\r
da1d0201 2541 }\r
2542\r
63886849 2543 ConfigInfo->Source = IP4_CONFIG_SOURCE_STATIC;\r
2544 Len = NIC_ITEM_CONFIG_SIZE;\r
2545 Status = HiiConfigRouting->ConfigToBlock (\r
2546 HiiConfigRouting,\r
2547 AccessResults,\r
2548 (UINT8 *) ConfigInfo,\r
2549 &Len,\r
2550 &AccessProgress\r
2551 );\r
da1d0201 2552 if (EFI_ERROR (Status)) {\r
2553 goto ON_EXIT;\r
2554 }\r
2555\r
2556 IsStatic = (BOOLEAN) (ConfigInfo->Source == IP4_CONFIG_SOURCE_STATIC);\r
1204fe83 2557\r
da1d0201 2558ON_EXIT:\r
2559\r
63886849 2560 if (AccessResults != NULL) {\r
2561 FreePool (AccessResults);\r
2562 }\r
2563 if (ConfigInfo != NULL) {\r
2564 FreePool (ConfigInfo);\r
2565 }\r
2566 if (ConfigResp != NULL) {\r
2567 FreePool (ConfigResp);\r
2568 }\r
2569 if (ConfigHdr != NULL) {\r
2570 FreePool (ConfigHdr);\r
2571 }\r
da1d0201 2572\r
2573 return IsStatic;\r
2574}\r
2575\r
2576/**\r
2577 Create an IPv4 device path node.\r
1204fe83 2578\r
b9008c87 2579 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.\r
2580 The header subtype of IPv4 device path node is MSG_IPv4_DP.\r
2581 The length of the IPv4 device path node in bytes is 19.\r
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
2609 SetDevicePathNodeLength (&Node->Header, 19);\r
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
2624}\r
2625\r
f6b7393c 2626/**\r
2627 Create an IPv6 device path node.\r
1204fe83 2628\r
f6b7393c 2629 The header type of IPv6 device path node is MESSAGING_DEVICE_PATH.\r
2630 The header subtype of IPv6 device path node is MSG_IPv6_DP.\r
2631 Get other info from parameters to make up the whole IPv6 device path node.\r
2632\r
2633 @param[in, out] Node Pointer to the IPv6 device path node.\r
2634 @param[in] Controller The controller handle.\r
2635 @param[in] LocalIp The local IPv6 address.\r
2636 @param[in] LocalPort The local port.\r
2637 @param[in] RemoteIp The remote IPv6 address.\r
2638 @param[in] RemotePort The remote port.\r
2639 @param[in] Protocol The protocol type in the IP header.\r
2640\r
2641**/\r
2642VOID\r
2643EFIAPI\r
2644NetLibCreateIPv6DPathNode (\r
2645 IN OUT IPv6_DEVICE_PATH *Node,\r
2646 IN EFI_HANDLE Controller,\r
2647 IN EFI_IPv6_ADDRESS *LocalIp,\r
2648 IN UINT16 LocalPort,\r
2649 IN EFI_IPv6_ADDRESS *RemoteIp,\r
2650 IN UINT16 RemotePort,\r
2651 IN UINT16 Protocol\r
2652 )\r
2653{\r
2654 Node->Header.Type = MESSAGING_DEVICE_PATH;\r
2655 Node->Header.SubType = MSG_IPv6_DP;\r
2656 SetDevicePathNodeLength (&Node->Header, sizeof (IPv6_DEVICE_PATH));\r
2657\r
2658 CopyMem (&Node->LocalIpAddress, LocalIp, sizeof (EFI_IPv6_ADDRESS));\r
2659 CopyMem (&Node->RemoteIpAddress, RemoteIp, sizeof (EFI_IPv6_ADDRESS));\r
2660\r
2661 Node->LocalPort = LocalPort;\r
2662 Node->RemotePort = RemotePort;\r
2663\r
2664 Node->Protocol = Protocol;\r
2665 Node->StaticIpAddress = FALSE;\r
2666}\r
da1d0201 2667\r
2668/**\r
2669 Find the UNDI/SNP handle from controller and protocol GUID.\r
1204fe83 2670\r
da1d0201 2671 For example, IP will open a MNP child to transmit/receive\r
2672 packets, when MNP is stopped, IP should also be stopped. IP\r
2673 needs to find its own private data which is related the IP's\r
2674 service binding instance that is install on UNDI/SNP handle.\r
2675 Now, the controller is either a MNP or ARP child handle. But\r
2676 IP opens these handle BY_DRIVER, use that info, we can get the\r
2677 UNDI/SNP handle.\r
2678\r
3e7104c2 2679 @param[in] Controller Then protocol handle to check.\r
2680 @param[in] ProtocolGuid The protocol that is related with the handle.\r
da1d0201 2681\r
3e7104c2 2682 @return The UNDI/SNP handle or NULL for errors.\r
da1d0201 2683\r
2684**/\r
2685EFI_HANDLE\r
7b414b4e 2686EFIAPI\r
da1d0201 2687NetLibGetNicHandle (\r
2688 IN EFI_HANDLE Controller,\r
2689 IN EFI_GUID *ProtocolGuid\r
2690 )\r
2691{\r
2692 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer;\r
2693 EFI_HANDLE Handle;\r
2694 EFI_STATUS Status;\r
2695 UINTN OpenCount;\r
2696 UINTN Index;\r
2697\r
2698 Status = gBS->OpenProtocolInformation (\r
2699 Controller,\r
2700 ProtocolGuid,\r
2701 &OpenBuffer,\r
2702 &OpenCount\r
2703 );\r
2704\r
2705 if (EFI_ERROR (Status)) {\r
2706 return NULL;\r
2707 }\r
2708\r
2709 Handle = NULL;\r
2710\r
2711 for (Index = 0; Index < OpenCount; Index++) {\r
e2851998 2712 if ((OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {\r
da1d0201 2713 Handle = OpenBuffer[Index].ControllerHandle;\r
2714 break;\r
2715 }\r
2716 }\r
2717\r
2718 gBS->FreePool (OpenBuffer);\r
2719 return Handle;\r
2720}\r
e4ef0031 2721\r
2722/**\r
2723 Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS.\r
2724\r
2725 @param[in] String The pointer to the Ascii string.\r
2726 @param[out] Ip4Address The pointer to the converted IPv4 address.\r
2727\r
dd29f3ed 2728 @retval EFI_SUCCESS Convert to IPv4 address successfully.\r
e4ef0031 2729 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.\r
2730\r
2731**/\r
2732EFI_STATUS\r
e798cd87 2733EFIAPI\r
e4ef0031 2734NetLibAsciiStrToIp4 (\r
2735 IN CONST CHAR8 *String,\r
2736 OUT EFI_IPv4_ADDRESS *Ip4Address\r
2737 )\r
2738{\r
2739 UINT8 Index;\r
2740 CHAR8 *Ip4Str;\r
2741 CHAR8 *TempStr;\r
2742 UINTN NodeVal;\r
2743\r
2744 if ((String == NULL) || (Ip4Address == NULL)) {\r
2745 return EFI_INVALID_PARAMETER;\r
2746 }\r
2747\r
2748 Ip4Str = (CHAR8 *) String;\r
2749\r
2750 for (Index = 0; Index < 4; Index++) {\r
2751 TempStr = Ip4Str;\r
2752\r
2753 while ((*Ip4Str != '\0') && (*Ip4Str != '.')) {\r
2754 Ip4Str++;\r
2755 }\r
2756\r
2757 //\r
2758 // The IPv4 address is X.X.X.X\r
2759 //\r
2760 if (*Ip4Str == '.') {\r
2761 if (Index == 3) {\r
2762 return EFI_INVALID_PARAMETER;\r
2763 }\r
2764 } else {\r
2765 if (Index != 3) {\r
2766 return EFI_INVALID_PARAMETER;\r
2767 }\r
2768 }\r
2769\r
9b6f044a 2770 //\r
2771 // Convert the string to IPv4 address. AsciiStrDecimalToUintn stops at the\r
dd29f3ed 2772 // first character that is not a valid decimal character, '.' or '\0' here.\r
9b6f044a 2773 //\r
e4ef0031 2774 NodeVal = AsciiStrDecimalToUintn (TempStr);\r
2775 if (NodeVal > 0xFF) {\r
2776 return EFI_INVALID_PARAMETER;\r
2777 }\r
2778\r
2779 Ip4Address->Addr[Index] = (UINT8) NodeVal;\r
2780\r
2781 Ip4Str++;\r
2782 }\r
2783\r
2784 return EFI_SUCCESS;\r
2785}\r
2786\r
2787\r
2788/**\r
2789 Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of the\r
2790 string is defined in RFC 4291 - Text Pepresentation of Addresses.\r
2791\r
2792 @param[in] String The pointer to the Ascii string.\r
2793 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
2794\r
dd29f3ed 2795 @retval EFI_SUCCESS Convert to IPv6 address successfully.\r
e4ef0031 2796 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.\r
2797\r
2798**/\r
2799EFI_STATUS\r
e798cd87 2800EFIAPI\r
e4ef0031 2801NetLibAsciiStrToIp6 (\r
2802 IN CONST CHAR8 *String,\r
2803 OUT EFI_IPv6_ADDRESS *Ip6Address\r
2804 )\r
2805{\r
2806 UINT8 Index;\r
2807 CHAR8 *Ip6Str;\r
2808 CHAR8 *TempStr;\r
2809 CHAR8 *TempStr2;\r
2810 UINT8 NodeCnt;\r
2811 UINT8 TailNodeCnt;\r
2812 UINT8 AllowedCnt;\r
2813 UINTN NodeVal;\r
2814 BOOLEAN Short;\r
2815 BOOLEAN Update;\r
77545563 2816 BOOLEAN LeadZero;\r
2817 UINT8 LeadZeroCnt;\r
2818 UINT8 Cnt;\r
e4ef0031 2819\r
2820 if ((String == NULL) || (Ip6Address == NULL)) {\r
2821 return EFI_INVALID_PARAMETER;\r
2822 }\r
2823\r
77545563 2824 Ip6Str = (CHAR8 *) String;\r
2825 AllowedCnt = 6;\r
2826 LeadZeroCnt = 0;\r
e4ef0031 2827\r
2828 //\r
2829 // An IPv6 address leading with : looks strange.\r
2830 //\r
2831 if (*Ip6Str == ':') {\r
2832 if (*(Ip6Str + 1) != ':') {\r
2833 return EFI_INVALID_PARAMETER;\r
2834 } else {\r
2835 AllowedCnt = 7;\r
dd29f3ed 2836 }\r
e4ef0031 2837 }\r
2838\r
2839 ZeroMem (Ip6Address, sizeof (EFI_IPv6_ADDRESS));\r
2840\r
2841 NodeCnt = 0;\r
2842 TailNodeCnt = 0;\r
2843 Short = FALSE;\r
2844 Update = FALSE;\r
77545563 2845 LeadZero = FALSE;\r
e4ef0031 2846\r
9b6f044a 2847 for (Index = 0; Index < 15; Index = (UINT8) (Index + 2)) {\r
e4ef0031 2848 TempStr = Ip6Str;\r
2849\r
2850 while ((*Ip6Str != '\0') && (*Ip6Str != ':')) {\r
2851 Ip6Str++;\r
2852 }\r
2853\r
2854 if ((*Ip6Str == '\0') && (Index != 14)) {\r
2855 return EFI_INVALID_PARAMETER;\r
2856 }\r
2857\r
2858 if (*Ip6Str == ':') {\r
2859 if (*(Ip6Str + 1) == ':') {\r
77545563 2860 if ((NodeCnt > 6) || \r
2861 ((*(Ip6Str + 2) != '\0') && (AsciiStrHexToUintn (Ip6Str + 2) == 0))) {\r
e4ef0031 2862 //\r
2863 // ::0 looks strange. report error to user.\r
2864 //\r
2865 return EFI_INVALID_PARAMETER;\r
dd29f3ed 2866 }\r
77545563 2867 if ((NodeCnt == 6) && (*(Ip6Str + 2) != '\0') && \r
2868 (AsciiStrHexToUintn (Ip6Str + 2) != 0)) {\r
2869 return EFI_INVALID_PARAMETER;\r
2870 }\r
e4ef0031 2871\r
2872 //\r
2873 // Skip the abbreviation part of IPv6 address.\r
2874 //\r
2875 TempStr2 = Ip6Str + 2;\r
2876 while ((*TempStr2 != '\0')) {\r
2877 if (*TempStr2 == ':') {\r
2878 if (*(TempStr2 + 1) == ':') {\r
2879 //\r
2880 // :: can only appear once in IPv6 address.\r
2881 //\r
2882 return EFI_INVALID_PARAMETER;\r
2883 }\r
dd29f3ed 2884\r
e4ef0031 2885 TailNodeCnt++;\r
2886 if (TailNodeCnt >= (AllowedCnt - NodeCnt)) {\r
2887 //\r
2888 // :: indicates one or more groups of 16 bits of zeros.\r
2889 //\r
2890 return EFI_INVALID_PARAMETER;\r
2891 }\r
2892 }\r
2893\r
2894 TempStr2++;\r
dd29f3ed 2895 }\r
e4ef0031 2896\r
2897 Short = TRUE;\r
2898 Update = TRUE;\r
2899\r
2900 Ip6Str = Ip6Str + 2;\r
2901 } else {\r
77545563 2902 if (*(Ip6Str + 1) == '\0') {\r
2903 return EFI_INVALID_PARAMETER;\r
2904 }\r
e4ef0031 2905 Ip6Str++;\r
2906 NodeCnt++;\r
2907 if ((Short && (NodeCnt > 6)) || (!Short && (NodeCnt > 7))) {\r
2908 //\r
2909 // There are more than 8 groups of 16 bits of zeros.\r
2910 //\r
2911 return EFI_INVALID_PARAMETER;\r
2912 }\r
dd29f3ed 2913 }\r
2914 }\r
e4ef0031 2915\r
2916 //\r
9b6f044a 2917 // Convert the string to IPv6 address. AsciiStrHexToUintn stops at the first\r
dd29f3ed 2918 // character that is not a valid hexadecimal character, ':' or '\0' here.\r
e4ef0031 2919 //\r
2920 NodeVal = AsciiStrHexToUintn (TempStr);\r
2921 if ((NodeVal > 0xFFFF) || (Index > 14)) {\r
2922 return EFI_INVALID_PARAMETER;\r
2923 }\r
77545563 2924 if (NodeVal != 0) {\r
2925 if ((*TempStr == '0') && \r
2926 ((*(TempStr + 2) == ':') || (*(TempStr + 3) == ':') || \r
2927 (*(TempStr + 2) == '\0') || (*(TempStr + 3) == '\0'))) {\r
2928 return EFI_INVALID_PARAMETER;\r
2929 }\r
2930 if ((*TempStr == '0') && (*(TempStr + 4) != '\0') && \r
2931 (*(TempStr + 4) != ':')) { \r
2932 return EFI_INVALID_PARAMETER;\r
2933 }\r
2934 } else {\r
2935 if (((*TempStr == '0') && (*(TempStr + 1) == '0') && \r
2936 ((*(TempStr + 2) == ':') || (*(TempStr + 2) == '\0'))) ||\r
2937 ((*TempStr == '0') && (*(TempStr + 1) == '0') && (*(TempStr + 2) == '0') && \r
2938 ((*(TempStr + 3) == ':') || (*(TempStr + 3) == '\0')))) {\r
2939 return EFI_INVALID_PARAMETER;\r
2940 }\r
2941 }\r
2942\r
2943 Cnt = 0;\r
2944 while ((TempStr[Cnt] != ':') && (TempStr[Cnt] != '\0')) {\r
2945 Cnt++; \r
2946 }\r
2947 if (LeadZeroCnt == 0) {\r
2948 if ((Cnt == 4) && (*TempStr == '0')) {\r
2949 LeadZero = TRUE;\r
2950 LeadZeroCnt++;\r
2951 }\r
2952 if ((Cnt != 0) && (Cnt < 4)) {\r
2953 LeadZero = FALSE;\r
2954 LeadZeroCnt++;\r
2955 }\r
2956 } else {\r
3af91e23 2957 if ((Cnt == 4) && (*TempStr == '0') && !LeadZero) {\r
77545563 2958 return EFI_INVALID_PARAMETER;\r
2959 }\r
3af91e23 2960 if ((Cnt != 0) && (Cnt < 4) && LeadZero) {\r
77545563 2961 return EFI_INVALID_PARAMETER;\r
2962 }\r
2963 } \r
e4ef0031 2964\r
2965 Ip6Address->Addr[Index] = (UINT8) (NodeVal >> 8);\r
2966 Ip6Address->Addr[Index + 1] = (UINT8) (NodeVal & 0xFF);\r
2967\r
2968 //\r
2969 // Skip the groups of zeros by ::\r
2970 //\r
2971 if (Short && Update) {\r
2972 Index = (UINT8) (16 - (TailNodeCnt + 2) * 2);\r
2973 Update = FALSE;\r
2974 }\r
2975 }\r
2976\r
2977 if ((!Short && Index != 16) || (*Ip6Str != '\0')) {\r
2978 return EFI_INVALID_PARAMETER;\r
2979 }\r
2980\r
2981 return EFI_SUCCESS;\r
2982}\r
2983\r
2984\r
2985/**\r
2986 Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS.\r
2987\r
2988 @param[in] String The pointer to the Ascii string.\r
2989 @param[out] Ip4Address The pointer to the converted IPv4 address.\r
2990\r
dd29f3ed 2991 @retval EFI_SUCCESS Convert to IPv4 address successfully.\r
e4ef0031 2992 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.\r
2993 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.\r
2994\r
2995**/\r
2996EFI_STATUS\r
e798cd87 2997EFIAPI\r
e4ef0031 2998NetLibStrToIp4 (\r
2999 IN CONST CHAR16 *String,\r
3000 OUT EFI_IPv4_ADDRESS *Ip4Address\r
3001 )\r
3002{\r
3003 CHAR8 *Ip4Str;\r
3004 EFI_STATUS Status;\r
dd29f3ed 3005\r
e4ef0031 3006 if ((String == NULL) || (Ip4Address == NULL)) {\r
3007 return EFI_INVALID_PARAMETER;\r
3008 }\r
3009\r
3010 Ip4Str = (CHAR8 *) AllocatePool ((StrLen (String) + 1) * sizeof (CHAR8));\r
3011 if (Ip4Str == NULL) {\r
3012 return EFI_OUT_OF_RESOURCES;\r
3013 }\r
3014\r
3015 UnicodeStrToAsciiStr (String, Ip4Str);\r
3016\r
3017 Status = NetLibAsciiStrToIp4 (Ip4Str, Ip4Address);\r
3018\r
3019 FreePool (Ip4Str);\r
3020\r
3021 return Status;\r
3022}\r
3023\r
3024\r
3025/**\r
3026 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of\r
3027 the string is defined in RFC 4291 - Text Pepresentation of Addresses.\r
3028\r
3029 @param[in] String The pointer to the Ascii string.\r
3030 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
3031\r
dd29f3ed 3032 @retval EFI_SUCCESS Convert to IPv6 address successfully.\r
e4ef0031 3033 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.\r
3034 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.\r
3035\r
3036**/\r
3037EFI_STATUS\r
e798cd87 3038EFIAPI\r
e4ef0031 3039NetLibStrToIp6 (\r
3040 IN CONST CHAR16 *String,\r
3041 OUT EFI_IPv6_ADDRESS *Ip6Address\r
dd29f3ed 3042 )\r
e4ef0031 3043{\r
3044 CHAR8 *Ip6Str;\r
3045 EFI_STATUS Status;\r
dd29f3ed 3046\r
e4ef0031 3047 if ((String == NULL) || (Ip6Address == NULL)) {\r
3048 return EFI_INVALID_PARAMETER;\r
3049 }\r
3050\r
3051 Ip6Str = (CHAR8 *) AllocatePool ((StrLen (String) + 1) * sizeof (CHAR8));\r
3052 if (Ip6Str == NULL) {\r
3053 return EFI_OUT_OF_RESOURCES;\r
3054 }\r
3055\r
3056 UnicodeStrToAsciiStr (String, Ip6Str);\r
3057\r
3058 Status = NetLibAsciiStrToIp6 (Ip6Str, Ip6Address);\r
3059\r
3060 FreePool (Ip6Str);\r
3061\r
3062 return Status;\r
3063}\r
3064\r
3065/**\r
3066 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length.\r
3067 The format of the string is defined in RFC 4291 - Text Pepresentation of Addresses\r
3068 Prefixes: ipv6-address/prefix-length.\r
3069\r
3070 @param[in] String The pointer to the Ascii string.\r
3071 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
3072 @param[out] PrefixLength The pointer to the converted prefix length.\r
3073\r
dd29f3ed 3074 @retval EFI_SUCCESS Convert to IPv6 address successfully.\r
e4ef0031 3075 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL.\r
3076 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource.\r
3077\r
3078**/\r
3079EFI_STATUS\r
e798cd87 3080EFIAPI\r
e4ef0031 3081NetLibStrToIp6andPrefix (\r
3082 IN CONST CHAR16 *String,\r
3083 OUT EFI_IPv6_ADDRESS *Ip6Address,\r
3084 OUT UINT8 *PrefixLength\r
dd29f3ed 3085 )\r
e4ef0031 3086{\r
dd29f3ed 3087 CHAR8 *Ip6Str;\r
e4ef0031 3088 CHAR8 *PrefixStr;\r
3089 CHAR8 *TempStr;\r
3090 EFI_STATUS Status;\r
3091 UINT8 Length;\r
dd29f3ed 3092\r
e4ef0031 3093 if ((String == NULL) || (Ip6Address == NULL) || (PrefixLength == NULL)) {\r
3094 return EFI_INVALID_PARAMETER;\r
3095 }\r
3096\r
3097 Ip6Str = (CHAR8 *) AllocatePool ((StrLen (String) + 1) * sizeof (CHAR8));\r
3098 if (Ip6Str == NULL) {\r
3099 return EFI_OUT_OF_RESOURCES;\r
3100 }\r
3101\r
3102 UnicodeStrToAsciiStr (String, Ip6Str);\r
3103\r
3104 //\r
3105 // Get the sub string describing prefix length.\r
3106 //\r
3107 TempStr = Ip6Str;\r
3108 while (*TempStr != '\0' && (*TempStr != '/')) {\r
3109 TempStr++;\r
3110 }\r
3111\r
3112 if (*TempStr == '/') {\r
3113 PrefixStr = TempStr + 1;\r
3114 } else {\r
3115 PrefixStr = NULL;\r
3116 }\r
3117\r
3118 //\r
3119 // Get the sub string describing IPv6 address and convert it.\r
3120 //\r
3121 *TempStr = '\0';\r
3122\r
3123 Status = NetLibAsciiStrToIp6 (Ip6Str, Ip6Address);\r
3124 if (EFI_ERROR (Status)) {\r
3125 goto Exit;\r
3126 }\r
3127\r
04bb6788 3128 //\r
3129 // If input string doesn't indicate the prefix length, return 0xff.\r
3130 //\r
3131 Length = 0xFF;\r
1dc1b43f 3132\r
e4ef0031 3133 //\r
3134 // Convert the string to prefix length\r
3135 //\r
e4ef0031 3136 if (PrefixStr != NULL) {\r
3137\r
3138 Status = EFI_INVALID_PARAMETER;\r
04bb6788 3139 Length = 0;\r
e4ef0031 3140 while (*PrefixStr != '\0') {\r
3141 if (NET_IS_DIGIT (*PrefixStr)) {\r
3142 Length = (UINT8) (Length * 10 + (*PrefixStr - '0'));\r
3143 if (Length >= IP6_PREFIX_NUM) {\r
3144 goto Exit;\r
3145 }\r
3146 } else {\r
3147 goto Exit;\r
3148 }\r
3149\r
3150 PrefixStr++;\r
3151 }\r
3152 }\r
3153\r
3154 *PrefixLength = Length;\r
3155 Status = EFI_SUCCESS;\r
3156\r
3157Exit:\r
3158\r
3159 FreePool (Ip6Str);\r
3160 return Status;\r
3161}\r
3162\r
57b301b5 3163\r
3164\r
3165/**\r
3166 This function obtains the system guid from the smbios table.\r
3167\r
3168 @param[out] SystemGuid The pointer of the returned system guid.\r
3169\r
3170 @retval EFI_SUCCESS Successfully obtained the system guid.\r
3171 @retval EFI_NOT_FOUND Did not find the SMBIOS table.\r
3172\r
3173**/\r
3174EFI_STATUS\r
3175EFIAPI\r
3176NetLibGetSystemGuid (\r
3177 OUT EFI_GUID *SystemGuid\r
3178 )\r
3179{\r
3180 EFI_STATUS Status;\r
3181 SMBIOS_TABLE_ENTRY_POINT *SmbiosTable;\r
3182 SMBIOS_STRUCTURE_POINTER Smbios;\r
3183 SMBIOS_STRUCTURE_POINTER SmbiosEnd;\r
3184 CHAR8 *String;\r
3185\r
3186 SmbiosTable = NULL;\r
3187 Status = EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **) &SmbiosTable);\r
3188\r
3189 if (EFI_ERROR (Status) || SmbiosTable == NULL) {\r
3190 return EFI_NOT_FOUND;\r
3191 }\r
3192\r
3193 Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) SmbiosTable->TableAddress;\r
3194 SmbiosEnd.Raw = (UINT8 *) (UINTN) (SmbiosTable->TableAddress + SmbiosTable->TableLength);\r
3195\r
3196 do {\r
3197 if (Smbios.Hdr->Type == 1) {\r
3198 if (Smbios.Hdr->Length < 0x19) {\r
3199 //\r
3200 // Older version did not support UUID.\r
3201 //\r
3202 return EFI_NOT_FOUND;\r
3203 }\r
3204 \r
3205 //\r
3206 // SMBIOS tables are byte packed so we need to do a byte copy to\r
3207 // prevend alignment faults on Itanium-based platform.\r
3208 //\r
3209 CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID));\r
3210 return EFI_SUCCESS;\r
3211 }\r
3212\r
3213 //\r
3214 // Go to the next SMBIOS structure. Each SMBIOS structure may include 2 parts:\r
3215 // 1. Formatted section; 2. Unformatted string section. So, 2 steps are needed\r
3216 // to skip one SMBIOS structure.\r
3217 //\r
3218 \r
3219 //\r
3220 // Step 1: Skip over formatted section.\r
3221 //\r
3222 String = (CHAR8 *) (Smbios.Raw + Smbios.Hdr->Length);\r
3223 \r
3224 //\r
3225 // Step 2: Skip over unformated string section.\r
3226 //\r
3227 do {\r
3228 //\r
3229 // Each string is terminated with a NULL(00h) BYTE and the sets of strings\r
3230 // is terminated with an additional NULL(00h) BYTE.\r
3231 //\r
3232 for ( ; *String != 0; String++) {\r
3233 }\r
3234\r
3235 if (*(UINT8*)++String == 0) {\r
3236 //\r
3237 // Pointer to the next SMBIOS structure.\r
3238 //\r
3239 Smbios.Raw = (UINT8 *)++String;\r
3240 break;\r
3241 } \r
3242 } while (TRUE);\r
3243 } while (Smbios.Raw < SmbiosEnd.Raw);\r
3244 return EFI_NOT_FOUND;\r
3245}\r