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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 20 | #include <arch/stages.h> |
| 21 | #include <assert.h> |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <stddef.h> |
| 24 | #include <string.h> |
| 25 | #include <cbfs.h> |
| 26 | #include <cbmem.h> |
| 27 | #include <console/console.h> |
| 28 | #include <console/vtxprintf.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <timestamp.h> |
Randall Spangler | 144c228 | 2014-12-03 17:35:53 -0800 | [diff] [blame] | 31 | #define NEED_VB20_INTERNALS /* TODO: remove me! */ |
| 32 | #include <vb2_api.h> |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 33 | #include <vboot_struct.h> |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 34 | #include "../chromeos.h" |
| 35 | #include "../fmap.h" |
| 36 | #include "../vboot_handoff.h" |
| 37 | #include "misc.h" |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 38 | |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 39 | static void *load_ramstage(struct vboot_handoff *vboot_handoff, |
| 40 | struct vboot_region *fw_main) |
| 41 | { |
| 42 | struct vboot_components *fw_info; |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 43 | void *ret; |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 44 | int i; |
| 45 | |
| 46 | fw_info = vboot_locate_components(fw_main); |
| 47 | if (fw_info == NULL) |
| 48 | die("failed to locate firmware components\n"); |
| 49 | |
| 50 | /* these offset & size are used to load a rw boot loader */ |
| 51 | for (i = 0; i < fw_info->num_components; i++) { |
| 52 | vboot_handoff->components[i].address = |
| 53 | fw_main->offset_addr + fw_info->entries[i].offset; |
| 54 | vboot_handoff->components[i].size = fw_info->entries[i].size; |
| 55 | } |
| 56 | |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 57 | timestamp_add_now(TS_START_COPYRAM); |
| 58 | ret = vboot_load_stage(CONFIG_VBOOT_RAMSTAGE_INDEX, fw_main, fw_info); |
| 59 | timestamp_add_now(TS_END_COPYRAM); |
| 60 | return ret; |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Sets vboot_handoff based on the information in vb2_shared_data |
| 65 | * |
Gediminas Ramanauskas | ceaabc9 | 2014-11-04 20:07:09 -0800 | [diff] [blame] | 66 | * TODO: Add VBSD_BOOT_FIRMWARE_SW_WP_ENABLED logic |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 67 | */ |
| 68 | static void fill_vboot_handoff(struct vboot_handoff *vboot_handoff, |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 69 | struct vb2_shared_data *vb2_sd) |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 70 | { |
| 71 | VbSharedDataHeader *vb_sd = |
| 72 | (VbSharedDataHeader *)vboot_handoff->shared_data; |
| 73 | uint32_t *oflags = &vboot_handoff->init_params.out_flags; |
| 74 | |
| 75 | vb_sd->flags |= VBSD_BOOT_FIRMWARE_VBOOT2; |
| 76 | |
| 77 | vboot_handoff->selected_firmware = vb2_sd->fw_slot; |
| 78 | |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 79 | vb_sd->firmware_index = vb2_sd->fw_slot; |
| 80 | |
| 81 | vb_sd->magic = VB_SHARED_DATA_MAGIC; |
| 82 | vb_sd->struct_version = VB_SHARED_DATA_VERSION; |
| 83 | vb_sd->struct_size = sizeof(VbSharedDataHeader); |
| 84 | vb_sd->data_size = VB_SHARED_DATA_MIN_SIZE; |
| 85 | vb_sd->data_used = sizeof(VbSharedDataHeader); |
Julius Werner | e319e48 | 2015-01-29 17:50:58 -0800 | [diff] [blame] | 86 | vb_sd->fw_version_tpm = vb2_sd->fw_version_secdata; |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 87 | |
Gediminas Ramanauskas | ceaabc9 | 2014-11-04 20:07:09 -0800 | [diff] [blame] | 88 | if (get_write_protect_state()) |
| 89 | vb_sd->flags |= VBSD_BOOT_FIRMWARE_WP_ENABLED; |
| 90 | |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 91 | if (vb2_sd->recovery_reason) { |
| 92 | vb_sd->firmware_index = 0xFF; |
| 93 | if (vb2_sd->recovery_reason == VB2_RECOVERY_RO_MANUAL) |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 94 | vb_sd->flags |= VBSD_BOOT_REC_SWITCH_ON; |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 95 | *oflags |= VB_INIT_OUT_ENABLE_RECOVERY; |
| 96 | *oflags |= VB_INIT_OUT_CLEAR_RAM; |
| 97 | *oflags |= VB_INIT_OUT_ENABLE_DISPLAY; |
| 98 | *oflags |= VB_INIT_OUT_ENABLE_USB_STORAGE; |
| 99 | } |
| 100 | if (vb2_sd->flags & VB2_SD_DEV_MODE_ENABLED) { |
| 101 | *oflags |= VB_INIT_OUT_ENABLE_DEVELOPER; |
| 102 | *oflags |= VB_INIT_OUT_CLEAR_RAM; |
| 103 | *oflags |= VB_INIT_OUT_ENABLE_DISPLAY; |
| 104 | *oflags |= VB_INIT_OUT_ENABLE_USB_STORAGE; |
| 105 | vb_sd->flags |= VBSD_BOOT_DEV_SWITCH_ON; |
| 106 | vb_sd->flags |= VBSD_LF_DEV_SWITCH_ON; |
| 107 | } |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 108 | /* TODO: Set these in depthcharge */ |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 109 | if (CONFIG_VIRTUAL_DEV_SWITCH) |
| 110 | vb_sd->flags |= VBSD_HONOR_VIRT_DEV_SWITCH; |
Daisuke Nojiri | 2624c8d | 2014-11-13 11:35:52 -0800 | [diff] [blame] | 111 | if (CONFIG_EC_SOFTWARE_SYNC) |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 112 | vb_sd->flags |= VBSD_EC_SOFTWARE_SYNC; |
Daisuke Nojiri | 2624c8d | 2014-11-13 11:35:52 -0800 | [diff] [blame] | 113 | if (!CONFIG_PHYSICAL_REC_SWITCH) |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 114 | vb_sd->flags |= VBSD_BOOT_REC_SWITCH_VIRTUAL; |
Daisuke Nojiri | 2624c8d | 2014-11-13 11:35:52 -0800 | [diff] [blame] | 115 | |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 116 | /* In vboot1, VBSD_FWB_TRIED is |
| 117 | * set only if B is booted as explicitly requested. Therefore, if B is |
| 118 | * booted because A was found bad, the flag should not be set. It's |
| 119 | * better not to touch it if we can only ambiguously control it. */ |
| 120 | /* if (vb2_sd->fw_slot) |
| 121 | vb_sd->flags |= VBSD_FWB_TRIED; */ |
| 122 | |
| 123 | /* copy kernel subkey if it's found */ |
| 124 | if (vb2_sd->workbuf_preamble_size) { |
| 125 | struct vb2_fw_preamble *fp; |
| 126 | uintptr_t dst, src; |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 127 | printk(BIOS_INFO, "Copying FW preamble\n"); |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 128 | fp = (struct vb2_fw_preamble *)((uintptr_t)vb2_sd + |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 129 | vb2_sd->workbuf_preamble_offset); |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 130 | src = (uintptr_t)&fp->kernel_subkey + |
| 131 | fp->kernel_subkey.key_offset; |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 132 | dst = (uintptr_t)vb_sd + sizeof(VbSharedDataHeader); |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 133 | assert(dst + fp->kernel_subkey.key_size <= |
| 134 | (uintptr_t)vboot_handoff + sizeof(*vboot_handoff)); |
| 135 | memcpy((void *)dst, (void *)src, |
| 136 | fp->kernel_subkey.key_size); |
| 137 | vb_sd->data_used += fp->kernel_subkey.key_size; |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 138 | vb_sd->kernel_subkey.key_offset = |
| 139 | dst - (uintptr_t)&vb_sd->kernel_subkey; |
| 140 | vb_sd->kernel_subkey.key_size = fp->kernel_subkey.key_size; |
| 141 | vb_sd->kernel_subkey.algorithm = fp->kernel_subkey.algorithm; |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 142 | vb_sd->kernel_subkey.key_version = |
| 143 | fp->kernel_subkey.key_version; |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | vb_sd->recovery_reason = vb2_sd->recovery_reason; |
| 147 | } |
| 148 | |
| 149 | /** |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 150 | * Load ramstage and return the entry point |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 151 | */ |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 152 | void *vboot2_load_ramstage(void) |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 153 | { |
| 154 | struct vboot_handoff *vh; |
| 155 | struct vb2_shared_data *sd; |
Daisuke Nojiri | 1bbac3f | 2014-09-12 09:59:46 -0700 | [diff] [blame] | 156 | struct vboot_region fw_main; |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 157 | struct vb2_working_data *wd = vboot_get_working_data(); |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 158 | |
Daisuke Nojiri | e5d1378 | 2014-12-18 11:59:06 -0800 | [diff] [blame] | 159 | sd = vboot_get_work_buffer(wd); |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 160 | sd->workbuf_hash_offset = 0; |
| 161 | sd->workbuf_hash_size = 0; |
| 162 | |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 163 | printk(BIOS_INFO, "creating vboot_handoff structure\n"); |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 164 | vh = cbmem_add(CBMEM_ID_VBOOT_HANDOFF, sizeof(*vh)); |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 165 | if (vh == NULL) |
| 166 | /* we don't need to failover gracefully here because this |
| 167 | * shouldn't happen with the image that has passed QA. */ |
| 168 | die("failed to allocate vboot_handoff structure\n"); |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 169 | |
| 170 | memset(vh, 0, sizeof(*vh)); |
| 171 | |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 172 | /* needed until we finish transtion to vboot2 for kernel verification */ |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 173 | fill_vboot_handoff(vh, sd); |
Daisuke Nojiri | efddcfb | 2014-09-04 09:55:34 -0700 | [diff] [blame] | 174 | |
| 175 | if (vboot_is_readonly_path(wd)) |
| 176 | /* we're on recovery path. continue to ro-ramstage. */ |
| 177 | return NULL; |
| 178 | |
Vadim Bendebury | 42001a7 | 2014-12-25 15:34:32 -0800 | [diff] [blame^] | 179 | if (IS_ENABLED(CONFIG_MULTIPLE_CBFS_INSTANCES)) { |
| 180 | return cbfs_load_stage(CBFS_DEFAULT_MEDIA, |
| 181 | CONFIG_CBFS_PREFIX "/ramstage"); |
| 182 | } else { |
| 183 | printk(BIOS_INFO, "loading ramstage from Slot %c\n", |
| 184 | sd->fw_slot ? 'B' : 'A'); |
| 185 | vb2_get_selected_region(wd, &fw_main); |
| 186 | return load_ramstage(vh, &fw_main); |
| 187 | } |
Daisuke Nojiri | 79cac09 | 2014-06-30 08:51:39 -0700 | [diff] [blame] | 188 | } |