xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/Platform/src/RunCommand.c (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
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 //**Introduction
36 // This module provides the platform specific entry and fail processing. The
37 // _plat__RunCommand() function is used to call to ExecuteCommand() in the TPM code.
38 // This function does whatever processing is necessary to set up the platform
39 // in anticipation of the call to the TPM including settup for error processing.
40 //
41 // The _plat__Fail() function is called when there is a failure in the TPM. The TPM
42 // code will have set the flag to indicate that the TPM is in failure mode.
43 // This call will then recursively call ExecuteCommand in order to build the
44 // failure mode response. When ExecuteCommand() returns to _plat__Fail(), the
45 // platform will do some platform specif operation to return to the environment in
46 // which the TPM is executing. For a simulator, setjmp/longjmp is used. For an OS,
47 // a system exit to the OS would be appropriate.
48 
49 //** Includes and locals
50 #include "Platform.h"
51 #include <setjmp.h>
52 #include "ExecCommand_fp.h"
53 
54 jmp_buf              s_jumpBuffer;
55 
56 //** Functions
57 
58 //***_plat__RunCommand()
59 // This version of RunCommand will set up a jum_buf and call ExecuteCommand(). If
60 // the command executes without failing, it will return and RunCommand will return.
61 // If there is a failure in the command, then _plat__Fail() is called and it will
62 // longjump back to RunCommand which will call ExecuteCommand again. However, this
63 // time, the TPM will be in failure mode so ExecuteCommand will simply build
64 // a failure response and return.
65 LIB_EXPORT void
_plat__RunCommand(uint32_t requestSize,unsigned char * request,uint32_t * responseSize,unsigned char ** response)66 _plat__RunCommand(
67     uint32_t         requestSize,   // IN: command buffer size
68     unsigned char   *request,       // IN: command buffer
69     uint32_t        *responseSize,  // IN/OUT: response buffer size
70     unsigned char   **response      // IN/OUT: response buffer
71     )
72 {
73     setjmp(s_jumpBuffer);
74     ExecuteCommand(requestSize, request, responseSize, response);
75 }
76 
77 
78 //***_plat__Fail()
79 // This is the platform depended failure exit for the TPM.
80 LIB_EXPORT NORETURN void
_plat__Fail(void)81 _plat__Fail(
82     void
83     )
84 {
85     longjmp(&s_jumpBuffer[0], 1);
86 }