aboutsummaryrefslogtreecommitdiff
path: root/NV_SetBits.c
blob: df163a9a707a667b4724881e5ecbcbe6c69db697 (plain)
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
// This file was extracted from the TCG Published
// Trusted Platform Module Library
// Part 3: Commands
// Family "2.0"
// Level 00 Revision 01.16
// October 30, 2014

#include "InternalRoutines.h"
#include "NV_SetBits_fp.h"
#include "NV_spt_fp.h"
//
//
//     Error Returns                 Meaning
//
//     TPM_RC_ATTRIBUTES             the TPMA_NV_BITS attribute is not SET in the Index referenced by
//                                   nvIndex
//     TPM_RC_NV_AUTHORIZATION       the authorization was valid but the authorizing entity (authHandle) is
//                                   not allowed to write to the Index referenced by nvIndex
//     TPM_RC_NV_LOCKED              the Index referenced by nvIndex is locked for writing
//
TPM_RC
TPM2_NV_SetBits(
   NV_SetBits_In     *in              // IN: input parameter list
   )
{
   TPM_RC            result;
   NV_INDEX          nvIndex;
   UINT64            oldValue;
   UINT64            newValue;

// Input Validation

   // Common access checks, NvWriteAccessCheck() may return TPM_RC_NV_AUTHORIZATION
   // or TPM_RC_NV_LOCKED
   // error may be returned at this point
   result = NvWriteAccessChecks(in->authHandle, in->nvIndex);
   if(result != TPM_RC_SUCCESS)
       return result;

   // Get NV index info
   NvGetIndexInfo(in->nvIndex, &nvIndex);

   // Make sure that this is a bit field
   if(nvIndex.publicArea.attributes.TPMA_NV_BITS != SET)
       return TPM_RC_ATTRIBUTES + RC_NV_SetBits_nvIndex;

   // If index is not been written, initialize it
   if(nvIndex.publicArea.attributes.TPMA_NV_WRITTEN == CLEAR)
       oldValue = 0;
   else
       // Read index data
       NvGetIntIndexData(in->nvIndex, &nvIndex, &oldValue);

   // Figure out what the new value is going to be
   newValue = oldValue | in->bits;

   // If the Index is not-orderly and it has changed, or if this is the first
   // write, NV will need to be updated.
   if(    (    nvIndex.publicArea.attributes.TPMA_NV_ORDERLY == CLEAR
           && newValue != oldValue)
       || nvIndex.publicArea.attributes.TPMA_NV_WRITTEN == CLEAR)
   {

// Internal Data Update
       // Check if NV is available. NvIsAvailable may return TPM_RC_NV_UNAVAILABLE
       // TPM_RC_NV_RATE or TPM_RC_SUCCESS.
       result = NvIsAvailable();
       if(result != TPM_RC_SUCCESS)
           return result;

       // Write index data back. If necessary, this function will SET
       // TPMA_NV_WRITTEN.
       result = NvWriteIndexData(in->nvIndex, &nvIndex, 0, 8, &newValue);
   }
   return result;

}