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() */
21 /* macros for conversion between host and (internet) network byte order */
23 #include <netinet/in.h> /* Consts and structs defined by the internet system */
24 #define BINARY_FILE_OPTS
27 #define BINARY_FILE_OPTS "b"
31 #include <libiberty.h>
33 /* from uClinux-x.x.x/include/linux */
34 #include "flat.h" /* Binary flat header description */
36 #if defined(__MINGW32__)
39 #define mkstemp(p) mktemp(p)
43 #if defined TARGET_bfin
44 # define flat_get_relocate_addr(addr) (addr & 0x03ffffff)
46 # define flat_get_relocate_addr(addr) (addr)
49 /****************************************************************************/
53 static int print = 0, print_relocs = 0, docompress = 0, ramload = 0,
54 stacksize = 0, ktrace = 0, l1stack = 0;
56 /****************************************************************************/
59 process_file(char *ifile, char *ofile)
61 int old_flags, old_stack, new_flags, new_stack;
63 struct flat_hdr old_hdr, new_hdr;
64 char *tfile, tmpbuf[256];
65 int input_error, output_error;
69 if (fopen_stream_u(&ifp, ifile, "r" BINARY_FILE_OPTS)) {
70 fprintf(stderr, "Cannot open %s\n", ifile);
74 if (fread_stream(&old_hdr, sizeof(old_hdr), 1, &ifp) != 1) {
75 fprintf(stderr, "Cannot read header of %s\n", ifile);
80 if (strncmp(old_hdr.magic, "bFLT", 4) != 0) {
81 fprintf(stderr, "Cannot read header of %s\n", ifile);
86 new_flags = old_flags = ntohl(old_hdr.flags);
87 new_stack = old_stack = ntohl(old_hdr.stack_size);
90 if (docompress == 1) {
91 new_flags |= FLAT_FLAG_GZIP;
92 new_flags &= ~FLAT_FLAG_GZDATA;
93 } else if (docompress == 2) {
94 new_flags |= FLAT_FLAG_GZDATA;
95 new_flags &= ~FLAT_FLAG_GZIP;
96 } else if (docompress < 0)
97 new_flags &= ~(FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA);
100 new_flags |= FLAT_FLAG_RAM;
101 else if (ramload < 0)
102 new_flags &= ~FLAT_FLAG_RAM;
105 new_flags |= FLAT_FLAG_KTRACE;
107 new_flags &= ~FLAT_FLAG_KTRACE;
110 new_flags |= FLAT_FLAG_L1STK;
111 else if (l1stack < 0)
112 new_flags &= ~FLAT_FLAG_L1STK;
115 new_stack = stacksize;
119 uint32_t reloc_count, reloc_start;
121 printf("%s\n", ifile);
122 printf(" Magic: %4.4s\n", old_hdr.magic);
123 printf(" Rev: %d\n", ntohl(old_hdr.rev));
124 t = (time_t) htonl(old_hdr.build_date);
125 printf(" Build Date: %s", t?ctime(&t):"not specified\n");
126 printf(" Entry: 0x%x\n", ntohl(old_hdr.entry));
127 printf(" Data Start: 0x%x\n", ntohl(old_hdr.data_start));
128 printf(" Data End: 0x%x\n", ntohl(old_hdr.data_end));
129 printf(" BSS End: 0x%x\n", ntohl(old_hdr.bss_end));
130 printf(" Stack Size: 0x%x\n", ntohl(old_hdr.stack_size));
131 reloc_start = ntohl(old_hdr.reloc_start);
132 printf(" Reloc Start: 0x%x\n", reloc_start);
133 reloc_count = ntohl(old_hdr.reloc_count);
134 printf(" Reloc Count: 0x%x\n", reloc_count);
135 printf(" Flags: 0x%x ( ", ntohl(old_hdr.flags));
137 if (old_flags & FLAT_FLAG_RAM)
138 printf("Load-to-Ram ");
139 if (old_flags & FLAT_FLAG_GOTPIC)
140 printf("Has-PIC-GOT ");
141 if (old_flags & FLAT_FLAG_GZIP)
142 printf("Gzip-Compressed ");
143 if (old_flags & FLAT_FLAG_GZDATA)
144 printf("Gzip-Data-Compressed ");
145 if (old_flags & FLAT_FLAG_KTRACE)
146 printf("Kernel-Traced-Load ");
147 if (old_flags & FLAT_FLAG_L1STK)
148 printf("L1-Scratch-Stack ");
153 uint32_t *relocs = xcalloc(reloc_count, sizeof(uint32_t));
157 printf(" Relocs:\n");
158 printf(" #\treloc ( address )\tdata\n");
160 if (old_flags & FLAT_FLAG_GZIP)
161 reopen_stream_compressed(&ifp);
162 if (fseek_stream(&ifp, reloc_start, SEEK_SET)) {
163 fprintf(stderr, "Cannot seek to relocs of %s\n", ifile);
167 if (fread_stream(relocs, sizeof(uint32_t), reloc_count, &ifp) == -1) {
168 fprintf(stderr, "Cannot read relocs of %s\n", ifile);
173 for (i = 0; i < reloc_count; ++i) {
174 uint32_t raddr, addr;
175 r = ntohl(relocs[i]);
176 raddr = flat_get_relocate_addr(r);
177 printf(" %u\t0x%08lx (0x%08"PRIx32")\t", i, r, raddr);
178 fseek_stream(&ifp, sizeof(old_hdr) + raddr, SEEK_SET);
179 fread_stream(&addr, sizeof(addr), 1, &ifp);
180 printf("%"PRIx32"\n", addr);
183 /* reset file position for below */
184 fseek_stream(&ifp, sizeof(old_hdr), SEEK_SET);
186 } else if (print > 1) {
187 static int first = 1;
188 unsigned int text, data, bss, stk, rel, tot;
191 printf("Flag Rev Text Data BSS Stack Relocs RAM Filename\n");
192 printf("-----------------------------------------------------------\n");
196 strcat(tmpbuf, (old_flags & FLAT_FLAG_KTRACE) ? "k" : "");
197 strcat(tmpbuf, (old_flags & FLAT_FLAG_RAM) ? "r" : "");
198 strcat(tmpbuf, (old_flags & FLAT_FLAG_GOTPIC) ? "p" : "");
199 strcat(tmpbuf, (old_flags & FLAT_FLAG_GZIP) ? "z" :
200 ((old_flags & FLAT_FLAG_GZDATA) ? "d" : ""));
201 printf("-%-3.3s ", tmpbuf);
202 printf("%3d ", ntohl(old_hdr.rev));
203 printf("%6d ", text=ntohl(old_hdr.data_start)-sizeof(struct flat_hdr));
204 printf("%6d ", data=ntohl(old_hdr.data_end)-ntohl(old_hdr.data_start));
205 printf("%6d ", bss=ntohl(old_hdr.bss_end)-ntohl(old_hdr.data_end));
206 printf("%6d ", stk=ntohl(old_hdr.stack_size));
207 printf("%6d ", rel=ntohl(old_hdr.reloc_count) * 4);
209 * work out how much RAM is needed per invocation, this
210 * calculation is dependent on the binfmt_flat implementation
212 tot = data; /* always need data */
214 if (old_flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))
215 tot += text + sizeof(struct flat_hdr);
217 if (bss + stk > rel) /* which is bigger ? */
224 * the total depends on whether the relocs are smaller/bigger than
227 printf("%s\n", ifile);
230 /* if there is nothing else to do, leave */
231 if (new_flags == old_flags && new_stack == old_stack) {
236 new_hdr.flags = htonl(new_flags);
237 new_hdr.stack_size = htonl(new_stack);
239 tfile = make_temp_file("flthdr");
241 if (fopen_stream_u(&ofp, tfile, "w" BINARY_FILE_OPTS)) {
242 fprintf(stderr, "Failed to open %s for writing\n", tfile);
247 /* Copy header (always uncompressed). */
248 if (fwrite_stream(&new_hdr, sizeof(new_hdr), 1, &ofp) != 1) {
249 fprintf(stderr, "Failed to write to %s\n", tfile);
254 /* Whole input file (including text) is compressed: start decompressing
256 if (old_flags & FLAT_FLAG_GZIP)
257 reopen_stream_compressed(&ifp);
259 /* Likewise, output file is compressed. Start compressing now. */
260 if (new_flags & FLAT_FLAG_GZIP) {
261 printf("zflat %s --> %s\n", ifile, ofile);
262 reopen_stream_compressed(&ofp);
266 ntohl(old_hdr.data_start) - sizeof(struct flat_hdr));
268 /* Only data and relocs were compressed in input. Start decompressing
270 if (old_flags & FLAT_FLAG_GZDATA)
271 reopen_stream_compressed(&ifp);
273 /* Only data/relocs to be compressed in output. Start compressing
275 if (new_flags & FLAT_FLAG_GZDATA) {
276 printf("zflat-data %s --> %s\n", ifile, ofile);
277 reopen_stream_compressed(&ofp);
280 transfer(&ifp, &ofp, -1);
282 input_error = ferror_stream(&ifp);
283 output_error = ferror_stream(&ofp);
285 if (input_error || output_error) {
286 fprintf(stderr, "Error on file pointer%s%s\n",
287 input_error ? " input" : "",
288 output_error ? " output" : "");
296 /* Copy temporary file to output location. */
297 fopen_stream_u(&ifp, tfile, "r" BINARY_FILE_OPTS);
298 fopen_stream_u(&ofp, ofile, "w" BINARY_FILE_OPTS);
300 transfer(&ifp, &ofp, -1);
309 /****************************************************************************/
315 fprintf(stderr, "%s\n", s);
316 fprintf(stderr, "usage: %s [options] flat-file\n", program_name);
317 fprintf(stderr, " Allows you to change an existing flat file\n\n");
318 fprintf(stderr, " -p : print current settings\n");
319 fprintf(stderr, " -P : print relocations\n");
320 fprintf(stderr, " -z : compressed flat file\n");
321 fprintf(stderr, " -d : compressed data-only flat file\n");
322 fprintf(stderr, " -Z : un-compressed flat file\n");
323 fprintf(stderr, " -r : ram load\n");
324 fprintf(stderr, " -R : do not RAM load\n");
325 fprintf(stderr, " -k : kernel traced load (for debug)\n");
326 fprintf(stderr, " -K : normal non-kernel traced load\n");
327 fprintf(stderr, " -u : place stack in L1 scratchpad memory\n");
328 fprintf(stderr, " -U : place stack in normal SDRAM memory\n");
329 fprintf(stderr, " -s size : stack size\n");
330 fprintf(stderr, " -o file : output-file\n"
331 " (default is to modify input file)\n");
335 /****************************************************************************/
338 main(int argc, char *argv[])
341 char *ofile = NULL, *ifile;
343 program_name = argv[0];
345 while ((c = getopt(argc, argv, "pPdzZrRuUkKs:o:")) != EOF) {
347 case 'p': print = 1; break;
348 case 'P': print_relocs = 1; break;
349 case 'z': docompress = 1; break;
350 case 'd': docompress = 2; break;
351 case 'Z': docompress = -1; break;
352 case 'r': ramload = 1; break;
353 case 'R': ramload = -1; break;
354 case 'k': ktrace = 1; break;
355 case 'K': ktrace = -1; break;
356 case 'u': l1stack = 1; break;
357 case 'U': l1stack = -1; break;
358 case 'o': ofile = optarg; break;
360 if (sscanf(optarg, "%i", &stacksize) != 1)
361 usage("invalid stack size");
364 usage("invalid option");
370 usage("No input files provided");
372 if (ofile && argc - optind > 1)
373 usage("-o can only be used with a single file");
375 if (!print && !docompress && !ramload && !stacksize) /* no args == print */
376 print = argc - optind; /* greater than 1 is short format */
378 for (c = optind; c < argc; c++) {
382 process_file(ifile, ofile);
389 /****************************************************************************/