]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Driver.c
1. Fix a bug in PXE driver that the PXE boot do not restart if a new boot option...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Driver.c
... / ...
CommitLineData
1/** @file\r
2 The driver binding and service binding protocol for IP4 driver.\r
3\r
4Copyright (c) 2005 - 2012, Intel Corporation. All rights reserved.<BR>\r
5This 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
12\r
13**/\r
14\r
15#include "Ip4Impl.h"\r
16\r
17EFI_DRIVER_BINDING_PROTOCOL gIp4DriverBinding = {\r
18 Ip4DriverBindingSupported,\r
19 Ip4DriverBindingStart,\r
20 Ip4DriverBindingStop,\r
21 0xa,\r
22 NULL,\r
23 NULL\r
24};\r
25\r
26/**\r
27 This is the declaration of an EFI image entry point. This entry point is\r
28 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including\r
29 both device drivers and bus drivers.\r
30\r
31 The entry point for IP4 driver which install the driver\r
32 binding and component name protocol on its image.\r
33\r
34 @param[in] ImageHandle The firmware allocated handle for the UEFI image.\r
35 @param[in] SystemTable A pointer to the EFI System Table.\r
36\r
37 @retval EFI_SUCCESS The operation completed successfully.\r
38 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
39\r
40**/\r
41EFI_STATUS\r
42EFIAPI\r
43Ip4DriverEntryPoint (\r
44 IN EFI_HANDLE ImageHandle,\r
45 IN EFI_SYSTEM_TABLE *SystemTable\r
46 )\r
47{\r
48 return EfiLibInstallDriverBindingComponentName2 (\r
49 ImageHandle,\r
50 SystemTable,\r
51 &gIp4DriverBinding,\r
52 ImageHandle,\r
53 &gIp4ComponentName,\r
54 &gIp4ComponentName2\r
55 );\r
56}\r
57\r
58/**\r
59 Test to see if this driver supports ControllerHandle. This service\r
60 is called by the EFI boot service ConnectController(). In\r
61 order to make drivers as small as possible, there are a few calling\r
62 restrictions for this service. ConnectController() must\r
63 follow these calling restrictions. If any other agent wishes to call\r
64 Supported() it must also follow these calling restrictions.\r
65\r
66 @param[in] This Protocol instance pointer.\r
67 @param[in] ControllerHandle Handle of device to test\r
68 @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r
69 device to start.\r
70\r
71 @retval EFI_SUCCESS This driver supports this device\r
72 @retval EFI_ALREADY_STARTED This driver is already running on this device\r
73 @retval other This driver does not support this device\r
74\r
75**/\r
76EFI_STATUS\r
77EFIAPI\r
78Ip4DriverBindingSupported (\r
79 IN EFI_DRIVER_BINDING_PROTOCOL * This,\r
80 IN EFI_HANDLE ControllerHandle,\r
81 IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL\r
82 )\r
83{\r
84 EFI_STATUS Status;\r
85\r
86 //\r
87 // Test for the MNP service binding Protocol\r
88 //\r
89 Status = gBS->OpenProtocol (\r
90 ControllerHandle,\r
91 &gEfiManagedNetworkServiceBindingProtocolGuid,\r
92 NULL,\r
93 This->DriverBindingHandle,\r
94 ControllerHandle,\r
95 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
96 );\r
97\r
98 if (EFI_ERROR (Status)) {\r
99 return Status;\r
100 }\r
101\r
102 //\r
103 // Test for the Arp service binding Protocol\r
104 //\r
105 Status = gBS->OpenProtocol (\r
106 ControllerHandle,\r
107 &gEfiArpServiceBindingProtocolGuid,\r
108 NULL,\r
109 This->DriverBindingHandle,\r
110 ControllerHandle,\r
111 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
112 );\r
113\r
114 return Status;\r
115}\r
116\r
117/**\r
118 Clean up a IP4 service binding instance. It will release all\r
119 the resource allocated by the instance. The instance may be\r
120 partly initialized, or partly destroyed. If a resource is\r
121 destroyed, it is marked as that in case the destroy failed and\r
122 being called again later.\r
123\r
124 @param[in] IpSb The IP4 serviceing binding instance to clean up\r
125\r
126 @retval EFI_SUCCESS The resource used by the instance are cleaned up\r
127 @retval other Failed to clean up some of the resources.\r
128\r
129**/\r
130EFI_STATUS\r
131Ip4CleanService (\r
132 IN IP4_SERVICE *IpSb\r
133 );\r
134\r
135\r
136/**\r
137 Create a new IP4 driver service binding private instance.\r
138\r
139 @param Controller The controller that has MNP service binding\r
140 installed\r
141 @param ImageHandle The IP4 driver's image handle\r
142 @param Service The variable to receive the newly created IP4\r
143 service.\r
144\r
145 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource\r
146 @retval EFI_SUCCESS A new IP4 service binding private is created.\r
147 @retval other Other error occurs.\r
148\r
149**/\r
150EFI_STATUS\r
151Ip4CreateService (\r
152 IN EFI_HANDLE Controller,\r
153 IN EFI_HANDLE ImageHandle,\r
154 OUT IP4_SERVICE **Service\r
155 )\r
156{\r
157 IP4_SERVICE *IpSb;\r
158 EFI_STATUS Status;\r
159\r
160 ASSERT (Service != NULL);\r
161\r
162 *Service = NULL;\r
163\r
164 //\r
165 // allocate a service private data then initialize all the filed to\r
166 // empty resources, so if any thing goes wrong when allocating\r
167 // resources, Ip4CleanService can be called to clean it up.\r
168 //\r
169 IpSb = AllocatePool (sizeof (IP4_SERVICE));\r
170\r
171 if (IpSb == NULL) {\r
172 return EFI_OUT_OF_RESOURCES;\r
173 }\r
174\r
175 IpSb->Signature = IP4_SERVICE_SIGNATURE;\r
176 IpSb->ServiceBinding.CreateChild = Ip4ServiceBindingCreateChild;\r
177 IpSb->ServiceBinding.DestroyChild = Ip4ServiceBindingDestroyChild;\r
178 IpSb->State = IP4_SERVICE_UNSTARTED;\r
179 IpSb->InDestroy = FALSE;\r
180\r
181 IpSb->NumChildren = 0;\r
182 InitializeListHead (&IpSb->Children);\r
183\r
184 InitializeListHead (&IpSb->Interfaces);\r
185 IpSb->DefaultInterface = NULL;\r
186 IpSb->DefaultRouteTable = NULL;\r
187\r
188 Ip4InitAssembleTable (&IpSb->Assemble);\r
189\r
190 IpSb->IgmpCtrl.Igmpv1QuerySeen = 0;\r
191 InitializeListHead (&IpSb->IgmpCtrl.Groups);\r
192\r
193 IpSb->Image = ImageHandle;\r
194 IpSb->Controller = Controller;\r
195\r
196 IpSb->MnpChildHandle = NULL;\r
197 IpSb->Mnp = NULL;\r
198\r
199 IpSb->MnpConfigData.ReceivedQueueTimeoutValue = 0;\r
200 IpSb->MnpConfigData.TransmitQueueTimeoutValue = 0;\r
201 IpSb->MnpConfigData.ProtocolTypeFilter = IP4_ETHER_PROTO;\r
202 IpSb->MnpConfigData.EnableUnicastReceive = TRUE;\r
203 IpSb->MnpConfigData.EnableMulticastReceive = TRUE;\r
204 IpSb->MnpConfigData.EnableBroadcastReceive = TRUE;\r
205 IpSb->MnpConfigData.EnablePromiscuousReceive = FALSE;\r
206 IpSb->MnpConfigData.FlushQueuesOnReset = TRUE;\r
207 IpSb->MnpConfigData.EnableReceiveTimestamps = FALSE;\r
208 IpSb->MnpConfigData.DisableBackgroundPolling = FALSE;\r
209\r
210 ZeroMem (&IpSb->SnpMode, sizeof (EFI_SIMPLE_NETWORK_MODE));\r
211\r
212 IpSb->Timer = NULL;\r
213 IpSb->Ip4Config = NULL;\r
214 IpSb->DoneEvent = NULL;\r
215 IpSb->ReconfigEvent = NULL;\r
216 IpSb->ActiveEvent = NULL;\r
217\r
218 //\r
219 // Create various resources. First create the route table, timer\r
220 // event and MNP child. IGMP, interface's initialization depend\r
221 // on the MNP child.\r
222 //\r
223 IpSb->DefaultRouteTable = Ip4CreateRouteTable ();\r
224\r
225 if (IpSb->DefaultRouteTable == NULL) {\r
226 Status = EFI_OUT_OF_RESOURCES;\r
227 goto ON_ERROR;\r
228 }\r
229\r
230 Status = gBS->CreateEvent (\r
231 EVT_NOTIFY_SIGNAL | EVT_TIMER,\r
232 TPL_CALLBACK,\r
233 Ip4TimerTicking,\r
234 IpSb,\r
235 &IpSb->Timer\r
236 );\r
237\r
238 if (EFI_ERROR (Status)) {\r
239 goto ON_ERROR;\r
240 }\r
241\r
242 Status = NetLibCreateServiceChild (\r
243 Controller,\r
244 ImageHandle,\r
245 &gEfiManagedNetworkServiceBindingProtocolGuid,\r
246 &IpSb->MnpChildHandle\r
247 );\r
248\r
249 if (EFI_ERROR (Status)) {\r
250 goto ON_ERROR;\r
251 }\r
252\r
253 Status = gBS->OpenProtocol (\r
254 IpSb->MnpChildHandle,\r
255 &gEfiManagedNetworkProtocolGuid,\r
256 (VOID **) &IpSb->Mnp,\r
257 ImageHandle,\r
258 Controller,\r
259 EFI_OPEN_PROTOCOL_BY_DRIVER\r
260 );\r
261\r
262 if (EFI_ERROR (Status)) {\r
263 goto ON_ERROR;\r
264 }\r
265\r
266 Status = Ip4ServiceConfigMnp (IpSb, TRUE);\r
267\r
268 if (EFI_ERROR (Status)) {\r
269 goto ON_ERROR;\r
270 }\r
271\r
272 Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &IpSb->SnpMode);\r
273\r
274 if (EFI_ERROR (Status)) {\r
275 goto ON_ERROR;\r
276 }\r
277\r
278 Status = Ip4InitIgmp (IpSb);\r
279\r
280 if (EFI_ERROR (Status)) {\r
281 goto ON_ERROR;\r
282 }\r
283\r
284 IpSb->DefaultInterface = Ip4CreateInterface (IpSb->Mnp, Controller, ImageHandle);\r
285\r
286 if (IpSb->DefaultInterface == NULL) {\r
287 Status = EFI_OUT_OF_RESOURCES;\r
288 goto ON_ERROR;\r
289 }\r
290\r
291 InsertHeadList (&IpSb->Interfaces, &IpSb->DefaultInterface->Link);\r
292\r
293 IpSb->MaxPacketSize = IpSb->SnpMode.MaxPacketSize - sizeof (IP4_HEAD);\r
294 if (NetLibGetVlanId (IpSb->Controller) != 0) {\r
295 //\r
296 // This is a VLAN device, reduce MTU by VLAN tag length\r
297 //\r
298 IpSb->MaxPacketSize -= NET_VLAN_TAG_LEN;\r
299 }\r
300 IpSb->OldMaxPacketSize = IpSb->MaxPacketSize;\r
301 IpSb->MacString = NULL;\r
302\r
303 *Service = IpSb;\r
304 return EFI_SUCCESS;\r
305\r
306ON_ERROR:\r
307 Ip4CleanService (IpSb);\r
308 FreePool (IpSb);\r
309\r
310 return Status;\r
311}\r
312\r
313\r
314/**\r
315 Clean up a IP4 service binding instance. It will release all\r
316 the resource allocated by the instance. The instance may be\r
317 partly initialized, or partly destroyed. If a resource is\r
318 destroyed, it is marked as that in case the destroy failed and\r
319 being called again later.\r
320\r
321 @param[in] IpSb The IP4 serviceing binding instance to clean up\r
322\r
323 @retval EFI_SUCCESS The resource used by the instance are cleaned up\r
324 @retval other Failed to clean up some of the resources.\r
325\r
326**/\r
327EFI_STATUS\r
328Ip4CleanService (\r
329 IN IP4_SERVICE *IpSb\r
330 )\r
331{\r
332 EFI_STATUS Status;\r
333\r
334 if (IpSb->DefaultInterface != NULL) {\r
335 Status = Ip4FreeInterface (IpSb->DefaultInterface, NULL);\r
336\r
337 if (EFI_ERROR (Status)) {\r
338 return Status;\r
339 }\r
340\r
341 IpSb->DefaultInterface = NULL;\r
342 }\r
343\r
344 if (IpSb->DefaultRouteTable != NULL) {\r
345 Ip4FreeRouteTable (IpSb->DefaultRouteTable);\r
346 IpSb->DefaultRouteTable = NULL;\r
347 }\r
348\r
349 Ip4CleanAssembleTable (&IpSb->Assemble);\r
350\r
351 if (IpSb->MnpChildHandle != NULL) {\r
352 if (IpSb->Mnp != NULL) {\r
353 gBS->CloseProtocol (\r
354 IpSb->MnpChildHandle,\r
355 &gEfiManagedNetworkProtocolGuid,\r
356 IpSb->Image,\r
357 IpSb->Controller\r
358 );\r
359\r
360 IpSb->Mnp = NULL;\r
361 }\r
362\r
363 NetLibDestroyServiceChild (\r
364 IpSb->Controller,\r
365 IpSb->Image,\r
366 &gEfiManagedNetworkServiceBindingProtocolGuid,\r
367 IpSb->MnpChildHandle\r
368 );\r
369\r
370 IpSb->MnpChildHandle = NULL;\r
371 }\r
372\r
373 if (IpSb->Timer != NULL) {\r
374 gBS->SetTimer (IpSb->Timer, TimerCancel, 0);\r
375 gBS->CloseEvent (IpSb->Timer);\r
376\r
377 IpSb->Timer = NULL;\r
378 }\r
379\r
380 if (IpSb->Ip4Config != NULL) {\r
381 IpSb->Ip4Config->Stop (IpSb->Ip4Config);\r
382\r
383 gBS->CloseProtocol (\r
384 IpSb->Controller,\r
385 &gEfiIp4ConfigProtocolGuid,\r
386 IpSb->Image,\r
387 IpSb->Controller\r
388 );\r
389\r
390 gBS->CloseEvent (IpSb->DoneEvent);\r
391 gBS->CloseEvent (IpSb->ReconfigEvent);\r
392 IpSb->ActiveEvent = NULL;\r
393 IpSb->Ip4Config = NULL;\r
394 }\r
395\r
396 return EFI_SUCCESS;\r
397}\r
398\r
399\r
400/**\r
401 Start this driver on ControllerHandle. This service is called by the\r
402 EFI boot service ConnectController(). In order to make\r
403 drivers as small as possible, there are a few calling restrictions for\r
404 this service. ConnectController() must follow these\r
405 calling restrictions. If any other agent wishes to call Start() it\r
406 must also follow these calling restrictions.\r
407\r
408 @param[in] This Protocol instance pointer.\r
409 @param[in] ControllerHandle Handle of device to bind driver to\r
410 @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r
411 device to start.\r
412\r
413 @retval EFI_SUCCESS This driver is added to ControllerHandle\r
414 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle\r
415 @retval other This driver does not support this device\r
416\r
417**/\r
418EFI_STATUS\r
419EFIAPI\r
420Ip4DriverBindingStart (\r
421 IN EFI_DRIVER_BINDING_PROTOCOL * This,\r
422 IN EFI_HANDLE ControllerHandle,\r
423 IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL\r
424 )\r
425{\r
426 IP4_SERVICE *IpSb;\r
427 EFI_STATUS Status;\r
428\r
429 //\r
430 // Test for the Ip4 service binding protocol\r
431 //\r
432 Status = gBS->OpenProtocol (\r
433 ControllerHandle,\r
434 &gEfiIp4ServiceBindingProtocolGuid,\r
435 NULL,\r
436 This->DriverBindingHandle,\r
437 ControllerHandle,\r
438 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
439 );\r
440\r
441 if (Status == EFI_SUCCESS) {\r
442 return EFI_ALREADY_STARTED;\r
443 }\r
444\r
445 Status = Ip4CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb);\r
446\r
447 if (EFI_ERROR (Status)) {\r
448 return Status;\r
449 }\r
450 ASSERT (IpSb != NULL);\r
451\r
452 //\r
453 // Install the Ip4ServiceBinding Protocol onto ControlerHandle\r
454 //\r
455 Status = gBS->InstallMultipleProtocolInterfaces (\r
456 &ControllerHandle,\r
457 &gEfiIp4ServiceBindingProtocolGuid,\r
458 &IpSb->ServiceBinding,\r
459 NULL\r
460 );\r
461\r
462 if (EFI_ERROR (Status)) {\r
463 goto FREE_SERVICE;\r
464 }\r
465\r
466 //\r
467 // ready to go: start the receiving and timer\r
468 //\r
469 Status = Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);\r
470\r
471 if (EFI_ERROR (Status)) {\r
472 goto UNINSTALL_PROTOCOL;\r
473 }\r
474\r
475 Status = gBS->SetTimer (IpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);\r
476\r
477 if (EFI_ERROR (Status)) {\r
478 goto UNINSTALL_PROTOCOL;\r
479 }\r
480\r
481 //\r
482 // Initialize the IP4 ID\r
483 //\r
484 mIp4Id = (UINT16)NET_RANDOM (NetRandomInitSeed ());\r
485\r
486 Ip4SetVariableData (IpSb);\r
487\r
488 return Status;\r
489\r
490UNINSTALL_PROTOCOL:\r
491 gBS->UninstallProtocolInterface (\r
492 ControllerHandle,\r
493 &gEfiIp4ServiceBindingProtocolGuid,\r
494 &IpSb->ServiceBinding\r
495 );\r
496\r
497FREE_SERVICE:\r
498 Ip4CleanService (IpSb);\r
499 FreePool (IpSb);\r
500\r
501 return Status;\r
502}\r
503\r
504\r
505/**\r
506 Stop this driver on ControllerHandle. This service is called by the\r
507 EFI boot service DisconnectController(). In order to\r
508 make drivers as small as possible, there are a few calling\r
509 restrictions for this service. DisconnectController()\r
510 must follow these calling restrictions. If any other agent wishes\r
511 to call Stop() it must also follow these calling restrictions.\r
512\r
513 @param[in] This Protocol instance pointer.\r
514 @param[in] ControllerHandle Handle of device to stop driver on\r
515 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number\r
516 of children is zero stop the entire bus driver.\r
517 @param[in] ChildHandleBuffer List of Child Handles to Stop.\r
518\r
519 @retval EFI_SUCCESS This driver is removed ControllerHandle\r
520 @retval other This driver was not removed from this device\r
521\r
522**/\r
523EFI_STATUS\r
524EFIAPI\r
525Ip4DriverBindingStop (\r
526 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
527 IN EFI_HANDLE ControllerHandle,\r
528 IN UINTN NumberOfChildren,\r
529 IN EFI_HANDLE *ChildHandleBuffer\r
530 )\r
531{\r
532 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;\r
533 IP4_SERVICE *IpSb;\r
534 IP4_PROTOCOL *IpInstance;\r
535 EFI_HANDLE NicHandle;\r
536 EFI_STATUS Status;\r
537 EFI_TPL OldTpl;\r
538 INTN State;\r
539 BOOLEAN IsArp;\r
540\r
541 //\r
542 // IP4 driver opens the MNP child, ARP children or the IP4_CONFIG protocol\r
543 // by driver. So the ControllerHandle may be the MNP child handle, ARP child\r
544 // handle, or the NIC (UNDI) handle because IP4_CONFIG protocol is installed\r
545 // in the NIC handle.\r
546 //\r
547 //\r
548 // First, check whether it is the IP4_CONFIG protocol being uninstalled.\r
549 // IP4_CONFIG protocol is installed on the NIC handle. It isn't necessary\r
550 // to clean up the default configuration if IP4_CONFIG is being stopped.\r
551 //\r
552 Status = gBS->OpenProtocol (\r
553 ControllerHandle,\r
554 &gEfiIp4ConfigProtocolGuid,\r
555 NULL,\r
556 This->DriverBindingHandle,\r
557 ControllerHandle,\r
558 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
559 );\r
560\r
561 if (Status == EFI_SUCCESS) {\r
562 //\r
563 // Retrieve the IP4 service binding protocol. If failed, it is\r
564 // likely that Ip4 ServiceBinding is uninstalled already. In this\r
565 // case, return immediately.\r
566 //\r
567 Status = gBS->OpenProtocol (\r
568 ControllerHandle,\r
569 &gEfiIp4ServiceBindingProtocolGuid,\r
570 (VOID **) &ServiceBinding,\r
571 This->DriverBindingHandle,\r
572 ControllerHandle,\r
573 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
574 );\r
575\r
576 if (EFI_ERROR (Status)) {\r
577 return EFI_DEVICE_ERROR;\r
578 }\r
579\r
580 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);\r
581\r
582 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
583\r
584 if (IpSb->Ip4Config != NULL && (IpSb->State != IP4_SERVICE_DESTROY)) {\r
585\r
586 IpSb->Ip4Config->Stop (IpSb->Ip4Config);\r
587\r
588 Status = gBS->CloseProtocol (\r
589 ControllerHandle,\r
590 &gEfiIp4ConfigProtocolGuid,\r
591 IpSb->Image,\r
592 ControllerHandle\r
593 );\r
594\r
595 if (EFI_ERROR (Status)) {\r
596 gBS->RestoreTPL (OldTpl);\r
597 return Status;\r
598 }\r
599\r
600 //\r
601 // If the auto configure hasn't complete, mark it as not started.\r
602 //\r
603 if (IpSb->State == IP4_SERVICE_STARTED) {\r
604 IpSb->State = IP4_SERVICE_UNSTARTED;\r
605 }\r
606\r
607 IpSb->Ip4Config = NULL;\r
608 gBS->CloseEvent (IpSb->DoneEvent);\r
609 gBS->CloseEvent (IpSb->ReconfigEvent);\r
610 }\r
611\r
612 gBS->RestoreTPL (OldTpl);\r
613 return EFI_SUCCESS;\r
614 }\r
615\r
616 //\r
617 // Either MNP or ARP protocol is being uninstalled. The controller\r
618 // handle is either the MNP child or ARP child. But, the IP4's\r
619 // service binding is installed on the NIC handle. So, need to open\r
620 // the protocol info to find the NIC handle.\r
621 //\r
622 IsArp = FALSE;\r
623 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);\r
624\r
625 if (NicHandle == NULL) {\r
626 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid);\r
627 IsArp = TRUE;\r
628 }\r
629\r
630 if (NicHandle == NULL) {\r
631 return EFI_DEVICE_ERROR;\r
632 }\r
633\r
634 //\r
635 // Retrieve the IP4 service binding protocol\r
636 //\r
637 Status = gBS->OpenProtocol (\r
638 NicHandle,\r
639 &gEfiIp4ServiceBindingProtocolGuid,\r
640 (VOID **) &ServiceBinding,\r
641 This->DriverBindingHandle,\r
642 NicHandle,\r
643 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
644 );\r
645\r
646 if (EFI_ERROR (Status)) {\r
647 return EFI_DEVICE_ERROR;\r
648 }\r
649\r
650 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);\r
651\r
652 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
653\r
654 if (IpSb->InDestroy) {\r
655 gBS->RestoreTPL (OldTpl);\r
656 return EFI_SUCCESS;\r
657 }\r
658\r
659 if (IsArp) {\r
660 while (!IsListEmpty (&IpSb->Children)) {\r
661 IpInstance = NET_LIST_HEAD (&IpSb->Children, IP4_PROTOCOL, Link);\r
662\r
663 ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);\r
664 }\r
665\r
666 if (IpSb->NumChildren != 0) {\r
667 Status = EFI_DEVICE_ERROR;\r
668 goto ON_ERROR;\r
669 }\r
670\r
671 IpSb->InDestroy = TRUE;\r
672\r
673 State = IpSb->State;\r
674 IpSb->State = IP4_SERVICE_DESTROY;\r
675\r
676 //\r
677 // Clear the variable data.\r
678 //\r
679 Ip4ClearVariableData (IpSb);\r
680\r
681 //\r
682 // OK, clean other resources then uninstall the service binding protocol.\r
683 //\r
684 Status = Ip4CleanService (IpSb);\r
685\r
686 if (EFI_ERROR (Status)) {\r
687 IpSb->State = State;\r
688 goto ON_ERROR;\r
689 }\r
690\r
691 gBS->UninstallProtocolInterface (\r
692 NicHandle,\r
693 &gEfiIp4ServiceBindingProtocolGuid,\r
694 ServiceBinding\r
695 );\r
696\r
697 FreePool (IpSb);\r
698 } else if (NumberOfChildren == 0) {\r
699 IpSb->InDestroy = TRUE;\r
700\r
701 State = IpSb->State;\r
702 IpSb->State = IP4_SERVICE_DESTROY;\r
703\r
704 //\r
705 // Clear the variable data.\r
706 //\r
707 Ip4ClearVariableData (IpSb);\r
708\r
709 //\r
710 // OK, clean other resources then uninstall the service binding protocol.\r
711 //\r
712 Status = Ip4CleanService (IpSb);\r
713\r
714 if (EFI_ERROR (Status)) {\r
715 IpSb->State = State;\r
716 goto ON_ERROR;\r
717 }\r
718\r
719 gBS->UninstallProtocolInterface (\r
720 NicHandle,\r
721 &gEfiIp4ServiceBindingProtocolGuid,\r
722 ServiceBinding\r
723 );\r
724\r
725 FreePool (IpSb);\r
726 } else {\r
727\r
728 while (!IsListEmpty (&IpSb->Children)) {\r
729 IpInstance = NET_LIST_HEAD (&IpSb->Children, IP4_PROTOCOL, Link);\r
730\r
731 ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);\r
732 }\r
733\r
734 if (IpSb->NumChildren != 0) {\r
735 Status = EFI_DEVICE_ERROR;\r
736 }\r
737 }\r
738\r
739ON_ERROR:\r
740\r
741 gBS->RestoreTPL (OldTpl);\r
742 return Status;\r
743}\r
744\r
745\r
746/**\r
747 Creates a child handle and installs a protocol.\r
748\r
749 The CreateChild() function installs a protocol on ChildHandle.\r
750 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.\r
751 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.\r
752\r
753 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
754 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,\r
755 then a new handle is created. If it is a pointer to an existing UEFI handle,\r
756 then the protocol is added to the existing UEFI handle.\r
757\r
758 @retval EFI_SUCCES The protocol was added to ChildHandle.\r
759 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.\r
760 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create\r
761 the child\r
762 @retval other The child handle was not created\r
763\r
764**/\r
765EFI_STATUS\r
766EFIAPI\r
767Ip4ServiceBindingCreateChild (\r
768 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
769 IN OUT EFI_HANDLE *ChildHandle\r
770 )\r
771{\r
772 IP4_SERVICE *IpSb;\r
773 IP4_PROTOCOL *IpInstance;\r
774 EFI_TPL OldTpl;\r
775 EFI_STATUS Status;\r
776 VOID *Mnp;\r
777\r
778 if ((This == NULL) || (ChildHandle == NULL)) {\r
779 return EFI_INVALID_PARAMETER;\r
780 }\r
781\r
782 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);\r
783 IpInstance = AllocatePool (sizeof (IP4_PROTOCOL));\r
784\r
785 if (IpInstance == NULL) {\r
786 return EFI_OUT_OF_RESOURCES;\r
787 }\r
788\r
789 Ip4InitProtocol (IpSb, IpInstance);\r
790\r
791 //\r
792 // Install Ip4 onto ChildHandle\r
793 //\r
794 Status = gBS->InstallMultipleProtocolInterfaces (\r
795 ChildHandle,\r
796 &gEfiIp4ProtocolGuid,\r
797 &IpInstance->Ip4Proto,\r
798 NULL\r
799 );\r
800\r
801 if (EFI_ERROR (Status)) {\r
802 goto ON_ERROR;\r
803 }\r
804\r
805 IpInstance->Handle = *ChildHandle;\r
806\r
807 //\r
808 // Open the Managed Network protocol BY_CHILD.\r
809 //\r
810 Status = gBS->OpenProtocol (\r
811 IpSb->MnpChildHandle,\r
812 &gEfiManagedNetworkProtocolGuid,\r
813 (VOID **) &Mnp,\r
814 gIp4DriverBinding.DriverBindingHandle,\r
815 IpInstance->Handle,\r
816 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
817 );\r
818 if (EFI_ERROR (Status)) {\r
819 gBS->UninstallMultipleProtocolInterfaces (\r
820 ChildHandle,\r
821 &gEfiIp4ProtocolGuid,\r
822 &IpInstance->Ip4Proto,\r
823 NULL\r
824 );\r
825\r
826 goto ON_ERROR;\r
827 }\r
828\r
829 //\r
830 // Insert it into the service binding instance.\r
831 //\r
832 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
833\r
834 InsertTailList (&IpSb->Children, &IpInstance->Link);\r
835 IpSb->NumChildren++;\r
836\r
837 gBS->RestoreTPL (OldTpl);\r
838\r
839ON_ERROR:\r
840\r
841 if (EFI_ERROR (Status)) {\r
842\r
843 Ip4CleanProtocol (IpInstance);\r
844\r
845 FreePool (IpInstance);\r
846 }\r
847\r
848 return Status;\r
849}\r
850\r
851\r
852/**\r
853 Destroys a child handle with a protocol installed on it.\r
854\r
855 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol\r
856 that was installed by CreateChild() from ChildHandle. If the removed protocol is the\r
857 last protocol on ChildHandle, then ChildHandle is destroyed.\r
858\r
859 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
860 @param ChildHandle Handle of the child to destroy\r
861\r
862 @retval EFI_SUCCES The protocol was removed from ChildHandle.\r
863 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.\r
864 @retval EFI_INVALID_PARAMETER Child handle is NULL.\r
865 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle\r
866 because its services are being used.\r
867 @retval other The child handle was not destroyed\r
868\r
869**/\r
870EFI_STATUS\r
871EFIAPI\r
872Ip4ServiceBindingDestroyChild (\r
873 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
874 IN EFI_HANDLE ChildHandle\r
875 )\r
876{\r
877 EFI_STATUS Status;\r
878 IP4_SERVICE *IpSb;\r
879 IP4_PROTOCOL *IpInstance;\r
880 EFI_IP4_PROTOCOL *Ip4;\r
881 EFI_TPL OldTpl;\r
882 INTN State;\r
883\r
884 if ((This == NULL) || (ChildHandle == NULL)) {\r
885 return EFI_INVALID_PARAMETER;\r
886 }\r
887\r
888 //\r
889 // Retrieve the private context data structures\r
890 //\r
891 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);\r
892\r
893 Status = gBS->OpenProtocol (\r
894 ChildHandle,\r
895 &gEfiIp4ProtocolGuid,\r
896 (VOID **) &Ip4,\r
897 gIp4DriverBinding.DriverBindingHandle,\r
898 ChildHandle,\r
899 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
900 );\r
901\r
902 if (EFI_ERROR (Status)) {\r
903 return EFI_UNSUPPORTED;\r
904 }\r
905\r
906 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (Ip4);\r
907\r
908 if (IpInstance->Service != IpSb) {\r
909 return EFI_INVALID_PARAMETER;\r
910 }\r
911\r
912 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
913\r
914 //\r
915 // A child can be destroyed more than once. For example,\r
916 // Ip4DriverBindingStop will destroy all of its children.\r
917 // when UDP driver is being stopped, it will destroy all\r
918 // the IP child it opens.\r
919 //\r
920 if (IpInstance->State == IP4_STATE_DESTROY) {\r
921 gBS->RestoreTPL (OldTpl);\r
922 return EFI_SUCCESS;\r
923 }\r
924\r
925 State = IpInstance->State;\r
926 IpInstance->State = IP4_STATE_DESTROY;\r
927\r
928 //\r
929 // Close the Managed Network protocol.\r
930 //\r
931 gBS->CloseProtocol (\r
932 IpSb->MnpChildHandle,\r
933 &gEfiManagedNetworkProtocolGuid,\r
934 gIp4DriverBinding.DriverBindingHandle,\r
935 ChildHandle\r
936 );\r
937\r
938 //\r
939 // Uninstall the IP4 protocol first. Many thing happens during\r
940 // this:\r
941 // 1. The consumer of the IP4 protocol will be stopped if it\r
942 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is\r
943 // stopped, IP driver's stop function will be called, and uninstall\r
944 // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This\r
945 // makes it possible to create the network stack bottom up, and\r
946 // stop it top down.\r
947 // 2. the upper layer will recycle the received packet. The recycle\r
948 // event's TPL is higher than this function. The recycle events\r
949 // will be called back before preceeding. If any packets not recycled,\r
950 // that means there is a resource leak.\r
951 //\r
952 Status = gBS->UninstallProtocolInterface (\r
953 ChildHandle,\r
954 &gEfiIp4ProtocolGuid,\r
955 &IpInstance->Ip4Proto\r
956 );\r
957\r
958 if (EFI_ERROR (Status)) {\r
959 goto ON_ERROR;\r
960 }\r
961\r
962 Status = Ip4CleanProtocol (IpInstance);\r
963\r
964 Ip4SetVariableData (IpSb);\r
965\r
966 if (EFI_ERROR (Status)) {\r
967 gBS->InstallMultipleProtocolInterfaces (\r
968 &ChildHandle,\r
969 &gEfiIp4ProtocolGuid,\r
970 Ip4,\r
971 NULL\r
972 );\r
973\r
974 goto ON_ERROR;\r
975 }\r
976\r
977 RemoveEntryList (&IpInstance->Link);\r
978 IpSb->NumChildren--;\r
979\r
980 gBS->RestoreTPL (OldTpl);\r
981\r
982 FreePool (IpInstance);\r
983 return EFI_SUCCESS;\r
984\r
985ON_ERROR:\r
986 IpInstance->State = State;\r
987 gBS->RestoreTPL (OldTpl);\r
988\r
989 return Status;\r
990}\r