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