1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************;
3 * Copyright (c) 2015 - 2017, Intel Corporation
4 * All rights reserved.
5 ***********************************************************************/
6
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include "tss2_tpm2_types.h"
12 #include "tss2_mu.h"
13 #include "sysapi_util.h"
14
Tss2_Sys_NV_Write_Prepare(TSS2_SYS_CONTEXT * sysContext,TPMI_RH_NV_AUTH authHandle,TPMI_RH_NV_INDEX nvIndex,const TPM2B_MAX_NV_BUFFER * data,UINT16 offset)15 TSS2_RC Tss2_Sys_NV_Write_Prepare(
16 TSS2_SYS_CONTEXT *sysContext,
17 TPMI_RH_NV_AUTH authHandle,
18 TPMI_RH_NV_INDEX nvIndex,
19 const TPM2B_MAX_NV_BUFFER *data,
20 UINT16 offset)
21 {
22 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
23 TSS2_RC rval;
24
25 if (!ctx)
26 return TSS2_SYS_RC_BAD_REFERENCE;
27
28 rval = CommonPreparePrologue(ctx, TPM2_CC_NV_Write);
29 if (rval)
30 return rval;
31
32 rval = Tss2_MU_UINT32_Marshal(authHandle, ctx->cmdBuffer,
33 ctx->maxCmdSize,
34 &ctx->nextData);
35 if (rval)
36 return rval;
37
38 rval = Tss2_MU_UINT32_Marshal(nvIndex, ctx->cmdBuffer,
39 ctx->maxCmdSize,
40 &ctx->nextData);
41 if (rval)
42 return rval;
43
44 if (!data) {
45 ctx->decryptNull = 1;
46
47 rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
48 ctx->maxCmdSize,
49 &ctx->nextData);
50 } else {
51 rval = Tss2_MU_TPM2B_MAX_NV_BUFFER_Marshal(data, ctx->cmdBuffer,
52 ctx->maxCmdSize,
53 &ctx->nextData);
54 }
55
56 if (rval)
57 return rval;
58
59 rval = Tss2_MU_UINT16_Marshal(offset, ctx->cmdBuffer,
60 ctx->maxCmdSize,
61 &ctx->nextData);
62 if (rval)
63 return rval;
64
65 ctx->decryptAllowed = 1;
66 ctx->encryptAllowed = 0;
67 ctx->authAllowed = 1;
68
69 return CommonPrepareEpilogue(ctx);
70 }
71
Tss2_Sys_NV_Write_Complete(TSS2_SYS_CONTEXT * sysContext)72 TSS2_RC Tss2_Sys_NV_Write_Complete (
73 TSS2_SYS_CONTEXT *sysContext)
74 {
75 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
76
77 if (!ctx)
78 return TSS2_SYS_RC_BAD_REFERENCE;
79
80 return CommonComplete(ctx);
81 }
82
Tss2_Sys_NV_Write(TSS2_SYS_CONTEXT * sysContext,TPMI_RH_NV_AUTH authHandle,TPMI_RH_NV_INDEX nvIndex,TSS2L_SYS_AUTH_COMMAND const * cmdAuthsArray,const TPM2B_MAX_NV_BUFFER * data,UINT16 offset,TSS2L_SYS_AUTH_RESPONSE * rspAuthsArray)83 TSS2_RC Tss2_Sys_NV_Write(
84 TSS2_SYS_CONTEXT *sysContext,
85 TPMI_RH_NV_AUTH authHandle,
86 TPMI_RH_NV_INDEX nvIndex,
87 TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray,
88 const TPM2B_MAX_NV_BUFFER *data,
89 UINT16 offset,
90 TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray)
91 {
92 _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
93 TSS2_RC rval;
94
95 rval = Tss2_Sys_NV_Write_Prepare(sysContext, authHandle, nvIndex, data, offset);
96 if (rval)
97 return rval;
98
99 rval = CommonOneCall(ctx, cmdAuthsArray, rspAuthsArray);
100 if (rval)
101 return rval;
102
103 return Tss2_Sys_NV_Write_Complete(sysContext);
104 }
105