blob: 794453527bdcbcb294870131851d026cd6af5293 [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbindd4a6d22013-02-27 22:50:12 -06002#include <assert.h>
Kyösti Mälkkiae98e832014-11-28 11:24:19 +02003#include <cbmem.h>
Aaron Durbin67514a72015-03-26 21:04:18 -05004#include <cbfs.h>
Aaron Durbinad935522012-12-24 14:28:37 -06005#include <stdint.h>
Aaron Durbine8c866a2013-02-08 17:05:36 -06006#include <stdlib.h>
Aaron Durbinad935522012-12-24 14:28:37 -06007#include <string.h>
8#include <console/console.h>
Ionela Voinescu00903e52015-01-09 13:14:20 +00009#include <program_loading.h>
Aaron Durbinad935522012-12-24 14:28:37 -060010#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 Durbinad935522012-12-24 14:28:37 -060015static 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. */
21static inline void *rmodule_load_addr(const struct rmodule *module,
Lee Leahye20a3192017-03-09 16:21:34 -080022 uintptr_t blob_addr)
Aaron Durbinad935522012-12-24 14:28:37 -060023{
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. */
29int 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 Leahye20a3192017-03-09 16:21:34 -080055 rhdr->payload_begin_offset;
Aaron Durbinad935522012-12-24 14:28:37 -060056 module->relocations = &base[rhdr->relocations_begin_offset];
57
58 return 0;
59}
60
61int rmodule_memory_size(const struct rmodule *module)
62{
63 return module->header->module_program_size;
64}
65
66void *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
78int rmodule_entry_offset(const struct rmodule *module)
79{
80 return module->header->module_entry_point -
81 module->header->module_link_start_address;
82}
83
84void *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
92static 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 Durbin3eb8eb72014-03-10 16:13:58 -0500102static inline size_t rmodule_number_relocations(const struct rmodule *module)
Aaron Durbinad935522012-12-24 14:28:37 -0600103{
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500104 size_t r;
Aaron Durbinad935522012-12-24 14:28:37 -0600105
106 r = module->header->relocations_end_offset;
107 r -= module->header->relocations_begin_offset;
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500108 r /= sizeof(uintptr_t);
Aaron Durbinad935522012-12-24 14:28:37 -0600109 return r;
110}
111
112static 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 Durbine8c866a2013-02-08 17:05:36 -0600118
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 Durbinad935522012-12-24 14:28:37 -0600124 memcpy(module->location, module->payload, module->payload_size);
125}
126
Aaron Durbinad935522012-12-24 14:28:37 -0600127static int rmodule_relocate(const struct rmodule *module)
128{
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500129 size_t num_relocations;
130 const uintptr_t *reloc;
131 uintptr_t adjustment;
Aaron Durbinad935522012-12-24 14:28:37 -0600132
133 /* Each relocation needs to be adjusted relative to the beginning of
134 * the loaded program. */
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500135 adjustment = (uintptr_t)rmodule_load_addr(module, 0);
Aaron Durbinad935522012-12-24 14:28:37 -0600136
137 reloc = module->relocations;
138 num_relocations = rmodule_number_relocations(module);
139
Furquan Shaikh7a3c3492014-07-22 10:59:28 -0700140 printk(BIOS_DEBUG, "Processing %zu relocs. Offset value of 0x%08lx\n",
141 num_relocations, (unsigned long)adjustment);
Aaron Durbinad935522012-12-24 14:28:37 -0600142
143 while (num_relocations > 0) {
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500144 uintptr_t *adjust_loc;
Aaron Durbinad935522012-12-24 14:28:37 -0600145
146 /* If the adjustment location is non-NULL adjust it. */
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500147 adjust_loc = rmodule_load_addr(module, *reloc);
Furquan Shaikh7a3c3492014-07-22 10:59:28 -0700148 printk(PK_ADJ_LEVEL, "Adjusting %p: 0x%08lx -> 0x%08lx\n",
149 adjust_loc, (unsigned long) *adjust_loc,
Furquan Shaikhae9cd012014-07-23 04:44:09 -0700150 (unsigned long) (*adjust_loc + adjustment));
Kyösti Mälkkic3bc6cb2018-06-25 18:51:05 +0300151 *adjust_loc += adjustment;
Aaron Durbinad935522012-12-24 14:28:37 -0600152
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500153 reloc++;
Aaron Durbinad935522012-12-24 14:28:37 -0600154 num_relocations--;
155 }
156
157 return 0;
158}
159
160int 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 Durbin3eb8eb72014-03-10 16:13:58 -0500165 * in the program is preserved. Default to 4KiB. */
166 return 4096;
Aaron Durbinad935522012-12-24 14:28:37 -0600167}
168
169int 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 Durbin55ed3102013-03-01 17:00:39 -0600175 * 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 Durbinad935522012-12-24 14:28:37 -0600179 */
180 module->location = base;
181 rmodule_copy_payload(module);
Aaron Durbin55ed3102013-03-01 17:00:39 -0600182 if (rmodule_relocate(module))
183 return -1;
Aaron Durbinad935522012-12-24 14:28:37 -0600184 rmodule_clear_bss(module);
Aaron Durbinf72f9e72014-03-25 15:31:00 -0500185
Aaron Durbin096f4572016-03-31 13:49:00 -0500186 prog_segment_loaded((uintptr_t)module->location,
Aaron Durbin6e76fff2015-03-20 09:42:05 -0500187 rmodule_memory_size(module), SEG_FINAL);
Aaron Durbinf72f9e72014-03-25 15:31:00 -0500188
Aaron Durbin55ed3102013-03-01 17:00:39 -0600189 return 0;
Aaron Durbinad935522012-12-24 14:28:37 -0600190}
191
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600192int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size,
Lee Leahye20a3192017-03-09 16:21:34 -0800193 size_t *region_size, int *load_offset)
Aaron Durbine8c866a2013-02-08 17:05:36 -0600194{
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600195 /* region_alignment must be a power of 2. */
196 if (region_alignment & (region_alignment - 1))
197 BUG();
Aaron Durbine8c866a2013-02-08 17:05:36 -0600198
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600199 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 Durbine8c866a2013-02-08 17:05:36 -0600208 * 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 Durbindd4a6d22013-02-27 22:50:12 -0600217 *region_size = ALIGN(rmodule_size + region_alignment, 4096);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600218 /* 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 Durbindd4a6d22013-02-27 22:50:12 -0600223 * +--------------------------------+ region_alignment + region_size
Aaron Durbine8c866a2013-02-08 17:05:36 -0600224 * | >= 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 Durbindd4a6d22013-02-27 22:50:12 -0600231 * +--------------------------------+ region_alignment
Aaron Durbine8c866a2013-02-08 17:05:36 -0600232 */
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600233 *load_offset = region_alignment;
Aaron Durbine8c866a2013-02-08 17:05:36 -0600234
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600235 return region_alignment - sizeof(struct rmodule_header);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600236}
Aaron Durbinf545abf2013-10-24 10:14:06 -0500237
Aaron Durbin899d13d2015-05-15 23:39:23 -0500238int rmodule_stage_load(struct rmod_stage_load *rsl)
Aaron Durbinf545abf2013-10-24 10:14:06 -0500239{
240 struct rmodule rmod_stage;
241 size_t region_size;
242 char *stage_region;
243 int rmodule_offset;
244 int load_offset;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500245 struct cbfs_stage stage;
246 void *rmod_loc;
247 struct region_device *fh;
Aaron Durbinf545abf2013-10-24 10:14:06 -0500248
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500249 if (rsl->prog == NULL || prog_name(rsl->prog) == NULL)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500250 return -1;
251
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500252 fh = prog_rdev(rsl->prog);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500253
254 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
Aaron Durbinf545abf2013-10-24 10:14:06 -0500255 return -1;
256
257 rmodule_offset =
258 rmodule_calc_region(DYN_CBMEM_ALIGN_SIZE,
Lee Leahye20a3192017-03-09 16:21:34 -0800259 stage.memlen, &region_size, &load_offset);
Aaron Durbinf545abf2013-10-24 10:14:06 -0500260
Aaron Durbin7ca65222015-04-06 17:18:18 -0500261 stage_region = cbmem_add(rsl->cbmem_id, region_size);
Aaron Durbinf545abf2013-10-24 10:14:06 -0500262
Aaron Durbin7ca65222015-04-06 17:18:18 -0500263 if (stage_region == NULL)
Aaron Durbinf545abf2013-10-24 10:14:06 -0500264 return -1;
265
Aaron Durbin899d13d2015-05-15 23:39:23 -0500266 rmod_loc = &stage_region[rmodule_offset];
Aaron Durbinf545abf2013-10-24 10:14:06 -0500267
Julius Werner540a9802019-12-09 13:03:29 -0800268 printk(BIOS_INFO, "Decompressing stage %s @ %p (%d bytes)\n",
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500269 prog_name(rsl->prog), rmod_loc, stage.memlen);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500270
Julius Werner09f29212015-09-29 13:51:35 -0700271 if (!cbfs_load_and_decompress(fh, sizeof(stage), stage.len, rmod_loc,
272 stage.memlen, stage.compression))
Aaron Durbinf545abf2013-10-24 10:14:06 -0500273 return -1;
274
Aaron Durbin899d13d2015-05-15 23:39:23 -0500275 if (rmodule_parse(rmod_loc, &rmod_stage))
Aaron Durbinf545abf2013-10-24 10:14:06 -0500276 return -1;
277
278 if (rmodule_load(&stage_region[load_offset], &rmod_stage))
279 return -1;
280
Aaron Durbin7ca65222015-04-06 17:18:18 -0500281 prog_set_area(rsl->prog, rmod_stage.location,
282 rmodule_memory_size(&rmod_stage));
Aaron Durbinf545abf2013-10-24 10:14:06 -0500283
Aaron Durbin94271b42016-03-17 23:13:34 -0500284 /* Allow caller to pick up parameters, if available. */
285 rsl->params = rmodule_parameters(&rmod_stage);
286
Kyösti Mälkkid87e4b32017-09-05 22:43:05 +0300287 prog_set_entry(rsl->prog, rmodule_entry(&rmod_stage), rsl->params);
288
Aaron Durbinf545abf2013-10-24 10:14:06 -0500289 return 0;
290}