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 | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | struct rmodule; |
| 35 | |
| 36 | /* Public API for loading rmdoules. */ |
| 37 | int rmodule_parse(void *ptr, struct rmodule *m); |
| 38 | void *rmodule_parameters(const struct rmodule *m); |
| 39 | void *rmodule_entry(const struct rmodule *m); |
| 40 | int rmodule_entry_offset(const struct rmodule *m); |
| 41 | int rmodule_memory_size(const struct rmodule *m); |
| 42 | int rmodule_load(void *loc, struct rmodule *m); |
| 43 | int rmodule_load_alignment(const struct rmodule *m); |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 44 | /* Returns the an aligned pointer that reflects a region used below addr |
| 45 | * based on the rmodule_size. i.e. the returned pointer up to addr is memory |
| 46 | * that may be utilized by the rmodule. program_start and rmodule_start |
| 47 | * are pointers updated to reflect where the rmodule program starts and where |
| 48 | * the rmodule (including header) should be placed respectively. */ |
| 49 | void *rmodule_find_region_below(void *addr, size_t rmodule_size, |
| 50 | void **program_start, void **rmodule_start); |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 51 | |
| 52 | #define FIELD_ENTRY(x_) ((u32)&x_) |
| 53 | #define RMODULE_HEADER(entry_, type_) \ |
| 54 | { \ |
| 55 | .magic = RMODULE_MAGIC, \ |
| 56 | .version = RMODULE_VERSION_1, \ |
| 57 | .type = type_, \ |
| 58 | .payload_begin_offset = FIELD_ENTRY(_payload_begin_offset), \ |
| 59 | .payload_end_offset = FIELD_ENTRY(_payload_end_offset), \ |
| 60 | .relocations_begin_offset = \ |
| 61 | FIELD_ENTRY(_relocations_begin_offset), \ |
| 62 | .relocations_end_offset = \ |
| 63 | FIELD_ENTRY(_relocations_end_offset), \ |
| 64 | .module_link_start_address = \ |
| 65 | FIELD_ENTRY(_module_link_start_addr), \ |
| 66 | .module_program_size = FIELD_ENTRY(_module_program_size), \ |
| 67 | .module_entry_point = FIELD_ENTRY(entry_), \ |
| 68 | .parameters_begin = FIELD_ENTRY(_module_params_begin), \ |
| 69 | .parameters_end = FIELD_ENTRY(_module_params_end), \ |
Aaron Durbin | f7c6d48 | 2013-02-06 15:47:31 -0600 | [diff] [blame] | 70 | .bss_begin = FIELD_ENTRY(_bss), \ |
| 71 | .bss_end = FIELD_ENTRY(_ebss), \ |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | #define DEFINE_RMODULE_HEADER(name_, entry_, type_) \ |
| 75 | struct rmodule_header name_ \ |
| 76 | __attribute__ ((section (".module_header"))) = \ |
| 77 | RMODULE_HEADER(entry_, type_) |
| 78 | |
| 79 | |
| 80 | /* Private data structures below should not be used directly. */ |
| 81 | |
| 82 | /* All fields with '_offset' in the name are byte offsets into the flat blob. |
| 83 | * The linker and the linker script takes are of assigning the values. */ |
| 84 | struct rmodule_header { |
| 85 | u16 magic; |
| 86 | u8 version; |
| 87 | u8 type; |
| 88 | /* The payload represents the program's loadable code and data. */ |
| 89 | u32 payload_begin_offset; |
| 90 | u32 payload_end_offset; |
| 91 | /* Begin and of relocation information about the program module. */ |
| 92 | u32 relocations_begin_offset; |
| 93 | u32 relocations_end_offset; |
| 94 | /* The starting address of the linked program. This address is vital |
| 95 | * for determining relocation offsets as the reloction info and other |
| 96 | * symbols (bss, entry point) need this value as a basis to calculate |
| 97 | * the offsets. |
| 98 | */ |
| 99 | u32 module_link_start_address; |
| 100 | /* The module_program_size is the size of memory used while running |
| 101 | * the program. The program is assumed to consume a contiguos amount |
| 102 | * of memory. */ |
| 103 | u32 module_program_size; |
| 104 | /* This is program's execution entry point. */ |
| 105 | u32 module_entry_point; |
| 106 | /* Optional paramter structure that can be used to pass data into |
| 107 | * the module. */ |
| 108 | u32 parameters_begin; |
| 109 | u32 parameters_end; |
| 110 | /* BSS section information so the loader can clear the bss. */ |
| 111 | u32 bss_begin; |
| 112 | u32 bss_end; |
Aaron Durbin | 3bf0ce7 | 2013-02-06 12:47:26 -0600 | [diff] [blame] | 113 | /* Add some room for growth. */ |
| 114 | u32 padding[4]; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 115 | } __attribute__ ((packed)); |
| 116 | |
| 117 | struct rmodule { |
| 118 | void *location; |
| 119 | struct rmodule_header *header; |
| 120 | const void *payload; |
| 121 | int payload_size; |
| 122 | void *relocations; |
| 123 | }; |
| 124 | |
| 125 | /* These are the symbols assumed that every module contains. The linker script |
| 126 | * provides these symbols. */ |
| 127 | extern char _relocations_begin_offset[]; |
| 128 | extern char _relocations_end_offset[]; |
| 129 | extern char _payload_end_offset[]; |
| 130 | extern char _payload_begin_offset[]; |
Aaron Durbin | f7c6d48 | 2013-02-06 15:47:31 -0600 | [diff] [blame] | 131 | extern char _bss[]; |
| 132 | extern char _ebss[]; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 133 | extern char _module_program_size[]; |
| 134 | extern char _module_link_start_addr[]; |
| 135 | extern char _module_params_begin[]; |
| 136 | extern char _module_params_end[]; |
| 137 | |
| 138 | #endif /* RMODULE_H */ |