| 1 |
|
| 2 |
|
| 3 |
#include <arch/romstage.h> |
| 4 |
#include <cbmem.h> |
| 5 |
#include <console/console.h> |
| 6 |
#include <cpu/x86/msr.h> |
| 7 |
#include <cpu/x86/mtrr.h> |
| 8 |
#include <cpu/x86/smm.h> |
| 9 |
#include <program_loading.h> |
| 10 |
#include <reset.h> |
| 11 |
#include <rmodule.h> |
| 12 |
#include <romstage_handoff.h> |
| 13 |
#include <security/vboot/vboot_common.h> |
| 14 |
#include <stage_cache.h> |
| 15 |
#include <timestamp.h> |
| 16 |
#include <types.h> |
| 17 |
|
| 18 |
static size_t var_mtrr_ctx_size(void) |
| 19 |
{ |
| 20 |
int mtrr_count = get_var_mtrr_count(); |
| 21 |
return sizeof(struct var_mtrr_context) + mtrr_count * 2 * sizeof(msr_t); |
| 22 |
} |
| 23 |
|
| 24 |
static enum cb_err postcar_frame_init(struct postcar_frame *pcf) |
| 25 |
{ |
| 26 |
memset(pcf, 0, sizeof(*pcf)); |
| 27 |
|
| 28 |
struct var_mtrr_context *ctx; |
| 29 |
|
| 30 |
ctx = cbmem_add(CBMEM_ID_ROMSTAGE_RAM_STACK, var_mtrr_ctx_size()); |
| 31 |
if (ctx == NULL) { |
| 32 |
printk(BIOS_ERR, "Couldn't add var_mtrr_ctx setup in cbmem.\n"); |
| 33 |
return CB_ERR; |
| 34 |
} |
| 35 |
|
| 36 |
pcf->mtrr = ctx; |
| 37 |
var_mtrr_context_init(pcf->mtrr); |
| 38 |
|
| 39 |
return CB_SUCCESS; |
| 40 |
} |
| 41 |
|
| 42 |
void postcar_frame_add_mtrr(struct postcar_frame *pcf, |
| 43 |
uintptr_t addr, size_t size, int type) |
| 44 |
{ |
| 45 |
var_mtrr_set(pcf->mtrr, addr, size, type); |
| 46 |
} |
| 47 |
|
| 48 |
void postcar_frame_add_romcache(struct postcar_frame *pcf, int type) |
| 49 |
{ |
| 50 |
if (!CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) |
| 51 |
return; |
| 52 |
postcar_frame_add_mtrr(pcf, CACHE_ROM_BASE, CACHE_ROM_SIZE, type); |
| 53 |
} |
| 54 |
|
| 55 |
static void postcar_frame_common_mtrrs(struct postcar_frame *pcf) |
| 56 |
{ |
| 57 |
if (pcf->skip_common_mtrr) |
| 58 |
return; |
| 59 |
|
| 60 |
|
| 61 |
postcar_frame_add_romcache(pcf, MTRR_TYPE_WRPROT); |
| 62 |
} |
| 63 |
|
| 64 |
static void run_postcar_phase(struct postcar_frame *pcf); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
void __noreturn prepare_and_run_postcar(void) |
| 69 |
{ |
| 70 |
struct postcar_frame pcf; |
| 71 |
|
| 72 |
if (postcar_frame_init(&pcf)) |
| 73 |
die("Unable to initialize postcar frame.\n"); |
| 74 |
|
| 75 |
fill_postcar_frame(&pcf); |
| 76 |
|
| 77 |
postcar_frame_common_mtrrs(&pcf); |
| 78 |
|
| 79 |
run_postcar_phase(&pcf); |
| 80 |
|
| 81 |
die("Failed to load postcar\n!"); |
| 82 |
} |
| 83 |
|
| 84 |
static void finalize_load(uintptr_t *reloc_params, uintptr_t mtrr_frame_ptr) |
| 85 |
{ |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
*reloc_params = mtrr_frame_ptr; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
prog_segment_loaded((uintptr_t)reloc_params, sizeof(uintptr_t), SEG_FINAL); |
| 97 |
prog_segment_loaded((uintptr_t)mtrr_frame_ptr, var_mtrr_ctx_size(), SEG_FINAL); |
| 98 |
} |
| 99 |
|
| 100 |
static void load_postcar_cbfs(struct prog *prog, struct postcar_frame *pcf) |
| 101 |
{ |
| 102 |
struct rmod_stage_load rsl = { |
| 103 |
.cbmem_id = CBMEM_ID_AFTER_CAR, |
| 104 |
.prog = prog, |
| 105 |
}; |
| 106 |
|
| 107 |
if (rmodule_stage_load(&rsl)) |
| 108 |
die_with_post_code(POSTCODE_INVALID_ROM, |
| 109 |
"Failed to load after CAR program.\n"); |
| 110 |
|
| 111 |
|
| 112 |
if (rsl.params == NULL) |
| 113 |
die_with_post_code(POSTCODE_INVALID_ROM, |
| 114 |
"No parameters found in after CAR program.\n"); |
| 115 |
|
| 116 |
finalize_load(rsl.params, (uintptr_t)pcf->mtrr); |
| 117 |
|
| 118 |
stage_cache_add(STAGE_POSTCAR, prog); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
void postcar_enable_tseg_cache(struct postcar_frame *pcf) |
| 129 |
{ |
| 130 |
uintptr_t smm_base; |
| 131 |
size_t smm_size; |
| 132 |
|
| 133 |
smm_region(&smm_base, &smm_size); |
| 134 |
postcar_frame_add_mtrr(pcf, smm_base, smm_size, |
| 135 |
MTRR_TYPE_WRBACK); |
| 136 |
} |
| 137 |
|
| 138 |
static void postcar_cache_invalid(void) |
| 139 |
{ |
| 140 |
printk(BIOS_ERR, "postcar cache invalid.\n"); |
| 141 |
board_reset(); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
static void postcar_flush_cache(void) |
| 149 |
{ |
| 150 |
uintptr_t cbmem_base; |
| 151 |
size_t cbmem_size; |
| 152 |
uintptr_t stage_cache_base; |
| 153 |
size_t stage_cache_size; |
| 154 |
|
| 155 |
if (cbmem_get_region((void **)&cbmem_base, &cbmem_size)) |
| 156 |
die("Could not find cbmem region"); |
| 157 |
prog_segment_loaded(cbmem_base, cbmem_size, SEG_FINAL); |
| 158 |
if (CONFIG(TSEG_STAGE_CACHE) && !romstage_handoff_is_resume()) { |
| 159 |
stage_cache_external_region((void **)&stage_cache_base, &stage_cache_size); |
| 160 |
prog_segment_loaded(stage_cache_base, stage_cache_size, SEG_FINAL); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
static void run_postcar_phase(struct postcar_frame *pcf) |
| 165 |
{ |
| 166 |
struct prog prog = |
| 167 |
PROG_INIT(PROG_POSTCAR, CONFIG_CBFS_PREFIX "/postcar"); |
| 168 |
|
| 169 |
vboot_run_logic(); |
| 170 |
|
| 171 |
if (resume_from_stage_cache()) { |
| 172 |
stage_cache_load_stage(STAGE_POSTCAR, &prog); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
finalize_load(prog.arg, (uintptr_t)pcf->mtrr); |
| 177 |
|
| 178 |
if (prog_entry(&prog) == NULL) |
| 179 |
postcar_cache_invalid(); |
| 180 |
} else |
| 181 |
load_postcar_cbfs(&prog, pcf); |
| 182 |
|
| 183 |
|
| 184 |
timestamp_add_now(TS_ROMSTAGE_END); |
| 185 |
|
| 186 |
console_time_report(); |
| 187 |
|
| 188 |
postcar_flush_cache(); |
| 189 |
|
| 190 |
prog_set_arg(&prog, (void *)cbmem_top()); |
| 191 |
|
| 192 |
prog_run(&prog); |
| 193 |
} |