blob: 845933487f39fd93578fc6bf40fcf301f0d8d644 [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.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000026
Aaron Durbin54ef3062014-03-05 12:12:09 -060027#include "elfparsing.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000028#include "common.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000029#include "cbfs.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000030
Furquan Shaikh405304a2014-10-30 11:44:20 -070031/* Checks if program segment contains the ignored section */
32static int is_phdr_ignored(Elf64_Phdr *phdr, Elf64_Shdr *shdr)
33{
34 /* If no ignored section, return false. */
35 if (shdr == NULL)
36 return 0;
37
38 Elf64_Addr sh_start = shdr->sh_addr;
39 Elf64_Addr sh_end = shdr->sh_addr + shdr->sh_size;
40 Elf64_Addr ph_start = phdr->p_vaddr;
41 Elf64_Addr ph_end = phdr->p_vaddr + phdr->p_memsz;
42
43 /* Return true only if section occupies whole of segment. */
44 if ((sh_start == ph_start) && (sh_end == ph_end)) {
45 DEBUG("Ignoring program segment at %p\n", (void *)ph_start);
46 return 1;
47 }
48
49 /* If shdr intersects phdr at all, its a conflict */
50 if (((sh_start >= ph_start) && (sh_start <= ph_end)) ||
51 ((sh_end >= ph_start) && (sh_end <= ph_end))) {
52 ERROR("Conflicting sections in segment\n");
53 exit(1);
54 }
55
56 /* Program header doesn't need to be ignored. */
57 return 0;
58}
59
60/* Find section header based on ignored section name */
61static Elf64_Shdr *find_ignored_section_header(struct parsed_elf *pelf,
62 const char *ignore_section)
63{
64 int i;
65 const char *shstrtab;
66
67 /* No section needs to be ignored */
68 if (ignore_section == NULL)
69 return NULL;
70
71 DEBUG("Section to be ignored: %s\n", ignore_section);
72
73 /* Get pointer to string table */
74 shstrtab = buffer_get(pelf->strtabs[pelf->ehdr.e_shstrndx]);
75
76 for (i = 0; i < pelf->ehdr.e_shnum; i++) {
77 Elf64_Shdr *shdr;
78 const char *section_name;
79
80 shdr = &pelf->shdr[i];
81 section_name = &shstrtab[shdr->sh_name];
82
83 /* If section name matches ignored string, return shdr */
84 if (strcmp(section_name, ignore_section) == 0)
85 return shdr;
86 }
87
88 /* No section matches ignore string */
89 return NULL;
90}
91
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080092/* returns size of result, or -1 if error.
93 * Note that, with the new code, this function
94 * works for all elf files, not just the restricted set.
95 */
96int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
Furquan Shaikh405304a2014-10-30 11:44:20 -070097 uint32_t arch, comp_algo algo, uint32_t *location,
98 const char *ignore_section)
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080099{
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700100 struct parsed_elf pelf;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800101 Elf64_Phdr *phdr;
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700102 Elf64_Ehdr *ehdr;
Furquan Shaikh405304a2014-10-30 11:44:20 -0700103 Elf64_Shdr *shdr_ignored;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800104 char *buffer;
105 struct buffer outheader;
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700106 int ret = -1;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800107
108 int headers;
109 int i, outlen;
110 uint32_t data_start, data_end, mem_end;
111
112 comp_func_ptr compress = compression_function(algo);
113 if (!compress)
114 return -1;
115
116 DEBUG("start: parse_elf_to_stage(location=0x%x)\n", *location);
117
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700118 int flags = ELF_PARSE_PHDR | ELF_PARSE_SHDR | ELF_PARSE_STRTAB;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800119
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700120 if (parse_elf(input, &pelf, flags)) {
121 ERROR("Couldn't parse ELF\n");
122 return -1;
123 }
124
125 ehdr = &pelf.ehdr;
126 phdr = &pelf.phdr[0];
127
Furquan Shaikh405304a2014-10-30 11:44:20 -0700128 /* Find the section header corresponding to ignored-section */
129 shdr_ignored = find_ignored_section_header(&pelf, ignore_section);
130
131 if (ignore_section && (shdr_ignored == NULL))
132 WARN("Ignore section not found\n");
133
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700134 headers = ehdr->e_phnum;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800135
Furquan Shaikh405304a2014-10-30 11:44:20 -0700136 /* Ignore the program header containing ignored section */
137 for (i = 0; i < headers; i++) {
138 if (is_phdr_ignored(&phdr[i], shdr_ignored))
139 phdr[i].p_type = PT_NULL;
140 }
141
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800142 data_start = ~0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000143 data_end = 0;
144 mem_end = 0;
145
146 for (i = 0; i < headers; i++) {
147 unsigned int start, mend, rend;
148
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800149 if (phdr[i].p_type != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000150 continue;
151
152 /* Empty segments are never interesting */
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800153 if (phdr[i].p_memsz == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000154 continue;
155
156 /* BSS */
157
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800158 start = phdr[i].p_paddr;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000159
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800160 mend = start + phdr[i].p_memsz;
161 rend = start + phdr[i].p_filesz;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000162
163 if (start < data_start)
164 data_start = start;
165
166 if (rend > data_end)
167 data_end = rend;
168
169 if (mend > mem_end)
170 mem_end = mend;
171 }
172
Patrick Georgi9341acd2009-12-23 12:52:56 +0000173 if (data_start < *location) {
174 data_start = *location;
175 }
176
Patrick Georgia6c337d2010-02-03 17:56:37 +0000177 if (data_end <= data_start) {
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800178 ERROR("data ends (%08lx) before it starts (%08lx). Make sure "
179 "the ELF file is correct and resides in ROM space.\n",
180 (unsigned long)data_end, (unsigned long)data_start);
Patrick Georgia6c337d2010-02-03 17:56:37 +0000181 exit(1);
182 }
183
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000184 /* allocate an intermediate buffer for the data */
185 buffer = calloc(data_end - data_start, 1);
186
187 if (buffer == NULL) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800188 ERROR("Unable to allocate memory: %m\n");
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700189 goto err;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000190 }
191
192 /* Copy the file data into the buffer */
193
194 for (i = 0; i < headers; i++) {
Patrick Georgi9341acd2009-12-23 12:52:56 +0000195 unsigned int l_start, l_offset = 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000196
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800197 if (phdr[i].p_type != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000198 continue;
199
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800200 if (phdr[i].p_memsz == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000201 continue;
202
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800203 l_start = phdr[i].p_paddr;
Patrick Georgi9341acd2009-12-23 12:52:56 +0000204 if (l_start < *location) {
205 l_offset = *location - l_start;
206 l_start = *location;
207 }
208
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800209 /* A legal ELF file can have a program header with
210 * non-zero length but zero-length file size and a
211 * non-zero offset which, added together, are > than
212 * input->size (i.e. the total file size). So we need
213 * to not even test in the case that p_filesz is zero.
214 */
215 if (! phdr[i].p_filesz)
216 continue;
217 if (input->size < (phdr[i].p_offset + phdr[i].p_filesz)){
218 ERROR("Underflow copying out the segment."
Paul Menzel470c37c2014-03-16 00:15:57 +0100219 "File has %zu bytes left, segment end is %zu\n",
220 input->size, (size_t)(phdr[i].p_offset + phdr[i].p_filesz));
Daniele Forsi8e898472014-07-27 12:01:40 +0200221 free(buffer);
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700222 goto err;
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800223 }
Patrick Georgi9341acd2009-12-23 12:52:56 +0000224 memcpy(buffer + (l_start - data_start),
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800225 &input->data[phdr[i].p_offset + l_offset],
226 phdr[i].p_filesz - l_offset);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000227 }
228
229 /* Now make the output buffer */
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800230 if (buffer_create(output, sizeof(struct cbfs_stage) + data_end - data_start,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800231 input->name) != 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800232 ERROR("Unable to allocate memory: %m\n");
Paul Menzel2c8f81b2013-04-11 10:45:11 +0200233 free(buffer);
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700234 goto err;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000235 }
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800236 memset(output->data, 0, output->size);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000237
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800238 /* Compress the data, at which point we'll know information
239 * to fill out the header. This seems backward but it works because
240 * - the output header is a known size (not always true in many xdr's)
241 * - we do need to know the compressed output size first
Gabe Black845aa142014-02-21 01:01:06 -0800242 * If compression fails or makes the data bigger, we'll warn about it
243 * and use the original data.
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800244 */
Gabe Blackdbd006b2014-02-20 23:38:49 -0800245 if (compress(buffer, data_end - data_start,
246 (output->data + sizeof(struct cbfs_stage)),
Gabe Black845aa142014-02-21 01:01:06 -0800247 &outlen) < 0 || outlen > data_end - data_start) {
248 WARN("Compression failed or would make the data bigger "
249 "- disabled.\n");
250 memcpy(output->data + sizeof(struct cbfs_stage),
251 buffer, data_end - data_start);
252 algo = CBFS_COMPRESS_NONE;
Gabe Blackdbd006b2014-02-20 23:38:49 -0800253 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700254 free(buffer);
255
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800256 /* Set up for output marshaling. */
257 outheader.data = output->data;
258 outheader.size = 0;
259 /* N.B. The original plan was that SELF data was B.E.
260 * but: this is all L.E.
261 * Maybe we should just change the spec.
262 */
263 xdr_le.put32(&outheader, algo);
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700264 xdr_le.put64(&outheader, ehdr->e_entry);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800265 xdr_le.put64(&outheader, data_start);
266 xdr_le.put32(&outheader, outlen);
267 xdr_le.put32(&outheader, mem_end - data_start);
268
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000269 if (*location)
270 *location -= sizeof(struct cbfs_stage);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800271 output->size = sizeof(struct cbfs_stage) + outlen;
Furquan Shaikhcc6f84c2014-10-30 11:28:27 -0700272 ret = 0;
273
274err:
275 parsed_elf_destroy(&pelf);
276 return ret;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000277}