1 // SPDX-License-Identifier: GPL-2.0
3 * Read a coreboot rmodule and execute it.
4 * The rmodule_header struct is from coreboot.
6 * Copyright (c) 2016 Google, Inc
11 #include <asm/arch/pei_data.h>
13 #define RMODULE_MAGIC 0xf8fe
14 #define RMODULE_VERSION_1 1
17 * All fields with '_offset' in the name are byte offsets into the flat blob.
18 * The linker and the linker script takes are of assigning the values.
20 struct rmodule_header {
24 /* The payload represents the program's loadable code and data */
25 uint32_t payload_begin_offset;
26 uint32_t payload_end_offset;
27 /* Begin and of relocation information about the program module */
28 uint32_t relocations_begin_offset;
29 uint32_t relocations_end_offset;
31 * The starting address of the linked program. This address is vital
32 * for determining relocation offsets as the relocation info and other
33 * symbols (bss, entry point) need this value as a basis to calculate
36 uint32_t module_link_start_address;
38 * The module_program_size is the size of memory used while running
39 * the program. The program is assumed to consume a contiguous amount
42 uint32_t module_program_size;
43 /* This is program's execution entry point */
44 uint32_t module_entry_point;
46 * Optional parameter structure that can be used to pass data into
49 uint32_t parameters_begin;
50 uint32_t parameters_end;
51 /* BSS section information so the loader can clear the bss */
54 /* Add some room for growth */
59 * cpu_run_reference_code() - Run the platform reference code
61 * Some platforms require a binary blob to be executed once SDRAM is
62 * available. This is used to set up various platform features, such as the
63 * platform controller hub (PCH). This function should be implemented by the
66 * @return 0 on success, -ve on failure
68 static int cpu_run_reference_code(void)
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;
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");
84 if (hdr->module_link_start_address != 0) {
85 debug("Link start address must be 0\n");
88 if (hdr->module_entry_point != 0) {
89 debug("Entry point must be 0\n");
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;
98 src = (char *)hdr + hdr->payload_begin_offset;
99 dest = (char *)CONFIG_X86_REFCODE_RUN_ADDR;
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);
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);
109 func = (asmlinkage int (*)(void *))dest;
110 debug("Running reference code at %p\n", func);
112 print_buffer(CONFIG_X86_REFCODE_RUN_ADDR, (void *)func, 1, 0x40, 0);
114 ret = func(pei_data);
116 debug("Reference code returned %d\n", ret);
119 debug("Refereence code completed\n");
124 int arch_early_init_r(void)
126 return cpu_run_reference_code();