]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Dhcp4Dxe/Dhcp4Driver.c
MdeModulePkg: Fix DHCP4 driver hang issue in some case.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Dhcp4Dxe / Dhcp4Driver.c
CommitLineData
772db4bb 1/** @file\r
2\r
35bc88dd 3Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 4This program and the accompanying materials\r
772db4bb 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
e798cd87 117EFIAPI\r
772db4bb 118DhcpConfigUdpIo (\r
b45b45b2 119 IN UDP_IO *UdpIo,\r
772db4bb 120 IN VOID *Context\r
121 )\r
122{\r
123 EFI_UDP4_CONFIG_DATA UdpConfigData;\r
124\r
125 UdpConfigData.AcceptBroadcast = TRUE;\r
126 UdpConfigData.AcceptPromiscuous = FALSE;\r
127 UdpConfigData.AcceptAnyPort = FALSE;\r
128 UdpConfigData.AllowDuplicatePort = TRUE;\r
129 UdpConfigData.TypeOfService = 0;\r
130 UdpConfigData.TimeToLive = 64;\r
131 UdpConfigData.DoNotFragment = FALSE;\r
132 UdpConfigData.ReceiveTimeout = 0;\r
133 UdpConfigData.TransmitTimeout = 0;\r
134\r
135 UdpConfigData.UseDefaultAddress = FALSE;\r
136 UdpConfigData.StationPort = DHCP_CLIENT_PORT;\r
137 UdpConfigData.RemotePort = DHCP_SERVER_PORT;\r
138\r
e48e37fc 139 ZeroMem (&UdpConfigData.StationAddress, sizeof (EFI_IPv4_ADDRESS));\r
140 ZeroMem (&UdpConfigData.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
141 ZeroMem (&UdpConfigData.RemoteAddress, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 142\r
b45b45b2 143 return UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfigData);;\r
772db4bb 144}\r
145\r
146\r
147\r
148/**\r
75dce340 149 Destroy the DHCP service. The Dhcp4 service may be partly initialized,\r
f9204641 150 or partly destroyed. If a resource is destroyed, it is marked as so in\r
151 case the destroy failed and being called again later.\r
772db4bb 152\r
75dce340 153 @param[in] DhcpSb The DHCP service instance to destroy.\r
772db4bb 154\r
f9204641 155 @retval EFI_SUCCESS Always return success.\r
772db4bb 156\r
157**/\r
158EFI_STATUS\r
159Dhcp4CloseService (\r
160 IN DHCP_SERVICE *DhcpSb\r
161 )\r
162{\r
163 DhcpCleanLease (DhcpSb);\r
164\r
165 if (DhcpSb->UdpIo != NULL) {\r
b45b45b2 166 UdpIoFreeIo (DhcpSb->UdpIo);\r
772db4bb 167 DhcpSb->UdpIo = NULL;\r
168 }\r
169\r
170 if (DhcpSb->Timer != NULL) {\r
171 gBS->SetTimer (DhcpSb->Timer, TimerCancel, 0);\r
172 gBS->CloseEvent (DhcpSb->Timer);\r
173\r
174 DhcpSb->Timer = NULL;\r
175 }\r
176\r
177 return EFI_SUCCESS;\r
178}\r
179\r
180\r
181\r
182/**\r
183 Create a new DHCP service binding instance for the controller.\r
184\r
3e8c18da 185 @param[in] Controller The controller to install DHCP service binding\r
186 protocol onto\r
187 @param[in] ImageHandle The driver's image handle\r
188 @param[out] Service The variable to receive the created DHCP service\r
189 instance.\r
772db4bb 190\r
191 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource .\r
192 @retval EFI_SUCCESS The DHCP service instance is created.\r
f9204641 193 @retval other Other error occurs.\r
772db4bb 194\r
195**/\r
196EFI_STATUS\r
197Dhcp4CreateService (\r
198 IN EFI_HANDLE Controller,\r
199 IN EFI_HANDLE ImageHandle,\r
200 OUT DHCP_SERVICE **Service\r
201 )\r
202{\r
203 DHCP_SERVICE *DhcpSb;\r
204 EFI_STATUS Status;\r
205\r
206 *Service = NULL;\r
e48e37fc 207 DhcpSb = AllocateZeroPool (sizeof (DHCP_SERVICE));\r
772db4bb 208\r
209 if (DhcpSb == NULL) {\r
210 return EFI_OUT_OF_RESOURCES;\r
211 }\r
212\r
213 DhcpSb->Signature = DHCP_SERVICE_SIGNATURE;\r
772db4bb 214 DhcpSb->ServiceState = DHCP_UNCONFIGED;\r
772db4bb 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
216f7970 356/**\r
357 Callback function which provided by user to remove one node in NetDestroyLinkList process.\r
358 \r
359 @param[in] Entry The entry to be removed.\r
360 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.\r
361\r
362 @retval EFI_SUCCESS The entry has been removed successfully.\r
363 @retval Others Fail to remove the entry.\r
364\r
365**/\r
366EFI_STATUS\r
1f7eb561 367EFIAPI\r
216f7970 368Dhcp4DestroyChildEntry (\r
369 IN LIST_ENTRY *Entry,\r
370 IN VOID *Context\r
1f7eb561 371 )\r
216f7970 372{\r
373 DHCP_PROTOCOL *Instance;\r
374 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;\r
375\r
376 if (Entry == NULL || Context == NULL) {\r
377 return EFI_INVALID_PARAMETER;\r
378 }\r
379\r
380 Instance = NET_LIST_USER_STRUCT_S (Entry, DHCP_PROTOCOL, Link, DHCP_PROTOCOL_SIGNATURE);\r
381 ServiceBinding = (EFI_SERVICE_BINDING_PROTOCOL *) Context;\r
382\r
383 return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);\r
384}\r
385\r
772db4bb 386\r
387/**\r
f9204641 388 Stop this driver on ControllerHandle. This service is called by the\r
389 EFI boot service DisconnectController(). In order to\r
390 make drivers as small as possible, there are a few calling\r
391 restrictions for this service. DisconnectController()\r
392 must follow these calling restrictions. If any other agent wishes\r
393 to call Stop() it must also follow these calling restrictions.\r
e2851998 394\r
3e8c18da 395 @param[in] This Protocol instance pointer.\r
396 @param[in] ControllerHandle Handle of device to stop driver on\r
397 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
398 children is zero stop the entire bus driver.\r
399 @param[in] ChildHandleBuffer List of Child Handles to Stop.\r
f9204641 400\r
401 @retval EFI_SUCCESS This driver is removed ControllerHandle\r
402 @retval other This driver was not removed from this device\r
772db4bb 403\r
404**/\r
405EFI_STATUS\r
406EFIAPI\r
407Dhcp4DriverBindingStop (\r
408 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
409 IN EFI_HANDLE ControllerHandle,\r
410 IN UINTN NumberOfChildren,\r
411 IN EFI_HANDLE *ChildHandleBuffer\r
412 )\r
413{\r
414 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;\r
415 DHCP_SERVICE *DhcpSb;\r
772db4bb 416 EFI_HANDLE NicHandle;\r
417 EFI_STATUS Status;\r
216f7970 418 LIST_ENTRY *List;\r
419 UINTN ListLength;\r
772db4bb 420\r
421 //\r
422 // DHCP driver opens UDP child, So, the ControllerHandle is the\r
423 // UDP child handle. locate the Nic handle first.\r
424 //\r
425 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp4ProtocolGuid);\r
426\r
427 if (NicHandle == NULL) {\r
216f7970 428 return EFI_SUCCESS;\r
772db4bb 429 }\r
430\r
431 Status = gBS->OpenProtocol (\r
432 NicHandle,\r
433 &gEfiDhcp4ServiceBindingProtocolGuid,\r
434 (VOID **) &ServiceBinding,\r
435 This->DriverBindingHandle,\r
436 NicHandle,\r
437 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
438 );\r
439\r
440 if (EFI_ERROR (Status)) {\r
441 return EFI_DEVICE_ERROR;\r
442 }\r
443\r
444 DhcpSb = DHCP_SERVICE_FROM_THIS (ServiceBinding);\r
216f7970 445 if (!IsListEmpty (&DhcpSb->Children)) {\r
446 //\r
447 // Destroy all the children instances before destory the service.\r
448 // \r
449 List = &DhcpSb->Children;\r
450 Status = NetDestroyLinkList (\r
451 List,\r
452 Dhcp4DestroyChildEntry,\r
453 ServiceBinding,\r
454 &ListLength\r
455 );\r
456 if (EFI_ERROR (Status) || ListLength != 0) {\r
457 Status = EFI_DEVICE_ERROR;\r
458 }\r
772db4bb 459 }\r
460\r
216f7970 461 if (NumberOfChildren == 0 && !IsListEmpty (&DhcpSb->Children)) {\r
462 Status = EFI_DEVICE_ERROR;\r
463 }\r
772db4bb 464\r
216f7970 465 if (NumberOfChildren == 0 && IsListEmpty (&DhcpSb->Children)) {\r
466 //\r
467 // Destroy the service itself if no child instance left.\r
468 //\r
75dce340 469 DhcpSb->ServiceState = DHCP_DESTROY;\r
772db4bb 470\r
c4a62a12 471 gBS->UninstallProtocolInterface (\r
472 NicHandle,\r
473 &gEfiDhcp4ServiceBindingProtocolGuid,\r
474 ServiceBinding\r
475 );\r
772db4bb 476\r
c4a62a12 477 Dhcp4CloseService (DhcpSb);\r
478\r
216f7970 479 if (gDhcpControllerNameTable != NULL) {\r
480 FreeUnicodeStringTable (gDhcpControllerNameTable);\r
481 gDhcpControllerNameTable = NULL;\r
c4a62a12 482 }\r
216f7970 483 FreePool (DhcpSb);\r
484 \r
485 Status = EFI_SUCCESS;\r
772db4bb 486 }\r
487\r
772db4bb 488 return Status;\r
489}\r
490\r
491\r
492/**\r
f9204641 493 Initialize a new DHCP instance.\r
772db4bb 494\r
495 @param DhcpSb The dhcp service instance\r
496 @param Instance The dhcp instance to initialize\r
497\r
772db4bb 498**/\r
499VOID\r
500DhcpInitProtocol (\r
f9204641 501 IN DHCP_SERVICE *DhcpSb,\r
502 IN OUT DHCP_PROTOCOL *Instance\r
772db4bb 503 )\r
504{\r
505 Instance->Signature = DHCP_PROTOCOL_SIGNATURE;\r
687a2e5f 506 CopyMem (&Instance->Dhcp4Protocol, &mDhcp4ProtocolTemplate, sizeof (Instance->Dhcp4Protocol));\r
e48e37fc 507 InitializeListHead (&Instance->Link);\r
772db4bb 508 Instance->Handle = NULL;\r
509 Instance->Service = DhcpSb;\r
75dce340 510 Instance->InDestroy = FALSE;\r
772db4bb 511 Instance->CompletionEvent = NULL;\r
512 Instance->RenewRebindEvent = NULL;\r
513 Instance->Token = NULL;\r
c4a62a12 514 Instance->UdpIo = NULL;\r
842d83d6 515 Instance->ElaspedTime = 0;\r
c4a62a12 516 NetbufQueInit (&Instance->ResponseQueue);\r
772db4bb 517}\r
518\r
519\r
520/**\r
3e8c18da 521 Creates a child handle and installs a protocol.\r
e2851998 522\r
523 The CreateChild() function installs a protocol on ChildHandle.\r
524 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.\r
3e8c18da 525 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.\r
772db4bb 526\r
3e8c18da 527 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
528 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,\r
e2851998 529 then a new handle is created. If it is a pointer to an existing UEFI handle,\r
3e8c18da 530 then the protocol is added to the existing UEFI handle.\r
772db4bb 531\r
3e8c18da 532 @retval EFI_SUCCES The protocol was added to ChildHandle.\r
f9204641 533 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.\r
534 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create\r
535 the child\r
536 @retval other The child handle was not created\r
772db4bb 537\r
538**/\r
539EFI_STATUS\r
540EFIAPI\r
541Dhcp4ServiceBindingCreateChild (\r
542 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
543 IN EFI_HANDLE *ChildHandle\r
544 )\r
545{\r
546 DHCP_SERVICE *DhcpSb;\r
547 DHCP_PROTOCOL *Instance;\r
548 EFI_STATUS Status;\r
549 EFI_TPL OldTpl;\r
550 VOID *Udp4;\r
551\r
552 if ((This == NULL) || (ChildHandle == NULL)) {\r
553 return EFI_INVALID_PARAMETER;\r
554 }\r
555\r
e48e37fc 556 Instance = AllocatePool (sizeof (*Instance));\r
772db4bb 557\r
558 if (Instance == NULL) {\r
559 return EFI_OUT_OF_RESOURCES;\r
560 }\r
561\r
562 DhcpSb = DHCP_SERVICE_FROM_THIS (This);\r
563 DhcpInitProtocol (DhcpSb, Instance);\r
564\r
565 //\r
566 // Install DHCP4 onto ChildHandle\r
567 //\r
568 Status = gBS->InstallMultipleProtocolInterfaces (\r
569 ChildHandle,\r
570 &gEfiDhcp4ProtocolGuid,\r
571 &Instance->Dhcp4Protocol,\r
572 NULL\r
573 );\r
574\r
575 if (EFI_ERROR (Status)) {\r
a4df47f1 576 FreePool (Instance);\r
772db4bb 577 return Status;\r
578 }\r
579\r
580 Instance->Handle = *ChildHandle;\r
581\r
582 //\r
583 // Open the Udp4 protocol BY_CHILD.\r
584 //\r
585 Status = gBS->OpenProtocol (\r
586 DhcpSb->UdpIo->UdpHandle,\r
587 &gEfiUdp4ProtocolGuid,\r
588 (VOID **) &Udp4,\r
589 gDhcp4DriverBinding.DriverBindingHandle,\r
590 Instance->Handle,\r
591 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
592 );\r
593 if (EFI_ERROR (Status)) {\r
594 gBS->UninstallMultipleProtocolInterfaces (\r
595 Instance->Handle,\r
596 &gEfiDhcp4ProtocolGuid,\r
597 &Instance->Dhcp4Protocol,\r
598 NULL\r
599 );\r
600\r
a4df47f1 601 FreePool (Instance);\r
772db4bb 602 return Status;\r
603 }\r
604\r
e48e37fc 605 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 606\r
e48e37fc 607 InsertTailList (&DhcpSb->Children, &Instance->Link);\r
772db4bb 608 DhcpSb->NumChildren++;\r
609\r
e48e37fc 610 gBS->RestoreTPL (OldTpl);\r
772db4bb 611\r
612 return EFI_SUCCESS;\r
613}\r
614\r
615\r
616/**\r
3e8c18da 617 Destroys a child handle with a protocol installed on it.\r
e2851998 618\r
619 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol\r
620 that was installed by CreateChild() from ChildHandle. If the removed protocol is the\r
3e8c18da 621 last protocol on ChildHandle, then ChildHandle is destroyed.\r
772db4bb 622\r
3e8c18da 623 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
f9204641 624 @param ChildHandle Handle of the child to destroy\r
772db4bb 625\r
3e8c18da 626 @retval EFI_SUCCES The protocol was removed from ChildHandle.\r
627 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.\r
284ee2e8 628 @retval EFI_INVALID_PARAMETER Child handle is NULL.\r
3e8c18da 629 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle\r
630 because its services are being used.\r
f9204641 631 @retval other The child handle was not destroyed\r
772db4bb 632\r
633**/\r
634EFI_STATUS\r
635EFIAPI\r
636Dhcp4ServiceBindingDestroyChild (\r
637 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
638 IN EFI_HANDLE ChildHandle\r
639 )\r
640{\r
641 DHCP_SERVICE *DhcpSb;\r
642 DHCP_PROTOCOL *Instance;\r
643 EFI_DHCP4_PROTOCOL *Dhcp;\r
644 EFI_TPL OldTpl;\r
645 EFI_STATUS Status;\r
646\r
647 if ((This == NULL) || (ChildHandle == NULL)) {\r
648 return EFI_INVALID_PARAMETER;\r
649 }\r
650\r
651 //\r
652 // Retrieve the private context data structures\r
653 //\r
654 Status = gBS->OpenProtocol (\r
655 ChildHandle,\r
656 &gEfiDhcp4ProtocolGuid,\r
657 (VOID **) &Dhcp,\r
658 gDhcp4DriverBinding.DriverBindingHandle,\r
659 ChildHandle,\r
660 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
661 );\r
662\r
663 if (EFI_ERROR (Status)) {\r
664 return EFI_UNSUPPORTED;\r
665 }\r
666\r
667 Instance = DHCP_INSTANCE_FROM_THIS (Dhcp);\r
668 DhcpSb = DHCP_SERVICE_FROM_THIS (This);\r
669\r
670 if (Instance->Service != DhcpSb) {\r
671 return EFI_INVALID_PARAMETER;\r
672 }\r
673\r
674 //\r
f9204641 675 // A child can be destroyed more than once. For example,\r
676 // Dhcp4DriverBindingStop will destroy all of its children.\r
75dce340 677 // when caller driver is being stopped, it will destroy the\r
772db4bb 678 // dhcp child it opens.\r
679 //\r
75dce340 680 if (Instance->InDestroy) {\r
772db4bb 681 return EFI_SUCCESS;\r
682 }\r
683\r
e48e37fc 684 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
75dce340 685 Instance->InDestroy = TRUE;\r
772db4bb 686\r
687 //\r
688 // Close the Udp4 protocol.\r
689 //\r
690 gBS->CloseProtocol (\r
691 DhcpSb->UdpIo->UdpHandle,\r
692 &gEfiUdp4ProtocolGuid,\r
693 gDhcp4DriverBinding.DriverBindingHandle,\r
694 ChildHandle\r
695 );\r
696\r
697 //\r
698 // Uninstall the DHCP4 protocol first to enable a top down destruction.\r
699 //\r
216f7970 700 gBS->RestoreTPL (OldTpl);\r
772db4bb 701 Status = gBS->UninstallProtocolInterface (\r
702 ChildHandle,\r
703 &gEfiDhcp4ProtocolGuid,\r
704 Dhcp\r
705 );\r
216f7970 706 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 707 if (EFI_ERROR (Status)) {\r
75dce340 708 Instance->InDestroy = FALSE;\r
772db4bb 709\r
e48e37fc 710 gBS->RestoreTPL (OldTpl);\r
772db4bb 711 return Status;\r
712 }\r
713\r
714 if (DhcpSb->ActiveChild == Instance) {\r
715 DhcpYieldControl (DhcpSb);\r
716 }\r
717\r
e48e37fc 718 RemoveEntryList (&Instance->Link);\r
772db4bb 719 DhcpSb->NumChildren--;\r
720\r
35bc88dd
WJ
721 if (Instance->UdpIo != NULL) {\r
722 UdpIoCleanIo (Instance->UdpIo);\r
723 gBS->CloseProtocol (\r
724 Instance->UdpIo->UdpHandle,\r
725 &gEfiUdp4ProtocolGuid,\r
726 Instance->Service->Image,\r
727 Instance->Handle\r
728 );\r
729 UdpIoFreeIo (Instance->UdpIo);\r
730 Instance->UdpIo = NULL;\r
731 Instance->Token = NULL;\r
732 }\r
733\r
e48e37fc 734 gBS->RestoreTPL (OldTpl);\r
772db4bb 735\r
a4df47f1 736 FreePool (Instance);\r
772db4bb 737 return EFI_SUCCESS;\r
738}\r