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