blob: 56529d2fb24d69c78ea05017662eca9c3306d950 [file] [log] [blame]
Aaron Durbinad935522012-12-24 14:28:37 -06001/*
2 * This file is part of the coreboot project.
3 *
Patrick Georgi5b2a2d02018-09-26 20:46:04 +02004 * Copyright (C) 2012 Google LLC
Aaron Durbinad935522012-12-24 14:28:37 -06005 *
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 Durbinad935522012-12-24 14:28:37 -060014 */
Aaron Durbindd4a6d22013-02-27 22:50:12 -060015#include <assert.h>
Kyösti Mälkkiae98e832014-11-28 11:24:19 +020016#include <cbmem.h>
Aaron Durbin67514a72015-03-26 21:04:18 -050017#include <cbfs.h>
Aaron Durbinad935522012-12-24 14:28:37 -060018#include <stdint.h>
Aaron Durbine8c866a2013-02-08 17:05:36 -060019#include <stdlib.h>
Aaron Durbinad935522012-12-24 14:28:37 -060020#include <string.h>
21#include <console/console.h>
Ionela Voinescu00903e52015-01-09 13:14:20 +000022#include <program_loading.h>
Aaron Durbinad935522012-12-24 14:28:37 -060023#include <rmodule.h>
24
25/* Change this define to get more verbose debugging for module loading. */
26#define PK_ADJ_LEVEL BIOS_NEVER
27
Aaron Durbinad935522012-12-24 14:28:37 -060028static inline int rmodule_is_loaded(const struct rmodule *module)
29{
30 return module->location != NULL;
31}
32
33/* Calculate a loaded program address based on the blob address. */
34static inline void *rmodule_load_addr(const struct rmodule *module,
Lee Leahye20a3192017-03-09 16:21:34 -080035 uintptr_t blob_addr)
Aaron Durbinad935522012-12-24 14:28:37 -060036{
37 char *loc = module->location;
38 return &loc[blob_addr - module->header->module_link_start_address];
39}
40
41/* Initialize a rmodule structure based on raw data. */
42int rmodule_parse(void *ptr, struct rmodule *module)
43{
44 char *base;
45 struct rmodule_header *rhdr;
46
47 base = ptr;
48 rhdr = ptr;
49
50 if (rhdr == NULL)
51 return -1;
52
53 /* Sanity check the raw data. */
54 if (rhdr->magic != RMODULE_MAGIC)
55 return -1;
56 if (rhdr->version != RMODULE_VERSION_1)
57 return -1;
58
59 /* Indicate the module hasn't been loaded yet. */
60 module->location = NULL;
61
62 /* The rmodule only needs a reference to the reloc_header. */
63 module->header = rhdr;
64
65 /* The payload lives after the header. */
66 module->payload = &base[rhdr->payload_begin_offset];
67 module->payload_size = rhdr->payload_end_offset -
Lee Leahye20a3192017-03-09 16:21:34 -080068 rhdr->payload_begin_offset;
Aaron Durbinad935522012-12-24 14:28:37 -060069 module->relocations = &base[rhdr->relocations_begin_offset];
70
71 return 0;
72}
73
74int rmodule_memory_size(const struct rmodule *module)
75{
76 return module->header->module_program_size;
77}
78
79void *rmodule_parameters(const struct rmodule *module)
80{
81 if (!rmodule_is_loaded(module))
82 return NULL;
83
84 /* Indicate if there are no parameters. */
85 if (module->header->parameters_begin == module->header->parameters_end)
86 return NULL;
87
88 return rmodule_load_addr(module, module->header->parameters_begin);
89}
90
91int rmodule_entry_offset(const struct rmodule *module)
92{
93 return module->header->module_entry_point -
94 module->header->module_link_start_address;
95}
96
97void *rmodule_entry(const struct rmodule *module)
98{
99 if (!rmodule_is_loaded(module))
100 return NULL;
101
102 return rmodule_load_addr(module, module->header->module_entry_point);
103}
104
105static void rmodule_clear_bss(struct rmodule *module)
106{
107 char *begin;
108 int size;
109
110 begin = rmodule_load_addr(module, module->header->bss_begin);
111 size = module->header->bss_end - module->header->bss_begin;
112 memset(begin, 0, size);
113}
114
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500115static inline size_t rmodule_number_relocations(const struct rmodule *module)
Aaron Durbinad935522012-12-24 14:28:37 -0600116{
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500117 size_t r;
Aaron Durbinad935522012-12-24 14:28:37 -0600118
119 r = module->header->relocations_end_offset;
120 r -= module->header->relocations_begin_offset;
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500121 r /= sizeof(uintptr_t);
Aaron Durbinad935522012-12-24 14:28:37 -0600122 return r;
123}
124
125static void rmodule_copy_payload(const struct rmodule *module)
126{
127 printk(BIOS_DEBUG, "Loading module at %p with entry %p. "
128 "filesize: 0x%x memsize: 0x%x\n",
129 module->location, rmodule_entry(module),
130 module->payload_size, rmodule_memory_size(module));
Aaron Durbine8c866a2013-02-08 17:05:36 -0600131
132 /* No need to copy the payload if the load location and the
133 * payload location are the same. */
134 if (module->location == module->payload)
135 return;
136
Aaron Durbinad935522012-12-24 14:28:37 -0600137 memcpy(module->location, module->payload, module->payload_size);
138}
139
Aaron Durbinad935522012-12-24 14:28:37 -0600140static int rmodule_relocate(const struct rmodule *module)
141{
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500142 size_t num_relocations;
143 const uintptr_t *reloc;
144 uintptr_t adjustment;
Aaron Durbinad935522012-12-24 14:28:37 -0600145
146 /* Each relocation needs to be adjusted relative to the beginning of
147 * the loaded program. */
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500148 adjustment = (uintptr_t)rmodule_load_addr(module, 0);
Aaron Durbinad935522012-12-24 14:28:37 -0600149
150 reloc = module->relocations;
151 num_relocations = rmodule_number_relocations(module);
152
Furquan Shaikh7a3c3492014-07-22 10:59:28 -0700153 printk(BIOS_DEBUG, "Processing %zu relocs. Offset value of 0x%08lx\n",
154 num_relocations, (unsigned long)adjustment);
Aaron Durbinad935522012-12-24 14:28:37 -0600155
156 while (num_relocations > 0) {
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500157 uintptr_t *adjust_loc;
Aaron Durbinad935522012-12-24 14:28:37 -0600158
159 /* If the adjustment location is non-NULL adjust it. */
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500160 adjust_loc = rmodule_load_addr(module, *reloc);
Furquan Shaikh7a3c3492014-07-22 10:59:28 -0700161 printk(PK_ADJ_LEVEL, "Adjusting %p: 0x%08lx -> 0x%08lx\n",
162 adjust_loc, (unsigned long) *adjust_loc,
Furquan Shaikhae9cd012014-07-23 04:44:09 -0700163 (unsigned long) (*adjust_loc + adjustment));
Kyösti Mälkkic3bc6cb2018-06-25 18:51:05 +0300164 *adjust_loc += adjustment;
Aaron Durbinad935522012-12-24 14:28:37 -0600165
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500166 reloc++;
Aaron Durbinad935522012-12-24 14:28:37 -0600167 num_relocations--;
168 }
169
170 return 0;
171}
172
173int rmodule_load_alignment(const struct rmodule *module)
174{
175 /* The load alignment is the start of the program's linked address.
176 * The base address where the program is loaded needs to be a multiple
177 * of the program's starting link address. That way all data alignment
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500178 * in the program is preserved. Default to 4KiB. */
179 return 4096;
Aaron Durbinad935522012-12-24 14:28:37 -0600180}
181
182int rmodule_load(void *base, struct rmodule *module)
183{
184 /*
185 * In order to load the module at a given address, the following steps
186 * take place:
187 * 1. Copy payload to base address.
Aaron Durbin55ed3102013-03-01 17:00:39 -0600188 * 2. Adjust relocations within the module to new base address.
189 * 3. Clear the bss segment last since the relocations live where
190 * the bss is. If an rmodule is being loaded from its load
191 * address the relocations need to be processed before the bss.
Aaron Durbinad935522012-12-24 14:28:37 -0600192 */
193 module->location = base;
194 rmodule_copy_payload(module);
Aaron Durbin55ed3102013-03-01 17:00:39 -0600195 if (rmodule_relocate(module))
196 return -1;
Aaron Durbinad935522012-12-24 14:28:37 -0600197 rmodule_clear_bss(module);
Aaron Durbinf72f9e72014-03-25 15:31:00 -0500198
Aaron Durbin096f4572016-03-31 13:49:00 -0500199 prog_segment_loaded((uintptr_t)module->location,
Aaron Durbin6e76fff2015-03-20 09:42:05 -0500200 rmodule_memory_size(module), SEG_FINAL);
Aaron Durbinf72f9e72014-03-25 15:31:00 -0500201
Aaron Durbin55ed3102013-03-01 17:00:39 -0600202 return 0;
Aaron Durbinad935522012-12-24 14:28:37 -0600203}
204
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600205int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size,
Lee Leahye20a3192017-03-09 16:21:34 -0800206 size_t *region_size, int *load_offset)
Aaron Durbine8c866a2013-02-08 17:05:36 -0600207{
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600208 /* region_alignment must be a power of 2. */
209 if (region_alignment & (region_alignment - 1))
210 BUG();
Aaron Durbine8c866a2013-02-08 17:05:36 -0600211
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600212 if (region_alignment < 4096)
213 region_alignment = 4096;
214
215 /* Sanity check rmodule_header size. The code below assumes it is less
216 * than the minimum alignment required. */
217 if (region_alignment < sizeof(struct rmodule_header))
218 BUG();
219
220 /* Place the rmodule according to alignment. The rmodule files
Aaron Durbine8c866a2013-02-08 17:05:36 -0600221 * themselves are packed as a header and a payload, however the rmodule
222 * itself is linked along with the header. The header starts at address
223 * 0. Immediately following the header in the file is the program,
224 * however its starting address is determined by the rmodule linker
225 * script. In short, sizeof(struct rmodule_header) can be less than
226 * or equal to the linked address of the program. Therefore we want
227 * to place the rmodule so that the program falls on the aligned
228 * address with the header just before it. Therefore, we need at least
229 * a page to account for the size of the header. */
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600230 *region_size = ALIGN(rmodule_size + region_alignment, 4096);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600231 /* The program starts immediately after the header. However,
232 * it needs to be aligned to a 4KiB boundary. Therefore, adjust the
233 * program location so that the program lands on a page boundary. The
234 * layout looks like the following:
235 *
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600236 * +--------------------------------+ region_alignment + region_size
Aaron Durbine8c866a2013-02-08 17:05:36 -0600237 * | >= 0 bytes from alignment |
238 * +--------------------------------+ program end (4KiB aligned)
239 * | program size |
240 * +--------------------------------+ program_begin (4KiB aligned)
241 * | sizeof(struct rmodule_header) |
242 * +--------------------------------+ rmodule header start
243 * | >= 0 bytes from alignment |
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600244 * +--------------------------------+ region_alignment
Aaron Durbine8c866a2013-02-08 17:05:36 -0600245 */
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600246 *load_offset = region_alignment;
Aaron Durbine8c866a2013-02-08 17:05:36 -0600247
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600248 return region_alignment - sizeof(struct rmodule_header);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600249}
Aaron Durbinf545abf2013-10-24 10:14:06 -0500250
Aaron Durbin899d13d2015-05-15 23:39:23 -0500251int rmodule_stage_load(struct rmod_stage_load *rsl)
Aaron Durbinf545abf2013-10-24 10:14:06 -0500252{
253 struct rmodule rmod_stage;
254 size_t region_size;
255 char *stage_region;
256 int rmodule_offset;
257 int load_offset;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500258 struct cbfs_stage stage;
259 void *rmod_loc;
260 struct region_device *fh;
Aaron Durbinf545abf2013-10-24 10:14:06 -0500261
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500262 if (rsl->prog == NULL || prog_name(rsl->prog) == NULL)
Aaron Durbin899d13d2015-05-15 23:39:23 -0500263 return -1;
264
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500265 fh = prog_rdev(rsl->prog);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500266
267 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
Aaron Durbinf545abf2013-10-24 10:14:06 -0500268 return -1;
269
270 rmodule_offset =
271 rmodule_calc_region(DYN_CBMEM_ALIGN_SIZE,
Lee Leahye20a3192017-03-09 16:21:34 -0800272 stage.memlen, &region_size, &load_offset);
Aaron Durbinf545abf2013-10-24 10:14:06 -0500273
Aaron Durbin7ca65222015-04-06 17:18:18 -0500274 stage_region = cbmem_add(rsl->cbmem_id, region_size);
Aaron Durbinf545abf2013-10-24 10:14:06 -0500275
Aaron Durbin7ca65222015-04-06 17:18:18 -0500276 if (stage_region == NULL)
Aaron Durbinf545abf2013-10-24 10:14:06 -0500277 return -1;
278
Aaron Durbin899d13d2015-05-15 23:39:23 -0500279 rmod_loc = &stage_region[rmodule_offset];
Aaron Durbinf545abf2013-10-24 10:14:06 -0500280
Aaron Durbin899d13d2015-05-15 23:39:23 -0500281 printk(BIOS_INFO, "Decompressing stage %s @ 0x%p (%d bytes)\n",
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500282 prog_name(rsl->prog), rmod_loc, stage.memlen);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500283
Julius Werner09f29212015-09-29 13:51:35 -0700284 if (!cbfs_load_and_decompress(fh, sizeof(stage), stage.len, rmod_loc,
285 stage.memlen, stage.compression))
Aaron Durbinf545abf2013-10-24 10:14:06 -0500286 return -1;
287
Aaron Durbin899d13d2015-05-15 23:39:23 -0500288 if (rmodule_parse(rmod_loc, &rmod_stage))
Aaron Durbinf545abf2013-10-24 10:14:06 -0500289 return -1;
290
291 if (rmodule_load(&stage_region[load_offset], &rmod_stage))
292 return -1;
293
Aaron Durbin7ca65222015-04-06 17:18:18 -0500294 prog_set_area(rsl->prog, rmod_stage.location,
295 rmodule_memory_size(&rmod_stage));
Aaron Durbinf545abf2013-10-24 10:14:06 -0500296
Aaron Durbin94271b42016-03-17 23:13:34 -0500297 /* Allow caller to pick up parameters, if available. */
298 rsl->params = rmodule_parameters(&rmod_stage);
299
Kyösti Mälkkid87e4b32017-09-05 22:43:05 +0300300 prog_set_entry(rsl->prog, rmodule_entry(&rmod_stage), rsl->params);
301
Aaron Durbinf545abf2013-10-24 10:14:06 -0500302 return 0;
303}