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