Coverage Report

Created: 2025-08-29 06:04

/src/tpm2/NV_Increment.c
Line
Count
Source (jump to first uncovered line)
1
// This file was extracted from the TCG Published
2
// Trusted Platform Module Library
3
// Part 3: Commands
4
// Family "2.0"
5
// Level 00 Revision 01.16
6
// October 30, 2014
7
8
#include "InternalRoutines.h"
9
#include "NV_Increment_fp.h"
10
#include "NV_spt_fp.h"
11
//
12
//
13
//     Error Returns                   Meaning
14
//
15
//     TPM_RC_ATTRIBUTES               NV index is not a counter
16
//     TPM_RC_NV_AUTHORIZATION         authorization failure
17
//     TPM_RC_NV_LOCKED                Index is write locked
18
//
19
TPM_RC
20
TPM2_NV_Increment(
21
   NV_Increment_In       *in                  // IN: input parameter list
22
   )
23
0
{
24
0
   TPM_RC            result;
25
0
   NV_INDEX          nvIndex;
26
0
   UINT64            countValue;
27
28
// Input Validation
29
30
   // Common access checks, a TPM_RC_NV_AUTHORIZATION or TPM_RC_NV_LOCKED
31
   // error may be returned at this point
32
0
   result = NvWriteAccessChecks(in->authHandle, in->nvIndex);
33
0
   if(result != TPM_RC_SUCCESS)
34
0
       return result;
35
36
   // Check if there are platform-specific reasons to prohibit updating this
37
   // index.
38
0
   if (!_plat__NvUpdateAllowed(in->nvIndex))
39
0
       return TPM_RC_NV_AUTHORIZATION;
40
41
   // Get NV index info
42
0
   NvGetIndexInfo(in->nvIndex, &nvIndex);
43
44
   // Make sure that this is a counter
45
0
   if(nvIndex.publicArea.attributes.TPMA_NV_COUNTER != SET)
46
0
       return TPM_RC_ATTRIBUTES + RC_NV_Increment_nvIndex;
47
48
// Internal Data Update
49
50
   // If counter index is not been written, initialize it
51
0
   if(nvIndex.publicArea.attributes.TPMA_NV_WRITTEN == CLEAR)
52
0
       countValue = NvInitialCounter();
53
0
   else
54
       // Read NV data in native format for TPM CPU.
55
0
       NvGetIntIndexData(in->nvIndex, &nvIndex, &countValue);
56
57
   // Do the increment
58
0
   countValue++;
59
60
   // If this is an orderly counter that just rolled over, need to be able to
61
   // write to NV to proceed. This check is done here, because NvWriteIndexData()
62
   // does not see if the update is for counter rollover.
63
0
   if(    nvIndex.publicArea.attributes.TPMA_NV_ORDERLY == SET
64
0
       && (countValue & MAX_ORDERLY_COUNT) == 0)
65
0
   {
66
0
       result = NvIsAvailable();
67
0
       if(result != TPM_RC_SUCCESS)
68
0
           return result;
69
70
       // Need to force an NV update
71
0
       g_updateNV = TRUE;
72
//
73
0
   }
74
75
   // Write NV data back. A TPM_RC_NV_UNAVAILABLE or TPM_RC_NV_RATE error may
76
   // be returned at this point. If necessary, this function will set the
77
   // TPMA_NV_WRITTEN attribute
78
0
   return NvWriteIndexData(in->nvIndex, &nvIndex, 0, 8, &countValue);
79
80
0
}