Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
Werner Zeh | a7835c4 | 2018-06-15 14:02:18 +0200 | [diff] [blame] | 6 | #include <commonlib/endian.h> |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 7 | |
Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 8 | #include "elfparsing.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 9 | #include "common.h" |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 10 | #include "cbfs.h" |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 11 | #include "fv.h" |
| 12 | #include "coff.h" |
Patrick Rudolph | 7ee05ed | 2018-04-26 09:35:13 +0200 | [diff] [blame] | 13 | #include "fdt.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 14 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 15 | /* serialize the seg array into the buffer. |
| 16 | * The buffer is assumed to be large enough. |
| 17 | */ |
Ronald G. Minnich | 818f369 | 2014-02-04 08:29:35 -0800 | [diff] [blame] | 18 | void xdr_segs(struct buffer *output, |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 19 | 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 Durbin | ca63027 | 2014-08-05 10:48:20 -0500 | [diff] [blame] | 36 | |
| 37 | void 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 Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 53 | int parse_elf_to_payload(const struct buffer *input, struct buffer *output, |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 54 | enum cbfs_compression algo) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 55 | { |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 56 | Elf64_Phdr *phdr; |
| 57 | Elf64_Ehdr ehdr; |
| 58 | Elf64_Shdr *shdr; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 59 | char *header; |
| 60 | char *strtab; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 61 | int headers; |
| 62 | int segments = 1; |
| 63 | int isize = 0, osize = 0; |
| 64 | int doffset = 0; |
Patrick Georgi | 96990a2 | 2014-09-29 10:08:35 +0200 | [diff] [blame] | 65 | struct cbfs_payload_segment *segs = NULL; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 66 | int i; |
Patrick Georgi | 96990a2 | 2014-09-29 10:08:35 +0200 | [diff] [blame] | 67 | int ret = 0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 68 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 69 | comp_func_ptr compress = compression_function(algo); |
| 70 | if (!compress) |
| 71 | return -1; |
| 72 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 73 | if (elf_headers(input, &ehdr, &phdr, &shdr) < 0) |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 74 | return -1; |
| 75 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 76 | DEBUG("start: parse_elf_to_payload\n"); |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 77 | headers = ehdr.e_phnum; |
| 78 | header = input->data; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 79 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 80 | strtab = &header[shdr[ehdr.e_shstrndx].sh_offset]; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 81 | |
| 82 | /* Count the number of headers - look for the .notes.pinfo |
| 83 | * section */ |
| 84 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 85 | for (i = 0; i < ehdr.e_shnum; i++) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 86 | char *name; |
| 87 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 88 | if (i == ehdr.e_shstrndx) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 89 | continue; |
| 90 | |
| 91 | if (shdr[i].sh_size == 0) |
| 92 | continue; |
| 93 | |
| 94 | name = (char *)(strtab + shdr[i].sh_name); |
| 95 | |
Ronald G. Minnich | 5a1af7b | 2009-09-17 15:35:08 +0000 | [diff] [blame] | 96 | if (!strcmp(name, ".note.pinfo")) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 97 | segments++; |
Ronald G. Minnich | 5a1af7b | 2009-09-17 15:35:08 +0000 | [diff] [blame] | 98 | isize += (unsigned int)shdr[i].sh_size; |
| 99 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* Now, regular headers - we only care about PT_LOAD headers, |
Patrick Georgi | 01cfecc | 2020-01-29 13:31:16 +0100 | [diff] [blame] | 103 | * because that's what we're actually going to load |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 104 | */ |
| 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 Kitching | a302e7f | 2018-07-19 07:36:38 +0800 | [diff] [blame] | 118 | /* Allocate and initialize the segment header array */ |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 119 | segs = calloc(segments, sizeof(*segs)); |
Patrick Georgi | 96990a2 | 2014-09-29 10:08:35 +0200 | [diff] [blame] | 120 | if (segs == NULL) { |
| 121 | ret = -1; |
| 122 | goto out; |
| 123 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 124 | /* Allocate a block of memory to store the data in */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 125 | if (buffer_create(output, (segments * sizeof(*segs)) + isize, |
Patrick Georgi | 96990a2 | 2014-09-29 10:08:35 +0200 | [diff] [blame] | 126 | input->name) != 0) { |
| 127 | ret = -1; |
| 128 | goto out; |
| 129 | } |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 130 | memset(output->data, 0, output->size); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 131 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 132 | doffset = (segments * sizeof(*segs)); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 133 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 134 | /* 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. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 145 | segments = 0; |
| 146 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 147 | for (i = 0; i < ehdr.e_shnum; i++) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 148 | char *name; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 149 | if (i == ehdr.e_shstrndx) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 150 | continue; |
| 151 | |
| 152 | if (shdr[i].sh_size == 0) |
| 153 | continue; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 154 | name = (char *)(strtab + shdr[i].sh_name); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 155 | 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 Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 161 | memcpy((unsigned long *)(output->data + doffset), |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 162 | &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. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 174 | if (phdr[i].p_memsz == 0) |
| 175 | continue; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 176 | if (phdr[i].p_filesz == 0) { |
| 177 | segs[segments].type = PAYLOAD_SEGMENT_BSS; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 178 | segs[segments].load_addr = phdr[i].p_paddr; |
| 179 | segs[segments].mem_len = phdr[i].p_memsz; |
| 180 | segs[segments].offset = doffset; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 181 | |
| 182 | segments++; |
| 183 | continue; |
| 184 | } |
| 185 | |
Stefan Reinauer | 51f6a20 | 2012-01-11 12:40:14 -0800 | [diff] [blame] | 186 | 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. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 190 | segs[segments].load_addr = phdr[i].p_paddr; |
| 191 | segs[segments].mem_len = phdr[i].p_memsz; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 192 | segs[segments].offset = doffset; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 193 | |
Gabe Black | 845aa14 | 2014-02-21 01:01:06 -0800 | [diff] [blame] | 194 | /* If the compression failed or made the section is larger, |
| 195 | use the original stuff */ |
| 196 | |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 197 | int len; |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 198 | if (compress((char *)&header[phdr[i].p_offset], |
Gabe Black | 845aa14 | 2014-02-21 01:01:06 -0800 | [diff] [blame] | 199 | 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. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 203 | segs[segments].compression = 0; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 204 | segs[segments].len = phdr[i].p_filesz; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 205 | memcpy(output->data + doffset, |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 206 | &header[phdr[i].p_offset], phdr[i].p_filesz); |
Gabe Black | 845aa14 | 2014-02-21 01:01:06 -0800 | [diff] [blame] | 207 | } else { |
| 208 | segs[segments].compression = algo; |
| 209 | segs[segments].len = len; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 212 | doffset += segs[segments].len; |
| 213 | osize += segs[segments].len; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 214 | |
| 215 | segments++; |
| 216 | } |
| 217 | |
| 218 | segs[segments].type = PAYLOAD_SEGMENT_ENTRY; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 219 | segs[segments++].load_addr = ehdr.e_entry; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 220 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 221 | output->size = (segments * sizeof(*segs)) + osize; |
| 222 | xdr_segs(output, segs, segments); |
Patrick Georgi | 96990a2 | 2014-09-29 10:08:35 +0200 | [diff] [blame] | 223 | |
| 224 | out: |
| 225 | if (segs) free(segs); |
| 226 | if (shdr) free(shdr); |
| 227 | if (phdr) free(phdr); |
| 228 | return ret; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 229 | } |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 230 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 231 | int parse_flat_binary_to_payload(const struct buffer *input, |
| 232 | struct buffer *output, |
Patrick Georgi | 7691e96 | 2024-02-17 21:09:13 +0100 | [diff] [blame] | 233 | uint64_t loadaddress, |
| 234 | uint64_t entrypoint, |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 235 | enum cbfs_compression algo) |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 236 | { |
| 237 | comp_func_ptr compress; |
Patrick Georgi | 71955a5 | 2018-07-19 17:33:57 +0200 | [diff] [blame] | 238 | struct cbfs_payload_segment segs[2] = { {0} }; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 239 | 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. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 246 | if (buffer_create(output, (sizeof(segs) + input->size), |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 247 | input->name) != 0) |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 248 | return -1; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 249 | memset(output->data, 0, output->size); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 250 | |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 251 | doffset = (2 * sizeof(*segs)); |
| 252 | |
| 253 | /* Prepare code segment */ |
| 254 | segs[0].type = PAYLOAD_SEGMENT_CODE; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 255 | segs[0].load_addr = loadaddress; |
| 256 | segs[0].mem_len = input->size; |
| 257 | segs[0].offset = doffset; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 258 | |
Gabe Black | 845aa14 | 2014-02-21 01:01:06 -0800 | [diff] [blame] | 259 | 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 Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 266 | segs[0].compression = 0; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 267 | segs[0].len = input->size; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 268 | memcpy(output->data + doffset, input->data, input->size); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* prepare entry point segment */ |
| 272 | segs[1].type = PAYLOAD_SEGMENT_ENTRY; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 273 | segs[1].load_addr = entrypoint; |
| 274 | output->size = doffset + segs[0].len; |
| 275 | xdr_segs(output, segs, 2); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 276 | return 0; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 277 | } |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 278 | |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 279 | int parse_fv_to_payload(const struct buffer *input, struct buffer *output, |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 280 | enum cbfs_compression algo) |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 281 | { |
| 282 | comp_func_ptr compress; |
Patrick Georgi | 71955a5 | 2018-07-19 17:33:57 +0200 | [diff] [blame] | 283 | struct cbfs_payload_segment segs[2] = { {0} }; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 284 | int doffset, len = 0; |
| 285 | firmware_volume_header_t *fv; |
Dun Tan | 4a35940 | 2021-09-01 10:29:41 +0800 | [diff] [blame] | 286 | firmware_volume_ext_header_t *fvh_ext; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 287 | ffs_file_header_t *fh; |
| 288 | common_section_header_t *cs; |
| 289 | dos_header_t *dh; |
| 290 | coff_header_t *ch; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 291 | int dh_offset; |
| 292 | |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 293 | uint32_t loadaddress = 0; |
| 294 | uint32_t entrypoint = 0; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 295 | |
| 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 Tan | 4a35940 | 2021-09-01 10:29:41 +0800 | [diff] [blame] | 309 | 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 Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 315 | while (fh->file_type == FILETYPE_PAD) { |
| 316 | unsigned long offset = (fh->size[2] << 16) | (fh->size[1] << 8) | fh->size[0]; |
Patrick Georgi | 50bda05 | 2017-07-05 13:30:37 +0200 | [diff] [blame] | 317 | DEBUG("skipping %lu bytes of FV padding\n", offset); |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 318 | fh = (ffs_file_header_t *)(((uintptr_t)fh) + offset); |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 319 | } |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 320 | if (fh->file_type != FILETYPE_SEC) { |
| 321 | ERROR("Not a usable UEFI firmware volume.\n"); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 322 | INFO("First file in first FV not a SEC core.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | cs = (common_section_header_t *)&fh[1]; |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 327 | while (cs->section_type == SECTION_RAW) { |
| 328 | unsigned long offset = (cs->size[2] << 16) | (cs->size[1] << 8) | cs->size[0]; |
Patrick Georgi | 50bda05 | 2017-07-05 13:30:37 +0200 | [diff] [blame] | 329 | DEBUG("skipping %lu bytes of section padding\n", offset); |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 330 | cs = (common_section_header_t *)(((uintptr_t)cs) + offset); |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 331 | } |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 332 | if (cs->section_type != SECTION_PE32) { |
| 333 | ERROR("Not a usable UEFI firmware volume.\n"); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 334 | INFO("Section type not PE32.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | dh = (dos_header_t *)&cs[1]; |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 339 | if (dh->signature != DOS_MAGIC) { |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 340 | ERROR("Not a usable UEFI firmware volume.\n"); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 341 | INFO("DOS header signature wrong.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 342 | 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 Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 348 | ch = (coff_header_t *)(((uintptr_t)dh)+dh->e_lfanew); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 349 | |
| 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 Chen | 5020fe3 | 2021-07-15 17:47:47 +0800 | [diff] [blame] | 362 | } else if (ch->machine == MACHINE_TYPE_X64 || ch->machine == MACHINE_TYPE_ARM64) { |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 363 | 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 Chen | 5020fe3 | 2021-07-15 17:47:47 +0800 | [diff] [blame] | 375 | ERROR("Machine type not x86, x64, or arm64.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 376 | return -1; |
| 377 | } |
| 378 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 379 | if (buffer_create(output, (sizeof(segs) + input->size), |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 380 | input->name) != 0) |
| 381 | return -1; |
| 382 | |
| 383 | memset(output->data, 0, output->size); |
| 384 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 385 | doffset = (sizeof(segs)); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 386 | |
| 387 | /* Prepare code segment */ |
| 388 | segs[0].type = PAYLOAD_SEGMENT_CODE; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 389 | segs[0].load_addr = loadaddress; |
| 390 | segs[0].mem_len = input->size; |
| 391 | segs[0].offset = doffset; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 392 | |
Gabe Black | 845aa14 | 2014-02-21 01:01:06 -0800 | [diff] [blame] | 393 | 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 Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 400 | segs[0].compression = 0; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 401 | segs[0].len = input->size; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 402 | memcpy(output->data + doffset, input->data, input->size); |
| 403 | } |
| 404 | |
| 405 | /* prepare entry point segment */ |
| 406 | segs[1].type = PAYLOAD_SEGMENT_ENTRY; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 407 | segs[1].load_addr = entrypoint; |
| 408 | output->size = doffset + segs[0].len; |
| 409 | xdr_segs(output, segs, 2); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 410 | return 0; |
| 411 | |
| 412 | } |
Patrick Rudolph | 7ee05ed | 2018-04-26 09:35:13 +0200 | [diff] [blame] | 413 | |
| 414 | int parse_fit_to_payload(const struct buffer *input, struct buffer *output, |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 415 | enum cbfs_compression algo) |
Patrick Rudolph | 7ee05ed | 2018-04-26 09:35:13 +0200 | [diff] [blame] | 416 | { |
| 417 | struct fdt_header *fdt_h; |
| 418 | |
| 419 | DEBUG("start: parse_fit_to_payload\n"); |
| 420 | |
| 421 | fdt_h = buffer_get(input); |
Werner Zeh | a7835c4 | 2018-06-15 14:02:18 +0200 | [diff] [blame] | 422 | if (read_be32(&fdt_h->magic) != FDT_HEADER_MAGIC) { |
Patrick Rudolph | 7ee05ed | 2018-04-26 09:35:13 +0200 | [diff] [blame] | 423 | 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 Reitberger | 9049dfd | 2022-10-12 10:47:39 -0400 | [diff] [blame] | 436 | " compress the kernel component instead!\n"); |
Patrick Rudolph | 7ee05ed | 2018-04-26 09:35:13 +0200 | [diff] [blame] | 437 | 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 | } |