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
30 #include <arpa/inet.h>
35 int parse_elf_to_payload(unsigned char *input, unsigned char **output,
46 int isize = 0, osize = 0;
48 struct cbfs_payload_segment *segs;
51 comp_func_ptr compress = compression_function(algo);
55 ehdr = (Elf32_Ehdr *) input;
56 headers = ehdr->e_phnum;
57 header = (char *)ehdr;
59 phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]);
60 shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]);
62 strtab = &header[shdr[ehdr->e_shstrndx].sh_offset];
64 /* Count the number of headers - look for the .notes.pinfo
67 for (i = 0; i < ehdr->e_shnum; i++) {
70 if (i == ehdr->e_shstrndx)
73 if (shdr[i].sh_size == 0)
76 name = (char *)(strtab + shdr[i].sh_name);
78 if (!strcmp(name, ".note.pinfo")) {
80 isize += (unsigned int)shdr[i].sh_size;
84 /* Now, regular headers - we only care about PT_LOAD headers,
85 * because thats what we're actually going to load
88 for (i = 0; i < headers; i++) {
89 if (phdr[i].p_type != PT_LOAD)
92 /* Empty segments are never interesting */
93 if (phdr[i].p_memsz == 0)
96 isize += phdr[i].p_filesz;
101 /* Allocate a block of memory to store the data in */
104 calloc((segments * sizeof(struct cbfs_payload_segment)) + isize, 1);
105 doffset = (segments * sizeof(struct cbfs_payload_segment));
110 segs = (struct cbfs_payload_segment *)sptr;
113 for (i = 0; i < ehdr->e_shnum; i++) {
116 if (i == ehdr->e_shstrndx)
119 if (shdr[i].sh_size == 0)
122 name = (char *)(strtab + shdr[i].sh_name);
124 if (!strcmp(name, ".note.pinfo")) {
125 segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
126 segs[segments].load_addr = 0;
127 segs[segments].len = (unsigned int)shdr[i].sh_size;
128 segs[segments].offset = doffset;
130 memcpy((unsigned long *)(sptr + doffset),
131 &header[shdr[i].sh_offset], shdr[i].sh_size);
133 doffset += segs[segments].len;
134 osize += segs[segments].len;
140 for (i = 0; i < headers; i++) {
141 if (phdr[i].p_type != PT_LOAD)
144 if (phdr[i].p_memsz == 0)
147 if (phdr[i].p_filesz == 0) {
148 segs[segments].type = PAYLOAD_SEGMENT_BSS;
149 segs[segments].load_addr =
150 (unsigned long long)htonl(phdr[i].p_paddr);
151 segs[segments].mem_len =
152 (unsigned int)htonl(phdr[i].p_memsz);
153 segs[segments].offset = htonl(doffset);
159 segs[segments].type = PAYLOAD_SEGMENT_DATA;
160 segs[segments].load_addr = (unsigned int)htonl(phdr[i].p_paddr);
161 segs[segments].mem_len = (unsigned int)htonl(phdr[i].p_memsz);
162 segs[segments].compression = htonl(algo);
163 segs[segments].offset = htonl(doffset);
166 compress((char *)&header[phdr[i].p_offset],
167 phdr[i].p_filesz, (char *)(sptr + doffset), &len);
168 segs[segments].len = htonl(len);
170 /* If the compressed section is larger, then use the
173 if ((unsigned int)len > phdr[i].p_filesz) {
174 segs[segments].compression = 0;
175 segs[segments].len = htonl(phdr[i].p_filesz);
177 memcpy((char *)(sptr + doffset),
178 &header[phdr[i].p_offset], phdr[i].p_filesz);
181 doffset += ntohl(segs[segments].len);
182 osize += ntohl(segs[segments].len);
187 segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
188 segs[segments++].load_addr = (unsigned long long)htonl(ehdr->e_entry);
192 return (segments * sizeof(struct cbfs_payload_segment)) + osize;