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
13 #include <asm/arch/pei_data.h>
15 #define RMODULE_MAGIC 0xf8fe
16 #define RMODULE_VERSION_1 1
19 * All fields with '_offset' in the name are byte offsets into the flat blob.
20 * The linker and the linker script takes are of assigning the values.
22 struct rmodule_header {
26 /* The payload represents the program's loadable code and data */
27 uint32_t payload_begin_offset;
28 uint32_t payload_end_offset;
29 /* Begin and of relocation information about the program module */
30 uint32_t relocations_begin_offset;
31 uint32_t relocations_end_offset;
33 * The starting address of the linked program. This address is vital
34 * for determining relocation offsets as the relocation info and other
35 * symbols (bss, entry point) need this value as a basis to calculate
38 uint32_t module_link_start_address;
40 * The module_program_size is the size of memory used while running
41 * the program. The program is assumed to consume a contiguous amount
44 uint32_t module_program_size;
45 /* This is program's execution entry point */
46 uint32_t module_entry_point;
48 * Optional parameter structure that can be used to pass data into
51 uint32_t parameters_begin;
52 uint32_t parameters_end;
53 /* BSS section information so the loader can clear the bss */
56 /* Add some room for growth */
61 * cpu_run_reference_code() - Run the platform reference code
63 * Some platforms require a binary blob to be executed once SDRAM is
64 * available. This is used to set up various platform features, such as the
65 * platform controller hub (PCH). This function should be implemented by the
68 * Return: 0 on success, -ve on failure
70 static int cpu_run_reference_code(void)
72 struct pei_data _pei_data __aligned(8);
73 struct pei_data *pei_data = &_pei_data;
74 asmlinkage int (*func)(void *);
75 struct rmodule_header *hdr;
80 hdr = (struct rmodule_header *)CONFIG_X86_REFCODE_ADDR;
81 debug("Extracting code from rmodule at %p\n", hdr);
82 if (hdr->magic != RMODULE_MAGIC) {
83 debug("Invalid rmodule magic\n");
86 if (hdr->module_link_start_address != 0) {
87 debug("Link start address must be 0\n");
90 if (hdr->module_entry_point != 0) {
91 debug("Entry point must be 0\n");
95 memset(pei_data, '\0', sizeof(struct pei_data));
96 broadwell_fill_pei_data(pei_data);
97 mainboard_fill_pei_data(pei_data);
98 pei_data->saved_data = (void *)&dummy;
100 src = (char *)hdr + hdr->payload_begin_offset;
101 dest = (char *)CONFIG_X86_REFCODE_RUN_ADDR;
103 size = hdr->payload_end_offset - hdr->payload_begin_offset;
104 debug("Copying refcode from %p to %p, size %x\n", src, dest, size);
105 memcpy(dest, src, size);
107 size = hdr->bss_end - hdr->bss_begin;
108 debug("Zeroing BSS at %p, size %x\n", dest + hdr->bss_begin, size);
109 memset(dest + hdr->bss_begin, '\0', size);
111 func = (asmlinkage int (*)(void *))dest;
112 debug("Running reference code at %p\n", func);
114 print_buffer(CONFIG_X86_REFCODE_RUN_ADDR, (void *)func, 1, 0x40, 0);
116 ret = func(pei_data);
118 debug("Reference code returned %d\n", ret);
121 debug("Refereence code completed\n");
126 int arch_early_init_r(void)
128 return cpu_run_reference_code();