blob: e7e0d99632d43e6b15838d47148da4955a20afd6 [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
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Daisuke Nojiri79cac092014-06-30 08:51:39 -070018 */
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>
Aaron Durbin0424c952015-03-28 23:56:22 -050029#include <fmap.h>
Daisuke Nojiri79cac092014-06-30 08:51:39 -070030#include <stdlib.h>
31#include <timestamp.h>
Randall Spangler144c2282014-12-03 17:35:53 -080032#define NEED_VB20_INTERNALS /* TODO: remove me! */
33#include <vb2_api.h>
Daisuke Nojiri79cac092014-06-30 08:51:39 -070034#include <vboot_struct.h>
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -070035#include "../chromeos.h"
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -070036#include "../vboot_handoff.h"
37#include "misc.h"
Daisuke Nojiri79cac092014-06-30 08:51:39 -070038
39/**
40 * Sets vboot_handoff based on the information in vb2_shared_data
41 *
Gediminas Ramanauskasceaabc92014-11-04 20:07:09 -080042 * TODO: Add VBSD_BOOT_FIRMWARE_SW_WP_ENABLED logic
Daisuke Nojiri79cac092014-06-30 08:51:39 -070043 */
44static void fill_vboot_handoff(struct vboot_handoff *vboot_handoff,
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -070045 struct vb2_shared_data *vb2_sd)
Daisuke Nojiri79cac092014-06-30 08:51:39 -070046{
47 VbSharedDataHeader *vb_sd =
48 (VbSharedDataHeader *)vboot_handoff->shared_data;
49 uint32_t *oflags = &vboot_handoff->init_params.out_flags;
50
51 vb_sd->flags |= VBSD_BOOT_FIRMWARE_VBOOT2;
52
53 vboot_handoff->selected_firmware = vb2_sd->fw_slot;
54
Daisuke Nojiri79cac092014-06-30 08:51:39 -070055 vb_sd->firmware_index = vb2_sd->fw_slot;
56
57 vb_sd->magic = VB_SHARED_DATA_MAGIC;
58 vb_sd->struct_version = VB_SHARED_DATA_VERSION;
59 vb_sd->struct_size = sizeof(VbSharedDataHeader);
60 vb_sd->data_size = VB_SHARED_DATA_MIN_SIZE;
61 vb_sd->data_used = sizeof(VbSharedDataHeader);
Julius Wernere319e482015-01-29 17:50:58 -080062 vb_sd->fw_version_tpm = vb2_sd->fw_version_secdata;
Daisuke Nojiri79cac092014-06-30 08:51:39 -070063
Gediminas Ramanauskasceaabc92014-11-04 20:07:09 -080064 if (get_write_protect_state())
65 vb_sd->flags |= VBSD_BOOT_FIRMWARE_WP_ENABLED;
66
Daisuke Nojiri79cac092014-06-30 08:51:39 -070067 if (vb2_sd->recovery_reason) {
68 vb_sd->firmware_index = 0xFF;
69 if (vb2_sd->recovery_reason == VB2_RECOVERY_RO_MANUAL)
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -070070 vb_sd->flags |= VBSD_BOOT_REC_SWITCH_ON;
Daisuke Nojiri79cac092014-06-30 08:51:39 -070071 *oflags |= VB_INIT_OUT_ENABLE_RECOVERY;
72 *oflags |= VB_INIT_OUT_CLEAR_RAM;
73 *oflags |= VB_INIT_OUT_ENABLE_DISPLAY;
74 *oflags |= VB_INIT_OUT_ENABLE_USB_STORAGE;
75 }
76 if (vb2_sd->flags & VB2_SD_DEV_MODE_ENABLED) {
77 *oflags |= VB_INIT_OUT_ENABLE_DEVELOPER;
78 *oflags |= VB_INIT_OUT_CLEAR_RAM;
79 *oflags |= VB_INIT_OUT_ENABLE_DISPLAY;
80 *oflags |= VB_INIT_OUT_ENABLE_USB_STORAGE;
81 vb_sd->flags |= VBSD_BOOT_DEV_SWITCH_ON;
82 vb_sd->flags |= VBSD_LF_DEV_SWITCH_ON;
83 }
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -070084 /* TODO: Set these in depthcharge */
Daisuke Nojiri79cac092014-06-30 08:51:39 -070085 if (CONFIG_VIRTUAL_DEV_SWITCH)
86 vb_sd->flags |= VBSD_HONOR_VIRT_DEV_SWITCH;
Daisuke Nojiri2624c8d2014-11-13 11:35:52 -080087 if (CONFIG_EC_SOFTWARE_SYNC)
Daisuke Nojiri79cac092014-06-30 08:51:39 -070088 vb_sd->flags |= VBSD_EC_SOFTWARE_SYNC;
Daisuke Nojiri2624c8d2014-11-13 11:35:52 -080089 if (!CONFIG_PHYSICAL_REC_SWITCH)
Daisuke Nojiri79cac092014-06-30 08:51:39 -070090 vb_sd->flags |= VBSD_BOOT_REC_SWITCH_VIRTUAL;
Daisuke Nojiri2624c8d2014-11-13 11:35:52 -080091
Daisuke Nojiri79cac092014-06-30 08:51:39 -070092 /* In vboot1, VBSD_FWB_TRIED is
93 * set only if B is booted as explicitly requested. Therefore, if B is
94 * booted because A was found bad, the flag should not be set. It's
95 * better not to touch it if we can only ambiguously control it. */
96 /* if (vb2_sd->fw_slot)
97 vb_sd->flags |= VBSD_FWB_TRIED; */
98
99 /* copy kernel subkey if it's found */
100 if (vb2_sd->workbuf_preamble_size) {
101 struct vb2_fw_preamble *fp;
102 uintptr_t dst, src;
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700103 printk(BIOS_INFO, "Copying FW preamble\n");
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -0700104 fp = (struct vb2_fw_preamble *)((uintptr_t)vb2_sd +
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700105 vb2_sd->workbuf_preamble_offset);
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700106 src = (uintptr_t)&fp->kernel_subkey +
107 fp->kernel_subkey.key_offset;
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700108 dst = (uintptr_t)vb_sd + sizeof(VbSharedDataHeader);
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700109 assert(dst + fp->kernel_subkey.key_size <=
110 (uintptr_t)vboot_handoff + sizeof(*vboot_handoff));
111 memcpy((void *)dst, (void *)src,
112 fp->kernel_subkey.key_size);
113 vb_sd->data_used += fp->kernel_subkey.key_size;
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700114 vb_sd->kernel_subkey.key_offset =
115 dst - (uintptr_t)&vb_sd->kernel_subkey;
116 vb_sd->kernel_subkey.key_size = fp->kernel_subkey.key_size;
117 vb_sd->kernel_subkey.algorithm = fp->kernel_subkey.algorithm;
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700118 vb_sd->kernel_subkey.key_version =
119 fp->kernel_subkey.key_version;
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700120 }
121
122 vb_sd->recovery_reason = vb2_sd->recovery_reason;
123}
124
Aaron Durbin17200ad2015-05-01 16:48:54 -0500125void vboot_fill_handoff(void)
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700126{
Aaron Durbin17200ad2015-05-01 16:48:54 -0500127 int i;
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700128 struct vboot_handoff *vh;
129 struct vb2_shared_data *sd;
Aaron Durbinb6981c02015-05-15 15:57:51 -0500130 struct region fw_main;
Aaron Durbin17200ad2015-05-01 16:48:54 -0500131 struct vboot_components *fw_info;
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700132 struct vb2_working_data *wd = vboot_get_working_data();
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700133
Daisuke Nojirie5d13782014-12-18 11:59:06 -0800134 sd = vboot_get_work_buffer(wd);
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700135 sd->workbuf_hash_offset = 0;
136 sd->workbuf_hash_size = 0;
137
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700138 printk(BIOS_INFO, "creating vboot_handoff structure\n");
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700139 vh = cbmem_add(CBMEM_ID_VBOOT_HANDOFF, sizeof(*vh));
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700140 if (vh == NULL)
141 /* we don't need to failover gracefully here because this
142 * shouldn't happen with the image that has passed QA. */
143 die("failed to allocate vboot_handoff structure\n");
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700144
145 memset(vh, 0, sizeof(*vh));
146
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700147 /* needed until we finish transtion to vboot2 for kernel verification */
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700148 fill_vboot_handoff(vh, sd);
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700149
Aaron Durbin3dc60c52015-05-13 14:00:28 -0500150 /* Nothing left to do in readonly path. */
151 if (vboot_is_readonly_path(wd))
152 return;
153
Aaron Durbin17200ad2015-05-01 16:48:54 -0500154 vb2_get_selected_region(wd, &fw_main);
155 fw_info = vboot_locate_components(&fw_main);
156 if (fw_info == NULL)
157 die("failed to locate firmware components\n");
Daisuke Nojiriefddcfb2014-09-04 09:55:34 -0700158
Aaron Durbin17200ad2015-05-01 16:48:54 -0500159 /* these offset & size are used to load a rw boot loader */
160 for (i = 0; i < fw_info->num_components; i++) {
161 vh->components[i].address =
Aaron Durbinb6981c02015-05-15 15:57:51 -0500162 region_offset(&fw_main) + fw_info->entries[i].offset;
Aaron Durbin17200ad2015-05-01 16:48:54 -0500163 vh->components[i].size = fw_info->entries[i].size;
Vadim Bendebury42001a72014-12-25 15:34:32 -0800164 }
Daisuke Nojiri79cac092014-06-30 08:51:39 -0700165}