1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2024, Linaro Limited
4  */
5 
6 /* Microsoft Reference Implementation for TPM 2.0
7  *
8  * The copyright in this software is being made available under the BSD
9  * License, included below. This software may be subject to other third
10  * party and contributor rights, including patent rights, and no such
11  * rights are granted under this license.
12  *
13  * Copyright (c) 2018 Microsoft Corporation
14  *
15  * All rights reserved.
16  *
17  * BSD License
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions are
21  * met:
22  *
23  * Redistributions of source code must retain the above copyright notice,
24  * this list of conditions and the following disclaimer.
25  *
26  * Redistributions in binary form must reproduce the above copyright
27  * notice, this list of conditions and the following disclaimer in the
28  * documentation and/or other materials provided with the distribution.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
31  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
33  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
36  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  */
42 
43 #ifndef FTPM_H
44 #define FTPM_H
45 
46 #include <TpmProfile.h>
47 #include <ftpm_ta.h>
48 
49 //
50 // These must match values from reference/TPM/include/TpmProfile.h
51 //
52 #define  MAX_COMMAND_SIZE       4096
53 #define  MAX_RESPONSE_SIZE      4096
54 
55 //
56 // Macro for intentionally unreferenced parameters
57 //
58 #define UNREFERENCED_PARAMETER(_Parameter_) (void)(_Parameter_)
59 
60 //
61 // Shorthand for TA functions taking uniform arg types
62 //
63 #define TA_ALL_PARAM_TYPE(a) TEE_PARAM_TYPES((a), (a), (a), (a))
64 
65 #ifndef BYTE_ARRAY_TO_UINT32
66 //
67 // Used to extract size field from TPM command buffers
68 //
69 #define BYTE_ARRAY_TO_UINT32(b)  (uint32_t)(  ((b)[0] << 24) \
70                                             + ((b)[1] << 16) \
71                                             + ((b)[2] << 8 ) \
72                                             +  (b)[3])
73 #endif
74 
75 //
76 // Entrypoint for reference implemntation
77 //
78 extern void ExecuteCommand(
79     uint32_t         requestSize,   // IN: command buffer size
80     unsigned char   *request,       // IN: command buffer
81     uint32_t        *responseSize,  // OUT: response buffer size
82     unsigned char   **response      // OUT: response buffer
83     );
84 
85 //
86 // External functions supporting TPM initialization
87 //
88 extern int  _plat__NVEnable(void *platParameter);
89 extern int  TPM_Manufacture(bool firstTime);
90 extern bool _plat__NvNeedsManufacture(void);
91 extern void _TPM_Init(void);
92 extern void _plat__NVDisable(void);
93 extern void _admin__SaveChipFlags(void);
94 
95 //
96 // External types/data supporting TPM initialization
97 //
98 typedef union {
99     uint32_t   flags;
100     struct {
101         uint32_t Remanufacture : 1;   // Perform a TPM_Remanufacture() on startup (SET by default)
102         uint32_t TpmStatePresent : 1; // Init TPM and NV with contents of TpmState and NVState on startup
103         uint32_t Reserved : 30;
104     }        fields;
105 } TPM_CHIP_STATE;
106 
107 extern TPM_CHIP_STATE g_chipFlags;
108 #endif /* FTPM_H */
109