Angel Pons | 986d50e | 2020-04-02 23:48:53 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 2 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 3 | #include <console/console.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 4 | #include <security/tpm/tspi/crtm.h> |
Sergii Dmytruk | 4191dbf | 2022-10-23 00:34:32 +0300 | [diff] [blame] | 5 | #include <security/tpm/tspi/logs.h> |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 6 | #include <security/tpm/tspi.h> |
| 7 | #include <security/tpm/tss.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 8 | #include <assert.h> |
| 9 | #include <security/vboot/misc.h> |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 10 | #include <vb2_api.h> |
Joel Kitching | 2eb89c8 | 2019-04-25 17:45:12 +0800 | [diff] [blame] | 11 | #include <vb2_sha.h> |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 12 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 13 | #if CONFIG(TPM1) |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 14 | static tpm_result_t tpm1_invoke_state_machine(void) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 15 | { |
Philipp Deppenwiese | 4d2af9d | 2018-08-14 09:46:55 -0700 | [diff] [blame] | 16 | uint8_t disabled; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 17 | uint8_t deactivated; |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 18 | tpm_result_t rc = TPM_SUCCESS; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 19 | |
Sergii Dmytruk | 47e9e8c | 2022-11-02 00:50:03 +0200 | [diff] [blame^] | 20 | if (tlcl_get_family() != TPM_1) |
| 21 | return rc; |
| 22 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 23 | /* Check that the TPM is enabled and activated. */ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 24 | rc = tlcl1_get_flags(&disabled, &deactivated, NULL); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 25 | if (rc != TPM_SUCCESS) { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 26 | printk(BIOS_ERR, "TPM Error (%#x): Can't read capabilities.\n", rc); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 27 | return rc; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 28 | } |
| 29 | |
Philipp Deppenwiese | 4d2af9d | 2018-08-14 09:46:55 -0700 | [diff] [blame] | 30 | if (disabled) { |
| 31 | printk(BIOS_INFO, "TPM: is disabled. Enabling...\n"); |
| 32 | |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 33 | rc = tlcl1_set_enable(); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 34 | if (rc != TPM_SUCCESS) { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 35 | printk(BIOS_ERR, "TPM Error (%#x): Can't set enabled state.\n", rc); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 36 | return rc; |
Philipp Deppenwiese | 4d2af9d | 2018-08-14 09:46:55 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 40 | if (!!deactivated != CONFIG(TPM_DEACTIVATE)) { |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 41 | printk(BIOS_INFO, |
| 42 | "TPM: Unexpected TPM deactivated state. Toggling...\n"); |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 43 | rc = tlcl1_set_deactivated(!deactivated); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 44 | if (rc != TPM_SUCCESS) { |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 45 | printk(BIOS_ERR, |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 46 | "TPM Error (%#x): Can't toggle deactivated state.\n", rc); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 47 | return rc; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | deactivated = !deactivated; |
Jon Murphy | 056952e | 2023-09-05 10:44:09 -0600 | [diff] [blame] | 51 | rc = TPM_CB_MUST_REBOOT; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 54 | return rc; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 55 | } |
| 56 | #endif |
| 57 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 58 | static tpm_result_t tpm_setup_s3_helper(void) |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 59 | { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 60 | tpm_result_t rc = tlcl_resume(); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 61 | switch (rc) { |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 62 | case TPM_SUCCESS: |
| 63 | break; |
| 64 | |
Jon Murphy | 056952e | 2023-09-05 10:44:09 -0600 | [diff] [blame] | 65 | case TPM_INVALID_POSTINIT: |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 66 | /* |
| 67 | * We're on a platform where the TPM maintains power |
| 68 | * in S3, so it's already initialized. |
| 69 | */ |
| 70 | printk(BIOS_INFO, "TPM: Already initialized.\n"); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 71 | rc = TPM_SUCCESS; |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 72 | break; |
| 73 | |
| 74 | default: |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 75 | printk(BIOS_ERR, "TPM: Resume failed (%#x).\n", rc); |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 76 | break; |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 77 | } |
| 78 | |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 79 | return rc; |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 80 | } |
| 81 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 82 | static tpm_result_t tpm_setup_epilogue(tpm_result_t rc) |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 83 | { |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 84 | if (rc != TPM_SUCCESS) |
lilacious | 40cb3fe | 2023-06-21 23:24:14 +0200 | [diff] [blame] | 85 | post_code(POSTCODE_TPM_FAILURE); |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 86 | else |
| 87 | printk(BIOS_INFO, "TPM: setup succeeded\n"); |
| 88 | |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 89 | return rc; |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 90 | } |
| 91 | |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 92 | static int tpm_is_setup; |
| 93 | static inline int tspi_tpm_is_setup(void) |
| 94 | { |
Julius Werner | 23a82e8 | 2020-03-31 13:32:10 -0700 | [diff] [blame] | 95 | /* |
| 96 | * vboot_logic_executed() only starts returning true at the end of |
| 97 | * verstage, but the vboot logic itself already wants to extend PCRs |
| 98 | * before that. So in the stage where verification actually runs, we |
| 99 | * need to check tpm_is_setup. Skip that check in all other stages so |
| 100 | * this whole function can be evaluated at compile time. |
| 101 | */ |
| 102 | if (CONFIG(VBOOT)) { |
| 103 | if (verification_should_run()) |
| 104 | return tpm_is_setup; |
| 105 | return vboot_logic_executed(); |
| 106 | } |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 107 | |
Arthur Heymans | 6f8e944 | 2021-03-29 14:23:53 +0200 | [diff] [blame] | 108 | if (CONFIG(TPM_MEASURED_BOOT_INIT_BOOTBLOCK)) |
| 109 | return ENV_BOOTBLOCK ? tpm_is_setup : 1; |
| 110 | |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 111 | if (ENV_RAMSTAGE) |
| 112 | return tpm_is_setup; |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 117 | /* |
| 118 | * tpm_setup starts the TPM and establishes the root of trust for the |
Jonathan Neuschäfer | 61322d7 | 2018-10-29 22:52:42 +0100 | [diff] [blame] | 119 | * anti-rollback mechanism. tpm_setup can fail for three reasons. 1 A bug. |
| 120 | * 2 a TPM hardware failure. 3 An unexpected TPM state due to some attack. In |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 121 | * general we cannot easily distinguish the kind of failure, so our strategy is |
Jonathan Neuschäfer | 61322d7 | 2018-10-29 22:52:42 +0100 | [diff] [blame] | 122 | * to reboot in recovery mode in all cases. The recovery mode calls tpm_setup |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 123 | * again, which executes (almost) the same sequence of operations. There is a |
| 124 | * good chance that, if recovery mode was entered because of a TPM failure, the |
| 125 | * failure will repeat itself. (In general this is impossible to guarantee |
| 126 | * because we have no way of creating the exact TPM initial state at the |
| 127 | * previous boot.) In recovery mode, we ignore the failure and continue, thus |
| 128 | * giving the recovery kernel a chance to fix things (that's why we don't set |
| 129 | * bGlobalLock). The choice is between a knowingly insecure device and a |
| 130 | * bricked device. |
| 131 | * |
| 132 | * As a side note, observe that we go through considerable hoops to avoid using |
| 133 | * the STCLEAR permissions for the index spaces. We do this to avoid writing |
| 134 | * to the TPM flashram at every reboot or wake-up, because of concerns about |
| 135 | * the durability of the NVRAM. |
| 136 | */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 137 | tpm_result_t tpm_setup(int s3flag) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 138 | { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 139 | tpm_result_t rc; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 140 | |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 141 | rc = tlcl_lib_init(); |
| 142 | if (rc != TPM_SUCCESS) { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 143 | printk(BIOS_ERR, "TPM Error (%#x): Can't initialize.\n", rc); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 144 | return tpm_setup_epilogue(rc); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* Handle special init for S3 resume path */ |
| 148 | if (s3flag) { |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 149 | printk(BIOS_INFO, "TPM: Handle S3 resume.\n"); |
| 150 | return tpm_setup_epilogue(tpm_setup_s3_helper()); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 153 | rc = tlcl_startup(); |
Arthur Heymans | 6d5fcf4 | 2019-10-14 17:06:27 +0200 | [diff] [blame] | 154 | if (CONFIG(TPM_STARTUP_IGNORE_POSTINIT) |
Jon Murphy | 056952e | 2023-09-05 10:44:09 -0600 | [diff] [blame] | 155 | && rc == TPM_INVALID_POSTINIT) { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 156 | printk(BIOS_DEBUG, "TPM Warn(%#x): ignoring invalid POSTINIT\n", rc); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 157 | rc = TPM_SUCCESS; |
Arthur Heymans | 6d5fcf4 | 2019-10-14 17:06:27 +0200 | [diff] [blame] | 158 | } |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 159 | if (rc != TPM_SUCCESS) { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 160 | printk(BIOS_ERR, "TPM Error (%#x): Can't run startup command.\n", rc); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 161 | return tpm_setup_epilogue(rc); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 164 | rc = tlcl_assert_physical_presence(); |
| 165 | if (rc != TPM_SUCCESS) { |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 166 | /* |
| 167 | * It is possible that the TPM was delivered with the physical |
| 168 | * presence command disabled. This tries enabling it, then |
| 169 | * tries asserting PP again. |
| 170 | */ |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 171 | rc = tlcl_physical_presence_cmd_enable(); |
| 172 | if (rc != TPM_SUCCESS) { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 173 | printk(BIOS_ERR, "TPM Error (%#x): Can't enable physical presence command.\n", rc); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 174 | return tpm_setup_epilogue(rc); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 177 | rc = tlcl_assert_physical_presence(); |
| 178 | if (rc != TPM_SUCCESS) { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 179 | printk(BIOS_ERR, "TPM Error (%#x): Can't assert physical presence.\n", rc); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 180 | return tpm_setup_epilogue(rc); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 184 | #if CONFIG(TPM1) |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 185 | rc = tpm1_invoke_state_machine(); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 186 | #endif |
Arthur Heymans | b192af1 | 2021-05-20 09:09:56 +0200 | [diff] [blame] | 187 | if (CONFIG(TPM_MEASURED_BOOT)) |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 188 | rc = tspi_measure_cache_to_pcr(); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 189 | |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 190 | tpm_is_setup = 1; |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 191 | return tpm_setup_epilogue(rc); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 194 | tpm_result_t tpm_clear_and_reenable(void) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 195 | { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 196 | tpm_result_t rc; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 197 | |
| 198 | printk(BIOS_INFO, "TPM: Clear and re-enable\n"); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 199 | rc = tlcl_force_clear(); |
| 200 | if (rc != TPM_SUCCESS) { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 201 | printk(BIOS_ERR, "TPM Error (%#x): Can't initiate a force clear.\n", rc); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 202 | return rc; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Sergii Dmytruk | 47e9e8c | 2022-11-02 00:50:03 +0200 | [diff] [blame^] | 205 | if (tlcl_get_family() == TPM_1) { |
| 206 | rc = tlcl1_set_enable(); |
| 207 | if (rc != TPM_SUCCESS) { |
| 208 | printk(BIOS_ERR, "TPM Error (%#x): Can't set enabled state.\n", rc); |
| 209 | return rc; |
| 210 | } |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 211 | |
Sergii Dmytruk | 47e9e8c | 2022-11-02 00:50:03 +0200 | [diff] [blame^] | 212 | rc = tlcl1_set_deactivated(0); |
| 213 | if (rc != TPM_SUCCESS) { |
| 214 | printk(BIOS_ERR, "TPM Error (%#x): Can't set deactivated state.\n", rc); |
| 215 | return rc; |
| 216 | } |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 217 | } |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 218 | |
| 219 | return TPM_SUCCESS; |
| 220 | } |
| 221 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 222 | tpm_result_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo, |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 223 | const uint8_t *digest, size_t digest_len, const char *name) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 224 | { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 225 | tpm_result_t rc; |
Philipp Deppenwiese | f849972 | 2018-07-30 01:27:47 +0200 | [diff] [blame] | 226 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 227 | if (!digest) |
Jon Murphy | 056952e | 2023-09-05 10:44:09 -0600 | [diff] [blame] | 228 | return TPM_IOERROR; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 229 | |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 230 | if (tspi_tpm_is_setup()) { |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 231 | rc = tlcl_lib_init(); |
| 232 | if (rc != TPM_SUCCESS) { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 233 | printk(BIOS_ERR, "TPM Error (%#x): Can't initialize library.\n", rc); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 234 | return rc; |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 235 | } |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 236 | |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 237 | printk(BIOS_DEBUG, "TPM: Extending digest for `%s` into PCR %d\n", name, pcr); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 238 | rc = tlcl_extend(pcr, digest, digest_algo); |
| 239 | if (rc != TPM_SUCCESS) { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 240 | printk(BIOS_ERR, "TPM Error (%#x): Extending hash for `%s` into PCR %d failed.\n", |
| 241 | rc, name, pcr); |
Jon Murphy | 2460481 | 2023-09-05 10:37:05 -0600 | [diff] [blame] | 242 | return rc; |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 243 | } |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | if (CONFIG(TPM_MEASURED_BOOT)) |
Sergii Dmytruk | 2710df7 | 2022-11-10 00:40:51 +0200 | [diff] [blame] | 247 | tpm_log_add_table_entry(name, pcr, digest_algo, digest, digest_len); |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 248 | |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 249 | printk(BIOS_DEBUG, "TPM: Digest of `%s` to PCR %d %s\n", |
| 250 | name, pcr, tspi_tpm_is_setup() ? "measured" : "logged"); |
| 251 | |
Furquan Shaikh | 38f3ffa | 2018-07-31 14:26:39 -0700 | [diff] [blame] | 252 | return TPM_SUCCESS; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 253 | } |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 254 | |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 255 | #if CONFIG(VBOOT_LIB) |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 256 | tpm_result_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr, |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 257 | const char *rname) |
| 258 | { |
| 259 | uint8_t digest[TPM_PCR_MAX_LEN], digest_len; |
| 260 | uint8_t buf[HASH_DATA_CHUNK_SIZE]; |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 261 | uint32_t offset; |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 262 | size_t len; |
| 263 | struct vb2_digest_context ctx; |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 264 | |
| 265 | if (!rdev || !rname) |
Jon Murphy | 056952e | 2023-09-05 10:44:09 -0600 | [diff] [blame] | 266 | return TPM_CB_INVALID_ARG; |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 267 | |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 268 | digest_len = vb2_digest_size(TPM_MEASURE_ALGO); |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 269 | assert(digest_len <= sizeof(digest)); |
Julius Werner | d96ca24 | 2022-08-08 18:08:35 -0700 | [diff] [blame] | 270 | if (vb2_digest_init(&ctx, vboot_hwcrypto_allowed(), TPM_MEASURE_ALGO, |
| 271 | region_device_sz(rdev))) { |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 272 | printk(BIOS_ERR, "TPM: Error initializing hash.\n"); |
Jon Murphy | 056952e | 2023-09-05 10:44:09 -0600 | [diff] [blame] | 273 | return TPM_CB_HASH_ERROR; |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 274 | } |
| 275 | /* |
| 276 | * Though one can mmap the full needed region on x86 this is not the |
| 277 | * case for e.g. ARM. In order to make this code as universal as |
| 278 | * possible across different platforms read the data to hash in chunks. |
| 279 | */ |
| 280 | for (offset = 0; offset < region_device_sz(rdev); offset += len) { |
| 281 | len = MIN(sizeof(buf), region_device_sz(rdev) - offset); |
| 282 | if (rdev_readat(rdev, buf, offset, len) < 0) { |
| 283 | printk(BIOS_ERR, "TPM: Not able to read region %s.\n", |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 284 | rname); |
Jon Murphy | 056952e | 2023-09-05 10:44:09 -0600 | [diff] [blame] | 285 | return TPM_CB_READ_FAILURE; |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 286 | } |
| 287 | if (vb2_digest_extend(&ctx, buf, len)) { |
| 288 | printk(BIOS_ERR, "TPM: Error extending hash.\n"); |
Jon Murphy | 056952e | 2023-09-05 10:44:09 -0600 | [diff] [blame] | 289 | return TPM_CB_HASH_ERROR; |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | if (vb2_digest_finalize(&ctx, digest, digest_len)) { |
| 293 | printk(BIOS_ERR, "TPM: Error finalizing hash.\n"); |
Jon Murphy | 056952e | 2023-09-05 10:44:09 -0600 | [diff] [blame] | 294 | return TPM_CB_HASH_ERROR; |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 295 | } |
Julius Werner | 7e7cc1a | 2021-08-11 18:19:23 -0700 | [diff] [blame] | 296 | return tpm_extend_pcr(pcr, TPM_MEASURE_ALGO, digest, digest_len, rname); |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 297 | } |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 298 | #endif /* VBOOT_LIB */ |