1 /* SPDX-License-Identifier: BSD-3-Clause */
4 * A lightweight TPM command library.
6 * The general idea is that TPM commands are array of bytes whose
7 * fields are mostly compile-time constant. The goal is to build much
8 * of the commands at compile time (or build time) and change some of
9 * the fields at run time as needed. The code in
10 * utility/tlcl_generator.c builds structures containing the commands,
11 * as well as the offsets of the fields that need to be set at run
17 #include <security/tpm/tis.h>
19 #include <security/tpm/tss.h>
21 #include "tss_internal.h"
22 #include "tss_commands.h"
24 #include <console/console.h>
25 #define VBDEBUG(format, args...) printk(BIOS_DEBUG, format, ## args)
27 static tpm_result_t
tpm_send_receive(const uint8_t *request
,
28 uint32_t request_length
,
30 uint32_t *response_length
)
32 size_t len
= *response_length
;
35 if (tlcl_tis_sendrecv
== NULL
) {
36 printk(BIOS_ERR
, "Attempted use of uninitialized TSS 1.2 stack\n");
40 rc
= tlcl_tis_sendrecv(request
, request_length
, response
, &len
);
43 /* check 64->32bit overflow and (re)check response buffer overflow */
44 if (len
> *response_length
)
47 *response_length
= len
;
51 /* Sets the size field of a TPM command. */
52 static inline void set_tpm_command_size(uint8_t *buffer
, uint32_t size
)
54 to_tpm_uint32(buffer
+ sizeof(uint16_t), size
);
57 /* Gets the size field of a TPM command. */
58 __attribute__((unused
))
59 static inline int tpm_command_size(const uint8_t *buffer
)
62 from_tpm_uint32(buffer
+ sizeof(uint16_t), &size
);
66 /* Gets the code field of a TPM command. */
67 static inline tpm_result_t
tpm_command_code(const uint8_t *buffer
)
70 from_tpm_uint32(buffer
+ sizeof(uint16_t) + sizeof(uint32_t), &rc
);
74 /* Gets the return code field of a TPM result. */
75 static inline tpm_result_t
tpm_return_code(const uint8_t *buffer
)
77 return tpm_command_code(buffer
);
81 * Like TlclSendReceive below, but do not retry if NEEDS_SELFTEST or
82 * DOING_SELFTEST errors are returned.
84 static tpm_result_t
tlcl1_send_receive_no_retry(const uint8_t *request
,
85 uint8_t *response
, int max_length
)
87 uint32_t response_length
= max_length
;
90 rc
= tpm_send_receive(request
, tpm_command_size(request
),
91 response
, &response_length
);
92 if (rc
!= TPM_SUCCESS
) {
93 /* Communication with TPM failed, so response is garbage */
94 VBDEBUG("TPM: command %#x send/receive failed: %#x\n",
95 tpm_command_code(request
), rc
);
98 /* Otherwise, use the result code from the response */
99 rc
= tpm_return_code(response
);
101 /* TODO: add paranoia about returned response_length vs. max_length
102 * (and possibly expected length from the response header). See
103 * crosbug.com/17017 */
105 VBDEBUG("TPM: command %#x returned %#x\n",
106 tpm_command_code(request
), rc
);
111 /* Sends a TPM command and gets a response. Returns 0 if success or the TPM
112 * error code if error. Waits for the self test to complete if needed. */
113 tpm_result_t
tlcl1_send_receive(const uint8_t *request
, uint8_t *response
, int max_length
)
115 tpm_result_t rc
= tlcl1_send_receive_no_retry(request
, response
, max_length
);
116 /* If the command fails because the self test has not completed, try it
117 * again after attempting to ensure that the self test has completed. */
118 if (rc
== TPM_NEEDS_SELFTEST
|| rc
== TPM_DOING_SELFTEST
) {
119 rc
= tlcl1_continue_self_test();
120 if (rc
!= TPM_SUCCESS
)
122 #if defined(TPM_BLOCKING_CONTINUESELFTEST) || defined(VB_RECOVERY_MODE)
123 /* Retry only once */
124 rc
= tlcl1_send_receive_no_retry(request
, response
, max_length
);
126 /* This needs serious testing. The TPM specification says: "iii.
127 * The caller MUST wait for the actions of TPM_ContinueSelfTest
128 * to complete before reissuing the command C1." But, if
129 * ContinueSelfTest is non-blocking, how do we know that the
130 * actions have completed other than trying again? */
132 rc
= tlcl1_send_receive_no_retry(request
, response
, max_length
);
133 } while (rc
== TPM_DOING_SELFTEST
);
139 /* Sends a command and returns the error code. */
140 static tpm_result_t
send(const uint8_t *command
)
142 uint8_t response
[TPM_LARGE_ENOUGH_COMMAND_SIZE
];
143 return tlcl1_send_receive(command
, response
, sizeof(response
));
146 /* Exported functions. */
148 tpm_result_t
tlcl1_startup(void)
150 VBDEBUG("TPM: Startup\n");
151 return send(tpm_startup_cmd
.buffer
);
154 tpm_result_t
tlcl1_resume(void)
156 VBDEBUG("TPM: Resume\n");
157 return send(tpm_resume_cmd
.buffer
);
160 tpm_result_t
tlcl1_save_state(void)
162 VBDEBUG("TPM: Save state\n");
163 return send(tpm_savestate_cmd
.buffer
);
166 tpm_result_t
tlcl1_self_test_full(void)
168 VBDEBUG("TPM: Self test full\n");
169 return send(tpm_selftestfull_cmd
.buffer
);
172 tpm_result_t
tlcl1_continue_self_test(void)
174 uint8_t response
[TPM_LARGE_ENOUGH_COMMAND_SIZE
];
175 VBDEBUG("TPM: Continue self test\n");
176 /* Call the No Retry version of SendReceive to avoid recursion. */
177 return tlcl1_send_receive_no_retry(tpm_continueselftest_cmd
.buffer
,
178 response
, sizeof(response
));
181 tpm_result_t
tlcl1_define_space(uint32_t index
, uint32_t perm
, uint32_t size
)
183 struct s_tpm_nv_definespace_cmd cmd
;
184 VBDEBUG("TPM: TlclDefineSpace(%#x, %#x, %d)\n", index
, perm
, size
);
185 memcpy(&cmd
, &tpm_nv_definespace_cmd
, sizeof(cmd
));
186 to_tpm_uint32(cmd
.buffer
+ tpm_nv_definespace_cmd
.index
, index
);
187 to_tpm_uint32(cmd
.buffer
+ tpm_nv_definespace_cmd
.perm
, perm
);
188 to_tpm_uint32(cmd
.buffer
+ tpm_nv_definespace_cmd
.size
, size
);
189 return send(cmd
.buffer
);
192 tpm_result_t
tlcl1_write(uint32_t index
, const void *data
, uint32_t length
)
194 struct s_tpm_nv_write_cmd cmd
;
195 uint8_t response
[TPM_LARGE_ENOUGH_COMMAND_SIZE
];
196 const int total_length
=
197 kTpmRequestHeaderLength
+ kWriteInfoLength
+ length
;
199 VBDEBUG("TPM: %s(%#x, %d)\n", __func__
, index
, length
);
200 memcpy(&cmd
, &tpm_nv_write_cmd
, sizeof(cmd
));
201 assert(total_length
<= TPM_LARGE_ENOUGH_COMMAND_SIZE
);
202 set_tpm_command_size(cmd
.buffer
, total_length
);
204 to_tpm_uint32(cmd
.buffer
+ tpm_nv_write_cmd
.index
, index
);
205 to_tpm_uint32(cmd
.buffer
+ tpm_nv_write_cmd
.length
, length
);
207 memcpy(cmd
.buffer
+ tpm_nv_write_cmd
.data
, data
, length
);
209 return tlcl1_send_receive(cmd
.buffer
, response
, sizeof(response
));
212 tpm_result_t
tlcl1_read(uint32_t index
, void *data
, uint32_t length
)
214 struct s_tpm_nv_read_cmd cmd
;
215 uint8_t response
[TPM_LARGE_ENOUGH_COMMAND_SIZE
];
216 uint32_t result_length
;
219 VBDEBUG("TPM: %s(%#x, %d)\n", __func__
, index
, length
);
220 memcpy(&cmd
, &tpm_nv_read_cmd
, sizeof(cmd
));
221 to_tpm_uint32(cmd
.buffer
+ tpm_nv_read_cmd
.index
, index
);
222 to_tpm_uint32(cmd
.buffer
+ tpm_nv_read_cmd
.length
, length
);
224 rc
= tlcl1_send_receive(cmd
.buffer
, response
, sizeof(response
));
225 if (rc
== TPM_SUCCESS
&& length
> 0) {
226 uint8_t *nv_read_cursor
= response
+ kTpmResponseHeaderLength
;
227 from_tpm_uint32(nv_read_cursor
, &result_length
);
228 if (result_length
> length
)
230 nv_read_cursor
+= sizeof(uint32_t);
231 memcpy(data
, nv_read_cursor
, result_length
);
237 tpm_result_t
tlcl1_assert_physical_presence(void)
239 VBDEBUG("TPM: Asserting physical presence\n");
240 return send(tpm_ppassert_cmd
.buffer
);
243 tpm_result_t
tlcl1_physical_presence_cmd_enable(void)
245 VBDEBUG("TPM: Enable the physical presence command\n");
246 return send(tpm_ppenable_cmd
.buffer
);
249 tpm_result_t
tlcl1_finalize_physical_presence(void)
251 VBDEBUG("TPM: Enable PP cmd, disable HW pp, and set lifetime lock\n");
252 return send(tpm_finalizepp_cmd
.buffer
);
255 tpm_result_t
tlcl1_set_nv_locked(void)
257 VBDEBUG("TPM: Set NV locked\n");
258 return tlcl1_define_space(TPM_NV_INDEX_LOCK
, 0, 0);
261 tpm_result_t
tlcl1_force_clear(void)
263 VBDEBUG("TPM: Force clear\n");
264 return send(tpm_forceclear_cmd
.buffer
);
267 tpm_result_t
tlcl1_set_enable(void)
269 VBDEBUG("TPM: Enabling TPM\n");
270 return send(tpm_physicalenable_cmd
.buffer
);
273 tpm_result_t
tlcl1_set_deactivated(uint8_t flag
)
275 struct s_tpm_physicalsetdeactivated_cmd cmd
;
276 VBDEBUG("TPM: SetDeactivated(%d)\n", flag
);
277 memcpy(&cmd
, &tpm_physicalsetdeactivated_cmd
, sizeof(cmd
));
278 *(cmd
.buffer
+ cmd
.deactivated
) = flag
;
279 return send(cmd
.buffer
);
282 tpm_result_t
tlcl1_get_permanent_flags(TPM_PERMANENT_FLAGS
*pflags
)
284 uint8_t response
[TPM_LARGE_ENOUGH_COMMAND_SIZE
];
287 tlcl1_send_receive(tpm_getflags_cmd
.buffer
, response
, sizeof(response
));
288 if (rc
!= TPM_SUCCESS
)
290 from_tpm_uint32(response
+ kTpmResponseHeaderLength
, &size
);
291 if (size
!= sizeof(TPM_PERMANENT_FLAGS
))
293 memcpy(pflags
, response
+ kTpmResponseHeaderLength
+ sizeof(size
),
294 sizeof(TPM_PERMANENT_FLAGS
));
298 tpm_result_t
tlcl1_get_flags(uint8_t *disable
, uint8_t *deactivated
, uint8_t *nvlocked
)
300 TPM_PERMANENT_FLAGS pflags
;
301 tpm_result_t rc
= tlcl1_get_permanent_flags(&pflags
);
302 if (rc
== TPM_SUCCESS
) {
304 *disable
= pflags
.disable
;
306 *deactivated
= pflags
.deactivated
;
308 *nvlocked
= pflags
.nvLocked
;
309 VBDEBUG("TPM: flags disable=%d, deactivated=%d, nvlocked=%d\n",
310 pflags
.disable
, pflags
.deactivated
, pflags
.nvLocked
);
315 tpm_result_t
tlcl1_set_global_lock(void)
317 VBDEBUG("TPM: Set global lock\n");
318 return tlcl1_write(TPM_NV_INDEX0
, NULL
, 0);
321 tpm_result_t
tlcl1_extend(int pcr_num
, const uint8_t *digest_data
,
322 enum vb2_hash_algorithm digest_algo
)
324 struct s_tpm_extend_cmd cmd
;
325 uint8_t response
[kTpmResponseHeaderLength
+ kPcrDigestLength
];
327 if (digest_algo
!= VB2_HASH_SHA1
)
328 return TPM_CB_INVALID_ARG
;
330 memcpy(&cmd
, &tpm_extend_cmd
, sizeof(cmd
));
331 to_tpm_uint32(cmd
.buffer
+ tpm_extend_cmd
.pcrNum
, pcr_num
);
332 memcpy(cmd
.buffer
+ cmd
.inDigest
, digest_data
, kPcrDigestLength
);
334 return tlcl1_send_receive(cmd
.buffer
, response
, sizeof(response
));
337 tpm_result_t
tlcl1_get_permissions(uint32_t index
, uint32_t *permissions
)
339 struct s_tpm_getpermissions_cmd cmd
;
340 uint8_t response
[TPM_LARGE_ENOUGH_COMMAND_SIZE
];
345 memcpy(&cmd
, &tpm_getpermissions_cmd
, sizeof(cmd
));
346 to_tpm_uint32(cmd
.buffer
+ tpm_getpermissions_cmd
.index
, index
);
347 rc
= tlcl1_send_receive(cmd
.buffer
, response
, sizeof(response
));
348 if (rc
!= TPM_SUCCESS
)
351 nvdata
= response
+ kTpmResponseHeaderLength
+ sizeof(size
);
352 from_tpm_uint32(nvdata
+ kNvDataPublicPermissionsOffset
, permissions
);