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 
36 //**Includes
37 // Force Global.h contents inclusion
38 #define GLOBAL_C
39 
40 #include "Admin.h"
41 
42 //**Types, Structures, and Defines
43 //
44 // List of pre-defined address of TPM state data
45 //
46 static UINT32       s_stateAddr[NV_TPM_STATE_LAST];
47 
48 //
49 // List of pre-defined TPM state data size in byte
50 //
51 static UINT32       s_stateSize[NV_TPM_STATE_LAST];
52 
53 //
54 // The current chip state
55 //
56 TPM_CHIP_STATE g_chipFlags;
57 
58 //
59 // The current PPI state
60 //
61 extern FTPM_PPI_STATE s_PPIState;
62 
63 //***_admin__NvInitState()
64 // Initialize the state NV runtime state values
65 void
_admin__NvInitState()66 _admin__NvInitState()
67 {
68     UINT16 i;
69     UINT32 stateAddr;
70 
71     //
72     // Initialize TPM saved runtime state
73     //
74     s_stateSize[NV_TPM_STATE_FLAGS] = sizeof(TPM_CHIP_STATE);
75     s_stateSize[NV_TPM_STATE_PPI] = sizeof(FTPM_PPI_STATE);
76 
77     //
78     // Initialize TPM state data addresses. Stored after the main NV space.
79     //
80     stateAddr = NV_MEMORY_SIZE;
81     for (i = 0; i < NV_TPM_STATE_LAST; i++) {
82         s_stateAddr[i] = stateAddr;
83         stateAddr += s_stateSize[i];
84     }
85 
86     pAssert(stateAddr <= (NV_MEMORY_SIZE + NV_TPM_STATE_SIZE));
87 }
88 
89 //***_admin__SaveChipFlags()
90 // Save the g_chipFlags runtime state
91 void
_admin__SaveChipFlags()92 _admin__SaveChipFlags()
93 {
94     _admin__NvWriteState(NV_TPM_STATE_FLAGS, &g_chipFlags);
95 }
96 
97 //***_admin__RestoreChipFlags()
98 // Restore the g_chipFlags runtime state
99 void
_admin__RestoreChipFlags()100 _admin__RestoreChipFlags()
101 {
102     _admin__NvReadState(NV_TPM_STATE_FLAGS, &g_chipFlags);
103 }
104 
105 //***_admin__SavePPIState()
106 // Save the s_PPIState runtime state
107 void
_admin__SavePPIState()108 _admin__SavePPIState()
109 {
110     _admin__NvWriteState(NV_TPM_STATE_PPI, &s_PPIState);
111 
112     _plat__NvCommit();
113 }
114 
115 //***_admin__RestorePPIState()
116 // Restore the s_PPIState runtime state
117 void
_admin__RestorePPIState()118 _admin__RestorePPIState()
119 {
120     _admin__NvReadState(NV_TPM_STATE_PPI, &s_PPIState);
121 }
122 
123 //***_admin__NvReadState()
124 // Read TPM state data from NV memory to RAM
125 void
_admin__NvReadState(NV_TPM_STATE type,void * buffer)126 _admin__NvReadState(
127     NV_TPM_STATE     type,    // IN: type of state data
128     void             *buffer  // OUT: data buffer
129     )
130 {
131     // Input type should be valid
132     pAssert(type >= 0 && type < NV_TPM_STATE_LAST);
133 
134     _plat__NvMemoryRead(s_stateAddr[type], s_stateSize[type], buffer);
135     return;
136 }
137 
138 //***_admin__NvWriteState()
139 // Write TPM state data to NV memory
140 void
_admin__NvWriteState(NV_TPM_STATE type,void * buffer)141 _admin__NvWriteState(
142     NV_TPM_STATE     type,    // IN: type of state data
143     void             *buffer  // IN: data buffer
144     )
145 {
146     // Input type should be valid
147     pAssert(type >= 0 && type < NV_TPM_STATE_LAST);
148 
149     _plat__NvMemoryWrite(s_stateAddr[type], s_stateSize[type], buffer);
150     return;
151 }