Coverage Report

Created: 2025-11-11 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tpm2/NV_Write.c
Line
Count
Source
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
25
TPM2_NV_Write(
26
   NV_Write_In       *in                 // IN: input parameter list
27
   )
28
0
{
29
0
   NV_INDEX          nvIndex;
30
0
   TPM_RC            result;
31
32
// Input Validation
33
34
   // Get NV index info
35
0
   NvGetIndexInfo(in->nvIndex, &nvIndex);
36
37
   // common access checks. NvWrtieAccessChecks() may return
38
   // TPM_RC_NV_AUTHORIZATION or TPM_RC_NV_LOCKED
39
0
   result = NvWriteAccessChecks(in->authHandle, in->nvIndex);
40
0
   if(result != TPM_RC_SUCCESS)
41
0
       return result;
42
43
   // Check if there are platform-specific reasons to prohibit updating this
44
   // index.
45
0
   if (!_plat__NvUpdateAllowed(in->nvIndex))
46
0
       return TPM_RC_NV_AUTHORIZATION;
47
48
   // Indexes in the virtual range cannot be written.
49
0
   if (_plat__NvGetHandleVirtualOffset(in->nvIndex))
50
0
       return TPM_RC_NV_LOCKED;
51
52
   // Bits index, extend index or counter index may not be updated by
53
   // TPM2_NV_Write
54
0
   if(   nvIndex.publicArea.attributes.TPMA_NV_COUNTER == SET
55
0
      || nvIndex.publicArea.attributes.TPMA_NV_BITS == SET
56
0
      || nvIndex.publicArea.attributes.TPMA_NV_EXTEND == SET)
57
0
       return TPM_RC_ATTRIBUTES;
58
59
    // Make sure that the offset is not too large
60
0
    if(in->offset > nvIndex.publicArea.dataSize)
61
0
        return TPM_RC_VALUE + RC_NV_Write_offset;
62
63
    // Make sure that the selection is within the range of the Index
64
0
    if(in->data.t.size > (nvIndex.publicArea.dataSize - in->offset))
65
0
        return TPM_RC_NV_RANGE;
66
67
   // If this index requires a full sized write, make sure that input range is
68
   // full sized
69
0
   if(   nvIndex.publicArea.attributes.TPMA_NV_WRITEALL == SET
70
0
      && in->data.t.size < nvIndex.publicArea.dataSize)
71
0
       return TPM_RC_NV_RANGE;
72
73
// Internal Data Update
74
75
   // Perform the write. This called routine will SET the TPMA_NV_WRITTEN
76
   // attribute if it has not already been SET. If NV isn't available, an error
77
   // will be returned.
78
0
   result = NvWriteIndexData(in->nvIndex, &nvIndex, in->offset,
79
0
                             in->data.t.size, in->data.t.buffer);
80
81
0
   _plat__NvInformIndexDataChanged(in->nvIndex);
82
83
0
   return result;
84
85
0
}