1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Read a coreboot rmodule and execute it.
3*4882a593Smuzhiyun * The rmodule_header struct is from coreboot.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2016 Google, Inc
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <common.h>
11*4882a593Smuzhiyun #include <errno.h>
12*4882a593Smuzhiyun #include <asm/arch/pei_data.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #define RMODULE_MAGIC 0xf8fe
15*4882a593Smuzhiyun #define RMODULE_VERSION_1 1
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /*
18*4882a593Smuzhiyun * All fields with '_offset' in the name are byte offsets into the flat blob.
19*4882a593Smuzhiyun * The linker and the linker script takes are of assigning the values.
20*4882a593Smuzhiyun */
21*4882a593Smuzhiyun struct rmodule_header {
22*4882a593Smuzhiyun uint16_t magic;
23*4882a593Smuzhiyun uint8_t version;
24*4882a593Smuzhiyun uint8_t type;
25*4882a593Smuzhiyun /* The payload represents the program's loadable code and data */
26*4882a593Smuzhiyun uint32_t payload_begin_offset;
27*4882a593Smuzhiyun uint32_t payload_end_offset;
28*4882a593Smuzhiyun /* Begin and of relocation information about the program module */
29*4882a593Smuzhiyun uint32_t relocations_begin_offset;
30*4882a593Smuzhiyun uint32_t relocations_end_offset;
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun * The starting address of the linked program. This address is vital
33*4882a593Smuzhiyun * for determining relocation offsets as the relocation info and other
34*4882a593Smuzhiyun * symbols (bss, entry point) need this value as a basis to calculate
35*4882a593Smuzhiyun * the offsets.
36*4882a593Smuzhiyun */
37*4882a593Smuzhiyun uint32_t module_link_start_address;
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun * The module_program_size is the size of memory used while running
40*4882a593Smuzhiyun * the program. The program is assumed to consume a contiguous amount
41*4882a593Smuzhiyun * of memory
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun uint32_t module_program_size;
44*4882a593Smuzhiyun /* This is program's execution entry point */
45*4882a593Smuzhiyun uint32_t module_entry_point;
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun * Optional parameter structure that can be used to pass data into
48*4882a593Smuzhiyun * the module
49*4882a593Smuzhiyun */
50*4882a593Smuzhiyun uint32_t parameters_begin;
51*4882a593Smuzhiyun uint32_t parameters_end;
52*4882a593Smuzhiyun /* BSS section information so the loader can clear the bss */
53*4882a593Smuzhiyun uint32_t bss_begin;
54*4882a593Smuzhiyun uint32_t bss_end;
55*4882a593Smuzhiyun /* Add some room for growth */
56*4882a593Smuzhiyun uint32_t padding[4];
57*4882a593Smuzhiyun } __packed;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /**
60*4882a593Smuzhiyun * cpu_run_reference_code() - Run the platform reference code
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * Some platforms require a binary blob to be executed once SDRAM is
63*4882a593Smuzhiyun * available. This is used to set up various platform features, such as the
64*4882a593Smuzhiyun * platform controller hub (PCH). This function should be implemented by the
65*4882a593Smuzhiyun * CPU-specific code.
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * @return 0 on success, -ve on failure
68*4882a593Smuzhiyun */
cpu_run_reference_code(void)69*4882a593Smuzhiyun static int cpu_run_reference_code(void)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun struct pei_data _pei_data __aligned(8);
72*4882a593Smuzhiyun struct pei_data *pei_data = &_pei_data;
73*4882a593Smuzhiyun asmlinkage int (*func)(void *);
74*4882a593Smuzhiyun struct rmodule_header *hdr;
75*4882a593Smuzhiyun char *src, *dest;
76*4882a593Smuzhiyun int ret, dummy;
77*4882a593Smuzhiyun int size;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun hdr = (struct rmodule_header *)CONFIG_X86_REFCODE_ADDR;
80*4882a593Smuzhiyun debug("Extracting code from rmodule at %p\n", hdr);
81*4882a593Smuzhiyun if (hdr->magic != RMODULE_MAGIC) {
82*4882a593Smuzhiyun debug("Invalid rmodule magic\n");
83*4882a593Smuzhiyun return -EINVAL;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun if (hdr->module_link_start_address != 0) {
86*4882a593Smuzhiyun debug("Link start address must be 0\n");
87*4882a593Smuzhiyun return -EPERM;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun if (hdr->module_entry_point != 0) {
90*4882a593Smuzhiyun debug("Entry point must be 0\n");
91*4882a593Smuzhiyun return -EPERM;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun memset(pei_data, '\0', sizeof(struct pei_data));
95*4882a593Smuzhiyun broadwell_fill_pei_data(pei_data);
96*4882a593Smuzhiyun mainboard_fill_pei_data(pei_data);
97*4882a593Smuzhiyun pei_data->saved_data = (void *)&dummy;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun src = (char *)hdr + hdr->payload_begin_offset;
100*4882a593Smuzhiyun dest = (char *)CONFIG_X86_REFCODE_RUN_ADDR;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun size = hdr->payload_end_offset - hdr->payload_begin_offset;
103*4882a593Smuzhiyun debug("Copying refcode from %p to %p, size %x\n", src, dest, size);
104*4882a593Smuzhiyun memcpy(dest, src, size);
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun size = hdr->bss_end - hdr->bss_begin;
107*4882a593Smuzhiyun debug("Zeroing BSS at %p, size %x\n", dest + hdr->bss_begin, size);
108*4882a593Smuzhiyun memset(dest + hdr->bss_begin, '\0', size);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun func = (asmlinkage int (*)(void *))dest;
111*4882a593Smuzhiyun debug("Running reference code at %p\n", func);
112*4882a593Smuzhiyun #ifdef DEBUG
113*4882a593Smuzhiyun print_buffer(CONFIG_X86_REFCODE_RUN_ADDR, (void *)func, 1, 0x40, 0);
114*4882a593Smuzhiyun #endif
115*4882a593Smuzhiyun ret = func(pei_data);
116*4882a593Smuzhiyun if (ret != 0) {
117*4882a593Smuzhiyun debug("Reference code returned %d\n", ret);
118*4882a593Smuzhiyun return -EL2HLT;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun debug("Refereence code completed\n");
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun return 0;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
arch_early_init_r(void)125*4882a593Smuzhiyun int arch_early_init_r(void)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun return cpu_run_reference_code();
128*4882a593Smuzhiyun }
129