1
2
3
4
5
6
7
8
9#include <common.h>
10#include <errno.h>
11#include <asm/arch/pei_data.h>
12
13#define RMODULE_MAGIC 0xf8fe
14#define RMODULE_VERSION_1 1
15
16
17
18
19
20struct rmodule_header {
21 uint16_t magic;
22 uint8_t version;
23 uint8_t type;
24
25 uint32_t payload_begin_offset;
26 uint32_t payload_end_offset;
27
28 uint32_t relocations_begin_offset;
29 uint32_t relocations_end_offset;
30
31
32
33
34
35
36 uint32_t module_link_start_address;
37
38
39
40
41
42 uint32_t module_program_size;
43
44 uint32_t module_entry_point;
45
46
47
48
49 uint32_t parameters_begin;
50 uint32_t parameters_end;
51
52 uint32_t bss_begin;
53 uint32_t bss_end;
54
55 uint32_t padding[4];
56} __packed;
57
58
59
60
61
62
63
64
65
66
67
68static int cpu_run_reference_code(void)
69{
70 struct pei_data _pei_data __aligned(8);
71 struct pei_data *pei_data = &_pei_data;
72 asmlinkage int (*func)(void *);
73 struct rmodule_header *hdr;
74 char *src, *dest;
75 int ret, dummy;
76 int size;
77
78 hdr = (struct rmodule_header *)CONFIG_X86_REFCODE_ADDR;
79 debug("Extracting code from rmodule at %p\n", hdr);
80 if (hdr->magic != RMODULE_MAGIC) {
81 debug("Invalid rmodule magic\n");
82 return -EINVAL;
83 }
84 if (hdr->module_link_start_address != 0) {
85 debug("Link start address must be 0\n");
86 return -EPERM;
87 }
88 if (hdr->module_entry_point != 0) {
89 debug("Entry point must be 0\n");
90 return -EPERM;
91 }
92
93 memset(pei_data, '\0', sizeof(struct pei_data));
94 broadwell_fill_pei_data(pei_data);
95 mainboard_fill_pei_data(pei_data);
96 pei_data->saved_data = (void *)&dummy;
97
98 src = (char *)hdr + hdr->payload_begin_offset;
99 dest = (char *)CONFIG_X86_REFCODE_RUN_ADDR;
100
101 size = hdr->payload_end_offset - hdr->payload_begin_offset;
102 debug("Copying refcode from %p to %p, size %x\n", src, dest, size);
103 memcpy(dest, src, size);
104
105 size = hdr->bss_end - hdr->bss_begin;
106 debug("Zeroing BSS at %p, size %x\n", dest + hdr->bss_begin, size);
107 memset(dest + hdr->bss_begin, '\0', size);
108
109 func = (asmlinkage int (*)(void *))dest;
110 debug("Running reference code at %p\n", func);
111#ifdef DEBUG
112 print_buffer(CONFIG_X86_REFCODE_RUN_ADDR, (void *)func, 1, 0x40, 0);
113#endif
114 ret = func(pei_data);
115 if (ret != 0) {
116 debug("Reference code returned %d\n", ret);
117 return -EL2HLT;
118 }
119 debug("Refereence code completed\n");
120
121 return 0;
122}
123
124int arch_early_init_r(void)
125{
126 return cpu_run_reference_code();
127}
128