4466cc125454ff500a995f5210f4bcb74b6cc5e6
[coreboot.git] / util / cbfstool / tools / rom-mkpayload.c
1 /*
2  * rom-mkpayload
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 #include <arpa/inet.h>
29
30 #include "common.h"
31 #include "../cbfs.h"
32
33 int parse_elf(unsigned char *input, unsigned char **output, int algo,
34               void (*compress) (char *, int, char *, int *))
35 {
36         Elf32_Phdr *phdr;
37         Elf32_Ehdr *ehdr;
38         Elf32_Shdr *shdr;
39         char *header;
40         char *strtab;
41         unsigned char *sptr;
42         int headers;
43         int segments = 1;
44         int isize = 0, osize = 0;
45         int doffset = 0;
46         struct cbfs_payload_segment *segs;
47         int i;
48
49         ehdr = (Elf32_Ehdr *) input;
50         headers = ehdr->e_phnum;
51         header = (char *)ehdr;
52
53         phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]);
54         shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]);
55
56         strtab = &header[shdr[ehdr->e_shstrndx].sh_offset];
57
58         /* Count the number of headers - look for the .notes.pinfo
59          * section */
60
61         for (i = 0; i < ehdr->e_shnum; i++) {
62                 char *name;
63
64                 if (i == ehdr->e_shstrndx)
65                         continue;
66
67                 if (shdr[i].sh_size == 0)
68                         continue;
69
70                 name = (char *)(strtab + shdr[i].sh_name);
71
72                 if (!strcmp(name, ".note.pinfo"))
73                         segments++;
74         }
75
76         /* Now, regular headers - we only care about PT_LOAD headers,
77          * because thats what we're actually going to load
78          */
79
80         for (i = 0; i < headers; i++) {
81                 if (phdr[i].p_type != PT_LOAD)
82                         continue;
83
84                 /* Empty segments are never interesting */
85                 if (phdr[i].p_memsz == 0)
86                         continue;
87
88                 isize += phdr[i].p_filesz;
89
90                 segments++;
91         }
92
93         /* Allocate a block of memory to store the data in */
94
95         sptr =
96             calloc((segments * sizeof(struct cbfs_payload_segment)) + isize,
97                    1);
98         doffset = (segments * sizeof(struct cbfs_payload_segment));
99
100         if (sptr == NULL)
101                 goto err;
102
103         segs = (struct cbfs_payload_segment *)sptr;
104         segments = 0;
105
106         for (i = 0; i < ehdr->e_shnum; i++) {
107                 char *name;
108
109                 if (i == ehdr->e_shstrndx)
110                         continue;
111
112                 if (shdr[i].sh_size == 0)
113                         continue;
114
115                 name = (char *)(strtab + shdr[i].sh_name);
116
117                 if (!strcmp(name, ".note.pinfo")) {
118                         segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
119                         segs[segments].load_addr = 0;
120                         segs[segments].len = (unsigned int)shdr[i].sh_size;
121                         segs[segments].offset = doffset;
122
123                         memcpy((unsigned long *)(sptr + doffset),
124                                &header[shdr[i].sh_offset], shdr[i].sh_size);
125
126                         doffset += segs[segments].len;
127                         osize += segs[segments].len;
128
129                         segments++;
130                 }
131         }
132
133         for (i = 0; i < headers; i++) {
134                 if (phdr[i].p_type != PT_LOAD)
135                         continue;
136
137                 if (phdr[i].p_memsz == 0)
138                         continue;
139
140                 if (phdr[i].p_filesz == 0) {
141                         segs[segments].type = PAYLOAD_SEGMENT_BSS;
142                         segs[segments].load_addr =
143                             (unsigned long long)htonl(phdr[i].p_paddr);
144                         segs[segments].mem_len = (unsigned int)htonl(phdr[i].p_memsz);
145                         segs[segments].offset = htonl(doffset);
146
147                         segments++;
148                         continue;
149                 }
150
151                 segs[segments].type = PAYLOAD_SEGMENT_DATA;
152                 segs[segments].load_addr = (unsigned int)htonl(phdr[i].p_paddr);
153                 segs[segments].mem_len = (unsigned int)htonl(phdr[i].p_memsz);
154                 segs[segments].compression = htonl(algo);
155                 segs[segments].offset = htonl(doffset);
156
157                 int len;
158                 compress((char *)&header[phdr[i].p_offset],
159                          phdr[i].p_filesz,
160                          (char *)(sptr + doffset), &len);
161                 segs[segments].len = htonl(len);
162
163                 /* If the compressed section is larger, then use the
164                    original stuff */
165
166                 if ((unsigned int)len > phdr[i].p_filesz) {
167                         segs[segments].compression = 0;
168                         segs[segments].len = htonl(phdr[i].p_filesz);
169
170                         memcpy((char *)(sptr + doffset),
171                                &header[phdr[i].p_offset], phdr[i].p_filesz);
172                 }
173
174                 doffset += ntohl(segs[segments].len);
175                 osize += ntohl(segs[segments].len);
176
177                 segments++;
178         }
179
180         segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
181         segs[segments++].load_addr = (unsigned long long)htonl(ehdr->e_entry);
182
183         *output = sptr;
184
185         return (segments * sizeof(struct cbfs_payload_segment)) + osize;
186
187 err:
188         return -1;
189 }
190
191 int main(int argc, char **argv)
192 {
193         void (*compress) (char *, int, char *, int *);
194         int algo;
195
196         char *output = NULL;
197         char *input = NULL;
198
199         unsigned char *buffer, *obuffer;
200         int size, osize;
201
202         while (1) {
203                 int option_index;
204                 static struct option longopt[] = {
205                         {"output", 1, 0, 'o'},
206                         {"lzma", 0, 0, 'l'},
207                         {"nocompress", 0, 0, 'n'},
208                 };
209
210                 signed char ch = getopt_long(argc, argv, "o:ln",
211                                              longopt, &option_index);
212
213                 if (ch == -1)
214                         break;
215
216                 switch (ch) {
217                 case 'o':
218                         output = optarg;
219                         break;
220                 case 'l':
221                         algo = CBFS_COMPRESS_LZMA;
222                         break;
223                 case 'n':
224                         algo = CBFS_COMPRESS_NONE;
225                         break;
226                 default:
227                         //usage();
228                         return -1;
229                 }
230         }
231
232         if (optind < argc)
233                 input = argv[optind];
234
235         if (input == NULL || !strcmp(input, "-"))
236                 buffer = file_read_to_buffer(STDIN_FILENO, &size);
237         else {
238                 printf("Reading from %s\n", input);
239                 buffer = file_read(input, &size);
240         }
241
242         if (!iself(buffer)) {
243                 fprintf(stderr, "E:  This does not appear to be an ELF file\n");
244                 return -1;
245         }
246
247         switch (algo) {
248         case CBFS_COMPRESS_NONE:
249                 compress = none_compress;
250                 break;
251         case CBFS_COMPRESS_LZMA:
252                 compress = lzma_compress;
253                 break;
254         }
255
256         osize = parse_elf(buffer, &obuffer, algo, compress);
257
258         if (osize == -1) {
259                 fprintf(stderr, "E:  Error while converting the payload\n");
260                 return -1;
261         }
262
263         if (output == NULL || !strcmp(output, "-"))
264                 file_write_from_buffer(STDOUT_FILENO, obuffer, osize);
265         else
266                 file_write(output, obuffer, osize);
267
268         return 0;
269 }