blob: 63c81a325ded7fc714d3bf502233c1e45a9dab0d [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;
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;
Patrick Georgi96990a22014-09-29 10:08:35 +020065 struct cbfs_payload_segment *segs = NULL;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000066 int i;
Patrick Georgi96990a22014-09-29 10:08:35 +020067 int ret = 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000068
Patrick Georgib7b56dd82009-09-14 13:29:27 +000069 comp_func_ptr compress = compression_function(algo);
70 if (!compress)
71 return -1;
72
Sol Boucher0e539312015-03-05 15:38:03 -080073 if (elf_headers(input, &ehdr, &phdr, &shdr) < 0)
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080074 return -1;
75
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080076 DEBUG("start: parse_elf_to_payload\n");
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080077 headers = ehdr.e_phnum;
78 header = input->data;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000079
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080080 strtab = &header[shdr[ehdr.e_shstrndx].sh_offset];
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000081
82 /* Count the number of headers - look for the .notes.pinfo
83 * section */
84
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080085 for (i = 0; i < ehdr.e_shnum; i++) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000086 char *name;
87
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080088 if (i == ehdr.e_shstrndx)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000089 continue;
90
91 if (shdr[i].sh_size == 0)
92 continue;
93
94 name = (char *)(strtab + shdr[i].sh_name);
95
Ronald G. Minnich5a1af7b2009-09-17 15:35:08 +000096 if (!strcmp(name, ".note.pinfo")) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000097 segments++;
Ronald G. Minnich5a1af7b2009-09-17 15:35:08 +000098 isize += (unsigned int)shdr[i].sh_size;
99 }
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000100 }
101
102 /* Now, regular headers - we only care about PT_LOAD headers,
Patrick Georgi01cfecc2020-01-29 13:31:16 +0100103 * because that's what we're actually going to load
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000104 */
105
106 for (i = 0; i < headers; i++) {
107 if (phdr[i].p_type != PT_LOAD)
108 continue;
109
110 /* Empty segments are never interesting */
111 if (phdr[i].p_memsz == 0)
112 continue;
113
114 isize += phdr[i].p_filesz;
115
116 segments++;
117 }
Joel Kitchinga302e7f2018-07-19 07:36:38 +0800118 /* Allocate and initialize the segment header array */
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800119 segs = calloc(segments, sizeof(*segs));
Patrick Georgi96990a22014-09-29 10:08:35 +0200120 if (segs == NULL) {
121 ret = -1;
122 goto out;
123 }
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000124 /* Allocate a block of memory to store the data in */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800125 if (buffer_create(output, (segments * sizeof(*segs)) + isize,
Patrick Georgi96990a22014-09-29 10:08:35 +0200126 input->name) != 0) {
127 ret = -1;
128 goto out;
129 }
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800130 memset(output->data, 0, output->size);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000131
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800132 doffset = (segments * sizeof(*segs));
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000133
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800134 /* set up for output marshaling. This is a bit
135 * tricky as we are marshaling the headers at the front,
136 * and the data starting after the headers. We need to convert
137 * the headers to the right format but the data
138 * passes through unchanged. Unlike most XDR code,
139 * we are doing these two concurrently. The doffset is
140 * used to compute the address for the raw data, and the
141 * outheader is used to marshal the headers. To make it simpler
142 * for The Reader, we set up the headers in a separate array,
143 * then marshal them all at once to the output.
144 */
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000145 segments = 0;
146
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800147 for (i = 0; i < ehdr.e_shnum; i++) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000148 char *name;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800149 if (i == ehdr.e_shstrndx)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000150 continue;
151
152 if (shdr[i].sh_size == 0)
153 continue;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000154 name = (char *)(strtab + shdr[i].sh_name);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000155 if (!strcmp(name, ".note.pinfo")) {
156 segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
157 segs[segments].load_addr = 0;
158 segs[segments].len = (unsigned int)shdr[i].sh_size;
159 segs[segments].offset = doffset;
160
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800161 memcpy((unsigned long *)(output->data + doffset),
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000162 &header[shdr[i].sh_offset], shdr[i].sh_size);
163
164 doffset += segs[segments].len;
165 osize += segs[segments].len;
166
167 segments++;
168 }
169 }
170
171 for (i = 0; i < headers; i++) {
172 if (phdr[i].p_type != PT_LOAD)
173 continue;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000174 if (phdr[i].p_memsz == 0)
175 continue;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000176 if (phdr[i].p_filesz == 0) {
177 segs[segments].type = PAYLOAD_SEGMENT_BSS;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800178 segs[segments].load_addr = phdr[i].p_paddr;
179 segs[segments].mem_len = phdr[i].p_memsz;
180 segs[segments].offset = doffset;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000181
182 segments++;
183 continue;
184 }
185
Stefan Reinauer51f6a202012-01-11 12:40:14 -0800186 if (phdr[i].p_flags & PF_X)
187 segs[segments].type = PAYLOAD_SEGMENT_CODE;
188 else
189 segs[segments].type = PAYLOAD_SEGMENT_DATA;
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;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800192 segs[segments].offset = doffset;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000193
Gabe Black845aa142014-02-21 01:01:06 -0800194 /* If the compression failed or made the section is larger,
195 use the original stuff */
196
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000197 int len;
Gabe Blackdbd006b2014-02-20 23:38:49 -0800198 if (compress((char *)&header[phdr[i].p_offset],
Gabe Black845aa142014-02-21 01:01:06 -0800199 phdr[i].p_filesz, output->data + doffset, &len) ||
200 (unsigned int)len > phdr[i].p_filesz) {
201 WARN("Compression failed or would make the data bigger "
202 "- disabled.\n");
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000203 segs[segments].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800204 segs[segments].len = phdr[i].p_filesz;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800205 memcpy(output->data + doffset,
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000206 &header[phdr[i].p_offset], phdr[i].p_filesz);
Gabe Black845aa142014-02-21 01:01:06 -0800207 } else {
208 segs[segments].compression = algo;
209 segs[segments].len = len;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000210 }
211
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800212 doffset += segs[segments].len;
213 osize += segs[segments].len;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000214
215 segments++;
216 }
217
218 segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800219 segs[segments++].load_addr = ehdr.e_entry;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000220
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800221 output->size = (segments * sizeof(*segs)) + osize;
222 xdr_segs(output, segs, segments);
Patrick Georgi96990a22014-09-29 10:08:35 +0200223
224out:
225 if (segs) free(segs);
226 if (shdr) free(shdr);
227 if (phdr) free(phdr);
228 return ret;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000229}
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800230
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800231int parse_flat_binary_to_payload(const struct buffer *input,
232 struct buffer *output,
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800233 uint32_t loadaddress,
234 uint32_t entrypoint,
Julius Wernerd4775652020-03-13 16:43:34 -0700235 enum cbfs_compression algo)
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800236{
237 comp_func_ptr compress;
Patrick Georgi71955a52018-07-19 17:33:57 +0200238 struct cbfs_payload_segment segs[2] = { {0} };
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800239 int doffset, len = 0;
240
241 compress = compression_function(algo);
242 if (!compress)
243 return -1;
244
245 DEBUG("start: parse_flat_binary_to_payload\n");
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800246 if (buffer_create(output, (sizeof(segs) + input->size),
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800247 input->name) != 0)
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800248 return -1;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800249 memset(output->data, 0, output->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800250
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800251 doffset = (2 * sizeof(*segs));
252
253 /* Prepare code segment */
254 segs[0].type = PAYLOAD_SEGMENT_CODE;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800255 segs[0].load_addr = loadaddress;
256 segs[0].mem_len = input->size;
257 segs[0].offset = doffset;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800258
Gabe Black845aa142014-02-21 01:01:06 -0800259 if (!compress(input->data, input->size, output->data + doffset, &len) &&
260 (unsigned int)len < input->size) {
261 segs[0].compression = algo;
262 segs[0].len = len;
263 } else {
264 WARN("Compression failed or would make the data bigger "
265 "- disabled.\n");
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800266 segs[0].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800267 segs[0].len = input->size;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800268 memcpy(output->data + doffset, input->data, input->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800269 }
270
271 /* prepare entry point segment */
272 segs[1].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800273 segs[1].load_addr = entrypoint;
274 output->size = doffset + segs[0].len;
275 xdr_segs(output, segs, 2);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800276 return 0;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800277}
Stefan Reinauer543a6822013-02-04 15:39:13 -0800278
Sol Boucher6310ccc2015-05-07 21:12:28 -0700279int parse_fv_to_payload(const struct buffer *input, struct buffer *output,
Julius Wernerd4775652020-03-13 16:43:34 -0700280 enum cbfs_compression algo)
Stefan Reinauer543a6822013-02-04 15:39:13 -0800281{
282 comp_func_ptr compress;
Patrick Georgi71955a52018-07-19 17:33:57 +0200283 struct cbfs_payload_segment segs[2] = { {0} };
Stefan Reinauer543a6822013-02-04 15:39:13 -0800284 int doffset, len = 0;
285 firmware_volume_header_t *fv;
Dun Tan4a359402021-09-01 10:29:41 +0800286 firmware_volume_ext_header_t *fvh_ext;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800287 ffs_file_header_t *fh;
288 common_section_header_t *cs;
289 dos_header_t *dh;
290 coff_header_t *ch;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800291 int dh_offset;
292
Stefan Reinauere8764182013-02-05 13:46:49 -0800293 uint32_t loadaddress = 0;
294 uint32_t entrypoint = 0;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800295
296 compress = compression_function(algo);
297 if (!compress)
298 return -1;
299
300 DEBUG("start: parse_fv_to_payload\n");
301
302 fv = (firmware_volume_header_t *)input->data;
303 if (fv->signature != FV_SIGNATURE) {
304 INFO("Not a UEFI firmware volume.\n");
305 return -1;
306 }
307
308 fh = (ffs_file_header_t *)(input->data + fv->header_length);
Dun Tan4a359402021-09-01 10:29:41 +0800309 if (fv->ext_header_offs != 0) {
310 fvh_ext = (firmware_volume_ext_header_t *)((uintptr_t)fv + fv->ext_header_offs);
311 fh = (ffs_file_header_t *)((uintptr_t)fvh_ext + fvh_ext->ext_header_size);
312 fh = (ffs_file_header_t *)(((uintptr_t)fh + 7) & ~7);
313 }
314
Patrick Georgi46102472013-02-09 13:26:19 +0100315 while (fh->file_type == FILETYPE_PAD) {
316 unsigned long offset = (fh->size[2] << 16) | (fh->size[1] << 8) | fh->size[0];
Patrick Georgi50bda052017-07-05 13:30:37 +0200317 DEBUG("skipping %lu bytes of FV padding\n", offset);
Sol Boucher0e539312015-03-05 15:38:03 -0800318 fh = (ffs_file_header_t *)(((uintptr_t)fh) + offset);
Patrick Georgi46102472013-02-09 13:26:19 +0100319 }
Stefan Reinauer543a6822013-02-04 15:39:13 -0800320 if (fh->file_type != FILETYPE_SEC) {
321 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800322 INFO("First file in first FV not a SEC core.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800323 return -1;
324 }
325
326 cs = (common_section_header_t *)&fh[1];
Patrick Georgi46102472013-02-09 13:26:19 +0100327 while (cs->section_type == SECTION_RAW) {
328 unsigned long offset = (cs->size[2] << 16) | (cs->size[1] << 8) | cs->size[0];
Patrick Georgi50bda052017-07-05 13:30:37 +0200329 DEBUG("skipping %lu bytes of section padding\n", offset);
Sol Boucher0e539312015-03-05 15:38:03 -0800330 cs = (common_section_header_t *)(((uintptr_t)cs) + offset);
Patrick Georgi46102472013-02-09 13:26:19 +0100331 }
Stefan Reinauer543a6822013-02-04 15:39:13 -0800332 if (cs->section_type != SECTION_PE32) {
333 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800334 INFO("Section type not PE32.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800335 return -1;
336 }
337
338 dh = (dos_header_t *)&cs[1];
Stefan Reinauere8764182013-02-05 13:46:49 -0800339 if (dh->signature != DOS_MAGIC) {
Stefan Reinauer543a6822013-02-04 15:39:13 -0800340 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800341 INFO("DOS header signature wrong.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800342 return -1;
343 }
344
345 dh_offset = (unsigned long)dh - (unsigned long)input->data;
346 DEBUG("dos header offset = %x\n", dh_offset);
347
Sol Boucher0e539312015-03-05 15:38:03 -0800348 ch = (coff_header_t *)(((uintptr_t)dh)+dh->e_lfanew);
Stefan Reinauere8764182013-02-05 13:46:49 -0800349
350 if (ch->machine == MACHINE_TYPE_X86) {
351 pe_opt_header_32_t *ph;
352 ph = (pe_opt_header_32_t *)&ch[1];
353 if (ph->signature != PE_HDR_32_MAGIC) {
354 WARN("PE header signature incorrect.\n");
355 return -1;
356 }
357 DEBUG("image base %x\n", ph->image_addr);
358 DEBUG("entry point %x\n", ph->entry_point);
359
360 loadaddress = ph->image_addr - dh_offset;
361 entrypoint = ph->image_addr + ph->entry_point;
Rex-BC Chen5020fe32021-07-15 17:47:47 +0800362 } else if (ch->machine == MACHINE_TYPE_X64 || ch->machine == MACHINE_TYPE_ARM64) {
Stefan Reinauere8764182013-02-05 13:46:49 -0800363 pe_opt_header_64_t *ph;
364 ph = (pe_opt_header_64_t *)&ch[1];
365 if (ph->signature != PE_HDR_64_MAGIC) {
366 WARN("PE header signature incorrect.\n");
367 return -1;
368 }
369 DEBUG("image base %lx\n", (unsigned long)ph->image_addr);
370 DEBUG("entry point %x\n", ph->entry_point);
371
372 loadaddress = ph->image_addr - dh_offset;
373 entrypoint = ph->image_addr + ph->entry_point;
374 } else {
Rex-BC Chen5020fe32021-07-15 17:47:47 +0800375 ERROR("Machine type not x86, x64, or arm64.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800376 return -1;
377 }
378
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800379 if (buffer_create(output, (sizeof(segs) + input->size),
Stefan Reinauer543a6822013-02-04 15:39:13 -0800380 input->name) != 0)
381 return -1;
382
383 memset(output->data, 0, output->size);
384
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800385 doffset = (sizeof(segs));
Stefan Reinauer543a6822013-02-04 15:39:13 -0800386
387 /* Prepare code segment */
388 segs[0].type = PAYLOAD_SEGMENT_CODE;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800389 segs[0].load_addr = loadaddress;
390 segs[0].mem_len = input->size;
391 segs[0].offset = doffset;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800392
Gabe Black845aa142014-02-21 01:01:06 -0800393 if (!compress(input->data, input->size, output->data + doffset, &len) &&
394 (unsigned int)len < input->size) {
395 segs[0].compression = algo;
396 segs[0].len = len;
397 } else {
398 WARN("Compression failed or would make the data bigger "
399 "- disabled.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800400 segs[0].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800401 segs[0].len = input->size;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800402 memcpy(output->data + doffset, input->data, input->size);
403 }
404
405 /* prepare entry point segment */
406 segs[1].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800407 segs[1].load_addr = entrypoint;
408 output->size = doffset + segs[0].len;
409 xdr_segs(output, segs, 2);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800410 return 0;
411
412}
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200413
414int parse_fit_to_payload(const struct buffer *input, struct buffer *output,
Julius Wernerd4775652020-03-13 16:43:34 -0700415 enum cbfs_compression algo)
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200416{
417 struct fdt_header *fdt_h;
418
419 DEBUG("start: parse_fit_to_payload\n");
420
421 fdt_h = buffer_get(input);
Werner Zeha7835c42018-06-15 14:02:18 +0200422 if (read_be32(&fdt_h->magic) != FDT_HEADER_MAGIC) {
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200423 INFO("Not a FIT payload.\n");
424 return -1;
425 }
426
427 /**
428 * For developers:
429 * Compress the kernel binary you're sourcing in your its-script
430 * manually with LZ4 or LZMA and add 'compression = "lz4"' or "lzma" to
431 * the kernel@1 node in the its-script before assembling the image with
432 * mkimage.
433 */
434 if (algo != CBFS_COMPRESS_NONE) {
435 ERROR("FIT images don't support whole-image compression,"
Fred Reitberger9049dfd2022-10-12 10:47:39 -0400436 " compress the kernel component instead!\n");
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200437 return -1;
438 }
439
440 if (buffer_create(output, buffer_size(input), input->name) != 0)
441 return -1;
442
443 memcpy(buffer_get(output), buffer_get(input), buffer_size(input));
444
445 DEBUG("done\n");
446
447 return 0;
448}