blob: b84e47ef16795f127ff21b0fbbb1ab34905d1b91 [file] [log] [blame]
Daisuke Nojiri79cac092014-06-30 08:51:39 -07001/*
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 Nojiriefddcfb2014-09-04 09:55:34 -070020#include <arch/stages.h>
21#include <assert.h>
Daisuke Nojiri79cac092014-06-30 08:51:39 -070022#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 Spangler144c2282014-12-03 17:35:53 -080031#define NEED_VB20_INTERNALS /* TODO: remove me! */
32#include <vb2_api.h>
Daisuke Nojiri79cac092014-06-30 08:51:39 -070033#include <vboot_struct.h>
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -070034#include "../chromeos.h"
35#include "../fmap.h"
36#include "../vboot_handoff.h"
37#include "misc.h"
Daisuke Nojiri79cac092014-06-30 08:51:39 -070038
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -070039static void *load_ramstage(struct vboot_handoff *vboot_handoff,
40 struct vboot_region *fw_main)
41{
42 struct vboot_components *fw_info;
Julius Wernera7d92442014-12-02 20:51:19 -080043 void *ret;
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -070044 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 Wernera7d92442014-12-02 20:51:19 -080057 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 Nojiriefddcfb2014-09-04 09:55:34 -070061}
62
Daisuke Nojiri79cac092014-06-30 08:51:39 -070063/**
64 * Sets vboot_handoff based on the information in vb2_shared_data
65 *
Gediminas Ramanauskasceaabc92014-11-04 20:07:09 -080066 * TODO: Add VBSD_BOOT_FIRMWARE_SW_WP_ENABLED logic
Daisuke Nojiri79cac092014-06-30 08:51:39 -070067 */
68static void fill_vboot_handoff(struct vboot_handoff *vboot_handoff,
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -070069 struct vb2_shared_data *vb2_sd)
Daisuke Nojiri79cac092014-06-30 08:51:39 -070070{
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 Nojiri79cac092014-06-30 08:51:39 -070079 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 Wernere319e482015-01-29 17:50:58 -080086 vb_sd->fw_version_tpm = vb2_sd->fw_version_secdata;
Daisuke Nojiri79cac092014-06-30 08:51:39 -070087
Gediminas Ramanauskasceaabc92014-11-04 20:07:09 -080088 if (get_write_protect_state())
89 vb_sd->flags |= VBSD_BOOT_FIRMWARE_WP_ENABLED;
90
Daisuke Nojiri79cac092014-06-30 08:51:39 -070091 if (vb2_sd->recovery_reason) {
92 vb_sd->firmware_index = 0xFF;
93 if (vb2_sd->recovery_reason == VB2_RECOVERY_RO_MANUAL)
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -070094 vb_sd->flags |= VBSD_BOOT_REC_SWITCH_ON;
Daisuke Nojiri79cac092014-06-30 08:51:39 -070095 *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 Nojiriefddcfb2014-09-04 09:55:34 -0700108 /* TODO: Set these in depthcharge */
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700109 if (CONFIG_VIRTUAL_DEV_SWITCH)
110 vb_sd->flags |= VBSD_HONOR_VIRT_DEV_SWITCH;
Daisuke Nojiri2624c8d2014-11-13 11:35:52 -0800111 if (CONFIG_EC_SOFTWARE_SYNC)
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700112 vb_sd->flags |= VBSD_EC_SOFTWARE_SYNC;
Daisuke Nojiri2624c8d2014-11-13 11:35:52 -0800113 if (!CONFIG_PHYSICAL_REC_SWITCH)
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700114 vb_sd->flags |= VBSD_BOOT_REC_SWITCH_VIRTUAL;
Daisuke Nojiri2624c8d2014-11-13 11:35:52 -0800115
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700116 /* 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 Nojiriefddcfb2014-09-04 09:55:34 -0700127 printk(BIOS_INFO, "Copying FW preamble\n");
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -0700128 fp = (struct vb2_fw_preamble *)((uintptr_t)vb2_sd +
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700129 vb2_sd->workbuf_preamble_offset);
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700130 src = (uintptr_t)&fp->kernel_subkey +
131 fp->kernel_subkey.key_offset;
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700132 dst = (uintptr_t)vb_sd + sizeof(VbSharedDataHeader);
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700133 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 Nojiri79cac092014-06-30 08:51:39 -0700138 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 Nojiriefddcfb2014-09-04 09:55:34 -0700142 vb_sd->kernel_subkey.key_version =
143 fp->kernel_subkey.key_version;
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700144 }
145
146 vb_sd->recovery_reason = vb2_sd->recovery_reason;
147}
148
149/**
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700150 * Load ramstage and return the entry point
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700151 */
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -0700152void *vboot2_load_ramstage(void)
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700153{
154 struct vboot_handoff *vh;
155 struct vb2_shared_data *sd;
Daisuke Nojiri1bbac3f2014-09-12 09:59:46 -0700156 struct vboot_region fw_main;
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700157 struct vb2_working_data *wd = vboot_get_working_data();
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700158
Daisuke Nojirie5d13782014-12-18 11:59:06 -0800159 sd = vboot_get_work_buffer(wd);
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700160 sd->workbuf_hash_offset = 0;
161 sd->workbuf_hash_size = 0;
162
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700163 printk(BIOS_INFO, "creating vboot_handoff structure\n");
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700164 vh = cbmem_add(CBMEM_ID_VBOOT_HANDOFF, sizeof(*vh));
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700165 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 Nojiri79cac092014-06-30 08:51:39 -0700169
170 memset(vh, 0, sizeof(*vh));
171
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700172 /* needed until we finish transtion to vboot2 for kernel verification */
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700173 fill_vboot_handoff(vh, sd);
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700174
175 if (vboot_is_readonly_path(wd))
176 /* we're on recovery path. continue to ro-ramstage. */
177 return NULL;
178
Vadim Bendebury42001a72014-12-25 15:34:32 -0800179 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 Nojiri79cac092014-06-30 08:51:39 -0700188}