xref: /aosp_15_r20/external/tpm2-tss/src/tss2-esys/api/Esys_Startup.c (revision 758e9fba6fc9adbf15340f70c73baee7b168b1c9)
1*758e9fbaSOystein Eftevaag /* SPDX-License-Identifier: BSD-2-Clause */
2*758e9fbaSOystein Eftevaag /*******************************************************************************
3*758e9fbaSOystein Eftevaag  * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4*758e9fbaSOystein Eftevaag  * All rights reserved.
5*758e9fbaSOystein Eftevaag  ******************************************************************************/
6*758e9fbaSOystein Eftevaag 
7*758e9fbaSOystein Eftevaag #ifdef HAVE_CONFIG_H
8*758e9fbaSOystein Eftevaag #include <config.h>
9*758e9fbaSOystein Eftevaag #endif
10*758e9fbaSOystein Eftevaag 
11*758e9fbaSOystein Eftevaag #include "tss2_mu.h"
12*758e9fbaSOystein Eftevaag #include "tss2_sys.h"
13*758e9fbaSOystein Eftevaag #include "tss2_esys.h"
14*758e9fbaSOystein Eftevaag 
15*758e9fbaSOystein Eftevaag #include "esys_types.h"
16*758e9fbaSOystein Eftevaag #include "esys_iutil.h"
17*758e9fbaSOystein Eftevaag #include "esys_mu.h"
18*758e9fbaSOystein Eftevaag #define LOGMODULE esys
19*758e9fbaSOystein Eftevaag #include "util/log.h"
20*758e9fbaSOystein Eftevaag #include "util/aux_util.h"
21*758e9fbaSOystein Eftevaag 
22*758e9fbaSOystein Eftevaag /** One-Call function for TPM2_Startup
23*758e9fbaSOystein Eftevaag  *
24*758e9fbaSOystein Eftevaag  * This function invokes the TPM2_Startup command in a one-call
25*758e9fbaSOystein Eftevaag  * variant. This means the function will block until the TPM response is
26*758e9fbaSOystein Eftevaag  * available. All input parameters are const. The memory for non-simple output
27*758e9fbaSOystein Eftevaag  * parameters is allocated by the function implementation.
28*758e9fbaSOystein Eftevaag  *
29*758e9fbaSOystein Eftevaag  * @param[in,out] esysContext The ESYS_CONTEXT.
30*758e9fbaSOystein Eftevaag  * @param[in]  startupType TPM2_SU_CLEAR or TPM2_SU_STATE.
31*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS if the function call was a success.
32*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
33*758e9fbaSOystein Eftevaag  *         pointers or required output handle references are NULL.
34*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
35*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
36*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
37*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
38*758e9fbaSOystein Eftevaag  *         operation already pending.
39*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
40*758e9fbaSOystein Eftevaag  *          at least contain the tag, response length, and response code.
41*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
42*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
43*758e9fbaSOystein Eftevaag            did not verify.
44*758e9fbaSOystein Eftevaag  * @retval TSS2_RCs produced by lower layers of the software stack may be
45*758e9fbaSOystein Eftevaag  *         returned to the caller unaltered unless handled internally.
46*758e9fbaSOystein Eftevaag  */
47*758e9fbaSOystein Eftevaag TSS2_RC
Esys_Startup(ESYS_CONTEXT * esysContext,TPM2_SU startupType)48*758e9fbaSOystein Eftevaag Esys_Startup(
49*758e9fbaSOystein Eftevaag     ESYS_CONTEXT *esysContext,
50*758e9fbaSOystein Eftevaag     TPM2_SU startupType)
51*758e9fbaSOystein Eftevaag {
52*758e9fbaSOystein Eftevaag     TSS2_RC r;
53*758e9fbaSOystein Eftevaag 
54*758e9fbaSOystein Eftevaag     r = Esys_Startup_Async(esysContext, startupType);
55*758e9fbaSOystein Eftevaag     return_if_error(r, "Error in async function");
56*758e9fbaSOystein Eftevaag 
57*758e9fbaSOystein Eftevaag     /* Set the timeout to indefinite for now, since we want _Finish to block */
58*758e9fbaSOystein Eftevaag     int32_t timeouttmp = esysContext->timeout;
59*758e9fbaSOystein Eftevaag     esysContext->timeout = -1;
60*758e9fbaSOystein Eftevaag     /*
61*758e9fbaSOystein Eftevaag      * Now we call the finish function, until return code is not equal to
62*758e9fbaSOystein Eftevaag      * from TSS2_BASE_RC_TRY_AGAIN.
63*758e9fbaSOystein Eftevaag      * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
64*758e9fbaSOystein Eftevaag      * have set the timeout to -1. This occurs for example if the TPM requests
65*758e9fbaSOystein Eftevaag      * a retransmission of the command via TPM2_RC_YIELDED.
66*758e9fbaSOystein Eftevaag      */
67*758e9fbaSOystein Eftevaag     do {
68*758e9fbaSOystein Eftevaag         r = Esys_Startup_Finish(esysContext);
69*758e9fbaSOystein Eftevaag         /* This is just debug information about the reattempt to finish the
70*758e9fbaSOystein Eftevaag            command */
71*758e9fbaSOystein Eftevaag         if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
72*758e9fbaSOystein Eftevaag             LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
73*758e9fbaSOystein Eftevaag                       " => resubmitting command", r);
74*758e9fbaSOystein Eftevaag     } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
75*758e9fbaSOystein Eftevaag 
76*758e9fbaSOystein Eftevaag     /* Restore the timeout value to the original value */
77*758e9fbaSOystein Eftevaag     esysContext->timeout = timeouttmp;
78*758e9fbaSOystein Eftevaag     return_if_error(r, "Esys Finish");
79*758e9fbaSOystein Eftevaag 
80*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
81*758e9fbaSOystein Eftevaag }
82*758e9fbaSOystein Eftevaag 
83*758e9fbaSOystein Eftevaag /** Asynchronous function for TPM2_Startup
84*758e9fbaSOystein Eftevaag  *
85*758e9fbaSOystein Eftevaag  * This function invokes the TPM2_Startup command in a asynchronous
86*758e9fbaSOystein Eftevaag  * variant. This means the function will return as soon as the command has been
87*758e9fbaSOystein Eftevaag  * sent downwards the stack to the TPM. All input parameters are const.
88*758e9fbaSOystein Eftevaag  * In order to retrieve the TPM's response call Esys_Startup_Finish.
89*758e9fbaSOystein Eftevaag  *
90*758e9fbaSOystein Eftevaag  * @param[in,out] esysContext The ESYS_CONTEXT.
91*758e9fbaSOystein Eftevaag  * @param[in]  startupType TPM2_SU_CLEAR or TPM2_SU_STATE.
92*758e9fbaSOystein Eftevaag  * @retval ESYS_RC_SUCCESS if the function call was a success.
93*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
94*758e9fbaSOystein Eftevaag  *         pointers or required output handle references are NULL.
95*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
96*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
97*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
98*758e9fbaSOystein Eftevaag  * @retval TSS2_RCs produced by lower layers of the software stack may be
99*758e9fbaSOystein Eftevaag            returned to the caller unaltered unless handled internally.
100*758e9fbaSOystein Eftevaag  */
101*758e9fbaSOystein Eftevaag TSS2_RC
Esys_Startup_Async(ESYS_CONTEXT * esysContext,TPM2_SU startupType)102*758e9fbaSOystein Eftevaag Esys_Startup_Async(
103*758e9fbaSOystein Eftevaag     ESYS_CONTEXT *esysContext,
104*758e9fbaSOystein Eftevaag     TPM2_SU startupType)
105*758e9fbaSOystein Eftevaag {
106*758e9fbaSOystein Eftevaag     TSS2_RC r;
107*758e9fbaSOystein Eftevaag     LOG_TRACE("context=%p, startupType=%04"PRIx16"",
108*758e9fbaSOystein Eftevaag               esysContext, startupType);
109*758e9fbaSOystein Eftevaag 
110*758e9fbaSOystein Eftevaag     /* Check context, sequence correctness and set state to error for now */
111*758e9fbaSOystein Eftevaag     if (esysContext == NULL) {
112*758e9fbaSOystein Eftevaag         LOG_ERROR("esyscontext is NULL.");
113*758e9fbaSOystein Eftevaag         return TSS2_ESYS_RC_BAD_REFERENCE;
114*758e9fbaSOystein Eftevaag     }
115*758e9fbaSOystein Eftevaag     r = iesys_check_sequence_async(esysContext);
116*758e9fbaSOystein Eftevaag     if (r != TSS2_RC_SUCCESS)
117*758e9fbaSOystein Eftevaag         return r;
118*758e9fbaSOystein Eftevaag     esysContext->state = _ESYS_STATE_INTERNALERROR;
119*758e9fbaSOystein Eftevaag 
120*758e9fbaSOystein Eftevaag     /* Initial invocation of SAPI to prepare the command buffer with parameters */
121*758e9fbaSOystein Eftevaag     r = Tss2_Sys_Startup_Prepare(esysContext->sys, startupType);
122*758e9fbaSOystein Eftevaag     return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
123*758e9fbaSOystein Eftevaag     /* Trigger execution and finish the async invocation */
124*758e9fbaSOystein Eftevaag     r = Tss2_Sys_ExecuteAsync(esysContext->sys);
125*758e9fbaSOystein Eftevaag     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
126*758e9fbaSOystein Eftevaag                           "Finish (Execute Async)");
127*758e9fbaSOystein Eftevaag 
128*758e9fbaSOystein Eftevaag     esysContext->state = _ESYS_STATE_SENT;
129*758e9fbaSOystein Eftevaag 
130*758e9fbaSOystein Eftevaag     return r;
131*758e9fbaSOystein Eftevaag }
132*758e9fbaSOystein Eftevaag 
133*758e9fbaSOystein Eftevaag /** Asynchronous finish function for TPM2_Startup
134*758e9fbaSOystein Eftevaag  *
135*758e9fbaSOystein Eftevaag  * This function returns the results of a TPM2_Startup command
136*758e9fbaSOystein Eftevaag  * invoked via Esys_Startup_Finish. All non-simple output parameters
137*758e9fbaSOystein Eftevaag  * are allocated by the function's implementation. NULL can be passed for every
138*758e9fbaSOystein Eftevaag  * output parameter if the value is not required.
139*758e9fbaSOystein Eftevaag  *
140*758e9fbaSOystein Eftevaag  * @param[in,out] esysContext The ESYS_CONTEXT.
141*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS on success
142*758e9fbaSOystein Eftevaag  * @retval ESYS_RC_SUCCESS if the function call was a success.
143*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
144*758e9fbaSOystein Eftevaag  *         pointers or required output handle references are NULL.
145*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
146*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
147*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
148*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
149*758e9fbaSOystein Eftevaag  *         operation already pending.
150*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
151*758e9fbaSOystein Eftevaag  *         TPM response is received.
152*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
153*758e9fbaSOystein Eftevaag  *         at least contain the tag, response length, and response code.
154*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
155*758e9fbaSOystein Eftevaag  *         not verify.
156*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
157*758e9fbaSOystein Eftevaag  * @retval TSS2_RCs produced by lower layers of the software stack may be
158*758e9fbaSOystein Eftevaag  *         returned to the caller unaltered unless handled internally.
159*758e9fbaSOystein Eftevaag  */
160*758e9fbaSOystein Eftevaag TSS2_RC
Esys_Startup_Finish(ESYS_CONTEXT * esysContext)161*758e9fbaSOystein Eftevaag Esys_Startup_Finish(
162*758e9fbaSOystein Eftevaag     ESYS_CONTEXT *esysContext)
163*758e9fbaSOystein Eftevaag {
164*758e9fbaSOystein Eftevaag     TSS2_RC r;
165*758e9fbaSOystein Eftevaag     LOG_TRACE("context=%p",
166*758e9fbaSOystein Eftevaag               esysContext);
167*758e9fbaSOystein Eftevaag 
168*758e9fbaSOystein Eftevaag     if (esysContext == NULL) {
169*758e9fbaSOystein Eftevaag         LOG_ERROR("esyscontext is NULL.");
170*758e9fbaSOystein Eftevaag         return TSS2_ESYS_RC_BAD_REFERENCE;
171*758e9fbaSOystein Eftevaag     }
172*758e9fbaSOystein Eftevaag 
173*758e9fbaSOystein Eftevaag     /* Check for correct sequence and set sequence to irregular for now */
174*758e9fbaSOystein Eftevaag     if (esysContext->state != _ESYS_STATE_SENT &&
175*758e9fbaSOystein Eftevaag         esysContext->state != _ESYS_STATE_RESUBMISSION) {
176*758e9fbaSOystein Eftevaag         LOG_ERROR("Esys called in bad sequence.");
177*758e9fbaSOystein Eftevaag         return TSS2_ESYS_RC_BAD_SEQUENCE;
178*758e9fbaSOystein Eftevaag     }
179*758e9fbaSOystein Eftevaag     esysContext->state = _ESYS_STATE_INTERNALERROR;
180*758e9fbaSOystein Eftevaag 
181*758e9fbaSOystein Eftevaag     /*Receive the TPM response and handle resubmissions if necessary. */
182*758e9fbaSOystein Eftevaag     r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
183*758e9fbaSOystein Eftevaag     if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
184*758e9fbaSOystein Eftevaag         LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
185*758e9fbaSOystein Eftevaag         esysContext->state = _ESYS_STATE_SENT;
186*758e9fbaSOystein Eftevaag         return r;
187*758e9fbaSOystein Eftevaag     }
188*758e9fbaSOystein Eftevaag     /* This block handle the resubmission of TPM commands given a certain set of
189*758e9fbaSOystein Eftevaag      * TPM response codes. */
190*758e9fbaSOystein Eftevaag     if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
191*758e9fbaSOystein Eftevaag         LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
192*758e9fbaSOystein Eftevaag             "resubmission: %" PRIx32, r);
193*758e9fbaSOystein Eftevaag         if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
194*758e9fbaSOystein Eftevaag             LOG_WARNING("Maximum number of (re)submissions has been reached.");
195*758e9fbaSOystein Eftevaag             esysContext->state = _ESYS_STATE_INIT;
196*758e9fbaSOystein Eftevaag             return r;
197*758e9fbaSOystein Eftevaag         }
198*758e9fbaSOystein Eftevaag         esysContext->state = _ESYS_STATE_SENT;
199*758e9fbaSOystein Eftevaag 	r = Tss2_Sys_ExecuteAsync(esysContext->sys);
200*758e9fbaSOystein Eftevaag         if (r != TSS2_RC_SUCCESS) {
201*758e9fbaSOystein Eftevaag             LOG_WARNING("Error attempting to resubmit");
202*758e9fbaSOystein Eftevaag             /* We do not set esysContext->state here but inherit the most recent
203*758e9fbaSOystein Eftevaag              * state of the _async function. */
204*758e9fbaSOystein Eftevaag             return r;
205*758e9fbaSOystein Eftevaag         }
206*758e9fbaSOystein Eftevaag         r = TSS2_ESYS_RC_TRY_AGAIN;
207*758e9fbaSOystein Eftevaag         LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
208*758e9fbaSOystein Eftevaag         return r;
209*758e9fbaSOystein Eftevaag     }
210*758e9fbaSOystein Eftevaag     /* The following is the "regular error" handling. */
211*758e9fbaSOystein Eftevaag     if (iesys_tpm_error(r) && r != TPM2_RC_INITIALIZE) {
212*758e9fbaSOystein Eftevaag         LOG_WARNING("Received TPM Error");
213*758e9fbaSOystein Eftevaag         esysContext->state = _ESYS_STATE_INIT;
214*758e9fbaSOystein Eftevaag         return r;
215*758e9fbaSOystein Eftevaag     } else if (r != TSS2_RC_SUCCESS && r != TPM2_RC_INITIALIZE) {
216*758e9fbaSOystein Eftevaag         LOG_ERROR("Received a non-TPM Error");
217*758e9fbaSOystein Eftevaag         esysContext->state = _ESYS_STATE_INTERNALERROR;
218*758e9fbaSOystein Eftevaag         return r;
219*758e9fbaSOystein Eftevaag     }
220*758e9fbaSOystein Eftevaag     r = Tss2_Sys_Startup_Complete(esysContext->sys);
221*758e9fbaSOystein Eftevaag     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
222*758e9fbaSOystein Eftevaag                           "Received error from SAPI unmarshaling" );
223*758e9fbaSOystein Eftevaag 
224*758e9fbaSOystein Eftevaag     esysContext->state = _ESYS_STATE_INIT;
225*758e9fbaSOystein Eftevaag 
226*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
227*758e9fbaSOystein Eftevaag }
228