2 Implement TPM1.2 NV storage related command.
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved. <BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/Tpm12CommandLib.h>
12 #include <Library/BaseLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/Tpm12DeviceLib.h>
18 // Max TPM NV value length
20 #define TPMNVVALUELENGTH 1024
25 TPM_RQU_COMMAND_HDR Hdr
;
26 TPM12_NV_DATA_PUBLIC PubInfo
;
28 } TPM_CMD_NV_DEFINE_SPACE
;
31 TPM_RQU_COMMAND_HDR Hdr
;
35 } TPM_CMD_NV_READ_VALUE
;
38 TPM_RSP_COMMAND_HDR Hdr
;
40 UINT8 Data
[TPMNVVALUELENGTH
];
41 } TPM_RSP_NV_READ_VALUE
;
44 TPM_RQU_COMMAND_HDR Hdr
;
48 UINT8 Data
[TPMNVVALUELENGTH
];
49 } TPM_CMD_NV_WRITE_VALUE
;
54 Send NV DefineSpace command to TPM1.2.
56 @param PubInfo The public parameters of the NV area.
57 @param EncAuth The encrypted AuthData, only valid if the attributes require subsequent authorization.
59 @retval EFI_SUCCESS Operation completed successfully.
60 @retval EFI_DEVICE_ERROR Unexpected device behavior.
65 IN TPM12_NV_DATA_PUBLIC
*PubInfo
,
66 IN TPM_ENCAUTH
*EncAuth
70 TPM_CMD_NV_DEFINE_SPACE Command
;
71 TPM_RSP_COMMAND_HDR Response
;
75 // send Tpm command TPM_ORD_NV_DefineSpace
77 Command
.Hdr
.tag
= SwapBytes16 (TPM_TAG_RQU_COMMAND
);
78 Command
.Hdr
.paramSize
= SwapBytes32 (sizeof (Command
));
79 Command
.Hdr
.ordinal
= SwapBytes32 (TPM_ORD_NV_DefineSpace
);
80 Command
.PubInfo
.tag
= SwapBytes16 (PubInfo
->tag
);
81 Command
.PubInfo
.nvIndex
= SwapBytes32 (PubInfo
->nvIndex
);
82 Command
.PubInfo
.pcrInfoRead
.pcrSelection
.sizeOfSelect
= SwapBytes16 (PubInfo
->pcrInfoRead
.pcrSelection
.sizeOfSelect
);
83 Command
.PubInfo
.pcrInfoRead
.pcrSelection
.pcrSelect
[0] = PubInfo
->pcrInfoRead
.pcrSelection
.pcrSelect
[0];
84 Command
.PubInfo
.pcrInfoRead
.pcrSelection
.pcrSelect
[1] = PubInfo
->pcrInfoRead
.pcrSelection
.pcrSelect
[1];
85 Command
.PubInfo
.pcrInfoRead
.pcrSelection
.pcrSelect
[2] = PubInfo
->pcrInfoRead
.pcrSelection
.pcrSelect
[2];
86 Command
.PubInfo
.pcrInfoRead
.localityAtRelease
= PubInfo
->pcrInfoRead
.localityAtRelease
;
87 CopyMem (&Command
.PubInfo
.pcrInfoRead
.digestAtRelease
, &PubInfo
->pcrInfoRead
.digestAtRelease
, sizeof(PubInfo
->pcrInfoRead
.digestAtRelease
));
88 Command
.PubInfo
.pcrInfoWrite
.pcrSelection
.sizeOfSelect
= SwapBytes16 (PubInfo
->pcrInfoWrite
.pcrSelection
.sizeOfSelect
);
89 Command
.PubInfo
.pcrInfoWrite
.pcrSelection
.pcrSelect
[0] = PubInfo
->pcrInfoWrite
.pcrSelection
.pcrSelect
[0];
90 Command
.PubInfo
.pcrInfoWrite
.pcrSelection
.pcrSelect
[1] = PubInfo
->pcrInfoWrite
.pcrSelection
.pcrSelect
[1];
91 Command
.PubInfo
.pcrInfoWrite
.pcrSelection
.pcrSelect
[2] = PubInfo
->pcrInfoWrite
.pcrSelection
.pcrSelect
[2];
92 Command
.PubInfo
.pcrInfoWrite
.localityAtRelease
= PubInfo
->pcrInfoWrite
.localityAtRelease
;
93 CopyMem (&Command
.PubInfo
.pcrInfoWrite
.digestAtRelease
, &PubInfo
->pcrInfoWrite
.digestAtRelease
, sizeof(PubInfo
->pcrInfoWrite
.digestAtRelease
));
94 Command
.PubInfo
.permission
.tag
= SwapBytes16 (PubInfo
->permission
.tag
);
95 Command
.PubInfo
.permission
.attributes
= SwapBytes32 (PubInfo
->permission
.attributes
);
96 Command
.PubInfo
.bReadSTClear
= PubInfo
->bReadSTClear
;
97 Command
.PubInfo
.bWriteSTClear
= PubInfo
->bWriteSTClear
;
98 Command
.PubInfo
.bWriteDefine
= PubInfo
->bWriteDefine
;
99 Command
.PubInfo
.dataSize
= SwapBytes32 (PubInfo
->dataSize
);
100 CopyMem (&Command
.EncAuth
, EncAuth
, sizeof(*EncAuth
));
101 Length
= sizeof (Response
);
102 Status
= Tpm12SubmitCommand (sizeof (Command
), (UINT8
*)&Command
, &Length
, (UINT8
*)&Response
);
103 if (EFI_ERROR (Status
)) {
106 DEBUG ((DEBUG_INFO
, "Tpm12NvDefineSpace - ReturnCode = %x\n", SwapBytes32 (Response
.returnCode
)));
107 switch (SwapBytes32 (Response
.returnCode
)) {
111 return EFI_DEVICE_ERROR
;
116 Send NV ReadValue command to TPM1.2.
118 @param NvIndex The index of the area to set.
119 @param Offset The offset into the area.
120 @param DataSize The size of the data area.
121 @param Data The data to set the area to.
123 @retval EFI_SUCCESS Operation completed successfully.
124 @retval EFI_DEVICE_ERROR Unexpected device behavior.
129 IN TPM_NV_INDEX NvIndex
,
131 IN OUT UINT32
*DataSize
,
136 TPM_CMD_NV_READ_VALUE Command
;
137 TPM_RSP_NV_READ_VALUE Response
;
141 // send Tpm command TPM_ORD_NV_ReadValue
143 Command
.Hdr
.tag
= SwapBytes16 (TPM_TAG_RQU_COMMAND
);
144 Command
.Hdr
.paramSize
= SwapBytes32 (sizeof (Command
));
145 Command
.Hdr
.ordinal
= SwapBytes32 (TPM_ORD_NV_ReadValue
);
146 Command
.NvIndex
= SwapBytes32 (NvIndex
);
147 Command
.Offset
= SwapBytes32 (Offset
);
148 Command
.DataSize
= SwapBytes32 (*DataSize
);
149 Length
= sizeof (Response
);
150 Status
= Tpm12SubmitCommand (sizeof (Command
), (UINT8
*)&Command
, &Length
, (UINT8
*)&Response
);
151 if (EFI_ERROR (Status
)) {
154 DEBUG ((DEBUG_INFO
, "Tpm12NvReadValue - ReturnCode = %x\n", SwapBytes32 (Response
.Hdr
.returnCode
)));
155 switch (SwapBytes32 (Response
.Hdr
.returnCode
)) {
159 return EFI_DEVICE_ERROR
;
163 // Return the response
165 if (SwapBytes32 (Response
.DataSize
) > *DataSize
) {
166 return EFI_BUFFER_TOO_SMALL
;
168 *DataSize
= SwapBytes32 (Response
.DataSize
);
169 ZeroMem (Data
, *DataSize
);
170 CopyMem (Data
, &Response
.Data
, *DataSize
);
176 Send NV WriteValue command to TPM1.2.
178 @param NvIndex The index of the area to set.
179 @param Offset The offset into the NV Area.
180 @param DataSize The size of the data parameter.
181 @param Data The data to set the area to.
183 @retval EFI_SUCCESS Operation completed successfully.
184 @retval EFI_DEVICE_ERROR Unexpected device behavior.
189 IN TPM_NV_INDEX NvIndex
,
196 TPM_CMD_NV_WRITE_VALUE Command
;
197 UINT32 CommandLength
;
198 TPM_RSP_COMMAND_HDR Response
;
199 UINT32 ResponseLength
;
201 if (DataSize
> sizeof (Command
.Data
)) {
202 return EFI_UNSUPPORTED
;
206 // send Tpm command TPM_ORD_NV_WriteValue
208 Command
.Hdr
.tag
= SwapBytes16 (TPM_TAG_RQU_COMMAND
);
209 CommandLength
= sizeof (Command
) - sizeof(Command
.Data
) + DataSize
;
210 Command
.Hdr
.paramSize
= SwapBytes32 (CommandLength
);
211 Command
.Hdr
.ordinal
= SwapBytes32 (TPM_ORD_NV_WriteValue
);
212 Command
.NvIndex
= SwapBytes32 (NvIndex
);
213 Command
.Offset
= SwapBytes32 (Offset
);
214 Command
.DataSize
= SwapBytes32 (DataSize
);
215 CopyMem (Command
.Data
, Data
, DataSize
);
216 ResponseLength
= sizeof (Response
);
217 Status
= Tpm12SubmitCommand (CommandLength
, (UINT8
*)&Command
, &ResponseLength
, (UINT8
*)&Response
);
218 if (EFI_ERROR (Status
)) {
221 DEBUG ((DEBUG_INFO
, "Tpm12NvWritedValue - ReturnCode = %x\n", SwapBytes32 (Response
.returnCode
)));
222 switch (SwapBytes32 (Response
.returnCode
)) {
226 return EFI_DEVICE_ERROR
;