]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Dhcp4Dxe/Dhcp4Driver.c
NetworkPkg: Apply uncrustify changes
[mirror_edk2.git] / NetworkPkg / Dhcp4Dxe / Dhcp4Driver.c
CommitLineData
772db4bb 1/** @file\r
2\r
d1102dba 3Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 4SPDX-License-Identifier: BSD-2-Clause-Patent\r
772db4bb 5\r
772db4bb 6**/\r
7\r
8#include "Dhcp4Impl.h"\r
9#include "Dhcp4Driver.h"\r
10\r
d1050b9d 11EFI_DRIVER_BINDING_PROTOCOL gDhcp4DriverBinding = {\r
772db4bb 12 Dhcp4DriverBindingSupported,\r
13 Dhcp4DriverBindingStart,\r
14 Dhcp4DriverBindingStop,\r
15 0xa,\r
16 NULL,\r
17 NULL\r
18};\r
19\r
d1050b9d 20EFI_SERVICE_BINDING_PROTOCOL mDhcp4ServiceBindingTemplate = {\r
772db4bb 21 Dhcp4ServiceBindingCreateChild,\r
22 Dhcp4ServiceBindingDestroyChild\r
23};\r
24\r
7bce0c5a 25/**\r
f9204641 26 This is the declaration of an EFI image entry point. This entry point is\r
27 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including\r
28 both device drivers and bus drivers.\r
e2851998 29\r
7bce0c5a 30 Entry point of the DHCP driver to install various protocols.\r
31\r
3e8c18da 32 @param[in] ImageHandle The firmware allocated handle for the UEFI image.\r
33 @param[in] SystemTable A pointer to the EFI System Table.\r
7bce0c5a 34\r
f9204641 35 @retval EFI_SUCCESS The operation completed successfully.\r
36 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
772db4bb 37\r
7bce0c5a 38**/\r
772db4bb 39EFI_STATUS\r
40EFIAPI\r
41Dhcp4DriverEntryPoint (\r
d1050b9d
MK
42 IN EFI_HANDLE ImageHandle,\r
43 IN EFI_SYSTEM_TABLE *SystemTable\r
772db4bb 44 )\r
772db4bb 45{\r
83cbd279 46 return EfiLibInstallDriverBindingComponentName2 (\r
772db4bb 47 ImageHandle,\r
48 SystemTable,\r
49 &gDhcp4DriverBinding,\r
50 ImageHandle,\r
51 &gDhcp4ComponentName,\r
83cbd279 52 &gDhcp4ComponentName2\r
772db4bb 53 );\r
54}\r
55\r
772db4bb 56/**\r
f9204641 57 Test to see if this driver supports ControllerHandle. This service\r
58 is called by the EFI boot service ConnectController(). In\r
59 order to make drivers as small as possible, there are a few calling\r
60 restrictions for this service. ConnectController() must\r
61 follow these calling restrictions. If any other agent wishes to call\r
62 Supported() it must also follow these calling restrictions.\r
63\r
3e8c18da 64 @param[in] This Protocol instance pointer.\r
65 @param[in] ControllerHandle Handle of device to test\r
66 @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r
67 device to start.\r
f9204641 68\r
69 @retval EFI_SUCCESS This driver supports this device\r
70 @retval EFI_ALREADY_STARTED This driver is already running on this device\r
71 @retval other This driver does not support this device\r
772db4bb 72\r
73**/\r
74EFI_STATUS\r
75EFIAPI\r
76Dhcp4DriverBindingSupported (\r
77 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
78 IN EFI_HANDLE ControllerHandle,\r
79 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
80 )\r
81{\r
82 EFI_STATUS Status;\r
83\r
84 Status = gBS->OpenProtocol (\r
85 ControllerHandle,\r
86 &gEfiUdp4ServiceBindingProtocolGuid,\r
87 NULL,\r
88 This->DriverBindingHandle,\r
89 ControllerHandle,\r
90 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
91 );\r
92\r
93 return Status;\r
94}\r
95\r
772db4bb 96/**\r
97 Configure the default UDP child to receive all the DHCP traffics\r
98 on this network interface.\r
99\r
b45b45b2 100 @param[in] UdpIo The UDP IO to configure\r
3e8c18da 101 @param[in] Context The context to the function\r
772db4bb 102\r
b45b45b2 103 @retval EFI_SUCCESS The UDP IO is successfully configured.\r
772db4bb 104 @retval Others Failed to configure the UDP child.\r
105\r
106**/\r
107EFI_STATUS\r
e798cd87 108EFIAPI\r
772db4bb 109DhcpConfigUdpIo (\r
d1050b9d
MK
110 IN UDP_IO *UdpIo,\r
111 IN VOID *Context\r
772db4bb 112 )\r
113{\r
d1050b9d
MK
114 EFI_UDP4_CONFIG_DATA UdpConfigData;\r
115\r
116 UdpConfigData.AcceptBroadcast = TRUE;\r
117 UdpConfigData.AcceptPromiscuous = FALSE;\r
118 UdpConfigData.AcceptAnyPort = FALSE;\r
119 UdpConfigData.AllowDuplicatePort = TRUE;\r
120 UdpConfigData.TypeOfService = 0;\r
121 UdpConfigData.TimeToLive = 64;\r
122 UdpConfigData.DoNotFragment = FALSE;\r
123 UdpConfigData.ReceiveTimeout = 0;\r
124 UdpConfigData.TransmitTimeout = 0;\r
125\r
126 UdpConfigData.UseDefaultAddress = FALSE;\r
127 UdpConfigData.StationPort = DHCP_CLIENT_PORT;\r
128 UdpConfigData.RemotePort = DHCP_SERVER_PORT;\r
772db4bb 129\r
e48e37fc 130 ZeroMem (&UdpConfigData.StationAddress, sizeof (EFI_IPv4_ADDRESS));\r
131 ZeroMem (&UdpConfigData.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
132 ZeroMem (&UdpConfigData.RemoteAddress, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 133\r
d1050b9d 134 return UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfigData);\r
772db4bb 135}\r
136\r
772db4bb 137/**\r
75dce340 138 Destroy the DHCP service. The Dhcp4 service may be partly initialized,\r
f9204641 139 or partly destroyed. If a resource is destroyed, it is marked as so in\r
140 case the destroy failed and being called again later.\r
772db4bb 141\r
75dce340 142 @param[in] DhcpSb The DHCP service instance to destroy.\r
772db4bb 143\r
f9204641 144 @retval EFI_SUCCESS Always return success.\r
772db4bb 145\r
146**/\r
147EFI_STATUS\r
148Dhcp4CloseService (\r
d1050b9d 149 IN DHCP_SERVICE *DhcpSb\r
772db4bb 150 )\r
151{\r
152 DhcpCleanLease (DhcpSb);\r
153\r
154 if (DhcpSb->UdpIo != NULL) {\r
b45b45b2 155 UdpIoFreeIo (DhcpSb->UdpIo);\r
772db4bb 156 DhcpSb->UdpIo = NULL;\r
157 }\r
158\r
159 if (DhcpSb->Timer != NULL) {\r
160 gBS->SetTimer (DhcpSb->Timer, TimerCancel, 0);\r
161 gBS->CloseEvent (DhcpSb->Timer);\r
162\r
163 DhcpSb->Timer = NULL;\r
164 }\r
165\r
166 return EFI_SUCCESS;\r
167}\r
168\r
772db4bb 169/**\r
170 Create a new DHCP service binding instance for the controller.\r
171\r
3e8c18da 172 @param[in] Controller The controller to install DHCP service binding\r
173 protocol onto\r
174 @param[in] ImageHandle The driver's image handle\r
175 @param[out] Service The variable to receive the created DHCP service\r
176 instance.\r
772db4bb 177\r
178 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource .\r
179 @retval EFI_SUCCESS The DHCP service instance is created.\r
f9204641 180 @retval other Other error occurs.\r
772db4bb 181\r
182**/\r
183EFI_STATUS\r
184Dhcp4CreateService (\r
d1050b9d
MK
185 IN EFI_HANDLE Controller,\r
186 IN EFI_HANDLE ImageHandle,\r
187 OUT DHCP_SERVICE **Service\r
772db4bb 188 )\r
189{\r
d1050b9d
MK
190 DHCP_SERVICE *DhcpSb;\r
191 EFI_STATUS Status;\r
772db4bb 192\r
d1050b9d
MK
193 *Service = NULL;\r
194 DhcpSb = AllocateZeroPool (sizeof (DHCP_SERVICE));\r
772db4bb 195\r
196 if (DhcpSb == NULL) {\r
197 return EFI_OUT_OF_RESOURCES;\r
198 }\r
199\r
d1050b9d
MK
200 DhcpSb->Signature = DHCP_SERVICE_SIGNATURE;\r
201 DhcpSb->ServiceState = DHCP_UNCONFIGED;\r
202 DhcpSb->Controller = Controller;\r
203 DhcpSb->Image = ImageHandle;\r
e48e37fc 204 InitializeListHead (&DhcpSb->Children);\r
d1050b9d
MK
205 DhcpSb->DhcpState = Dhcp4Stopped;\r
206 DhcpSb->Xid = NET_RANDOM (NetRandomInitSeed ());\r
f9204641 207 CopyMem (\r
208 &DhcpSb->ServiceBinding,\r
209 &mDhcp4ServiceBindingTemplate,\r
210 sizeof (EFI_SERVICE_BINDING_PROTOCOL)\r
211 );\r
772db4bb 212 //\r
213 // Create various resources, UdpIo, Timer, and get Mac address\r
214 //\r
215 Status = gBS->CreateEvent (\r
216 EVT_NOTIFY_SIGNAL | EVT_TIMER,\r
e48e37fc 217 TPL_CALLBACK,\r
772db4bb 218 DhcpOnTimerTick,\r
219 DhcpSb,\r
220 &DhcpSb->Timer\r
221 );\r
222\r
223 if (EFI_ERROR (Status)) {\r
224 goto ON_ERROR;\r
225 }\r
226\r
b45b45b2 227 DhcpSb->UdpIo = UdpIoCreateIo (\r
228 Controller,\r
229 ImageHandle,\r
230 DhcpConfigUdpIo,\r
231 UDP_IO_UDP4_VERSION,\r
232 NULL\r
233 );\r
772db4bb 234\r
235 if (DhcpSb->UdpIo == NULL) {\r
236 Status = EFI_OUT_OF_RESOURCES;\r
237 goto ON_ERROR;\r
238 }\r
239\r
d1050b9d 240 DhcpSb->HwLen = (UINT8)DhcpSb->UdpIo->SnpMode.HwAddressSize;\r
772db4bb 241 DhcpSb->HwType = DhcpSb->UdpIo->SnpMode.IfType;\r
687a2e5f 242 CopyMem (&DhcpSb->Mac, &DhcpSb->UdpIo->SnpMode.CurrentAddress, sizeof (DhcpSb->Mac));\r
772db4bb 243\r
d1050b9d 244 *Service = DhcpSb;\r
772db4bb 245 return EFI_SUCCESS;\r
246\r
247ON_ERROR:\r
248 Dhcp4CloseService (DhcpSb);\r
a4df47f1 249 FreePool (DhcpSb);\r
772db4bb 250\r
251 return Status;\r
252}\r
253\r
772db4bb 254/**\r
f9204641 255 Start this driver on ControllerHandle. This service is called by the\r
256 EFI boot service ConnectController(). In order to make\r
257 drivers as small as possible, there are a few calling restrictions for\r
258 this service. ConnectController() must follow these\r
259 calling restrictions. If any other agent wishes to call Start() it\r
260 must also follow these calling restrictions.\r
261\r
3e8c18da 262 @param[in] This Protocol instance pointer.\r
263 @param[in] ControllerHandle Handle of device to bind driver to\r
264 @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r
265 device to start.\r
f9204641 266\r
267 @retval EFI_SUCCESS This driver is added to ControllerHandle\r
268 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle\r
269 @retval other This driver does not support this device\r
772db4bb 270\r
271**/\r
272EFI_STATUS\r
273EFIAPI\r
274Dhcp4DriverBindingStart (\r
275 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
276 IN EFI_HANDLE ControllerHandle,\r
277 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
278 )\r
279{\r
d1050b9d
MK
280 DHCP_SERVICE *DhcpSb;\r
281 EFI_STATUS Status;\r
772db4bb 282\r
283 //\r
284 // First: test for the DHCP4 Protocol\r
285 //\r
286 Status = gBS->OpenProtocol (\r
287 ControllerHandle,\r
288 &gEfiDhcp4ServiceBindingProtocolGuid,\r
289 NULL,\r
290 This->DriverBindingHandle,\r
291 ControllerHandle,\r
292 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
293 );\r
294\r
295 if (Status == EFI_SUCCESS) {\r
296 return EFI_ALREADY_STARTED;\r
297 }\r
298\r
299 Status = Dhcp4CreateService (ControllerHandle, This->DriverBindingHandle, &DhcpSb);\r
300\r
301 if (EFI_ERROR (Status)) {\r
302 return Status;\r
303 }\r
d1050b9d 304\r
e2851998 305 ASSERT (DhcpSb != NULL);\r
772db4bb 306\r
c5bcc2e2 307 //\r
308 // Start the receiving\r
309 //\r
310 Status = UdpIoRecvDatagram (DhcpSb->UdpIo, DhcpInput, DhcpSb, 0);\r
311\r
312 if (EFI_ERROR (Status)) {\r
313 goto ON_ERROR;\r
314 }\r
d1050b9d 315\r
772db4bb 316 Status = gBS->SetTimer (DhcpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);\r
317\r
318 if (EFI_ERROR (Status)) {\r
319 goto ON_ERROR;\r
320 }\r
321\r
322 //\r
c194ccca 323 // Install the Dhcp4ServiceBinding Protocol onto ControllerHandle\r
772db4bb 324 //\r
325 Status = gBS->InstallMultipleProtocolInterfaces (\r
326 &ControllerHandle,\r
327 &gEfiDhcp4ServiceBindingProtocolGuid,\r
328 &DhcpSb->ServiceBinding,\r
329 NULL\r
330 );\r
331\r
332 if (EFI_ERROR (Status)) {\r
333 goto ON_ERROR;\r
334 }\r
335\r
336 return Status;\r
337\r
338ON_ERROR:\r
339 Dhcp4CloseService (DhcpSb);\r
a4df47f1 340 FreePool (DhcpSb);\r
772db4bb 341 return Status;\r
342}\r
343\r
216f7970 344/**\r
345 Callback function which provided by user to remove one node in NetDestroyLinkList process.\r
d1102dba 346\r
216f7970 347 @param[in] Entry The entry to be removed.\r
348 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.\r
349\r
350 @retval EFI_SUCCESS The entry has been removed successfully.\r
351 @retval Others Fail to remove the entry.\r
352\r
353**/\r
354EFI_STATUS\r
1f7eb561 355EFIAPI\r
216f7970 356Dhcp4DestroyChildEntry (\r
d1050b9d
MK
357 IN LIST_ENTRY *Entry,\r
358 IN VOID *Context\r
1f7eb561 359 )\r
216f7970 360{\r
d1050b9d
MK
361 DHCP_PROTOCOL *Instance;\r
362 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;\r
216f7970 363\r
d1050b9d 364 if ((Entry == NULL) || (Context == NULL)) {\r
216f7970 365 return EFI_INVALID_PARAMETER;\r
366 }\r
367\r
d1050b9d
MK
368 Instance = NET_LIST_USER_STRUCT_S (Entry, DHCP_PROTOCOL, Link, DHCP_PROTOCOL_SIGNATURE);\r
369 ServiceBinding = (EFI_SERVICE_BINDING_PROTOCOL *)Context;\r
216f7970 370\r
371 return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);\r
372}\r
373\r
772db4bb 374/**\r
f9204641 375 Stop this driver on ControllerHandle. This service is called by the\r
376 EFI boot service DisconnectController(). In order to\r
377 make drivers as small as possible, there are a few calling\r
378 restrictions for this service. DisconnectController()\r
379 must follow these calling restrictions. If any other agent wishes\r
380 to call Stop() it must also follow these calling restrictions.\r
e2851998 381\r
3e8c18da 382 @param[in] This Protocol instance pointer.\r
383 @param[in] ControllerHandle Handle of device to stop driver on\r
384 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
385 children is zero stop the entire bus driver.\r
386 @param[in] ChildHandleBuffer List of Child Handles to Stop.\r
f9204641 387\r
388 @retval EFI_SUCCESS This driver is removed ControllerHandle\r
389 @retval other This driver was not removed from this device\r
772db4bb 390\r
391**/\r
392EFI_STATUS\r
393EFIAPI\r
394Dhcp4DriverBindingStop (\r
395 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
396 IN EFI_HANDLE ControllerHandle,\r
397 IN UINTN NumberOfChildren,\r
398 IN EFI_HANDLE *ChildHandleBuffer\r
399 )\r
400{\r
401 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;\r
402 DHCP_SERVICE *DhcpSb;\r
772db4bb 403 EFI_HANDLE NicHandle;\r
404 EFI_STATUS Status;\r
216f7970 405 LIST_ENTRY *List;\r
406 UINTN ListLength;\r
772db4bb 407\r
408 //\r
409 // DHCP driver opens UDP child, So, the ControllerHandle is the\r
410 // UDP child handle. locate the Nic handle first.\r
411 //\r
412 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp4ProtocolGuid);\r
413\r
414 if (NicHandle == NULL) {\r
216f7970 415 return EFI_SUCCESS;\r
772db4bb 416 }\r
417\r
d1050b9d 418 Status = gBS->OpenProtocol (\r
772db4bb 419 NicHandle,\r
420 &gEfiDhcp4ServiceBindingProtocolGuid,\r
d1050b9d 421 (VOID **)&ServiceBinding,\r
772db4bb 422 This->DriverBindingHandle,\r
423 NicHandle,\r
424 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
425 );\r
426\r
427 if (EFI_ERROR (Status)) {\r
428 return EFI_DEVICE_ERROR;\r
429 }\r
430\r
431 DhcpSb = DHCP_SERVICE_FROM_THIS (ServiceBinding);\r
216f7970 432 if (!IsListEmpty (&DhcpSb->Children)) {\r
433 //\r
c194ccca 434 // Destroy all the children instances before destroy the service.\r
d1102dba 435 //\r
d1050b9d 436 List = &DhcpSb->Children;\r
216f7970 437 Status = NetDestroyLinkList (\r
438 List,\r
439 Dhcp4DestroyChildEntry,\r
440 ServiceBinding,\r
441 &ListLength\r
442 );\r
d1050b9d 443 if (EFI_ERROR (Status) || (ListLength != 0)) {\r
216f7970 444 Status = EFI_DEVICE_ERROR;\r
445 }\r
772db4bb 446 }\r
447\r
d1050b9d 448 if ((NumberOfChildren == 0) && !IsListEmpty (&DhcpSb->Children)) {\r
216f7970 449 Status = EFI_DEVICE_ERROR;\r
450 }\r
772db4bb 451\r
d1050b9d 452 if ((NumberOfChildren == 0) && IsListEmpty (&DhcpSb->Children)) {\r
216f7970 453 //\r
454 // Destroy the service itself if no child instance left.\r
455 //\r
75dce340 456 DhcpSb->ServiceState = DHCP_DESTROY;\r
772db4bb 457\r
c4a62a12 458 gBS->UninstallProtocolInterface (\r
459 NicHandle,\r
460 &gEfiDhcp4ServiceBindingProtocolGuid,\r
461 ServiceBinding\r
462 );\r
772db4bb 463\r
c4a62a12 464 Dhcp4CloseService (DhcpSb);\r
465\r
216f7970 466 if (gDhcpControllerNameTable != NULL) {\r
467 FreeUnicodeStringTable (gDhcpControllerNameTable);\r
468 gDhcpControllerNameTable = NULL;\r
c4a62a12 469 }\r
d1050b9d 470\r
216f7970 471 FreePool (DhcpSb);\r
d1102dba 472\r
216f7970 473 Status = EFI_SUCCESS;\r
772db4bb 474 }\r
475\r
772db4bb 476 return Status;\r
477}\r
478\r
772db4bb 479/**\r
f9204641 480 Initialize a new DHCP instance.\r
772db4bb 481\r
482 @param DhcpSb The dhcp service instance\r
483 @param Instance The dhcp instance to initialize\r
484\r
772db4bb 485**/\r
486VOID\r
487DhcpInitProtocol (\r
d1050b9d
MK
488 IN DHCP_SERVICE *DhcpSb,\r
489 IN OUT DHCP_PROTOCOL *Instance\r
772db4bb 490 )\r
491{\r
d1050b9d 492 Instance->Signature = DHCP_PROTOCOL_SIGNATURE;\r
687a2e5f 493 CopyMem (&Instance->Dhcp4Protocol, &mDhcp4ProtocolTemplate, sizeof (Instance->Dhcp4Protocol));\r
e48e37fc 494 InitializeListHead (&Instance->Link);\r
d1050b9d
MK
495 Instance->Handle = NULL;\r
496 Instance->Service = DhcpSb;\r
497 Instance->InDestroy = FALSE;\r
498 Instance->CompletionEvent = NULL;\r
499 Instance->RenewRebindEvent = NULL;\r
500 Instance->Token = NULL;\r
501 Instance->UdpIo = NULL;\r
502 Instance->ElaspedTime = 0;\r
c4a62a12 503 NetbufQueInit (&Instance->ResponseQueue);\r
772db4bb 504}\r
505\r
772db4bb 506/**\r
3e8c18da 507 Creates a child handle and installs a protocol.\r
e2851998 508\r
509 The CreateChild() function installs a protocol on ChildHandle.\r
510 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.\r
3e8c18da 511 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.\r
772db4bb 512\r
3e8c18da 513 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
514 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,\r
e2851998 515 then a new handle is created. If it is a pointer to an existing UEFI handle,\r
3e8c18da 516 then the protocol is added to the existing UEFI handle.\r
772db4bb 517\r
c194ccca 518 @retval EFI_SUCCESS The protocol was added to ChildHandle.\r
f9204641 519 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.\r
2048c585 520 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create\r
f9204641 521 the child\r
522 @retval other The child handle was not created\r
772db4bb 523\r
524**/\r
525EFI_STATUS\r
526EFIAPI\r
527Dhcp4ServiceBindingCreateChild (\r
528 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
529 IN EFI_HANDLE *ChildHandle\r
530 )\r
531{\r
d1050b9d
MK
532 DHCP_SERVICE *DhcpSb;\r
533 DHCP_PROTOCOL *Instance;\r
534 EFI_STATUS Status;\r
535 EFI_TPL OldTpl;\r
536 VOID *Udp4;\r
772db4bb 537\r
538 if ((This == NULL) || (ChildHandle == NULL)) {\r
539 return EFI_INVALID_PARAMETER;\r
540 }\r
541\r
e48e37fc 542 Instance = AllocatePool (sizeof (*Instance));\r
772db4bb 543\r
544 if (Instance == NULL) {\r
545 return EFI_OUT_OF_RESOURCES;\r
546 }\r
547\r
548 DhcpSb = DHCP_SERVICE_FROM_THIS (This);\r
549 DhcpInitProtocol (DhcpSb, Instance);\r
550\r
551 //\r
552 // Install DHCP4 onto ChildHandle\r
553 //\r
554 Status = gBS->InstallMultipleProtocolInterfaces (\r
555 ChildHandle,\r
556 &gEfiDhcp4ProtocolGuid,\r
557 &Instance->Dhcp4Protocol,\r
558 NULL\r
559 );\r
560\r
561 if (EFI_ERROR (Status)) {\r
a4df47f1 562 FreePool (Instance);\r
772db4bb 563 return Status;\r
564 }\r
565\r
d1050b9d 566 Instance->Handle = *ChildHandle;\r
772db4bb 567\r
568 //\r
569 // Open the Udp4 protocol BY_CHILD.\r
570 //\r
571 Status = gBS->OpenProtocol (\r
572 DhcpSb->UdpIo->UdpHandle,\r
573 &gEfiUdp4ProtocolGuid,\r
d1050b9d 574 (VOID **)&Udp4,\r
772db4bb 575 gDhcp4DriverBinding.DriverBindingHandle,\r
576 Instance->Handle,\r
577 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
578 );\r
579 if (EFI_ERROR (Status)) {\r
580 gBS->UninstallMultipleProtocolInterfaces (\r
581 Instance->Handle,\r
582 &gEfiDhcp4ProtocolGuid,\r
583 &Instance->Dhcp4Protocol,\r
584 NULL\r
585 );\r
586\r
a4df47f1 587 FreePool (Instance);\r
772db4bb 588 return Status;\r
589 }\r
590\r
e48e37fc 591 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 592\r
e48e37fc 593 InsertTailList (&DhcpSb->Children, &Instance->Link);\r
772db4bb 594 DhcpSb->NumChildren++;\r
595\r
e48e37fc 596 gBS->RestoreTPL (OldTpl);\r
772db4bb 597\r
598 return EFI_SUCCESS;\r
599}\r
600\r
772db4bb 601/**\r
3e8c18da 602 Destroys a child handle with a protocol installed on it.\r
e2851998 603\r
604 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol\r
605 that was installed by CreateChild() from ChildHandle. If the removed protocol is the\r
3e8c18da 606 last protocol on ChildHandle, then ChildHandle is destroyed.\r
772db4bb 607\r
3e8c18da 608 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
f9204641 609 @param ChildHandle Handle of the child to destroy\r
772db4bb 610\r
c194ccca 611 @retval EFI_SUCCESS The protocol was removed from ChildHandle.\r
3e8c18da 612 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.\r
284ee2e8 613 @retval EFI_INVALID_PARAMETER Child handle is NULL.\r
3e8c18da 614 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle\r
615 because its services are being used.\r
f9204641 616 @retval other The child handle was not destroyed\r
772db4bb 617\r
618**/\r
619EFI_STATUS\r
620EFIAPI\r
621Dhcp4ServiceBindingDestroyChild (\r
622 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
623 IN EFI_HANDLE ChildHandle\r
624 )\r
625{\r
d1050b9d
MK
626 DHCP_SERVICE *DhcpSb;\r
627 DHCP_PROTOCOL *Instance;\r
628 EFI_DHCP4_PROTOCOL *Dhcp;\r
629 EFI_TPL OldTpl;\r
630 EFI_STATUS Status;\r
772db4bb 631\r
632 if ((This == NULL) || (ChildHandle == NULL)) {\r
633 return EFI_INVALID_PARAMETER;\r
634 }\r
635\r
636 //\r
637 // Retrieve the private context data structures\r
638 //\r
639 Status = gBS->OpenProtocol (\r
640 ChildHandle,\r
641 &gEfiDhcp4ProtocolGuid,\r
d1050b9d 642 (VOID **)&Dhcp,\r
772db4bb 643 gDhcp4DriverBinding.DriverBindingHandle,\r
644 ChildHandle,\r
645 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
646 );\r
647\r
648 if (EFI_ERROR (Status)) {\r
649 return EFI_UNSUPPORTED;\r
650 }\r
651\r
d1050b9d
MK
652 Instance = DHCP_INSTANCE_FROM_THIS (Dhcp);\r
653 DhcpSb = DHCP_SERVICE_FROM_THIS (This);\r
772db4bb 654\r
655 if (Instance->Service != DhcpSb) {\r
656 return EFI_INVALID_PARAMETER;\r
657 }\r
658\r
659 //\r
f9204641 660 // A child can be destroyed more than once. For example,\r
661 // Dhcp4DriverBindingStop will destroy all of its children.\r
75dce340 662 // when caller driver is being stopped, it will destroy the\r
772db4bb 663 // dhcp child it opens.\r
664 //\r
75dce340 665 if (Instance->InDestroy) {\r
772db4bb 666 return EFI_SUCCESS;\r
667 }\r
668\r
d1050b9d 669 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
75dce340 670 Instance->InDestroy = TRUE;\r
772db4bb 671\r
672 //\r
673 // Close the Udp4 protocol.\r
674 //\r
675 gBS->CloseProtocol (\r
676 DhcpSb->UdpIo->UdpHandle,\r
677 &gEfiUdp4ProtocolGuid,\r
678 gDhcp4DriverBinding.DriverBindingHandle,\r
679 ChildHandle\r
680 );\r
681\r
682 //\r
683 // Uninstall the DHCP4 protocol first to enable a top down destruction.\r
684 //\r
216f7970 685 gBS->RestoreTPL (OldTpl);\r
772db4bb 686 Status = gBS->UninstallProtocolInterface (\r
687 ChildHandle,\r
688 &gEfiDhcp4ProtocolGuid,\r
689 Dhcp\r
690 );\r
216f7970 691 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 692 if (EFI_ERROR (Status)) {\r
75dce340 693 Instance->InDestroy = FALSE;\r
772db4bb 694\r
e48e37fc 695 gBS->RestoreTPL (OldTpl);\r
772db4bb 696 return Status;\r
697 }\r
698\r
699 if (DhcpSb->ActiveChild == Instance) {\r
700 DhcpYieldControl (DhcpSb);\r
701 }\r
702\r
e48e37fc 703 RemoveEntryList (&Instance->Link);\r
772db4bb 704 DhcpSb->NumChildren--;\r
705\r
35bc88dd
WJ
706 if (Instance->UdpIo != NULL) {\r
707 UdpIoCleanIo (Instance->UdpIo);\r
708 gBS->CloseProtocol (\r
709 Instance->UdpIo->UdpHandle,\r
710 &gEfiUdp4ProtocolGuid,\r
711 Instance->Service->Image,\r
712 Instance->Handle\r
713 );\r
714 UdpIoFreeIo (Instance->UdpIo);\r
715 Instance->UdpIo = NULL;\r
716 Instance->Token = NULL;\r
717 }\r
718\r
e48e37fc 719 gBS->RestoreTPL (OldTpl);\r
772db4bb 720\r
a4df47f1 721 FreePool (Instance);\r
772db4bb 722 return EFI_SUCCESS;\r
723}\r