Udp4Driver.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /** @file
  2. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include "Udp4Impl.h"
  6. EFI_DRIVER_BINDING_PROTOCOL gUdp4DriverBinding = {
  7. Udp4DriverBindingSupported,
  8. Udp4DriverBindingStart,
  9. Udp4DriverBindingStop,
  10. 0xa,
  11. NULL,
  12. NULL
  13. };
  14. EFI_SERVICE_BINDING_PROTOCOL mUdp4ServiceBinding = {
  15. Udp4ServiceBindingCreateChild,
  16. Udp4ServiceBindingDestroyChild
  17. };
  18. /**
  19. Callback function which provided by user to remove one node in NetDestroyLinkList process.
  20. @param[in] Entry The entry to be removed.
  21. @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
  22. @retval EFI_SUCCESS The entry has been removed successfully.
  23. @retval Others Fail to remove the entry.
  24. **/
  25. EFI_STATUS
  26. EFIAPI
  27. Udp4DestroyChildEntryInHandleBuffer (
  28. IN LIST_ENTRY *Entry,
  29. IN VOID *Context
  30. )
  31. {
  32. UDP4_INSTANCE_DATA *Instance;
  33. EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
  34. UINTN NumberOfChildren;
  35. EFI_HANDLE *ChildHandleBuffer;
  36. if ((Entry == NULL) || (Context == NULL)) {
  37. return EFI_INVALID_PARAMETER;
  38. }
  39. Instance = NET_LIST_USER_STRUCT_S (Entry, UDP4_INSTANCE_DATA, Link, UDP4_INSTANCE_DATA_SIGNATURE);
  40. ServiceBinding = ((UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->ServiceBinding;
  41. NumberOfChildren = ((UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->NumberOfChildren;
  42. ChildHandleBuffer = ((UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->ChildHandleBuffer;
  43. if (!NetIsInHandleBuffer (Instance->ChildHandle, NumberOfChildren, ChildHandleBuffer)) {
  44. return EFI_SUCCESS;
  45. }
  46. return ServiceBinding->DestroyChild (ServiceBinding, Instance->ChildHandle);
  47. }
  48. /**
  49. Test to see if this driver supports ControllerHandle. This service
  50. is called by the EFI boot service ConnectController(). In
  51. order to make drivers as small as possible, there are a few calling
  52. restrictions for this service. ConnectController() must
  53. follow these calling restrictions. If any other agent wishes to call
  54. Supported() it must also follow these calling restrictions.
  55. @param[in] This Protocol instance pointer.
  56. @param[in] ControllerHandle Handle of device to test
  57. @param[in] RemainingDevicePath Optional parameter use to pick a specific child
  58. device to start.
  59. @retval EFI_SUCCESS This driver supports this device
  60. @retval EFI_ALREADY_STARTED This driver is already running on this device
  61. @retval other This driver does not support this device
  62. **/
  63. EFI_STATUS
  64. EFIAPI
  65. Udp4DriverBindingSupported (
  66. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  67. IN EFI_HANDLE ControllerHandle,
  68. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  69. )
  70. {
  71. EFI_STATUS Status;
  72. //
  73. // Test for the Udp4ServiceBinding Protocol
  74. //
  75. Status = gBS->OpenProtocol (
  76. ControllerHandle,
  77. &gEfiUdp4ServiceBindingProtocolGuid,
  78. NULL,
  79. This->DriverBindingHandle,
  80. ControllerHandle,
  81. EFI_OPEN_PROTOCOL_TEST_PROTOCOL
  82. );
  83. if (!EFI_ERROR (Status)) {
  84. return EFI_ALREADY_STARTED;
  85. }
  86. //
  87. // Test for the Ip4 Protocol
  88. //
  89. Status = gBS->OpenProtocol (
  90. ControllerHandle,
  91. &gEfiIp4ServiceBindingProtocolGuid,
  92. NULL,
  93. This->DriverBindingHandle,
  94. ControllerHandle,
  95. EFI_OPEN_PROTOCOL_TEST_PROTOCOL
  96. );
  97. return Status;
  98. }
  99. /**
  100. Start this driver on ControllerHandle. This service is called by the
  101. EFI boot service ConnectController(). In order to make
  102. drivers as small as possible, there are a few calling restrictions for
  103. this service. ConnectController() must follow these
  104. calling restrictions. If any other agent wishes to call Start() it
  105. must also follow these calling restrictions.
  106. @param[in] This Protocol instance pointer.
  107. @param[in] ControllerHandle Handle of device to bind driver to
  108. @param[in] RemainingDevicePath Optional parameter use to pick a specific child
  109. device to start.
  110. @retval EFI_SUCCESS This driver is added to ControllerHandle
  111. @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
  112. @retval other This driver does not support this device
  113. **/
  114. EFI_STATUS
  115. EFIAPI
  116. Udp4DriverBindingStart (
  117. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  118. IN EFI_HANDLE ControllerHandle,
  119. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  120. )
  121. {
  122. EFI_STATUS Status;
  123. UDP4_SERVICE_DATA *Udp4Service;
  124. //
  125. // Allocate Private Context Data Structure.
  126. //
  127. Udp4Service = AllocatePool (sizeof (UDP4_SERVICE_DATA));
  128. if (Udp4Service == NULL) {
  129. return EFI_OUT_OF_RESOURCES;
  130. }
  131. Status = Udp4CreateService (Udp4Service, This->DriverBindingHandle, ControllerHandle);
  132. if (EFI_ERROR (Status)) {
  133. FreePool (Udp4Service);
  134. return Status;
  135. }
  136. //
  137. // Install the Udp4ServiceBindingProtocol on the ControllerHandle.
  138. //
  139. Status = gBS->InstallMultipleProtocolInterfaces (
  140. &ControllerHandle,
  141. &gEfiUdp4ServiceBindingProtocolGuid,
  142. &Udp4Service->ServiceBinding,
  143. NULL
  144. );
  145. if (EFI_ERROR (Status)) {
  146. Udp4CleanService (Udp4Service);
  147. FreePool (Udp4Service);
  148. }
  149. return Status;
  150. }
  151. /**
  152. Stop this driver on ControllerHandle. This service is called by the
  153. EFI boot service DisconnectController(). In order to
  154. make drivers as small as possible, there are a few calling
  155. restrictions for this service. DisconnectController()
  156. must follow these calling restrictions. If any other agent wishes
  157. to call Stop() it must also follow these calling restrictions.
  158. @param[in] This Protocol instance pointer.
  159. @param[in] ControllerHandle Handle of device to stop driver on
  160. @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
  161. children is zero stop the entire bus driver.
  162. @param[in] ChildHandleBuffer List of Child Handles to Stop.
  163. @retval EFI_SUCCESS This driver is removed ControllerHandle
  164. @retval other This driver was not removed from this device
  165. **/
  166. EFI_STATUS
  167. EFIAPI
  168. Udp4DriverBindingStop (
  169. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  170. IN EFI_HANDLE ControllerHandle,
  171. IN UINTN NumberOfChildren,
  172. IN EFI_HANDLE *ChildHandleBuffer
  173. )
  174. {
  175. EFI_STATUS Status;
  176. EFI_HANDLE NicHandle;
  177. EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
  178. UDP4_SERVICE_DATA *Udp4Service;
  179. UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
  180. LIST_ENTRY *List;
  181. //
  182. // Find the NicHandle where UDP4 ServiceBinding Protocol is installed.
  183. //
  184. NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiIp4ProtocolGuid);
  185. if (NicHandle == NULL) {
  186. return EFI_SUCCESS;
  187. }
  188. //
  189. // Retrieve the UDP4 ServiceBinding Protocol.
  190. //
  191. Status = gBS->OpenProtocol (
  192. NicHandle,
  193. &gEfiUdp4ServiceBindingProtocolGuid,
  194. (VOID **)&ServiceBinding,
  195. This->DriverBindingHandle,
  196. NicHandle,
  197. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  198. );
  199. if (EFI_ERROR (Status)) {
  200. return EFI_DEVICE_ERROR;
  201. }
  202. Udp4Service = UDP4_SERVICE_DATA_FROM_THIS (ServiceBinding);
  203. if (NumberOfChildren != 0) {
  204. //
  205. // NumberOfChildren is not zero, destroy the children instances in ChildHandleBuffer.
  206. //
  207. List = &Udp4Service->ChildrenList;
  208. Context.ServiceBinding = ServiceBinding;
  209. Context.NumberOfChildren = NumberOfChildren;
  210. Context.ChildHandleBuffer = ChildHandleBuffer;
  211. Status = NetDestroyLinkList (
  212. List,
  213. Udp4DestroyChildEntryInHandleBuffer,
  214. &Context,
  215. NULL
  216. );
  217. } else {
  218. gBS->UninstallMultipleProtocolInterfaces (
  219. NicHandle,
  220. &gEfiUdp4ServiceBindingProtocolGuid,
  221. &Udp4Service->ServiceBinding,
  222. NULL
  223. );
  224. Udp4CleanService (Udp4Service);
  225. if (gUdpControllerNameTable != NULL) {
  226. FreeUnicodeStringTable (gUdpControllerNameTable);
  227. gUdpControllerNameTable = NULL;
  228. }
  229. FreePool (Udp4Service);
  230. }
  231. return Status;
  232. }
  233. /**
  234. Creates a child handle and installs a protocol.
  235. The CreateChild() function installs a protocol on ChildHandle.
  236. If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
  237. If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
  238. @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
  239. @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL,
  240. then a new handle is created. If it is a pointer to an existing UEFI handle,
  241. then the protocol is added to the existing UEFI handle.
  242. @retval EFI_SUCCESS The protocol was added to ChildHandle.
  243. @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
  244. @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
  245. the child
  246. @retval other The child handle was not created
  247. **/
  248. EFI_STATUS
  249. EFIAPI
  250. Udp4ServiceBindingCreateChild (
  251. IN EFI_SERVICE_BINDING_PROTOCOL *This,
  252. IN EFI_HANDLE *ChildHandle
  253. )
  254. {
  255. EFI_STATUS Status;
  256. UDP4_SERVICE_DATA *Udp4Service;
  257. UDP4_INSTANCE_DATA *Instance;
  258. EFI_TPL OldTpl;
  259. VOID *Ip4;
  260. if ((This == NULL) || (ChildHandle == NULL)) {
  261. return EFI_INVALID_PARAMETER;
  262. }
  263. Udp4Service = UDP4_SERVICE_DATA_FROM_THIS (This);
  264. //
  265. // Allocate the instance private data structure.
  266. //
  267. Instance = AllocateZeroPool (sizeof (UDP4_INSTANCE_DATA));
  268. if (Instance == NULL) {
  269. return EFI_OUT_OF_RESOURCES;
  270. }
  271. Udp4InitInstance (Udp4Service, Instance);
  272. //
  273. // Add an IpInfo for this instance.
  274. //
  275. Instance->IpInfo = IpIoAddIp (Udp4Service->IpIo);
  276. if (Instance->IpInfo == NULL) {
  277. Status = EFI_OUT_OF_RESOURCES;
  278. goto ON_ERROR;
  279. }
  280. //
  281. // Install the Udp4Protocol for this instance.
  282. //
  283. Status = gBS->InstallMultipleProtocolInterfaces (
  284. ChildHandle,
  285. &gEfiUdp4ProtocolGuid,
  286. &Instance->Udp4Proto,
  287. NULL
  288. );
  289. if (EFI_ERROR (Status)) {
  290. goto ON_ERROR;
  291. }
  292. Instance->ChildHandle = *ChildHandle;
  293. //
  294. // Open the default Ip4 protocol in the IP_IO BY_CHILD.
  295. //
  296. Status = gBS->OpenProtocol (
  297. Udp4Service->IpIo->ChildHandle,
  298. &gEfiIp4ProtocolGuid,
  299. (VOID **)&Ip4,
  300. gUdp4DriverBinding.DriverBindingHandle,
  301. Instance->ChildHandle,
  302. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  303. );
  304. if (EFI_ERROR (Status)) {
  305. goto ON_ERROR;
  306. }
  307. //
  308. // Open this instance's Ip4 protocol in the IpInfo BY_CHILD.
  309. //
  310. Status = gBS->OpenProtocol (
  311. Instance->IpInfo->ChildHandle,
  312. &gEfiIp4ProtocolGuid,
  313. (VOID **)&Ip4,
  314. gUdp4DriverBinding.DriverBindingHandle,
  315. Instance->ChildHandle,
  316. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  317. );
  318. if (EFI_ERROR (Status)) {
  319. goto ON_ERROR;
  320. }
  321. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  322. //
  323. // Link this instance into the service context data and increase the ChildrenNumber.
  324. //
  325. InsertTailList (&Udp4Service->ChildrenList, &Instance->Link);
  326. Udp4Service->ChildrenNumber++;
  327. gBS->RestoreTPL (OldTpl);
  328. return EFI_SUCCESS;
  329. ON_ERROR:
  330. if (Instance->ChildHandle != NULL) {
  331. gBS->UninstallMultipleProtocolInterfaces (
  332. Instance->ChildHandle,
  333. &gEfiUdp4ProtocolGuid,
  334. &Instance->Udp4Proto,
  335. NULL
  336. );
  337. }
  338. if (Instance->IpInfo != NULL) {
  339. IpIoRemoveIp (Udp4Service->IpIo, Instance->IpInfo);
  340. }
  341. Udp4CleanInstance (Instance);
  342. FreePool (Instance);
  343. return Status;
  344. }
  345. /**
  346. Destroys a child handle with a protocol installed on it.
  347. The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
  348. that was installed by CreateChild() from ChildHandle. If the removed protocol is the
  349. last protocol on ChildHandle, then ChildHandle is destroyed.
  350. @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
  351. @param[in] ChildHandle Handle of the child to destroy
  352. @retval EFI_SUCCESS The protocol was removed from ChildHandle.
  353. @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
  354. @retval EFI_INVALID_PARAMETER Child handle is NULL.
  355. @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
  356. because its services are being used.
  357. @retval other The child handle was not destroyed
  358. **/
  359. EFI_STATUS
  360. EFIAPI
  361. Udp4ServiceBindingDestroyChild (
  362. IN EFI_SERVICE_BINDING_PROTOCOL *This,
  363. IN EFI_HANDLE ChildHandle
  364. )
  365. {
  366. EFI_STATUS Status;
  367. UDP4_SERVICE_DATA *Udp4Service;
  368. EFI_UDP4_PROTOCOL *Udp4Proto;
  369. UDP4_INSTANCE_DATA *Instance;
  370. EFI_TPL OldTpl;
  371. if ((This == NULL) || (ChildHandle == NULL)) {
  372. return EFI_INVALID_PARAMETER;
  373. }
  374. Udp4Service = UDP4_SERVICE_DATA_FROM_THIS (This);
  375. //
  376. // Try to get the Udp4 protocol from the ChildHandle.
  377. //
  378. Status = gBS->OpenProtocol (
  379. ChildHandle,
  380. &gEfiUdp4ProtocolGuid,
  381. (VOID **)&Udp4Proto,
  382. gUdp4DriverBinding.DriverBindingHandle,
  383. ChildHandle,
  384. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  385. );
  386. if (EFI_ERROR (Status)) {
  387. return EFI_UNSUPPORTED;
  388. }
  389. Instance = UDP4_INSTANCE_DATA_FROM_THIS (Udp4Proto);
  390. if (Instance->InDestroy) {
  391. return EFI_SUCCESS;
  392. }
  393. //
  394. // Use the Destroyed flag to avoid the re-entering of the following code.
  395. //
  396. Instance->InDestroy = TRUE;
  397. //
  398. // Close the Ip4 protocol.
  399. //
  400. gBS->CloseProtocol (
  401. Udp4Service->IpIo->ChildHandle,
  402. &gEfiIp4ProtocolGuid,
  403. gUdp4DriverBinding.DriverBindingHandle,
  404. Instance->ChildHandle
  405. );
  406. //
  407. // Close the Ip4 protocol on this instance's IpInfo.
  408. //
  409. gBS->CloseProtocol (
  410. Instance->IpInfo->ChildHandle,
  411. &gEfiIp4ProtocolGuid,
  412. gUdp4DriverBinding.DriverBindingHandle,
  413. Instance->ChildHandle
  414. );
  415. //
  416. // Uninstall the Udp4Protocol previously installed on the ChildHandle.
  417. //
  418. Status = gBS->UninstallMultipleProtocolInterfaces (
  419. ChildHandle,
  420. &gEfiUdp4ProtocolGuid,
  421. (VOID *)&Instance->Udp4Proto,
  422. NULL
  423. );
  424. if (EFI_ERROR (Status)) {
  425. Instance->InDestroy = FALSE;
  426. return Status;
  427. }
  428. //
  429. // Reset the configuration in case the instance's consumer forgets to do this.
  430. //
  431. Udp4Proto->Configure (Udp4Proto, NULL);
  432. //
  433. // Remove the IpInfo this instance consumes.
  434. //
  435. IpIoRemoveIp (Udp4Service->IpIo, Instance->IpInfo);
  436. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  437. //
  438. // Remove this instance from the service context data's ChildrenList.
  439. //
  440. RemoveEntryList (&Instance->Link);
  441. Udp4Service->ChildrenNumber--;
  442. //
  443. // Clean the instance.
  444. //
  445. Udp4CleanInstance (Instance);
  446. gBS->RestoreTPL (OldTpl);
  447. FreePool (Instance);
  448. return EFI_SUCCESS;
  449. }
  450. /**
  451. This is the declaration of an EFI image entry point. This entry point is
  452. the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
  453. both device drivers and bus drivers.
  454. The entry point for Udp4 driver which installs the driver binding
  455. and component name protocol on its ImageHandle.
  456. @param[in] ImageHandle The firmware allocated handle for the UEFI image.
  457. @param[in] SystemTable A pointer to the EFI System Table.
  458. @retval EFI_SUCCESS The operation completed successfully.
  459. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
  460. **/
  461. EFI_STATUS
  462. EFIAPI
  463. Udp4DriverEntryPoint (
  464. IN EFI_HANDLE ImageHandle,
  465. IN EFI_SYSTEM_TABLE *SystemTable
  466. )
  467. {
  468. EFI_STATUS Status;
  469. //
  470. // Install the Udp4DriverBinding and Udp4ComponentName protocols.
  471. //
  472. Status = EfiLibInstallDriverBindingComponentName2 (
  473. ImageHandle,
  474. SystemTable,
  475. &gUdp4DriverBinding,
  476. ImageHandle,
  477. &gUdp4ComponentName,
  478. &gUdp4ComponentName2
  479. );
  480. if (!EFI_ERROR (Status)) {
  481. //
  482. // Initialize the UDP random port.
  483. //
  484. mUdp4RandomPort = (UINT16)(((UINT16)NetRandomInitSeed ()) % UDP4_PORT_KNOWN + UDP4_PORT_KNOWN);
  485. }
  486. return Status;
  487. }