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 */
20 #include <netinet/in.h> /* Consts and structs defined by the internet system */
22 /* from uClinux-x.x.x/include/linux */
23 #include "flat.h" /* Binary flat header description */
25 /****************************************************************************/
29 static char cmd[1024];
30 static int print = 0, compress = 0, ramload = 0, stacksize = 0, ktrace = 0;
31 static int short_format = 0;
33 /****************************************************************************/
36 transferr(FILE *ifp, FILE *ofp, int count)
40 while (count == -1 || count > 0) {
41 if (count == -1 || count > sizeof(cmd))
45 n = fread(cmd, 1, num, ifp);
48 if (fwrite(cmd, n, 1, ofp) != 1) {
49 fprintf(stderr, "Write failed :-(\n");
56 fprintf(stderr, "Failed to transferr %d bytes\n", count);
61 /****************************************************************************/
64 process_file(char *ifile, char *ofile)
66 int old_flags, old_stack, new_flags, new_stack;
69 struct flat_hdr old_hdr, new_hdr;
73 *tfile = *tfile2 = '\0';
75 if ((ifp = fopen(ifile, "rb")) == NULL) {
76 fprintf(stderr, "Cannot open %s\n", ifile);
80 if (fread(&old_hdr, sizeof(old_hdr), 1, ifp) != 1) {
81 fprintf(stderr, "Cannot read header of %s\n", ifile);
85 if (strncmp(old_hdr.magic, "bFLT", 4) != 0) {
86 fprintf(stderr, "Cannot read header of %s\n", ifile);
90 new_flags = old_flags = ntohl(old_hdr.flags);
91 new_stack = old_stack = ntohl(old_hdr.stack_size);
95 new_flags |= FLAT_FLAG_GZIP;
96 new_flags &= ~FLAT_FLAG_GZDATA;
97 } else if (compress == 2) {
98 new_flags |= FLAT_FLAG_GZDATA;
99 new_flags &= ~FLAT_FLAG_GZIP;
100 } else if (compress < 0)
101 new_flags &= ~(FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA);
104 new_flags |= FLAT_FLAG_RAM;
105 else if (ramload < 0)
106 new_flags &= ~FLAT_FLAG_RAM;
109 new_flags |= FLAT_FLAG_KTRACE;
111 new_flags &= ~FLAT_FLAG_KTRACE;
114 new_stack = stacksize;
119 printf("%s\n", ifile);
120 printf(" Magic: %4.4s\n", old_hdr.magic);
121 printf(" Rev: %d\n", ntohl(old_hdr.rev));
122 t = (time_t) htonl(old_hdr.build_date);
123 printf(" Build Date: %s", t?ctime(&t):"not specified\n");
124 printf(" Entry: 0x%x\n", ntohl(old_hdr.entry));
125 printf(" Data Start: 0x%x\n", ntohl(old_hdr.data_start));
126 printf(" Data End: 0x%x\n", ntohl(old_hdr.data_end));
127 printf(" BSS End: 0x%x\n", ntohl(old_hdr.bss_end));
128 printf(" Stack Size: 0x%x\n", ntohl(old_hdr.stack_size));
129 printf(" Reloc Start: 0x%x\n", ntohl(old_hdr.reloc_start));
130 printf(" Reloc Count: 0x%x\n", ntohl(old_hdr.reloc_count));
131 printf(" Flags: 0x%x ( ", ntohl(old_hdr.flags));
133 if (old_flags & FLAT_FLAG_RAM)
134 printf("Load-to-Ram ");
135 if (old_flags & FLAT_FLAG_GOTPIC)
136 printf("Has-PIC-GOT ");
137 if (old_flags & FLAT_FLAG_GZIP)
138 printf("Gzip-Compressed ");
139 if (old_flags & FLAT_FLAG_GZDATA)
140 printf("Gzip-Data-Compressed ");
141 if (old_flags & FLAT_FLAG_KTRACE)
142 printf("Kernel-Traced-Load ");
145 } else if (print > 1) {
146 static int first = 1;
147 unsigned int text, data, bss, stk, rel, tot;
150 printf("Flag Rev Text Data BSS Stack Relocs RAM Filename\n");
151 printf("-----------------------------------------------------------\n");
155 strcat(tfile, (old_flags & FLAT_FLAG_KTRACE) ? "k" : "");
156 strcat(tfile, (old_flags & FLAT_FLAG_RAM) ? "r" : "");
157 strcat(tfile, (old_flags & FLAT_FLAG_GOTPIC) ? "p" : "");
158 strcat(tfile, (old_flags & FLAT_FLAG_GZIP) ? "z" :
159 ((old_flags & FLAT_FLAG_GZDATA) ? "d" : ""));
160 printf("-%-3.3s ", tfile);
161 printf("%3d ", ntohl(old_hdr.rev));
162 printf("%6d ", text=ntohl(old_hdr.data_start)-sizeof(struct flat_hdr));
163 printf("%6d ", data=ntohl(old_hdr.data_end)-ntohl(old_hdr.data_start));
164 printf("%6d ", bss=ntohl(old_hdr.bss_end)-ntohl(old_hdr.data_end));
165 printf("%6d ", stk=ntohl(old_hdr.stack_size));
166 printf("%6d ", rel=ntohl(old_hdr.reloc_count) * 4);
168 * work out how much RAM is needed per invocation, this
169 * calculation is dependent on the binfmt_flat implementation
171 tot = data; /* always need data */
173 if (old_flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))
174 tot += text + sizeof(struct flat_hdr);
176 if (bss + stk > rel) /* which is bigger ? */
183 * the total depends on whether the relocs are smaller/bigger than
186 printf("%s\n", ifile);
189 /* if there is nothing else to do, leave */
190 if (new_flags == old_flags && new_stack == old_stack)
193 new_hdr.flags = htonl(new_flags);
194 new_hdr.stack_size = htonl(new_stack);
196 strcpy(tfile, "/tmp/flatXXXXXX");
198 if ((ofp = fopen(tfile, "wb")) == NULL) {
199 fprintf(stderr, "Failed to open %s for writing\n", tfile);
205 if (fwrite(&new_hdr, sizeof(new_hdr), 1, ofp) != 1) {
206 fprintf(stderr, "Failed to write to %s\n", tfile);
213 * get ourselves a fully uncompressed copy of the text/data/relocs
214 * so that we can manipulate it more easily
216 if (old_flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
219 strcpy(tfile2, "/tmp/flat2XXXXXX");
222 if (old_flags & FLAT_FLAG_GZDATA) {
223 tfp = fopen(tfile2, "wb");
225 fprintf(stderr, "Failed to open %s for writing\n", tfile2);
228 transferr(ifp, tfp, ntohl(old_hdr.data_start) -
229 sizeof(struct flat_hdr));
233 sprintf(cmd, "gunzip >> %s", tfile2);
234 tfp = popen(cmd, "wb");
239 transferr(ifp, tfp, -1);
243 ifp = fopen(tfile2, "rb");
245 fprintf(stderr, "Failed to open %s for reading\n", tfile2);
252 if (new_flags & FLAT_FLAG_GZIP) {
253 printf("zflat %s --> %s\n", ifile, ofile);
255 sprintf(cmd, "gzip -9 -f >> %s", tfile);
256 ofp = popen(cmd, "wb");
258 } else if (new_flags & FLAT_FLAG_GZDATA) {
259 printf("zflat-data %s --> %s\n", ifile, ofile);
260 transferr(ifp, ofp, ntohl(new_hdr.data_start) -
261 sizeof(struct flat_hdr));
263 sprintf(cmd, "gzip -9 -f >> %s", tfile);
264 ofp = popen(cmd, "wb");
268 if (!ofp) { /* can only happen if using gzip/gunzip */
269 fprintf(stderr, "Can't run cmd %s\n", cmd);
275 transferr(ifp, ofp, -1);
277 if (ferror(ifp) || ferror(ofp)) {
278 fprintf(stderr, "Error on file pointer%s%s\n",
279 ferror(ifp) ? " input" : "", ferror(ofp) ? " output" : "");
291 /* cheat a little here to preserve file permissions */
292 sprintf(cmd, "cp %s %s", tfile, ofile);
298 /****************************************************************************/
304 fprintf(stderr, "%s\n", s);
305 fprintf(stderr, "usage: %s [options] flat-file\n", program_name);
306 fprintf(stderr, " Allows you to change an existing flat file\n\n");
307 fprintf(stderr, " -p : print current settings\n");
308 fprintf(stderr, " -z : compressed flat file\n");
309 fprintf(stderr, " -d : compressed data-only flat file\n");
310 fprintf(stderr, " -Z : un-compressed flat file\n");
311 fprintf(stderr, " -r : ram load\n");
312 fprintf(stderr, " -R : do not RAM load\n");
313 fprintf(stderr, " -k : kernel traced load (for debug)\n");
314 fprintf(stderr, " -K : normal non-kernel traced load\n");
315 fprintf(stderr, " -s size : stack size\n");
316 fprintf(stderr, " -o file : output-file\n"
317 " (default is to modify input file)\n");
321 /****************************************************************************/
324 main(int argc, char *argv[])
327 char *ofile = NULL, *ifile;
329 program_name = argv[0];
331 while ((c = getopt(argc, argv, "pdzZrRkKs:o:")) != EOF) {
333 case 'p': print = 1; break;
334 case 'z': compress = 1; break;
335 case 'd': compress = 2; break;
336 case 'Z': compress = -1; break;
337 case 'r': ramload = 1; break;
338 case 'R': ramload = -1; break;
339 case 'k': ktrace = 1; break;
340 case 'K': ktrace = -1; break;
341 case 's': stacksize = atoi(optarg); break;
342 case 'o': ofile = optarg; break;
344 usage("invalid option");
350 usage("No input files provided");
352 if (ofile && argc - optind > 1)
353 usage("-o can only be used with a single file");
355 if (!print && !compress && !ramload && !stacksize) /* no args == print */
356 print = argc - optind; /* greater than 1 is short format */
358 for (c = optind; c < argc; c++) {
362 process_file(ifile, ofile);
369 /****************************************************************************/