2 * Read a coreboot rmodule and execute it.
3 * The rmodule_header struct is from coreboot.
5 * Copyright (c) 2016 Google, Inc
7 * SPDX-License-Identifier: GPL-2.0
12 #include <asm/arch/pei_data.h>
14 #define RMODULE_MAGIC 0xf8fe
15 #define RMODULE_VERSION_1 1
18 * All fields with '_offset' in the name are byte offsets into the flat blob.
19 * The linker and the linker script takes are of assigning the values.
21 struct rmodule_header {
25 /* The payload represents the program's loadable code and data */
26 uint32_t payload_begin_offset;
27 uint32_t payload_end_offset;
28 /* Begin and of relocation information about the program module */
29 uint32_t relocations_begin_offset;
30 uint32_t relocations_end_offset;
32 * The starting address of the linked program. This address is vital
33 * for determining relocation offsets as the relocation info and other
34 * symbols (bss, entry point) need this value as a basis to calculate
37 uint32_t module_link_start_address;
39 * The module_program_size is the size of memory used while running
40 * the program. The program is assumed to consume a contiguous amount
43 uint32_t module_program_size;
44 /* This is program's execution entry point */
45 uint32_t module_entry_point;
47 * Optional parameter structure that can be used to pass data into
50 uint32_t parameters_begin;
51 uint32_t parameters_end;
52 /* BSS section information so the loader can clear the bss */
55 /* Add some room for growth */
59 int cpu_run_reference_code(void)
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;
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");
75 if (hdr->module_link_start_address != 0) {
76 debug("Link start address must be 0\n");
79 if (hdr->module_entry_point != 0) {
80 debug("Entry point must be 0\n");
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;
89 src = (char *)hdr + hdr->payload_begin_offset;
90 dest = (char *)CONFIG_X86_REFCODE_RUN_ADDR;
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);
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);
100 func = (asmlinkage int (*)(void *))dest;
101 debug("Running reference code at %p\n", func);
103 print_buffer(CONFIG_X86_REFCODE_RUN_ADDR, (void *)func, 1, 0x40, 0);
105 ret = func(pei_data);
107 debug("Reference code returned %d\n", ret);
110 debug("Refereence code completed\n");