ab2df664d771a90bdcb0de10e569f69623c185ea
[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, 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
17 #include <PiDxe.h>
18 #include <IndustryStandard/Acpi.h>
19 #include <Guid/PhysicalPresenceData.h>
20 #include <Guid/MemoryOverwriteControl.h>
21 #include <Protocol/SmmSwDispatch2.h>
22 #include <Protocol/AcpiTable.h>
23 #include <Protocol/SmmVariable.h>
24
25 #include <Library/BaseLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/SmmServicesTableLib.h>
29 #include <Library/UefiDriverEntryPoint.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/DxeServicesLib.h>
32
33 //
34 // AML parsing definitions
35 //
36 #define AML_OPREGION_OP 0x80
37 #define AML_BYTE_PREFIX 0x0A
38 #define AML_DWORD_PREFIX 0x0C
39
40 #pragma pack(1)
41 typedef struct {
42 UINT8 SoftwareSmi;
43 UINT32 Parameter;
44 UINT32 Response;
45 UINT32 Request;
46 UINT32 LastRequest;
47 UINT32 ReturnCode;
48 } PHYSICAL_PRESENCE_NVS;
49
50 typedef struct {
51 UINT8 SoftwareSmi;
52 UINT32 Parameter;
53 UINT32 Request;
54 } MEMORY_CLEAR_NVS;
55
56 typedef struct {
57 PHYSICAL_PRESENCE_NVS PhysicalPresence;
58 MEMORY_CLEAR_NVS MemoryClear;
59 } TCG_NVS;
60
61 typedef struct {
62 UINT8 OpRegionOp;
63 UINT32 NameString;
64 UINT8 RegionSpace;
65 UINT8 DWordPrefix;
66 UINT32 RegionOffset;
67 UINT8 BytePrefix;
68 UINT8 RegionLen;
69 } AML_OP_REGION_32_8;
70 #pragma pack()
71
72 EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable;
73 TCG_NVS *mTcgNvs;
74
75 /**
76 Software SMI callback for TPM physical presence which is called from ACPI method.
77
78 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
79 @param[in] Context Points to an optional handler context which was specified when the
80 handler was registered.
81 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
82 be conveyed from a non-SMM environment into an SMM environment.
83 @param[in, out] CommBufferSize The size of the CommBuffer.
84
85 @retval EFI_SUCCESS The interrupt was handled successfully.
86
87 **/
88 EFI_STATUS
89 EFIAPI
90 PhysicalPresenceCallback (
91 IN EFI_HANDLE DispatchHandle,
92 IN CONST VOID *Context,
93 IN OUT VOID *CommBuffer,
94 IN OUT UINTN *CommBufferSize
95 )
96 {
97 EFI_STATUS Status;
98 UINTN DataSize;
99 EFI_PHYSICAL_PRESENCE PpData;
100 UINT8 Flags;
101 BOOLEAN RequestConfirmed;
102
103 //
104 // Get the Physical Presence variable
105 //
106 DataSize = sizeof (EFI_PHYSICAL_PRESENCE);
107 Status = mSmmVariable->SmmGetVariable (
108 PHYSICAL_PRESENCE_VARIABLE,
109 &gEfiPhysicalPresenceGuid,
110 NULL,
111 &DataSize,
112 &PpData
113 );
114 if (EFI_ERROR (Status)) {
115 return EFI_SUCCESS;
116 }
117
118 DEBUG ((EFI_D_INFO, "[TPM] PP callback, Parameter = %x\n", mTcgNvs->PhysicalPresence.Parameter));
119 if (mTcgNvs->PhysicalPresence.Parameter == 5) {
120 //
121 // Return TPM Operation Response to OS Environment
122 //
123 mTcgNvs->PhysicalPresence.LastRequest = PpData.LastPPRequest;
124 mTcgNvs->PhysicalPresence.Response = PpData.PPResponse;
125
126 } else if ((mTcgNvs->PhysicalPresence.Parameter == 2) || (mTcgNvs->PhysicalPresence.Parameter == 7)) {
127 //
128 // Submit TPM Operation Request to Pre-OS Environment
129 //
130
131 if (mTcgNvs->PhysicalPresence.Request == SET_OPERATOR_AUTH) {
132 //
133 // This command requires UI to prompt user for Auth data, NOT implemented.
134 //
135 mTcgNvs->PhysicalPresence.ReturnCode = 1;
136 return EFI_SUCCESS;
137 }
138
139 if (PpData.PPRequest != mTcgNvs->PhysicalPresence.Request) {
140 PpData.PPRequest = (UINT8) mTcgNvs->PhysicalPresence.Request;
141 DataSize = sizeof (EFI_PHYSICAL_PRESENCE);
142 Status = mSmmVariable->SmmSetVariable (
143 PHYSICAL_PRESENCE_VARIABLE,
144 &gEfiPhysicalPresenceGuid,
145 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
146 DataSize,
147 &PpData
148 );
149 }
150
151 if (EFI_ERROR (Status)) {
152 //
153 // General failure.
154 //
155 mTcgNvs->PhysicalPresence.ReturnCode = 2;
156 return EFI_SUCCESS;
157 }
158 mTcgNvs->PhysicalPresence.ReturnCode = 0;
159 } else if (mTcgNvs->PhysicalPresence.Parameter == 8) {
160 //
161 // Get User Confirmation Status for Operation
162 //
163 Flags = PpData.Flags;
164 RequestConfirmed = FALSE;
165
166 switch (mTcgNvs->PhysicalPresence.Request) {
167 case ENABLE:
168 case DISABLE:
169 case ACTIVATE:
170 case DEACTIVATE:
171 case ENABLE_ACTIVATE:
172 case DEACTIVATE_DISABLE:
173 case SET_OWNER_INSTALL_TRUE:
174 case SET_OWNER_INSTALL_FALSE:
175 case ENABLE_ACTIVATE_OWNER_TRUE:
176 case DEACTIVATE_DISABLE_OWNER_FALSE:
177 if ((Flags & FLAG_NO_PPI_PROVISION) != 0) {
178 RequestConfirmed = TRUE;
179 }
180 break;
181
182 case CLEAR:
183 case ENABLE_ACTIVATE_CLEAR:
184 if ((Flags & FLAG_NO_PPI_CLEAR) != 0) {
185 RequestConfirmed = TRUE;
186 }
187 break;
188
189 case DEFERRED_PP_UNOWNERED_FIELD_UPGRADE:
190 if ((Flags & FLAG_NO_PPI_MAINTENANCE) != 0) {
191 RequestConfirmed = TRUE;
192 }
193 break;
194
195 case ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE:
196 case CLEAR_ENABLE_ACTIVATE:
197 if ((Flags & FLAG_NO_PPI_CLEAR) != 0 && (Flags & FLAG_NO_PPI_PROVISION) != 0) {
198 RequestConfirmed = TRUE;
199 }
200 break;
201
202 case SET_NO_PPI_PROVISION_FALSE:
203 case SET_NO_PPI_CLEAR_FALSE:
204 case SET_NO_PPI_MAINTENANCE_FALSE:
205 case NO_ACTION:
206 RequestConfirmed = TRUE;
207 break;
208
209 case SET_OPERATOR_AUTH:
210 //
211 // This command requires UI to prompt user for Auth data
212 // Here it is NOT implemented
213 //
214 mTcgNvs->PhysicalPresence.ReturnCode = 0;
215 return EFI_SUCCESS;
216 }
217
218 if (RequestConfirmed) {
219 //
220 // Allowed and physically present user not required
221 //
222 mTcgNvs->PhysicalPresence.ReturnCode = 4;
223 } else {
224 //
225 // Allowed and physically present user required
226 //
227 mTcgNvs->PhysicalPresence.ReturnCode = 3;
228 }
229 }
230
231 return EFI_SUCCESS;
232 }
233
234
235 /**
236 Software SMI callback for MemoryClear which is called from ACPI method.
237
238 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
239 @param[in] Context Points to an optional handler context which was specified when the
240 handler was registered.
241 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
242 be conveyed from a non-SMM environment into an SMM environment.
243 @param[in, out] CommBufferSize The size of the CommBuffer.
244
245 @retval EFI_SUCCESS The interrupt was handled successfully.
246
247 **/
248 EFI_STATUS
249 EFIAPI
250 MemoryClearCallback (
251 IN EFI_HANDLE DispatchHandle,
252 IN CONST VOID *Context,
253 IN OUT VOID *CommBuffer,
254 IN OUT UINTN *CommBufferSize
255 )
256 {
257 EFI_STATUS Status;
258 UINTN DataSize;
259 UINT8 MorControl;
260
261 if (mTcgNvs->MemoryClear.Parameter == 1) {
262 //
263 // Called from ACPI _DSM method, save the MOR data to variable.
264 //
265 MorControl = (UINT8) mTcgNvs->MemoryClear.Request;
266 } else if (mTcgNvs->MemoryClear.Parameter == 2) {
267 //
268 // Called from ACPI _PTS method, setup ClearMemory flags if needed.
269 //
270 DataSize = sizeof (UINT8);
271 Status = mSmmVariable->SmmGetVariable (
272 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
273 &gEfiMemoryOverwriteControlDataGuid,
274 NULL,
275 &DataSize,
276 &MorControl
277 );
278 if (EFI_ERROR (Status)) {
279 ASSERT (Status == EFI_NOT_FOUND);
280 return EFI_SUCCESS;
281 }
282
283 if (MOR_CLEAR_MEMORY_VALUE (MorControl) == 0x0) {
284 return EFI_SUCCESS;
285 }
286 MorControl &= ~MOR_CLEAR_MEMORY_BIT_MASK;
287 }
288
289 DataSize = sizeof (UINT8);
290 Status = mSmmVariable->SmmSetVariable (
291 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
292 &gEfiMemoryOverwriteControlDataGuid,
293 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
294 DataSize,
295 &MorControl
296 );
297 ASSERT_EFI_ERROR (Status);
298
299 return EFI_SUCCESS;
300 }
301
302 /**
303 Find the operation region in TCG ACPI table by given Name and Size,
304 and initialize it if the region is found.
305
306 @param[in, out] Table The TPM item in ACPI table.
307 @param[in] Name The name string to find in TPM table.
308 @param[in] Size The size of the region to find.
309
310 @return The allocated address for the found region.
311
312 **/
313 VOID *
314 AssignOpRegion (
315 EFI_ACPI_DESCRIPTION_HEADER *Table,
316 UINT32 Name,
317 UINT16 Size
318 )
319 {
320 EFI_STATUS Status;
321 AML_OP_REGION_32_8 *OpRegion;
322 EFI_PHYSICAL_ADDRESS MemoryAddress;
323
324 MemoryAddress = SIZE_4GB - 1;
325
326 //
327 // Patch some pointers for the ASL code before loading the SSDT.
328 //
329 for (OpRegion = (AML_OP_REGION_32_8 *) (Table + 1);
330 OpRegion <= (AML_OP_REGION_32_8 *) ((UINT8 *) Table + Table->Length);
331 OpRegion = (AML_OP_REGION_32_8 *) ((UINT8 *) OpRegion + 1)) {
332 if ((OpRegion->OpRegionOp == AML_OPREGION_OP) &&
333 (OpRegion->NameString == Name) &&
334 (OpRegion->RegionLen == Size) &&
335 (OpRegion->DWordPrefix == AML_DWORD_PREFIX) &&
336 (OpRegion->BytePrefix == AML_BYTE_PREFIX)) {
337
338 Status = gBS->AllocatePages(AllocateMaxAddress, EfiACPIMemoryNVS, EFI_SIZE_TO_PAGES (Size), &MemoryAddress);
339 ASSERT_EFI_ERROR (Status);
340 ZeroMem ((VOID *)(UINTN)MemoryAddress, Size);
341 OpRegion->RegionOffset = (UINT32) (UINTN) MemoryAddress;
342 break;
343 }
344 }
345
346 return (VOID *) (UINTN) MemoryAddress;
347 }
348
349 /**
350 Initialize and publish TPM items in ACPI table.
351
352 @retval EFI_SUCCESS The TCG ACPI table is published successfully.
353 @retval Others The TCG ACPI table is not published.
354
355 **/
356 EFI_STATUS
357 PublishAcpiTable (
358 VOID
359 )
360 {
361 EFI_STATUS Status;
362 EFI_ACPI_TABLE_PROTOCOL *AcpiTable;
363 UINTN TableKey;
364 EFI_ACPI_DESCRIPTION_HEADER *Table;
365 UINTN TableSize;
366
367 Status = GetSectionFromFv (
368 &gEfiCallerIdGuid,
369 EFI_SECTION_RAW,
370 0,
371 (VOID **) &Table,
372 &TableSize
373 );
374 ASSERT_EFI_ERROR (Status);
375
376 ASSERT (Table->OemTableId == SIGNATURE_64 ('T', 'c', 'g', 'T', 'a', 'b', 'l', 'e'));
377 mTcgNvs = AssignOpRegion (Table, SIGNATURE_32 ('T', 'N', 'V', 'S'), (UINT16) sizeof (TCG_NVS));
378 ASSERT (mTcgNvs != NULL);
379
380 //
381 // Publish the TPM ACPI table
382 //
383 Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **) &AcpiTable);
384 ASSERT_EFI_ERROR (Status);
385
386 TableKey = 0;
387 Status = AcpiTable->InstallAcpiTable (
388 AcpiTable,
389 Table,
390 TableSize,
391 &TableKey
392 );
393 ASSERT_EFI_ERROR (Status);
394
395 return Status;
396 }
397
398 /**
399 The driver's entry point.
400
401 It install callbacks for TPM physical presence and MemoryClear, and locate
402 SMM variable to be used in the callback function.
403
404 @param[in] ImageHandle The firmware allocated handle for the EFI image.
405 @param[in] SystemTable A pointer to the EFI System Table.
406
407 @retval EFI_SUCCESS The entry point is executed successfully.
408 @retval Others Some error occurs when executing this entry point.
409
410 **/
411 EFI_STATUS
412 EFIAPI
413 InitializeTcgSmm (
414 IN EFI_HANDLE ImageHandle,
415 IN EFI_SYSTEM_TABLE *SystemTable
416 )
417 {
418 EFI_STATUS Status;
419 EFI_SMM_SW_DISPATCH2_PROTOCOL *SwDispatch;
420 EFI_SMM_SW_REGISTER_CONTEXT SwContext;
421 EFI_HANDLE SwHandle;
422
423 Status = PublishAcpiTable ();
424 ASSERT_EFI_ERROR (Status);
425
426 //
427 // Get the Sw dispatch protocol and register SMI callback functions.
428 //
429 Status = gSmst->SmmLocateProtocol (&gEfiSmmSwDispatch2ProtocolGuid, NULL, (VOID**)&SwDispatch);
430 ASSERT_EFI_ERROR (Status);
431 SwContext.SwSmiInputValue = (UINTN) -1;
432 Status = SwDispatch->Register (SwDispatch, PhysicalPresenceCallback, &SwContext, &SwHandle);
433 ASSERT_EFI_ERROR (Status);
434 if (EFI_ERROR (Status)) {
435 return Status;
436 }
437 mTcgNvs->PhysicalPresence.SoftwareSmi = (UINT8) SwContext.SwSmiInputValue;
438
439 SwContext.SwSmiInputValue = (UINTN) -1;
440 Status = SwDispatch->Register (SwDispatch, MemoryClearCallback, &SwContext, &SwHandle);
441 ASSERT_EFI_ERROR (Status);
442 if (EFI_ERROR (Status)) {
443 return Status;
444 }
445 mTcgNvs->MemoryClear.SoftwareSmi = (UINT8) SwContext.SwSmiInputValue;
446
447 //
448 // Locate SmmVariableProtocol.
449 //
450 Status = gSmst->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID**)&mSmmVariable);
451 ASSERT_EFI_ERROR (Status);
452
453 return EFI_SUCCESS;
454 }
455