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 | */ |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 19 | #include <assert.h> |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 20 | #include <stdint.h> |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 21 | #include <stdlib.h> |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 22 | #include <string.h> |
| 23 | #include <console/console.h> |
| 24 | #include <rmodule.h> |
| 25 | |
| 26 | /* Change this define to get more verbose debugging for module loading. */ |
| 27 | #define PK_ADJ_LEVEL BIOS_NEVER |
| 28 | |
| 29 | #if CONFIG_ARCH_X86 |
| 30 | /* |
| 31 | * On X86, the only relocations currently allowed are R_386_RELATIVE which |
| 32 | * have '0' for the symbol info in the relocation metadata (in r_info). |
| 33 | * The reason is that the module is fully linked and just has the relocations' |
| 34 | * locations. |
| 35 | */ |
| 36 | typedef struct { |
Aaron Durbin | 4e6ad1b | 2014-03-10 09:53:34 -0500 | [diff] [blame] | 37 | uint32_t r_offset; |
| 38 | uint32_t r_info; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 39 | } Elf32_Rel; |
| 40 | |
| 41 | #define R_386_RELATIVE 8 |
| 42 | |
| 43 | #define RELOCTION_ENTRY_SIZE sizeof(Elf32_Rel) |
| 44 | static inline int rmodule_reloc_offset(const void *reloc) |
| 45 | { |
| 46 | const Elf32_Rel *rel = reloc; |
| 47 | return rel->r_offset; |
| 48 | } |
| 49 | |
| 50 | static inline int rmodule_reloc_valid(const void *reloc) |
| 51 | { |
| 52 | const Elf32_Rel *rel = reloc; |
| 53 | return (rel->r_info == R_386_RELATIVE); |
| 54 | } |
| 55 | |
| 56 | static inline void *remodule_next_reloc(const void *reloc) |
| 57 | { |
| 58 | const Elf32_Rel *rel = reloc; |
| 59 | rel++; |
| 60 | return (void *)rel; |
| 61 | } |
| 62 | |
| 63 | #else |
| 64 | #error Arch needs to add relocation information support for RMODULE |
| 65 | #endif |
| 66 | |
| 67 | static inline int rmodule_is_loaded(const struct rmodule *module) |
| 68 | { |
| 69 | return module->location != NULL; |
| 70 | } |
| 71 | |
| 72 | /* Calculate a loaded program address based on the blob address. */ |
| 73 | static inline void *rmodule_load_addr(const struct rmodule *module, |
Aaron Durbin | 4e6ad1b | 2014-03-10 09:53:34 -0500 | [diff] [blame] | 74 | uint32_t blob_addr) |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 75 | { |
| 76 | char *loc = module->location; |
| 77 | return &loc[blob_addr - module->header->module_link_start_address]; |
| 78 | } |
| 79 | |
| 80 | /* Initialize a rmodule structure based on raw data. */ |
| 81 | int rmodule_parse(void *ptr, struct rmodule *module) |
| 82 | { |
| 83 | char *base; |
| 84 | struct rmodule_header *rhdr; |
| 85 | |
| 86 | base = ptr; |
| 87 | rhdr = ptr; |
| 88 | |
| 89 | if (rhdr == NULL) |
| 90 | return -1; |
| 91 | |
| 92 | /* Sanity check the raw data. */ |
| 93 | if (rhdr->magic != RMODULE_MAGIC) |
| 94 | return -1; |
| 95 | if (rhdr->version != RMODULE_VERSION_1) |
| 96 | return -1; |
| 97 | |
| 98 | /* Indicate the module hasn't been loaded yet. */ |
| 99 | module->location = NULL; |
| 100 | |
| 101 | /* The rmodule only needs a reference to the reloc_header. */ |
| 102 | module->header = rhdr; |
| 103 | |
| 104 | /* The payload lives after the header. */ |
| 105 | module->payload = &base[rhdr->payload_begin_offset]; |
| 106 | module->payload_size = rhdr->payload_end_offset - |
| 107 | rhdr->payload_begin_offset; |
| 108 | module->relocations = &base[rhdr->relocations_begin_offset]; |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | int rmodule_memory_size(const struct rmodule *module) |
| 114 | { |
| 115 | return module->header->module_program_size; |
| 116 | } |
| 117 | |
| 118 | void *rmodule_parameters(const struct rmodule *module) |
| 119 | { |
| 120 | if (!rmodule_is_loaded(module)) |
| 121 | return NULL; |
| 122 | |
| 123 | /* Indicate if there are no parameters. */ |
| 124 | if (module->header->parameters_begin == module->header->parameters_end) |
| 125 | return NULL; |
| 126 | |
| 127 | return rmodule_load_addr(module, module->header->parameters_begin); |
| 128 | } |
| 129 | |
| 130 | int rmodule_entry_offset(const struct rmodule *module) |
| 131 | { |
| 132 | return module->header->module_entry_point - |
| 133 | module->header->module_link_start_address; |
| 134 | } |
| 135 | |
| 136 | void *rmodule_entry(const struct rmodule *module) |
| 137 | { |
| 138 | if (!rmodule_is_loaded(module)) |
| 139 | return NULL; |
| 140 | |
| 141 | return rmodule_load_addr(module, module->header->module_entry_point); |
| 142 | } |
| 143 | |
| 144 | static void rmodule_clear_bss(struct rmodule *module) |
| 145 | { |
| 146 | char *begin; |
| 147 | int size; |
| 148 | |
| 149 | begin = rmodule_load_addr(module, module->header->bss_begin); |
| 150 | size = module->header->bss_end - module->header->bss_begin; |
| 151 | memset(begin, 0, size); |
| 152 | } |
| 153 | |
| 154 | static inline int rmodule_number_relocations(const struct rmodule *module) |
| 155 | { |
| 156 | int r; |
| 157 | |
| 158 | r = module->header->relocations_end_offset; |
| 159 | r -= module->header->relocations_begin_offset; |
| 160 | r /= RELOCTION_ENTRY_SIZE; |
| 161 | return r; |
| 162 | } |
| 163 | |
| 164 | static void rmodule_copy_payload(const struct rmodule *module) |
| 165 | { |
| 166 | printk(BIOS_DEBUG, "Loading module at %p with entry %p. " |
| 167 | "filesize: 0x%x memsize: 0x%x\n", |
| 168 | module->location, rmodule_entry(module), |
| 169 | module->payload_size, rmodule_memory_size(module)); |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 170 | |
| 171 | /* No need to copy the payload if the load location and the |
| 172 | * payload location are the same. */ |
| 173 | if (module->location == module->payload) |
| 174 | return; |
| 175 | |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 176 | memcpy(module->location, module->payload, module->payload_size); |
| 177 | } |
| 178 | |
Aaron Durbin | 4e6ad1b | 2014-03-10 09:53:34 -0500 | [diff] [blame] | 179 | static inline uint32_t *rmodule_adjustment_location(const struct rmodule *module, |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 180 | const void *reloc) |
| 181 | { |
| 182 | int reloc_offset; |
| 183 | |
| 184 | /* Don't relocate header field entries -- only program relocations. */ |
| 185 | reloc_offset = rmodule_reloc_offset(reloc); |
| 186 | if (reloc_offset < module->header->module_link_start_address) |
| 187 | return NULL; |
| 188 | |
| 189 | return rmodule_load_addr(module, reloc_offset); |
| 190 | } |
| 191 | |
| 192 | static int rmodule_relocate(const struct rmodule *module) |
| 193 | { |
| 194 | int num_relocations; |
| 195 | const void *reloc; |
Aaron Durbin | 4e6ad1b | 2014-03-10 09:53:34 -0500 | [diff] [blame] | 196 | uint32_t adjustment; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 197 | |
| 198 | /* Each relocation needs to be adjusted relative to the beginning of |
| 199 | * the loaded program. */ |
Aaron Durbin | 4e6ad1b | 2014-03-10 09:53:34 -0500 | [diff] [blame] | 200 | adjustment = (uint32_t)rmodule_load_addr(module, 0); |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 201 | |
| 202 | reloc = module->relocations; |
| 203 | num_relocations = rmodule_number_relocations(module); |
| 204 | |
| 205 | printk(BIOS_DEBUG, "Processing %d relocs with adjust value of 0x%08x\n", |
| 206 | num_relocations, adjustment); |
| 207 | |
| 208 | while (num_relocations > 0) { |
Aaron Durbin | 4e6ad1b | 2014-03-10 09:53:34 -0500 | [diff] [blame] | 209 | uint32_t *adjust_loc; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 210 | |
| 211 | if (!rmodule_reloc_valid(reloc)) |
| 212 | return -1; |
| 213 | |
| 214 | /* If the adjustment location is non-NULL adjust it. */ |
| 215 | adjust_loc = rmodule_adjustment_location(module, reloc); |
| 216 | if (adjust_loc != NULL) { |
| 217 | printk(PK_ADJ_LEVEL, "Adjusting %p: 0x%08x -> 0x%08x\n", |
| 218 | adjust_loc, *adjust_loc, |
| 219 | *adjust_loc + adjustment); |
| 220 | *adjust_loc += adjustment; |
| 221 | } |
| 222 | |
| 223 | reloc = remodule_next_reloc(reloc); |
| 224 | num_relocations--; |
| 225 | } |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | int rmodule_load_alignment(const struct rmodule *module) |
| 231 | { |
| 232 | /* The load alignment is the start of the program's linked address. |
| 233 | * The base address where the program is loaded needs to be a multiple |
| 234 | * of the program's starting link address. That way all data alignment |
Martin Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 235 | * in the program is preserved. */ |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 236 | return module->header->module_link_start_address; |
| 237 | } |
| 238 | |
| 239 | int rmodule_load(void *base, struct rmodule *module) |
| 240 | { |
| 241 | /* |
| 242 | * In order to load the module at a given address, the following steps |
| 243 | * take place: |
| 244 | * 1. Copy payload to base address. |
Aaron Durbin | 55ed310 | 2013-03-01 17:00:39 -0600 | [diff] [blame] | 245 | * 2. Adjust relocations within the module to new base address. |
| 246 | * 3. Clear the bss segment last since the relocations live where |
| 247 | * the bss is. If an rmodule is being loaded from its load |
| 248 | * address the relocations need to be processed before the bss. |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 249 | */ |
| 250 | module->location = base; |
| 251 | rmodule_copy_payload(module); |
Aaron Durbin | 55ed310 | 2013-03-01 17:00:39 -0600 | [diff] [blame] | 252 | if (rmodule_relocate(module)) |
| 253 | return -1; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 254 | rmodule_clear_bss(module); |
Aaron Durbin | 55ed310 | 2013-03-01 17:00:39 -0600 | [diff] [blame] | 255 | return 0; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 256 | } |
| 257 | |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 258 | int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size, |
| 259 | size_t *region_size, int *load_offset) |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 260 | { |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 261 | /* region_alignment must be a power of 2. */ |
| 262 | if (region_alignment & (region_alignment - 1)) |
| 263 | BUG(); |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 264 | |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 265 | if (region_alignment < 4096) |
| 266 | region_alignment = 4096; |
| 267 | |
| 268 | /* Sanity check rmodule_header size. The code below assumes it is less |
| 269 | * than the minimum alignment required. */ |
| 270 | if (region_alignment < sizeof(struct rmodule_header)) |
| 271 | BUG(); |
| 272 | |
| 273 | /* Place the rmodule according to alignment. The rmodule files |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 274 | * themselves are packed as a header and a payload, however the rmodule |
| 275 | * itself is linked along with the header. The header starts at address |
| 276 | * 0. Immediately following the header in the file is the program, |
| 277 | * however its starting address is determined by the rmodule linker |
| 278 | * script. In short, sizeof(struct rmodule_header) can be less than |
| 279 | * or equal to the linked address of the program. Therefore we want |
| 280 | * to place the rmodule so that the program falls on the aligned |
| 281 | * address with the header just before it. Therefore, we need at least |
| 282 | * a page to account for the size of the header. */ |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 283 | *region_size = ALIGN(rmodule_size + region_alignment, 4096); |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 284 | /* The program starts immediately after the header. However, |
| 285 | * it needs to be aligned to a 4KiB boundary. Therefore, adjust the |
| 286 | * program location so that the program lands on a page boundary. The |
| 287 | * layout looks like the following: |
| 288 | * |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 289 | * +--------------------------------+ region_alignment + region_size |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 290 | * | >= 0 bytes from alignment | |
| 291 | * +--------------------------------+ program end (4KiB aligned) |
| 292 | * | program size | |
| 293 | * +--------------------------------+ program_begin (4KiB aligned) |
| 294 | * | sizeof(struct rmodule_header) | |
| 295 | * +--------------------------------+ rmodule header start |
| 296 | * | >= 0 bytes from alignment | |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 297 | * +--------------------------------+ region_alignment |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 298 | */ |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 299 | *load_offset = region_alignment; |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 300 | |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 301 | return region_alignment - sizeof(struct rmodule_header); |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 302 | } |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 303 | |
| 304 | #if CONFIG_DYNAMIC_CBMEM |
| 305 | #include <cbmem.h> |
| 306 | #include <cbfs_core.h> |
| 307 | |
| 308 | int rmodule_stage_load(struct rmod_stage_load *rsl, struct cbfs_stage *stage) |
| 309 | { |
| 310 | struct rmodule rmod_stage; |
| 311 | size_t region_size; |
| 312 | char *stage_region; |
| 313 | int rmodule_offset; |
| 314 | int load_offset; |
| 315 | const struct cbmem_entry *cbmem_entry; |
| 316 | |
| 317 | if (stage == NULL || rsl->name == NULL) |
| 318 | return -1; |
| 319 | |
| 320 | rmodule_offset = |
| 321 | rmodule_calc_region(DYN_CBMEM_ALIGN_SIZE, |
| 322 | stage->memlen, ®ion_size, &load_offset); |
| 323 | |
| 324 | cbmem_entry = cbmem_entry_add(rsl->cbmem_id, region_size); |
| 325 | |
| 326 | if (cbmem_entry == NULL) |
| 327 | return -1; |
| 328 | |
| 329 | stage_region = cbmem_entry_start(cbmem_entry); |
| 330 | |
| 331 | printk(BIOS_INFO, "Decompressing stage %s @ 0x%p (%d bytes)\n", |
| 332 | rsl->name, &stage_region[rmodule_offset], stage->memlen); |
| 333 | |
| 334 | if (!cbfs_decompress(stage->compression, &stage[1], |
| 335 | &stage_region[rmodule_offset], stage->len)) |
| 336 | return -1; |
| 337 | |
| 338 | if (rmodule_parse(&stage_region[rmodule_offset], &rmod_stage)) |
| 339 | return -1; |
| 340 | |
| 341 | if (rmodule_load(&stage_region[load_offset], &rmod_stage)) |
| 342 | return -1; |
| 343 | |
| 344 | rsl->cbmem_entry = cbmem_entry; |
| 345 | rsl->entry = rmodule_entry(&rmod_stage); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | int rmodule_stage_load_from_cbfs(struct rmod_stage_load *rsl) |
| 351 | { |
| 352 | struct cbfs_stage *stage; |
| 353 | |
| 354 | stage = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, |
| 355 | rsl->name, CBFS_TYPE_STAGE, NULL); |
| 356 | |
| 357 | if (stage == NULL) |
| 358 | return -1; |
| 359 | |
| 360 | return rmodule_stage_load(rsl, stage); |
| 361 | } |
| 362 | |
| 363 | #endif /* DYNAMIC_CBMEM */ |