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