Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Glass | cb9a352 | 2016-03-11 22:07:24 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Read a coreboot rmodule and execute it. |
| 4 | * The rmodule_header struct is from coreboot. |
| 5 | * |
| 6 | * Copyright (c) 2016 Google, Inc |
Simon Glass | cb9a352 | 2016-03-11 22:07:24 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 9 | #include <common.h> |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 10 | #include <display_options.h> |
Simon Glass | cb9a352 | 2016-03-11 22:07:24 -0700 | [diff] [blame] | 11 | #include <errno.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 12 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | cb9a352 | 2016-03-11 22:07:24 -0700 | [diff] [blame] | 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 | */ |
| 23 | struct 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 Meng | 6f40e7c | 2017-07-30 06:23:13 -0700 | [diff] [blame] | 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 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 69 | * Return: 0 on success, -ve on failure |
Bin Meng | 6f40e7c | 2017-07-30 06:23:13 -0700 | [diff] [blame] | 70 | */ |
| 71 | static int cpu_run_reference_code(void) |
Simon Glass | cb9a352 | 2016-03-11 22:07:24 -0700 | [diff] [blame] | 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 | |
Tom Rini | 1aaf3e6 | 2022-12-04 10:14:08 -0500 | [diff] [blame] | 81 | hdr = (struct rmodule_header *)CFG_X86_REFCODE_ADDR; |
Simon Glass | cb9a352 | 2016-03-11 22:07:24 -0700 | [diff] [blame] | 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; |
Tom Rini | 5dde409 | 2022-12-04 10:14:09 -0500 | [diff] [blame] | 102 | dest = (char *)CFG_X86_REFCODE_RUN_ADDR; |
Simon Glass | cb9a352 | 2016-03-11 22:07:24 -0700 | [diff] [blame] | 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 |
Tom Rini | 5dde409 | 2022-12-04 10:14:09 -0500 | [diff] [blame] | 115 | print_buffer(CFG_X86_REFCODE_RUN_ADDR, (void *)func, 1, 0x40, 0); |
Simon Glass | cb9a352 | 2016-03-11 22:07:24 -0700 | [diff] [blame] | 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 | } |
Bin Meng | 6f40e7c | 2017-07-30 06:23:13 -0700 | [diff] [blame] | 126 | |
| 127 | int arch_early_init_r(void) |
| 128 | { |
| 129 | return cpu_run_reference_code(); |
| 130 | } |