Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 ChromeOS Authors |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | #ifndef RMODULE_H |
| 20 | #define RMODULE_H |
| 21 | |
| 22 | #include <stdint.h> |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 23 | #include <stddef.h> |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 24 | |
| 25 | #define RMODULE_MAGIC 0xf8fe |
| 26 | #define RMODULE_VERSION_1 1 |
| 27 | |
| 28 | enum { |
| 29 | RMODULE_TYPE_SMM, |
Aaron Durbin | 9be4c47 | 2013-01-12 00:41:44 -0600 | [diff] [blame] | 30 | RMODULE_TYPE_SIPI_VECTOR, |
Aaron Durbin | f7c6d48 | 2013-02-06 15:47:31 -0600 | [diff] [blame] | 31 | RMODULE_TYPE_STAGE, |
Aaron Durbin | c065089 | 2013-03-01 17:10:28 -0600 | [diff] [blame] | 32 | RMODULE_TYPE_VBOOT, |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | struct rmodule; |
| 36 | |
| 37 | /* Public API for loading rmdoules. */ |
| 38 | int rmodule_parse(void *ptr, struct rmodule *m); |
| 39 | void *rmodule_parameters(const struct rmodule *m); |
| 40 | void *rmodule_entry(const struct rmodule *m); |
| 41 | int rmodule_entry_offset(const struct rmodule *m); |
| 42 | int rmodule_memory_size(const struct rmodule *m); |
| 43 | int rmodule_load(void *loc, struct rmodule *m); |
| 44 | int rmodule_load_alignment(const struct rmodule *m); |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 45 | /* rmodule_calc_region() calculates the region size, offset to place an |
| 46 | * rmodule in memory, and load address offset based off of a region allocator |
| 47 | * with an alignment of region_alignment. This function helps place an rmodule |
| 48 | * in the same location in ram it will run from. The offset to place the |
| 49 | * rmodule into the region allocated of size region_size is returned. The |
| 50 | * load_offset is the address to load and relocate the rmodule. |
| 51 | * region_alignment must be a power of 2. */ |
| 52 | int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size, |
| 53 | size_t *region_size, int *load_offset); |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 54 | |
| 55 | #define FIELD_ENTRY(x_) ((u32)&x_) |
| 56 | #define RMODULE_HEADER(entry_, type_) \ |
| 57 | { \ |
| 58 | .magic = RMODULE_MAGIC, \ |
| 59 | .version = RMODULE_VERSION_1, \ |
| 60 | .type = type_, \ |
| 61 | .payload_begin_offset = FIELD_ENTRY(_payload_begin_offset), \ |
| 62 | .payload_end_offset = FIELD_ENTRY(_payload_end_offset), \ |
| 63 | .relocations_begin_offset = \ |
| 64 | FIELD_ENTRY(_relocations_begin_offset), \ |
| 65 | .relocations_end_offset = \ |
| 66 | FIELD_ENTRY(_relocations_end_offset), \ |
| 67 | .module_link_start_address = \ |
| 68 | FIELD_ENTRY(_module_link_start_addr), \ |
| 69 | .module_program_size = FIELD_ENTRY(_module_program_size), \ |
| 70 | .module_entry_point = FIELD_ENTRY(entry_), \ |
| 71 | .parameters_begin = FIELD_ENTRY(_module_params_begin), \ |
| 72 | .parameters_end = FIELD_ENTRY(_module_params_end), \ |
Aaron Durbin | f7c6d48 | 2013-02-06 15:47:31 -0600 | [diff] [blame] | 73 | .bss_begin = FIELD_ENTRY(_bss), \ |
| 74 | .bss_end = FIELD_ENTRY(_ebss), \ |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | #define DEFINE_RMODULE_HEADER(name_, entry_, type_) \ |
| 78 | struct rmodule_header name_ \ |
| 79 | __attribute__ ((section (".module_header"))) = \ |
| 80 | RMODULE_HEADER(entry_, type_) |
| 81 | |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame^] | 82 | /* Support for loading rmodule stages. This API is only available when |
| 83 | * using dynamic cbmem because it uses the dynamic cbmem API to obtain |
| 84 | * the backing store region for the stage. */ |
| 85 | #if CONFIG_DYNAMIC_CBMEM |
| 86 | struct cbfs_stage; |
| 87 | struct cbmem_entry; |
| 88 | |
| 89 | struct rmod_stage_load { |
| 90 | /* Inputs */ |
| 91 | uint32_t cbmem_id; |
| 92 | const char *name; |
| 93 | /* Outputs */ |
| 94 | const struct cbmem_entry *cbmem_entry; |
| 95 | void *entry; |
| 96 | }; |
| 97 | |
| 98 | /* Both of the following functions return 0 on success, -1 on error. */ |
| 99 | int rmodule_stage_load(struct rmod_stage_load *rsl, struct cbfs_stage *stage); |
| 100 | int rmodule_stage_load_from_cbfs(struct rmod_stage_load *rsl); |
| 101 | #endif |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 102 | |
| 103 | /* Private data structures below should not be used directly. */ |
| 104 | |
| 105 | /* All fields with '_offset' in the name are byte offsets into the flat blob. |
| 106 | * The linker and the linker script takes are of assigning the values. */ |
| 107 | struct rmodule_header { |
| 108 | u16 magic; |
| 109 | u8 version; |
| 110 | u8 type; |
| 111 | /* The payload represents the program's loadable code and data. */ |
| 112 | u32 payload_begin_offset; |
| 113 | u32 payload_end_offset; |
| 114 | /* Begin and of relocation information about the program module. */ |
| 115 | u32 relocations_begin_offset; |
| 116 | u32 relocations_end_offset; |
| 117 | /* The starting address of the linked program. This address is vital |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 118 | * for determining relocation offsets as the relocation info and other |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 119 | * symbols (bss, entry point) need this value as a basis to calculate |
| 120 | * the offsets. |
| 121 | */ |
| 122 | u32 module_link_start_address; |
| 123 | /* The module_program_size is the size of memory used while running |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 124 | * the program. The program is assumed to consume a contiguous amount |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 125 | * of memory. */ |
| 126 | u32 module_program_size; |
| 127 | /* This is program's execution entry point. */ |
| 128 | u32 module_entry_point; |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 129 | /* Optional parameter structure that can be used to pass data into |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 130 | * the module. */ |
| 131 | u32 parameters_begin; |
| 132 | u32 parameters_end; |
| 133 | /* BSS section information so the loader can clear the bss. */ |
| 134 | u32 bss_begin; |
| 135 | u32 bss_end; |
Aaron Durbin | 3bf0ce7 | 2013-02-06 12:47:26 -0600 | [diff] [blame] | 136 | /* Add some room for growth. */ |
| 137 | u32 padding[4]; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 138 | } __attribute__ ((packed)); |
| 139 | |
| 140 | struct rmodule { |
| 141 | void *location; |
| 142 | struct rmodule_header *header; |
| 143 | const void *payload; |
| 144 | int payload_size; |
| 145 | void *relocations; |
| 146 | }; |
| 147 | |
| 148 | /* These are the symbols assumed that every module contains. The linker script |
| 149 | * provides these symbols. */ |
| 150 | extern char _relocations_begin_offset[]; |
| 151 | extern char _relocations_end_offset[]; |
| 152 | extern char _payload_end_offset[]; |
| 153 | extern char _payload_begin_offset[]; |
Aaron Durbin | f7c6d48 | 2013-02-06 15:47:31 -0600 | [diff] [blame] | 154 | extern char _bss[]; |
| 155 | extern char _ebss[]; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 156 | extern char _module_program_size[]; |
| 157 | extern char _module_link_start_addr[]; |
| 158 | extern char _module_params_begin[]; |
| 159 | extern char _module_params_end[]; |
| 160 | |
| 161 | #endif /* RMODULE_H */ |