1 /** @file
2   This library is used by other modules to measure data to TPM.
3 
4 Copyright (c) 2020, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include <PiPei.h>
10 
11 #include <Library/BaseMemoryLib.h>
12 #include <Library/PeiServicesLib.h>
13 #include <Library/PeiServicesTablePointerLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/HobLib.h>
16 #include <Library/TpmMeasurementLib.h>
17 
18 #include <Ppi/Tcg.h>
19 #include <IndustryStandard/UefiTcgPlatform.h>
20 
21 /**
22   Tpm measure and log data, and extend the measurement result into a specific PCR.
23 
24   @param[in]  PcrIndex         PCR Index.
25   @param[in]  EventType        Event type.
26   @param[in]  EventLog         Measurement event log.
27   @param[in]  LogLen           Event log length in bytes.
28   @param[in]  HashData         The start of the data buffer to be hashed, extended.
29   @param[in]  HashDataLen      The length, in bytes, of the buffer referenced by HashData
30 
31   @retval EFI_SUCCESS           Operation completed successfully.
32   @retval EFI_UNSUPPORTED       TPM device not available.
33   @retval EFI_OUT_OF_RESOURCES  Out of memory.
34   @retval EFI_DEVICE_ERROR      The operation was unsuccessful.
35 **/
36 EFI_STATUS
37 EFIAPI
TpmMeasureAndLogData(IN UINT32 PcrIndex,IN UINT32 EventType,IN VOID * EventLog,IN UINT32 LogLen,IN VOID * HashData,IN UINT64 HashDataLen)38 TpmMeasureAndLogData (
39   IN UINT32             PcrIndex,
40   IN UINT32             EventType,
41   IN VOID               *EventLog,
42   IN UINT32             LogLen,
43   IN VOID               *HashData,
44   IN UINT64             HashDataLen
45   )
46 {
47   EFI_STATUS                Status;
48   EDKII_TCG_PPI             *TcgPpi;
49   TCG_PCR_EVENT_HDR         TcgEventHdr;
50 
51   Status = PeiServicesLocatePpi (
52              &gEdkiiTcgPpiGuid,
53              0,
54              NULL,
55              (VOID**)&TcgPpi
56              );
57   if (EFI_ERROR(Status)) {
58     return Status;
59   }
60 
61   TcgEventHdr.PCRIndex  = PcrIndex;
62   TcgEventHdr.EventType = EventType;
63   TcgEventHdr.EventSize = LogLen;
64 
65   Status = TcgPpi->HashLogExtendEvent (
66                      TcgPpi,
67                      0,
68                      HashData,
69                      (UINTN)HashDataLen,
70                      &TcgEventHdr,
71                      EventLog
72                      );
73   return Status;
74 }
75