blob: 0ae956276cf2621035426ec4813a6290928c5b05 [file] [log] [blame]
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -07001/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -07002 *
Martin Roth08d808f2017-02-09 18:06:16 -08003 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following disclaimer
11 * in the documentation and/or other materials provided with the
12 * distribution.
13 * * Neither the name of Google Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070031 * Functions for querying, manipulating and locking rollback indices
32 * stored in the TPM NVRAM.
33 */
34
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010035#include <security/vboot/antirollback.h>
Christian Walter0bd84ed2019-07-23 10:26:30 +020036#include <security/vboot/tpm_common.h>
Randall Spangler144c2282014-12-03 17:35:53 -080037#include <string.h>
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010038#include <security/tpm/tspi.h>
Randall Spangler144c2282014-12-03 17:35:53 -080039#include <vb2_api.h>
Vadim Bendebury10ea1042016-06-06 12:12:34 -070040#include <console/console.h>
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070041
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070042#ifdef FOR_TEST
Daisuke Nojiri57990972014-07-15 19:47:32 -070043#include <stdio.h>
44#define VBDEBUG(format, args...) printf(format, ## args)
45#else
Daisuke Nojiri57990972014-07-15 19:47:32 -070046#define VBDEBUG(format, args...) \
47 printk(BIOS_INFO, "%s():%d: " format, __func__, __LINE__, ## args)
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070048#endif
49
Daisuke Nojiri57990972014-07-15 19:47:32 -070050#define RETURN_ON_FAILURE(tpm_cmd) do { \
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070051 uint32_t result_; \
Daisuke Nojiri57990972014-07-15 19:47:32 -070052 if ((result_ = (tpm_cmd)) != TPM_SUCCESS) { \
53 VBDEBUG("Antirollback: %08x returned by " #tpm_cmd \
54 "\n", (int)result_); \
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070055 return result_; \
56 } \
57 } while (0)
58
Philipp Deppenwiesef8499722018-07-30 01:27:47 +020059#define TPM_PCR_GBB_FLAGS_NAME "GBB flags"
60#define TPM_PCR_GBB_HWID_NAME "GBB HWID"
Julius Werner76e33032015-01-30 18:45:27 -080061
Vadim Bendebury10ea1042016-06-06 12:12:34 -070062static uint32_t safe_write(uint32_t index, const void *data, uint32_t length);
63
Daisuke Nojiri57990972014-07-15 19:47:32 -070064static uint32_t read_space_firmware(struct vb2_context *ctx)
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070065{
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070066 int attempts = 3;
67
68 while (attempts--) {
Joel Kitching928511a2019-12-11 17:25:02 +080069 RETURN_ON_FAILURE(tlcl_read(FIRMWARE_NV_INDEX,
70 ctx->secdata_firmware,
71 VB2_SECDATA_FIRMWARE_SIZE));
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070072
Joel Kitching928511a2019-12-11 17:25:02 +080073 if (vb2api_secdata_firmware_check(ctx) == VB2_SUCCESS)
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070074 return TPM_SUCCESS;
75
Daisuke Nojiri57990972014-07-15 19:47:32 -070076 VBDEBUG("TPM: %s() - bad CRC\n", __func__);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070077 }
78
Daisuke Nojiri57990972014-07-15 19:47:32 -070079 VBDEBUG("TPM: %s() - too many bad CRCs, giving up\n", __func__);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070080 return TPM_E_CORRUPTED_STATE;
81}
82
Furquan Shaikhb038f412016-11-07 23:47:11 -080083static uint32_t read_space_rec_hash(uint8_t *data)
84{
85 RETURN_ON_FAILURE(tlcl_read(REC_HASH_NV_INDEX, data,
86 REC_HASH_NV_SIZE));
87 return TPM_SUCCESS;
88}
89
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -070090static uint32_t write_secdata(uint32_t index,
91 const uint8_t *secdata,
92 uint32_t len)
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070093{
dnojiri58cf6032020-01-28 12:34:20 -080094 uint8_t sd[MAX(VB2_SECDATA_KERNEL_SIZE, VB2_SECDATA_FIRMWARE_SIZE)];
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -070095 uint32_t rv;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070096 int attempts = 3;
97
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -070098 if (len > sizeof(sd)) {
99 VBDEBUG("TPM: %s() - data is too large\n", __func__);
100 return TPM_E_WRITE_FAILURE;
101 }
102
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700103 while (attempts--) {
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -0700104 rv = safe_write(index, secdata, len);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700105 /* Can't write, not gonna try again */
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -0700106 if (rv != TPM_SUCCESS)
107 return rv;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700108
109 /* Read it back to be sure it got the right values. */
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -0700110 rv = tlcl_read(index, sd, len);
111 if (rv == TPM_SUCCESS && memcmp(secdata, sd, len) == 0)
112 return rv;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700113
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -0700114 VBDEBUG("TPM: %s() failed. trying again\n", __func__);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700115 /* Try writing it again. Maybe it was garbled on the way out. */
116 }
117
Daisuke Nojiri57990972014-07-15 19:47:32 -0700118 VBDEBUG("TPM: %s() - too many failures, giving up\n", __func__);
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -0700119
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700120 return TPM_E_CORRUPTED_STATE;
121}
122
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700123/*
Furquan Shaikhb038f412016-11-07 23:47:11 -0800124 * This is used to initialize the TPM space for recovery hash after defining
125 * it. Since there is no data available to calculate hash at the point where TPM
126 * space is defined, initialize it to all 0s.
127 */
128static const uint8_t rec_hash_data[REC_HASH_NV_SIZE] = { };
129
Julius Wernercd49cce2019-03-05 16:53:33 -0800130#if CONFIG(TPM2)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100131/*
132 * Different sets of NVRAM space attributes apply to the "ro" spaces,
133 * i.e. those which should not be possible to delete or modify once
134 * the RO exits, and the rest of the NVRAM spaces.
135 */
Elyes HAOUAS68ec3eb2019-06-22 09:21:18 +0200136static const TPMA_NV ro_space_attributes = {
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100137 .TPMA_NV_PPWRITE = 1,
138 .TPMA_NV_AUTHREAD = 1,
139 .TPMA_NV_PPREAD = 1,
140 .TPMA_NV_PLATFORMCREATE = 1,
141 .TPMA_NV_WRITE_STCLEAR = 1,
142 .TPMA_NV_POLICY_DELETE = 1,
143};
144
Elyes HAOUAS68ec3eb2019-06-22 09:21:18 +0200145static const TPMA_NV rw_space_attributes = {
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100146 .TPMA_NV_PPWRITE = 1,
147 .TPMA_NV_AUTHREAD = 1,
148 .TPMA_NV_PPREAD = 1,
149 .TPMA_NV_PLATFORMCREATE = 1,
150};
151
152/*
153 * This policy digest was obtained using TPM2_PolicyPCR
154 * selecting only PCR_0 with a value of all zeros.
155 */
Elyes HAOUAS68ec3eb2019-06-22 09:21:18 +0200156static const uint8_t pcr0_unchanged_policy[] = {
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100157 0x09, 0x93, 0x3C, 0xCE, 0xEB, 0xB4, 0x41, 0x11, 0x18, 0x81, 0x1D,
158 0xD4, 0x47, 0x78, 0x80, 0x08, 0x88, 0x86, 0x62, 0x2D, 0xD7, 0x79,
159 0x94, 0x46, 0x62, 0x26, 0x68, 0x8E, 0xEE, 0xE6, 0x6A, 0xA1};
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700160
161/* Nothing special in the TPM2 path yet. */
162static uint32_t safe_write(uint32_t index, const void *data, uint32_t length)
163{
164 return tlcl_write(index, data, length);
165}
166
Andrey Pronin278a5062018-01-26 12:47:51 -0800167static uint32_t set_space(const char *name, uint32_t index, const void *data,
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100168 uint32_t length, const TPMA_NV nv_attributes,
169 const uint8_t *nv_policy, size_t nv_policy_size)
Andrey Pronin278a5062018-01-26 12:47:51 -0800170{
171 uint32_t rv;
172
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100173 rv = tlcl_define_space(index, length, nv_attributes, nv_policy,
174 nv_policy_size);
Andrey Pronin278a5062018-01-26 12:47:51 -0800175 if (rv == TPM_E_NV_DEFINED) {
176 /*
177 * Continue with writing: it may be defined, but not written
178 * to. In that case a subsequent tlcl_read() would still return
179 * TPM_E_BADINDEX on TPM 2.0. The cases when some non-firmware
180 * space is defined while the firmware space is not there
181 * should be rare (interrupted initialization), so no big harm
182 * in writing once again even if it was written already.
183 */
184 VBDEBUG("%s: %s space already exists\n", __func__, name);
185 rv = TPM_SUCCESS;
186 }
187
188 if (rv != TPM_SUCCESS)
189 return rv;
190
Julius Werner683657e2019-12-04 12:50:43 -0800191 return write_secdata(index, data, length);
Andrey Pronin278a5062018-01-26 12:47:51 -0800192}
193
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700194static uint32_t set_firmware_space(const void *firmware_blob)
195{
Andrey Pronin278a5062018-01-26 12:47:51 -0800196 return set_space("firmware", FIRMWARE_NV_INDEX, firmware_blob,
Joel Kitching928511a2019-12-11 17:25:02 +0800197 VB2_SECDATA_FIRMWARE_SIZE, ro_space_attributes,
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100198 pcr0_unchanged_policy, sizeof(pcr0_unchanged_policy));
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700199}
200
201static uint32_t set_kernel_space(const void *kernel_blob)
202{
Andrey Pronin278a5062018-01-26 12:47:51 -0800203 return set_space("kernel", KERNEL_NV_INDEX, kernel_blob,
Joel Kitching928511a2019-12-11 17:25:02 +0800204 VB2_SECDATA_KERNEL_SIZE, rw_space_attributes, NULL, 0);
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700205}
206
Furquan Shaikhb038f412016-11-07 23:47:11 -0800207static uint32_t set_rec_hash_space(const uint8_t *data)
208{
Andrey Pronin278a5062018-01-26 12:47:51 -0800209 return set_space("MRC Hash", REC_HASH_NV_INDEX, data,
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100210 REC_HASH_NV_SIZE,
211 ro_space_attributes, pcr0_unchanged_policy,
212 sizeof(pcr0_unchanged_policy));
Furquan Shaikhb038f412016-11-07 23:47:11 -0800213}
214
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700215static uint32_t _factory_initialize_tpm(struct vb2_context *ctx)
216{
dnojiri58cf6032020-01-28 12:34:20 -0800217 vb2api_secdata_kernel_create(ctx);
218
Vadim Bendeburyadfbbde2016-07-03 15:56:41 -0700219 RETURN_ON_FAILURE(tlcl_force_clear());
Vadim Bendebury38837012016-11-14 16:36:26 -0800220
221 /*
222 * Of all NVRAM spaces defined by this function the firmware space
223 * must be defined last, because its existence is considered an
224 * indication that TPM factory initialization was successfully
225 * completed.
226 */
Joel Kitching928511a2019-12-11 17:25:02 +0800227 RETURN_ON_FAILURE(set_kernel_space(ctx->secdata_kernel));
Furquan Shaikhb038f412016-11-07 23:47:11 -0800228
Julius Wernercd49cce2019-03-05 16:53:33 -0800229 if (CONFIG(VBOOT_HAS_REC_HASH_SPACE))
Furquan Shaikhb038f412016-11-07 23:47:11 -0800230 RETURN_ON_FAILURE(set_rec_hash_space(rec_hash_data));
231
Joel Kitching928511a2019-12-11 17:25:02 +0800232 RETURN_ON_FAILURE(set_firmware_space(ctx->secdata_firmware));
Vadim Bendebury38837012016-11-14 16:36:26 -0800233
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700234 return TPM_SUCCESS;
235}
236
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700237uint32_t antirollback_lock_space_firmware(void)
238{
239 return tlcl_lock_nv_write(FIRMWARE_NV_INDEX);
240}
241
Furquan Shaikhb038f412016-11-07 23:47:11 -0800242uint32_t antirollback_lock_space_rec_hash(void)
243{
244 return tlcl_lock_nv_write(REC_HASH_NV_INDEX);
245}
246
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700247#else
248
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700249/**
250 * Like tlcl_write(), but checks for write errors due to hitting the 64-write
251 * limit and clears the TPM when that happens. This can only happen when the
252 * TPM is unowned, so it is OK to clear it (and we really have no choice).
253 * This is not expected to happen frequently, but it could happen.
254 */
255
256static uint32_t safe_write(uint32_t index, const void *data, uint32_t length)
257{
258 uint32_t result = tlcl_write(index, data, length);
259 if (result == TPM_E_MAXNVWRITES) {
260 RETURN_ON_FAILURE(tpm_clear_and_reenable());
261 return tlcl_write(index, data, length);
262 } else {
263 return result;
264 }
265}
266
267/**
268 * Similarly to safe_write(), this ensures we don't fail a DefineSpace because
269 * we hit the TPM write limit. This is even less likely to happen than with
270 * writes because we only define spaces once at initialization, but we'd
271 * rather be paranoid about this.
272 */
273static uint32_t safe_define_space(uint32_t index, uint32_t perm, uint32_t size)
274{
275 uint32_t result = tlcl_define_space(index, perm, size);
276 if (result == TPM_E_MAXNVWRITES) {
277 RETURN_ON_FAILURE(tpm_clear_and_reenable());
278 return tlcl_define_space(index, perm, size);
279 } else {
280 return result;
281 }
282}
283
Furquan Shaikhb038f412016-11-07 23:47:11 -0800284static uint32_t set_rec_hash_space(const uint8_t *data)
285{
286 RETURN_ON_FAILURE(safe_define_space(REC_HASH_NV_INDEX,
287 TPM_NV_PER_GLOBALLOCK |
288 TPM_NV_PER_PPWRITE,
289 REC_HASH_NV_SIZE));
290 RETURN_ON_FAILURE(write_secdata(REC_HASH_NV_INDEX, data,
291 REC_HASH_NV_SIZE));
292
293 return TPM_SUCCESS;
294}
295
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700296static uint32_t _factory_initialize_tpm(struct vb2_context *ctx)
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700297{
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700298 TPM_PERMANENT_FLAGS pflags;
299 uint32_t result;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700300
dnojiri58cf6032020-01-28 12:34:20 -0800301 vb2api_secdata_kernel_create_v0(ctx);
302
Daisuke Nojiri57990972014-07-15 19:47:32 -0700303 result = tlcl_get_permanent_flags(&pflags);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700304 if (result != TPM_SUCCESS)
305 return result;
306
307 /*
308 * TPM may come from the factory without physical presence finalized.
309 * Fix if necessary.
310 */
Daisuke Nojiri57990972014-07-15 19:47:32 -0700311 VBDEBUG("TPM: physicalPresenceLifetimeLock=%d\n",
312 pflags.physicalPresenceLifetimeLock);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700313 if (!pflags.physicalPresenceLifetimeLock) {
Daisuke Nojiri57990972014-07-15 19:47:32 -0700314 VBDEBUG("TPM: Finalizing physical presence\n");
315 RETURN_ON_FAILURE(tlcl_finalize_physical_presence());
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700316 }
317
318 /*
319 * The TPM will not enforce the NV authorization restrictions until the
320 * execution of a TPM_NV_DefineSpace with the handle of
321 * TPM_NV_INDEX_LOCK. Here we create that space if it doesn't already
322 * exist. */
Daisuke Nojiri57990972014-07-15 19:47:32 -0700323 VBDEBUG("TPM: nvLocked=%d\n", pflags.nvLocked);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700324 if (!pflags.nvLocked) {
Daisuke Nojiri57990972014-07-15 19:47:32 -0700325 VBDEBUG("TPM: Enabling NV locking\n");
326 RETURN_ON_FAILURE(tlcl_set_nv_locked());
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700327 }
328
329 /* Clear TPM owner, in case the TPM is already owned for some reason. */
Daisuke Nojiri57990972014-07-15 19:47:32 -0700330 VBDEBUG("TPM: Clearing owner\n");
331 RETURN_ON_FAILURE(tpm_clear_and_reenable());
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700332
Joel Kitching928511a2019-12-11 17:25:02 +0800333 /* Define and write secdata_kernel space. */
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -0700334 RETURN_ON_FAILURE(safe_define_space(KERNEL_NV_INDEX,
335 TPM_NV_PER_PPWRITE,
dnojiri58cf6032020-01-28 12:34:20 -0800336 VB2_SECDATA_KERNEL_SIZE_V02));
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -0700337 RETURN_ON_FAILURE(write_secdata(KERNEL_NV_INDEX,
Joel Kitching928511a2019-12-11 17:25:02 +0800338 ctx->secdata_kernel,
dnojiri58cf6032020-01-28 12:34:20 -0800339 VB2_SECDATA_KERNEL_SIZE_V02));
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -0700340
Joel Kitching928511a2019-12-11 17:25:02 +0800341 /* Define and write secdata_firmware space. */
Daisuke Nojiri57990972014-07-15 19:47:32 -0700342 RETURN_ON_FAILURE(safe_define_space(FIRMWARE_NV_INDEX,
Joel Kitching928511a2019-12-11 17:25:02 +0800343 TPM_NV_PER_GLOBALLOCK |
344 TPM_NV_PER_PPWRITE,
345 VB2_SECDATA_FIRMWARE_SIZE));
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -0700346 RETURN_ON_FAILURE(write_secdata(FIRMWARE_NV_INDEX,
Joel Kitching928511a2019-12-11 17:25:02 +0800347 ctx->secdata_firmware,
348 VB2_SECDATA_FIRMWARE_SIZE));
Furquan Shaikhb038f412016-11-07 23:47:11 -0800349
350 /* Define and set rec hash space, if available. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800351 if (CONFIG(VBOOT_HAS_REC_HASH_SPACE))
Furquan Shaikhb038f412016-11-07 23:47:11 -0800352 RETURN_ON_FAILURE(set_rec_hash_space(rec_hash_data));
353
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700354 return TPM_SUCCESS;
355}
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700356
357uint32_t antirollback_lock_space_firmware(void)
358{
359 return tlcl_set_global_lock();
360}
Furquan Shaikhb038f412016-11-07 23:47:11 -0800361
362uint32_t antirollback_lock_space_rec_hash(void)
363{
364 /*
365 * Nothing needs to be done here, since global lock is already set while
366 * locking firmware space.
367 */
368 return TPM_SUCCESS;
369}
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700370#endif
371
Vadim Bendebury673a2662016-11-11 09:33:43 -0800372/**
373 * Perform one-time initializations.
374 *
375 * Create the NVRAM spaces, and set their initial values as needed. Sets the
376 * nvLocked bit and ensures the physical presence command is enabled and
377 * locked.
378 */
379static uint32_t factory_initialize_tpm(struct vb2_context *ctx)
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700380{
381 uint32_t result;
382
dnojiri58cf6032020-01-28 12:34:20 -0800383 /*
384 * Set initial values of secdata_firmware space.
385 * kernel space is created in _factory_initialize_tpm().
386 */
Joel Kitching928511a2019-12-11 17:25:02 +0800387 vb2api_secdata_firmware_create(ctx);
Vadim Bendebury10ea1042016-06-06 12:12:34 -0700388
389 VBDEBUG("TPM: factory initialization\n");
390
391 /*
392 * Do a full test. This only happens the first time the device is
393 * turned on in the factory, so performance is not an issue. This is
394 * almost certainly not necessary, but it gives us more confidence
395 * about some code paths below that are difficult to
396 * test---specifically the ones that set lifetime flags, and are only
397 * executed once per physical TPM.
398 */
399 result = tlcl_self_test_full();
400 if (result != TPM_SUCCESS)
401 return result;
402
403 result = _factory_initialize_tpm(ctx);
404 if (result != TPM_SUCCESS)
405 return result;
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -0700406
Julius Werner683657e2019-12-04 12:50:43 -0800407 /* _factory_initialize_tpm() writes initial secdata values to TPM
408 immediately, so let vboot know that it's up to date now. */
409 ctx->flags &= ~(VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED |
410 VB2_CONTEXT_SECDATA_KERNEL_CHANGED);
411
Daisuke Nojiri97ea9c02014-09-29 13:02:29 -0700412 VBDEBUG("TPM: factory initialization successful\n");
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700413
414 return TPM_SUCCESS;
415}
416
Daisuke Nojiri57990972014-07-15 19:47:32 -0700417uint32_t antirollback_read_space_firmware(struct vb2_context *ctx)
418{
419 uint32_t rv;
420
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700421 /* Read the firmware space. */
Daisuke Nojiri57990972014-07-15 19:47:32 -0700422 rv = read_space_firmware(ctx);
423 if (rv == TPM_E_BADINDEX) {
Julius Werner683657e2019-12-04 12:50:43 -0800424 /* This seems the first time we've run. Initialize the TPM. */
Daisuke Nojiri57990972014-07-15 19:47:32 -0700425 VBDEBUG("TPM: Not initialized yet.\n");
426 RETURN_ON_FAILURE(factory_initialize_tpm(ctx));
427 } else if (rv != TPM_SUCCESS) {
428 VBDEBUG("TPM: Firmware space in a bad state; giving up.\n");
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700429 return TPM_E_CORRUPTED_STATE;
430 }
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700431
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700432 return TPM_SUCCESS;
433}
434
Daisuke Nojiri57990972014-07-15 19:47:32 -0700435uint32_t antirollback_write_space_firmware(struct vb2_context *ctx)
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700436{
Julius Wernercd49cce2019-03-05 16:53:33 -0800437 if (CONFIG(CR50_IMMEDIATELY_COMMIT_FW_SECDATA))
Aaron Durbineeb77372017-03-08 11:23:11 -0600438 tlcl_cr50_enable_nvcommits();
Joel Kitching928511a2019-12-11 17:25:02 +0800439 return write_secdata(FIRMWARE_NV_INDEX, ctx->secdata_firmware,
440 VB2_SECDATA_FIRMWARE_SIZE);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700441}
Furquan Shaikhb038f412016-11-07 23:47:11 -0800442
443uint32_t antirollback_read_space_rec_hash(uint8_t *data, uint32_t size)
444{
445 if (size != REC_HASH_NV_SIZE) {
446 VBDEBUG("TPM: Incorrect buffer size for rec hash. "
447 "(Expected=0x%x Actual=0x%x).\n", REC_HASH_NV_SIZE,
448 size);
449 return TPM_E_READ_FAILURE;
450 }
451 return read_space_rec_hash(data);
452}
453
454uint32_t antirollback_write_space_rec_hash(const uint8_t *data, uint32_t size)
455{
456 uint8_t spc_data[REC_HASH_NV_SIZE];
457 uint32_t rv;
458
459 if (size != REC_HASH_NV_SIZE) {
460 VBDEBUG("TPM: Incorrect buffer size for rec hash. "
461 "(Expected=0x%x Actual=0x%x).\n", REC_HASH_NV_SIZE,
462 size);
463 return TPM_E_WRITE_FAILURE;
464 }
465
466 rv = read_space_rec_hash(spc_data);
467 if (rv == TPM_E_BADINDEX) {
468 /*
469 * If space is not defined already for recovery hash, define
470 * new space.
471 */
472 VBDEBUG("TPM: Initializing recovery hash space.\n");
473 return set_rec_hash_space(data);
474 }
475
476 if (rv != TPM_SUCCESS)
477 return rv;
478
479 return write_secdata(REC_HASH_NV_INDEX, data, size);
480}
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100481
Joel Kitching220ac042019-07-31 14:19:00 +0800482vb2_error_t vb2ex_tpm_clear_owner(struct vb2_context *ctx)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100483{
484 uint32_t rv;
485 printk(BIOS_INFO, "Clearing TPM owner\n");
486 rv = tpm_clear_and_reenable();
487 if (rv)
488 return VB2_ERROR_EX_TPM_CLEAR_OWNER;
489 return VB2_SUCCESS;
490}