blob: c76739ac51aff78c23e815c2a91b3d6549bbfaff [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
Philipp Deppenwiese64e2d192017-10-18 17:13:07 +020016#include <security/tpm/antirollback.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050017#include <arch/exception.h>
18#include <assert.h>
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070019#include <bootmode.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050020#include <console/console.h>
21#include <console/vtxprintf.h>
22#include <delay.h>
23#include <string.h>
24#include <timestamp.h>
25#include <vb2_api.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020026#include <security/vboot/misc.h>
27#include <security/vboot/vbnv.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050028
Aaron Durbin87c9fae2016-01-22 15:26:04 -060029/* The max hash size to expect is for SHA512. */
30#define VBOOT_MAX_HASH_SIZE VB2_SHA512_DIGEST_SIZE
31
Aaron Durbin588ad7b2015-09-29 17:56:59 -050032#define TODO_BLOCK_SIZE 1024
33
34static int is_slot_a(struct vb2_context *ctx)
35{
36 return !(ctx->flags & VB2_CONTEXT_FW_SLOT_B);
37}
38
39/* exports */
40
41void vb2ex_printf(const char *func, const char *fmt, ...)
42{
43 va_list args;
44
Randall Spanglere80c6f62017-01-20 14:48:53 -080045 if (func)
46 printk(BIOS_INFO, "VB2:%s() ", func);
47
Aaron Durbin588ad7b2015-09-29 17:56:59 -050048 va_start(args, fmt);
49 do_printk_va_list(BIOS_INFO, fmt, args);
50 va_end(args);
51
52 return;
53}
54
55int vb2ex_tpm_clear_owner(struct vb2_context *ctx)
56{
57 uint32_t rv;
58 printk(BIOS_INFO, "Clearing TPM owner\n");
59 rv = tpm_clear_and_reenable();
60 if (rv)
61 return VB2_ERROR_EX_TPM_CLEAR_OWNER;
62 return VB2_SUCCESS;
63}
64
65int vb2ex_read_resource(struct vb2_context *ctx,
66 enum vb2_resource_index index,
67 uint32_t offset,
68 void *buf,
69 uint32_t size)
70{
71 struct region_device rdev;
72 const char *name;
73
74 switch (index) {
75 case VB2_RES_GBB:
76 name = "GBB";
77 break;
78 case VB2_RES_FW_VBLOCK:
79 if (is_slot_a(ctx))
80 name = "VBLOCK_A";
81 else
82 name = "VBLOCK_B";
83 break;
84 default:
85 return VB2_ERROR_EX_READ_RESOURCE_INDEX;
86 }
87
88 if (vboot_named_region_device(name, &rdev))
89 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
90
91 if (rdev_readat(&rdev, buf, offset, size) != size)
92 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
93
94 return VB2_SUCCESS;
95}
96
97/* No-op stubs that can be overridden by SoCs with hardware crypto support. */
98__attribute__((weak))
99int vb2ex_hwcrypto_digest_init(enum vb2_hash_algorithm hash_alg,
100 uint32_t data_size)
101{
102 return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED;
103}
104
105__attribute__((weak))
106int vb2ex_hwcrypto_digest_extend(const uint8_t *buf, uint32_t size)
107{
108 BUG(); /* Should never get called if init() returned an error. */
109 return VB2_ERROR_UNKNOWN;
110}
111
112__attribute__((weak))
113int vb2ex_hwcrypto_digest_finalize(uint8_t *digest, uint32_t digest_size)
114{
115 BUG(); /* Should never get called if init() returned an error. */
116 return VB2_ERROR_UNKNOWN;
117}
118
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600119static int handle_digest_result(void *slot_hash, size_t slot_hash_sz)
120{
121 int is_resume;
122
123 /*
Naresh G Solanki1d04d162016-11-08 00:27:41 +0530124 * Chrome EC is the only support for vboot_save_hash() &
125 * vboot_retrieve_hash(), if Chrome EC is not enabled then return.
Naresh G Solanki3d44d692016-11-06 12:51:14 +0530126 */
127 if (!IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC))
128 return 0;
129
130 /*
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600131 * Nothing to do since resuming on the platform doesn't require
132 * vboot verification again.
133 */
134 if (!IS_ENABLED(CONFIG_RESUME_PATH_SAME_AS_BOOT))
135 return 0;
136
137 /*
138 * Assume that if vboot doesn't start in bootblock verified
139 * RW memory init code is not employed. i.e. memory init code
140 * lives in RO CBFS.
141 */
142 if (!IS_ENABLED(CONFIG_VBOOT_STARTS_IN_BOOTBLOCK))
143 return 0;
144
145 is_resume = vboot_platform_is_resuming();
146
147 if (is_resume > 0) {
148 uint8_t saved_hash[VBOOT_MAX_HASH_SIZE];
149 const size_t saved_hash_sz = sizeof(saved_hash);
150
151 assert(slot_hash_sz == saved_hash_sz);
152
153 printk(BIOS_DEBUG, "Platform is resuming.\n");
154
155 if (vboot_retrieve_hash(saved_hash, saved_hash_sz)) {
156 printk(BIOS_ERR, "Couldn't retrieve saved hash.\n");
157 return -1;
158 }
159
160 if (memcmp(saved_hash, slot_hash, slot_hash_sz)) {
161 printk(BIOS_ERR, "Hash mismatch on resume.\n");
162 return -1;
163 }
164 } else if (is_resume < 0)
165 printk(BIOS_ERR, "Unable to determine if platform resuming.\n");
166
167 printk(BIOS_DEBUG, "Saving vboot hash.\n");
168
169 /* Always save the hash for the current boot. */
170 if (vboot_save_hash(slot_hash, slot_hash_sz)) {
171 printk(BIOS_ERR, "Error saving vboot hash.\n");
172 /* Though this is an error don't report it up since it could
173 * lead to a reboot loop. The consequence of this is that
174 * we will most likely fail resuming because of EC issues or
175 * the hash digest not matching. */
176 return 0;
177 }
178
179 return 0;
180}
181
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500182static int hash_body(struct vb2_context *ctx, struct region_device *fw_main)
183{
184 uint64_t load_ts;
185 uint32_t expected_size;
186 uint8_t block[TODO_BLOCK_SIZE];
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600187 uint8_t hash_digest[VBOOT_MAX_HASH_SIZE];
188 const size_t hash_digest_sz = sizeof(hash_digest);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500189 size_t block_size = sizeof(block);
190 size_t offset;
191 int rv;
192
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600193 /* Clear the full digest so that any hash digests less than the
194 * max have trailing zeros. */
195 memset(hash_digest, 0, hash_digest_sz);
196
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500197 /*
198 * Since loading the firmware and calculating its hash is intertwined,
199 * we use this little trick to measure them separately and pretend it
200 * was first loaded and then hashed in one piece with the timestamps.
201 * (This split won't make sense with memory-mapped media like on x86.)
202 */
203 load_ts = timestamp_get();
204 timestamp_add(TS_START_HASH_BODY, load_ts);
205
206 expected_size = region_device_sz(fw_main);
207 offset = 0;
208
209 /* Start the body hash */
210 rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY, &expected_size);
211 if (rv)
212 return rv;
213
Aaron Durbin4121f9e2016-01-27 14:23:17 -0600214 /*
215 * Honor vboot's RW slot size. The expected size is pulled out of
216 * the preamble and obtained through vb2api_init_hash() above. By
217 * creating sub region the RW slot portion of the boot media is
218 * limited.
219 */
220 if (rdev_chain(fw_main, fw_main, 0, expected_size)) {
221 printk(BIOS_ERR, "Unable to restrict CBFS size.\n");
222 return VB2_ERROR_UNKNOWN;
223 }
224
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500225 /* Extend over the body */
226 while (expected_size) {
227 uint64_t temp_ts;
228 if (block_size > expected_size)
229 block_size = expected_size;
230
231 temp_ts = timestamp_get();
232 if (rdev_readat(fw_main, block, offset, block_size) < 0)
233 return VB2_ERROR_UNKNOWN;
234 load_ts += timestamp_get() - temp_ts;
235
236 rv = vb2api_extend_hash(ctx, block, block_size);
237 if (rv)
238 return rv;
239
240 expected_size -= block_size;
241 offset += block_size;
242 }
243
244 timestamp_add(TS_DONE_LOADING, load_ts);
245 timestamp_add_now(TS_DONE_HASHING);
246
247 /* Check the result (with RSA signature verification) */
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600248 rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500249 if (rv)
250 return rv;
251
252 timestamp_add_now(TS_END_HASH_BODY);
253
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600254 if (handle_digest_result(hash_digest, hash_digest_sz))
255 return VB2_ERROR_UNKNOWN;
256
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500257 return VB2_SUCCESS;
258}
259
260static int locate_firmware(struct vb2_context *ctx,
261 struct region_device *fw_main)
262{
263 const char *name;
264
265 if (is_slot_a(ctx))
266 name = "FW_MAIN_A";
267 else
268 name = "FW_MAIN_B";
269
270 return vboot_named_region_device(name, fw_main);
271}
272
273/**
274 * Save non-volatile and/or secure data if needed.
275 */
276static void save_if_needed(struct vb2_context *ctx)
277{
278 if (ctx->flags & VB2_CONTEXT_NVDATA_CHANGED) {
279 printk(BIOS_INFO, "Saving nvdata\n");
280 save_vbnv(ctx->nvdata);
281 ctx->flags &= ~VB2_CONTEXT_NVDATA_CHANGED;
282 }
283 if (ctx->flags & VB2_CONTEXT_SECDATA_CHANGED) {
284 printk(BIOS_INFO, "Saving secdata\n");
285 antirollback_write_space_firmware(ctx);
286 ctx->flags &= ~VB2_CONTEXT_SECDATA_CHANGED;
287 }
288}
289
290static uint32_t extend_pcrs(struct vb2_context *ctx)
291{
292 return tpm_extend_pcr(ctx, 0, BOOT_MODE_PCR) ||
293 tpm_extend_pcr(ctx, 1, HWID_DIGEST_PCR);
294}
295
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500296/**
297 * Verify and select the firmware in the RW image
298 *
299 * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage).
300 * when per-stage verification is ready.
301 */
302void verstage_main(void)
303{
304 struct vb2_context ctx;
305 struct region_device fw_main;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500306 int rv;
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500307
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500308 timestamp_add_now(TS_START_VBOOT);
309
310 /* Set up context and work buffer */
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500311 vb2_init_work_context(&ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500312
Aaron Durbin118a84f2017-09-15 11:15:07 -0600313 /* Initialize and read nvdata from non-volatile storage. */
314 vbnv_init(ctx.nvdata);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500315
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800316 /* Set S3 resume flag if vboot should behave differently when selecting
317 * which slot to boot. This is only relevant to vboot if the platform
318 * does verification of memory init and thus must ensure it resumes with
319 * the same slot that it booted from. */
320 if (IS_ENABLED(CONFIG_RESUME_PATH_SAME_AS_BOOT) &&
321 IS_ENABLED(CONFIG_VBOOT_STARTS_IN_BOOTBLOCK) &&
322 vboot_platform_is_resuming())
323 ctx.flags |= VB2_CONTEXT_S3_RESUME;
324
Duncan Lauriea613a312016-03-14 09:32:08 -0700325 /* Read secdata from TPM. Initialize TPM if secdata not found. We don't
326 * check the return value here because vb2api_fw_phase1 will catch
327 * invalid secdata and tell us what to do (=reboot). */
328 timestamp_add_now(TS_START_TPMINIT);
329 antirollback_read_space_firmware(&ctx);
330 timestamp_add_now(TS_END_TPMINIT);
331
Julius Werner58c39382017-02-13 17:53:29 -0800332 if (IS_ENABLED(CONFIG_VBOOT_PHYSICAL_DEV_SWITCH) &&
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500333 get_developer_mode_switch())
334 ctx.flags |= VB2_CONTEXT_FORCE_DEVELOPER_MODE;
335
336 if (get_recovery_mode_switch()) {
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500337 ctx.flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE;
338 if (IS_ENABLED(CONFIG_VBOOT_DISABLE_DEV_ON_RECOVERY))
339 ctx.flags |= VB2_DISABLE_DEVELOPER_MODE;
340 }
341
Julius Werner58c39382017-02-13 17:53:29 -0800342 if (IS_ENABLED(CONFIG_VBOOT_WIPEOUT_SUPPORTED) &&
343 get_wipeout_mode_switch())
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500344 ctx.flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE;
345
Julius Werner58c39382017-02-13 17:53:29 -0800346 if (IS_ENABLED(CONFIG_VBOOT_LID_SWITCH) && !get_lid_switch())
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500347 ctx.flags |= VB2_CONTEXT_NOFAIL_BOOT;
348
349 /* Do early init (set up secdata and NVRAM, load GBB) */
350 printk(BIOS_INFO, "Phase 1\n");
351 rv = vb2api_fw_phase1(&ctx);
352
353 if (rv) {
354 /*
355 * If vb2api_fw_phase1 fails, check for return value.
356 * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue
357 * into recovery mode.
358 * For any other error code, save context if needed and reboot.
359 */
360 if (rv == VB2_ERROR_API_PHASE1_RECOVERY) {
361 printk(BIOS_INFO, "Recovery requested (%x)\n", rv);
362 save_if_needed(&ctx);
363 extend_pcrs(&ctx); /* ignore failures */
364 timestamp_add_now(TS_END_VBOOT);
365 return;
366 }
367
368 printk(BIOS_INFO, "Reboot reqested (%x)\n", rv);
369 save_if_needed(&ctx);
370 vboot_reboot();
371 }
372
373 /* Determine which firmware slot to boot (based on NVRAM) */
374 printk(BIOS_INFO, "Phase 2\n");
375 rv = vb2api_fw_phase2(&ctx);
376 if (rv) {
377 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
378 save_if_needed(&ctx);
379 vboot_reboot();
380 }
381
382 /* Try that slot (verify its keyblock and preamble) */
383 printk(BIOS_INFO, "Phase 3\n");
384 timestamp_add_now(TS_START_VERIFY_SLOT);
385 rv = vb2api_fw_phase3(&ctx);
386 timestamp_add_now(TS_END_VERIFY_SLOT);
387 if (rv) {
388 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
389 save_if_needed(&ctx);
390 vboot_reboot();
391 }
392
393 printk(BIOS_INFO, "Phase 4\n");
394 rv = locate_firmware(&ctx, &fw_main);
395 if (rv)
396 die("Failed to read FMAP to locate firmware");
397
398 rv = hash_body(&ctx, &fw_main);
399 save_if_needed(&ctx);
400 if (rv) {
401 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
402 vboot_reboot();
403 }
404
405 rv = extend_pcrs(&ctx);
406 if (rv) {
407 printk(BIOS_WARNING, "Failed to extend TPM PCRs (%#x)\n", rv);
408 vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv);
409 save_if_needed(&ctx);
410 vboot_reboot();
411 }
412
413 /* Lock TPM */
414 rv = antirollback_lock_space_firmware();
415 if (rv) {
416 printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv);
417 vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0);
418 save_if_needed(&ctx);
419 vboot_reboot();
420 }
421
Furquan Shaikhb038f412016-11-07 23:47:11 -0800422 /* Lock rec hash space if available. */
423 if (IS_ENABLED(CONFIG_VBOOT_HAS_REC_HASH_SPACE)) {
424 rv = antirollback_lock_space_rec_hash();
425 if (rv) {
426 printk(BIOS_INFO, "Failed to lock rec hash space(%x)\n",
427 rv);
428 vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR,
429 0);
430 save_if_needed(&ctx);
431 vboot_reboot();
432 }
433 }
434
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500435 printk(BIOS_INFO, "Slot %c is selected\n", is_slot_a(&ctx) ? 'A' : 'B');
Aaron Durbin6d720f32015-12-08 17:00:23 -0600436 vb2_set_selected_region(region_device_region(&fw_main));
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500437 timestamp_add_now(TS_END_VBOOT);
438}