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 | |
| 26 | #include "common.h" |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 27 | #include "cbfs.h" |
Hung-Te Lin | 4505ceb | 2013-01-28 22:40:10 +0800 | [diff] [blame] | 28 | #include "elf.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 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 32 | int parse_elf_to_payload(const struct buffer *input, |
| 33 | struct buffer *output, comp_algo algo) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 34 | { |
| 35 | Elf32_Phdr *phdr; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 36 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *)input->data; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 37 | Elf32_Shdr *shdr; |
| 38 | char *header; |
| 39 | char *strtab; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 40 | int headers; |
| 41 | int segments = 1; |
| 42 | int isize = 0, osize = 0; |
| 43 | int doffset = 0; |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame] | 44 | struct cbfs_payload_segment *segs; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 45 | int i; |
| 46 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 47 | if(!iself((unsigned char *)input->data)){ |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 48 | INFO("The payload file is not in ELF format!\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 49 | return -1; |
Cristi Magherusan | 19a99c6 | 2009-09-25 22:21:47 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 52 | // The tool may work in architecture-independent way. |
| 53 | if (arch != CBFS_ARCHITECTURE_UNKNOWN && |
| 54 | !((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) && |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 55 | !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 56 | ERROR("The payload file has the wrong architecture\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 57 | return -1; |
| 58 | } |
Cristi Magherusan | 19a99c6 | 2009-09-25 22:21:47 +0000 | [diff] [blame] | 59 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 60 | comp_func_ptr compress = compression_function(algo); |
| 61 | if (!compress) |
| 62 | return -1; |
| 63 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 64 | DEBUG("start: parse_elf_to_payload\n"); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 65 | headers = ehdr->e_phnum; |
| 66 | header = (char *)ehdr; |
| 67 | |
| 68 | phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]); |
| 69 | shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]); |
| 70 | |
| 71 | strtab = &header[shdr[ehdr->e_shstrndx].sh_offset]; |
| 72 | |
| 73 | /* Count the number of headers - look for the .notes.pinfo |
| 74 | * section */ |
| 75 | |
| 76 | for (i = 0; i < ehdr->e_shnum; i++) { |
| 77 | char *name; |
| 78 | |
| 79 | if (i == ehdr->e_shstrndx) |
| 80 | continue; |
| 81 | |
| 82 | if (shdr[i].sh_size == 0) |
| 83 | continue; |
| 84 | |
| 85 | name = (char *)(strtab + shdr[i].sh_name); |
| 86 | |
Ronald G. Minnich | 5a1af7b | 2009-09-17 15:35:08 +0000 | [diff] [blame] | 87 | if (!strcmp(name, ".note.pinfo")) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 88 | segments++; |
Ronald G. Minnich | 5a1af7b | 2009-09-17 15:35:08 +0000 | [diff] [blame] | 89 | isize += (unsigned int)shdr[i].sh_size; |
| 90 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* Now, regular headers - we only care about PT_LOAD headers, |
| 94 | * because thats what we're actually going to load |
| 95 | */ |
| 96 | |
| 97 | for (i = 0; i < headers; i++) { |
| 98 | if (phdr[i].p_type != PT_LOAD) |
| 99 | continue; |
| 100 | |
| 101 | /* Empty segments are never interesting */ |
| 102 | if (phdr[i].p_memsz == 0) |
| 103 | continue; |
| 104 | |
| 105 | isize += phdr[i].p_filesz; |
| 106 | |
| 107 | segments++; |
| 108 | } |
| 109 | |
| 110 | /* Allocate a block of memory to store the data in */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 111 | if (buffer_create(output, (segments * sizeof(*segs)) + isize, |
| 112 | input->name) != 0) |
| 113 | return -1; |
| 114 | memset(output->data, 0, output->size); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 115 | |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame] | 116 | doffset = (segments * sizeof(struct cbfs_payload_segment)); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 117 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 118 | segs = (struct cbfs_payload_segment *)output->data; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 119 | segments = 0; |
| 120 | |
| 121 | for (i = 0; i < ehdr->e_shnum; i++) { |
| 122 | char *name; |
| 123 | |
| 124 | if (i == ehdr->e_shstrndx) |
| 125 | continue; |
| 126 | |
| 127 | if (shdr[i].sh_size == 0) |
| 128 | continue; |
| 129 | |
| 130 | name = (char *)(strtab + shdr[i].sh_name); |
| 131 | |
| 132 | if (!strcmp(name, ".note.pinfo")) { |
| 133 | segs[segments].type = PAYLOAD_SEGMENT_PARAMS; |
| 134 | segs[segments].load_addr = 0; |
| 135 | segs[segments].len = (unsigned int)shdr[i].sh_size; |
| 136 | segs[segments].offset = doffset; |
| 137 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 138 | memcpy((unsigned long *)(output->data + doffset), |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 139 | &header[shdr[i].sh_offset], shdr[i].sh_size); |
| 140 | |
| 141 | doffset += segs[segments].len; |
| 142 | osize += segs[segments].len; |
| 143 | |
| 144 | segments++; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | for (i = 0; i < headers; i++) { |
| 149 | if (phdr[i].p_type != PT_LOAD) |
| 150 | continue; |
| 151 | |
| 152 | if (phdr[i].p_memsz == 0) |
| 153 | continue; |
| 154 | |
| 155 | if (phdr[i].p_filesz == 0) { |
| 156 | segs[segments].type = PAYLOAD_SEGMENT_BSS; |
| 157 | segs[segments].load_addr = |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 158 | (uint64_t)htonll(phdr[i].p_paddr); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 159 | segs[segments].mem_len = |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 160 | (uint32_t)htonl(phdr[i].p_memsz); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 161 | segs[segments].offset = htonl(doffset); |
| 162 | |
| 163 | segments++; |
| 164 | continue; |
| 165 | } |
| 166 | |
Stefan Reinauer | 51f6a20 | 2012-01-11 12:40:14 -0800 | [diff] [blame] | 167 | if (phdr[i].p_flags & PF_X) |
| 168 | segs[segments].type = PAYLOAD_SEGMENT_CODE; |
| 169 | else |
| 170 | segs[segments].type = PAYLOAD_SEGMENT_DATA; |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 171 | segs[segments].load_addr = (uint64_t)htonll(phdr[i].p_paddr); |
| 172 | segs[segments].mem_len = (uint32_t)htonl(phdr[i].p_memsz); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 173 | segs[segments].compression = htonl(algo); |
| 174 | segs[segments].offset = htonl(doffset); |
| 175 | |
| 176 | int len; |
| 177 | compress((char *)&header[phdr[i].p_offset], |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 178 | phdr[i].p_filesz, output->data + doffset, &len); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 179 | segs[segments].len = htonl(len); |
| 180 | |
| 181 | /* If the compressed section is larger, then use the |
| 182 | original stuff */ |
| 183 | |
Stefan Reinauer | 14ad50e | 2009-04-04 22:18:26 +0000 | [diff] [blame] | 184 | if ((unsigned int)len > phdr[i].p_filesz) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 185 | segs[segments].compression = 0; |
| 186 | segs[segments].len = htonl(phdr[i].p_filesz); |
| 187 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 188 | memcpy(output->data + doffset, |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 189 | &header[phdr[i].p_offset], phdr[i].p_filesz); |
| 190 | } |
| 191 | |
| 192 | doffset += ntohl(segs[segments].len); |
| 193 | osize += ntohl(segs[segments].len); |
| 194 | |
| 195 | segments++; |
| 196 | } |
| 197 | |
| 198 | segs[segments].type = PAYLOAD_SEGMENT_ENTRY; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 199 | segs[segments++].load_addr = htonll(ehdr->e_entry); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 200 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 201 | output->size = (segments * sizeof(struct cbfs_payload_segment)) + osize; |
| 202 | return 0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 203 | } |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 204 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 205 | int parse_flat_binary_to_payload(const struct buffer *input, |
| 206 | struct buffer *output, |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 207 | uint32_t loadaddress, |
| 208 | uint32_t entrypoint, |
| 209 | comp_algo algo) |
| 210 | { |
| 211 | comp_func_ptr compress; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 212 | struct cbfs_payload_segment *segs; |
| 213 | int doffset, len = 0; |
| 214 | |
| 215 | compress = compression_function(algo); |
| 216 | if (!compress) |
| 217 | return -1; |
| 218 | |
| 219 | DEBUG("start: parse_flat_binary_to_payload\n"); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 220 | if (buffer_create(output, (2 * sizeof(*segs) + input->size), |
| 221 | input->name) != 0) |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 222 | return -1; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 223 | memset(output->data, 0, output->size); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 224 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 225 | segs = (struct cbfs_payload_segment *)output->data; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 226 | doffset = (2 * sizeof(*segs)); |
| 227 | |
| 228 | /* Prepare code segment */ |
| 229 | segs[0].type = PAYLOAD_SEGMENT_CODE; |
| 230 | segs[0].load_addr = htonll(loadaddress); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 231 | segs[0].mem_len = htonl(input->size); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 232 | segs[0].offset = htonl(doffset); |
| 233 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 234 | compress(input->data, input->size, output->data + doffset, &len); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 235 | segs[0].compression = htonl(algo); |
| 236 | segs[0].len = htonl(len); |
| 237 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 238 | if ((unsigned int)len >= input->size) { |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 239 | WARN("Compressing data would make it bigger - disabled.\n"); |
| 240 | segs[0].compression = 0; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 241 | segs[0].len = htonl(input->size); |
| 242 | memcpy(output->data + doffset, input->data, input->size); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* prepare entry point segment */ |
| 246 | segs[1].type = PAYLOAD_SEGMENT_ENTRY; |
| 247 | segs[1].load_addr = htonll(entrypoint); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 248 | output->size = doffset + ntohl(segs[0].len); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 249 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 250 | return 0; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 251 | } |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 252 | |
| 253 | int parse_fv_to_payload(const struct buffer *input, |
| 254 | struct buffer *output, comp_algo algo) |
| 255 | { |
| 256 | comp_func_ptr compress; |
| 257 | struct cbfs_payload_segment *segs; |
| 258 | int doffset, len = 0; |
| 259 | firmware_volume_header_t *fv; |
| 260 | ffs_file_header_t *fh; |
| 261 | common_section_header_t *cs; |
| 262 | dos_header_t *dh; |
| 263 | coff_header_t *ch; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 264 | int dh_offset; |
| 265 | |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 266 | uint32_t loadaddress = 0; |
| 267 | uint32_t entrypoint = 0; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 268 | |
| 269 | compress = compression_function(algo); |
| 270 | if (!compress) |
| 271 | return -1; |
| 272 | |
| 273 | DEBUG("start: parse_fv_to_payload\n"); |
| 274 | |
| 275 | fv = (firmware_volume_header_t *)input->data; |
| 276 | if (fv->signature != FV_SIGNATURE) { |
| 277 | INFO("Not a UEFI firmware volume.\n"); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | fh = (ffs_file_header_t *)(input->data + fv->header_length); |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 282 | while (fh->file_type == FILETYPE_PAD) { |
| 283 | unsigned long offset = (fh->size[2] << 16) | (fh->size[1] << 8) | fh->size[0]; |
| 284 | ERROR("skipping %d bytes of FV padding\n", offset); |
| 285 | fh = (ffs_file_header_t *)(((void*)fh) + offset); |
| 286 | } |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 287 | if (fh->file_type != FILETYPE_SEC) { |
| 288 | ERROR("Not a usable UEFI firmware volume.\n"); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 289 | INFO("First file in first FV not a SEC core.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | cs = (common_section_header_t *)&fh[1]; |
Patrick Georgi | 4610247 | 2013-02-09 13:26:19 +0100 | [diff] [blame] | 294 | while (cs->section_type == SECTION_RAW) { |
| 295 | unsigned long offset = (cs->size[2] << 16) | (cs->size[1] << 8) | cs->size[0]; |
| 296 | ERROR("skipping %d bytes of section padding\n", offset); |
| 297 | cs = (common_section_header_t *)(((void*)cs) + offset); |
| 298 | } |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 299 | if (cs->section_type != SECTION_PE32) { |
| 300 | ERROR("Not a usable UEFI firmware volume.\n"); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 301 | INFO("Section type not PE32.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 302 | return -1; |
| 303 | } |
| 304 | |
| 305 | dh = (dos_header_t *)&cs[1]; |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 306 | if (dh->signature != DOS_MAGIC) { |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 307 | ERROR("Not a usable UEFI firmware volume.\n"); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 308 | INFO("DOS header signature wrong.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | dh_offset = (unsigned long)dh - (unsigned long)input->data; |
| 313 | DEBUG("dos header offset = %x\n", dh_offset); |
| 314 | |
| 315 | ch = (coff_header_t *)(((void *)dh)+dh->e_lfanew); |
Stefan Reinauer | e876418 | 2013-02-05 13:46:49 -0800 | [diff] [blame] | 316 | |
| 317 | if (ch->machine == MACHINE_TYPE_X86) { |
| 318 | pe_opt_header_32_t *ph; |
| 319 | ph = (pe_opt_header_32_t *)&ch[1]; |
| 320 | if (ph->signature != PE_HDR_32_MAGIC) { |
| 321 | WARN("PE header signature incorrect.\n"); |
| 322 | return -1; |
| 323 | } |
| 324 | DEBUG("image base %x\n", ph->image_addr); |
| 325 | DEBUG("entry point %x\n", ph->entry_point); |
| 326 | |
| 327 | loadaddress = ph->image_addr - dh_offset; |
| 328 | entrypoint = ph->image_addr + ph->entry_point; |
| 329 | } else if (ch->machine == MACHINE_TYPE_X64) { |
| 330 | pe_opt_header_64_t *ph; |
| 331 | ph = (pe_opt_header_64_t *)&ch[1]; |
| 332 | if (ph->signature != PE_HDR_64_MAGIC) { |
| 333 | WARN("PE header signature incorrect.\n"); |
| 334 | return -1; |
| 335 | } |
| 336 | DEBUG("image base %lx\n", (unsigned long)ph->image_addr); |
| 337 | DEBUG("entry point %x\n", ph->entry_point); |
| 338 | |
| 339 | loadaddress = ph->image_addr - dh_offset; |
| 340 | entrypoint = ph->image_addr + ph->entry_point; |
| 341 | } else { |
| 342 | ERROR("Machine type not x86 or x64.\n"); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 343 | return -1; |
| 344 | } |
| 345 | |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 346 | if (buffer_create(output, (2 * sizeof(*segs) + input->size), |
| 347 | input->name) != 0) |
| 348 | return -1; |
| 349 | |
| 350 | memset(output->data, 0, output->size); |
| 351 | |
| 352 | segs = (struct cbfs_payload_segment *)output->data; |
| 353 | doffset = (2 * sizeof(*segs)); |
| 354 | |
| 355 | /* Prepare code segment */ |
| 356 | segs[0].type = PAYLOAD_SEGMENT_CODE; |
| 357 | segs[0].load_addr = htonll(loadaddress); |
| 358 | segs[0].mem_len = htonl(input->size); |
| 359 | segs[0].offset = htonl(doffset); |
| 360 | |
| 361 | compress(input->data, input->size, output->data + doffset, &len); |
| 362 | segs[0].compression = htonl(algo); |
| 363 | segs[0].len = htonl(len); |
| 364 | |
| 365 | if ((unsigned int)len >= input->size) { |
| 366 | WARN("Compressing data would make it bigger - disabled.\n"); |
| 367 | segs[0].compression = 0; |
| 368 | segs[0].len = htonl(input->size); |
| 369 | memcpy(output->data + doffset, input->data, input->size); |
| 370 | } |
| 371 | |
| 372 | /* prepare entry point segment */ |
| 373 | segs[1].type = PAYLOAD_SEGMENT_ENTRY; |
| 374 | segs[1].load_addr = htonll(entrypoint); |
| 375 | output->size = doffset + ntohl(segs[0].len); |
| 376 | |
| 377 | return 0; |
| 378 | |
| 379 | } |