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 unsigned int idemp(unsigned int x)
37 unsigned int swap32(unsigned int x)
39 return ((x>>24) | ((x>>8) & 0xff00) | ((x<<8) & 0xff0000) | (x<<24));
42 unsigned int (*elf32_to_native)(unsigned int)=idemp;
44 int parse_elf(unsigned char *input, unsigned char **output,
45 int mode, void (*compress) (char *, int, char *, int *))
48 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
49 char *header, *buffer;
54 struct cbfs_stage *stage;
55 unsigned int data_start, data_end, mem_end;
57 int elf_bigendian = 0;
58 int host_bigendian = 0;
59 if (ehdr->e_ident[EI_DATA]==ELFDATA2MSB) {
62 if ((unsigned int)"1234"==0x31323334) {
65 if (elf_bigendian != host_bigendian) {
66 elf32_to_native = swap32;
69 headers = ehdr->e_phnum;
70 header = (char *)ehdr;
72 phdr = (Elf32_Phdr *) & header[elf32_to_native(ehdr->e_phoff)];
74 /* Now, regular headers - we only care about PT_LOAD headers,
75 * because thats what we're actually going to load
78 data_start = 0xFFFFFFFF;
82 for (i = 0; i < headers; i++) {
83 unsigned int start, mend, rend;
85 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
88 /* Empty segments are never interesting */
89 if (elf32_to_native(phdr[i].p_memsz) == 0)
94 start = elf32_to_native(phdr[i].p_paddr);
96 mend = start + elf32_to_native(phdr[i].p_memsz);
97 rend = start + elf32_to_native(phdr[i].p_filesz);
99 if (start < data_start)
109 /* allocate an intermediate buffer for the data */
110 buffer = calloc(data_end - data_start, 1);
112 if (buffer == NULL) {
113 fprintf(stderr, "E: Unable to allocate memory: %m\n");
117 /* Copy the file data into the buffer */
119 for (i = 0; i < headers; i++) {
121 if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
124 if (elf32_to_native(phdr[i].p_memsz) == 0)
127 memcpy(buffer + (elf32_to_native(phdr[i].p_paddr) - data_start),
128 &header[elf32_to_native(phdr[i].p_offset)], elf32_to_native(phdr[i].p_filesz));
131 /* Now make the output buffer */
132 out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1);
135 fprintf(stderr, "E: Unable to allocate memory: %m\n");
139 stage = (struct cbfs_stage *)out;
141 stage->load = data_start;
142 stage->memlen = mem_end - data_start;
143 stage->compression = mode;
144 stage->entry = ehdr->e_entry;
146 compress(buffer, data_end - data_start,
147 (char *)(out + sizeof(struct cbfs_stage)),
152 return sizeof(struct cbfs_stage) + stage->len;
155 int main(int argc, char **argv)
157 void (*compress) (char *, int, char *, int *);
158 int algo = CBFS_COMPRESS_LZMA;
163 unsigned char *buffer, *obuffer;
168 static struct option longopt[] = {
169 {"output", 1, 0, 'o'},
171 {"nocompress", 0, 0, 'n'},
174 signed char ch = getopt_long(argc, argv, "o:ln",
175 longopt, &option_index);
185 algo = CBFS_COMPRESS_LZMA;
188 algo = CBFS_COMPRESS_NONE;
197 input = argv[optind];
199 if (input == NULL || !strcmp(input, "-"))
200 buffer = file_read_to_buffer(STDIN_FILENO, &size);
202 buffer = file_read(input, &size);
204 if (!iself(buffer)) {
205 fprintf(stderr, "E: The incoming file is not an ELF\n");
210 case CBFS_COMPRESS_NONE:
211 compress = none_compress;
213 case CBFS_COMPRESS_LZMA:
214 compress = lzma_compress;
218 osize = parse_elf(buffer, &obuffer, algo, compress);
221 fprintf(stderr, "E: Error while converting the ELF\n");
225 if (output == NULL || !strcmp(output, "-"))
226 file_write_from_buffer(STDOUT_FILENO, obuffer, osize);
228 file_write(output, obuffer, osize);