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. |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 14 | */ |
Aaron Durbin | dd4a6d2 | 2013-02-27 22:50:12 -0600 | [diff] [blame] | 15 | #include <assert.h> |
Kyösti Mälkki | ae98e83 | 2014-11-28 11:24:19 +0200 | [diff] [blame] | 16 | #include <cbmem.h> |
Aaron Durbin | 67514a7 | 2015-03-26 21:04:18 -0500 | [diff] [blame] | 17 | #include <cbfs.h> |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 18 | #include <stdint.h> |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 19 | #include <stdlib.h> |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 20 | #include <string.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 21 | #include <lib.h> |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 22 | #include <console/console.h> |
Ionela Voinescu | 00903e5 | 2015-01-09 13:14:20 +0000 | [diff] [blame] | 23 | #include <program_loading.h> |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 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 | |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 29 | static inline int rmodule_is_loaded(const struct rmodule *module) |
| 30 | { |
| 31 | return module->location != NULL; |
| 32 | } |
| 33 | |
| 34 | /* Calculate a loaded program address based on the blob address. */ |
| 35 | static inline void *rmodule_load_addr(const struct rmodule *module, |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 36 | uintptr_t blob_addr) |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 37 | { |
| 38 | char *loc = module->location; |
| 39 | return &loc[blob_addr - module->header->module_link_start_address]; |
| 40 | } |
| 41 | |
| 42 | /* Initialize a rmodule structure based on raw data. */ |
| 43 | int rmodule_parse(void *ptr, struct rmodule *module) |
| 44 | { |
| 45 | char *base; |
| 46 | struct rmodule_header *rhdr; |
| 47 | |
| 48 | base = ptr; |
| 49 | rhdr = ptr; |
| 50 | |
| 51 | if (rhdr == NULL) |
| 52 | return -1; |
| 53 | |
| 54 | /* Sanity check the raw data. */ |
| 55 | if (rhdr->magic != RMODULE_MAGIC) |
| 56 | return -1; |
| 57 | if (rhdr->version != RMODULE_VERSION_1) |
| 58 | return -1; |
| 59 | |
| 60 | /* Indicate the module hasn't been loaded yet. */ |
| 61 | module->location = NULL; |
| 62 | |
| 63 | /* The rmodule only needs a reference to the reloc_header. */ |
| 64 | module->header = rhdr; |
| 65 | |
| 66 | /* The payload lives after the header. */ |
| 67 | module->payload = &base[rhdr->payload_begin_offset]; |
| 68 | module->payload_size = rhdr->payload_end_offset - |
| 69 | rhdr->payload_begin_offset; |
| 70 | module->relocations = &base[rhdr->relocations_begin_offset]; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int rmodule_memory_size(const struct rmodule *module) |
| 76 | { |
| 77 | return module->header->module_program_size; |
| 78 | } |
| 79 | |
| 80 | void *rmodule_parameters(const struct rmodule *module) |
| 81 | { |
| 82 | if (!rmodule_is_loaded(module)) |
| 83 | return NULL; |
| 84 | |
| 85 | /* Indicate if there are no parameters. */ |
| 86 | if (module->header->parameters_begin == module->header->parameters_end) |
| 87 | return NULL; |
| 88 | |
| 89 | return rmodule_load_addr(module, module->header->parameters_begin); |
| 90 | } |
| 91 | |
| 92 | int rmodule_entry_offset(const struct rmodule *module) |
| 93 | { |
| 94 | return module->header->module_entry_point - |
| 95 | module->header->module_link_start_address; |
| 96 | } |
| 97 | |
| 98 | void *rmodule_entry(const struct rmodule *module) |
| 99 | { |
| 100 | if (!rmodule_is_loaded(module)) |
| 101 | return NULL; |
| 102 | |
| 103 | return rmodule_load_addr(module, module->header->module_entry_point); |
| 104 | } |
| 105 | |
| 106 | static void rmodule_clear_bss(struct rmodule *module) |
| 107 | { |
| 108 | char *begin; |
| 109 | int size; |
| 110 | |
| 111 | begin = rmodule_load_addr(module, module->header->bss_begin); |
| 112 | size = module->header->bss_end - module->header->bss_begin; |
| 113 | memset(begin, 0, size); |
| 114 | } |
| 115 | |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 116 | static inline size_t rmodule_number_relocations(const struct rmodule *module) |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 117 | { |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 118 | size_t r; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 119 | |
| 120 | r = module->header->relocations_end_offset; |
| 121 | r -= module->header->relocations_begin_offset; |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 122 | r /= sizeof(uintptr_t); |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 123 | return r; |
| 124 | } |
| 125 | |
| 126 | static void rmodule_copy_payload(const struct rmodule *module) |
| 127 | { |
| 128 | printk(BIOS_DEBUG, "Loading module at %p with entry %p. " |
| 129 | "filesize: 0x%x memsize: 0x%x\n", |
| 130 | module->location, rmodule_entry(module), |
| 131 | module->payload_size, rmodule_memory_size(module)); |
Aaron Durbin | e8c866a | 2013-02-08 17:05:36 -0600 | [diff] [blame] | 132 | |
| 133 | /* No need to copy the payload if the load location and the |
| 134 | * payload location are the same. */ |
| 135 | if (module->location == module->payload) |
| 136 | return; |
| 137 | |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 138 | memcpy(module->location, module->payload, module->payload_size); |
| 139 | } |
| 140 | |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 141 | static int rmodule_relocate(const struct rmodule *module) |
| 142 | { |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 143 | size_t num_relocations; |
| 144 | const uintptr_t *reloc; |
| 145 | uintptr_t adjustment; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 146 | |
| 147 | /* Each relocation needs to be adjusted relative to the beginning of |
| 148 | * the loaded program. */ |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 149 | adjustment = (uintptr_t)rmodule_load_addr(module, 0); |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 150 | |
| 151 | reloc = module->relocations; |
| 152 | num_relocations = rmodule_number_relocations(module); |
| 153 | |
Furquan Shaikh | 7a3c349 | 2014-07-22 10:59:28 -0700 | [diff] [blame] | 154 | printk(BIOS_DEBUG, "Processing %zu relocs. Offset value of 0x%08lx\n", |
| 155 | num_relocations, (unsigned long)adjustment); |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 156 | |
| 157 | while (num_relocations > 0) { |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 158 | uintptr_t *adjust_loc; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 159 | |
| 160 | /* If the adjustment location is non-NULL adjust it. */ |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 161 | adjust_loc = rmodule_load_addr(module, *reloc); |
Furquan Shaikh | 7a3c349 | 2014-07-22 10:59:28 -0700 | [diff] [blame] | 162 | printk(PK_ADJ_LEVEL, "Adjusting %p: 0x%08lx -> 0x%08lx\n", |
| 163 | adjust_loc, (unsigned long) *adjust_loc, |
Furquan Shaikh | ae9cd01 | 2014-07-23 04:44:09 -0700 | [diff] [blame] | 164 | (unsigned long) (*adjust_loc + adjustment)); |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 165 | *adjust_loc += adjustment; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 166 | |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 167 | reloc++; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 168 | num_relocations--; |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int rmodule_load_alignment(const struct rmodule *module) |
| 175 | { |
| 176 | /* The load alignment is the start of the program's linked address. |
| 177 | * The base address where the program is loaded needs to be a multiple |
| 178 | * of the program's starting link address. That way all data alignment |
Aaron Durbin | 3eb8eb7 | 2014-03-10 16:13:58 -0500 | [diff] [blame] | 179 | * in the program is preserved. Default to 4KiB. */ |
| 180 | return 4096; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | int rmodule_load(void *base, struct rmodule *module) |
| 184 | { |
| 185 | /* |
| 186 | * In order to load the module at a given address, the following steps |
| 187 | * take place: |
| 188 | * 1. Copy payload to base address. |
Aaron Durbin | 55ed310 | 2013-03-01 17:00:39 -0600 | [diff] [blame] | 189 | * 2. Adjust relocations within the module to new base address. |
| 190 | * 3. Clear the bss segment last since the relocations live where |
| 191 | * the bss is. If an rmodule is being loaded from its load |
| 192 | * address the relocations need to be processed before the bss. |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 193 | */ |
| 194 | module->location = base; |
| 195 | rmodule_copy_payload(module); |
Aaron Durbin | 55ed310 | 2013-03-01 17:00:39 -0600 | [diff] [blame] | 196 | if (rmodule_relocate(module)) |
| 197 | return -1; |
Aaron Durbin | ad93552 | 2012-12-24 14:28:37 -0600 | [diff] [blame] | 198 | rmodule_clear_bss(module); |
Aaron Durbin | f72f9e7 | 2014-03-25 15:31:00 -0500 | [diff] [blame] | 199 | |
Aaron Durbin | 096f457 | 2016-03-31 13:49:00 -0500 | [diff] [blame] | 200 | prog_segment_loaded((uintptr_t)module->location, |
Aaron Durbin | 6e76fff | 2015-03-20 09:42:05 -0500 | [diff] [blame] | 201 | rmodule_memory_size(module), SEG_FINAL); |
Aaron Durbin | f72f9e7 | 2014-03-25 15:31:00 -0500 | [diff] [blame] | 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 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 252 | int rmodule_stage_load(struct rmod_stage_load *rsl) |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 253 | { |
| 254 | struct rmodule rmod_stage; |
| 255 | size_t region_size; |
| 256 | char *stage_region; |
| 257 | int rmodule_offset; |
| 258 | int load_offset; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 259 | struct cbfs_stage stage; |
| 260 | void *rmod_loc; |
| 261 | struct region_device *fh; |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 262 | |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 263 | if (rsl->prog == NULL || prog_name(rsl->prog) == NULL) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 264 | return -1; |
| 265 | |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 266 | fh = prog_rdev(rsl->prog); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 267 | |
| 268 | if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage)) |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 269 | return -1; |
| 270 | |
| 271 | rmodule_offset = |
| 272 | rmodule_calc_region(DYN_CBMEM_ALIGN_SIZE, |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 273 | stage.memlen, ®ion_size, &load_offset); |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 274 | |
Aaron Durbin | 7ca6522 | 2015-04-06 17:18:18 -0500 | [diff] [blame] | 275 | stage_region = cbmem_add(rsl->cbmem_id, region_size); |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 276 | |
Aaron Durbin | 7ca6522 | 2015-04-06 17:18:18 -0500 | [diff] [blame] | 277 | if (stage_region == NULL) |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 278 | return -1; |
| 279 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 280 | rmod_loc = &stage_region[rmodule_offset]; |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 281 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 282 | printk(BIOS_INFO, "Decompressing stage %s @ 0x%p (%d bytes)\n", |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 283 | prog_name(rsl->prog), rmod_loc, stage.memlen); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 284 | |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 285 | if (!cbfs_load_and_decompress(fh, sizeof(stage), stage.len, rmod_loc, |
| 286 | stage.memlen, stage.compression)) |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 287 | return -1; |
| 288 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 289 | if (rmodule_parse(rmod_loc, &rmod_stage)) |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 290 | return -1; |
| 291 | |
| 292 | if (rmodule_load(&stage_region[load_offset], &rmod_stage)) |
| 293 | return -1; |
| 294 | |
Aaron Durbin | 7ca6522 | 2015-04-06 17:18:18 -0500 | [diff] [blame] | 295 | prog_set_area(rsl->prog, rmod_stage.location, |
| 296 | rmodule_memory_size(&rmod_stage)); |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 297 | prog_set_entry(rsl->prog, rmodule_entry(&rmod_stage), NULL); |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 298 | |
Aaron Durbin | 94271b4 | 2016-03-17 23:13:34 -0500 | [diff] [blame] | 299 | /* Allow caller to pick up parameters, if available. */ |
| 300 | rsl->params = rmodule_parameters(&rmod_stage); |
| 301 | |
Aaron Durbin | f545abf | 2013-10-24 10:14:06 -0500 | [diff] [blame] | 302 | return 0; |
| 303 | } |