blob: 558db3cc8de86cd06fffd5de84bbc0ad49042955 [file] [log] [blame]
Aaron Durbinae5d83e2013-10-24 10:21:43 -05001/*
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
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080020#include <string.h>
21#include <arch/acpi.h>
Aaron Durbinae5d83e2013-10-24 10:21:43 -050022#include <cbmem.h>
23#include <console/console.h>
Aaron Durbin43b7db72015-03-27 20:53:30 -050024#include <console/streams.h>
Aaron Durbin8fa62832013-10-28 09:54:22 -050025#include <cpu/x86/tsc.h>
Aaron Durbin460703b2015-03-27 21:17:22 -050026#include <program_loading.h>
Aaron Durbinae5d83e2013-10-24 10:21:43 -050027#include <rmodule.h>
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060028#include <stage_cache.h>
Aaron Durbin43b7db72015-03-27 20:53:30 -050029#if IS_ENABLED(CONFIG_CHROMEOS)
Aaron Durbin7f177592013-12-13 13:05:24 -080030#include <vendorcode/google/chromeos/vboot_handoff.h>
Aaron Durbin43b7db72015-03-27 20:53:30 -050031#endif
Aaron Durbinae5d83e2013-10-24 10:21:43 -050032
Julius Werner18ea2d32014-10-07 16:42:17 -070033#include <soc/ramstage.h>
34#include <soc/efi_wrapper.h>
Aaron Durbinae5d83e2013-10-24 10:21:43 -050035
36static void ABI_X86 send_to_console(unsigned char b)
37{
38 console_tx_byte(b);
39}
40
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080041static efi_wrapper_entry_t load_refcode_from_cache(void)
42{
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060043 struct prog refcode;
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080044
45 printk(BIOS_DEBUG, "refcode loading from cache.\n");
46
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060047 stage_cache_load_stage(STAGE_REFCODE, &refcode);
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080048
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060049 return (efi_wrapper_entry_t)prog_entry(&refcode);
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080050}
51
52static void cache_refcode(const struct rmod_stage_load *rsl)
53{
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060054 stage_cache_add(STAGE_REFCODE, rsl->prog);
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080055}
56
Aaron Durbin43b7db72015-03-27 20:53:30 -050057#if IS_ENABLED(CONFIG_CHROMEOS)
58static int load_refcode_from_vboot(struct rmod_stage_load *refcode)
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080059{
Aaron Durbin43b7db72015-03-27 20:53:30 -050060 struct vboot_handoff *vboot_handoff;
61 const struct firmware_component *fwc;
62 struct cbfs_stage *stage;
63
64 vboot_handoff = cbmem_find(CBMEM_ID_VBOOT_HANDOFF);
65 fwc = &vboot_handoff->components[CONFIG_VBOOT_REFCODE_INDEX];
66
67 if (vboot_handoff == NULL ||
68 vboot_handoff->selected_firmware == VB_SELECT_FIRMWARE_READONLY ||
69 CONFIG_VBOOT_REFCODE_INDEX >= MAX_PARSED_FW_COMPONENTS ||
70 fwc->size == 0 || fwc->address == 0)
71 return -1;
72
Aaron Durbin7f177592013-12-13 13:05:24 -080073 printk(BIOS_DEBUG, "refcode loading from vboot rw area.\n");
Aaron Durbin43b7db72015-03-27 20:53:30 -050074 stage = (void *)(uintptr_t)fwc->address;
Aaron Durbin7f177592013-12-13 13:05:24 -080075
Aaron Durbin460703b2015-03-27 21:17:22 -050076 if (rmodule_stage_load(refcode, stage)) {
Aaron Durbin7f177592013-12-13 13:05:24 -080077 printk(BIOS_DEBUG, "Error loading reference code.\n");
78 return -1;
79 }
80 return 0;
81}
Aaron Durbin43b7db72015-03-27 20:53:30 -050082#else
83static int load_refcode_from_vboot(struct rmod_stage_load *refcode)
84{
85 return -1;
86}
87#endif
Aaron Durbin7f177592013-12-13 13:05:24 -080088
89static int load_refcode_from_cbfs(struct rmod_stage_load *refcode)
90{
91 printk(BIOS_DEBUG, "refcode loading from cbfs.\n");
92
Aaron Durbin460703b2015-03-27 21:17:22 -050093 if (rmodule_stage_load_from_cbfs(refcode)) {
Aaron Durbin7f177592013-12-13 13:05:24 -080094 printk(BIOS_DEBUG, "Error loading reference code.\n");
95 return -1;
96 }
97
98 return 0;
99}
100
101static efi_wrapper_entry_t load_reference_code(void)
102{
Aaron Durbin460703b2015-03-27 21:17:22 -0500103 struct prog prog = {
104 .name = CONFIG_CBFS_PREFIX "/refcode",
105 };
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800106 struct rmod_stage_load refcode = {
107 .cbmem_id = CBMEM_ID_REFCODE,
Aaron Durbin460703b2015-03-27 21:17:22 -0500108 .prog = &prog,
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800109 };
110
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +0300111 if (acpi_is_wakeup_s3()) {
Aaron Durbin7f177592013-12-13 13:05:24 -0800112 return load_refcode_from_cache();
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800113 }
114
Aaron Durbin43b7db72015-03-27 20:53:30 -0500115 if (load_refcode_from_vboot(&refcode) ||
116 load_refcode_from_cbfs(&refcode))
117 return NULL;
Aaron Durbin7f177592013-12-13 13:05:24 -0800118
119 /* Cache loaded reference code. */
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800120 cache_refcode(&refcode);
121
Aaron Durbin460703b2015-03-27 21:17:22 -0500122 return prog_entry(&prog);
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800123}
124
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500125void baytrail_run_reference_code(void)
126{
127 int ret;
128 efi_wrapper_entry_t entry;
129 struct efi_wrapper_params wrp = {
130 .version = EFI_WRAPPER_VER,
131 .console_out = send_to_console,
132 };
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500133
Aaron Durbin7f177592013-12-13 13:05:24 -0800134 entry = load_reference_code();
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500135
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800136 if (entry == NULL)
137 return;
138
Aaron Durbin8fa62832013-10-28 09:54:22 -0500139 wrp.tsc_ticks_per_microsecond = tsc_freq_mhz();
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500140
141 /* Call into reference code. */
142 ret = entry(&wrp);
143
144 if (ret != 0) {
145 printk(BIOS_DEBUG, "Reference code returned %d\n", ret);
146 return;
147 }
148}