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