]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.c
SecurityPkg: Fix typos in comments
[mirror_edk2.git] / SecurityPkg / Tcg / Tcg2Dxe / Tcg2Dxe.c
1 /** @file
2 This module implements Tcg2 Protocol.
3
4 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <PiDxe.h>
17 #include <IndustryStandard/Acpi.h>
18 #include <IndustryStandard/PeImage.h>
19 #include <IndustryStandard/TcpaAcpi.h>
20
21 #include <Guid/GlobalVariable.h>
22 #include <Guid/HobList.h>
23 #include <Guid/TcgEventHob.h>
24 #include <Guid/EventGroup.h>
25 #include <Guid/EventExitBootServiceFailed.h>
26 #include <Guid/ImageAuthentication.h>
27 #include <Guid/TpmInstance.h>
28
29 #include <Protocol/DevicePath.h>
30 #include <Protocol/MpService.h>
31 #include <Protocol/VariableWrite.h>
32 #include <Protocol/Tcg2Protocol.h>
33 #include <Protocol/TrEEProtocol.h>
34
35 #include <Library/DebugLib.h>
36 #include <Library/BaseMemoryLib.h>
37 #include <Library/UefiRuntimeServicesTableLib.h>
38 #include <Library/UefiDriverEntryPoint.h>
39 #include <Library/HobLib.h>
40 #include <Library/UefiBootServicesTableLib.h>
41 #include <Library/BaseLib.h>
42 #include <Library/MemoryAllocationLib.h>
43 #include <Library/PrintLib.h>
44 #include <Library/Tpm2CommandLib.h>
45 #include <Library/PcdLib.h>
46 #include <Library/UefiLib.h>
47 #include <Library/Tpm2DeviceLib.h>
48 #include <Library/HashLib.h>
49 #include <Library/PerformanceLib.h>
50 #include <Library/ReportStatusCodeLib.h>
51 #include <Library/Tcg2PhysicalPresenceLib.h>
52
53 #define PERF_ID_TCG2_DXE 0x3120
54
55 typedef struct {
56 CHAR16 *VariableName;
57 EFI_GUID *VendorGuid;
58 } VARIABLE_TYPE;
59
60 #define TCG2_DEFAULT_MAX_COMMAND_SIZE 0x1000
61 #define TCG2_DEFAULT_MAX_RESPONSE_SIZE 0x1000
62
63 typedef struct {
64 EFI_GUID *EventGuid;
65 EFI_TCG2_EVENT_LOG_FORMAT LogFormat;
66 } TCG2_EVENT_INFO_STRUCT;
67
68 TCG2_EVENT_INFO_STRUCT mTcg2EventInfo[] = {
69 {&gTcgEventEntryHobGuid, EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2},
70 {&gTcgEvent2EntryHobGuid, EFI_TCG2_EVENT_LOG_FORMAT_TCG_2},
71 };
72
73 #define TCG_EVENT_LOG_AREA_COUNT_MAX 2
74
75 typedef struct {
76 EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat;
77 EFI_PHYSICAL_ADDRESS Lasa;
78 UINT64 Laml;
79 UINTN EventLogSize;
80 UINT8 *LastEvent;
81 BOOLEAN EventLogStarted;
82 BOOLEAN EventLogTruncated;
83 } TCG_EVENT_LOG_AREA_STRUCT;
84
85 typedef struct _TCG_DXE_DATA {
86 EFI_TCG2_BOOT_SERVICE_CAPABILITY BsCap;
87 TCG_EVENT_LOG_AREA_STRUCT EventLogAreaStruct[TCG_EVENT_LOG_AREA_COUNT_MAX];
88 BOOLEAN GetEventLogCalled[TCG_EVENT_LOG_AREA_COUNT_MAX];
89 TCG_EVENT_LOG_AREA_STRUCT FinalEventLogAreaStruct[TCG_EVENT_LOG_AREA_COUNT_MAX];
90 EFI_TCG2_FINAL_EVENTS_TABLE *FinalEventsTable[TCG_EVENT_LOG_AREA_COUNT_MAX];
91 } TCG_DXE_DATA;
92
93 TCG_DXE_DATA mTcgDxeData = {
94 {
95 sizeof (EFI_TCG2_BOOT_SERVICE_CAPABILITY), // Size
96 { 1, 1 }, // StructureVersion
97 { 1, 1 }, // ProtocolVersion
98 EFI_TCG2_BOOT_HASH_ALG_SHA1, // HashAlgorithmBitmap
99 EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2, // SupportedEventLogs
100 TRUE, // TPMPresentFlag
101 TCG2_DEFAULT_MAX_COMMAND_SIZE, // MaxCommandSize
102 TCG2_DEFAULT_MAX_RESPONSE_SIZE, // MaxResponseSize
103 0, // ManufacturerID
104 0, // NumberOfPCRBanks
105 0, // ActivePcrBanks
106 },
107 };
108
109 UINTN mBootAttempts = 0;
110 CHAR16 mBootVarName[] = L"BootOrder";
111
112 VARIABLE_TYPE mVariableType[] = {
113 {EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid},
114 {EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid},
115 {EFI_KEY_EXCHANGE_KEY_NAME, &gEfiGlobalVariableGuid},
116 {EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid},
117 {EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid},
118 };
119
120 EFI_HANDLE mImageHandle;
121
122 /**
123 Measure PE image into TPM log based on the authenticode image hashing in
124 PE/COFF Specification 8.0 Appendix A.
125
126 Caution: This function may receive untrusted input.
127 PE/COFF image is external input, so this function will validate its data structure
128 within this image buffer before use.
129
130 Notes: PE/COFF image is checked by BasePeCoffLib PeCoffLoaderGetImageInfo().
131
132 @param[in] PCRIndex TPM PCR index
133 @param[in] ImageAddress Start address of image buffer.
134 @param[in] ImageSize Image size
135 @param[out] DigestList Digeest list of this image.
136
137 @retval EFI_SUCCESS Successfully measure image.
138 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.
139 @retval other error value
140 **/
141 EFI_STATUS
142 MeasurePeImageAndExtend (
143 IN UINT32 PCRIndex,
144 IN EFI_PHYSICAL_ADDRESS ImageAddress,
145 IN UINTN ImageSize,
146 OUT TPML_DIGEST_VALUES *DigestList
147 );
148
149 /**
150
151 This function dump raw data.
152
153 @param Data raw data
154 @param Size raw data size
155
156 **/
157 VOID
158 InternalDumpData (
159 IN UINT8 *Data,
160 IN UINTN Size
161 )
162 {
163 UINTN Index;
164 for (Index = 0; Index < Size; Index++) {
165 DEBUG ((EFI_D_INFO, "%02x", (UINTN)Data[Index]));
166 }
167 }
168
169 /**
170
171 This function dump raw data with colume format.
172
173 @param Data raw data
174 @param Size raw data size
175
176 **/
177 VOID
178 InternalDumpHex (
179 IN UINT8 *Data,
180 IN UINTN Size
181 )
182 {
183 UINTN Index;
184 UINTN Count;
185 UINTN Left;
186
187 #define COLUME_SIZE (16 * 2)
188
189 Count = Size / COLUME_SIZE;
190 Left = Size % COLUME_SIZE;
191 for (Index = 0; Index < Count; Index++) {
192 DEBUG ((EFI_D_INFO, "%04x: ", Index * COLUME_SIZE));
193 InternalDumpData (Data + Index * COLUME_SIZE, COLUME_SIZE);
194 DEBUG ((EFI_D_INFO, "\n"));
195 }
196
197 if (Left != 0) {
198 DEBUG ((EFI_D_INFO, "%04x: ", Index * COLUME_SIZE));
199 InternalDumpData (Data + Index * COLUME_SIZE, Left);
200 DEBUG ((EFI_D_INFO, "\n"));
201 }
202 }
203
204 /**
205 Get All processors EFI_CPU_LOCATION in system. LocationBuf is allocated inside the function
206 Caller is responsible to free LocationBuf.
207
208 @param[out] LocationBuf Returns Processor Location Buffer.
209 @param[out] Num Returns processor number.
210
211 @retval EFI_SUCCESS Operation completed successfully.
212 @retval EFI_UNSUPPORTED MpService protocol not found.
213
214 **/
215 EFI_STATUS
216 GetProcessorsCpuLocation (
217 OUT EFI_CPU_PHYSICAL_LOCATION **LocationBuf,
218 OUT UINTN *Num
219 )
220 {
221 EFI_STATUS Status;
222 EFI_MP_SERVICES_PROTOCOL *MpProtocol;
223 UINTN ProcessorNum;
224 UINTN EnabledProcessorNum;
225 EFI_PROCESSOR_INFORMATION ProcessorInfo;
226 EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
227 UINTN Index;
228
229 Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **) &MpProtocol);
230 if (EFI_ERROR (Status)) {
231 //
232 // MP protocol is not installed
233 //
234 return EFI_UNSUPPORTED;
235 }
236
237 Status = MpProtocol->GetNumberOfProcessors(
238 MpProtocol,
239 &ProcessorNum,
240 &EnabledProcessorNum
241 );
242 if (EFI_ERROR(Status)){
243 return Status;
244 }
245
246 Status = gBS->AllocatePool(
247 EfiBootServicesData,
248 sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
249 (VOID **) &ProcessorLocBuf
250 );
251 if (EFI_ERROR(Status)){
252 return Status;
253 }
254
255 //
256 // Get each processor Location info
257 //
258 for (Index = 0; Index < ProcessorNum; Index++) {
259 Status = MpProtocol->GetProcessorInfo(
260 MpProtocol,
261 Index,
262 &ProcessorInfo
263 );
264 if (EFI_ERROR(Status)){
265 FreePool(ProcessorLocBuf);
266 return Status;
267 }
268
269 //
270 // Get all Processor Location info & measure
271 //
272 CopyMem(
273 &ProcessorLocBuf[Index],
274 &ProcessorInfo.Location,
275 sizeof(EFI_CPU_PHYSICAL_LOCATION)
276 );
277 }
278
279 *LocationBuf = ProcessorLocBuf;
280 *Num = ProcessorNum;
281
282 return Status;
283 }
284
285 /**
286 The EFI_TCG2_PROTOCOL GetCapability function call provides protocol
287 capability information and state information.
288
289 @param[in] This Indicates the calling context
290 @param[in, out] ProtocolCapability The caller allocates memory for a EFI_TCG2_BOOT_SERVICE_CAPABILITY
291 structure and sets the size field to the size of the structure allocated.
292 The callee fills in the fields with the EFI protocol capability information
293 and the current EFI TCG2 state information up to the number of fields which
294 fit within the size of the structure passed in.
295
296 @retval EFI_SUCCESS Operation completed successfully.
297 @retval EFI_DEVICE_ERROR The command was unsuccessful.
298 The ProtocolCapability variable will not be populated.
299 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
300 The ProtocolCapability variable will not be populated.
301 @retval EFI_BUFFER_TOO_SMALL The ProtocolCapability variable is too small to hold the full response.
302 It will be partially populated (required Size field will be set).
303 **/
304 EFI_STATUS
305 EFIAPI
306 Tcg2GetCapability (
307 IN EFI_TCG2_PROTOCOL *This,
308 IN OUT EFI_TCG2_BOOT_SERVICE_CAPABILITY *ProtocolCapability
309 )
310 {
311 DEBUG ((DEBUG_VERBOSE, "Tcg2GetCapability ...\n"));
312
313 if ((This == NULL) || (ProtocolCapability == NULL)) {
314 return EFI_INVALID_PARAMETER;
315 }
316
317 DEBUG ((DEBUG_VERBOSE, "Size - 0x%x\n", ProtocolCapability->Size));
318 DEBUG ((DEBUG_VERBOSE, " 1.1 - 0x%x, 1.0 - 0x%x\n", sizeof(EFI_TCG2_BOOT_SERVICE_CAPABILITY), sizeof(TREE_BOOT_SERVICE_CAPABILITY_1_0)));
319
320 if (ProtocolCapability->Size < mTcgDxeData.BsCap.Size) {
321 //
322 // Handle the case that firmware support 1.1 but OS only support 1.0.
323 //
324 if ((mTcgDxeData.BsCap.ProtocolVersion.Major > 0x01) ||
325 ((mTcgDxeData.BsCap.ProtocolVersion.Major == 0x01) && ((mTcgDxeData.BsCap.ProtocolVersion.Minor > 0x00)))) {
326 if (ProtocolCapability->Size >= sizeof(TREE_BOOT_SERVICE_CAPABILITY_1_0)) {
327 CopyMem (ProtocolCapability, &mTcgDxeData.BsCap, sizeof(TREE_BOOT_SERVICE_CAPABILITY_1_0));
328 ProtocolCapability->Size = sizeof(TREE_BOOT_SERVICE_CAPABILITY_1_0);
329 ProtocolCapability->StructureVersion.Major = 1;
330 ProtocolCapability->StructureVersion.Minor = 0;
331 ProtocolCapability->ProtocolVersion.Major = 1;
332 ProtocolCapability->ProtocolVersion.Minor = 0;
333 DEBUG ((EFI_D_ERROR, "TreeGetCapability (Compatible) - %r\n", EFI_SUCCESS));
334 return EFI_SUCCESS;
335 }
336 }
337 ProtocolCapability->Size = mTcgDxeData.BsCap.Size;
338 return EFI_BUFFER_TOO_SMALL;
339 }
340
341 CopyMem (ProtocolCapability, &mTcgDxeData.BsCap, mTcgDxeData.BsCap.Size);
342 DEBUG ((DEBUG_VERBOSE, "Tcg2GetCapability - %r\n", EFI_SUCCESS));
343 return EFI_SUCCESS;
344 }
345
346 /**
347 This function dump PCR event.
348
349 @param[in] EventHdr TCG PCR event structure.
350 **/
351 VOID
352 DumpEvent (
353 IN TCG_PCR_EVENT_HDR *EventHdr
354 )
355 {
356 UINTN Index;
357
358 DEBUG ((EFI_D_INFO, " Event:\n"));
359 DEBUG ((EFI_D_INFO, " PCRIndex - %d\n", EventHdr->PCRIndex));
360 DEBUG ((EFI_D_INFO, " EventType - 0x%08x\n", EventHdr->EventType));
361 DEBUG ((EFI_D_INFO, " Digest - "));
362 for (Index = 0; Index < sizeof(TCG_DIGEST); Index++) {
363 DEBUG ((EFI_D_INFO, "%02x ", EventHdr->Digest.digest[Index]));
364 }
365 DEBUG ((EFI_D_INFO, "\n"));
366 DEBUG ((EFI_D_INFO, " EventSize - 0x%08x\n", EventHdr->EventSize));
367 InternalDumpHex ((UINT8 *)(EventHdr + 1), EventHdr->EventSize);
368 }
369
370 /**
371 This function dump TCG_EfiSpecIDEventStruct.
372
373 @param[in] TcgEfiSpecIdEventStruct A pointer to TCG_EfiSpecIDEventStruct.
374 **/
375 VOID
376 DumpTcgEfiSpecIdEventStruct (
377 IN TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct
378 )
379 {
380 TCG_EfiSpecIdEventAlgorithmSize *DigestSize;
381 UINTN Index;
382 UINT8 *VendorInfoSize;
383 UINT8 *VendorInfo;
384 UINT32 NumberOfAlgorithms;
385
386 DEBUG ((EFI_D_INFO, " TCG_EfiSpecIDEventStruct:\n"));
387 DEBUG ((EFI_D_INFO, " signature - '"));
388 for (Index = 0; Index < sizeof(TcgEfiSpecIdEventStruct->signature); Index++) {
389 DEBUG ((EFI_D_INFO, "%c", TcgEfiSpecIdEventStruct->signature[Index]));
390 }
391 DEBUG ((EFI_D_INFO, "'\n"));
392 DEBUG ((EFI_D_INFO, " platformClass - 0x%08x\n", TcgEfiSpecIdEventStruct->platformClass));
393 DEBUG ((EFI_D_INFO, " specVersion - %d.%d%d\n", TcgEfiSpecIdEventStruct->specVersionMajor, TcgEfiSpecIdEventStruct->specVersionMinor, TcgEfiSpecIdEventStruct->specErrata));
394 DEBUG ((EFI_D_INFO, " uintnSize - 0x%02x\n", TcgEfiSpecIdEventStruct->uintnSize));
395
396 CopyMem (&NumberOfAlgorithms, TcgEfiSpecIdEventStruct + 1, sizeof(NumberOfAlgorithms));
397 DEBUG ((EFI_D_INFO, " NumberOfAlgorithms - 0x%08x\n", NumberOfAlgorithms));
398
399 DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct + sizeof(*TcgEfiSpecIdEventStruct) + sizeof(NumberOfAlgorithms));
400 for (Index = 0; Index < NumberOfAlgorithms; Index++) {
401 DEBUG ((EFI_D_INFO, " digest(%d)\n", Index));
402 DEBUG ((EFI_D_INFO, " algorithmId - 0x%04x\n", DigestSize[Index].algorithmId));
403 DEBUG ((EFI_D_INFO, " digestSize - 0x%04x\n", DigestSize[Index].digestSize));
404 }
405 VendorInfoSize = (UINT8 *)&DigestSize[NumberOfAlgorithms];
406 DEBUG ((EFI_D_INFO, " VendorInfoSize - 0x%02x\n", *VendorInfoSize));
407 VendorInfo = VendorInfoSize + 1;
408 DEBUG ((EFI_D_INFO, " VendorInfo - "));
409 for (Index = 0; Index < *VendorInfoSize; Index++) {
410 DEBUG ((EFI_D_INFO, "%02x ", VendorInfo[Index]));
411 }
412 DEBUG ((EFI_D_INFO, "\n"));
413 }
414
415 /**
416 This function get size of TCG_EfiSpecIDEventStruct.
417
418 @param[in] TcgEfiSpecIdEventStruct A pointer to TCG_EfiSpecIDEventStruct.
419 **/
420 UINTN
421 GetTcgEfiSpecIdEventStructSize (
422 IN TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct
423 )
424 {
425 TCG_EfiSpecIdEventAlgorithmSize *DigestSize;
426 UINT8 *VendorInfoSize;
427 UINT32 NumberOfAlgorithms;
428
429 CopyMem (&NumberOfAlgorithms, TcgEfiSpecIdEventStruct + 1, sizeof(NumberOfAlgorithms));
430
431 DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct + sizeof(*TcgEfiSpecIdEventStruct) + sizeof(NumberOfAlgorithms));
432 VendorInfoSize = (UINT8 *)&DigestSize[NumberOfAlgorithms];
433 return sizeof(TCG_EfiSpecIDEventStruct) + sizeof(UINT32) + (NumberOfAlgorithms * sizeof(TCG_EfiSpecIdEventAlgorithmSize)) + sizeof(UINT8) + (*VendorInfoSize);
434 }
435
436 /**
437 This function dump PCR event 2.
438
439 @param[in] TcgPcrEvent2 TCG PCR event 2 structure.
440 **/
441 VOID
442 DumpEvent2 (
443 IN TCG_PCR_EVENT2 *TcgPcrEvent2
444 )
445 {
446 UINTN Index;
447 UINT32 DigestIndex;
448 UINT32 DigestCount;
449 TPMI_ALG_HASH HashAlgo;
450 UINT32 DigestSize;
451 UINT8 *DigestBuffer;
452 UINT32 EventSize;
453 UINT8 *EventBuffer;
454
455 DEBUG ((EFI_D_INFO, " Event:\n"));
456 DEBUG ((EFI_D_INFO, " PCRIndex - %d\n", TcgPcrEvent2->PCRIndex));
457 DEBUG ((EFI_D_INFO, " EventType - 0x%08x\n", TcgPcrEvent2->EventType));
458
459 DEBUG ((EFI_D_INFO, " DigestCount: 0x%08x\n", TcgPcrEvent2->Digest.count));
460
461 DigestCount = TcgPcrEvent2->Digest.count;
462 HashAlgo = TcgPcrEvent2->Digest.digests[0].hashAlg;
463 DigestBuffer = (UINT8 *)&TcgPcrEvent2->Digest.digests[0].digest;
464 for (DigestIndex = 0; DigestIndex < DigestCount; DigestIndex++) {
465 DEBUG ((EFI_D_INFO, " HashAlgo : 0x%04x\n", HashAlgo));
466 DEBUG ((EFI_D_INFO, " Digest(%d): ", DigestIndex));
467 DigestSize = GetHashSizeFromAlgo (HashAlgo);
468 for (Index = 0; Index < DigestSize; Index++) {
469 DEBUG ((EFI_D_INFO, "%02x ", DigestBuffer[Index]));
470 }
471 DEBUG ((EFI_D_INFO, "\n"));
472 //
473 // Prepare next
474 //
475 CopyMem (&HashAlgo, DigestBuffer + DigestSize, sizeof(TPMI_ALG_HASH));
476 DigestBuffer = DigestBuffer + DigestSize + sizeof(TPMI_ALG_HASH);
477 }
478 DEBUG ((EFI_D_INFO, "\n"));
479 DigestBuffer = DigestBuffer - sizeof(TPMI_ALG_HASH);
480
481 CopyMem (&EventSize, DigestBuffer, sizeof(TcgPcrEvent2->EventSize));
482 DEBUG ((EFI_D_INFO, " EventSize - 0x%08x\n", EventSize));
483 EventBuffer = DigestBuffer + sizeof(TcgPcrEvent2->EventSize);
484 InternalDumpHex (EventBuffer, EventSize);
485 }
486
487 /**
488 This function returns size of TCG PCR event 2.
489
490 @param[in] TcgPcrEvent2 TCG PCR event 2 structure.
491
492 @return size of TCG PCR event 2.
493 **/
494 UINTN
495 GetPcrEvent2Size (
496 IN TCG_PCR_EVENT2 *TcgPcrEvent2
497 )
498 {
499 UINT32 DigestIndex;
500 UINT32 DigestCount;
501 TPMI_ALG_HASH HashAlgo;
502 UINT32 DigestSize;
503 UINT8 *DigestBuffer;
504 UINT32 EventSize;
505 UINT8 *EventBuffer;
506
507 DigestCount = TcgPcrEvent2->Digest.count;
508 HashAlgo = TcgPcrEvent2->Digest.digests[0].hashAlg;
509 DigestBuffer = (UINT8 *)&TcgPcrEvent2->Digest.digests[0].digest;
510 for (DigestIndex = 0; DigestIndex < DigestCount; DigestIndex++) {
511 DigestSize = GetHashSizeFromAlgo (HashAlgo);
512 //
513 // Prepare next
514 //
515 CopyMem (&HashAlgo, DigestBuffer + DigestSize, sizeof(TPMI_ALG_HASH));
516 DigestBuffer = DigestBuffer + DigestSize + sizeof(TPMI_ALG_HASH);
517 }
518 DigestBuffer = DigestBuffer - sizeof(TPMI_ALG_HASH);
519
520 CopyMem (&EventSize, DigestBuffer, sizeof(TcgPcrEvent2->EventSize));
521 EventBuffer = DigestBuffer + sizeof(TcgPcrEvent2->EventSize);
522
523 return (UINTN)EventBuffer + EventSize - (UINTN)TcgPcrEvent2;
524 }
525
526 /**
527 This function dump event log.
528
529 @param[in] EventLogFormat The type of the event log for which the information is requested.
530 @param[in] EventLogLocation A pointer to the memory address of the event log.
531 @param[in] EventLogLastEntry If the Event Log contains more than one entry, this is a pointer to the
532 address of the start of the last entry in the event log in memory.
533 @param[in] FinalEventsTable A pointer to the memory address of the final event table.
534 **/
535 VOID
536 DumpEventLog (
537 IN EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat,
538 IN EFI_PHYSICAL_ADDRESS EventLogLocation,
539 IN EFI_PHYSICAL_ADDRESS EventLogLastEntry,
540 IN EFI_TCG2_FINAL_EVENTS_TABLE *FinalEventsTable
541 )
542 {
543 TCG_PCR_EVENT_HDR *EventHdr;
544 TCG_PCR_EVENT2 *TcgPcrEvent2;
545 TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct;
546 UINTN NumberOfEvents;
547
548 DEBUG ((EFI_D_INFO, "EventLogFormat: (0x%x)\n", EventLogFormat));
549
550 switch (EventLogFormat) {
551 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2:
552 EventHdr = (TCG_PCR_EVENT_HDR *)(UINTN)EventLogLocation;
553 while ((UINTN)EventHdr <= EventLogLastEntry) {
554 DumpEvent (EventHdr);
555 EventHdr = (TCG_PCR_EVENT_HDR *)((UINTN)EventHdr + sizeof(TCG_PCR_EVENT_HDR) + EventHdr->EventSize);
556 }
557 if (FinalEventsTable == NULL) {
558 DEBUG ((EFI_D_INFO, "FinalEventsTable: NOT FOUND\n"));
559 } else {
560 DEBUG ((EFI_D_INFO, "FinalEventsTable: (0x%x)\n", FinalEventsTable));
561 DEBUG ((EFI_D_INFO, " Version: (0x%x)\n", FinalEventsTable->Version));
562 DEBUG ((EFI_D_INFO, " NumberOfEvents: (0x%x)\n", FinalEventsTable->NumberOfEvents));
563
564 EventHdr = (TCG_PCR_EVENT_HDR *)(UINTN)(FinalEventsTable + 1);
565 for (NumberOfEvents = 0; NumberOfEvents < FinalEventsTable->NumberOfEvents; NumberOfEvents++) {
566 DumpEvent (EventHdr);
567 EventHdr = (TCG_PCR_EVENT_HDR *)((UINTN)EventHdr + sizeof(TCG_PCR_EVENT_HDR) + EventHdr->EventSize);
568 }
569 }
570 break;
571 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2:
572 //
573 // Dump first event
574 //
575 EventHdr = (TCG_PCR_EVENT_HDR *)(UINTN)EventLogLocation;
576 DumpEvent (EventHdr);
577
578 TcgEfiSpecIdEventStruct = (TCG_EfiSpecIDEventStruct *)(EventHdr + 1);
579 DumpTcgEfiSpecIdEventStruct (TcgEfiSpecIdEventStruct);
580
581 TcgPcrEvent2 = (TCG_PCR_EVENT2 *)((UINTN)TcgEfiSpecIdEventStruct + GetTcgEfiSpecIdEventStructSize (TcgEfiSpecIdEventStruct));
582 while ((UINTN)TcgPcrEvent2 <= EventLogLastEntry) {
583 DumpEvent2 (TcgPcrEvent2);
584 TcgPcrEvent2 = (TCG_PCR_EVENT2 *)((UINTN)TcgPcrEvent2 + GetPcrEvent2Size (TcgPcrEvent2));
585 }
586
587 if (FinalEventsTable == NULL) {
588 DEBUG ((EFI_D_INFO, "FinalEventsTable: NOT FOUND\n"));
589 } else {
590 DEBUG ((EFI_D_INFO, "FinalEventsTable: (0x%x)\n", FinalEventsTable));
591 DEBUG ((EFI_D_INFO, " Version: (0x%x)\n", FinalEventsTable->Version));
592 DEBUG ((EFI_D_INFO, " NumberOfEvents: (0x%x)\n", FinalEventsTable->NumberOfEvents));
593
594 TcgPcrEvent2 = (TCG_PCR_EVENT2 *)(UINTN)(FinalEventsTable + 1);
595 for (NumberOfEvents = 0; NumberOfEvents < FinalEventsTable->NumberOfEvents; NumberOfEvents++) {
596 DumpEvent2 (TcgPcrEvent2);
597 TcgPcrEvent2 = (TCG_PCR_EVENT2 *)((UINTN)TcgPcrEvent2 + GetPcrEvent2Size (TcgPcrEvent2));
598 }
599 }
600 break;
601 }
602
603 return ;
604 }
605
606 /**
607 The EFI_TCG2_PROTOCOL Get Event Log function call allows a caller to
608 retrieve the address of a given event log and its last entry.
609
610 @param[in] This Indicates the calling context
611 @param[in] EventLogFormat The type of the event log for which the information is requested.
612 @param[out] EventLogLocation A pointer to the memory address of the event log.
613 @param[out] EventLogLastEntry If the Event Log contains more than one entry, this is a pointer to the
614 address of the start of the last entry in the event log in memory.
615 @param[out] EventLogTruncated If the Event Log is missing at least one entry because an event would
616 have exceeded the area allocated for events, this value is set to TRUE.
617 Otherwise, the value will be FALSE and the Event Log will be complete.
618
619 @retval EFI_SUCCESS Operation completed successfully.
620 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect
621 (e.g. asking for an event log whose format is not supported).
622 **/
623 EFI_STATUS
624 EFIAPI
625 Tcg2GetEventLog (
626 IN EFI_TCG2_PROTOCOL *This,
627 IN EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat,
628 OUT EFI_PHYSICAL_ADDRESS *EventLogLocation,
629 OUT EFI_PHYSICAL_ADDRESS *EventLogLastEntry,
630 OUT BOOLEAN *EventLogTruncated
631 )
632 {
633 UINTN Index;
634
635 DEBUG ((EFI_D_INFO, "Tcg2GetEventLog ... (0x%x)\n", EventLogFormat));
636
637 if (This == NULL) {
638 return EFI_INVALID_PARAMETER;
639 }
640
641 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
642 if (EventLogFormat == mTcg2EventInfo[Index].LogFormat) {
643 break;
644 }
645 }
646
647 if (Index == sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0])) {
648 return EFI_INVALID_PARAMETER;
649 }
650
651 if ((mTcg2EventInfo[Index].LogFormat & mTcgDxeData.BsCap.SupportedEventLogs) == 0) {
652 return EFI_INVALID_PARAMETER;
653 }
654
655 if (!mTcgDxeData.BsCap.TPMPresentFlag) {
656 if (EventLogLocation != NULL) {
657 *EventLogLocation = 0;
658 }
659 if (EventLogLastEntry != NULL) {
660 *EventLogLastEntry = 0;
661 }
662 if (EventLogTruncated != NULL) {
663 *EventLogTruncated = FALSE;
664 }
665 return EFI_SUCCESS;
666 }
667
668 if (EventLogLocation != NULL) {
669 *EventLogLocation = mTcgDxeData.EventLogAreaStruct[Index].Lasa;
670 DEBUG ((EFI_D_INFO, "Tcg2GetEventLog (EventLogLocation - %x)\n", *EventLogLocation));
671 }
672
673 if (EventLogLastEntry != NULL) {
674 if (!mTcgDxeData.EventLogAreaStruct[Index].EventLogStarted) {
675 *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)0;
676 } else {
677 *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)mTcgDxeData.EventLogAreaStruct[Index].LastEvent;
678 }
679 DEBUG ((EFI_D_INFO, "Tcg2GetEventLog (EventLogLastEntry - %x)\n", *EventLogLastEntry));
680 }
681
682 if (EventLogTruncated != NULL) {
683 *EventLogTruncated = mTcgDxeData.EventLogAreaStruct[Index].EventLogTruncated;
684 DEBUG ((EFI_D_INFO, "Tcg2GetEventLog (EventLogTruncated - %x)\n", *EventLogTruncated));
685 }
686
687 DEBUG ((EFI_D_INFO, "Tcg2GetEventLog - %r\n", EFI_SUCCESS));
688
689 // Dump Event Log for debug purpose
690 if ((EventLogLocation != NULL) && (EventLogLastEntry != NULL)) {
691 DumpEventLog (EventLogFormat, *EventLogLocation, *EventLogLastEntry, mTcgDxeData.FinalEventsTable[Index]);
692 }
693
694 //
695 // All events generated after the invocation of EFI_TCG2_GET_EVENT_LOG SHALL be stored
696 // in an instance of an EFI_CONFIGURATION_TABLE named by the VendorGuid of EFI_TCG2_FINAL_EVENTS_TABLE_GUID.
697 //
698 mTcgDxeData.GetEventLogCalled[Index] = TRUE;
699
700 return EFI_SUCCESS;
701 }
702
703 /**
704 Add a new entry to the Event Log.
705
706 @param[in, out] EventLogPtr Pointer to the Event Log data.
707 @param[in, out] LogSize Size of the Event Log.
708 @param[in] MaxSize Maximum size of the Event Log.
709 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure.
710 @param[in] NewEventHdrSize New event header size.
711 @param[in] NewEventData Pointer to the new event data.
712 @param[in] NewEventSize New event data size.
713
714 @retval EFI_SUCCESS The new event log entry was added.
715 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
716
717 **/
718 EFI_STATUS
719 TcgCommLogEvent (
720 IN OUT UINT8 **EventLogPtr,
721 IN OUT UINTN *LogSize,
722 IN UINTN MaxSize,
723 IN VOID *NewEventHdr,
724 IN UINT32 NewEventHdrSize,
725 IN UINT8 *NewEventData,
726 IN UINT32 NewEventSize
727 )
728 {
729 UINTN NewLogSize;
730
731 if (NewEventSize > MAX_ADDRESS - NewEventHdrSize) {
732 return EFI_OUT_OF_RESOURCES;
733 }
734
735 NewLogSize = NewEventHdrSize + NewEventSize;
736
737 if (NewLogSize > MAX_ADDRESS - *LogSize) {
738 return EFI_OUT_OF_RESOURCES;
739 }
740
741 if (NewLogSize + *LogSize > MaxSize) {
742 DEBUG ((EFI_D_INFO, " MaxSize - 0x%x\n", MaxSize));
743 DEBUG ((EFI_D_INFO, " NewLogSize - 0x%x\n", NewLogSize));
744 DEBUG ((EFI_D_INFO, " LogSize - 0x%x\n", *LogSize));
745 DEBUG ((EFI_D_INFO, "TcgCommLogEvent - %r\n", EFI_OUT_OF_RESOURCES));
746 return EFI_OUT_OF_RESOURCES;
747 }
748
749 *EventLogPtr += *LogSize;
750 *LogSize += NewLogSize;
751 CopyMem (*EventLogPtr, NewEventHdr, NewEventHdrSize);
752 CopyMem (
753 *EventLogPtr + NewEventHdrSize,
754 NewEventData,
755 NewEventSize
756 );
757 return EFI_SUCCESS;
758 }
759
760 /**
761 Add a new entry to the Event Log.
762
763 @param[in] EventLogFormat The type of the event log for which the information is requested.
764 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure.
765 @param[in] NewEventHdrSize New event header size.
766 @param[in] NewEventData Pointer to the new event data.
767 @param[in] NewEventSize New event data size.
768
769 @retval EFI_SUCCESS The new event log entry was added.
770 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
771
772 **/
773 EFI_STATUS
774 TcgDxeLogEvent (
775 IN EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat,
776 IN VOID *NewEventHdr,
777 IN UINT32 NewEventHdrSize,
778 IN UINT8 *NewEventData,
779 IN UINT32 NewEventSize
780 )
781 {
782 EFI_STATUS Status;
783 UINTN Index;
784 TCG_EVENT_LOG_AREA_STRUCT *EventLogAreaStruct;
785
786 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
787 if (EventLogFormat == mTcg2EventInfo[Index].LogFormat) {
788 break;
789 }
790 }
791
792 if (Index == sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0])) {
793 return EFI_INVALID_PARAMETER;
794 }
795
796 //
797 // Record to normal event log
798 //
799 EventLogAreaStruct = &mTcgDxeData.EventLogAreaStruct[Index];
800
801 if (EventLogAreaStruct->EventLogTruncated) {
802 return EFI_VOLUME_FULL;
803 }
804
805 EventLogAreaStruct->LastEvent = (UINT8*)(UINTN)EventLogAreaStruct->Lasa;
806 Status = TcgCommLogEvent (
807 &EventLogAreaStruct->LastEvent,
808 &EventLogAreaStruct->EventLogSize,
809 (UINTN)EventLogAreaStruct->Laml,
810 NewEventHdr,
811 NewEventHdrSize,
812 NewEventData,
813 NewEventSize
814 );
815
816 if (Status == EFI_OUT_OF_RESOURCES) {
817 EventLogAreaStruct->EventLogTruncated = TRUE;
818 return EFI_VOLUME_FULL;
819 } else if (Status == EFI_SUCCESS) {
820 EventLogAreaStruct->EventLogStarted = TRUE;
821 }
822
823 //
824 // If GetEventLog is called, record to FinalEventsTable, too.
825 //
826 if (mTcgDxeData.GetEventLogCalled[Index]) {
827 if (mTcgDxeData.FinalEventsTable[Index] == NULL) {
828 //
829 // no need for FinalEventsTable
830 //
831 return EFI_SUCCESS;
832 }
833 EventLogAreaStruct = &mTcgDxeData.FinalEventLogAreaStruct[Index];
834
835 if (EventLogAreaStruct->EventLogTruncated) {
836 return EFI_VOLUME_FULL;
837 }
838
839 EventLogAreaStruct->LastEvent = (UINT8*)(UINTN)EventLogAreaStruct->Lasa;
840 Status = TcgCommLogEvent (
841 &EventLogAreaStruct->LastEvent,
842 &EventLogAreaStruct->EventLogSize,
843 (UINTN)EventLogAreaStruct->Laml,
844 NewEventHdr,
845 NewEventHdrSize,
846 NewEventData,
847 NewEventSize
848 );
849 if (Status == EFI_OUT_OF_RESOURCES) {
850 EventLogAreaStruct->EventLogTruncated = TRUE;
851 return EFI_VOLUME_FULL;
852 } else if (Status == EFI_SUCCESS) {
853 EventLogAreaStruct->EventLogStarted = TRUE;
854 //
855 // Increase the NumberOfEvents in FinalEventsTable
856 //
857 (mTcgDxeData.FinalEventsTable[Index])->NumberOfEvents ++;
858 DEBUG ((EFI_D_INFO, "FinalEventsTable->NumberOfEvents - 0x%x\n", (mTcgDxeData.FinalEventsTable[Index])->NumberOfEvents));
859 DEBUG ((EFI_D_INFO, " Size - 0x%x\n", (UINTN)EventLogAreaStruct->LastEvent - (UINTN)mTcgDxeData.FinalEventsTable[Index]));
860 }
861 }
862
863 return Status;
864 }
865
866 /**
867 Get TPML_DIGEST_VALUES compact binary buffer size.
868
869 @param[in] DigestListBin TPML_DIGEST_VALUES compact binary buffer.
870
871 @return TPML_DIGEST_VALUES compact binary buffer size.
872 **/
873 UINT32
874 GetDigestListBinSize (
875 IN VOID *DigestListBin
876 )
877 {
878 UINTN Index;
879 UINT16 DigestSize;
880 UINT32 TotalSize;
881 UINT32 Count;
882 TPMI_ALG_HASH HashAlg;
883
884 Count = ReadUnaligned32 (DigestListBin);
885 TotalSize = sizeof(Count);
886 DigestListBin = (UINT8 *)DigestListBin + sizeof(Count);
887 for (Index = 0; Index < Count; Index++) {
888 HashAlg = ReadUnaligned16 (DigestListBin);
889 TotalSize += sizeof(HashAlg);
890 DigestListBin = (UINT8 *)DigestListBin + sizeof(HashAlg);
891
892 DigestSize = GetHashSizeFromAlgo (HashAlg);
893 TotalSize += DigestSize;
894 DigestListBin = (UINT8 *)DigestListBin + DigestSize;
895 }
896
897 return TotalSize;
898 }
899
900 /**
901 Add a new entry to the Event Log.
902
903 @param[in] DigestList A list of digest.
904 @param[in,out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
905 @param[in] NewEventData Pointer to the new event data.
906
907 @retval EFI_SUCCESS The new event log entry was added.
908 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
909 **/
910 EFI_STATUS
911 TcgDxeLogHashEvent (
912 IN TPML_DIGEST_VALUES *DigestList,
913 IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,
914 IN UINT8 *NewEventData
915 )
916 {
917 EFI_STATUS Status;
918 EFI_TPL OldTpl;
919 UINTN Index;
920 EFI_STATUS RetStatus;
921 TCG_PCR_EVENT2 TcgPcrEvent2;
922 UINT8 *DigestBuffer;
923
924 DEBUG ((EFI_D_INFO, "SupportedEventLogs - 0x%08x\n", mTcgDxeData.BsCap.SupportedEventLogs));
925
926 RetStatus = EFI_SUCCESS;
927 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
928 if ((mTcgDxeData.BsCap.SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {
929 DEBUG ((EFI_D_INFO, " LogFormat - 0x%08x\n", mTcg2EventInfo[Index].LogFormat));
930 switch (mTcg2EventInfo[Index].LogFormat) {
931 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2:
932 Status = GetDigestFromDigestList (TPM_ALG_SHA1, DigestList, &NewEventHdr->Digest);
933 if (!EFI_ERROR (Status)) {
934 //
935 // Enter critical region
936 //
937 OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
938 Status = TcgDxeLogEvent (
939 mTcg2EventInfo[Index].LogFormat,
940 NewEventHdr,
941 sizeof(TCG_PCR_EVENT_HDR),
942 NewEventData,
943 NewEventHdr->EventSize
944 );
945 if (Status != EFI_SUCCESS) {
946 RetStatus = Status;
947 }
948 gBS->RestoreTPL (OldTpl);
949 //
950 // Exit critical region
951 //
952 }
953 break;
954 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2:
955 ZeroMem (&TcgPcrEvent2, sizeof(TcgPcrEvent2));
956 TcgPcrEvent2.PCRIndex = NewEventHdr->PCRIndex;
957 TcgPcrEvent2.EventType = NewEventHdr->EventType;
958 DigestBuffer = (UINT8 *)&TcgPcrEvent2.Digest;
959 DigestBuffer = CopyDigestListToBuffer (DigestBuffer, DigestList, mTcgDxeData.BsCap.ActivePcrBanks);
960 CopyMem (DigestBuffer, &NewEventHdr->EventSize, sizeof(NewEventHdr->EventSize));
961 DigestBuffer = DigestBuffer + sizeof(NewEventHdr->EventSize);
962
963 //
964 // Enter critical region
965 //
966 OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
967 Status = TcgDxeLogEvent (
968 mTcg2EventInfo[Index].LogFormat,
969 &TcgPcrEvent2,
970 sizeof(TcgPcrEvent2.PCRIndex) + sizeof(TcgPcrEvent2.EventType) + GetDigestListSize (DigestList) + sizeof(TcgPcrEvent2.EventSize),
971 NewEventData,
972 NewEventHdr->EventSize
973 );
974 if (Status != EFI_SUCCESS) {
975 RetStatus = Status;
976 }
977 gBS->RestoreTPL (OldTpl);
978 //
979 // Exit critical region
980 //
981 break;
982 }
983 }
984 }
985
986 return RetStatus;
987 }
988
989 /**
990 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
991 and add an entry to the Event Log.
992
993 @param[in] Flags Bitmap providing additional information.
994 @param[in] HashData Physical address of the start of the data buffer
995 to be hashed, extended, and logged.
996 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
997 @param[in, out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
998 @param[in] NewEventData Pointer to the new event data.
999
1000 @retval EFI_SUCCESS Operation completed successfully.
1001 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
1002 @retval EFI_DEVICE_ERROR The command was unsuccessful.
1003
1004 **/
1005 EFI_STATUS
1006 TcgDxeHashLogExtendEvent (
1007 IN UINT64 Flags,
1008 IN UINT8 *HashData,
1009 IN UINT64 HashDataLen,
1010 IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,
1011 IN UINT8 *NewEventData
1012 )
1013 {
1014 EFI_STATUS Status;
1015 TPML_DIGEST_VALUES DigestList;
1016
1017 if (!mTcgDxeData.BsCap.TPMPresentFlag) {
1018 return EFI_DEVICE_ERROR;
1019 }
1020
1021 Status = HashAndExtend (
1022 NewEventHdr->PCRIndex,
1023 HashData,
1024 (UINTN)HashDataLen,
1025 &DigestList
1026 );
1027 if (!EFI_ERROR (Status)) {
1028 if ((Flags & EFI_TCG2_EXTEND_ONLY) == 0) {
1029 Status = TcgDxeLogHashEvent (&DigestList, NewEventHdr, NewEventData);
1030 }
1031 }
1032
1033 if (Status == EFI_DEVICE_ERROR) {
1034 DEBUG ((EFI_D_ERROR, "TcgDxeHashLogExtendEvent - %r. Disable TPM.\n", Status));
1035 mTcgDxeData.BsCap.TPMPresentFlag = FALSE;
1036 REPORT_STATUS_CODE (
1037 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1038 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)
1039 );
1040 }
1041
1042 return Status;
1043 }
1044
1045 /**
1046 The EFI_TCG2_PROTOCOL HashLogExtendEvent function call provides callers with
1047 an opportunity to extend and optionally log events without requiring
1048 knowledge of actual TPM commands.
1049 The extend operation will occur even if this function cannot create an event
1050 log entry (e.g. due to the event log being full).
1051
1052 @param[in] This Indicates the calling context
1053 @param[in] Flags Bitmap providing additional information.
1054 @param[in] DataToHash Physical address of the start of the data buffer to be hashed.
1055 @param[in] DataToHashLen The length in bytes of the buffer referenced by DataToHash.
1056 @param[in] Event Pointer to data buffer containing information about the event.
1057
1058 @retval EFI_SUCCESS Operation completed successfully.
1059 @retval EFI_DEVICE_ERROR The command was unsuccessful.
1060 @retval EFI_VOLUME_FULL The extend operation occurred, but the event could not be written to one or more event logs.
1061 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1062 @retval EFI_UNSUPPORTED The PE/COFF image type is not supported.
1063 **/
1064 EFI_STATUS
1065 EFIAPI
1066 Tcg2HashLogExtendEvent (
1067 IN EFI_TCG2_PROTOCOL *This,
1068 IN UINT64 Flags,
1069 IN EFI_PHYSICAL_ADDRESS DataToHash,
1070 IN UINT64 DataToHashLen,
1071 IN EFI_TCG2_EVENT *Event
1072 )
1073 {
1074 EFI_STATUS Status;
1075 TCG_PCR_EVENT_HDR NewEventHdr;
1076 TPML_DIGEST_VALUES DigestList;
1077
1078 DEBUG ((DEBUG_VERBOSE, "Tcg2HashLogExtendEvent ...\n"));
1079
1080 if ((This == NULL) || (DataToHash == 0) || (Event == NULL)) {
1081 return EFI_INVALID_PARAMETER;
1082 }
1083
1084 if (!mTcgDxeData.BsCap.TPMPresentFlag) {
1085 return EFI_DEVICE_ERROR;
1086 }
1087
1088 if (Event->Size < Event->Header.HeaderSize + sizeof(UINT32)) {
1089 return EFI_INVALID_PARAMETER;
1090 }
1091
1092 if (Event->Header.PCRIndex > MAX_PCR_INDEX) {
1093 return EFI_INVALID_PARAMETER;
1094 }
1095
1096 NewEventHdr.PCRIndex = Event->Header.PCRIndex;
1097 NewEventHdr.EventType = Event->Header.EventType;
1098 NewEventHdr.EventSize = Event->Size - sizeof(UINT32) - Event->Header.HeaderSize;
1099 if ((Flags & PE_COFF_IMAGE) != 0) {
1100 Status = MeasurePeImageAndExtend (
1101 NewEventHdr.PCRIndex,
1102 DataToHash,
1103 (UINTN)DataToHashLen,
1104 &DigestList
1105 );
1106 if (!EFI_ERROR (Status)) {
1107 if ((Flags & EFI_TCG2_EXTEND_ONLY) == 0) {
1108 Status = TcgDxeLogHashEvent (&DigestList, &NewEventHdr, Event->Event);
1109 }
1110 }
1111 if (Status == EFI_DEVICE_ERROR) {
1112 DEBUG ((EFI_D_ERROR, "MeasurePeImageAndExtend - %r. Disable TPM.\n", Status));
1113 mTcgDxeData.BsCap.TPMPresentFlag = FALSE;
1114 REPORT_STATUS_CODE (
1115 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1116 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)
1117 );
1118 }
1119 } else {
1120 Status = TcgDxeHashLogExtendEvent (
1121 Flags,
1122 (UINT8 *) (UINTN) DataToHash,
1123 DataToHashLen,
1124 &NewEventHdr,
1125 Event->Event
1126 );
1127 }
1128 DEBUG ((DEBUG_VERBOSE, "Tcg2HashLogExtendEvent - %r\n", Status));
1129 return Status;
1130 }
1131
1132 /**
1133 This service enables the sending of commands to the TPM.
1134
1135 @param[in] This Indicates the calling context
1136 @param[in] InputParameterBlockSize Size of the TPM input parameter block.
1137 @param[in] InputParameterBlock Pointer to the TPM input parameter block.
1138 @param[in] OutputParameterBlockSize Size of the TPM output parameter block.
1139 @param[in] OutputParameterBlock Pointer to the TPM output parameter block.
1140
1141 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
1142 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
1143 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1144 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
1145 **/
1146 EFI_STATUS
1147 EFIAPI
1148 Tcg2SubmitCommand (
1149 IN EFI_TCG2_PROTOCOL *This,
1150 IN UINT32 InputParameterBlockSize,
1151 IN UINT8 *InputParameterBlock,
1152 IN UINT32 OutputParameterBlockSize,
1153 IN UINT8 *OutputParameterBlock
1154 )
1155 {
1156 EFI_STATUS Status;
1157
1158 DEBUG ((EFI_D_INFO, "Tcg2SubmitCommand ...\n"));
1159
1160 if ((This == NULL) ||
1161 (InputParameterBlockSize == 0) || (InputParameterBlock == NULL) ||
1162 (OutputParameterBlockSize == 0) || (OutputParameterBlock == NULL)) {
1163 return EFI_INVALID_PARAMETER;
1164 }
1165
1166 if (!mTcgDxeData.BsCap.TPMPresentFlag) {
1167 return EFI_DEVICE_ERROR;
1168 }
1169
1170 if (InputParameterBlockSize > mTcgDxeData.BsCap.MaxCommandSize) {
1171 return EFI_INVALID_PARAMETER;
1172 }
1173 if (OutputParameterBlockSize > mTcgDxeData.BsCap.MaxResponseSize) {
1174 return EFI_INVALID_PARAMETER;
1175 }
1176
1177 Status = Tpm2SubmitCommand (
1178 InputParameterBlockSize,
1179 InputParameterBlock,
1180 &OutputParameterBlockSize,
1181 OutputParameterBlock
1182 );
1183 DEBUG ((EFI_D_INFO, "Tcg2SubmitCommand - %r\n", Status));
1184 return Status;
1185 }
1186
1187 /**
1188 This service returns the currently active PCR banks.
1189
1190 @param[in] This Indicates the calling context
1191 @param[out] ActivePcrBanks Pointer to the variable receiving the bitmap of currently active PCR banks.
1192
1193 @retval EFI_SUCCESS The bitmap of active PCR banks was stored in the ActivePcrBanks parameter.
1194 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1195 **/
1196 EFI_STATUS
1197 EFIAPI
1198 Tcg2GetActivePCRBanks (
1199 IN EFI_TCG2_PROTOCOL *This,
1200 OUT UINT32 *ActivePcrBanks
1201 )
1202 {
1203 if (ActivePcrBanks == NULL) {
1204 return EFI_INVALID_PARAMETER;
1205 }
1206 *ActivePcrBanks = mTcgDxeData.BsCap.ActivePcrBanks;
1207 return EFI_SUCCESS;
1208 }
1209
1210 /**
1211 This service sets the currently active PCR banks.
1212
1213 @param[in] This Indicates the calling context
1214 @param[in] ActivePcrBanks Bitmap of the requested active PCR banks. At least one bit SHALL be set.
1215
1216 @retval EFI_SUCCESS The bitmap in ActivePcrBank parameter is already active.
1217 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1218 **/
1219 EFI_STATUS
1220 EFIAPI
1221 Tcg2SetActivePCRBanks (
1222 IN EFI_TCG2_PROTOCOL *This,
1223 IN UINT32 ActivePcrBanks
1224 )
1225 {
1226 EFI_STATUS Status;
1227 UINT32 ReturnCode;
1228
1229 DEBUG ((EFI_D_INFO, "Tcg2SetActivePCRBanks ... (0x%x)\n", ActivePcrBanks));
1230
1231 if (ActivePcrBanks == 0) {
1232 return EFI_INVALID_PARAMETER;
1233 }
1234 if ((ActivePcrBanks & (~mTcgDxeData.BsCap.HashAlgorithmBitmap)) != 0) {
1235 return EFI_INVALID_PARAMETER;
1236 }
1237 if (ActivePcrBanks == mTcgDxeData.BsCap.ActivePcrBanks) {
1238 //
1239 // Need clear previous SET_PCR_BANKS setting
1240 //
1241 ReturnCode = Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction (TCG2_PHYSICAL_PRESENCE_NO_ACTION, 0);
1242 } else {
1243 ReturnCode = Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction (TCG2_PHYSICAL_PRESENCE_SET_PCR_BANKS, ActivePcrBanks);
1244 }
1245
1246 if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_SUCCESS) {
1247 Status = EFI_SUCCESS;
1248 } else if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE) {
1249 Status = EFI_OUT_OF_RESOURCES;
1250 } else if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_NOT_IMPLEMENTED) {
1251 Status = EFI_UNSUPPORTED;
1252 } else {
1253 Status = EFI_DEVICE_ERROR;
1254 }
1255
1256 DEBUG ((EFI_D_INFO, "Tcg2SetActivePCRBanks - %r\n", Status));
1257
1258 return Status;
1259 }
1260
1261 /**
1262 This service retrieves the result of a previous invocation of SetActivePcrBanks.
1263
1264 @param[in] This Indicates the calling context
1265 @param[out] OperationPresent Non-zero value to indicate a SetActivePcrBank operation was invoked during the last boot.
1266 @param[out] Response The response from the SetActivePcrBank request.
1267
1268 @retval EFI_SUCCESS The result value could be returned.
1269 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1270 **/
1271 EFI_STATUS
1272 EFIAPI
1273 Tcg2GetResultOfSetActivePcrBanks (
1274 IN EFI_TCG2_PROTOCOL *This,
1275 OUT UINT32 *OperationPresent,
1276 OUT UINT32 *Response
1277 )
1278 {
1279 UINT32 ReturnCode;
1280
1281 if ((OperationPresent == NULL) || (Response == NULL)) {
1282 return EFI_INVALID_PARAMETER;
1283 }
1284
1285 ReturnCode = Tcg2PhysicalPresenceLibReturnOperationResponseToOsFunction (OperationPresent, Response);
1286 if (ReturnCode == TCG_PP_RETURN_TPM_OPERATION_RESPONSE_SUCCESS) {
1287 return EFI_SUCCESS;
1288 } else {
1289 return EFI_UNSUPPORTED;
1290 }
1291 }
1292
1293 EFI_TCG2_PROTOCOL mTcg2Protocol = {
1294 Tcg2GetCapability,
1295 Tcg2GetEventLog,
1296 Tcg2HashLogExtendEvent,
1297 Tcg2SubmitCommand,
1298 Tcg2GetActivePCRBanks,
1299 Tcg2SetActivePCRBanks,
1300 Tcg2GetResultOfSetActivePcrBanks,
1301 };
1302
1303 /**
1304 Initialize the Event Log and log events passed from the PEI phase.
1305
1306 @retval EFI_SUCCESS Operation completed successfully.
1307 @retval EFI_OUT_OF_RESOURCES Out of memory.
1308
1309 **/
1310 EFI_STATUS
1311 SetupEventLog (
1312 VOID
1313 )
1314 {
1315 EFI_STATUS Status;
1316 VOID *TcgEvent;
1317 EFI_PEI_HOB_POINTERS GuidHob;
1318 EFI_PHYSICAL_ADDRESS Lasa;
1319 UINTN Index;
1320 UINT32 DigestListBinSize;
1321 UINT32 EventSize;
1322 TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct;
1323 UINT8 TempBuf[sizeof(TCG_EfiSpecIDEventStruct) + sizeof(UINT32) + (HASH_COUNT * sizeof(TCG_EfiSpecIdEventAlgorithmSize)) + sizeof(UINT8)];
1324 TCG_PCR_EVENT_HDR FirstPcrEvent;
1325 TCG_EfiSpecIdEventAlgorithmSize *DigestSize;
1326 TCG_EfiSpecIdEventAlgorithmSize *TempDigestSize;
1327 UINT8 *VendorInfoSize;
1328 UINT32 NumberOfAlgorithms;
1329
1330 DEBUG ((EFI_D_INFO, "SetupEventLog\n"));
1331
1332 //
1333 // 1. Create Log Area
1334 //
1335 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
1336 if ((mTcgDxeData.BsCap.SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {
1337 mTcgDxeData.EventLogAreaStruct[Index].EventLogFormat = mTcg2EventInfo[Index].LogFormat;
1338 Lasa = (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1);
1339 Status = gBS->AllocatePages (
1340 AllocateMaxAddress,
1341 EfiBootServicesData,
1342 EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcgLogAreaMinLen)),
1343 &Lasa
1344 );
1345 if (EFI_ERROR (Status)) {
1346 return Status;
1347 }
1348 mTcgDxeData.EventLogAreaStruct[Index].Lasa = Lasa;
1349 mTcgDxeData.EventLogAreaStruct[Index].Laml = PcdGet32 (PcdTcgLogAreaMinLen);
1350 //
1351 // To initialize them as 0xFF is recommended
1352 // because the OS can know the last entry for that.
1353 //
1354 SetMem ((VOID *)(UINTN)Lasa, PcdGet32 (PcdTcgLogAreaMinLen), 0xFF);
1355 //
1356 // Create first entry for Log Header Entry Data
1357 //
1358 if (mTcg2EventInfo[Index].LogFormat != EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2) {
1359 //
1360 // TcgEfiSpecIdEventStruct
1361 //
1362 TcgEfiSpecIdEventStruct = (TCG_EfiSpecIDEventStruct *)TempBuf;
1363 CopyMem (TcgEfiSpecIdEventStruct->signature, TCG_EfiSpecIDEventStruct_SIGNATURE_03, sizeof(TcgEfiSpecIdEventStruct->signature));
1364 TcgEfiSpecIdEventStruct->platformClass = PcdGet8 (PcdTpmPlatformClass);
1365 TcgEfiSpecIdEventStruct->specVersionMajor = TCG_EfiSpecIDEventStruct_SPEC_VERSION_MAJOR_TPM2;
1366 TcgEfiSpecIdEventStruct->specVersionMinor = TCG_EfiSpecIDEventStruct_SPEC_VERSION_MINOR_TPM2;
1367 TcgEfiSpecIdEventStruct->specErrata = TCG_EfiSpecIDEventStruct_SPEC_ERRATA_TPM2;
1368 TcgEfiSpecIdEventStruct->uintnSize = sizeof(UINTN)/sizeof(UINT32);
1369 NumberOfAlgorithms = 0;
1370 DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct + sizeof(*TcgEfiSpecIdEventStruct) + sizeof(NumberOfAlgorithms));
1371 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA1) != 0) {
1372 TempDigestSize = DigestSize;
1373 TempDigestSize += NumberOfAlgorithms;
1374 TempDigestSize->algorithmId = TPM_ALG_SHA1;
1375 TempDigestSize->digestSize = SHA1_DIGEST_SIZE;
1376 NumberOfAlgorithms++;
1377 }
1378 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA256) != 0) {
1379 TempDigestSize = DigestSize;
1380 TempDigestSize += NumberOfAlgorithms;
1381 TempDigestSize->algorithmId = TPM_ALG_SHA256;
1382 TempDigestSize->digestSize = SHA256_DIGEST_SIZE;
1383 NumberOfAlgorithms++;
1384 }
1385 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA384) != 0) {
1386 TempDigestSize = DigestSize;
1387 TempDigestSize += NumberOfAlgorithms;
1388 TempDigestSize->algorithmId = TPM_ALG_SHA384;
1389 TempDigestSize->digestSize = SHA384_DIGEST_SIZE;
1390 NumberOfAlgorithms++;
1391 }
1392 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA512) != 0) {
1393 TempDigestSize = DigestSize;
1394 TempDigestSize += NumberOfAlgorithms;
1395 TempDigestSize->algorithmId = TPM_ALG_SHA512;
1396 TempDigestSize->digestSize = SHA512_DIGEST_SIZE;
1397 NumberOfAlgorithms++;
1398 }
1399 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SM3_256) != 0) {
1400 TempDigestSize = DigestSize;
1401 TempDigestSize += NumberOfAlgorithms;
1402 TempDigestSize->algorithmId = TPM_ALG_SM3_256;
1403 TempDigestSize->digestSize = SM3_256_DIGEST_SIZE;
1404 NumberOfAlgorithms++;
1405 }
1406 CopyMem (TcgEfiSpecIdEventStruct + 1, &NumberOfAlgorithms, sizeof(NumberOfAlgorithms));
1407 TempDigestSize = DigestSize;
1408 TempDigestSize += NumberOfAlgorithms;
1409 VendorInfoSize = (UINT8 *)TempDigestSize;
1410 *VendorInfoSize = 0;
1411
1412 //
1413 // FirstPcrEvent
1414 //
1415 FirstPcrEvent.PCRIndex = 0;
1416 FirstPcrEvent.EventType = EV_NO_ACTION;
1417 ZeroMem (&FirstPcrEvent.Digest, sizeof(FirstPcrEvent.Digest));
1418 FirstPcrEvent.EventSize = (UINT32)GetTcgEfiSpecIdEventStructSize (TcgEfiSpecIdEventStruct);
1419
1420 //
1421 // Record
1422 //
1423 Status = TcgDxeLogEvent (
1424 mTcg2EventInfo[Index].LogFormat,
1425 &FirstPcrEvent,
1426 sizeof(FirstPcrEvent),
1427 (UINT8 *)TcgEfiSpecIdEventStruct,
1428 FirstPcrEvent.EventSize
1429 );
1430 }
1431 }
1432 }
1433
1434 //
1435 // 2. Create Final Log Area
1436 //
1437 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
1438 if ((mTcgDxeData.BsCap.SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {
1439 if (mTcg2EventInfo[Index].LogFormat == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
1440 Lasa = (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1);
1441 Status = gBS->AllocatePages (
1442 AllocateMaxAddress,
1443 EfiACPIMemoryNVS,
1444 EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcg2FinalLogAreaLen)),
1445 &Lasa
1446 );
1447 if (EFI_ERROR (Status)) {
1448 return Status;
1449 }
1450 SetMem ((VOID *)(UINTN)Lasa, PcdGet32 (PcdTcg2FinalLogAreaLen), 0xFF);
1451
1452 //
1453 // Initialize
1454 //
1455 mTcgDxeData.FinalEventsTable[Index] = (VOID *)(UINTN)Lasa;
1456 (mTcgDxeData.FinalEventsTable[Index])->Version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
1457 (mTcgDxeData.FinalEventsTable[Index])->NumberOfEvents = 0;
1458
1459 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogFormat = mTcg2EventInfo[Index].LogFormat;
1460 mTcgDxeData.FinalEventLogAreaStruct[Index].Lasa = Lasa + sizeof(EFI_TCG2_FINAL_EVENTS_TABLE);
1461 mTcgDxeData.FinalEventLogAreaStruct[Index].Laml = PcdGet32 (PcdTcg2FinalLogAreaLen) - sizeof(EFI_TCG2_FINAL_EVENTS_TABLE);
1462 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogSize = 0;
1463 mTcgDxeData.FinalEventLogAreaStruct[Index].LastEvent = (VOID *)(UINTN)mTcgDxeData.FinalEventLogAreaStruct[Index].Lasa;
1464 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogStarted = FALSE;
1465 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogTruncated = FALSE;
1466
1467 //
1468 // Install to configuration table for EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
1469 //
1470 Status = gBS->InstallConfigurationTable (&gEfiTcg2FinalEventsTableGuid, (VOID *)mTcgDxeData.FinalEventsTable[Index]);
1471 if (EFI_ERROR (Status)) {
1472 return Status;
1473 }
1474 } else {
1475 //
1476 // No need to handle EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2
1477 //
1478 mTcgDxeData.FinalEventsTable[Index] = NULL;
1479 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogFormat = mTcg2EventInfo[Index].LogFormat;
1480 mTcgDxeData.FinalEventLogAreaStruct[Index].Lasa = 0;
1481 mTcgDxeData.FinalEventLogAreaStruct[Index].Laml = 0;
1482 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogSize = 0;
1483 mTcgDxeData.FinalEventLogAreaStruct[Index].LastEvent = 0;
1484 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogStarted = FALSE;
1485 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogTruncated = FALSE;
1486 }
1487 }
1488 }
1489
1490 //
1491 // 3. Sync data from PEI to DXE
1492 //
1493 Status = EFI_SUCCESS;
1494 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
1495 if ((mTcgDxeData.BsCap.SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {
1496 GuidHob.Raw = GetHobList ();
1497 Status = EFI_SUCCESS;
1498 while (!EFI_ERROR (Status) &&
1499 (GuidHob.Raw = GetNextGuidHob (mTcg2EventInfo[Index].EventGuid, GuidHob.Raw)) != NULL) {
1500 TcgEvent = GET_GUID_HOB_DATA (GuidHob.Guid);
1501 GuidHob.Raw = GET_NEXT_HOB (GuidHob);
1502 switch (mTcg2EventInfo[Index].LogFormat) {
1503 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2:
1504 Status = TcgDxeLogEvent (
1505 mTcg2EventInfo[Index].LogFormat,
1506 TcgEvent,
1507 sizeof(TCG_PCR_EVENT_HDR),
1508 ((TCG_PCR_EVENT*)TcgEvent)->Event,
1509 ((TCG_PCR_EVENT_HDR*)TcgEvent)->EventSize
1510 );
1511 break;
1512 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2:
1513 DigestListBinSize = GetDigestListBinSize ((UINT8 *)TcgEvent + sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE));
1514 CopyMem (&EventSize, (UINT8 *)TcgEvent + sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE) + DigestListBinSize, sizeof(UINT32));
1515 Status = TcgDxeLogEvent (
1516 mTcg2EventInfo[Index].LogFormat,
1517 TcgEvent,
1518 sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE) + DigestListBinSize + sizeof(UINT32),
1519 (UINT8 *)TcgEvent + sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE) + DigestListBinSize + sizeof(UINT32),
1520 EventSize
1521 );
1522 break;
1523 }
1524 }
1525 }
1526 }
1527
1528 return Status;
1529 }
1530
1531 /**
1532 Measure and log an action string, and extend the measurement result into PCR[5].
1533
1534 @param[in] String A specific string that indicates an Action event.
1535
1536 @retval EFI_SUCCESS Operation completed successfully.
1537 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1538
1539 **/
1540 EFI_STATUS
1541 TcgMeasureAction (
1542 IN CHAR8 *String
1543 )
1544 {
1545 TCG_PCR_EVENT_HDR TcgEvent;
1546
1547 TcgEvent.PCRIndex = 5;
1548 TcgEvent.EventType = EV_EFI_ACTION;
1549 TcgEvent.EventSize = (UINT32)AsciiStrLen (String);
1550 return TcgDxeHashLogExtendEvent (
1551 0,
1552 (UINT8*)String,
1553 TcgEvent.EventSize,
1554 &TcgEvent,
1555 (UINT8 *) String
1556 );
1557 }
1558
1559 /**
1560 Measure and log EFI handoff tables, and extend the measurement result into PCR[1].
1561
1562 @retval EFI_SUCCESS Operation completed successfully.
1563 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1564
1565 **/
1566 EFI_STATUS
1567 MeasureHandoffTables (
1568 VOID
1569 )
1570 {
1571 EFI_STATUS Status;
1572 TCG_PCR_EVENT_HDR TcgEvent;
1573 EFI_HANDOFF_TABLE_POINTERS HandoffTables;
1574 UINTN ProcessorNum;
1575 EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
1576
1577 ProcessorLocBuf = NULL;
1578 Status = EFI_SUCCESS;
1579
1580 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_SERVER) {
1581 //
1582 // Tcg Server spec.
1583 // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]
1584 //
1585 Status = GetProcessorsCpuLocation(&ProcessorLocBuf, &ProcessorNum);
1586
1587 if (!EFI_ERROR(Status)){
1588 TcgEvent.PCRIndex = 1;
1589 TcgEvent.EventType = EV_TABLE_OF_DEVICES;
1590 TcgEvent.EventSize = sizeof (HandoffTables);
1591
1592 HandoffTables.NumberOfTables = 1;
1593 HandoffTables.TableEntry[0].VendorGuid = gEfiMpServiceProtocolGuid;
1594 HandoffTables.TableEntry[0].VendorTable = ProcessorLocBuf;
1595
1596 Status = TcgDxeHashLogExtendEvent (
1597 0,
1598 (UINT8*)(UINTN)ProcessorLocBuf,
1599 sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
1600 &TcgEvent,
1601 (UINT8*)&HandoffTables
1602 );
1603
1604 FreePool(ProcessorLocBuf);
1605 }
1606 }
1607
1608 return Status;
1609 }
1610
1611 /**
1612 Measure and log Separator event, and extend the measurement result into a specific PCR.
1613
1614 @param[in] PCRIndex PCR index.
1615
1616 @retval EFI_SUCCESS Operation completed successfully.
1617 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1618
1619 **/
1620 EFI_STATUS
1621 MeasureSeparatorEvent (
1622 IN TPM_PCRINDEX PCRIndex
1623 )
1624 {
1625 TCG_PCR_EVENT_HDR TcgEvent;
1626 UINT32 EventData;
1627
1628 DEBUG ((EFI_D_INFO, "MeasureSeparatorEvent Pcr - %x\n", PCRIndex));
1629
1630 EventData = 0;
1631 TcgEvent.PCRIndex = PCRIndex;
1632 TcgEvent.EventType = EV_SEPARATOR;
1633 TcgEvent.EventSize = (UINT32)sizeof (EventData);
1634 return TcgDxeHashLogExtendEvent (
1635 0,
1636 (UINT8 *)&EventData,
1637 sizeof (EventData),
1638 &TcgEvent,
1639 (UINT8 *)&EventData
1640 );
1641 }
1642
1643 /**
1644 Measure and log an EFI variable, and extend the measurement result into a specific PCR.
1645
1646 @param[in] PCRIndex PCR Index.
1647 @param[in] EventType Event type.
1648 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1649 @param[in] VendorGuid A unique identifier for the vendor.
1650 @param[in] VarData The content of the variable data.
1651 @param[in] VarSize The size of the variable data.
1652
1653 @retval EFI_SUCCESS Operation completed successfully.
1654 @retval EFI_OUT_OF_RESOURCES Out of memory.
1655 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1656
1657 **/
1658 EFI_STATUS
1659 MeasureVariable (
1660 IN TPM_PCRINDEX PCRIndex,
1661 IN TCG_EVENTTYPE EventType,
1662 IN CHAR16 *VarName,
1663 IN EFI_GUID *VendorGuid,
1664 IN VOID *VarData,
1665 IN UINTN VarSize
1666 )
1667 {
1668 EFI_STATUS Status;
1669 TCG_PCR_EVENT_HDR TcgEvent;
1670 UINTN VarNameLength;
1671 EFI_VARIABLE_DATA_TREE *VarLog;
1672
1673 DEBUG ((EFI_D_INFO, "Tcg2Dxe: MeasureVariable (Pcr - %x, EventType - %x, ", (UINTN)PCRIndex, (UINTN)EventType));
1674 DEBUG ((EFI_D_INFO, "VariableName - %s, VendorGuid - %g)\n", VarName, VendorGuid));
1675
1676 VarNameLength = StrLen (VarName);
1677 TcgEvent.PCRIndex = PCRIndex;
1678 TcgEvent.EventType = EventType;
1679
1680 TcgEvent.EventSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize
1681 - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));
1682
1683 VarLog = (EFI_VARIABLE_DATA_TREE *)AllocatePool (TcgEvent.EventSize);
1684 if (VarLog == NULL) {
1685 return EFI_OUT_OF_RESOURCES;
1686 }
1687
1688 VarLog->VariableName = *VendorGuid;
1689 VarLog->UnicodeNameLength = VarNameLength;
1690 VarLog->VariableDataLength = VarSize;
1691 CopyMem (
1692 VarLog->UnicodeName,
1693 VarName,
1694 VarNameLength * sizeof (*VarName)
1695 );
1696 if (VarSize != 0 && VarData != NULL) {
1697 CopyMem (
1698 (CHAR16 *)VarLog->UnicodeName + VarNameLength,
1699 VarData,
1700 VarSize
1701 );
1702 }
1703
1704 if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {
1705 //
1706 // Digest is the event data (EFI_VARIABLE_DATA)
1707 //
1708 Status = TcgDxeHashLogExtendEvent (
1709 0,
1710 (UINT8*)VarLog,
1711 TcgEvent.EventSize,
1712 &TcgEvent,
1713 (UINT8*)VarLog
1714 );
1715 } else {
1716 Status = TcgDxeHashLogExtendEvent (
1717 0,
1718 (UINT8*)VarData,
1719 VarSize,
1720 &TcgEvent,
1721 (UINT8*)VarLog
1722 );
1723 }
1724 FreePool (VarLog);
1725 return Status;
1726 }
1727
1728 /**
1729 Read then Measure and log an EFI variable, and extend the measurement result into a specific PCR.
1730
1731 @param[in] PCRIndex PCR Index.
1732 @param[in] EventType Event type.
1733 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1734 @param[in] VendorGuid A unique identifier for the vendor.
1735 @param[out] VarSize The size of the variable data.
1736 @param[out] VarData Pointer to the content of the variable.
1737
1738 @retval EFI_SUCCESS Operation completed successfully.
1739 @retval EFI_OUT_OF_RESOURCES Out of memory.
1740 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1741
1742 **/
1743 EFI_STATUS
1744 ReadAndMeasureVariable (
1745 IN TPM_PCRINDEX PCRIndex,
1746 IN TCG_EVENTTYPE EventType,
1747 IN CHAR16 *VarName,
1748 IN EFI_GUID *VendorGuid,
1749 OUT UINTN *VarSize,
1750 OUT VOID **VarData
1751 )
1752 {
1753 EFI_STATUS Status;
1754
1755 Status = GetVariable2 (VarName, VendorGuid, VarData, VarSize);
1756 if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {
1757 if (EFI_ERROR (Status)) {
1758 //
1759 // It is valid case, so we need handle it.
1760 //
1761 *VarData = NULL;
1762 *VarSize = 0;
1763 }
1764 } else {
1765 //
1766 // if status error, VarData is freed and set NULL by GetVariable2
1767 //
1768 if (EFI_ERROR (Status)) {
1769 return EFI_NOT_FOUND;
1770 }
1771 }
1772
1773 Status = MeasureVariable (
1774 PCRIndex,
1775 EventType,
1776 VarName,
1777 VendorGuid,
1778 *VarData,
1779 *VarSize
1780 );
1781 return Status;
1782 }
1783
1784 /**
1785 Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].
1786
1787 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1788 @param[in] VendorGuid A unique identifier for the vendor.
1789 @param[out] VarSize The size of the variable data.
1790 @param[out] VarData Pointer to the content of the variable.
1791
1792 @retval EFI_SUCCESS Operation completed successfully.
1793 @retval EFI_OUT_OF_RESOURCES Out of memory.
1794 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1795
1796 **/
1797 EFI_STATUS
1798 ReadAndMeasureBootVariable (
1799 IN CHAR16 *VarName,
1800 IN EFI_GUID *VendorGuid,
1801 OUT UINTN *VarSize,
1802 OUT VOID **VarData
1803 )
1804 {
1805 return ReadAndMeasureVariable (
1806 5,
1807 EV_EFI_VARIABLE_BOOT,
1808 VarName,
1809 VendorGuid,
1810 VarSize,
1811 VarData
1812 );
1813 }
1814
1815 /**
1816 Read then Measure and log an EFI Secure variable, and extend the measurement result into PCR[7].
1817
1818 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1819 @param[in] VendorGuid A unique identifier for the vendor.
1820 @param[out] VarSize The size of the variable data.
1821 @param[out] VarData Pointer to the content of the variable.
1822
1823 @retval EFI_SUCCESS Operation completed successfully.
1824 @retval EFI_OUT_OF_RESOURCES Out of memory.
1825 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1826
1827 **/
1828 EFI_STATUS
1829 ReadAndMeasureSecureVariable (
1830 IN CHAR16 *VarName,
1831 IN EFI_GUID *VendorGuid,
1832 OUT UINTN *VarSize,
1833 OUT VOID **VarData
1834 )
1835 {
1836 return ReadAndMeasureVariable (
1837 7,
1838 EV_EFI_VARIABLE_DRIVER_CONFIG,
1839 VarName,
1840 VendorGuid,
1841 VarSize,
1842 VarData
1843 );
1844 }
1845
1846 /**
1847 Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.
1848
1849 The EFI boot variables are BootOrder and Boot#### variables.
1850
1851 @retval EFI_SUCCESS Operation completed successfully.
1852 @retval EFI_OUT_OF_RESOURCES Out of memory.
1853 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1854
1855 **/
1856 EFI_STATUS
1857 MeasureAllBootVariables (
1858 VOID
1859 )
1860 {
1861 EFI_STATUS Status;
1862 UINT16 *BootOrder;
1863 UINTN BootCount;
1864 UINTN Index;
1865 VOID *BootVarData;
1866 UINTN Size;
1867
1868 Status = ReadAndMeasureBootVariable (
1869 mBootVarName,
1870 &gEfiGlobalVariableGuid,
1871 &BootCount,
1872 (VOID **) &BootOrder
1873 );
1874 if (Status == EFI_NOT_FOUND || BootOrder == NULL) {
1875 return EFI_SUCCESS;
1876 }
1877
1878 if (EFI_ERROR (Status)) {
1879 //
1880 // BootOrder can't be NULL if status is not EFI_NOT_FOUND
1881 //
1882 FreePool (BootOrder);
1883 return Status;
1884 }
1885
1886 BootCount /= sizeof (*BootOrder);
1887 for (Index = 0; Index < BootCount; Index++) {
1888 UnicodeSPrint (mBootVarName, sizeof (mBootVarName), L"Boot%04x", BootOrder[Index]);
1889 Status = ReadAndMeasureBootVariable (
1890 mBootVarName,
1891 &gEfiGlobalVariableGuid,
1892 &Size,
1893 &BootVarData
1894 );
1895 if (!EFI_ERROR (Status)) {
1896 FreePool (BootVarData);
1897 }
1898 }
1899
1900 FreePool (BootOrder);
1901 return EFI_SUCCESS;
1902 }
1903
1904 /**
1905 Measure and log all EFI Secure variables, and extend the measurement result into a specific PCR.
1906
1907 The EFI boot variables are BootOrder and Boot#### variables.
1908
1909 @retval EFI_SUCCESS Operation completed successfully.
1910 @retval EFI_OUT_OF_RESOURCES Out of memory.
1911 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1912
1913 **/
1914 EFI_STATUS
1915 MeasureAllSecureVariables (
1916 VOID
1917 )
1918 {
1919 EFI_STATUS Status;
1920 VOID *Data;
1921 UINTN DataSize;
1922 UINTN Index;
1923
1924 Status = EFI_NOT_FOUND;
1925 for (Index = 0; Index < sizeof(mVariableType)/sizeof(mVariableType[0]); Index++) {
1926 Status = ReadAndMeasureSecureVariable (
1927 mVariableType[Index].VariableName,
1928 mVariableType[Index].VendorGuid,
1929 &DataSize,
1930 &Data
1931 );
1932 if (!EFI_ERROR (Status)) {
1933 if (Data != NULL) {
1934 FreePool (Data);
1935 }
1936 }
1937 }
1938
1939 return EFI_SUCCESS;
1940 }
1941
1942 /**
1943 Measure and log launch of FirmwareDebugger, and extend the measurement result into a specific PCR.
1944
1945 @retval EFI_SUCCESS Operation completed successfully.
1946 @retval EFI_OUT_OF_RESOURCES Out of memory.
1947 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1948
1949 **/
1950 EFI_STATUS
1951 MeasureLaunchOfFirmwareDebugger (
1952 VOID
1953 )
1954 {
1955 TCG_PCR_EVENT_HDR TcgEvent;
1956
1957 TcgEvent.PCRIndex = 7;
1958 TcgEvent.EventType = EV_EFI_ACTION;
1959 TcgEvent.EventSize = sizeof(FIRMWARE_DEBUGGER_EVENT_STRING) - 1;
1960 return TcgDxeHashLogExtendEvent (
1961 0,
1962 (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING,
1963 sizeof(FIRMWARE_DEBUGGER_EVENT_STRING) - 1,
1964 &TcgEvent,
1965 (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING
1966 );
1967 }
1968
1969 /**
1970 Measure and log all Secure Boot Policy, and extend the measurement result into a specific PCR.
1971
1972 Platform firmware adhering to the policy must therefore measure the following values into PCR[7]: (in order listed)
1973 - The contents of the SecureBoot variable
1974 - The contents of the PK variable
1975 - The contents of the KEK variable
1976 - The contents of the EFI_IMAGE_SECURITY_DATABASE variable
1977 - The contents of the EFI_IMAGE_SECURITY_DATABASE1 variable
1978 - Separator
1979 - Entries in the EFI_IMAGE_SECURITY_DATABASE that are used to validate EFI Drivers or EFI Boot Applications in the boot path
1980
1981 NOTE: Because of the above, UEFI variables PK, KEK, EFI_IMAGE_SECURITY_DATABASE,
1982 EFI_IMAGE_SECURITY_DATABASE1 and SecureBoot SHALL NOT be measured into PCR[3].
1983
1984 @param[in] Event Event whose notification function is being invoked
1985 @param[in] Context Pointer to the notification function's context
1986 **/
1987 VOID
1988 EFIAPI
1989 MeasureSecureBootPolicy (
1990 IN EFI_EVENT Event,
1991 IN VOID *Context
1992 )
1993 {
1994 EFI_STATUS Status;
1995 VOID *Protocol;
1996
1997 Status = gBS->LocateProtocol (&gEfiVariableWriteArchProtocolGuid, NULL, (VOID **)&Protocol);
1998 if (EFI_ERROR (Status)) {
1999 return;
2000 }
2001
2002 if (PcdGetBool (PcdFirmwareDebuggerInitialized)) {
2003 Status = MeasureLaunchOfFirmwareDebugger ();
2004 DEBUG ((EFI_D_INFO, "MeasureLaunchOfFirmwareDebugger - %r\n", Status));
2005 }
2006
2007 Status = MeasureAllSecureVariables ();
2008 DEBUG ((EFI_D_INFO, "MeasureAllSecureVariables - %r\n", Status));
2009
2010 //
2011 // We need measure Separator(7) here, because this event must be between SecureBootPolicy (Configure)
2012 // and ImageVerification (Authority)
2013 // There might be a case that we need measure UEFI image from DriverOrder, besides BootOrder. So
2014 // the Authority measurement happen before ReadToBoot event.
2015 //
2016 Status = MeasureSeparatorEvent (7);
2017 DEBUG ((EFI_D_INFO, "MeasureSeparatorEvent - %r\n", Status));
2018 return ;
2019 }
2020
2021 /**
2022 Ready to Boot Event notification handler.
2023
2024 Sequence of OS boot events is measured in this event notification handler.
2025
2026 @param[in] Event Event whose notification function is being invoked
2027 @param[in] Context Pointer to the notification function's context
2028
2029 **/
2030 VOID
2031 EFIAPI
2032 OnReadyToBoot (
2033 IN EFI_EVENT Event,
2034 IN VOID *Context
2035 )
2036 {
2037 EFI_STATUS Status;
2038 TPM_PCRINDEX PcrIndex;
2039
2040 PERF_START_EX (mImageHandle, "EventRec", "Tcg2Dxe", 0, PERF_ID_TCG2_DXE);
2041 if (mBootAttempts == 0) {
2042
2043 //
2044 // Measure handoff tables.
2045 //
2046 Status = MeasureHandoffTables ();
2047 if (EFI_ERROR (Status)) {
2048 DEBUG ((EFI_D_ERROR, "HOBs not Measured. Error!\n"));
2049 }
2050
2051 //
2052 // Measure BootOrder & Boot#### variables.
2053 //
2054 Status = MeasureAllBootVariables ();
2055 if (EFI_ERROR (Status)) {
2056 DEBUG ((EFI_D_ERROR, "Boot Variables not Measured. Error!\n"));
2057 }
2058
2059 //
2060 // 1. This is the first boot attempt.
2061 //
2062 Status = TcgMeasureAction (
2063 EFI_CALLING_EFI_APPLICATION
2064 );
2065 if (EFI_ERROR (Status)) {
2066 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION));
2067 }
2068
2069 //
2070 // 2. Draw a line between pre-boot env and entering post-boot env.
2071 // PCR[7] is already done.
2072 //
2073 for (PcrIndex = 0; PcrIndex < 7; PcrIndex++) {
2074 Status = MeasureSeparatorEvent (PcrIndex);
2075 if (EFI_ERROR (Status)) {
2076 DEBUG ((DEBUG_ERROR, "Separator Event not Measured. Error!\n"));
2077 }
2078 }
2079
2080 //
2081 // 3. Measure GPT. It would be done in SAP driver.
2082 //
2083
2084 //
2085 // 4. Measure PE/COFF OS loader. It would be done in SAP driver.
2086 //
2087
2088 //
2089 // 5. Read & Measure variable. BootOrder already measured.
2090 //
2091 } else {
2092 //
2093 // 6. Not first attempt, meaning a return from last attempt
2094 //
2095 Status = TcgMeasureAction (
2096 EFI_RETURNING_FROM_EFI_APPLICATOIN
2097 );
2098 if (EFI_ERROR (Status)) {
2099 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_RETURNING_FROM_EFI_APPLICATOIN));
2100 }
2101 }
2102
2103 DEBUG ((EFI_D_INFO, "TPM2 Tcg2Dxe Measure Data when ReadyToBoot\n"));
2104 //
2105 // Increase boot attempt counter.
2106 //
2107 mBootAttempts++;
2108 PERF_END_EX (mImageHandle, "EventRec", "Tcg2Dxe", 0, PERF_ID_TCG2_DXE + 1);
2109 }
2110
2111 /**
2112 Exit Boot Services Event notification handler.
2113
2114 Measure invocation and success of ExitBootServices.
2115
2116 @param[in] Event Event whose notification function is being invoked
2117 @param[in] Context Pointer to the notification function's context
2118
2119 **/
2120 VOID
2121 EFIAPI
2122 OnExitBootServices (
2123 IN EFI_EVENT Event,
2124 IN VOID *Context
2125 )
2126 {
2127 EFI_STATUS Status;
2128
2129 //
2130 // Measure invocation of ExitBootServices,
2131 //
2132 Status = TcgMeasureAction (
2133 EFI_EXIT_BOOT_SERVICES_INVOCATION
2134 );
2135 if (EFI_ERROR (Status)) {
2136 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION));
2137 }
2138
2139 //
2140 // Measure success of ExitBootServices
2141 //
2142 Status = TcgMeasureAction (
2143 EFI_EXIT_BOOT_SERVICES_SUCCEEDED
2144 );
2145 if (EFI_ERROR (Status)) {
2146 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED));
2147 }
2148 }
2149
2150 /**
2151 Exit Boot Services Failed Event notification handler.
2152
2153 Measure Failure of ExitBootServices.
2154
2155 @param[in] Event Event whose notification function is being invoked
2156 @param[in] Context Pointer to the notification function's context
2157
2158 **/
2159 VOID
2160 EFIAPI
2161 OnExitBootServicesFailed (
2162 IN EFI_EVENT Event,
2163 IN VOID *Context
2164 )
2165 {
2166 EFI_STATUS Status;
2167
2168 //
2169 // Measure Failure of ExitBootServices,
2170 //
2171 Status = TcgMeasureAction (
2172 EFI_EXIT_BOOT_SERVICES_FAILED
2173 );
2174 if (EFI_ERROR (Status)) {
2175 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED));
2176 }
2177
2178 }
2179
2180 /**
2181 The function install Tcg2 protocol.
2182
2183 @retval EFI_SUCCESS Tcg2 protocol is installed.
2184 @retval other Some error occurs.
2185 **/
2186 EFI_STATUS
2187 InstallTcg2 (
2188 VOID
2189 )
2190 {
2191 EFI_STATUS Status;
2192 EFI_HANDLE Handle;
2193
2194 Handle = NULL;
2195 Status = gBS->InstallMultipleProtocolInterfaces (
2196 &Handle,
2197 &gEfiTcg2ProtocolGuid,
2198 &mTcg2Protocol,
2199 NULL
2200 );
2201 return Status;
2202 }
2203
2204 /**
2205 The driver's entry point. It publishes EFI Tcg2 Protocol.
2206
2207 @param[in] ImageHandle The firmware allocated handle for the EFI image.
2208 @param[in] SystemTable A pointer to the EFI System Table.
2209
2210 @retval EFI_SUCCESS The entry point is executed successfully.
2211 @retval other Some error occurs when executing this entry point.
2212 **/
2213 EFI_STATUS
2214 EFIAPI
2215 DriverEntry (
2216 IN EFI_HANDLE ImageHandle,
2217 IN EFI_SYSTEM_TABLE *SystemTable
2218 )
2219 {
2220 EFI_STATUS Status;
2221 EFI_EVENT Event;
2222 VOID *Registration;
2223 UINT32 MaxCommandSize;
2224 UINT32 MaxResponseSize;
2225 UINTN Index;
2226 EFI_TCG2_EVENT_ALGORITHM_BITMAP TpmHashAlgorithmBitmap;
2227 UINT32 ActivePCRBanks;
2228 UINT32 NumberOfPCRBanks;
2229
2230 mImageHandle = ImageHandle;
2231
2232 if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceNoneGuid) ||
2233 CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){
2234 DEBUG ((EFI_D_ERROR, "No TPM2 instance required!\n"));
2235 return EFI_UNSUPPORTED;
2236 }
2237
2238 if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {
2239 DEBUG ((EFI_D_ERROR, "TPM2 error!\n"));
2240 return EFI_DEVICE_ERROR;
2241 }
2242
2243 Status = Tpm2RequestUseTpm ();
2244 if (EFI_ERROR (Status)) {
2245 DEBUG ((EFI_D_ERROR, "TPM2 not detected!\n"));
2246 return Status;
2247 }
2248
2249 //
2250 // Fill information
2251 //
2252 ASSERT (TCG_EVENT_LOG_AREA_COUNT_MAX == sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]));
2253
2254 mTcgDxeData.BsCap.Size = sizeof(EFI_TCG2_BOOT_SERVICE_CAPABILITY);
2255 mTcgDxeData.BsCap.ProtocolVersion.Major = 1;
2256 mTcgDxeData.BsCap.ProtocolVersion.Minor = 1;
2257 mTcgDxeData.BsCap.StructureVersion.Major = 1;
2258 mTcgDxeData.BsCap.StructureVersion.Minor = 1;
2259
2260 DEBUG ((EFI_D_INFO, "Tcg2.ProtocolVersion - %02x.%02x\n", mTcgDxeData.BsCap.ProtocolVersion.Major, mTcgDxeData.BsCap.ProtocolVersion.Minor));
2261 DEBUG ((EFI_D_INFO, "Tcg2.StructureVersion - %02x.%02x\n", mTcgDxeData.BsCap.StructureVersion.Major, mTcgDxeData.BsCap.StructureVersion.Minor));
2262
2263 Status = Tpm2GetCapabilityManufactureID (&mTcgDxeData.BsCap.ManufacturerID);
2264 if (EFI_ERROR (Status)) {
2265 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityManufactureID fail!\n"));
2266 } else {
2267 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityManufactureID - %08x\n", mTcgDxeData.BsCap.ManufacturerID));
2268 }
2269
2270 DEBUG_CODE (
2271 UINT32 FirmwareVersion1;
2272 UINT32 FirmwareVersion2;
2273
2274 Status = Tpm2GetCapabilityFirmwareVersion (&FirmwareVersion1, &FirmwareVersion2);
2275 if (EFI_ERROR (Status)) {
2276 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityFirmwareVersion fail!\n"));
2277 } else {
2278 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityFirmwareVersion - %08x %08x\n", FirmwareVersion1, FirmwareVersion2));
2279 }
2280 );
2281
2282 Status = Tpm2GetCapabilityMaxCommandResponseSize (&MaxCommandSize, &MaxResponseSize);
2283 if (EFI_ERROR (Status)) {
2284 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityMaxCommandResponseSize fail!\n"));
2285 } else {
2286 mTcgDxeData.BsCap.MaxCommandSize = (UINT16)MaxCommandSize;
2287 mTcgDxeData.BsCap.MaxResponseSize = (UINT16)MaxResponseSize;
2288 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityMaxCommandResponseSize - %08x, %08x\n", MaxCommandSize, MaxResponseSize));
2289 }
2290
2291 //
2292 // Get supported PCR and current Active PCRs
2293 //
2294 Status = Tpm2GetCapabilitySupportedAndActivePcrs (&TpmHashAlgorithmBitmap, &ActivePCRBanks);
2295 ASSERT_EFI_ERROR (Status);
2296
2297 mTcgDxeData.BsCap.HashAlgorithmBitmap = TpmHashAlgorithmBitmap & PcdGet32 (PcdTcg2HashAlgorithmBitmap);
2298 mTcgDxeData.BsCap.ActivePcrBanks = ActivePCRBanks & PcdGet32 (PcdTcg2HashAlgorithmBitmap);
2299
2300 //
2301 // Need calculate NumberOfPCRBanks here, because HashAlgorithmBitmap might be removed by PCD.
2302 //
2303 NumberOfPCRBanks = 0;
2304 for (Index = 0; Index < 32; Index++) {
2305 if ((mTcgDxeData.BsCap.HashAlgorithmBitmap & (1u << Index)) != 0) {
2306 NumberOfPCRBanks++;
2307 }
2308 }
2309
2310 if (PcdGet32 (PcdTcg2NumberOfPCRBanks) == 0) {
2311 mTcgDxeData.BsCap.NumberOfPCRBanks = NumberOfPCRBanks;
2312 } else {
2313 mTcgDxeData.BsCap.NumberOfPCRBanks = PcdGet32 (PcdTcg2NumberOfPCRBanks);
2314 if (PcdGet32 (PcdTcg2NumberOfPCRBanks) > NumberOfPCRBanks) {
2315 DEBUG ((EFI_D_ERROR, "ERROR: PcdTcg2NumberOfPCRBanks(0x%x) > NumberOfPCRBanks(0x%x)\n", PcdGet32 (PcdTcg2NumberOfPCRBanks), NumberOfPCRBanks));
2316 mTcgDxeData.BsCap.NumberOfPCRBanks = NumberOfPCRBanks;
2317 }
2318 }
2319
2320 mTcgDxeData.BsCap.SupportedEventLogs = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2 | EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
2321 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA1) == 0) {
2322 //
2323 // No need to expose TCG1.2 event log if SHA1 bank does not exist.
2324 //
2325 mTcgDxeData.BsCap.SupportedEventLogs &= ~EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
2326 }
2327
2328 DEBUG ((EFI_D_INFO, "Tcg2.SupportedEventLogs - 0x%08x\n", mTcgDxeData.BsCap.SupportedEventLogs));
2329 DEBUG ((EFI_D_INFO, "Tcg2.HashAlgorithmBitmap - 0x%08x\n", mTcgDxeData.BsCap.HashAlgorithmBitmap));
2330 DEBUG ((EFI_D_INFO, "Tcg2.NumberOfPCRBanks - 0x%08x\n", mTcgDxeData.BsCap.NumberOfPCRBanks));
2331 DEBUG ((EFI_D_INFO, "Tcg2.ActivePcrBanks - 0x%08x\n", mTcgDxeData.BsCap.ActivePcrBanks));
2332
2333 if (mTcgDxeData.BsCap.TPMPresentFlag) {
2334 //
2335 // Setup the log area and copy event log from hob list to it
2336 //
2337 Status = SetupEventLog ();
2338 ASSERT_EFI_ERROR (Status);
2339
2340 //
2341 // Measure handoff tables, Boot#### variables etc.
2342 //
2343 Status = EfiCreateEventReadyToBootEx (
2344 TPL_CALLBACK,
2345 OnReadyToBoot,
2346 NULL,
2347 &Event
2348 );
2349
2350 Status = gBS->CreateEventEx (
2351 EVT_NOTIFY_SIGNAL,
2352 TPL_NOTIFY,
2353 OnExitBootServices,
2354 NULL,
2355 &gEfiEventExitBootServicesGuid,
2356 &Event
2357 );
2358
2359 //
2360 // Measure Exit Boot Service failed
2361 //
2362 Status = gBS->CreateEventEx (
2363 EVT_NOTIFY_SIGNAL,
2364 TPL_NOTIFY,
2365 OnExitBootServicesFailed,
2366 NULL,
2367 &gEventExitBootServicesFailedGuid,
2368 &Event
2369 );
2370
2371 //
2372 // Create event callback, because we need access variable on SecureBootPolicyVariable
2373 // We should use VariableWriteArch instead of VariableArch, because Variable driver
2374 // may update SecureBoot value based on last setting.
2375 //
2376 EfiCreateProtocolNotifyEvent (&gEfiVariableWriteArchProtocolGuid, TPL_CALLBACK, MeasureSecureBootPolicy, NULL, &Registration);
2377 }
2378
2379 //
2380 // Install Tcg2Protocol
2381 //
2382 Status = InstallTcg2 ();
2383 DEBUG ((EFI_D_INFO, "InstallTcg2 - %r\n", Status));
2384
2385 return Status;
2386 }