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