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