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