]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.c
SecurityPkg Tcg2: Remove use of module internal API InternalIsZeroBuffer()
[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 This function get digest from digest list.
868
869 @param HashAlg digest algorithm
870 @param DigestList digest list
871 @param Digest digest
872
873 @retval EFI_SUCCESS Sha1Digest is found and returned.
874 @retval EFI_NOT_FOUND Sha1Digest is not found.
875 **/
876 EFI_STATUS
877 Tpm2GetDigestFromDigestList (
878 IN TPMI_ALG_HASH HashAlg,
879 IN TPML_DIGEST_VALUES *DigestList,
880 IN VOID *Digest
881 )
882 {
883 UINTN Index;
884 UINT16 DigestSize;
885
886 DigestSize = GetHashSizeFromAlgo (HashAlg);
887 for (Index = 0; Index < DigestList->count; Index++) {
888 if (DigestList->digests[Index].hashAlg == HashAlg) {
889 CopyMem (
890 Digest,
891 &DigestList->digests[Index].digest,
892 DigestSize
893 );
894 return EFI_SUCCESS;
895 }
896 }
897
898 return EFI_NOT_FOUND;
899 }
900
901 /**
902 Get TPML_DIGEST_VALUES data size.
903
904 @param[in] DigestList TPML_DIGEST_VALUES data.
905
906 @return TPML_DIGEST_VALUES data size.
907 **/
908 UINT32
909 GetDigestListSize (
910 IN TPML_DIGEST_VALUES *DigestList
911 )
912 {
913 UINTN Index;
914 UINT16 DigestSize;
915 UINT32 TotalSize;
916
917 TotalSize = sizeof(DigestList->count);
918 for (Index = 0; Index < DigestList->count; Index++) {
919 DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg);
920 TotalSize += sizeof(DigestList->digests[Index].hashAlg) + DigestSize;
921 }
922
923 return TotalSize;
924 }
925
926 /**
927 Get TPML_DIGEST_VALUES compact binary buffer size.
928
929 @param[in] DigestListBin TPML_DIGEST_VALUES compact binary buffer.
930
931 @return TPML_DIGEST_VALUES compact binary buffer size.
932 **/
933 UINT32
934 GetDigestListBinSize (
935 IN VOID *DigestListBin
936 )
937 {
938 UINTN Index;
939 UINT16 DigestSize;
940 UINT32 TotalSize;
941 UINT32 Count;
942 TPMI_ALG_HASH HashAlg;
943
944 Count = ReadUnaligned32 (DigestListBin);
945 TotalSize = sizeof(Count);
946 DigestListBin = (UINT8 *)DigestListBin + sizeof(Count);
947 for (Index = 0; Index < Count; Index++) {
948 HashAlg = ReadUnaligned16 (DigestListBin);
949 TotalSize += sizeof(HashAlg);
950 DigestListBin = (UINT8 *)DigestListBin + sizeof(HashAlg);
951
952 DigestSize = GetHashSizeFromAlgo (HashAlg);
953 TotalSize += DigestSize;
954 DigestListBin = (UINT8 *)DigestListBin + DigestSize;
955 }
956
957 return TotalSize;
958 }
959
960 /**
961 Return if hash alg is supported in TPM PCR bank.
962
963 @param HashAlg Hash algorithm to be checked.
964
965 @retval TRUE Hash algorithm is supported.
966 @retval FALSE Hash algorithm is not supported.
967 **/
968 BOOLEAN
969 IsHashAlgSupportedInPcrBank (
970 IN TPMI_ALG_HASH HashAlg
971 )
972 {
973 switch (HashAlg) {
974 case TPM_ALG_SHA1:
975 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA1) != 0) {
976 return TRUE;
977 }
978 break;
979 case TPM_ALG_SHA256:
980 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA256) != 0) {
981 return TRUE;
982 }
983 break;
984 case TPM_ALG_SHA384:
985 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA384) != 0) {
986 return TRUE;
987 }
988 break;
989 case TPM_ALG_SHA512:
990 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA512) != 0) {
991 return TRUE;
992 }
993 break;
994 case TPM_ALG_SM3_256:
995 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SM3_256) != 0) {
996 return TRUE;
997 }
998 break;
999 }
1000
1001 return FALSE;
1002 }
1003
1004 /**
1005 Copy TPML_DIGEST_VALUES into a buffer
1006
1007 @param[in,out] Buffer Buffer to hold TPML_DIGEST_VALUES.
1008 @param[in] DigestList TPML_DIGEST_VALUES to be copied.
1009
1010 @return The end of buffer to hold TPML_DIGEST_VALUES.
1011 **/
1012 VOID *
1013 CopyDigestListToBuffer (
1014 IN OUT VOID *Buffer,
1015 IN TPML_DIGEST_VALUES *DigestList
1016 )
1017 {
1018 UINTN Index;
1019 UINT16 DigestSize;
1020
1021 CopyMem (Buffer, &DigestList->count, sizeof(DigestList->count));
1022 Buffer = (UINT8 *)Buffer + sizeof(DigestList->count);
1023 for (Index = 0; Index < DigestList->count; Index++) {
1024 if (!IsHashAlgSupportedInPcrBank (DigestList->digests[Index].hashAlg)) {
1025 DEBUG ((EFI_D_ERROR, "WARNING: TPM2 Event log has HashAlg unsupported by PCR bank (0x%x)\n", DigestList->digests[Index].hashAlg));
1026 continue;
1027 }
1028 CopyMem (Buffer, &DigestList->digests[Index].hashAlg, sizeof(DigestList->digests[Index].hashAlg));
1029 Buffer = (UINT8 *)Buffer + sizeof(DigestList->digests[Index].hashAlg);
1030 DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg);
1031 CopyMem (Buffer, &DigestList->digests[Index].digest, DigestSize);
1032 Buffer = (UINT8 *)Buffer + DigestSize;
1033 }
1034
1035 return Buffer;
1036 }
1037
1038 /**
1039 Add a new entry to the Event Log.
1040
1041 @param[in] DigestList A list of digest.
1042 @param[in,out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
1043 @param[in] NewEventData Pointer to the new event data.
1044
1045 @retval EFI_SUCCESS The new event log entry was added.
1046 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
1047 **/
1048 EFI_STATUS
1049 TcgDxeLogHashEvent (
1050 IN TPML_DIGEST_VALUES *DigestList,
1051 IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,
1052 IN UINT8 *NewEventData
1053 )
1054 {
1055 EFI_STATUS Status;
1056 EFI_TPL OldTpl;
1057 UINTN Index;
1058 EFI_STATUS RetStatus;
1059 TCG_PCR_EVENT2 TcgPcrEvent2;
1060 UINT8 *DigestBuffer;
1061
1062 DEBUG ((EFI_D_INFO, "SupportedEventLogs - 0x%08x\n", mTcgDxeData.BsCap.SupportedEventLogs));
1063
1064 RetStatus = EFI_SUCCESS;
1065 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
1066 if ((mTcgDxeData.BsCap.SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {
1067 DEBUG ((EFI_D_INFO, " LogFormat - 0x%08x\n", mTcg2EventInfo[Index].LogFormat));
1068 switch (mTcg2EventInfo[Index].LogFormat) {
1069 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2:
1070 Status = Tpm2GetDigestFromDigestList (TPM_ALG_SHA1, DigestList, &NewEventHdr->Digest);
1071 if (!EFI_ERROR (Status)) {
1072 //
1073 // Enter critical region
1074 //
1075 OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
1076 Status = TcgDxeLogEvent (
1077 mTcg2EventInfo[Index].LogFormat,
1078 NewEventHdr,
1079 sizeof(TCG_PCR_EVENT_HDR),
1080 NewEventData,
1081 NewEventHdr->EventSize
1082 );
1083 if (Status != EFI_SUCCESS) {
1084 RetStatus = Status;
1085 }
1086 gBS->RestoreTPL (OldTpl);
1087 //
1088 // Exit critical region
1089 //
1090 }
1091 break;
1092 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2:
1093 ZeroMem (&TcgPcrEvent2, sizeof(TcgPcrEvent2));
1094 TcgPcrEvent2.PCRIndex = NewEventHdr->PCRIndex;
1095 TcgPcrEvent2.EventType = NewEventHdr->EventType;
1096 DigestBuffer = (UINT8 *)&TcgPcrEvent2.Digest;
1097 DigestBuffer = CopyDigestListToBuffer (DigestBuffer, DigestList);
1098 CopyMem (DigestBuffer, &NewEventHdr->EventSize, sizeof(NewEventHdr->EventSize));
1099 DigestBuffer = DigestBuffer + sizeof(NewEventHdr->EventSize);
1100
1101 //
1102 // Enter critical region
1103 //
1104 OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
1105 Status = TcgDxeLogEvent (
1106 mTcg2EventInfo[Index].LogFormat,
1107 &TcgPcrEvent2,
1108 sizeof(TcgPcrEvent2.PCRIndex) + sizeof(TcgPcrEvent2.EventType) + GetDigestListSize (DigestList) + sizeof(TcgPcrEvent2.EventSize),
1109 NewEventData,
1110 NewEventHdr->EventSize
1111 );
1112 if (Status != EFI_SUCCESS) {
1113 RetStatus = Status;
1114 }
1115 gBS->RestoreTPL (OldTpl);
1116 //
1117 // Exit critical region
1118 //
1119 break;
1120 }
1121 }
1122 }
1123
1124 return RetStatus;
1125 }
1126
1127 /**
1128 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
1129 and add an entry to the Event Log.
1130
1131 @param[in] Flags Bitmap providing additional information.
1132 @param[in] HashData Physical address of the start of the data buffer
1133 to be hashed, extended, and logged.
1134 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
1135 @param[in, out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
1136 @param[in] NewEventData Pointer to the new event data.
1137
1138 @retval EFI_SUCCESS Operation completed successfully.
1139 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
1140 @retval EFI_DEVICE_ERROR The command was unsuccessful.
1141
1142 **/
1143 EFI_STATUS
1144 TcgDxeHashLogExtendEvent (
1145 IN UINT64 Flags,
1146 IN UINT8 *HashData,
1147 IN UINT64 HashDataLen,
1148 IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,
1149 IN UINT8 *NewEventData
1150 )
1151 {
1152 EFI_STATUS Status;
1153 TPML_DIGEST_VALUES DigestList;
1154
1155 if (!mTcgDxeData.BsCap.TPMPresentFlag) {
1156 return EFI_DEVICE_ERROR;
1157 }
1158
1159 Status = HashAndExtend (
1160 NewEventHdr->PCRIndex,
1161 HashData,
1162 (UINTN)HashDataLen,
1163 &DigestList
1164 );
1165 if (!EFI_ERROR (Status)) {
1166 if ((Flags & EFI_TCG2_EXTEND_ONLY) == 0) {
1167 Status = TcgDxeLogHashEvent (&DigestList, NewEventHdr, NewEventData);
1168 }
1169 }
1170
1171 if (Status == EFI_DEVICE_ERROR) {
1172 DEBUG ((EFI_D_ERROR, "TcgDxeHashLogExtendEvent - %r. Disable TPM.\n", Status));
1173 mTcgDxeData.BsCap.TPMPresentFlag = FALSE;
1174 REPORT_STATUS_CODE (
1175 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1176 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)
1177 );
1178 }
1179
1180 return Status;
1181 }
1182
1183 /**
1184 The EFI_TCG2_PROTOCOL HashLogExtendEvent function call provides callers with
1185 an opportunity to extend and optionally log events without requiring
1186 knowledge of actual TPM commands.
1187 The extend operation will occur even if this function cannot create an event
1188 log entry (e.g. due to the event log being full).
1189
1190 @param[in] This Indicates the calling context
1191 @param[in] Flags Bitmap providing additional information.
1192 @param[in] DataToHash Physical address of the start of the data buffer to be hashed.
1193 @param[in] DataToHashLen The length in bytes of the buffer referenced by DataToHash.
1194 @param[in] Event Pointer to data buffer containing information about the event.
1195
1196 @retval EFI_SUCCESS Operation completed successfully.
1197 @retval EFI_DEVICE_ERROR The command was unsuccessful.
1198 @retval EFI_VOLUME_FULL The extend operation occurred, but the event could not be written to one or more event logs.
1199 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1200 @retval EFI_UNSUPPORTED The PE/COFF image type is not supported.
1201 **/
1202 EFI_STATUS
1203 EFIAPI
1204 Tcg2HashLogExtendEvent (
1205 IN EFI_TCG2_PROTOCOL *This,
1206 IN UINT64 Flags,
1207 IN EFI_PHYSICAL_ADDRESS DataToHash,
1208 IN UINT64 DataToHashLen,
1209 IN EFI_TCG2_EVENT *Event
1210 )
1211 {
1212 EFI_STATUS Status;
1213 TCG_PCR_EVENT_HDR NewEventHdr;
1214 TPML_DIGEST_VALUES DigestList;
1215
1216 DEBUG ((DEBUG_VERBOSE, "Tcg2HashLogExtendEvent ...\n"));
1217
1218 if ((This == NULL) || (DataToHash == 0) || (Event == NULL)) {
1219 return EFI_INVALID_PARAMETER;
1220 }
1221
1222 if (!mTcgDxeData.BsCap.TPMPresentFlag) {
1223 return EFI_DEVICE_ERROR;
1224 }
1225
1226 if (Event->Size < Event->Header.HeaderSize + sizeof(UINT32)) {
1227 return EFI_INVALID_PARAMETER;
1228 }
1229
1230 if (Event->Header.PCRIndex > MAX_PCR_INDEX) {
1231 return EFI_INVALID_PARAMETER;
1232 }
1233
1234 NewEventHdr.PCRIndex = Event->Header.PCRIndex;
1235 NewEventHdr.EventType = Event->Header.EventType;
1236 NewEventHdr.EventSize = Event->Size - sizeof(UINT32) - Event->Header.HeaderSize;
1237 if ((Flags & PE_COFF_IMAGE) != 0) {
1238 Status = MeasurePeImageAndExtend (
1239 NewEventHdr.PCRIndex,
1240 DataToHash,
1241 (UINTN)DataToHashLen,
1242 &DigestList
1243 );
1244 if (!EFI_ERROR (Status)) {
1245 if ((Flags & EFI_TCG2_EXTEND_ONLY) == 0) {
1246 Status = TcgDxeLogHashEvent (&DigestList, &NewEventHdr, Event->Event);
1247 }
1248 }
1249 if (Status == EFI_DEVICE_ERROR) {
1250 DEBUG ((EFI_D_ERROR, "MeasurePeImageAndExtend - %r. Disable TPM.\n", Status));
1251 mTcgDxeData.BsCap.TPMPresentFlag = FALSE;
1252 REPORT_STATUS_CODE (
1253 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1254 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)
1255 );
1256 }
1257 } else {
1258 Status = TcgDxeHashLogExtendEvent (
1259 Flags,
1260 (UINT8 *) (UINTN) DataToHash,
1261 DataToHashLen,
1262 &NewEventHdr,
1263 Event->Event
1264 );
1265 }
1266 DEBUG ((DEBUG_VERBOSE, "Tcg2HashLogExtendEvent - %r\n", Status));
1267 return Status;
1268 }
1269
1270 /**
1271 This service enables the sending of commands to the TPM.
1272
1273 @param[in] This Indicates the calling context
1274 @param[in] InputParameterBlockSize Size of the TPM input parameter block.
1275 @param[in] InputParameterBlock Pointer to the TPM input parameter block.
1276 @param[in] OutputParameterBlockSize Size of the TPM output parameter block.
1277 @param[in] OutputParameterBlock Pointer to the TPM output parameter block.
1278
1279 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
1280 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
1281 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1282 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
1283 **/
1284 EFI_STATUS
1285 EFIAPI
1286 Tcg2SubmitCommand (
1287 IN EFI_TCG2_PROTOCOL *This,
1288 IN UINT32 InputParameterBlockSize,
1289 IN UINT8 *InputParameterBlock,
1290 IN UINT32 OutputParameterBlockSize,
1291 IN UINT8 *OutputParameterBlock
1292 )
1293 {
1294 EFI_STATUS Status;
1295
1296 DEBUG ((EFI_D_INFO, "Tcg2SubmitCommand ...\n"));
1297
1298 if ((This == NULL) ||
1299 (InputParameterBlockSize == 0) || (InputParameterBlock == NULL) ||
1300 (OutputParameterBlockSize == 0) || (OutputParameterBlock == NULL)) {
1301 return EFI_INVALID_PARAMETER;
1302 }
1303
1304 if (!mTcgDxeData.BsCap.TPMPresentFlag) {
1305 return EFI_DEVICE_ERROR;
1306 }
1307
1308 if (InputParameterBlockSize > mTcgDxeData.BsCap.MaxCommandSize) {
1309 return EFI_INVALID_PARAMETER;
1310 }
1311 if (OutputParameterBlockSize > mTcgDxeData.BsCap.MaxResponseSize) {
1312 return EFI_INVALID_PARAMETER;
1313 }
1314
1315 Status = Tpm2SubmitCommand (
1316 InputParameterBlockSize,
1317 InputParameterBlock,
1318 &OutputParameterBlockSize,
1319 OutputParameterBlock
1320 );
1321 DEBUG ((EFI_D_INFO, "Tcg2SubmitCommand - %r\n", Status));
1322 return Status;
1323 }
1324
1325 /**
1326 This service returns the currently active PCR banks.
1327
1328 @param[in] This Indicates the calling context
1329 @param[out] ActivePcrBanks Pointer to the variable receiving the bitmap of currently active PCR banks.
1330
1331 @retval EFI_SUCCESS The bitmap of active PCR banks was stored in the ActivePcrBanks parameter.
1332 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1333 **/
1334 EFI_STATUS
1335 EFIAPI
1336 Tcg2GetActivePCRBanks (
1337 IN EFI_TCG2_PROTOCOL *This,
1338 OUT UINT32 *ActivePcrBanks
1339 )
1340 {
1341 if (ActivePcrBanks == NULL) {
1342 return EFI_INVALID_PARAMETER;
1343 }
1344 *ActivePcrBanks = mTcgDxeData.BsCap.ActivePcrBanks;
1345 return EFI_SUCCESS;
1346 }
1347
1348 /**
1349 This service sets the currently active PCR banks.
1350
1351 @param[in] This Indicates the calling context
1352 @param[in] ActivePcrBanks Bitmap of the requested active PCR banks. At least one bit SHALL be set.
1353
1354 @retval EFI_SUCCESS The bitmap in ActivePcrBank parameter is already active.
1355 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1356 **/
1357 EFI_STATUS
1358 EFIAPI
1359 Tcg2SetActivePCRBanks (
1360 IN EFI_TCG2_PROTOCOL *This,
1361 IN UINT32 ActivePcrBanks
1362 )
1363 {
1364 EFI_STATUS Status;
1365 UINT32 ReturnCode;
1366
1367 DEBUG ((EFI_D_INFO, "Tcg2SetActivePCRBanks ... (0x%x)\n", ActivePcrBanks));
1368
1369 if (ActivePcrBanks == 0) {
1370 return EFI_INVALID_PARAMETER;
1371 }
1372 if ((ActivePcrBanks & (~mTcgDxeData.BsCap.HashAlgorithmBitmap)) != 0) {
1373 return EFI_INVALID_PARAMETER;
1374 }
1375 if (ActivePcrBanks == mTcgDxeData.BsCap.ActivePcrBanks) {
1376 //
1377 // Need clear previous SET_PCR_BANKS setting
1378 //
1379 ReturnCode = Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction (TCG2_PHYSICAL_PRESENCE_NO_ACTION, 0);
1380 } else {
1381 ReturnCode = Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction (TCG2_PHYSICAL_PRESENCE_SET_PCR_BANKS, ActivePcrBanks);
1382 }
1383
1384 if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_SUCCESS) {
1385 Status = EFI_SUCCESS;
1386 } else if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE) {
1387 Status = EFI_OUT_OF_RESOURCES;
1388 } else if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_NOT_IMPLEMENTED) {
1389 Status = EFI_UNSUPPORTED;
1390 } else {
1391 Status = EFI_DEVICE_ERROR;
1392 }
1393
1394 DEBUG ((EFI_D_INFO, "Tcg2SetActivePCRBanks - %r\n", Status));
1395
1396 return Status;
1397 }
1398
1399 /**
1400 This service retrieves the result of a previous invocation of SetActivePcrBanks.
1401
1402 @param[in] This Indicates the calling context
1403 @param[out] OperationPresent Non-zero value to indicate a SetActivePcrBank operation was invoked during the last boot.
1404 @param[out] Response The response from the SetActivePcrBank request.
1405
1406 @retval EFI_SUCCESS The result value could be returned.
1407 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1408 **/
1409 EFI_STATUS
1410 EFIAPI
1411 Tcg2GetResultOfSetActivePcrBanks (
1412 IN EFI_TCG2_PROTOCOL *This,
1413 OUT UINT32 *OperationPresent,
1414 OUT UINT32 *Response
1415 )
1416 {
1417 UINT32 ReturnCode;
1418
1419 if ((OperationPresent == NULL) || (Response == NULL)) {
1420 return EFI_INVALID_PARAMETER;
1421 }
1422
1423 ReturnCode = Tcg2PhysicalPresenceLibReturnOperationResponseToOsFunction (OperationPresent, Response);
1424 if (ReturnCode == TCG_PP_RETURN_TPM_OPERATION_RESPONSE_SUCCESS) {
1425 return EFI_SUCCESS;
1426 } else {
1427 return EFI_UNSUPPORTED;
1428 }
1429 }
1430
1431 EFI_TCG2_PROTOCOL mTcg2Protocol = {
1432 Tcg2GetCapability,
1433 Tcg2GetEventLog,
1434 Tcg2HashLogExtendEvent,
1435 Tcg2SubmitCommand,
1436 Tcg2GetActivePCRBanks,
1437 Tcg2SetActivePCRBanks,
1438 Tcg2GetResultOfSetActivePcrBanks,
1439 };
1440
1441 /**
1442 Initialize the Event Log and log events passed from the PEI phase.
1443
1444 @retval EFI_SUCCESS Operation completed successfully.
1445 @retval EFI_OUT_OF_RESOURCES Out of memory.
1446
1447 **/
1448 EFI_STATUS
1449 SetupEventLog (
1450 VOID
1451 )
1452 {
1453 EFI_STATUS Status;
1454 VOID *TcgEvent;
1455 EFI_PEI_HOB_POINTERS GuidHob;
1456 EFI_PHYSICAL_ADDRESS Lasa;
1457 UINTN Index;
1458 UINT32 DigestListBinSize;
1459 UINT32 EventSize;
1460 TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct;
1461 UINT8 TempBuf[sizeof(TCG_EfiSpecIDEventStruct) + sizeof(UINT32) + (HASH_COUNT * sizeof(TCG_EfiSpecIdEventAlgorithmSize)) + sizeof(UINT8)];
1462 TCG_PCR_EVENT_HDR FirstPcrEvent;
1463 TCG_EfiSpecIdEventAlgorithmSize *DigestSize;
1464 TCG_EfiSpecIdEventAlgorithmSize *TempDigestSize;
1465 UINT8 *VendorInfoSize;
1466 UINT32 NumberOfAlgorithms;
1467
1468 DEBUG ((EFI_D_INFO, "SetupEventLog\n"));
1469
1470 //
1471 // 1. Create Log Area
1472 //
1473 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
1474 if ((mTcgDxeData.BsCap.SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {
1475 mTcgDxeData.EventLogAreaStruct[Index].EventLogFormat = mTcg2EventInfo[Index].LogFormat;
1476 Lasa = (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1);
1477 Status = gBS->AllocatePages (
1478 AllocateMaxAddress,
1479 EfiBootServicesData,
1480 EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcgLogAreaMinLen)),
1481 &Lasa
1482 );
1483 if (EFI_ERROR (Status)) {
1484 return Status;
1485 }
1486 mTcgDxeData.EventLogAreaStruct[Index].Lasa = Lasa;
1487 mTcgDxeData.EventLogAreaStruct[Index].Laml = PcdGet32 (PcdTcgLogAreaMinLen);
1488 //
1489 // To initialize them as 0xFF is recommended
1490 // because the OS can know the last entry for that.
1491 //
1492 SetMem ((VOID *)(UINTN)Lasa, PcdGet32 (PcdTcgLogAreaMinLen), 0xFF);
1493 //
1494 // Create first entry for Log Header Entry Data
1495 //
1496 if (mTcg2EventInfo[Index].LogFormat != EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2) {
1497 //
1498 // TcgEfiSpecIdEventStruct
1499 //
1500 TcgEfiSpecIdEventStruct = (TCG_EfiSpecIDEventStruct *)TempBuf;
1501 CopyMem (TcgEfiSpecIdEventStruct->signature, TCG_EfiSpecIDEventStruct_SIGNATURE_03, sizeof(TcgEfiSpecIdEventStruct->signature));
1502 TcgEfiSpecIdEventStruct->platformClass = PcdGet8 (PcdTpmPlatformClass);
1503 TcgEfiSpecIdEventStruct->specVersionMajor = TCG_EfiSpecIDEventStruct_SPEC_VERSION_MAJOR_TPM2;
1504 TcgEfiSpecIdEventStruct->specVersionMinor = TCG_EfiSpecIDEventStruct_SPEC_VERSION_MINOR_TPM2;
1505 TcgEfiSpecIdEventStruct->specErrata = TCG_EfiSpecIDEventStruct_SPEC_ERRATA_TPM2;
1506 TcgEfiSpecIdEventStruct->uintnSize = sizeof(UINTN)/sizeof(UINT32);
1507 NumberOfAlgorithms = 0;
1508 DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct + sizeof(*TcgEfiSpecIdEventStruct) + sizeof(NumberOfAlgorithms));
1509 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA1) != 0) {
1510 TempDigestSize = DigestSize;
1511 TempDigestSize += NumberOfAlgorithms;
1512 TempDigestSize->algorithmId = TPM_ALG_SHA1;
1513 TempDigestSize->digestSize = SHA1_DIGEST_SIZE;
1514 NumberOfAlgorithms++;
1515 }
1516 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA256) != 0) {
1517 TempDigestSize = DigestSize;
1518 TempDigestSize += NumberOfAlgorithms;
1519 TempDigestSize->algorithmId = TPM_ALG_SHA256;
1520 TempDigestSize->digestSize = SHA256_DIGEST_SIZE;
1521 NumberOfAlgorithms++;
1522 }
1523 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA384) != 0) {
1524 TempDigestSize = DigestSize;
1525 TempDigestSize += NumberOfAlgorithms;
1526 TempDigestSize->algorithmId = TPM_ALG_SHA384;
1527 TempDigestSize->digestSize = SHA384_DIGEST_SIZE;
1528 NumberOfAlgorithms++;
1529 }
1530 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA512) != 0) {
1531 TempDigestSize = DigestSize;
1532 TempDigestSize += NumberOfAlgorithms;
1533 TempDigestSize->algorithmId = TPM_ALG_SHA512;
1534 TempDigestSize->digestSize = SHA512_DIGEST_SIZE;
1535 NumberOfAlgorithms++;
1536 }
1537 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SM3_256) != 0) {
1538 TempDigestSize = DigestSize;
1539 TempDigestSize += NumberOfAlgorithms;
1540 TempDigestSize->algorithmId = TPM_ALG_SM3_256;
1541 TempDigestSize->digestSize = SM3_256_DIGEST_SIZE;
1542 NumberOfAlgorithms++;
1543 }
1544 CopyMem (TcgEfiSpecIdEventStruct + 1, &NumberOfAlgorithms, sizeof(NumberOfAlgorithms));
1545 TempDigestSize = DigestSize;
1546 TempDigestSize += NumberOfAlgorithms;
1547 VendorInfoSize = (UINT8 *)TempDigestSize;
1548 *VendorInfoSize = 0;
1549
1550 //
1551 // FirstPcrEvent
1552 //
1553 FirstPcrEvent.PCRIndex = 0;
1554 FirstPcrEvent.EventType = EV_NO_ACTION;
1555 ZeroMem (&FirstPcrEvent.Digest, sizeof(FirstPcrEvent.Digest));
1556 FirstPcrEvent.EventSize = (UINT32)GetTcgEfiSpecIdEventStructSize (TcgEfiSpecIdEventStruct);
1557
1558 //
1559 // Record
1560 //
1561 Status = TcgDxeLogEvent (
1562 mTcg2EventInfo[Index].LogFormat,
1563 &FirstPcrEvent,
1564 sizeof(FirstPcrEvent),
1565 (UINT8 *)TcgEfiSpecIdEventStruct,
1566 FirstPcrEvent.EventSize
1567 );
1568 }
1569 }
1570 }
1571
1572 //
1573 // 2. Create Final Log Area
1574 //
1575 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
1576 if ((mTcgDxeData.BsCap.SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {
1577 if (mTcg2EventInfo[Index].LogFormat == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
1578 Lasa = (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1);
1579 Status = gBS->AllocatePages (
1580 AllocateMaxAddress,
1581 EfiACPIMemoryNVS,
1582 EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcg2FinalLogAreaLen)),
1583 &Lasa
1584 );
1585 if (EFI_ERROR (Status)) {
1586 return Status;
1587 }
1588 SetMem ((VOID *)(UINTN)Lasa, PcdGet32 (PcdTcg2FinalLogAreaLen), 0xFF);
1589
1590 //
1591 // Initialize
1592 //
1593 mTcgDxeData.FinalEventsTable[Index] = (VOID *)(UINTN)Lasa;
1594 (mTcgDxeData.FinalEventsTable[Index])->Version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
1595 (mTcgDxeData.FinalEventsTable[Index])->NumberOfEvents = 0;
1596
1597 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogFormat = mTcg2EventInfo[Index].LogFormat;
1598 mTcgDxeData.FinalEventLogAreaStruct[Index].Lasa = Lasa + sizeof(EFI_TCG2_FINAL_EVENTS_TABLE);
1599 mTcgDxeData.FinalEventLogAreaStruct[Index].Laml = PcdGet32 (PcdTcg2FinalLogAreaLen) - sizeof(EFI_TCG2_FINAL_EVENTS_TABLE);
1600 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogSize = 0;
1601 mTcgDxeData.FinalEventLogAreaStruct[Index].LastEvent = (VOID *)(UINTN)mTcgDxeData.FinalEventLogAreaStruct[Index].Lasa;
1602 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogStarted = FALSE;
1603 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogTruncated = FALSE;
1604
1605 //
1606 // Install to configuration table for EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
1607 //
1608 Status = gBS->InstallConfigurationTable (&gEfiTcg2FinalEventsTableGuid, (VOID *)mTcgDxeData.FinalEventsTable[Index]);
1609 if (EFI_ERROR (Status)) {
1610 return Status;
1611 }
1612 } else {
1613 //
1614 // No need to handle EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2
1615 //
1616 mTcgDxeData.FinalEventsTable[Index] = NULL;
1617 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogFormat = mTcg2EventInfo[Index].LogFormat;
1618 mTcgDxeData.FinalEventLogAreaStruct[Index].Lasa = 0;
1619 mTcgDxeData.FinalEventLogAreaStruct[Index].Laml = 0;
1620 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogSize = 0;
1621 mTcgDxeData.FinalEventLogAreaStruct[Index].LastEvent = 0;
1622 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogStarted = FALSE;
1623 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogTruncated = FALSE;
1624 }
1625 }
1626 }
1627
1628 //
1629 // 3. Sync data from PEI to DXE
1630 //
1631 Status = EFI_SUCCESS;
1632 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
1633 if ((mTcgDxeData.BsCap.SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {
1634 GuidHob.Raw = GetHobList ();
1635 Status = EFI_SUCCESS;
1636 while (!EFI_ERROR (Status) &&
1637 (GuidHob.Raw = GetNextGuidHob (mTcg2EventInfo[Index].EventGuid, GuidHob.Raw)) != NULL) {
1638 TcgEvent = GET_GUID_HOB_DATA (GuidHob.Guid);
1639 GuidHob.Raw = GET_NEXT_HOB (GuidHob);
1640 switch (mTcg2EventInfo[Index].LogFormat) {
1641 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2:
1642 Status = TcgDxeLogEvent (
1643 mTcg2EventInfo[Index].LogFormat,
1644 TcgEvent,
1645 sizeof(TCG_PCR_EVENT_HDR),
1646 ((TCG_PCR_EVENT*)TcgEvent)->Event,
1647 ((TCG_PCR_EVENT_HDR*)TcgEvent)->EventSize
1648 );
1649 break;
1650 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2:
1651 DigestListBinSize = GetDigestListBinSize ((UINT8 *)TcgEvent + sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE));
1652 CopyMem (&EventSize, (UINT8 *)TcgEvent + sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE) + DigestListBinSize, sizeof(UINT32));
1653 Status = TcgDxeLogEvent (
1654 mTcg2EventInfo[Index].LogFormat,
1655 TcgEvent,
1656 sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE) + DigestListBinSize + sizeof(UINT32),
1657 (UINT8 *)TcgEvent + sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE) + DigestListBinSize + sizeof(UINT32),
1658 EventSize
1659 );
1660 break;
1661 }
1662 }
1663 }
1664 }
1665
1666 return Status;
1667 }
1668
1669 /**
1670 Measure and log an action string, and extend the measurement result into PCR[5].
1671
1672 @param[in] String A specific string that indicates an Action event.
1673
1674 @retval EFI_SUCCESS Operation completed successfully.
1675 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1676
1677 **/
1678 EFI_STATUS
1679 TcgMeasureAction (
1680 IN CHAR8 *String
1681 )
1682 {
1683 TCG_PCR_EVENT_HDR TcgEvent;
1684
1685 TcgEvent.PCRIndex = 5;
1686 TcgEvent.EventType = EV_EFI_ACTION;
1687 TcgEvent.EventSize = (UINT32)AsciiStrLen (String);
1688 return TcgDxeHashLogExtendEvent (
1689 0,
1690 (UINT8*)String,
1691 TcgEvent.EventSize,
1692 &TcgEvent,
1693 (UINT8 *) String
1694 );
1695 }
1696
1697 /**
1698 Measure and log EFI handoff tables, and extend the measurement result into PCR[1].
1699
1700 @retval EFI_SUCCESS Operation completed successfully.
1701 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1702
1703 **/
1704 EFI_STATUS
1705 MeasureHandoffTables (
1706 VOID
1707 )
1708 {
1709 EFI_STATUS Status;
1710 TCG_PCR_EVENT_HDR TcgEvent;
1711 EFI_HANDOFF_TABLE_POINTERS HandoffTables;
1712 UINTN ProcessorNum;
1713 EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
1714
1715 ProcessorLocBuf = NULL;
1716 Status = EFI_SUCCESS;
1717
1718 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_SERVER) {
1719 //
1720 // Tcg Server spec.
1721 // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]
1722 //
1723 Status = GetProcessorsCpuLocation(&ProcessorLocBuf, &ProcessorNum);
1724
1725 if (!EFI_ERROR(Status)){
1726 TcgEvent.PCRIndex = 1;
1727 TcgEvent.EventType = EV_TABLE_OF_DEVICES;
1728 TcgEvent.EventSize = sizeof (HandoffTables);
1729
1730 HandoffTables.NumberOfTables = 1;
1731 HandoffTables.TableEntry[0].VendorGuid = gEfiMpServiceProtocolGuid;
1732 HandoffTables.TableEntry[0].VendorTable = ProcessorLocBuf;
1733
1734 Status = TcgDxeHashLogExtendEvent (
1735 0,
1736 (UINT8*)(UINTN)ProcessorLocBuf,
1737 sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
1738 &TcgEvent,
1739 (UINT8*)&HandoffTables
1740 );
1741
1742 FreePool(ProcessorLocBuf);
1743 }
1744 }
1745
1746 return Status;
1747 }
1748
1749 /**
1750 Measure and log Separator event, and extend the measurement result into a specific PCR.
1751
1752 @param[in] PCRIndex PCR index.
1753
1754 @retval EFI_SUCCESS Operation completed successfully.
1755 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1756
1757 **/
1758 EFI_STATUS
1759 MeasureSeparatorEvent (
1760 IN TPM_PCRINDEX PCRIndex
1761 )
1762 {
1763 TCG_PCR_EVENT_HDR TcgEvent;
1764 UINT32 EventData;
1765
1766 DEBUG ((EFI_D_INFO, "MeasureSeparatorEvent Pcr - %x\n", PCRIndex));
1767
1768 EventData = 0;
1769 TcgEvent.PCRIndex = PCRIndex;
1770 TcgEvent.EventType = EV_SEPARATOR;
1771 TcgEvent.EventSize = (UINT32)sizeof (EventData);
1772 return TcgDxeHashLogExtendEvent (
1773 0,
1774 (UINT8 *)&EventData,
1775 sizeof (EventData),
1776 &TcgEvent,
1777 (UINT8 *)&EventData
1778 );
1779 }
1780
1781 /**
1782 Measure and log an EFI variable, and extend the measurement result into a specific PCR.
1783
1784 @param[in] PCRIndex PCR Index.
1785 @param[in] EventType Event type.
1786 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1787 @param[in] VendorGuid A unique identifier for the vendor.
1788 @param[in] VarData The content of the variable data.
1789 @param[in] VarSize The size of the variable data.
1790
1791 @retval EFI_SUCCESS Operation completed successfully.
1792 @retval EFI_OUT_OF_RESOURCES Out of memory.
1793 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1794
1795 **/
1796 EFI_STATUS
1797 MeasureVariable (
1798 IN TPM_PCRINDEX PCRIndex,
1799 IN TCG_EVENTTYPE EventType,
1800 IN CHAR16 *VarName,
1801 IN EFI_GUID *VendorGuid,
1802 IN VOID *VarData,
1803 IN UINTN VarSize
1804 )
1805 {
1806 EFI_STATUS Status;
1807 TCG_PCR_EVENT_HDR TcgEvent;
1808 UINTN VarNameLength;
1809 EFI_VARIABLE_DATA_TREE *VarLog;
1810
1811 DEBUG ((EFI_D_INFO, "Tcg2Dxe: MeasureVariable (Pcr - %x, EventType - %x, ", (UINTN)PCRIndex, (UINTN)EventType));
1812 DEBUG ((EFI_D_INFO, "VariableName - %s, VendorGuid - %g)\n", VarName, VendorGuid));
1813
1814 VarNameLength = StrLen (VarName);
1815 TcgEvent.PCRIndex = PCRIndex;
1816 TcgEvent.EventType = EventType;
1817
1818 TcgEvent.EventSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize
1819 - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));
1820
1821 VarLog = (EFI_VARIABLE_DATA_TREE *)AllocatePool (TcgEvent.EventSize);
1822 if (VarLog == NULL) {
1823 return EFI_OUT_OF_RESOURCES;
1824 }
1825
1826 VarLog->VariableName = *VendorGuid;
1827 VarLog->UnicodeNameLength = VarNameLength;
1828 VarLog->VariableDataLength = VarSize;
1829 CopyMem (
1830 VarLog->UnicodeName,
1831 VarName,
1832 VarNameLength * sizeof (*VarName)
1833 );
1834 if (VarSize != 0 && VarData != NULL) {
1835 CopyMem (
1836 (CHAR16 *)VarLog->UnicodeName + VarNameLength,
1837 VarData,
1838 VarSize
1839 );
1840 }
1841
1842 if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {
1843 //
1844 // Digest is the event data (EFI_VARIABLE_DATA)
1845 //
1846 Status = TcgDxeHashLogExtendEvent (
1847 0,
1848 (UINT8*)VarLog,
1849 TcgEvent.EventSize,
1850 &TcgEvent,
1851 (UINT8*)VarLog
1852 );
1853 } else {
1854 Status = TcgDxeHashLogExtendEvent (
1855 0,
1856 (UINT8*)VarData,
1857 VarSize,
1858 &TcgEvent,
1859 (UINT8*)VarLog
1860 );
1861 }
1862 FreePool (VarLog);
1863 return Status;
1864 }
1865
1866 /**
1867 Read then Measure and log an EFI variable, and extend the measurement result into a specific PCR.
1868
1869 @param[in] PCRIndex PCR Index.
1870 @param[in] EventType Event type.
1871 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1872 @param[in] VendorGuid A unique identifier for the vendor.
1873 @param[out] VarSize The size of the variable data.
1874 @param[out] VarData Pointer to the content of the variable.
1875
1876 @retval EFI_SUCCESS Operation completed successfully.
1877 @retval EFI_OUT_OF_RESOURCES Out of memory.
1878 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1879
1880 **/
1881 EFI_STATUS
1882 ReadAndMeasureVariable (
1883 IN TPM_PCRINDEX PCRIndex,
1884 IN TCG_EVENTTYPE EventType,
1885 IN CHAR16 *VarName,
1886 IN EFI_GUID *VendorGuid,
1887 OUT UINTN *VarSize,
1888 OUT VOID **VarData
1889 )
1890 {
1891 EFI_STATUS Status;
1892
1893 Status = GetVariable2 (VarName, VendorGuid, VarData, VarSize);
1894 if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {
1895 if (EFI_ERROR (Status)) {
1896 //
1897 // It is valid case, so we need handle it.
1898 //
1899 *VarData = NULL;
1900 *VarSize = 0;
1901 }
1902 } else {
1903 //
1904 // if status error, VarData is freed and set NULL by GetVariable2
1905 //
1906 if (EFI_ERROR (Status)) {
1907 return EFI_NOT_FOUND;
1908 }
1909 }
1910
1911 Status = MeasureVariable (
1912 PCRIndex,
1913 EventType,
1914 VarName,
1915 VendorGuid,
1916 *VarData,
1917 *VarSize
1918 );
1919 return Status;
1920 }
1921
1922 /**
1923 Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].
1924
1925 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1926 @param[in] VendorGuid A unique identifier for the vendor.
1927 @param[out] VarSize The size of the variable data.
1928 @param[out] VarData Pointer to the content of the variable.
1929
1930 @retval EFI_SUCCESS Operation completed successfully.
1931 @retval EFI_OUT_OF_RESOURCES Out of memory.
1932 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1933
1934 **/
1935 EFI_STATUS
1936 ReadAndMeasureBootVariable (
1937 IN CHAR16 *VarName,
1938 IN EFI_GUID *VendorGuid,
1939 OUT UINTN *VarSize,
1940 OUT VOID **VarData
1941 )
1942 {
1943 return ReadAndMeasureVariable (
1944 5,
1945 EV_EFI_VARIABLE_BOOT,
1946 VarName,
1947 VendorGuid,
1948 VarSize,
1949 VarData
1950 );
1951 }
1952
1953 /**
1954 Read then Measure and log an EFI Secure variable, and extend the measurement result into PCR[7].
1955
1956 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1957 @param[in] VendorGuid A unique identifier for the vendor.
1958 @param[out] VarSize The size of the variable data.
1959 @param[out] VarData Pointer to the content of the variable.
1960
1961 @retval EFI_SUCCESS Operation completed successfully.
1962 @retval EFI_OUT_OF_RESOURCES Out of memory.
1963 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1964
1965 **/
1966 EFI_STATUS
1967 ReadAndMeasureSecureVariable (
1968 IN CHAR16 *VarName,
1969 IN EFI_GUID *VendorGuid,
1970 OUT UINTN *VarSize,
1971 OUT VOID **VarData
1972 )
1973 {
1974 return ReadAndMeasureVariable (
1975 7,
1976 EV_EFI_VARIABLE_DRIVER_CONFIG,
1977 VarName,
1978 VendorGuid,
1979 VarSize,
1980 VarData
1981 );
1982 }
1983
1984 /**
1985 Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.
1986
1987 The EFI boot variables are BootOrder and Boot#### variables.
1988
1989 @retval EFI_SUCCESS Operation completed successfully.
1990 @retval EFI_OUT_OF_RESOURCES Out of memory.
1991 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1992
1993 **/
1994 EFI_STATUS
1995 MeasureAllBootVariables (
1996 VOID
1997 )
1998 {
1999 EFI_STATUS Status;
2000 UINT16 *BootOrder;
2001 UINTN BootCount;
2002 UINTN Index;
2003 VOID *BootVarData;
2004 UINTN Size;
2005
2006 Status = ReadAndMeasureBootVariable (
2007 mBootVarName,
2008 &gEfiGlobalVariableGuid,
2009 &BootCount,
2010 (VOID **) &BootOrder
2011 );
2012 if (Status == EFI_NOT_FOUND || BootOrder == NULL) {
2013 return EFI_SUCCESS;
2014 }
2015
2016 if (EFI_ERROR (Status)) {
2017 //
2018 // BootOrder can't be NULL if status is not EFI_NOT_FOUND
2019 //
2020 FreePool (BootOrder);
2021 return Status;
2022 }
2023
2024 BootCount /= sizeof (*BootOrder);
2025 for (Index = 0; Index < BootCount; Index++) {
2026 UnicodeSPrint (mBootVarName, sizeof (mBootVarName), L"Boot%04x", BootOrder[Index]);
2027 Status = ReadAndMeasureBootVariable (
2028 mBootVarName,
2029 &gEfiGlobalVariableGuid,
2030 &Size,
2031 &BootVarData
2032 );
2033 if (!EFI_ERROR (Status)) {
2034 FreePool (BootVarData);
2035 }
2036 }
2037
2038 FreePool (BootOrder);
2039 return EFI_SUCCESS;
2040 }
2041
2042 /**
2043 Measure and log all EFI Secure variables, and extend the measurement result into a specific PCR.
2044
2045 The EFI boot variables are BootOrder and Boot#### variables.
2046
2047 @retval EFI_SUCCESS Operation completed successfully.
2048 @retval EFI_OUT_OF_RESOURCES Out of memory.
2049 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
2050
2051 **/
2052 EFI_STATUS
2053 MeasureAllSecureVariables (
2054 VOID
2055 )
2056 {
2057 EFI_STATUS Status;
2058 VOID *Data;
2059 UINTN DataSize;
2060 UINTN Index;
2061
2062 Status = EFI_NOT_FOUND;
2063 for (Index = 0; Index < sizeof(mVariableType)/sizeof(mVariableType[0]); Index++) {
2064 Status = ReadAndMeasureSecureVariable (
2065 mVariableType[Index].VariableName,
2066 mVariableType[Index].VendorGuid,
2067 &DataSize,
2068 &Data
2069 );
2070 if (!EFI_ERROR (Status)) {
2071 if (Data != NULL) {
2072 FreePool (Data);
2073 }
2074 }
2075 }
2076
2077 return EFI_SUCCESS;
2078 }
2079
2080 /**
2081 Measure and log launch of FirmwareDebugger, and extend the measurement result into a specific PCR.
2082
2083 @retval EFI_SUCCESS Operation completed successfully.
2084 @retval EFI_OUT_OF_RESOURCES Out of memory.
2085 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
2086
2087 **/
2088 EFI_STATUS
2089 MeasureLaunchOfFirmwareDebugger (
2090 VOID
2091 )
2092 {
2093 TCG_PCR_EVENT_HDR TcgEvent;
2094
2095 TcgEvent.PCRIndex = 7;
2096 TcgEvent.EventType = EV_EFI_ACTION;
2097 TcgEvent.EventSize = sizeof(FIRMWARE_DEBUGGER_EVENT_STRING) - 1;
2098 return TcgDxeHashLogExtendEvent (
2099 0,
2100 (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING,
2101 sizeof(FIRMWARE_DEBUGGER_EVENT_STRING) - 1,
2102 &TcgEvent,
2103 (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING
2104 );
2105 }
2106
2107 /**
2108 Measure and log all Secure Boot Policy, and extend the measurement result into a specific PCR.
2109
2110 Platform firmware adhering to the policy must therefore measure the following values into PCR[7]: (in order listed)
2111 - The contents of the SecureBoot variable
2112 - The contents of the PK variable
2113 - The contents of the KEK variable
2114 - The contents of the EFI_IMAGE_SECURITY_DATABASE variable
2115 - The contents of the EFI_IMAGE_SECURITY_DATABASE1 variable
2116 - Separator
2117 - Entries in the EFI_IMAGE_SECURITY_DATABASE that are used to validate EFI Drivers or EFI Boot Applications in the boot path
2118
2119 NOTE: Because of the above, UEFI variables PK, KEK, EFI_IMAGE_SECURITY_DATABASE,
2120 EFI_IMAGE_SECURITY_DATABASE1 and SecureBoot SHALL NOT be measured into PCR[3].
2121
2122 @param[in] Event Event whose notification function is being invoked
2123 @param[in] Context Pointer to the notification function's context
2124 **/
2125 VOID
2126 EFIAPI
2127 MeasureSecureBootPolicy (
2128 IN EFI_EVENT Event,
2129 IN VOID *Context
2130 )
2131 {
2132 EFI_STATUS Status;
2133 VOID *Protocol;
2134
2135 Status = gBS->LocateProtocol (&gEfiVariableWriteArchProtocolGuid, NULL, (VOID **)&Protocol);
2136 if (EFI_ERROR (Status)) {
2137 return;
2138 }
2139
2140 if (PcdGetBool (PcdFirmwareDebuggerInitialized)) {
2141 Status = MeasureLaunchOfFirmwareDebugger ();
2142 DEBUG ((EFI_D_INFO, "MeasureLaunchOfFirmwareDebugger - %r\n", Status));
2143 }
2144
2145 Status = MeasureAllSecureVariables ();
2146 DEBUG ((EFI_D_INFO, "MeasureAllSecureVariables - %r\n", Status));
2147
2148 //
2149 // We need measure Separator(7) here, because this event must be between SecureBootPolicy (Configure)
2150 // and ImageVerification (Authority)
2151 // There might be a case that we need measure UEFI image from DriverOrder, besides BootOrder. So
2152 // the Authority measurement happen before ReadToBoot event.
2153 //
2154 Status = MeasureSeparatorEvent (7);
2155 DEBUG ((EFI_D_INFO, "MeasureSeparatorEvent - %r\n", Status));
2156 return ;
2157 }
2158
2159 /**
2160 Ready to Boot Event notification handler.
2161
2162 Sequence of OS boot events is measured in this event notification handler.
2163
2164 @param[in] Event Event whose notification function is being invoked
2165 @param[in] Context Pointer to the notification function's context
2166
2167 **/
2168 VOID
2169 EFIAPI
2170 OnReadyToBoot (
2171 IN EFI_EVENT Event,
2172 IN VOID *Context
2173 )
2174 {
2175 EFI_STATUS Status;
2176 TPM_PCRINDEX PcrIndex;
2177
2178 PERF_START_EX (mImageHandle, "EventRec", "Tcg2Dxe", 0, PERF_ID_TCG2_DXE);
2179 if (mBootAttempts == 0) {
2180
2181 //
2182 // Measure handoff tables.
2183 //
2184 Status = MeasureHandoffTables ();
2185 if (EFI_ERROR (Status)) {
2186 DEBUG ((EFI_D_ERROR, "HOBs not Measured. Error!\n"));
2187 }
2188
2189 //
2190 // Measure BootOrder & Boot#### variables.
2191 //
2192 Status = MeasureAllBootVariables ();
2193 if (EFI_ERROR (Status)) {
2194 DEBUG ((EFI_D_ERROR, "Boot Variables not Measured. Error!\n"));
2195 }
2196
2197 //
2198 // 1. This is the first boot attempt.
2199 //
2200 Status = TcgMeasureAction (
2201 EFI_CALLING_EFI_APPLICATION
2202 );
2203 if (EFI_ERROR (Status)) {
2204 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION));
2205 }
2206
2207 //
2208 // 2. Draw a line between pre-boot env and entering post-boot env.
2209 // PCR[7] is already done.
2210 //
2211 for (PcrIndex = 0; PcrIndex < 7; PcrIndex++) {
2212 Status = MeasureSeparatorEvent (PcrIndex);
2213 if (EFI_ERROR (Status)) {
2214 DEBUG ((EFI_D_ERROR, "Seperator Event not Measured. Error!\n"));
2215 }
2216 }
2217
2218 //
2219 // 3. Measure GPT. It would be done in SAP driver.
2220 //
2221
2222 //
2223 // 4. Measure PE/COFF OS loader. It would be done in SAP driver.
2224 //
2225
2226 //
2227 // 5. Read & Measure variable. BootOrder already measured.
2228 //
2229 } else {
2230 //
2231 // 6. Not first attempt, meaning a return from last attempt
2232 //
2233 Status = TcgMeasureAction (
2234 EFI_RETURNING_FROM_EFI_APPLICATOIN
2235 );
2236 if (EFI_ERROR (Status)) {
2237 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_RETURNING_FROM_EFI_APPLICATOIN));
2238 }
2239 }
2240
2241 DEBUG ((EFI_D_INFO, "TPM2 Tcg2Dxe Measure Data when ReadyToBoot\n"));
2242 //
2243 // Increase boot attempt counter.
2244 //
2245 mBootAttempts++;
2246 PERF_END_EX (mImageHandle, "EventRec", "Tcg2Dxe", 0, PERF_ID_TCG2_DXE + 1);
2247 }
2248
2249 /**
2250 Exit Boot Services Event notification handler.
2251
2252 Measure invocation and success of ExitBootServices.
2253
2254 @param[in] Event Event whose notification function is being invoked
2255 @param[in] Context Pointer to the notification function's context
2256
2257 **/
2258 VOID
2259 EFIAPI
2260 OnExitBootServices (
2261 IN EFI_EVENT Event,
2262 IN VOID *Context
2263 )
2264 {
2265 EFI_STATUS Status;
2266
2267 //
2268 // Measure invocation of ExitBootServices,
2269 //
2270 Status = TcgMeasureAction (
2271 EFI_EXIT_BOOT_SERVICES_INVOCATION
2272 );
2273 if (EFI_ERROR (Status)) {
2274 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION));
2275 }
2276
2277 //
2278 // Measure success of ExitBootServices
2279 //
2280 Status = TcgMeasureAction (
2281 EFI_EXIT_BOOT_SERVICES_SUCCEEDED
2282 );
2283 if (EFI_ERROR (Status)) {
2284 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED));
2285 }
2286 }
2287
2288 /**
2289 Exit Boot Services Failed Event notification handler.
2290
2291 Measure Failure of ExitBootServices.
2292
2293 @param[in] Event Event whose notification function is being invoked
2294 @param[in] Context Pointer to the notification function's context
2295
2296 **/
2297 VOID
2298 EFIAPI
2299 OnExitBootServicesFailed (
2300 IN EFI_EVENT Event,
2301 IN VOID *Context
2302 )
2303 {
2304 EFI_STATUS Status;
2305
2306 //
2307 // Measure Failure of ExitBootServices,
2308 //
2309 Status = TcgMeasureAction (
2310 EFI_EXIT_BOOT_SERVICES_FAILED
2311 );
2312 if (EFI_ERROR (Status)) {
2313 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED));
2314 }
2315
2316 }
2317
2318 /**
2319 The function install Tcg2 protocol.
2320
2321 @retval EFI_SUCCESS Tcg2 protocol is installed.
2322 @retval other Some error occurs.
2323 **/
2324 EFI_STATUS
2325 InstallTcg2 (
2326 VOID
2327 )
2328 {
2329 EFI_STATUS Status;
2330 EFI_HANDLE Handle;
2331
2332 Handle = NULL;
2333 Status = gBS->InstallMultipleProtocolInterfaces (
2334 &Handle,
2335 &gEfiTcg2ProtocolGuid,
2336 &mTcg2Protocol,
2337 NULL
2338 );
2339 return Status;
2340 }
2341
2342 /**
2343 The driver's entry point. It publishes EFI Tcg2 Protocol.
2344
2345 @param[in] ImageHandle The firmware allocated handle for the EFI image.
2346 @param[in] SystemTable A pointer to the EFI System Table.
2347
2348 @retval EFI_SUCCESS The entry point is executed successfully.
2349 @retval other Some error occurs when executing this entry point.
2350 **/
2351 EFI_STATUS
2352 EFIAPI
2353 DriverEntry (
2354 IN EFI_HANDLE ImageHandle,
2355 IN EFI_SYSTEM_TABLE *SystemTable
2356 )
2357 {
2358 EFI_STATUS Status;
2359 EFI_EVENT Event;
2360 VOID *Registration;
2361 UINT32 MaxCommandSize;
2362 UINT32 MaxResponseSize;
2363 TPML_PCR_SELECTION Pcrs;
2364 UINTN Index;
2365 EFI_TCG2_EVENT_ALGORITHM_BITMAP TpmHashAlgorithmBitmap;
2366 UINT32 ActivePCRBanks;
2367 UINT32 NumberOfPCRBanks;
2368
2369 mImageHandle = ImageHandle;
2370
2371 if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceNoneGuid) ||
2372 CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){
2373 DEBUG ((EFI_D_ERROR, "No TPM2 instance required!\n"));
2374 return EFI_UNSUPPORTED;
2375 }
2376
2377 if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {
2378 DEBUG ((EFI_D_ERROR, "TPM2 error!\n"));
2379 return EFI_DEVICE_ERROR;
2380 }
2381
2382 Status = Tpm2RequestUseTpm ();
2383 if (EFI_ERROR (Status)) {
2384 DEBUG ((EFI_D_ERROR, "TPM2 not detected!\n"));
2385 return Status;
2386 }
2387
2388 //
2389 // Fill information
2390 //
2391 ASSERT (TCG_EVENT_LOG_AREA_COUNT_MAX == sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]));
2392
2393 mTcgDxeData.BsCap.Size = sizeof(EFI_TCG2_BOOT_SERVICE_CAPABILITY);
2394 mTcgDxeData.BsCap.ProtocolVersion.Major = 1;
2395 mTcgDxeData.BsCap.ProtocolVersion.Minor = 1;
2396 mTcgDxeData.BsCap.StructureVersion.Major = 1;
2397 mTcgDxeData.BsCap.StructureVersion.Minor = 1;
2398
2399 DEBUG ((EFI_D_INFO, "Tcg2.ProtocolVersion - %02x.%02x\n", mTcgDxeData.BsCap.ProtocolVersion.Major, mTcgDxeData.BsCap.ProtocolVersion.Minor));
2400 DEBUG ((EFI_D_INFO, "Tcg2.StructureVersion - %02x.%02x\n", mTcgDxeData.BsCap.StructureVersion.Major, mTcgDxeData.BsCap.StructureVersion.Minor));
2401
2402 Status = Tpm2GetCapabilityManufactureID (&mTcgDxeData.BsCap.ManufacturerID);
2403 if (EFI_ERROR (Status)) {
2404 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityManufactureID fail!\n"));
2405 } else {
2406 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityManufactureID - %08x\n", mTcgDxeData.BsCap.ManufacturerID));
2407 }
2408
2409 DEBUG_CODE (
2410 UINT32 FirmwareVersion1;
2411 UINT32 FirmwareVersion2;
2412
2413 Status = Tpm2GetCapabilityFirmwareVersion (&FirmwareVersion1, &FirmwareVersion2);
2414 if (EFI_ERROR (Status)) {
2415 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityFirmwareVersion fail!\n"));
2416 } else {
2417 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityFirmwareVersion - %08x %08x\n", FirmwareVersion1, FirmwareVersion2));
2418 }
2419 );
2420
2421 Status = Tpm2GetCapabilityMaxCommandResponseSize (&MaxCommandSize, &MaxResponseSize);
2422 if (EFI_ERROR (Status)) {
2423 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityMaxCommandResponseSize fail!\n"));
2424 } else {
2425 mTcgDxeData.BsCap.MaxCommandSize = (UINT16)MaxCommandSize;
2426 mTcgDxeData.BsCap.MaxResponseSize = (UINT16)MaxResponseSize;
2427 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityMaxCommandResponseSize - %08x, %08x\n", MaxCommandSize, MaxResponseSize));
2428 }
2429
2430 //
2431 // Get supported PCR and current Active PCRs
2432 //
2433 Status = Tpm2GetCapabilityPcrs (&Pcrs);
2434 if (EFI_ERROR (Status)) {
2435 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityPcrs fail!\n"));
2436 TpmHashAlgorithmBitmap = EFI_TCG2_BOOT_HASH_ALG_SHA1;
2437 ActivePCRBanks = EFI_TCG2_BOOT_HASH_ALG_SHA1;
2438 } else {
2439 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityPcrs Count - %08x\n", Pcrs.count));
2440 TpmHashAlgorithmBitmap = 0;
2441 ActivePCRBanks = 0;
2442 for (Index = 0; Index < Pcrs.count; Index++) {
2443 DEBUG ((EFI_D_INFO, "hash - %x\n", Pcrs.pcrSelections[Index].hash));
2444 switch (Pcrs.pcrSelections[Index].hash) {
2445 case TPM_ALG_SHA1:
2446 TpmHashAlgorithmBitmap |= EFI_TCG2_BOOT_HASH_ALG_SHA1;
2447 if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
2448 ActivePCRBanks |= EFI_TCG2_BOOT_HASH_ALG_SHA1;
2449 }
2450 break;
2451 case TPM_ALG_SHA256:
2452 TpmHashAlgorithmBitmap |= EFI_TCG2_BOOT_HASH_ALG_SHA256;
2453 if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
2454 ActivePCRBanks |= EFI_TCG2_BOOT_HASH_ALG_SHA256;
2455 }
2456 break;
2457 case TPM_ALG_SHA384:
2458 TpmHashAlgorithmBitmap |= EFI_TCG2_BOOT_HASH_ALG_SHA384;
2459 if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
2460 ActivePCRBanks |= EFI_TCG2_BOOT_HASH_ALG_SHA384;
2461 }
2462 break;
2463 case TPM_ALG_SHA512:
2464 TpmHashAlgorithmBitmap |= EFI_TCG2_BOOT_HASH_ALG_SHA512;
2465 if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
2466 ActivePCRBanks |= EFI_TCG2_BOOT_HASH_ALG_SHA512;
2467 }
2468 break;
2469 case TPM_ALG_SM3_256:
2470 TpmHashAlgorithmBitmap |= EFI_TCG2_BOOT_HASH_ALG_SM3_256;
2471 if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
2472 ActivePCRBanks |= EFI_TCG2_BOOT_HASH_ALG_SM3_256;
2473 }
2474 break;
2475 }
2476 }
2477 }
2478 mTcgDxeData.BsCap.HashAlgorithmBitmap = TpmHashAlgorithmBitmap & PcdGet32 (PcdTcg2HashAlgorithmBitmap);
2479 mTcgDxeData.BsCap.ActivePcrBanks = ActivePCRBanks & PcdGet32 (PcdTcg2HashAlgorithmBitmap);
2480
2481 //
2482 // Need calculate NumberOfPCRBanks here, because HashAlgorithmBitmap might be removed by PCD.
2483 //
2484 NumberOfPCRBanks = 0;
2485 for (Index = 0; Index < 32; Index++) {
2486 if ((mTcgDxeData.BsCap.HashAlgorithmBitmap & (1u << Index)) != 0) {
2487 NumberOfPCRBanks++;
2488 }
2489 }
2490
2491 if (PcdGet32 (PcdTcg2NumberOfPCRBanks) == 0) {
2492 mTcgDxeData.BsCap.NumberOfPCRBanks = NumberOfPCRBanks;
2493 } else {
2494 mTcgDxeData.BsCap.NumberOfPCRBanks = PcdGet32 (PcdTcg2NumberOfPCRBanks);
2495 if (PcdGet32 (PcdTcg2NumberOfPCRBanks) > NumberOfPCRBanks) {
2496 DEBUG ((EFI_D_ERROR, "ERROR: PcdTcg2NumberOfPCRBanks(0x%x) > NumberOfPCRBanks(0x%x)\n", PcdGet32 (PcdTcg2NumberOfPCRBanks), NumberOfPCRBanks));
2497 mTcgDxeData.BsCap.NumberOfPCRBanks = NumberOfPCRBanks;
2498 }
2499 }
2500
2501 mTcgDxeData.BsCap.SupportedEventLogs = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2 | EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
2502 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA1) == 0) {
2503 //
2504 // No need to expose TCG1.2 event log if SHA1 bank does not exist.
2505 //
2506 mTcgDxeData.BsCap.SupportedEventLogs &= ~EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
2507 }
2508
2509 DEBUG ((EFI_D_INFO, "Tcg2.SupportedEventLogs - 0x%08x\n", mTcgDxeData.BsCap.SupportedEventLogs));
2510 DEBUG ((EFI_D_INFO, "Tcg2.HashAlgorithmBitmap - 0x%08x\n", mTcgDxeData.BsCap.HashAlgorithmBitmap));
2511 DEBUG ((EFI_D_INFO, "Tcg2.NumberOfPCRBanks - 0x%08x\n", mTcgDxeData.BsCap.NumberOfPCRBanks));
2512 DEBUG ((EFI_D_INFO, "Tcg2.ActivePcrBanks - 0x%08x\n", mTcgDxeData.BsCap.ActivePcrBanks));
2513
2514 if (mTcgDxeData.BsCap.TPMPresentFlag) {
2515 //
2516 // Setup the log area and copy event log from hob list to it
2517 //
2518 Status = SetupEventLog ();
2519 ASSERT_EFI_ERROR (Status);
2520
2521 //
2522 // Measure handoff tables, Boot#### variables etc.
2523 //
2524 Status = EfiCreateEventReadyToBootEx (
2525 TPL_CALLBACK,
2526 OnReadyToBoot,
2527 NULL,
2528 &Event
2529 );
2530
2531 Status = gBS->CreateEventEx (
2532 EVT_NOTIFY_SIGNAL,
2533 TPL_NOTIFY,
2534 OnExitBootServices,
2535 NULL,
2536 &gEfiEventExitBootServicesGuid,
2537 &Event
2538 );
2539
2540 //
2541 // Measure Exit Boot Service failed
2542 //
2543 Status = gBS->CreateEventEx (
2544 EVT_NOTIFY_SIGNAL,
2545 TPL_NOTIFY,
2546 OnExitBootServicesFailed,
2547 NULL,
2548 &gEventExitBootServicesFailedGuid,
2549 &Event
2550 );
2551
2552 //
2553 // Create event callback, because we need access variable on SecureBootPolicyVariable
2554 // We should use VariableWriteArch instead of VariableArch, because Variable driver
2555 // may update SecureBoot value based on last setting.
2556 //
2557 EfiCreateProtocolNotifyEvent (&gEfiVariableWriteArchProtocolGuid, TPL_CALLBACK, MeasureSecureBootPolicy, NULL, &Registration);
2558 }
2559
2560 //
2561 // Install Tcg2Protocol
2562 //
2563 Status = InstallTcg2 ();
2564 DEBUG ((EFI_D_INFO, "InstallTcg2 - %r\n", Status));
2565
2566 return Status;
2567 }