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> |
| 21 | #include <cbfs_core.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 | |
Ionela Voinescu | 00903e5 | 2015-01-09 13:14:20 +0000 | [diff] [blame^] | 203 | arch_program_segment_loaded((uintptr_t)module->location, |
| 204 | rmodule_memory_size(module)); |
| 205 | arch_program_loaded(); |
Aaron Durbin | f72f9e7 | 2014-03-25 15:31:00 -0500 | [diff] [blame] | 206 | |
Aaron Durbin | 55ed310 | 2013-03-01 17:00:39 -0600 | [diff] [blame] | 207 | return 0; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 208 | } |
| 209 | |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 210 | int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size, |
| 211 | size_t *region_size, int *load_offset) |
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 | /* region_alignment must be a power of 2. */ |
| 214 | if (region_alignment & (region_alignment - 1)) |
| 215 | BUG(); |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 216 | |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 217 | if (region_alignment < 4096) |
| 218 | region_alignment = 4096; |
| 219 | |
| 220 | /* Sanity check rmodule_header size. The code below assumes it is less |
| 221 | * than the minimum alignment required. */ |
| 222 | if (region_alignment < sizeof(struct rmodule_header)) |
| 223 | BUG(); |
| 224 | |
| 225 | /* Place the rmodule according to alignment. The rmodule files |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 226 | * themselves are packed as a header and a payload, however the rmodule |
| 227 | * itself is linked along with the header. The header starts at address |
| 228 | * 0. Immediately following the header in the file is the program, |
| 229 | * however its starting address is determined by the rmodule linker |
| 230 | * script. In short, sizeof(struct rmodule_header) can be less than |
| 231 | * or equal to the linked address of the program. Therefore we want |
| 232 | * to place the rmodule so that the program falls on the aligned |
| 233 | * address with the header just before it. Therefore, we need at least |
| 234 | * a page to account for the size of the header. */ |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 235 | *region_size = ALIGN(rmodule_size + region_alignment, 4096); |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 236 | /* The program starts immediately after the header. However, |
| 237 | * it needs to be aligned to a 4KiB boundary. Therefore, adjust the |
| 238 | * program location so that the program lands on a page boundary. The |
| 239 | * layout looks like the following: |
| 240 | * |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 241 | * +--------------------------------+ region_alignment + region_size |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 242 | * | >= 0 bytes from alignment | |
| 243 | * +--------------------------------+ program end (4KiB aligned) |
| 244 | * | program size | |
| 245 | * +--------------------------------+ program_begin (4KiB aligned) |
| 246 | * | sizeof(struct rmodule_header) | |
| 247 | * +--------------------------------+ rmodule header start |
| 248 | * | >= 0 bytes from alignment | |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 249 | * +--------------------------------+ region_alignment |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 250 | */ |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 251 | *load_offset = region_alignment; |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 252 | |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 253 | return region_alignment - sizeof(struct rmodule_header); |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 254 | } |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 255 | |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 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 | } |