]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/TrEEDxe/TrEEDxe.c
Update code to support VS2013 tool chain.
[mirror_edk2.git] / SecurityPkg / Tcg / TrEEDxe / TrEEDxe.c
CommitLineData
c1d93242
JY
1/** @file\r
2 This module implements TrEE Protocol.\r
3 \r
4Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <PiDxe.h>\r
16#include <IndustryStandard/Acpi.h>\r
17#include <IndustryStandard/PeImage.h>\r
18#include <IndustryStandard/SmBios.h>\r
19#include <IndustryStandard/TcpaAcpi.h>\r
20\r
21#include <Guid/GlobalVariable.h>\r
22#include <Guid/SmBios.h>\r
23#include <Guid/HobList.h>\r
24#include <Guid/TcgEventHob.h>\r
25#include <Guid/EventGroup.h>\r
26#include <Guid/EventExitBootServiceFailed.h>\r
27#include <Guid/ImageAuthentication.h>\r
28#include <Guid/TpmInstance.h>\r
29\r
30#include <Protocol/DevicePath.h>\r
31#include <Protocol/AcpiTable.h>\r
32#include <Protocol/MpService.h>\r
33#include <Protocol/VariableWrite.h>\r
34#include <Protocol/TrEEProtocol.h>\r
35\r
36#include <Library/DebugLib.h>\r
37#include <Library/BaseMemoryLib.h>\r
38#include <Library/UefiRuntimeServicesTableLib.h>\r
39#include <Library/UefiDriverEntryPoint.h>\r
40#include <Library/HobLib.h>\r
41#include <Library/UefiBootServicesTableLib.h>\r
42#include <Library/BaseLib.h>\r
43#include <Library/MemoryAllocationLib.h>\r
44#include <Library/PrintLib.h>\r
45#include <Library/Tpm2CommandLib.h>\r
46#include <Library/PcdLib.h>\r
47#include <Library/UefiLib.h>\r
48#include <Library/Tpm2DeviceLib.h>\r
49#include <Library/HashLib.h>\r
50#include <Library/PerformanceLib.h>\r
51\r
52#define PERF_ID_TREE_DXE 0x3120\r
53\r
54typedef struct {\r
55 CHAR16 *VariableName;\r
56 EFI_GUID *VendorGuid;\r
57} VARIABLE_TYPE;\r
58\r
59#define EFI_TCG_LOG_AREA_SIZE 0x10000\r
60\r
61#define TREE_DEFAULT_MAX_COMMAND_SIZE 0x1000\r
62#define TREE_DEFAULT_MAX_RESPONSE_SIZE 0x1000\r
63\r
64typedef struct {\r
65 EFI_GUID *EventGuid;\r
66 TREE_EVENT_LOG_FORMAT LogFormat;\r
67 UINT32 BootHashAlg;\r
68 UINT16 DigestAlgID;\r
69 TPMI_ALG_HASH TpmHashAlgo;\r
70} TREE_EVENT_INFO_STRUCT;\r
71\r
72TREE_EVENT_INFO_STRUCT mTreeEventInfo[] = {\r
73 {&gTcgEventEntryHobGuid, TREE_EVENT_LOG_FORMAT_TCG_1_2, TREE_BOOT_HASH_ALG_SHA1, 0, TPM_ALG_SHA1},\r
74};\r
75\r
76#define TCG_EVENT_LOG_AREA_COUNT_MAX 5\r
77\r
78typedef struct {\r
79 TREE_EVENT_LOG_FORMAT EventLogFormat;\r
80 EFI_PHYSICAL_ADDRESS Lasa;\r
81 UINT64 Laml;\r
82 UINTN EventLogSize;\r
83 UINT8 *LastEvent;\r
84 BOOLEAN EventLogStarted;\r
85 BOOLEAN EventLogTruncated;\r
86} TCG_EVENT_LOG_AREA_STRUCT;\r
87\r
88typedef struct _TCG_DXE_DATA {\r
89 TREE_BOOT_SERVICE_CAPABILITY BsCap;\r
90 EFI_TCG_CLIENT_ACPI_TABLE *TcgClientAcpiTable;\r
91 EFI_TCG_SERVER_ACPI_TABLE *TcgServerAcpiTable;\r
92 TCG_EVENT_LOG_AREA_STRUCT EventLogAreaStruct[TCG_EVENT_LOG_AREA_COUNT_MAX];\r
93} TCG_DXE_DATA;\r
94\r
95EFI_TCG_CLIENT_ACPI_TABLE mTcgClientAcpiTemplate = {\r
96 {\r
97 EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE,\r
98 sizeof (mTcgClientAcpiTemplate),\r
99 0x02 //Revision\r
100 //\r
101 // Compiler initializes the remaining bytes to 0\r
102 // These fields should be filled in in production\r
103 //\r
104 },\r
105 0, // 0 for PC Client Platform Class\r
106 0, // Log Area Max Length\r
107 (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1) // Log Area Start Address\r
108};\r
109\r
110//\r
111// The following EFI_TCG_SERVER_ACPI_TABLE default setting is just one example,\r
112// the TPM device connectes to LPC, and also defined the ACPI _UID as 0xFF,\r
113// this _UID can be changed and should match with the _UID setting of the TPM \r
114// ACPI device object \r
115//\r
116EFI_TCG_SERVER_ACPI_TABLE mTcgServerAcpiTemplate = {\r
117 {\r
118 EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE,\r
119 sizeof (mTcgServerAcpiTemplate),\r
120 0x02 //Revision\r
121 //\r
122 // Compiler initializes the remaining bytes to 0\r
123 // These fields should be filled in in production\r
124 //\r
125 },\r
126 1, // 1 for Server Platform Class\r
127 0, // Reserved\r
128 0, // Log Area Max Length\r
129 (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1), // Log Area Start Address\r
130 0x0100, // TCG Specification revision 1.0\r
131 2, // Device Flags\r
132 0, // Interrupt Flags\r
133 0, // GPE\r
134 {0}, // Reserved 3 bytes\r
135 0, // Global System Interrupt\r
136 {\r
137 EFI_ACPI_3_0_SYSTEM_MEMORY,\r
138 0,\r
139 0,\r
140 EFI_ACPI_3_0_BYTE,\r
141 0x0 // Base Address\r
142 },\r
143 0, // Reserved\r
144 {0}, // Configuration Address\r
145 0xFF, // ACPI _UID value of the device, can be changed for different platforms\r
146 0, // ACPI _UID value of the device, can be changed for different platforms\r
147 0, // ACPI _UID value of the device, can be changed for different platforms\r
148 0 // ACPI _UID value of the device, can be changed for different platforms\r
149};\r
150\r
151TCG_DXE_DATA mTcgDxeData = {\r
152 {\r
153 sizeof (TREE_BOOT_SERVICE_CAPABILITY_1_0), // Size\r
154 { 1, 0 }, // StructureVersion\r
155 { 1, 0 }, // ProtocolVersion\r
156 TREE_BOOT_HASH_ALG_SHA1, // HashAlgorithmBitmap\r
157 TREE_EVENT_LOG_FORMAT_TCG_1_2, // SupportedEventLogs\r
158 TRUE, // TrEEPresentFlag\r
159 TREE_DEFAULT_MAX_COMMAND_SIZE, // MaxCommandSize\r
160 TREE_DEFAULT_MAX_RESPONSE_SIZE, // MaxResponseSize\r
161 0 // ManufacturerID\r
162 },\r
163 &mTcgClientAcpiTemplate,\r
164 &mTcgServerAcpiTemplate,\r
165};\r
166\r
167UINTN mBootAttempts = 0;\r
168CHAR16 mBootVarName[] = L"BootOrder";\r
169\r
170VARIABLE_TYPE mVariableType[] = {\r
171 {EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid},\r
172 {EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid},\r
173 {EFI_KEY_EXCHANGE_KEY_NAME, &gEfiGlobalVariableGuid},\r
174 {EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid},\r
175 {EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid},\r
176};\r
177\r
178EFI_HANDLE mImageHandle;\r
179\r
180/**\r
181 Measure PE image into TPM log based on the authenticode image hashing in\r
182 PE/COFF Specification 8.0 Appendix A.\r
183\r
184 Caution: This function may receive untrusted input.\r
185 PE/COFF image is external input, so this function will validate its data structure\r
186 within this image buffer before use.\r
187\r
188 @param[in] PCRIndex TPM PCR index\r
189 @param[in] ImageAddress Start address of image buffer.\r
190 @param[in] ImageSize Image size\r
191 @param[out] DigestList Digeest list of this image.\r
192\r
193 @retval EFI_SUCCESS Successfully measure image.\r
194 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.\r
195 @retval other error value\r
196**/\r
197EFI_STATUS\r
198MeasurePeImageAndExtend (\r
199 IN UINT32 PCRIndex,\r
200 IN EFI_PHYSICAL_ADDRESS ImageAddress,\r
201 IN UINTN ImageSize,\r
202 OUT TPML_DIGEST_VALUES *DigestList\r
203 );\r
204\r
205/**\r
206\r
207 This function dump raw data.\r
208\r
209 @param Data raw data\r
210 @param Size raw data size\r
211\r
212**/\r
213VOID\r
214InternalDumpData (\r
215 IN UINT8 *Data,\r
216 IN UINTN Size\r
217 )\r
218{\r
219 UINTN Index;\r
220 for (Index = 0; Index < Size; Index++) {\r
221 DEBUG ((EFI_D_INFO, "%02x", (UINTN)Data[Index]));\r
222 }\r
223}\r
224\r
225/**\r
226\r
227 This function dump raw data with colume format.\r
228\r
229 @param Data raw data\r
230 @param Size raw data size\r
231\r
232**/\r
233VOID\r
234InternalDumpHex (\r
235 IN UINT8 *Data,\r
236 IN UINTN Size\r
237 )\r
238{\r
239 UINTN Index;\r
240 UINTN Count;\r
241 UINTN Left;\r
242\r
243#define COLUME_SIZE (16 * 2)\r
244\r
245 Count = Size / COLUME_SIZE;\r
246 Left = Size % COLUME_SIZE;\r
247 for (Index = 0; Index < Count; Index++) {\r
248 DEBUG ((EFI_D_INFO, "%04x: ", Index * COLUME_SIZE));\r
249 InternalDumpData (Data + Index * COLUME_SIZE, COLUME_SIZE);\r
250 DEBUG ((EFI_D_INFO, "\n"));\r
251 }\r
252\r
253 if (Left != 0) {\r
254 DEBUG ((EFI_D_INFO, "%04x: ", Index * COLUME_SIZE));\r
255 InternalDumpData (Data + Index * COLUME_SIZE, Left);\r
256 DEBUG ((EFI_D_INFO, "\n"));\r
257 }\r
258}\r
259\r
260/**\r
261 Get All processors EFI_CPU_LOCATION in system. LocationBuf is allocated inside the function\r
262 Caller is responsible to free LocationBuf.\r
263\r
264 @param[out] LocationBuf Returns Processor Location Buffer.\r
265 @param[out] Num Returns processor number.\r
266\r
267 @retval EFI_SUCCESS Operation completed successfully.\r
268 @retval EFI_UNSUPPORTED MpService protocol not found.\r
269\r
270**/\r
271EFI_STATUS\r
272GetProcessorsCpuLocation (\r
273 OUT EFI_CPU_PHYSICAL_LOCATION **LocationBuf,\r
274 OUT UINTN *Num\r
275 )\r
276{\r
277 EFI_STATUS Status;\r
278 EFI_MP_SERVICES_PROTOCOL *MpProtocol;\r
279 UINTN ProcessorNum;\r
280 UINTN EnabledProcessorNum;\r
281 EFI_PROCESSOR_INFORMATION ProcessorInfo;\r
282 EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;\r
283 UINTN Index;\r
284\r
285 Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **) &MpProtocol);\r
286 if (EFI_ERROR (Status)) {\r
287 //\r
288 // MP protocol is not installed\r
289 //\r
290 return EFI_UNSUPPORTED;\r
291 }\r
292\r
293 Status = MpProtocol->GetNumberOfProcessors(\r
294 MpProtocol,\r
295 &ProcessorNum,\r
296 &EnabledProcessorNum\r
297 );\r
298 if (EFI_ERROR(Status)){\r
299 return Status;\r
300 }\r
301\r
302 Status = gBS->AllocatePool(\r
303 EfiBootServicesData,\r
304 sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,\r
305 (VOID **) &ProcessorLocBuf\r
306 );\r
307 if (EFI_ERROR(Status)){\r
308 return Status;\r
309 }\r
310\r
311 //\r
312 // Get each processor Location info\r
313 //\r
314 for (Index = 0; Index < ProcessorNum; Index++) {\r
315 Status = MpProtocol->GetProcessorInfo(\r
316 MpProtocol,\r
317 Index,\r
318 &ProcessorInfo\r
319 );\r
320 if (EFI_ERROR(Status)){\r
321 FreePool(ProcessorLocBuf);\r
322 return Status;\r
323 }\r
324\r
325 //\r
326 // Get all Processor Location info & measure\r
327 //\r
328 CopyMem(\r
329 &ProcessorLocBuf[Index],\r
330 &ProcessorInfo.Location,\r
331 sizeof(EFI_CPU_PHYSICAL_LOCATION)\r
332 );\r
333 }\r
334\r
335 *LocationBuf = ProcessorLocBuf;\r
336 *Num = ProcessorNum;\r
337\r
338 return Status;\r
339}\r
340\r
341/**\r
342 The EFI_TREE_PROTOCOL GetCapability function call provides protocol\r
343 capability information and state information about the TrEE.\r
344\r
345 @param[in] This Indicates the calling context\r
346 @param[in, out] ProtocolCapability The caller allocates memory for a TREE_BOOT_SERVICE_CAPABILITY\r
347 structure and sets the size field to the size of the structure allocated.\r
348 The callee fills in the fields with the EFI protocol capability information\r
349 and the current TrEE state information up to the number of fields which\r
350 fit within the size of the structure passed in.\r
351\r
352 @retval EFI_SUCCESS Operation completed successfully.\r
353 @retval EFI_DEVICE_ERROR The command was unsuccessful.\r
354 The ProtocolCapability variable will not be populated. \r
355 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.\r
356 The ProtocolCapability variable will not be populated.\r
357 @retval EFI_BUFFER_TOO_SMALL The ProtocolCapability variable is too small to hold the full response.\r
358 It will be partially populated (required Size field will be set). \r
359**/\r
360EFI_STATUS\r
361EFIAPI\r
362TreeGetCapability (\r
363 IN EFI_TREE_PROTOCOL *This,\r
364 IN OUT TREE_BOOT_SERVICE_CAPABILITY *ProtocolCapability\r
365 )\r
366{\r
367 DEBUG ((EFI_D_ERROR, "TreeGetCapability ...\n"));\r
368\r
369 if ((This == NULL) || (ProtocolCapability == NULL)) {\r
370 return EFI_INVALID_PARAMETER;\r
371 }\r
372\r
373 if (ProtocolCapability->Size < mTcgDxeData.BsCap.Size) {\r
374 ProtocolCapability->Size = mTcgDxeData.BsCap.Size;\r
375 return EFI_BUFFER_TOO_SMALL;\r
376 }\r
377\r
378 CopyMem (ProtocolCapability, &mTcgDxeData.BsCap, mTcgDxeData.BsCap.Size);\r
379 DEBUG ((EFI_D_ERROR, "TreeGetCapability - %r\n", EFI_SUCCESS));\r
380 return EFI_SUCCESS;\r
381}\r
382\r
383/**\r
384 This function dump event log.\r
385\r
386 @param[in] EventLogFormat The type of the event log for which the information is requested.\r
387 @param[in] EventLogLocation A pointer to the memory address of the event log.\r
388 @param[in] EventLogLastEntry If the Event Log contains more than one entry, this is a pointer to the\r
389 address of the start of the last entry in the event log in memory.\r
390**/\r
391VOID\r
392DumpEventLog (\r
393 IN TREE_EVENT_LOG_FORMAT EventLogFormat,\r
394 IN EFI_PHYSICAL_ADDRESS EventLogLocation,\r
395 IN EFI_PHYSICAL_ADDRESS EventLogLastEntry\r
396 )\r
397{\r
398 TCG_PCR_EVENT_HDR *EventHdr;\r
399 UINTN Index;\r
400\r
401 DEBUG ((EFI_D_INFO, "EventLogFormat: (0x%x)\n", EventLogFormat));\r
402 \r
403 switch (EventLogFormat) {\r
404 case TREE_EVENT_LOG_FORMAT_TCG_1_2:\r
405 EventHdr = (TCG_PCR_EVENT_HDR *)(UINTN)EventLogLocation;\r
406 while ((UINTN)EventHdr <= EventLogLastEntry) {\r
407 DEBUG ((EFI_D_INFO, " Event:\n"));\r
408 DEBUG ((EFI_D_INFO, " PCRIndex - %d\n", EventHdr->PCRIndex));\r
409 DEBUG ((EFI_D_INFO, " EventType - 0x%08x\n", EventHdr->EventType));\r
410 DEBUG ((EFI_D_INFO, " Digest - "));\r
411 for (Index = 0; Index < sizeof(TCG_DIGEST); Index++) {\r
412 DEBUG ((EFI_D_INFO, "%02x ", EventHdr->Digest.digest[Index]));\r
413 }\r
414 DEBUG ((EFI_D_INFO, "\n"));\r
415 DEBUG ((EFI_D_INFO, " EventSize - 0x%08x\n", EventHdr->EventSize));\r
416 InternalDumpHex ((UINT8 *)(EventHdr + 1), EventHdr->EventSize);\r
417 EventHdr = (TCG_PCR_EVENT_HDR *)((UINTN)EventHdr + sizeof(TCG_PCR_EVENT_HDR) + EventHdr->EventSize);\r
418 }\r
419 break;\r
420 }\r
421\r
422 return ;\r
423}\r
424\r
425/**\r
426 The EFI_TREE_PROTOCOL Get Event Log function call allows a caller to\r
427 retrieve the address of a given event log and its last entry. \r
428\r
429 @param[in] This Indicates the calling context\r
430 @param[in] EventLogFormat The type of the event log for which the information is requested.\r
431 @param[out] EventLogLocation A pointer to the memory address of the event log.\r
432 @param[out] EventLogLastEntry If the Event Log contains more than one entry, this is a pointer to the\r
433 address of the start of the last entry in the event log in memory.\r
434 @param[out] EventLogTruncated If the Event Log is missing at least one entry because an event would\r
435 have exceeded the area allocated for events, this value is set to TRUE.\r
436 Otherwise, the value will be FALSE and the Event Log will be complete.\r
437\r
438 @retval EFI_SUCCESS Operation completed successfully.\r
439 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect\r
440 (e.g. asking for an event log whose format is not supported).\r
441**/\r
442EFI_STATUS\r
443EFIAPI\r
444TreeGetEventLog (\r
445 IN EFI_TREE_PROTOCOL *This,\r
446 IN TREE_EVENT_LOG_FORMAT EventLogFormat,\r
447 OUT EFI_PHYSICAL_ADDRESS *EventLogLocation,\r
448 OUT EFI_PHYSICAL_ADDRESS *EventLogLastEntry,\r
449 OUT BOOLEAN *EventLogTruncated\r
450 )\r
451{\r
452 UINTN Index;\r
453\r
454 DEBUG ((EFI_D_ERROR, "TreeGetEventLog ...\n"));\r
455\r
456 if (This == NULL) {\r
457 return EFI_INVALID_PARAMETER;\r
458 }\r
459\r
460 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {\r
461 if (EventLogFormat == mTreeEventInfo[Index].LogFormat) {\r
462 break;\r
463 }\r
464 }\r
465\r
466 if (Index == sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0])) {\r
467 return EFI_INVALID_PARAMETER;\r
468 }\r
469\r
470 if (!mTcgDxeData.BsCap.TrEEPresentFlag) {\r
471 if (EventLogLocation != NULL) {\r
472 *EventLogLocation = 0;\r
473 }\r
474 if (EventLogLastEntry != NULL) {\r
475 *EventLogLastEntry = 0;\r
476 }\r
477 if (EventLogTruncated != NULL) {\r
478 *EventLogTruncated = FALSE;\r
479 }\r
480 return EFI_SUCCESS;\r
481 }\r
482\r
483 if (EventLogLocation != NULL) {\r
484 *EventLogLocation = mTcgDxeData.EventLogAreaStruct[Index].Lasa;\r
485 DEBUG ((EFI_D_ERROR, "TreeGetEventLog (EventLogLocation - %x)\n", *EventLogLocation));\r
486 }\r
487\r
488 if (EventLogLastEntry != NULL) {\r
489 if (!mTcgDxeData.EventLogAreaStruct[Index].EventLogStarted) {\r
490 *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)0;\r
491 } else {\r
492 *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)mTcgDxeData.EventLogAreaStruct[Index].LastEvent;\r
493 }\r
494 DEBUG ((EFI_D_ERROR, "TreeGetEventLog (EventLogLastEntry - %x)\n", *EventLogLastEntry));\r
495 }\r
496\r
497 if (EventLogTruncated != NULL) {\r
498 *EventLogTruncated = mTcgDxeData.EventLogAreaStruct[Index].EventLogTruncated;\r
499 DEBUG ((EFI_D_ERROR, "TreeGetEventLog (EventLogTruncated - %x)\n", *EventLogTruncated));\r
500 }\r
501\r
502 DEBUG ((EFI_D_ERROR, "TreeGetEventLog - %r\n", EFI_SUCCESS));\r
503\r
504 // Dump Event Log for debug purpose\r
505 if ((EventLogLocation != NULL) && (EventLogLastEntry != NULL)) {\r
506 DumpEventLog (EventLogFormat, *EventLogLocation, *EventLogLastEntry);\r
507 }\r
508\r
509 return EFI_SUCCESS;\r
510}\r
511\r
512/**\r
513 Add a new entry to the Event Log.\r
514\r
515 @param[in, out] EventLogPtr Pointer to the Event Log data. \r
516 @param[in, out] LogSize Size of the Event Log. \r
517 @param[in] MaxSize Maximum size of the Event Log.\r
518 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure. \r
519 @param[in] NewEventHdrSize New event header size.\r
520 @param[in] NewEventData Pointer to the new event data. \r
521 @param[in] NewEventSize New event data size.\r
522 \r
523 @retval EFI_SUCCESS The new event log entry was added.\r
524 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r
525\r
526**/\r
527EFI_STATUS\r
528TcgCommLogEvent (\r
529 IN OUT UINT8 **EventLogPtr,\r
530 IN OUT UINTN *LogSize,\r
531 IN UINTN MaxSize,\r
532 IN VOID *NewEventHdr,\r
533 IN UINT32 NewEventHdrSize,\r
534 IN UINT8 *NewEventData,\r
535 IN UINT32 NewEventSize\r
536 )\r
537{\r
538 UINTN NewLogSize;\r
539\r
540 if (NewEventSize > MAX_ADDRESS - NewEventHdrSize) {\r
541 return EFI_OUT_OF_RESOURCES;\r
542 }\r
543\r
544 NewLogSize = NewEventHdrSize + NewEventSize;\r
545\r
546 if (NewLogSize > MAX_ADDRESS - *LogSize) {\r
547 return EFI_OUT_OF_RESOURCES;\r
548 }\r
549\r
550 if (NewLogSize + *LogSize > MaxSize) {\r
551 DEBUG ((EFI_D_INFO, " MaxSize - 0x%x\n", MaxSize));\r
552 DEBUG ((EFI_D_INFO, " NewLogSize - 0x%x\n", NewLogSize));\r
553 DEBUG ((EFI_D_INFO, " LogSize - 0x%x\n", *LogSize));\r
554 DEBUG ((EFI_D_INFO, "TcgCommLogEvent - %r\n", EFI_OUT_OF_RESOURCES));\r
555 return EFI_OUT_OF_RESOURCES;\r
556 }\r
557\r
558 *EventLogPtr += *LogSize;\r
559 *LogSize += NewLogSize;\r
560 CopyMem (*EventLogPtr, NewEventHdr, NewEventHdrSize);\r
561 CopyMem (\r
562 *EventLogPtr + NewEventHdrSize,\r
563 NewEventData,\r
564 NewEventSize\r
565 );\r
566 return EFI_SUCCESS;\r
567}\r
568\r
569/**\r
570 Add a new entry to the Event Log.\r
571\r
572 @param[in] EventLogFormat The type of the event log for which the information is requested.\r
573 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure. \r
574 @param[in] NewEventHdrSize New event header size.\r
575 @param[in] NewEventData Pointer to the new event data. \r
576 @param[in] NewEventSize New event data size.\r
577\r
578 @retval EFI_SUCCESS The new event log entry was added.\r
579 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r
580\r
581**/\r
582EFI_STATUS\r
583TcgDxeLogEvent (\r
584 IN TREE_EVENT_LOG_FORMAT EventLogFormat,\r
585 IN VOID *NewEventHdr,\r
586 IN UINT32 NewEventHdrSize,\r
587 IN UINT8 *NewEventData,\r
588 IN UINT32 NewEventSize\r
589 )\r
590{\r
591 EFI_STATUS Status;\r
592 UINTN Index;\r
593\r
594 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {\r
595 if (EventLogFormat == mTreeEventInfo[Index].LogFormat) {\r
596 break;\r
597 }\r
598 }\r
599\r
600 if (Index == sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0])) {\r
601 return EFI_INVALID_PARAMETER;\r
602 }\r
603\r
604 if (mTcgDxeData.EventLogAreaStruct[Index].EventLogTruncated) {\r
605 return EFI_VOLUME_FULL;\r
606 }\r
607\r
608 mTcgDxeData.EventLogAreaStruct[Index].LastEvent = (UINT8*)(UINTN)mTcgDxeData.EventLogAreaStruct[Index].Lasa;\r
609 Status = TcgCommLogEvent (\r
610 &mTcgDxeData.EventLogAreaStruct[Index].LastEvent,\r
611 &mTcgDxeData.EventLogAreaStruct[Index].EventLogSize,\r
612 (UINTN)mTcgDxeData.EventLogAreaStruct[Index].Laml,\r
613 NewEventHdr,\r
614 NewEventHdrSize,\r
615 NewEventData,\r
616 NewEventSize\r
617 );\r
618 \r
619 if (Status == EFI_DEVICE_ERROR) {\r
620 return EFI_DEVICE_ERROR;\r
621 } else if (Status == EFI_OUT_OF_RESOURCES) {\r
622 mTcgDxeData.EventLogAreaStruct[Index].EventLogTruncated = TRUE;\r
623 return EFI_VOLUME_FULL;\r
624 } else if (Status == EFI_SUCCESS) {\r
625 mTcgDxeData.EventLogAreaStruct[Index].EventLogStarted = TRUE;\r
626 }\r
627\r
628 return Status;\r
629}\r
630\r
631/**\r
632 This function return hash algorithm from event log format.\r
633\r
634 @param[in] EventLogFormat Event log format.\r
635\r
636 @return hash algorithm.\r
637**/\r
638TPMI_ALG_HASH\r
639TrEEGetHashAlgoFromLogFormat (\r
640 IN TREE_EVENT_LOG_FORMAT EventLogFormat\r
641 )\r
642{\r
643 UINTN Index;\r
644\r
645 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {\r
646 if (mTreeEventInfo[Index].LogFormat == EventLogFormat) {\r
647 return mTreeEventInfo[Index].TpmHashAlgo;\r
648 }\r
649 }\r
650 return TPM_ALG_SHA1;\r
651}\r
652\r
653/**\r
654 This function return hash algorithm ID from event log format.\r
655\r
656 @param[in] EventLogFormat Event log format.\r
657\r
658 @return hash algorithm ID.\r
659**/\r
660UINT16\r
661TrEEGetAlgIDFromLogFormat (\r
662 IN TREE_EVENT_LOG_FORMAT EventLogFormat\r
663 )\r
664{\r
665 UINTN Index;\r
666\r
667 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {\r
668 if (mTreeEventInfo[Index].LogFormat == EventLogFormat) {\r
669 return mTreeEventInfo[Index].DigestAlgID;\r
670 }\r
671 }\r
672 return 0;\r
673}\r
674\r
675/**\r
676 This function return boot hash algorithm from event log format.\r
677\r
678 @param[in] EventLogFormat Event log format.\r
679\r
680 @return boot hash algorithm.\r
681**/\r
682UINT32\r
683TrEEGetBootHashAlgFromLogFormat (\r
684 IN TREE_EVENT_LOG_FORMAT EventLogFormat\r
685 )\r
686{\r
687 UINTN Index;\r
688\r
689 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {\r
690 if (mTreeEventInfo[Index].LogFormat == EventLogFormat) {\r
691 return mTreeEventInfo[Index].BootHashAlg;\r
692 }\r
693 }\r
694 return TREE_BOOT_HASH_ALG_SHA1;\r
695}\r
696\r
697/**\r
698 This function get digest from digest list.\r
699\r
700 @param HashAlg digest algorithm\r
701 @param DigestList digest list\r
702 @param Digest digest\r
703\r
704 @retval EFI_SUCCESS Sha1Digest is found and returned.\r
705 @retval EFI_NOT_FOUND Sha1Digest is not found.\r
706**/\r
707EFI_STATUS\r
708Tpm2GetDigestFromDigestList (\r
709 IN TPMI_ALG_HASH HashAlg,\r
710 IN TPML_DIGEST_VALUES *DigestList,\r
711 IN VOID *Digest\r
712 )\r
713{\r
714 UINTN Index;\r
715 UINT16 DigestSize;\r
716\r
717 DigestSize = GetHashSizeFromAlgo (HashAlg);\r
718 for (Index = 0; Index < DigestList->count; Index++) {\r
719 if (DigestList->digests[Index].hashAlg == HashAlg) {\r
720 CopyMem (\r
721 Digest,\r
722 &DigestList->digests[Index].digest,\r
723 DigestSize\r
724 );\r
725 return EFI_SUCCESS;\r
726 }\r
727 }\r
728\r
729 return EFI_NOT_FOUND;\r
730}\r
731\r
732/**\r
733 Add a new entry to the Event Log.\r
734\r
735 @param[in] DigestList A list of digest.\r
736 @param[in,out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.\r
737 @param[in] NewEventData Pointer to the new event data.\r
738\r
739 @retval EFI_SUCCESS The new event log entry was added.\r
740 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r
741**/\r
742EFI_STATUS\r
743TcgDxeLogHashEvent (\r
744 IN TPML_DIGEST_VALUES *DigestList,\r
745 IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,\r
746 IN UINT8 *NewEventData\r
747 )\r
748{\r
749 EFI_STATUS Status;\r
750 EFI_TPL OldTpl;\r
751 UINTN Index;\r
752 EFI_STATUS RetStatus;\r
753\r
754 RetStatus = EFI_SUCCESS;\r
755 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {\r
756 DEBUG ((EFI_D_INFO, " LogFormat - 0x%08x\n", mTreeEventInfo[Index].LogFormat));\r
757 switch (mTreeEventInfo[Index].LogFormat) {\r
758 case TREE_EVENT_LOG_FORMAT_TCG_1_2:\r
759 Status = Tpm2GetDigestFromDigestList (TPM_ALG_SHA1, DigestList, &NewEventHdr->Digest);\r
760 if (!EFI_ERROR (Status)) {\r
761 //\r
762 // Enter critical region\r
763 //\r
764 OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
765 Status = TcgDxeLogEvent (\r
766 mTreeEventInfo[Index].LogFormat,\r
767 NewEventHdr,\r
768 sizeof(TCG_PCR_EVENT_HDR),\r
769 NewEventData,\r
770 NewEventHdr->EventSize\r
771 );\r
772 if (Status != EFI_SUCCESS) {\r
773 RetStatus = Status;\r
774 }\r
775 gBS->RestoreTPL (OldTpl);\r
776 //\r
777 // Exit critical region\r
778 //\r
779 }\r
780 break;\r
781 }\r
782 }\r
783\r
784 return RetStatus;\r
785}\r
786\r
787/**\r
788 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,\r
789 and add an entry to the Event Log.\r
790\r
791 @param[in] Flags Bitmap providing additional information.\r
792 @param[in] HashData Physical address of the start of the data buffer \r
793 to be hashed, extended, and logged.\r
794 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r
795 @param[in, out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure. \r
796 @param[in] NewEventData Pointer to the new event data. \r
797\r
798 @retval EFI_SUCCESS Operation completed successfully.\r
799 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r
800 @retval EFI_DEVICE_ERROR The command was unsuccessful.\r
801\r
802**/\r
803EFI_STATUS\r
804TcgDxeHashLogExtendEvent (\r
805 IN UINT64 Flags,\r
806 IN UINT8 *HashData,\r
807 IN UINT64 HashDataLen,\r
808 IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,\r
809 IN UINT8 *NewEventData\r
810 )\r
811{\r
812 EFI_STATUS Status;\r
813 TPML_DIGEST_VALUES DigestList;\r
814\r
815 Status = HashAndExtend (\r
816 NewEventHdr->PCRIndex,\r
817 HashData,\r
818 (UINTN)HashDataLen,\r
819 &DigestList\r
820 );\r
821 if (!EFI_ERROR (Status)) {\r
822 if ((Flags & TREE_EXTEND_ONLY) == 0) {\r
823 Status = TcgDxeLogHashEvent (&DigestList, NewEventHdr, NewEventData);\r
824 }\r
825 }\r
826\r
827 return Status;\r
828}\r
829\r
830/**\r
831 The EFI_TREE_PROTOCOL HashLogExtendEvent function call provides callers with\r
832 an opportunity to extend and optionally log events without requiring\r
833 knowledge of actual TPM commands. \r
834 The extend operation will occur even if this function cannot create an event\r
835 log entry (e.g. due to the event log being full). \r
836\r
837 @param[in] This Indicates the calling context\r
838 @param[in] Flags Bitmap providing additional information.\r
839 @param[in] DataToHash Physical address of the start of the data buffer to be hashed. \r
840 @param[in] DataToHashLen The length in bytes of the buffer referenced by DataToHash.\r
841 @param[in] Event Pointer to data buffer containing information about the event.\r
842\r
843 @retval EFI_SUCCESS Operation completed successfully.\r
844 @retval EFI_DEVICE_ERROR The command was unsuccessful.\r
845 @retval EFI_VOLUME_FULL The extend operation occurred, but the event could not be written to one or more event logs.\r
846 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.\r
847 @retval EFI_UNSUPPORTED The PE/COFF image type is not supported.\r
848**/\r
849EFI_STATUS\r
850EFIAPI\r
851TreeHashLogExtendEvent (\r
852 IN EFI_TREE_PROTOCOL *This,\r
853 IN UINT64 Flags,\r
854 IN EFI_PHYSICAL_ADDRESS DataToHash,\r
855 IN UINT64 DataToHashLen,\r
856 IN TrEE_EVENT *Event\r
857 )\r
858{\r
859 EFI_STATUS Status;\r
860 TCG_PCR_EVENT_HDR NewEventHdr;\r
861 TPML_DIGEST_VALUES DigestList;\r
862\r
863 DEBUG ((EFI_D_ERROR, "TreeHashLogExtendEvent ...\n"));\r
864\r
865 if ((This == NULL) || (DataToHash == 0) || (Event == NULL)) {\r
866 return EFI_INVALID_PARAMETER;\r
867 }\r
868\r
869 if (!mTcgDxeData.BsCap.TrEEPresentFlag) {\r
870 return EFI_UNSUPPORTED;\r
871 }\r
872\r
873 if (Event->Size < Event->Header.HeaderSize + sizeof(UINT32)) {\r
874 return EFI_INVALID_PARAMETER;\r
875 }\r
876\r
877 if (Event->Header.PCRIndex > MAX_PCR_INDEX) {\r
878 return EFI_INVALID_PARAMETER;\r
879 }\r
880\r
881 NewEventHdr.PCRIndex = Event->Header.PCRIndex;\r
882 NewEventHdr.EventType = Event->Header.EventType;\r
883 NewEventHdr.EventSize = Event->Size - sizeof(UINT32) - Event->Header.HeaderSize;\r
884 if ((Flags & PE_COFF_IMAGE) != 0) {\r
885 Status = MeasurePeImageAndExtend (\r
886 NewEventHdr.PCRIndex,\r
887 DataToHash,\r
888 (UINTN)DataToHashLen,\r
889 &DigestList\r
890 );\r
891 if (!EFI_ERROR (Status)) {\r
892 if ((Flags & TREE_EXTEND_ONLY) == 0) {\r
893 Status = TcgDxeLogHashEvent (&DigestList, &NewEventHdr, Event->Event);\r
894 }\r
895 }\r
896 } else {\r
897 Status = TcgDxeHashLogExtendEvent (\r
898 Flags,\r
899 (UINT8 *) (UINTN) DataToHash,\r
900 DataToHashLen,\r
901 &NewEventHdr,\r
902 Event->Event\r
903 );\r
904 }\r
905 DEBUG ((EFI_D_ERROR, "TreeHashLogExtendEvent - %r\n", Status));\r
906 return Status;\r
907}\r
908\r
909/**\r
910 This service enables the sending of commands to the TrEE.\r
911\r
912 @param[in] This Indicates the calling context\r
913 @param[in] InputParameterBlockSize Size of the TrEE input parameter block.\r
914 @param[in] InputParameterBlock Pointer to the TrEE input parameter block.\r
915 @param[in] OutputParameterBlockSize Size of the TrEE output parameter block.\r
916 @param[in] OutputParameterBlock Pointer to the TrEE output parameter block.\r
917\r
918 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.\r
919 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.\r
920 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.\r
921 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small. \r
922**/\r
923EFI_STATUS\r
924EFIAPI\r
925TreeSubmitCommand (\r
926 IN EFI_TREE_PROTOCOL *This,\r
927 IN UINT32 InputParameterBlockSize,\r
928 IN UINT8 *InputParameterBlock,\r
929 IN UINT32 OutputParameterBlockSize,\r
930 IN UINT8 *OutputParameterBlock\r
931 )\r
932{\r
933 EFI_STATUS Status;\r
934\r
935 DEBUG ((EFI_D_ERROR, "TreeSubmitCommand ...\n"));\r
936\r
937 if ((This == NULL) ||\r
938 (InputParameterBlockSize == 0) || (InputParameterBlock == NULL) ||\r
939 (OutputParameterBlockSize == 0) || (OutputParameterBlock == NULL)) {\r
940 return EFI_INVALID_PARAMETER;\r
941 }\r
942\r
943 if (!mTcgDxeData.BsCap.TrEEPresentFlag) {\r
944 return EFI_UNSUPPORTED;\r
945 }\r
946\r
947 if (InputParameterBlockSize >= mTcgDxeData.BsCap.MaxCommandSize) {\r
948 return EFI_INVALID_PARAMETER;\r
949 }\r
950 if (OutputParameterBlockSize >= mTcgDxeData.BsCap.MaxResponseSize) {\r
951 return EFI_INVALID_PARAMETER;\r
952 }\r
953\r
954 Status = Tpm2SubmitCommand (\r
955 InputParameterBlockSize,\r
956 InputParameterBlock,\r
957 &OutputParameterBlockSize,\r
958 OutputParameterBlock\r
959 );\r
960 DEBUG ((EFI_D_ERROR, "TreeSubmitCommand - %r\n", Status));\r
961 return Status;\r
962}\r
963\r
964\r
965EFI_TREE_PROTOCOL mTreeProtocol = {\r
966 TreeGetCapability,\r
967 TreeGetEventLog,\r
968 TreeHashLogExtendEvent,\r
969 TreeSubmitCommand\r
970};\r
971\r
972/**\r
973 Initialize the Event Log and log events passed from the PEI phase.\r
974\r
975 @retval EFI_SUCCESS Operation completed successfully.\r
976 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
977\r
978**/\r
979EFI_STATUS\r
980SetupEventLog (\r
981 VOID\r
982 )\r
983{\r
984 EFI_STATUS Status;\r
985 VOID *TcgEvent;\r
986 EFI_PEI_HOB_POINTERS GuidHob;\r
987 EFI_PHYSICAL_ADDRESS Lasa;\r
988 UINTN Index;\r
989\r
990 DEBUG ((EFI_D_INFO, "SetupEventLog\n"));\r
991\r
992 //\r
993 // 1. Create Log Area\r
994 //\r
995 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {\r
996 mTcgDxeData.EventLogAreaStruct[Index].EventLogFormat = mTreeEventInfo[Index].LogFormat;\r
997 Lasa = (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1);\r
998 Status = gBS->AllocatePages (\r
999 AllocateMaxAddress,\r
1000 EfiACPIMemoryNVS,\r
1001 EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE),\r
1002 &Lasa\r
1003 );\r
1004 if (EFI_ERROR (Status)) {\r
1005 return Status;\r
1006 }\r
1007 mTcgDxeData.EventLogAreaStruct[Index].Lasa = Lasa;\r
1008 mTcgDxeData.EventLogAreaStruct[Index].Laml = EFI_TCG_LOG_AREA_SIZE;\r
1009 //\r
1010 // To initialize them as 0xFF is recommended \r
1011 // because the OS can know the last entry for that.\r
1012 //\r
1013 SetMem ((VOID *)(UINTN)Lasa, EFI_TCG_LOG_AREA_SIZE, 0xFF);\r
1014 }\r
1015\r
1016 //\r
1017 // 2. Create ACPI table for TCG1.2 only\r
1018 //\r
1019 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {\r
1020 mTcgClientAcpiTemplate.Lasa = mTcgDxeData.EventLogAreaStruct[0].Lasa;\r
1021 mTcgClientAcpiTemplate.Laml = EFI_TCG_LOG_AREA_SIZE;\r
1022 } else {\r
1023 mTcgServerAcpiTemplate.Lasa = mTcgDxeData.EventLogAreaStruct[0].Lasa;\r
1024 mTcgServerAcpiTemplate.Laml = EFI_TCG_LOG_AREA_SIZE;\r
1025 }\r
1026\r
1027 //\r
1028 // 3. Sync data from PEI to DXE\r
1029 //\r
1030 Status = EFI_SUCCESS;\r
1031 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {\r
1032 GuidHob.Raw = GetHobList ();\r
1033 Status = EFI_SUCCESS;\r
1034 while (!EFI_ERROR (Status) && \r
1035 (GuidHob.Raw = GetNextGuidHob (mTreeEventInfo[Index].EventGuid, GuidHob.Raw)) != NULL) {\r
1036 TcgEvent = GET_GUID_HOB_DATA (GuidHob.Guid);\r
1037 GuidHob.Raw = GET_NEXT_HOB (GuidHob);\r
1038 switch (mTreeEventInfo[Index].LogFormat) {\r
1039 case TREE_EVENT_LOG_FORMAT_TCG_1_2:\r
1040 Status = TcgDxeLogEvent (\r
1041 mTreeEventInfo[Index].LogFormat,\r
1042 TcgEvent,\r
1043 sizeof(TCG_PCR_EVENT_HDR),\r
1044 ((TCG_PCR_EVENT*)TcgEvent)->Event,\r
1045 ((TCG_PCR_EVENT_HDR*)TcgEvent)->EventSize\r
1046 );\r
1047 break;\r
1048 }\r
1049 }\r
1050 }\r
1051\r
1052 return Status;\r
1053}\r
1054\r
1055/**\r
1056 Measure and log an action string, and extend the measurement result into PCR[5].\r
1057\r
1058 @param[in] String A specific string that indicates an Action event. \r
1059 \r
1060 @retval EFI_SUCCESS Operation completed successfully.\r
1061 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
1062\r
1063**/\r
1064EFI_STATUS\r
1065TcgMeasureAction (\r
1066 IN CHAR8 *String\r
1067 )\r
1068{\r
1069 TCG_PCR_EVENT_HDR TcgEvent;\r
1070\r
1071 TcgEvent.PCRIndex = 5;\r
1072 TcgEvent.EventType = EV_EFI_ACTION;\r
1073 TcgEvent.EventSize = (UINT32)AsciiStrLen (String);\r
1074 return TcgDxeHashLogExtendEvent (\r
1075 0,\r
1076 (UINT8*)String,\r
1077 TcgEvent.EventSize,\r
1078 &TcgEvent,\r
1079 (UINT8 *) String\r
1080 );\r
1081}\r
1082\r
1083/**\r
1084 Measure and log EFI handoff tables, and extend the measurement result into PCR[1].\r
1085\r
1086 @retval EFI_SUCCESS Operation completed successfully.\r
1087 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
1088\r
1089**/\r
1090EFI_STATUS\r
1091MeasureHandoffTables (\r
1092 VOID\r
1093 )\r
1094{\r
1095 EFI_STATUS Status;\r
1096 SMBIOS_TABLE_ENTRY_POINT *SmbiosTable;\r
1097 TCG_PCR_EVENT_HDR TcgEvent;\r
1098 EFI_HANDOFF_TABLE_POINTERS HandoffTables;\r
1099 UINTN ProcessorNum;\r
1100 EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;\r
1101\r
1102 //\r
1103 // Measure SMBIOS with EV_EFI_HANDOFF_TABLES to PCR[1]\r
1104 //\r
1105 Status = EfiGetSystemConfigurationTable (\r
1106 &gEfiSmbiosTableGuid,\r
1107 (VOID **) &SmbiosTable\r
1108 );\r
1109\r
1110 if (!EFI_ERROR (Status)) {\r
1111 ASSERT (SmbiosTable != NULL);\r
1112\r
1113 TcgEvent.PCRIndex = 1;\r
1114 TcgEvent.EventType = EV_EFI_HANDOFF_TABLES;\r
1115 TcgEvent.EventSize = sizeof (HandoffTables);\r
1116\r
1117 HandoffTables.NumberOfTables = 1;\r
1118 HandoffTables.TableEntry[0].VendorGuid = gEfiSmbiosTableGuid;\r
1119 HandoffTables.TableEntry[0].VendorTable = SmbiosTable;\r
1120\r
1121 DEBUG ((DEBUG_INFO, "The Smbios Table starts at: 0x%x\n", SmbiosTable->TableAddress));\r
1122 DEBUG ((DEBUG_INFO, "The Smbios Table size: 0x%x\n", SmbiosTable->TableLength));\r
1123\r
1124 Status = TcgDxeHashLogExtendEvent (\r
1125 0,\r
1126 (UINT8*)(UINTN)SmbiosTable->TableAddress,\r
1127 SmbiosTable->TableLength,\r
1128 &TcgEvent,\r
1129 (UINT8*)&HandoffTables\r
1130 );\r
1131 }\r
1132\r
1133 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_SERVER) {\r
1134 //\r
1135 // Tcg Server spec. \r
1136 // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]\r
1137 //\r
1138 Status = GetProcessorsCpuLocation(&ProcessorLocBuf, &ProcessorNum);\r
1139\r
1140 if (!EFI_ERROR(Status)){\r
1141 TcgEvent.PCRIndex = 1;\r
1142 TcgEvent.EventType = EV_TABLE_OF_DEVICES;\r
1143 TcgEvent.EventSize = sizeof (HandoffTables);\r
1144\r
1145 HandoffTables.NumberOfTables = 1;\r
1146 HandoffTables.TableEntry[0].VendorGuid = gEfiMpServiceProtocolGuid;\r
1147 HandoffTables.TableEntry[0].VendorTable = ProcessorLocBuf;\r
1148\r
1149 Status = TcgDxeHashLogExtendEvent (\r
1150 0,\r
1151 (UINT8*)(UINTN)ProcessorLocBuf,\r
1152 sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,\r
1153 &TcgEvent,\r
1154 (UINT8*)&HandoffTables\r
1155 );\r
1156\r
1157 FreePool(ProcessorLocBuf);\r
1158 }\r
1159 }\r
1160\r
1161 return Status;\r
1162}\r
1163\r
1164/**\r
1165 Measure and log Separator event, and extend the measurement result into a specific PCR.\r
1166\r
1167 @param[in] PCRIndex PCR index. \r
1168\r
1169 @retval EFI_SUCCESS Operation completed successfully.\r
1170 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
1171\r
1172**/\r
1173EFI_STATUS\r
1174MeasureSeparatorEvent (\r
1175 IN TPM_PCRINDEX PCRIndex\r
1176 )\r
1177{\r
1178 TCG_PCR_EVENT_HDR TcgEvent;\r
1179 UINT32 EventData;\r
1180\r
1181 DEBUG ((EFI_D_ERROR, "MeasureSeparatorEvent Pcr - %x\n", PCRIndex));\r
1182\r
1183 EventData = 0;\r
1184 TcgEvent.PCRIndex = PCRIndex;\r
1185 TcgEvent.EventType = EV_SEPARATOR;\r
1186 TcgEvent.EventSize = (UINT32)sizeof (EventData);\r
1187 return TcgDxeHashLogExtendEvent (\r
1188 0,\r
1189 (UINT8 *)&EventData,\r
1190 sizeof (EventData),\r
1191 &TcgEvent,\r
1192 (UINT8 *)&EventData\r
1193 );\r
1194}\r
1195\r
1196/**\r
1197 Measure and log an EFI variable, and extend the measurement result into a specific PCR.\r
1198\r
1199 @param[in] PCRIndex PCR Index. \r
1200 @param[in] EventType Event type. \r
1201 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.\r
1202 @param[in] VendorGuid A unique identifier for the vendor.\r
1203 @param[in] VarData The content of the variable data. \r
1204 @param[in] VarSize The size of the variable data. \r
1205 \r
1206 @retval EFI_SUCCESS Operation completed successfully.\r
1207 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
1208 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
1209\r
1210**/\r
1211EFI_STATUS\r
1212MeasureVariable (\r
1213 IN TPM_PCRINDEX PCRIndex,\r
1214 IN TCG_EVENTTYPE EventType,\r
1215 IN CHAR16 *VarName,\r
1216 IN EFI_GUID *VendorGuid,\r
1217 IN VOID *VarData,\r
1218 IN UINTN VarSize\r
1219 )\r
1220{\r
1221 EFI_STATUS Status;\r
1222 TCG_PCR_EVENT_HDR TcgEvent;\r
1223 UINTN VarNameLength;\r
1224 EFI_VARIABLE_DATA_TREE *VarLog;\r
1225\r
1226 ASSERT ((VarSize == 0 && VarData == NULL) || (VarSize != 0 && VarData != NULL));\r
1227\r
1228 DEBUG ((EFI_D_ERROR, "TrEEDxe: MeasureVariable (Pcr - %x, EventType - %x, ", (UINTN)PCRIndex, (UINTN)EventType));\r
1229 DEBUG ((EFI_D_ERROR, "VariableName - %s, VendorGuid - %g)\n", VarName, VendorGuid));\r
1230\r
1231 VarNameLength = StrLen (VarName);\r
1232 TcgEvent.PCRIndex = PCRIndex;\r
1233 TcgEvent.EventType = EventType;\r
1234 TcgEvent.EventSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize\r
1235 - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));\r
1236\r
1237 VarLog = (EFI_VARIABLE_DATA_TREE*)AllocatePool (TcgEvent.EventSize);\r
1238 if (VarLog == NULL) {\r
1239 return EFI_OUT_OF_RESOURCES;\r
1240 }\r
1241\r
1242 VarLog->VariableName = *VendorGuid;\r
1243 VarLog->UnicodeNameLength = VarNameLength;\r
1244 VarLog->VariableDataLength = VarSize;\r
1245 CopyMem (\r
1246 VarLog->UnicodeName,\r
1247 VarName,\r
1248 VarNameLength * sizeof (*VarName)\r
1249 );\r
1250 if (VarSize != 0) {\r
1251 CopyMem (\r
1252 (CHAR16 *)VarLog->UnicodeName + VarNameLength,\r
1253 VarData,\r
1254 VarSize\r
1255 );\r
1256 }\r
1257\r
1258 if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {\r
1259 //\r
1260 // Digest is the event data (EFI_VARIABLE_DATA_TREE)\r
1261 //\r
1262 Status = TcgDxeHashLogExtendEvent (\r
1263 0,\r
1264 (UINT8*)VarLog,\r
1265 TcgEvent.EventSize,\r
1266 &TcgEvent,\r
1267 (UINT8*)VarLog\r
1268 );\r
1269 } else {\r
1270 Status = TcgDxeHashLogExtendEvent (\r
1271 0,\r
1272 (UINT8*)VarData,\r
1273 VarSize,\r
1274 &TcgEvent,\r
1275 (UINT8*)VarLog\r
1276 );\r
1277 }\r
1278 FreePool (VarLog);\r
1279 return Status;\r
1280}\r
1281\r
1282/**\r
1283 Read then Measure and log an EFI variable, and extend the measurement result into a specific PCR.\r
1284\r
1285 @param[in] PCRIndex PCR Index. \r
1286 @param[in] EventType Event type. \r
1287 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.\r
1288 @param[in] VendorGuid A unique identifier for the vendor.\r
1289 @param[out] VarSize The size of the variable data. \r
1290 @param[out] VarData Pointer to the content of the variable. \r
1291 \r
1292 @retval EFI_SUCCESS Operation completed successfully.\r
1293 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
1294 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
1295\r
1296**/\r
1297EFI_STATUS\r
1298ReadAndMeasureVariable (\r
1299 IN TPM_PCRINDEX PCRIndex,\r
1300 IN TCG_EVENTTYPE EventType,\r
1301 IN CHAR16 *VarName,\r
1302 IN EFI_GUID *VendorGuid,\r
1303 OUT UINTN *VarSize,\r
1304 OUT VOID **VarData\r
1305 )\r
1306{\r
1307 EFI_STATUS Status;\r
1308\r
1309 Status = GetVariable2 (VarName, VendorGuid, VarData, VarSize);\r
1310 if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {\r
1311 if (EFI_ERROR (Status)) {\r
1312 //\r
1313 // It is valid case, so we need handle it.\r
1314 //\r
1315 *VarData = NULL;\r
1316 *VarSize = 0;\r
1317 }\r
1318 } else {\r
1319 if (EFI_ERROR (Status)) {\r
1320 return Status;\r
1321 }\r
1322 ASSERT (*VarData != NULL);\r
1323 }\r
1324\r
1325 Status = MeasureVariable (\r
1326 PCRIndex,\r
1327 EventType,\r
1328 VarName,\r
1329 VendorGuid,\r
1330 *VarData,\r
1331 *VarSize\r
1332 );\r
1333 return Status;\r
1334}\r
1335\r
1336/**\r
1337 Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].\r
1338\r
1339 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.\r
1340 @param[in] VendorGuid A unique identifier for the vendor.\r
1341 @param[out] VarSize The size of the variable data. \r
1342 @param[out] VarData Pointer to the content of the variable. \r
1343 \r
1344 @retval EFI_SUCCESS Operation completed successfully.\r
1345 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
1346 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
1347\r
1348**/\r
1349EFI_STATUS\r
1350ReadAndMeasureBootVariable (\r
1351 IN CHAR16 *VarName,\r
1352 IN EFI_GUID *VendorGuid,\r
1353 OUT UINTN *VarSize,\r
1354 OUT VOID **VarData\r
1355 )\r
1356{\r
1357 return ReadAndMeasureVariable (\r
1358 5,\r
1359 EV_EFI_VARIABLE_BOOT,\r
1360 VarName,\r
1361 VendorGuid,\r
1362 VarSize,\r
1363 VarData\r
1364 );\r
1365}\r
1366\r
1367/**\r
1368 Read then Measure and log an EFI Secure variable, and extend the measurement result into PCR[7].\r
1369\r
1370 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.\r
1371 @param[in] VendorGuid A unique identifier for the vendor.\r
1372 @param[out] VarSize The size of the variable data. \r
1373 @param[out] VarData Pointer to the content of the variable. \r
1374 \r
1375 @retval EFI_SUCCESS Operation completed successfully.\r
1376 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
1377 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
1378\r
1379**/\r
1380EFI_STATUS\r
1381ReadAndMeasureSecureVariable (\r
1382 IN CHAR16 *VarName,\r
1383 IN EFI_GUID *VendorGuid,\r
1384 OUT UINTN *VarSize,\r
1385 OUT VOID **VarData\r
1386 )\r
1387{\r
1388 return ReadAndMeasureVariable (\r
1389 7,\r
1390 EV_EFI_VARIABLE_DRIVER_CONFIG,\r
1391 VarName,\r
1392 VendorGuid,\r
1393 VarSize,\r
1394 VarData\r
1395 );\r
1396}\r
1397\r
1398/**\r
1399 Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.\r
1400\r
1401 The EFI boot variables are BootOrder and Boot#### variables.\r
1402\r
1403 @retval EFI_SUCCESS Operation completed successfully.\r
1404 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
1405 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
1406\r
1407**/\r
1408EFI_STATUS\r
1409MeasureAllBootVariables (\r
1410 VOID\r
1411 )\r
1412{\r
1413 EFI_STATUS Status;\r
1414 UINT16 *BootOrder;\r
1415 UINTN BootCount;\r
1416 UINTN Index;\r
1417 VOID *BootVarData;\r
1418 UINTN Size;\r
1419\r
1420 Status = ReadAndMeasureBootVariable (\r
1421 mBootVarName,\r
1422 &gEfiGlobalVariableGuid,\r
1423 &BootCount,\r
1424 (VOID **) &BootOrder\r
1425 );\r
1426 if (Status == EFI_NOT_FOUND) {\r
1427 return EFI_SUCCESS;\r
1428 }\r
1429 ASSERT (BootOrder != NULL);\r
1430\r
1431 if (EFI_ERROR (Status)) {\r
1432 FreePool (BootOrder);\r
1433 return Status;\r
1434 }\r
1435\r
1436 BootCount /= sizeof (*BootOrder);\r
1437 for (Index = 0; Index < BootCount; Index++) {\r
1438 UnicodeSPrint (mBootVarName, sizeof (mBootVarName), L"Boot%04x", BootOrder[Index]);\r
1439 Status = ReadAndMeasureBootVariable (\r
1440 mBootVarName,\r
1441 &gEfiGlobalVariableGuid,\r
1442 &Size,\r
1443 &BootVarData\r
1444 );\r
1445 if (!EFI_ERROR (Status)) {\r
1446 FreePool (BootVarData);\r
1447 }\r
1448 }\r
1449\r
1450 FreePool (BootOrder);\r
1451 return EFI_SUCCESS;\r
1452}\r
1453\r
1454/**\r
1455 Measure and log all EFI Secure variables, and extend the measurement result into a specific PCR.\r
1456\r
1457 The EFI boot variables are BootOrder and Boot#### variables.\r
1458\r
1459 @retval EFI_SUCCESS Operation completed successfully.\r
1460 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
1461 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
1462\r
1463**/\r
1464EFI_STATUS\r
1465MeasureAllSecureVariables (\r
1466 VOID\r
1467 )\r
1468{\r
1469 EFI_STATUS Status;\r
1470 VOID *Data;\r
1471 UINTN DataSize;\r
1472 UINTN Index;\r
1473\r
1474 Status = EFI_NOT_FOUND;\r
1475 for (Index = 0; Index < sizeof(mVariableType)/sizeof(mVariableType[0]); Index++) {\r
1476 Status = ReadAndMeasureSecureVariable (\r
1477 mVariableType[Index].VariableName,\r
1478 mVariableType[Index].VendorGuid,\r
1479 &DataSize,\r
1480 &Data\r
1481 );\r
1482 if (!EFI_ERROR (Status)) {\r
1483 if (Data != NULL) {\r
1484 FreePool (Data);\r
1485 }\r
1486 }\r
1487 }\r
1488\r
1489 return EFI_SUCCESS;\r
1490}\r
1491\r
1492/**\r
1493 Measure and log launch of FirmwareDebugger, and extend the measurement result into a specific PCR.\r
1494\r
1495 @retval EFI_SUCCESS Operation completed successfully.\r
1496 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
1497 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
1498\r
1499**/\r
1500EFI_STATUS\r
1501MeasureLaunchOfFirmwareDebugger (\r
1502 VOID\r
1503 )\r
1504{\r
1505 TCG_PCR_EVENT_HDR TcgEvent;\r
1506\r
1507 TcgEvent.PCRIndex = 7;\r
1508 TcgEvent.EventType = EV_EFI_ACTION;\r
1509 TcgEvent.EventSize = sizeof(FIRMWARE_DEBUGGER_EVENT_STRING) - 1;\r
1510 return TcgDxeHashLogExtendEvent (\r
1511 0,\r
1512 (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING,\r
1513 sizeof(FIRMWARE_DEBUGGER_EVENT_STRING) - 1,\r
1514 &TcgEvent,\r
1515 (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING\r
1516 );\r
1517}\r
1518\r
1519/**\r
1520 Measure and log all Secure Boot Policy, and extend the measurement result into a specific PCR.\r
1521\r
1522 Platform firmware adhering to the policy must therefore measure the following values into PCR[7]: (in order listed)\r
1523 - The contents of the SecureBoot variable\r
1524 - The contents of the PK variable\r
1525 - The contents of the KEK variable\r
1526 - The contents of the EFI_IMAGE_SECURITY_DATABASE variable\r
1527 - The contents of the EFI_IMAGE_SECURITY_DATABASE1 variable\r
1528 - Separator\r
1529 - Entries in the EFI_IMAGE_SECURITY_DATABASE that are used to validate EFI Drivers or EFI Boot Applications in the boot path\r
1530\r
1531 NOTE: Because of the above, UEFI variables PK, KEK, EFI_IMAGE_SECURITY_DATABASE,\r
1532 EFI_IMAGE_SECURITY_DATABASE1 and SecureBoot SHALL NOT be measured into PCR[3].\r
1533\r
1534 @param[in] Event Event whose notification function is being invoked\r
1535 @param[in] Context Pointer to the notification function's context\r
1536**/\r
1537VOID\r
1538EFIAPI\r
1539MeasureSecureBootPolicy (\r
1540 IN EFI_EVENT Event,\r
1541 IN VOID *Context\r
1542 )\r
1543{\r
1544 EFI_STATUS Status;\r
1545 VOID *Protocol;\r
1546\r
1547 Status = gBS->LocateProtocol (&gEfiVariableWriteArchProtocolGuid, NULL, (VOID **)&Protocol);\r
1548 if (EFI_ERROR (Status)) {\r
1549 return;\r
1550 }\r
1551\r
1552 if (PcdGetBool (PcdFirmwareDebuggerInitialized)) {\r
1553 Status = MeasureLaunchOfFirmwareDebugger ();\r
1554 DEBUG ((EFI_D_ERROR, "MeasureLaunchOfFirmwareDebugger - %r\n", Status));\r
1555 }\r
1556\r
1557 Status = MeasureAllSecureVariables ();\r
1558 DEBUG ((EFI_D_ERROR, "MeasureAllSecureVariables - %r\n", Status));\r
1559\r
1560 //\r
1561 // We need measure Separator(7) here, because this event must be between SecureBootPolicy (Configure)\r
1562 // and ImageVerification (Authority)\r
1563 // There might be a case that we need measure UEFI image from DriverOrder, besides BootOrder. So\r
1564 // the Authority measurement happen before ReadToBoot event.\r
1565 //\r
1566 Status = MeasureSeparatorEvent (7);\r
1567 DEBUG ((EFI_D_ERROR, "MeasureSeparatorEvent - %r\n", Status));\r
1568 return ;\r
1569}\r
1570\r
1571/**\r
1572 Ready to Boot Event notification handler.\r
1573\r
1574 Sequence of OS boot events is measured in this event notification handler.\r
1575\r
1576 @param[in] Event Event whose notification function is being invoked\r
1577 @param[in] Context Pointer to the notification function's context\r
1578\r
1579**/\r
1580VOID\r
1581EFIAPI\r
1582OnReadyToBoot (\r
1583 IN EFI_EVENT Event,\r
1584 IN VOID *Context\r
1585 )\r
1586{\r
1587 EFI_STATUS Status;\r
1588 TPM_PCRINDEX PcrIndex;\r
1589\r
1590 PERF_START_EX (mImageHandle, "EventRec", "TrEEDxe", 0, PERF_ID_TREE_DXE);\r
1591 if (mBootAttempts == 0) {\r
1592\r
1593 //\r
1594 // Measure handoff tables.\r
1595 //\r
1596 Status = MeasureHandoffTables ();\r
1597 if (EFI_ERROR (Status)) {\r
1598 DEBUG ((EFI_D_ERROR, "HOBs not Measured. Error!\n"));\r
1599 }\r
1600\r
1601 //\r
1602 // Measure BootOrder & Boot#### variables.\r
1603 //\r
1604 Status = MeasureAllBootVariables ();\r
1605 if (EFI_ERROR (Status)) {\r
1606 DEBUG ((EFI_D_ERROR, "Boot Variables not Measured. Error!\n"));\r
1607 }\r
1608\r
1609 //\r
1610 // 1. This is the first boot attempt.\r
1611 //\r
1612 Status = TcgMeasureAction (\r
1613 EFI_CALLING_EFI_APPLICATION\r
1614 );\r
1615\r
1616 //\r
1617 // 2. Draw a line between pre-boot env and entering post-boot env.\r
1618 // PCR[7] is already done.\r
1619 //\r
1620 for (PcrIndex = 0; PcrIndex < 7; PcrIndex++) {\r
1621 Status = MeasureSeparatorEvent (PcrIndex);\r
1622 }\r
1623\r
1624 //\r
1625 // 3. Measure GPT. It would be done in SAP driver.\r
1626 //\r
1627\r
1628 //\r
1629 // 4. Measure PE/COFF OS loader. It would be done in SAP driver.\r
1630 //\r
1631\r
1632 //\r
1633 // 5. Read & Measure variable. BootOrder already measured.\r
1634 //\r
1635 } else {\r
1636 //\r
1637 // 6. Not first attempt, meaning a return from last attempt\r
1638 //\r
1639 Status = TcgMeasureAction (\r
1640 EFI_RETURNING_FROM_EFI_APPLICATOIN\r
1641 );\r
1642 }\r
1643\r
1644 DEBUG ((EFI_D_INFO, "TPM2 TrEEDxe Measure Data when ReadyToBoot\n"));\r
1645 //\r
1646 // Increase boot attempt counter.\r
1647 //\r
1648 mBootAttempts++;\r
1649 PERF_END_EX (mImageHandle, "EventRec", "TrEEDxe", 0, PERF_ID_TREE_DXE + 1);\r
1650}\r
1651\r
1652/**\r
1653 Install TCG ACPI Table when ACPI Table Protocol is available.\r
1654\r
1655 A system's firmware uses an ACPI table to identify the system's TCG capabilities \r
1656 to the Post-Boot environment. The information in this ACPI table is not guaranteed \r
1657 to be valid until the Host Platform transitions from pre-boot state to post-boot state. \r
1658\r
1659 @param[in] Event Event whose notification function is being invoked\r
1660 @param[in] Context Pointer to the notification function's context\r
1661**/\r
1662VOID\r
1663EFIAPI\r
1664InstallAcpiTable (\r
1665 IN EFI_EVENT Event,\r
1666 IN VOID *Context\r
1667 )\r
1668{\r
1669 UINTN TableKey;\r
1670 EFI_STATUS Status;\r
1671 EFI_ACPI_TABLE_PROTOCOL *AcpiTable;\r
1672 UINT8 Checksum;\r
1673 UINT64 OemTableId;\r
1674\r
1675 Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **)&AcpiTable);\r
1676 if (EFI_ERROR (Status)) {\r
1677 return;\r
1678 }\r
1679\r
1680 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {\r
1681 CopyMem (mTcgClientAcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTcgClientAcpiTemplate.Header.OemId));\r
1682 OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);\r
1683 CopyMem (&mTcgClientAcpiTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));\r
1684 mTcgClientAcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);\r
1685 mTcgClientAcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);\r
1686 mTcgClientAcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);\r
1687 //\r
1688 // The ACPI table must be checksumed before calling the InstallAcpiTable() \r
1689 // service of the ACPI table protocol to install it.\r
1690 //\r
1691 Checksum = CalculateCheckSum8 ((UINT8 *)&mTcgClientAcpiTemplate, sizeof (mTcgClientAcpiTemplate));\r
1692 mTcgClientAcpiTemplate.Header.Checksum = Checksum;\r
1693\r
1694 Status = AcpiTable->InstallAcpiTable (\r
1695 AcpiTable,\r
1696 &mTcgClientAcpiTemplate,\r
1697 sizeof (mTcgClientAcpiTemplate),\r
1698 &TableKey\r
1699 );\r
1700 } else {\r
1701 CopyMem (mTcgServerAcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTcgServerAcpiTemplate.Header.OemId));\r
1702 OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);\r
1703 CopyMem (&mTcgServerAcpiTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));\r
1704 mTcgServerAcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);\r
1705 mTcgServerAcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);\r
1706 mTcgServerAcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);\r
1707 //\r
1708 // The ACPI table must be checksumed before calling the InstallAcpiTable() \r
1709 // service of the ACPI table protocol to install it.\r
1710 //\r
1711 Checksum = CalculateCheckSum8 ((UINT8 *)&mTcgServerAcpiTemplate, sizeof (mTcgServerAcpiTemplate));\r
1712 mTcgServerAcpiTemplate.Header.Checksum = Checksum;\r
1713\r
1714 mTcgServerAcpiTemplate.BaseAddress.Address = PcdGet64 (PcdTpmBaseAddress);\r
1715 Status = AcpiTable->InstallAcpiTable (\r
1716 AcpiTable,\r
1717 &mTcgServerAcpiTemplate,\r
1718 sizeof (mTcgServerAcpiTemplate),\r
1719 &TableKey\r
1720 );\r
1721 }\r
1722 ASSERT_EFI_ERROR (Status);\r
1723}\r
1724\r
1725/**\r
1726 Exit Boot Services Event notification handler.\r
1727\r
1728 Measure invocation and success of ExitBootServices.\r
1729\r
1730 @param[in] Event Event whose notification function is being invoked\r
1731 @param[in] Context Pointer to the notification function's context\r
1732\r
1733**/\r
1734VOID\r
1735EFIAPI\r
1736OnExitBootServices (\r
1737 IN EFI_EVENT Event,\r
1738 IN VOID *Context\r
1739 )\r
1740{\r
1741 EFI_STATUS Status;\r
1742\r
1743 //\r
1744 // Measure invocation of ExitBootServices,\r
1745 //\r
1746 Status = TcgMeasureAction (\r
1747 EFI_EXIT_BOOT_SERVICES_INVOCATION\r
1748 );\r
1749 ASSERT_EFI_ERROR (Status);\r
1750\r
1751 //\r
1752 // Measure success of ExitBootServices\r
1753 //\r
1754 Status = TcgMeasureAction (\r
1755 EFI_EXIT_BOOT_SERVICES_SUCCEEDED\r
1756 );\r
1757 ASSERT_EFI_ERROR (Status);\r
1758}\r
1759\r
1760/**\r
1761 Exit Boot Services Failed Event notification handler.\r
1762\r
1763 Measure Failure of ExitBootServices.\r
1764\r
1765 @param[in] Event Event whose notification function is being invoked\r
1766 @param[in] Context Pointer to the notification function's context\r
1767\r
1768**/\r
1769VOID\r
1770EFIAPI\r
1771OnExitBootServicesFailed (\r
1772 IN EFI_EVENT Event,\r
1773 IN VOID *Context\r
1774 )\r
1775{\r
1776 EFI_STATUS Status;\r
1777\r
1778 //\r
1779 // Measure Failure of ExitBootServices,\r
1780 //\r
1781 Status = TcgMeasureAction (\r
1782 EFI_EXIT_BOOT_SERVICES_FAILED\r
1783 );\r
1784 ASSERT_EFI_ERROR (Status);\r
1785\r
1786}\r
1787\r
1788/**\r
1789 The function install TrEE protocol.\r
1790 \r
1791 @retval EFI_SUCCESS TrEE protocol is installed.\r
1792 @retval other Some error occurs.\r
1793**/\r
1794EFI_STATUS\r
1795InstallTrEE (\r
1796 VOID\r
1797 )\r
1798{\r
1799 EFI_STATUS Status;\r
1800 EFI_HANDLE Handle;\r
1801\r
1802 Handle = NULL;\r
1803 Status = gBS->InstallMultipleProtocolInterfaces (\r
1804 &Handle,\r
1805 &gEfiTrEEProtocolGuid,\r
1806 &mTreeProtocol,\r
1807 NULL\r
1808 );\r
1809 return Status;\r
1810}\r
1811\r
1812/**\r
1813 The driver's entry point. It publishes EFI TrEE Protocol.\r
1814\r
1815 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
1816 @param[in] SystemTable A pointer to the EFI System Table.\r
1817 \r
1818 @retval EFI_SUCCESS The entry point is executed successfully.\r
1819 @retval other Some error occurs when executing this entry point.\r
1820**/\r
1821EFI_STATUS\r
1822EFIAPI\r
1823DriverEntry (\r
1824 IN EFI_HANDLE ImageHandle,\r
1825 IN EFI_SYSTEM_TABLE *SystemTable\r
1826 )\r
1827{\r
1828 EFI_STATUS Status;\r
1829 EFI_EVENT Event;\r
1830 VOID *Registration;\r
1831 UINT32 MaxCommandSize;\r
1832 UINT32 MaxResponseSize;\r
1833 TPML_PCR_SELECTION Pcrs;\r
1834 UINTN Index;\r
1835 UINT32 TpmHashAlgorithmBitmap;\r
1836\r
1837 mImageHandle = ImageHandle;\r
1838\r
1839 if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceNoneGuid) ||\r
1840 CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){\r
1841 DEBUG ((EFI_D_ERROR, "No TPM2 instance required!\n"));\r
1842 return EFI_UNSUPPORTED;\r
1843 }\r
1844\r
1845 Status = Tpm2RequestUseTpm ();\r
1846 if (EFI_ERROR (Status)) {\r
1847 DEBUG ((EFI_D_ERROR, "TPM not detected!\n"));\r
1848 return Status;\r
1849 }\r
1850\r
1851 //\r
1852 // Fill information\r
1853 //\r
1854 DEBUG ((EFI_D_ERROR, "TrEE.ProtocolVersion - %02x.%02x\n", mTcgDxeData.BsCap.ProtocolVersion.Major, mTcgDxeData.BsCap.ProtocolVersion.Minor));\r
1855 DEBUG ((EFI_D_ERROR, "TrEE.StructureVersion - %02x.%02x\n", mTcgDxeData.BsCap.StructureVersion.Major, mTcgDxeData.BsCap.StructureVersion.Minor));\r
1856\r
1857 Status = Tpm2GetCapabilityManufactureID (&mTcgDxeData.BsCap.ManufacturerID);\r
1858 if (EFI_ERROR (Status)) {\r
1859 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityManufactureID fail!\n"));\r
1860 } else {\r
1861 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityManufactureID - %08x\n", mTcgDxeData.BsCap.ManufacturerID));\r
1862 }\r
1863\r
1864 DEBUG_CODE (\r
1865 UINT32 FirmwareVersion1;\r
1866 UINT32 FirmwareVersion2;\r
1867\r
1868 Status = Tpm2GetCapabilityFirmwareVersion (&FirmwareVersion1, &FirmwareVersion2);\r
1869 if (EFI_ERROR (Status)) {\r
1870 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityFirmwareVersion fail!\n"));\r
1871 } else {\r
1872 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityFirmwareVersion - %08x %08x\n", FirmwareVersion1, FirmwareVersion2));\r
1873 }\r
1874 );\r
1875\r
1876 Status = Tpm2GetCapabilityMaxCommandResponseSize (&MaxCommandSize, &MaxResponseSize);\r
1877 if (EFI_ERROR (Status)) {\r
1878 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityMaxCommandResponseSize fail!\n"));\r
1879 } else {\r
1880 mTcgDxeData.BsCap.MaxCommandSize = (UINT16)MaxCommandSize;\r
1881 mTcgDxeData.BsCap.MaxResponseSize = (UINT16)MaxResponseSize;\r
1882 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityMaxCommandResponseSize - %08x, %08x\n", MaxCommandSize, MaxResponseSize));\r
1883 }\r
1884\r
1885 Status = Tpm2GetCapabilityPcrs (&Pcrs);\r
1886 if (EFI_ERROR (Status)) {\r
1887 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityPcrs fail!\n"));\r
1888 TpmHashAlgorithmBitmap = TREE_BOOT_HASH_ALG_SHA1;\r
1889 } else {\r
1890 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityPcrs Count - %08x\n", Pcrs.count));\r
1891 TpmHashAlgorithmBitmap = 0;\r
1892 for (Index = 0; Index < Pcrs.count; Index++) {\r
1893 DEBUG ((EFI_D_ERROR, "hash - %x\n", Pcrs.pcrSelections[Index].hash));\r
1894 switch (Pcrs.pcrSelections[Index].hash) {\r
1895 case TPM_ALG_SHA1:\r
1896 TpmHashAlgorithmBitmap |= TREE_BOOT_HASH_ALG_SHA1;\r
1897 break;\r
1898 case TPM_ALG_SHA256:\r
1899 TpmHashAlgorithmBitmap |= TREE_BOOT_HASH_ALG_SHA256;\r
1900 break;\r
1901 case TPM_ALG_SHA384:\r
1902 TpmHashAlgorithmBitmap |= TREE_BOOT_HASH_ALG_SHA384;\r
1903 break;\r
1904 case TPM_ALG_SHA512:\r
1905 TpmHashAlgorithmBitmap |= TREE_BOOT_HASH_ALG_SHA512;\r
1906 break;\r
1907 case TPM_ALG_SM3_256:\r
1908 // TBD: Spec not define TREE_BOOT_HASH_ALG_SM3_256 yet\r
1909 break;\r
1910 }\r
1911 }\r
1912 }\r
1913 DEBUG ((EFI_D_ERROR, "TPM.HashAlgorithmBitmap - 0x%08x\n", TpmHashAlgorithmBitmap));\r
1914\r
1915 DEBUG ((EFI_D_ERROR, "TrEE.SupportedEventLogs - 0x%08x\n", mTcgDxeData.BsCap.SupportedEventLogs));\r
1916 mTcgDxeData.BsCap.HashAlgorithmBitmap = TpmHashAlgorithmBitmap;\r
1917 DEBUG ((EFI_D_ERROR, "TrEE.HashAlgorithmBitmap - 0x%08x\n", mTcgDxeData.BsCap.HashAlgorithmBitmap));\r
1918\r
1919 if (mTcgDxeData.BsCap.TrEEPresentFlag) {\r
1920 //\r
1921 // Setup the log area and copy event log from hob list to it\r
1922 //\r
1923 Status = SetupEventLog ();\r
1924 ASSERT_EFI_ERROR (Status);\r
1925\r
1926 //\r
1927 // Measure handoff tables, Boot#### variables etc.\r
1928 //\r
1929 Status = EfiCreateEventReadyToBootEx (\r
1930 TPL_CALLBACK,\r
1931 OnReadyToBoot,\r
1932 NULL,\r
1933 &Event\r
1934 );\r
1935\r
1936 Status = gBS->CreateEventEx (\r
1937 EVT_NOTIFY_SIGNAL,\r
1938 TPL_NOTIFY,\r
1939 OnExitBootServices,\r
1940 NULL,\r
1941 &gEfiEventExitBootServicesGuid,\r
1942 &Event\r
1943 );\r
1944\r
1945 //\r
1946 // Measure Exit Boot Service failed \r
1947 //\r
1948 Status = gBS->CreateEventEx (\r
1949 EVT_NOTIFY_SIGNAL,\r
1950 TPL_NOTIFY,\r
1951 OnExitBootServicesFailed,\r
1952 NULL,\r
1953 &gEventExitBootServicesFailedGuid,\r
1954 &Event\r
1955 );\r
1956\r
1957 //\r
1958 // Create event callback, because we need access variable on SecureBootPolicyVariable\r
1959 // We should use VariableWriteArch instead of VariableArch, because Variable driver\r
1960 // may update SecureBoot value based on last setting.\r
1961 //\r
1962 EfiCreateProtocolNotifyEvent (&gEfiVariableWriteArchProtocolGuid, TPL_CALLBACK, MeasureSecureBootPolicy, NULL, &Registration);\r
1963 }\r
1964\r
1965 //\r
1966 // Install ACPI Table\r
1967 //\r
1968 EfiCreateProtocolNotifyEvent (&gEfiAcpiTableProtocolGuid, TPL_CALLBACK, InstallAcpiTable, NULL, &Registration);\r
1969\r
1970 //\r
1971 // Install TrEEProtocol\r
1972 //\r
1973 Status = InstallTrEE ();\r
1974 DEBUG ((EFI_D_ERROR, "InstallTrEE - %r\n", Status));\r
1975\r
1976 return Status;\r
1977}\r