blob: 7ab9824843dc5a521dfba6a46da771d01e0f0389 [file] [log] [blame]
Aaron Durbin588ad7b2015-09-29 17:56:59 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2014 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbin588ad7b2015-09-29 17:56:59 -050014 */
15
Aaron Durbin588ad7b2015-09-29 17:56:59 -050016#include <arch/exception.h>
17#include <assert.h>
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070018#include <bootmode.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050019#include <console/console.h>
20#include <console/vtxprintf.h>
21#include <delay.h>
22#include <string.h>
23#include <timestamp.h>
24#include <vb2_api.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020025#include <security/vboot/misc.h>
26#include <security/vboot/vbnv.h>
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010027#include <security/vboot/vboot_crtm.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050028
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010029#include "antirollback.h"
30
Aaron Durbin87c9fae2016-01-22 15:26:04 -060031/* The max hash size to expect is for SHA512. */
32#define VBOOT_MAX_HASH_SIZE VB2_SHA512_DIGEST_SIZE
33
Aaron Durbin588ad7b2015-09-29 17:56:59 -050034#define TODO_BLOCK_SIZE 1024
35
36static int is_slot_a(struct vb2_context *ctx)
37{
38 return !(ctx->flags & VB2_CONTEXT_FW_SLOT_B);
39}
40
41/* exports */
42
43void vb2ex_printf(const char *func, const char *fmt, ...)
44{
45 va_list args;
46
Randall Spanglere80c6f62017-01-20 14:48:53 -080047 if (func)
48 printk(BIOS_INFO, "VB2:%s() ", func);
49
Aaron Durbin588ad7b2015-09-29 17:56:59 -050050 va_start(args, fmt);
Kyösti Mälkki7132f252019-02-14 23:08:29 +020051 vprintk(BIOS_INFO, fmt, args);
Aaron Durbin588ad7b2015-09-29 17:56:59 -050052 va_end(args);
53
54 return;
55}
56
Aaron Durbin588ad7b2015-09-29 17:56:59 -050057int vb2ex_read_resource(struct vb2_context *ctx,
58 enum vb2_resource_index index,
59 uint32_t offset,
60 void *buf,
61 uint32_t size)
62{
63 struct region_device rdev;
64 const char *name;
65
66 switch (index) {
67 case VB2_RES_GBB:
68 name = "GBB";
69 break;
70 case VB2_RES_FW_VBLOCK:
71 if (is_slot_a(ctx))
72 name = "VBLOCK_A";
73 else
74 name = "VBLOCK_B";
75 break;
76 default:
77 return VB2_ERROR_EX_READ_RESOURCE_INDEX;
78 }
79
80 if (vboot_named_region_device(name, &rdev))
81 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
82
83 if (rdev_readat(&rdev, buf, offset, size) != size)
84 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
85
86 return VB2_SUCCESS;
87}
88
89/* No-op stubs that can be overridden by SoCs with hardware crypto support. */
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010090__weak int vb2ex_hwcrypto_digest_init(enum vb2_hash_algorithm hash_alg,
91 uint32_t data_size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050092{
93 return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED;
94}
95
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010096__weak int vb2ex_hwcrypto_digest_extend(const uint8_t *buf, uint32_t size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050097{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010098 BUG(); /* Should never get called if init() returned an error. */
Aaron Durbin588ad7b2015-09-29 17:56:59 -050099 return VB2_ERROR_UNKNOWN;
100}
101
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100102__weak int vb2ex_hwcrypto_digest_finalize(uint8_t *digest, uint32_t digest_size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500103{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100104 BUG(); /* Should never get called if init() returned an error. */
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500105 return VB2_ERROR_UNKNOWN;
106}
107
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600108static int handle_digest_result(void *slot_hash, size_t slot_hash_sz)
109{
110 int is_resume;
111
112 /*
Naresh G Solanki1d04d162016-11-08 00:27:41 +0530113 * Chrome EC is the only support for vboot_save_hash() &
114 * vboot_retrieve_hash(), if Chrome EC is not enabled then return.
Naresh G Solanki3d44d692016-11-06 12:51:14 +0530115 */
116 if (!IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC))
117 return 0;
118
119 /*
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600120 * Nothing to do since resuming on the platform doesn't require
121 * vboot verification again.
122 */
123 if (!IS_ENABLED(CONFIG_RESUME_PATH_SAME_AS_BOOT))
124 return 0;
125
126 /*
127 * Assume that if vboot doesn't start in bootblock verified
128 * RW memory init code is not employed. i.e. memory init code
129 * lives in RO CBFS.
130 */
131 if (!IS_ENABLED(CONFIG_VBOOT_STARTS_IN_BOOTBLOCK))
132 return 0;
133
134 is_resume = vboot_platform_is_resuming();
135
136 if (is_resume > 0) {
137 uint8_t saved_hash[VBOOT_MAX_HASH_SIZE];
138 const size_t saved_hash_sz = sizeof(saved_hash);
139
140 assert(slot_hash_sz == saved_hash_sz);
141
142 printk(BIOS_DEBUG, "Platform is resuming.\n");
143
144 if (vboot_retrieve_hash(saved_hash, saved_hash_sz)) {
145 printk(BIOS_ERR, "Couldn't retrieve saved hash.\n");
146 return -1;
147 }
148
149 if (memcmp(saved_hash, slot_hash, slot_hash_sz)) {
150 printk(BIOS_ERR, "Hash mismatch on resume.\n");
151 return -1;
152 }
153 } else if (is_resume < 0)
154 printk(BIOS_ERR, "Unable to determine if platform resuming.\n");
155
156 printk(BIOS_DEBUG, "Saving vboot hash.\n");
157
158 /* Always save the hash for the current boot. */
159 if (vboot_save_hash(slot_hash, slot_hash_sz)) {
160 printk(BIOS_ERR, "Error saving vboot hash.\n");
161 /* Though this is an error don't report it up since it could
162 * lead to a reboot loop. The consequence of this is that
163 * we will most likely fail resuming because of EC issues or
164 * the hash digest not matching. */
165 return 0;
166 }
167
168 return 0;
169}
170
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500171static int hash_body(struct vb2_context *ctx, struct region_device *fw_main)
172{
173 uint64_t load_ts;
174 uint32_t expected_size;
175 uint8_t block[TODO_BLOCK_SIZE];
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600176 uint8_t hash_digest[VBOOT_MAX_HASH_SIZE];
177 const size_t hash_digest_sz = sizeof(hash_digest);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500178 size_t block_size = sizeof(block);
179 size_t offset;
180 int rv;
181
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600182 /* Clear the full digest so that any hash digests less than the
183 * max have trailing zeros. */
184 memset(hash_digest, 0, hash_digest_sz);
185
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500186 /*
187 * Since loading the firmware and calculating its hash is intertwined,
188 * we use this little trick to measure them separately and pretend it
189 * was first loaded and then hashed in one piece with the timestamps.
190 * (This split won't make sense with memory-mapped media like on x86.)
191 */
192 load_ts = timestamp_get();
193 timestamp_add(TS_START_HASH_BODY, load_ts);
194
195 expected_size = region_device_sz(fw_main);
196 offset = 0;
197
198 /* Start the body hash */
199 rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY, &expected_size);
200 if (rv)
201 return rv;
202
Aaron Durbin4121f9e2016-01-27 14:23:17 -0600203 /*
204 * Honor vboot's RW slot size. The expected size is pulled out of
205 * the preamble and obtained through vb2api_init_hash() above. By
206 * creating sub region the RW slot portion of the boot media is
207 * limited.
208 */
209 if (rdev_chain(fw_main, fw_main, 0, expected_size)) {
210 printk(BIOS_ERR, "Unable to restrict CBFS size.\n");
211 return VB2_ERROR_UNKNOWN;
212 }
213
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500214 /* Extend over the body */
215 while (expected_size) {
216 uint64_t temp_ts;
217 if (block_size > expected_size)
218 block_size = expected_size;
219
220 temp_ts = timestamp_get();
221 if (rdev_readat(fw_main, block, offset, block_size) < 0)
222 return VB2_ERROR_UNKNOWN;
223 load_ts += timestamp_get() - temp_ts;
224
225 rv = vb2api_extend_hash(ctx, block, block_size);
226 if (rv)
227 return rv;
228
229 expected_size -= block_size;
230 offset += block_size;
231 }
232
233 timestamp_add(TS_DONE_LOADING, load_ts);
234 timestamp_add_now(TS_DONE_HASHING);
235
236 /* Check the result (with RSA signature verification) */
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600237 rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500238 if (rv)
239 return rv;
240
241 timestamp_add_now(TS_END_HASH_BODY);
242
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600243 if (handle_digest_result(hash_digest, hash_digest_sz))
244 return VB2_ERROR_UNKNOWN;
245
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500246 return VB2_SUCCESS;
247}
248
249static int locate_firmware(struct vb2_context *ctx,
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100250 struct region_device *fw_main)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500251{
252 const char *name;
253
254 if (is_slot_a(ctx))
255 name = "FW_MAIN_A";
256 else
257 name = "FW_MAIN_B";
258
259 return vboot_named_region_device(name, fw_main);
260}
261
262/**
263 * Save non-volatile and/or secure data if needed.
264 */
265static void save_if_needed(struct vb2_context *ctx)
266{
267 if (ctx->flags & VB2_CONTEXT_NVDATA_CHANGED) {
268 printk(BIOS_INFO, "Saving nvdata\n");
269 save_vbnv(ctx->nvdata);
270 ctx->flags &= ~VB2_CONTEXT_NVDATA_CHANGED;
271 }
272 if (ctx->flags & VB2_CONTEXT_SECDATA_CHANGED) {
273 printk(BIOS_INFO, "Saving secdata\n");
274 antirollback_write_space_firmware(ctx);
275 ctx->flags &= ~VB2_CONTEXT_SECDATA_CHANGED;
276 }
277}
278
279static uint32_t extend_pcrs(struct vb2_context *ctx)
280{
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100281 return vboot_extend_pcr(ctx, 0, BOOT_MODE_PCR) ||
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100282 vboot_extend_pcr(ctx, 1, HWID_DIGEST_PCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500283}
284
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500285/**
286 * Verify and select the firmware in the RW image
287 *
288 * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage).
289 * when per-stage verification is ready.
290 */
291void verstage_main(void)
292{
293 struct vb2_context ctx;
294 struct region_device fw_main;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500295 int rv;
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500296
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500297 timestamp_add_now(TS_START_VBOOT);
298
299 /* Set up context and work buffer */
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500300 vb2_init_work_context(&ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500301
Aaron Durbin118a84f2017-09-15 11:15:07 -0600302 /* Initialize and read nvdata from non-volatile storage. */
303 vbnv_init(ctx.nvdata);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500304
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800305 /* Set S3 resume flag if vboot should behave differently when selecting
306 * which slot to boot. This is only relevant to vboot if the platform
307 * does verification of memory init and thus must ensure it resumes with
308 * the same slot that it booted from. */
309 if (IS_ENABLED(CONFIG_RESUME_PATH_SAME_AS_BOOT) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100310 vboot_platform_is_resuming())
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800311 ctx.flags |= VB2_CONTEXT_S3_RESUME;
312
Duncan Lauriea613a312016-03-14 09:32:08 -0700313 /* Read secdata from TPM. Initialize TPM if secdata not found. We don't
314 * check the return value here because vb2api_fw_phase1 will catch
315 * invalid secdata and tell us what to do (=reboot). */
316 timestamp_add_now(TS_START_TPMINIT);
317 antirollback_read_space_firmware(&ctx);
318 timestamp_add_now(TS_END_TPMINIT);
319
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100320 /* Enable measured boot mode */
321 if (IS_ENABLED(CONFIG_VBOOT_MEASURED_BOOT) &&
322 !(ctx.flags & VB2_CONTEXT_S3_RESUME)) {
323 if (vboot_init_crtm() != VB2_SUCCESS)
324 die("Initializing measured boot mode failed!");
325 }
326
Julius Werner58c39382017-02-13 17:53:29 -0800327 if (IS_ENABLED(CONFIG_VBOOT_PHYSICAL_DEV_SWITCH) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100328 get_developer_mode_switch())
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500329 ctx.flags |= VB2_CONTEXT_FORCE_DEVELOPER_MODE;
330
331 if (get_recovery_mode_switch()) {
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500332 ctx.flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE;
333 if (IS_ENABLED(CONFIG_VBOOT_DISABLE_DEV_ON_RECOVERY))
Joel Kitchingba5ae5b2019-01-11 19:30:42 +0800334 ctx.flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500335 }
336
Julius Werner58c39382017-02-13 17:53:29 -0800337 if (IS_ENABLED(CONFIG_VBOOT_WIPEOUT_SUPPORTED) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100338 get_wipeout_mode_switch())
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500339 ctx.flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE;
340
Julius Werner58c39382017-02-13 17:53:29 -0800341 if (IS_ENABLED(CONFIG_VBOOT_LID_SWITCH) && !get_lid_switch())
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500342 ctx.flags |= VB2_CONTEXT_NOFAIL_BOOT;
343
344 /* Do early init (set up secdata and NVRAM, load GBB) */
345 printk(BIOS_INFO, "Phase 1\n");
346 rv = vb2api_fw_phase1(&ctx);
347
348 if (rv) {
349 /*
350 * If vb2api_fw_phase1 fails, check for return value.
351 * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue
352 * into recovery mode.
353 * For any other error code, save context if needed and reboot.
354 */
355 if (rv == VB2_ERROR_API_PHASE1_RECOVERY) {
356 printk(BIOS_INFO, "Recovery requested (%x)\n", rv);
357 save_if_needed(&ctx);
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100358 extend_pcrs(&ctx); /* ignore failures */
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500359 timestamp_add_now(TS_END_VBOOT);
360 return;
361 }
362
Raul E Rangel3a591742018-07-17 14:44:43 -0600363 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500364 save_if_needed(&ctx);
365 vboot_reboot();
366 }
367
368 /* Determine which firmware slot to boot (based on NVRAM) */
369 printk(BIOS_INFO, "Phase 2\n");
370 rv = vb2api_fw_phase2(&ctx);
371 if (rv) {
372 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
373 save_if_needed(&ctx);
374 vboot_reboot();
375 }
376
377 /* Try that slot (verify its keyblock and preamble) */
378 printk(BIOS_INFO, "Phase 3\n");
379 timestamp_add_now(TS_START_VERIFY_SLOT);
380 rv = vb2api_fw_phase3(&ctx);
381 timestamp_add_now(TS_END_VERIFY_SLOT);
382 if (rv) {
383 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
384 save_if_needed(&ctx);
385 vboot_reboot();
386 }
387
388 printk(BIOS_INFO, "Phase 4\n");
389 rv = locate_firmware(&ctx, &fw_main);
390 if (rv)
391 die("Failed to read FMAP to locate firmware");
392
393 rv = hash_body(&ctx, &fw_main);
394 save_if_needed(&ctx);
395 if (rv) {
396 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
397 vboot_reboot();
398 }
399
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800400 /* Only extend PCRs once on boot. */
401 if (!(ctx.flags & VB2_CONTEXT_S3_RESUME)) {
402 timestamp_add_now(TS_START_TPMPCR);
403 rv = extend_pcrs(&ctx);
404 if (rv) {
405 printk(BIOS_WARNING,
406 "Failed to extend TPM PCRs (%#x)\n", rv);
407 vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv);
408 save_if_needed(&ctx);
409 vboot_reboot();
410 }
411 timestamp_add_now(TS_END_TPMPCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500412 }
413
414 /* Lock TPM */
Raul E Rangel4c518e12018-05-11 11:08:07 -0600415
416 timestamp_add_now(TS_START_TPMLOCK);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500417 rv = antirollback_lock_space_firmware();
418 if (rv) {
419 printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv);
420 vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0);
421 save_if_needed(&ctx);
422 vboot_reboot();
423 }
Raul E Rangel4c518e12018-05-11 11:08:07 -0600424 timestamp_add_now(TS_END_TPMLOCK);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500425
Furquan Shaikhb038f412016-11-07 23:47:11 -0800426 /* Lock rec hash space if available. */
427 if (IS_ENABLED(CONFIG_VBOOT_HAS_REC_HASH_SPACE)) {
428 rv = antirollback_lock_space_rec_hash();
429 if (rv) {
430 printk(BIOS_INFO, "Failed to lock rec hash space(%x)\n",
431 rv);
432 vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR,
433 0);
434 save_if_needed(&ctx);
435 vboot_reboot();
436 }
437 }
438
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500439 printk(BIOS_INFO, "Slot %c is selected\n", is_slot_a(&ctx) ? 'A' : 'B');
Aaron Durbin6d720f32015-12-08 17:00:23 -0600440 vb2_set_selected_region(region_device_region(&fw_main));
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500441 timestamp_add_now(TS_END_VBOOT);
442}