blob: 9a356e8b3fc4e3c6b3ad8d37327c64e0df92fd3a [file] [log] [blame]
Patrick Georgi7333a112020-05-08 20:48:04 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00002
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
Werner Zeha7835c42018-06-15 14:02:18 +02006#include <commonlib/endian.h>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00007
Aaron Durbin54ef3062014-03-05 12:12:09 -06008#include "elfparsing.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00009#include "common.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000010#include "cbfs.h"
Stefan Reinauer543a6822013-02-04 15:39:13 -080011#include "fv.h"
12#include "coff.h"
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +020013#include "fdt.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000014
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080015/* serialize the seg array into the buffer.
16 * The buffer is assumed to be large enough.
17 */
Ronald G. Minnich818f3692014-02-04 08:29:35 -080018void xdr_segs(struct buffer *output,
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080019 struct cbfs_payload_segment *segs, int nseg)
20{
21 struct buffer outheader;
22 int i;
23
24 outheader.data = output->data;
25 outheader.size = 0;
26
27 for(i = 0; i < nseg; i++){
28 xdr_be.put32(&outheader, segs[i].type);
29 xdr_be.put32(&outheader, segs[i].compression);
30 xdr_be.put32(&outheader, segs[i].offset);
31 xdr_be.put64(&outheader, segs[i].load_addr);
32 xdr_be.put32(&outheader, segs[i].len);
33 xdr_be.put32(&outheader, segs[i].mem_len);
34 }
35}
Aaron Durbinca630272014-08-05 10:48:20 -050036
37void xdr_get_seg(struct cbfs_payload_segment *out,
38 struct cbfs_payload_segment *in)
39{
40 struct buffer inheader;
41
42 inheader.data = (void *)in;
43 inheader.size = sizeof(*in);
44
45 out->type = xdr_be.get32(&inheader);
46 out->compression = xdr_be.get32(&inheader);
47 out->offset = xdr_be.get32(&inheader);
48 out->load_addr = xdr_be.get64(&inheader);
49 out->len = xdr_be.get32(&inheader);
50 out->mem_len = xdr_be.get32(&inheader);
51}
52
Sol Boucher0e539312015-03-05 15:38:03 -080053int parse_elf_to_payload(const struct buffer *input, struct buffer *output,
Julius Wernerd4775652020-03-13 16:43:34 -070054 enum cbfs_compression 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;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000058 char *header;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000059 int headers;
60 int segments = 1;
61 int isize = 0, osize = 0;
62 int doffset = 0;
Patrick Georgi96990a22014-09-29 10:08:35 +020063 struct cbfs_payload_segment *segs = NULL;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000064 int i;
Patrick Georgi96990a22014-09-29 10:08:35 +020065 int ret = 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000066
Patrick Georgib7b56dd82009-09-14 13:29:27 +000067 comp_func_ptr compress = compression_function(algo);
68 if (!compress)
69 return -1;
70
Julius Werner06e3dca2024-03-06 13:34:44 -080071 if (elf_headers(input, &ehdr, &phdr, NULL) < 0)
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080072 return -1;
73
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080074 DEBUG("start: parse_elf_to_payload\n");
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080075 headers = ehdr.e_phnum;
76 header = input->data;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000077
Julius Werner06e3dca2024-03-06 13:34:44 -080078 /* Count the number of segment headers - we only care about PT_LOAD
79 headers, because that's what we're actually going to load */
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000080 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 }
Joel Kitchinga302e7f2018-07-19 07:36:38 +080092 /* Allocate and initialize the segment header array */
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080093 segs = calloc(segments, sizeof(*segs));
Patrick Georgi96990a22014-09-29 10:08:35 +020094 if (segs == NULL) {
95 ret = -1;
96 goto out;
97 }
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000098 /* Allocate a block of memory to store the data in */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080099 if (buffer_create(output, (segments * sizeof(*segs)) + isize,
Patrick Georgi96990a22014-09-29 10:08:35 +0200100 input->name) != 0) {
101 ret = -1;
102 goto out;
103 }
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800104 memset(output->data, 0, output->size);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000105
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800106 doffset = (segments * sizeof(*segs));
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000107
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800108 /* set up for output marshaling. This is a bit
109 * tricky as we are marshaling the headers at the front,
110 * and the data starting after the headers. We need to convert
111 * the headers to the right format but the data
112 * passes through unchanged. Unlike most XDR code,
113 * we are doing these two concurrently. The doffset is
114 * used to compute the address for the raw data, and the
115 * outheader is used to marshal the headers. To make it simpler
116 * for The Reader, we set up the headers in a separate array,
117 * then marshal them all at once to the output.
118 */
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000119 segments = 0;
120
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000121 for (i = 0; i < headers; i++) {
122 if (phdr[i].p_type != PT_LOAD)
123 continue;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000124 if (phdr[i].p_memsz == 0)
125 continue;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000126 if (phdr[i].p_filesz == 0) {
127 segs[segments].type = PAYLOAD_SEGMENT_BSS;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800128 segs[segments].load_addr = phdr[i].p_paddr;
129 segs[segments].mem_len = phdr[i].p_memsz;
130 segs[segments].offset = doffset;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000131
132 segments++;
133 continue;
134 }
135
Stefan Reinauer51f6a202012-01-11 12:40:14 -0800136 if (phdr[i].p_flags & PF_X)
137 segs[segments].type = PAYLOAD_SEGMENT_CODE;
138 else
139 segs[segments].type = PAYLOAD_SEGMENT_DATA;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800140 segs[segments].load_addr = phdr[i].p_paddr;
141 segs[segments].mem_len = phdr[i].p_memsz;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800142 segs[segments].offset = doffset;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000143
Gabe Black845aa142014-02-21 01:01:06 -0800144 /* If the compression failed or made the section is larger,
145 use the original stuff */
146
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000147 int len;
Gabe Blackdbd006b2014-02-20 23:38:49 -0800148 if (compress((char *)&header[phdr[i].p_offset],
Gabe Black845aa142014-02-21 01:01:06 -0800149 phdr[i].p_filesz, output->data + doffset, &len) ||
150 (unsigned int)len > phdr[i].p_filesz) {
151 WARN("Compression failed or would make the data bigger "
152 "- disabled.\n");
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000153 segs[segments].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800154 segs[segments].len = phdr[i].p_filesz;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800155 memcpy(output->data + doffset,
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000156 &header[phdr[i].p_offset], phdr[i].p_filesz);
Gabe Black845aa142014-02-21 01:01:06 -0800157 } else {
158 segs[segments].compression = algo;
159 segs[segments].len = len;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000160 }
161
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800162 doffset += segs[segments].len;
163 osize += segs[segments].len;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000164
165 segments++;
166 }
167
168 segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800169 segs[segments++].load_addr = ehdr.e_entry;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000170
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800171 output->size = (segments * sizeof(*segs)) + osize;
172 xdr_segs(output, segs, segments);
Patrick Georgi96990a22014-09-29 10:08:35 +0200173
174out:
175 if (segs) free(segs);
Patrick Georgi96990a22014-09-29 10:08:35 +0200176 if (phdr) free(phdr);
177 return ret;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000178}
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800179
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800180int parse_flat_binary_to_payload(const struct buffer *input,
181 struct buffer *output,
Patrick Georgi7691e962024-02-17 21:09:13 +0100182 uint64_t loadaddress,
183 uint64_t entrypoint,
Julius Wernerd4775652020-03-13 16:43:34 -0700184 enum cbfs_compression algo)
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800185{
186 comp_func_ptr compress;
Patrick Georgi71955a52018-07-19 17:33:57 +0200187 struct cbfs_payload_segment segs[2] = { {0} };
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800188 int doffset, len = 0;
189
190 compress = compression_function(algo);
191 if (!compress)
192 return -1;
193
194 DEBUG("start: parse_flat_binary_to_payload\n");
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800195 if (buffer_create(output, (sizeof(segs) + input->size),
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800196 input->name) != 0)
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800197 return -1;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800198 memset(output->data, 0, output->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800199
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800200 doffset = (2 * sizeof(*segs));
201
202 /* Prepare code segment */
203 segs[0].type = PAYLOAD_SEGMENT_CODE;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800204 segs[0].load_addr = loadaddress;
205 segs[0].mem_len = input->size;
206 segs[0].offset = doffset;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800207
Gabe Black845aa142014-02-21 01:01:06 -0800208 if (!compress(input->data, input->size, output->data + doffset, &len) &&
209 (unsigned int)len < input->size) {
210 segs[0].compression = algo;
211 segs[0].len = len;
212 } else {
213 WARN("Compression failed or would make the data bigger "
214 "- disabled.\n");
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800215 segs[0].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800216 segs[0].len = input->size;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800217 memcpy(output->data + doffset, input->data, input->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800218 }
219
220 /* prepare entry point segment */
221 segs[1].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800222 segs[1].load_addr = entrypoint;
223 output->size = doffset + segs[0].len;
224 xdr_segs(output, segs, 2);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800225 return 0;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800226}
Stefan Reinauer543a6822013-02-04 15:39:13 -0800227
Sol Boucher6310ccc2015-05-07 21:12:28 -0700228int parse_fv_to_payload(const struct buffer *input, struct buffer *output,
Julius Wernerd4775652020-03-13 16:43:34 -0700229 enum cbfs_compression algo)
Stefan Reinauer543a6822013-02-04 15:39:13 -0800230{
231 comp_func_ptr compress;
Patrick Georgi71955a52018-07-19 17:33:57 +0200232 struct cbfs_payload_segment segs[2] = { {0} };
Stefan Reinauer543a6822013-02-04 15:39:13 -0800233 int doffset, len = 0;
234 firmware_volume_header_t *fv;
Dun Tan4a359402021-09-01 10:29:41 +0800235 firmware_volume_ext_header_t *fvh_ext;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800236 ffs_file_header_t *fh;
237 common_section_header_t *cs;
238 dos_header_t *dh;
239 coff_header_t *ch;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800240 int dh_offset;
241
Stefan Reinauere8764182013-02-05 13:46:49 -0800242 uint32_t loadaddress = 0;
243 uint32_t entrypoint = 0;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800244
245 compress = compression_function(algo);
246 if (!compress)
247 return -1;
248
249 DEBUG("start: parse_fv_to_payload\n");
250
251 fv = (firmware_volume_header_t *)input->data;
252 if (fv->signature != FV_SIGNATURE) {
253 INFO("Not a UEFI firmware volume.\n");
254 return -1;
255 }
256
257 fh = (ffs_file_header_t *)(input->data + fv->header_length);
Dun Tan4a359402021-09-01 10:29:41 +0800258 if (fv->ext_header_offs != 0) {
259 fvh_ext = (firmware_volume_ext_header_t *)((uintptr_t)fv + fv->ext_header_offs);
260 fh = (ffs_file_header_t *)((uintptr_t)fvh_ext + fvh_ext->ext_header_size);
261 fh = (ffs_file_header_t *)(((uintptr_t)fh + 7) & ~7);
262 }
263
Patrick Georgi46102472013-02-09 13:26:19 +0100264 while (fh->file_type == FILETYPE_PAD) {
265 unsigned long offset = (fh->size[2] << 16) | (fh->size[1] << 8) | fh->size[0];
Patrick Georgi50bda052017-07-05 13:30:37 +0200266 DEBUG("skipping %lu bytes of FV padding\n", offset);
Sol Boucher0e539312015-03-05 15:38:03 -0800267 fh = (ffs_file_header_t *)(((uintptr_t)fh) + offset);
Patrick Georgi46102472013-02-09 13:26:19 +0100268 }
Stefan Reinauer543a6822013-02-04 15:39:13 -0800269 if (fh->file_type != FILETYPE_SEC) {
270 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800271 INFO("First file in first FV not a SEC core.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800272 return -1;
273 }
274
275 cs = (common_section_header_t *)&fh[1];
Patrick Georgi46102472013-02-09 13:26:19 +0100276 while (cs->section_type == SECTION_RAW) {
277 unsigned long offset = (cs->size[2] << 16) | (cs->size[1] << 8) | cs->size[0];
Patrick Georgi50bda052017-07-05 13:30:37 +0200278 DEBUG("skipping %lu bytes of section padding\n", offset);
Sol Boucher0e539312015-03-05 15:38:03 -0800279 cs = (common_section_header_t *)(((uintptr_t)cs) + offset);
Patrick Georgi46102472013-02-09 13:26:19 +0100280 }
Stefan Reinauer543a6822013-02-04 15:39:13 -0800281 if (cs->section_type != SECTION_PE32) {
282 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800283 INFO("Section type not PE32.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800284 return -1;
285 }
286
287 dh = (dos_header_t *)&cs[1];
Stefan Reinauere8764182013-02-05 13:46:49 -0800288 if (dh->signature != DOS_MAGIC) {
Stefan Reinauer543a6822013-02-04 15:39:13 -0800289 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800290 INFO("DOS header signature wrong.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800291 return -1;
292 }
293
294 dh_offset = (unsigned long)dh - (unsigned long)input->data;
295 DEBUG("dos header offset = %x\n", dh_offset);
296
Sol Boucher0e539312015-03-05 15:38:03 -0800297 ch = (coff_header_t *)(((uintptr_t)dh)+dh->e_lfanew);
Stefan Reinauere8764182013-02-05 13:46:49 -0800298
299 if (ch->machine == MACHINE_TYPE_X86) {
300 pe_opt_header_32_t *ph;
301 ph = (pe_opt_header_32_t *)&ch[1];
302 if (ph->signature != PE_HDR_32_MAGIC) {
303 WARN("PE header signature incorrect.\n");
304 return -1;
305 }
306 DEBUG("image base %x\n", ph->image_addr);
307 DEBUG("entry point %x\n", ph->entry_point);
308
309 loadaddress = ph->image_addr - dh_offset;
310 entrypoint = ph->image_addr + ph->entry_point;
Rex-BC Chen5020fe32021-07-15 17:47:47 +0800311 } else if (ch->machine == MACHINE_TYPE_X64 || ch->machine == MACHINE_TYPE_ARM64) {
Stefan Reinauere8764182013-02-05 13:46:49 -0800312 pe_opt_header_64_t *ph;
313 ph = (pe_opt_header_64_t *)&ch[1];
314 if (ph->signature != PE_HDR_64_MAGIC) {
315 WARN("PE header signature incorrect.\n");
316 return -1;
317 }
318 DEBUG("image base %lx\n", (unsigned long)ph->image_addr);
319 DEBUG("entry point %x\n", ph->entry_point);
320
321 loadaddress = ph->image_addr - dh_offset;
322 entrypoint = ph->image_addr + ph->entry_point;
323 } else {
Rex-BC Chen5020fe32021-07-15 17:47:47 +0800324 ERROR("Machine type not x86, x64, or arm64.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800325 return -1;
326 }
327
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800328 if (buffer_create(output, (sizeof(segs) + input->size),
Stefan Reinauer543a6822013-02-04 15:39:13 -0800329 input->name) != 0)
330 return -1;
331
332 memset(output->data, 0, output->size);
333
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800334 doffset = (sizeof(segs));
Stefan Reinauer543a6822013-02-04 15:39:13 -0800335
336 /* Prepare code segment */
337 segs[0].type = PAYLOAD_SEGMENT_CODE;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800338 segs[0].load_addr = loadaddress;
339 segs[0].mem_len = input->size;
340 segs[0].offset = doffset;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800341
Gabe Black845aa142014-02-21 01:01:06 -0800342 if (!compress(input->data, input->size, output->data + doffset, &len) &&
343 (unsigned int)len < input->size) {
344 segs[0].compression = algo;
345 segs[0].len = len;
346 } else {
347 WARN("Compression failed or would make the data bigger "
348 "- disabled.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800349 segs[0].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800350 segs[0].len = input->size;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800351 memcpy(output->data + doffset, input->data, input->size);
352 }
353
354 /* prepare entry point segment */
355 segs[1].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800356 segs[1].load_addr = entrypoint;
357 output->size = doffset + segs[0].len;
358 xdr_segs(output, segs, 2);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800359 return 0;
360
361}
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200362
363int parse_fit_to_payload(const struct buffer *input, struct buffer *output,
Julius Wernerd4775652020-03-13 16:43:34 -0700364 enum cbfs_compression algo)
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200365{
366 struct fdt_header *fdt_h;
367
368 DEBUG("start: parse_fit_to_payload\n");
369
370 fdt_h = buffer_get(input);
Werner Zeha7835c42018-06-15 14:02:18 +0200371 if (read_be32(&fdt_h->magic) != FDT_HEADER_MAGIC) {
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200372 INFO("Not a FIT payload.\n");
373 return -1;
374 }
375
376 /**
377 * For developers:
378 * Compress the kernel binary you're sourcing in your its-script
379 * manually with LZ4 or LZMA and add 'compression = "lz4"' or "lzma" to
380 * the kernel@1 node in the its-script before assembling the image with
381 * mkimage.
382 */
383 if (algo != CBFS_COMPRESS_NONE) {
384 ERROR("FIT images don't support whole-image compression,"
Fred Reitberger9049dfd2022-10-12 10:47:39 -0400385 " compress the kernel component instead!\n");
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200386 return -1;
387 }
388
389 if (buffer_create(output, buffer_size(input), input->name) != 0)
390 return -1;
391
392 memcpy(buffer_get(output), buffer_get(input), buffer_size(input));
393
394 DEBUG("done\n");
395
396 return 0;
397}