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