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