20090819-2-trim-down-cbfs:
[coreboot.git] / util / cbfstool / tools / cbfs-mkstage.c
1 /*
2  * cbfs-mkstage
3  *
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"
30 #include "../cbfs.h"
31
32 unsigned int idemp(unsigned int x)
33 {
34         return x;
35 }
36
37 unsigned int swap32(unsigned int x)
38 {
39         return ((x>>24) | ((x>>8) & 0xff00) | ((x<<8) & 0xff0000) | (x<<24));
40 }
41
42 unsigned int (*elf32_to_native)(unsigned int)=idemp;
43
44 int 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;
49         char *header, *buffer;
50         unsigned char *out;
51
52         int headers;
53         int i;
54         struct cbfs_stage *stage;
55         unsigned int data_start, data_end, mem_end;
56
57         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
69         headers = ehdr->e_phnum;
70         header = (char *)ehdr;
71
72         phdr = (Elf32_Phdr *) & header[elf32_to_native(ehdr->e_phoff)];
73
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
85                 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
86                         continue;
87
88                 /* Empty segments are never interesting */
89                 if (elf32_to_native(phdr[i].p_memsz) == 0)
90                         continue;
91
92                 /* BSS */
93
94                 start = elf32_to_native(phdr[i].p_paddr);
95
96                 mend = start + elf32_to_native(phdr[i].p_memsz);
97                 rend = start + elf32_to_native(phdr[i].p_filesz);
98
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
121                 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
122                         continue;
123
124                 if (elf32_to_native(phdr[i].p_memsz) == 0)
125                         continue;
126
127                 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));
129         }
130
131         /* Now make the output buffer */
132         out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1);
133
134         if (out == NULL) {
135                 fprintf(stderr, "E: Unable to allocate memory: %m\n");
136                 return -1;
137         }
138
139         stage = (struct cbfs_stage *)out;
140
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,
147                  (char *)(out + sizeof(struct cbfs_stage)),
148                  (int *)&stage->len);
149
150         *output = out;
151
152         return sizeof(struct cbfs_stage) + stage->len;
153 }
154
155 int main(int argc, char **argv)
156 {
157         void (*compress) (char *, int, char *, int *);
158         int algo = CBFS_COMPRESS_LZMA;
159
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':
185                         algo = CBFS_COMPRESS_LZMA;
186                         break;
187                 case 'n':
188                         algo = CBFS_COMPRESS_NONE;
189                         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) {
210         case CBFS_COMPRESS_NONE:
211                 compress = none_compress;
212                 break;
213         case CBFS_COMPRESS_LZMA:
214                 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 }