blob: ae875f36349be33aea7797a5c3ecc65b8659a049 [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;
286 ffs_file_header_t *fh;
287 common_section_header_t *cs;
288 dos_header_t *dh;
289 coff_header_t *ch;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800290 int dh_offset;
291
Stefan Reinauere8764182013-02-05 13:46:49 -0800292 uint32_t loadaddress = 0;
293 uint32_t entrypoint = 0;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800294
295 compress = compression_function(algo);
296 if (!compress)
297 return -1;
298
299 DEBUG("start: parse_fv_to_payload\n");
300
301 fv = (firmware_volume_header_t *)input->data;
302 if (fv->signature != FV_SIGNATURE) {
303 INFO("Not a UEFI firmware volume.\n");
304 return -1;
305 }
306
307 fh = (ffs_file_header_t *)(input->data + fv->header_length);
Patrick Georgi46102472013-02-09 13:26:19 +0100308 while (fh->file_type == FILETYPE_PAD) {
309 unsigned long offset = (fh->size[2] << 16) | (fh->size[1] << 8) | fh->size[0];
Patrick Georgi50bda052017-07-05 13:30:37 +0200310 DEBUG("skipping %lu bytes of FV padding\n", offset);
Sol Boucher0e539312015-03-05 15:38:03 -0800311 fh = (ffs_file_header_t *)(((uintptr_t)fh) + offset);
Patrick Georgi46102472013-02-09 13:26:19 +0100312 }
Stefan Reinauer543a6822013-02-04 15:39:13 -0800313 if (fh->file_type != FILETYPE_SEC) {
314 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800315 INFO("First file in first FV not a SEC core.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800316 return -1;
317 }
318
319 cs = (common_section_header_t *)&fh[1];
Patrick Georgi46102472013-02-09 13:26:19 +0100320 while (cs->section_type == SECTION_RAW) {
321 unsigned long offset = (cs->size[2] << 16) | (cs->size[1] << 8) | cs->size[0];
Patrick Georgi50bda052017-07-05 13:30:37 +0200322 DEBUG("skipping %lu bytes of section padding\n", offset);
Sol Boucher0e539312015-03-05 15:38:03 -0800323 cs = (common_section_header_t *)(((uintptr_t)cs) + offset);
Patrick Georgi46102472013-02-09 13:26:19 +0100324 }
Stefan Reinauer543a6822013-02-04 15:39:13 -0800325 if (cs->section_type != SECTION_PE32) {
326 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800327 INFO("Section type not PE32.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800328 return -1;
329 }
330
331 dh = (dos_header_t *)&cs[1];
Stefan Reinauere8764182013-02-05 13:46:49 -0800332 if (dh->signature != DOS_MAGIC) {
Stefan Reinauer543a6822013-02-04 15:39:13 -0800333 ERROR("Not a usable UEFI firmware volume.\n");
Stefan Reinauere8764182013-02-05 13:46:49 -0800334 INFO("DOS header signature wrong.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800335 return -1;
336 }
337
338 dh_offset = (unsigned long)dh - (unsigned long)input->data;
339 DEBUG("dos header offset = %x\n", dh_offset);
340
Sol Boucher0e539312015-03-05 15:38:03 -0800341 ch = (coff_header_t *)(((uintptr_t)dh)+dh->e_lfanew);
Stefan Reinauere8764182013-02-05 13:46:49 -0800342
343 if (ch->machine == MACHINE_TYPE_X86) {
344 pe_opt_header_32_t *ph;
345 ph = (pe_opt_header_32_t *)&ch[1];
346 if (ph->signature != PE_HDR_32_MAGIC) {
347 WARN("PE header signature incorrect.\n");
348 return -1;
349 }
350 DEBUG("image base %x\n", ph->image_addr);
351 DEBUG("entry point %x\n", ph->entry_point);
352
353 loadaddress = ph->image_addr - dh_offset;
354 entrypoint = ph->image_addr + ph->entry_point;
355 } else if (ch->machine == MACHINE_TYPE_X64) {
356 pe_opt_header_64_t *ph;
357 ph = (pe_opt_header_64_t *)&ch[1];
358 if (ph->signature != PE_HDR_64_MAGIC) {
359 WARN("PE header signature incorrect.\n");
360 return -1;
361 }
362 DEBUG("image base %lx\n", (unsigned long)ph->image_addr);
363 DEBUG("entry point %x\n", ph->entry_point);
364
365 loadaddress = ph->image_addr - dh_offset;
366 entrypoint = ph->image_addr + ph->entry_point;
367 } else {
368 ERROR("Machine type not x86 or x64.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800369 return -1;
370 }
371
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800372 if (buffer_create(output, (sizeof(segs) + input->size),
Stefan Reinauer543a6822013-02-04 15:39:13 -0800373 input->name) != 0)
374 return -1;
375
376 memset(output->data, 0, output->size);
377
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800378 doffset = (sizeof(segs));
Stefan Reinauer543a6822013-02-04 15:39:13 -0800379
380 /* Prepare code segment */
381 segs[0].type = PAYLOAD_SEGMENT_CODE;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800382 segs[0].load_addr = loadaddress;
383 segs[0].mem_len = input->size;
384 segs[0].offset = doffset;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800385
Gabe Black845aa142014-02-21 01:01:06 -0800386 if (!compress(input->data, input->size, output->data + doffset, &len) &&
387 (unsigned int)len < input->size) {
388 segs[0].compression = algo;
389 segs[0].len = len;
390 } else {
391 WARN("Compression failed or would make the data bigger "
392 "- disabled.\n");
Stefan Reinauer543a6822013-02-04 15:39:13 -0800393 segs[0].compression = 0;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800394 segs[0].len = input->size;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800395 memcpy(output->data + doffset, input->data, input->size);
396 }
397
398 /* prepare entry point segment */
399 segs[1].type = PAYLOAD_SEGMENT_ENTRY;
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -0800400 segs[1].load_addr = entrypoint;
401 output->size = doffset + segs[0].len;
402 xdr_segs(output, segs, 2);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800403 return 0;
404
405}
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200406
407int parse_fit_to_payload(const struct buffer *input, struct buffer *output,
Julius Wernerd4775652020-03-13 16:43:34 -0700408 enum cbfs_compression algo)
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200409{
410 struct fdt_header *fdt_h;
411
412 DEBUG("start: parse_fit_to_payload\n");
413
414 fdt_h = buffer_get(input);
Werner Zeha7835c42018-06-15 14:02:18 +0200415 if (read_be32(&fdt_h->magic) != FDT_HEADER_MAGIC) {
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200416 INFO("Not a FIT payload.\n");
417 return -1;
418 }
419
420 /**
421 * For developers:
422 * Compress the kernel binary you're sourcing in your its-script
423 * manually with LZ4 or LZMA and add 'compression = "lz4"' or "lzma" to
424 * the kernel@1 node in the its-script before assembling the image with
425 * mkimage.
426 */
427 if (algo != CBFS_COMPRESS_NONE) {
428 ERROR("FIT images don't support whole-image compression,"
429 " compress the kernel component instead!\n")
430 return -1;
431 }
432
433 if (buffer_create(output, buffer_size(input), input->name) != 0)
434 return -1;
435
436 memcpy(buffer_get(output), buffer_get(input), buffer_size(input));
437
438 DEBUG("done\n");
439
440 return 0;
441}