]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Implement TPM1.2 NV storage related command.\r | |
3 | \r | |
4 | Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved. <BR>\r | |
5 | (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r | |
6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
7 | \r | |
8 | **/\r | |
9 | \r | |
10 | #include <PiPei.h>\r | |
11 | #include <Library/Tpm12CommandLib.h>\r | |
12 | #include <Library/BaseLib.h>\r | |
13 | #include <Library/DebugLib.h>\r | |
14 | #include <Library/BaseMemoryLib.h>\r | |
15 | #include <Library/Tpm12DeviceLib.h>\r | |
16 | \r | |
17 | //\r | |
18 | // Max TPM NV value length\r | |
19 | //\r | |
20 | #define TPMNVVALUELENGTH 1024\r | |
21 | \r | |
22 | #pragma pack(1)\r | |
23 | \r | |
24 | typedef struct {\r | |
25 | TPM_RQU_COMMAND_HDR Hdr;\r | |
26 | TPM12_NV_DATA_PUBLIC PubInfo;\r | |
27 | TPM_ENCAUTH EncAuth;\r | |
28 | } TPM_CMD_NV_DEFINE_SPACE;\r | |
29 | \r | |
30 | typedef struct {\r | |
31 | TPM_RQU_COMMAND_HDR Hdr;\r | |
32 | TPM_NV_INDEX NvIndex;\r | |
33 | UINT32 Offset;\r | |
34 | UINT32 DataSize;\r | |
35 | } TPM_CMD_NV_READ_VALUE;\r | |
36 | \r | |
37 | typedef struct {\r | |
38 | TPM_RSP_COMMAND_HDR Hdr;\r | |
39 | UINT32 DataSize;\r | |
40 | UINT8 Data[TPMNVVALUELENGTH];\r | |
41 | } TPM_RSP_NV_READ_VALUE;\r | |
42 | \r | |
43 | typedef struct {\r | |
44 | TPM_RQU_COMMAND_HDR Hdr;\r | |
45 | TPM_NV_INDEX NvIndex;\r | |
46 | UINT32 Offset;\r | |
47 | UINT32 DataSize;\r | |
48 | UINT8 Data[TPMNVVALUELENGTH];\r | |
49 | } TPM_CMD_NV_WRITE_VALUE;\r | |
50 | \r | |
51 | #pragma pack()\r | |
52 | \r | |
53 | /**\r | |
54 | Send NV DefineSpace command to TPM1.2.\r | |
55 | \r | |
56 | @param PubInfo The public parameters of the NV area.\r | |
57 | @param EncAuth The encrypted AuthData, only valid if the attributes require subsequent authorization.\r | |
58 | \r | |
59 | @retval EFI_SUCCESS Operation completed successfully.\r | |
60 | @retval EFI_DEVICE_ERROR Unexpected device behavior.\r | |
61 | **/\r | |
62 | EFI_STATUS\r | |
63 | EFIAPI\r | |
64 | Tpm12NvDefineSpace (\r | |
65 | IN TPM12_NV_DATA_PUBLIC *PubInfo,\r | |
66 | IN TPM_ENCAUTH *EncAuth\r | |
67 | )\r | |
68 | {\r | |
69 | EFI_STATUS Status;\r | |
70 | TPM_CMD_NV_DEFINE_SPACE Command;\r | |
71 | TPM_RSP_COMMAND_HDR Response;\r | |
72 | UINT32 Length;\r | |
73 | \r | |
74 | //\r | |
75 | // send Tpm command TPM_ORD_NV_DefineSpace\r | |
76 | //\r | |
77 | Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r | |
78 | Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));\r | |
79 | Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_DefineSpace);\r | |
80 | Command.PubInfo.tag = SwapBytes16 (PubInfo->tag);\r | |
81 | Command.PubInfo.nvIndex = SwapBytes32 (PubInfo->nvIndex);\r | |
82 | Command.PubInfo.pcrInfoRead.pcrSelection.sizeOfSelect = SwapBytes16 (PubInfo->pcrInfoRead.pcrSelection.sizeOfSelect);\r | |
83 | Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[0] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[0];\r | |
84 | Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[1] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[1];\r | |
85 | Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[2] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[2];\r | |
86 | Command.PubInfo.pcrInfoRead.localityAtRelease = PubInfo->pcrInfoRead.localityAtRelease;\r | |
87 | CopyMem (&Command.PubInfo.pcrInfoRead.digestAtRelease, &PubInfo->pcrInfoRead.digestAtRelease, sizeof(PubInfo->pcrInfoRead.digestAtRelease));\r | |
88 | Command.PubInfo.pcrInfoWrite.pcrSelection.sizeOfSelect = SwapBytes16 (PubInfo->pcrInfoWrite.pcrSelection.sizeOfSelect);\r | |
89 | Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[0] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[0];\r | |
90 | Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[1] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[1];\r | |
91 | Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[2] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[2];\r | |
92 | Command.PubInfo.pcrInfoWrite.localityAtRelease = PubInfo->pcrInfoWrite.localityAtRelease;\r | |
93 | CopyMem (&Command.PubInfo.pcrInfoWrite.digestAtRelease, &PubInfo->pcrInfoWrite.digestAtRelease, sizeof(PubInfo->pcrInfoWrite.digestAtRelease));\r | |
94 | Command.PubInfo.permission.tag = SwapBytes16 (PubInfo->permission.tag);\r | |
95 | Command.PubInfo.permission.attributes = SwapBytes32 (PubInfo->permission.attributes);\r | |
96 | Command.PubInfo.bReadSTClear = PubInfo->bReadSTClear;\r | |
97 | Command.PubInfo.bWriteSTClear = PubInfo->bWriteSTClear;\r | |
98 | Command.PubInfo.bWriteDefine = PubInfo->bWriteDefine;\r | |
99 | Command.PubInfo.dataSize = SwapBytes32 (PubInfo->dataSize);\r | |
100 | CopyMem (&Command.EncAuth, EncAuth, sizeof(*EncAuth));\r | |
101 | Length = sizeof (Response);\r | |
102 | Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r | |
103 | if (EFI_ERROR (Status)) {\r | |
104 | return Status;\r | |
105 | }\r | |
106 | DEBUG ((DEBUG_INFO, "Tpm12NvDefineSpace - ReturnCode = %x\n", SwapBytes32 (Response.returnCode)));\r | |
107 | switch (SwapBytes32 (Response.returnCode)) {\r | |
108 | case TPM_SUCCESS:\r | |
109 | return EFI_SUCCESS;\r | |
110 | default:\r | |
111 | return EFI_DEVICE_ERROR;\r | |
112 | }\r | |
113 | }\r | |
114 | \r | |
115 | /**\r | |
116 | Send NV ReadValue command to TPM1.2.\r | |
117 | \r | |
118 | @param NvIndex The index of the area to set.\r | |
119 | @param Offset The offset into the area.\r | |
120 | @param DataSize The size of the data area.\r | |
121 | @param Data The data to set the area to.\r | |
122 | \r | |
123 | @retval EFI_SUCCESS Operation completed successfully.\r | |
124 | @retval EFI_DEVICE_ERROR Unexpected device behavior.\r | |
125 | **/\r | |
126 | EFI_STATUS\r | |
127 | EFIAPI\r | |
128 | Tpm12NvReadValue (\r | |
129 | IN TPM_NV_INDEX NvIndex,\r | |
130 | IN UINT32 Offset,\r | |
131 | IN OUT UINT32 *DataSize,\r | |
132 | OUT UINT8 *Data\r | |
133 | )\r | |
134 | {\r | |
135 | EFI_STATUS Status;\r | |
136 | TPM_CMD_NV_READ_VALUE Command;\r | |
137 | TPM_RSP_NV_READ_VALUE Response;\r | |
138 | UINT32 Length;\r | |
139 | \r | |
140 | //\r | |
141 | // send Tpm command TPM_ORD_NV_ReadValue\r | |
142 | //\r | |
143 | Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r | |
144 | Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));\r | |
145 | Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_ReadValue);\r | |
146 | Command.NvIndex = SwapBytes32 (NvIndex);\r | |
147 | Command.Offset = SwapBytes32 (Offset);\r | |
148 | Command.DataSize = SwapBytes32 (*DataSize);\r | |
149 | Length = sizeof (Response);\r | |
150 | Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r | |
151 | if (EFI_ERROR (Status)) {\r | |
152 | return Status;\r | |
153 | }\r | |
154 | DEBUG ((DEBUG_INFO, "Tpm12NvReadValue - ReturnCode = %x\n", SwapBytes32 (Response.Hdr.returnCode)));\r | |
155 | switch (SwapBytes32 (Response.Hdr.returnCode)) {\r | |
156 | case TPM_SUCCESS:\r | |
157 | break;\r | |
158 | default:\r | |
159 | return EFI_DEVICE_ERROR;\r | |
160 | }\r | |
161 | \r | |
162 | //\r | |
163 | // Return the response\r | |
164 | //\r | |
165 | if (SwapBytes32 (Response.DataSize) > *DataSize) {\r | |
166 | return EFI_BUFFER_TOO_SMALL;\r | |
167 | }\r | |
168 | *DataSize = SwapBytes32 (Response.DataSize);\r | |
169 | ZeroMem (Data, *DataSize);\r | |
170 | CopyMem (Data, &Response.Data, *DataSize);\r | |
171 | \r | |
172 | return EFI_SUCCESS;\r | |
173 | }\r | |
174 | \r | |
175 | /**\r | |
176 | Send NV WriteValue command to TPM1.2.\r | |
177 | \r | |
178 | @param NvIndex The index of the area to set.\r | |
179 | @param Offset The offset into the NV Area.\r | |
180 | @param DataSize The size of the data parameter.\r | |
181 | @param Data The data to set the area to.\r | |
182 | \r | |
183 | @retval EFI_SUCCESS Operation completed successfully.\r | |
184 | @retval EFI_DEVICE_ERROR Unexpected device behavior.\r | |
185 | **/\r | |
186 | EFI_STATUS\r | |
187 | EFIAPI\r | |
188 | Tpm12NvWriteValue (\r | |
189 | IN TPM_NV_INDEX NvIndex,\r | |
190 | IN UINT32 Offset,\r | |
191 | IN UINT32 DataSize,\r | |
192 | IN UINT8 *Data\r | |
193 | )\r | |
194 | {\r | |
195 | EFI_STATUS Status;\r | |
196 | TPM_CMD_NV_WRITE_VALUE Command;\r | |
197 | UINT32 CommandLength;\r | |
198 | TPM_RSP_COMMAND_HDR Response;\r | |
199 | UINT32 ResponseLength;\r | |
200 | \r | |
201 | if (DataSize > sizeof (Command.Data)) {\r | |
202 | return EFI_UNSUPPORTED;\r | |
203 | }\r | |
204 | \r | |
205 | //\r | |
206 | // send Tpm command TPM_ORD_NV_WriteValue\r | |
207 | //\r | |
208 | Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r | |
209 | CommandLength = sizeof (Command) - sizeof(Command.Data) + DataSize;\r | |
210 | Command.Hdr.paramSize = SwapBytes32 (CommandLength);\r | |
211 | Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_WriteValue);\r | |
212 | Command.NvIndex = SwapBytes32 (NvIndex);\r | |
213 | Command.Offset = SwapBytes32 (Offset);\r | |
214 | Command.DataSize = SwapBytes32 (DataSize);\r | |
215 | CopyMem (Command.Data, Data, DataSize);\r | |
216 | ResponseLength = sizeof (Response);\r | |
217 | Status = Tpm12SubmitCommand (CommandLength, (UINT8 *)&Command, &ResponseLength, (UINT8 *)&Response);\r | |
218 | if (EFI_ERROR (Status)) {\r | |
219 | return Status;\r | |
220 | }\r | |
221 | DEBUG ((DEBUG_INFO, "Tpm12NvWriteValue - ReturnCode = %x\n", SwapBytes32 (Response.returnCode)));\r | |
222 | switch (SwapBytes32 (Response.returnCode)) {\r | |
223 | case TPM_SUCCESS:\r | |
224 | return EFI_SUCCESS;\r | |
225 | default:\r | |
226 | return EFI_DEVICE_ERROR;\r | |
227 | }\r | |
228 | }\r |