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