1e7994858SSimon Glass /*
2e7994858SSimon Glass * Read a coreboot rmodule and execute it.
3e7994858SSimon Glass * The rmodule_header struct is from coreboot.
4e7994858SSimon Glass *
5e7994858SSimon Glass * Copyright (c) 2016 Google, Inc
6e7994858SSimon Glass *
7e7994858SSimon Glass * SPDX-License-Identifier: GPL-2.0
8e7994858SSimon Glass */
9e7994858SSimon Glass
10e7994858SSimon Glass #include <common.h>
11e7994858SSimon Glass #include <errno.h>
12e7994858SSimon Glass #include <asm/arch/pei_data.h>
13e7994858SSimon Glass
14e7994858SSimon Glass #define RMODULE_MAGIC 0xf8fe
15e7994858SSimon Glass #define RMODULE_VERSION_1 1
16e7994858SSimon Glass
17e7994858SSimon Glass /*
18e7994858SSimon Glass * All fields with '_offset' in the name are byte offsets into the flat blob.
19e7994858SSimon Glass * The linker and the linker script takes are of assigning the values.
20e7994858SSimon Glass */
21e7994858SSimon Glass struct rmodule_header {
22e7994858SSimon Glass uint16_t magic;
23e7994858SSimon Glass uint8_t version;
24e7994858SSimon Glass uint8_t type;
25e7994858SSimon Glass /* The payload represents the program's loadable code and data */
26e7994858SSimon Glass uint32_t payload_begin_offset;
27e7994858SSimon Glass uint32_t payload_end_offset;
28e7994858SSimon Glass /* Begin and of relocation information about the program module */
29e7994858SSimon Glass uint32_t relocations_begin_offset;
30e7994858SSimon Glass uint32_t relocations_end_offset;
31e7994858SSimon Glass /*
32e7994858SSimon Glass * The starting address of the linked program. This address is vital
33e7994858SSimon Glass * for determining relocation offsets as the relocation info and other
34e7994858SSimon Glass * symbols (bss, entry point) need this value as a basis to calculate
35e7994858SSimon Glass * the offsets.
36e7994858SSimon Glass */
37e7994858SSimon Glass uint32_t module_link_start_address;
38e7994858SSimon Glass /*
39e7994858SSimon Glass * The module_program_size is the size of memory used while running
40e7994858SSimon Glass * the program. The program is assumed to consume a contiguous amount
41e7994858SSimon Glass * of memory
42e7994858SSimon Glass */
43e7994858SSimon Glass uint32_t module_program_size;
44e7994858SSimon Glass /* This is program's execution entry point */
45e7994858SSimon Glass uint32_t module_entry_point;
46e7994858SSimon Glass /*
47e7994858SSimon Glass * Optional parameter structure that can be used to pass data into
48e7994858SSimon Glass * the module
49e7994858SSimon Glass */
50e7994858SSimon Glass uint32_t parameters_begin;
51e7994858SSimon Glass uint32_t parameters_end;
52e7994858SSimon Glass /* BSS section information so the loader can clear the bss */
53e7994858SSimon Glass uint32_t bss_begin;
54e7994858SSimon Glass uint32_t bss_end;
55e7994858SSimon Glass /* Add some room for growth */
56e7994858SSimon Glass uint32_t padding[4];
57e7994858SSimon Glass } __packed;
58e7994858SSimon Glass
59*5d89b37fSBin Meng /**
60*5d89b37fSBin Meng * cpu_run_reference_code() - Run the platform reference code
61*5d89b37fSBin Meng *
62*5d89b37fSBin Meng * Some platforms require a binary blob to be executed once SDRAM is
63*5d89b37fSBin Meng * available. This is used to set up various platform features, such as the
64*5d89b37fSBin Meng * platform controller hub (PCH). This function should be implemented by the
65*5d89b37fSBin Meng * CPU-specific code.
66*5d89b37fSBin Meng *
67*5d89b37fSBin Meng * @return 0 on success, -ve on failure
68*5d89b37fSBin Meng */
cpu_run_reference_code(void)69*5d89b37fSBin Meng static int cpu_run_reference_code(void)
70e7994858SSimon Glass {
71e7994858SSimon Glass struct pei_data _pei_data __aligned(8);
72e7994858SSimon Glass struct pei_data *pei_data = &_pei_data;
73e7994858SSimon Glass asmlinkage int (*func)(void *);
74e7994858SSimon Glass struct rmodule_header *hdr;
75e7994858SSimon Glass char *src, *dest;
76e7994858SSimon Glass int ret, dummy;
77e7994858SSimon Glass int size;
78e7994858SSimon Glass
79e7994858SSimon Glass hdr = (struct rmodule_header *)CONFIG_X86_REFCODE_ADDR;
80e7994858SSimon Glass debug("Extracting code from rmodule at %p\n", hdr);
81e7994858SSimon Glass if (hdr->magic != RMODULE_MAGIC) {
82e7994858SSimon Glass debug("Invalid rmodule magic\n");
83e7994858SSimon Glass return -EINVAL;
84e7994858SSimon Glass }
85e7994858SSimon Glass if (hdr->module_link_start_address != 0) {
86e7994858SSimon Glass debug("Link start address must be 0\n");
87e7994858SSimon Glass return -EPERM;
88e7994858SSimon Glass }
89e7994858SSimon Glass if (hdr->module_entry_point != 0) {
90e7994858SSimon Glass debug("Entry point must be 0\n");
91e7994858SSimon Glass return -EPERM;
92e7994858SSimon Glass }
93e7994858SSimon Glass
94e7994858SSimon Glass memset(pei_data, '\0', sizeof(struct pei_data));
95e7994858SSimon Glass broadwell_fill_pei_data(pei_data);
96e7994858SSimon Glass mainboard_fill_pei_data(pei_data);
97e7994858SSimon Glass pei_data->saved_data = (void *)&dummy;
98e7994858SSimon Glass
99e7994858SSimon Glass src = (char *)hdr + hdr->payload_begin_offset;
100e7994858SSimon Glass dest = (char *)CONFIG_X86_REFCODE_RUN_ADDR;
101e7994858SSimon Glass
102e7994858SSimon Glass size = hdr->payload_end_offset - hdr->payload_begin_offset;
103e7994858SSimon Glass debug("Copying refcode from %p to %p, size %x\n", src, dest, size);
104e7994858SSimon Glass memcpy(dest, src, size);
105e7994858SSimon Glass
106e7994858SSimon Glass size = hdr->bss_end - hdr->bss_begin;
107e7994858SSimon Glass debug("Zeroing BSS at %p, size %x\n", dest + hdr->bss_begin, size);
108e7994858SSimon Glass memset(dest + hdr->bss_begin, '\0', size);
109e7994858SSimon Glass
110e7994858SSimon Glass func = (asmlinkage int (*)(void *))dest;
111e7994858SSimon Glass debug("Running reference code at %p\n", func);
112e7994858SSimon Glass #ifdef DEBUG
113e7994858SSimon Glass print_buffer(CONFIG_X86_REFCODE_RUN_ADDR, (void *)func, 1, 0x40, 0);
114e7994858SSimon Glass #endif
115e7994858SSimon Glass ret = func(pei_data);
116e7994858SSimon Glass if (ret != 0) {
117e7994858SSimon Glass debug("Reference code returned %d\n", ret);
118e7994858SSimon Glass return -EL2HLT;
119e7994858SSimon Glass }
120e7994858SSimon Glass debug("Refereence code completed\n");
121e7994858SSimon Glass
122e7994858SSimon Glass return 0;
123e7994858SSimon Glass }
124*5d89b37fSBin Meng
arch_early_init_r(void)125*5d89b37fSBin Meng int arch_early_init_r(void)
126*5d89b37fSBin Meng {
127*5d89b37fSBin Meng return cpu_run_reference_code();
128*5d89b37fSBin Meng }
129