4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5 * 2009 coresystems GmbH
6 * written by Patrick Georgi <patrick.georgi@coresystems.de>
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.
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.
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
34 unsigned int idemp(unsigned int x)
39 unsigned int swap32(unsigned int x)
41 return ((x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) |
45 unsigned int (*elf32_to_native) (unsigned int) = idemp;
47 /* returns size of result, or -1 if error */
48 int parse_elf_to_stage(unsigned char *input, unsigned char **output,
49 comp_algo algo, uint32_t * location)
52 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
53 char *header, *buffer;
58 struct cbfs_stage *stage;
59 unsigned int data_start, data_end, mem_end;
61 int elf_bigendian = 0;
62 int host_bigendian = 0;
64 comp_func_ptr compress = compression_function(algo);
69 fprintf(stderr, "E: The incoming file is not an ELF\n");
73 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
76 char test[4] = "1234";
77 uint32_t inttest = *(uint32_t *) test;
78 if (inttest == 0x31323334) {
81 if (elf_bigendian != host_bigendian) {
82 elf32_to_native = swap32;
85 headers = ehdr->e_phnum;
86 header = (char *)ehdr;
88 phdr = (Elf32_Phdr *) & header[elf32_to_native(ehdr->e_phoff)];
90 /* Now, regular headers - we only care about PT_LOAD headers,
91 * because thats what we're actually going to load
94 data_start = 0xFFFFFFFF;
98 for (i = 0; i < headers; i++) {
99 unsigned int start, mend, rend;
101 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
104 /* Empty segments are never interesting */
105 if (elf32_to_native(phdr[i].p_memsz) == 0)
110 start = elf32_to_native(phdr[i].p_paddr);
112 mend = start + elf32_to_native(phdr[i].p_memsz);
113 rend = start + elf32_to_native(phdr[i].p_filesz);
115 if (start < data_start)
125 if (data_start < *location) {
126 data_start = *location;
129 /* allocate an intermediate buffer for the data */
130 buffer = calloc(data_end - data_start, 1);
132 if (buffer == NULL) {
133 fprintf(stderr, "E: Unable to allocate memory: %m\n");
137 /* Copy the file data into the buffer */
139 for (i = 0; i < headers; i++) {
140 unsigned int l_start, l_offset = 0;
142 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
145 if (elf32_to_native(phdr[i].p_memsz) == 0)
148 l_start = elf32_to_native(phdr[i].p_paddr);
149 if (l_start < *location) {
150 l_offset = *location - l_start;
154 memcpy(buffer + (l_start - data_start),
155 &header[elf32_to_native(phdr[i].p_offset)+l_offset],
156 elf32_to_native(phdr[i].p_filesz)-l_offset);
159 /* Now make the output buffer */
160 out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1);
163 fprintf(stderr, "E: Unable to allocate memory: %m\n");
167 stage = (struct cbfs_stage *)out;
169 stage->load = data_start;
170 stage->memlen = mem_end - data_start;
171 stage->compression = algo;
172 stage->entry = ehdr->e_entry;
174 compress(buffer, data_end - data_start,
175 (char *)(out + sizeof(struct cbfs_stage)), (int *)&stage->len);
180 *location -= sizeof(struct cbfs_stage);
181 return sizeof(struct cbfs_stage) + stage->len;