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_Write_fp.h"
10 #include "NV_spt_fp.h"
11 //
12 //
13 //     Error Returns                    Meaning
14 //
15 //     TPM_RC_ATTRIBUTES                Index referenced by nvIndex has either TPMA_NV_BITS,
16 //                                      TPMA_NV_COUNTER, or TPMA_NV_EVENT attribute SET
17 //     TPM_RC_NV_AUTHORIZATION          the authorization was valid but the authorizing entity (authHandle) is
18 //                                      not allowed to write to the Index referenced by nvIndex
19 //     TPM_RC_NV_LOCKED                 Index referenced by nvIndex is write locked
20 //     TPM_RC_NV_RANGE                  if TPMA_NV_WRITEALL is SET then the write is not the size of the
21 //                                      Index referenced by nvIndex; otherwise, the write extends beyond the
22 //                                      limits of the Index
23 //
24 TPM_RC
TPM2_NV_Write(NV_Write_In * in)25 TPM2_NV_Write(
26    NV_Write_In       *in                 // IN: input parameter list
27    )
28 {
29    NV_INDEX          nvIndex;
30    TPM_RC            result;
31 
32 // Input Validation
33 
34    // Get NV index info
35    NvGetIndexInfo(in->nvIndex, &nvIndex);
36 
37    // common access checks. NvWrtieAccessChecks() may return
38    // TPM_RC_NV_AUTHORIZATION or TPM_RC_NV_LOCKED
39    result = NvWriteAccessChecks(in->authHandle, in->nvIndex);
40    if(result != TPM_RC_SUCCESS)
41        return result;
42 
43    // Bits index, extend index or counter index may not be updated by
44    // TPM2_NV_Write
45    if(   nvIndex.publicArea.attributes.TPMA_NV_COUNTER == SET
46       || nvIndex.publicArea.attributes.TPMA_NV_BITS == SET
47       || nvIndex.publicArea.attributes.TPMA_NV_EXTEND == SET)
48        return TPM_RC_ATTRIBUTES;
49 
50    // Too much data
51    if((in->data.t.size + in->offset) > nvIndex.publicArea.dataSize)
52        return TPM_RC_NV_RANGE;
53 
54    // If this index requires a full sized write, make sure that input range is
55    // full sized
56    if(   nvIndex.publicArea.attributes.TPMA_NV_WRITEALL == SET
57       && in->data.t.size < nvIndex.publicArea.dataSize)
58        return TPM_RC_NV_RANGE;
59 
60 // Internal Data Update
61 
62    // Perform the write. This called routine will SET the TPMA_NV_WRITTEN
63    // attribute if it has not already been SET. If NV isn't available, an error
64    // will be returned.
65    return NvWriteIndexData(in->nvIndex, &nvIndex, in->offset,
66                            in->data.t.size, in->data.t.buffer);
67 
68 }
69