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() */
19 /* macros for conversion between host and (internet) network byte order */
21 #include <netinet/in.h> /* Consts and structs defined by the internet system */
22 #define BINARY_FILE_OPTS
25 #define BINARY_FILE_OPTS "b"
28 /* from uClinux-x.x.x/include/linux */
29 #include "flat.h" /* Binary flat header description */
31 #if defined(__MINGW32__)
34 #define mkstemp(p) mktemp(p)
38 /****************************************************************************/
42 static char cmd[1024];
43 static int print = 0, compress = 0, ramload = 0, stacksize = 0, ktrace = 0;
44 static int short_format = 0;
46 /****************************************************************************/
49 transferr(FILE *ifp, FILE *ofp, int count)
53 while (count == -1 || count > 0) {
54 if (count == -1 || count > sizeof(cmd))
58 n = fread(cmd, 1, num, ifp);
61 if (fwrite(cmd, n, 1, ofp) != 1) {
62 fprintf(stderr, "Write failed :-(\n");
69 fprintf(stderr, "Failed to transferr %d bytes\n", count);
74 /****************************************************************************/
77 process_file(char *ifile, char *ofile)
79 int old_flags, old_stack, new_flags, new_stack;
82 struct flat_hdr old_hdr, new_hdr;
86 *tfile = *tfile2 = '\0';
88 if ((ifp = fopen(ifile, "r" BINARY_FILE_OPTS)) == NULL) {
89 fprintf(stderr, "Cannot open %s\n", ifile);
93 if (fread(&old_hdr, sizeof(old_hdr), 1, ifp) != 1) {
94 fprintf(stderr, "Cannot read header of %s\n", ifile);
98 if (strncmp(old_hdr.magic, "bFLT", 4) != 0) {
99 fprintf(stderr, "Cannot read header of %s\n", ifile);
103 new_flags = old_flags = ntohl(old_hdr.flags);
104 new_stack = old_stack = ntohl(old_hdr.stack_size);
108 new_flags |= FLAT_FLAG_GZIP;
109 new_flags &= ~FLAT_FLAG_GZDATA;
110 } else if (compress == 2) {
111 new_flags |= FLAT_FLAG_GZDATA;
112 new_flags &= ~FLAT_FLAG_GZIP;
113 } else if (compress < 0)
114 new_flags &= ~(FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA);
117 new_flags |= FLAT_FLAG_RAM;
118 else if (ramload < 0)
119 new_flags &= ~FLAT_FLAG_RAM;
122 new_flags |= FLAT_FLAG_KTRACE;
124 new_flags &= ~FLAT_FLAG_KTRACE;
127 new_stack = stacksize;
132 printf("%s\n", ifile);
133 printf(" Magic: %4.4s\n", old_hdr.magic);
134 printf(" Rev: %d\n", ntohl(old_hdr.rev));
135 t = (time_t) htonl(old_hdr.build_date);
136 printf(" Build Date: %s", t?ctime(&t):"not specified\n");
137 printf(" Entry: 0x%x\n", ntohl(old_hdr.entry));
138 printf(" Data Start: 0x%x\n", ntohl(old_hdr.data_start));
139 printf(" Data End: 0x%x\n", ntohl(old_hdr.data_end));
140 printf(" BSS End: 0x%x\n", ntohl(old_hdr.bss_end));
141 printf(" Stack Size: 0x%x\n", ntohl(old_hdr.stack_size));
142 printf(" Reloc Start: 0x%x\n", ntohl(old_hdr.reloc_start));
143 printf(" Reloc Count: 0x%x\n", ntohl(old_hdr.reloc_count));
144 printf(" Flags: 0x%x ( ", ntohl(old_hdr.flags));
146 if (old_flags & FLAT_FLAG_RAM)
147 printf("Load-to-Ram ");
148 if (old_flags & FLAT_FLAG_GOTPIC)
149 printf("Has-PIC-GOT ");
150 if (old_flags & FLAT_FLAG_GZIP)
151 printf("Gzip-Compressed ");
152 if (old_flags & FLAT_FLAG_GZDATA)
153 printf("Gzip-Data-Compressed ");
154 if (old_flags & FLAT_FLAG_KTRACE)
155 printf("Kernel-Traced-Load ");
158 } else if (print > 1) {
159 static int first = 1;
160 unsigned int text, data, bss, stk, rel, tot;
163 printf("Flag Rev Text Data BSS Stack Relocs RAM Filename\n");
164 printf("-----------------------------------------------------------\n");
168 strcat(tfile, (old_flags & FLAT_FLAG_KTRACE) ? "k" : "");
169 strcat(tfile, (old_flags & FLAT_FLAG_RAM) ? "r" : "");
170 strcat(tfile, (old_flags & FLAT_FLAG_GOTPIC) ? "p" : "");
171 strcat(tfile, (old_flags & FLAT_FLAG_GZIP) ? "z" :
172 ((old_flags & FLAT_FLAG_GZDATA) ? "d" : ""));
173 printf("-%-3.3s ", tfile);
174 printf("%3d ", ntohl(old_hdr.rev));
175 printf("%6d ", text=ntohl(old_hdr.data_start)-sizeof(struct flat_hdr));
176 printf("%6d ", data=ntohl(old_hdr.data_end)-ntohl(old_hdr.data_start));
177 printf("%6d ", bss=ntohl(old_hdr.bss_end)-ntohl(old_hdr.data_end));
178 printf("%6d ", stk=ntohl(old_hdr.stack_size));
179 printf("%6d ", rel=ntohl(old_hdr.reloc_count) * 4);
181 * work out how much RAM is needed per invocation, this
182 * calculation is dependent on the binfmt_flat implementation
184 tot = data; /* always need data */
186 if (old_flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))
187 tot += text + sizeof(struct flat_hdr);
189 if (bss + stk > rel) /* which is bigger ? */
196 * the total depends on whether the relocs are smaller/bigger than
199 printf("%s\n", ifile);
202 /* if there is nothing else to do, leave */
203 if (new_flags == old_flags && new_stack == old_stack)
206 new_hdr.flags = htonl(new_flags);
207 new_hdr.stack_size = htonl(new_stack);
209 strcpy(tfile, "/tmp/flatXXXXXX");
211 if ((ofp = fopen(tfile, "w" BINARY_FILE_OPTS)) == NULL) {
212 fprintf(stderr, "Failed to open %s for writing\n", tfile);
218 if (fwrite(&new_hdr, sizeof(new_hdr), 1, ofp) != 1) {
219 fprintf(stderr, "Failed to write to %s\n", tfile);
226 * get ourselves a fully uncompressed copy of the text/data/relocs
227 * so that we can manipulate it more easily
229 if (old_flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
232 strcpy(tfile2, "/tmp/flat2XXXXXX");
235 if (old_flags & FLAT_FLAG_GZDATA) {
236 tfp = fopen(tfile2, "w" BINARY_FILE_OPTS);
238 fprintf(stderr, "Failed to open %s for writing\n", tfile2);
241 transferr(ifp, tfp, ntohl(old_hdr.data_start) -
242 sizeof(struct flat_hdr));
246 sprintf(cmd, "gunzip >> %s", tfile2);
247 tfp = popen(cmd, "w" BINARY_FILE_OPTS);
252 transferr(ifp, tfp, -1);
256 ifp = fopen(tfile2, "r" BINARY_FILE_OPTS);
258 fprintf(stderr, "Failed to open %s for reading\n", tfile2);
265 if (new_flags & FLAT_FLAG_GZIP) {
266 printf("zflat %s --> %s\n", ifile, ofile);
268 sprintf(cmd, "gzip -9 -f >> %s", tfile);
269 ofp = popen(cmd, "w" BINARY_FILE_OPTS);
271 } else if (new_flags & FLAT_FLAG_GZDATA) {
272 printf("zflat-data %s --> %s\n", ifile, ofile);
273 transferr(ifp, ofp, ntohl(new_hdr.data_start) -
274 sizeof(struct flat_hdr));
276 sprintf(cmd, "gzip -9 -f >> %s", tfile);
277 ofp = popen(cmd, "w" BINARY_FILE_OPTS);
281 if (!ofp) { /* can only happen if using gzip/gunzip */
282 fprintf(stderr, "Can't run cmd %s\n", cmd);
288 transferr(ifp, ofp, -1);
290 if (ferror(ifp) || ferror(ofp)) {
291 fprintf(stderr, "Error on file pointer%s%s\n",
292 ferror(ifp) ? " input" : "", ferror(ofp) ? " output" : "");
304 /* cheat a little here to preserve file permissions */
305 sprintf(cmd, "cp %s %s", tfile, ofile);
311 /****************************************************************************/
317 fprintf(stderr, "%s\n", s);
318 fprintf(stderr, "usage: %s [options] flat-file\n", program_name);
319 fprintf(stderr, " Allows you to change an existing flat file\n\n");
320 fprintf(stderr, " -p : print current settings\n");
321 fprintf(stderr, " -z : compressed flat file\n");
322 fprintf(stderr, " -d : compressed data-only flat file\n");
323 fprintf(stderr, " -Z : un-compressed flat file\n");
324 fprintf(stderr, " -r : ram load\n");
325 fprintf(stderr, " -R : do not RAM load\n");
326 fprintf(stderr, " -k : kernel traced load (for debug)\n");
327 fprintf(stderr, " -K : normal non-kernel traced load\n");
328 fprintf(stderr, " -s size : stack size\n");
329 fprintf(stderr, " -o file : output-file\n"
330 " (default is to modify input file)\n");
334 /****************************************************************************/
337 main(int argc, char *argv[])
340 char *ofile = NULL, *ifile;
342 program_name = argv[0];
344 while ((c = getopt(argc, argv, "pdzZrRkKs:o:")) != EOF) {
346 case 'p': print = 1; break;
347 case 'z': compress = 1; break;
348 case 'd': compress = 2; break;
349 case 'Z': compress = -1; break;
350 case 'r': ramload = 1; break;
351 case 'R': ramload = -1; break;
352 case 'k': ktrace = 1; break;
353 case 'K': ktrace = -1; break;
354 case 's': stacksize = atoi(optarg); break;
355 case 'o': ofile = optarg; break;
357 usage("invalid option");
363 usage("No input files provided");
365 if (ofile && argc - optind > 1)
366 usage("-o can only be used with a single file");
368 if (!print && !compress && !ramload && !stacksize) /* no args == print */
369 print = argc - optind; /* greater than 1 is short format */
371 for (c = optind; c < argc; c++) {
375 process_file(ifile, ofile);
382 /****************************************************************************/