xref: /aosp_15_r20/external/ms-tpm-20-ref/TPMCmd/tpm/include/NV.h (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
1*5c591343SA. Cody Schuffelen /* Microsoft Reference Implementation for TPM 2.0
2*5c591343SA. Cody Schuffelen  *
3*5c591343SA. Cody Schuffelen  *  The copyright in this software is being made available under the BSD License,
4*5c591343SA. Cody Schuffelen  *  included below. This software may be subject to other third party and
5*5c591343SA. Cody Schuffelen  *  contributor rights, including patent rights, and no such rights are granted
6*5c591343SA. Cody Schuffelen  *  under this license.
7*5c591343SA. Cody Schuffelen  *
8*5c591343SA. Cody Schuffelen  *  Copyright (c) Microsoft Corporation
9*5c591343SA. Cody Schuffelen  *
10*5c591343SA. Cody Schuffelen  *  All rights reserved.
11*5c591343SA. Cody Schuffelen  *
12*5c591343SA. Cody Schuffelen  *  BSD License
13*5c591343SA. Cody Schuffelen  *
14*5c591343SA. Cody Schuffelen  *  Redistribution and use in source and binary forms, with or without modification,
15*5c591343SA. Cody Schuffelen  *  are permitted provided that the following conditions are met:
16*5c591343SA. Cody Schuffelen  *
17*5c591343SA. Cody Schuffelen  *  Redistributions of source code must retain the above copyright notice, this list
18*5c591343SA. Cody Schuffelen  *  of conditions and the following disclaimer.
19*5c591343SA. Cody Schuffelen  *
20*5c591343SA. Cody Schuffelen  *  Redistributions in binary form must reproduce the above copyright notice, this
21*5c591343SA. Cody Schuffelen  *  list of conditions and the following disclaimer in the documentation and/or
22*5c591343SA. Cody Schuffelen  *  other materials provided with the distribution.
23*5c591343SA. Cody Schuffelen  *
24*5c591343SA. Cody Schuffelen  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25*5c591343SA. Cody Schuffelen  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*5c591343SA. Cody Schuffelen  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27*5c591343SA. Cody Schuffelen  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28*5c591343SA. Cody Schuffelen  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29*5c591343SA. Cody Schuffelen  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30*5c591343SA. Cody Schuffelen  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31*5c591343SA. Cody Schuffelen  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32*5c591343SA. Cody Schuffelen  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*5c591343SA. Cody Schuffelen  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*5c591343SA. Cody Schuffelen  */
35*5c591343SA. Cody Schuffelen //** Index Type Definitions
36*5c591343SA. Cody Schuffelen 
37*5c591343SA. Cody Schuffelen // These definitions allow the same code to be used pre and post 1.21. The main
38*5c591343SA. Cody Schuffelen // action is to redefine the index type values from the bit values.
39*5c591343SA. Cody Schuffelen // Use TPM_NT_ORDINARY to indicate if the TPM_NT type is defined
40*5c591343SA. Cody Schuffelen 
41*5c591343SA. Cody Schuffelen #ifndef    _NV_H_
42*5c591343SA. Cody Schuffelen #define    _NV_H_
43*5c591343SA. Cody Schuffelen 
44*5c591343SA. Cody Schuffelen 
45*5c591343SA. Cody Schuffelen #ifdef     TPM_NT_ORDINARY
46*5c591343SA. Cody Schuffelen // If TPM_NT_ORDINARY is defined, then the TPM_NT field is present in a TPMA_NV
47*5c591343SA. Cody Schuffelen #   define GET_TPM_NT(attributes) GET_ATTRIBUTE(attributes, TPMA_NV, TPM_NT)
48*5c591343SA. Cody Schuffelen #else
49*5c591343SA. Cody Schuffelen // If TPM_NT_ORDINARY is not defined, then need to synthesize it from the
50*5c591343SA. Cody Schuffelen // attributes
51*5c591343SA. Cody Schuffelen #   define GetNv_TPM_NV(attributes)                             \
52*5c591343SA. Cody Schuffelen         (   IS_ATTRIBUTE(attributes, TPMA_NV, COUNTER)          \
53*5c591343SA. Cody Schuffelen         +   (IS_ATTRIBUTE(attributes, TPMA_NV, BITS) << 1)      \
54*5c591343SA. Cody Schuffelen         +   (IS_ATTRIBUTE(attributes, TPMA_NV, EXTEND) << 2)    \
55*5c591343SA. Cody Schuffelen         )
56*5c591343SA. Cody Schuffelen #   define TPM_NT_ORDINARY (0)
57*5c591343SA. Cody Schuffelen #   define TPM_NT_COUNTER  (1)
58*5c591343SA. Cody Schuffelen #   define TPM_NT_BITS     (2)
59*5c591343SA. Cody Schuffelen #   define TPM_NT_EXTEND   (4)
60*5c591343SA. Cody Schuffelen #endif
61*5c591343SA. Cody Schuffelen 
62*5c591343SA. Cody Schuffelen 
63*5c591343SA. Cody Schuffelen //** Attribute Macros
64*5c591343SA. Cody Schuffelen // These macros are used to isolate the differences in the way that the index type
65*5c591343SA. Cody Schuffelen // changed in version 1.21 of the specification
66*5c591343SA. Cody Schuffelen #   define IsNvOrdinaryIndex(attributes)                        \
67*5c591343SA. Cody Schuffelen                 (GET_TPM_NT(attributes) == TPM_NT_ORDINARY)
68*5c591343SA. Cody Schuffelen 
69*5c591343SA. Cody Schuffelen #   define  IsNvCounterIndex(attributes)                        \
70*5c591343SA. Cody Schuffelen                 (GET_TPM_NT(attributes) == TPM_NT_COUNTER)
71*5c591343SA. Cody Schuffelen 
72*5c591343SA. Cody Schuffelen #   define  IsNvBitsIndex(attributes)                           \
73*5c591343SA. Cody Schuffelen                (GET_TPM_NT(attributes) == TPM_NT_BITS)
74*5c591343SA. Cody Schuffelen 
75*5c591343SA. Cody Schuffelen #   define  IsNvExtendIndex(attributes)                         \
76*5c591343SA. Cody Schuffelen                 (GET_TPM_NT(attributes) == TPM_NT_EXTEND)
77*5c591343SA. Cody Schuffelen 
78*5c591343SA. Cody Schuffelen #ifdef TPM_NT_PIN_PASS
79*5c591343SA. Cody Schuffelen #   define  IsNvPinPassIndex(attributes)                        \
80*5c591343SA. Cody Schuffelen                 (GET_TPM_NT(attributes) == TPM_NT_PIN_PASS)
81*5c591343SA. Cody Schuffelen #endif
82*5c591343SA. Cody Schuffelen 
83*5c591343SA. Cody Schuffelen #ifdef TPM_NT_PIN_FAIL
84*5c591343SA. Cody Schuffelen #   define  IsNvPinFailIndex(attributes)                        \
85*5c591343SA. Cody Schuffelen                 (GET_TPM_NT(attributes) == TPM_NT_PIN_FAIL)
86*5c591343SA. Cody Schuffelen #endif
87*5c591343SA. Cody Schuffelen 
88*5c591343SA. Cody Schuffelen typedef struct {
89*5c591343SA. Cody Schuffelen     UINT32      size;
90*5c591343SA. Cody Schuffelen     TPM_HANDLE  handle;
91*5c591343SA. Cody Schuffelen } NV_ENTRY_HEADER;
92*5c591343SA. Cody Schuffelen 
93*5c591343SA. Cody Schuffelen #define NV_EVICT_OBJECT_SIZE        \
94*5c591343SA. Cody Schuffelen      (sizeof(UINT32)  + sizeof(TPM_HANDLE) + sizeof(OBJECT))
95*5c591343SA. Cody Schuffelen 
96*5c591343SA. Cody Schuffelen #define NV_INDEX_COUNTER_SIZE       \
97*5c591343SA. Cody Schuffelen     (sizeof(UINT32) + sizeof(NV_INDEX) + sizeof(UINT64))
98*5c591343SA. Cody Schuffelen 
99*5c591343SA. Cody Schuffelen #define NV_RAM_INDEX_COUNTER_SIZE   \
100*5c591343SA. Cody Schuffelen     (sizeof(NV_RAM_HEADER) + sizeof(UINT64))
101*5c591343SA. Cody Schuffelen 
102*5c591343SA. Cody Schuffelen typedef struct {
103*5c591343SA. Cody Schuffelen     UINT32          size;
104*5c591343SA. Cody Schuffelen     TPM_HANDLE      handle;
105*5c591343SA. Cody Schuffelen     TPMA_NV         attributes;
106*5c591343SA. Cody Schuffelen } NV_RAM_HEADER;
107*5c591343SA. Cody Schuffelen 
108*5c591343SA. Cody Schuffelen // Defines the end-of-list marker for NV. The list terminator is
109*5c591343SA. Cody Schuffelen // a UINT32 of zero, followed by the current value of s_maxCounter which is a
110*5c591343SA. Cody Schuffelen // 64-bit value. The structure is defined as an array of 3 UINT32 values so that
111*5c591343SA. Cody Schuffelen // there is no padding between the  UINT32 list end marker and the UINT64 maxCounter
112*5c591343SA. Cody Schuffelen // value.
113*5c591343SA. Cody Schuffelen typedef UINT32 NV_LIST_TERMINATOR[3];
114*5c591343SA. Cody Schuffelen 
115*5c591343SA. Cody Schuffelen //** Orderly RAM Values
116*5c591343SA. Cody Schuffelen // The following defines are for accessing orderly RAM values.
117*5c591343SA. Cody Schuffelen 
118*5c591343SA. Cody Schuffelen // This is the initialize for the RAM reference iterator.
119*5c591343SA. Cody Schuffelen #define     NV_RAM_REF_INIT         0
120*5c591343SA. Cody Schuffelen // This is the starting address of the RAM space used for orderly data
121*5c591343SA. Cody Schuffelen #define     RAM_ORDERLY_START               \
122*5c591343SA. Cody Schuffelen                 (&s_indexOrderlyRam[0])
123*5c591343SA. Cody Schuffelen // This is the offset within NV that is used to save the orderly data on an
124*5c591343SA. Cody Schuffelen // orderly shutdown.
125*5c591343SA. Cody Schuffelen #define     NV_ORDERLY_START                \
126*5c591343SA. Cody Schuffelen                 (NV_INDEX_RAM_DATA)
127*5c591343SA. Cody Schuffelen // This is the end of the orderly RAM space. It is actually the first byte after the
128*5c591343SA. Cody Schuffelen // last byte of orderly RAM data
129*5c591343SA. Cody Schuffelen #define     RAM_ORDERLY_END                 \
130*5c591343SA. Cody Schuffelen                 (RAM_ORDERLY_START + sizeof(s_indexOrderlyRam))
131*5c591343SA. Cody Schuffelen // This is the end of the orderly space in NV memory. As with RAM_ORDERLY_END, it is
132*5c591343SA. Cody Schuffelen // actually the offset of the first byte after the end of the NV orderly data.
133*5c591343SA. Cody Schuffelen #define     NV_ORDERLY_END                  \
134*5c591343SA. Cody Schuffelen                 (NV_ORDERLY_START + sizeof(s_indexOrderlyRam))
135*5c591343SA. Cody Schuffelen 
136*5c591343SA. Cody Schuffelen // Macro to check that an orderly RAM address is with range.
137*5c591343SA. Cody Schuffelen #define ORDERLY_RAM_ADDRESS_OK(start, offset)       \
138*5c591343SA. Cody Schuffelen         ((start >= RAM_ORDERLY_START) && ((start + offset - 1) < RAM_ORDERLY_END))
139*5c591343SA. Cody Schuffelen 
140*5c591343SA. Cody Schuffelen 
141*5c591343SA. Cody Schuffelen #define RETURN_IF_NV_IS_NOT_AVAILABLE               \
142*5c591343SA. Cody Schuffelen {                                                   \
143*5c591343SA. Cody Schuffelen     if(g_NvStatus != TPM_RC_SUCCESS)                \
144*5c591343SA. Cody Schuffelen         return g_NvStatus;                          \
145*5c591343SA. Cody Schuffelen }
146*5c591343SA. Cody Schuffelen 
147*5c591343SA. Cody Schuffelen // Routinely have to clear the orderly flag and fail if the
148*5c591343SA. Cody Schuffelen // NV is not available so that it can be cleared.
149*5c591343SA. Cody Schuffelen #define RETURN_IF_ORDERLY                           \
150*5c591343SA. Cody Schuffelen {                                                   \
151*5c591343SA. Cody Schuffelen     if(NvClearOrderly() != TPM_RC_SUCCESS)          \
152*5c591343SA. Cody Schuffelen         return g_NvStatus;                          \
153*5c591343SA. Cody Schuffelen }
154*5c591343SA. Cody Schuffelen 
155*5c591343SA. Cody Schuffelen #define NV_IS_AVAILABLE     (g_NvStatus == TPM_RC_SUCCESS)
156*5c591343SA. Cody Schuffelen 
157*5c591343SA. Cody Schuffelen #define IS_ORDERLY(value)   (value < SU_DA_USED_VALUE)
158*5c591343SA. Cody Schuffelen 
159*5c591343SA. Cody Schuffelen #define NV_IS_ORDERLY       (IS_ORDERLY(gp.orderlyState))
160*5c591343SA. Cody Schuffelen 
161*5c591343SA. Cody Schuffelen // Macro to set the NV UPDATE_TYPE. This deals with the fact that the update is
162*5c591343SA. Cody Schuffelen // possibly a combination of UT_NV and UT_ORDERLY.
163*5c591343SA. Cody Schuffelen #define SET_NV_UPDATE(type)     g_updateNV |= (type)
164*5c591343SA. Cody Schuffelen 
165*5c591343SA. Cody Schuffelen #endif  // _NV_H_