1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2024, Linaro Limited
4  */
5 /* Microsoft Reference Implementation for TPM 2.0
6  *
7  * The copyright in this software is being made available under the BSD
8  * License, included below. This software may be subject to other third
9  * party and contributor rights, including patent rights, and no such
10  * rights are granted under this license.
11  *
12  * Copyright (c) 2018 Microsoft Corporation
13  *
14  * All rights reserved.
15  *
16  * BSD License
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are
20  * met:
21  *
22  * Redistributions of source code must retain the above copyright notice,
23  * this list of conditions and the following disclaimer.
24  *
25  * Redistributions in binary form must reproduce the above copyright
26  * notice, this list of conditions and the following disclaimer in the
27  * documentation and/or other materials provided with the distribution.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
30  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
32  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
35  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 //**Introduction
43 /*
44     This file contains the admin interfaces.
45 */
46 
47 #ifndef _ADMIN_H
48 #define _ADMIN_H
49 
50 //**Includes
51 #include <stdint.h>
52 #include <trace.h>
53 #include "swap.h"
54 #include "TpmProfile.h"
55 #include "TpmSal.h"
56 #include "TpmError.h"
57 #include "GpMacros.h"
58 
59 // Parameter reference and types from ref impl headers
60 #ifndef UNREFERENCED_PARAMETER
61 #define UNREFERENCED_PARAMETER(a) do { (void)(a); } while (0)
62 #endif
63 
64 #if defined(__GNUC__)
65 typedef unsigned char   UCHAR;
66 typedef unsigned char * PUCHAR;
67 typedef void            VOID;
68 typedef void *          PVOID;
69 #endif
70 
71 // Admin space tacked on to NV, padded out to NV_BLOCK_SIZE alignment.
72 #define NV_TPM_STATE_SIZE   0x200
73 
74 // Actual size of Admin space used. (See note in NVMem.c)
75 #define TPM_STATE_SIZE      0x10
76 
77 // Select TPM types/defines for AdminPPI.c
78 typedef UINT16  TPM_ST;
79 #define TPM_ST_NO_SESSIONS  (TPM_ST)(0x8001)
80 
81 typedef UINT32  TPM_RC;
82 #define TPM_RC_SUCCESS      (TPM_RC)(0x000)
83 #define RC_VER1             (TPM_RC)(0x100)
84 #define TPM_RC_BAD_TAG      (TPM_RC)(0x01E)
85 
86 // Chip flags
87 typedef union {
88     UINT32   flags;
89     struct {
90         UINT32 Remanufacture   : 1;  // Ignored on OpTEE platforms
91         UINT32 TpmStatePresent : 1;  // Set when sate present (startup STATE)
92         UINT32 Reserved        : 30;
93     }        fields;
94 } TPM_CHIP_STATE;
95 
96 //
97 // The current NV Chip state
98 //
99 extern TPM_CHIP_STATE g_chipFlags;
100 
101 //
102 // Simulated Physical Presence Interface (PPI)
103 //
104 #define FTPM_PPI_CMD_QUERY               0
105 #define FTPM_PPI_CMD_VERSION             1
106 #define FTPM_PPI_CMD_SUBMIT_OP_REQ       2
107 #define FTPM_PPI_CMD_GET_PENDING_OP      3
108 #define FTPM_PPI_CMD_GET_PLATFORM_ACTION 4
109 #define FTPM_PPI_CMD_RETURN_OP_RESP      5
110 #define FTPM_PPI_CMD_SUBMIT_USER_LANG    6
111 #define FTPM_PPI_CMD_SUBMIT_OP_REQ2      7
112 #define FTPM_PPI_CMD_GET_USER_CONF       8
113 
114 #define FTPM_PPI_OP_NOP                  0
115 #define FTPM_PPI_OP_ENABLE               1
116 #define FTPM_PPI_OP_DISABLE              2
117 #define FTPM_PPI_OP_ACTIVATE             3
118 #define FTPM_PPI_OP_DEACTIVATE           4
119 #define FTPM_PPI_OP_CLEAR                5
120 #define FTPM_PPI_OP_E_A                  6
121 #define FTPM_PPI_OP_D_D                  7
122 #define FTPM_PPI_OP_OWNERINSTALL_TRUE    8
123 #define FTPM_PPI_OP_OWNERINSTALL_FALSE   9
124 #define FTPM_PPI_OP_E_A_OI_TRUE         10
125 #define FTPM_PPI_OP_OI_FALSE_D_D        11
126 #define FTPM_PPI_OP_FIELD_UPGRADE       12
127 #define FTPM_PPI_OP_OPERATOR_AUTH       13
128 #define FTPM_PPI_OP_C_E_A               14
129 #define FTPM_PPI_OP_SET_NO_PROV_FALSE   15
130 #define FTPM_PPI_OP_SET_NO_PROV_TRUE    16
131 #define FTPM_PPI_OP_SET_NO_CLEAR_FALSE  17
132 #define FTPM_PPI_OP_SET_NO_CLEAR_TRUE   18
133 #define FTPM_PPI_OP_SET_NO_MAINT_FALSE  19
134 #define FTPM_PPI_OP_SET_NO_MAINT_TRUE   20
135 #define FTPM_PPI_OP_E_A_C               21
136 #define FTPM_PPI_OP_E_A_C_E_A           22
137 #define FTPM_PPI_OP_RESERVED_FIRST      23
138 #define FTPM_PPI_OP_RESERVED_LAST      127
139 #define FTPM_PPI_OP_VENDOR_FIRST       128
140 
141 #define FTPM_PPI_VERSION               0x00322E31 // "1.2"
142 
143 #define FTPM_PPI_OP_NOT_IMPLEMENTED    0xFFFFFFFF // Any Op other than E_A_C_E_A
144 
145 #pragma pack(1)
146 typedef struct {
147     UINT32 PendingPseudoOp;
148     UINT32 PseudoOpFromLastBoot;
149     UINT32 ReturnResponse;
150 } FTPM_PPI_STATE;
151 #pragma pack()
152 
153 //
154 // The types of TPM runtime state stored to NV
155 //
156 typedef enum {
157     NV_TPM_STATE_FLAGS = 0,
158     NV_TPM_STATE_PPI,
159     NV_TPM_STATE_LAST           // A mark of the end of the TPM state
160 } NV_TPM_STATE;
161 
162 //***_admin__NvInitState()
163 // Initialize the NV admin state
164 void
165 _admin__NvInitState();
166 
167 //***_admin__NvReadState()
168 // Read TPM state data from NV memory to RAM
169 void
170 _admin__NvReadState(
171     NV_TPM_STATE     type,    // IN: type of state data
172     void             *buffer  // OUT: data buffer
173     );
174 
175 //***_admin__NvWriteState()
176 // Write TPM state data to NV memory
177 void
178 _admin__NvWriteState(
179     NV_TPM_STATE     type,    // IN: type of state data
180     void             *buffer  // IN: data buffer
181     );
182 
183 //
184 // Save and restore runtime state
185 //
186 
187 
188 //***_admin__SaveChipFlags()
189 // Save the g_chipFlags runtime state
190 void
191 _admin__SaveChipFlags();
192 
193 //***_admin__RestoreChipFlags()
194 // Restore the g_chipFlags runtime state
195 void
196 _admin__RestoreChipFlags();
197 
198 //***_admin__SavePPIState()
199 // Save the s_PPIState runtime state
200 void
201 _admin__SavePPIState();
202 
203 //***_admin__RestorePPIState()
204 // Restore the s_PPIState runtime state
205 void
206 _admin__RestorePPIState();
207 
208 //***_admin__PPICommand()
209 // Returns 1 when PPI command has been consumed
210 // Returns 0 when it is not a properly formated PPI command,
211 //           caller should pass through to TPM
212 //
213 int
214 _admin__PPICommand(
215     UINT32 CommandSize,
216     __in_ecount(CommandSize) UINT8 *CommandBuffer,
217     UINT32 *ResponseSize,
218     __deref_out_ecount(*ResponseSize) UINT8 **ResponseBuffer
219 );
220 
221 #endif
222