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;
45 /****************************************************************************/
48 transferr(FILE *ifp, FILE *ofp, int count)
52 while (count == -1 || count > 0) {
53 if (count == -1 || count > sizeof(cmd))
57 n = fread(cmd, 1, num, ifp);
60 if (fwrite(cmd, n, 1, ofp) != 1) {
61 fprintf(stderr, "Write failed :-(\n");
68 fprintf(stderr, "Failed to transferr %d bytes\n", count);
73 /****************************************************************************/
76 process_file(char *ifile, char *ofile)
78 int old_flags, old_stack, new_flags, new_stack;
81 struct flat_hdr old_hdr, new_hdr;
85 *tfile = *tfile2 = '\0';
87 if ((ifp = fopen(ifile, "r" BINARY_FILE_OPTS)) == NULL) {
88 fprintf(stderr, "Cannot open %s\n", ifile);
92 if (fread(&old_hdr, sizeof(old_hdr), 1, ifp) != 1) {
93 fprintf(stderr, "Cannot read header of %s\n", ifile);
97 if (strncmp(old_hdr.magic, "bFLT", 4) != 0) {
98 fprintf(stderr, "Cannot read header of %s\n", ifile);
102 new_flags = old_flags = ntohl(old_hdr.flags);
103 new_stack = old_stack = ntohl(old_hdr.stack_size);
107 new_flags |= FLAT_FLAG_GZIP;
108 new_flags &= ~FLAT_FLAG_GZDATA;
109 } else if (compress == 2) {
110 new_flags |= FLAT_FLAG_GZDATA;
111 new_flags &= ~FLAT_FLAG_GZIP;
112 } else if (compress < 0)
113 new_flags &= ~(FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA);
116 new_flags |= FLAT_FLAG_RAM;
117 else if (ramload < 0)
118 new_flags &= ~FLAT_FLAG_RAM;
121 new_flags |= FLAT_FLAG_KTRACE;
123 new_flags &= ~FLAT_FLAG_KTRACE;
126 new_stack = stacksize;
131 printf("%s\n", ifile);
132 printf(" Magic: %4.4s\n", old_hdr.magic);
133 printf(" Rev: %d\n", ntohl(old_hdr.rev));
134 t = (time_t) htonl(old_hdr.build_date);
135 printf(" Build Date: %s", t?ctime(&t):"not specified\n");
136 printf(" Entry: 0x%x\n", ntohl(old_hdr.entry));
137 printf(" Data Start: 0x%x\n", ntohl(old_hdr.data_start));
138 printf(" Data End: 0x%x\n", ntohl(old_hdr.data_end));
139 printf(" BSS End: 0x%x\n", ntohl(old_hdr.bss_end));
140 printf(" Stack Size: 0x%x\n", ntohl(old_hdr.stack_size));
141 printf(" Reloc Start: 0x%x\n", ntohl(old_hdr.reloc_start));
142 printf(" Reloc Count: 0x%x\n", ntohl(old_hdr.reloc_count));
143 printf(" Flags: 0x%x ( ", ntohl(old_hdr.flags));
145 if (old_flags & FLAT_FLAG_RAM)
146 printf("Load-to-Ram ");
147 if (old_flags & FLAT_FLAG_GOTPIC)
148 printf("Has-PIC-GOT ");
149 if (old_flags & FLAT_FLAG_GZIP)
150 printf("Gzip-Compressed ");
151 if (old_flags & FLAT_FLAG_GZDATA)
152 printf("Gzip-Data-Compressed ");
153 if (old_flags & FLAT_FLAG_KTRACE)
154 printf("Kernel-Traced-Load ");
157 } else if (print > 1) {
158 static int first = 1;
159 unsigned int text, data, bss, stk, rel, tot;
162 printf("Flag Rev Text Data BSS Stack Relocs RAM Filename\n");
163 printf("-----------------------------------------------------------\n");
167 strcat(tfile, (old_flags & FLAT_FLAG_KTRACE) ? "k" : "");
168 strcat(tfile, (old_flags & FLAT_FLAG_RAM) ? "r" : "");
169 strcat(tfile, (old_flags & FLAT_FLAG_GOTPIC) ? "p" : "");
170 strcat(tfile, (old_flags & FLAT_FLAG_GZIP) ? "z" :
171 ((old_flags & FLAT_FLAG_GZDATA) ? "d" : ""));
172 printf("-%-3.3s ", tfile);
173 printf("%3d ", ntohl(old_hdr.rev));
174 printf("%6d ", text=ntohl(old_hdr.data_start)-sizeof(struct flat_hdr));
175 printf("%6d ", data=ntohl(old_hdr.data_end)-ntohl(old_hdr.data_start));
176 printf("%6d ", bss=ntohl(old_hdr.bss_end)-ntohl(old_hdr.data_end));
177 printf("%6d ", stk=ntohl(old_hdr.stack_size));
178 printf("%6d ", rel=ntohl(old_hdr.reloc_count) * 4);
180 * work out how much RAM is needed per invocation, this
181 * calculation is dependent on the binfmt_flat implementation
183 tot = data; /* always need data */
185 if (old_flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))
186 tot += text + sizeof(struct flat_hdr);
188 if (bss + stk > rel) /* which is bigger ? */
195 * the total depends on whether the relocs are smaller/bigger than
198 printf("%s\n", ifile);
201 /* if there is nothing else to do, leave */
202 if (new_flags == old_flags && new_stack == old_stack)
205 new_hdr.flags = htonl(new_flags);
206 new_hdr.stack_size = htonl(new_stack);
208 strcpy(tfile, "/tmp/flatXXXXXX");
210 if ((ofp = fopen(tfile, "w" BINARY_FILE_OPTS)) == NULL) {
211 fprintf(stderr, "Failed to open %s for writing\n", tfile);
217 if (fwrite(&new_hdr, sizeof(new_hdr), 1, ofp) != 1) {
218 fprintf(stderr, "Failed to write to %s\n", tfile);
225 * get ourselves a fully uncompressed copy of the text/data/relocs
226 * so that we can manipulate it more easily
228 if (old_flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
231 strcpy(tfile2, "/tmp/flat2XXXXXX");
234 if (old_flags & FLAT_FLAG_GZDATA) {
235 tfp = fopen(tfile2, "w" BINARY_FILE_OPTS);
237 fprintf(stderr, "Failed to open %s for writing\n", tfile2);
240 transferr(ifp, tfp, ntohl(old_hdr.data_start) -
241 sizeof(struct flat_hdr));
245 sprintf(cmd, "gunzip >> %s", tfile2);
246 tfp = popen(cmd, "w" BINARY_FILE_OPTS);
251 transferr(ifp, tfp, -1);
255 ifp = fopen(tfile2, "r" BINARY_FILE_OPTS);
257 fprintf(stderr, "Failed to open %s for reading\n", tfile2);
264 if (new_flags & FLAT_FLAG_GZIP) {
265 printf("zflat %s --> %s\n", ifile, ofile);
267 sprintf(cmd, "gzip -9 -f >> %s", tfile);
268 ofp = popen(cmd, "w" BINARY_FILE_OPTS);
270 } else if (new_flags & FLAT_FLAG_GZDATA) {
271 printf("zflat-data %s --> %s\n", ifile, ofile);
272 transferr(ifp, ofp, ntohl(new_hdr.data_start) -
273 sizeof(struct flat_hdr));
275 sprintf(cmd, "gzip -9 -f >> %s", tfile);
276 ofp = popen(cmd, "w" BINARY_FILE_OPTS);
280 if (!ofp) { /* can only happen if using gzip/gunzip */
281 fprintf(stderr, "Can't run cmd %s\n", cmd);
287 transferr(ifp, ofp, -1);
289 if (ferror(ifp) || ferror(ofp)) {
290 fprintf(stderr, "Error on file pointer%s%s\n",
291 ferror(ifp) ? " input" : "", ferror(ofp) ? " output" : "");
303 /* cheat a little here to preserve file permissions */
304 sprintf(cmd, "cp %s %s", tfile, ofile);
310 /****************************************************************************/
316 fprintf(stderr, "%s\n", s);
317 fprintf(stderr, "usage: %s [options] flat-file\n", program_name);
318 fprintf(stderr, " Allows you to change an existing flat file\n\n");
319 fprintf(stderr, " -p : print current settings\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, " -s size : stack size\n");
328 fprintf(stderr, " -o file : output-file\n"
329 " (default is to modify input file)\n");
333 /****************************************************************************/
336 main(int argc, char *argv[])
339 char *ofile = NULL, *ifile;
341 program_name = argv[0];
343 while ((c = getopt(argc, argv, "pdzZrRkKs:o:")) != EOF) {
345 case 'p': print = 1; break;
346 case 'z': compress = 1; break;
347 case 'd': compress = 2; break;
348 case 'Z': compress = -1; break;
349 case 'r': ramload = 1; break;
350 case 'R': ramload = -1; break;
351 case 'k': ktrace = 1; break;
352 case 'K': ktrace = -1; break;
353 case 'o': ofile = optarg; break;
355 if (sscanf(optarg, "%i", &stacksize) != 1)
356 usage("invalid stack size");
359 usage("invalid option");
365 usage("No input files provided");
367 if (ofile && argc - optind > 1)
368 usage("-o can only be used with a single file");
370 if (!print && !compress && !ramload && !stacksize) /* no args == print */
371 print = argc - optind; /* greater than 1 is short format */
373 for (c = optind; c < argc; c++) {
377 process_file(ifile, ofile);
384 /****************************************************************************/