1 /****************************************************************************/
3 * A simple program to manipulate flat files
5 * Copyright (C) 2001-2003 SnapGear Inc, davidm@snapgear.com
6 * Copyright (C) 2001 Lineo, davidm@lineo.com
8 * This is Free Software, under the GNU Public Licence v2 or greater.
11 /****************************************************************************/
13 #include <stdio.h> /* Userland pieces of the ANSI C standard I/O package */
14 #include <unistd.h> /* Userland prototypes of the Unix std system calls */
16 #include <stdlib.h> /* exit() */
17 #include <string.h> /* strcat(), strcpy() */
22 #include <libiberty.h>
25 const char *elf2flt_progname;
27 /* from uClinux-x.x.x/include/linux */
28 #include "flat.h" /* Binary flat header description */
30 #if defined(__MINGW32__)
33 #define mkstemp(p) mktemp(p)
37 #if defined TARGET_bfin
38 # define flat_get_relocate_addr(addr) (addr & 0x03ffffff)
40 # define flat_get_relocate_addr(addr) (addr)
43 /****************************************************************************/
45 static int print = 0, print_relocs = 0, docompress = 0, ramload = 0,
46 stacksize = 0, ktrace = 0, l1stack = 0;
48 /****************************************************************************/
51 process_file(char *ifile, char *ofile)
53 int old_flags, old_stack, new_flags, new_stack;
55 struct flat_hdr old_hdr, new_hdr;
56 char *tfile, tmpbuf[256];
57 int input_error, output_error;
61 if (fopen_stream_u(&ifp, ifile, "r" BINARY_FILE_OPTS)) {
62 fprintf(stderr, "Cannot open %s\n", ifile);
66 if (fread_stream(&old_hdr, sizeof(old_hdr), 1, &ifp) != 1) {
67 fprintf(stderr, "Cannot read header of %s\n", ifile);
72 if (strncmp(old_hdr.magic, "bFLT", 4) != 0) {
73 fprintf(stderr, "Cannot read header of %s\n", ifile);
78 new_flags = old_flags = ntohl(old_hdr.flags);
79 new_stack = old_stack = ntohl(old_hdr.stack_size);
82 if (docompress == 1) {
83 new_flags |= FLAT_FLAG_GZIP;
84 new_flags &= ~FLAT_FLAG_GZDATA;
85 } else if (docompress == 2) {
86 new_flags |= FLAT_FLAG_GZDATA;
87 new_flags &= ~FLAT_FLAG_GZIP;
88 } else if (docompress < 0)
89 new_flags &= ~(FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA);
92 new_flags |= FLAT_FLAG_RAM;
94 new_flags &= ~FLAT_FLAG_RAM;
97 new_flags |= FLAT_FLAG_KTRACE;
99 new_flags &= ~FLAT_FLAG_KTRACE;
102 new_flags |= FLAT_FLAG_L1STK;
103 else if (l1stack < 0)
104 new_flags &= ~FLAT_FLAG_L1STK;
107 new_stack = stacksize;
111 uint32_t reloc_count, reloc_start;
113 printf("%s\n", ifile);
114 printf(" Magic: %4.4s\n", old_hdr.magic);
115 printf(" Rev: %d\n", ntohl(old_hdr.rev));
116 t = (time_t) htonl(old_hdr.build_date);
117 printf(" Build Date: %s", t?ctime(&t):"not specified\n");
118 printf(" Entry: 0x%x\n", ntohl(old_hdr.entry));
119 printf(" Data Start: 0x%x\n", ntohl(old_hdr.data_start));
120 printf(" Data End: 0x%x\n", ntohl(old_hdr.data_end));
121 printf(" BSS End: 0x%x\n", ntohl(old_hdr.bss_end));
122 printf(" Stack Size: 0x%x\n", ntohl(old_hdr.stack_size));
123 reloc_start = ntohl(old_hdr.reloc_start);
124 printf(" Reloc Start: 0x%x\n", reloc_start);
125 reloc_count = ntohl(old_hdr.reloc_count);
126 printf(" Reloc Count: 0x%x\n", reloc_count);
127 printf(" Flags: 0x%x ( ", ntohl(old_hdr.flags));
129 if (old_flags & FLAT_FLAG_RAM)
130 printf("Load-to-Ram ");
131 if (old_flags & FLAT_FLAG_GOTPIC)
132 printf("Has-PIC-GOT ");
133 if (old_flags & FLAT_FLAG_GZIP)
134 printf("Gzip-Compressed ");
135 if (old_flags & FLAT_FLAG_GZDATA)
136 printf("Gzip-Data-Compressed ");
137 if (old_flags & FLAT_FLAG_KTRACE)
138 printf("Kernel-Traced-Load ");
139 if (old_flags & FLAT_FLAG_L1STK)
140 printf("L1-Scratch-Stack ");
145 uint32_t *relocs = xcalloc(reloc_count, sizeof(uint32_t));
149 printf(" Relocs:\n");
150 printf(" #\treloc ( address )\tdata\n");
152 if (old_flags & FLAT_FLAG_GZIP)
153 reopen_stream_compressed(&ifp);
154 if (fseek_stream(&ifp, reloc_start, SEEK_SET)) {
155 fprintf(stderr, "Cannot seek to relocs of %s\n", ifile);
159 if (fread_stream(relocs, sizeof(uint32_t), reloc_count, &ifp) == -1) {
160 fprintf(stderr, "Cannot read relocs of %s\n", ifile);
165 for (i = 0; i < reloc_count; ++i) {
166 uint32_t raddr, addr;
167 r = ntohl(relocs[i]);
168 raddr = flat_get_relocate_addr(r);
169 printf(" %u\t0x%08lx (0x%08"PRIx32")\t", i, r, raddr);
170 fseek_stream(&ifp, sizeof(old_hdr) + raddr, SEEK_SET);
171 fread_stream(&addr, sizeof(addr), 1, &ifp);
172 printf("%"PRIx32"\n", addr);
175 /* reset file position for below */
176 fseek_stream(&ifp, sizeof(old_hdr), SEEK_SET);
178 } else if (print > 1) {
179 static int first = 1;
180 unsigned int text, data, bss, stk, rel, tot;
183 printf("Flag Rev Text Data BSS Stack Relocs RAM Filename\n");
184 printf("-----------------------------------------------------------\n");
188 strcat(tmpbuf, (old_flags & FLAT_FLAG_KTRACE) ? "k" : "");
189 strcat(tmpbuf, (old_flags & FLAT_FLAG_RAM) ? "r" : "");
190 strcat(tmpbuf, (old_flags & FLAT_FLAG_GOTPIC) ? "p" : "");
191 strcat(tmpbuf, (old_flags & FLAT_FLAG_GZIP) ? "z" :
192 ((old_flags & FLAT_FLAG_GZDATA) ? "d" : ""));
193 printf("-%-3.3s ", tmpbuf);
194 printf("%3d ", ntohl(old_hdr.rev));
195 printf("%6d ", text=ntohl(old_hdr.data_start)-sizeof(struct flat_hdr));
196 printf("%6d ", data=ntohl(old_hdr.data_end)-ntohl(old_hdr.data_start));
197 printf("%6d ", bss=ntohl(old_hdr.bss_end)-ntohl(old_hdr.data_end));
198 printf("%6d ", stk=ntohl(old_hdr.stack_size));
199 printf("%6d ", rel=ntohl(old_hdr.reloc_count) * 4);
201 * work out how much RAM is needed per invocation, this
202 * calculation is dependent on the binfmt_flat implementation
204 tot = data; /* always need data */
206 if (old_flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))
207 tot += text + sizeof(struct flat_hdr);
209 if (bss + stk > rel) /* which is bigger ? */
216 * the total depends on whether the relocs are smaller/bigger than
219 printf("%s\n", ifile);
222 /* if there is nothing else to do, leave */
223 if (new_flags == old_flags && new_stack == old_stack) {
228 new_hdr.flags = htonl(new_flags);
229 new_hdr.stack_size = htonl(new_stack);
231 tfile = make_temp_file("flthdr");
233 if (fopen_stream_u(&ofp, tfile, "w" BINARY_FILE_OPTS)) {
235 fatal("Failed to open %s for writing\n", tfile);
238 /* Copy header (always uncompressed). */
239 if (fwrite_stream(&new_hdr, sizeof(new_hdr), 1, &ofp) != 1) {
241 fatal("Failed to write to %s\n", tfile);
244 /* Whole input file (including text) is compressed: start decompressing
246 if (old_flags & FLAT_FLAG_GZIP)
247 reopen_stream_compressed(&ifp);
249 /* Likewise, output file is compressed. Start compressing now. */
250 if (new_flags & FLAT_FLAG_GZIP) {
251 printf("zflat %s --> %s\n", ifile, ofile);
252 reopen_stream_compressed(&ofp);
256 ntohl(old_hdr.data_start) - sizeof(struct flat_hdr));
258 /* Only data and relocs were compressed in input. Start decompressing
260 if (old_flags & FLAT_FLAG_GZDATA)
261 reopen_stream_compressed(&ifp);
263 /* Only data/relocs to be compressed in output. Start compressing
265 if (new_flags & FLAT_FLAG_GZDATA) {
266 printf("zflat-data %s --> %s\n", ifile, ofile);
267 reopen_stream_compressed(&ofp);
270 transfer(&ifp, &ofp, -1);
272 input_error = ferror_stream(&ifp);
273 output_error = ferror_stream(&ofp);
275 if (input_error || output_error) {
277 fatal("Error on file pointer%s%s\n",
278 input_error ? " input" : "",
279 output_error ? " output" : "");
285 /* Copy temporary file to output location. */
286 fopen_stream_u(&ifp, tfile, "r" BINARY_FILE_OPTS);
287 fopen_stream_u(&ofp, ofile, "w" BINARY_FILE_OPTS);
289 transfer(&ifp, &ofp, -1);
298 /****************************************************************************/
304 fprintf(stderr, "%s\n", s);
305 fprintf(stderr, "usage: %s [options] flat-file\n", elf2flt_progname);
306 fprintf(stderr, " Allows you to change an existing flat file\n\n");
307 fprintf(stderr, " -p : print current settings\n");
308 fprintf(stderr, " -P : print relocations\n");
309 fprintf(stderr, " -z : compressed flat file\n");
310 fprintf(stderr, " -d : compressed data-only flat file\n");
311 fprintf(stderr, " -Z : un-compressed flat file\n");
312 fprintf(stderr, " -r : ram load\n");
313 fprintf(stderr, " -R : do not RAM load\n");
314 fprintf(stderr, " -k : kernel traced load (for debug)\n");
315 fprintf(stderr, " -K : normal non-kernel traced load\n");
316 fprintf(stderr, " -u : place stack in L1 scratchpad memory\n");
317 fprintf(stderr, " -U : place stack in normal SDRAM memory\n");
318 fprintf(stderr, " -s size : stack size\n");
319 fprintf(stderr, " -o file : output-file\n"
320 " (default is to modify input file)\n");
324 /****************************************************************************/
327 main(int argc, char *argv[])
330 char *ofile = NULL, *ifile;
332 elf2flt_progname = argv[0];
335 while ((c = getopt(argc, argv, "pPdzZrRuUkKs:o:")) != EOF) {
337 case 'p': print = 1; break;
338 case 'P': print_relocs = 1; break;
339 case 'z': docompress = 1; break;
340 case 'd': docompress = 2; break;
341 case 'Z': docompress = -1; break;
342 case 'r': ramload = 1; break;
343 case 'R': ramload = -1; break;
344 case 'k': ktrace = 1; break;
345 case 'K': ktrace = -1; break;
346 case 'u': l1stack = 1; break;
347 case 'U': l1stack = -1; break;
348 case 'o': ofile = optarg; break;
350 if (sscanf(optarg, "%i", &stacksize) != 1)
351 usage("invalid stack size");
354 usage("invalid option");
361 usage("No input files provided");
363 if (ofile && argc - optind > 1)
364 usage("-o can only be used with a single file");
366 if (!print && noargs) /* no args == print */
367 print = argc - optind; /* greater than 1 is short format */
369 for (c = optind; c < argc; c++) {
373 process_file(ifile, ofile);
380 /****************************************************************************/