blob: 4a2f4d8dee969ae8b6089ea51e2d75fdda6f1c8f [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
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080031/* returns size of result, or -1 if error.
32 * Note that, with the new code, this function
33 * works for all elf files, not just the restricted set.
34 */
35int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060036 uint32_t arch, comp_algo algo, uint32_t *location)
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080037{
38 Elf64_Phdr *phdr;
39 Elf64_Ehdr ehdr;
40 char *buffer;
41 struct buffer outheader;
42
43 int headers;
44 int i, outlen;
45 uint32_t data_start, data_end, mem_end;
46
47 comp_func_ptr compress = compression_function(algo);
48 if (!compress)
49 return -1;
50
51 DEBUG("start: parse_elf_to_stage(location=0x%x)\n", *location);
52
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060053 if (elf_headers(input, arch, &ehdr, &phdr, NULL) < 0)
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080054 return -1;
55
56 headers = ehdr.e_phnum;
57
58 data_start = ~0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000059 data_end = 0;
60 mem_end = 0;
61
62 for (i = 0; i < headers; i++) {
63 unsigned int start, mend, rend;
64
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080065 if (phdr[i].p_type != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000066 continue;
67
68 /* Empty segments are never interesting */
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080069 if (phdr[i].p_memsz == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000070 continue;
71
72 /* BSS */
73
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080074 start = phdr[i].p_paddr;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000075
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080076 mend = start + phdr[i].p_memsz;
77 rend = start + phdr[i].p_filesz;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000078
79 if (start < data_start)
80 data_start = start;
81
82 if (rend > data_end)
83 data_end = rend;
84
85 if (mend > mem_end)
86 mem_end = mend;
87 }
88
Patrick Georgi9341acd2009-12-23 12:52:56 +000089 if (data_start < *location) {
90 data_start = *location;
91 }
92
Patrick Georgia6c337d2010-02-03 17:56:37 +000093 if (data_end <= data_start) {
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080094 ERROR("data ends (%08lx) before it starts (%08lx). Make sure "
95 "the ELF file is correct and resides in ROM space.\n",
96 (unsigned long)data_end, (unsigned long)data_start);
Patrick Georgia6c337d2010-02-03 17:56:37 +000097 exit(1);
98 }
99
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000100 /* allocate an intermediate buffer for the data */
101 buffer = calloc(data_end - data_start, 1);
102
103 if (buffer == NULL) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800104 ERROR("Unable to allocate memory: %m\n");
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000105 return -1;
106 }
107
108 /* Copy the file data into the buffer */
109
110 for (i = 0; i < headers; i++) {
Patrick Georgi9341acd2009-12-23 12:52:56 +0000111 unsigned int l_start, l_offset = 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000112
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800113 if (phdr[i].p_type != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000114 continue;
115
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800116 if (phdr[i].p_memsz == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000117 continue;
118
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800119 l_start = phdr[i].p_paddr;
Patrick Georgi9341acd2009-12-23 12:52:56 +0000120 if (l_start < *location) {
121 l_offset = *location - l_start;
122 l_start = *location;
123 }
124
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800125 /* A legal ELF file can have a program header with
126 * non-zero length but zero-length file size and a
127 * non-zero offset which, added together, are > than
128 * input->size (i.e. the total file size). So we need
129 * to not even test in the case that p_filesz is zero.
130 */
131 if (! phdr[i].p_filesz)
132 continue;
133 if (input->size < (phdr[i].p_offset + phdr[i].p_filesz)){
134 ERROR("Underflow copying out the segment."
Paul Menzel470c37c2014-03-16 00:15:57 +0100135 "File has %zu bytes left, segment end is %zu\n",
136 input->size, (size_t)(phdr[i].p_offset + phdr[i].p_filesz));
Daniele Forsi8e898472014-07-27 12:01:40 +0200137 free(buffer);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800138 return -1;
139 }
Patrick Georgi9341acd2009-12-23 12:52:56 +0000140 memcpy(buffer + (l_start - data_start),
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800141 &input->data[phdr[i].p_offset + l_offset],
142 phdr[i].p_filesz - l_offset);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000143 }
144
145 /* Now make the output buffer */
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800146 if (buffer_create(output, sizeof(struct cbfs_stage) + data_end - data_start,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800147 input->name) != 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800148 ERROR("Unable to allocate memory: %m\n");
Paul Menzel2c8f81b2013-04-11 10:45:11 +0200149 free(buffer);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000150 return -1;
151 }
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800152 memset(output->data, 0, output->size);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000153
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800154 /* Compress the data, at which point we'll know information
155 * to fill out the header. This seems backward but it works because
156 * - the output header is a known size (not always true in many xdr's)
157 * - we do need to know the compressed output size first
Gabe Black845aa142014-02-21 01:01:06 -0800158 * If compression fails or makes the data bigger, we'll warn about it
159 * and use the original data.
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800160 */
Gabe Blackdbd006b2014-02-20 23:38:49 -0800161 if (compress(buffer, data_end - data_start,
162 (output->data + sizeof(struct cbfs_stage)),
Gabe Black845aa142014-02-21 01:01:06 -0800163 &outlen) < 0 || outlen > data_end - data_start) {
164 WARN("Compression failed or would make the data bigger "
165 "- disabled.\n");
166 memcpy(output->data + sizeof(struct cbfs_stage),
167 buffer, data_end - data_start);
168 algo = CBFS_COMPRESS_NONE;
Gabe Blackdbd006b2014-02-20 23:38:49 -0800169 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700170 free(buffer);
171
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800172 /* Set up for output marshaling. */
173 outheader.data = output->data;
174 outheader.size = 0;
175 /* N.B. The original plan was that SELF data was B.E.
176 * but: this is all L.E.
177 * Maybe we should just change the spec.
178 */
179 xdr_le.put32(&outheader, algo);
180 xdr_le.put64(&outheader, ehdr.e_entry);
181 xdr_le.put64(&outheader, data_start);
182 xdr_le.put32(&outheader, outlen);
183 xdr_le.put32(&outheader, mem_end - data_start);
184
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000185 if (*location)
186 *location -= sizeof(struct cbfs_stage);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800187 output->size = sizeof(struct cbfs_stage) + outlen;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800188 return 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000189}