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 | |
| 27 | #include "common.h" |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 28 | #include "cbfs.h" |
Hung-Te Lin | 4505ceb | 2013-01-28 22:40:10 +0800 | [diff] [blame] | 29 | #include "elf.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 30 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 31 | static unsigned int idemp(unsigned int x) |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 32 | { |
| 33 | return x; |
| 34 | } |
| 35 | |
Stefan Reinauer | 5ff7c13 | 2011-10-31 12:56:45 -0700 | [diff] [blame] | 36 | /* This is a wrapper around the swab32() macro to make it |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 37 | * usable for the current implementation of parse_elf_to_stage() |
| 38 | */ |
| 39 | static unsigned int swap32(unsigned int x) |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 40 | { |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 41 | return swab32(x); |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 44 | unsigned int (*elf32_to_native) (unsigned int) = idemp; |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 45 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 46 | /* returns size of result, or -1 if error */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 47 | int parse_elf_to_stage(const struct buffer *input, struct buffer *output, |
| 48 | comp_algo algo, uint32_t *location) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 49 | { |
| 50 | Elf32_Phdr *phdr; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 51 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *)input->data; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 52 | char *header, *buffer; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 53 | |
| 54 | int headers; |
| 55 | int i; |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame] | 56 | struct cbfs_stage *stage; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 57 | unsigned int data_start, data_end, mem_end; |
| 58 | |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 59 | int elf_bigendian = 0; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 60 | |
| 61 | comp_func_ptr compress = compression_function(algo); |
| 62 | if (!compress) |
| 63 | return -1; |
| 64 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 65 | DEBUG("start: parse_elf_to_stage(location=0x%x)\n", *location); |
| 66 | if (!iself((unsigned char *)input->data)) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 67 | ERROR("The stage file is not in ELF format!\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 68 | return -1; |
| 69 | } |
| 70 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 71 | // The tool may work in architecture-independent way. |
| 72 | if (arch != CBFS_ARCHITECTURE_UNKNOWN && |
| 73 | !((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) && |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 74 | !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 75 | ERROR("The stage file has the wrong architecture\n"); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) { |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 80 | elf_bigendian = 1; |
| 81 | } |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 82 | if (elf_bigendian != is_big_endian()) { |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 83 | elf32_to_native = swap32; |
| 84 | } |
| 85 | |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 86 | headers = ehdr->e_phnum; |
| 87 | header = (char *)ehdr; |
| 88 | |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 89 | phdr = (Elf32_Phdr *) & header[elf32_to_native(ehdr->e_phoff)]; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 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 | data_start = 0xFFFFFFFF; |
| 96 | data_end = 0; |
| 97 | mem_end = 0; |
| 98 | |
| 99 | for (i = 0; i < headers; i++) { |
| 100 | unsigned int start, mend, rend; |
| 101 | |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 102 | if (elf32_to_native(phdr[i].p_type) != PT_LOAD) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 103 | continue; |
| 104 | |
| 105 | /* Empty segments are never interesting */ |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 106 | if (elf32_to_native(phdr[i].p_memsz) == 0) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 107 | continue; |
| 108 | |
| 109 | /* BSS */ |
| 110 | |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 111 | start = elf32_to_native(phdr[i].p_paddr); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 112 | |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 113 | mend = start + elf32_to_native(phdr[i].p_memsz); |
| 114 | rend = start + elf32_to_native(phdr[i].p_filesz); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 115 | |
| 116 | if (start < data_start) |
| 117 | data_start = start; |
| 118 | |
| 119 | if (rend > data_end) |
| 120 | data_end = rend; |
| 121 | |
| 122 | if (mend > mem_end) |
| 123 | mem_end = mend; |
| 124 | } |
| 125 | |
Patrick Georgi | 9341acd | 2009-12-23 12:52:56 +0000 | [diff] [blame] | 126 | if (data_start < *location) { |
| 127 | data_start = *location; |
| 128 | } |
| 129 | |
Patrick Georgi | a6c337d | 2010-02-03 17:56:37 +0000 | [diff] [blame] | 130 | if (data_end <= data_start) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 131 | ERROR("data ends before it starts. Make sure the " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 132 | "ELF file is correct and resides in ROM space.\n"); |
Patrick Georgi | a6c337d | 2010-02-03 17:56:37 +0000 | [diff] [blame] | 133 | exit(1); |
| 134 | } |
| 135 | |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 136 | /* allocate an intermediate buffer for the data */ |
| 137 | buffer = calloc(data_end - data_start, 1); |
| 138 | |
| 139 | if (buffer == NULL) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 140 | ERROR("Unable to allocate memory: %m\n"); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | /* Copy the file data into the buffer */ |
| 145 | |
| 146 | for (i = 0; i < headers; i++) { |
Patrick Georgi | 9341acd | 2009-12-23 12:52:56 +0000 | [diff] [blame] | 147 | unsigned int l_start, l_offset = 0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 148 | |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 149 | if (elf32_to_native(phdr[i].p_type) != PT_LOAD) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 150 | continue; |
| 151 | |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 152 | if (elf32_to_native(phdr[i].p_memsz) == 0) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 153 | continue; |
| 154 | |
Patrick Georgi | 9341acd | 2009-12-23 12:52:56 +0000 | [diff] [blame] | 155 | l_start = elf32_to_native(phdr[i].p_paddr); |
| 156 | if (l_start < *location) { |
| 157 | l_offset = *location - l_start; |
| 158 | l_start = *location; |
| 159 | } |
| 160 | |
| 161 | memcpy(buffer + (l_start - data_start), |
| 162 | &header[elf32_to_native(phdr[i].p_offset)+l_offset], |
| 163 | elf32_to_native(phdr[i].p_filesz)-l_offset); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* Now make the output buffer */ |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 167 | if (buffer_create(output, sizeof(*stage) + data_end - data_start, |
| 168 | input->name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 169 | ERROR("Unable to allocate memory: %m\n"); |
Paul Menzel | 2c8f81b | 2013-04-11 10:45:11 +0200 | [diff] [blame^] | 170 | free(buffer); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 171 | return -1; |
| 172 | } |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 173 | memset(output->data, 0, output->size); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 174 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 175 | stage = (struct cbfs_stage *)output->data; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 176 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 177 | stage->load = data_start; /* FIXME: htonll */ |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 178 | stage->memlen = mem_end - data_start; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 179 | stage->compression = algo; |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 180 | stage->entry = ehdr->e_entry; /* FIXME: htonll */ |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 181 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 182 | compress(buffer, data_end - data_start, (output->data + sizeof(*stage)), |
| 183 | (int *)&stage->len); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 184 | free(buffer); |
| 185 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 186 | if (*location) |
| 187 | *location -= sizeof(struct cbfs_stage); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 188 | output->size = sizeof(*stage) + stage->len; |
| 189 | return 0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 190 | } |