Coverage Report

Created: 2025-10-10 06:29

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