Fix the return status when physical presence variable and MemoryOverwriteRequestContr...
[mirror_edk2.git] / SecurityPkg / Tcg / TrEESmm / TrEESmm.c
1 /** @file
2 It updates TPM2 items in ACPI table and registers SMI2 callback
3 functions for TrEE physical presence, ClearMemory, and sample
4 for dTPM StartMethod.
5
6 Caution: This module requires additional review when modified.
7 This driver will have external input - variable and ACPINvs data in SMM mode.
8 This external input must be validated carefully to avoid security issue.
9
10 PhysicalPresenceCallback() and MemoryClearCallback() will receive untrusted input and do some check.
11
12 Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>
13 This program and the accompanying materials
14 are licensed and made available under the terms and conditions of the BSD License
15 which accompanies this distribution. The full text of the license may be found at
16 http://opensource.org/licenses/bsd-license.php
17
18 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
19 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20
21 **/
22
23 #include "TrEESmm.h"
24
25 EFI_TPM2_ACPI_TABLE mTpm2AcpiTemplate = {
26 {
27 EFI_ACPI_5_0_TRUSTED_COMPUTING_PLATFORM_2_TABLE_SIGNATURE,
28 sizeof (mTpm2AcpiTemplate),
29 EFI_TPM2_ACPI_TABLE_REVISION,
30 //
31 // Compiler initializes the remaining bytes to 0
32 // These fields should be filled in in production
33 //
34 },
35 0, // Flags
36 0, // Control Area
37 EFI_TPM2_ACPI_TABLE_START_METHOD_TIS, // StartMethod
38 };
39
40 EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable;
41 TCG_NVS *mTcgNvs;
42
43 /**
44 Software SMI callback for TPM physical presence which is called from ACPI method.
45
46 Caution: This function may receive untrusted input.
47 Variable and ACPINvs are external input, so this function will validate
48 its data structure to be valid value.
49
50 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
51 @param[in] Context Points to an optional handler context which was specified when the
52 handler was registered.
53 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
54 be conveyed from a non-SMM environment into an SMM environment.
55 @param[in, out] CommBufferSize The size of the CommBuffer.
56
57 @retval EFI_SUCCESS The interrupt was handled successfully.
58
59 **/
60 EFI_STATUS
61 EFIAPI
62 PhysicalPresenceCallback (
63 IN EFI_HANDLE DispatchHandle,
64 IN CONST VOID *Context,
65 IN OUT VOID *CommBuffer,
66 IN OUT UINTN *CommBufferSize
67 )
68 {
69 EFI_STATUS Status;
70 UINTN DataSize;
71 EFI_TREE_PHYSICAL_PRESENCE PpData;
72 UINT8 Flags;
73 BOOLEAN RequestConfirmed;
74
75 //
76 // Get the Physical Presence variable
77 //
78 DataSize = sizeof (EFI_TREE_PHYSICAL_PRESENCE);
79 Status = mSmmVariable->SmmGetVariable (
80 TREE_PHYSICAL_PRESENCE_VARIABLE,
81 &gEfiTrEEPhysicalPresenceGuid,
82 NULL,
83 &DataSize,
84 &PpData
85 );
86 if (EFI_ERROR (Status)) {
87 mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_GENERAL_FAILURE;
88 DEBUG ((EFI_D_ERROR, "[TPM] Get PP variable failure! Status = %r\n", Status));
89 return EFI_SUCCESS;
90 }
91
92 DEBUG ((EFI_D_INFO, "[TPM2] PP callback, Parameter = %x, Request = %x\n", mTcgNvs->PhysicalPresence.Parameter, mTcgNvs->PhysicalPresence.Request));
93
94 if (mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_RETURN_REQUEST_RESPONSE_TO_OS) {
95 mTcgNvs->PhysicalPresence.LastRequest = PpData.LastPPRequest;
96 mTcgNvs->PhysicalPresence.Response = PpData.PPResponse;
97 } else if ((mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS)
98 || (mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS_2)) {
99 if (mTcgNvs->PhysicalPresence.Request > TREE_PHYSICAL_PRESENCE_NO_ACTION_MAX) {
100 //
101 // This command requires UI to prompt user for Auth data.
102 //
103 mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_NOT_IMPLEMENTED;
104 return EFI_SUCCESS;
105 }
106
107 if (PpData.PPRequest != mTcgNvs->PhysicalPresence.Request) {
108 PpData.PPRequest = (UINT8) mTcgNvs->PhysicalPresence.Request;
109 DataSize = sizeof (EFI_TREE_PHYSICAL_PRESENCE);
110 Status = mSmmVariable->SmmSetVariable (
111 TREE_PHYSICAL_PRESENCE_VARIABLE,
112 &gEfiTrEEPhysicalPresenceGuid,
113 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
114 DataSize,
115 &PpData
116 );
117 }
118
119 if (EFI_ERROR (Status)) {
120 mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_GENERAL_FAILURE;
121 DEBUG ((EFI_D_ERROR, "[TPM] Set PP variable failure! Status = %r\n", Status));
122 return EFI_SUCCESS;
123 }
124 mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_SUCCESS;
125 } else if (mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_GET_USER_CONFIRMATION_STATUS_FOR_REQUEST) {
126 //
127 // Get the Physical Presence flags
128 //
129 DataSize = sizeof (UINT8);
130 Status = mSmmVariable->SmmGetVariable (
131 TREE_PHYSICAL_PRESENCE_FLAGS_VARIABLE,
132 &gEfiTrEEPhysicalPresenceGuid,
133 NULL,
134 &DataSize,
135 &Flags
136 );
137 if (EFI_ERROR (Status)) {
138 mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_GENERAL_FAILURE;
139 DEBUG ((EFI_D_ERROR, "[TPM] Get PP flags failure! Status = %r\n", Status));
140 return EFI_SUCCESS;
141 }
142
143 RequestConfirmed = FALSE;
144
145 switch (mTcgNvs->PhysicalPresence.Request) {
146
147 case TREE_PHYSICAL_PRESENCE_CLEAR_CONTROL_CLEAR:
148 case TREE_PHYSICAL_PRESENCE_CLEAR_CONTROL_CLEAR_2:
149 case TREE_PHYSICAL_PRESENCE_CLEAR_CONTROL_CLEAR_3:
150 case TREE_PHYSICAL_PRESENCE_CLEAR_CONTROL_CLEAR_4:
151 if ((Flags & TREE_FLAG_NO_PPI_CLEAR) != 0) {
152 RequestConfirmed = TRUE;
153 }
154 break;
155
156 case TREE_PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_FALSE:
157 RequestConfirmed = TRUE;
158 break;
159
160 case TREE_PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_TRUE:
161 break;
162
163 default:
164 if (mTcgNvs->PhysicalPresence.Request <= TREE_PHYSICAL_PRESENCE_NO_ACTION_MAX) {
165 RequestConfirmed = TRUE;
166 } else {
167 mTcgNvs->PhysicalPresence.ReturnCode = PP_REQUEST_NOT_IMPLEMENTED;
168 return EFI_SUCCESS;
169 }
170 break;
171 }
172
173 if (RequestConfirmed) {
174 mTcgNvs->PhysicalPresence.ReturnCode = PP_REQUEST_ALLOWED_AND_PPUSER_NOT_REQUIRED;
175 } else {
176 mTcgNvs->PhysicalPresence.ReturnCode = PP_REQUEST_ALLOWED_AND_PPUSER_REQUIRED;
177 }
178 }
179
180 return EFI_SUCCESS;
181 }
182
183
184 /**
185 Software SMI callback for MemoryClear which is called from ACPI method.
186
187 Caution: This function may receive untrusted input.
188 Variable and ACPINvs are external input, so this function will validate
189 its data structure to be valid value.
190
191 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
192 @param[in] Context Points to an optional handler context which was specified when the
193 handler was registered.
194 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
195 be conveyed from a non-SMM environment into an SMM environment.
196 @param[in, out] CommBufferSize The size of the CommBuffer.
197
198 @retval EFI_SUCCESS The interrupt was handled successfully.
199
200 **/
201 EFI_STATUS
202 EFIAPI
203 MemoryClearCallback (
204 IN EFI_HANDLE DispatchHandle,
205 IN CONST VOID *Context,
206 IN OUT VOID *CommBuffer,
207 IN OUT UINTN *CommBufferSize
208 )
209 {
210 EFI_STATUS Status;
211 UINTN DataSize;
212 UINT8 MorControl;
213
214 mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_SUCCESS;
215 if (mTcgNvs->MemoryClear.Parameter == ACPI_FUNCTION_DSM_MEMORY_CLEAR_INTERFACE) {
216 MorControl = (UINT8) mTcgNvs->MemoryClear.Request;
217 } else if (mTcgNvs->MemoryClear.Parameter == ACPI_FUNCTION_PTS_CLEAR_MOR_BIT) {
218 DataSize = sizeof (UINT8);
219 Status = mSmmVariable->SmmGetVariable (
220 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
221 &gEfiMemoryOverwriteControlDataGuid,
222 NULL,
223 &DataSize,
224 &MorControl
225 );
226 if (EFI_ERROR (Status)) {
227 mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_GENERAL_FAILURE;
228 DEBUG ((EFI_D_ERROR, "[TPM] Get MOR variable failure! Status = %r\n", Status));
229 return EFI_SUCCESS;
230 }
231
232 if (MOR_CLEAR_MEMORY_VALUE (MorControl) == 0x0) {
233 return EFI_SUCCESS;
234 }
235 MorControl &= ~MOR_CLEAR_MEMORY_BIT_MASK;
236 }
237
238 DataSize = sizeof (UINT8);
239 Status = mSmmVariable->SmmSetVariable (
240 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
241 &gEfiMemoryOverwriteControlDataGuid,
242 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
243 DataSize,
244 &MorControl
245 );
246 if (EFI_ERROR (Status)) {
247 mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_GENERAL_FAILURE;
248 DEBUG ((EFI_D_ERROR, "[TPM] Set MOR variable failure! Status = %r\n", Status));
249 }
250
251 return EFI_SUCCESS;
252 }
253
254 /**
255 Find the operation region in TCG ACPI table by given Name and Size,
256 and initialize it if the region is found.
257
258 @param[in, out] Table The TPM item in ACPI table.
259 @param[in] Name The name string to find in TPM table.
260 @param[in] Size The size of the region to find.
261
262 @return The allocated address for the found region.
263
264 **/
265 VOID *
266 AssignOpRegion (
267 EFI_ACPI_DESCRIPTION_HEADER *Table,
268 UINT32 Name,
269 UINT16 Size
270 )
271 {
272 EFI_STATUS Status;
273 AML_OP_REGION_32_8 *OpRegion;
274 EFI_PHYSICAL_ADDRESS MemoryAddress;
275
276 MemoryAddress = SIZE_4GB - 1;
277
278 //
279 // Patch some pointers for the ASL code before loading the SSDT.
280 //
281 for (OpRegion = (AML_OP_REGION_32_8 *) (Table + 1);
282 OpRegion <= (AML_OP_REGION_32_8 *) ((UINT8 *) Table + Table->Length);
283 OpRegion = (AML_OP_REGION_32_8 *) ((UINT8 *) OpRegion + 1)) {
284 if ((OpRegion->OpRegionOp == AML_EXT_REGION_OP) &&
285 (OpRegion->NameString == Name) &&
286 (OpRegion->DWordPrefix == AML_DWORD_PREFIX) &&
287 (OpRegion->BytePrefix == AML_BYTE_PREFIX)) {
288
289 Status = gBS->AllocatePages(AllocateMaxAddress, EfiACPIMemoryNVS, EFI_SIZE_TO_PAGES (Size), &MemoryAddress);
290 ASSERT_EFI_ERROR (Status);
291 ZeroMem ((VOID *)(UINTN)MemoryAddress, Size);
292 OpRegion->RegionOffset = (UINT32) (UINTN) MemoryAddress;
293 OpRegion->RegionLen = (UINT8) Size;
294 break;
295 }
296 }
297
298 return (VOID *) (UINTN) MemoryAddress;
299 }
300
301 /**
302 Initialize and publish TPM items in ACPI table.
303
304 @retval EFI_SUCCESS The TCG ACPI table is published successfully.
305 @retval Others The TCG ACPI table is not published.
306
307 **/
308 EFI_STATUS
309 PublishAcpiTable (
310 VOID
311 )
312 {
313 EFI_STATUS Status;
314 EFI_ACPI_TABLE_PROTOCOL *AcpiTable;
315 UINTN TableKey;
316 EFI_ACPI_DESCRIPTION_HEADER *Table;
317 UINTN TableSize;
318
319 Status = GetSectionFromFv (
320 &gEfiCallerIdGuid,
321 EFI_SECTION_RAW,
322 0,
323 (VOID **) &Table,
324 &TableSize
325 );
326 ASSERT_EFI_ERROR (Status);
327
328
329 //
330 // Measure to PCR[0] with event EV_POST_CODE ACPI DATA
331 //
332 TpmMeasureAndLogData(
333 0,
334 EV_POST_CODE,
335 EV_POSTCODE_INFO_ACPI_DATA,
336 ACPI_DATA_LEN,
337 Table,
338 TableSize
339 );
340
341
342 ASSERT (Table->OemTableId == SIGNATURE_64 ('T', 'p', 'm', '2', 'T', 'a', 'b', 'l'));
343 CopyMem (Table->OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (Table->OemId) );
344 mTcgNvs = AssignOpRegion (Table, SIGNATURE_32 ('T', 'N', 'V', 'S'), (UINT16) sizeof (TCG_NVS));
345 ASSERT (mTcgNvs != NULL);
346
347 //
348 // Publish the TPM ACPI table
349 //
350 Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **) &AcpiTable);
351 ASSERT_EFI_ERROR (Status);
352
353 TableKey = 0;
354 Status = AcpiTable->InstallAcpiTable (
355 AcpiTable,
356 Table,
357 TableSize,
358 &TableKey
359 );
360 ASSERT_EFI_ERROR (Status);
361
362 return Status;
363 }
364
365 /**
366 Publish TPM2 ACPI table
367
368 @retval EFI_SUCCESS The TPM2 ACPI table is published successfully.
369 @retval Others The TPM2 ACPI table is not published.
370
371 **/
372 EFI_STATUS
373 PublishTpm2 (
374 VOID
375 )
376 {
377 EFI_STATUS Status;
378 EFI_ACPI_TABLE_PROTOCOL *AcpiTable;
379 UINTN TableKey;
380 UINT64 OemTableId;
381
382 //
383 // Measure to PCR[0] with event EV_POST_CODE ACPI DATA
384 //
385 TpmMeasureAndLogData(
386 0,
387 EV_POST_CODE,
388 EV_POSTCODE_INFO_ACPI_DATA,
389 ACPI_DATA_LEN,
390 &mTpm2AcpiTemplate,
391 sizeof(mTpm2AcpiTemplate)
392 );
393
394 CopyMem (mTpm2AcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTpm2AcpiTemplate.Header.OemId));
395 OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);
396 CopyMem (&mTpm2AcpiTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));
397 mTpm2AcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
398 mTpm2AcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
399 mTpm2AcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
400
401 //
402 // Construct ACPI table
403 //
404 Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **) &AcpiTable);
405 ASSERT_EFI_ERROR (Status);
406
407 Status = AcpiTable->InstallAcpiTable (
408 AcpiTable,
409 &mTpm2AcpiTemplate,
410 sizeof(mTpm2AcpiTemplate),
411 &TableKey
412 );
413 ASSERT_EFI_ERROR (Status);
414
415 return Status;
416 }
417
418 /**
419 The driver's entry point.
420
421 It install callbacks for TPM physical presence and MemoryClear, and locate
422 SMM variable to be used in the callback function.
423
424 @param[in] ImageHandle The firmware allocated handle for the EFI image.
425 @param[in] SystemTable A pointer to the EFI System Table.
426
427 @retval EFI_SUCCESS The entry point is executed successfully.
428 @retval Others Some error occurs when executing this entry point.
429
430 **/
431 EFI_STATUS
432 EFIAPI
433 InitializeTcgSmm (
434 IN EFI_HANDLE ImageHandle,
435 IN EFI_SYSTEM_TABLE *SystemTable
436 )
437 {
438 EFI_STATUS Status;
439 EFI_SMM_SW_DISPATCH2_PROTOCOL *SwDispatch;
440 EFI_SMM_SW_REGISTER_CONTEXT SwContext;
441 EFI_HANDLE SwHandle;
442
443 if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm20DtpmGuid)){
444 DEBUG ((EFI_D_ERROR, "No TPM2 DTPM instance required!\n"));
445 return EFI_UNSUPPORTED;
446 }
447
448 Status = PublishAcpiTable ();
449 ASSERT_EFI_ERROR (Status);
450
451 //
452 // Get the Sw dispatch protocol and register SMI callback functions.
453 //
454 Status = gSmst->SmmLocateProtocol (&gEfiSmmSwDispatch2ProtocolGuid, NULL, (VOID**)&SwDispatch);
455 ASSERT_EFI_ERROR (Status);
456 SwContext.SwSmiInputValue = (UINTN) -1;
457 Status = SwDispatch->Register (SwDispatch, PhysicalPresenceCallback, &SwContext, &SwHandle);
458 ASSERT_EFI_ERROR (Status);
459 if (EFI_ERROR (Status)) {
460 return Status;
461 }
462 mTcgNvs->PhysicalPresence.SoftwareSmi = (UINT8) SwContext.SwSmiInputValue;
463
464 SwContext.SwSmiInputValue = (UINTN) -1;
465 Status = SwDispatch->Register (SwDispatch, MemoryClearCallback, &SwContext, &SwHandle);
466 ASSERT_EFI_ERROR (Status);
467 if (EFI_ERROR (Status)) {
468 return Status;
469 }
470 mTcgNvs->MemoryClear.SoftwareSmi = (UINT8) SwContext.SwSmiInputValue;
471
472 //
473 // Locate SmmVariableProtocol.
474 //
475 Status = gSmst->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID**)&mSmmVariable);
476 ASSERT_EFI_ERROR (Status);
477
478 //
479 // Set TPM2 ACPI table
480 //
481 Status = PublishTpm2 ();
482 ASSERT_EFI_ERROR (Status);
483
484
485 return EFI_SUCCESS;
486 }
487