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