Dhcp4Driver.c 21 KB

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