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 int parse_elf_to_payload(unsigned char *input, unsigned char **output,
45 int isize = 0, osize = 0;
47 struct cbfs_payload_segment *segs;
51 printf("Fatal error: the payload file is not in ELF format!\n");
56 comp_func_ptr compress = compression_function(algo);
60 ehdr = (Elf32_Ehdr *) input;
61 headers = ehdr->e_phnum;
62 header = (char *)ehdr;
64 phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]);
65 shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]);
67 strtab = &header[shdr[ehdr->e_shstrndx].sh_offset];
69 /* Count the number of headers - look for the .notes.pinfo
72 for (i = 0; i < ehdr->e_shnum; i++) {
75 if (i == ehdr->e_shstrndx)
78 if (shdr[i].sh_size == 0)
81 name = (char *)(strtab + shdr[i].sh_name);
83 if (!strcmp(name, ".note.pinfo")) {
85 isize += (unsigned int)shdr[i].sh_size;
89 /* Now, regular headers - we only care about PT_LOAD headers,
90 * because thats what we're actually going to load
93 for (i = 0; i < headers; i++) {
94 if (phdr[i].p_type != PT_LOAD)
97 /* Empty segments are never interesting */
98 if (phdr[i].p_memsz == 0)
101 isize += phdr[i].p_filesz;
106 /* Allocate a block of memory to store the data in */
109 calloc((segments * sizeof(struct cbfs_payload_segment)) + isize, 1);
110 doffset = (segments * sizeof(struct cbfs_payload_segment));
115 segs = (struct cbfs_payload_segment *)sptr;
118 for (i = 0; i < ehdr->e_shnum; i++) {
121 if (i == ehdr->e_shstrndx)
124 if (shdr[i].sh_size == 0)
127 name = (char *)(strtab + shdr[i].sh_name);
129 if (!strcmp(name, ".note.pinfo")) {
130 segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
131 segs[segments].load_addr = 0;
132 segs[segments].len = (unsigned int)shdr[i].sh_size;
133 segs[segments].offset = doffset;
135 memcpy((unsigned long *)(sptr + doffset),
136 &header[shdr[i].sh_offset], shdr[i].sh_size);
138 doffset += segs[segments].len;
139 osize += segs[segments].len;
145 for (i = 0; i < headers; i++) {
146 if (phdr[i].p_type != PT_LOAD)
149 if (phdr[i].p_memsz == 0)
152 if (phdr[i].p_filesz == 0) {
153 segs[segments].type = PAYLOAD_SEGMENT_BSS;
154 segs[segments].load_addr =
155 (uint64_t)htonll(phdr[i].p_paddr);
156 segs[segments].mem_len =
157 (uint32_t)htonl(phdr[i].p_memsz);
158 segs[segments].offset = htonl(doffset);
164 if (phdr[i].p_flags & PF_X)
165 segs[segments].type = PAYLOAD_SEGMENT_CODE;
167 segs[segments].type = PAYLOAD_SEGMENT_DATA;
168 segs[segments].load_addr = (uint64_t)htonll(phdr[i].p_paddr);
169 segs[segments].mem_len = (uint32_t)htonl(phdr[i].p_memsz);
170 segs[segments].compression = htonl(algo);
171 segs[segments].offset = htonl(doffset);
174 compress((char *)&header[phdr[i].p_offset],
175 phdr[i].p_filesz, (char *)(sptr + doffset), &len);
176 segs[segments].len = htonl(len);
178 /* If the compressed section is larger, then use the
181 if ((unsigned int)len > phdr[i].p_filesz) {
182 segs[segments].compression = 0;
183 segs[segments].len = htonl(phdr[i].p_filesz);
185 memcpy((char *)(sptr + doffset),
186 &header[phdr[i].p_offset], phdr[i].p_filesz);
189 doffset += ntohl(segs[segments].len);
190 osize += ntohl(segs[segments].len);
195 segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
196 segs[segments++].load_addr = (uint64_t)htonll(ehdr->e_entry);
200 return (segments * sizeof(struct cbfs_payload_segment)) + osize;