Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rom-mkstage |
| 3 | * |
| 4 | * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net> |
| 5 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | #include "elf.h" |
| 25 | #include <fcntl.h> |
| 26 | #include <getopt.h> |
| 27 | #include <sys/stat.h> |
| 28 | |
| 29 | #include "common.h" |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame^] | 30 | #include "../cbfs.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 31 | |
| 32 | int parse_elf(unsigned char *input, unsigned char **output, |
| 33 | int mode, void (*compress) (char *, int, char *, int *)) |
| 34 | { |
| 35 | Elf32_Phdr *phdr; |
| 36 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input; |
| 37 | Elf32_Shdr *shdr; |
| 38 | char *header, *buffer; |
| 39 | unsigned char *out; |
| 40 | |
| 41 | int headers; |
| 42 | int i; |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame^] | 43 | struct cbfs_stage *stage; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 44 | unsigned int data_start, data_end, mem_end; |
| 45 | |
| 46 | headers = ehdr->e_phnum; |
| 47 | header = (char *)ehdr; |
| 48 | |
| 49 | phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]); |
| 50 | shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]); |
| 51 | |
| 52 | /* Now, regular headers - we only care about PT_LOAD headers, |
| 53 | * because thats what we're actually going to load |
| 54 | */ |
| 55 | |
| 56 | data_start = 0xFFFFFFFF; |
| 57 | data_end = 0; |
| 58 | mem_end = 0; |
| 59 | |
| 60 | for (i = 0; i < headers; i++) { |
| 61 | unsigned int start, mend, rend; |
| 62 | |
| 63 | if (phdr[i].p_type != PT_LOAD) |
| 64 | continue; |
| 65 | |
| 66 | /* Empty segments are never interesting */ |
| 67 | if (phdr[i].p_memsz == 0) |
| 68 | continue; |
| 69 | |
| 70 | /* BSS */ |
| 71 | |
| 72 | start = phdr[i].p_paddr; |
| 73 | |
| 74 | mend = start + phdr[i].p_memsz; |
| 75 | rend = start + phdr[i].p_filesz; |
| 76 | |
| 77 | if (start < data_start) |
| 78 | data_start = start; |
| 79 | |
| 80 | if (rend > data_end) |
| 81 | data_end = rend; |
| 82 | |
| 83 | if (mend > mem_end) |
| 84 | mem_end = mend; |
| 85 | } |
| 86 | |
| 87 | /* allocate an intermediate buffer for the data */ |
| 88 | buffer = calloc(data_end - data_start, 1); |
| 89 | |
| 90 | if (buffer == NULL) { |
| 91 | fprintf(stderr, "E: Unable to allocate memory: %m\n"); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | /* Copy the file data into the buffer */ |
| 96 | |
| 97 | for (i = 0; i < headers; i++) { |
| 98 | |
| 99 | if (phdr[i].p_type != PT_LOAD) |
| 100 | continue; |
| 101 | |
| 102 | if (phdr[i].p_memsz == 0) |
| 103 | continue; |
| 104 | |
| 105 | memcpy(buffer + (phdr[i].p_paddr - data_start), |
| 106 | &header[phdr[i].p_offset], phdr[i].p_filesz); |
| 107 | } |
| 108 | |
| 109 | /* Now make the output buffer */ |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame^] | 110 | out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 111 | |
| 112 | if (out == NULL) { |
| 113 | fprintf(stderr, "E: Unable to allocate memory: %m\n"); |
| 114 | return -1; |
| 115 | } |
| 116 | |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame^] | 117 | stage = (struct cbfs_stage *)out; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 118 | |
| 119 | stage->load = data_start; |
| 120 | stage->memlen = mem_end - data_start; |
| 121 | stage->compression = mode; |
| 122 | stage->entry = ehdr->e_entry; |
| 123 | |
| 124 | compress(buffer, data_end - data_start, |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame^] | 125 | (char *)(out + sizeof(struct cbfs_stage)), |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 126 | (int *)&stage->len); |
| 127 | |
| 128 | *output = out; |
| 129 | |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame^] | 130 | return sizeof(struct cbfs_stage) + stage->len; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | int main(int argc, char **argv) |
| 134 | { |
| 135 | void (*compress) (char *, int, char *, int *); |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame^] | 136 | int algo = CBFS_COMPRESS_LZMA; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 137 | |
| 138 | char *output = NULL; |
| 139 | char *input = NULL; |
| 140 | |
| 141 | unsigned char *buffer, *obuffer; |
| 142 | int size, osize; |
| 143 | |
| 144 | while (1) { |
| 145 | int option_index; |
| 146 | static struct option longopt[] = { |
| 147 | {"output", 1, 0, 'o'}, |
| 148 | {"lzma", 0, 0, 'l'}, |
| 149 | {"nocompress", 0, 0, 'n'}, |
| 150 | }; |
| 151 | |
| 152 | signed char ch = getopt_long(argc, argv, "o:ln", |
| 153 | longopt, &option_index); |
| 154 | |
| 155 | if (ch == -1) |
| 156 | break; |
| 157 | |
| 158 | switch (ch) { |
| 159 | case 'o': |
| 160 | output = optarg; |
| 161 | break; |
| 162 | case 'l': |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame^] | 163 | algo = CBFS_COMPRESS_LZMA; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 164 | break; |
| 165 | case 'n': |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame^] | 166 | algo = CBFS_COMPRESS_NONE; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 167 | break; |
| 168 | default: |
| 169 | //usage(); |
| 170 | return -1; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (optind < argc) |
| 175 | input = argv[optind]; |
| 176 | |
| 177 | if (input == NULL || !strcmp(input, "-")) |
| 178 | buffer = file_read_to_buffer(STDIN_FILENO, &size); |
| 179 | else |
| 180 | buffer = file_read(input, &size); |
| 181 | |
| 182 | if (!iself(buffer)) { |
| 183 | fprintf(stderr, "E: The incoming file is not an ELF\n"); |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | switch (algo) { |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame^] | 188 | case CBFS_COMPRESS_NONE: |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 189 | compress = none_compress; |
| 190 | break; |
Peter Stuge | 1d862de | 2009-04-14 00:08:34 +0000 | [diff] [blame^] | 191 | case CBFS_COMPRESS_LZMA: |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 192 | compress = lzma_compress; |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | osize = parse_elf(buffer, &obuffer, algo, compress); |
| 197 | |
| 198 | if (osize == -1) { |
| 199 | fprintf(stderr, "E: Error while converting the ELF\n"); |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | if (output == NULL || !strcmp(output, "-")) |
| 204 | file_write_from_buffer(STDOUT_FILENO, obuffer, osize); |
| 205 | else |
| 206 | file_write(output, obuffer, osize); |
| 207 | |
| 208 | return 0; |
| 209 | } |