blob: b1563500bf7a04510316808d3ba35a5f98c0657c [file] [log] [blame]
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00001/*
Peter Stuge45ae92ff2009-04-14 19:48:32 +00002 * cbfs-mkstage
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00003 *
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
Patrick Georgib7b56dd82009-09-14 13:29:27 +00005 * 2009 coresystems GmbH
6 * written by Patrick Georgi <patrick.georgi@coresystems.de>
David Hendricks90ca3b62012-11-16 14:48:22 -08007 * Copyright (C) 2012 Google, Inc.
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000017 */
18
Francis Rowe3fb8b0d2014-11-21 02:38:48 +000019#include <inttypes.h>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000023
Aaron Durbin54ef3062014-03-05 12:12:09 -060024#include "elfparsing.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000025#include "common.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000026#include "cbfs.h"
Aaron Durbin4be16742015-09-15 17:00:23 -050027#include "rmodule.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000028
Furquan Shaikh405304a2014-10-30 11:44:20 -070029/* Checks if program segment contains the ignored section */
30static int is_phdr_ignored(Elf64_Phdr *phdr, Elf64_Shdr *shdr)
31{
32 /* If no ignored section, return false. */
33 if (shdr == NULL)
34 return 0;
35
36 Elf64_Addr sh_start = shdr->sh_addr;
37 Elf64_Addr sh_end = shdr->sh_addr + shdr->sh_size;
38 Elf64_Addr ph_start = phdr->p_vaddr;
39 Elf64_Addr ph_end = phdr->p_vaddr + phdr->p_memsz;
40
41 /* Return true only if section occupies whole of segment. */
42 if ((sh_start == ph_start) && (sh_end == ph_end)) {
Francis Rowe3fb8b0d2014-11-21 02:38:48 +000043 DEBUG("Ignoring program segment at 0x%" PRIx64 "\n", ph_start);
Furquan Shaikh405304a2014-10-30 11:44:20 -070044 return 1;
45 }
46
47 /* If shdr intersects phdr at all, its a conflict */
48 if (((sh_start >= ph_start) && (sh_start <= ph_end)) ||
49 ((sh_end >= ph_start) && (sh_end <= ph_end))) {
50 ERROR("Conflicting sections in segment\n");
51 exit(1);
52 }
53
54 /* Program header doesn't need to be ignored. */
55 return 0;
56}
57
58/* Find section header based on ignored section name */
59static Elf64_Shdr *find_ignored_section_header(struct parsed_elf *pelf,
60 const char *ignore_section)
61{
62 int i;
63 const char *shstrtab;
64
65 /* No section needs to be ignored */
66 if (ignore_section == NULL)
67 return NULL;
68
69 DEBUG("Section to be ignored: %s\n", ignore_section);
70
71 /* Get pointer to string table */
72 shstrtab = buffer_get(pelf->strtabs[pelf->ehdr.e_shstrndx]);
73
74 for (i = 0; i < pelf->ehdr.e_shnum; i++) {
75 Elf64_Shdr *shdr;
76 const char *section_name;
77
78 shdr = &pelf->shdr[i];
79 section_name = &shstrtab[shdr->sh_name];
80
81 /* If section name matches ignored string, return shdr */
82 if (strcmp(section_name, ignore_section) == 0)
83 return shdr;
84 }
85
86 /* No section matches ignore string */
87 return NULL;
88}
89
Aaron Durbin4be16742015-09-15 17:00:23 -050090static void fill_cbfs_stage(struct buffer *outheader, enum comp_algo algo,
91 uint64_t entry, uint64_t loadaddr,
92 uint32_t filesize, uint32_t memsize)
93{
94 /* N.B. The original plan was that SELF data was B.E.
95 * but: this is all L.E.
96 * Maybe we should just change the spec.
97 */
98 xdr_le.put32(outheader, algo);
99 xdr_le.put64(outheader, entry);
100 xdr_le.put64(outheader, loadaddr);
101 xdr_le.put32(outheader, filesize);
102 xdr_le.put32(outheader, memsize);
103}
104
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800105/* returns size of result, or -1 if error.
106 * Note that, with the new code, this function
107 * works for all elf files, not just the restricted set.
108 */
109int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
Sol Boucher6310ccc2015-05-07 21:12:28 -0700110 enum comp_algo algo, uint32_t *location,
Furquan Shaikh405304a2014-10-30 11:44:20 -0700111 const char *ignore_section)
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800112{
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700113 struct parsed_elf pelf;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800114 Elf64_Phdr *phdr;
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700115 Elf64_Ehdr *ehdr;
Furquan Shaikh405304a2014-10-30 11:44:20 -0700116 Elf64_Shdr *shdr_ignored;
Furquan Shaikhf7a5b562015-05-29 12:46:18 -0700117 Elf64_Addr virt_to_phys;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800118 char *buffer;
119 struct buffer outheader;
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700120 int ret = -1;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800121
122 int headers;
123 int i, outlen;
124 uint32_t data_start, data_end, mem_end;
125
126 comp_func_ptr compress = compression_function(algo);
127 if (!compress)
128 return -1;
129
130 DEBUG("start: parse_elf_to_stage(location=0x%x)\n", *location);
131
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700132 int flags = ELF_PARSE_PHDR | ELF_PARSE_SHDR | ELF_PARSE_STRTAB;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800133
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700134 if (parse_elf(input, &pelf, flags)) {
135 ERROR("Couldn't parse ELF\n");
136 return -1;
137 }
138
139 ehdr = &pelf.ehdr;
140 phdr = &pelf.phdr[0];
141
Furquan Shaikh405304a2014-10-30 11:44:20 -0700142 /* Find the section header corresponding to ignored-section */
143 shdr_ignored = find_ignored_section_header(&pelf, ignore_section);
144
145 if (ignore_section && (shdr_ignored == NULL))
146 WARN("Ignore section not found\n");
147
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700148 headers = ehdr->e_phnum;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800149
Furquan Shaikh405304a2014-10-30 11:44:20 -0700150 /* Ignore the program header containing ignored section */
151 for (i = 0; i < headers; i++) {
152 if (is_phdr_ignored(&phdr[i], shdr_ignored))
153 phdr[i].p_type = PT_NULL;
154 }
155
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800156 data_start = ~0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000157 data_end = 0;
158 mem_end = 0;
Furquan Shaikhf7a5b562015-05-29 12:46:18 -0700159 virt_to_phys = 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000160
161 for (i = 0; i < headers; i++) {
162 unsigned int start, mend, rend;
163
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800164 if (phdr[i].p_type != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000165 continue;
166
167 /* Empty segments are never interesting */
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800168 if (phdr[i].p_memsz == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000169 continue;
170
171 /* BSS */
172
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800173 start = phdr[i].p_paddr;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000174
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800175 mend = start + phdr[i].p_memsz;
176 rend = start + phdr[i].p_filesz;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000177
178 if (start < data_start)
179 data_start = start;
180
181 if (rend > data_end)
182 data_end = rend;
183
184 if (mend > mem_end)
185 mem_end = mend;
Furquan Shaikhf7a5b562015-05-29 12:46:18 -0700186
187 if (virt_to_phys == 0)
188 virt_to_phys = phdr[i].p_paddr - phdr[i].p_vaddr;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000189 }
190
Patrick Georgi9341acd2009-12-23 12:52:56 +0000191 if (data_start < *location) {
192 data_start = *location;
193 }
194
Patrick Georgia6c337d2010-02-03 17:56:37 +0000195 if (data_end <= data_start) {
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800196 ERROR("data ends (%08lx) before it starts (%08lx). Make sure "
197 "the ELF file is correct and resides in ROM space.\n",
198 (unsigned long)data_end, (unsigned long)data_start);
Patrick Georgia6c337d2010-02-03 17:56:37 +0000199 exit(1);
200 }
201
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000202 /* allocate an intermediate buffer for the data */
203 buffer = calloc(data_end - data_start, 1);
204
205 if (buffer == NULL) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800206 ERROR("Unable to allocate memory: %m\n");
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700207 goto err;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000208 }
209
210 /* Copy the file data into the buffer */
211
212 for (i = 0; i < headers; i++) {
Patrick Georgi9341acd2009-12-23 12:52:56 +0000213 unsigned int l_start, l_offset = 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000214
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800215 if (phdr[i].p_type != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000216 continue;
217
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800218 if (phdr[i].p_memsz == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000219 continue;
220
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800221 l_start = phdr[i].p_paddr;
Patrick Georgi9341acd2009-12-23 12:52:56 +0000222 if (l_start < *location) {
223 l_offset = *location - l_start;
224 l_start = *location;
225 }
226
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800227 /* A legal ELF file can have a program header with
228 * non-zero length but zero-length file size and a
229 * non-zero offset which, added together, are > than
230 * input->size (i.e. the total file size). So we need
231 * to not even test in the case that p_filesz is zero.
232 */
233 if (! phdr[i].p_filesz)
234 continue;
235 if (input->size < (phdr[i].p_offset + phdr[i].p_filesz)){
236 ERROR("Underflow copying out the segment."
Paul Menzel470c37c2014-03-16 00:15:57 +0100237 "File has %zu bytes left, segment end is %zu\n",
238 input->size, (size_t)(phdr[i].p_offset + phdr[i].p_filesz));
Daniele Forsi8e898472014-07-27 12:01:40 +0200239 free(buffer);
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700240 goto err;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800241 }
Patrick Georgi9341acd2009-12-23 12:52:56 +0000242 memcpy(buffer + (l_start - data_start),
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800243 &input->data[phdr[i].p_offset + l_offset],
244 phdr[i].p_filesz - l_offset);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000245 }
246
247 /* Now make the output buffer */
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800248 if (buffer_create(output, sizeof(struct cbfs_stage) + data_end - data_start,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800249 input->name) != 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800250 ERROR("Unable to allocate memory: %m\n");
Paul Menzel2c8f81b2013-04-11 10:45:11 +0200251 free(buffer);
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700252 goto err;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000253 }
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800254 memset(output->data, 0, output->size);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000255
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800256 /* Compress the data, at which point we'll know information
257 * to fill out the header. This seems backward but it works because
258 * - the output header is a known size (not always true in many xdr's)
259 * - we do need to know the compressed output size first
Gabe Black845aa142014-02-21 01:01:06 -0800260 * If compression fails or makes the data bigger, we'll warn about it
261 * and use the original data.
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800262 */
Gabe Blackdbd006b2014-02-20 23:38:49 -0800263 if (compress(buffer, data_end - data_start,
264 (output->data + sizeof(struct cbfs_stage)),
Sol Boucher0e539312015-03-05 15:38:03 -0800265 &outlen) < 0 || (unsigned)outlen > data_end - data_start) {
Gabe Black845aa142014-02-21 01:01:06 -0800266 WARN("Compression failed or would make the data bigger "
267 "- disabled.\n");
268 memcpy(output->data + sizeof(struct cbfs_stage),
269 buffer, data_end - data_start);
270 algo = CBFS_COMPRESS_NONE;
Gabe Blackdbd006b2014-02-20 23:38:49 -0800271 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700272 free(buffer);
273
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800274 /* Set up for output marshaling. */
275 outheader.data = output->data;
276 outheader.size = 0;
Aaron Durbin4be16742015-09-15 17:00:23 -0500277
Furquan Shaikhf7a5b562015-05-29 12:46:18 -0700278 /* Coreboot expects entry point to be physical address. Thus, adjust the
279 * entry point accordingly.
280 */
Aaron Durbin4be16742015-09-15 17:00:23 -0500281 fill_cbfs_stage(&outheader, algo, ehdr->e_entry + virt_to_phys,
282 data_start, outlen, mem_end - data_start);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800283
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000284 if (*location)
285 *location -= sizeof(struct cbfs_stage);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800286 output->size = sizeof(struct cbfs_stage) + outlen;
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700287 ret = 0;
288
289err:
290 parsed_elf_destroy(&pelf);
291 return ret;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000292}
Aaron Durbin4be16742015-09-15 17:00:23 -0500293
294struct xip_context {
295 struct rmod_context rmodctx;
296 size_t ignored_section_idx;
297 Elf64_Shdr *ignored_section;
298};
299
300static int rmod_filter(struct reloc_filter *f, const Elf64_Rela *r)
301{
302 size_t symbol_index;
303 int reloc_type;
304 struct parsed_elf *pelf;
305 Elf64_Sym *sym;
306 struct xip_context *xipctx;
307
308 xipctx = f->context;
309 pelf = &xipctx->rmodctx.pelf;
310
311 /* Allow everything through if there isn't an ignored section. */
312 if (xipctx->ignored_section == NULL)
313 return 1;
314
315 reloc_type = ELF64_R_TYPE(r->r_info);
316 symbol_index = ELF64_R_SYM(r->r_info);
317 sym = &pelf->syms[symbol_index];
318
319 /* Nothing to filter. Relocation is not being applied to the
320 * ignored section. */
321 if (sym->st_shndx != xipctx->ignored_section_idx)
322 return 1;
323
324 /* If there is any relocation to the ignored section that isn't
325 * absolute fail as current assumptions are that all relocations
326 * are absolute. */
327 if (reloc_type != R_386_32) {
328 ERROR("Invalid reloc to ignored section: %x\n", reloc_type);
329 return -1;
330 }
331
332 /* Relocation referencing ignored section. Don't emit it. */
333 return 0;
334}
335
336int parse_elf_to_xip_stage(const struct buffer *input, struct buffer *output,
337 uint32_t *location, const char *ignore_section)
338{
339 struct xip_context xipctx;
340 struct rmod_context *rmodctx;
341 struct reloc_filter filter;
342 struct parsed_elf *pelf;
343 size_t output_sz;
344 uint32_t adjustment;
345 struct buffer binput;
346 struct buffer boutput;
347 Elf64_Xword i;
348 int ret = -1;
349
350 xipctx.ignored_section_idx = 0;
351 rmodctx = &xipctx.rmodctx;
352 pelf = &rmodctx->pelf;
353
354 if (rmodule_init(rmodctx, input))
355 return -1;
356
357 /* Only support x86 XIP currently. */
358 if (rmodctx->pelf.ehdr.e_machine != EM_386) {
359 ERROR("Only support XIP stages for x86\n");
360 goto out;
361 }
362
363 xipctx.ignored_section =
364 find_ignored_section_header(pelf, ignore_section);
365
366 if (xipctx.ignored_section != NULL)
367 xipctx.ignored_section_idx =
368 xipctx.ignored_section - pelf->shdr;
369
370 filter.filter = rmod_filter;
371 filter.context = &xipctx;
372
373 if (rmodule_collect_relocations(rmodctx, &filter))
374 goto out;
375
376 output_sz = sizeof(struct cbfs_stage) + pelf->phdr->p_filesz;
377 if (buffer_create(output, output_sz, input->name) != 0) {
378 ERROR("Unable to allocate memory: %m\n");
379 goto out;
380 }
381 buffer_clone(&boutput, output);
382 memset(buffer_get(&boutput), 0, output_sz);
383 buffer_set_size(&boutput, 0);
384
385 /* Single loadable segment. The entire segment moves to final
386 * location from based on virtual address of loadable segment. */
387 adjustment = *location - pelf->phdr->p_vaddr;
388 DEBUG("Relocation adjustment: %08x\n", adjustment);
389
390 fill_cbfs_stage(&boutput, CBFS_COMPRESS_NONE,
391 (uint32_t)pelf->ehdr.e_entry + adjustment,
392 (uint32_t)pelf->phdr->p_vaddr + adjustment,
393 pelf->phdr->p_filesz, pelf->phdr->p_memsz);
394 /* Need an adjustable buffer. */
395 buffer_clone(&binput, input);
396 buffer_seek(&binput, pelf->phdr->p_offset);
397 bputs(&boutput, buffer_get(&binput), pelf->phdr->p_filesz);
398
399 buffer_clone(&boutput, output);
400 buffer_seek(&boutput, sizeof(struct cbfs_stage));
401
402 /* Make adjustments to all the relocations within the program. */
403 for (i = 0; i < rmodctx->nrelocs; i++) {
404 size_t reloc_offset;
405 uint32_t val;
406 struct buffer in, out;
407
408 /* The relocations represent in-program addresses of the
409 * linked program. Obtain the offset into the program to do
410 * the adjustment. */
411 reloc_offset = rmodctx->emitted_relocs[i] - pelf->phdr->p_vaddr;
412
413 buffer_clone(&out, &boutput);
414 buffer_seek(&out, reloc_offset);
415 buffer_clone(&in, &out);
416 /* Appease around xdr semantics: xdr decrements buffer
417 * size when get()ing and appends to size when put()ing. */
418 buffer_set_size(&out, 0);
419
420 val = xdr_le.get32(&in);
421 DEBUG("reloc %zx %08x -> %08x\n", reloc_offset, val,
422 val + adjustment);
423 xdr_le.put32(&out, val + adjustment);
424 }
425
426 /* Need to back up the location to include cbfs stage metadata. */
427 *location -= sizeof(struct cbfs_stage);
428 ret = 0;
429
430out:
431 rmodule_cleanup(rmodctx);
432 return ret;
433}