4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
28 #include <arpa/inet.h>
33 int parse_elf(unsigned char *input, unsigned char **output, int algo,
34 void (*compress) (char *, int, char *, int *))
44 int isize = 0, osize = 0;
46 struct cbfs_payload_segment *segs;
49 ehdr = (Elf32_Ehdr *) input;
50 headers = ehdr->e_phnum;
51 header = (char *)ehdr;
53 phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]);
54 shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]);
56 strtab = &header[shdr[ehdr->e_shstrndx].sh_offset];
58 /* Count the number of headers - look for the .notes.pinfo
61 for (i = 0; i < ehdr->e_shnum; i++) {
64 if (i == ehdr->e_shstrndx)
67 if (shdr[i].sh_size == 0)
70 name = (char *)(strtab + shdr[i].sh_name);
72 if (!strcmp(name, ".note.pinfo"))
76 /* Now, regular headers - we only care about PT_LOAD headers,
77 * because thats what we're actually going to load
80 for (i = 0; i < headers; i++) {
81 if (phdr[i].p_type != PT_LOAD)
84 /* Empty segments are never interesting */
85 if (phdr[i].p_memsz == 0)
88 isize += phdr[i].p_filesz;
93 /* Allocate a block of memory to store the data in */
96 calloc((segments * sizeof(struct cbfs_payload_segment)) + isize,
98 doffset = (segments * sizeof(struct cbfs_payload_segment));
103 segs = (struct cbfs_payload_segment *)sptr;
106 for (i = 0; i < ehdr->e_shnum; i++) {
109 if (i == ehdr->e_shstrndx)
112 if (shdr[i].sh_size == 0)
115 name = (char *)(strtab + shdr[i].sh_name);
117 if (!strcmp(name, ".note.pinfo")) {
118 segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
119 segs[segments].load_addr = 0;
120 segs[segments].len = (unsigned int)shdr[i].sh_size;
121 segs[segments].offset = doffset;
123 memcpy((unsigned long *)(sptr + doffset),
124 &header[shdr[i].sh_offset], shdr[i].sh_size);
126 doffset += segs[segments].len;
127 osize += segs[segments].len;
133 for (i = 0; i < headers; i++) {
134 if (phdr[i].p_type != PT_LOAD)
137 if (phdr[i].p_memsz == 0)
140 if (phdr[i].p_filesz == 0) {
141 segs[segments].type = PAYLOAD_SEGMENT_BSS;
142 segs[segments].load_addr =
143 (unsigned long long)htonl(phdr[i].p_paddr);
144 segs[segments].mem_len = (unsigned int)htonl(phdr[i].p_memsz);
145 segs[segments].offset = htonl(doffset);
151 segs[segments].type = PAYLOAD_SEGMENT_DATA;
152 segs[segments].load_addr = (unsigned int)htonl(phdr[i].p_paddr);
153 segs[segments].mem_len = (unsigned int)htonl(phdr[i].p_memsz);
154 segs[segments].compression = htonl(algo);
155 segs[segments].offset = htonl(doffset);
158 compress((char *)&header[phdr[i].p_offset],
160 (char *)(sptr + doffset), &len);
161 segs[segments].len = htonl(len);
163 /* If the compressed section is larger, then use the
166 if ((unsigned int)len > phdr[i].p_filesz) {
167 segs[segments].compression = 0;
168 segs[segments].len = htonl(phdr[i].p_filesz);
170 memcpy((char *)(sptr + doffset),
171 &header[phdr[i].p_offset], phdr[i].p_filesz);
174 doffset += ntohl(segs[segments].len);
175 osize += ntohl(segs[segments].len);
180 segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
181 segs[segments++].load_addr = (unsigned long long)htonl(ehdr->e_entry);
185 return (segments * sizeof(struct cbfs_payload_segment)) + osize;
191 int main(int argc, char **argv)
193 void (*compress) (char *, int, char *, int *);
199 unsigned char *buffer, *obuffer;
204 static struct option longopt[] = {
205 {"output", 1, 0, 'o'},
207 {"nocompress", 0, 0, 'n'},
210 signed char ch = getopt_long(argc, argv, "o:ln",
211 longopt, &option_index);
221 algo = CBFS_COMPRESS_LZMA;
224 algo = CBFS_COMPRESS_NONE;
233 input = argv[optind];
235 if (input == NULL || !strcmp(input, "-"))
236 buffer = file_read_to_buffer(STDIN_FILENO, &size);
238 printf("Reading from %s\n", input);
239 buffer = file_read(input, &size);
242 if (!iself(buffer)) {
243 fprintf(stderr, "E: This does not appear to be an ELF file\n");
248 case CBFS_COMPRESS_NONE:
249 compress = none_compress;
251 case CBFS_COMPRESS_LZMA:
252 compress = lzma_compress;
256 osize = parse_elf(buffer, &obuffer, algo, compress);
259 fprintf(stderr, "E: Error while converting the payload\n");
263 if (output == NULL || !strcmp(output, "-"))
264 file_write_from_buffer(STDOUT_FILENO, obuffer, osize);
266 file_write(output, obuffer, osize);