blob: 693d7cc0990e9725bb67965e02dc3d4537d0bb96 [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>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include "elf.h"
27#include <fcntl.h>
28#include <getopt.h>
29#include <sys/stat.h>
30
31#include "common.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000032#include "cbfs.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000033
Patrick Georgib203c2f2009-08-20 14:48:03 +000034unsigned int idemp(unsigned int x)
35{
36 return x;
37}
38
39unsigned int swap32(unsigned int x)
40{
Patrick Georgib7b56dd82009-09-14 13:29:27 +000041 return ((x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) |
42 (x << 24));
Patrick Georgib203c2f2009-08-20 14:48:03 +000043}
44
Patrick Georgib7b56dd82009-09-14 13:29:27 +000045unsigned int (*elf32_to_native) (unsigned int) = idemp;
Patrick Georgib203c2f2009-08-20 14:48:03 +000046
Patrick Georgib7b56dd82009-09-14 13:29:27 +000047/* returns size of result, or -1 if error */
48int parse_elf_to_stage(unsigned char *input, unsigned char **output,
49 comp_algo algo, uint32_t * location)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000050{
51 Elf32_Phdr *phdr;
52 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000053 char *header, *buffer;
54 unsigned char *out;
55
56 int headers;
57 int i;
Peter Stuge1d862de2009-04-14 00:08:34 +000058 struct cbfs_stage *stage;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000059 unsigned int data_start, data_end, mem_end;
60
Patrick Georgib203c2f2009-08-20 14:48:03 +000061 int elf_bigendian = 0;
62 int host_bigendian = 0;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000063
64 comp_func_ptr compress = compression_function(algo);
65 if (!compress)
66 return -1;
67
68 if (!iself(input)) {
69 fprintf(stderr, "E: The incoming file is not an ELF\n");
70 return -1;
71 }
72
73 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
Patrick Georgib203c2f2009-08-20 14:48:03 +000074 elf_bigendian = 1;
75 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +000076 char test[4] = "1234";
77 uint32_t inttest = *(uint32_t *) test;
78 if (inttest == 0x31323334) {
Patrick Georgib203c2f2009-08-20 14:48:03 +000079 host_bigendian = 1;
80 }
81 if (elf_bigendian != host_bigendian) {
82 elf32_to_native = swap32;
83 }
84
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000085 headers = ehdr->e_phnum;
86 header = (char *)ehdr;
87
Patrick Georgib203c2f2009-08-20 14:48:03 +000088 phdr = (Elf32_Phdr *) & header[elf32_to_native(ehdr->e_phoff)];
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000089
90 /* Now, regular headers - we only care about PT_LOAD headers,
91 * because thats what we're actually going to load
92 */
93
94 data_start = 0xFFFFFFFF;
95 data_end = 0;
96 mem_end = 0;
97
98 for (i = 0; i < headers; i++) {
99 unsigned int start, mend, rend;
100
Patrick Georgib203c2f2009-08-20 14:48:03 +0000101 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000102 continue;
103
104 /* Empty segments are never interesting */
Patrick Georgib203c2f2009-08-20 14:48:03 +0000105 if (elf32_to_native(phdr[i].p_memsz) == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000106 continue;
107
108 /* BSS */
109
Patrick Georgib203c2f2009-08-20 14:48:03 +0000110 start = elf32_to_native(phdr[i].p_paddr);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000111
Patrick Georgib203c2f2009-08-20 14:48:03 +0000112 mend = start + elf32_to_native(phdr[i].p_memsz);
113 rend = start + elf32_to_native(phdr[i].p_filesz);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000114
115 if (start < data_start)
116 data_start = start;
117
118 if (rend > data_end)
119 data_end = rend;
120
121 if (mend > mem_end)
122 mem_end = mend;
123 }
124
125 /* allocate an intermediate buffer for the data */
126 buffer = calloc(data_end - data_start, 1);
127
128 if (buffer == NULL) {
129 fprintf(stderr, "E: Unable to allocate memory: %m\n");
130 return -1;
131 }
132
133 /* Copy the file data into the buffer */
134
135 for (i = 0; i < headers; i++) {
136
Patrick Georgib203c2f2009-08-20 14:48:03 +0000137 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000138 continue;
139
Patrick Georgib203c2f2009-08-20 14:48:03 +0000140 if (elf32_to_native(phdr[i].p_memsz) == 0)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000141 continue;
142
Patrick Georgib203c2f2009-08-20 14:48:03 +0000143 memcpy(buffer + (elf32_to_native(phdr[i].p_paddr) - data_start),
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000144 &header[elf32_to_native(phdr[i].p_offset)],
145 elf32_to_native(phdr[i].p_filesz));
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000146 }
147
148 /* Now make the output buffer */
Peter Stuge1d862de2009-04-14 00:08:34 +0000149 out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000150
151 if (out == NULL) {
152 fprintf(stderr, "E: Unable to allocate memory: %m\n");
153 return -1;
154 }
155
Peter Stuge1d862de2009-04-14 00:08:34 +0000156 stage = (struct cbfs_stage *)out;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000157
158 stage->load = data_start;
159 stage->memlen = mem_end - data_start;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000160 stage->compression = algo;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000161 stage->entry = ehdr->e_entry;
162
163 compress(buffer, data_end - data_start,
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000164 (char *)(out + sizeof(struct cbfs_stage)), (int *)&stage->len);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000165
166 *output = out;
167
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000168 if (*location)
169 *location -= sizeof(struct cbfs_stage);
Peter Stuge1d862de2009-04-14 00:08:34 +0000170 return sizeof(struct cbfs_stage) + stage->len;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000171}