]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/TcgSmm/TcgSmm.c
Enhance TcgSmm driver to handle potential SetVariable failure case.
[mirror_edk2.git] / SecurityPkg / Tcg / TcgSmm / TcgSmm.c
1 /** @file
2 It updates TPM items in ACPI table and registers SMI callback
3 functions for physical presence and ClearMemory.
4
5 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "TcgSmm.h"
17
18 EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable;
19 TCG_NVS *mTcgNvs;
20
21 /**
22 Software SMI callback for TPM physical presence which is called from ACPI method.
23
24 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
25 @param[in] Context Points to an optional handler context which was specified when the
26 handler was registered.
27 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
28 be conveyed from a non-SMM environment into an SMM environment.
29 @param[in, out] CommBufferSize The size of the CommBuffer.
30
31 @retval EFI_SUCCESS The interrupt was handled successfully.
32
33 **/
34 EFI_STATUS
35 EFIAPI
36 PhysicalPresenceCallback (
37 IN EFI_HANDLE DispatchHandle,
38 IN CONST VOID *Context,
39 IN OUT VOID *CommBuffer,
40 IN OUT UINTN *CommBufferSize
41 )
42 {
43 EFI_STATUS Status;
44 UINTN DataSize;
45 EFI_PHYSICAL_PRESENCE PpData;
46 UINT8 Flags;
47 BOOLEAN RequestConfirmed;
48
49 //
50 // Get the Physical Presence variable
51 //
52 DataSize = sizeof (EFI_PHYSICAL_PRESENCE);
53 Status = mSmmVariable->SmmGetVariable (
54 PHYSICAL_PRESENCE_VARIABLE,
55 &gEfiPhysicalPresenceGuid,
56 NULL,
57 &DataSize,
58 &PpData
59 );
60 if (EFI_ERROR (Status)) {
61 return EFI_SUCCESS;
62 }
63
64 DEBUG ((EFI_D_INFO, "[TPM] PP callback, Parameter = %x\n", mTcgNvs->PhysicalPresence.Parameter));
65 if (mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_RETURN_REQUEST_RESPONSE_TO_OS) {
66 mTcgNvs->PhysicalPresence.LastRequest = PpData.LastPPRequest;
67 mTcgNvs->PhysicalPresence.Response = PpData.PPResponse;
68 } else if ((mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS)
69 || (mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS_2)) {
70 if (mTcgNvs->PhysicalPresence.Request == PHYSICAL_PRESENCE_SET_OPERATOR_AUTH) {
71 //
72 // This command requires UI to prompt user for Auth data.
73 //
74 mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_NOT_IMPLEMENTED;
75 return EFI_SUCCESS;
76 }
77
78 if (PpData.PPRequest != mTcgNvs->PhysicalPresence.Request) {
79 PpData.PPRequest = (UINT8) mTcgNvs->PhysicalPresence.Request;
80 DataSize = sizeof (EFI_PHYSICAL_PRESENCE);
81 Status = mSmmVariable->SmmSetVariable (
82 PHYSICAL_PRESENCE_VARIABLE,
83 &gEfiPhysicalPresenceGuid,
84 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
85 DataSize,
86 &PpData
87 );
88 }
89
90 if (EFI_ERROR (Status)) {
91 mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_GENERAL_FAILURE;
92 return EFI_SUCCESS;
93 }
94 mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_SUCCESS;
95 } else if (mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_GET_USER_CONFIRMATION_STATUS_FOR_REQUEST) {
96 Flags = PpData.Flags;
97 RequestConfirmed = FALSE;
98
99 switch (mTcgNvs->PhysicalPresence.Request) {
100 case PHYSICAL_PRESENCE_ENABLE:
101 case PHYSICAL_PRESENCE_DISABLE:
102 case PHYSICAL_PRESENCE_ACTIVATE:
103 case PHYSICAL_PRESENCE_DEACTIVATE:
104 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE:
105 case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE:
106 case PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE:
107 case PHYSICAL_PRESENCE_SET_OWNER_INSTALL_FALSE:
108 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_OWNER_TRUE:
109 case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE_OWNER_FALSE:
110 if ((Flags & FLAG_NO_PPI_PROVISION) != 0) {
111 RequestConfirmed = TRUE;
112 }
113 break;
114
115 case PHYSICAL_PRESENCE_CLEAR:
116 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR:
117 if ((Flags & FLAG_NO_PPI_CLEAR) != 0) {
118 RequestConfirmed = TRUE;
119 }
120 break;
121
122 case PHYSICAL_PRESENCE_DEFERRED_PP_UNOWNERED_FIELD_UPGRADE:
123 if ((Flags & FLAG_NO_PPI_MAINTENANCE) != 0) {
124 RequestConfirmed = TRUE;
125 }
126 break;
127
128 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE:
129 case PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE:
130 if ((Flags & FLAG_NO_PPI_CLEAR) != 0 && (Flags & FLAG_NO_PPI_PROVISION) != 0) {
131 RequestConfirmed = TRUE;
132 }
133 break;
134
135 case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_FALSE:
136 case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_FALSE:
137 case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_FALSE:
138 case PHYSICAL_PRESENCE_NO_ACTION:
139 RequestConfirmed = TRUE;
140 break;
141
142 case PHYSICAL_PRESENCE_SET_OPERATOR_AUTH:
143 //
144 // This command requires UI to prompt user for Auth data
145 //
146 mTcgNvs->PhysicalPresence.ReturnCode = PP_REQUEST_NOT_IMPLEMENTED;
147 return EFI_SUCCESS;
148 }
149
150 if (RequestConfirmed) {
151 mTcgNvs->PhysicalPresence.ReturnCode = PP_REQUEST_ALLOWED_AND_PPUSER_NOT_REQUIRED;
152 } else {
153 mTcgNvs->PhysicalPresence.ReturnCode = PP_REQUEST_ALLOWED_AND_PPUSER_REQUIRED;
154 }
155 }
156
157 return EFI_SUCCESS;
158 }
159
160
161 /**
162 Software SMI callback for MemoryClear which is called from ACPI method.
163
164 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
165 @param[in] Context Points to an optional handler context which was specified when the
166 handler was registered.
167 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
168 be conveyed from a non-SMM environment into an SMM environment.
169 @param[in, out] CommBufferSize The size of the CommBuffer.
170
171 @retval EFI_SUCCESS The interrupt was handled successfully.
172
173 **/
174 EFI_STATUS
175 EFIAPI
176 MemoryClearCallback (
177 IN EFI_HANDLE DispatchHandle,
178 IN CONST VOID *Context,
179 IN OUT VOID *CommBuffer,
180 IN OUT UINTN *CommBufferSize
181 )
182 {
183 EFI_STATUS Status;
184 UINTN DataSize;
185 UINT8 MorControl;
186
187 mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_SUCCESS;
188 if (mTcgNvs->MemoryClear.Parameter == ACPI_FUNCTION_DSM_MEMORY_CLEAR_INTERFACE) {
189 MorControl = (UINT8) mTcgNvs->MemoryClear.Request;
190 } else if (mTcgNvs->MemoryClear.Parameter == ACPI_FUNCTION_PTS_CLEAR_MOR_BIT) {
191 DataSize = sizeof (UINT8);
192 Status = mSmmVariable->SmmGetVariable (
193 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
194 &gEfiMemoryOverwriteControlDataGuid,
195 NULL,
196 &DataSize,
197 &MorControl
198 );
199 if (EFI_ERROR (Status)) {
200 return EFI_SUCCESS;
201 }
202
203 if (MOR_CLEAR_MEMORY_VALUE (MorControl) == 0x0) {
204 return EFI_SUCCESS;
205 }
206 MorControl &= ~MOR_CLEAR_MEMORY_BIT_MASK;
207 }
208
209 DataSize = sizeof (UINT8);
210 Status = mSmmVariable->SmmSetVariable (
211 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
212 &gEfiMemoryOverwriteControlDataGuid,
213 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
214 DataSize,
215 &MorControl
216 );
217 if (EFI_ERROR (Status)) {
218 mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_GENERAL_FAILURE;
219 }
220
221 return EFI_SUCCESS;
222 }
223
224 /**
225 Find the operation region in TCG ACPI table by given Name and Size,
226 and initialize it if the region is found.
227
228 @param[in, out] Table The TPM item in ACPI table.
229 @param[in] Name The name string to find in TPM table.
230 @param[in] Size The size of the region to find.
231
232 @return The allocated address for the found region.
233
234 **/
235 VOID *
236 AssignOpRegion (
237 EFI_ACPI_DESCRIPTION_HEADER *Table,
238 UINT32 Name,
239 UINT16 Size
240 )
241 {
242 EFI_STATUS Status;
243 AML_OP_REGION_32_8 *OpRegion;
244 EFI_PHYSICAL_ADDRESS MemoryAddress;
245
246 MemoryAddress = SIZE_4GB - 1;
247
248 //
249 // Patch some pointers for the ASL code before loading the SSDT.
250 //
251 for (OpRegion = (AML_OP_REGION_32_8 *) (Table + 1);
252 OpRegion <= (AML_OP_REGION_32_8 *) ((UINT8 *) Table + Table->Length);
253 OpRegion = (AML_OP_REGION_32_8 *) ((UINT8 *) OpRegion + 1)) {
254 if ((OpRegion->OpRegionOp == AML_EXT_REGION_OP) &&
255 (OpRegion->NameString == Name) &&
256 (OpRegion->DWordPrefix == AML_DWORD_PREFIX) &&
257 (OpRegion->BytePrefix == AML_BYTE_PREFIX)) {
258
259 Status = gBS->AllocatePages(AllocateMaxAddress, EfiACPIMemoryNVS, EFI_SIZE_TO_PAGES (Size), &MemoryAddress);
260 ASSERT_EFI_ERROR (Status);
261 ZeroMem ((VOID *)(UINTN)MemoryAddress, Size);
262 OpRegion->RegionOffset = (UINT32) (UINTN) MemoryAddress;
263 OpRegion->RegionLen = (UINT8) Size;
264 break;
265 }
266 }
267
268 return (VOID *) (UINTN) MemoryAddress;
269 }
270
271 /**
272 Initialize and publish TPM items in ACPI table.
273
274 @retval EFI_SUCCESS The TCG ACPI table is published successfully.
275 @retval Others The TCG ACPI table is not published.
276
277 **/
278 EFI_STATUS
279 PublishAcpiTable (
280 VOID
281 )
282 {
283 EFI_STATUS Status;
284 EFI_ACPI_TABLE_PROTOCOL *AcpiTable;
285 UINTN TableKey;
286 EFI_ACPI_DESCRIPTION_HEADER *Table;
287 UINTN TableSize;
288
289 Status = GetSectionFromFv (
290 &gEfiCallerIdGuid,
291 EFI_SECTION_RAW,
292 0,
293 (VOID **) &Table,
294 &TableSize
295 );
296 ASSERT_EFI_ERROR (Status);
297
298 ASSERT (Table->OemTableId == SIGNATURE_64 ('T', 'c', 'g', 'T', 'a', 'b', 'l', 'e'));
299 mTcgNvs = AssignOpRegion (Table, SIGNATURE_32 ('T', 'N', 'V', 'S'), (UINT16) sizeof (TCG_NVS));
300 ASSERT (mTcgNvs != NULL);
301
302 //
303 // Publish the TPM ACPI table
304 //
305 Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **) &AcpiTable);
306 ASSERT_EFI_ERROR (Status);
307
308 TableKey = 0;
309 Status = AcpiTable->InstallAcpiTable (
310 AcpiTable,
311 Table,
312 TableSize,
313 &TableKey
314 );
315 ASSERT_EFI_ERROR (Status);
316
317 return Status;
318 }
319
320 /**
321 The driver's entry point.
322
323 It install callbacks for TPM physical presence and MemoryClear, and locate
324 SMM variable to be used in the callback function.
325
326 @param[in] ImageHandle The firmware allocated handle for the EFI image.
327 @param[in] SystemTable A pointer to the EFI System Table.
328
329 @retval EFI_SUCCESS The entry point is executed successfully.
330 @retval Others Some error occurs when executing this entry point.
331
332 **/
333 EFI_STATUS
334 EFIAPI
335 InitializeTcgSmm (
336 IN EFI_HANDLE ImageHandle,
337 IN EFI_SYSTEM_TABLE *SystemTable
338 )
339 {
340 EFI_STATUS Status;
341 EFI_SMM_SW_DISPATCH2_PROTOCOL *SwDispatch;
342 EFI_SMM_SW_REGISTER_CONTEXT SwContext;
343 EFI_HANDLE SwHandle;
344
345 Status = PublishAcpiTable ();
346 ASSERT_EFI_ERROR (Status);
347
348 //
349 // Get the Sw dispatch protocol and register SMI callback functions.
350 //
351 Status = gSmst->SmmLocateProtocol (&gEfiSmmSwDispatch2ProtocolGuid, NULL, (VOID**)&SwDispatch);
352 ASSERT_EFI_ERROR (Status);
353 SwContext.SwSmiInputValue = (UINTN) -1;
354 Status = SwDispatch->Register (SwDispatch, PhysicalPresenceCallback, &SwContext, &SwHandle);
355 ASSERT_EFI_ERROR (Status);
356 if (EFI_ERROR (Status)) {
357 return Status;
358 }
359 mTcgNvs->PhysicalPresence.SoftwareSmi = (UINT8) SwContext.SwSmiInputValue;
360
361 SwContext.SwSmiInputValue = (UINTN) -1;
362 Status = SwDispatch->Register (SwDispatch, MemoryClearCallback, &SwContext, &SwHandle);
363 ASSERT_EFI_ERROR (Status);
364 if (EFI_ERROR (Status)) {
365 return Status;
366 }
367 mTcgNvs->MemoryClear.SoftwareSmi = (UINT8) SwContext.SwSmiInputValue;
368
369 //
370 // Locate SmmVariableProtocol.
371 //
372 Status = gSmst->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID**)&mSmmVariable);
373 ASSERT_EFI_ERROR (Status);
374
375 return EFI_SUCCESS;
376 }
377