aboutsummaryrefslogtreecommitdiff
path: root/NV_Write.c
blob: ea6224d3275a942812598dc6bb504d2f7b7fad21 (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
// 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_Write_fp.h"
#include "NV_spt_fp.h"
//
//
//     Error Returns                    Meaning
//
//     TPM_RC_ATTRIBUTES                Index referenced by nvIndex has either TPMA_NV_BITS,
//                                      TPMA_NV_COUNTER, or TPMA_NV_EVENT attribute SET
//     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                 Index referenced by nvIndex is write locked
//     TPM_RC_NV_RANGE                  if TPMA_NV_WRITEALL is SET then the write is not the size of the
//                                      Index referenced by nvIndex; otherwise, the write extends beyond the
//                                      limits of the Index
//
TPM_RC
TPM2_NV_Write(
   NV_Write_In       *in                 // IN: input parameter list
   )
{
   NV_INDEX          nvIndex;
   TPM_RC            result;

// Input Validation

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

   // common access checks. NvWrtieAccessChecks() may return
   // TPM_RC_NV_AUTHORIZATION or TPM_RC_NV_LOCKED
   result = NvWriteAccessChecks(in->authHandle, in->nvIndex);
   if(result != TPM_RC_SUCCESS)
       return result;

   // Bits index, extend index or counter index may not be updated by
   // TPM2_NV_Write
   if(   nvIndex.publicArea.attributes.TPMA_NV_COUNTER == SET
      || nvIndex.publicArea.attributes.TPMA_NV_BITS == SET
      || nvIndex.publicArea.attributes.TPMA_NV_EXTEND == SET)
       return TPM_RC_ATTRIBUTES;

   // Too much data
   if((in->data.t.size + in->offset) > nvIndex.publicArea.dataSize)
       return TPM_RC_NV_RANGE;

   // If this index requires a full sized write, make sure that input range is
   // full sized
   if(   nvIndex.publicArea.attributes.TPMA_NV_WRITEALL == SET
      && in->data.t.size < nvIndex.publicArea.dataSize)
       return TPM_RC_NV_RANGE;

// Internal Data Update

   // Perform the write. This called routine will SET the TPMA_NV_WRITTEN
   // attribute if it has not already been SET. If NV isn't available, an error
   // will be returned.
   return NvWriteIndexData(in->nvIndex, &nvIndex, in->offset,
                           in->data.t.size, in->data.t.buffer);

}