Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 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. |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
Aaron Durbin | 1f0ce3a | 2016-11-18 09:05:18 -0600 | [diff] [blame] | 16 | /* This needs to be pulled in first so that the handoff code below and |
| 17 | * peek into the vb2 data structures. Additionally, vboot doesn't currently |
| 18 | * include what it uses in its own headers. Provide the types it's after. |
| 19 | * TODO: fix this necessity. */ |
| 20 | #define NEED_VB20_INTERNALS |
| 21 | #include <stddef.h> |
| 22 | #include <stdint.h> |
| 23 | #include <vb2_api.h> |
| 24 | |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 25 | #include <arch/stages.h> |
| 26 | #include <assert.h> |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 27 | #include <bootmode.h> |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <cbfs.h> |
| 30 | #include <cbmem.h> |
| 31 | #include <console/console.h> |
| 32 | #include <console/vtxprintf.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 33 | #include <fmap.h> |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 34 | #include <stdlib.h> |
| 35 | #include <timestamp.h> |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 36 | #include <vboot_struct.h> |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 37 | #include <security/vboot/vbnv.h> |
| 38 | #include <security/vboot/misc.h> |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Sets vboot_handoff based on the information in vb2_shared_data |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 42 | */ |
| 43 | static void fill_vboot_handoff(struct vboot_handoff *vboot_handoff, |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 44 | struct vb2_shared_data *vb2_sd) |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 45 | { |
| 46 | VbSharedDataHeader *vb_sd = |
| 47 | (VbSharedDataHeader *)vboot_handoff->shared_data; |
| 48 | uint32_t *oflags = &vboot_handoff->init_params.out_flags; |
| 49 | |
| 50 | vb_sd->flags |= VBSD_BOOT_FIRMWARE_VBOOT2; |
| 51 | |
| 52 | vboot_handoff->selected_firmware = vb2_sd->fw_slot; |
| 53 | |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 54 | vb_sd->firmware_index = vb2_sd->fw_slot; |
| 55 | |
| 56 | vb_sd->magic = VB_SHARED_DATA_MAGIC; |
| 57 | vb_sd->struct_version = VB_SHARED_DATA_VERSION; |
| 58 | vb_sd->struct_size = sizeof(VbSharedDataHeader); |
| 59 | vb_sd->data_size = VB_SHARED_DATA_MIN_SIZE; |
| 60 | vb_sd->data_used = sizeof(VbSharedDataHeader); |
Julius Werner | e319e48 | 2015-01-29 17:50:58 -0800 | [diff] [blame] | 61 | vb_sd->fw_version_tpm = vb2_sd->fw_version_secdata; |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 62 | |
Gediminas Ramanauskas | ceaabc9 | 2014-11-04 20:07:09 -0800 | [diff] [blame] | 63 | if (get_write_protect_state()) |
| 64 | vb_sd->flags |= VBSD_BOOT_FIRMWARE_WP_ENABLED; |
| 65 | |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 66 | if (vb2_sd->recovery_reason) { |
| 67 | vb_sd->firmware_index = 0xFF; |
Daisuke Nojiri | 6ce7459 | 2015-10-21 13:31:44 -0700 | [diff] [blame] | 68 | if (vb2_sd->flags & VB2_SD_FLAG_MANUAL_RECOVERY) |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 69 | vb_sd->flags |= VBSD_BOOT_REC_SWITCH_ON; |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 70 | *oflags |= VB_INIT_OUT_ENABLE_RECOVERY; |
| 71 | *oflags |= VB_INIT_OUT_CLEAR_RAM; |
| 72 | *oflags |= VB_INIT_OUT_ENABLE_DISPLAY; |
| 73 | *oflags |= VB_INIT_OUT_ENABLE_USB_STORAGE; |
| 74 | } |
| 75 | if (vb2_sd->flags & VB2_SD_DEV_MODE_ENABLED) { |
| 76 | *oflags |= VB_INIT_OUT_ENABLE_DEVELOPER; |
| 77 | *oflags |= VB_INIT_OUT_CLEAR_RAM; |
| 78 | *oflags |= VB_INIT_OUT_ENABLE_DISPLAY; |
| 79 | *oflags |= VB_INIT_OUT_ENABLE_USB_STORAGE; |
| 80 | vb_sd->flags |= VBSD_BOOT_DEV_SWITCH_ON; |
| 81 | vb_sd->flags |= VBSD_LF_DEV_SWITCH_ON; |
| 82 | } |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 83 | /* TODO: Set these in depthcharge */ |
Julius Werner | 58c3938 | 2017-02-13 17:53:29 -0800 | [diff] [blame] | 84 | if (!IS_ENABLED(CONFIG_VBOOT_PHYSICAL_DEV_SWITCH)) |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 85 | vb_sd->flags |= VBSD_HONOR_VIRT_DEV_SWITCH; |
Daisuke Nojiri | 6732b4f | 2017-08-18 13:05:56 -0700 | [diff] [blame] | 86 | if (IS_ENABLED(CONFIG_VBOOT_EC_SOFTWARE_SYNC)) { |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 87 | vb_sd->flags |= VBSD_EC_SOFTWARE_SYNC; |
Daisuke Nojiri | 6732b4f | 2017-08-18 13:05:56 -0700 | [diff] [blame] | 88 | if (IS_ENABLED(CONFIG_VBOOT_EC_SLOW_UPDATE)) |
| 89 | vb_sd->flags |= VBSD_EC_SLOW_UPDATE; |
| 90 | if (IS_ENABLED(CONFIG_VBOOT_EC_EFS)) |
| 91 | vb_sd->flags |= VBSD_EC_EFS; |
| 92 | } |
Julius Werner | 58c3938 | 2017-02-13 17:53:29 -0800 | [diff] [blame] | 93 | if (!IS_ENABLED(CONFIG_VBOOT_PHYSICAL_REC_SWITCH)) |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 94 | vb_sd->flags |= VBSD_BOOT_REC_SWITCH_VIRTUAL; |
Duncan Laurie | 6324de1 | 2016-01-20 13:15:12 -0800 | [diff] [blame] | 95 | if (IS_ENABLED(CONFIG_VBOOT_OPROM_MATTERS)) { |
| 96 | vb_sd->flags |= VBSD_OPROM_MATTERS; |
| 97 | /* |
| 98 | * Inform vboot if the display was enabled by dev/rec |
| 99 | * mode or was requested by vboot kernel phase. |
| 100 | */ |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 101 | if ((*oflags & VB_INIT_OUT_ENABLE_DISPLAY) || |
Duncan Laurie | 6324de1 | 2016-01-20 13:15:12 -0800 | [diff] [blame] | 102 | vboot_wants_oprom()) { |
| 103 | vb_sd->flags |= VBSD_OPROM_LOADED; |
| 104 | *oflags |= VB_INIT_OUT_ENABLE_DISPLAY; |
| 105 | } |
| 106 | } |
Daisuke Nojiri | 2624c8d | 2014-11-13 11:35:52 -0800 | [diff] [blame] | 107 | |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 108 | /* In vboot1, VBSD_FWB_TRIED is |
| 109 | * set only if B is booted as explicitly requested. Therefore, if B is |
| 110 | * booted because A was found bad, the flag should not be set. It's |
| 111 | * better not to touch it if we can only ambiguously control it. */ |
| 112 | /* if (vb2_sd->fw_slot) |
| 113 | vb_sd->flags |= VBSD_FWB_TRIED; */ |
| 114 | |
| 115 | /* copy kernel subkey if it's found */ |
| 116 | if (vb2_sd->workbuf_preamble_size) { |
| 117 | struct vb2_fw_preamble *fp; |
| 118 | uintptr_t dst, src; |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 119 | printk(BIOS_INFO, "Copying FW preamble\n"); |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 120 | fp = (struct vb2_fw_preamble *)((uintptr_t)vb2_sd + |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 121 | vb2_sd->workbuf_preamble_offset); |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 122 | src = (uintptr_t)&fp->kernel_subkey + |
| 123 | fp->kernel_subkey.key_offset; |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 124 | dst = (uintptr_t)vb_sd + sizeof(VbSharedDataHeader); |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 125 | assert(dst + fp->kernel_subkey.key_size <= |
| 126 | (uintptr_t)vboot_handoff + sizeof(*vboot_handoff)); |
| 127 | memcpy((void *)dst, (void *)src, |
| 128 | fp->kernel_subkey.key_size); |
| 129 | vb_sd->data_used += fp->kernel_subkey.key_size; |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 130 | vb_sd->kernel_subkey.key_offset = |
| 131 | dst - (uintptr_t)&vb_sd->kernel_subkey; |
| 132 | vb_sd->kernel_subkey.key_size = fp->kernel_subkey.key_size; |
| 133 | vb_sd->kernel_subkey.algorithm = fp->kernel_subkey.algorithm; |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 134 | vb_sd->kernel_subkey.key_version = |
| 135 | fp->kernel_subkey.key_version; |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | vb_sd->recovery_reason = vb2_sd->recovery_reason; |
| 139 | } |
| 140 | |
Aaron Durbin | 17200ad | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 141 | void vboot_fill_handoff(void) |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 142 | { |
| 143 | struct vboot_handoff *vh; |
| 144 | struct vb2_shared_data *sd; |
| 145 | |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 146 | sd = vb2_get_shared_data(); |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 147 | sd->workbuf_hash_offset = 0; |
| 148 | sd->workbuf_hash_size = 0; |
| 149 | |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 150 | printk(BIOS_INFO, "creating vboot_handoff structure\n"); |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 151 | vh = cbmem_add(CBMEM_ID_VBOOT_HANDOFF, sizeof(*vh)); |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 152 | if (vh == NULL) |
| 153 | /* we don't need to failover gracefully here because this |
| 154 | * shouldn't happen with the image that has passed QA. */ |
| 155 | die("failed to allocate vboot_handoff structure\n"); |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 156 | |
| 157 | memset(vh, 0, sizeof(*vh)); |
| 158 | |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 159 | /* needed until we finish transtion to vboot2 for kernel verification */ |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 160 | fill_vboot_handoff(vh, sd); |
Aaron Durbin | d87cc3f | 2016-02-01 17:37:16 -0600 | [diff] [blame] | 161 | |
Furquan Shaikh | 626eea2 | 2016-11-12 11:09:28 -0800 | [diff] [blame] | 162 | |
| 163 | /* Log the recovery mode switches if required, before clearing them. */ |
| 164 | log_recovery_mode_switch(); |
| 165 | |
Aaron Durbin | d87cc3f | 2016-02-01 17:37:16 -0600 | [diff] [blame] | 166 | /* |
| 167 | * The recovery mode switch is cleared (typically backed by EC) here |
| 168 | * to allow multiple queries to get_recovery_mode_switch() and have |
| 169 | * them return consistent results during the verified boot path as well |
| 170 | * as dram initialization. x86 systems ignore the saved dram settings |
| 171 | * in the recovery path in order to start from a clean slate. Therefore |
| 172 | * clear the state here since this function is called when memory |
| 173 | * is known to be up. |
| 174 | */ |
| 175 | clear_recovery_mode_switch(); |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 176 | } |
Aaron Durbin | b593366 | 2015-10-07 16:03:41 -0500 | [diff] [blame] | 177 | |
| 178 | /* |
Julius Werner | fa8fa7d | 2017-03-16 19:32:48 -0700 | [diff] [blame] | 179 | * For platforms that employ VBOOT_STARTS_IN_ROMSTAGE, the vboot |
Aaron Durbin | b593366 | 2015-10-07 16:03:41 -0500 | [diff] [blame] | 180 | * verification doesn't happen until after cbmem is brought online. |
| 181 | * Therefore, the vboot results would not be initialized so don't |
| 182 | * automatically add results when cbmem comes online. |
| 183 | */ |
Julius Werner | fa8fa7d | 2017-03-16 19:32:48 -0700 | [diff] [blame] | 184 | #if !IS_ENABLED(CONFIG_VBOOT_STARTS_IN_ROMSTAGE) |
Aaron Durbin | b593366 | 2015-10-07 16:03:41 -0500 | [diff] [blame] | 185 | static void vb2_fill_handoff_cbmem(int unused) |
| 186 | { |
| 187 | vboot_fill_handoff(); |
| 188 | } |
| 189 | ROMSTAGE_CBMEM_INIT_HOOK(vb2_fill_handoff_cbmem) |
| 190 | #endif |