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