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