blob: bc6ab48191fb2faad5e51cb2d28896b1fe88fc88 [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
16#include <antirollback.h>
17#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>
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070026#include <vboot/misc.h>
27#include <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
45 printk(BIOS_INFO, "VB2:%s() ", func);
46 va_start(args, fmt);
47 do_printk_va_list(BIOS_INFO, fmt, args);
48 va_end(args);
49
50 return;
51}
52
53int vb2ex_tpm_clear_owner(struct vb2_context *ctx)
54{
55 uint32_t rv;
56 printk(BIOS_INFO, "Clearing TPM owner\n");
57 rv = tpm_clear_and_reenable();
58 if (rv)
59 return VB2_ERROR_EX_TPM_CLEAR_OWNER;
60 return VB2_SUCCESS;
61}
62
63int vb2ex_read_resource(struct vb2_context *ctx,
64 enum vb2_resource_index index,
65 uint32_t offset,
66 void *buf,
67 uint32_t size)
68{
69 struct region_device rdev;
70 const char *name;
71
72 switch (index) {
73 case VB2_RES_GBB:
74 name = "GBB";
75 break;
76 case VB2_RES_FW_VBLOCK:
77 if (is_slot_a(ctx))
78 name = "VBLOCK_A";
79 else
80 name = "VBLOCK_B";
81 break;
82 default:
83 return VB2_ERROR_EX_READ_RESOURCE_INDEX;
84 }
85
86 if (vboot_named_region_device(name, &rdev))
87 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
88
89 if (rdev_readat(&rdev, buf, offset, size) != size)
90 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
91
92 return VB2_SUCCESS;
93}
94
95/* No-op stubs that can be overridden by SoCs with hardware crypto support. */
96__attribute__((weak))
97int vb2ex_hwcrypto_digest_init(enum vb2_hash_algorithm hash_alg,
98 uint32_t data_size)
99{
100 return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED;
101}
102
103__attribute__((weak))
104int vb2ex_hwcrypto_digest_extend(const uint8_t *buf, uint32_t size)
105{
106 BUG(); /* Should never get called if init() returned an error. */
107 return VB2_ERROR_UNKNOWN;
108}
109
110__attribute__((weak))
111int vb2ex_hwcrypto_digest_finalize(uint8_t *digest, uint32_t digest_size)
112{
113 BUG(); /* Should never get called if init() returned an error. */
114 return VB2_ERROR_UNKNOWN;
115}
116
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600117static int handle_digest_result(void *slot_hash, size_t slot_hash_sz)
118{
119 int is_resume;
120
121 /*
122 * Nothing to do since resuming on the platform doesn't require
123 * vboot verification again.
124 */
125 if (!IS_ENABLED(CONFIG_RESUME_PATH_SAME_AS_BOOT))
126 return 0;
127
128 /*
129 * Assume that if vboot doesn't start in bootblock verified
130 * RW memory init code is not employed. i.e. memory init code
131 * lives in RO CBFS.
132 */
133 if (!IS_ENABLED(CONFIG_VBOOT_STARTS_IN_BOOTBLOCK))
134 return 0;
135
136 is_resume = vboot_platform_is_resuming();
137
138 if (is_resume > 0) {
139 uint8_t saved_hash[VBOOT_MAX_HASH_SIZE];
140 const size_t saved_hash_sz = sizeof(saved_hash);
141
142 assert(slot_hash_sz == saved_hash_sz);
143
144 printk(BIOS_DEBUG, "Platform is resuming.\n");
145
146 if (vboot_retrieve_hash(saved_hash, saved_hash_sz)) {
147 printk(BIOS_ERR, "Couldn't retrieve saved hash.\n");
148 return -1;
149 }
150
151 if (memcmp(saved_hash, slot_hash, slot_hash_sz)) {
152 printk(BIOS_ERR, "Hash mismatch on resume.\n");
153 return -1;
154 }
155 } else if (is_resume < 0)
156 printk(BIOS_ERR, "Unable to determine if platform resuming.\n");
157
158 printk(BIOS_DEBUG, "Saving vboot hash.\n");
159
160 /* Always save the hash for the current boot. */
161 if (vboot_save_hash(slot_hash, slot_hash_sz)) {
162 printk(BIOS_ERR, "Error saving vboot hash.\n");
163 /* Though this is an error don't report it up since it could
164 * lead to a reboot loop. The consequence of this is that
165 * we will most likely fail resuming because of EC issues or
166 * the hash digest not matching. */
167 return 0;
168 }
169
170 return 0;
171}
172
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500173static int hash_body(struct vb2_context *ctx, struct region_device *fw_main)
174{
175 uint64_t load_ts;
176 uint32_t expected_size;
177 uint8_t block[TODO_BLOCK_SIZE];
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600178 uint8_t hash_digest[VBOOT_MAX_HASH_SIZE];
179 const size_t hash_digest_sz = sizeof(hash_digest);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500180 size_t block_size = sizeof(block);
181 size_t offset;
182 int rv;
183
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600184 /* Clear the full digest so that any hash digests less than the
185 * max have trailing zeros. */
186 memset(hash_digest, 0, hash_digest_sz);
187
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500188 /*
189 * Since loading the firmware and calculating its hash is intertwined,
190 * we use this little trick to measure them separately and pretend it
191 * was first loaded and then hashed in one piece with the timestamps.
192 * (This split won't make sense with memory-mapped media like on x86.)
193 */
194 load_ts = timestamp_get();
195 timestamp_add(TS_START_HASH_BODY, load_ts);
196
197 expected_size = region_device_sz(fw_main);
198 offset = 0;
199
200 /* Start the body hash */
201 rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY, &expected_size);
202 if (rv)
203 return rv;
204
Aaron Durbin4121f9e2016-01-27 14:23:17 -0600205 /*
206 * Honor vboot's RW slot size. The expected size is pulled out of
207 * the preamble and obtained through vb2api_init_hash() above. By
208 * creating sub region the RW slot portion of the boot media is
209 * limited.
210 */
211 if (rdev_chain(fw_main, fw_main, 0, expected_size)) {
212 printk(BIOS_ERR, "Unable to restrict CBFS size.\n");
213 return VB2_ERROR_UNKNOWN;
214 }
215
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500216 /* Extend over the body */
217 while (expected_size) {
218 uint64_t temp_ts;
219 if (block_size > expected_size)
220 block_size = expected_size;
221
222 temp_ts = timestamp_get();
223 if (rdev_readat(fw_main, block, offset, block_size) < 0)
224 return VB2_ERROR_UNKNOWN;
225 load_ts += timestamp_get() - temp_ts;
226
227 rv = vb2api_extend_hash(ctx, block, block_size);
228 if (rv)
229 return rv;
230
231 expected_size -= block_size;
232 offset += block_size;
233 }
234
235 timestamp_add(TS_DONE_LOADING, load_ts);
236 timestamp_add_now(TS_DONE_HASHING);
237
238 /* Check the result (with RSA signature verification) */
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600239 rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500240 if (rv)
241 return rv;
242
243 timestamp_add_now(TS_END_HASH_BODY);
244
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600245 if (handle_digest_result(hash_digest, hash_digest_sz))
246 return VB2_ERROR_UNKNOWN;
247
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500248 return VB2_SUCCESS;
249}
250
251static int locate_firmware(struct vb2_context *ctx,
252 struct region_device *fw_main)
253{
254 const char *name;
255
256 if (is_slot_a(ctx))
257 name = "FW_MAIN_A";
258 else
259 name = "FW_MAIN_B";
260
261 return vboot_named_region_device(name, fw_main);
262}
263
264/**
265 * Save non-volatile and/or secure data if needed.
266 */
267static void save_if_needed(struct vb2_context *ctx)
268{
269 if (ctx->flags & VB2_CONTEXT_NVDATA_CHANGED) {
270 printk(BIOS_INFO, "Saving nvdata\n");
271 save_vbnv(ctx->nvdata);
272 ctx->flags &= ~VB2_CONTEXT_NVDATA_CHANGED;
273 }
274 if (ctx->flags & VB2_CONTEXT_SECDATA_CHANGED) {
275 printk(BIOS_INFO, "Saving secdata\n");
276 antirollback_write_space_firmware(ctx);
277 ctx->flags &= ~VB2_CONTEXT_SECDATA_CHANGED;
278 }
279}
280
281static uint32_t extend_pcrs(struct vb2_context *ctx)
282{
283 return tpm_extend_pcr(ctx, 0, BOOT_MODE_PCR) ||
284 tpm_extend_pcr(ctx, 1, HWID_DIGEST_PCR);
285}
286
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500287/**
288 * Verify and select the firmware in the RW image
289 *
290 * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage).
291 * when per-stage verification is ready.
292 */
293void verstage_main(void)
294{
295 struct vb2_context ctx;
296 struct region_device fw_main;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500297 int rv;
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500298
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500299 timestamp_add_now(TS_START_VBOOT);
300
301 /* Set up context and work buffer */
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500302 vb2_init_work_context(&ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500303
Furquan Shaikhbe87f732016-06-29 11:26:27 -0700304 /* Read nvdata from a non-volatile storage. */
305 read_vbnv(ctx.nvdata);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500306
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800307 /* Set S3 resume flag if vboot should behave differently when selecting
308 * which slot to boot. This is only relevant to vboot if the platform
309 * does verification of memory init and thus must ensure it resumes with
310 * the same slot that it booted from. */
311 if (IS_ENABLED(CONFIG_RESUME_PATH_SAME_AS_BOOT) &&
312 IS_ENABLED(CONFIG_VBOOT_STARTS_IN_BOOTBLOCK) &&
313 vboot_platform_is_resuming())
314 ctx.flags |= VB2_CONTEXT_S3_RESUME;
315
Duncan Lauriea613a312016-03-14 09:32:08 -0700316 /* Read secdata from TPM. Initialize TPM if secdata not found. We don't
317 * check the return value here because vb2api_fw_phase1 will catch
318 * invalid secdata and tell us what to do (=reboot). */
319 timestamp_add_now(TS_START_TPMINIT);
320 antirollback_read_space_firmware(&ctx);
321 timestamp_add_now(TS_END_TPMINIT);
322
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500323 if (!IS_ENABLED(CONFIG_VIRTUAL_DEV_SWITCH) &&
324 get_developer_mode_switch())
325 ctx.flags |= VB2_CONTEXT_FORCE_DEVELOPER_MODE;
326
327 if (get_recovery_mode_switch()) {
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500328 ctx.flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE;
329 if (IS_ENABLED(CONFIG_VBOOT_DISABLE_DEV_ON_RECOVERY))
330 ctx.flags |= VB2_DISABLE_DEVELOPER_MODE;
331 }
332
333 if (IS_ENABLED(CONFIG_WIPEOUT_SUPPORTED) && get_wipeout_mode_switch())
334 ctx.flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE;
335
336 if (IS_ENABLED(CONFIG_LID_SWITCH) && !get_lid_switch())
337 ctx.flags |= VB2_CONTEXT_NOFAIL_BOOT;
338
339 /* Do early init (set up secdata and NVRAM, load GBB) */
340 printk(BIOS_INFO, "Phase 1\n");
341 rv = vb2api_fw_phase1(&ctx);
342
343 if (rv) {
344 /*
345 * If vb2api_fw_phase1 fails, check for return value.
346 * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue
347 * into recovery mode.
348 * For any other error code, save context if needed and reboot.
349 */
350 if (rv == VB2_ERROR_API_PHASE1_RECOVERY) {
351 printk(BIOS_INFO, "Recovery requested (%x)\n", rv);
352 save_if_needed(&ctx);
353 extend_pcrs(&ctx); /* ignore failures */
354 timestamp_add_now(TS_END_VBOOT);
355 return;
356 }
357
358 printk(BIOS_INFO, "Reboot reqested (%x)\n", rv);
359 save_if_needed(&ctx);
360 vboot_reboot();
361 }
362
363 /* Determine which firmware slot to boot (based on NVRAM) */
364 printk(BIOS_INFO, "Phase 2\n");
365 rv = vb2api_fw_phase2(&ctx);
366 if (rv) {
367 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
368 save_if_needed(&ctx);
369 vboot_reboot();
370 }
371
372 /* Try that slot (verify its keyblock and preamble) */
373 printk(BIOS_INFO, "Phase 3\n");
374 timestamp_add_now(TS_START_VERIFY_SLOT);
375 rv = vb2api_fw_phase3(&ctx);
376 timestamp_add_now(TS_END_VERIFY_SLOT);
377 if (rv) {
378 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
379 save_if_needed(&ctx);
380 vboot_reboot();
381 }
382
383 printk(BIOS_INFO, "Phase 4\n");
384 rv = locate_firmware(&ctx, &fw_main);
385 if (rv)
386 die("Failed to read FMAP to locate firmware");
387
388 rv = hash_body(&ctx, &fw_main);
389 save_if_needed(&ctx);
390 if (rv) {
391 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
392 vboot_reboot();
393 }
394
395 rv = extend_pcrs(&ctx);
396 if (rv) {
397 printk(BIOS_WARNING, "Failed to extend TPM PCRs (%#x)\n", rv);
398 vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv);
399 save_if_needed(&ctx);
400 vboot_reboot();
401 }
402
403 /* Lock TPM */
404 rv = antirollback_lock_space_firmware();
405 if (rv) {
406 printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv);
407 vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0);
408 save_if_needed(&ctx);
409 vboot_reboot();
410 }
411
412 printk(BIOS_INFO, "Slot %c is selected\n", is_slot_a(&ctx) ? 'A' : 'B');
Aaron Durbin6d720f32015-12-08 17:00:23 -0600413 vb2_set_selected_region(region_device_region(&fw_main));
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500414 timestamp_add_now(TS_END_VBOOT);
415}