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;
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_stack = stacksize;
114 uint32_t reloc_count, reloc_start;
116 printf("%s\n", ifile);
117 printf(" Magic: %4.4s\n", old_hdr.magic);
118 printf(" Rev: %d\n", ntohl(old_hdr.rev));
119 t = (time_t) htonl(old_hdr.build_date);
120 printf(" Build Date: %s", t?ctime(&t):"not specified\n");
121 printf(" Entry: 0x%x\n", ntohl(old_hdr.entry));
122 printf(" Data Start: 0x%x\n", ntohl(old_hdr.data_start));
123 printf(" Data End: 0x%x\n", ntohl(old_hdr.data_end));
124 printf(" BSS End: 0x%x\n", ntohl(old_hdr.bss_end));
125 printf(" Stack Size: 0x%x\n", ntohl(old_hdr.stack_size));
126 reloc_start = ntohl(old_hdr.reloc_start);
127 printf(" Reloc Start: 0x%x\n", reloc_start);
128 reloc_count = ntohl(old_hdr.reloc_count);
129 printf(" Reloc Count: 0x%x\n", reloc_count);
130 printf(" Flags: 0x%x ( ", ntohl(old_hdr.flags));
132 if (old_flags & FLAT_FLAG_RAM)
133 printf("Load-to-Ram ");
134 if (old_flags & FLAT_FLAG_GOTPIC)
135 printf("Has-PIC-GOT ");
136 if (old_flags & FLAT_FLAG_GZIP)
137 printf("Gzip-Compressed ");
138 if (old_flags & FLAT_FLAG_GZDATA)
139 printf("Gzip-Data-Compressed ");
140 if (old_flags & FLAT_FLAG_KTRACE)
141 printf("Kernel-Traced-Load ");
146 uint32_t *relocs = xcalloc(reloc_count, sizeof(uint32_t));
150 printf(" Relocs:\n");
151 printf(" #\treloc ( address )\tdata\n");
153 if (old_flags & FLAT_FLAG_GZIP)
154 reopen_stream_compressed(&ifp);
155 if (fseek_stream(&ifp, reloc_start, SEEK_SET)) {
156 fprintf(stderr, "Cannot seek to relocs of %s\n", ifile);
160 if (fread_stream(relocs, sizeof(uint32_t), reloc_count, &ifp) == -1) {
161 fprintf(stderr, "Cannot read relocs of %s\n", ifile);
166 for (i = 0; i < reloc_count; ++i) {
167 uint32_t raddr, addr;
168 r = ntohl(relocs[i]);
169 raddr = flat_get_relocate_addr(r);
170 printf(" %u\t0x%08lx (0x%08"PRIx32")\t", i, r, raddr);
171 fseek_stream(&ifp, sizeof(old_hdr) + raddr, SEEK_SET);
172 fread_stream(&addr, sizeof(addr), 1, &ifp);
173 printf("%"PRIx32"\n", addr);
176 /* reset file position for below */
177 fseek_stream(&ifp, sizeof(old_hdr), SEEK_SET);
179 } else if (print > 1) {
180 static int first = 1;
181 unsigned int text, data, bss, stk, rel, tot;
184 printf("Flag Rev Text Data BSS Stack Relocs RAM Filename\n");
185 printf("-----------------------------------------------------------\n");
189 strcat(tmpbuf, (old_flags & FLAT_FLAG_KTRACE) ? "k" : "");
190 strcat(tmpbuf, (old_flags & FLAT_FLAG_RAM) ? "r" : "");
191 strcat(tmpbuf, (old_flags & FLAT_FLAG_GOTPIC) ? "p" : "");
192 strcat(tmpbuf, (old_flags & FLAT_FLAG_GZIP) ? "z" :
193 ((old_flags & FLAT_FLAG_GZDATA) ? "d" : ""));
194 printf("-%-3.3s ", tmpbuf);
195 printf("%3d ", ntohl(old_hdr.rev));
196 printf("%6d ", text=ntohl(old_hdr.data_start)-sizeof(struct flat_hdr));
197 printf("%6d ", data=ntohl(old_hdr.data_end)-ntohl(old_hdr.data_start));
198 printf("%6d ", bss=ntohl(old_hdr.bss_end)-ntohl(old_hdr.data_end));
199 printf("%6d ", stk=ntohl(old_hdr.stack_size));
200 printf("%6d ", rel=ntohl(old_hdr.reloc_count) * 4);
202 * work out how much RAM is needed per invocation, this
203 * calculation is dependent on the binfmt_flat implementation
205 tot = data; /* always need data */
207 if (old_flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))
208 tot += text + sizeof(struct flat_hdr);
210 if (bss + stk > rel) /* which is bigger ? */
217 * the total depends on whether the relocs are smaller/bigger than
220 printf("%s\n", ifile);
223 /* if there is nothing else to do, leave */
224 if (new_flags == old_flags && new_stack == old_stack) {
229 new_hdr.flags = htonl(new_flags);
230 new_hdr.stack_size = htonl(new_stack);
232 tfile = make_temp_file("flthdr");
234 if (fopen_stream_u(&ofp, tfile, "w" BINARY_FILE_OPTS)) {
235 fprintf(stderr, "Failed to open %s for writing\n", tfile);
240 /* Copy header (always uncompressed). */
241 if (fwrite_stream(&new_hdr, sizeof(new_hdr), 1, &ofp) != 1) {
242 fprintf(stderr, "Failed to write to %s\n", tfile);
247 /* Whole input file (including text) is compressed: start decompressing
249 if (old_flags & FLAT_FLAG_GZIP)
250 reopen_stream_compressed(&ifp);
252 /* Likewise, output file is compressed. Start compressing now. */
253 if (new_flags & FLAT_FLAG_GZIP) {
254 printf("zflat %s --> %s\n", ifile, ofile);
255 reopen_stream_compressed(&ofp);
259 ntohl(old_hdr.data_start) - sizeof(struct flat_hdr));
261 /* Only data and relocs were compressed in input. Start decompressing
263 if (old_flags & FLAT_FLAG_GZDATA)
264 reopen_stream_compressed(&ifp);
266 /* Only data/relocs to be compressed in output. Start compressing
268 if (new_flags & FLAT_FLAG_GZDATA) {
269 printf("zflat-data %s --> %s\n", ifile, ofile);
270 reopen_stream_compressed(&ofp);
273 transfer(&ifp, &ofp, -1);
275 input_error = ferror_stream(&ifp);
276 output_error = ferror_stream(&ofp);
278 if (input_error || output_error) {
279 fprintf(stderr, "Error on file pointer%s%s\n",
280 input_error ? " input" : "",
281 output_error ? " output" : "");
289 /* Copy temporary file to output location. */
290 fopen_stream_u(&ifp, tfile, "r" BINARY_FILE_OPTS);
291 fopen_stream_u(&ofp, ofile, "w" BINARY_FILE_OPTS);
293 transfer(&ifp, &ofp, -1);
302 /****************************************************************************/
308 fprintf(stderr, "%s\n", s);
309 fprintf(stderr, "usage: %s [options] flat-file\n", program_name);
310 fprintf(stderr, " Allows you to change an existing flat file\n\n");
311 fprintf(stderr, " -p : print current settings\n");
312 fprintf(stderr, " -P : print relocations\n");
313 fprintf(stderr, " -z : compressed flat file\n");
314 fprintf(stderr, " -d : compressed data-only flat file\n");
315 fprintf(stderr, " -Z : un-compressed flat file\n");
316 fprintf(stderr, " -r : ram load\n");
317 fprintf(stderr, " -R : do not RAM load\n");
318 fprintf(stderr, " -k : kernel traced load (for debug)\n");
319 fprintf(stderr, " -K : normal non-kernel traced load\n");
320 fprintf(stderr, " -s size : stack size\n");
321 fprintf(stderr, " -o file : output-file\n"
322 " (default is to modify input file)\n");
326 /****************************************************************************/
329 main(int argc, char *argv[])
332 char *ofile = NULL, *ifile;
334 program_name = argv[0];
336 while ((c = getopt(argc, argv, "pPdzZrRkKs:o:")) != EOF) {
338 case 'p': print = 1; break;
339 case 'P': print_relocs = 1; break;
340 case 'z': docompress = 1; break;
341 case 'd': docompress = 2; break;
342 case 'Z': docompress = -1; break;
343 case 'r': ramload = 1; break;
344 case 'R': ramload = -1; break;
345 case 'k': ktrace = 1; break;
346 case 'K': ktrace = -1; break;
347 case 'o': ofile = optarg; break;
349 if (sscanf(optarg, "%i", &stacksize) != 1)
350 usage("invalid stack size");
353 usage("invalid option");
359 usage("No input files provided");
361 if (ofile && argc - optind > 1)
362 usage("-o can only be used with a single file");
364 if (!print && !docompress && !ramload && !stacksize) /* no args == print */
365 print = argc - optind; /* greater than 1 is short format */
367 for (c = optind; c < argc; c++) {
371 process_file(ifile, ofile);
378 /****************************************************************************/