blob: b1dd1c027c61875d6d0a3ef27d63df0b8abc3f60 [file] [log] [blame]
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00001/*
Peter Stuge45ae92ff2009-04-14 19:48:32 +00002 * cbfs-mkpayload
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>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000025
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080026#include "elf.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000027#include "common.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000028#include "cbfs.h"
Stefan Reinauer543a6822013-02-04 15:39:13 -080029#include "fv.h"
30#include "coff.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000031
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080032/* serialize the seg array into the buffer.
33 * The buffer is assumed to be large enough.
34 */
35static void xdr_segs(struct buffer *output,
36 struct cbfs_payload_segment *segs, int nseg)
37{
38 struct buffer outheader;
39 int i;
40
41 outheader.data = output->data;
42 outheader.size = 0;
43
44 for(i = 0; i < nseg; i++){
45 xdr_be.put32(&outheader, segs[i].type);
46 xdr_be.put32(&outheader, segs[i].compression);
47 xdr_be.put32(&outheader, segs[i].offset);
48 xdr_be.put64(&outheader, segs[i].load_addr);
49 xdr_be.put32(&outheader, segs[i].len);
50 xdr_be.put32(&outheader, segs[i].mem_len);
51 }
52}
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080053int parse_elf_to_payload(const struct buffer *input,
54 struct buffer *output, comp_algo algo)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000055{
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080056 Elf64_Phdr *phdr;
57 Elf64_Ehdr ehdr;
58 Elf64_Shdr *shdr;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000059 char *header;
60 char *strtab;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000061 int headers;
62 int segments = 1;
63 int isize = 0, osize = 0;
64 int doffset = 0;
Peter Stuge1d862de2009-04-14 00:08:34 +000065 struct cbfs_payload_segment *segs;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000066 int i;
67
Patrick Georgib7b56dd82009-09-14 13:29:27 +000068 comp_func_ptr compress = compression_function(algo);
69 if (!compress)
70 return -1;
71
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080072 if (elf_headers(input, &ehdr, &phdr, &shdr) < 0)
73 return -1;
74
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080075 DEBUG("start: parse_elf_to_payload\n");
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080076 headers = ehdr.e_phnum;
77 header = input->data;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000078
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080079 strtab = &header[shdr[ehdr.e_shstrndx].sh_offset];
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000080
81 /* Count the number of headers - look for the .notes.pinfo
82 * section */
83
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080084 for (i = 0; i < ehdr.e_shnum; i++) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000085 char *name;
86
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080087 if (i == ehdr.e_shstrndx)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000088 continue;
89
90 if (shdr[i].sh_size == 0)
91 continue;
92
93 name = (char *)(strtab + shdr[i].sh_name);
94
Ronald G. Minnich5a1af7b2009-09-17 15:35:08 +000095 if (!strcmp(name, ".note.pinfo")) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000096 segments++;
Ronald G. Minnich5a1af7b2009-09-17 15:35:08 +000097 isize += (unsigned int)shdr[i].sh_size;
98 }
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000099 }
100
101 /* Now, regular headers - we only care about PT_LOAD headers,
102 * because thats what we're actually going to load
103 */
104
105 for (i = 0; i < headers; i++) {
106 if (phdr[i].p_type != PT_LOAD)
107 continue;
108
109 /* Empty segments are never interesting */
110 if (phdr[i].p_memsz == 0)
111 continue;
112
113 isize += phdr[i].p_filesz;
114
115 segments++;
116 }
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800117 /* allocate the segment header array */
118 segs = calloc(segments, sizeof(*segs));
119 if (segs == NULL)
120 return -1;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000121 /* Allocate a block of memory to store the data in */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800122 if (buffer_create(output, (segments * sizeof(*segs)) + isize,
123 input->name) != 0)
124 return -1;
125 memset(output->data, 0, output->size);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000126
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800127 doffset = (segments * sizeof(*segs));
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000128
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800129 /* set up for output marshaling. This is a bit
130 * tricky as we are marshaling the headers at the front,
131 * and the data starting after the headers. We need to convert
132 * the headers to the right format but the data
133 * passes through unchanged. Unlike most XDR code,
134 * we are doing these two concurrently. The doffset is
135 * used to compute the address for the raw data, and the
136 * outheader is used to marshal the headers. To make it simpler
137 * for The Reader, we set up the headers in a separate array,
138 * then marshal them all at once to the output.
139 */
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000140 segments = 0;
141
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800142 for (i = 0; i < ehdr.e_shnum; i++) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000143 char *name;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800144 if (i == ehdr.e_shstrndx)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000145 continue;
146
147 if (shdr[i].sh_size == 0)
148 continue;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000149 name = (char *)(strtab + shdr[i].sh_name);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000150 if (!strcmp(name, ".note.pinfo")) {
151 segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
152 segs[segments].load_addr = 0;
153 segs[segments].len = (unsigned int)shdr[i].sh_size;
154 segs[segments].offset = doffset;
155
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800156 memcpy((unsigned long *)(output->data + doffset),
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000157 &header[shdr[i].sh_offset], shdr[i].sh_size);
158
159 doffset += segs[segments].len;
160 osize += segs[segments].len;
161
162 segments++;
163 }
164 }
165
166 for (i = 0; i < headers; i++) {
167 if (phdr[i].p_type != PT_LOAD)
168 continue;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000169 if (phdr[i].p_memsz == 0)
170 continue;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000171 if (phdr[i].p_filesz == 0) {
172 segs[segments].type = PAYLOAD_SEGMENT_BSS;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800173 segs[segments].load_addr = phdr[i].p_paddr;
174 segs[segments].mem_len = phdr[i].p_memsz;
175 segs[segments].offset = doffset;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000176
177 segments++;
178 continue;
179 }
180
Stefan Reinauer51f6a202012-01-11 12:40:14 -0800181 if (phdr[i].p_flags & PF_X)
182 segs[segments].type = PAYLOAD_SEGMENT_CODE;
183 else
184 segs[segments].type = PAYLOAD_SEGMENT_DATA;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800185 segs[segments].load_addr = phdr[i].p_paddr;
186 segs[segments].mem_len = phdr[i].p_memsz;
187 segs[segments].compression = algo;
188 segs[segments].offset = doffset;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000189
190 int len;
191 compress((char *)&header[phdr[i].p_offset],
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800192 phdr[i].p_filesz, output->data + doffset, &len);
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800193 segs[segments].len = len;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000194
195 /* If the compressed section is larger, then use the
196 original stuff */
197
Stefan Reinauer14ad50e2009-04-04 22:18:26 +0000198 if ((unsigned int)len > phdr[i].p_filesz) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000199 segs[segments].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800200 segs[segments].len = phdr[i].p_filesz;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800201 memcpy(output->data + doffset,
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000202 &header[phdr[i].p_offset], phdr[i].p_filesz);
203 }
204
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800205 doffset += segs[segments].len;
206 osize += segs[segments].len;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000207
208 segments++;
209 }
210
211 segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800212 segs[segments++].load_addr = ehdr.e_entry;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000213
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800214 output->size = (segments * sizeof(*segs)) + osize;
215 xdr_segs(output, segs, segments);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800216 return 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000217}
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800218
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800219int parse_flat_binary_to_payload(const struct buffer *input,
220 struct buffer *output,
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800221 uint32_t loadaddress,
222 uint32_t entrypoint,
223 comp_algo algo)
224{
225 comp_func_ptr compress;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800226 struct cbfs_payload_segment segs[2];
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800227 int doffset, len = 0;
228
229 compress = compression_function(algo);
230 if (!compress)
231 return -1;
232
233 DEBUG("start: parse_flat_binary_to_payload\n");
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800234 if (buffer_create(output, (sizeof(segs) + input->size),
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800235 input->name) != 0)
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800236 return -1;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800237 memset(output->data, 0, output->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800238
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800239 doffset = (2 * sizeof(*segs));
240
241 /* Prepare code segment */
242 segs[0].type = PAYLOAD_SEGMENT_CODE;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800243 segs[0].load_addr = loadaddress;
244 segs[0].mem_len = input->size;
245 segs[0].offset = doffset;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800246
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800247 compress(input->data, input->size, output->data + doffset, &len);
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800248 segs[0].compression = algo;
249 segs[0].len = len;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800250
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800251 if ((unsigned int)len >= input->size) {
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800252 WARN("Compressing data would make it bigger - disabled.\n");
253 segs[0].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800254 segs[0].len = input->size;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800255 memcpy(output->data + doffset, input->data, input->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800256 }
257
258 /* prepare entry point segment */
259 segs[1].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800260 segs[1].load_addr = entrypoint;
261 output->size = doffset + segs[0].len;
262 xdr_segs(output, segs, 2);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800263 return 0;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800264}
Stefan Reinauer543a6822013-02-04 15:39:13 -0800265
266int parse_fv_to_payload(const struct buffer *input,
267 struct buffer *output, comp_algo algo)
268{
269 comp_func_ptr compress;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800270 struct cbfs_payload_segment segs[2];
Stefan Reinauer543a6822013-02-04 15:39:13 -0800271 int doffset, len = 0;
272 firmware_volume_header_t *fv;
273 ffs_file_header_t *fh;
274 common_section_header_t *cs;
275 dos_header_t *dh;
276 coff_header_t *ch;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800277 int dh_offset;
278
Stefan Reinauere8764182013-02-05 13:46:49 -0800279 uint32_t loadaddress = 0;
280 uint32_t entrypoint = 0;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800281
282 compress = compression_function(algo);
283 if (!compress)
284 return -1;
285
286 DEBUG("start: parse_fv_to_payload\n");
287
288 fv = (firmware_volume_header_t *)input->data;
289 if (fv->signature != FV_SIGNATURE) {
290 INFO("Not a UEFI firmware volume.\n");
291 return -1;
292 }
293
294 fh = (ffs_file_header_t *)(input->data + fv->header_length);
Patrick Georgi46102472013-02-09 13:26:19 +0100295 while (fh->file_type == FILETYPE_PAD) {
296 unsigned long offset = (fh->size[2] << 16) | (fh->size[1] << 8) | fh->size[0];
Hung-Te Lin7b654a92013-02-18 18:35:00 +0800297 ERROR("skipping %lu bytes of FV padding\n", offset);
Patrick Georgi46102472013-02-09 13:26:19 +0100298 fh = (ffs_file_header_t *)(((void*)fh) + offset);
299 }
Stefan Reinauer543a6822013-02-04 15:39:13 -0800300 if (fh->file_type != FILETYPE_SEC) {
301 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800302 INFO("First file in first FV not a SEC core.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800303 return -1;
304 }
305
306 cs = (common_section_header_t *)&fh[1];
Patrick Georgi46102472013-02-09 13:26:19 +0100307 while (cs->section_type == SECTION_RAW) {
308 unsigned long offset = (cs->size[2] << 16) | (cs->size[1] << 8) | cs->size[0];
Hung-Te Lin7b654a92013-02-18 18:35:00 +0800309 ERROR("skipping %lu bytes of section padding\n", offset);
Patrick Georgi46102472013-02-09 13:26:19 +0100310 cs = (common_section_header_t *)(((void*)cs) + offset);
311 }
Stefan Reinauer543a6822013-02-04 15:39:13 -0800312 if (cs->section_type != SECTION_PE32) {
313 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800314 INFO("Section type not PE32.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800315 return -1;
316 }
317
318 dh = (dos_header_t *)&cs[1];
Stefan Reinauere8764182013-02-05 13:46:49 -0800319 if (dh->signature != DOS_MAGIC) {
Stefan Reinauer543a6822013-02-04 15:39:13 -0800320 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800321 INFO("DOS header signature wrong.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800322 return -1;
323 }
324
325 dh_offset = (unsigned long)dh - (unsigned long)input->data;
326 DEBUG("dos header offset = %x\n", dh_offset);
327
328 ch = (coff_header_t *)(((void *)dh)+dh->e_lfanew);
Stefan Reinauere8764182013-02-05 13:46:49 -0800329
330 if (ch->machine == MACHINE_TYPE_X86) {
331 pe_opt_header_32_t *ph;
332 ph = (pe_opt_header_32_t *)&ch[1];
333 if (ph->signature != PE_HDR_32_MAGIC) {
334 WARN("PE header signature incorrect.\n");
335 return -1;
336 }
337 DEBUG("image base %x\n", ph->image_addr);
338 DEBUG("entry point %x\n", ph->entry_point);
339
340 loadaddress = ph->image_addr - dh_offset;
341 entrypoint = ph->image_addr + ph->entry_point;
342 } else if (ch->machine == MACHINE_TYPE_X64) {
343 pe_opt_header_64_t *ph;
344 ph = (pe_opt_header_64_t *)&ch[1];
345 if (ph->signature != PE_HDR_64_MAGIC) {
346 WARN("PE header signature incorrect.\n");
347 return -1;
348 }
349 DEBUG("image base %lx\n", (unsigned long)ph->image_addr);
350 DEBUG("entry point %x\n", ph->entry_point);
351
352 loadaddress = ph->image_addr - dh_offset;
353 entrypoint = ph->image_addr + ph->entry_point;
354 } else {
355 ERROR("Machine type not x86 or x64.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800356 return -1;
357 }
358
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800359 if (buffer_create(output, (sizeof(segs) + input->size),
Stefan Reinauer543a6822013-02-04 15:39:13 -0800360 input->name) != 0)
361 return -1;
362
363 memset(output->data, 0, output->size);
364
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800365 doffset = (sizeof(segs));
Stefan Reinauer543a6822013-02-04 15:39:13 -0800366
367 /* Prepare code segment */
368 segs[0].type = PAYLOAD_SEGMENT_CODE;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800369 segs[0].load_addr = loadaddress;
370 segs[0].mem_len = input->size;
371 segs[0].offset = doffset;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800372
373 compress(input->data, input->size, output->data + doffset, &len);
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800374 segs[0].compression = algo;
375 segs[0].len = len;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800376
377 if ((unsigned int)len >= input->size) {
378 WARN("Compressing data would make it bigger - disabled.\n");
379 segs[0].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800380 segs[0].len = input->size;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800381 memcpy(output->data + doffset, input->data, input->size);
382 }
383
384 /* prepare entry point segment */
385 segs[1].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800386 segs[1].load_addr = entrypoint;
387 output->size = doffset + segs[0].len;
388 xdr_segs(output, segs, 2);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800389 return 0;
390
391}
Patrick Georgide36d332013-08-27 20:22:21 +0200392