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 //** Index Type Definitions 36 37 // These definitions allow the same code to be used pre and post 1.21. The main 38 // action is to redefine the index type values from the bit values. 39 // Use TPM_NT_ORDINARY to indicate if the TPM_NT type is defined 40 41 #ifndef _NV_H_ 42 #define _NV_H_ 43 44 45 #ifdef TPM_NT_ORDINARY 46 // If TPM_NT_ORDINARY is defined, then the TPM_NT field is present in a TPMA_NV 47 # define GET_TPM_NT(attributes) GET_ATTRIBUTE(attributes, TPMA_NV, TPM_NT) 48 #else 49 // If TPM_NT_ORDINARY is not defined, then need to synthesize it from the 50 // attributes 51 # define GetNv_TPM_NV(attributes) \ 52 ( IS_ATTRIBUTE(attributes, TPMA_NV, COUNTER) \ 53 + (IS_ATTRIBUTE(attributes, TPMA_NV, BITS) << 1) \ 54 + (IS_ATTRIBUTE(attributes, TPMA_NV, EXTEND) << 2) \ 55 ) 56 # define TPM_NT_ORDINARY (0) 57 # define TPM_NT_COUNTER (1) 58 # define TPM_NT_BITS (2) 59 # define TPM_NT_EXTEND (4) 60 #endif 61 62 63 //** Attribute Macros 64 // These macros are used to isolate the differences in the way that the index type 65 // changed in version 1.21 of the specification 66 # define IsNvOrdinaryIndex(attributes) \ 67 (GET_TPM_NT(attributes) == TPM_NT_ORDINARY) 68 69 # define IsNvCounterIndex(attributes) \ 70 (GET_TPM_NT(attributes) == TPM_NT_COUNTER) 71 72 # define IsNvBitsIndex(attributes) \ 73 (GET_TPM_NT(attributes) == TPM_NT_BITS) 74 75 # define IsNvExtendIndex(attributes) \ 76 (GET_TPM_NT(attributes) == TPM_NT_EXTEND) 77 78 #ifdef TPM_NT_PIN_PASS 79 # define IsNvPinPassIndex(attributes) \ 80 (GET_TPM_NT(attributes) == TPM_NT_PIN_PASS) 81 #endif 82 83 #ifdef TPM_NT_PIN_FAIL 84 # define IsNvPinFailIndex(attributes) \ 85 (GET_TPM_NT(attributes) == TPM_NT_PIN_FAIL) 86 #endif 87 88 typedef struct { 89 UINT32 size; 90 TPM_HANDLE handle; 91 } NV_ENTRY_HEADER; 92 93 #define NV_EVICT_OBJECT_SIZE \ 94 (sizeof(UINT32) + sizeof(TPM_HANDLE) + sizeof(OBJECT)) 95 96 #define NV_INDEX_COUNTER_SIZE \ 97 (sizeof(UINT32) + sizeof(NV_INDEX) + sizeof(UINT64)) 98 99 #define NV_RAM_INDEX_COUNTER_SIZE \ 100 (sizeof(NV_RAM_HEADER) + sizeof(UINT64)) 101 102 typedef struct { 103 UINT32 size; 104 TPM_HANDLE handle; 105 TPMA_NV attributes; 106 } NV_RAM_HEADER; 107 108 // Defines the end-of-list marker for NV. The list terminator is 109 // a UINT32 of zero, followed by the current value of s_maxCounter which is a 110 // 64-bit value. The structure is defined as an array of 3 UINT32 values so that 111 // there is no padding between the UINT32 list end marker and the UINT64 maxCounter 112 // value. 113 typedef UINT32 NV_LIST_TERMINATOR[3]; 114 115 //** Orderly RAM Values 116 // The following defines are for accessing orderly RAM values. 117 118 // This is the initialize for the RAM reference iterator. 119 #define NV_RAM_REF_INIT 0 120 // This is the starting address of the RAM space used for orderly data 121 #define RAM_ORDERLY_START \ 122 (&s_indexOrderlyRam[0]) 123 // This is the offset within NV that is used to save the orderly data on an 124 // orderly shutdown. 125 #define NV_ORDERLY_START \ 126 (NV_INDEX_RAM_DATA) 127 // This is the end of the orderly RAM space. It is actually the first byte after the 128 // last byte of orderly RAM data 129 #define RAM_ORDERLY_END \ 130 (RAM_ORDERLY_START + sizeof(s_indexOrderlyRam)) 131 // This is the end of the orderly space in NV memory. As with RAM_ORDERLY_END, it is 132 // actually the offset of the first byte after the end of the NV orderly data. 133 #define NV_ORDERLY_END \ 134 (NV_ORDERLY_START + sizeof(s_indexOrderlyRam)) 135 136 // Macro to check that an orderly RAM address is with range. 137 #define ORDERLY_RAM_ADDRESS_OK(start, offset) \ 138 ((start >= RAM_ORDERLY_START) && ((start + offset - 1) < RAM_ORDERLY_END)) 139 140 141 #define RETURN_IF_NV_IS_NOT_AVAILABLE \ 142 { \ 143 if(g_NvStatus != TPM_RC_SUCCESS) \ 144 return g_NvStatus; \ 145 } 146 147 // Routinely have to clear the orderly flag and fail if the 148 // NV is not available so that it can be cleared. 149 #define RETURN_IF_ORDERLY \ 150 { \ 151 if(NvClearOrderly() != TPM_RC_SUCCESS) \ 152 return g_NvStatus; \ 153 } 154 155 #define NV_IS_AVAILABLE (g_NvStatus == TPM_RC_SUCCESS) 156 157 #define IS_ORDERLY(value) (value < SU_DA_USED_VALUE) 158 159 #define NV_IS_ORDERLY (IS_ORDERLY(gp.orderlyState)) 160 161 // Macro to set the NV UPDATE_TYPE. This deals with the fact that the update is 162 // possibly a combination of UT_NV and UT_ORDERLY. 163 #define SET_NV_UPDATE(type) g_updateNV |= (type) 164 165 #endif // _NV_H_