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