blob: adbe313b1820e323edce18eaeba8e37cca7275ba [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
Aaron Durbin54ef3062014-03-05 12:12:09 -060026#include "elfparsing.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 */
Ronald G. Minnich818f3692014-02-04 08:29:35 -080035void xdr_segs(struct buffer *output,
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080036 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}
Aaron Durbinca630272014-08-05 10:48:20 -050053
54void xdr_get_seg(struct cbfs_payload_segment *out,
55 struct cbfs_payload_segment *in)
56{
57 struct buffer inheader;
58
59 inheader.data = (void *)in;
60 inheader.size = sizeof(*in);
61
62 out->type = xdr_be.get32(&inheader);
63 out->compression = xdr_be.get32(&inheader);
64 out->offset = xdr_be.get32(&inheader);
65 out->load_addr = xdr_be.get64(&inheader);
66 out->len = xdr_be.get32(&inheader);
67 out->mem_len = xdr_be.get32(&inheader);
68}
69
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080070int parse_elf_to_payload(const struct buffer *input,
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060071 struct buffer *output, uint32_t arch, comp_algo algo)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000072{
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080073 Elf64_Phdr *phdr;
74 Elf64_Ehdr ehdr;
75 Elf64_Shdr *shdr;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000076 char *header;
77 char *strtab;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000078 int headers;
79 int segments = 1;
80 int isize = 0, osize = 0;
81 int doffset = 0;
Peter Stuge1d862de2009-04-14 00:08:34 +000082 struct cbfs_payload_segment *segs;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000083 int i;
84
Patrick Georgib7b56dd82009-09-14 13:29:27 +000085 comp_func_ptr compress = compression_function(algo);
86 if (!compress)
87 return -1;
88
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060089 if (elf_headers(input, arch, &ehdr, &phdr, &shdr) < 0)
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080090 return -1;
91
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080092 DEBUG("start: parse_elf_to_payload\n");
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080093 headers = ehdr.e_phnum;
94 header = input->data;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000095
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080096 strtab = &header[shdr[ehdr.e_shstrndx].sh_offset];
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000097
98 /* Count the number of headers - look for the .notes.pinfo
99 * section */
100
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800101 for (i = 0; i < ehdr.e_shnum; i++) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000102 char *name;
103
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800104 if (i == ehdr.e_shstrndx)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000105 continue;
106
107 if (shdr[i].sh_size == 0)
108 continue;
109
110 name = (char *)(strtab + shdr[i].sh_name);
111
Ronald G. Minnich5a1af7b2009-09-17 15:35:08 +0000112 if (!strcmp(name, ".note.pinfo")) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000113 segments++;
Ronald G. Minnich5a1af7b2009-09-17 15:35:08 +0000114 isize += (unsigned int)shdr[i].sh_size;
115 }
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000116 }
117
118 /* Now, regular headers - we only care about PT_LOAD headers,
119 * because thats what we're actually going to load
120 */
121
122 for (i = 0; i < headers; i++) {
123 if (phdr[i].p_type != PT_LOAD)
124 continue;
125
126 /* Empty segments are never interesting */
127 if (phdr[i].p_memsz == 0)
128 continue;
129
130 isize += phdr[i].p_filesz;
131
132 segments++;
133 }
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800134 /* allocate the segment header array */
135 segs = calloc(segments, sizeof(*segs));
136 if (segs == NULL)
137 return -1;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000138 /* Allocate a block of memory to store the data in */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800139 if (buffer_create(output, (segments * sizeof(*segs)) + isize,
140 input->name) != 0)
141 return -1;
142 memset(output->data, 0, output->size);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000143
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800144 doffset = (segments * sizeof(*segs));
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000145
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800146 /* set up for output marshaling. This is a bit
147 * tricky as we are marshaling the headers at the front,
148 * and the data starting after the headers. We need to convert
149 * the headers to the right format but the data
150 * passes through unchanged. Unlike most XDR code,
151 * we are doing these two concurrently. The doffset is
152 * used to compute the address for the raw data, and the
153 * outheader is used to marshal the headers. To make it simpler
154 * for The Reader, we set up the headers in a separate array,
155 * then marshal them all at once to the output.
156 */
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000157 segments = 0;
158
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800159 for (i = 0; i < ehdr.e_shnum; i++) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000160 char *name;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800161 if (i == ehdr.e_shstrndx)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000162 continue;
163
164 if (shdr[i].sh_size == 0)
165 continue;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000166 name = (char *)(strtab + shdr[i].sh_name);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000167 if (!strcmp(name, ".note.pinfo")) {
168 segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
169 segs[segments].load_addr = 0;
170 segs[segments].len = (unsigned int)shdr[i].sh_size;
171 segs[segments].offset = doffset;
172
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800173 memcpy((unsigned long *)(output->data + doffset),
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000174 &header[shdr[i].sh_offset], shdr[i].sh_size);
175
176 doffset += segs[segments].len;
177 osize += segs[segments].len;
178
179 segments++;
180 }
181 }
182
183 for (i = 0; i < headers; i++) {
184 if (phdr[i].p_type != PT_LOAD)
185 continue;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000186 if (phdr[i].p_memsz == 0)
187 continue;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000188 if (phdr[i].p_filesz == 0) {
189 segs[segments].type = PAYLOAD_SEGMENT_BSS;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800190 segs[segments].load_addr = phdr[i].p_paddr;
191 segs[segments].mem_len = phdr[i].p_memsz;
192 segs[segments].offset = doffset;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000193
194 segments++;
195 continue;
196 }
197
Stefan Reinauer51f6a202012-01-11 12:40:14 -0800198 if (phdr[i].p_flags & PF_X)
199 segs[segments].type = PAYLOAD_SEGMENT_CODE;
200 else
201 segs[segments].type = PAYLOAD_SEGMENT_DATA;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800202 segs[segments].load_addr = phdr[i].p_paddr;
203 segs[segments].mem_len = phdr[i].p_memsz;
204 segs[segments].compression = algo;
205 segs[segments].offset = doffset;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000206
207 int len;
208 compress((char *)&header[phdr[i].p_offset],
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800209 phdr[i].p_filesz, output->data + doffset, &len);
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800210 segs[segments].len = len;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000211
212 /* If the compressed section is larger, then use the
213 original stuff */
214
Stefan Reinauer14ad50e2009-04-04 22:18:26 +0000215 if ((unsigned int)len > phdr[i].p_filesz) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000216 segs[segments].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800217 segs[segments].len = phdr[i].p_filesz;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800218 memcpy(output->data + doffset,
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000219 &header[phdr[i].p_offset], phdr[i].p_filesz);
220 }
221
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800222 doffset += segs[segments].len;
223 osize += segs[segments].len;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000224
225 segments++;
226 }
227
228 segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800229 segs[segments++].load_addr = ehdr.e_entry;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000230
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800231 output->size = (segments * sizeof(*segs)) + osize;
232 xdr_segs(output, segs, segments);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800233 return 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000234}
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800235
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800236int parse_flat_binary_to_payload(const struct buffer *input,
237 struct buffer *output,
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800238 uint32_t loadaddress,
239 uint32_t entrypoint,
240 comp_algo algo)
241{
242 comp_func_ptr compress;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800243 struct cbfs_payload_segment segs[2];
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800244 int doffset, len = 0;
245
246 compress = compression_function(algo);
247 if (!compress)
248 return -1;
249
250 DEBUG("start: parse_flat_binary_to_payload\n");
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800251 if (buffer_create(output, (sizeof(segs) + input->size),
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800252 input->name) != 0)
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800253 return -1;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800254 memset(output->data, 0, output->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800255
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800256 doffset = (2 * sizeof(*segs));
257
258 /* Prepare code segment */
259 segs[0].type = PAYLOAD_SEGMENT_CODE;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800260 segs[0].load_addr = loadaddress;
261 segs[0].mem_len = input->size;
262 segs[0].offset = doffset;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800263
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800264 compress(input->data, input->size, output->data + doffset, &len);
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800265 segs[0].compression = algo;
266 segs[0].len = len;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800267
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800268 if ((unsigned int)len >= input->size) {
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800269 WARN("Compressing data would make it bigger - disabled.\n");
270 segs[0].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800271 segs[0].len = input->size;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800272 memcpy(output->data + doffset, input->data, input->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800273 }
274
275 /* prepare entry point segment */
276 segs[1].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800277 segs[1].load_addr = entrypoint;
278 output->size = doffset + segs[0].len;
279 xdr_segs(output, segs, 2);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800280 return 0;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800281}
Stefan Reinauer543a6822013-02-04 15:39:13 -0800282
283int parse_fv_to_payload(const struct buffer *input,
284 struct buffer *output, comp_algo algo)
285{
286 comp_func_ptr compress;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800287 struct cbfs_payload_segment segs[2];
Stefan Reinauer543a6822013-02-04 15:39:13 -0800288 int doffset, len = 0;
289 firmware_volume_header_t *fv;
290 ffs_file_header_t *fh;
291 common_section_header_t *cs;
292 dos_header_t *dh;
293 coff_header_t *ch;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800294 int dh_offset;
295
Stefan Reinauere8764182013-02-05 13:46:49 -0800296 uint32_t loadaddress = 0;
297 uint32_t entrypoint = 0;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800298
299 compress = compression_function(algo);
300 if (!compress)
301 return -1;
302
303 DEBUG("start: parse_fv_to_payload\n");
304
305 fv = (firmware_volume_header_t *)input->data;
306 if (fv->signature != FV_SIGNATURE) {
307 INFO("Not a UEFI firmware volume.\n");
308 return -1;
309 }
310
311 fh = (ffs_file_header_t *)(input->data + fv->header_length);
Patrick Georgi46102472013-02-09 13:26:19 +0100312 while (fh->file_type == FILETYPE_PAD) {
313 unsigned long offset = (fh->size[2] << 16) | (fh->size[1] << 8) | fh->size[0];
Hung-Te Lin7b654a92013-02-18 18:35:00 +0800314 ERROR("skipping %lu bytes of FV padding\n", offset);
Patrick Georgi46102472013-02-09 13:26:19 +0100315 fh = (ffs_file_header_t *)(((void*)fh) + offset);
316 }
Stefan Reinauer543a6822013-02-04 15:39:13 -0800317 if (fh->file_type != FILETYPE_SEC) {
318 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800319 INFO("First file in first FV not a SEC core.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800320 return -1;
321 }
322
323 cs = (common_section_header_t *)&fh[1];
Patrick Georgi46102472013-02-09 13:26:19 +0100324 while (cs->section_type == SECTION_RAW) {
325 unsigned long offset = (cs->size[2] << 16) | (cs->size[1] << 8) | cs->size[0];
Hung-Te Lin7b654a92013-02-18 18:35:00 +0800326 ERROR("skipping %lu bytes of section padding\n", offset);
Patrick Georgi46102472013-02-09 13:26:19 +0100327 cs = (common_section_header_t *)(((void*)cs) + offset);
328 }
Stefan Reinauer543a6822013-02-04 15:39:13 -0800329 if (cs->section_type != SECTION_PE32) {
330 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800331 INFO("Section type not PE32.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800332 return -1;
333 }
334
335 dh = (dos_header_t *)&cs[1];
Stefan Reinauere8764182013-02-05 13:46:49 -0800336 if (dh->signature != DOS_MAGIC) {
Stefan Reinauer543a6822013-02-04 15:39:13 -0800337 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800338 INFO("DOS header signature wrong.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800339 return -1;
340 }
341
342 dh_offset = (unsigned long)dh - (unsigned long)input->data;
343 DEBUG("dos header offset = %x\n", dh_offset);
344
345 ch = (coff_header_t *)(((void *)dh)+dh->e_lfanew);
Stefan Reinauere8764182013-02-05 13:46:49 -0800346
347 if (ch->machine == MACHINE_TYPE_X86) {
348 pe_opt_header_32_t *ph;
349 ph = (pe_opt_header_32_t *)&ch[1];
350 if (ph->signature != PE_HDR_32_MAGIC) {
351 WARN("PE header signature incorrect.\n");
352 return -1;
353 }
354 DEBUG("image base %x\n", ph->image_addr);
355 DEBUG("entry point %x\n", ph->entry_point);
356
357 loadaddress = ph->image_addr - dh_offset;
358 entrypoint = ph->image_addr + ph->entry_point;
359 } else if (ch->machine == MACHINE_TYPE_X64) {
360 pe_opt_header_64_t *ph;
361 ph = (pe_opt_header_64_t *)&ch[1];
362 if (ph->signature != PE_HDR_64_MAGIC) {
363 WARN("PE header signature incorrect.\n");
364 return -1;
365 }
366 DEBUG("image base %lx\n", (unsigned long)ph->image_addr);
367 DEBUG("entry point %x\n", ph->entry_point);
368
369 loadaddress = ph->image_addr - dh_offset;
370 entrypoint = ph->image_addr + ph->entry_point;
371 } else {
372 ERROR("Machine type not x86 or x64.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800373 return -1;
374 }
375
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800376 if (buffer_create(output, (sizeof(segs) + input->size),
Stefan Reinauer543a6822013-02-04 15:39:13 -0800377 input->name) != 0)
378 return -1;
379
380 memset(output->data, 0, output->size);
381
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800382 doffset = (sizeof(segs));
Stefan Reinauer543a6822013-02-04 15:39:13 -0800383
384 /* Prepare code segment */
385 segs[0].type = PAYLOAD_SEGMENT_CODE;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800386 segs[0].load_addr = loadaddress;
387 segs[0].mem_len = input->size;
388 segs[0].offset = doffset;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800389
390 compress(input->data, input->size, output->data + doffset, &len);
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800391 segs[0].compression = algo;
392 segs[0].len = len;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800393
394 if ((unsigned int)len >= input->size) {
395 WARN("Compressing data would make it bigger - disabled.\n");
396 segs[0].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800397 segs[0].len = input->size;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800398 memcpy(output->data + doffset, input->data, input->size);
399 }
400
401 /* prepare entry point segment */
402 segs[1].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800403 segs[1].load_addr = entrypoint;
404 output->size = doffset + segs[0].len;
405 xdr_segs(output, segs, 2);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800406 return 0;
407
408}
Patrick Georgide36d332013-08-27 20:22:21 +0200409