1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/********************************************************************************/
/* */
/* NV TPM persistent and state save data */
/* Written by Ken Goldman */
/* IBM Thomas J. Watson Research Center */
/* $Id: NVReserved.c 1476 2019-06-10 19:32:03Z kgoldman $ */
/* */
/* Licenses and Notices */
/* */
/* 1. Copyright Licenses: */
/* */
/* - Trusted Computing Group (TCG) grants to the user of the source code in */
/* this specification (the "Source Code") a worldwide, irrevocable, */
/* nonexclusive, royalty free, copyright license to reproduce, create */
/* derivative works, distribute, display and perform the Source Code and */
/* derivative works thereof, and to grant others the rights granted herein. */
/* */
/* - The TCG grants to the user of the other parts of the specification */
/* (other than the Source Code) the rights to reproduce, distribute, */
/* display, and perform the specification solely for the purpose of */
/* developing products based on such documents. */
/* */
/* 2. Source Code Distribution Conditions: */
/* */
/* - Redistributions of Source Code must retain the above copyright licenses, */
/* this list of conditions and the following disclaimers. */
/* */
/* - Redistributions in binary form must reproduce the above copyright */
/* licenses, this list of conditions and the following disclaimers in the */
/* documentation and/or other materials provided with the distribution. */
/* */
/* 3. Disclaimers: */
/* */
/* - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF */
/* LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH */
/* RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES) */
/* THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE. */
/* Contact TCG Administration (admin@trustedcomputinggroup.org) for */
/* information on specification licensing rights available through TCG */
/* membership agreements. */
/* */
/* - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED */
/* WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR */
/* FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR */
/* NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY */
/* OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE. */
/* */
/* - Without limitation, TCG and its members and licensors disclaim all */
/* liability, including liability for infringement of any proprietary */
/* rights, relating to use of information in this specification and to the */
/* implementation of this specification, and TCG disclaims all liability for */
/* cost of procurement of substitute goods or services, lost profits, loss */
/* of use, loss of data or any incidental, consequential, direct, indirect, */
/* or special damages, whether under contract, tort, warranty or otherwise, */
/* arising in any way out of use or reliance upon this specification or any */
/* information herein. */
/* */
/* (c) Copyright IBM Corp. and others, 2016 - 2019 */
/* */
/********************************************************************************/
// 8.5 NVReserved.c
/* 8.5.2 Includes, Defines and Data Definitions */
#define NV_C
#include "Tpm.h"
/* 8.5.3 Functions */
/* 8.5.3.1 NvInitStatic() */
/* This function initializes the static variables used in the NV subsystem. */
static void
NvInitStatic(
void
)
{
// In some implementations, the end of NV is variable and is set at boot time.
// This value will be the same for each boot, but is not necessarily known
// at compile time.
s_evictNvEnd = (NV_REF)NV_MEMORY_SIZE;
return;
}
/* 8.5.3.2 NvCheckState() */
/* Function to check the NV state by accessing the platform-specific function to get the NV state.
The result state is registered in s_NvIsAvailable that will be reported by NvIsAvailable(). */
/* This function is called at the beginning of ExecuteCommand() before any potential check of
g_NvStatus. */
void
NvCheckState(
void
)
{
int func_return;
//
func_return = _plat__IsNvAvailable();
if(func_return == 0)
g_NvStatus = TPM_RC_SUCCESS;
else if(func_return == 1)
g_NvStatus = TPM_RC_NV_UNAVAILABLE;
else
g_NvStatus = TPM_RC_NV_RATE;
return;
}
/* 8.5.3.3 NvCommit */
/* This is a wrapper for the platform function to commit pending NV writes. */
BOOL
NvCommit(
void
)
{
return (_plat__NvCommit() == 0);
}
/* 8.5.3.4 NvPowerOn() */
/* This function is called at _TPM_Init() to initialize the NV environment. */
/* Return Values Meaning */
/* TRUE all NV was initialized */
/* FALSE the NV containing saved state had an error and TPM2_Startup(CLEAR) is required */
BOOL
NvPowerOn(
void
)
{
int nvError = 0;
// If power was lost, need to re-establish the RAM data that is loaded from
// NV and initialize the static variables
if(g_powerWasLost)
{
if((nvError = _plat__NVEnable(0)) < 0)
LOG_FAILURE(FATAL_ERROR_NV_UNRECOVERABLE); /* libtpms changed */
NvInitStatic();
}
return nvError == 0;
}
/* 8.5.3.5 NvManufacture() */
/* This function initializes the NV system at pre-install time. */
/* This function should only be called in a manufacturing environment or in a simulation. */
/* The layout of NV memory space is an implementation choice. */
void
NvManufacture(
void
)
{
#if SIMULATION
// Simulate the NV memory being in the erased state.
_plat__NvMemoryClear(0, NV_MEMORY_SIZE);
#endif
// Initialize static variables
NvInitStatic();
// Clear the RAM used for Orderly Index data
MemorySet(s_indexOrderlyRam, 0, RAM_INDEX_SPACE);
// Write that Orderly Index data to NV
NvUpdateIndexOrderlyData();
// Initialize the next offset of the first entry in evict/index list to 0 (the
// end of list marker) and the initial s_maxCounterValue;
NvSetMaxCount(0);
// Put the end of list marker at the end of memory. This contains the MaxCount
// value as well as the end marker.
NvWriteNvListEnd(NV_USER_DYNAMIC);
return;
}
/* 8.5.3.6 NvRead() */
/* This function is used to move reserved data from NV memory to RAM. */
void
NvRead(
void *outBuffer, // OUT: buffer to receive data
UINT32 nvOffset, // IN: offset in NV of value
UINT32 size // IN: size of the value to read
)
{
// Input type should be valid
pAssert(nvOffset + size < NV_MEMORY_SIZE);
_plat__NvMemoryRead(nvOffset, size, outBuffer);
return;
}
/* 8.5.3.7 NvWrite() */
/* This function is used to post reserved data for writing to NV memory. Before the TPM completes
the operation, the value will be written. */
BOOL
NvWrite(
UINT32 nvOffset, // IN: location in NV to receive data
UINT32 size, // IN: size of the data to move
void *inBuffer // IN: location containing data to write
)
{
// Input type should be valid
if(nvOffset + size <= NV_MEMORY_SIZE)
{
// Set the flag that a NV write happened
SET_NV_UPDATE(UT_NV);
return _plat__NvMemoryWrite(nvOffset, size, inBuffer);
}
return FALSE;
}
#if 0 // libtpms added being (for Coverity)
/* 8.5.3.8 NvUpdatePersistent() */
/* This function is used to update a value in the PERSISTENT_DATA structure and commits the value to
NV. */
void
NvUpdatePersistent(
UINT32 offset, // IN: location in PERMANENT_DATA to be updated
UINT32 size, // IN: size of the value
void *buffer // IN: the new data
)
{
pAssert(offset + size <= sizeof(gp));
MemoryCopy(&gp + offset, buffer, size);
NvWrite(offset, size, buffer);
}
/* 8.5.3.9 NvClearPersistent() */
/* This function is used to clear a persistent data entry and commit it to NV */
void
NvClearPersistent(
UINT32 offset, // IN: the offset in the PERMANENT_DATA
// structure to be cleared (zeroed)
UINT32 size // IN: number of bytes to clear
)
{
pAssert(offset + size <= sizeof(gp));
MemorySet((&gp) + offset, 0, size);
NvWrite(offset, size, (&gp) + offset);
}
#endif // libtpms added end
/* 8.5.3.10 NvReadPersistent() */
/* This function reads persistent data to the RAM copy of the gp structure. */
void
NvReadPersistent(
void
)
{
NvRead(&gp, NV_PERSISTENT_DATA, sizeof(gp));
return;
}
|