blob: b85a8f99dc07d337f1b925132ad9401bdd00504d [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>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24#include "elf.h"
25#include <fcntl.h>
26#include <getopt.h>
27#include <sys/stat.h>
28
29#include "common.h"
Peter Stuge1d862de2009-04-14 00:08:34 +000030#include "../cbfs.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000031
Patrick Georgib203c2f2009-08-20 14:48:03 +000032unsigned int idemp(unsigned int x)
33{
34 return x;
35}
36
37unsigned int swap32(unsigned int x)
38{
39 return ((x>>24) | ((x>>8) & 0xff00) | ((x<<8) & 0xff0000) | (x<<24));
40}
41
42unsigned int (*elf32_to_native)(unsigned int)=idemp;
43
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000044int parse_elf(unsigned char *input, unsigned char **output,
45 int mode, void (*compress) (char *, int, char *, int *))
46{
47 Elf32_Phdr *phdr;
48 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000049 char *header, *buffer;
50 unsigned char *out;
51
52 int headers;
53 int i;
Peter Stuge1d862de2009-04-14 00:08:34 +000054 struct cbfs_stage *stage;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000055 unsigned int data_start, data_end, mem_end;
56
Patrick Georgib203c2f2009-08-20 14:48:03 +000057 int elf_bigendian = 0;
58 int host_bigendian = 0;
59 if (ehdr->e_ident[EI_DATA]==ELFDATA2MSB) {
60 elf_bigendian = 1;
61 }
62 if ((unsigned int)"1234"==0x31323334) {
63 host_bigendian = 1;
64 }
65 if (elf_bigendian != host_bigendian) {
66 elf32_to_native = swap32;
67 }
68
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000069 headers = ehdr->e_phnum;
70 header = (char *)ehdr;
71
Patrick Georgib203c2f2009-08-20 14:48:03 +000072 phdr = (Elf32_Phdr *) & header[elf32_to_native(ehdr->e_phoff)];
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000073
74 /* Now, regular headers - we only care about PT_LOAD headers,
75 * because thats what we're actually going to load
76 */
77
78 data_start = 0xFFFFFFFF;
79 data_end = 0;
80 mem_end = 0;
81
82 for (i = 0; i < headers; i++) {
83 unsigned int start, mend, rend;
84
Patrick Georgib203c2f2009-08-20 14:48:03 +000085 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000086 continue;
87
88 /* Empty segments are never interesting */
Patrick Georgib203c2f2009-08-20 14:48:03 +000089 if (elf32_to_native(phdr[i].p_memsz) == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000090 continue;
91
92 /* BSS */
93
Patrick Georgib203c2f2009-08-20 14:48:03 +000094 start = elf32_to_native(phdr[i].p_paddr);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000095
Patrick Georgib203c2f2009-08-20 14:48:03 +000096 mend = start + elf32_to_native(phdr[i].p_memsz);
97 rend = start + elf32_to_native(phdr[i].p_filesz);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000098
99 if (start < data_start)
100 data_start = start;
101
102 if (rend > data_end)
103 data_end = rend;
104
105 if (mend > mem_end)
106 mem_end = mend;
107 }
108
109 /* allocate an intermediate buffer for the data */
110 buffer = calloc(data_end - data_start, 1);
111
112 if (buffer == NULL) {
113 fprintf(stderr, "E: Unable to allocate memory: %m\n");
114 return -1;
115 }
116
117 /* Copy the file data into the buffer */
118
119 for (i = 0; i < headers; i++) {
120
Patrick Georgib203c2f2009-08-20 14:48:03 +0000121 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000122 continue;
123
Patrick Georgib203c2f2009-08-20 14:48:03 +0000124 if (elf32_to_native(phdr[i].p_memsz) == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000125 continue;
126
Patrick Georgib203c2f2009-08-20 14:48:03 +0000127 memcpy(buffer + (elf32_to_native(phdr[i].p_paddr) - data_start),
128 &header[elf32_to_native(phdr[i].p_offset)], elf32_to_native(phdr[i].p_filesz));
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000129 }
130
131 /* Now make the output buffer */
Peter Stuge1d862de2009-04-14 00:08:34 +0000132 out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000133
134 if (out == NULL) {
135 fprintf(stderr, "E: Unable to allocate memory: %m\n");
136 return -1;
137 }
138
Peter Stuge1d862de2009-04-14 00:08:34 +0000139 stage = (struct cbfs_stage *)out;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000140
141 stage->load = data_start;
142 stage->memlen = mem_end - data_start;
143 stage->compression = mode;
144 stage->entry = ehdr->e_entry;
145
146 compress(buffer, data_end - data_start,
Peter Stuge1d862de2009-04-14 00:08:34 +0000147 (char *)(out + sizeof(struct cbfs_stage)),
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000148 (int *)&stage->len);
149
150 *output = out;
151
Peter Stuge1d862de2009-04-14 00:08:34 +0000152 return sizeof(struct cbfs_stage) + stage->len;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000153}
154
155int main(int argc, char **argv)
156{
157 void (*compress) (char *, int, char *, int *);
Peter Stuge1d862de2009-04-14 00:08:34 +0000158 int algo = CBFS_COMPRESS_LZMA;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000159
160 char *output = NULL;
161 char *input = NULL;
162
163 unsigned char *buffer, *obuffer;
164 int size, osize;
165
166 while (1) {
167 int option_index;
168 static struct option longopt[] = {
169 {"output", 1, 0, 'o'},
170 {"lzma", 0, 0, 'l'},
171 {"nocompress", 0, 0, 'n'},
172 };
173
174 signed char ch = getopt_long(argc, argv, "o:ln",
175 longopt, &option_index);
176
177 if (ch == -1)
178 break;
179
180 switch (ch) {
181 case 'o':
182 output = optarg;
183 break;
184 case 'l':
Peter Stuge1d862de2009-04-14 00:08:34 +0000185 algo = CBFS_COMPRESS_LZMA;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000186 break;
187 case 'n':
Peter Stuge1d862de2009-04-14 00:08:34 +0000188 algo = CBFS_COMPRESS_NONE;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000189 break;
190 default:
191 //usage();
192 return -1;
193 }
194 }
195
196 if (optind < argc)
197 input = argv[optind];
198
199 if (input == NULL || !strcmp(input, "-"))
200 buffer = file_read_to_buffer(STDIN_FILENO, &size);
201 else
202 buffer = file_read(input, &size);
203
204 if (!iself(buffer)) {
205 fprintf(stderr, "E: The incoming file is not an ELF\n");
206 return -1;
207 }
208
209 switch (algo) {
Peter Stuge1d862de2009-04-14 00:08:34 +0000210 case CBFS_COMPRESS_NONE:
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000211 compress = none_compress;
212 break;
Peter Stuge1d862de2009-04-14 00:08:34 +0000213 case CBFS_COMPRESS_LZMA:
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000214 compress = lzma_compress;
215 break;
216 }
217
218 osize = parse_elf(buffer, &obuffer, algo, compress);
219
220 if (osize == -1) {
221 fprintf(stderr, "E: Error while converting the ELF\n");
222 return -1;
223 }
224
225 if (output == NULL || !strcmp(output, "-"))
226 file_write_from_buffer(STDOUT_FILENO, obuffer, osize);
227 else
228 file_write(output, obuffer, osize);
229
230 return 0;
231}