blob: e99f10a38b4894832e7ce6c417573bb2c3627fc4 [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Aaron Durbindd4a6d22013-02-27 22:50:12 -06003#include <assert.h>
Kyösti Mälkkiae98e832014-11-28 11:24:19 +02004#include <cbmem.h>
Aaron Durbin67514a72015-03-26 21:04:18 -05005#include <cbfs.h>
Aaron Durbinad935522012-12-24 14:28:37 -06006#include <stdint.h>
Aaron Durbine8c866a2013-02-08 17:05:36 -06007#include <stdlib.h>
Aaron Durbinad935522012-12-24 14:28:37 -06008#include <string.h>
9#include <console/console.h>
Ionela Voinescu00903e52015-01-09 13:14:20 +000010#include <program_loading.h>
Aaron Durbinad935522012-12-24 14:28:37 -060011#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 Durbinad935522012-12-24 14:28:37 -060016static 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. */
22static inline void *rmodule_load_addr(const struct rmodule *module,
Lee Leahye20a3192017-03-09 16:21:34 -080023 uintptr_t blob_addr)
Aaron Durbinad935522012-12-24 14:28:37 -060024{
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. */
30int 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 Leahye20a3192017-03-09 16:21:34 -080056 rhdr->payload_begin_offset;
Aaron Durbinad935522012-12-24 14:28:37 -060057 module->relocations = &base[rhdr->relocations_begin_offset];
58
59 return 0;
60}
61
62int rmodule_memory_size(const struct rmodule *module)
63{
64 return module->header->module_program_size;
65}
66
67void *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
79int rmodule_entry_offset(const struct rmodule *module)
80{
81 return module->header->module_entry_point -
82 module->header->module_link_start_address;
83}
84
85void *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
93static 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 Durbin3eb8eb72014-03-10 16:13:58 -0500103static inline size_t rmodule_number_relocations(const struct rmodule *module)
Aaron Durbinad935522012-12-24 14:28:37 -0600104{
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500105 size_t r;
Aaron Durbinad935522012-12-24 14:28:37 -0600106
107 r = module->header->relocations_end_offset;
108 r -= module->header->relocations_begin_offset;
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500109 r /= sizeof(uintptr_t);
Aaron Durbinad935522012-12-24 14:28:37 -0600110 return r;
111}
112
113static 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 Durbine8c866a2013-02-08 17:05:36 -0600119
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 Durbinad935522012-12-24 14:28:37 -0600125 memcpy(module->location, module->payload, module->payload_size);
126}
127
Aaron Durbinad935522012-12-24 14:28:37 -0600128static int rmodule_relocate(const struct rmodule *module)
129{
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500130 size_t num_relocations;
131 const uintptr_t *reloc;
132 uintptr_t adjustment;
Aaron Durbinad935522012-12-24 14:28:37 -0600133
134 /* Each relocation needs to be adjusted relative to the beginning of
135 * the loaded program. */
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500136 adjustment = (uintptr_t)rmodule_load_addr(module, 0);
Aaron Durbinad935522012-12-24 14:28:37 -0600137
138 reloc = module->relocations;
139 num_relocations = rmodule_number_relocations(module);
140
Furquan Shaikh7a3c3492014-07-22 10:59:28 -0700141 printk(BIOS_DEBUG, "Processing %zu relocs. Offset value of 0x%08lx\n",
142 num_relocations, (unsigned long)adjustment);
Aaron Durbinad935522012-12-24 14:28:37 -0600143
144 while (num_relocations > 0) {
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500145 uintptr_t *adjust_loc;
Aaron Durbinad935522012-12-24 14:28:37 -0600146
147 /* If the adjustment location is non-NULL adjust it. */
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500148 adjust_loc = rmodule_load_addr(module, *reloc);
Furquan Shaikh7a3c3492014-07-22 10:59:28 -0700149 printk(PK_ADJ_LEVEL, "Adjusting %p: 0x%08lx -> 0x%08lx\n",
150 adjust_loc, (unsigned long) *adjust_loc,
Furquan Shaikhae9cd012014-07-23 04:44:09 -0700151 (unsigned long) (*adjust_loc + adjustment));
Kyösti Mälkkic3bc6cb2018-06-25 18:51:05 +0300152 *adjust_loc += adjustment;
Aaron Durbinad935522012-12-24 14:28:37 -0600153
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500154 reloc++;
Aaron Durbinad935522012-12-24 14:28:37 -0600155 num_relocations--;
156 }
157
158 return 0;
159}
160
161int 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 Durbin3eb8eb72014-03-10 16:13:58 -0500166 * in the program is preserved. Default to 4KiB. */
167 return 4096;
Aaron Durbinad935522012-12-24 14:28:37 -0600168}
169
170int 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 Durbin55ed3102013-03-01 17:00:39 -0600176 * 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 Durbinad935522012-12-24 14:28:37 -0600180 */
181 module->location = base;
182 rmodule_copy_payload(module);
Aaron Durbin55ed3102013-03-01 17:00:39 -0600183 if (rmodule_relocate(module))
184 return -1;
Aaron Durbinad935522012-12-24 14:28:37 -0600185 rmodule_clear_bss(module);
Aaron Durbinf72f9e72014-03-25 15:31:00 -0500186
Aaron Durbin096f4572016-03-31 13:49:00 -0500187 prog_segment_loaded((uintptr_t)module->location,
Aaron Durbin6e76fff2015-03-20 09:42:05 -0500188 rmodule_memory_size(module), SEG_FINAL);
Aaron Durbinf72f9e72014-03-25 15:31:00 -0500189
Aaron Durbin55ed3102013-03-01 17:00:39 -0600190 return 0;
Aaron Durbinad935522012-12-24 14:28:37 -0600191}
192
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600193int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size,
Lee Leahye20a3192017-03-09 16:21:34 -0800194 size_t *region_size, int *load_offset)
Aaron Durbine8c866a2013-02-08 17:05:36 -0600195{
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600196 /* region_alignment must be a power of 2. */
197 if (region_alignment & (region_alignment - 1))
198 BUG();
Aaron Durbine8c866a2013-02-08 17:05:36 -0600199
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600200 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 Durbine8c866a2013-02-08 17:05:36 -0600209 * 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 Durbindd4a6d22013-02-27 22:50:12 -0600218 *region_size = ALIGN(rmodule_size + region_alignment, 4096);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600219 /* 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 Durbindd4a6d22013-02-27 22:50:12 -0600224 * +--------------------------------+ region_alignment + region_size
Aaron Durbine8c866a2013-02-08 17:05:36 -0600225 * | >= 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 Durbindd4a6d22013-02-27 22:50:12 -0600232 * +--------------------------------+ region_alignment
Aaron Durbine8c866a2013-02-08 17:05:36 -0600233 */
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600234 *load_offset = region_alignment;
Aaron Durbine8c866a2013-02-08 17:05:36 -0600235
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600236 return region_alignment - sizeof(struct rmodule_header);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600237}
Aaron Durbinf545abf2013-10-24 10:14:06 -0500238
Aaron Durbin899d13d2015-05-15 23:39:23 -0500239int rmodule_stage_load(struct rmod_stage_load *rsl)
Aaron Durbinf545abf2013-10-24 10:14:06 -0500240{
241 struct rmodule rmod_stage;
242 size_t region_size;
243 char *stage_region;
244 int rmodule_offset;
245 int load_offset;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500246 struct cbfs_stage stage;
247 void *rmod_loc;
248 struct region_device *fh;
Aaron Durbinf545abf2013-10-24 10:14:06 -0500249
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500250 if (rsl->prog == NULL || prog_name(rsl->prog) == NULL)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500251 return -1;
252
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500253 fh = prog_rdev(rsl->prog);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500254
255 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
Aaron Durbinf545abf2013-10-24 10:14:06 -0500256 return -1;
257
258 rmodule_offset =
259 rmodule_calc_region(DYN_CBMEM_ALIGN_SIZE,
Lee Leahye20a3192017-03-09 16:21:34 -0800260 stage.memlen, &region_size, &load_offset);
Aaron Durbinf545abf2013-10-24 10:14:06 -0500261
Aaron Durbin7ca65222015-04-06 17:18:18 -0500262 stage_region = cbmem_add(rsl->cbmem_id, region_size);
Aaron Durbinf545abf2013-10-24 10:14:06 -0500263
Aaron Durbin7ca65222015-04-06 17:18:18 -0500264 if (stage_region == NULL)
Aaron Durbinf545abf2013-10-24 10:14:06 -0500265 return -1;
266
Aaron Durbin899d13d2015-05-15 23:39:23 -0500267 rmod_loc = &stage_region[rmodule_offset];
Aaron Durbinf545abf2013-10-24 10:14:06 -0500268
Julius Werner540a9802019-12-09 13:03:29 -0800269 printk(BIOS_INFO, "Decompressing stage %s @ %p (%d bytes)\n",
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500270 prog_name(rsl->prog), rmod_loc, stage.memlen);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500271
Julius Werner09f29212015-09-29 13:51:35 -0700272 if (!cbfs_load_and_decompress(fh, sizeof(stage), stage.len, rmod_loc,
273 stage.memlen, stage.compression))
Aaron Durbinf545abf2013-10-24 10:14:06 -0500274 return -1;
275
Aaron Durbin899d13d2015-05-15 23:39:23 -0500276 if (rmodule_parse(rmod_loc, &rmod_stage))
Aaron Durbinf545abf2013-10-24 10:14:06 -0500277 return -1;
278
279 if (rmodule_load(&stage_region[load_offset], &rmod_stage))
280 return -1;
281
Aaron Durbin7ca65222015-04-06 17:18:18 -0500282 prog_set_area(rsl->prog, rmod_stage.location,
283 rmodule_memory_size(&rmod_stage));
Aaron Durbinf545abf2013-10-24 10:14:06 -0500284
Aaron Durbin94271b42016-03-17 23:13:34 -0500285 /* Allow caller to pick up parameters, if available. */
286 rsl->params = rmodule_parameters(&rmod_stage);
287
Kyösti Mälkkid87e4b32017-09-05 22:43:05 +0300288 prog_set_entry(rsl->prog, rmodule_entry(&rmod_stage), rsl->params);
289
Aaron Durbinf545abf2013-10-24 10:14:06 -0500290 return 0;
291}