1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Read a coreboot rmodule and execute it.
4 * The rmodule_header struct is from coreboot.
5 *
6 * Copyright (c) 2016 Google, Inc
7 */
8
9#include <common.h>
10#include <display_options.h>
11#include <errno.h>
12#include <init.h>
13#include <log.h>
14#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
61/**
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 *
69 * Return: 0 on success, -ve on failure
70 */
71static int cpu_run_reference_code(void)
72{
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
81	hdr = (struct rmodule_header *)CFG_X86_REFCODE_ADDR;
82	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;
102	dest = (char *)CFG_X86_REFCODE_RUN_ADDR;
103
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
115	print_buffer(CFG_X86_REFCODE_RUN_ADDR, (void *)func, 1, 0x40, 0);
116#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}
126
127int arch_early_init_r(void)
128{
129	return cpu_run_reference_code();
130}
131