Driver.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /** @file
  2. This is service binding for Hash driver.
  3. Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Driver.h"
  7. EFI_SERVICE_BINDING_PROTOCOL mHash2ServiceBindingProtocol = {
  8. Hash2ServiceBindingCreateChild,
  9. Hash2ServiceBindingDestroyChild
  10. };
  11. /**
  12. Creates a child handle with a set of I/O services.
  13. @param[in] This Protocol instance pointer.
  14. @param[in, out] ChildHandle Pointer to the handle of the child to create. If
  15. it is NULL, then a new handle is created. If
  16. it is not NULL, then the I/O services are added
  17. to the existing child handle.
  18. @retval EFI_SUCCESS The protocol was added to ChildHandle.
  19. @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
  20. @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
  21. create the child.
  22. @retval Others The child handle was not created.
  23. **/
  24. EFI_STATUS
  25. EFIAPI
  26. Hash2ServiceBindingCreateChild (
  27. IN EFI_SERVICE_BINDING_PROTOCOL *This,
  28. IN OUT EFI_HANDLE *ChildHandle
  29. )
  30. {
  31. EFI_STATUS Status;
  32. HASH2_SERVICE_DATA *Hash2ServiceData;
  33. HASH2_INSTANCE_DATA *Instance;
  34. EFI_TPL OldTpl;
  35. if ((This == NULL) || (ChildHandle == NULL)) {
  36. return EFI_INVALID_PARAMETER;
  37. }
  38. Hash2ServiceData = HASH2_SERVICE_DATA_FROM_THIS (This);
  39. //
  40. // Allocate buffer for the new instance.
  41. //
  42. Instance = AllocateZeroPool (sizeof (HASH2_INSTANCE_DATA));
  43. if (Instance == NULL) {
  44. return EFI_OUT_OF_RESOURCES;
  45. }
  46. //
  47. // Init the instance data.
  48. //
  49. Instance->Signature = HASH2_INSTANCE_DATA_SIGNATURE;
  50. CopyMem (&Instance->Hash2Protocol, &mHash2Protocol, sizeof (Instance->Hash2Protocol));
  51. Instance->Hash2ServiceData = Hash2ServiceData;
  52. Status = gBS->InstallMultipleProtocolInterfaces (
  53. ChildHandle,
  54. &gEfiHash2ProtocolGuid,
  55. &Instance->Hash2Protocol,
  56. NULL
  57. );
  58. if (EFI_ERROR (Status)) {
  59. FreePool (Instance);
  60. return Status;
  61. }
  62. Instance->Handle = *ChildHandle;
  63. //
  64. // Add the child instance into ChildrenList.
  65. //
  66. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  67. InsertTailList (&Hash2ServiceData->ChildrenList, &Instance->InstEntry);
  68. gBS->RestoreTPL (OldTpl);
  69. return Status;
  70. }
  71. /**
  72. Destroys a child handle with a set of I/O services.
  73. The DestroyChild() function does the opposite of CreateChild(). It removes a
  74. protocol that was installed by CreateChild() from ChildHandle. If the removed
  75. protocol is the last protocol on ChildHandle, then ChildHandle is destroyed.
  76. @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL
  77. instance.
  78. @param[in] ChildHandle Handle of the child to destroy.
  79. @retval EFI_SUCCESS The protocol was removed from ChildHandle.
  80. @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that
  81. is being removed.
  82. @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
  83. @retval EFI_ACCESS_DENIED The protocol could not be removed from the
  84. ChildHandle because its services are being
  85. used.
  86. @retval Others The child handle was not destroyed.
  87. **/
  88. EFI_STATUS
  89. EFIAPI
  90. Hash2ServiceBindingDestroyChild (
  91. IN EFI_SERVICE_BINDING_PROTOCOL *This,
  92. IN EFI_HANDLE ChildHandle
  93. )
  94. {
  95. EFI_STATUS Status;
  96. HASH2_SERVICE_DATA *Hash2ServiceData;
  97. EFI_HASH2_PROTOCOL *Hash2Protocol;
  98. HASH2_INSTANCE_DATA *Instance;
  99. EFI_TPL OldTpl;
  100. LIST_ENTRY *Entry;
  101. if ((This == NULL) || (ChildHandle == NULL)) {
  102. return EFI_INVALID_PARAMETER;
  103. }
  104. Hash2ServiceData = HASH2_SERVICE_DATA_FROM_THIS (This);
  105. //
  106. // Check if this ChildHandle is valid
  107. //
  108. Instance = NULL;
  109. for (Entry = (&Hash2ServiceData->ChildrenList)->ForwardLink; Entry != (&Hash2ServiceData->ChildrenList); Entry = Entry->ForwardLink) {
  110. Instance = HASH2_INSTANCE_DATA_FROM_LINK (Entry);
  111. if (Instance->Handle == ChildHandle) {
  112. break;
  113. } else {
  114. Instance = NULL;
  115. }
  116. }
  117. if (Instance == NULL) {
  118. DEBUG ((DEBUG_ERROR, "Hash2ServiceBindingDestroyChild - Invalid handle\n"));
  119. return EFI_UNSUPPORTED;
  120. }
  121. //
  122. // Get HashProtocol
  123. //
  124. Status = gBS->HandleProtocol (
  125. ChildHandle,
  126. &gEfiHash2ProtocolGuid,
  127. (VOID **)&Hash2Protocol
  128. );
  129. if (EFI_ERROR (Status)) {
  130. return Status;
  131. }
  132. ASSERT (Hash2Protocol == &Instance->Hash2Protocol);
  133. //
  134. // Uninstall the Hash protocol.
  135. //
  136. Status = gBS->UninstallMultipleProtocolInterfaces (
  137. ChildHandle,
  138. &gEfiHash2ProtocolGuid,
  139. &Instance->Hash2Protocol,
  140. NULL
  141. );
  142. if (EFI_ERROR (Status)) {
  143. return Status;
  144. }
  145. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  146. //
  147. // Remove this instance from the ChildrenList.
  148. //
  149. RemoveEntryList (&Instance->InstEntry);
  150. gBS->RestoreTPL (OldTpl);
  151. FreePool (Instance);
  152. return Status;
  153. }
  154. /**
  155. The entry point for Hash driver which installs the service binding protocol.
  156. @param[in] ImageHandle The image handle of the driver.
  157. @param[in] SystemTable The system table.
  158. @retval EFI_SUCCESS The service binding protocols is successfully installed.
  159. @retval Others Other errors as indicated.
  160. **/
  161. EFI_STATUS
  162. EFIAPI
  163. Hash2DriverEntryPoint (
  164. IN EFI_HANDLE ImageHandle,
  165. IN EFI_SYSTEM_TABLE *SystemTable
  166. )
  167. {
  168. EFI_STATUS Status;
  169. HASH2_SERVICE_DATA *Hash2ServiceData;
  170. //
  171. // Initialize the Hash Service Data.
  172. //
  173. Hash2ServiceData = AllocateZeroPool (sizeof (HASH2_SERVICE_DATA));
  174. if (Hash2ServiceData == NULL) {
  175. return EFI_OUT_OF_RESOURCES;
  176. }
  177. Hash2ServiceData->Signature = HASH2_SERVICE_DATA_SIGNATURE;
  178. CopyMem (&Hash2ServiceData->ServiceBinding, &mHash2ServiceBindingProtocol, sizeof (EFI_SERVICE_BINDING_PROTOCOL));
  179. InitializeListHead (&Hash2ServiceData->ChildrenList);
  180. //
  181. // Install the HASH Service Binding Protocol
  182. //
  183. Status = gBS->InstallMultipleProtocolInterfaces (
  184. &Hash2ServiceData->ServiceHandle,
  185. &gEfiHash2ServiceBindingProtocolGuid,
  186. &Hash2ServiceData->ServiceBinding,
  187. NULL
  188. );
  189. if (EFI_ERROR (Status)) {
  190. FreePool (Hash2ServiceData);
  191. }
  192. return Status;
  193. }