OSDN Git Service

967d64063a5cc29a7ed3f8b5d9fabf85943fcafc
[uclinux-h8/elf2flt.git] / flthdr.c
1 /****************************************************************************/
2 /*
3  *      A simple program to manipulate flat files
4  *
5  *      Copyright (C) 2001-2003 SnapGear Inc, davidm@snapgear.com
6  *      Copyright (C) 2001 Lineo, davidm@lineo.com
7  *
8  * This is Free Software, under the GNU Public Licence v2 or greater.
9  *
10  */
11 /****************************************************************************/
12
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    */
15 #include <time.h>
16 #include <stdlib.h>   /* exit() */
17 #include <string.h>   /* strcat(), strcpy() */
18
19 /* macros for conversion between host and (internet) network byte order */
20 #ifndef WIN32
21 #include <netinet/in.h> /* Consts and structs defined by the internet system */
22 #define BINARY_FILE_OPTS
23 #else
24 #include <winsock2.h>
25 #define BINARY_FILE_OPTS "b"
26 #endif
27
28 /* from uClinux-x.x.x/include/linux */
29 #include "flat.h"     /* Binary flat header description                      */
30
31 #if defined(__MINGW32__)
32 #include <getopt.h>
33
34 #define mkstemp(p) mktemp(p)
35
36 #endif
37
38 /****************************************************************************/
39
40 char *program_name;
41
42 static char cmd[1024];
43 static int print = 0, compress = 0, ramload = 0, stacksize = 0, ktrace = 0;
44 static int short_format = 0;
45
46 /****************************************************************************/
47
48 void
49 transferr(FILE *ifp, FILE *ofp, int count)
50 {
51         int n, num;
52
53         while (count == -1 || count > 0) {
54                 if (count == -1 || count > sizeof(cmd))
55                         num = sizeof(cmd);
56                 else
57                         num = count;
58                 n = fread(cmd, 1, num, ifp);
59                 if (n == 0)
60                         break;
61                 if (fwrite(cmd, n, 1, ofp) != 1) {
62                         fprintf(stderr, "Write failed :-(\n");
63                         exit(1);
64                 }
65                 if (count != -1)
66                         count -= n;
67         }
68         if (count > 0) {
69                 fprintf(stderr, "Failed to transferr %d bytes\n", count);
70                 exit(1);
71         }
72 }
73         
74 /****************************************************************************/
75
76 void
77 process_file(char *ifile, char *ofile)
78 {
79         int old_flags, old_stack, new_flags, new_stack;
80         FILE *ifp, *ofp;
81         int ofp_is_pipe = 0;
82         struct flat_hdr old_hdr, new_hdr;
83         char tfile[256];
84         char tfile2[256];
85
86         *tfile = *tfile2 = '\0';
87
88         if ((ifp = fopen(ifile, "r" BINARY_FILE_OPTS)) == NULL) {
89                 fprintf(stderr, "Cannot open %s\n", ifile);
90                 return;
91         }
92
93         if (fread(&old_hdr, sizeof(old_hdr), 1, ifp) != 1) {
94                 fprintf(stderr, "Cannot read header of %s\n", ifile);
95                 return;
96         }
97
98         if (strncmp(old_hdr.magic, "bFLT", 4) != 0) {
99                 fprintf(stderr, "Cannot read header of %s\n", ifile);
100                 return;
101         }
102
103         new_flags = old_flags = ntohl(old_hdr.flags);
104         new_stack = old_stack = ntohl(old_hdr.stack_size);
105         new_hdr = old_hdr;
106
107         if (compress == 1) {
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);
115         
116         if (ramload > 0)
117                 new_flags |= FLAT_FLAG_RAM;
118         else if (ramload < 0)
119                 new_flags &= ~FLAT_FLAG_RAM;
120         
121         if (ktrace > 0)
122                 new_flags |= FLAT_FLAG_KTRACE;
123         else if (ktrace < 0)
124                 new_flags &= ~FLAT_FLAG_KTRACE;
125         
126         if (stacksize)
127                 new_stack = stacksize;
128
129         if (print == 1) {
130                 time_t t;
131
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));
145                 if (old_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 ");
156                         printf(")\n");
157                 }
158         } else if (print > 1) {
159                 static int first = 1;
160                 unsigned int text, data, bss, stk, rel, tot;
161
162                 if (first) {
163                         printf("Flag Rev   Text   Data    BSS  Stack Relocs    RAM Filename\n");
164                         printf("-----------------------------------------------------------\n");
165                         first = 0;
166                 }
167                 *tfile = '\0';
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);
180                 /*
181                  * work out how much RAM is needed per invocation, this
182                  * calculation is dependent on the binfmt_flat implementation
183                  */
184                 tot = data; /* always need data */
185
186                 if (old_flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))
187                         tot += text + sizeof(struct flat_hdr);
188                 
189                 if (bss + stk > rel) /* which is bigger ? */
190                         tot += bss + stk;
191                 else
192                         tot += rel;
193
194                 printf("%6d ", tot);
195                 /*
196                  * the total depends on whether the relocs are smaller/bigger than
197                  * the BSS
198                  */
199                 printf("%s\n", ifile);
200         }
201
202         /* if there is nothing else to do, leave */
203         if (new_flags == old_flags && new_stack == old_stack)
204                 return;
205         
206         new_hdr.flags = htonl(new_flags);
207         new_hdr.stack_size = htonl(new_stack);
208
209         strcpy(tfile, "/tmp/flatXXXXXX");
210         mkstemp(tfile);
211         if ((ofp = fopen(tfile, "w" BINARY_FILE_OPTS)) == NULL) {
212                 fprintf(stderr, "Failed to open %s for writing\n", tfile);
213                 unlink(tfile);
214                 unlink(tfile2);
215                 exit(1);
216         }
217
218         if (fwrite(&new_hdr, sizeof(new_hdr), 1, ofp) != 1) {
219                 fprintf(stderr, "Failed to write to  %s\n", tfile);
220                 unlink(tfile);
221                 unlink(tfile2);
222                 exit(1);
223         }
224
225         /*
226          * get ourselves a fully uncompressed copy of the text/data/relocs
227          * so that we can manipulate it more easily
228          */
229         if (old_flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
230                 FILE *tfp;
231
232                 strcpy(tfile2, "/tmp/flat2XXXXXX");
233                 mkstemp(tfile2);
234                 
235                 if (old_flags & FLAT_FLAG_GZDATA) {
236                         tfp = fopen(tfile2, "w" BINARY_FILE_OPTS);
237                         if (!tfp) {
238                                 fprintf(stderr, "Failed to open %s for writing\n", tfile2);
239                                 exit(1);
240                         }
241                         transferr(ifp, tfp, ntohl(old_hdr.data_start) -
242                                         sizeof(struct flat_hdr));
243                         fclose(tfp);
244                 }
245
246                 sprintf(cmd, "gunzip >> %s", tfile2);
247                 tfp = popen(cmd, "w" BINARY_FILE_OPTS);
248                 if(!tfp) {
249                         perror("popen");
250                         exit(1);
251                 }
252                 transferr(ifp, tfp, -1);
253                 pclose(tfp);
254
255                 fclose(ifp);
256                 ifp = fopen(tfile2, "r" BINARY_FILE_OPTS);
257                 if (!ifp) {
258                         fprintf(stderr, "Failed to open %s for reading\n", tfile2);
259                         unlink(tfile);
260                         unlink(tfile2);
261                         exit(1);
262                 }
263         }
264
265         if (new_flags & FLAT_FLAG_GZIP) {
266                 printf("zflat %s --> %s\n", ifile, ofile);
267                 fclose(ofp);
268                 sprintf(cmd, "gzip -9 -f >> %s", tfile);
269                 ofp = popen(cmd, "w" BINARY_FILE_OPTS);
270                 ofp_is_pipe = 1;
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));
275                 fclose(ofp);
276                 sprintf(cmd, "gzip -9 -f >> %s", tfile);
277                 ofp = popen(cmd, "w" BINARY_FILE_OPTS);
278                 ofp_is_pipe = 1;
279         }
280
281         if (!ofp) { /* can only happen if using gzip/gunzip */
282                 fprintf(stderr, "Can't run cmd %s\n", cmd);
283                 unlink(tfile);
284                 unlink(tfile2);
285                 exit(1);
286         }
287
288         transferr(ifp, ofp, -1);
289         
290         if (ferror(ifp) || ferror(ofp)) {
291                 fprintf(stderr, "Error on file pointer%s%s\n",
292                                 ferror(ifp) ? " input" : "", ferror(ofp) ? " output" : "");
293                 unlink(tfile);
294                 unlink(tfile2);
295                 exit(1);
296         }
297
298         fclose(ifp);
299         if (ofp_is_pipe)
300                 pclose(ofp);
301         else
302                 fclose(ofp);
303
304         /* cheat a little here to preserve file permissions */
305         sprintf(cmd, "cp %s %s", tfile, ofile);
306         system(cmd);
307         unlink(tfile);
308         unlink(tfile2);
309 }
310
311 /****************************************************************************/
312
313 void
314 usage(char *s)
315 {
316         if (s)
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");
331         exit(1);
332 }
333
334 /****************************************************************************/
335
336 int
337 main(int argc, char *argv[])
338 {
339         int c;
340         char *ofile = NULL, *ifile;
341
342         program_name = argv[0];
343
344         while ((c = getopt(argc, argv, "pdzZrRkKs:o:")) != EOF) {
345                 switch (c) {
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;
356                 default:
357                         usage("invalid option");
358                         break;
359                 }
360         }
361
362         if (optind >= argc)
363                 usage("No input files provided");
364
365         if (ofile && argc - optind > 1)
366                 usage("-o can only be used with a single file");
367         
368         if (!print && !compress && !ramload && !stacksize) /* no args == print */
369                 print = argc - optind; /* greater than 1 is short format */
370         
371         for (c = optind; c < argc; c++) {
372                 ifile = argv[c];
373                 if (!ofile)
374                         ofile = ifile;
375                 process_file(ifile, ofile);
376                 ofile = NULL;
377         }
378         
379         exit(0);
380 }
381
382 /****************************************************************************/