Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [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 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <arch/acpi.h> |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 22 | #include <cbmem.h> |
| 23 | #include <console/console.h> |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 24 | #include <console/streams.h> |
Aaron Durbin | 8fa6283 | 2013-10-28 09:54:22 -0500 | [diff] [blame] | 25 | #include <cpu/x86/tsc.h> |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 26 | #include <program_loading.h> |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 27 | #include <rmodule.h> |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame^] | 28 | #include <stage_cache.h> |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 29 | #if IS_ENABLED(CONFIG_CHROMEOS) |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 30 | #include <vendorcode/google/chromeos/vboot_handoff.h> |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 31 | #endif |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 32 | |
Julius Werner | 18ea2d3 | 2014-10-07 16:42:17 -0700 | [diff] [blame] | 33 | #include <soc/ramstage.h> |
| 34 | #include <soc/efi_wrapper.h> |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 35 | |
| 36 | static void ABI_X86 send_to_console(unsigned char b) |
| 37 | { |
| 38 | console_tx_byte(b); |
| 39 | } |
| 40 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 41 | static efi_wrapper_entry_t load_refcode_from_cache(void) |
| 42 | { |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame^] | 43 | struct prog refcode; |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 44 | |
| 45 | printk(BIOS_DEBUG, "refcode loading from cache.\n"); |
| 46 | |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame^] | 47 | stage_cache_load_stage(STAGE_REFCODE, &refcode); |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 48 | |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame^] | 49 | return (efi_wrapper_entry_t)prog_entry(&refcode); |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static void cache_refcode(const struct rmod_stage_load *rsl) |
| 53 | { |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame^] | 54 | stage_cache_add(STAGE_REFCODE, rsl->prog); |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 57 | #if IS_ENABLED(CONFIG_CHROMEOS) |
| 58 | static int load_refcode_from_vboot(struct rmod_stage_load *refcode) |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 59 | { |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 60 | 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 Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 73 | printk(BIOS_DEBUG, "refcode loading from vboot rw area.\n"); |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 74 | stage = (void *)(uintptr_t)fwc->address; |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 75 | |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 76 | if (rmodule_stage_load(refcode, stage)) { |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 77 | printk(BIOS_DEBUG, "Error loading reference code.\n"); |
| 78 | return -1; |
| 79 | } |
| 80 | return 0; |
| 81 | } |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 82 | #else |
| 83 | static int load_refcode_from_vboot(struct rmod_stage_load *refcode) |
| 84 | { |
| 85 | return -1; |
| 86 | } |
| 87 | #endif |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 88 | |
| 89 | static int load_refcode_from_cbfs(struct rmod_stage_load *refcode) |
| 90 | { |
| 91 | printk(BIOS_DEBUG, "refcode loading from cbfs.\n"); |
| 92 | |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 93 | if (rmodule_stage_load_from_cbfs(refcode)) { |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 94 | printk(BIOS_DEBUG, "Error loading reference code.\n"); |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static efi_wrapper_entry_t load_reference_code(void) |
| 102 | { |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 103 | struct prog prog = { |
| 104 | .name = CONFIG_CBFS_PREFIX "/refcode", |
| 105 | }; |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 106 | struct rmod_stage_load refcode = { |
| 107 | .cbmem_id = CBMEM_ID_REFCODE, |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 108 | .prog = &prog, |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
Kyösti Mälkki | c3ed886 | 2014-06-19 19:50:51 +0300 | [diff] [blame] | 111 | if (acpi_is_wakeup_s3()) { |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 112 | return load_refcode_from_cache(); |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 115 | if (load_refcode_from_vboot(&refcode) || |
| 116 | load_refcode_from_cbfs(&refcode)) |
| 117 | return NULL; |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 118 | |
| 119 | /* Cache loaded reference code. */ |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 120 | cache_refcode(&refcode); |
| 121 | |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 122 | return prog_entry(&prog); |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 125 | void 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 Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 133 | |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 134 | entry = load_reference_code(); |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 135 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 136 | if (entry == NULL) |
| 137 | return; |
| 138 | |
Aaron Durbin | 8fa6283 | 2013-10-28 09:54:22 -0500 | [diff] [blame] | 139 | wrp.tsc_ticks_per_microsecond = tsc_freq_mhz(); |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 140 | |
| 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 | } |