1 /** @file
2   Implement TPM1.2 PCR related commands.
3 
4 Copyright (c) 2016, Intel Corporation. All rights reserved. <BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution.  The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9 
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #include <PiPei.h>
16 #include <Library/Tpm12CommandLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/Tpm12DeviceLib.h>
21 
22 #pragma pack(1)
23 
24 typedef struct {
25   TPM_RQU_COMMAND_HDR   Hdr;
26   TPM_PCRINDEX          PcrIndex;
27   TPM_DIGEST            TpmDigest;
28 } TPM_CMD_EXTEND;
29 
30 typedef struct {
31   TPM_RSP_COMMAND_HDR   Hdr;
32   TPM_DIGEST            TpmDigest;
33 } TPM_RSP_EXTEND;
34 
35 #pragma pack()
36 
37 /**
38 Extend a TPM PCR.
39 
40 @param[in]  DigestToExtend    The 160 bit value representing the event to be recorded.
41 @param[in]  PcrIndex          The PCR to be updated.
42 @param[out] NewPcrValue       New PCR value after extend.
43 
44 @retval EFI_SUCCESS           Operation completed successfully.
45 @retval EFI_TIMEOUT           The register can't run into the expected status in time.
46 @retval EFI_BUFFER_TOO_SMALL  Response data buffer is too small.
47 @retval EFI_DEVICE_ERROR      Unexpected device behavior.
48 
49 **/
50 EFI_STATUS
51 EFIAPI
Tpm12Extend(IN TPM_DIGEST * DigestToExtend,IN TPM_PCRINDEX PcrIndex,OUT TPM_DIGEST * NewPcrValue)52 Tpm12Extend (
53   IN  TPM_DIGEST    *DigestToExtend,
54   IN  TPM_PCRINDEX  PcrIndex,
55   OUT TPM_DIGEST    *NewPcrValue
56   )
57 {
58   EFI_STATUS      Status;
59   TPM_CMD_EXTEND  Command;
60   TPM_RSP_EXTEND  Response;
61   UINT32          Length;
62 
63   //
64   // send Tpm command TPM_ORD_Extend
65   //
66   Command.Hdr.tag       = SwapBytes16 (TPM_TAG_RQU_COMMAND);
67   Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));
68   Command.Hdr.ordinal   = SwapBytes32 (TPM_ORD_Extend);
69   Command.PcrIndex      = SwapBytes32 (PcrIndex);
70   CopyMem (&Command.TpmDigest, DigestToExtend, sizeof (Command.TpmDigest));
71   Length = sizeof (Response);
72   Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);
73   if (EFI_ERROR (Status)) {
74     return Status;
75   }
76 
77   if (SwapBytes32(Response.Hdr.returnCode) != TPM_SUCCESS) {
78     DEBUG ((EFI_D_ERROR, "Tpm12Extend: Response Code error! 0x%08x\r\n", SwapBytes32(Response.Hdr.returnCode)));
79     return EFI_DEVICE_ERROR;
80   }
81 
82   if (NewPcrValue != NULL) {
83     CopyMem (NewPcrValue, &Response.TpmDigest, sizeof (*NewPcrValue));
84   }
85 
86   return Status;
87 }
88