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