blob: be8573861ba8846c9fe24b94867a66ac4f8f7858 [file] [log] [blame]
Patrick Georgi7333a112020-05-08 20:48:04 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00002
Francis Rowe3fb8b0d2014-11-21 02:38:48 +00003#include <inttypes.h>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00004#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00007
Aaron Durbin54ef3062014-03-05 12:12:09 -06008#include "elfparsing.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00009#include "common.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000010#include "cbfs.h"
Aaron Durbin4be16742015-09-15 17:00:23 -050011#include "rmodule.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000012
Furquan Shaikh405304a2014-10-30 11:44:20 -070013/* Checks if program segment contains the ignored section */
14static int is_phdr_ignored(Elf64_Phdr *phdr, Elf64_Shdr *shdr)
15{
16 /* If no ignored section, return false. */
17 if (shdr == NULL)
18 return 0;
19
20 Elf64_Addr sh_start = shdr->sh_addr;
21 Elf64_Addr sh_end = shdr->sh_addr + shdr->sh_size;
22 Elf64_Addr ph_start = phdr->p_vaddr;
23 Elf64_Addr ph_end = phdr->p_vaddr + phdr->p_memsz;
24
25 /* Return true only if section occupies whole of segment. */
26 if ((sh_start == ph_start) && (sh_end == ph_end)) {
Francis Rowe3fb8b0d2014-11-21 02:38:48 +000027 DEBUG("Ignoring program segment at 0x%" PRIx64 "\n", ph_start);
Furquan Shaikh405304a2014-10-30 11:44:20 -070028 return 1;
29 }
30
31 /* If shdr intersects phdr at all, its a conflict */
32 if (((sh_start >= ph_start) && (sh_start <= ph_end)) ||
33 ((sh_end >= ph_start) && (sh_end <= ph_end))) {
34 ERROR("Conflicting sections in segment\n");
35 exit(1);
36 }
37
38 /* Program header doesn't need to be ignored. */
39 return 0;
40}
41
42/* Find section header based on ignored section name */
43static Elf64_Shdr *find_ignored_section_header(struct parsed_elf *pelf,
44 const char *ignore_section)
45{
46 int i;
47 const char *shstrtab;
48
49 /* No section needs to be ignored */
50 if (ignore_section == NULL)
51 return NULL;
52
53 DEBUG("Section to be ignored: %s\n", ignore_section);
54
55 /* Get pointer to string table */
56 shstrtab = buffer_get(pelf->strtabs[pelf->ehdr.e_shstrndx]);
57
58 for (i = 0; i < pelf->ehdr.e_shnum; i++) {
59 Elf64_Shdr *shdr;
60 const char *section_name;
61
62 shdr = &pelf->shdr[i];
63 section_name = &shstrtab[shdr->sh_name];
64
65 /* If section name matches ignored string, return shdr */
66 if (strcmp(section_name, ignore_section) == 0)
67 return shdr;
68 }
69
70 /* No section matches ignore string */
71 return NULL;
72}
73
Julius Werner81dc20e2020-10-15 17:37:57 -070074static int fill_cbfs_stageheader(struct cbfs_file_attr_stageheader *stageheader,
75 uint64_t entry, uint64_t loadaddr,
76 uint32_t memsize)
Aaron Durbin4be16742015-09-15 17:00:23 -050077{
Julius Werner81dc20e2020-10-15 17:37:57 -070078 if (entry - loadaddr >= memsize) {
79 ERROR("stage entry point out of bounds!\n");
80 return -1;
81 }
82
Alex James02001a382021-12-19 16:41:59 -060083 stageheader->loadaddr = htobe64(loadaddr);
84 stageheader->memlen = htobe32(memsize);
85 stageheader->entry_offset = htobe32(entry - loadaddr);
Julius Werner81dc20e2020-10-15 17:37:57 -070086
87 return 0;
Aaron Durbin4be16742015-09-15 17:00:23 -050088}
89
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080090/* returns size of result, or -1 if error.
91 * Note that, with the new code, this function
92 * works for all elf files, not just the restricted set.
93 */
94int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
Julius Werner81dc20e2020-10-15 17:37:57 -070095 const char *ignore_section,
96 struct cbfs_file_attr_stageheader *stageheader)
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080097{
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -070098 struct parsed_elf pelf;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080099 Elf64_Phdr *phdr;
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700100 Elf64_Ehdr *ehdr;
Furquan Shaikh405304a2014-10-30 11:44:20 -0700101 Elf64_Shdr *shdr_ignored;
Furquan Shaikhf7a5b562015-05-29 12:46:18 -0700102 Elf64_Addr virt_to_phys;
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700103 int ret = -1;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800104
105 int headers;
Julius Werner81dc20e2020-10-15 17:37:57 -0700106 int i;
HC Yen14ec1992015-01-22 09:57:34 +0800107 uint64_t data_start, data_end, mem_end;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800108
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700109 int flags = ELF_PARSE_PHDR | ELF_PARSE_SHDR | ELF_PARSE_STRTAB;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800110
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700111 if (parse_elf(input, &pelf, flags)) {
112 ERROR("Couldn't parse ELF\n");
113 return -1;
114 }
115
116 ehdr = &pelf.ehdr;
117 phdr = &pelf.phdr[0];
118
Furquan Shaikh405304a2014-10-30 11:44:20 -0700119 /* Find the section header corresponding to ignored-section */
120 shdr_ignored = find_ignored_section_header(&pelf, ignore_section);
121
122 if (ignore_section && (shdr_ignored == NULL))
123 WARN("Ignore section not found\n");
124
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700125 headers = ehdr->e_phnum;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800126
Furquan Shaikh405304a2014-10-30 11:44:20 -0700127 /* Ignore the program header containing ignored section */
128 for (i = 0; i < headers; i++) {
129 if (is_phdr_ignored(&phdr[i], shdr_ignored))
130 phdr[i].p_type = PT_NULL;
131 }
132
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800133 data_start = ~0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000134 data_end = 0;
135 mem_end = 0;
Furquan Shaikhf7a5b562015-05-29 12:46:18 -0700136 virt_to_phys = 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000137
138 for (i = 0; i < headers; i++) {
HC Yen14ec1992015-01-22 09:57:34 +0800139 uint64_t start, mend, rend;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000140
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800141 if (phdr[i].p_type != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000142 continue;
143
144 /* Empty segments are never interesting */
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800145 if (phdr[i].p_memsz == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000146 continue;
147
148 /* BSS */
149
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800150 start = phdr[i].p_paddr;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000151
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800152 mend = start + phdr[i].p_memsz;
153 rend = start + phdr[i].p_filesz;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000154
155 if (start < data_start)
156 data_start = start;
157
158 if (rend > data_end)
159 data_end = rend;
160
161 if (mend > mem_end)
162 mem_end = mend;
Furquan Shaikhf7a5b562015-05-29 12:46:18 -0700163
164 if (virt_to_phys == 0)
165 virt_to_phys = phdr[i].p_paddr - phdr[i].p_vaddr;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000166 }
167
Patrick Georgia6c337d2010-02-03 17:56:37 +0000168 if (data_end <= data_start) {
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800169 ERROR("data ends (%08lx) before it starts (%08lx). Make sure "
170 "the ELF file is correct and resides in ROM space.\n",
171 (unsigned long)data_end, (unsigned long)data_start);
Patrick Georgia6c337d2010-02-03 17:56:37 +0000172 exit(1);
173 }
174
Julius Werner81dc20e2020-10-15 17:37:57 -0700175 if (buffer_create(output, data_end - data_start, input->name) != 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800176 ERROR("Unable to allocate memory: %m\n");
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700177 goto err;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000178 }
Julius Werner81dc20e2020-10-15 17:37:57 -0700179 memset(output->data, 0, output->size);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000180
Julius Werner81dc20e2020-10-15 17:37:57 -0700181 /* Copy the file data into the output buffer */
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000182
183 for (i = 0; i < headers; i++) {
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800184 if (phdr[i].p_type != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000185 continue;
186
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800187 if (phdr[i].p_memsz == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000188 continue;
189
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800190 /* A legal ELF file can have a program header with
191 * non-zero length but zero-length file size and a
192 * non-zero offset which, added together, are > than
193 * input->size (i.e. the total file size). So we need
194 * to not even test in the case that p_filesz is zero.
195 */
Julius Wernerff61a392021-01-12 15:21:03 -0800196 if (!phdr[i].p_filesz)
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800197 continue;
198 if (input->size < (phdr[i].p_offset + phdr[i].p_filesz)){
199 ERROR("Underflow copying out the segment."
Paul Menzel470c37c2014-03-16 00:15:57 +0100200 "File has %zu bytes left, segment end is %zu\n",
201 input->size, (size_t)(phdr[i].p_offset + phdr[i].p_filesz));
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700202 goto err;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800203 }
Julius Werner81dc20e2020-10-15 17:37:57 -0700204 memcpy(&output->data[phdr[i].p_paddr - data_start],
Julius Wernerff61a392021-01-12 15:21:03 -0800205 &input->data[phdr[i].p_offset],
206 phdr[i].p_filesz);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000207 }
208
Martin Rotha5648112017-06-03 20:05:42 -0600209 /* coreboot expects entry point to be physical address. Thus, adjust the
Julius Werner81dc20e2020-10-15 17:37:57 -0700210 entry point accordingly. */
211 ret = fill_cbfs_stageheader(stageheader, ehdr->e_entry + virt_to_phys,
212 data_start, mem_end - data_start);
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700213err:
214 parsed_elf_destroy(&pelf);
215 return ret;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000216}
Aaron Durbin4be16742015-09-15 17:00:23 -0500217
218struct xip_context {
219 struct rmod_context rmodctx;
220 size_t ignored_section_idx;
221 Elf64_Shdr *ignored_section;
222};
223
224static int rmod_filter(struct reloc_filter *f, const Elf64_Rela *r)
225{
226 size_t symbol_index;
227 int reloc_type;
228 struct parsed_elf *pelf;
229 Elf64_Sym *sym;
230 struct xip_context *xipctx;
231
232 xipctx = f->context;
233 pelf = &xipctx->rmodctx.pelf;
234
235 /* Allow everything through if there isn't an ignored section. */
236 if (xipctx->ignored_section == NULL)
237 return 1;
238
239 reloc_type = ELF64_R_TYPE(r->r_info);
240 symbol_index = ELF64_R_SYM(r->r_info);
241 sym = &pelf->syms[symbol_index];
242
243 /* Nothing to filter. Relocation is not being applied to the
244 * ignored section. */
245 if (sym->st_shndx != xipctx->ignored_section_idx)
246 return 1;
247
248 /* If there is any relocation to the ignored section that isn't
249 * absolute fail as current assumptions are that all relocations
250 * are absolute. */
Patrick Rudolph21046a32018-11-26 15:37:51 +0100251 if ((reloc_type != R_386_32) &&
252 (reloc_type != R_AMD64_64) &&
253 (reloc_type != R_AMD64_32)) {
Aaron Durbin4be16742015-09-15 17:00:23 -0500254 ERROR("Invalid reloc to ignored section: %x\n", reloc_type);
255 return -1;
256 }
257
258 /* Relocation referencing ignored section. Don't emit it. */
259 return 0;
260}
261
262int parse_elf_to_xip_stage(const struct buffer *input, struct buffer *output,
Arthur Heymans5bb7dc42021-06-22 15:21:46 +0200263 uint32_t location, const char *ignore_section,
Julius Werner81dc20e2020-10-15 17:37:57 -0700264 struct cbfs_file_attr_stageheader *stageheader)
Aaron Durbin4be16742015-09-15 17:00:23 -0500265{
266 struct xip_context xipctx;
267 struct rmod_context *rmodctx;
268 struct reloc_filter filter;
269 struct parsed_elf *pelf;
Aaron Durbin4be16742015-09-15 17:00:23 -0500270 uint32_t adjustment;
271 struct buffer binput;
272 struct buffer boutput;
273 Elf64_Xword i;
274 int ret = -1;
275
276 xipctx.ignored_section_idx = 0;
277 rmodctx = &xipctx.rmodctx;
278 pelf = &rmodctx->pelf;
279
280 if (rmodule_init(rmodctx, input))
281 return -1;
282
Patrick Rudolph21046a32018-11-26 15:37:51 +0100283 /* Only support x86 / x86_64 XIP currently. */
284 if ((rmodctx->pelf.ehdr.e_machine != EM_386) &&
285 (rmodctx->pelf.ehdr.e_machine != EM_X86_64)) {
286 ERROR("Only support XIP stages for x86/x86_64\n");
Aaron Durbin4be16742015-09-15 17:00:23 -0500287 goto out;
288 }
289
290 xipctx.ignored_section =
291 find_ignored_section_header(pelf, ignore_section);
292
293 if (xipctx.ignored_section != NULL)
294 xipctx.ignored_section_idx =
295 xipctx.ignored_section - pelf->shdr;
296
297 filter.filter = rmod_filter;
298 filter.context = &xipctx;
299
300 if (rmodule_collect_relocations(rmodctx, &filter))
301 goto out;
302
Julius Werner81dc20e2020-10-15 17:37:57 -0700303 if (buffer_create(output, pelf->phdr->p_filesz, input->name) != 0) {
Aaron Durbin4be16742015-09-15 17:00:23 -0500304 ERROR("Unable to allocate memory: %m\n");
305 goto out;
306 }
307 buffer_clone(&boutput, output);
Julius Werner81dc20e2020-10-15 17:37:57 -0700308 memset(buffer_get(&boutput), 0, pelf->phdr->p_filesz);
Aaron Durbin4be16742015-09-15 17:00:23 -0500309 buffer_set_size(&boutput, 0);
310
311 /* Single loadable segment. The entire segment moves to final
312 * location from based on virtual address of loadable segment. */
Arthur Heymans5bb7dc42021-06-22 15:21:46 +0200313 adjustment = location - pelf->phdr->p_vaddr;
Aaron Durbin4be16742015-09-15 17:00:23 -0500314 DEBUG("Relocation adjustment: %08x\n", adjustment);
315
Julius Werner81dc20e2020-10-15 17:37:57 -0700316 fill_cbfs_stageheader(stageheader,
317 (uint32_t)pelf->ehdr.e_entry + adjustment,
318 (uint32_t)pelf->phdr->p_vaddr + adjustment,
319 pelf->phdr->p_memsz);
Aaron Durbin4be16742015-09-15 17:00:23 -0500320 /* Need an adjustable buffer. */
321 buffer_clone(&binput, input);
322 buffer_seek(&binput, pelf->phdr->p_offset);
323 bputs(&boutput, buffer_get(&binput), pelf->phdr->p_filesz);
324
325 buffer_clone(&boutput, output);
Aaron Durbin4be16742015-09-15 17:00:23 -0500326
327 /* Make adjustments to all the relocations within the program. */
328 for (i = 0; i < rmodctx->nrelocs; i++) {
329 size_t reloc_offset;
330 uint32_t val;
331 struct buffer in, out;
332
333 /* The relocations represent in-program addresses of the
334 * linked program. Obtain the offset into the program to do
335 * the adjustment. */
336 reloc_offset = rmodctx->emitted_relocs[i] - pelf->phdr->p_vaddr;
337
338 buffer_clone(&out, &boutput);
339 buffer_seek(&out, reloc_offset);
340 buffer_clone(&in, &out);
341 /* Appease around xdr semantics: xdr decrements buffer
342 * size when get()ing and appends to size when put()ing. */
343 buffer_set_size(&out, 0);
344
345 val = xdr_le.get32(&in);
346 DEBUG("reloc %zx %08x -> %08x\n", reloc_offset, val,
347 val + adjustment);
348 xdr_le.put32(&out, val + adjustment);
349 }
350
Aaron Durbin4be16742015-09-15 17:00:23 -0500351 ret = 0;
352
353out:
354 rmodule_cleanup(rmodctx);
355 return ret;
356}