]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Dhcp4Dxe/Dhcp4Driver.c
add back CAPSULE_HOB_INFO definition for back-compatible.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Dhcp4Dxe / Dhcp4Driver.c
CommitLineData
772db4bb 1/** @file\r
2\r
c5bcc2e2 3Copyright (c) 2006 - 2009, Intel Corporation.<BR>\r
772db4bb 4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
772db4bb 12**/\r
13\r
14#include "Dhcp4Impl.h"\r
15#include "Dhcp4Driver.h"\r
16\r
17EFI_DRIVER_BINDING_PROTOCOL gDhcp4DriverBinding = {\r
18 Dhcp4DriverBindingSupported,\r
19 Dhcp4DriverBindingStart,\r
20 Dhcp4DriverBindingStop,\r
21 0xa,\r
22 NULL,\r
23 NULL\r
24};\r
25\r
f9204641 26EFI_SERVICE_BINDING_PROTOCOL mDhcp4ServiceBindingTemplate = {\r
772db4bb 27 Dhcp4ServiceBindingCreateChild,\r
28 Dhcp4ServiceBindingDestroyChild\r
29};\r
30\r
7bce0c5a 31/**\r
f9204641 32 This is the declaration of an EFI image entry point. This entry point is\r
33 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including\r
34 both device drivers and bus drivers.\r
35 \r
7bce0c5a 36 Entry point of the DHCP driver to install various protocols.\r
37\r
3e8c18da 38 @param[in] ImageHandle The firmware allocated handle for the UEFI image.\r
39 @param[in] SystemTable A pointer to the EFI System Table.\r
7bce0c5a 40\r
f9204641 41 @retval EFI_SUCCESS The operation completed successfully.\r
42 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
772db4bb 43\r
7bce0c5a 44**/\r
772db4bb 45EFI_STATUS\r
46EFIAPI\r
47Dhcp4DriverEntryPoint (\r
48 IN EFI_HANDLE ImageHandle,\r
49 IN EFI_SYSTEM_TABLE *SystemTable\r
50 )\r
772db4bb 51{\r
83cbd279 52 return EfiLibInstallDriverBindingComponentName2 (\r
772db4bb 53 ImageHandle,\r
54 SystemTable,\r
55 &gDhcp4DriverBinding,\r
56 ImageHandle,\r
57 &gDhcp4ComponentName,\r
83cbd279 58 &gDhcp4ComponentName2\r
772db4bb 59 );\r
60}\r
61\r
62\r
63/**\r
f9204641 64 Test to see if this driver supports ControllerHandle. This service\r
65 is called by the EFI boot service ConnectController(). In\r
66 order to make drivers as small as possible, there are a few calling\r
67 restrictions for this service. ConnectController() must\r
68 follow these calling restrictions. If any other agent wishes to call\r
69 Supported() it must also follow these calling restrictions.\r
70\r
3e8c18da 71 @param[in] This Protocol instance pointer.\r
72 @param[in] ControllerHandle Handle of device to test\r
73 @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r
74 device to start.\r
f9204641 75\r
76 @retval EFI_SUCCESS This driver supports this device\r
77 @retval EFI_ALREADY_STARTED This driver is already running on this device\r
78 @retval other This driver does not support this device\r
772db4bb 79\r
80**/\r
81EFI_STATUS\r
82EFIAPI\r
83Dhcp4DriverBindingSupported (\r
84 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
85 IN EFI_HANDLE ControllerHandle,\r
86 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
87 )\r
88{\r
89 EFI_STATUS Status;\r
90\r
91 Status = gBS->OpenProtocol (\r
92 ControllerHandle,\r
93 &gEfiUdp4ServiceBindingProtocolGuid,\r
94 NULL,\r
95 This->DriverBindingHandle,\r
96 ControllerHandle,\r
97 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
98 );\r
99\r
100 return Status;\r
101}\r
102\r
103\r
104\r
105/**\r
106 Configure the default UDP child to receive all the DHCP traffics\r
107 on this network interface.\r
108\r
3e8c18da 109 @param[in] UdpIo The UDP IO port to configure\r
110 @param[in] Context The context to the function\r
772db4bb 111\r
112 @retval EFI_SUCCESS The UDP IO port is successfully configured.\r
113 @retval Others Failed to configure the UDP child.\r
114\r
115**/\r
116EFI_STATUS\r
117DhcpConfigUdpIo (\r
118 IN UDP_IO_PORT *UdpIo,\r
119 IN VOID *Context\r
120 )\r
121{\r
122 EFI_UDP4_CONFIG_DATA UdpConfigData;\r
123\r
124 UdpConfigData.AcceptBroadcast = TRUE;\r
125 UdpConfigData.AcceptPromiscuous = FALSE;\r
126 UdpConfigData.AcceptAnyPort = FALSE;\r
127 UdpConfigData.AllowDuplicatePort = TRUE;\r
128 UdpConfigData.TypeOfService = 0;\r
129 UdpConfigData.TimeToLive = 64;\r
130 UdpConfigData.DoNotFragment = FALSE;\r
131 UdpConfigData.ReceiveTimeout = 0;\r
132 UdpConfigData.TransmitTimeout = 0;\r
133\r
134 UdpConfigData.UseDefaultAddress = FALSE;\r
135 UdpConfigData.StationPort = DHCP_CLIENT_PORT;\r
136 UdpConfigData.RemotePort = DHCP_SERVER_PORT;\r
137\r
e48e37fc 138 ZeroMem (&UdpConfigData.StationAddress, sizeof (EFI_IPv4_ADDRESS));\r
139 ZeroMem (&UdpConfigData.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
140 ZeroMem (&UdpConfigData.RemoteAddress, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 141\r
142 return UdpIo->Udp->Configure (UdpIo->Udp, &UdpConfigData);;\r
143}\r
144\r
145\r
146\r
147/**\r
148 Destory the DHCP service. The Dhcp4 service may be partly initialized,\r
f9204641 149 or partly destroyed. If a resource is destroyed, it is marked as so in\r
150 case the destroy failed and being called again later.\r
772db4bb 151\r
3e8c18da 152 @param[in] DhcpSb The DHCP service instance to destory.\r
772db4bb 153\r
f9204641 154 @retval EFI_SUCCESS Always return success.\r
772db4bb 155\r
156**/\r
157EFI_STATUS\r
158Dhcp4CloseService (\r
159 IN DHCP_SERVICE *DhcpSb\r
160 )\r
161{\r
162 DhcpCleanLease (DhcpSb);\r
163\r
164 if (DhcpSb->UdpIo != NULL) {\r
165 UdpIoFreePort (DhcpSb->UdpIo);\r
166 DhcpSb->UdpIo = NULL;\r
167 }\r
168\r
169 if (DhcpSb->Timer != NULL) {\r
170 gBS->SetTimer (DhcpSb->Timer, TimerCancel, 0);\r
171 gBS->CloseEvent (DhcpSb->Timer);\r
172\r
173 DhcpSb->Timer = NULL;\r
174 }\r
175\r
176 return EFI_SUCCESS;\r
177}\r
178\r
179\r
180\r
181/**\r
182 Create a new DHCP service binding instance for the controller.\r
183\r
3e8c18da 184 @param[in] Controller The controller to install DHCP service binding\r
185 protocol onto\r
186 @param[in] ImageHandle The driver's image handle\r
187 @param[out] Service The variable to receive the created DHCP service\r
188 instance.\r
772db4bb 189\r
190 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource .\r
191 @retval EFI_SUCCESS The DHCP service instance is created.\r
f9204641 192 @retval other Other error occurs.\r
772db4bb 193\r
194**/\r
195EFI_STATUS\r
196Dhcp4CreateService (\r
197 IN EFI_HANDLE Controller,\r
198 IN EFI_HANDLE ImageHandle,\r
199 OUT DHCP_SERVICE **Service\r
200 )\r
201{\r
202 DHCP_SERVICE *DhcpSb;\r
203 EFI_STATUS Status;\r
204\r
205 *Service = NULL;\r
e48e37fc 206 DhcpSb = AllocateZeroPool (sizeof (DHCP_SERVICE));\r
772db4bb 207\r
208 if (DhcpSb == NULL) {\r
209 return EFI_OUT_OF_RESOURCES;\r
210 }\r
211\r
212 DhcpSb->Signature = DHCP_SERVICE_SIGNATURE;\r
772db4bb 213 DhcpSb->ServiceState = DHCP_UNCONFIGED;\r
214 DhcpSb->InDestory = FALSE;\r
215 DhcpSb->Controller = Controller;\r
216 DhcpSb->Image = ImageHandle;\r
e48e37fc 217 InitializeListHead (&DhcpSb->Children);\r
772db4bb 218 DhcpSb->DhcpState = Dhcp4Stopped;\r
219 DhcpSb->Xid = NET_RANDOM (NetRandomInitSeed ());\r
f9204641 220 CopyMem (\r
221 &DhcpSb->ServiceBinding,\r
222 &mDhcp4ServiceBindingTemplate,\r
223 sizeof (EFI_SERVICE_BINDING_PROTOCOL)\r
224 );\r
772db4bb 225 //\r
226 // Create various resources, UdpIo, Timer, and get Mac address\r
227 //\r
228 Status = gBS->CreateEvent (\r
229 EVT_NOTIFY_SIGNAL | EVT_TIMER,\r
e48e37fc 230 TPL_CALLBACK,\r
772db4bb 231 DhcpOnTimerTick,\r
232 DhcpSb,\r
233 &DhcpSb->Timer\r
234 );\r
235\r
236 if (EFI_ERROR (Status)) {\r
237 goto ON_ERROR;\r
238 }\r
239\r
240 DhcpSb->UdpIo = UdpIoCreatePort (Controller, ImageHandle, DhcpConfigUdpIo, NULL);\r
241\r
242 if (DhcpSb->UdpIo == NULL) {\r
243 Status = EFI_OUT_OF_RESOURCES;\r
244 goto ON_ERROR;\r
245 }\r
246\r
247 DhcpSb->HwLen = (UINT8) DhcpSb->UdpIo->SnpMode.HwAddressSize;\r
248 DhcpSb->HwType = DhcpSb->UdpIo->SnpMode.IfType;\r
687a2e5f 249 CopyMem (&DhcpSb->Mac, &DhcpSb->UdpIo->SnpMode.CurrentAddress, sizeof (DhcpSb->Mac));\r
772db4bb 250\r
251 *Service = DhcpSb;\r
252 return EFI_SUCCESS;\r
253\r
254ON_ERROR:\r
255 Dhcp4CloseService (DhcpSb);\r
a4df47f1 256 FreePool (DhcpSb);\r
772db4bb 257\r
258 return Status;\r
259}\r
260\r
261\r
262/**\r
f9204641 263 Start this driver on ControllerHandle. This service is called by the\r
264 EFI boot service ConnectController(). In order to make\r
265 drivers as small as possible, there are a few calling restrictions for\r
266 this service. ConnectController() must follow these\r
267 calling restrictions. If any other agent wishes to call Start() it\r
268 must also follow these calling restrictions.\r
269\r
3e8c18da 270 @param[in] This Protocol instance pointer.\r
271 @param[in] ControllerHandle Handle of device to bind driver to\r
272 @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r
273 device to start.\r
f9204641 274\r
275 @retval EFI_SUCCESS This driver is added to ControllerHandle\r
276 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle\r
277 @retval other This driver does not support this device\r
772db4bb 278\r
279**/\r
280EFI_STATUS\r
281EFIAPI\r
282Dhcp4DriverBindingStart (\r
283 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
284 IN EFI_HANDLE ControllerHandle,\r
285 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
286 )\r
287{\r
288 DHCP_SERVICE *DhcpSb;\r
289 EFI_STATUS Status;\r
290\r
291 //\r
292 // First: test for the DHCP4 Protocol\r
293 //\r
294 Status = gBS->OpenProtocol (\r
295 ControllerHandle,\r
296 &gEfiDhcp4ServiceBindingProtocolGuid,\r
297 NULL,\r
298 This->DriverBindingHandle,\r
299 ControllerHandle,\r
300 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
301 );\r
302\r
303 if (Status == EFI_SUCCESS) {\r
304 return EFI_ALREADY_STARTED;\r
305 }\r
306\r
307 Status = Dhcp4CreateService (ControllerHandle, This->DriverBindingHandle, &DhcpSb);\r
308\r
309 if (EFI_ERROR (Status)) {\r
310 return Status;\r
311 }\r
312\r
c5bcc2e2 313 //\r
314 // Start the receiving\r
315 //\r
316 Status = UdpIoRecvDatagram (DhcpSb->UdpIo, DhcpInput, DhcpSb, 0);\r
317\r
318 if (EFI_ERROR (Status)) {\r
319 goto ON_ERROR;\r
320 }\r
772db4bb 321 Status = gBS->SetTimer (DhcpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);\r
322\r
323 if (EFI_ERROR (Status)) {\r
324 goto ON_ERROR;\r
325 }\r
326\r
327 //\r
328 // Install the Dhcp4ServiceBinding Protocol onto ControlerHandle\r
329 //\r
330 Status = gBS->InstallMultipleProtocolInterfaces (\r
331 &ControllerHandle,\r
332 &gEfiDhcp4ServiceBindingProtocolGuid,\r
333 &DhcpSb->ServiceBinding,\r
334 NULL\r
335 );\r
336\r
337 if (EFI_ERROR (Status)) {\r
338 goto ON_ERROR;\r
339 }\r
340\r
341 return Status;\r
342\r
343ON_ERROR:\r
344 Dhcp4CloseService (DhcpSb);\r
a4df47f1 345 FreePool (DhcpSb);\r
772db4bb 346 return Status;\r
347}\r
348\r
349\r
350/**\r
f9204641 351 Stop this driver on ControllerHandle. This service is called by the\r
352 EFI boot service DisconnectController(). In order to\r
353 make drivers as small as possible, there are a few calling\r
354 restrictions for this service. DisconnectController()\r
355 must follow these calling restrictions. If any other agent wishes\r
356 to call Stop() it must also follow these calling restrictions.\r
357 \r
3e8c18da 358 @param[in] This Protocol instance pointer.\r
359 @param[in] ControllerHandle Handle of device to stop driver on\r
360 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
361 children is zero stop the entire bus driver.\r
362 @param[in] ChildHandleBuffer List of Child Handles to Stop.\r
f9204641 363\r
364 @retval EFI_SUCCESS This driver is removed ControllerHandle\r
365 @retval other This driver was not removed from this device\r
772db4bb 366\r
367**/\r
368EFI_STATUS\r
369EFIAPI\r
370Dhcp4DriverBindingStop (\r
371 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
372 IN EFI_HANDLE ControllerHandle,\r
373 IN UINTN NumberOfChildren,\r
374 IN EFI_HANDLE *ChildHandleBuffer\r
375 )\r
376{\r
377 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;\r
378 DHCP_SERVICE *DhcpSb;\r
379 DHCP_PROTOCOL *Instance;\r
380 EFI_HANDLE NicHandle;\r
381 EFI_STATUS Status;\r
382 EFI_TPL OldTpl;\r
383\r
384 //\r
385 // DHCP driver opens UDP child, So, the ControllerHandle is the\r
386 // UDP child handle. locate the Nic handle first.\r
387 //\r
388 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp4ProtocolGuid);\r
389\r
390 if (NicHandle == NULL) {\r
c4a62a12 391 return EFI_DEVICE_ERROR;\r
772db4bb 392 }\r
393\r
394 Status = gBS->OpenProtocol (\r
395 NicHandle,\r
396 &gEfiDhcp4ServiceBindingProtocolGuid,\r
397 (VOID **) &ServiceBinding,\r
398 This->DriverBindingHandle,\r
399 NicHandle,\r
400 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
401 );\r
402\r
403 if (EFI_ERROR (Status)) {\r
404 return EFI_DEVICE_ERROR;\r
405 }\r
406\r
407 DhcpSb = DHCP_SERVICE_FROM_THIS (ServiceBinding);\r
408\r
409 if (DhcpSb->InDestory) {\r
410 return EFI_SUCCESS;\r
411 }\r
412\r
e48e37fc 413 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 414\r
c4a62a12 415 if (NumberOfChildren == 0) {\r
772db4bb 416\r
c4a62a12 417 DhcpSb->InDestory = TRUE;\r
418 DhcpSb->ServiceState = DHCP_DESTORY;\r
772db4bb 419\r
c4a62a12 420 gBS->UninstallProtocolInterface (\r
421 NicHandle,\r
422 &gEfiDhcp4ServiceBindingProtocolGuid,\r
423 ServiceBinding\r
424 );\r
772db4bb 425\r
c4a62a12 426 Dhcp4CloseService (DhcpSb);\r
427\r
a4df47f1 428 FreePool (DhcpSb);\r
c4a62a12 429 } else {\r
430 //\r
431 // Don't use NET_LIST_FOR_EACH_SAFE here, Dhcp4ServiceBindingDestoryChild\r
432 // may cause other child to be deleted.\r
433 //\r
e48e37fc 434 while (!IsListEmpty (&DhcpSb->Children)) {\r
c4a62a12 435 Instance = NET_LIST_HEAD (&DhcpSb->Children, DHCP_PROTOCOL, Link);\r
436 ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);\r
437 }\r
438\r
439 if (DhcpSb->NumChildren != 0) {\r
440 Status = EFI_DEVICE_ERROR;\r
441 }\r
772db4bb 442 }\r
443\r
e48e37fc 444 gBS->RestoreTPL (OldTpl);\r
772db4bb 445\r
772db4bb 446 return Status;\r
447}\r
448\r
449\r
450/**\r
f9204641 451 Initialize a new DHCP instance.\r
772db4bb 452\r
453 @param DhcpSb The dhcp service instance\r
454 @param Instance The dhcp instance to initialize\r
455\r
772db4bb 456**/\r
457VOID\r
458DhcpInitProtocol (\r
f9204641 459 IN DHCP_SERVICE *DhcpSb,\r
460 IN OUT DHCP_PROTOCOL *Instance\r
772db4bb 461 )\r
462{\r
463 Instance->Signature = DHCP_PROTOCOL_SIGNATURE;\r
687a2e5f 464 CopyMem (&Instance->Dhcp4Protocol, &mDhcp4ProtocolTemplate, sizeof (Instance->Dhcp4Protocol));\r
e48e37fc 465 InitializeListHead (&Instance->Link);\r
772db4bb 466 Instance->Handle = NULL;\r
467 Instance->Service = DhcpSb;\r
468 Instance->InDestory = FALSE;\r
469 Instance->CompletionEvent = NULL;\r
470 Instance->RenewRebindEvent = NULL;\r
471 Instance->Token = NULL;\r
c4a62a12 472 Instance->UdpIo = NULL;\r
473 NetbufQueInit (&Instance->ResponseQueue);\r
772db4bb 474}\r
475\r
476\r
477/**\r
3e8c18da 478 Creates a child handle and installs a protocol.\r
479 \r
480 The CreateChild() function installs a protocol on ChildHandle. \r
481 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. \r
482 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.\r
772db4bb 483\r
3e8c18da 484 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
485 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,\r
486 then a new handle is created. If it is a pointer to an existing UEFI handle, \r
487 then the protocol is added to the existing UEFI handle.\r
772db4bb 488\r
3e8c18da 489 @retval EFI_SUCCES The protocol was added to ChildHandle.\r
f9204641 490 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.\r
491 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create\r
492 the child\r
493 @retval other The child handle was not created\r
772db4bb 494\r
495**/\r
496EFI_STATUS\r
497EFIAPI\r
498Dhcp4ServiceBindingCreateChild (\r
499 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
500 IN EFI_HANDLE *ChildHandle\r
501 )\r
502{\r
503 DHCP_SERVICE *DhcpSb;\r
504 DHCP_PROTOCOL *Instance;\r
505 EFI_STATUS Status;\r
506 EFI_TPL OldTpl;\r
507 VOID *Udp4;\r
508\r
509 if ((This == NULL) || (ChildHandle == NULL)) {\r
510 return EFI_INVALID_PARAMETER;\r
511 }\r
512\r
e48e37fc 513 Instance = AllocatePool (sizeof (*Instance));\r
772db4bb 514\r
515 if (Instance == NULL) {\r
516 return EFI_OUT_OF_RESOURCES;\r
517 }\r
518\r
519 DhcpSb = DHCP_SERVICE_FROM_THIS (This);\r
520 DhcpInitProtocol (DhcpSb, Instance);\r
521\r
522 //\r
523 // Install DHCP4 onto ChildHandle\r
524 //\r
525 Status = gBS->InstallMultipleProtocolInterfaces (\r
526 ChildHandle,\r
527 &gEfiDhcp4ProtocolGuid,\r
528 &Instance->Dhcp4Protocol,\r
529 NULL\r
530 );\r
531\r
532 if (EFI_ERROR (Status)) {\r
a4df47f1 533 FreePool (Instance);\r
772db4bb 534 return Status;\r
535 }\r
536\r
537 Instance->Handle = *ChildHandle;\r
538\r
539 //\r
540 // Open the Udp4 protocol BY_CHILD.\r
541 //\r
542 Status = gBS->OpenProtocol (\r
543 DhcpSb->UdpIo->UdpHandle,\r
544 &gEfiUdp4ProtocolGuid,\r
545 (VOID **) &Udp4,\r
546 gDhcp4DriverBinding.DriverBindingHandle,\r
547 Instance->Handle,\r
548 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
549 );\r
550 if (EFI_ERROR (Status)) {\r
551 gBS->UninstallMultipleProtocolInterfaces (\r
552 Instance->Handle,\r
553 &gEfiDhcp4ProtocolGuid,\r
554 &Instance->Dhcp4Protocol,\r
555 NULL\r
556 );\r
557\r
a4df47f1 558 FreePool (Instance);\r
772db4bb 559 return Status;\r
560 }\r
561\r
e48e37fc 562 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 563\r
e48e37fc 564 InsertTailList (&DhcpSb->Children, &Instance->Link);\r
772db4bb 565 DhcpSb->NumChildren++;\r
566\r
e48e37fc 567 gBS->RestoreTPL (OldTpl);\r
772db4bb 568\r
569 return EFI_SUCCESS;\r
570}\r
571\r
572\r
573/**\r
3e8c18da 574 Destroys a child handle with a protocol installed on it.\r
575 \r
576 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol \r
577 that was installed by CreateChild() from ChildHandle. If the removed protocol is the \r
578 last protocol on ChildHandle, then ChildHandle is destroyed.\r
772db4bb 579\r
3e8c18da 580 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
f9204641 581 @param ChildHandle Handle of the child to destroy\r
772db4bb 582\r
3e8c18da 583 @retval EFI_SUCCES The protocol was removed from ChildHandle.\r
584 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.\r
585 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.\r
586 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle\r
587 because its services are being used.\r
f9204641 588 @retval other The child handle was not destroyed\r
772db4bb 589\r
590**/\r
591EFI_STATUS\r
592EFIAPI\r
593Dhcp4ServiceBindingDestroyChild (\r
594 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
595 IN EFI_HANDLE ChildHandle\r
596 )\r
597{\r
598 DHCP_SERVICE *DhcpSb;\r
599 DHCP_PROTOCOL *Instance;\r
600 EFI_DHCP4_PROTOCOL *Dhcp;\r
601 EFI_TPL OldTpl;\r
602 EFI_STATUS Status;\r
603\r
604 if ((This == NULL) || (ChildHandle == NULL)) {\r
605 return EFI_INVALID_PARAMETER;\r
606 }\r
607\r
608 //\r
609 // Retrieve the private context data structures\r
610 //\r
611 Status = gBS->OpenProtocol (\r
612 ChildHandle,\r
613 &gEfiDhcp4ProtocolGuid,\r
614 (VOID **) &Dhcp,\r
615 gDhcp4DriverBinding.DriverBindingHandle,\r
616 ChildHandle,\r
617 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
618 );\r
619\r
620 if (EFI_ERROR (Status)) {\r
621 return EFI_UNSUPPORTED;\r
622 }\r
623\r
624 Instance = DHCP_INSTANCE_FROM_THIS (Dhcp);\r
625 DhcpSb = DHCP_SERVICE_FROM_THIS (This);\r
626\r
627 if (Instance->Service != DhcpSb) {\r
628 return EFI_INVALID_PARAMETER;\r
629 }\r
630\r
631 //\r
f9204641 632 // A child can be destroyed more than once. For example,\r
633 // Dhcp4DriverBindingStop will destroy all of its children.\r
772db4bb 634 // when caller driver is being stopped, it will destory the\r
635 // dhcp child it opens.\r
636 //\r
637 if (Instance->InDestory) {\r
638 return EFI_SUCCESS;\r
639 }\r
640\r
e48e37fc 641 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 642 Instance->InDestory = TRUE;\r
643\r
644 //\r
645 // Close the Udp4 protocol.\r
646 //\r
647 gBS->CloseProtocol (\r
648 DhcpSb->UdpIo->UdpHandle,\r
649 &gEfiUdp4ProtocolGuid,\r
650 gDhcp4DriverBinding.DriverBindingHandle,\r
651 ChildHandle\r
652 );\r
653\r
654 //\r
655 // Uninstall the DHCP4 protocol first to enable a top down destruction.\r
656 //\r
657 Status = gBS->UninstallProtocolInterface (\r
658 ChildHandle,\r
659 &gEfiDhcp4ProtocolGuid,\r
660 Dhcp\r
661 );\r
662\r
663 if (EFI_ERROR (Status)) {\r
664 Instance->InDestory = FALSE;\r
665\r
e48e37fc 666 gBS->RestoreTPL (OldTpl);\r
772db4bb 667 return Status;\r
668 }\r
669\r
670 if (DhcpSb->ActiveChild == Instance) {\r
671 DhcpYieldControl (DhcpSb);\r
672 }\r
673\r
e48e37fc 674 RemoveEntryList (&Instance->Link);\r
772db4bb 675 DhcpSb->NumChildren--;\r
676\r
e48e37fc 677 gBS->RestoreTPL (OldTpl);\r
772db4bb 678\r
a4df47f1 679 FreePool (Instance);\r
772db4bb 680 return EFI_SUCCESS;\r
681}\r