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