blob: 35893d9d96a5e62ba3762541e0d84dafaed5eca3 [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
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Aaron Durbinad935522012-12-24 14:28:37 -060018 */
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>
Aaron Durbin899d13d2015-05-15 23:39:23 -050025#include <lib.h>
Aaron Durbinad935522012-12-24 14:28:37 -060026#include <console/console.h>
Ionela Voinescu00903e52015-01-09 13:14:20 +000027#include <program_loading.h>
Aaron Durbinad935522012-12-24 14:28:37 -060028#include <rmodule.h>
29
30/* Change this define to get more verbose debugging for module loading. */
31#define PK_ADJ_LEVEL BIOS_NEVER
32
Aaron Durbinad935522012-12-24 14:28:37 -060033static inline int rmodule_is_loaded(const struct rmodule *module)
34{
35 return module->location != NULL;
36}
37
38/* Calculate a loaded program address based on the blob address. */
39static inline void *rmodule_load_addr(const struct rmodule *module,
Aaron Durbin3eb8eb72014-03-10 16:13:58 -050040 uintptr_t blob_addr)
Aaron Durbinad935522012-12-24 14:28:37 -060041{
42 char *loc = module->location;
43 return &loc[blob_addr - module->header->module_link_start_address];
44}
45
46/* Initialize a rmodule structure based on raw data. */
47int rmodule_parse(void *ptr, struct rmodule *module)
48{
49 char *base;
50 struct rmodule_header *rhdr;
51
52 base = ptr;
53 rhdr = ptr;
54
55 if (rhdr == NULL)
56 return -1;
57
58 /* Sanity check the raw data. */
59 if (rhdr->magic != RMODULE_MAGIC)
60 return -1;
61 if (rhdr->version != RMODULE_VERSION_1)
62 return -1;
63
64 /* Indicate the module hasn't been loaded yet. */
65 module->location = NULL;
66
67 /* The rmodule only needs a reference to the reloc_header. */
68 module->header = rhdr;
69
70 /* The payload lives after the header. */
71 module->payload = &base[rhdr->payload_begin_offset];
72 module->payload_size = rhdr->payload_end_offset -
73 rhdr->payload_begin_offset;
74 module->relocations = &base[rhdr->relocations_begin_offset];
75
76 return 0;
77}
78
79int rmodule_memory_size(const struct rmodule *module)
80{
81 return module->header->module_program_size;
82}
83
84void *rmodule_parameters(const struct rmodule *module)
85{
86 if (!rmodule_is_loaded(module))
87 return NULL;
88
89 /* Indicate if there are no parameters. */
90 if (module->header->parameters_begin == module->header->parameters_end)
91 return NULL;
92
93 return rmodule_load_addr(module, module->header->parameters_begin);
94}
95
96int rmodule_entry_offset(const struct rmodule *module)
97{
98 return module->header->module_entry_point -
99 module->header->module_link_start_address;
100}
101
102void *rmodule_entry(const struct rmodule *module)
103{
104 if (!rmodule_is_loaded(module))
105 return NULL;
106
107 return rmodule_load_addr(module, module->header->module_entry_point);
108}
109
110static void rmodule_clear_bss(struct rmodule *module)
111{
112 char *begin;
113 int size;
114
115 begin = rmodule_load_addr(module, module->header->bss_begin);
116 size = module->header->bss_end - module->header->bss_begin;
117 memset(begin, 0, size);
118}
119
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500120static inline size_t rmodule_number_relocations(const struct rmodule *module)
Aaron Durbinad935522012-12-24 14:28:37 -0600121{
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500122 size_t r;
Aaron Durbinad935522012-12-24 14:28:37 -0600123
124 r = module->header->relocations_end_offset;
125 r -= module->header->relocations_begin_offset;
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500126 r /= sizeof(uintptr_t);
Aaron Durbinad935522012-12-24 14:28:37 -0600127 return r;
128}
129
130static void rmodule_copy_payload(const struct rmodule *module)
131{
132 printk(BIOS_DEBUG, "Loading module at %p with entry %p. "
133 "filesize: 0x%x memsize: 0x%x\n",
134 module->location, rmodule_entry(module),
135 module->payload_size, rmodule_memory_size(module));
Aaron Durbine8c866a2013-02-08 17:05:36 -0600136
137 /* No need to copy the payload if the load location and the
138 * payload location are the same. */
139 if (module->location == module->payload)
140 return;
141
Aaron Durbinad935522012-12-24 14:28:37 -0600142 memcpy(module->location, module->payload, module->payload_size);
143}
144
Aaron Durbinad935522012-12-24 14:28:37 -0600145static int rmodule_relocate(const struct rmodule *module)
146{
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500147 size_t num_relocations;
148 const uintptr_t *reloc;
149 uintptr_t adjustment;
Aaron Durbinad935522012-12-24 14:28:37 -0600150
151 /* Each relocation needs to be adjusted relative to the beginning of
152 * the loaded program. */
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500153 adjustment = (uintptr_t)rmodule_load_addr(module, 0);
Aaron Durbinad935522012-12-24 14:28:37 -0600154
155 reloc = module->relocations;
156 num_relocations = rmodule_number_relocations(module);
157
Furquan Shaikh7a3c3492014-07-22 10:59:28 -0700158 printk(BIOS_DEBUG, "Processing %zu relocs. Offset value of 0x%08lx\n",
159 num_relocations, (unsigned long)adjustment);
Aaron Durbinad935522012-12-24 14:28:37 -0600160
161 while (num_relocations > 0) {
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500162 uintptr_t *adjust_loc;
Aaron Durbinad935522012-12-24 14:28:37 -0600163
164 /* If the adjustment location is non-NULL adjust it. */
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500165 adjust_loc = rmodule_load_addr(module, *reloc);
Furquan Shaikh7a3c3492014-07-22 10:59:28 -0700166 printk(PK_ADJ_LEVEL, "Adjusting %p: 0x%08lx -> 0x%08lx\n",
167 adjust_loc, (unsigned long) *adjust_loc,
Furquan Shaikhae9cd012014-07-23 04:44:09 -0700168 (unsigned long) (*adjust_loc + adjustment));
Aaron Durbinad935522012-12-24 14:28:37 -0600169 *adjust_loc += adjustment;
Aaron Durbinad935522012-12-24 14:28:37 -0600170
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500171 reloc++;
Aaron Durbinad935522012-12-24 14:28:37 -0600172 num_relocations--;
173 }
174
175 return 0;
176}
177
178int rmodule_load_alignment(const struct rmodule *module)
179{
180 /* The load alignment is the start of the program's linked address.
181 * The base address where the program is loaded needs to be a multiple
182 * of the program's starting link address. That way all data alignment
Aaron Durbin3eb8eb72014-03-10 16:13:58 -0500183 * in the program is preserved. Default to 4KiB. */
184 return 4096;
Aaron Durbinad935522012-12-24 14:28:37 -0600185}
186
187int rmodule_load(void *base, struct rmodule *module)
188{
189 /*
190 * In order to load the module at a given address, the following steps
191 * take place:
192 * 1. Copy payload to base address.
Aaron Durbin55ed3102013-03-01 17:00:39 -0600193 * 2. Adjust relocations within the module to new base address.
194 * 3. Clear the bss segment last since the relocations live where
195 * the bss is. If an rmodule is being loaded from its load
196 * address the relocations need to be processed before the bss.
Aaron Durbinad935522012-12-24 14:28:37 -0600197 */
198 module->location = base;
199 rmodule_copy_payload(module);
Aaron Durbin55ed3102013-03-01 17:00:39 -0600200 if (rmodule_relocate(module))
201 return -1;
Aaron Durbinad935522012-12-24 14:28:37 -0600202 rmodule_clear_bss(module);
Aaron Durbinf72f9e72014-03-25 15:31:00 -0500203
Aaron Durbin6e76fff2015-03-20 09:42:05 -0500204 arch_segment_loaded((uintptr_t)module->location,
205 rmodule_memory_size(module), SEG_FINAL);
Aaron Durbinf72f9e72014-03-25 15:31:00 -0500206
Aaron Durbin55ed3102013-03-01 17:00:39 -0600207 return 0;
Aaron Durbinad935522012-12-24 14:28:37 -0600208}
209
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600210int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size,
211 size_t *region_size, int *load_offset)
Aaron Durbine8c866a2013-02-08 17:05:36 -0600212{
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600213 /* region_alignment must be a power of 2. */
214 if (region_alignment & (region_alignment - 1))
215 BUG();
Aaron Durbine8c866a2013-02-08 17:05:36 -0600216
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600217 if (region_alignment < 4096)
218 region_alignment = 4096;
219
220 /* Sanity check rmodule_header size. The code below assumes it is less
221 * than the minimum alignment required. */
222 if (region_alignment < sizeof(struct rmodule_header))
223 BUG();
224
225 /* Place the rmodule according to alignment. The rmodule files
Aaron Durbine8c866a2013-02-08 17:05:36 -0600226 * themselves are packed as a header and a payload, however the rmodule
227 * itself is linked along with the header. The header starts at address
228 * 0. Immediately following the header in the file is the program,
229 * however its starting address is determined by the rmodule linker
230 * script. In short, sizeof(struct rmodule_header) can be less than
231 * or equal to the linked address of the program. Therefore we want
232 * to place the rmodule so that the program falls on the aligned
233 * address with the header just before it. Therefore, we need at least
234 * a page to account for the size of the header. */
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600235 *region_size = ALIGN(rmodule_size + region_alignment, 4096);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600236 /* The program starts immediately after the header. However,
237 * it needs to be aligned to a 4KiB boundary. Therefore, adjust the
238 * program location so that the program lands on a page boundary. The
239 * layout looks like the following:
240 *
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600241 * +--------------------------------+ region_alignment + region_size
Aaron Durbine8c866a2013-02-08 17:05:36 -0600242 * | >= 0 bytes from alignment |
243 * +--------------------------------+ program end (4KiB aligned)
244 * | program size |
245 * +--------------------------------+ program_begin (4KiB aligned)
246 * | sizeof(struct rmodule_header) |
247 * +--------------------------------+ rmodule header start
248 * | >= 0 bytes from alignment |
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600249 * +--------------------------------+ region_alignment
Aaron Durbine8c866a2013-02-08 17:05:36 -0600250 */
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600251 *load_offset = region_alignment;
Aaron Durbine8c866a2013-02-08 17:05:36 -0600252
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600253 return region_alignment - sizeof(struct rmodule_header);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600254}
Aaron Durbinf545abf2013-10-24 10:14:06 -0500255
Aaron Durbin899d13d2015-05-15 23:39:23 -0500256int rmodule_stage_load(struct rmod_stage_load *rsl)
Aaron Durbinf545abf2013-10-24 10:14:06 -0500257{
258 struct rmodule rmod_stage;
259 size_t region_size;
260 char *stage_region;
261 int rmodule_offset;
262 int load_offset;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500263 struct cbfs_stage stage;
264 void *rmod_loc;
265 struct region_device *fh;
Aaron Durbinf545abf2013-10-24 10:14:06 -0500266
Aaron Durbin899d13d2015-05-15 23:39:23 -0500267 if (rsl->prog == NULL || rsl->prog->name == NULL)
268 return -1;
269
270 fh = &rsl->prog->rdev;
271
272 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
Aaron Durbinf545abf2013-10-24 10:14:06 -0500273 return -1;
274
275 rmodule_offset =
276 rmodule_calc_region(DYN_CBMEM_ALIGN_SIZE,
Aaron Durbin899d13d2015-05-15 23:39:23 -0500277 stage.memlen, &region_size, &load_offset);
Aaron Durbinf545abf2013-10-24 10:14:06 -0500278
Aaron Durbin7ca65222015-04-06 17:18:18 -0500279 stage_region = cbmem_add(rsl->cbmem_id, region_size);
Aaron Durbinf545abf2013-10-24 10:14:06 -0500280
Aaron Durbin7ca65222015-04-06 17:18:18 -0500281 if (stage_region == NULL)
Aaron Durbinf545abf2013-10-24 10:14:06 -0500282 return -1;
283
Aaron Durbin899d13d2015-05-15 23:39:23 -0500284 rmod_loc = &stage_region[rmodule_offset];
Aaron Durbinf545abf2013-10-24 10:14:06 -0500285
Aaron Durbin899d13d2015-05-15 23:39:23 -0500286 printk(BIOS_INFO, "Decompressing stage %s @ 0x%p (%d bytes)\n",
287 rsl->prog->name, rmod_loc, stage.memlen);
288
289 if (stage.compression == CBFS_COMPRESS_NONE) {
290 if (rdev_readat(fh, rmod_loc, sizeof(stage), stage.len) !=
291 stage.len)
292 return -1;
293 } else if (stage.compression == CBFS_COMPRESS_LZMA) {
294 size_t fsize;
295 void *map = rdev_mmap(fh, sizeof(stage), stage.len);
296
297 if (map == NULL)
298 return -1;
299
300 fsize = ulzma(map, rmod_loc);
301
302 rdev_munmap(fh, map);
303
304 if (!fsize)
305 return -1;
306 } else
Aaron Durbinf545abf2013-10-24 10:14:06 -0500307 return -1;
308
Aaron Durbin899d13d2015-05-15 23:39:23 -0500309 if (rmodule_parse(rmod_loc, &rmod_stage))
Aaron Durbinf545abf2013-10-24 10:14:06 -0500310 return -1;
311
312 if (rmodule_load(&stage_region[load_offset], &rmod_stage))
313 return -1;
314
Aaron Durbin7ca65222015-04-06 17:18:18 -0500315 prog_set_area(rsl->prog, rmod_stage.location,
316 rmodule_memory_size(&rmod_stage));
Aaron Durbin460703b2015-03-27 21:17:22 -0500317 prog_set_entry(rsl->prog, rmodule_entry(&rmod_stage), NULL);
Aaron Durbinf545abf2013-10-24 10:14:06 -0500318
319 return 0;
320}