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