blob: 0541d4b48e64c8ba72aac5a74b4e8d61f1570e5c [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>
26#include <unistd.h>
27#include "elf.h"
28#include <fcntl.h>
29#include <getopt.h>
30#include <sys/stat.h>
31
32#include "common.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000033#include "cbfs.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000034
Stefan Reinauer63217582012-10-29 16:52:36 -070035static unsigned int idemp(unsigned int x)
Patrick Georgib203c2f2009-08-20 14:48:03 +000036{
37 return x;
38}
39
Stefan Reinauer5ff7c132011-10-31 12:56:45 -070040/* This is a wrapper around the swab32() macro to make it
Stefan Reinauera1e48242011-10-21 14:24:57 -070041 * usable for the current implementation of parse_elf_to_stage()
42 */
43static unsigned int swap32(unsigned int x)
Patrick Georgib203c2f2009-08-20 14:48:03 +000044{
Stefan Reinauera1e48242011-10-21 14:24:57 -070045 return swab32(x);
Patrick Georgib203c2f2009-08-20 14:48:03 +000046}
47
Patrick Georgib7b56dd82009-09-14 13:29:27 +000048unsigned int (*elf32_to_native) (unsigned int) = idemp;
Patrick Georgib203c2f2009-08-20 14:48:03 +000049
Patrick Georgib7b56dd82009-09-14 13:29:27 +000050/* returns size of result, or -1 if error */
51int parse_elf_to_stage(unsigned char *input, unsigned char **output,
52 comp_algo algo, uint32_t * location)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000053{
54 Elf32_Phdr *phdr;
55 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000056 char *header, *buffer;
57 unsigned char *out;
58
59 int headers;
60 int i;
Peter Stuge1d862de2009-04-14 00:08:34 +000061 struct cbfs_stage *stage;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000062 unsigned int data_start, data_end, mem_end;
63
Patrick Georgib203c2f2009-08-20 14:48:03 +000064 int elf_bigendian = 0;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000065
66 comp_func_ptr compress = compression_function(algo);
67 if (!compress)
68 return -1;
69
70 if (!iself(input)) {
David Hendricks90ca3b62012-11-16 14:48:22 -080071 fprintf(stderr, "E: The stage file is not in ELF format!\n");
72 return -1;
73 }
74
75 if (!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) &&
76 !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) {
77 fprintf(stderr, "E: The stage file has the wrong architecture\n");
Patrick Georgib7b56dd82009-09-14 13:29:27 +000078 return -1;
79 }
80
81 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
Patrick Georgib203c2f2009-08-20 14:48:03 +000082 elf_bigendian = 1;
83 }
Patrick Georgib203c2f2009-08-20 14:48:03 +000084 if (elf_bigendian != host_bigendian) {
85 elf32_to_native = swap32;
86 }
87
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000088 headers = ehdr->e_phnum;
89 header = (char *)ehdr;
90
Patrick Georgib203c2f2009-08-20 14:48:03 +000091 phdr = (Elf32_Phdr *) & header[elf32_to_native(ehdr->e_phoff)];
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000092
93 /* Now, regular headers - we only care about PT_LOAD headers,
94 * because thats what we're actually going to load
95 */
96
97 data_start = 0xFFFFFFFF;
98 data_end = 0;
99 mem_end = 0;
100
101 for (i = 0; i < headers; i++) {
102 unsigned int start, mend, rend;
103
Patrick Georgib203c2f2009-08-20 14:48:03 +0000104 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000105 continue;
106
107 /* Empty segments are never interesting */
Patrick Georgib203c2f2009-08-20 14:48:03 +0000108 if (elf32_to_native(phdr[i].p_memsz) == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000109 continue;
110
111 /* BSS */
112
Patrick Georgib203c2f2009-08-20 14:48:03 +0000113 start = elf32_to_native(phdr[i].p_paddr);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000114
Patrick Georgib203c2f2009-08-20 14:48:03 +0000115 mend = start + elf32_to_native(phdr[i].p_memsz);
116 rend = start + elf32_to_native(phdr[i].p_filesz);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000117
118 if (start < data_start)
119 data_start = start;
120
121 if (rend > data_end)
122 data_end = rend;
123
124 if (mend > mem_end)
125 mem_end = mend;
126 }
127
Patrick Georgi9341acd2009-12-23 12:52:56 +0000128 if (data_start < *location) {
129 data_start = *location;
130 }
131
Patrick Georgia6c337d2010-02-03 17:56:37 +0000132 if (data_end <= data_start) {
Stefan Reinauer63217582012-10-29 16:52:36 -0700133 fprintf(stderr, "E: data ends before it starts. Make sure the "
134 "ELF file is correct and resides in ROM space.\n");
Patrick Georgia6c337d2010-02-03 17:56:37 +0000135 exit(1);
136 }
137
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000138 /* allocate an intermediate buffer for the data */
139 buffer = calloc(data_end - data_start, 1);
140
141 if (buffer == NULL) {
142 fprintf(stderr, "E: Unable to allocate memory: %m\n");
143 return -1;
144 }
145
146 /* Copy the file data into the buffer */
147
148 for (i = 0; i < headers; i++) {
Patrick Georgi9341acd2009-12-23 12:52:56 +0000149 unsigned int l_start, l_offset = 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000150
Patrick Georgib203c2f2009-08-20 14:48:03 +0000151 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000152 continue;
153
Patrick Georgib203c2f2009-08-20 14:48:03 +0000154 if (elf32_to_native(phdr[i].p_memsz) == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000155 continue;
156
Patrick Georgi9341acd2009-12-23 12:52:56 +0000157 l_start = elf32_to_native(phdr[i].p_paddr);
158 if (l_start < *location) {
159 l_offset = *location - l_start;
160 l_start = *location;
161 }
162
163 memcpy(buffer + (l_start - data_start),
164 &header[elf32_to_native(phdr[i].p_offset)+l_offset],
165 elf32_to_native(phdr[i].p_filesz)-l_offset);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000166 }
167
168 /* Now make the output buffer */
Peter Stuge1d862de2009-04-14 00:08:34 +0000169 out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000170
171 if (out == NULL) {
172 fprintf(stderr, "E: Unable to allocate memory: %m\n");
173 return -1;
174 }
175
Peter Stuge1d862de2009-04-14 00:08:34 +0000176 stage = (struct cbfs_stage *)out;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000177
Stefan Reinauera1e48242011-10-21 14:24:57 -0700178 stage->load = data_start; /* FIXME: htonll */
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000179 stage->memlen = mem_end - data_start;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000180 stage->compression = algo;
Stefan Reinauera1e48242011-10-21 14:24:57 -0700181 stage->entry = ehdr->e_entry; /* FIXME: htonll */
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000182
183 compress(buffer, data_end - data_start,
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000184 (char *)(out + sizeof(struct cbfs_stage)), (int *)&stage->len);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000185
Stefan Reinauer63217582012-10-29 16:52:36 -0700186 free(buffer);
187
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000188 *output = out;
189
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000190 if (*location)
191 *location -= sizeof(struct cbfs_stage);
Peter Stuge1d862de2009-04-14 00:08:34 +0000192 return sizeof(struct cbfs_stage) + stage->len;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000193}