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" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 29 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 30 | int parse_elf_to_payload(const struct buffer *input, |
| 31 | struct buffer *output, comp_algo algo) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 32 | { |
| 33 | Elf32_Phdr *phdr; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 34 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *)input->data; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 35 | Elf32_Shdr *shdr; |
| 36 | char *header; |
| 37 | char *strtab; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 38 | int headers; |
| 39 | int segments = 1; |
| 40 | int isize = 0, osize = 0; |
| 41 | int doffset = 0; |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame] | 42 | struct cbfs_payload_segment *segs; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 43 | int i; |
| 44 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 45 | if(!iself((unsigned char *)input->data)){ |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 46 | ERROR("The payload file is not in ELF format!\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 47 | return -1; |
Cristi Magherusan | 19a99c6 | 2009-09-25 22:21:47 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 50 | // The tool may work in architecture-independent way. |
| 51 | if (arch != CBFS_ARCHITECTURE_UNKNOWN && |
| 52 | !((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) && |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 53 | !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 54 | ERROR("The payload file has the wrong architecture\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 55 | return -1; |
| 56 | } |
Cristi Magherusan | 19a99c6 | 2009-09-25 22:21:47 +0000 | [diff] [blame] | 57 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 58 | comp_func_ptr compress = compression_function(algo); |
| 59 | if (!compress) |
| 60 | return -1; |
| 61 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 62 | DEBUG("start: parse_elf_to_payload\n"); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 63 | headers = ehdr->e_phnum; |
| 64 | header = (char *)ehdr; |
| 65 | |
| 66 | phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]); |
| 67 | shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]); |
| 68 | |
| 69 | strtab = &header[shdr[ehdr->e_shstrndx].sh_offset]; |
| 70 | |
| 71 | /* Count the number of headers - look for the .notes.pinfo |
| 72 | * section */ |
| 73 | |
| 74 | for (i = 0; i < ehdr->e_shnum; i++) { |
| 75 | char *name; |
| 76 | |
| 77 | if (i == ehdr->e_shstrndx) |
| 78 | continue; |
| 79 | |
| 80 | if (shdr[i].sh_size == 0) |
| 81 | continue; |
| 82 | |
| 83 | name = (char *)(strtab + shdr[i].sh_name); |
| 84 | |
Ronald G. Minnich | 5a1af7b | 2009-09-17 15:35:08 +0000 | [diff] [blame] | 85 | if (!strcmp(name, ".note.pinfo")) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 86 | segments++; |
Ronald G. Minnich | 5a1af7b | 2009-09-17 15:35:08 +0000 | [diff] [blame] | 87 | isize += (unsigned int)shdr[i].sh_size; |
| 88 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* Now, regular headers - we only care about PT_LOAD headers, |
| 92 | * because thats what we're actually going to load |
| 93 | */ |
| 94 | |
| 95 | for (i = 0; i < headers; i++) { |
| 96 | if (phdr[i].p_type != PT_LOAD) |
| 97 | continue; |
| 98 | |
| 99 | /* Empty segments are never interesting */ |
| 100 | if (phdr[i].p_memsz == 0) |
| 101 | continue; |
| 102 | |
| 103 | isize += phdr[i].p_filesz; |
| 104 | |
| 105 | segments++; |
| 106 | } |
| 107 | |
| 108 | /* Allocate a block of memory to store the data in */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 109 | if (buffer_create(output, (segments * sizeof(*segs)) + isize, |
| 110 | input->name) != 0) |
| 111 | return -1; |
| 112 | memset(output->data, 0, output->size); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 113 | |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame] | 114 | doffset = (segments * sizeof(struct cbfs_payload_segment)); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 115 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 116 | segs = (struct cbfs_payload_segment *)output->data; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 117 | segments = 0; |
| 118 | |
| 119 | for (i = 0; i < ehdr->e_shnum; i++) { |
| 120 | char *name; |
| 121 | |
| 122 | if (i == ehdr->e_shstrndx) |
| 123 | continue; |
| 124 | |
| 125 | if (shdr[i].sh_size == 0) |
| 126 | continue; |
| 127 | |
| 128 | name = (char *)(strtab + shdr[i].sh_name); |
| 129 | |
| 130 | if (!strcmp(name, ".note.pinfo")) { |
| 131 | segs[segments].type = PAYLOAD_SEGMENT_PARAMS; |
| 132 | segs[segments].load_addr = 0; |
| 133 | segs[segments].len = (unsigned int)shdr[i].sh_size; |
| 134 | segs[segments].offset = doffset; |
| 135 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 136 | memcpy((unsigned long *)(output->data + doffset), |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 137 | &header[shdr[i].sh_offset], shdr[i].sh_size); |
| 138 | |
| 139 | doffset += segs[segments].len; |
| 140 | osize += segs[segments].len; |
| 141 | |
| 142 | segments++; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | for (i = 0; i < headers; i++) { |
| 147 | if (phdr[i].p_type != PT_LOAD) |
| 148 | continue; |
| 149 | |
| 150 | if (phdr[i].p_memsz == 0) |
| 151 | continue; |
| 152 | |
| 153 | if (phdr[i].p_filesz == 0) { |
| 154 | segs[segments].type = PAYLOAD_SEGMENT_BSS; |
| 155 | segs[segments].load_addr = |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 156 | (uint64_t)htonll(phdr[i].p_paddr); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 157 | segs[segments].mem_len = |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 158 | (uint32_t)htonl(phdr[i].p_memsz); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 159 | segs[segments].offset = htonl(doffset); |
| 160 | |
| 161 | segments++; |
| 162 | continue; |
| 163 | } |
| 164 | |
Stefan Reinauer | 51f6a20 | 2012-01-11 12:40:14 -0800 | [diff] [blame] | 165 | if (phdr[i].p_flags & PF_X) |
| 166 | segs[segments].type = PAYLOAD_SEGMENT_CODE; |
| 167 | else |
| 168 | segs[segments].type = PAYLOAD_SEGMENT_DATA; |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 169 | segs[segments].load_addr = (uint64_t)htonll(phdr[i].p_paddr); |
| 170 | segs[segments].mem_len = (uint32_t)htonl(phdr[i].p_memsz); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 171 | segs[segments].compression = htonl(algo); |
| 172 | segs[segments].offset = htonl(doffset); |
| 173 | |
| 174 | int len; |
| 175 | compress((char *)&header[phdr[i].p_offset], |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 176 | phdr[i].p_filesz, output->data + doffset, &len); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 177 | segs[segments].len = htonl(len); |
| 178 | |
| 179 | /* If the compressed section is larger, then use the |
| 180 | original stuff */ |
| 181 | |
Stefan Reinauer | 14ad50e | 2009-04-04 22:18:26 +0000 | [diff] [blame] | 182 | if ((unsigned int)len > phdr[i].p_filesz) { |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 183 | segs[segments].compression = 0; |
| 184 | segs[segments].len = htonl(phdr[i].p_filesz); |
| 185 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 186 | memcpy(output->data + doffset, |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 187 | &header[phdr[i].p_offset], phdr[i].p_filesz); |
| 188 | } |
| 189 | |
| 190 | doffset += ntohl(segs[segments].len); |
| 191 | osize += ntohl(segs[segments].len); |
| 192 | |
| 193 | segments++; |
| 194 | } |
| 195 | |
| 196 | segs[segments].type = PAYLOAD_SEGMENT_ENTRY; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 197 | segs[segments++].load_addr = htonll(ehdr->e_entry); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 198 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 199 | output->size = (segments * sizeof(struct cbfs_payload_segment)) + osize; |
| 200 | return 0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 201 | } |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 202 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 203 | int parse_flat_binary_to_payload(const struct buffer *input, |
| 204 | struct buffer *output, |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 205 | uint32_t loadaddress, |
| 206 | uint32_t entrypoint, |
| 207 | comp_algo algo) |
| 208 | { |
| 209 | comp_func_ptr compress; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 210 | struct cbfs_payload_segment *segs; |
| 211 | int doffset, len = 0; |
| 212 | |
| 213 | compress = compression_function(algo); |
| 214 | if (!compress) |
| 215 | return -1; |
| 216 | |
| 217 | DEBUG("start: parse_flat_binary_to_payload\n"); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 218 | if (buffer_create(output, (2 * sizeof(*segs) + input->size), |
| 219 | input->name) != 0) |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 220 | return -1; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 221 | memset(output->data, 0, output->size); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 222 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 223 | segs = (struct cbfs_payload_segment *)output->data; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 224 | doffset = (2 * sizeof(*segs)); |
| 225 | |
| 226 | /* Prepare code segment */ |
| 227 | segs[0].type = PAYLOAD_SEGMENT_CODE; |
| 228 | segs[0].load_addr = htonll(loadaddress); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 229 | segs[0].mem_len = htonl(input->size); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 230 | segs[0].offset = htonl(doffset); |
| 231 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 232 | compress(input->data, input->size, output->data + doffset, &len); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 233 | segs[0].compression = htonl(algo); |
| 234 | segs[0].len = htonl(len); |
| 235 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 236 | if ((unsigned int)len >= input->size) { |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 237 | WARN("Compressing data would make it bigger - disabled.\n"); |
| 238 | segs[0].compression = 0; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 239 | segs[0].len = htonl(input->size); |
| 240 | memcpy(output->data + doffset, input->data, input->size); |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* prepare entry point segment */ |
| 244 | segs[1].type = PAYLOAD_SEGMENT_ENTRY; |
| 245 | segs[1].load_addr = htonll(entrypoint); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame^] | 246 | output->size = doffset + ntohl(segs[0].len); |
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 | return 0; |
Hung-Te Lin | 05dccae | 2013-01-28 15:04:30 +0800 | [diff] [blame] | 249 | } |