blob: 492ea7c23447756307cd170ab5c4f7ceb2084a5d [file] [log] [blame]
Vadim Bendebury56797522015-05-20 10:32:25 -07001// This file was extracted from the TCG Published
2// Trusted Platform Module Library
3// Part 4: Supporting Routines
4// Family "2.0"
5// Level 00 Revision 01.16
6// October 30, 2014
7
8#define _SWAP_H // Preclude inclusion of unnecessary simulator header
9#include <stdlib.h>
10#include <stdio.h>
11#include <stdint.h>
12#include <setjmp.h>
13#include "bool.h"
14#include "Platform.h"
15#include "ExecCommand_fp.h"
16#include "Manufacture_fp.h"
17#include "DRTM_fp.h"
18#include "_TPM_Init_fp.h"
19#include "TpmFail_fp.h"
20#include <windows.h>
21#include "TpmTcpProtocol.h"
22static BOOL s_isPowerOn = FALSE;
23//
24//
25// Functions
26//
27// Signal_PowerOn()
28//
29// This function processes a power-on indicataion. Amoung other things, it calls the _TPM_Init() hangler.
30//
31void
32_rpc__Signal_PowerOn(
33 BOOL isReset
34 )
35{
36 // if power is on and this is not a call to do TPM reset then return
37 if(s_isPowerOn && !isReset)
38 return;
39 // If this is a reset but power is not on, then return
40 if(isReset && !s_isPowerOn)
41 return;
42 // Pass power on signal to platform
43 if(isReset)
44 _plat__Signal_Reset();
45 else
46 _plat__Signal_PowerOn();
47 // Pass power on signal to TPM
48 _TPM_Init();
49 // Set state as power on
50 s_isPowerOn = TRUE;
51}
52//
53//
54// Signal_PowerOff()
55//
56// This function processes the power off indication. Its primary funtion is to set a flag indicating that the next
57// power on indication should cause _TPM_Init() to be called.
58//
59void
60_rpc__Signal_PowerOff(
61 void
62 )
63{
64 if(!s_isPowerOn) return;
65 // Pass power off signal to platform
66 _plat__Signal_PowerOff();
67 s_isPowerOn = FALSE;
68 return;
69}
70//
71//
72// _rpc__ForceFailureMode()
73//
74// This function is used to debug the Failure Mode logic of the TPM. It will set a flag in the TPM code such
75// that the next call to TPM2_SelfTest() will result in a failure, putting the TPM into Failure Mode.
76//
77void
78_rpc__ForceFailureMode(
79 void
80 )
81{
82 SetForceFailureMode();
83}
84//
85//
86// _rpc__Signal_PhysicalPresenceOn()
87//
88// This function is called to simulate activation of the physical presence pin.
89//
90void
91_rpc__Signal_PhysicalPresenceOn(
92 void
93 )
94{
95 // If TPM is power off, reject this signal
96 if(!s_isPowerOn) return;
97 // Pass physical presence on to platform
98 _plat__Signal_PhysicalPresenceOn();
99 return;
100}
101//
102//
103// _rpc__Signal_PhysicalPresenceOff()
104//
105// This function is called to simulate deactivation of the physical presence pin.
106//
107void
108_rpc__Signal_PhysicalPresenceOff(
109 void
110 )
111{
112 // If TPM is power off, reject this signal
113 if(!s_isPowerOn) return;
114 // Pass physical presence off to platform
115 _plat__Signal_PhysicalPresenceOff();
116 return;
117}
118//
119//
120// _rpc__Signal_Hash_Start()
121//
122// This function is called to simulate a _TPM_Hash_Start() event. It will call
123//
124void
125_rpc__Signal_Hash_Start(
126 void
127 )
128{
129 // If TPM is power off, reject this signal
130 if(!s_isPowerOn) return;
131 // Pass _TPM_Hash_Start signal to TPM
132 Signal_Hash_Start();
133 return;
134}
135//
136//
137// _rpc__Signal_Hash_Data()
138//
139// This function is called to simulate a _TPM_Hash_Data() event.
140//
141void
142_rpc__Signal_Hash_Data(
143 _IN_BUFFER input
144 )
145{
146 // If TPM is power off, reject this signal
147 if(!s_isPowerOn) return;
148 // Pass _TPM_Hash_Data signal to TPM
149 Signal_Hash_Data(input.BufferSize, input.Buffer);
150 return;
151}
152//
153//
154// _rpc__Signal_HashEnd()
155//
156// This function is called to simulate a _TPM_Hash_End() event.
157//
158void
159_rpc__Signal_HashEnd(
160 void
161 )
162{
163 // If TPM is power off, reject this signal
164 if(!s_isPowerOn) return;
165 // Pass _TPM_HashEnd signal to TPM
166 Signal_Hash_End();
167 return;
168}
169//
170// Command interface Entry of a RPC call
171void
172_rpc__Send_Command(
173 unsigned char locality,
174 _IN_BUFFER request,
175 _OUT_BUFFER *response
176 )
177{
178 // If TPM is power off, reject any commands.
179 if(!s_isPowerOn) {
180 response->BufferSize = 0;
181 return;
182 }
183 // Set the locality of the command so that it doesn't change during the command
184 _plat__LocalitySet(locality);
185 // Do implementation-specific command dispatch
186 ExecuteCommand(request.BufferSize, request.Buffer,
187 &response->BufferSize, &response->Buffer);
188 return;
189}
190//
191//
192// _rpc__Signal_CancelOn()
193//
194// This function is used to turn on the indication to cancel a command in process. An executing command is
195// not interrupted. The command code may perodically check this indication to see if it should abort the
196// current command processing and returned TPM_RC_CANCELLED.
197//
198void
199_rpc__Signal_CancelOn(
200 void
201 )
202{
203 // If TPM is power off, reject this signal
204 if(!s_isPowerOn) return;
205 // Set the platform canceling flag.
206 _plat__SetCancel();
207 return;
208}
209//
210//
211// _rpc__Signal_CancelOff()
212//
213// This function is used to turn off the indication to cancel a command in process.
214//
215void
216_rpc__Signal_CancelOff(
217 void
218 )
219{
220 // If TPM is power off, reject this signal
221 if(!s_isPowerOn) return;
222 // Set the platform canceling flag.
223 _plat__ClearCancel();
224 return;
225}
226//
227//
228//
229// _rpc__Signal_NvOn()
230//
231// In a system where the NV memory used by the TPM is not within the TPM, the NV may not always be
232// available. This function turns on the indicator that indicates that NV is available.
233//
234void
235_rpc__Signal_NvOn(
236 void
237 )
238{
239 // If TPM is power off, reject this signal
240 if(!s_isPowerOn) return;
241 _plat__SetNvAvail();
242 return;
243}
244//
245//
246// _rpc__Signal_NvOff()
247//
248// This function is used to set the indication that NV memory is no longer available.
249//
250void
251_rpc__Signal_NvOff(
252 void
253 )
254{
255 // If TPM is power off, reject this signal
256 if(!s_isPowerOn) return;
257 _plat__ClearNvAvail();
258 return;
259}
260//
261//
262// _rpc__Shutdown()
263//
264// This function is used to stop the TPM simulator.
265//
266void
267_rpc__Shutdown(
268 void
269 )
270{
271 RPC_STATUS status;
272 // Stop TPM
273 TPM_TearDown();
274 status = RpcMgmtStopServerListening(NULL);
275 if (status != RPC_S_OK)
276 {
277 printf_s("RpcMgmtStopServerListening returned: 0x%x\n", status);
278 exit(status);
279 }
280 status = RpcServerUnregisterIf(NULL, NULL, FALSE);
281 if (status != RPC_S_OK)
282 {
283 printf_s("RpcServerUnregisterIf returned 0x%x\n", status);
284 exit(status);
285 }
286}