util/: Replace GPLv2 boiler plate with SPDX header
Used commands:
perl -i -p0e 's|\/\*[\s*]*.*is free software[:;][\s*]*you[\s*]*can[\s*]*redistribute[\s*]*it[\s*]*and\/or[\s*]*modify[\s*]*it[\s*]*under[\s*]*the[\s*]*terms[\s*]*of[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*as[\s*]*published[\s*]*by[\s*]*the[\s*]*Free[\s*]*Software[\s*]*Foundation[;,][\s*]*version[\s*]*2[\s*]*of[\s*]*the[\s*]*License.[\s*]*This[\s*]*program[\s*]*is[\s*]*distributed[\s*]*in[\s*]*the[\s*]*hope[\s*]*that[\s*]*it[\s*]*will[\s*]*be[\s*]*useful,[\s*]*but[\s*]*WITHOUT[\s*]*ANY[\s*]*WARRANTY;[\s*]*without[\s*]*even[\s*]*the[\s*]*implied[\s*]*warranty[\s*]*of[\s*]*MERCHANTABILITY[\s*]*or[\s*]*FITNESS[\s*]*FOR[\s*]*A[\s*]*PARTICULAR[\s*]*PURPOSE.[\s*]*See[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*for[\s*]*more[\s*]*details.[\s*]*\*\/|/* SPDX-License-Identifier: GPL-2.0-only */|' $(cat filelist)
perl -i -p0e 's|This[\s*]*program[\s*]*is[\s*]*free[\s*]*software[:;][\s*]*you[\s*]*can[\s*]*redistribute[\s*]*it[\s*]*and/or[\s*]*modify[\s*]*it[\s*]*under[\s*]*the[\s*]*terms[\s*]*of[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*as[\s*]*published[\s*]*by[\s*]*the[\s*]*Free[\s*]*Software[\s*]*Foundation[;,][\s*]*either[\s*]*version[\s*]*2[\s*]*of[\s*]*the[\s*]*License,[\s*]*or[\s*]*.at[\s*]*your[\s*]*option.*[\s*]*any[\s*]*later[\s*]*version.[\s*]*This[\s*]*program[\s*]*is[\s*]*distributed[\s*]*in[\s*]*the[\s*]*hope[\s*]*that[\s*]*it[\s*]*will[\s*]*be[\s*]*useful,[\s*]*but[\s*]*WITHOUT[\s*]*ANY[\s*]*WARRANTY;[\s*]*without[\s*]*even[\s*]*the[\s*]*implied[\s*]*warranty[\s*]*of[\s*]*MERCHANTABILITY[\s*]*or[\s*]*FITNESS[\s*]*FOR[\s*]*A[\s*]*PARTICULAR[\s*]*PURPOSE.[\s*]*See[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*for[\s*]*more[\s*]*details.[\s*]*\*\/|/* SPDX-License-Identifier: GPL-2.0-or-later */|' $(cat filelist)
perl -i -p0e 's|\/\*[\s*]*.*This[\s*#]*program[\s*#]*is[\s*#]*free[\s*#]*software[;:,][\s*#]*you[\s*#]*can[\s*#]*redistribute[\s*#]*it[\s*#]*and/or[\s*#]*modify[\s*#]*it[\s*#]*under[\s*#]*the[\s*#]*terms[\s*#]*of[\s*#]*the[\s*#]*GNU[\s*#]*General[\s*#]*Public[\s*#]*License[\s*#]*as[\s*#]*published[\s*#]*by[\s*#]*the[\s*#]*Free[\s*#]*Software[\s*#]*Foundation[;:,][\s*#]*either[\s*#]*version[\s*#]*3[\s*#]*of[\s*#]*the[\s*#]*License[;:,][\s*#]*or[\s*#]*.at[\s*#]*your[\s*#]*option.*[\s*#]*any[\s*#]*later[\s*#]*version.[\s*#]*This[\s*#]*program[\s*#]*is[\s*#]*distributed[\s*#]*in[\s*#]*the[\s*#]*hope[\s*#]*that[\s*#]*it[\s*#]*will[\s*#]*be[\s*#]*useful[;:,][\s*#]*but[\s*#]*WITHOUT[\s*#]*ANY[\s*#]*WARRANTY[;:,][\s*#]*without[\s*#]*even[\s*#]*the[\s*#]*implied[\s*#]*warranty[\s*#]*of[\s*#]*MERCHANTABILITY[\s*#]*or[\s*#]*FITNESS[\s*#]*FOR[\s*#]*A[\s*#]*PARTICULAR[\s*#]*PURPOSE.[\s*#]*See[\s*#]*the[\s*#]*GNU[\s*#]*General[\s*#]*Public[\s*#]*License[\s*#]*for[\s*#]*more[\s*#]*details.[\s*]*\*\/|/* SPDX-License-Identifier: GPL-3.0-or-later */|' $(cat filelist)
perl -i -p0e 's|(\#\#*)[\w]*.*is free software[:;][\#\s]*you[\#\s]*can[\#\s]*redistribute[\#\s]*it[\#\s]*and\/or[\#\s]*modify[\#\s]*it[\s\#]*under[\s \#]*the[\s\#]*terms[\s\#]*of[\s\#]*the[\s\#]*GNU[\s\#]*General[\s\#]*Public[\s\#]*License[\s\#]*as[\s\#]*published[\s\#]*by[\s\#]*the[\s\#]*Free[\s\#]*Software[\s\#]*Foundation[;,][\s\#]*version[\s\#]*2[\s\#]*of[\s\#]*the[\s\#]*License.*[\s\#]*This[\s\#]*program[\s\#]*is[\s\#]*distributed[\s\#]*in[\s\#]*the[\s\#]*hope[\s\#]*that[\s\#]*it[\s\#]*will[\#\s]*be[\#\s]*useful,[\#\s]*but[\#\s]*WITHOUT[\#\s]*ANY[\#\s]*WARRANTY;[\#\s]*without[\#\s]*even[\#\s]*the[\#\s]*implied[\#\s]*warranty[\#\s]*of[\#\s]*MERCHANTABILITY[\#\s]*or[\#\s]*FITNESS[\#\s]*FOR[\#\s]*A[\#\s]*PARTICULAR[\#\s]*PURPOSE.[\#\s]*See[\#\s]*the[\#\s]*GNU[\#\s]*General[\#\s]*Public[\#\s]*License[\#\s]*for[\#\s]*more[\#\s]*details.\s(#* *\n)*|\1 SPDX-License-Identifier: GPL-2.0-only\n\n|' $(cat filelist)
perl -i -p0e 's|(\#\#*)[\w*]*.*is free software[:;][\s*]*you[\s*]*can[\s*]*redistribute[\s*]*it[\s*]*and\/or[\s*]*modify[\s*]*it[\s*]*under[\s*]*the[\s*]*terms[\s*]*of[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*as[\s*]*published[\s*]*by[\s*]*the[\s*]*Free[\s*]*Software[\s*]*Foundation[;,][\s*]*version[\s*]*2[\s*]*of[\s*]*the[\s*]*License.[\s*]*This[\s*]*program[\s*]*is[\s*]*distributed[\s*]*in[\s*]*the[\s*]*hope[\s*]*that[\s*]*it[\s*]*will[\s*]*be[\s*]*useful,[\s*]*but[\s*]*WITHOUT[\s*]*ANY[\s*]*WARRANTY;[\s*]*without[\s*]*even[\s*]*the[\s*]*implied[\s*]*warranty[\s*]*of[\s*]*MERCHANTABILITY[\s*]*or[\s*]*FITNESS[\s*]*FOR[\s*]*A[\s*]*PARTICULAR[\s*]*PURPOSE.[\s*]*See[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*for[\s*]*more[\s*]*details.\s(#* *\n)*|\1 SPDX-License-Identifier: GPL-2.0-only\n\n|' $(cat filelist)
Change-Id: I1008a63b804f355a916221ac994701d7584f60ff
Signed-off-by: Patrick Georgi <pgeorgi@google.com>
Signed-off-by: Elyes HAOUAS <ehaouas@noos.fr>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/41177
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
2020-05-08 20:48:04 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2009-03-31 13:57:36 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-06-15 14:02:18 +02:00
|
|
|
#include <commonlib/endian.h>
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2014-03-05 19:12:09 +01:00
|
|
|
#include "elfparsing.h"
|
2009-03-31 13:57:36 +02:00
|
|
|
#include "common.h"
|
2009-09-14 15:29:27 +02:00
|
|
|
#include "cbfs.h"
|
2013-02-05 00:39:13 +01:00
|
|
|
#include "fv.h"
|
|
|
|
#include "coff.h"
|
2018-04-26 09:35:13 +02:00
|
|
|
#include "fdt.h"
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2013-12-30 22:16:18 +01:00
|
|
|
/* serialize the seg array into the buffer.
|
|
|
|
* The buffer is assumed to be large enough.
|
|
|
|
*/
|
2014-02-04 17:29:35 +01:00
|
|
|
void xdr_segs(struct buffer *output,
|
2013-12-30 22:16:18 +01:00
|
|
|
struct cbfs_payload_segment *segs, int nseg)
|
|
|
|
{
|
|
|
|
struct buffer outheader;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
outheader.data = output->data;
|
|
|
|
outheader.size = 0;
|
|
|
|
|
|
|
|
for(i = 0; i < nseg; i++){
|
|
|
|
xdr_be.put32(&outheader, segs[i].type);
|
|
|
|
xdr_be.put32(&outheader, segs[i].compression);
|
|
|
|
xdr_be.put32(&outheader, segs[i].offset);
|
|
|
|
xdr_be.put64(&outheader, segs[i].load_addr);
|
|
|
|
xdr_be.put32(&outheader, segs[i].len);
|
|
|
|
xdr_be.put32(&outheader, segs[i].mem_len);
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 17:48:20 +02:00
|
|
|
|
|
|
|
void xdr_get_seg(struct cbfs_payload_segment *out,
|
|
|
|
struct cbfs_payload_segment *in)
|
|
|
|
{
|
|
|
|
struct buffer inheader;
|
|
|
|
|
|
|
|
inheader.data = (void *)in;
|
|
|
|
inheader.size = sizeof(*in);
|
|
|
|
|
|
|
|
out->type = xdr_be.get32(&inheader);
|
|
|
|
out->compression = xdr_be.get32(&inheader);
|
|
|
|
out->offset = xdr_be.get32(&inheader);
|
|
|
|
out->load_addr = xdr_be.get64(&inheader);
|
|
|
|
out->len = xdr_be.get32(&inheader);
|
|
|
|
out->mem_len = xdr_be.get32(&inheader);
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:38:03 +01:00
|
|
|
int parse_elf_to_payload(const struct buffer *input, struct buffer *output,
|
2015-05-08 06:12:28 +02:00
|
|
|
enum comp_algo algo)
|
2009-03-31 13:57:36 +02:00
|
|
|
{
|
2013-12-30 22:16:18 +01:00
|
|
|
Elf64_Phdr *phdr;
|
|
|
|
Elf64_Ehdr ehdr;
|
|
|
|
Elf64_Shdr *shdr;
|
2009-03-31 13:57:36 +02:00
|
|
|
char *header;
|
|
|
|
char *strtab;
|
|
|
|
int headers;
|
|
|
|
int segments = 1;
|
|
|
|
int isize = 0, osize = 0;
|
|
|
|
int doffset = 0;
|
2014-09-29 10:08:35 +02:00
|
|
|
struct cbfs_payload_segment *segs = NULL;
|
2009-03-31 13:57:36 +02:00
|
|
|
int i;
|
2014-09-29 10:08:35 +02:00
|
|
|
int ret = 0;
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2009-09-14 15:29:27 +02:00
|
|
|
comp_func_ptr compress = compression_function(algo);
|
|
|
|
if (!compress)
|
|
|
|
return -1;
|
|
|
|
|
2015-03-06 00:38:03 +01:00
|
|
|
if (elf_headers(input, &ehdr, &phdr, &shdr) < 0)
|
2013-12-30 22:16:18 +01:00
|
|
|
return -1;
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2013-12-30 22:16:18 +01:00
|
|
|
DEBUG("start: parse_elf_to_payload\n");
|
|
|
|
headers = ehdr.e_phnum;
|
|
|
|
header = input->data;
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2013-12-30 22:16:18 +01:00
|
|
|
strtab = &header[shdr[ehdr.e_shstrndx].sh_offset];
|
2009-03-31 13:57:36 +02:00
|
|
|
|
|
|
|
/* Count the number of headers - look for the .notes.pinfo
|
|
|
|
* section */
|
|
|
|
|
2013-12-30 22:16:18 +01:00
|
|
|
for (i = 0; i < ehdr.e_shnum; i++) {
|
2009-03-31 13:57:36 +02:00
|
|
|
char *name;
|
|
|
|
|
2013-12-30 22:16:18 +01:00
|
|
|
if (i == ehdr.e_shstrndx)
|
2009-03-31 13:57:36 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (shdr[i].sh_size == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
name = (char *)(strtab + shdr[i].sh_name);
|
|
|
|
|
2009-09-17 17:35:08 +02:00
|
|
|
if (!strcmp(name, ".note.pinfo")) {
|
2009-03-31 13:57:36 +02:00
|
|
|
segments++;
|
2009-09-17 17:35:08 +02:00
|
|
|
isize += (unsigned int)shdr[i].sh_size;
|
|
|
|
}
|
2009-03-31 13:57:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, regular headers - we only care about PT_LOAD headers,
|
2020-01-29 13:31:16 +01:00
|
|
|
* because that's what we're actually going to load
|
2009-03-31 13:57:36 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < headers; i++) {
|
|
|
|
if (phdr[i].p_type != PT_LOAD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Empty segments are never interesting */
|
|
|
|
if (phdr[i].p_memsz == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
isize += phdr[i].p_filesz;
|
|
|
|
|
|
|
|
segments++;
|
|
|
|
}
|
2018-07-19 01:36:38 +02:00
|
|
|
/* Allocate and initialize the segment header array */
|
2013-12-30 22:16:18 +01:00
|
|
|
segs = calloc(segments, sizeof(*segs));
|
2014-09-29 10:08:35 +02:00
|
|
|
if (segs == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-03-31 13:57:36 +02:00
|
|
|
/* Allocate a block of memory to store the data in */
|
2013-01-29 08:22:11 +01:00
|
|
|
if (buffer_create(output, (segments * sizeof(*segs)) + isize,
|
2014-09-29 10:08:35 +02:00
|
|
|
input->name) != 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-01-29 08:22:11 +01:00
|
|
|
memset(output->data, 0, output->size);
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2013-12-30 22:16:18 +01:00
|
|
|
doffset = (segments * sizeof(*segs));
|
|
|
|
|
|
|
|
/* set up for output marshaling. This is a bit
|
|
|
|
* tricky as we are marshaling the headers at the front,
|
|
|
|
* and the data starting after the headers. We need to convert
|
|
|
|
* the headers to the right format but the data
|
|
|
|
* passes through unchanged. Unlike most XDR code,
|
|
|
|
* we are doing these two concurrently. The doffset is
|
|
|
|
* used to compute the address for the raw data, and the
|
|
|
|
* outheader is used to marshal the headers. To make it simpler
|
|
|
|
* for The Reader, we set up the headers in a separate array,
|
|
|
|
* then marshal them all at once to the output.
|
|
|
|
*/
|
2009-03-31 13:57:36 +02:00
|
|
|
segments = 0;
|
|
|
|
|
2013-12-30 22:16:18 +01:00
|
|
|
for (i = 0; i < ehdr.e_shnum; i++) {
|
2009-03-31 13:57:36 +02:00
|
|
|
char *name;
|
2013-12-30 22:16:18 +01:00
|
|
|
if (i == ehdr.e_shstrndx)
|
2009-03-31 13:57:36 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (shdr[i].sh_size == 0)
|
|
|
|
continue;
|
|
|
|
name = (char *)(strtab + shdr[i].sh_name);
|
|
|
|
if (!strcmp(name, ".note.pinfo")) {
|
|
|
|
segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
|
|
|
|
segs[segments].load_addr = 0;
|
|
|
|
segs[segments].len = (unsigned int)shdr[i].sh_size;
|
|
|
|
segs[segments].offset = doffset;
|
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
memcpy((unsigned long *)(output->data + doffset),
|
2009-03-31 13:57:36 +02:00
|
|
|
&header[shdr[i].sh_offset], shdr[i].sh_size);
|
|
|
|
|
|
|
|
doffset += segs[segments].len;
|
|
|
|
osize += segs[segments].len;
|
|
|
|
|
|
|
|
segments++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < headers; i++) {
|
|
|
|
if (phdr[i].p_type != PT_LOAD)
|
|
|
|
continue;
|
|
|
|
if (phdr[i].p_memsz == 0)
|
|
|
|
continue;
|
|
|
|
if (phdr[i].p_filesz == 0) {
|
|
|
|
segs[segments].type = PAYLOAD_SEGMENT_BSS;
|
2013-12-30 22:16:18 +01:00
|
|
|
segs[segments].load_addr = phdr[i].p_paddr;
|
|
|
|
segs[segments].mem_len = phdr[i].p_memsz;
|
|
|
|
segs[segments].offset = doffset;
|
2009-03-31 13:57:36 +02:00
|
|
|
|
|
|
|
segments++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-11 21:40:14 +01:00
|
|
|
if (phdr[i].p_flags & PF_X)
|
|
|
|
segs[segments].type = PAYLOAD_SEGMENT_CODE;
|
|
|
|
else
|
|
|
|
segs[segments].type = PAYLOAD_SEGMENT_DATA;
|
2013-12-30 22:16:18 +01:00
|
|
|
segs[segments].load_addr = phdr[i].p_paddr;
|
|
|
|
segs[segments].mem_len = phdr[i].p_memsz;
|
|
|
|
segs[segments].offset = doffset;
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2014-02-21 10:01:06 +01:00
|
|
|
/* If the compression failed or made the section is larger,
|
|
|
|
use the original stuff */
|
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
int len;
|
2014-02-21 08:38:49 +01:00
|
|
|
if (compress((char *)&header[phdr[i].p_offset],
|
2014-02-21 10:01:06 +01:00
|
|
|
phdr[i].p_filesz, output->data + doffset, &len) ||
|
|
|
|
(unsigned int)len > phdr[i].p_filesz) {
|
|
|
|
WARN("Compression failed or would make the data bigger "
|
|
|
|
"- disabled.\n");
|
2009-03-31 13:57:36 +02:00
|
|
|
segs[segments].compression = 0;
|
2013-12-30 22:16:18 +01:00
|
|
|
segs[segments].len = phdr[i].p_filesz;
|
2013-01-29 08:22:11 +01:00
|
|
|
memcpy(output->data + doffset,
|
2009-03-31 13:57:36 +02:00
|
|
|
&header[phdr[i].p_offset], phdr[i].p_filesz);
|
2014-02-21 10:01:06 +01:00
|
|
|
} else {
|
|
|
|
segs[segments].compression = algo;
|
|
|
|
segs[segments].len = len;
|
2009-03-31 13:57:36 +02:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:16:18 +01:00
|
|
|
doffset += segs[segments].len;
|
|
|
|
osize += segs[segments].len;
|
2009-03-31 13:57:36 +02:00
|
|
|
|
|
|
|
segments++;
|
|
|
|
}
|
|
|
|
|
|
|
|
segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
|
2013-12-30 22:16:18 +01:00
|
|
|
segs[segments++].load_addr = ehdr.e_entry;
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2013-12-30 22:16:18 +01:00
|
|
|
output->size = (segments * sizeof(*segs)) + osize;
|
|
|
|
xdr_segs(output, segs, segments);
|
2014-09-29 10:08:35 +02:00
|
|
|
|
|
|
|
out:
|
|
|
|
if (segs) free(segs);
|
|
|
|
if (shdr) free(shdr);
|
|
|
|
if (phdr) free(phdr);
|
|
|
|
return ret;
|
2009-03-31 13:57:36 +02:00
|
|
|
}
|
2013-01-28 08:04:30 +01:00
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
int parse_flat_binary_to_payload(const struct buffer *input,
|
|
|
|
struct buffer *output,
|
2013-01-28 08:04:30 +01:00
|
|
|
uint32_t loadaddress,
|
|
|
|
uint32_t entrypoint,
|
2015-05-08 06:12:28 +02:00
|
|
|
enum comp_algo algo)
|
2013-01-28 08:04:30 +01:00
|
|
|
{
|
|
|
|
comp_func_ptr compress;
|
2018-07-19 17:33:57 +02:00
|
|
|
struct cbfs_payload_segment segs[2] = { {0} };
|
2013-01-28 08:04:30 +01:00
|
|
|
int doffset, len = 0;
|
|
|
|
|
|
|
|
compress = compression_function(algo);
|
|
|
|
if (!compress)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
DEBUG("start: parse_flat_binary_to_payload\n");
|
2013-12-30 22:16:18 +01:00
|
|
|
if (buffer_create(output, (sizeof(segs) + input->size),
|
2013-01-29 08:22:11 +01:00
|
|
|
input->name) != 0)
|
2013-01-28 08:04:30 +01:00
|
|
|
return -1;
|
2013-01-29 08:22:11 +01:00
|
|
|
memset(output->data, 0, output->size);
|
2013-01-28 08:04:30 +01:00
|
|
|
|
|
|
|
doffset = (2 * sizeof(*segs));
|
|
|
|
|
|
|
|
/* Prepare code segment */
|
|
|
|
segs[0].type = PAYLOAD_SEGMENT_CODE;
|
2013-12-30 22:16:18 +01:00
|
|
|
segs[0].load_addr = loadaddress;
|
|
|
|
segs[0].mem_len = input->size;
|
|
|
|
segs[0].offset = doffset;
|
2013-01-28 08:04:30 +01:00
|
|
|
|
2014-02-21 10:01:06 +01:00
|
|
|
if (!compress(input->data, input->size, output->data + doffset, &len) &&
|
|
|
|
(unsigned int)len < input->size) {
|
|
|
|
segs[0].compression = algo;
|
|
|
|
segs[0].len = len;
|
|
|
|
} else {
|
|
|
|
WARN("Compression failed or would make the data bigger "
|
|
|
|
"- disabled.\n");
|
2013-01-28 08:04:30 +01:00
|
|
|
segs[0].compression = 0;
|
2013-12-30 22:16:18 +01:00
|
|
|
segs[0].len = input->size;
|
2013-01-29 08:22:11 +01:00
|
|
|
memcpy(output->data + doffset, input->data, input->size);
|
2013-01-28 08:04:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* prepare entry point segment */
|
|
|
|
segs[1].type = PAYLOAD_SEGMENT_ENTRY;
|
2013-12-30 22:16:18 +01:00
|
|
|
segs[1].load_addr = entrypoint;
|
|
|
|
output->size = doffset + segs[0].len;
|
|
|
|
xdr_segs(output, segs, 2);
|
2013-01-29 08:22:11 +01:00
|
|
|
return 0;
|
2013-01-28 08:04:30 +01:00
|
|
|
}
|
2013-02-05 00:39:13 +01:00
|
|
|
|
2015-05-08 06:12:28 +02:00
|
|
|
int parse_fv_to_payload(const struct buffer *input, struct buffer *output,
|
|
|
|
enum comp_algo algo)
|
2013-02-05 00:39:13 +01:00
|
|
|
{
|
|
|
|
comp_func_ptr compress;
|
2018-07-19 17:33:57 +02:00
|
|
|
struct cbfs_payload_segment segs[2] = { {0} };
|
2013-02-05 00:39:13 +01:00
|
|
|
int doffset, len = 0;
|
|
|
|
firmware_volume_header_t *fv;
|
|
|
|
ffs_file_header_t *fh;
|
|
|
|
common_section_header_t *cs;
|
|
|
|
dos_header_t *dh;
|
|
|
|
coff_header_t *ch;
|
|
|
|
int dh_offset;
|
|
|
|
|
2013-02-05 22:46:49 +01:00
|
|
|
uint32_t loadaddress = 0;
|
|
|
|
uint32_t entrypoint = 0;
|
2013-02-05 00:39:13 +01:00
|
|
|
|
|
|
|
compress = compression_function(algo);
|
|
|
|
if (!compress)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
DEBUG("start: parse_fv_to_payload\n");
|
|
|
|
|
|
|
|
fv = (firmware_volume_header_t *)input->data;
|
|
|
|
if (fv->signature != FV_SIGNATURE) {
|
|
|
|
INFO("Not a UEFI firmware volume.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fh = (ffs_file_header_t *)(input->data + fv->header_length);
|
2013-02-09 13:26:19 +01:00
|
|
|
while (fh->file_type == FILETYPE_PAD) {
|
|
|
|
unsigned long offset = (fh->size[2] << 16) | (fh->size[1] << 8) | fh->size[0];
|
2017-07-05 13:30:37 +02:00
|
|
|
DEBUG("skipping %lu bytes of FV padding\n", offset);
|
2015-03-06 00:38:03 +01:00
|
|
|
fh = (ffs_file_header_t *)(((uintptr_t)fh) + offset);
|
2013-02-09 13:26:19 +01:00
|
|
|
}
|
2013-02-05 00:39:13 +01:00
|
|
|
if (fh->file_type != FILETYPE_SEC) {
|
|
|
|
ERROR("Not a usable UEFI firmware volume.\n");
|
2013-02-05 22:46:49 +01:00
|
|
|
INFO("First file in first FV not a SEC core.\n");
|
2013-02-05 00:39:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cs = (common_section_header_t *)&fh[1];
|
2013-02-09 13:26:19 +01:00
|
|
|
while (cs->section_type == SECTION_RAW) {
|
|
|
|
unsigned long offset = (cs->size[2] << 16) | (cs->size[1] << 8) | cs->size[0];
|
2017-07-05 13:30:37 +02:00
|
|
|
DEBUG("skipping %lu bytes of section padding\n", offset);
|
2015-03-06 00:38:03 +01:00
|
|
|
cs = (common_section_header_t *)(((uintptr_t)cs) + offset);
|
2013-02-09 13:26:19 +01:00
|
|
|
}
|
2013-02-05 00:39:13 +01:00
|
|
|
if (cs->section_type != SECTION_PE32) {
|
|
|
|
ERROR("Not a usable UEFI firmware volume.\n");
|
2013-02-05 22:46:49 +01:00
|
|
|
INFO("Section type not PE32.\n");
|
2013-02-05 00:39:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dh = (dos_header_t *)&cs[1];
|
2013-02-05 22:46:49 +01:00
|
|
|
if (dh->signature != DOS_MAGIC) {
|
2013-02-05 00:39:13 +01:00
|
|
|
ERROR("Not a usable UEFI firmware volume.\n");
|
2013-02-05 22:46:49 +01:00
|
|
|
INFO("DOS header signature wrong.\n");
|
2013-02-05 00:39:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dh_offset = (unsigned long)dh - (unsigned long)input->data;
|
|
|
|
DEBUG("dos header offset = %x\n", dh_offset);
|
|
|
|
|
2015-03-06 00:38:03 +01:00
|
|
|
ch = (coff_header_t *)(((uintptr_t)dh)+dh->e_lfanew);
|
2013-02-05 00:39:13 +01:00
|
|
|
|
2013-02-05 22:46:49 +01:00
|
|
|
if (ch->machine == MACHINE_TYPE_X86) {
|
|
|
|
pe_opt_header_32_t *ph;
|
|
|
|
ph = (pe_opt_header_32_t *)&ch[1];
|
|
|
|
if (ph->signature != PE_HDR_32_MAGIC) {
|
|
|
|
WARN("PE header signature incorrect.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
DEBUG("image base %x\n", ph->image_addr);
|
|
|
|
DEBUG("entry point %x\n", ph->entry_point);
|
|
|
|
|
|
|
|
loadaddress = ph->image_addr - dh_offset;
|
|
|
|
entrypoint = ph->image_addr + ph->entry_point;
|
|
|
|
} else if (ch->machine == MACHINE_TYPE_X64) {
|
|
|
|
pe_opt_header_64_t *ph;
|
|
|
|
ph = (pe_opt_header_64_t *)&ch[1];
|
|
|
|
if (ph->signature != PE_HDR_64_MAGIC) {
|
|
|
|
WARN("PE header signature incorrect.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
DEBUG("image base %lx\n", (unsigned long)ph->image_addr);
|
|
|
|
DEBUG("entry point %x\n", ph->entry_point);
|
|
|
|
|
|
|
|
loadaddress = ph->image_addr - dh_offset;
|
|
|
|
entrypoint = ph->image_addr + ph->entry_point;
|
|
|
|
} else {
|
|
|
|
ERROR("Machine type not x86 or x64.\n");
|
2013-02-05 00:39:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:16:18 +01:00
|
|
|
if (buffer_create(output, (sizeof(segs) + input->size),
|
2013-02-05 00:39:13 +01:00
|
|
|
input->name) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(output->data, 0, output->size);
|
|
|
|
|
2013-12-30 22:16:18 +01:00
|
|
|
doffset = (sizeof(segs));
|
2013-02-05 00:39:13 +01:00
|
|
|
|
|
|
|
/* Prepare code segment */
|
|
|
|
segs[0].type = PAYLOAD_SEGMENT_CODE;
|
2013-12-30 22:16:18 +01:00
|
|
|
segs[0].load_addr = loadaddress;
|
|
|
|
segs[0].mem_len = input->size;
|
|
|
|
segs[0].offset = doffset;
|
2013-02-05 00:39:13 +01:00
|
|
|
|
2014-02-21 10:01:06 +01:00
|
|
|
if (!compress(input->data, input->size, output->data + doffset, &len) &&
|
|
|
|
(unsigned int)len < input->size) {
|
|
|
|
segs[0].compression = algo;
|
|
|
|
segs[0].len = len;
|
|
|
|
} else {
|
|
|
|
WARN("Compression failed or would make the data bigger "
|
|
|
|
"- disabled.\n");
|
2013-02-05 00:39:13 +01:00
|
|
|
segs[0].compression = 0;
|
2013-12-30 22:16:18 +01:00
|
|
|
segs[0].len = input->size;
|
2013-02-05 00:39:13 +01:00
|
|
|
memcpy(output->data + doffset, input->data, input->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prepare entry point segment */
|
|
|
|
segs[1].type = PAYLOAD_SEGMENT_ENTRY;
|
2013-12-30 22:16:18 +01:00
|
|
|
segs[1].load_addr = entrypoint;
|
|
|
|
output->size = doffset + segs[0].len;
|
|
|
|
xdr_segs(output, segs, 2);
|
2013-02-05 00:39:13 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
2018-04-26 09:35:13 +02:00
|
|
|
|
|
|
|
int parse_fit_to_payload(const struct buffer *input, struct buffer *output,
|
|
|
|
enum comp_algo algo)
|
|
|
|
{
|
|
|
|
struct fdt_header *fdt_h;
|
|
|
|
|
|
|
|
DEBUG("start: parse_fit_to_payload\n");
|
|
|
|
|
|
|
|
fdt_h = buffer_get(input);
|
2018-06-15 14:02:18 +02:00
|
|
|
if (read_be32(&fdt_h->magic) != FDT_HEADER_MAGIC) {
|
2018-04-26 09:35:13 +02:00
|
|
|
INFO("Not a FIT payload.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For developers:
|
|
|
|
* Compress the kernel binary you're sourcing in your its-script
|
|
|
|
* manually with LZ4 or LZMA and add 'compression = "lz4"' or "lzma" to
|
|
|
|
* the kernel@1 node in the its-script before assembling the image with
|
|
|
|
* mkimage.
|
|
|
|
*/
|
|
|
|
if (algo != CBFS_COMPRESS_NONE) {
|
|
|
|
ERROR("FIT images don't support whole-image compression,"
|
|
|
|
" compress the kernel component instead!\n")
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_create(output, buffer_size(input), input->name) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memcpy(buffer_get(output), buffer_get(input), buffer_size(input));
|
|
|
|
|
|
|
|
DEBUG("done\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|