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
32 int parse_elf(unsigned char *input, unsigned char **output,
33 int mode, void (*compress) (char *, int, char *, int *))
36 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
38 char *header, *buffer;
43 struct romfs_stage *stage;
44 unsigned int data_start, data_end, mem_end;
46 headers = ehdr->e_phnum;
47 header = (char *)ehdr;
49 phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]);
50 shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]);
52 /* Now, regular headers - we only care about PT_LOAD headers,
53 * because thats what we're actually going to load
56 data_start = 0xFFFFFFFF;
60 for (i = 0; i < headers; i++) {
61 unsigned int start, mend, rend;
63 if (phdr[i].p_type != PT_LOAD)
66 /* Empty segments are never interesting */
67 if (phdr[i].p_memsz == 0)
72 start = phdr[i].p_paddr;
74 mend = start + phdr[i].p_memsz;
75 rend = start + phdr[i].p_filesz;
77 if (start < data_start)
87 /* allocate an intermediate buffer for the data */
88 buffer = calloc(data_end - data_start, 1);
91 fprintf(stderr, "E: Unable to allocate memory: %m\n");
95 /* Copy the file data into the buffer */
97 for (i = 0; i < headers; i++) {
99 if (phdr[i].p_type != PT_LOAD)
102 if (phdr[i].p_memsz == 0)
105 memcpy(buffer + (phdr[i].p_paddr - data_start),
106 &header[phdr[i].p_offset], phdr[i].p_filesz);
109 /* Now make the output buffer */
110 out = calloc(sizeof(struct romfs_stage) + data_end - data_start, 1);
113 fprintf(stderr, "E: Unable to allocate memory: %m\n");
117 stage = (struct romfs_stage *)out;
119 stage->load = data_start;
120 stage->memlen = mem_end - data_start;
121 stage->compression = mode;
122 stage->entry = ehdr->e_entry;
124 compress(buffer, data_end - data_start,
125 (char *)(out + sizeof(struct romfs_stage)),
130 return sizeof(struct romfs_stage) + stage->len;
133 int main(int argc, char **argv)
135 void (*compress) (char *, int, char *, int *);
136 int algo = ROMFS_COMPRESS_LZMA;
141 unsigned char *buffer, *obuffer;
146 static struct option longopt[] = {
147 {"output", 1, 0, 'o'},
149 {"nocompress", 0, 0, 'n'},
152 signed char ch = getopt_long(argc, argv, "o:ln",
153 longopt, &option_index);
163 algo = ROMFS_COMPRESS_LZMA;
166 algo = ROMFS_COMPRESS_NONE;
175 input = argv[optind];
177 if (input == NULL || !strcmp(input, "-"))
178 buffer = file_read_to_buffer(STDIN_FILENO, &size);
180 buffer = file_read(input, &size);
182 if (!iself(buffer)) {
183 fprintf(stderr, "E: The incoming file is not an ELF\n");
188 case ROMFS_COMPRESS_NONE:
189 compress = none_compress;
191 case ROMFS_COMPRESS_LZMA:
192 compress = lzma_compress;
196 osize = parse_elf(buffer, &obuffer, algo, compress);
199 fprintf(stderr, "E: Error while converting the ELF\n");
203 if (output == NULL || !strcmp(output, "-"))
204 file_write_from_buffer(STDOUT_FILENO, obuffer, osize);
206 file_write(output, obuffer, osize);