Angel Pons | 986d50e | 2020-04-02 23:48:53 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 2 | |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 3 | #include <boot_device.h> |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 4 | #include <cbfs.h> |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 5 | #include <commonlib/bsd/cbfs_private.h> |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 6 | #include <console/console.h> |
Aaron Durbin | f7ce40b | 2016-08-24 14:58:12 -0500 | [diff] [blame] | 7 | #include <ec/google/chromeec/ec.h> |
Aaron Durbin | 09560fa | 2015-05-12 16:43:10 -0500 | [diff] [blame] | 8 | #include <rmodule.h> |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 9 | #include <security/vboot/misc.h> |
| 10 | #include <security/vboot/symbols.h> |
| 11 | #include <security/vboot/vboot_common.h> |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 12 | |
Julius Werner | 73d042b | 2017-03-17 16:54:48 -0700 | [diff] [blame] | 13 | /* Ensure vboot configuration is valid: */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 14 | _Static_assert(CONFIG(VBOOT_STARTS_IN_BOOTBLOCK) + |
Martin Roth | 8a3a3c8 | 2020-05-04 10:13:45 -0600 | [diff] [blame] | 15 | CONFIG(VBOOT_STARTS_BEFORE_BOOTBLOCK) + |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 16 | CONFIG(VBOOT_STARTS_IN_ROMSTAGE) == 1, |
Martin Roth | 8a3a3c8 | 2020-05-04 10:13:45 -0600 | [diff] [blame] | 17 | "vboot must start in bootblock, PSP or romstage (but only one!)"); |
| 18 | _Static_assert(!CONFIG(VBOOT_SEPARATE_VERSTAGE) || CONFIG(VBOOT_STARTS_IN_BOOTBLOCK) || |
| 19 | CONFIG(VBOOT_STARTS_BEFORE_BOOTBLOCK), |
| 20 | "stand-alone verstage must start in or before bootblock "); |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 21 | _Static_assert(!CONFIG(VBOOT_RETURN_FROM_VERSTAGE) || |
| 22 | CONFIG(VBOOT_SEPARATE_VERSTAGE), |
Julius Werner | 73d042b | 2017-03-17 16:54:48 -0700 | [diff] [blame] | 23 | "return from verstage only makes sense for separate verstages"); |
| 24 | |
Arthur Heymans | 344e86b | 2019-11-20 19:47:10 +0100 | [diff] [blame] | 25 | int vboot_executed; |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 26 | |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 27 | static void after_verstage(void) |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 28 | { |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 29 | vboot_executed = 1; /* Mark verstage execution complete. */ |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 30 | |
| 31 | const struct cbfs_boot_device *cbd = vboot_get_cbfs_boot_device(); |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 32 | if (!cbd) /* Can't initialize RW CBFS in recovery mode. */ |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 33 | return; |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 34 | |
| 35 | cb_err_t err = cbfs_init_boot_device(cbd, NULL); /* TODO: RW hash */ |
| 36 | if (err && err != CB_CBFS_CACHE_FULL) /* TODO: -> recovery? */ |
| 37 | die("RW CBFS initialization failure: %d", err); |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Wim Vervoorn | 1058dd8 | 2019-11-01 10:22:22 +0100 | [diff] [blame] | 40 | void vboot_run_logic(void) |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 41 | { |
Paul Kocialkowski | 1811768 | 2016-05-14 15:30:52 +0200 | [diff] [blame] | 42 | if (verification_should_run()) { |
Julius Werner | 58c3938 | 2017-02-13 17:53:29 -0800 | [diff] [blame] | 43 | /* Note: this path is not used for VBOOT_RETURN_FROM_VERSTAGE */ |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 44 | verstage_main(); |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 45 | after_verstage(); |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 46 | } else if (verstage_should_load()) { |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 47 | struct cbfsf file; |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 48 | struct prog verstage = |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 49 | PROG_INIT(PROG_VERSTAGE, |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 50 | CONFIG_CBFS_PREFIX "/verstage"); |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 51 | |
Aaron Durbin | ce2c50d | 2015-05-13 13:33:27 -0500 | [diff] [blame] | 52 | printk(BIOS_DEBUG, "VBOOT: Loading verstage.\n"); |
| 53 | |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 54 | /* load verstage from RO */ |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 55 | if (cbfs_boot_locate(&file, prog_name(&verstage), NULL)) |
| 56 | die("failed to load verstage"); |
| 57 | |
| 58 | cbfs_file_data(prog_rdev(&verstage), &file); |
| 59 | |
| 60 | if (cbfs_prog_stage_load(&verstage)) |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 61 | die("failed to load verstage"); |
| 62 | |
| 63 | /* verify and select a slot */ |
| 64 | prog_run(&verstage); |
| 65 | |
| 66 | /* This is not actually possible to hit this condition at |
| 67 | * runtime, but this provides a hint to the compiler for dead |
| 68 | * code elimination below. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 69 | if (!CONFIG(VBOOT_RETURN_FROM_VERSTAGE)) |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 70 | return; |
| 71 | |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 72 | after_verstage(); |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 73 | } |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 76 | const struct cbfs_boot_device *vboot_get_cbfs_boot_device(void) |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 77 | { |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 78 | /* Don't honor vboot results until the vboot logic has run. */ |
Joel Kitching | af8471c | 2019-03-13 22:38:07 +0800 | [diff] [blame] | 79 | if (!vboot_logic_executed()) |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 80 | return NULL; |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 81 | |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 82 | static struct cbfs_boot_device cbd; |
| 83 | if (region_device_sz(&cbd.rdev)) |
| 84 | return &cbd; |
Yu-Ping Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 85 | |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 86 | struct vb2_context *ctx = vboot_get_context(); |
Yu-Ping Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 87 | if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 88 | return NULL; |
Aaron Durbin | b6981c0 | 2015-05-15 15:57:51 -0500 | [diff] [blame] | 89 | |
Julius Werner | 1e37c9c | 2019-12-11 17:09:39 -0800 | [diff] [blame] | 90 | boot_device_init(); |
| 91 | if (vboot_locate_firmware(ctx, &cbd.rdev)) |
| 92 | return NULL; |
| 93 | |
| 94 | cbfs_boot_device_find_mcache(&cbd, CBMEM_ID_CBFS_RW_MCACHE); |
| 95 | |
| 96 | return &cbd; |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 97 | } |