1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/*
* This file is part of the coreboot project.
*
* Copyright (C) 2012 ChromeOS Authors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <assert.h>
#include <cbmem.h>
#include <cbfs.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <lib.h>
#include <console/console.h>
#include <program_loading.h>
#include <rmodule.h>
/* Change this define to get more verbose debugging for module loading. */
#define PK_ADJ_LEVEL BIOS_NEVER
static inline int rmodule_is_loaded(const struct rmodule *module)
{
return module->location != NULL;
}
/* Calculate a loaded program address based on the blob address. */
static inline void *rmodule_load_addr(const struct rmodule *module,
uintptr_t blob_addr)
{
char *loc = module->location;
return &loc[blob_addr - module->header->module_link_start_address];
}
/* Initialize a rmodule structure based on raw data. */
int rmodule_parse(void *ptr, struct rmodule *module)
{
char *base;
struct rmodule_header *rhdr;
base = ptr;
rhdr = ptr;
if (rhdr == NULL)
return -1;
/* Sanity check the raw data. */
if (rhdr->magic != RMODULE_MAGIC)
return -1;
if (rhdr->version != RMODULE_VERSION_1)
return -1;
/* Indicate the module hasn't been loaded yet. */
module->location = NULL;
/* The rmodule only needs a reference to the reloc_header. */
module->header = rhdr;
/* The payload lives after the header. */
module->payload = &base[rhdr->payload_begin_offset];
module->payload_size = rhdr->payload_end_offset -
rhdr->payload_begin_offset;
module->relocations = &base[rhdr->relocations_begin_offset];
return 0;
}
int rmodule_memory_size(const struct rmodule *module)
{
return module->header->module_program_size;
}
void *rmodule_parameters(const struct rmodule *module)
{
if (!rmodule_is_loaded(module))
return NULL;
/* Indicate if there are no parameters. */
if (module->header->parameters_begin == module->header->parameters_end)
return NULL;
return rmodule_load_addr(module, module->header->parameters_begin);
}
int rmodule_entry_offset(const struct rmodule *module)
{
return module->header->module_entry_point -
module->header->module_link_start_address;
}
void *rmodule_entry(const struct rmodule *module)
{
if (!rmodule_is_loaded(module))
return NULL;
return rmodule_load_addr(module, module->header->module_entry_point);
}
static void rmodule_clear_bss(struct rmodule *module)
{
char *begin;
int size;
begin = rmodule_load_addr(module, module->header->bss_begin);
size = module->header->bss_end - module->header->bss_begin;
memset(begin, 0, size);
}
static inline size_t rmodule_number_relocations(const struct rmodule *module)
{
size_t r;
r = module->header->relocations_end_offset;
r -= module->header->relocations_begin_offset;
r /= sizeof(uintptr_t);
return r;
}
static void rmodule_copy_payload(const struct rmodule *module)
{
printk(BIOS_DEBUG, "Loading module at %p with entry %p. "
"filesize: 0x%x memsize: 0x%x\n",
module->location, rmodule_entry(module),
module->payload_size, rmodule_memory_size(module));
/* No need to copy the payload if the load location and the
* payload location are the same. */
if (module->location == module->payload)
return;
memcpy(module->location, module->payload, module->payload_size);
}
static int rmodule_relocate(const struct rmodule *module)
{
size_t num_relocations;
const uintptr_t *reloc;
uintptr_t adjustment;
/* Each relocation needs to be adjusted relative to the beginning of
* the loaded program. */
adjustment = (uintptr_t)rmodule_load_addr(module, 0);
reloc = module->relocations;
num_relocations = rmodule_number_relocations(module);
printk(BIOS_DEBUG, "Processing %zu relocs. Offset value of 0x%08lx\n",
num_relocations, (unsigned long)adjustment);
while (num_relocations > 0) {
uintptr_t *adjust_loc;
/* If the adjustment location is non-NULL adjust it. */
adjust_loc = rmodule_load_addr(module, *reloc);
printk(PK_ADJ_LEVEL, "Adjusting %p: 0x%08lx -> 0x%08lx\n",
adjust_loc, (unsigned long) *adjust_loc,
(unsigned long) (*adjust_loc + adjustment));
*adjust_loc += adjustment;
reloc++;
num_relocations--;
}
return 0;
}
int rmodule_load_alignment(const struct rmodule *module)
{
/* The load alignment is the start of the program's linked address.
* The base address where the program is loaded needs to be a multiple
* of the program's starting link address. That way all data alignment
* in the program is preserved. Default to 4KiB. */
return 4096;
}
int rmodule_load(void *base, struct rmodule *module)
{
/*
* In order to load the module at a given address, the following steps
* take place:
* 1. Copy payload to base address.
* 2. Adjust relocations within the module to new base address.
* 3. Clear the bss segment last since the relocations live where
* the bss is. If an rmodule is being loaded from its load
* address the relocations need to be processed before the bss.
*/
module->location = base;
rmodule_copy_payload(module);
if (rmodule_relocate(module))
return -1;
rmodule_clear_bss(module);
arch_segment_loaded((uintptr_t)module->location,
rmodule_memory_size(module), SEG_FINAL);
return 0;
}
int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size,
size_t *region_size, int *load_offset)
{
/* region_alignment must be a power of 2. */
if (region_alignment & (region_alignment - 1))
BUG();
if (region_alignment < 4096)
region_alignment = 4096;
/* Sanity check rmodule_header size. The code below assumes it is less
* than the minimum alignment required. */
if (region_alignment < sizeof(struct rmodule_header))
BUG();
/* Place the rmodule according to alignment. The rmodule files
* themselves are packed as a header and a payload, however the rmodule
* itself is linked along with the header. The header starts at address
* 0. Immediately following the header in the file is the program,
* however its starting address is determined by the rmodule linker
* script. In short, sizeof(struct rmodule_header) can be less than
* or equal to the linked address of the program. Therefore we want
* to place the rmodule so that the program falls on the aligned
* address with the header just before it. Therefore, we need at least
* a page to account for the size of the header. */
*region_size = ALIGN(rmodule_size + region_alignment, 4096);
/* The program starts immediately after the header. However,
* it needs to be aligned to a 4KiB boundary. Therefore, adjust the
* program location so that the program lands on a page boundary. The
* layout looks like the following:
*
* +--------------------------------+ region_alignment + region_size
* | >= 0 bytes from alignment |
* +--------------------------------+ program end (4KiB aligned)
* | program size |
* +--------------------------------+ program_begin (4KiB aligned)
* | sizeof(struct rmodule_header) |
* +--------------------------------+ rmodule header start
* | >= 0 bytes from alignment |
* +--------------------------------+ region_alignment
*/
*load_offset = region_alignment;
return region_alignment - sizeof(struct rmodule_header);
}
int rmodule_stage_load(struct rmod_stage_load *rsl)
{
struct rmodule rmod_stage;
size_t region_size;
char *stage_region;
int rmodule_offset;
int load_offset;
struct cbfs_stage stage;
void *rmod_loc;
struct region_device *fh;
const int use_lzma = ENV_RAMSTAGE
|| (ENV_ROMSTAGE && IS_ENABLED(CONFIG_COMPRESS_RAMSTAGE));
if (rsl->prog == NULL || prog_name(rsl->prog) == NULL)
return -1;
fh = prog_rdev(rsl->prog);
if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
return -1;
rmodule_offset =
rmodule_calc_region(DYN_CBMEM_ALIGN_SIZE,
stage.memlen, ®ion_size, &load_offset);
stage_region = cbmem_add(rsl->cbmem_id, region_size);
if (stage_region == NULL)
return -1;
rmod_loc = &stage_region[rmodule_offset];
printk(BIOS_INFO, "Decompressing stage %s @ 0x%p (%d bytes)\n",
prog_name(rsl->prog), rmod_loc, stage.memlen);
if (stage.compression == CBFS_COMPRESS_NONE) {
if (rdev_readat(fh, rmod_loc, sizeof(stage), stage.len) !=
stage.len)
return -1;
} else if (use_lzma && (stage.compression == CBFS_COMPRESS_LZMA)) {
size_t fsize;
void *map = rdev_mmap(fh, sizeof(stage), stage.len);
if (map == NULL)
return -1;
fsize = ulzma(map, rmod_loc);
rdev_munmap(fh, map);
if (!fsize)
return -1;
} else
return -1;
if (rmodule_parse(rmod_loc, &rmod_stage))
return -1;
if (rmodule_load(&stage_region[load_offset], &rmod_stage))
return -1;
prog_set_area(rsl->prog, rmod_stage.location,
rmodule_memory_size(&rmod_stage));
prog_set_entry(rsl->prog, rmodule_entry(&rmod_stage), NULL);
return 0;
}
|