blob: 908297ba0416d8f88f0aa8830a00f0c68573a762 [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>
Aaron Durbinad935522012-12-24 14:28:37 -060020#include <stdint.h>
Aaron Durbine8c866a2013-02-08 17:05:36 -060021#include <stdlib.h>
Aaron Durbinad935522012-12-24 14:28:37 -060022#include <string.h>
Aaron Durbinf72f9e72014-03-25 15:31:00 -050023#include <arch/cache.h>
Aaron Durbinad935522012-12-24 14:28:37 -060024#include <console/console.h>
25#include <rmodule.h>
26
27/* Change this define to get more verbose debugging for module loading. */
28#define PK_ADJ_LEVEL BIOS_NEVER
29
Aaron Durbinad935522012-12-24 14:28:37 -060030static inline int rmodule_is_loaded(const struct rmodule *module)
31{
32 return module->location != NULL;
33}
34
35/* Calculate a loaded program address based on the blob address. */
36static inline void *rmodule_load_addr(const struct rmodule *module,
Aaron Durbin3eb8eb72014-03-10 16:13:58 -050037 uintptr_t blob_addr)
Aaron Durbinad935522012-12-24 14:28:37 -060038{
39 char *loc = module->location;
40 return &loc[blob_addr - module->header->module_link_start_address];
41}
42
43/* Initialize a rmodule structure based on raw data. */
44int rmodule_parse(void *ptr, struct rmodule *module)
45{
46 char *base;
47 struct rmodule_header *rhdr;
48
49 base = ptr;
50 rhdr = ptr;
51
52 if (rhdr == NULL)
53 return -1;
54
55 /* Sanity check the raw data. */
56 if (rhdr->magic != RMODULE_MAGIC)
57 return -1;
58 if (rhdr->version != RMODULE_VERSION_1)
59 return -1;
60
61 /* Indicate the module hasn't been loaded yet. */
62 module->location = NULL;
63
64 /* The rmodule only needs a reference to the reloc_header. */
65 module->header = rhdr;
66
67 /* The payload lives after the header. */
68 module->payload = &base[rhdr->payload_begin_offset];
69 module->payload_size = rhdr->payload_end_offset -
70 rhdr->payload_begin_offset;
71 module->relocations = &base[rhdr->relocations_begin_offset];
72
73 return 0;
74}
75
76int rmodule_memory_size(const struct rmodule *module)
77{
78 return module->header->module_program_size;
79}
80
81void *rmodule_parameters(const struct rmodule *module)
82{
83 if (!rmodule_is_loaded(module))
84 return NULL;
85
86 /* Indicate if there are no parameters. */
87 if (module->header->parameters_begin == module->header->parameters_end)
88 return NULL;
89
90 return rmodule_load_addr(module, module->header->parameters_begin);
91}
92
93int rmodule_entry_offset(const struct rmodule *module)
94{
95 return module->header->module_entry_point -
96 module->header->module_link_start_address;
97}
98
99void *rmodule_entry(const struct rmodule *module)
100{
101 if (!rmodule_is_loaded(module))
102 return NULL;
103
104 return rmodule_load_addr(module, module->header->module_entry_point);
105}
106
107static void rmodule_clear_bss(struct rmodule *module)
108{
109 char *begin;
110 int size;
111
112 begin = rmodule_load_addr(module, module->header->bss_begin);
113 size = module->header->bss_end - module->header->bss_begin;
114 memset(begin, 0, size);
115}
116
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500117static inline size_t rmodule_number_relocations(const struct rmodule *module)
Aaron Durbinad935522012-12-24 14:28:37 -0600118{
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500119 size_t r;
Aaron Durbinad935522012-12-24 14:28:37 -0600120
121 r = module->header->relocations_end_offset;
122 r -= module->header->relocations_begin_offset;
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500123 r /= sizeof(uintptr_t);
Aaron Durbinad935522012-12-24 14:28:37 -0600124 return r;
125}
126
127static void rmodule_copy_payload(const struct rmodule *module)
128{
129 printk(BIOS_DEBUG, "Loading module at %p with entry %p. "
130 "filesize: 0x%x memsize: 0x%x\n",
131 module->location, rmodule_entry(module),
132 module->payload_size, rmodule_memory_size(module));
Aaron Durbine8c866a2013-02-08 17:05:36 -0600133
134 /* No need to copy the payload if the load location and the
135 * payload location are the same. */
136 if (module->location == module->payload)
137 return;
138
Aaron Durbinad935522012-12-24 14:28:37 -0600139 memcpy(module->location, module->payload, module->payload_size);
140}
141
Aaron Durbinad935522012-12-24 14:28:37 -0600142static int rmodule_relocate(const struct rmodule *module)
143{
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500144 size_t num_relocations;
145 const uintptr_t *reloc;
146 uintptr_t adjustment;
Aaron Durbinad935522012-12-24 14:28:37 -0600147
148 /* Each relocation needs to be adjusted relative to the beginning of
149 * the loaded program. */
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500150 adjustment = (uintptr_t)rmodule_load_addr(module, 0);
Aaron Durbinad935522012-12-24 14:28:37 -0600151
152 reloc = module->relocations;
153 num_relocations = rmodule_number_relocations(module);
154
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500155 printk(BIOS_DEBUG, "Processing %zu relocs. Offset value of 0x%08x\n",
Aaron Durbinad935522012-12-24 14:28:37 -0600156 num_relocations, adjustment);
157
158 while (num_relocations > 0) {
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500159 uintptr_t *adjust_loc;
Aaron Durbinad935522012-12-24 14:28:37 -0600160
161 /* If the adjustment location is non-NULL adjust it. */
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500162 adjust_loc = rmodule_load_addr(module, *reloc);
163 printk(PK_ADJ_LEVEL, "Adjusting %p: 0x%08x -> 0x%08x\n",
Aaron Durbinad935522012-12-24 14:28:37 -0600164 adjust_loc, *adjust_loc,
165 *adjust_loc + adjustment);
166 *adjust_loc += adjustment;
Aaron Durbinad935522012-12-24 14:28:37 -0600167
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500168 reloc++;
Aaron Durbinad935522012-12-24 14:28:37 -0600169 num_relocations--;
170 }
171
172 return 0;
173}
174
175int rmodule_load_alignment(const struct rmodule *module)
176{
177 /* The load alignment is the start of the program's linked address.
178 * The base address where the program is loaded needs to be a multiple
179 * of the program's starting link address. That way all data alignment
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500180 * in the program is preserved. Default to 4KiB. */
181 return 4096;
Aaron Durbinad935522012-12-24 14:28:37 -0600182}
183
184int rmodule_load(void *base, struct rmodule *module)
185{
186 /*
187 * In order to load the module at a given address, the following steps
188 * take place:
189 * 1. Copy payload to base address.
Aaron Durbin55ed3102013-03-01 17:00:39 -0600190 * 2. Adjust relocations within the module to new base address.
191 * 3. Clear the bss segment last since the relocations live where
192 * the bss is. If an rmodule is being loaded from its load
193 * address the relocations need to be processed before the bss.
Aaron Durbinad935522012-12-24 14:28:37 -0600194 */
195 module->location = base;
196 rmodule_copy_payload(module);
Aaron Durbin55ed3102013-03-01 17:00:39 -0600197 if (rmodule_relocate(module))
198 return -1;
Aaron Durbinad935522012-12-24 14:28:37 -0600199 rmodule_clear_bss(module);
Aaron Durbinf72f9e72014-03-25 15:31:00 -0500200
201 cache_sync_instructions();
202
Aaron Durbin55ed3102013-03-01 17:00:39 -0600203 return 0;
Aaron Durbinad935522012-12-24 14:28:37 -0600204}
205
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600206int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size,
207 size_t *region_size, int *load_offset)
Aaron Durbine8c866a2013-02-08 17:05:36 -0600208{
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600209 /* region_alignment must be a power of 2. */
210 if (region_alignment & (region_alignment - 1))
211 BUG();
Aaron Durbine8c866a2013-02-08 17:05:36 -0600212
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600213 if (region_alignment < 4096)
214 region_alignment = 4096;
215
216 /* Sanity check rmodule_header size. The code below assumes it is less
217 * than the minimum alignment required. */
218 if (region_alignment < sizeof(struct rmodule_header))
219 BUG();
220
221 /* Place the rmodule according to alignment. The rmodule files
Aaron Durbine8c866a2013-02-08 17:05:36 -0600222 * themselves are packed as a header and a payload, however the rmodule
223 * itself is linked along with the header. The header starts at address
224 * 0. Immediately following the header in the file is the program,
225 * however its starting address is determined by the rmodule linker
226 * script. In short, sizeof(struct rmodule_header) can be less than
227 * or equal to the linked address of the program. Therefore we want
228 * to place the rmodule so that the program falls on the aligned
229 * address with the header just before it. Therefore, we need at least
230 * a page to account for the size of the header. */
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600231 *region_size = ALIGN(rmodule_size + region_alignment, 4096);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600232 /* The program starts immediately after the header. However,
233 * it needs to be aligned to a 4KiB boundary. Therefore, adjust the
234 * program location so that the program lands on a page boundary. The
235 * layout looks like the following:
236 *
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600237 * +--------------------------------+ region_alignment + region_size
Aaron Durbine8c866a2013-02-08 17:05:36 -0600238 * | >= 0 bytes from alignment |
239 * +--------------------------------+ program end (4KiB aligned)
240 * | program size |
241 * +--------------------------------+ program_begin (4KiB aligned)
242 * | sizeof(struct rmodule_header) |
243 * +--------------------------------+ rmodule header start
244 * | >= 0 bytes from alignment |
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600245 * +--------------------------------+ region_alignment
Aaron Durbine8c866a2013-02-08 17:05:36 -0600246 */
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600247 *load_offset = region_alignment;
Aaron Durbine8c866a2013-02-08 17:05:36 -0600248
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600249 return region_alignment - sizeof(struct rmodule_header);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600250}
Aaron Durbinf545abf2013-10-24 10:14:06 -0500251
252#if CONFIG_DYNAMIC_CBMEM
253#include <cbmem.h>
254#include <cbfs_core.h>
255
256int rmodule_stage_load(struct rmod_stage_load *rsl, struct cbfs_stage *stage)
257{
258 struct rmodule rmod_stage;
259 size_t region_size;
260 char *stage_region;
261 int rmodule_offset;
262 int load_offset;
263 const struct cbmem_entry *cbmem_entry;
264
265 if (stage == NULL || rsl->name == NULL)
266 return -1;
267
268 rmodule_offset =
269 rmodule_calc_region(DYN_CBMEM_ALIGN_SIZE,
270 stage->memlen, &region_size, &load_offset);
271
272 cbmem_entry = cbmem_entry_add(rsl->cbmem_id, region_size);
273
274 if (cbmem_entry == NULL)
275 return -1;
276
277 stage_region = cbmem_entry_start(cbmem_entry);
278
279 printk(BIOS_INFO, "Decompressing stage %s @ 0x%p (%d bytes)\n",
280 rsl->name, &stage_region[rmodule_offset], stage->memlen);
281
282 if (!cbfs_decompress(stage->compression, &stage[1],
283 &stage_region[rmodule_offset], stage->len))
284 return -1;
285
286 if (rmodule_parse(&stage_region[rmodule_offset], &rmod_stage))
287 return -1;
288
289 if (rmodule_load(&stage_region[load_offset], &rmod_stage))
290 return -1;
291
292 rsl->cbmem_entry = cbmem_entry;
293 rsl->entry = rmodule_entry(&rmod_stage);
294
295 return 0;
296}
297
298int rmodule_stage_load_from_cbfs(struct rmod_stage_load *rsl)
299{
300 struct cbfs_stage *stage;
301
302 stage = cbfs_get_file_content(CBFS_DEFAULT_MEDIA,
303 rsl->name, CBFS_TYPE_STAGE, NULL);
304
305 if (stage == NULL)
306 return -1;
307
308 return rmodule_stage_load(rsl, stage);
309}
310
311#endif /* DYNAMIC_CBMEM */