]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Dhcp4Dxe/Dhcp4Driver.c
add security check.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Dhcp4Dxe / Dhcp4Driver.c
CommitLineData
772db4bb 1/** @file\r
2\r
3e8c18da 3Copyright (c) 2006 - 2008, 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
e48e37fc 256 gBS->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
313 Status = gBS->SetTimer (DhcpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);\r
314\r
315 if (EFI_ERROR (Status)) {\r
316 goto ON_ERROR;\r
317 }\r
318\r
319 //\r
320 // Install the Dhcp4ServiceBinding Protocol onto ControlerHandle\r
321 //\r
322 Status = gBS->InstallMultipleProtocolInterfaces (\r
323 &ControllerHandle,\r
324 &gEfiDhcp4ServiceBindingProtocolGuid,\r
325 &DhcpSb->ServiceBinding,\r
326 NULL\r
327 );\r
328\r
329 if (EFI_ERROR (Status)) {\r
330 goto ON_ERROR;\r
331 }\r
332\r
333 return Status;\r
334\r
335ON_ERROR:\r
336 Dhcp4CloseService (DhcpSb);\r
e48e37fc 337 gBS->FreePool (DhcpSb);\r
772db4bb 338 return Status;\r
339}\r
340\r
341\r
342/**\r
f9204641 343 Stop this driver on ControllerHandle. This service is called by the\r
344 EFI boot service DisconnectController(). In order to\r
345 make drivers as small as possible, there are a few calling\r
346 restrictions for this service. DisconnectController()\r
347 must follow these calling restrictions. If any other agent wishes\r
348 to call Stop() it must also follow these calling restrictions.\r
349 \r
3e8c18da 350 @param[in] This Protocol instance pointer.\r
351 @param[in] ControllerHandle Handle of device to stop driver on\r
352 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
353 children is zero stop the entire bus driver.\r
354 @param[in] ChildHandleBuffer List of Child Handles to Stop.\r
f9204641 355\r
356 @retval EFI_SUCCESS This driver is removed ControllerHandle\r
357 @retval other This driver was not removed from this device\r
772db4bb 358\r
359**/\r
360EFI_STATUS\r
361EFIAPI\r
362Dhcp4DriverBindingStop (\r
363 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
364 IN EFI_HANDLE ControllerHandle,\r
365 IN UINTN NumberOfChildren,\r
366 IN EFI_HANDLE *ChildHandleBuffer\r
367 )\r
368{\r
369 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;\r
370 DHCP_SERVICE *DhcpSb;\r
371 DHCP_PROTOCOL *Instance;\r
372 EFI_HANDLE NicHandle;\r
373 EFI_STATUS Status;\r
374 EFI_TPL OldTpl;\r
375\r
376 //\r
377 // DHCP driver opens UDP child, So, the ControllerHandle is the\r
378 // UDP child handle. locate the Nic handle first.\r
379 //\r
380 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp4ProtocolGuid);\r
381\r
382 if (NicHandle == NULL) {\r
c4a62a12 383 return EFI_DEVICE_ERROR;\r
772db4bb 384 }\r
385\r
386 Status = gBS->OpenProtocol (\r
387 NicHandle,\r
388 &gEfiDhcp4ServiceBindingProtocolGuid,\r
389 (VOID **) &ServiceBinding,\r
390 This->DriverBindingHandle,\r
391 NicHandle,\r
392 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
393 );\r
394\r
395 if (EFI_ERROR (Status)) {\r
396 return EFI_DEVICE_ERROR;\r
397 }\r
398\r
399 DhcpSb = DHCP_SERVICE_FROM_THIS (ServiceBinding);\r
400\r
401 if (DhcpSb->InDestory) {\r
402 return EFI_SUCCESS;\r
403 }\r
404\r
e48e37fc 405 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 406\r
c4a62a12 407 if (NumberOfChildren == 0) {\r
772db4bb 408\r
c4a62a12 409 DhcpSb->InDestory = TRUE;\r
410 DhcpSb->ServiceState = DHCP_DESTORY;\r
772db4bb 411\r
c4a62a12 412 gBS->UninstallProtocolInterface (\r
413 NicHandle,\r
414 &gEfiDhcp4ServiceBindingProtocolGuid,\r
415 ServiceBinding\r
416 );\r
772db4bb 417\r
c4a62a12 418 Dhcp4CloseService (DhcpSb);\r
419\r
e48e37fc 420 gBS->FreePool (DhcpSb);\r
c4a62a12 421 } else {\r
422 //\r
423 // Don't use NET_LIST_FOR_EACH_SAFE here, Dhcp4ServiceBindingDestoryChild\r
424 // may cause other child to be deleted.\r
425 //\r
e48e37fc 426 while (!IsListEmpty (&DhcpSb->Children)) {\r
c4a62a12 427 Instance = NET_LIST_HEAD (&DhcpSb->Children, DHCP_PROTOCOL, Link);\r
428 ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);\r
429 }\r
430\r
431 if (DhcpSb->NumChildren != 0) {\r
432 Status = EFI_DEVICE_ERROR;\r
433 }\r
772db4bb 434 }\r
435\r
e48e37fc 436 gBS->RestoreTPL (OldTpl);\r
772db4bb 437\r
772db4bb 438 return Status;\r
439}\r
440\r
441\r
442/**\r
f9204641 443 Initialize a new DHCP instance.\r
772db4bb 444\r
445 @param DhcpSb The dhcp service instance\r
446 @param Instance The dhcp instance to initialize\r
447\r
772db4bb 448**/\r
449VOID\r
450DhcpInitProtocol (\r
f9204641 451 IN DHCP_SERVICE *DhcpSb,\r
452 IN OUT DHCP_PROTOCOL *Instance\r
772db4bb 453 )\r
454{\r
455 Instance->Signature = DHCP_PROTOCOL_SIGNATURE;\r
687a2e5f 456 CopyMem (&Instance->Dhcp4Protocol, &mDhcp4ProtocolTemplate, sizeof (Instance->Dhcp4Protocol));\r
e48e37fc 457 InitializeListHead (&Instance->Link);\r
772db4bb 458 Instance->Handle = NULL;\r
459 Instance->Service = DhcpSb;\r
460 Instance->InDestory = FALSE;\r
461 Instance->CompletionEvent = NULL;\r
462 Instance->RenewRebindEvent = NULL;\r
463 Instance->Token = NULL;\r
c4a62a12 464 Instance->UdpIo = NULL;\r
465 NetbufQueInit (&Instance->ResponseQueue);\r
772db4bb 466}\r
467\r
468\r
469/**\r
3e8c18da 470 Creates a child handle and installs a protocol.\r
471 \r
472 The CreateChild() function installs a protocol on ChildHandle. \r
473 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle. \r
474 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.\r
772db4bb 475\r
3e8c18da 476 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
477 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,\r
478 then a new handle is created. If it is a pointer to an existing UEFI handle, \r
479 then the protocol is added to the existing UEFI handle.\r
772db4bb 480\r
3e8c18da 481 @retval EFI_SUCCES The protocol was added to ChildHandle.\r
f9204641 482 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.\r
483 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create\r
484 the child\r
485 @retval other The child handle was not created\r
772db4bb 486\r
487**/\r
488EFI_STATUS\r
489EFIAPI\r
490Dhcp4ServiceBindingCreateChild (\r
491 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
492 IN EFI_HANDLE *ChildHandle\r
493 )\r
494{\r
495 DHCP_SERVICE *DhcpSb;\r
496 DHCP_PROTOCOL *Instance;\r
497 EFI_STATUS Status;\r
498 EFI_TPL OldTpl;\r
499 VOID *Udp4;\r
500\r
501 if ((This == NULL) || (ChildHandle == NULL)) {\r
502 return EFI_INVALID_PARAMETER;\r
503 }\r
504\r
e48e37fc 505 Instance = AllocatePool (sizeof (*Instance));\r
772db4bb 506\r
507 if (Instance == NULL) {\r
508 return EFI_OUT_OF_RESOURCES;\r
509 }\r
510\r
511 DhcpSb = DHCP_SERVICE_FROM_THIS (This);\r
512 DhcpInitProtocol (DhcpSb, Instance);\r
513\r
514 //\r
515 // Install DHCP4 onto ChildHandle\r
516 //\r
517 Status = gBS->InstallMultipleProtocolInterfaces (\r
518 ChildHandle,\r
519 &gEfiDhcp4ProtocolGuid,\r
520 &Instance->Dhcp4Protocol,\r
521 NULL\r
522 );\r
523\r
524 if (EFI_ERROR (Status)) {\r
e48e37fc 525 gBS->FreePool (Instance);\r
772db4bb 526 return Status;\r
527 }\r
528\r
529 Instance->Handle = *ChildHandle;\r
530\r
531 //\r
532 // Open the Udp4 protocol BY_CHILD.\r
533 //\r
534 Status = gBS->OpenProtocol (\r
535 DhcpSb->UdpIo->UdpHandle,\r
536 &gEfiUdp4ProtocolGuid,\r
537 (VOID **) &Udp4,\r
538 gDhcp4DriverBinding.DriverBindingHandle,\r
539 Instance->Handle,\r
540 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
541 );\r
542 if (EFI_ERROR (Status)) {\r
543 gBS->UninstallMultipleProtocolInterfaces (\r
544 Instance->Handle,\r
545 &gEfiDhcp4ProtocolGuid,\r
546 &Instance->Dhcp4Protocol,\r
547 NULL\r
548 );\r
549\r
e48e37fc 550 gBS->FreePool (Instance);\r
772db4bb 551 return Status;\r
552 }\r
553\r
e48e37fc 554 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 555\r
e48e37fc 556 InsertTailList (&DhcpSb->Children, &Instance->Link);\r
772db4bb 557 DhcpSb->NumChildren++;\r
558\r
e48e37fc 559 gBS->RestoreTPL (OldTpl);\r
772db4bb 560\r
561 return EFI_SUCCESS;\r
562}\r
563\r
564\r
565/**\r
3e8c18da 566 Destroys a child handle with a protocol installed on it.\r
567 \r
568 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol \r
569 that was installed by CreateChild() from ChildHandle. If the removed protocol is the \r
570 last protocol on ChildHandle, then ChildHandle is destroyed.\r
772db4bb 571\r
3e8c18da 572 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
f9204641 573 @param ChildHandle Handle of the child to destroy\r
772db4bb 574\r
3e8c18da 575 @retval EFI_SUCCES The protocol was removed from ChildHandle.\r
576 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.\r
577 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.\r
578 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle\r
579 because its services are being used.\r
f9204641 580 @retval other The child handle was not destroyed\r
772db4bb 581\r
582**/\r
583EFI_STATUS\r
584EFIAPI\r
585Dhcp4ServiceBindingDestroyChild (\r
586 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
587 IN EFI_HANDLE ChildHandle\r
588 )\r
589{\r
590 DHCP_SERVICE *DhcpSb;\r
591 DHCP_PROTOCOL *Instance;\r
592 EFI_DHCP4_PROTOCOL *Dhcp;\r
593 EFI_TPL OldTpl;\r
594 EFI_STATUS Status;\r
595\r
596 if ((This == NULL) || (ChildHandle == NULL)) {\r
597 return EFI_INVALID_PARAMETER;\r
598 }\r
599\r
600 //\r
601 // Retrieve the private context data structures\r
602 //\r
603 Status = gBS->OpenProtocol (\r
604 ChildHandle,\r
605 &gEfiDhcp4ProtocolGuid,\r
606 (VOID **) &Dhcp,\r
607 gDhcp4DriverBinding.DriverBindingHandle,\r
608 ChildHandle,\r
609 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
610 );\r
611\r
612 if (EFI_ERROR (Status)) {\r
613 return EFI_UNSUPPORTED;\r
614 }\r
615\r
616 Instance = DHCP_INSTANCE_FROM_THIS (Dhcp);\r
617 DhcpSb = DHCP_SERVICE_FROM_THIS (This);\r
618\r
619 if (Instance->Service != DhcpSb) {\r
620 return EFI_INVALID_PARAMETER;\r
621 }\r
622\r
623 //\r
f9204641 624 // A child can be destroyed more than once. For example,\r
625 // Dhcp4DriverBindingStop will destroy all of its children.\r
772db4bb 626 // when caller driver is being stopped, it will destory the\r
627 // dhcp child it opens.\r
628 //\r
629 if (Instance->InDestory) {\r
630 return EFI_SUCCESS;\r
631 }\r
632\r
e48e37fc 633 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 634 Instance->InDestory = TRUE;\r
635\r
636 //\r
637 // Close the Udp4 protocol.\r
638 //\r
639 gBS->CloseProtocol (\r
640 DhcpSb->UdpIo->UdpHandle,\r
641 &gEfiUdp4ProtocolGuid,\r
642 gDhcp4DriverBinding.DriverBindingHandle,\r
643 ChildHandle\r
644 );\r
645\r
646 //\r
647 // Uninstall the DHCP4 protocol first to enable a top down destruction.\r
648 //\r
649 Status = gBS->UninstallProtocolInterface (\r
650 ChildHandle,\r
651 &gEfiDhcp4ProtocolGuid,\r
652 Dhcp\r
653 );\r
654\r
655 if (EFI_ERROR (Status)) {\r
656 Instance->InDestory = FALSE;\r
657\r
e48e37fc 658 gBS->RestoreTPL (OldTpl);\r
772db4bb 659 return Status;\r
660 }\r
661\r
662 if (DhcpSb->ActiveChild == Instance) {\r
663 DhcpYieldControl (DhcpSb);\r
664 }\r
665\r
e48e37fc 666 RemoveEntryList (&Instance->Link);\r
772db4bb 667 DhcpSb->NumChildren--;\r
668\r
e48e37fc 669 gBS->RestoreTPL (OldTpl);\r
772db4bb 670\r
e48e37fc 671 gBS->FreePool (Instance);\r
772db4bb 672 return EFI_SUCCESS;\r
673}\r