Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The Chromium OS Authors. All rights reserved. |
Frans Hendriks | 589eff7 | 2019-06-26 10:43:40 +0200 | [diff] [blame] | 3 | * Copyright 2017-2019 Eltan B.V. |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include <console/console.h> |
| 9 | #include <endian.h> |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 10 | #include <string.h> |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 11 | #include <vb2_api.h> |
Philipp Deppenwiese | 86391f1 | 2017-10-18 21:54:24 +0200 | [diff] [blame] | 12 | #include <security/tpm/tis.h> |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 13 | #include <security/tpm/tss.h> |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 14 | |
Philipp Deppenwiese | d88fb36 | 2017-10-18 20:26:18 +0200 | [diff] [blame] | 15 | #include "tss_structures.h" |
| 16 | #include "tss_marshaling.h" |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * This file provides interface between firmware and TPM2 device. The TPM1.2 |
| 20 | * API was copied as is and relevant functions modified to comply with the |
| 21 | * TPM2 specification. |
| 22 | */ |
| 23 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 24 | void *tpm_process_command(TPM_CC command, void *command_body) |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 25 | { |
Aaron Durbin | ee049fa | 2017-03-25 00:38:45 -0500 | [diff] [blame] | 26 | struct obuf ob; |
| 27 | struct ibuf ib; |
| 28 | size_t out_size; |
Duncan Laurie | ed75b27 | 2016-07-15 04:51:45 -0700 | [diff] [blame] | 29 | size_t in_size; |
Aaron Durbin | ee049fa | 2017-03-25 00:38:45 -0500 | [diff] [blame] | 30 | const uint8_t *sendb; |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 31 | /* Command/response buffer. */ |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame^] | 32 | static uint8_t cr_buffer[TPM_BUFFER_SIZE]; |
Victor Prupis | f706020 | 2016-08-19 10:45:04 -0700 | [diff] [blame] | 33 | |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame^] | 34 | obuf_init(&ob, cr_buffer, sizeof(cr_buffer)); |
Aaron Durbin | ee049fa | 2017-03-25 00:38:45 -0500 | [diff] [blame] | 35 | |
| 36 | if (tpm_marshal_command(command, command_body, &ob) < 0) { |
| 37 | printk(BIOS_ERR, "command %#x\n", command); |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 38 | return NULL; |
| 39 | } |
| 40 | |
Aaron Durbin | ee049fa | 2017-03-25 00:38:45 -0500 | [diff] [blame] | 41 | sendb = obuf_contents(&ob, &out_size); |
| 42 | |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 43 | in_size = sizeof(cr_buffer); |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame^] | 44 | if (tis_sendrecv(sendb, out_size, cr_buffer, &in_size)) { |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 45 | printk(BIOS_ERR, "tpm transaction failed\n"); |
| 46 | return NULL; |
| 47 | } |
| 48 | |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame^] | 49 | ibuf_init(&ib, cr_buffer, in_size); |
Aaron Durbin | ee049fa | 2017-03-25 00:38:45 -0500 | [diff] [blame] | 50 | |
| 51 | return tpm_unmarshal_response(command, &ib); |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Furquan Shaikh | cc3365a | 2016-09-30 12:53:19 -0700 | [diff] [blame] | 54 | static uint32_t tlcl_send_startup(TPM_SU type) |
| 55 | { |
| 56 | struct tpm2_startup startup; |
| 57 | struct tpm2_response *response; |
| 58 | |
| 59 | startup.startup_type = type; |
| 60 | response = tpm_process_command(TPM2_Startup, &startup); |
| 61 | |
Joel Kitching | c5d0a2e | 2018-10-12 15:52:00 +0800 | [diff] [blame] | 62 | /* IO error, tpm2_response pointer is empty. */ |
| 63 | if (response == NULL) { |
| 64 | printk(BIOS_ERR, "%s: TPM communication error\n", __func__); |
| 65 | return TPM_E_IOERROR; |
Furquan Shaikh | cc3365a | 2016-09-30 12:53:19 -0700 | [diff] [blame] | 66 | } |
Joel Kitching | c5d0a2e | 2018-10-12 15:52:00 +0800 | [diff] [blame] | 67 | |
Joel Kitching | f97ff0c | 2018-09-26 17:52:54 +0800 | [diff] [blame] | 68 | printk(BIOS_INFO, "%s: Startup return code is %x\n", |
| 69 | __func__, response->hdr.tpm_code); |
Joel Kitching | c5d0a2e | 2018-10-12 15:52:00 +0800 | [diff] [blame] | 70 | |
| 71 | switch (response->hdr.tpm_code) { |
| 72 | case TPM_RC_INITIALIZE: |
| 73 | /* TPM already initialized. */ |
| 74 | return TPM_E_INVALID_POSTINIT; |
| 75 | case TPM2_RC_SUCCESS: |
| 76 | return TPM_SUCCESS; |
| 77 | } |
| 78 | |
| 79 | /* Collapse any other errors into TPM_E_IOERROR. */ |
Joel Kitching | f97ff0c | 2018-09-26 17:52:54 +0800 | [diff] [blame] | 80 | return TPM_E_IOERROR; |
Furquan Shaikh | cc3365a | 2016-09-30 12:53:19 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 83 | uint32_t tlcl_resume(void) |
| 84 | { |
Furquan Shaikh | cc3365a | 2016-09-30 12:53:19 -0700 | [diff] [blame] | 85 | return tlcl_send_startup(TPM_SU_STATE); |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Joel Kitching | 2e690ee | 2018-11-15 16:48:53 +0800 | [diff] [blame] | 88 | static uint32_t tlcl_send_shutdown(TPM_SU type) |
| 89 | { |
| 90 | struct tpm2_shutdown shutdown; |
| 91 | struct tpm2_response *response; |
| 92 | |
| 93 | shutdown.shutdown_type = type; |
| 94 | response = tpm_process_command(TPM2_Shutdown, &shutdown); |
| 95 | |
| 96 | /* IO error, tpm2_response pointer is empty. */ |
| 97 | if (response == NULL) { |
| 98 | printk(BIOS_ERR, "%s: TPM communication error\n", __func__); |
| 99 | return TPM_E_IOERROR; |
| 100 | } |
| 101 | |
| 102 | printk(BIOS_INFO, "%s: Shutdown return code is %x\n", |
| 103 | __func__, response->hdr.tpm_code); |
| 104 | |
| 105 | if (response->hdr.tpm_code == TPM2_RC_SUCCESS) |
| 106 | return TPM_SUCCESS; |
| 107 | |
| 108 | /* Collapse any other errors into TPM_E_IOERROR. */ |
| 109 | return TPM_E_IOERROR; |
| 110 | } |
| 111 | |
| 112 | uint32_t tlcl_save_state(void) |
| 113 | { |
| 114 | return tlcl_send_shutdown(TPM_SU_STATE); |
| 115 | } |
| 116 | |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 117 | uint32_t tlcl_assert_physical_presence(void) |
| 118 | { |
| 119 | /* |
| 120 | * Nothing to do on TPM2 for this, use platform hierarchy availability |
| 121 | * instead. |
| 122 | */ |
| 123 | return TPM_SUCCESS; |
| 124 | } |
| 125 | |
Vadim Bendebury | f5ef699 | 2016-07-03 22:20:17 -0700 | [diff] [blame] | 126 | /* |
Julius Werner | b3426c0 | 2019-09-11 14:24:47 -0700 | [diff] [blame] | 127 | * The caller will provide the digest in a 32 byte buffer, let's consider it a |
| 128 | * sha256 digest. |
Vadim Bendebury | f5ef699 | 2016-07-03 22:20:17 -0700 | [diff] [blame] | 129 | */ |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 130 | uint32_t tlcl_extend(int pcr_num, const uint8_t *in_digest, |
| 131 | uint8_t *out_digest) |
| 132 | { |
Vadim Bendebury | f5ef699 | 2016-07-03 22:20:17 -0700 | [diff] [blame] | 133 | struct tpm2_pcr_extend_cmd pcr_ext_cmd; |
| 134 | struct tpm2_response *response; |
| 135 | |
| 136 | pcr_ext_cmd.pcrHandle = HR_PCR + pcr_num; |
Julius Werner | b3426c0 | 2019-09-11 14:24:47 -0700 | [diff] [blame] | 137 | pcr_ext_cmd.digests.count = 1; |
| 138 | pcr_ext_cmd.digests.digests[0].hashAlg = TPM_ALG_SHA256; |
| 139 | memcpy(pcr_ext_cmd.digests.digests[0].digest.sha256, in_digest, |
| 140 | sizeof(pcr_ext_cmd.digests.digests[0].digest.sha256)); |
Vadim Bendebury | f5ef699 | 2016-07-03 22:20:17 -0700 | [diff] [blame] | 141 | |
| 142 | response = tpm_process_command(TPM2_PCR_Extend, &pcr_ext_cmd); |
| 143 | |
Julius Werner | b3426c0 | 2019-09-11 14:24:47 -0700 | [diff] [blame] | 144 | printk(BIOS_INFO, "%s: response is %x\n", |
Vadim Bendebury | f5ef699 | 2016-07-03 22:20:17 -0700 | [diff] [blame] | 145 | __func__, response ? response->hdr.tpm_code : -1); |
| 146 | if (!response || response->hdr.tpm_code) |
| 147 | return TPM_E_IOERROR; |
| 148 | |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 149 | return TPM_SUCCESS; |
| 150 | } |
| 151 | |
| 152 | uint32_t tlcl_finalize_physical_presence(void) |
| 153 | { |
| 154 | /* Nothing needs to be done with tpm2. */ |
| 155 | printk(BIOS_INFO, "%s:%s:%d\n", __FILE__, __func__, __LINE__); |
| 156 | return TPM_SUCCESS; |
| 157 | } |
| 158 | |
| 159 | uint32_t tlcl_force_clear(void) |
| 160 | { |
Vadim Bendebury | adfbbde | 2016-07-03 15:56:41 -0700 | [diff] [blame] | 161 | struct tpm2_response *response; |
| 162 | |
| 163 | response = tpm_process_command(TPM2_Clear, NULL); |
| 164 | printk(BIOS_INFO, "%s: response is %x\n", |
| 165 | __func__, response ? response->hdr.tpm_code : -1); |
| 166 | |
| 167 | if (!response || response->hdr.tpm_code) |
| 168 | return TPM_E_IOERROR; |
| 169 | |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 170 | return TPM_SUCCESS; |
| 171 | } |
| 172 | |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame^] | 173 | static uint8_t tlcl_init_done; |
Furquan Shaikh | 8b5d04e | 2016-11-10 09:49:05 -0800 | [diff] [blame] | 174 | |
| 175 | /* This function is called directly by vboot, uses vboot return types. */ |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 176 | uint32_t tlcl_lib_init(void) |
| 177 | { |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame^] | 178 | if (tlcl_init_done) |
Furquan Shaikh | 8b5d04e | 2016-11-10 09:49:05 -0800 | [diff] [blame] | 179 | return VB2_SUCCESS; |
| 180 | |
Frans Hendriks | 3891d27 | 2019-06-12 11:14:55 +0200 | [diff] [blame] | 181 | if (tis_init()) { |
| 182 | printk(BIOS_ERR, "%s: tis_init returned error\n", __func__); |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 183 | return VB2_ERROR_UNKNOWN; |
Frans Hendriks | 3891d27 | 2019-06-12 11:14:55 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | if (tis_open()) { |
| 187 | printk(BIOS_ERR, "%s: tis_open returned error\n", __func__); |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 188 | return VB2_ERROR_UNKNOWN; |
Frans Hendriks | 3891d27 | 2019-06-12 11:14:55 +0200 | [diff] [blame] | 189 | } |
Furquan Shaikh | 8b5d04e | 2016-11-10 09:49:05 -0800 | [diff] [blame] | 190 | |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame^] | 191 | tlcl_init_done = 1; |
Furquan Shaikh | 8b5d04e | 2016-11-10 09:49:05 -0800 | [diff] [blame] | 192 | |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 193 | return VB2_SUCCESS; |
| 194 | } |
| 195 | |
| 196 | uint32_t tlcl_physical_presence_cmd_enable(void) |
| 197 | { |
| 198 | printk(BIOS_INFO, "%s:%s:%d\n", __FILE__, __func__, __LINE__); |
| 199 | return TPM_SUCCESS; |
| 200 | } |
| 201 | |
| 202 | uint32_t tlcl_read(uint32_t index, void *data, uint32_t length) |
| 203 | { |
| 204 | struct tpm2_nv_read_cmd nv_readc; |
| 205 | struct tpm2_response *response; |
| 206 | |
| 207 | memset(&nv_readc, 0, sizeof(nv_readc)); |
| 208 | |
| 209 | nv_readc.nvIndex = HR_NV_INDEX + index; |
| 210 | nv_readc.size = length; |
| 211 | |
| 212 | response = tpm_process_command(TPM2_NV_Read, &nv_readc); |
| 213 | |
| 214 | /* Need to map tpm error codes into internal values. */ |
| 215 | if (!response) |
| 216 | return TPM_E_READ_FAILURE; |
| 217 | |
| 218 | printk(BIOS_INFO, "%s:%d index %#x return code %x\n", |
| 219 | __FILE__, __LINE__, index, response->hdr.tpm_code); |
| 220 | switch (response->hdr.tpm_code) { |
| 221 | case 0: |
| 222 | break; |
| 223 | |
Vadim Bendebury | 08f9359 | 2017-06-21 12:23:22 -0700 | [diff] [blame] | 224 | /* Uninitialized, returned if the space hasn't been written. */ |
| 225 | case TPM_RC_NV_UNINITIALIZED: |
| 226 | /* |
| 227 | * Bad index, cr50 specific value, returned if the space |
| 228 | * hasn't been defined. |
| 229 | */ |
| 230 | case TPM_RC_CR50_NV_UNDEFINED: |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 231 | return TPM_E_BADINDEX; |
| 232 | |
| 233 | default: |
| 234 | return TPM_E_READ_FAILURE; |
| 235 | } |
| 236 | |
| 237 | if (length > response->nvr.buffer.t.size) |
| 238 | return TPM_E_RESPONSE_TOO_LARGE; |
| 239 | |
| 240 | if (length < response->nvr.buffer.t.size) |
| 241 | return TPM_E_READ_EMPTY; |
| 242 | |
| 243 | memcpy(data, response->nvr.buffer.t.buffer, length); |
| 244 | |
| 245 | return TPM_SUCCESS; |
| 246 | } |
| 247 | |
| 248 | uint32_t tlcl_self_test_full(void) |
| 249 | { |
| 250 | struct tpm2_self_test st; |
| 251 | struct tpm2_response *response; |
| 252 | |
| 253 | st.yes_no = 1; |
| 254 | |
| 255 | response = tpm_process_command(TPM2_SelfTest, &st); |
| 256 | printk(BIOS_INFO, "%s: response is %x\n", |
| 257 | __func__, response ? response->hdr.tpm_code : -1); |
| 258 | return TPM_SUCCESS; |
| 259 | } |
| 260 | |
Vadim Bendebury | 4c0851c | 2016-07-03 17:08:10 -0700 | [diff] [blame] | 261 | uint32_t tlcl_lock_nv_write(uint32_t index) |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 262 | { |
Vadim Bendebury | 4c0851c | 2016-07-03 17:08:10 -0700 | [diff] [blame] | 263 | struct tpm2_response *response; |
| 264 | /* TPM Wll reject attempts to write at non-defined index. */ |
| 265 | struct tpm2_nv_write_lock_cmd nv_wl = { |
| 266 | .nvIndex = HR_NV_INDEX + index, |
| 267 | }; |
| 268 | |
| 269 | response = tpm_process_command(TPM2_NV_WriteLock, &nv_wl); |
| 270 | |
| 271 | printk(BIOS_INFO, "%s: response is %x\n", |
| 272 | __func__, response ? response->hdr.tpm_code : -1); |
| 273 | |
| 274 | if (!response || response->hdr.tpm_code) |
| 275 | return TPM_E_IOERROR; |
| 276 | |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 277 | return TPM_SUCCESS; |
| 278 | } |
| 279 | |
| 280 | uint32_t tlcl_startup(void) |
| 281 | { |
Furquan Shaikh | cc3365a | 2016-09-30 12:53:19 -0700 | [diff] [blame] | 282 | return tlcl_send_startup(TPM_SU_CLEAR); |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | uint32_t tlcl_write(uint32_t index, const void *data, uint32_t length) |
| 286 | { |
| 287 | struct tpm2_nv_write_cmd nv_writec; |
| 288 | struct tpm2_response *response; |
| 289 | |
| 290 | memset(&nv_writec, 0, sizeof(nv_writec)); |
| 291 | |
| 292 | nv_writec.nvIndex = HR_NV_INDEX + index; |
| 293 | nv_writec.data.t.size = length; |
| 294 | nv_writec.data.t.buffer = data; |
| 295 | |
| 296 | response = tpm_process_command(TPM2_NV_Write, &nv_writec); |
| 297 | |
Vadim Bendebury | 1ec7603 | 2016-07-05 22:30:16 -0700 | [diff] [blame] | 298 | printk(BIOS_INFO, "%s: response is %x\n", |
| 299 | __func__, response ? response->hdr.tpm_code : -1); |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 300 | |
Vadim Bendebury | 1ec7603 | 2016-07-05 22:30:16 -0700 | [diff] [blame] | 301 | /* Need to map tpm error codes into internal values. */ |
| 302 | if (!response || response->hdr.tpm_code) |
| 303 | return TPM_E_WRITE_FAILURE; |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 304 | |
| 305 | return TPM_SUCCESS; |
| 306 | } |
| 307 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 308 | uint32_t tlcl_define_space(uint32_t space_index, size_t space_size, |
| 309 | const TPMA_NV nv_attributes, |
| 310 | const uint8_t *nv_policy, size_t nv_policy_size) |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 311 | { |
| 312 | struct tpm2_nv_define_space_cmd nvds_cmd; |
| 313 | struct tpm2_response *response; |
| 314 | |
| 315 | /* Prepare the define space command structure. */ |
| 316 | memset(&nvds_cmd, 0, sizeof(nvds_cmd)); |
| 317 | |
| 318 | nvds_cmd.publicInfo.dataSize = space_size; |
| 319 | nvds_cmd.publicInfo.nvIndex = HR_NV_INDEX + space_index; |
| 320 | nvds_cmd.publicInfo.nameAlg = TPM_ALG_SHA256; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 321 | nvds_cmd.publicInfo.attributes = nv_attributes; |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 322 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 323 | /* |
| 324 | * Use policy digest based on default pcr0 value. This makes |
| 325 | * sure that the space can not be deleted as soon as PCR0 |
| 326 | * value has been extended from default. |
| 327 | */ |
| 328 | if (nv_policy && nv_policy_size) { |
| 329 | nvds_cmd.publicInfo.authPolicy.t.buffer = nv_policy; |
| 330 | nvds_cmd.publicInfo.authPolicy.t.size = nv_policy_size; |
Vadim Bendebury | 289ee8f | 2016-11-11 09:36:50 -0800 | [diff] [blame] | 331 | } |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 332 | |
| 333 | response = tpm_process_command(TPM2_NV_DefineSpace, &nvds_cmd); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 334 | printk(BIOS_INFO, "%s: response is %x\n", __func__, |
| 335 | response ? response->hdr.tpm_code : -1); |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 336 | |
| 337 | if (!response) |
| 338 | return TPM_E_NO_DEVICE; |
| 339 | |
Vadim Bendebury | af8ae93 | 2016-11-11 14:15:31 -0800 | [diff] [blame] | 340 | /* Map TPM2 retrun codes into common vboot represenation. */ |
Lee Leahy | 45fde70 | 2017-03-08 18:02:24 -0800 | [diff] [blame] | 341 | switch (response->hdr.tpm_code) { |
Vadim Bendebury | af8ae93 | 2016-11-11 14:15:31 -0800 | [diff] [blame] | 342 | case TPM2_RC_SUCCESS: |
| 343 | return TPM_SUCCESS; |
| 344 | case TPM2_RC_NV_DEFINED: |
| 345 | return TPM_E_NV_DEFINED; |
| 346 | default: |
| 347 | return TPM_E_INTERNAL_INCONSISTENCY; |
| 348 | } |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 349 | } |
Aaron Durbin | f56c778 | 2017-01-10 17:44:42 -0600 | [diff] [blame] | 350 | |
Frans Hendriks | 7e220ca | 2019-06-28 10:18:22 +0200 | [diff] [blame] | 351 | uint16_t tlcl_get_hash_size_from_algo(TPMI_ALG_HASH hash_algo) |
| 352 | { |
| 353 | uint16_t value; |
| 354 | |
| 355 | switch (hash_algo) { |
| 356 | case TPM_ALG_ERROR: |
| 357 | value = 1; |
| 358 | break; |
| 359 | case TPM_ALG_SHA1: |
| 360 | value = SHA1_DIGEST_SIZE; |
| 361 | break; |
| 362 | case TPM_ALG_SHA256: |
| 363 | value = SHA256_DIGEST_SIZE; |
| 364 | break; |
| 365 | case TPM_ALG_SHA384: |
| 366 | value = SHA384_DIGEST_SIZE; |
| 367 | break; |
| 368 | case TPM_ALG_SHA512: |
| 369 | value = SHA512_DIGEST_SIZE; |
| 370 | break; |
| 371 | case TPM_ALG_SM3_256: |
| 372 | value = SM3_256_DIGEST_SIZE; |
| 373 | break; |
| 374 | default: |
| 375 | printk(BIOS_SPEW, "%s: unknown hash algorithm %d\n", __func__, |
| 376 | hash_algo); |
| 377 | value = 0; |
| 378 | }; |
| 379 | |
| 380 | return value; |
| 381 | } |
| 382 | |
Aaron Durbin | f56c778 | 2017-01-10 17:44:42 -0600 | [diff] [blame] | 383 | uint32_t tlcl_disable_platform_hierarchy(void) |
| 384 | { |
| 385 | struct tpm2_response *response; |
| 386 | struct tpm2_hierarchy_control_cmd hc = { |
| 387 | .enable = TPM_RH_PLATFORM, |
| 388 | .state = 0, |
| 389 | }; |
| 390 | |
| 391 | response = tpm_process_command(TPM2_Hierarchy_Control, &hc); |
| 392 | |
| 393 | if (!response || response->hdr.tpm_code) |
| 394 | return TPM_E_INTERNAL_INCONSISTENCY; |
| 395 | |
| 396 | return TPM_SUCCESS; |
| 397 | } |
Frans Hendriks | 589eff7 | 2019-06-26 10:43:40 +0200 | [diff] [blame] | 398 | |
| 399 | uint32_t tlcl_get_capability(TPM_CAP capability, uint32_t property, |
| 400 | uint32_t property_count, |
| 401 | TPMS_CAPABILITY_DATA *capability_data) |
| 402 | { |
| 403 | struct tpm2_get_capability cmd; |
| 404 | struct tpm2_response *response; |
| 405 | |
| 406 | cmd.capability = capability; |
| 407 | cmd.property = property; |
| 408 | cmd.propertyCount = property_count; |
| 409 | |
| 410 | if (property_count > 1) { |
| 411 | printk(BIOS_ERR, "%s: property_count more than one not " |
| 412 | "supported yet\n", __func__); |
| 413 | return TPM_E_IOERROR; |
| 414 | } |
| 415 | |
| 416 | response = tpm_process_command(TPM2_GetCapability, &cmd); |
| 417 | |
| 418 | if (!response) { |
| 419 | printk(BIOS_ERR, "%s: Command Failed\n", __func__); |
| 420 | return TPM_E_IOERROR; |
| 421 | } |
| 422 | |
| 423 | memcpy(capability_data, &response->gc.cd, sizeof(TPMS_CAPABILITY_DATA)); |
| 424 | return TPM_SUCCESS; |
| 425 | } |