blob: 4374bdadf571d24a3d35f63e1a29fc56904e52bb [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
27#include "common.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000028#include "cbfs.h"
Hung-Te Lin4505ceb2013-01-28 22:40:10 +080029#include "elf.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000030
Stefan Reinauer63217582012-10-29 16:52:36 -070031static unsigned int idemp(unsigned int x)
Patrick Georgib203c2f2009-08-20 14:48:03 +000032{
33 return x;
34}
35
Stefan Reinauer5ff7c132011-10-31 12:56:45 -070036/* This is a wrapper around the swab32() macro to make it
Stefan Reinauera1e48242011-10-21 14:24:57 -070037 * usable for the current implementation of parse_elf_to_stage()
38 */
39static unsigned int swap32(unsigned int x)
Patrick Georgib203c2f2009-08-20 14:48:03 +000040{
Stefan Reinauera1e48242011-10-21 14:24:57 -070041 return swab32(x);
Patrick Georgib203c2f2009-08-20 14:48:03 +000042}
43
Patrick Georgib7b56dd82009-09-14 13:29:27 +000044unsigned int (*elf32_to_native) (unsigned int) = idemp;
Patrick Georgib203c2f2009-08-20 14:48:03 +000045
Patrick Georgib7b56dd82009-09-14 13:29:27 +000046/* returns size of result, or -1 if error */
47int parse_elf_to_stage(unsigned char *input, unsigned char **output,
48 comp_algo algo, uint32_t * location)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000049{
50 Elf32_Phdr *phdr;
51 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000052 char *header, *buffer;
53 unsigned char *out;
54
55 int headers;
56 int i;
Peter Stuge1d862de2009-04-14 00:08:34 +000057 struct cbfs_stage *stage;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000058 unsigned int data_start, data_end, mem_end;
59
Patrick Georgib203c2f2009-08-20 14:48:03 +000060 int elf_bigendian = 0;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000061
62 comp_func_ptr compress = compression_function(algo);
63 if (!compress)
64 return -1;
65
66 if (!iself(input)) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +080067 ERROR("The stage file is not in ELF format!\n");
David Hendricks90ca3b62012-11-16 14:48:22 -080068 return -1;
69 }
70
71 if (!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) &&
72 !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +080073 ERROR("The stage file has the wrong architecture\n");
Patrick Georgib7b56dd82009-09-14 13:29:27 +000074 return -1;
75 }
76
77 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
Patrick Georgib203c2f2009-08-20 14:48:03 +000078 elf_bigendian = 1;
79 }
Hung-Te Lin332795c2013-01-28 15:53:34 +080080 if (elf_bigendian != is_big_endian()) {
Patrick Georgib203c2f2009-08-20 14:48:03 +000081 elf32_to_native = swap32;
82 }
83
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000084 headers = ehdr->e_phnum;
85 header = (char *)ehdr;
86
Patrick Georgib203c2f2009-08-20 14:48:03 +000087 phdr = (Elf32_Phdr *) & header[elf32_to_native(ehdr->e_phoff)];
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000088
89 /* Now, regular headers - we only care about PT_LOAD headers,
90 * because thats what we're actually going to load
91 */
92
93 data_start = 0xFFFFFFFF;
94 data_end = 0;
95 mem_end = 0;
96
97 for (i = 0; i < headers; i++) {
98 unsigned int start, mend, rend;
99
Patrick Georgib203c2f2009-08-20 14:48:03 +0000100 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000101 continue;
102
103 /* Empty segments are never interesting */
Patrick Georgib203c2f2009-08-20 14:48:03 +0000104 if (elf32_to_native(phdr[i].p_memsz) == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000105 continue;
106
107 /* BSS */
108
Patrick Georgib203c2f2009-08-20 14:48:03 +0000109 start = elf32_to_native(phdr[i].p_paddr);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000110
Patrick Georgib203c2f2009-08-20 14:48:03 +0000111 mend = start + elf32_to_native(phdr[i].p_memsz);
112 rend = start + elf32_to_native(phdr[i].p_filesz);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000113
114 if (start < data_start)
115 data_start = start;
116
117 if (rend > data_end)
118 data_end = rend;
119
120 if (mend > mem_end)
121 mem_end = mend;
122 }
123
Patrick Georgi9341acd2009-12-23 12:52:56 +0000124 if (data_start < *location) {
125 data_start = *location;
126 }
127
Patrick Georgia6c337d2010-02-03 17:56:37 +0000128 if (data_end <= data_start) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800129 ERROR("data ends before it starts. Make sure the "
Stefan Reinauer63217582012-10-29 16:52:36 -0700130 "ELF file is correct and resides in ROM space.\n");
Patrick Georgia6c337d2010-02-03 17:56:37 +0000131 exit(1);
132 }
133
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000134 /* allocate an intermediate buffer for the data */
135 buffer = calloc(data_end - data_start, 1);
136
137 if (buffer == NULL) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800138 ERROR("Unable to allocate memory: %m\n");
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000139 return -1;
140 }
141
142 /* Copy the file data into the buffer */
143
144 for (i = 0; i < headers; i++) {
Patrick Georgi9341acd2009-12-23 12:52:56 +0000145 unsigned int l_start, l_offset = 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000146
Patrick Georgib203c2f2009-08-20 14:48:03 +0000147 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000148 continue;
149
Patrick Georgib203c2f2009-08-20 14:48:03 +0000150 if (elf32_to_native(phdr[i].p_memsz) == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000151 continue;
152
Patrick Georgi9341acd2009-12-23 12:52:56 +0000153 l_start = elf32_to_native(phdr[i].p_paddr);
154 if (l_start < *location) {
155 l_offset = *location - l_start;
156 l_start = *location;
157 }
158
159 memcpy(buffer + (l_start - data_start),
160 &header[elf32_to_native(phdr[i].p_offset)+l_offset],
161 elf32_to_native(phdr[i].p_filesz)-l_offset);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000162 }
163
164 /* Now make the output buffer */
Peter Stuge1d862de2009-04-14 00:08:34 +0000165 out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000166
167 if (out == NULL) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800168 ERROR("Unable to allocate memory: %m\n");
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000169 return -1;
170 }
171
Peter Stuge1d862de2009-04-14 00:08:34 +0000172 stage = (struct cbfs_stage *)out;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000173
Stefan Reinauera1e48242011-10-21 14:24:57 -0700174 stage->load = data_start; /* FIXME: htonll */
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000175 stage->memlen = mem_end - data_start;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000176 stage->compression = algo;
Stefan Reinauera1e48242011-10-21 14:24:57 -0700177 stage->entry = ehdr->e_entry; /* FIXME: htonll */
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000178
179 compress(buffer, data_end - data_start,
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000180 (char *)(out + sizeof(struct cbfs_stage)), (int *)&stage->len);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000181
Stefan Reinauer63217582012-10-29 16:52:36 -0700182 free(buffer);
183
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000184 *output = out;
185
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000186 if (*location)
187 *location -= sizeof(struct cbfs_stage);
Peter Stuge1d862de2009-04-14 00:08:34 +0000188 return sizeof(struct cbfs_stage) + stage->len;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000189}