Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 1 | /* |
Peter Stuge | 45ae92ff | 2009-04-14 19:48:32 +0000 | [diff] [blame] | 2 | * cbfs-mkpayload |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 5 | * 2009 coresystems GmbH |
| 6 | * written by Patrick Georgi <patrick.georgi@coresystems.de> |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 25 | |
Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 26 | #include "elfparsing.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 27 | #include "common.h" |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 28 | #include "cbfs.h" |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 29 | #include "fv.h" |
| 30 | #include "coff.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 31 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 32 | /* serialize the seg array into the buffer. |
| 33 | * The buffer is assumed to be large enough. |
| 34 | */ |
Ronald G. Minnich | 818f369 | 2014-02-04 08:29:35 -0800 | [diff] [blame] | 35 | void xdr_segs(struct buffer *output, |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 36 | struct cbfs_payload_segment *segs, int nseg) |
| 37 | { |
| 38 | struct buffer outheader; |
| 39 | int i; |
| 40 | |
| 41 | outheader.data = output->data; |
| 42 | outheader.size = 0; |
| 43 | |
| 44 | for(i = 0; i < nseg; i++){ |
| 45 | xdr_be.put32(&outheader, segs[i].type); |
| 46 | xdr_be.put32(&outheader, segs[i].compression); |
| 47 | xdr_be.put32(&outheader, segs[i].offset); |
| 48 | xdr_be.put64(&outheader, segs[i].load_addr); |
| 49 | xdr_be.put32(&outheader, segs[i].len); |
| 50 | xdr_be.put32(&outheader, segs[i].mem_len); |
| 51 | } |
| 52 | } |
Aaron Durbin | ca63027 | 2014-08-05 10:48:20 -0500 | [diff] [blame] | 53 | |
| 54 | void xdr_get_seg(struct cbfs_payload_segment *out, |
| 55 | struct cbfs_payload_segment *in) |
| 56 | { |
| 57 | struct buffer inheader; |
| 58 | |
| 59 | inheader.data = (void *)in; |
| 60 | inheader.size = sizeof(*in); |
| 61 | |
| 62 | out->type = xdr_be.get32(&inheader); |
| 63 | out->compression = xdr_be.get32(&inheader); |
| 64 | out->offset = xdr_be.get32(&inheader); |
| 65 | out->load_addr = xdr_be.get64(&inheader); |
| 66 | out->len = xdr_be.get32(&inheader); |
| 67 | out->mem_len = xdr_be.get32(&inheader); |
| 68 | } |
| 69 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 70 | int parse_elf_to_payload(const struct buffer *input, struct buffer *output, |
| 71 | comp_algo algo) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 72 | { |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 73 | Elf64_Phdr *phdr; |
| 74 | Elf64_Ehdr ehdr; |
| 75 | Elf64_Shdr *shdr; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 76 | char *header; |
| 77 | char *strtab; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 78 | int headers; |
| 79 | int segments = 1; |
| 80 | int isize = 0, osize = 0; |
| 81 | int doffset = 0; |
Patrick Georgi | 96990a2 | 2014-09-29 10:08:35 +0200 | [diff] [blame] | 82 | struct cbfs_payload_segment *segs = NULL; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 83 | int i; |
Patrick Georgi | 96990a2 | 2014-09-29 10:08:35 +0200 | [diff] [blame] | 84 | int ret = 0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 85 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 86 | comp_func_ptr compress = compression_function(algo); |
| 87 | if (!compress) |
| 88 | return -1; |
| 89 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 90 | if (elf_headers(input, &ehdr, &phdr, &shdr) < 0) |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 91 | return -1; |
| 92 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 93 | DEBUG("start: parse_elf_to_payload\n"); |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 94 | headers = ehdr.e_phnum; |
| 95 | header = input->data; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 96 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 97 | strtab = &header[shdr[ehdr.e_shstrndx].sh_offset]; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 98 | |
| 99 | /* Count the number of headers - look for the .notes.pinfo |
| 100 | * section */ |
| 101 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 102 | for (i = 0; i < ehdr.e_shnum; i++) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 103 | char *name; |
| 104 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 105 | if (i == ehdr.e_shstrndx) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 106 | continue; |
| 107 | |
| 108 | if (shdr[i].sh_size == 0) |
| 109 | continue; |
| 110 | |
| 111 | name = (char *)(strtab + shdr[i].sh_name); |
| 112 | |
Ronald G. Minnich | 5a1af7b | 2009-09-17 15:35:08 +0000 | [diff] [blame] | 113 | if (!strcmp(name, ".note.pinfo")) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 114 | segments++; |
Ronald G. Minnich | 5a1af7b | 2009-09-17 15:35:08 +0000 | [diff] [blame] | 115 | isize += (unsigned int)shdr[i].sh_size; |
| 116 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* Now, regular headers - we only care about PT_LOAD headers, |
| 120 | * because thats what we're actually going to load |
| 121 | */ |
| 122 | |
| 123 | for (i = 0; i < headers; i++) { |
| 124 | if (phdr[i].p_type != PT_LOAD) |
| 125 | continue; |
| 126 | |
| 127 | /* Empty segments are never interesting */ |
| 128 | if (phdr[i].p_memsz == 0) |
| 129 | continue; |
| 130 | |
| 131 | isize += phdr[i].p_filesz; |
| 132 | |
| 133 | segments++; |
| 134 | } |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 135 | /* allocate the segment header array */ |
| 136 | segs = calloc(segments, sizeof(*segs)); |
Patrick Georgi | 96990a2 | 2014-09-29 10:08:35 +0200 | [diff] [blame] | 137 | if (segs == NULL) { |
| 138 | ret = -1; |
| 139 | goto out; |
| 140 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 141 | /* Allocate a block of memory to store the data in */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 142 | if (buffer_create(output, (segments * sizeof(*segs)) + isize, |
Patrick Georgi | 96990a2 | 2014-09-29 10:08:35 +0200 | [diff] [blame] | 143 | input->name) != 0) { |
| 144 | ret = -1; |
| 145 | goto out; |
| 146 | } |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 147 | memset(output->data, 0, output->size); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 148 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 149 | doffset = (segments * sizeof(*segs)); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 150 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 151 | /* set up for output marshaling. This is a bit |
| 152 | * tricky as we are marshaling the headers at the front, |
| 153 | * and the data starting after the headers. We need to convert |
| 154 | * the headers to the right format but the data |
| 155 | * passes through unchanged. Unlike most XDR code, |
| 156 | * we are doing these two concurrently. The doffset is |
| 157 | * used to compute the address for the raw data, and the |
| 158 | * outheader is used to marshal the headers. To make it simpler |
| 159 | * for The Reader, we set up the headers in a separate array, |
| 160 | * then marshal them all at once to the output. |
| 161 | */ |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 162 | segments = 0; |
| 163 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 164 | for (i = 0; i < ehdr.e_shnum; i++) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 165 | char *name; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 166 | if (i == ehdr.e_shstrndx) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 167 | continue; |
| 168 | |
| 169 | if (shdr[i].sh_size == 0) |
| 170 | continue; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 171 | name = (char *)(strtab + shdr[i].sh_name); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 172 | if (!strcmp(name, ".note.pinfo")) { |
| 173 | segs[segments].type = PAYLOAD_SEGMENT_PARAMS; |
| 174 | segs[segments].load_addr = 0; |
| 175 | segs[segments].len = (unsigned int)shdr[i].sh_size; |
| 176 | segs[segments].offset = doffset; |
| 177 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 178 | memcpy((unsigned long *)(output->data + doffset), |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 179 | &header[shdr[i].sh_offset], shdr[i].sh_size); |
| 180 | |
| 181 | doffset += segs[segments].len; |
| 182 | osize += segs[segments].len; |
| 183 | |
| 184 | segments++; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | for (i = 0; i < headers; i++) { |
| 189 | if (phdr[i].p_type != PT_LOAD) |
| 190 | continue; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 191 | if (phdr[i].p_memsz == 0) |
| 192 | continue; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 193 | if (phdr[i].p_filesz == 0) { |
| 194 | segs[segments].type = PAYLOAD_SEGMENT_BSS; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 195 | segs[segments].load_addr = phdr[i].p_paddr; |
| 196 | segs[segments].mem_len = phdr[i].p_memsz; |
| 197 | segs[segments].offset = doffset; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 198 | |
| 199 | segments++; |
| 200 | continue; |
| 201 | } |
| 202 | |
Stefan Reinauer | 51f6a20 | 2012-01-11 12:40:14 -0800 | [diff] [blame] | 203 | if (phdr[i].p_flags & PF_X) |
| 204 | segs[segments].type = PAYLOAD_SEGMENT_CODE; |
| 205 | else |
| 206 | segs[segments].type = PAYLOAD_SEGMENT_DATA; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 207 | segs[segments].load_addr = phdr[i].p_paddr; |
| 208 | segs[segments].mem_len = phdr[i].p_memsz; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 209 | segs[segments].offset = doffset; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 210 | |
Gabe Black | 845aa14 | 2014-02-21 01:01:06 -0800 | [diff] [blame] | 211 | /* If the compression failed or made the section is larger, |
| 212 | use the original stuff */ |
| 213 | |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 214 | int len; |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 215 | if (compress((char *)&header[phdr[i].p_offset], |
Gabe Black | 845aa14 | 2014-02-21 01:01:06 -0800 | [diff] [blame] | 216 | phdr[i].p_filesz, output->data + doffset, &len) || |
| 217 | (unsigned int)len > phdr[i].p_filesz) { |
| 218 | WARN("Compression failed or would make the data bigger " |
| 219 | "- disabled.\n"); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 220 | segs[segments].compression = 0; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 221 | segs[segments].len = phdr[i].p_filesz; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 222 | memcpy(output->data + doffset, |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 223 | &header[phdr[i].p_offset], phdr[i].p_filesz); |
Gabe Black | 845aa14 | 2014-02-21 01:01:06 -0800 | [diff] [blame] | 224 | } else { |
| 225 | segs[segments].compression = algo; |
| 226 | segs[segments].len = len; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 229 | doffset += segs[segments].len; |
| 230 | osize += segs[segments].len; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 231 | |
| 232 | segments++; |
| 233 | } |
| 234 | |
| 235 | segs[segments].type = PAYLOAD_SEGMENT_ENTRY; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 236 | segs[segments++].load_addr = ehdr.e_entry; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 237 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 238 | output->size = (segments * sizeof(*segs)) + osize; |
| 239 | xdr_segs(output, segs, segments); |
Patrick Georgi | 96990a2 | 2014-09-29 10:08:35 +0200 | [diff] [blame] | 240 | |
| 241 | out: |
| 242 | if (segs) free(segs); |
| 243 | if (shdr) free(shdr); |
| 244 | if (phdr) free(phdr); |
| 245 | return ret; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 246 | } |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 247 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 248 | int parse_flat_binary_to_payload(const struct buffer *input, |
| 249 | struct buffer *output, |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 250 | uint32_t loadaddress, |
| 251 | uint32_t entrypoint, |
| 252 | comp_algo algo) |
| 253 | { |
| 254 | comp_func_ptr compress; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 255 | struct cbfs_payload_segment segs[2]; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 256 | int doffset, len = 0; |
| 257 | |
| 258 | compress = compression_function(algo); |
| 259 | if (!compress) |
| 260 | return -1; |
| 261 | |
| 262 | DEBUG("start: parse_flat_binary_to_payload\n"); |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 263 | if (buffer_create(output, (sizeof(segs) + input->size), |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 264 | input->name) != 0) |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 265 | return -1; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 266 | memset(output->data, 0, output->size); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 267 | |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 268 | doffset = (2 * sizeof(*segs)); |
| 269 | |
| 270 | /* Prepare code segment */ |
| 271 | segs[0].type = PAYLOAD_SEGMENT_CODE; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 272 | segs[0].load_addr = loadaddress; |
| 273 | segs[0].mem_len = input->size; |
| 274 | segs[0].offset = doffset; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 275 | |
Gabe Black | 845aa14 | 2014-02-21 01:01:06 -0800 | [diff] [blame] | 276 | if (!compress(input->data, input->size, output->data + doffset, &len) && |
| 277 | (unsigned int)len < input->size) { |
| 278 | segs[0].compression = algo; |
| 279 | segs[0].len = len; |
| 280 | } else { |
| 281 | WARN("Compression failed or would make the data bigger " |
| 282 | "- disabled.\n"); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 283 | segs[0].compression = 0; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 284 | segs[0].len = input->size; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 285 | memcpy(output->data + doffset, input->data, input->size); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /* prepare entry point segment */ |
| 289 | segs[1].type = PAYLOAD_SEGMENT_ENTRY; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 290 | segs[1].load_addr = entrypoint; |
| 291 | output->size = doffset + segs[0].len; |
| 292 | xdr_segs(output, segs, 2); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 293 | return 0; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 294 | } |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 295 | |
| 296 | int parse_fv_to_payload(const struct buffer *input, |
| 297 | struct buffer *output, comp_algo algo) |
| 298 | { |
| 299 | comp_func_ptr compress; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 300 | struct cbfs_payload_segment segs[2]; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 301 | int doffset, len = 0; |
| 302 | firmware_volume_header_t *fv; |
| 303 | ffs_file_header_t *fh; |
| 304 | common_section_header_t *cs; |
| 305 | dos_header_t *dh; |
| 306 | coff_header_t *ch; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 307 | int dh_offset; |
| 308 | |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 309 | uint32_t loadaddress = 0; |
| 310 | uint32_t entrypoint = 0; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 311 | |
| 312 | compress = compression_function(algo); |
| 313 | if (!compress) |
| 314 | return -1; |
| 315 | |
| 316 | DEBUG("start: parse_fv_to_payload\n"); |
| 317 | |
| 318 | fv = (firmware_volume_header_t *)input->data; |
| 319 | if (fv->signature != FV_SIGNATURE) { |
| 320 | INFO("Not a UEFI firmware volume.\n"); |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | fh = (ffs_file_header_t *)(input->data + fv->header_length); |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 325 | while (fh->file_type == FILETYPE_PAD) { |
| 326 | unsigned long offset = (fh->size[2] << 16) | (fh->size[1] << 8) | fh->size[0]; |
Hung-Te Lin | 7b654a9 | 2013-02-18 18:35:00 +0800 | [diff] [blame] | 327 | ERROR("skipping %lu bytes of FV padding\n", offset); |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 328 | fh = (ffs_file_header_t *)(((uintptr_t)fh) + offset); |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 329 | } |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 330 | if (fh->file_type != FILETYPE_SEC) { |
| 331 | ERROR("Not a usable UEFI firmware volume.\n"); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 332 | INFO("First file in first FV not a SEC core.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 333 | return -1; |
| 334 | } |
| 335 | |
| 336 | cs = (common_section_header_t *)&fh[1]; |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 337 | while (cs->section_type == SECTION_RAW) { |
| 338 | unsigned long offset = (cs->size[2] << 16) | (cs->size[1] << 8) | cs->size[0]; |
Hung-Te Lin | 7b654a9 | 2013-02-18 18:35:00 +0800 | [diff] [blame] | 339 | ERROR("skipping %lu bytes of section padding\n", offset); |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 340 | cs = (common_section_header_t *)(((uintptr_t)cs) + offset); |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 341 | } |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 342 | if (cs->section_type != SECTION_PE32) { |
| 343 | ERROR("Not a usable UEFI firmware volume.\n"); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 344 | INFO("Section type not PE32.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | dh = (dos_header_t *)&cs[1]; |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 349 | if (dh->signature != DOS_MAGIC) { |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 350 | ERROR("Not a usable UEFI firmware volume.\n"); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 351 | INFO("DOS header signature wrong.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 352 | return -1; |
| 353 | } |
| 354 | |
| 355 | dh_offset = (unsigned long)dh - (unsigned long)input->data; |
| 356 | DEBUG("dos header offset = %x\n", dh_offset); |
| 357 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 358 | ch = (coff_header_t *)(((uintptr_t)dh)+dh->e_lfanew); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 359 | |
| 360 | if (ch->machine == MACHINE_TYPE_X86) { |
| 361 | pe_opt_header_32_t *ph; |
| 362 | ph = (pe_opt_header_32_t *)&ch[1]; |
| 363 | if (ph->signature != PE_HDR_32_MAGIC) { |
| 364 | WARN("PE header signature incorrect.\n"); |
| 365 | return -1; |
| 366 | } |
| 367 | DEBUG("image base %x\n", ph->image_addr); |
| 368 | DEBUG("entry point %x\n", ph->entry_point); |
| 369 | |
| 370 | loadaddress = ph->image_addr - dh_offset; |
| 371 | entrypoint = ph->image_addr + ph->entry_point; |
| 372 | } else if (ch->machine == MACHINE_TYPE_X64) { |
| 373 | pe_opt_header_64_t *ph; |
| 374 | ph = (pe_opt_header_64_t *)&ch[1]; |
| 375 | if (ph->signature != PE_HDR_64_MAGIC) { |
| 376 | WARN("PE header signature incorrect.\n"); |
| 377 | return -1; |
| 378 | } |
| 379 | DEBUG("image base %lx\n", (unsigned long)ph->image_addr); |
| 380 | DEBUG("entry point %x\n", ph->entry_point); |
| 381 | |
| 382 | loadaddress = ph->image_addr - dh_offset; |
| 383 | entrypoint = ph->image_addr + ph->entry_point; |
| 384 | } else { |
| 385 | ERROR("Machine type not x86 or x64.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 386 | return -1; |
| 387 | } |
| 388 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 389 | if (buffer_create(output, (sizeof(segs) + input->size), |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 390 | input->name) != 0) |
| 391 | return -1; |
| 392 | |
| 393 | memset(output->data, 0, output->size); |
| 394 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 395 | doffset = (sizeof(segs)); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 396 | |
| 397 | /* Prepare code segment */ |
| 398 | segs[0].type = PAYLOAD_SEGMENT_CODE; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 399 | segs[0].load_addr = loadaddress; |
| 400 | segs[0].mem_len = input->size; |
| 401 | segs[0].offset = doffset; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 402 | |
Gabe Black | 845aa14 | 2014-02-21 01:01:06 -0800 | [diff] [blame] | 403 | if (!compress(input->data, input->size, output->data + doffset, &len) && |
| 404 | (unsigned int)len < input->size) { |
| 405 | segs[0].compression = algo; |
| 406 | segs[0].len = len; |
| 407 | } else { |
| 408 | WARN("Compression failed or would make the data bigger " |
| 409 | "- disabled.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 410 | segs[0].compression = 0; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 411 | segs[0].len = input->size; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 412 | memcpy(output->data + doffset, input->data, input->size); |
| 413 | } |
| 414 | |
| 415 | /* prepare entry point segment */ |
| 416 | segs[1].type = PAYLOAD_SEGMENT_ENTRY; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 417 | segs[1].load_addr = entrypoint; |
| 418 | output->size = doffset + segs[0].len; |
| 419 | xdr_segs(output, segs, 2); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 420 | return 0; |
| 421 | |
| 422 | } |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 423 | |