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-mkstage |
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> |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 7 | * Copyright (C) 2012 Google, Inc. |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; version 2 of the License. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 26 | |
Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 27 | #include "elfparsing.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 28 | #include "common.h" |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 29 | #include "cbfs.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 30 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 31 | /* returns size of result, or -1 if error. |
| 32 | * Note that, with the new code, this function |
| 33 | * works for all elf files, not just the restricted set. |
| 34 | */ |
| 35 | int parse_elf_to_stage(const struct buffer *input, struct buffer *output, |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 36 | uint32_t arch, comp_algo algo, uint32_t *location) |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 37 | { |
Furquan Shaikh | cc6f84c | 2014-10-30 11:28:27 -0700 | [diff] [blame^] | 38 | struct parsed_elf pelf; |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 39 | Elf64_Phdr *phdr; |
Furquan Shaikh | cc6f84c | 2014-10-30 11:28:27 -0700 | [diff] [blame^] | 40 | Elf64_Ehdr *ehdr; |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 41 | char *buffer; |
| 42 | struct buffer outheader; |
Furquan Shaikh | cc6f84c | 2014-10-30 11:28:27 -0700 | [diff] [blame^] | 43 | int ret = -1; |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 44 | |
| 45 | int headers; |
| 46 | int i, outlen; |
| 47 | uint32_t data_start, data_end, mem_end; |
| 48 | |
| 49 | comp_func_ptr compress = compression_function(algo); |
| 50 | if (!compress) |
| 51 | return -1; |
| 52 | |
| 53 | DEBUG("start: parse_elf_to_stage(location=0x%x)\n", *location); |
| 54 | |
Furquan Shaikh | cc6f84c | 2014-10-30 11:28:27 -0700 | [diff] [blame^] | 55 | int flags = ELF_PARSE_PHDR | ELF_PARSE_SHDR | ELF_PARSE_STRTAB; |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 56 | |
Furquan Shaikh | cc6f84c | 2014-10-30 11:28:27 -0700 | [diff] [blame^] | 57 | if (parse_elf(input, &pelf, flags)) { |
| 58 | ERROR("Couldn't parse ELF\n"); |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | ehdr = &pelf.ehdr; |
| 63 | phdr = &pelf.phdr[0]; |
| 64 | |
| 65 | headers = ehdr->e_phnum; |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 66 | |
| 67 | data_start = ~0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 68 | data_end = 0; |
| 69 | mem_end = 0; |
| 70 | |
| 71 | for (i = 0; i < headers; i++) { |
| 72 | unsigned int start, mend, rend; |
| 73 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 74 | if (phdr[i].p_type != PT_LOAD) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 75 | continue; |
| 76 | |
| 77 | /* Empty segments are never interesting */ |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 78 | if (phdr[i].p_memsz == 0) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 79 | continue; |
| 80 | |
| 81 | /* BSS */ |
| 82 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 83 | start = phdr[i].p_paddr; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 84 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 85 | mend = start + phdr[i].p_memsz; |
| 86 | rend = start + phdr[i].p_filesz; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 87 | |
| 88 | if (start < data_start) |
| 89 | data_start = start; |
| 90 | |
| 91 | if (rend > data_end) |
| 92 | data_end = rend; |
| 93 | |
| 94 | if (mend > mem_end) |
| 95 | mem_end = mend; |
| 96 | } |
| 97 | |
Patrick Georgi | 9341acd | 2009-12-23 12:52:56 +0000 | [diff] [blame] | 98 | if (data_start < *location) { |
| 99 | data_start = *location; |
| 100 | } |
| 101 | |
Patrick Georgi | a6c337d | 2010-02-03 17:56:37 +0000 | [diff] [blame] | 102 | if (data_end <= data_start) { |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 103 | ERROR("data ends (%08lx) before it starts (%08lx). Make sure " |
| 104 | "the ELF file is correct and resides in ROM space.\n", |
| 105 | (unsigned long)data_end, (unsigned long)data_start); |
Patrick Georgi | a6c337d | 2010-02-03 17:56:37 +0000 | [diff] [blame] | 106 | exit(1); |
| 107 | } |
| 108 | |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 109 | /* allocate an intermediate buffer for the data */ |
| 110 | buffer = calloc(data_end - data_start, 1); |
| 111 | |
| 112 | if (buffer == NULL) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 113 | ERROR("Unable to allocate memory: %m\n"); |
Furquan Shaikh | cc6f84c | 2014-10-30 11:28:27 -0700 | [diff] [blame^] | 114 | goto err; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* Copy the file data into the buffer */ |
| 118 | |
| 119 | for (i = 0; i < headers; i++) { |
Patrick Georgi | 9341acd | 2009-12-23 12:52:56 +0000 | [diff] [blame] | 120 | unsigned int l_start, l_offset = 0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 121 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 122 | if (phdr[i].p_type != PT_LOAD) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 123 | continue; |
| 124 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 125 | if (phdr[i].p_memsz == 0) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 126 | continue; |
| 127 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 128 | l_start = phdr[i].p_paddr; |
Patrick Georgi | 9341acd | 2009-12-23 12:52:56 +0000 | [diff] [blame] | 129 | if (l_start < *location) { |
| 130 | l_offset = *location - l_start; |
| 131 | l_start = *location; |
| 132 | } |
| 133 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 134 | /* A legal ELF file can have a program header with |
| 135 | * non-zero length but zero-length file size and a |
| 136 | * non-zero offset which, added together, are > than |
| 137 | * input->size (i.e. the total file size). So we need |
| 138 | * to not even test in the case that p_filesz is zero. |
| 139 | */ |
| 140 | if (! phdr[i].p_filesz) |
| 141 | continue; |
| 142 | if (input->size < (phdr[i].p_offset + phdr[i].p_filesz)){ |
| 143 | ERROR("Underflow copying out the segment." |
Paul Menzel | 470c37c | 2014-03-16 00:15:57 +0100 | [diff] [blame] | 144 | "File has %zu bytes left, segment end is %zu\n", |
| 145 | input->size, (size_t)(phdr[i].p_offset + phdr[i].p_filesz)); |
Daniele Forsi | 8e89847 | 2014-07-27 12:01:40 +0200 | [diff] [blame] | 146 | free(buffer); |
Furquan Shaikh | cc6f84c | 2014-10-30 11:28:27 -0700 | [diff] [blame^] | 147 | goto err; |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 148 | } |
Patrick Georgi | 9341acd | 2009-12-23 12:52:56 +0000 | [diff] [blame] | 149 | memcpy(buffer + (l_start - data_start), |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 150 | &input->data[phdr[i].p_offset + l_offset], |
| 151 | phdr[i].p_filesz - l_offset); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* Now make the output buffer */ |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 155 | if (buffer_create(output, sizeof(struct cbfs_stage) + data_end - data_start, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 156 | input->name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 157 | ERROR("Unable to allocate memory: %m\n"); |
Paul Menzel | 2c8f81b | 2013-04-11 10:45:11 +0200 | [diff] [blame] | 158 | free(buffer); |
Furquan Shaikh | cc6f84c | 2014-10-30 11:28:27 -0700 | [diff] [blame^] | 159 | goto err; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 160 | } |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 161 | memset(output->data, 0, output->size); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 162 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 163 | /* Compress the data, at which point we'll know information |
| 164 | * to fill out the header. This seems backward but it works because |
| 165 | * - the output header is a known size (not always true in many xdr's) |
| 166 | * - we do need to know the compressed output size first |
Gabe Black | 845aa14 | 2014-02-21 01:01:06 -0800 | [diff] [blame] | 167 | * If compression fails or makes the data bigger, we'll warn about it |
| 168 | * and use the original data. |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 169 | */ |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 170 | if (compress(buffer, data_end - data_start, |
| 171 | (output->data + sizeof(struct cbfs_stage)), |
Gabe Black | 845aa14 | 2014-02-21 01:01:06 -0800 | [diff] [blame] | 172 | &outlen) < 0 || outlen > data_end - data_start) { |
| 173 | WARN("Compression failed or would make the data bigger " |
| 174 | "- disabled.\n"); |
| 175 | memcpy(output->data + sizeof(struct cbfs_stage), |
| 176 | buffer, data_end - data_start); |
| 177 | algo = CBFS_COMPRESS_NONE; |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 178 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 179 | free(buffer); |
| 180 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 181 | /* Set up for output marshaling. */ |
| 182 | outheader.data = output->data; |
| 183 | outheader.size = 0; |
| 184 | /* N.B. The original plan was that SELF data was B.E. |
| 185 | * but: this is all L.E. |
| 186 | * Maybe we should just change the spec. |
| 187 | */ |
| 188 | xdr_le.put32(&outheader, algo); |
Furquan Shaikh | cc6f84c | 2014-10-30 11:28:27 -0700 | [diff] [blame^] | 189 | xdr_le.put64(&outheader, ehdr->e_entry); |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 190 | xdr_le.put64(&outheader, data_start); |
| 191 | xdr_le.put32(&outheader, outlen); |
| 192 | xdr_le.put32(&outheader, mem_end - data_start); |
| 193 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 194 | if (*location) |
| 195 | *location -= sizeof(struct cbfs_stage); |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 196 | output->size = sizeof(struct cbfs_stage) + outlen; |
Furquan Shaikh | cc6f84c | 2014-10-30 11:28:27 -0700 | [diff] [blame^] | 197 | ret = 0; |
| 198 | |
| 199 | err: |
| 200 | parsed_elf_destroy(&pelf); |
| 201 | return ret; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 202 | } |