1 /** @file
2   Implement TPM1.2 PCR related commands.
3 
4 Copyright (c) 2016, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include <PiPei.h>
10 #include <Library/Tpm12CommandLib.h>
11 #include <Library/BaseLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/Tpm12DeviceLib.h>
15 
16 #pragma pack(1)
17 
18 typedef struct {
19   TPM_RQU_COMMAND_HDR   Hdr;
20   TPM_PCRINDEX          PcrIndex;
21   TPM_DIGEST            TpmDigest;
22 } TPM_CMD_EXTEND;
23 
24 typedef struct {
25   TPM_RSP_COMMAND_HDR   Hdr;
26   TPM_DIGEST            TpmDigest;
27 } TPM_RSP_EXTEND;
28 
29 #pragma pack()
30 
31 /**
32 Extend a TPM PCR.
33 
34 @param[in]  DigestToExtend    The 160 bit value representing the event to be recorded.
35 @param[in]  PcrIndex          The PCR to be updated.
36 @param[out] NewPcrValue       New PCR value after extend.
37 
38 @retval EFI_SUCCESS           Operation completed successfully.
39 @retval EFI_TIMEOUT           The register can't run into the expected status in time.
40 @retval EFI_BUFFER_TOO_SMALL  Response data buffer is too small.
41 @retval EFI_DEVICE_ERROR      Unexpected device behavior.
42 
43 **/
44 EFI_STATUS
45 EFIAPI
Tpm12Extend(IN TPM_DIGEST * DigestToExtend,IN TPM_PCRINDEX PcrIndex,OUT TPM_DIGEST * NewPcrValue)46 Tpm12Extend (
47   IN  TPM_DIGEST    *DigestToExtend,
48   IN  TPM_PCRINDEX  PcrIndex,
49   OUT TPM_DIGEST    *NewPcrValue
50   )
51 {
52   EFI_STATUS      Status;
53   TPM_CMD_EXTEND  Command;
54   TPM_RSP_EXTEND  Response;
55   UINT32          Length;
56 
57   //
58   // send Tpm command TPM_ORD_Extend
59   //
60   Command.Hdr.tag       = SwapBytes16 (TPM_TAG_RQU_COMMAND);
61   Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));
62   Command.Hdr.ordinal   = SwapBytes32 (TPM_ORD_Extend);
63   Command.PcrIndex      = SwapBytes32 (PcrIndex);
64   CopyMem (&Command.TpmDigest, DigestToExtend, sizeof (Command.TpmDigest));
65   Length = sizeof (Response);
66   Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);
67   if (EFI_ERROR (Status)) {
68     return Status;
69   }
70 
71   if (SwapBytes32(Response.Hdr.returnCode) != TPM_SUCCESS) {
72     DEBUG ((EFI_D_ERROR, "Tpm12Extend: Response Code error! 0x%08x\r\n", SwapBytes32(Response.Hdr.returnCode)));
73     return EFI_DEVICE_ERROR;
74   }
75 
76   if (NewPcrValue != NULL) {
77     CopyMem (NewPcrValue, &Response.TpmDigest, sizeof (*NewPcrValue));
78   }
79 
80   return Status;
81 }
82