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 | #include <stdint.h> |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 20 | #include <stdlib.h> |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <console/console.h> |
| 23 | #include <rmodule.h> |
| 24 | |
| 25 | /* Change this define to get more verbose debugging for module loading. */ |
| 26 | #define PK_ADJ_LEVEL BIOS_NEVER |
| 27 | |
| 28 | #if CONFIG_ARCH_X86 |
| 29 | /* |
| 30 | * On X86, the only relocations currently allowed are R_386_RELATIVE which |
| 31 | * have '0' for the symbol info in the relocation metadata (in r_info). |
| 32 | * The reason is that the module is fully linked and just has the relocations' |
| 33 | * locations. |
| 34 | */ |
| 35 | typedef struct { |
| 36 | u32 r_offset; |
| 37 | u32 r_info; |
| 38 | } Elf32_Rel; |
| 39 | |
| 40 | #define R_386_RELATIVE 8 |
| 41 | |
| 42 | #define RELOCTION_ENTRY_SIZE sizeof(Elf32_Rel) |
| 43 | static inline int rmodule_reloc_offset(const void *reloc) |
| 44 | { |
| 45 | const Elf32_Rel *rel = reloc; |
| 46 | return rel->r_offset; |
| 47 | } |
| 48 | |
| 49 | static inline int rmodule_reloc_valid(const void *reloc) |
| 50 | { |
| 51 | const Elf32_Rel *rel = reloc; |
| 52 | return (rel->r_info == R_386_RELATIVE); |
| 53 | } |
| 54 | |
| 55 | static inline void *remodule_next_reloc(const void *reloc) |
| 56 | { |
| 57 | const Elf32_Rel *rel = reloc; |
| 58 | rel++; |
| 59 | return (void *)rel; |
| 60 | } |
| 61 | |
| 62 | #else |
| 63 | #error Arch needs to add relocation information support for RMODULE |
| 64 | #endif |
| 65 | |
| 66 | static inline int rmodule_is_loaded(const struct rmodule *module) |
| 67 | { |
| 68 | return module->location != NULL; |
| 69 | } |
| 70 | |
| 71 | /* Calculate a loaded program address based on the blob address. */ |
| 72 | static inline void *rmodule_load_addr(const struct rmodule *module, |
| 73 | u32 blob_addr) |
| 74 | { |
| 75 | char *loc = module->location; |
| 76 | return &loc[blob_addr - module->header->module_link_start_address]; |
| 77 | } |
| 78 | |
| 79 | /* Initialize a rmodule structure based on raw data. */ |
| 80 | int rmodule_parse(void *ptr, struct rmodule *module) |
| 81 | { |
| 82 | char *base; |
| 83 | struct rmodule_header *rhdr; |
| 84 | |
| 85 | base = ptr; |
| 86 | rhdr = ptr; |
| 87 | |
| 88 | if (rhdr == NULL) |
| 89 | return -1; |
| 90 | |
| 91 | /* Sanity check the raw data. */ |
| 92 | if (rhdr->magic != RMODULE_MAGIC) |
| 93 | return -1; |
| 94 | if (rhdr->version != RMODULE_VERSION_1) |
| 95 | return -1; |
| 96 | |
| 97 | /* Indicate the module hasn't been loaded yet. */ |
| 98 | module->location = NULL; |
| 99 | |
| 100 | /* The rmodule only needs a reference to the reloc_header. */ |
| 101 | module->header = rhdr; |
| 102 | |
| 103 | /* The payload lives after the header. */ |
| 104 | module->payload = &base[rhdr->payload_begin_offset]; |
| 105 | module->payload_size = rhdr->payload_end_offset - |
| 106 | rhdr->payload_begin_offset; |
| 107 | module->relocations = &base[rhdr->relocations_begin_offset]; |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | int rmodule_memory_size(const struct rmodule *module) |
| 113 | { |
| 114 | return module->header->module_program_size; |
| 115 | } |
| 116 | |
| 117 | void *rmodule_parameters(const struct rmodule *module) |
| 118 | { |
| 119 | if (!rmodule_is_loaded(module)) |
| 120 | return NULL; |
| 121 | |
| 122 | /* Indicate if there are no parameters. */ |
| 123 | if (module->header->parameters_begin == module->header->parameters_end) |
| 124 | return NULL; |
| 125 | |
| 126 | return rmodule_load_addr(module, module->header->parameters_begin); |
| 127 | } |
| 128 | |
| 129 | int rmodule_entry_offset(const struct rmodule *module) |
| 130 | { |
| 131 | return module->header->module_entry_point - |
| 132 | module->header->module_link_start_address; |
| 133 | } |
| 134 | |
| 135 | void *rmodule_entry(const struct rmodule *module) |
| 136 | { |
| 137 | if (!rmodule_is_loaded(module)) |
| 138 | return NULL; |
| 139 | |
| 140 | return rmodule_load_addr(module, module->header->module_entry_point); |
| 141 | } |
| 142 | |
| 143 | static void rmodule_clear_bss(struct rmodule *module) |
| 144 | { |
| 145 | char *begin; |
| 146 | int size; |
| 147 | |
| 148 | begin = rmodule_load_addr(module, module->header->bss_begin); |
| 149 | size = module->header->bss_end - module->header->bss_begin; |
| 150 | memset(begin, 0, size); |
| 151 | } |
| 152 | |
| 153 | static inline int rmodule_number_relocations(const struct rmodule *module) |
| 154 | { |
| 155 | int r; |
| 156 | |
| 157 | r = module->header->relocations_end_offset; |
| 158 | r -= module->header->relocations_begin_offset; |
| 159 | r /= RELOCTION_ENTRY_SIZE; |
| 160 | return r; |
| 161 | } |
| 162 | |
| 163 | static void rmodule_copy_payload(const struct rmodule *module) |
| 164 | { |
| 165 | printk(BIOS_DEBUG, "Loading module at %p with entry %p. " |
| 166 | "filesize: 0x%x memsize: 0x%x\n", |
| 167 | module->location, rmodule_entry(module), |
| 168 | module->payload_size, rmodule_memory_size(module)); |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 169 | |
| 170 | /* No need to copy the payload if the load location and the |
| 171 | * payload location are the same. */ |
| 172 | if (module->location == module->payload) |
| 173 | return; |
| 174 | |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 175 | memcpy(module->location, module->payload, module->payload_size); |
| 176 | } |
| 177 | |
| 178 | static inline u32 *rmodule_adjustment_location(const struct rmodule *module, |
| 179 | const void *reloc) |
| 180 | { |
| 181 | int reloc_offset; |
| 182 | |
| 183 | /* Don't relocate header field entries -- only program relocations. */ |
| 184 | reloc_offset = rmodule_reloc_offset(reloc); |
| 185 | if (reloc_offset < module->header->module_link_start_address) |
| 186 | return NULL; |
| 187 | |
| 188 | return rmodule_load_addr(module, reloc_offset); |
| 189 | } |
| 190 | |
| 191 | static int rmodule_relocate(const struct rmodule *module) |
| 192 | { |
| 193 | int num_relocations; |
| 194 | const void *reloc; |
| 195 | u32 adjustment; |
| 196 | |
| 197 | /* Each relocation needs to be adjusted relative to the beginning of |
| 198 | * the loaded program. */ |
| 199 | adjustment = (u32)rmodule_load_addr(module, 0); |
| 200 | |
| 201 | reloc = module->relocations; |
| 202 | num_relocations = rmodule_number_relocations(module); |
| 203 | |
| 204 | printk(BIOS_DEBUG, "Processing %d relocs with adjust value of 0x%08x\n", |
| 205 | num_relocations, adjustment); |
| 206 | |
| 207 | while (num_relocations > 0) { |
| 208 | u32 *adjust_loc; |
| 209 | |
| 210 | if (!rmodule_reloc_valid(reloc)) |
| 211 | return -1; |
| 212 | |
| 213 | /* If the adjustment location is non-NULL adjust it. */ |
| 214 | adjust_loc = rmodule_adjustment_location(module, reloc); |
| 215 | if (adjust_loc != NULL) { |
| 216 | printk(PK_ADJ_LEVEL, "Adjusting %p: 0x%08x -> 0x%08x\n", |
| 217 | adjust_loc, *adjust_loc, |
| 218 | *adjust_loc + adjustment); |
| 219 | *adjust_loc += adjustment; |
| 220 | } |
| 221 | |
| 222 | reloc = remodule_next_reloc(reloc); |
| 223 | num_relocations--; |
| 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | int rmodule_load_alignment(const struct rmodule *module) |
| 230 | { |
| 231 | /* The load alignment is the start of the program's linked address. |
| 232 | * The base address where the program is loaded needs to be a multiple |
| 233 | * of the program's starting link address. That way all data alignment |
| 234 | * in the program is presered. */ |
| 235 | return module->header->module_link_start_address; |
| 236 | } |
| 237 | |
| 238 | int rmodule_load(void *base, struct rmodule *module) |
| 239 | { |
| 240 | /* |
| 241 | * In order to load the module at a given address, the following steps |
| 242 | * take place: |
| 243 | * 1. Copy payload to base address. |
| 244 | * 2. Clear the bss segment. |
| 245 | * 3. Adjust relocations within the module to new base address. |
| 246 | */ |
| 247 | module->location = base; |
| 248 | rmodule_copy_payload(module); |
| 249 | rmodule_clear_bss(module); |
| 250 | return rmodule_relocate(module); |
| 251 | } |
| 252 | |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 253 | void *rmodule_find_region_below(void *addr, size_t rmodule_size, |
| 254 | void **program_start, void **rmodule_start) |
| 255 | { |
| 256 | unsigned long ceiling; |
| 257 | unsigned long program_base; |
| 258 | unsigned long placement_loc; |
| 259 | unsigned long program_begin; |
| 260 | |
| 261 | ceiling = (unsigned long)addr; |
| 262 | /* Place the rmodule just under the ceiling. The rmodule files |
| 263 | * themselves are packed as a header and a payload, however the rmodule |
| 264 | * itself is linked along with the header. The header starts at address |
| 265 | * 0. Immediately following the header in the file is the program, |
| 266 | * however its starting address is determined by the rmodule linker |
| 267 | * script. In short, sizeof(struct rmodule_header) can be less than |
| 268 | * or equal to the linked address of the program. Therefore we want |
| 269 | * to place the rmodule so that the program falls on the aligned |
| 270 | * address with the header just before it. Therefore, we need at least |
| 271 | * a page to account for the size of the header. */ |
| 272 | program_base = ALIGN((ceiling - (rmodule_size + 4096)), 4096); |
| 273 | /* The program starts immediately after the header. However, |
| 274 | * it needs to be aligned to a 4KiB boundary. Therefore, adjust the |
| 275 | * program location so that the program lands on a page boundary. The |
| 276 | * layout looks like the following: |
| 277 | * |
| 278 | * +--------------------------------+ ceiling |
| 279 | * | >= 0 bytes from alignment | |
| 280 | * +--------------------------------+ program end (4KiB aligned) |
| 281 | * | program size | |
| 282 | * +--------------------------------+ program_begin (4KiB aligned) |
| 283 | * | sizeof(struct rmodule_header) | |
| 284 | * +--------------------------------+ rmodule header start |
| 285 | * | >= 0 bytes from alignment | |
| 286 | * +--------------------------------+ program_base (4KiB aligned) |
| 287 | */ |
| 288 | placement_loc = ALIGN(program_base + sizeof(struct rmodule_header), |
| 289 | 4096) - sizeof(struct rmodule_header); |
| 290 | program_begin = placement_loc + sizeof(struct rmodule_header); |
| 291 | |
| 292 | *program_start = (void *)program_begin; |
| 293 | *rmodule_start = (void *)placement_loc; |
| 294 | |
| 295 | return (void *)program_base; |
| 296 | } |