blob: 9f3dabf40fa608ba418cbfbe623125fc6fcad8c3 [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
26#include "common.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000027#include "cbfs.h"
Hung-Te Lin4505ceb2013-01-28 22:40:10 +080028#include "elf.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
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080032int parse_elf_to_payload(const struct buffer *input,
33 struct buffer *output, comp_algo algo)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000034{
35 Elf32_Phdr *phdr;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080036 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)input->data;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000037 Elf32_Shdr *shdr;
38 char *header;
39 char *strtab;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000040 int headers;
41 int segments = 1;
42 int isize = 0, osize = 0;
43 int doffset = 0;
Peter Stuge1d862de2009-04-14 00:08:34 +000044 struct cbfs_payload_segment *segs;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000045 int i;
46
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080047 if(!iself((unsigned char *)input->data)){
Stefan Reinauer543a6822013-02-04 15:39:13 -080048 INFO("The payload file is not in ELF format!\n");
David Hendricks90ca3b62012-11-16 14:48:22 -080049 return -1;
Cristi Magherusan19a99c62009-09-25 22:21:47 +000050 }
51
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080052 // The tool may work in architecture-independent way.
53 if (arch != CBFS_ARCHITECTURE_UNKNOWN &&
54 !((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) &&
David Hendricks90ca3b62012-11-16 14:48:22 -080055 !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +080056 ERROR("The payload file has the wrong architecture\n");
David Hendricks90ca3b62012-11-16 14:48:22 -080057 return -1;
58 }
Cristi Magherusan19a99c62009-09-25 22:21:47 +000059
Patrick Georgib7b56dd82009-09-14 13:29:27 +000060 comp_func_ptr compress = compression_function(algo);
61 if (!compress)
62 return -1;
63
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080064 DEBUG("start: parse_elf_to_payload\n");
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000065 headers = ehdr->e_phnum;
66 header = (char *)ehdr;
67
68 phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]);
69 shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]);
70
71 strtab = &header[shdr[ehdr->e_shstrndx].sh_offset];
72
73 /* Count the number of headers - look for the .notes.pinfo
74 * section */
75
76 for (i = 0; i < ehdr->e_shnum; i++) {
77 char *name;
78
79 if (i == ehdr->e_shstrndx)
80 continue;
81
82 if (shdr[i].sh_size == 0)
83 continue;
84
85 name = (char *)(strtab + shdr[i].sh_name);
86
Ronald G. Minnich5a1af7b2009-09-17 15:35:08 +000087 if (!strcmp(name, ".note.pinfo")) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000088 segments++;
Ronald G. Minnich5a1af7b2009-09-17 15:35:08 +000089 isize += (unsigned int)shdr[i].sh_size;
90 }
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000091 }
92
93 /* Now, regular headers - we only care about PT_LOAD headers,
94 * because thats what we're actually going to load
95 */
96
97 for (i = 0; i < headers; i++) {
98 if (phdr[i].p_type != PT_LOAD)
99 continue;
100
101 /* Empty segments are never interesting */
102 if (phdr[i].p_memsz == 0)
103 continue;
104
105 isize += phdr[i].p_filesz;
106
107 segments++;
108 }
109
110 /* Allocate a block of memory to store the data in */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800111 if (buffer_create(output, (segments * sizeof(*segs)) + isize,
112 input->name) != 0)
113 return -1;
114 memset(output->data, 0, output->size);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000115
Peter Stuge1d862de2009-04-14 00:08:34 +0000116 doffset = (segments * sizeof(struct cbfs_payload_segment));
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000117
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800118 segs = (struct cbfs_payload_segment *)output->data;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000119 segments = 0;
120
121 for (i = 0; i < ehdr->e_shnum; i++) {
122 char *name;
123
124 if (i == ehdr->e_shstrndx)
125 continue;
126
127 if (shdr[i].sh_size == 0)
128 continue;
129
130 name = (char *)(strtab + shdr[i].sh_name);
131
132 if (!strcmp(name, ".note.pinfo")) {
133 segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
134 segs[segments].load_addr = 0;
135 segs[segments].len = (unsigned int)shdr[i].sh_size;
136 segs[segments].offset = doffset;
137
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800138 memcpy((unsigned long *)(output->data + doffset),
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000139 &header[shdr[i].sh_offset], shdr[i].sh_size);
140
141 doffset += segs[segments].len;
142 osize += segs[segments].len;
143
144 segments++;
145 }
146 }
147
148 for (i = 0; i < headers; i++) {
149 if (phdr[i].p_type != PT_LOAD)
150 continue;
151
152 if (phdr[i].p_memsz == 0)
153 continue;
154
155 if (phdr[i].p_filesz == 0) {
156 segs[segments].type = PAYLOAD_SEGMENT_BSS;
157 segs[segments].load_addr =
Stefan Reinauera1e48242011-10-21 14:24:57 -0700158 (uint64_t)htonll(phdr[i].p_paddr);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000159 segs[segments].mem_len =
Stefan Reinauera1e48242011-10-21 14:24:57 -0700160 (uint32_t)htonl(phdr[i].p_memsz);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000161 segs[segments].offset = htonl(doffset);
162
163 segments++;
164 continue;
165 }
166
Stefan Reinauer51f6a202012-01-11 12:40:14 -0800167 if (phdr[i].p_flags & PF_X)
168 segs[segments].type = PAYLOAD_SEGMENT_CODE;
169 else
170 segs[segments].type = PAYLOAD_SEGMENT_DATA;
Stefan Reinauera1e48242011-10-21 14:24:57 -0700171 segs[segments].load_addr = (uint64_t)htonll(phdr[i].p_paddr);
172 segs[segments].mem_len = (uint32_t)htonl(phdr[i].p_memsz);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000173 segs[segments].compression = htonl(algo);
174 segs[segments].offset = htonl(doffset);
175
176 int len;
177 compress((char *)&header[phdr[i].p_offset],
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800178 phdr[i].p_filesz, output->data + doffset, &len);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000179 segs[segments].len = htonl(len);
180
181 /* If the compressed section is larger, then use the
182 original stuff */
183
Stefan Reinauer14ad50e2009-04-04 22:18:26 +0000184 if ((unsigned int)len > phdr[i].p_filesz) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000185 segs[segments].compression = 0;
186 segs[segments].len = htonl(phdr[i].p_filesz);
187
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800188 memcpy(output->data + doffset,
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000189 &header[phdr[i].p_offset], phdr[i].p_filesz);
190 }
191
192 doffset += ntohl(segs[segments].len);
193 osize += ntohl(segs[segments].len);
194
195 segments++;
196 }
197
198 segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800199 segs[segments++].load_addr = htonll(ehdr->e_entry);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000200
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800201 output->size = (segments * sizeof(struct cbfs_payload_segment)) + osize;
202 return 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000203}
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800204
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800205int parse_flat_binary_to_payload(const struct buffer *input,
206 struct buffer *output,
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800207 uint32_t loadaddress,
208 uint32_t entrypoint,
209 comp_algo algo)
210{
211 comp_func_ptr compress;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800212 struct cbfs_payload_segment *segs;
213 int doffset, len = 0;
214
215 compress = compression_function(algo);
216 if (!compress)
217 return -1;
218
219 DEBUG("start: parse_flat_binary_to_payload\n");
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800220 if (buffer_create(output, (2 * sizeof(*segs) + input->size),
221 input->name) != 0)
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800222 return -1;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800223 memset(output->data, 0, output->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800224
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800225 segs = (struct cbfs_payload_segment *)output->data;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800226 doffset = (2 * sizeof(*segs));
227
228 /* Prepare code segment */
229 segs[0].type = PAYLOAD_SEGMENT_CODE;
230 segs[0].load_addr = htonll(loadaddress);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800231 segs[0].mem_len = htonl(input->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800232 segs[0].offset = htonl(doffset);
233
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800234 compress(input->data, input->size, output->data + doffset, &len);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800235 segs[0].compression = htonl(algo);
236 segs[0].len = htonl(len);
237
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800238 if ((unsigned int)len >= input->size) {
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800239 WARN("Compressing data would make it bigger - disabled.\n");
240 segs[0].compression = 0;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800241 segs[0].len = htonl(input->size);
242 memcpy(output->data + doffset, input->data, input->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800243 }
244
245 /* prepare entry point segment */
246 segs[1].type = PAYLOAD_SEGMENT_ENTRY;
247 segs[1].load_addr = htonll(entrypoint);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800248 output->size = doffset + ntohl(segs[0].len);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800249
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800250 return 0;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800251}
Stefan Reinauer543a6822013-02-04 15:39:13 -0800252
253int parse_fv_to_payload(const struct buffer *input,
254 struct buffer *output, comp_algo algo)
255{
256 comp_func_ptr compress;
257 struct cbfs_payload_segment *segs;
258 int doffset, len = 0;
259 firmware_volume_header_t *fv;
260 ffs_file_header_t *fh;
261 common_section_header_t *cs;
262 dos_header_t *dh;
263 coff_header_t *ch;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800264 int dh_offset;
265
Stefan Reinauere8764182013-02-05 13:46:49 -0800266 uint32_t loadaddress = 0;
267 uint32_t entrypoint = 0;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800268
269 compress = compression_function(algo);
270 if (!compress)
271 return -1;
272
273 DEBUG("start: parse_fv_to_payload\n");
274
275 fv = (firmware_volume_header_t *)input->data;
276 if (fv->signature != FV_SIGNATURE) {
277 INFO("Not a UEFI firmware volume.\n");
278 return -1;
279 }
280
281 fh = (ffs_file_header_t *)(input->data + fv->header_length);
Patrick Georgi46102472013-02-09 13:26:19 +0100282 while (fh->file_type == FILETYPE_PAD) {
283 unsigned long offset = (fh->size[2] << 16) | (fh->size[1] << 8) | fh->size[0];
Hung-Te Lin7b654a92013-02-18 18:35:00 +0800284 ERROR("skipping %lu bytes of FV padding\n", offset);
Patrick Georgi46102472013-02-09 13:26:19 +0100285 fh = (ffs_file_header_t *)(((void*)fh) + offset);
286 }
Stefan Reinauer543a6822013-02-04 15:39:13 -0800287 if (fh->file_type != FILETYPE_SEC) {
288 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800289 INFO("First file in first FV not a SEC core.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800290 return -1;
291 }
292
293 cs = (common_section_header_t *)&fh[1];
Patrick Georgi46102472013-02-09 13:26:19 +0100294 while (cs->section_type == SECTION_RAW) {
295 unsigned long offset = (cs->size[2] << 16) | (cs->size[1] << 8) | cs->size[0];
Hung-Te Lin7b654a92013-02-18 18:35:00 +0800296 ERROR("skipping %lu bytes of section padding\n", offset);
Patrick Georgi46102472013-02-09 13:26:19 +0100297 cs = (common_section_header_t *)(((void*)cs) + offset);
298 }
Stefan Reinauer543a6822013-02-04 15:39:13 -0800299 if (cs->section_type != SECTION_PE32) {
300 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800301 INFO("Section type not PE32.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800302 return -1;
303 }
304
305 dh = (dos_header_t *)&cs[1];
Stefan Reinauere8764182013-02-05 13:46:49 -0800306 if (dh->signature != DOS_MAGIC) {
Stefan Reinauer543a6822013-02-04 15:39:13 -0800307 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800308 INFO("DOS header signature wrong.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800309 return -1;
310 }
311
312 dh_offset = (unsigned long)dh - (unsigned long)input->data;
313 DEBUG("dos header offset = %x\n", dh_offset);
314
315 ch = (coff_header_t *)(((void *)dh)+dh->e_lfanew);
Stefan Reinauere8764182013-02-05 13:46:49 -0800316
317 if (ch->machine == MACHINE_TYPE_X86) {
318 pe_opt_header_32_t *ph;
319 ph = (pe_opt_header_32_t *)&ch[1];
320 if (ph->signature != PE_HDR_32_MAGIC) {
321 WARN("PE header signature incorrect.\n");
322 return -1;
323 }
324 DEBUG("image base %x\n", ph->image_addr);
325 DEBUG("entry point %x\n", ph->entry_point);
326
327 loadaddress = ph->image_addr - dh_offset;
328 entrypoint = ph->image_addr + ph->entry_point;
329 } else if (ch->machine == MACHINE_TYPE_X64) {
330 pe_opt_header_64_t *ph;
331 ph = (pe_opt_header_64_t *)&ch[1];
332 if (ph->signature != PE_HDR_64_MAGIC) {
333 WARN("PE header signature incorrect.\n");
334 return -1;
335 }
336 DEBUG("image base %lx\n", (unsigned long)ph->image_addr);
337 DEBUG("entry point %x\n", ph->entry_point);
338
339 loadaddress = ph->image_addr - dh_offset;
340 entrypoint = ph->image_addr + ph->entry_point;
341 } else {
342 ERROR("Machine type not x86 or x64.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800343 return -1;
344 }
345
Stefan Reinauer543a6822013-02-04 15:39:13 -0800346 if (buffer_create(output, (2 * sizeof(*segs) + input->size),
347 input->name) != 0)
348 return -1;
349
350 memset(output->data, 0, output->size);
351
352 segs = (struct cbfs_payload_segment *)output->data;
353 doffset = (2 * sizeof(*segs));
354
355 /* Prepare code segment */
356 segs[0].type = PAYLOAD_SEGMENT_CODE;
357 segs[0].load_addr = htonll(loadaddress);
358 segs[0].mem_len = htonl(input->size);
359 segs[0].offset = htonl(doffset);
360
361 compress(input->data, input->size, output->data + doffset, &len);
362 segs[0].compression = htonl(algo);
363 segs[0].len = htonl(len);
364
365 if ((unsigned int)len >= input->size) {
366 WARN("Compressing data would make it bigger - disabled.\n");
367 segs[0].compression = 0;
368 segs[0].len = htonl(input->size);
369 memcpy(output->data + doffset, input->data, input->size);
370 }
371
372 /* prepare entry point segment */
373 segs[1].type = PAYLOAD_SEGMENT_ENTRY;
374 segs[1].load_addr = htonll(entrypoint);
375 output->size = doffset + ntohl(segs[0].len);
376
377 return 0;
378
379}