blob: d7dc969805749c916bfc1d6a1e106e40ca8c7d45 [file] [log] [blame]
Aaron Durbinad935522012-12-24 14:28:37 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2012 ChromeOS Authors
5 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
Aaron Durbindd4a6d22013-02-27 22:50:12 -060019#include <assert.h>
Kyösti Mälkkiae98e832014-11-28 11:24:19 +020020#include <cbmem.h>
Aaron Durbin67514a72015-03-26 21:04:18 -050021#include <cbfs.h>
Aaron Durbinad935522012-12-24 14:28:37 -060022#include <stdint.h>
Aaron Durbine8c866a2013-02-08 17:05:36 -060023#include <stdlib.h>
Aaron Durbinad935522012-12-24 14:28:37 -060024#include <string.h>
25#include <console/console.h>
Ionela Voinescu00903e52015-01-09 13:14:20 +000026#include <program_loading.h>
Aaron Durbinad935522012-12-24 14:28:37 -060027#include <rmodule.h>
28
29/* Change this define to get more verbose debugging for module loading. */
30#define PK_ADJ_LEVEL BIOS_NEVER
31
Aaron Durbinad935522012-12-24 14:28:37 -060032static inline int rmodule_is_loaded(const struct rmodule *module)
33{
34 return module->location != NULL;
35}
36
37/* Calculate a loaded program address based on the blob address. */
38static inline void *rmodule_load_addr(const struct rmodule *module,
Aaron Durbin3eb8eb72014-03-10 16:13:58 -050039 uintptr_t blob_addr)
Aaron Durbinad935522012-12-24 14:28:37 -060040{
41 char *loc = module->location;
42 return &loc[blob_addr - module->header->module_link_start_address];
43}
44
45/* Initialize a rmodule structure based on raw data. */
46int rmodule_parse(void *ptr, struct rmodule *module)
47{
48 char *base;
49 struct rmodule_header *rhdr;
50
51 base = ptr;
52 rhdr = ptr;
53
54 if (rhdr == NULL)
55 return -1;
56
57 /* Sanity check the raw data. */
58 if (rhdr->magic != RMODULE_MAGIC)
59 return -1;
60 if (rhdr->version != RMODULE_VERSION_1)
61 return -1;
62
63 /* Indicate the module hasn't been loaded yet. */
64 module->location = NULL;
65
66 /* The rmodule only needs a reference to the reloc_header. */
67 module->header = rhdr;
68
69 /* The payload lives after the header. */
70 module->payload = &base[rhdr->payload_begin_offset];
71 module->payload_size = rhdr->payload_end_offset -
72 rhdr->payload_begin_offset;
73 module->relocations = &base[rhdr->relocations_begin_offset];
74
75 return 0;
76}
77
78int rmodule_memory_size(const struct rmodule *module)
79{
80 return module->header->module_program_size;
81}
82
83void *rmodule_parameters(const struct rmodule *module)
84{
85 if (!rmodule_is_loaded(module))
86 return NULL;
87
88 /* Indicate if there are no parameters. */
89 if (module->header->parameters_begin == module->header->parameters_end)
90 return NULL;
91
92 return rmodule_load_addr(module, module->header->parameters_begin);
93}
94
95int rmodule_entry_offset(const struct rmodule *module)
96{
97 return module->header->module_entry_point -
98 module->header->module_link_start_address;
99}
100
101void *rmodule_entry(const struct rmodule *module)
102{
103 if (!rmodule_is_loaded(module))
104 return NULL;
105
106 return rmodule_load_addr(module, module->header->module_entry_point);
107}
108
109static void rmodule_clear_bss(struct rmodule *module)
110{
111 char *begin;
112 int size;
113
114 begin = rmodule_load_addr(module, module->header->bss_begin);
115 size = module->header->bss_end - module->header->bss_begin;
116 memset(begin, 0, size);
117}
118
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500119static inline size_t rmodule_number_relocations(const struct rmodule *module)
Aaron Durbinad935522012-12-24 14:28:37 -0600120{
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500121 size_t r;
Aaron Durbinad935522012-12-24 14:28:37 -0600122
123 r = module->header->relocations_end_offset;
124 r -= module->header->relocations_begin_offset;
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500125 r /= sizeof(uintptr_t);
Aaron Durbinad935522012-12-24 14:28:37 -0600126 return r;
127}
128
129static void rmodule_copy_payload(const struct rmodule *module)
130{
131 printk(BIOS_DEBUG, "Loading module at %p with entry %p. "
132 "filesize: 0x%x memsize: 0x%x\n",
133 module->location, rmodule_entry(module),
134 module->payload_size, rmodule_memory_size(module));
Aaron Durbine8c866a2013-02-08 17:05:36 -0600135
136 /* No need to copy the payload if the load location and the
137 * payload location are the same. */
138 if (module->location == module->payload)
139 return;
140
Aaron Durbinad935522012-12-24 14:28:37 -0600141 memcpy(module->location, module->payload, module->payload_size);
142}
143
Aaron Durbinad935522012-12-24 14:28:37 -0600144static int rmodule_relocate(const struct rmodule *module)
145{
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500146 size_t num_relocations;
147 const uintptr_t *reloc;
148 uintptr_t adjustment;
Aaron Durbinad935522012-12-24 14:28:37 -0600149
150 /* Each relocation needs to be adjusted relative to the beginning of
151 * the loaded program. */
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500152 adjustment = (uintptr_t)rmodule_load_addr(module, 0);
Aaron Durbinad935522012-12-24 14:28:37 -0600153
154 reloc = module->relocations;
155 num_relocations = rmodule_number_relocations(module);
156
Furquan Shaikh7a3c3492014-07-22 10:59:28 -0700157 printk(BIOS_DEBUG, "Processing %zu relocs. Offset value of 0x%08lx\n",
158 num_relocations, (unsigned long)adjustment);
Aaron Durbinad935522012-12-24 14:28:37 -0600159
160 while (num_relocations > 0) {
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500161 uintptr_t *adjust_loc;
Aaron Durbinad935522012-12-24 14:28:37 -0600162
163 /* If the adjustment location is non-NULL adjust it. */
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500164 adjust_loc = rmodule_load_addr(module, *reloc);
Furquan Shaikh7a3c3492014-07-22 10:59:28 -0700165 printk(PK_ADJ_LEVEL, "Adjusting %p: 0x%08lx -> 0x%08lx\n",
166 adjust_loc, (unsigned long) *adjust_loc,
Furquan Shaikhae9cd012014-07-23 04:44:09 -0700167 (unsigned long) (*adjust_loc + adjustment));
Aaron Durbinad935522012-12-24 14:28:37 -0600168 *adjust_loc += adjustment;
Aaron Durbinad935522012-12-24 14:28:37 -0600169
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500170 reloc++;
Aaron Durbinad935522012-12-24 14:28:37 -0600171 num_relocations--;
172 }
173
174 return 0;
175}
176
177int rmodule_load_alignment(const struct rmodule *module)
178{
179 /* The load alignment is the start of the program's linked address.
180 * The base address where the program is loaded needs to be a multiple
181 * of the program's starting link address. That way all data alignment
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500182 * in the program is preserved. Default to 4KiB. */
183 return 4096;
Aaron Durbinad935522012-12-24 14:28:37 -0600184}
185
186int rmodule_load(void *base, struct rmodule *module)
187{
188 /*
189 * In order to load the module at a given address, the following steps
190 * take place:
191 * 1. Copy payload to base address.
Aaron Durbin55ed3102013-03-01 17:00:39 -0600192 * 2. Adjust relocations within the module to new base address.
193 * 3. Clear the bss segment last since the relocations live where
194 * the bss is. If an rmodule is being loaded from its load
195 * address the relocations need to be processed before the bss.
Aaron Durbinad935522012-12-24 14:28:37 -0600196 */
197 module->location = base;
198 rmodule_copy_payload(module);
Aaron Durbin55ed3102013-03-01 17:00:39 -0600199 if (rmodule_relocate(module))
200 return -1;
Aaron Durbinad935522012-12-24 14:28:37 -0600201 rmodule_clear_bss(module);
Aaron Durbinf72f9e72014-03-25 15:31:00 -0500202
Aaron Durbin6e76fff2015-03-20 09:42:05 -0500203 arch_segment_loaded((uintptr_t)module->location,
204 rmodule_memory_size(module), SEG_FINAL);
Aaron Durbinf72f9e72014-03-25 15:31:00 -0500205
Aaron Durbin55ed3102013-03-01 17:00:39 -0600206 return 0;
Aaron Durbinad935522012-12-24 14:28:37 -0600207}
208
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600209int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size,
210 size_t *region_size, int *load_offset)
Aaron Durbine8c866a2013-02-08 17:05:36 -0600211{
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600212 /* region_alignment must be a power of 2. */
213 if (region_alignment & (region_alignment - 1))
214 BUG();
Aaron Durbine8c866a2013-02-08 17:05:36 -0600215
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600216 if (region_alignment < 4096)
217 region_alignment = 4096;
218
219 /* Sanity check rmodule_header size. The code below assumes it is less
220 * than the minimum alignment required. */
221 if (region_alignment < sizeof(struct rmodule_header))
222 BUG();
223
224 /* Place the rmodule according to alignment. The rmodule files
Aaron Durbine8c866a2013-02-08 17:05:36 -0600225 * themselves are packed as a header and a payload, however the rmodule
226 * itself is linked along with the header. The header starts at address
227 * 0. Immediately following the header in the file is the program,
228 * however its starting address is determined by the rmodule linker
229 * script. In short, sizeof(struct rmodule_header) can be less than
230 * or equal to the linked address of the program. Therefore we want
231 * to place the rmodule so that the program falls on the aligned
232 * address with the header just before it. Therefore, we need at least
233 * a page to account for the size of the header. */
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600234 *region_size = ALIGN(rmodule_size + region_alignment, 4096);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600235 /* The program starts immediately after the header. However,
236 * it needs to be aligned to a 4KiB boundary. Therefore, adjust the
237 * program location so that the program lands on a page boundary. The
238 * layout looks like the following:
239 *
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600240 * +--------------------------------+ region_alignment + region_size
Aaron Durbine8c866a2013-02-08 17:05:36 -0600241 * | >= 0 bytes from alignment |
242 * +--------------------------------+ program end (4KiB aligned)
243 * | program size |
244 * +--------------------------------+ program_begin (4KiB aligned)
245 * | sizeof(struct rmodule_header) |
246 * +--------------------------------+ rmodule header start
247 * | >= 0 bytes from alignment |
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600248 * +--------------------------------+ region_alignment
Aaron Durbine8c866a2013-02-08 17:05:36 -0600249 */
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600250 *load_offset = region_alignment;
Aaron Durbine8c866a2013-02-08 17:05:36 -0600251
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600252 return region_alignment - sizeof(struct rmodule_header);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600253}
Aaron Durbinf545abf2013-10-24 10:14:06 -0500254
Aaron Durbinf545abf2013-10-24 10:14:06 -0500255int rmodule_stage_load(struct rmod_stage_load *rsl, struct cbfs_stage *stage)
256{
257 struct rmodule rmod_stage;
258 size_t region_size;
259 char *stage_region;
260 int rmodule_offset;
261 int load_offset;
262 const struct cbmem_entry *cbmem_entry;
263
Aaron Durbin460703b2015-03-27 21:17:22 -0500264 if (stage == NULL || rsl->prog == NULL || rsl->prog->name == NULL)
Aaron Durbinf545abf2013-10-24 10:14:06 -0500265 return -1;
266
267 rmodule_offset =
268 rmodule_calc_region(DYN_CBMEM_ALIGN_SIZE,
269 stage->memlen, &region_size, &load_offset);
270
271 cbmem_entry = cbmem_entry_add(rsl->cbmem_id, region_size);
272
273 if (cbmem_entry == NULL)
274 return -1;
275
276 stage_region = cbmem_entry_start(cbmem_entry);
277
278 printk(BIOS_INFO, "Decompressing stage %s @ 0x%p (%d bytes)\n",
Aaron Durbin460703b2015-03-27 21:17:22 -0500279 rsl->prog->name, &stage_region[rmodule_offset], stage->memlen);
Aaron Durbinf545abf2013-10-24 10:14:06 -0500280
281 if (!cbfs_decompress(stage->compression, &stage[1],
282 &stage_region[rmodule_offset], stage->len))
283 return -1;
284
285 if (rmodule_parse(&stage_region[rmodule_offset], &rmod_stage))
286 return -1;
287
288 if (rmodule_load(&stage_region[load_offset], &rmod_stage))
289 return -1;
290
Aaron Durbin460703b2015-03-27 21:17:22 -0500291 prog_set_area(rsl->prog, stage_region, cbmem_entry_size(cbmem_entry));
292 prog_set_entry(rsl->prog, rmodule_entry(&rmod_stage), NULL);
Aaron Durbinf545abf2013-10-24 10:14:06 -0500293
294 return 0;
295}
296
297int rmodule_stage_load_from_cbfs(struct rmod_stage_load *rsl)
298{
299 struct cbfs_stage *stage;
300
301 stage = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
Aaron Durbin460703b2015-03-27 21:17:22 -0500302 rsl->prog->name, CBFS_TYPE_STAGE, NULL);
Aaron Durbinf545abf2013-10-24 10:14:06 -0500303
304 if (stage == NULL)
305 return -1;
306
307 return rmodule_stage_load(rsl, stage);
308}