blob: 33ff79a4c2c37bfb86fe3127c8505da0e741f72d [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;
43 int i;
44
45 fw_info = vboot_locate_components(fw_main);
46 if (fw_info == NULL)
47 die("failed to locate firmware components\n");
48
49 /* these offset & size are used to load a rw boot loader */
50 for (i = 0; i < fw_info->num_components; i++) {
51 vboot_handoff->components[i].address =
52 fw_main->offset_addr + fw_info->entries[i].offset;
53 vboot_handoff->components[i].size = fw_info->entries[i].size;
54 }
55
56 return vboot_load_stage(CONFIG_VBOOT_RAMSTAGE_INDEX, fw_main, fw_info);
57}
58
Daisuke Nojiri79cac092014-06-30 08:51:39 -070059/**
60 * Sets vboot_handoff based on the information in vb2_shared_data
61 *
Gediminas Ramanauskasceaabc92014-11-04 20:07:09 -080062 * TODO: Add VBSD_BOOT_FIRMWARE_SW_WP_ENABLED logic
Daisuke Nojiri79cac092014-06-30 08:51:39 -070063 */
64static void fill_vboot_handoff(struct vboot_handoff *vboot_handoff,
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -070065 struct vb2_shared_data *vb2_sd)
Daisuke Nojiri79cac092014-06-30 08:51:39 -070066{
67 VbSharedDataHeader *vb_sd =
68 (VbSharedDataHeader *)vboot_handoff->shared_data;
69 uint32_t *oflags = &vboot_handoff->init_params.out_flags;
70
71 vb_sd->flags |= VBSD_BOOT_FIRMWARE_VBOOT2;
72
73 vboot_handoff->selected_firmware = vb2_sd->fw_slot;
74
Daisuke Nojiri79cac092014-06-30 08:51:39 -070075 vb_sd->firmware_index = vb2_sd->fw_slot;
76
77 vb_sd->magic = VB_SHARED_DATA_MAGIC;
78 vb_sd->struct_version = VB_SHARED_DATA_VERSION;
79 vb_sd->struct_size = sizeof(VbSharedDataHeader);
80 vb_sd->data_size = VB_SHARED_DATA_MIN_SIZE;
81 vb_sd->data_used = sizeof(VbSharedDataHeader);
Daisuke Nojiri183ad812014-11-17 09:54:08 -080082 vb_sd->fw_version_tpm = vb2_sd->fw_version;
Daisuke Nojiri79cac092014-06-30 08:51:39 -070083
Gediminas Ramanauskasceaabc92014-11-04 20:07:09 -080084 if (get_write_protect_state())
85 vb_sd->flags |= VBSD_BOOT_FIRMWARE_WP_ENABLED;
86
Daisuke Nojiri79cac092014-06-30 08:51:39 -070087 if (vb2_sd->recovery_reason) {
88 vb_sd->firmware_index = 0xFF;
89 if (vb2_sd->recovery_reason == VB2_RECOVERY_RO_MANUAL)
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -070090 vb_sd->flags |= VBSD_BOOT_REC_SWITCH_ON;
Daisuke Nojiri79cac092014-06-30 08:51:39 -070091 *oflags |= VB_INIT_OUT_ENABLE_RECOVERY;
92 *oflags |= VB_INIT_OUT_CLEAR_RAM;
93 *oflags |= VB_INIT_OUT_ENABLE_DISPLAY;
94 *oflags |= VB_INIT_OUT_ENABLE_USB_STORAGE;
95 }
96 if (vb2_sd->flags & VB2_SD_DEV_MODE_ENABLED) {
97 *oflags |= VB_INIT_OUT_ENABLE_DEVELOPER;
98 *oflags |= VB_INIT_OUT_CLEAR_RAM;
99 *oflags |= VB_INIT_OUT_ENABLE_DISPLAY;
100 *oflags |= VB_INIT_OUT_ENABLE_USB_STORAGE;
101 vb_sd->flags |= VBSD_BOOT_DEV_SWITCH_ON;
102 vb_sd->flags |= VBSD_LF_DEV_SWITCH_ON;
103 }
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700104 /* TODO: Set these in depthcharge */
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700105 if (CONFIG_VIRTUAL_DEV_SWITCH)
106 vb_sd->flags |= VBSD_HONOR_VIRT_DEV_SWITCH;
Daisuke Nojiri2624c8d2014-11-13 11:35:52 -0800107 if (CONFIG_EC_SOFTWARE_SYNC)
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700108 vb_sd->flags |= VBSD_EC_SOFTWARE_SYNC;
Daisuke Nojiri2624c8d2014-11-13 11:35:52 -0800109 if (!CONFIG_PHYSICAL_REC_SWITCH)
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700110 vb_sd->flags |= VBSD_BOOT_REC_SWITCH_VIRTUAL;
Daisuke Nojiri2624c8d2014-11-13 11:35:52 -0800111
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700112 /* In vboot1, VBSD_FWB_TRIED is
113 * set only if B is booted as explicitly requested. Therefore, if B is
114 * booted because A was found bad, the flag should not be set. It's
115 * better not to touch it if we can only ambiguously control it. */
116 /* if (vb2_sd->fw_slot)
117 vb_sd->flags |= VBSD_FWB_TRIED; */
118
119 /* copy kernel subkey if it's found */
120 if (vb2_sd->workbuf_preamble_size) {
121 struct vb2_fw_preamble *fp;
122 uintptr_t dst, src;
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700123 printk(BIOS_INFO, "Copying FW preamble\n");
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -0700124 fp = (struct vb2_fw_preamble *)((uintptr_t)vb2_sd +
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700125 vb2_sd->workbuf_preamble_offset);
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700126 src = (uintptr_t)&fp->kernel_subkey +
127 fp->kernel_subkey.key_offset;
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700128 dst = (uintptr_t)vb_sd + sizeof(VbSharedDataHeader);
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700129 assert(dst + fp->kernel_subkey.key_size <=
130 (uintptr_t)vboot_handoff + sizeof(*vboot_handoff));
131 memcpy((void *)dst, (void *)src,
132 fp->kernel_subkey.key_size);
133 vb_sd->data_used += fp->kernel_subkey.key_size;
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700134 vb_sd->kernel_subkey.key_offset =
135 dst - (uintptr_t)&vb_sd->kernel_subkey;
136 vb_sd->kernel_subkey.key_size = fp->kernel_subkey.key_size;
137 vb_sd->kernel_subkey.algorithm = fp->kernel_subkey.algorithm;
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700138 vb_sd->kernel_subkey.key_version =
139 fp->kernel_subkey.key_version;
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700140 }
141
142 vb_sd->recovery_reason = vb2_sd->recovery_reason;
143}
144
145/**
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700146 * Load ramstage and return the entry point
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700147 */
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -0700148void *vboot2_load_ramstage(void)
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700149{
150 struct vboot_handoff *vh;
151 struct vb2_shared_data *sd;
Daisuke Nojiri1bbac3f2014-09-12 09:59:46 -0700152 struct vboot_region fw_main;
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700153 struct vb2_working_data *wd = vboot_get_working_data();
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700154
Daisuke Nojiri1bbac3f2014-09-12 09:59:46 -0700155 sd = (struct vb2_shared_data *)(uintptr_t)wd->buffer;
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700156 sd->workbuf_hash_offset = 0;
157 sd->workbuf_hash_size = 0;
158
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700159 printk(BIOS_INFO, "creating vboot_handoff structure\n");
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700160 vh = cbmem_add(CBMEM_ID_VBOOT_HANDOFF, sizeof(*vh));
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700161 if (vh == NULL)
162 /* we don't need to failover gracefully here because this
163 * shouldn't happen with the image that has passed QA. */
164 die("failed to allocate vboot_handoff structure\n");
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700165
166 memset(vh, 0, sizeof(*vh));
167
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700168 /* needed until we finish transtion to vboot2 for kernel verification */
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700169 fill_vboot_handoff(vh, sd);
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700170
171 if (vboot_is_readonly_path(wd))
172 /* we're on recovery path. continue to ro-ramstage. */
173 return NULL;
174
175 printk(BIOS_INFO,
176 "loading ramstage from Slot %c\n", sd->fw_slot ? 'B' : 'A');
Daisuke Nojiri1bbac3f2014-09-12 09:59:46 -0700177 vb2_get_selected_region(wd, &fw_main);
178
179 return load_ramstage(vh, &fw_main);
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700180}