]>
Commit | Line | Data |
---|---|---|
da1d0201 | 1 | /** @file\r |
3e7104c2 | 2 | Network library.\r |
3 | \r | |
ce4106be | 4 | Copyright (c) 2005 - 2009, Intel Corporation.<BR>\r |
da1d0201 | 5 | All rights reserved. This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
da1d0201 | 12 | **/\r |
13 | \r | |
3e7104c2 | 14 | #include <Uefi.h>\r |
da1d0201 | 15 | \r |
752ef5d8 | 16 | #include <Protocol/DriverBinding.h>\r |
da1d0201 | 17 | #include <Protocol/ServiceBinding.h>\r |
18 | #include <Protocol/SimpleNetwork.h>\r | |
63886849 | 19 | #include <Protocol/HiiConfigRouting.h>\r |
3012ce5c | 20 | #include <Protocol/ComponentName.h>\r |
21 | #include <Protocol/ComponentName2.h>\r | |
da1d0201 | 22 | \r |
63886849 | 23 | #include <Guid/NicIp4ConfigNvData.h>\r |
24 | \r | |
da1d0201 | 25 | #include <Library/NetLib.h>\r |
26 | #include <Library/BaseLib.h>\r | |
27 | #include <Library/DebugLib.h>\r | |
28 | #include <Library/BaseMemoryLib.h>\r | |
29 | #include <Library/UefiBootServicesTableLib.h>\r | |
30 | #include <Library/UefiRuntimeServicesTableLib.h>\r | |
da1d0201 | 31 | #include <Library/MemoryAllocationLib.h>\r |
1232b214 | 32 | #include <Library/DevicePathLib.h>\r |
63886849 | 33 | #include <Library/HiiLib.h>\r |
34 | #include <Library/PrintLib.h>\r | |
da1d0201 | 35 | \r |
ac7e320c LG |
36 | GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mNetLibHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r |
37 | \r | |
ce4106be | 38 | #define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE\r |
63886849 | 39 | \r |
da1d0201 | 40 | //\r |
41 | // All the supported IP4 maskes in host byte order.\r | |
42 | //\r | |
2a86ff1c | 43 | IP4_ADDR gIp4AllMasks[IP4_MASK_NUM] = {\r |
da1d0201 | 44 | 0x00000000,\r |
45 | 0x80000000,\r | |
46 | 0xC0000000,\r | |
47 | 0xE0000000,\r | |
48 | 0xF0000000,\r | |
49 | 0xF8000000,\r | |
50 | 0xFC000000,\r | |
51 | 0xFE000000,\r | |
52 | \r | |
53 | 0xFF000000,\r | |
54 | 0xFF800000,\r | |
55 | 0xFFC00000,\r | |
56 | 0xFFE00000,\r | |
57 | 0xFFF00000,\r | |
58 | 0xFFF80000,\r | |
59 | 0xFFFC0000,\r | |
60 | 0xFFFE0000,\r | |
61 | \r | |
62 | 0xFFFF0000,\r | |
63 | 0xFFFF8000,\r | |
64 | 0xFFFFC000,\r | |
65 | 0xFFFFE000,\r | |
66 | 0xFFFFF000,\r | |
67 | 0xFFFFF800,\r | |
68 | 0xFFFFFC00,\r | |
69 | 0xFFFFFE00,\r | |
70 | \r | |
71 | 0xFFFFFF00,\r | |
72 | 0xFFFFFF80,\r | |
73 | 0xFFFFFFC0,\r | |
74 | 0xFFFFFFE0,\r | |
75 | 0xFFFFFFF0,\r | |
76 | 0xFFFFFFF8,\r | |
77 | 0xFFFFFFFC,\r | |
78 | 0xFFFFFFFE,\r | |
79 | 0xFFFFFFFF,\r | |
80 | };\r | |
81 | \r | |
82 | EFI_IPv4_ADDRESS mZeroIp4Addr = {{0, 0, 0, 0}};\r | |
83 | \r | |
da1d0201 | 84 | /**\r |
b9008c87 | 85 | Return the length of the mask. \r |
86 | \r | |
87 | Return the length of the mask, the correct value is from 0 to 32.\r | |
88 | If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.\r | |
da1d0201 | 89 | NetMask is in the host byte order.\r |
90 | \r | |
3e7104c2 | 91 | @param[in] NetMask The netmask to get the length from.\r |
da1d0201 | 92 | \r |
b9008c87 | 93 | @return The length of the netmask, IP4_MASK_NUM if the mask is invalid.\r |
3e7104c2 | 94 | \r |
da1d0201 | 95 | **/\r |
96 | INTN\r | |
7b414b4e | 97 | EFIAPI\r |
da1d0201 | 98 | NetGetMaskLength (\r |
99 | IN IP4_ADDR NetMask\r | |
100 | )\r | |
101 | {\r | |
102 | INTN Index;\r | |
103 | \r | |
104 | for (Index = 0; Index < IP4_MASK_NUM; Index++) {\r | |
2a86ff1c | 105 | if (NetMask == gIp4AllMasks[Index]) {\r |
da1d0201 | 106 | break;\r |
107 | }\r | |
108 | }\r | |
109 | \r | |
110 | return Index;\r | |
111 | }\r | |
112 | \r | |
113 | \r | |
114 | \r | |
115 | /**\r | |
b9008c87 | 116 | Return the class of the IP address, such as class A, B, C.\r |
da1d0201 | 117 | Addr is in host byte order.\r |
b9008c87 | 118 | \r |
119 | The address of class A starts with 0.\r | |
120 | If the address belong to class A, return IP4_ADDR_CLASSA.\r | |
121 | The address of class B starts with 10. \r | |
122 | If the address belong to class B, return IP4_ADDR_CLASSB.\r | |
123 | The address of class C starts with 110. \r | |
124 | If the address belong to class C, return IP4_ADDR_CLASSC.\r | |
125 | The address of class D starts with 1110. \r | |
126 | If the address belong to class D, return IP4_ADDR_CLASSD.\r | |
127 | The address of class E starts with 1111.\r | |
128 | If the address belong to class E, return IP4_ADDR_CLASSE.\r | |
da1d0201 | 129 | \r |
b9008c87 | 130 | \r |
3e7104c2 | 131 | @param[in] Addr The address to get the class from.\r |
da1d0201 | 132 | \r |
3e7104c2 | 133 | @return IP address class, such as IP4_ADDR_CLASSA.\r |
da1d0201 | 134 | \r |
135 | **/\r | |
136 | INTN\r | |
7b414b4e | 137 | EFIAPI\r |
da1d0201 | 138 | NetGetIpClass (\r |
139 | IN IP4_ADDR Addr\r | |
140 | )\r | |
141 | {\r | |
142 | UINT8 ByteOne;\r | |
143 | \r | |
144 | ByteOne = (UINT8) (Addr >> 24);\r | |
145 | \r | |
146 | if ((ByteOne & 0x80) == 0) {\r | |
147 | return IP4_ADDR_CLASSA;\r | |
148 | \r | |
149 | } else if ((ByteOne & 0xC0) == 0x80) {\r | |
150 | return IP4_ADDR_CLASSB;\r | |
151 | \r | |
152 | } else if ((ByteOne & 0xE0) == 0xC0) {\r | |
153 | return IP4_ADDR_CLASSC;\r | |
154 | \r | |
155 | } else if ((ByteOne & 0xF0) == 0xE0) {\r | |
156 | return IP4_ADDR_CLASSD;\r | |
157 | \r | |
158 | } else {\r | |
159 | return IP4_ADDR_CLASSE;\r | |
160 | \r | |
161 | }\r | |
162 | }\r | |
163 | \r | |
164 | \r | |
165 | /**\r | |
166 | Check whether the IP is a valid unicast address according to\r | |
b9008c87 | 167 | the netmask. If NetMask is zero, use the IP address's class to get the default mask.\r |
168 | \r | |
169 | If Ip is 0, IP is not a valid unicast address.\r | |
170 | Class D address is used for multicasting and class E address is reserved for future. If Ip\r | |
171 | belongs to class D or class E, IP is not a valid unicast address. \r | |
172 | If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address.\r | |
da1d0201 | 173 | \r |
3e7104c2 | 174 | @param[in] Ip The IP to check against.\r |
175 | @param[in] NetMask The mask of the IP.\r | |
da1d0201 | 176 | \r |
3e7104c2 | 177 | @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.\r |
da1d0201 | 178 | \r |
179 | **/\r | |
180 | BOOLEAN\r | |
7b414b4e | 181 | EFIAPI\r |
da1d0201 | 182 | Ip4IsUnicast (\r |
183 | IN IP4_ADDR Ip,\r | |
184 | IN IP4_ADDR NetMask\r | |
185 | )\r | |
186 | {\r | |
187 | INTN Class;\r | |
188 | \r | |
189 | Class = NetGetIpClass (Ip);\r | |
190 | \r | |
191 | if ((Ip == 0) || (Class >= IP4_ADDR_CLASSD)) {\r | |
192 | return FALSE;\r | |
193 | }\r | |
194 | \r | |
195 | if (NetMask == 0) {\r | |
2a86ff1c | 196 | NetMask = gIp4AllMasks[Class << 3];\r |
da1d0201 | 197 | }\r |
198 | \r | |
199 | if (((Ip &~NetMask) == ~NetMask) || ((Ip &~NetMask) == 0)) {\r | |
200 | return FALSE;\r | |
201 | }\r | |
202 | \r | |
203 | return TRUE;\r | |
204 | }\r | |
205 | \r | |
fb115c61 | 206 | /**\r |
207 | Check whether the incoming IPv6 address is a valid unicast address.\r | |
208 | \r | |
209 | If the address is a multicast address has binary 0xFF at the start, it is not\r | |
210 | a valid unicast address. If the address is unspecified ::, it is not a valid\r | |
211 | unicast address to be assigned to any node. If the address is loopback address\r | |
212 | ::1, it is also not a valid unicast address to be assigned to any physical\r | |
213 | interface. \r | |
214 | \r | |
215 | @param[in] Ip6 The IPv6 address to check against.\r | |
216 | \r | |
217 | @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE.\r | |
218 | \r | |
219 | **/ \r | |
220 | BOOLEAN\r | |
221 | Ip6IsValidUnicast (\r | |
222 | IN EFI_IPv6_ADDRESS *Ip6\r | |
223 | ) \r | |
224 | {\r | |
225 | UINT8 t;\r | |
226 | UINT8 i;\r | |
227 | \r | |
228 | if (Ip6->Addr[0] == 0xFF) {\r | |
229 | return FALSE;\r | |
230 | }\r | |
231 | \r | |
232 | for (i = 0; i < 15; i++) {\r | |
233 | if (Ip6->Addr[i] != 0) {\r | |
234 | return TRUE;\r | |
235 | }\r | |
236 | }\r | |
237 | \r | |
238 | t = Ip6->Addr[i];\r | |
239 | \r | |
240 | if (t == 0x0 || t == 0x1) {\r | |
241 | return FALSE;\r | |
242 | }\r | |
243 | \r | |
244 | return TRUE; \r | |
245 | }\r | |
da1d0201 | 246 | \r |
247 | /**\r | |
248 | Initialize a random seed using current time.\r | |
b9008c87 | 249 | \r |
250 | Get current time first. Then initialize a random seed based on some basic \r | |
251 | mathematics operation on the hour, day, minute, second, nanosecond and year \r | |
252 | of the current time.\r | |
253 | \r | |
da1d0201 | 254 | @return The random seed initialized with current time.\r |
255 | \r | |
256 | **/\r | |
257 | UINT32\r | |
7b414b4e | 258 | EFIAPI\r |
da1d0201 | 259 | NetRandomInitSeed (\r |
260 | VOID\r | |
261 | )\r | |
262 | {\r | |
263 | EFI_TIME Time;\r | |
264 | UINT32 Seed;\r | |
265 | \r | |
266 | gRT->GetTime (&Time, NULL);\r | |
36ee91ca | 267 | Seed = (~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second);\r |
da1d0201 | 268 | Seed ^= Time.Nanosecond;\r |
269 | Seed ^= Time.Year << 7;\r | |
270 | \r | |
271 | return Seed;\r | |
272 | }\r | |
273 | \r | |
274 | \r | |
275 | /**\r | |
b9008c87 | 276 | Extract a UINT32 from a byte stream.\r |
277 | \r | |
278 | Copy a UINT32 from a byte stream, then converts it from Network \r | |
279 | byte order to host byte order. Use this function to avoid alignment error.\r | |
da1d0201 | 280 | \r |
3e7104c2 | 281 | @param[in] Buf The buffer to extract the UINT32.\r |
da1d0201 | 282 | \r |
283 | @return The UINT32 extracted.\r | |
284 | \r | |
285 | **/\r | |
286 | UINT32\r | |
7b414b4e | 287 | EFIAPI\r |
da1d0201 | 288 | NetGetUint32 (\r |
289 | IN UINT8 *Buf\r | |
290 | )\r | |
291 | {\r | |
292 | UINT32 Value;\r | |
293 | \r | |
e48e37fc | 294 | CopyMem (&Value, Buf, sizeof (UINT32));\r |
da1d0201 | 295 | return NTOHL (Value);\r |
296 | }\r | |
297 | \r | |
298 | \r | |
299 | /**\r | |
b9008c87 | 300 | Put a UINT32 to the byte stream in network byte order. \r |
301 | \r | |
302 | Converts a UINT32 from host byte order to network byte order. Then copy it to the \r | |
303 | byte stream.\r | |
da1d0201 | 304 | \r |
3e7104c2 | 305 | @param[in, out] Buf The buffer to put the UINT32.\r |
306 | @param[in] Data The data to put.\r | |
307 | \r | |
da1d0201 | 308 | **/\r |
309 | VOID\r | |
7b414b4e | 310 | EFIAPI\r |
da1d0201 | 311 | NetPutUint32 (\r |
3e7104c2 | 312 | IN OUT UINT8 *Buf,\r |
313 | IN UINT32 Data\r | |
da1d0201 | 314 | )\r |
315 | {\r | |
316 | Data = HTONL (Data);\r | |
e48e37fc | 317 | CopyMem (Buf, &Data, sizeof (UINT32));\r |
da1d0201 | 318 | }\r |
319 | \r | |
320 | \r | |
321 | /**\r | |
b9008c87 | 322 | Remove the first node entry on the list, and return the removed node entry.\r |
323 | \r | |
324 | Removes the first node Entry from a doubly linked list. It is up to the caller of\r | |
325 | this function to release the memory used by the first node if that is required. On\r | |
326 | exit, the removed node is returned. \r | |
327 | \r | |
328 | If Head is NULL, then ASSERT().\r | |
329 | If Head was not initialized, then ASSERT().\r | |
330 | If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r | |
331 | linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,\r | |
332 | then ASSERT(). \r | |
da1d0201 | 333 | \r |
3e7104c2 | 334 | @param[in, out] Head The list header.\r |
da1d0201 | 335 | \r |
b9008c87 | 336 | @return The first node entry that is removed from the list, NULL if the list is empty.\r |
da1d0201 | 337 | \r |
338 | **/\r | |
e48e37fc | 339 | LIST_ENTRY *\r |
7b414b4e | 340 | EFIAPI\r |
da1d0201 | 341 | NetListRemoveHead (\r |
3e7104c2 | 342 | IN OUT LIST_ENTRY *Head\r |
da1d0201 | 343 | )\r |
344 | {\r | |
e48e37fc | 345 | LIST_ENTRY *First;\r |
da1d0201 | 346 | \r |
347 | ASSERT (Head != NULL);\r | |
348 | \r | |
e48e37fc | 349 | if (IsListEmpty (Head)) {\r |
da1d0201 | 350 | return NULL;\r |
351 | }\r | |
352 | \r | |
353 | First = Head->ForwardLink;\r | |
354 | Head->ForwardLink = First->ForwardLink;\r | |
355 | First->ForwardLink->BackLink = Head;\r | |
356 | \r | |
357 | DEBUG_CODE (\r | |
e48e37fc | 358 | First->ForwardLink = (LIST_ENTRY *) NULL;\r |
359 | First->BackLink = (LIST_ENTRY *) NULL;\r | |
da1d0201 | 360 | );\r |
361 | \r | |
362 | return First;\r | |
363 | }\r | |
364 | \r | |
365 | \r | |
366 | /**\r | |
b9008c87 | 367 | Remove the last node entry on the list and and return the removed node entry.\r |
368 | \r | |
369 | Removes the last node entry from a doubly linked list. It is up to the caller of\r | |
370 | this function to release the memory used by the first node if that is required. On\r | |
371 | exit, the removed node is returned. \r | |
da1d0201 | 372 | \r |
b9008c87 | 373 | If Head is NULL, then ASSERT().\r |
374 | If Head was not initialized, then ASSERT().\r | |
375 | If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r | |
376 | linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,\r | |
377 | then ASSERT(). \r | |
378 | \r | |
3e7104c2 | 379 | @param[in, out] Head The list head.\r |
da1d0201 | 380 | \r |
b9008c87 | 381 | @return The last node entry that is removed from the list, NULL if the list is empty.\r |
da1d0201 | 382 | \r |
383 | **/\r | |
e48e37fc | 384 | LIST_ENTRY *\r |
7b414b4e | 385 | EFIAPI\r |
da1d0201 | 386 | NetListRemoveTail (\r |
3e7104c2 | 387 | IN OUT LIST_ENTRY *Head\r |
da1d0201 | 388 | )\r |
389 | {\r | |
e48e37fc | 390 | LIST_ENTRY *Last;\r |
da1d0201 | 391 | \r |
392 | ASSERT (Head != NULL);\r | |
393 | \r | |
e48e37fc | 394 | if (IsListEmpty (Head)) {\r |
da1d0201 | 395 | return NULL;\r |
396 | }\r | |
397 | \r | |
398 | Last = Head->BackLink;\r | |
399 | Head->BackLink = Last->BackLink;\r | |
400 | Last->BackLink->ForwardLink = Head;\r | |
401 | \r | |
402 | DEBUG_CODE (\r | |
e48e37fc | 403 | Last->ForwardLink = (LIST_ENTRY *) NULL;\r |
404 | Last->BackLink = (LIST_ENTRY *) NULL;\r | |
da1d0201 | 405 | );\r |
406 | \r | |
407 | return Last;\r | |
408 | }\r | |
409 | \r | |
410 | \r | |
411 | /**\r | |
b9008c87 | 412 | Insert a new node entry after a designated node entry of a doubly linked list.\r |
413 | \r | |
414 | Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry\r | |
415 | of the doubly linked list.\r | |
416 | \r | |
3e7104c2 | 417 | @param[in, out] PrevEntry The previous entry to insert after.\r |
418 | @param[in, out] NewEntry The new entry to insert.\r | |
da1d0201 | 419 | \r |
420 | **/\r | |
421 | VOID\r | |
7b414b4e | 422 | EFIAPI\r |
da1d0201 | 423 | NetListInsertAfter (\r |
3e7104c2 | 424 | IN OUT LIST_ENTRY *PrevEntry,\r |
425 | IN OUT LIST_ENTRY *NewEntry\r | |
da1d0201 | 426 | )\r |
427 | {\r | |
428 | NewEntry->BackLink = PrevEntry;\r | |
429 | NewEntry->ForwardLink = PrevEntry->ForwardLink;\r | |
430 | PrevEntry->ForwardLink->BackLink = NewEntry;\r | |
431 | PrevEntry->ForwardLink = NewEntry;\r | |
432 | }\r | |
433 | \r | |
434 | \r | |
435 | /**\r | |
b9008c87 | 436 | Insert a new node entry before a designated node entry of a doubly linked list.\r |
437 | \r | |
438 | Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry\r | |
439 | of the doubly linked list.\r | |
440 | \r | |
3e7104c2 | 441 | @param[in, out] PostEntry The entry to insert before.\r |
442 | @param[in, out] NewEntry The new entry to insert.\r | |
da1d0201 | 443 | \r |
444 | **/\r | |
445 | VOID\r | |
7b414b4e | 446 | EFIAPI\r |
da1d0201 | 447 | NetListInsertBefore (\r |
3e7104c2 | 448 | IN OUT LIST_ENTRY *PostEntry,\r |
449 | IN OUT LIST_ENTRY *NewEntry\r | |
da1d0201 | 450 | )\r |
451 | {\r | |
452 | NewEntry->ForwardLink = PostEntry;\r | |
453 | NewEntry->BackLink = PostEntry->BackLink;\r | |
454 | PostEntry->BackLink->ForwardLink = NewEntry;\r | |
455 | PostEntry->BackLink = NewEntry;\r | |
456 | }\r | |
457 | \r | |
458 | \r | |
459 | /**\r | |
460 | Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.\r | |
b9008c87 | 461 | \r |
462 | Initialize the forward and backward links of two head nodes donated by Map->Used \r | |
463 | and Map->Recycled of two doubly linked lists.\r | |
464 | Initializes the count of the <Key, Value> pairs in the netmap to zero.\r | |
465 | \r | |
466 | If Map is NULL, then ASSERT().\r | |
8f5e6151 | 467 | If the address of Map->Used is NULL, then ASSERT().\r |
b9008c87 | 468 | If the address of Map->Recycled is NULl, then ASSERT().\r |
469 | \r | |
3e7104c2 | 470 | @param[in, out] Map The netmap to initialize.\r |
da1d0201 | 471 | \r |
472 | **/\r | |
473 | VOID\r | |
7b414b4e | 474 | EFIAPI\r |
da1d0201 | 475 | NetMapInit (\r |
3e7104c2 | 476 | IN OUT NET_MAP *Map\r |
da1d0201 | 477 | )\r |
478 | {\r | |
479 | ASSERT (Map != NULL);\r | |
480 | \r | |
e48e37fc | 481 | InitializeListHead (&Map->Used);\r |
482 | InitializeListHead (&Map->Recycled);\r | |
da1d0201 | 483 | Map->Count = 0;\r |
484 | }\r | |
485 | \r | |
486 | \r | |
487 | /**\r | |
488 | To clean up the netmap, that is, release allocated memories.\r | |
b9008c87 | 489 | \r |
490 | Removes all nodes of the Used doubly linked list and free memory of all related netmap items.\r | |
491 | Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.\r | |
492 | The number of the <Key, Value> pairs in the netmap is set to be zero.\r | |
493 | \r | |
494 | If Map is NULL, then ASSERT().\r | |
495 | \r | |
3e7104c2 | 496 | @param[in, out] Map The netmap to clean up.\r |
da1d0201 | 497 | \r |
498 | **/\r | |
499 | VOID\r | |
7b414b4e | 500 | EFIAPI\r |
da1d0201 | 501 | NetMapClean (\r |
3e7104c2 | 502 | IN OUT NET_MAP *Map\r |
da1d0201 | 503 | )\r |
504 | {\r | |
505 | NET_MAP_ITEM *Item;\r | |
e48e37fc | 506 | LIST_ENTRY *Entry;\r |
507 | LIST_ENTRY *Next;\r | |
da1d0201 | 508 | \r |
509 | ASSERT (Map != NULL);\r | |
510 | \r | |
511 | NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Used) {\r | |
512 | Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r | |
513 | \r | |
e48e37fc | 514 | RemoveEntryList (&Item->Link);\r |
da1d0201 | 515 | Map->Count--;\r |
516 | \r | |
e48e37fc | 517 | gBS->FreePool (Item);\r |
da1d0201 | 518 | }\r |
519 | \r | |
e48e37fc | 520 | ASSERT ((Map->Count == 0) && IsListEmpty (&Map->Used));\r |
da1d0201 | 521 | \r |
522 | NET_LIST_FOR_EACH_SAFE (Entry, Next, &Map->Recycled) {\r | |
523 | Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r | |
524 | \r | |
e48e37fc | 525 | RemoveEntryList (&Item->Link);\r |
526 | gBS->FreePool (Item);\r | |
da1d0201 | 527 | }\r |
528 | \r | |
e48e37fc | 529 | ASSERT (IsListEmpty (&Map->Recycled));\r |
da1d0201 | 530 | }\r |
531 | \r | |
532 | \r | |
533 | /**\r | |
b9008c87 | 534 | Test whether the netmap is empty and return true if it is.\r |
535 | \r | |
536 | If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.\r | |
537 | \r | |
538 | If Map is NULL, then ASSERT().\r | |
539 | \r | |
540 | \r | |
3e7104c2 | 541 | @param[in] Map The net map to test.\r |
da1d0201 | 542 | \r |
543 | @return TRUE if the netmap is empty, otherwise FALSE.\r | |
544 | \r | |
545 | **/\r | |
546 | BOOLEAN\r | |
7b414b4e | 547 | EFIAPI\r |
da1d0201 | 548 | NetMapIsEmpty (\r |
549 | IN NET_MAP *Map\r | |
550 | )\r | |
551 | {\r | |
552 | ASSERT (Map != NULL);\r | |
553 | return (BOOLEAN) (Map->Count == 0);\r | |
554 | }\r | |
555 | \r | |
556 | \r | |
557 | /**\r | |
558 | Return the number of the <Key, Value> pairs in the netmap.\r | |
559 | \r | |
3e7104c2 | 560 | @param[in] Map The netmap to get the entry number.\r |
da1d0201 | 561 | \r |
562 | @return The entry number in the netmap.\r | |
563 | \r | |
564 | **/\r | |
565 | UINTN\r | |
7b414b4e | 566 | EFIAPI\r |
da1d0201 | 567 | NetMapGetCount (\r |
568 | IN NET_MAP *Map\r | |
569 | )\r | |
570 | {\r | |
571 | return Map->Count;\r | |
572 | }\r | |
573 | \r | |
574 | \r | |
575 | /**\r | |
b9008c87 | 576 | Return one allocated item. \r |
577 | \r | |
578 | If the Recycled doubly linked list of the netmap is empty, it will try to allocate \r | |
579 | a batch of items if there are enough resources and add corresponding nodes to the begining\r | |
580 | of the Recycled doubly linked list of the netmap. Otherwise, it will directly remove\r | |
581 | the fist node entry of the Recycled doubly linked list and return the corresponding item.\r | |
582 | \r | |
583 | If Map is NULL, then ASSERT().\r | |
584 | \r | |
3e7104c2 | 585 | @param[in, out] Map The netmap to allocate item for.\r |
da1d0201 | 586 | \r |
3e7104c2 | 587 | @return The allocated item. If NULL, the\r |
588 | allocation failed due to resource limit.\r | |
da1d0201 | 589 | \r |
590 | **/\r | |
da1d0201 | 591 | NET_MAP_ITEM *\r |
592 | NetMapAllocItem (\r | |
3e7104c2 | 593 | IN OUT NET_MAP *Map\r |
da1d0201 | 594 | )\r |
595 | {\r | |
596 | NET_MAP_ITEM *Item;\r | |
e48e37fc | 597 | LIST_ENTRY *Head;\r |
da1d0201 | 598 | UINTN Index;\r |
599 | \r | |
600 | ASSERT (Map != NULL);\r | |
601 | \r | |
602 | Head = &Map->Recycled;\r | |
603 | \r | |
e48e37fc | 604 | if (IsListEmpty (Head)) {\r |
da1d0201 | 605 | for (Index = 0; Index < NET_MAP_INCREAMENT; Index++) {\r |
e48e37fc | 606 | Item = AllocatePool (sizeof (NET_MAP_ITEM));\r |
da1d0201 | 607 | \r |
608 | if (Item == NULL) {\r | |
609 | if (Index == 0) {\r | |
610 | return NULL;\r | |
611 | }\r | |
612 | \r | |
613 | break;\r | |
614 | }\r | |
615 | \r | |
e48e37fc | 616 | InsertHeadList (Head, &Item->Link);\r |
da1d0201 | 617 | }\r |
618 | }\r | |
619 | \r | |
620 | Item = NET_LIST_HEAD (Head, NET_MAP_ITEM, Link);\r | |
621 | NetListRemoveHead (Head);\r | |
622 | \r | |
623 | return Item;\r | |
624 | }\r | |
625 | \r | |
626 | \r | |
627 | /**\r | |
628 | Allocate an item to save the <Key, Value> pair to the head of the netmap.\r | |
b9008c87 | 629 | \r |
630 | Allocate an item to save the <Key, Value> pair and add corresponding node entry\r | |
631 | to the beginning of the Used doubly linked list. The number of the <Key, Value> \r | |
632 | pairs in the netmap increase by 1.\r | |
da1d0201 | 633 | \r |
b9008c87 | 634 | If Map is NULL, then ASSERT().\r |
635 | \r | |
3e7104c2 | 636 | @param[in, out] Map The netmap to insert into.\r |
637 | @param[in] Key The user's key.\r | |
638 | @param[in] Value The user's value for the key.\r | |
da1d0201 | 639 | \r |
3e7104c2 | 640 | @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r |
641 | @retval EFI_SUCCESS The item is inserted to the head.\r | |
da1d0201 | 642 | \r |
643 | **/\r | |
644 | EFI_STATUS\r | |
7b414b4e | 645 | EFIAPI\r |
da1d0201 | 646 | NetMapInsertHead (\r |
3e7104c2 | 647 | IN OUT NET_MAP *Map,\r |
da1d0201 | 648 | IN VOID *Key,\r |
649 | IN VOID *Value OPTIONAL\r | |
650 | )\r | |
651 | {\r | |
652 | NET_MAP_ITEM *Item;\r | |
653 | \r | |
654 | ASSERT (Map != NULL);\r | |
655 | \r | |
656 | Item = NetMapAllocItem (Map);\r | |
657 | \r | |
658 | if (Item == NULL) {\r | |
659 | return EFI_OUT_OF_RESOURCES;\r | |
660 | }\r | |
661 | \r | |
662 | Item->Key = Key;\r | |
663 | Item->Value = Value;\r | |
e48e37fc | 664 | InsertHeadList (&Map->Used, &Item->Link);\r |
da1d0201 | 665 | \r |
666 | Map->Count++;\r | |
667 | return EFI_SUCCESS;\r | |
668 | }\r | |
669 | \r | |
670 | \r | |
671 | /**\r | |
672 | Allocate an item to save the <Key, Value> pair to the tail of the netmap.\r | |
673 | \r | |
b9008c87 | 674 | Allocate an item to save the <Key, Value> pair and add corresponding node entry\r |
675 | to the tail of the Used doubly linked list. The number of the <Key, Value> \r | |
676 | pairs in the netmap increase by 1.\r | |
677 | \r | |
678 | If Map is NULL, then ASSERT().\r | |
679 | \r | |
3e7104c2 | 680 | @param[in, out] Map The netmap to insert into.\r |
681 | @param[in] Key The user's key.\r | |
682 | @param[in] Value The user's value for the key.\r | |
da1d0201 | 683 | \r |
3e7104c2 | 684 | @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r |
685 | @retval EFI_SUCCESS The item is inserted to the tail.\r | |
da1d0201 | 686 | \r |
687 | **/\r | |
688 | EFI_STATUS\r | |
7b414b4e | 689 | EFIAPI\r |
da1d0201 | 690 | NetMapInsertTail (\r |
3e7104c2 | 691 | IN OUT NET_MAP *Map,\r |
da1d0201 | 692 | IN VOID *Key,\r |
693 | IN VOID *Value OPTIONAL\r | |
694 | )\r | |
695 | {\r | |
696 | NET_MAP_ITEM *Item;\r | |
697 | \r | |
698 | ASSERT (Map != NULL);\r | |
699 | \r | |
700 | Item = NetMapAllocItem (Map);\r | |
701 | \r | |
702 | if (Item == NULL) {\r | |
703 | return EFI_OUT_OF_RESOURCES;\r | |
704 | }\r | |
705 | \r | |
706 | Item->Key = Key;\r | |
707 | Item->Value = Value;\r | |
e48e37fc | 708 | InsertTailList (&Map->Used, &Item->Link);\r |
da1d0201 | 709 | \r |
710 | Map->Count++;\r | |
711 | \r | |
712 | return EFI_SUCCESS;\r | |
713 | }\r | |
714 | \r | |
715 | \r | |
716 | /**\r | |
b9008c87 | 717 | Check whether the item is in the Map and return TRUE if it is.\r |
da1d0201 | 718 | \r |
3e7104c2 | 719 | @param[in] Map The netmap to search within.\r |
720 | @param[in] Item The item to search.\r | |
da1d0201 | 721 | \r |
722 | @return TRUE if the item is in the netmap, otherwise FALSE.\r | |
723 | \r | |
724 | **/\r | |
da1d0201 | 725 | BOOLEAN\r |
726 | NetItemInMap (\r | |
727 | IN NET_MAP *Map,\r | |
728 | IN NET_MAP_ITEM *Item\r | |
729 | )\r | |
730 | {\r | |
e48e37fc | 731 | LIST_ENTRY *ListEntry;\r |
da1d0201 | 732 | \r |
733 | NET_LIST_FOR_EACH (ListEntry, &Map->Used) {\r | |
734 | if (ListEntry == &Item->Link) {\r | |
735 | return TRUE;\r | |
736 | }\r | |
737 | }\r | |
738 | \r | |
739 | return FALSE;\r | |
740 | }\r | |
741 | \r | |
742 | \r | |
743 | /**\r | |
b9008c87 | 744 | Find the key in the netmap and returns the point to the item contains the Key.\r |
745 | \r | |
746 | Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every \r | |
747 | item with the key to search. It returns the point to the item contains the Key if found.\r | |
da1d0201 | 748 | \r |
b9008c87 | 749 | If Map is NULL, then ASSERT().\r |
750 | \r | |
3e7104c2 | 751 | @param[in] Map The netmap to search within.\r |
752 | @param[in] Key The key to search.\r | |
da1d0201 | 753 | \r |
754 | @return The point to the item contains the Key, or NULL if Key isn't in the map.\r | |
755 | \r | |
756 | **/\r | |
757 | NET_MAP_ITEM *\r | |
7b414b4e | 758 | EFIAPI\r |
da1d0201 | 759 | NetMapFindKey (\r |
760 | IN NET_MAP *Map,\r | |
761 | IN VOID *Key\r | |
762 | )\r | |
763 | {\r | |
e48e37fc | 764 | LIST_ENTRY *Entry;\r |
da1d0201 | 765 | NET_MAP_ITEM *Item;\r |
766 | \r | |
767 | ASSERT (Map != NULL);\r | |
768 | \r | |
769 | NET_LIST_FOR_EACH (Entry, &Map->Used) {\r | |
770 | Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r | |
771 | \r | |
772 | if (Item->Key == Key) {\r | |
773 | return Item;\r | |
774 | }\r | |
775 | }\r | |
776 | \r | |
777 | return NULL;\r | |
778 | }\r | |
779 | \r | |
780 | \r | |
781 | /**\r | |
b9008c87 | 782 | Remove the node entry of the item from the netmap and return the key of the removed item.\r |
783 | \r | |
784 | Remove the node entry of the item from the Used doubly linked list of the netmap. \r | |
785 | The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node \r | |
786 | entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,\r | |
787 | Value will point to the value of the item. It returns the key of the removed item.\r | |
788 | \r | |
789 | If Map is NULL, then ASSERT().\r | |
790 | If Item is NULL, then ASSERT().\r | |
791 | if item in not in the netmap, then ASSERT().\r | |
792 | \r | |
3e7104c2 | 793 | @param[in, out] Map The netmap to remove the item from.\r |
794 | @param[in, out] Item The item to remove.\r | |
795 | @param[out] Value The variable to receive the value if not NULL.\r | |
da1d0201 | 796 | \r |
3e7104c2 | 797 | @return The key of the removed item.\r |
da1d0201 | 798 | \r |
799 | **/\r | |
800 | VOID *\r | |
7b414b4e | 801 | EFIAPI\r |
da1d0201 | 802 | NetMapRemoveItem (\r |
3e7104c2 | 803 | IN OUT NET_MAP *Map,\r |
804 | IN OUT NET_MAP_ITEM *Item,\r | |
805 | OUT VOID **Value OPTIONAL\r | |
da1d0201 | 806 | )\r |
807 | {\r | |
808 | ASSERT ((Map != NULL) && (Item != NULL));\r | |
809 | ASSERT (NetItemInMap (Map, Item));\r | |
810 | \r | |
e48e37fc | 811 | RemoveEntryList (&Item->Link);\r |
da1d0201 | 812 | Map->Count--;\r |
e48e37fc | 813 | InsertHeadList (&Map->Recycled, &Item->Link);\r |
da1d0201 | 814 | \r |
815 | if (Value != NULL) {\r | |
816 | *Value = Item->Value;\r | |
817 | }\r | |
818 | \r | |
819 | return Item->Key;\r | |
820 | }\r | |
821 | \r | |
822 | \r | |
823 | /**\r | |
b9008c87 | 824 | Remove the first node entry on the netmap and return the key of the removed item.\r |
da1d0201 | 825 | \r |
b9008c87 | 826 | Remove the first node entry from the Used doubly linked list of the netmap. \r |
827 | The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node \r | |
828 | entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r | |
829 | parameter Value will point to the value of the item. It returns the key of the removed item.\r | |
830 | \r | |
831 | If Map is NULL, then ASSERT().\r | |
832 | If the Used doubly linked list is empty, then ASSERT().\r | |
833 | \r | |
3e7104c2 | 834 | @param[in, out] Map The netmap to remove the head from.\r |
835 | @param[out] Value The variable to receive the value if not NULL.\r | |
da1d0201 | 836 | \r |
3e7104c2 | 837 | @return The key of the item removed.\r |
da1d0201 | 838 | \r |
839 | **/\r | |
840 | VOID *\r | |
7b414b4e | 841 | EFIAPI\r |
da1d0201 | 842 | NetMapRemoveHead (\r |
3e7104c2 | 843 | IN OUT NET_MAP *Map,\r |
da1d0201 | 844 | OUT VOID **Value OPTIONAL\r |
845 | )\r | |
846 | {\r | |
847 | NET_MAP_ITEM *Item;\r | |
848 | \r | |
849 | //\r | |
850 | // Often, it indicates a programming error to remove\r | |
851 | // the first entry in an empty list\r | |
852 | //\r | |
e48e37fc | 853 | ASSERT (Map && !IsListEmpty (&Map->Used));\r |
da1d0201 | 854 | \r |
855 | Item = NET_LIST_HEAD (&Map->Used, NET_MAP_ITEM, Link);\r | |
e48e37fc | 856 | RemoveEntryList (&Item->Link);\r |
da1d0201 | 857 | Map->Count--;\r |
e48e37fc | 858 | InsertHeadList (&Map->Recycled, &Item->Link);\r |
da1d0201 | 859 | \r |
860 | if (Value != NULL) {\r | |
861 | *Value = Item->Value;\r | |
862 | }\r | |
863 | \r | |
864 | return Item->Key;\r | |
865 | }\r | |
866 | \r | |
867 | \r | |
868 | /**\r | |
b9008c87 | 869 | Remove the last node entry on the netmap and return the key of the removed item.\r |
da1d0201 | 870 | \r |
b9008c87 | 871 | Remove the last node entry from the Used doubly linked list of the netmap. \r |
872 | The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node \r | |
873 | entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r | |
874 | parameter Value will point to the value of the item. It returns the key of the removed item.\r | |
875 | \r | |
876 | If Map is NULL, then ASSERT().\r | |
877 | If the Used doubly linked list is empty, then ASSERT().\r | |
878 | \r | |
3e7104c2 | 879 | @param[in, out] Map The netmap to remove the tail from.\r |
880 | @param[out] Value The variable to receive the value if not NULL.\r | |
da1d0201 | 881 | \r |
3e7104c2 | 882 | @return The key of the item removed.\r |
da1d0201 | 883 | \r |
884 | **/\r | |
885 | VOID *\r | |
7b414b4e | 886 | EFIAPI\r |
da1d0201 | 887 | NetMapRemoveTail (\r |
3e7104c2 | 888 | IN OUT NET_MAP *Map,\r |
da1d0201 | 889 | OUT VOID **Value OPTIONAL\r |
890 | )\r | |
891 | {\r | |
892 | NET_MAP_ITEM *Item;\r | |
893 | \r | |
894 | //\r | |
895 | // Often, it indicates a programming error to remove\r | |
896 | // the last entry in an empty list\r | |
897 | //\r | |
e48e37fc | 898 | ASSERT (Map && !IsListEmpty (&Map->Used));\r |
da1d0201 | 899 | \r |
900 | Item = NET_LIST_TAIL (&Map->Used, NET_MAP_ITEM, Link);\r | |
e48e37fc | 901 | RemoveEntryList (&Item->Link);\r |
da1d0201 | 902 | Map->Count--;\r |
e48e37fc | 903 | InsertHeadList (&Map->Recycled, &Item->Link);\r |
da1d0201 | 904 | \r |
905 | if (Value != NULL) {\r | |
906 | *Value = Item->Value;\r | |
907 | }\r | |
908 | \r | |
909 | return Item->Key;\r | |
910 | }\r | |
911 | \r | |
912 | \r | |
913 | /**\r | |
b9008c87 | 914 | Iterate through the netmap and call CallBack for each item.\r |
915 | \r | |
916 | It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break\r | |
917 | from the loop. It returns the CallBack's last return value. This function is \r | |
918 | delete safe for the current item.\r | |
da1d0201 | 919 | \r |
b9008c87 | 920 | If Map is NULL, then ASSERT().\r |
921 | If CallBack is NULL, then ASSERT().\r | |
922 | \r | |
3e7104c2 | 923 | @param[in] Map The Map to iterate through.\r |
924 | @param[in] CallBack The callback function to call for each item.\r | |
925 | @param[in] Arg The opaque parameter to the callback.\r | |
da1d0201 | 926 | \r |
3e7104c2 | 927 | @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item\r |
928 | return EFI_SUCCESS.\r | |
929 | @retval Others It returns the CallBack's last return value.\r | |
da1d0201 | 930 | \r |
931 | **/\r | |
932 | EFI_STATUS\r | |
7b414b4e | 933 | EFIAPI\r |
da1d0201 | 934 | NetMapIterate (\r |
935 | IN NET_MAP *Map,\r | |
936 | IN NET_MAP_CALLBACK CallBack,\r | |
937 | IN VOID *Arg\r | |
938 | )\r | |
939 | {\r | |
940 | \r | |
e48e37fc | 941 | LIST_ENTRY *Entry;\r |
942 | LIST_ENTRY *Next;\r | |
943 | LIST_ENTRY *Head;\r | |
b9008c87 | 944 | NET_MAP_ITEM *Item;\r |
945 | EFI_STATUS Result;\r | |
da1d0201 | 946 | \r |
947 | ASSERT ((Map != NULL) && (CallBack != NULL));\r | |
948 | \r | |
949 | Head = &Map->Used;\r | |
950 | \r | |
e48e37fc | 951 | if (IsListEmpty (Head)) {\r |
da1d0201 | 952 | return EFI_SUCCESS;\r |
953 | }\r | |
954 | \r | |
955 | NET_LIST_FOR_EACH_SAFE (Entry, Next, Head) {\r | |
956 | Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r | |
957 | Result = CallBack (Map, Item, Arg);\r | |
958 | \r | |
959 | if (EFI_ERROR (Result)) {\r | |
960 | return Result;\r | |
961 | }\r | |
962 | }\r | |
963 | \r | |
964 | return EFI_SUCCESS;\r | |
965 | }\r | |
966 | \r | |
967 | \r | |
968 | /**\r | |
969 | This is the default unload handle for all the network drivers.\r | |
970 | \r | |
b9008c87 | 971 | Disconnect the driver specified by ImageHandle from all the devices in the handle database.\r |
972 | Uninstall all the protocols installed in the driver entry point.\r | |
973 | \r | |
3e7104c2 | 974 | @param[in] ImageHandle The drivers' driver image.\r |
da1d0201 | 975 | \r |
976 | @retval EFI_SUCCESS The image is unloaded.\r | |
977 | @retval Others Failed to unload the image.\r | |
978 | \r | |
979 | **/\r | |
980 | EFI_STATUS\r | |
981 | EFIAPI\r | |
982 | NetLibDefaultUnload (\r | |
983 | IN EFI_HANDLE ImageHandle\r | |
984 | )\r | |
985 | {\r | |
986 | EFI_STATUS Status;\r | |
987 | EFI_HANDLE *DeviceHandleBuffer;\r | |
988 | UINTN DeviceHandleCount;\r | |
989 | UINTN Index;\r | |
990 | EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;\r | |
991 | EFI_COMPONENT_NAME_PROTOCOL *ComponentName;\r | |
3012ce5c | 992 | EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r |
da1d0201 | 993 | \r |
994 | //\r | |
995 | // Get the list of all the handles in the handle database.\r | |
996 | // If there is an error getting the list, then the unload\r | |
997 | // operation fails.\r | |
998 | //\r | |
999 | Status = gBS->LocateHandleBuffer (\r | |
1000 | AllHandles,\r | |
1001 | NULL,\r | |
1002 | NULL,\r | |
1003 | &DeviceHandleCount,\r | |
1004 | &DeviceHandleBuffer\r | |
1005 | );\r | |
1006 | \r | |
1007 | if (EFI_ERROR (Status)) {\r | |
1008 | return Status;\r | |
1009 | }\r | |
1010 | \r | |
1011 | //\r | |
1012 | // Disconnect the driver specified by ImageHandle from all\r | |
1013 | // the devices in the handle database.\r | |
1014 | //\r | |
1015 | for (Index = 0; Index < DeviceHandleCount; Index++) {\r | |
1016 | Status = gBS->DisconnectController (\r | |
1017 | DeviceHandleBuffer[Index],\r | |
1018 | ImageHandle,\r | |
1019 | NULL\r | |
1020 | );\r | |
1021 | }\r | |
1022 | \r | |
1023 | //\r | |
1024 | // Uninstall all the protocols installed in the driver entry point\r | |
1025 | //\r | |
1026 | for (Index = 0; Index < DeviceHandleCount; Index++) {\r | |
1027 | Status = gBS->HandleProtocol (\r | |
1028 | DeviceHandleBuffer[Index],\r | |
1029 | &gEfiDriverBindingProtocolGuid,\r | |
1030 | (VOID **) &DriverBinding\r | |
1031 | );\r | |
1032 | \r | |
1033 | if (EFI_ERROR (Status)) {\r | |
1034 | continue;\r | |
1035 | }\r | |
1036 | \r | |
1037 | if (DriverBinding->ImageHandle != ImageHandle) {\r | |
1038 | continue;\r | |
1039 | }\r | |
1040 | \r | |
1041 | gBS->UninstallProtocolInterface (\r | |
1042 | ImageHandle,\r | |
1043 | &gEfiDriverBindingProtocolGuid,\r | |
1044 | DriverBinding\r | |
1045 | );\r | |
1046 | Status = gBS->HandleProtocol (\r | |
1047 | DeviceHandleBuffer[Index],\r | |
1048 | &gEfiComponentNameProtocolGuid,\r | |
1049 | (VOID **) &ComponentName\r | |
1050 | );\r | |
1051 | if (!EFI_ERROR (Status)) {\r | |
1052 | gBS->UninstallProtocolInterface (\r | |
1053 | ImageHandle,\r | |
1054 | &gEfiComponentNameProtocolGuid,\r | |
1055 | ComponentName\r | |
1056 | );\r | |
1057 | }\r | |
1058 | \r | |
1059 | Status = gBS->HandleProtocol (\r | |
1060 | DeviceHandleBuffer[Index],\r | |
3012ce5c | 1061 | &gEfiComponentName2ProtocolGuid,\r |
1062 | (VOID **) &ComponentName2\r | |
da1d0201 | 1063 | );\r |
da1d0201 | 1064 | if (!EFI_ERROR (Status)) {\r |
1065 | gBS->UninstallProtocolInterface (\r | |
3012ce5c | 1066 | ImageHandle,\r |
1067 | &gEfiComponentName2ProtocolGuid,\r | |
1068 | ComponentName2\r | |
1069 | );\r | |
da1d0201 | 1070 | }\r |
1071 | }\r | |
1072 | \r | |
1073 | //\r | |
1074 | // Free the buffer containing the list of handles from the handle database\r | |
1075 | //\r | |
1076 | if (DeviceHandleBuffer != NULL) {\r | |
1077 | gBS->FreePool (DeviceHandleBuffer);\r | |
1078 | }\r | |
1079 | \r | |
1080 | return EFI_SUCCESS;\r | |
1081 | }\r | |
1082 | \r | |
1083 | \r | |
1084 | \r | |
1085 | /**\r | |
1086 | Create a child of the service that is identified by ServiceBindingGuid.\r | |
b9008c87 | 1087 | \r |
1088 | Get the ServiceBinding Protocol first, then use it to create a child.\r | |
da1d0201 | 1089 | \r |
b9008c87 | 1090 | If ServiceBindingGuid is NULL, then ASSERT().\r |
1091 | If ChildHandle is NULL, then ASSERT().\r | |
1092 | \r | |
3e7104c2 | 1093 | @param[in] Controller The controller which has the service installed.\r |
1094 | @param[in] Image The image handle used to open service.\r | |
1095 | @param[in] ServiceBindingGuid The service's Guid.\r | |
8f5e6151 | 1096 | @param[in, out] ChildHandle The handle to receive the create child.\r |
da1d0201 | 1097 | \r |
1098 | @retval EFI_SUCCESS The child is successfully created.\r | |
1099 | @retval Others Failed to create the child.\r | |
1100 | \r | |
1101 | **/\r | |
1102 | EFI_STATUS\r | |
7b414b4e | 1103 | EFIAPI\r |
da1d0201 | 1104 | NetLibCreateServiceChild (\r |
1105 | IN EFI_HANDLE Controller,\r | |
1106 | IN EFI_HANDLE Image,\r | |
1107 | IN EFI_GUID *ServiceBindingGuid,\r | |
3e7104c2 | 1108 | IN OUT EFI_HANDLE *ChildHandle\r |
da1d0201 | 1109 | )\r |
1110 | {\r | |
1111 | EFI_STATUS Status;\r | |
1112 | EFI_SERVICE_BINDING_PROTOCOL *Service;\r | |
1113 | \r | |
1114 | \r | |
1115 | ASSERT ((ServiceBindingGuid != NULL) && (ChildHandle != NULL));\r | |
1116 | \r | |
1117 | //\r | |
1118 | // Get the ServiceBinding Protocol\r | |
1119 | //\r | |
1120 | Status = gBS->OpenProtocol (\r | |
1121 | Controller,\r | |
1122 | ServiceBindingGuid,\r | |
1123 | (VOID **) &Service,\r | |
1124 | Image,\r | |
1125 | Controller,\r | |
1126 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
1127 | );\r | |
1128 | \r | |
1129 | if (EFI_ERROR (Status)) {\r | |
1130 | return Status;\r | |
1131 | }\r | |
1132 | \r | |
1133 | //\r | |
1134 | // Create a child\r | |
1135 | //\r | |
1136 | Status = Service->CreateChild (Service, ChildHandle);\r | |
1137 | return Status;\r | |
1138 | }\r | |
1139 | \r | |
1140 | \r | |
1141 | /**\r | |
1142 | Destory a child of the service that is identified by ServiceBindingGuid.\r | |
b9008c87 | 1143 | \r |
1144 | Get the ServiceBinding Protocol first, then use it to destroy a child.\r | |
1145 | \r | |
1146 | If ServiceBindingGuid is NULL, then ASSERT().\r | |
1147 | \r | |
3e7104c2 | 1148 | @param[in] Controller The controller which has the service installed.\r |
1149 | @param[in] Image The image handle used to open service.\r | |
1150 | @param[in] ServiceBindingGuid The service's Guid.\r | |
8f5e6151 | 1151 | @param[in] ChildHandle The child to destory.\r |
da1d0201 | 1152 | \r |
1153 | @retval EFI_SUCCESS The child is successfully destoried.\r | |
1154 | @retval Others Failed to destory the child.\r | |
1155 | \r | |
1156 | **/\r | |
1157 | EFI_STATUS\r | |
7b414b4e | 1158 | EFIAPI\r |
da1d0201 | 1159 | NetLibDestroyServiceChild (\r |
1160 | IN EFI_HANDLE Controller,\r | |
1161 | IN EFI_HANDLE Image,\r | |
1162 | IN EFI_GUID *ServiceBindingGuid,\r | |
1163 | IN EFI_HANDLE ChildHandle\r | |
1164 | )\r | |
1165 | {\r | |
1166 | EFI_STATUS Status;\r | |
1167 | EFI_SERVICE_BINDING_PROTOCOL *Service;\r | |
1168 | \r | |
1169 | ASSERT (ServiceBindingGuid != NULL);\r | |
1170 | \r | |
1171 | //\r | |
1172 | // Get the ServiceBinding Protocol\r | |
1173 | //\r | |
1174 | Status = gBS->OpenProtocol (\r | |
1175 | Controller,\r | |
1176 | ServiceBindingGuid,\r | |
1177 | (VOID **) &Service,\r | |
1178 | Image,\r | |
1179 | Controller,\r | |
1180 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
1181 | );\r | |
1182 | \r | |
1183 | if (EFI_ERROR (Status)) {\r | |
1184 | return Status;\r | |
1185 | }\r | |
1186 | \r | |
1187 | //\r | |
1188 | // destory the child\r | |
1189 | //\r | |
1190 | Status = Service->DestroyChild (Service, ChildHandle);\r | |
1191 | return Status;\r | |
1192 | }\r | |
1193 | \r | |
1194 | \r | |
1195 | /**\r | |
1196 | Convert the mac address of the simple network protocol installed on\r | |
1197 | SnpHandle to a unicode string. Callers are responsible for freeing the\r | |
1198 | string storage.\r | |
1199 | \r | |
b9008c87 | 1200 | Get the mac address of the Simple Network protocol from the SnpHandle. Then convert\r |
1201 | the mac address into a unicode string. It takes 2 unicode characters to represent \r | |
1202 | a 1 byte binary buffer. Plus one unicode character for the null-terminator.\r | |
1203 | \r | |
1204 | \r | |
3e7104c2 | 1205 | @param[in] SnpHandle The handle where the simple network protocol is\r |
1206 | installed on.\r | |
1207 | @param[in] ImageHandle The image handle used to act as the agent handle to\r | |
1208 | get the simple network protocol.\r | |
1209 | @param[out] MacString The pointer to store the address of the string\r | |
1210 | representation of the mac address.\r | |
1211 | \r | |
1212 | @retval EFI_SUCCESS Convert the mac address a unicode string successfully.\r | |
da1d0201 | 1213 | @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.\r |
3e7104c2 | 1214 | @retval Others Failed to open the simple network protocol.\r |
da1d0201 | 1215 | \r |
1216 | **/\r | |
1217 | EFI_STATUS\r | |
7b414b4e | 1218 | EFIAPI\r |
da1d0201 | 1219 | NetLibGetMacString (\r |
3e7104c2 | 1220 | IN EFI_HANDLE SnpHandle,\r |
1221 | IN EFI_HANDLE ImageHandle,\r | |
1222 | OUT CHAR16 **MacString\r | |
da1d0201 | 1223 | )\r |
1224 | {\r | |
1225 | EFI_STATUS Status;\r | |
1226 | EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r | |
1227 | EFI_SIMPLE_NETWORK_MODE *Mode;\r | |
1228 | CHAR16 *MacAddress;\r | |
1229 | UINTN Index;\r | |
1230 | \r | |
1231 | *MacString = NULL;\r | |
1232 | \r | |
1233 | //\r | |
1234 | // Get the Simple Network protocol from the SnpHandle.\r | |
1235 | //\r | |
1236 | Status = gBS->OpenProtocol (\r | |
1237 | SnpHandle,\r | |
1238 | &gEfiSimpleNetworkProtocolGuid,\r | |
1239 | (VOID **) &Snp,\r | |
1240 | ImageHandle,\r | |
1241 | SnpHandle,\r | |
1242 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
1243 | );\r | |
1244 | if (EFI_ERROR (Status)) {\r | |
1245 | return Status;\r | |
1246 | }\r | |
1247 | \r | |
1248 | Mode = Snp->Mode;\r | |
1249 | \r | |
1250 | //\r | |
1251 | // It takes 2 unicode characters to represent a 1 byte binary buffer.\r | |
1252 | // Plus one unicode character for the null-terminator.\r | |
1253 | //\r | |
e48e37fc | 1254 | MacAddress = AllocatePool ((2 * Mode->HwAddressSize + 1) * sizeof (CHAR16));\r |
da1d0201 | 1255 | if (MacAddress == NULL) {\r |
1256 | return EFI_OUT_OF_RESOURCES;\r | |
1257 | }\r | |
1258 | \r | |
1259 | //\r | |
1260 | // Convert the mac address into a unicode string.\r | |
1261 | //\r | |
1262 | for (Index = 0; Index < Mode->HwAddressSize; Index++) {\r | |
ac7e320c LG |
1263 | MacAddress[Index * 2] = (CHAR16) mNetLibHexStr[(Mode->CurrentAddress.Addr[Index] >> 4) & 0x0F];\r |
1264 | MacAddress[Index * 2 + 1] = (CHAR16) mNetLibHexStr[Mode->CurrentAddress.Addr[Index] & 0x0F];\r | |
da1d0201 | 1265 | }\r |
1266 | \r | |
1267 | MacAddress[Mode->HwAddressSize * 2] = L'\0';\r | |
1268 | \r | |
1269 | *MacString = MacAddress;\r | |
1270 | \r | |
1271 | return EFI_SUCCESS;\r | |
1272 | }\r | |
1273 | \r | |
1274 | /**\r | |
1275 | Check the default address used by the IPv4 driver is static or dynamic (acquired\r | |
1276 | from DHCP).\r | |
1277 | \r | |
b9008c87 | 1278 | If the controller handle does not have the NIC Ip4 Config Protocol installed, the \r |
1279 | default address is static. If the EFI variable to save the configuration is not found,\r | |
1280 | the default address is static. Otherwise, get the result from the EFI variable which \r | |
1281 | saving the configuration.\r | |
1282 | \r | |
3e7104c2 | 1283 | @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol\r |
1284 | relative with the default address to judge.\r | |
da1d0201 | 1285 | \r |
1286 | @retval TRUE If the default address is static.\r | |
1287 | @retval FALSE If the default address is acquired from DHCP.\r | |
1288 | \r | |
1289 | **/\r | |
da1d0201 | 1290 | BOOLEAN\r |
1291 | NetLibDefaultAddressIsStatic (\r | |
1292 | IN EFI_HANDLE Controller\r | |
1293 | )\r | |
1294 | {\r | |
63886849 | 1295 | EFI_STATUS Status;\r |
1296 | EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;\r | |
1297 | UINTN Len;\r | |
1298 | NIC_IP4_CONFIG_INFO *ConfigInfo;\r | |
1299 | BOOLEAN IsStatic;\r | |
1300 | EFI_STRING ConfigHdr;\r | |
1301 | EFI_STRING ConfigResp;\r | |
1302 | EFI_STRING AccessProgress;\r | |
1303 | EFI_STRING AccessResults;\r | |
1304 | EFI_STRING String;\r | |
1305 | \r | |
1306 | ConfigInfo = NULL;\r | |
1307 | ConfigHdr = NULL;\r | |
1308 | ConfigResp = NULL;\r | |
1309 | AccessProgress = NULL;\r | |
1310 | AccessResults = NULL;\r | |
1311 | IsStatic = TRUE;\r | |
1312 | \r | |
1313 | Status = gBS->LocateProtocol (\r | |
1314 | &gEfiHiiConfigRoutingProtocolGuid,\r | |
1315 | NULL,\r | |
1316 | (VOID **) &HiiConfigRouting\r | |
1317 | );\r | |
da1d0201 | 1318 | if (EFI_ERROR (Status)) {\r |
1319 | return TRUE;\r | |
1320 | }\r | |
1321 | \r | |
63886849 | 1322 | //\r |
1323 | // Construct config request string header\r | |
1324 | //\r | |
1325 | ConfigHdr = HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid, EFI_NIC_IP4_CONFIG_VARIABLE, Controller);\r | |
894d038a | 1326 | if (ConfigHdr == NULL) {\r |
1327 | return TRUE;\r | |
1328 | }\r | |
63886849 | 1329 | \r |
1330 | Len = StrLen (ConfigHdr);\r | |
ce4106be | 1331 | ConfigResp = AllocateZeroPool ((Len + NIC_ITEM_CONFIG_SIZE * 2 + 100) * sizeof (CHAR16));\r |
63886849 | 1332 | if (ConfigResp == NULL) {\r |
1333 | goto ON_EXIT;\r | |
1334 | }\r | |
1335 | StrCpy (ConfigResp, ConfigHdr);\r | |
1336 | \r | |
1337 | String = ConfigResp + Len;\r | |
1338 | UnicodeSPrint (\r | |
1339 | String, \r | |
ce4106be | 1340 | (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16), \r |
63886849 | 1341 | L"&OFFSET=%04X&WIDTH=%04X", \r |
1342 | OFFSET_OF (NIC_IP4_CONFIG_INFO, Source), \r | |
1343 | sizeof (UINT32)\r | |
1344 | );\r | |
1345 | \r | |
1346 | Status = HiiConfigRouting->ExtractConfig (\r | |
1347 | HiiConfigRouting,\r | |
1348 | ConfigResp,\r | |
1349 | &AccessProgress,\r | |
1350 | &AccessResults\r | |
1351 | );\r | |
1352 | if (EFI_ERROR (Status)) {\r | |
1353 | goto ON_EXIT;\r | |
da1d0201 | 1354 | }\r |
1355 | \r | |
ce4106be | 1356 | ConfigInfo = AllocateZeroPool (sizeof (NIC_ITEM_CONFIG_SIZE));\r |
da1d0201 | 1357 | if (ConfigInfo == NULL) {\r |
63886849 | 1358 | goto ON_EXIT;\r |
da1d0201 | 1359 | }\r |
1360 | \r | |
63886849 | 1361 | ConfigInfo->Source = IP4_CONFIG_SOURCE_STATIC;\r |
1362 | Len = NIC_ITEM_CONFIG_SIZE;\r | |
1363 | Status = HiiConfigRouting->ConfigToBlock (\r | |
1364 | HiiConfigRouting,\r | |
1365 | AccessResults,\r | |
1366 | (UINT8 *) ConfigInfo,\r | |
1367 | &Len,\r | |
1368 | &AccessProgress\r | |
1369 | );\r | |
da1d0201 | 1370 | if (EFI_ERROR (Status)) {\r |
1371 | goto ON_EXIT;\r | |
1372 | }\r | |
1373 | \r | |
1374 | IsStatic = (BOOLEAN) (ConfigInfo->Source == IP4_CONFIG_SOURCE_STATIC);\r | |
63886849 | 1375 | \r |
da1d0201 | 1376 | ON_EXIT:\r |
1377 | \r | |
63886849 | 1378 | if (AccessResults != NULL) {\r |
1379 | FreePool (AccessResults);\r | |
1380 | }\r | |
1381 | if (ConfigInfo != NULL) {\r | |
1382 | FreePool (ConfigInfo);\r | |
1383 | }\r | |
1384 | if (ConfigResp != NULL) {\r | |
1385 | FreePool (ConfigResp);\r | |
1386 | }\r | |
1387 | if (ConfigHdr != NULL) {\r | |
1388 | FreePool (ConfigHdr);\r | |
1389 | }\r | |
da1d0201 | 1390 | \r |
1391 | return IsStatic;\r | |
1392 | }\r | |
1393 | \r | |
1394 | /**\r | |
1395 | Create an IPv4 device path node.\r | |
b9008c87 | 1396 | \r |
1397 | The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.\r | |
1398 | The header subtype of IPv4 device path node is MSG_IPv4_DP.\r | |
1399 | The length of the IPv4 device path node in bytes is 19.\r | |
1400 | Get other info from parameters to make up the whole IPv4 device path node.\r | |
da1d0201 | 1401 | \r |
3e7104c2 | 1402 | @param[in, out] Node Pointer to the IPv4 device path node.\r |
1403 | @param[in] Controller The handle where the NIC IP4 config protocol resides.\r | |
1404 | @param[in] LocalIp The local IPv4 address.\r | |
1405 | @param[in] LocalPort The local port.\r | |
1406 | @param[in] RemoteIp The remote IPv4 address.\r | |
1407 | @param[in] RemotePort The remote port.\r | |
1408 | @param[in] Protocol The protocol type in the IP header.\r | |
1409 | @param[in] UseDefaultAddress Whether this instance is using default address or not.\r | |
da1d0201 | 1410 | \r |
da1d0201 | 1411 | **/\r |
1412 | VOID\r | |
7b414b4e | 1413 | EFIAPI\r |
da1d0201 | 1414 | NetLibCreateIPv4DPathNode (\r |
1415 | IN OUT IPv4_DEVICE_PATH *Node,\r | |
1416 | IN EFI_HANDLE Controller,\r | |
1417 | IN IP4_ADDR LocalIp,\r | |
1418 | IN UINT16 LocalPort,\r | |
1419 | IN IP4_ADDR RemoteIp,\r | |
1420 | IN UINT16 RemotePort,\r | |
1421 | IN UINT16 Protocol,\r | |
1422 | IN BOOLEAN UseDefaultAddress\r | |
1423 | )\r | |
1424 | {\r | |
1425 | Node->Header.Type = MESSAGING_DEVICE_PATH;\r | |
1426 | Node->Header.SubType = MSG_IPv4_DP;\r | |
1427 | SetDevicePathNodeLength (&Node->Header, 19);\r | |
1428 | \r | |
e48e37fc | 1429 | CopyMem (&Node->LocalIpAddress, &LocalIp, sizeof (EFI_IPv4_ADDRESS));\r |
1430 | CopyMem (&Node->RemoteIpAddress, &RemoteIp, sizeof (EFI_IPv4_ADDRESS));\r | |
da1d0201 | 1431 | \r |
1432 | Node->LocalPort = LocalPort;\r | |
1433 | Node->RemotePort = RemotePort;\r | |
1434 | \r | |
1435 | Node->Protocol = Protocol;\r | |
1436 | \r | |
1437 | if (!UseDefaultAddress) {\r | |
1438 | Node->StaticIpAddress = TRUE;\r | |
1439 | } else {\r | |
1440 | Node->StaticIpAddress = NetLibDefaultAddressIsStatic (Controller);\r | |
1441 | }\r | |
1442 | }\r | |
1443 | \r | |
1444 | \r | |
1445 | /**\r | |
1446 | Find the UNDI/SNP handle from controller and protocol GUID.\r | |
b9008c87 | 1447 | \r |
da1d0201 | 1448 | For example, IP will open a MNP child to transmit/receive\r |
1449 | packets, when MNP is stopped, IP should also be stopped. IP\r | |
1450 | needs to find its own private data which is related the IP's\r | |
1451 | service binding instance that is install on UNDI/SNP handle.\r | |
1452 | Now, the controller is either a MNP or ARP child handle. But\r | |
1453 | IP opens these handle BY_DRIVER, use that info, we can get the\r | |
1454 | UNDI/SNP handle.\r | |
1455 | \r | |
3e7104c2 | 1456 | @param[in] Controller Then protocol handle to check.\r |
1457 | @param[in] ProtocolGuid The protocol that is related with the handle.\r | |
da1d0201 | 1458 | \r |
3e7104c2 | 1459 | @return The UNDI/SNP handle or NULL for errors.\r |
da1d0201 | 1460 | \r |
1461 | **/\r | |
1462 | EFI_HANDLE\r | |
7b414b4e | 1463 | EFIAPI\r |
da1d0201 | 1464 | NetLibGetNicHandle (\r |
1465 | IN EFI_HANDLE Controller,\r | |
1466 | IN EFI_GUID *ProtocolGuid\r | |
1467 | )\r | |
1468 | {\r | |
1469 | EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenBuffer;\r | |
1470 | EFI_HANDLE Handle;\r | |
1471 | EFI_STATUS Status;\r | |
1472 | UINTN OpenCount;\r | |
1473 | UINTN Index;\r | |
1474 | \r | |
1475 | Status = gBS->OpenProtocolInformation (\r | |
1476 | Controller,\r | |
1477 | ProtocolGuid,\r | |
1478 | &OpenBuffer,\r | |
1479 | &OpenCount\r | |
1480 | );\r | |
1481 | \r | |
1482 | if (EFI_ERROR (Status)) {\r | |
1483 | return NULL;\r | |
1484 | }\r | |
1485 | \r | |
1486 | Handle = NULL;\r | |
1487 | \r | |
1488 | for (Index = 0; Index < OpenCount; Index++) {\r | |
1489 | if (OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {\r | |
1490 | Handle = OpenBuffer[Index].ControllerHandle;\r | |
1491 | break;\r | |
1492 | }\r | |
1493 | }\r | |
1494 | \r | |
1495 | gBS->FreePool (OpenBuffer);\r | |
1496 | return Handle;\r | |
1497 | }\r |