blob: 462c7d76f28f16d824378a148dabca7f3feee0d2 [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>
23#include <console/console.h>
24#include <rmodule.h>
25
26/* Change this define to get more verbose debugging for module loading. */
27#define PK_ADJ_LEVEL BIOS_NEVER
28
29#if CONFIG_ARCH_X86
30/*
31 * On X86, the only relocations currently allowed are R_386_RELATIVE which
32 * have '0' for the symbol info in the relocation metadata (in r_info).
33 * The reason is that the module is fully linked and just has the relocations'
34 * locations.
35 */
36typedef struct {
37 u32 r_offset;
38 u32 r_info;
39} Elf32_Rel;
40
41#define R_386_RELATIVE 8
42
43#define RELOCTION_ENTRY_SIZE sizeof(Elf32_Rel)
44static inline int rmodule_reloc_offset(const void *reloc)
45{
46 const Elf32_Rel *rel = reloc;
47 return rel->r_offset;
48}
49
50static inline int rmodule_reloc_valid(const void *reloc)
51{
52 const Elf32_Rel *rel = reloc;
53 return (rel->r_info == R_386_RELATIVE);
54}
55
56static inline void *remodule_next_reloc(const void *reloc)
57{
58 const Elf32_Rel *rel = reloc;
59 rel++;
60 return (void *)rel;
61}
62
63#else
64#error Arch needs to add relocation information support for RMODULE
65#endif
66
67static inline int rmodule_is_loaded(const struct rmodule *module)
68{
69 return module->location != NULL;
70}
71
72/* Calculate a loaded program address based on the blob address. */
73static inline void *rmodule_load_addr(const struct rmodule *module,
74 u32 blob_addr)
75{
76 char *loc = module->location;
77 return &loc[blob_addr - module->header->module_link_start_address];
78}
79
80/* Initialize a rmodule structure based on raw data. */
81int rmodule_parse(void *ptr, struct rmodule *module)
82{
83 char *base;
84 struct rmodule_header *rhdr;
85
86 base = ptr;
87 rhdr = ptr;
88
89 if (rhdr == NULL)
90 return -1;
91
92 /* Sanity check the raw data. */
93 if (rhdr->magic != RMODULE_MAGIC)
94 return -1;
95 if (rhdr->version != RMODULE_VERSION_1)
96 return -1;
97
98 /* Indicate the module hasn't been loaded yet. */
99 module->location = NULL;
100
101 /* The rmodule only needs a reference to the reloc_header. */
102 module->header = rhdr;
103
104 /* The payload lives after the header. */
105 module->payload = &base[rhdr->payload_begin_offset];
106 module->payload_size = rhdr->payload_end_offset -
107 rhdr->payload_begin_offset;
108 module->relocations = &base[rhdr->relocations_begin_offset];
109
110 return 0;
111}
112
113int rmodule_memory_size(const struct rmodule *module)
114{
115 return module->header->module_program_size;
116}
117
118void *rmodule_parameters(const struct rmodule *module)
119{
120 if (!rmodule_is_loaded(module))
121 return NULL;
122
123 /* Indicate if there are no parameters. */
124 if (module->header->parameters_begin == module->header->parameters_end)
125 return NULL;
126
127 return rmodule_load_addr(module, module->header->parameters_begin);
128}
129
130int rmodule_entry_offset(const struct rmodule *module)
131{
132 return module->header->module_entry_point -
133 module->header->module_link_start_address;
134}
135
136void *rmodule_entry(const struct rmodule *module)
137{
138 if (!rmodule_is_loaded(module))
139 return NULL;
140
141 return rmodule_load_addr(module, module->header->module_entry_point);
142}
143
144static void rmodule_clear_bss(struct rmodule *module)
145{
146 char *begin;
147 int size;
148
149 begin = rmodule_load_addr(module, module->header->bss_begin);
150 size = module->header->bss_end - module->header->bss_begin;
151 memset(begin, 0, size);
152}
153
154static inline int rmodule_number_relocations(const struct rmodule *module)
155{
156 int r;
157
158 r = module->header->relocations_end_offset;
159 r -= module->header->relocations_begin_offset;
160 r /= RELOCTION_ENTRY_SIZE;
161 return r;
162}
163
164static void rmodule_copy_payload(const struct rmodule *module)
165{
166 printk(BIOS_DEBUG, "Loading module at %p with entry %p. "
167 "filesize: 0x%x memsize: 0x%x\n",
168 module->location, rmodule_entry(module),
169 module->payload_size, rmodule_memory_size(module));
Aaron Durbine8c866a2013-02-08 17:05:36 -0600170
171 /* No need to copy the payload if the load location and the
172 * payload location are the same. */
173 if (module->location == module->payload)
174 return;
175
Aaron Durbinad935522012-12-24 14:28:37 -0600176 memcpy(module->location, module->payload, module->payload_size);
177}
178
179static inline u32 *rmodule_adjustment_location(const struct rmodule *module,
180 const void *reloc)
181{
182 int reloc_offset;
183
184 /* Don't relocate header field entries -- only program relocations. */
185 reloc_offset = rmodule_reloc_offset(reloc);
186 if (reloc_offset < module->header->module_link_start_address)
187 return NULL;
188
189 return rmodule_load_addr(module, reloc_offset);
190}
191
192static int rmodule_relocate(const struct rmodule *module)
193{
194 int num_relocations;
195 const void *reloc;
196 u32 adjustment;
197
198 /* Each relocation needs to be adjusted relative to the beginning of
199 * the loaded program. */
200 adjustment = (u32)rmodule_load_addr(module, 0);
201
202 reloc = module->relocations;
203 num_relocations = rmodule_number_relocations(module);
204
205 printk(BIOS_DEBUG, "Processing %d relocs with adjust value of 0x%08x\n",
206 num_relocations, adjustment);
207
208 while (num_relocations > 0) {
209 u32 *adjust_loc;
210
211 if (!rmodule_reloc_valid(reloc))
212 return -1;
213
214 /* If the adjustment location is non-NULL adjust it. */
215 adjust_loc = rmodule_adjustment_location(module, reloc);
216 if (adjust_loc != NULL) {
217 printk(PK_ADJ_LEVEL, "Adjusting %p: 0x%08x -> 0x%08x\n",
218 adjust_loc, *adjust_loc,
219 *adjust_loc + adjustment);
220 *adjust_loc += adjustment;
221 }
222
223 reloc = remodule_next_reloc(reloc);
224 num_relocations--;
225 }
226
227 return 0;
228}
229
230int rmodule_load_alignment(const struct rmodule *module)
231{
232 /* The load alignment is the start of the program's linked address.
233 * The base address where the program is loaded needs to be a multiple
234 * of the program's starting link address. That way all data alignment
Martin Rothcbf2bd72013-07-09 21:51:14 -0600235 * in the program is preserved. */
Aaron Durbinad935522012-12-24 14:28:37 -0600236 return module->header->module_link_start_address;
237}
238
239int rmodule_load(void *base, struct rmodule *module)
240{
241 /*
242 * In order to load the module at a given address, the following steps
243 * take place:
244 * 1. Copy payload to base address.
Aaron Durbin55ed3102013-03-01 17:00:39 -0600245 * 2. Adjust relocations within the module to new base address.
246 * 3. Clear the bss segment last since the relocations live where
247 * the bss is. If an rmodule is being loaded from its load
248 * address the relocations need to be processed before the bss.
Aaron Durbinad935522012-12-24 14:28:37 -0600249 */
250 module->location = base;
251 rmodule_copy_payload(module);
Aaron Durbin55ed3102013-03-01 17:00:39 -0600252 if (rmodule_relocate(module))
253 return -1;
Aaron Durbinad935522012-12-24 14:28:37 -0600254 rmodule_clear_bss(module);
Aaron Durbin55ed3102013-03-01 17:00:39 -0600255 return 0;
Aaron Durbinad935522012-12-24 14:28:37 -0600256}
257
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600258int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size,
259 size_t *region_size, int *load_offset)
Aaron Durbine8c866a2013-02-08 17:05:36 -0600260{
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600261 /* region_alignment must be a power of 2. */
262 if (region_alignment & (region_alignment - 1))
263 BUG();
Aaron Durbine8c866a2013-02-08 17:05:36 -0600264
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600265 if (region_alignment < 4096)
266 region_alignment = 4096;
267
268 /* Sanity check rmodule_header size. The code below assumes it is less
269 * than the minimum alignment required. */
270 if (region_alignment < sizeof(struct rmodule_header))
271 BUG();
272
273 /* Place the rmodule according to alignment. The rmodule files
Aaron Durbine8c866a2013-02-08 17:05:36 -0600274 * themselves are packed as a header and a payload, however the rmodule
275 * itself is linked along with the header. The header starts at address
276 * 0. Immediately following the header in the file is the program,
277 * however its starting address is determined by the rmodule linker
278 * script. In short, sizeof(struct rmodule_header) can be less than
279 * or equal to the linked address of the program. Therefore we want
280 * to place the rmodule so that the program falls on the aligned
281 * address with the header just before it. Therefore, we need at least
282 * a page to account for the size of the header. */
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600283 *region_size = ALIGN(rmodule_size + region_alignment, 4096);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600284 /* The program starts immediately after the header. However,
285 * it needs to be aligned to a 4KiB boundary. Therefore, adjust the
286 * program location so that the program lands on a page boundary. The
287 * layout looks like the following:
288 *
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600289 * +--------------------------------+ region_alignment + region_size
Aaron Durbine8c866a2013-02-08 17:05:36 -0600290 * | >= 0 bytes from alignment |
291 * +--------------------------------+ program end (4KiB aligned)
292 * | program size |
293 * +--------------------------------+ program_begin (4KiB aligned)
294 * | sizeof(struct rmodule_header) |
295 * +--------------------------------+ rmodule header start
296 * | >= 0 bytes from alignment |
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600297 * +--------------------------------+ region_alignment
Aaron Durbine8c866a2013-02-08 17:05:36 -0600298 */
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600299 *load_offset = region_alignment;
Aaron Durbine8c866a2013-02-08 17:05:36 -0600300
Aaron Durbindd4a6d22013-02-27 22:50:12 -0600301 return region_alignment - sizeof(struct rmodule_header);
Aaron Durbine8c866a2013-02-08 17:05:36 -0600302}