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 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 70 | int parse_elf_to_payload(const struct buffer *input, |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 71 | struct buffer *output, uint32_t arch, 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; |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame] | 82 | struct cbfs_payload_segment *segs; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 83 | int i; |
| 84 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 85 | comp_func_ptr compress = compression_function(algo); |
| 86 | if (!compress) |
| 87 | return -1; |
| 88 | |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 89 | if (elf_headers(input, arch, &ehdr, &phdr, &shdr) < 0) |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 90 | return -1; |
| 91 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 92 | DEBUG("start: parse_elf_to_payload\n"); |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 93 | headers = ehdr.e_phnum; |
| 94 | header = input->data; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 95 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 96 | strtab = &header[shdr[ehdr.e_shstrndx].sh_offset]; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 97 | |
| 98 | /* Count the number of headers - look for the .notes.pinfo |
| 99 | * section */ |
| 100 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 101 | for (i = 0; i < ehdr.e_shnum; i++) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 102 | char *name; |
| 103 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 104 | if (i == ehdr.e_shstrndx) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 105 | continue; |
| 106 | |
| 107 | if (shdr[i].sh_size == 0) |
| 108 | continue; |
| 109 | |
| 110 | name = (char *)(strtab + shdr[i].sh_name); |
| 111 | |
Ronald G. Minnich | 5a1af7b | 2009-09-17 15:35:08 +0000 | [diff] [blame] | 112 | if (!strcmp(name, ".note.pinfo")) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 113 | segments++; |
Ronald G. Minnich | 5a1af7b | 2009-09-17 15:35:08 +0000 | [diff] [blame] | 114 | isize += (unsigned int)shdr[i].sh_size; |
| 115 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* Now, regular headers - we only care about PT_LOAD headers, |
| 119 | * because thats what we're actually going to load |
| 120 | */ |
| 121 | |
| 122 | for (i = 0; i < headers; i++) { |
| 123 | if (phdr[i].p_type != PT_LOAD) |
| 124 | continue; |
| 125 | |
| 126 | /* Empty segments are never interesting */ |
| 127 | if (phdr[i].p_memsz == 0) |
| 128 | continue; |
| 129 | |
| 130 | isize += phdr[i].p_filesz; |
| 131 | |
| 132 | segments++; |
| 133 | } |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 134 | /* allocate the segment header array */ |
| 135 | segs = calloc(segments, sizeof(*segs)); |
| 136 | if (segs == NULL) |
| 137 | return -1; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 138 | /* Allocate a block of memory to store the data in */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 139 | if (buffer_create(output, (segments * sizeof(*segs)) + isize, |
| 140 | input->name) != 0) |
| 141 | return -1; |
| 142 | memset(output->data, 0, output->size); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 143 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 144 | doffset = (segments * sizeof(*segs)); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 145 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 146 | /* set up for output marshaling. This is a bit |
| 147 | * tricky as we are marshaling the headers at the front, |
| 148 | * and the data starting after the headers. We need to convert |
| 149 | * the headers to the right format but the data |
| 150 | * passes through unchanged. Unlike most XDR code, |
| 151 | * we are doing these two concurrently. The doffset is |
| 152 | * used to compute the address for the raw data, and the |
| 153 | * outheader is used to marshal the headers. To make it simpler |
| 154 | * for The Reader, we set up the headers in a separate array, |
| 155 | * then marshal them all at once to the output. |
| 156 | */ |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 157 | segments = 0; |
| 158 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 159 | for (i = 0; i < ehdr.e_shnum; i++) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 160 | char *name; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 161 | if (i == ehdr.e_shstrndx) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 162 | continue; |
| 163 | |
| 164 | if (shdr[i].sh_size == 0) |
| 165 | continue; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 166 | name = (char *)(strtab + shdr[i].sh_name); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 167 | if (!strcmp(name, ".note.pinfo")) { |
| 168 | segs[segments].type = PAYLOAD_SEGMENT_PARAMS; |
| 169 | segs[segments].load_addr = 0; |
| 170 | segs[segments].len = (unsigned int)shdr[i].sh_size; |
| 171 | segs[segments].offset = doffset; |
| 172 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 173 | memcpy((unsigned long *)(output->data + doffset), |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 174 | &header[shdr[i].sh_offset], shdr[i].sh_size); |
| 175 | |
| 176 | doffset += segs[segments].len; |
| 177 | osize += segs[segments].len; |
| 178 | |
| 179 | segments++; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | for (i = 0; i < headers; i++) { |
| 184 | if (phdr[i].p_type != PT_LOAD) |
| 185 | continue; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 186 | if (phdr[i].p_memsz == 0) |
| 187 | continue; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 188 | if (phdr[i].p_filesz == 0) { |
| 189 | segs[segments].type = PAYLOAD_SEGMENT_BSS; |
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; |
| 192 | segs[segments].offset = doffset; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 193 | |
| 194 | segments++; |
| 195 | continue; |
| 196 | } |
| 197 | |
Stefan Reinauer | 51f6a20 | 2012-01-11 12:40:14 -0800 | [diff] [blame] | 198 | if (phdr[i].p_flags & PF_X) |
| 199 | segs[segments].type = PAYLOAD_SEGMENT_CODE; |
| 200 | else |
| 201 | segs[segments].type = PAYLOAD_SEGMENT_DATA; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 202 | segs[segments].load_addr = phdr[i].p_paddr; |
| 203 | segs[segments].mem_len = phdr[i].p_memsz; |
| 204 | segs[segments].compression = algo; |
| 205 | segs[segments].offset = doffset; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 206 | |
| 207 | int len; |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 208 | if (compress((char *)&header[phdr[i].p_offset], |
| 209 | phdr[i].p_filesz, output->data + doffset, &len)) { |
| 210 | buffer_delete(output); |
| 211 | return -1; |
| 212 | } |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 213 | segs[segments].len = len; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 214 | |
| 215 | /* If the compressed section is larger, then use the |
| 216 | original stuff */ |
| 217 | |
Stefan Reinauer | 14ad50e | 2009-04-04 22:18:26 +0000 | [diff] [blame] | 218 | if ((unsigned int)len > phdr[i].p_filesz) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 219 | segs[segments].compression = 0; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 220 | segs[segments].len = phdr[i].p_filesz; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 221 | memcpy(output->data + doffset, |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 222 | &header[phdr[i].p_offset], phdr[i].p_filesz); |
| 223 | } |
| 224 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 225 | doffset += segs[segments].len; |
| 226 | osize += segs[segments].len; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 227 | |
| 228 | segments++; |
| 229 | } |
| 230 | |
| 231 | segs[segments].type = PAYLOAD_SEGMENT_ENTRY; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 232 | segs[segments++].load_addr = ehdr.e_entry; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 233 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 234 | output->size = (segments * sizeof(*segs)) + osize; |
| 235 | xdr_segs(output, segs, segments); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 236 | return 0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 237 | } |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 238 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 239 | int parse_flat_binary_to_payload(const struct buffer *input, |
| 240 | struct buffer *output, |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 241 | uint32_t loadaddress, |
| 242 | uint32_t entrypoint, |
| 243 | comp_algo algo) |
| 244 | { |
| 245 | comp_func_ptr compress; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 246 | struct cbfs_payload_segment segs[2]; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 247 | int doffset, len = 0; |
| 248 | |
| 249 | compress = compression_function(algo); |
| 250 | if (!compress) |
| 251 | return -1; |
| 252 | |
| 253 | DEBUG("start: parse_flat_binary_to_payload\n"); |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 254 | if (buffer_create(output, (sizeof(segs) + input->size), |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 255 | input->name) != 0) |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 256 | return -1; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 257 | memset(output->data, 0, output->size); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 258 | |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 259 | doffset = (2 * sizeof(*segs)); |
| 260 | |
| 261 | /* Prepare code segment */ |
| 262 | segs[0].type = PAYLOAD_SEGMENT_CODE; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 263 | segs[0].load_addr = loadaddress; |
| 264 | segs[0].mem_len = input->size; |
| 265 | segs[0].offset = doffset; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 266 | |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 267 | if (compress(input->data, input->size, output->data + doffset, &len)) { |
| 268 | buffer_delete(output); |
| 269 | return -1; |
| 270 | } |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 271 | segs[0].compression = algo; |
| 272 | segs[0].len = len; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 273 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 274 | if ((unsigned int)len >= input->size) { |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 275 | WARN("Compressing data would make it bigger - disabled.\n"); |
| 276 | segs[0].compression = 0; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 277 | segs[0].len = input->size; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 278 | memcpy(output->data + doffset, input->data, input->size); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* prepare entry point segment */ |
| 282 | segs[1].type = PAYLOAD_SEGMENT_ENTRY; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 283 | segs[1].load_addr = entrypoint; |
| 284 | output->size = doffset + segs[0].len; |
| 285 | xdr_segs(output, segs, 2); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 286 | return 0; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 287 | } |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 288 | |
| 289 | int parse_fv_to_payload(const struct buffer *input, |
| 290 | struct buffer *output, comp_algo algo) |
| 291 | { |
| 292 | comp_func_ptr compress; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 293 | struct cbfs_payload_segment segs[2]; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 294 | int doffset, len = 0; |
| 295 | firmware_volume_header_t *fv; |
| 296 | ffs_file_header_t *fh; |
| 297 | common_section_header_t *cs; |
| 298 | dos_header_t *dh; |
| 299 | coff_header_t *ch; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 300 | int dh_offset; |
| 301 | |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 302 | uint32_t loadaddress = 0; |
| 303 | uint32_t entrypoint = 0; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 304 | |
| 305 | compress = compression_function(algo); |
| 306 | if (!compress) |
| 307 | return -1; |
| 308 | |
| 309 | DEBUG("start: parse_fv_to_payload\n"); |
| 310 | |
| 311 | fv = (firmware_volume_header_t *)input->data; |
| 312 | if (fv->signature != FV_SIGNATURE) { |
| 313 | INFO("Not a UEFI firmware volume.\n"); |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | fh = (ffs_file_header_t *)(input->data + fv->header_length); |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 318 | while (fh->file_type == FILETYPE_PAD) { |
| 319 | 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] | 320 | ERROR("skipping %lu bytes of FV padding\n", offset); |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 321 | fh = (ffs_file_header_t *)(((void*)fh) + offset); |
| 322 | } |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 323 | if (fh->file_type != FILETYPE_SEC) { |
| 324 | ERROR("Not a usable UEFI firmware volume.\n"); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 325 | INFO("First file in first FV not a SEC core.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 326 | return -1; |
| 327 | } |
| 328 | |
| 329 | cs = (common_section_header_t *)&fh[1]; |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 330 | while (cs->section_type == SECTION_RAW) { |
| 331 | 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] | 332 | ERROR("skipping %lu bytes of section padding\n", offset); |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 333 | cs = (common_section_header_t *)(((void*)cs) + offset); |
| 334 | } |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 335 | if (cs->section_type != SECTION_PE32) { |
| 336 | ERROR("Not a usable UEFI firmware volume.\n"); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 337 | INFO("Section type not PE32.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 338 | return -1; |
| 339 | } |
| 340 | |
| 341 | dh = (dos_header_t *)&cs[1]; |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 342 | if (dh->signature != DOS_MAGIC) { |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 343 | ERROR("Not a usable UEFI firmware volume.\n"); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 344 | INFO("DOS header signature wrong.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | dh_offset = (unsigned long)dh - (unsigned long)input->data; |
| 349 | DEBUG("dos header offset = %x\n", dh_offset); |
| 350 | |
| 351 | ch = (coff_header_t *)(((void *)dh)+dh->e_lfanew); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 352 | |
| 353 | if (ch->machine == MACHINE_TYPE_X86) { |
| 354 | pe_opt_header_32_t *ph; |
| 355 | ph = (pe_opt_header_32_t *)&ch[1]; |
| 356 | if (ph->signature != PE_HDR_32_MAGIC) { |
| 357 | WARN("PE header signature incorrect.\n"); |
| 358 | return -1; |
| 359 | } |
| 360 | DEBUG("image base %x\n", ph->image_addr); |
| 361 | DEBUG("entry point %x\n", ph->entry_point); |
| 362 | |
| 363 | loadaddress = ph->image_addr - dh_offset; |
| 364 | entrypoint = ph->image_addr + ph->entry_point; |
| 365 | } else if (ch->machine == MACHINE_TYPE_X64) { |
| 366 | pe_opt_header_64_t *ph; |
| 367 | ph = (pe_opt_header_64_t *)&ch[1]; |
| 368 | if (ph->signature != PE_HDR_64_MAGIC) { |
| 369 | WARN("PE header signature incorrect.\n"); |
| 370 | return -1; |
| 371 | } |
| 372 | DEBUG("image base %lx\n", (unsigned long)ph->image_addr); |
| 373 | DEBUG("entry point %x\n", ph->entry_point); |
| 374 | |
| 375 | loadaddress = ph->image_addr - dh_offset; |
| 376 | entrypoint = ph->image_addr + ph->entry_point; |
| 377 | } else { |
| 378 | ERROR("Machine type not x86 or x64.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 379 | return -1; |
| 380 | } |
| 381 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 382 | if (buffer_create(output, (sizeof(segs) + input->size), |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 383 | input->name) != 0) |
| 384 | return -1; |
| 385 | |
| 386 | memset(output->data, 0, output->size); |
| 387 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 388 | doffset = (sizeof(segs)); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 389 | |
| 390 | /* Prepare code segment */ |
| 391 | segs[0].type = PAYLOAD_SEGMENT_CODE; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 392 | segs[0].load_addr = loadaddress; |
| 393 | segs[0].mem_len = input->size; |
| 394 | segs[0].offset = doffset; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 395 | |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 396 | if (compress(input->data, input->size, output->data + doffset, &len)) { |
| 397 | buffer_delete(output); |
| 398 | return -1; |
| 399 | } |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 400 | segs[0].compression = algo; |
| 401 | segs[0].len = len; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 402 | |
| 403 | if ((unsigned int)len >= input->size) { |
| 404 | WARN("Compressing data would make it bigger - disabled.\n"); |
| 405 | segs[0].compression = 0; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 406 | segs[0].len = input->size; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 407 | memcpy(output->data + doffset, input->data, input->size); |
| 408 | } |
| 409 | |
| 410 | /* prepare entry point segment */ |
| 411 | segs[1].type = PAYLOAD_SEGMENT_ENTRY; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 412 | segs[1].load_addr = entrypoint; |
| 413 | output->size = doffset + segs[0].len; |
| 414 | xdr_segs(output, segs, 2); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 415 | return 0; |
| 416 | |
| 417 | } |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 418 | |