-
Notifications
You must be signed in to change notification settings - Fork 41
/
flthdr.c
383 lines (322 loc) · 10.8 KB
/
flthdr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/****************************************************************************/
/*
* A simple program to manipulate flat files
*
* Copyright (C) 2001-2003 SnapGear Inc, [email protected]
* Copyright (C) 2001 Lineo, [email protected]
*
* This is Free Software, under the GNU Public Licence v2 or greater.
*
*/
/****************************************************************************/
#include <stdio.h> /* Userland pieces of the ANSI C standard I/O package */
#include <unistd.h> /* Userland prototypes of the Unix std system calls */
#include <time.h>
#include <stdlib.h> /* exit() */
#include <string.h> /* strcat(), strcpy() */
#include <inttypes.h>
#include <assert.h>
#include "compress.h"
#include <libiberty.h>
#include "stubs.h"
const char *elf2flt_progname;
/* from uClinux-x.x.x/include/linux */
#include "flat.h" /* Binary flat header description */
#if defined(__MINGW32__)
#include <getopt.h>
#endif
#if defined TARGET_bfin
# define flat_get_relocate_addr(addr) (addr & 0x03ffffff)
#else
# define flat_get_relocate_addr(addr) (addr)
#endif
/****************************************************************************/
static int print = 0, print_relocs = 0, docompress = 0, ramload = 0,
stacksize = 0, ktrace = 0, l1stack = 0;
/****************************************************************************/
void
process_file(const char *ifile, const char *ofile)
{
int old_flags, old_stack, new_flags, new_stack;
stream ifp, ofp;
struct flat_hdr old_hdr, new_hdr;
char *tfile, tmpbuf[256];
int input_error, output_error;
*tmpbuf = '\0';
if (fopen_stream_u(&ifp, ifile, "r" BINARY_FILE_OPTS)) {
fprintf(stderr, "Cannot open %s\n", ifile);
return;
}
if (fread_stream(&old_hdr, sizeof(old_hdr), 1, &ifp) != 1) {
fprintf(stderr, "Cannot read header of %s\n", ifile);
fclose_stream(&ifp);
return;
}
if (strncmp(old_hdr.magic, "bFLT", 4) != 0) {
fprintf(stderr, "Cannot read header of %s\n", ifile);
fclose_stream(&ifp);
return;
}
new_flags = old_flags = ntohl(old_hdr.flags);
new_stack = old_stack = ntohl(old_hdr.stack_size);
new_hdr = old_hdr;
if (docompress == 1) {
new_flags |= FLAT_FLAG_GZIP;
new_flags &= ~FLAT_FLAG_GZDATA;
} else if (docompress == 2) {
new_flags |= FLAT_FLAG_GZDATA;
new_flags &= ~FLAT_FLAG_GZIP;
} else if (docompress < 0)
new_flags &= ~(FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA);
if (ramload > 0)
new_flags |= FLAT_FLAG_RAM;
else if (ramload < 0)
new_flags &= ~FLAT_FLAG_RAM;
if (ktrace > 0)
new_flags |= FLAT_FLAG_KTRACE;
else if (ktrace < 0)
new_flags &= ~FLAT_FLAG_KTRACE;
if (l1stack > 0)
new_flags |= FLAT_FLAG_L1STK;
else if (l1stack < 0)
new_flags &= ~FLAT_FLAG_L1STK;
if (stacksize)
new_stack = stacksize;
if (print == 1) {
time_t t;
uint32_t reloc_count, reloc_start;
printf("%s\n", ifile);
printf(" Magic: %4.4s\n", old_hdr.magic);
printf(" Rev: %d\n", ntohl(old_hdr.rev));
t = (time_t) htonl(old_hdr.build_date);
printf(" Build Date: %s", t?ctime(&t):"not specified\n");
printf(" Entry: 0x%x\n", ntohl(old_hdr.entry));
printf(" Data Start: 0x%x\n", ntohl(old_hdr.data_start));
printf(" Data End: 0x%x\n", ntohl(old_hdr.data_end));
printf(" BSS End: 0x%x\n", ntohl(old_hdr.bss_end));
printf(" Stack Size: 0x%x\n", ntohl(old_hdr.stack_size));
reloc_start = ntohl(old_hdr.reloc_start);
printf(" Reloc Start: 0x%x\n", reloc_start);
reloc_count = ntohl(old_hdr.reloc_count);
printf(" Reloc Count: 0x%x\n", reloc_count);
printf(" Flags: 0x%x ( ", ntohl(old_hdr.flags));
if (old_flags) {
if (old_flags & FLAT_FLAG_RAM)
printf("Load-to-Ram ");
if (old_flags & FLAT_FLAG_GOTPIC)
printf("Has-PIC-GOT ");
if (old_flags & FLAT_FLAG_GZIP)
printf("Gzip-Compressed ");
if (old_flags & FLAT_FLAG_GZDATA)
printf("Gzip-Data-Compressed ");
if (old_flags & FLAT_FLAG_KTRACE)
printf("Kernel-Traced-Load ");
if (old_flags & FLAT_FLAG_L1STK)
printf("L1-Scratch-Stack ");
}
printf(")\n");
if (print_relocs) {
uint32_t *relocs = xcalloc(reloc_count, sizeof(uint32_t));
uint32_t i;
unsigned long r;
printf(" Relocs:\n");
printf(" #\treloc ( address )\tdata\n");
if (old_flags & FLAT_FLAG_GZIP)
reopen_stream_compressed(&ifp);
if (fseek_stream(&ifp, reloc_start, SEEK_SET)) {
fprintf(stderr, "Cannot seek to relocs of %s\n", ifile);
fclose_stream(&ifp);
return;
}
if (fread_stream(relocs, sizeof(uint32_t), reloc_count, &ifp) == -1) {
fprintf(stderr, "Cannot read relocs of %s\n", ifile);
fclose_stream(&ifp);
return;
}
for (i = 0; i < reloc_count; ++i) {
uint32_t raddr, addr;
r = ntohl(relocs[i]);
raddr = flat_get_relocate_addr(r);
printf(" %u\t0x%08lx (0x%08"PRIx32")\t", i, r, raddr);
fseek_stream(&ifp, sizeof(old_hdr) + raddr, SEEK_SET);
fread_stream(&addr, sizeof(addr), 1, &ifp);
printf("%"PRIx32"\n", addr);
}
/* reset file position for below */
fseek_stream(&ifp, sizeof(old_hdr), SEEK_SET);
}
} else if (print > 1) {
static int first = 1;
unsigned int text, data, bss, stk, rel, tot;
if (first) {
printf("Flag Rev Text Data BSS Stack Relocs RAM Filename\n");
printf("-----------------------------------------------------------\n");
first = 0;
}
*tmpbuf = '\0';
strcat(tmpbuf, (old_flags & FLAT_FLAG_KTRACE) ? "k" : "");
strcat(tmpbuf, (old_flags & FLAT_FLAG_RAM) ? "r" : "");
strcat(tmpbuf, (old_flags & FLAT_FLAG_GOTPIC) ? "p" : "");
strcat(tmpbuf, (old_flags & FLAT_FLAG_GZIP) ? "z" :
((old_flags & FLAT_FLAG_GZDATA) ? "d" : ""));
printf("-%-3.3s ", tmpbuf);
printf("%3d ", ntohl(old_hdr.rev));
printf("%6d ", text=ntohl(old_hdr.data_start)-sizeof(struct flat_hdr));
printf("%6d ", data=ntohl(old_hdr.data_end)-ntohl(old_hdr.data_start));
printf("%6d ", bss=ntohl(old_hdr.bss_end)-ntohl(old_hdr.data_end));
printf("%6d ", stk=ntohl(old_hdr.stack_size));
printf("%6d ", rel=ntohl(old_hdr.reloc_count) * 4);
/*
* work out how much RAM is needed per invocation, this
* calculation is dependent on the binfmt_flat implementation
*/
tot = data; /* always need data */
if (old_flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))
tot += text + sizeof(struct flat_hdr);
if (bss + stk > rel) /* which is bigger ? */
tot += bss + stk;
else
tot += rel;
printf("%6d ", tot);
/*
* the total depends on whether the relocs are smaller/bigger than
* the BSS
*/
printf("%s\n", ifile);
}
/* if there is nothing else to do, leave */
if (new_flags == old_flags && new_stack == old_stack) {
fclose_stream(&ifp);
return;
}
new_hdr.flags = htonl(new_flags);
new_hdr.stack_size = htonl(new_stack);
tfile = make_temp_file("flthdr");
if (fopen_stream_u(&ofp, tfile, "w" BINARY_FILE_OPTS)) {
unlink(tfile);
fatal("Failed to open %s for writing\n", tfile);
}
/* Copy header (always uncompressed). */
if (fwrite_stream(&new_hdr, sizeof(new_hdr), 1, &ofp) != 1) {
unlink(tfile);
fatal("Failed to write to %s\n", tfile);
}
/* Whole input file (including text) is compressed: start decompressing
now. */
if (old_flags & FLAT_FLAG_GZIP)
reopen_stream_compressed(&ifp);
/* Likewise, output file is compressed. Start compressing now. */
if (new_flags & FLAT_FLAG_GZIP) {
printf("zflat %s --> %s\n", ifile, ofile);
reopen_stream_compressed(&ofp);
}
transfer(&ifp, &ofp,
ntohl(old_hdr.data_start) - sizeof(struct flat_hdr));
/* Only data and relocs were compressed in input. Start decompressing
from here. */
if (old_flags & FLAT_FLAG_GZDATA)
reopen_stream_compressed(&ifp);
/* Only data/relocs to be compressed in output. Start compressing
from here. */
if (new_flags & FLAT_FLAG_GZDATA) {
printf("zflat-data %s --> %s\n", ifile, ofile);
reopen_stream_compressed(&ofp);
}
transfer(&ifp, &ofp, -1);
input_error = ferror_stream(&ifp);
output_error = ferror_stream(&ofp);
if (input_error || output_error) {
unlink(tfile);
fatal("Error on file pointer%s%s\n",
input_error ? " input" : "",
output_error ? " output" : "");
}
fclose_stream(&ifp);
fclose_stream(&ofp);
/* Copy temporary file to output location. */
fopen_stream_u(&ifp, tfile, "r" BINARY_FILE_OPTS);
fopen_stream_u(&ofp, ofile, "w" BINARY_FILE_OPTS);
transfer(&ifp, &ofp, -1);
fclose_stream(&ifp);
fclose_stream(&ofp);
unlink(tfile);
free(tfile);
}
/****************************************************************************/
void
usage(const char *s)
{
FILE *out = s ? stderr : stdout;
if (s)
fprintf(out, "%s\n", s);
fprintf(out,
"Usage: %s [options] flat-file\n"
" Allows you to change an existing flat file\n\n"
" -p : print current settings\n"
" -P : print relocations\n"
" -z : compressed flat file\n"
" -d : compressed data-only flat file\n"
" -Z : un-compressed flat file\n"
" -r : ram load\n"
" -R : do not RAM load\n"
" -k : kernel traced load (for debug)\n"
" -K : normal non-kernel traced load\n"
" -u : place stack in L1 scratchpad memory\n"
" -U : place stack in normal SDRAM memory\n"
" -s size : stack size\n"
" -o file : output-file\n"
" (default is to modify input file)\n",
elf2flt_progname);
exit(s ? 1 : 0);
}
/****************************************************************************/
int
main(int argc, char *argv[])
{
int c, noargs;
const char *ofile = NULL, *ifile;
elf2flt_progname = argv[0];
noargs = 1;
while ((c = getopt(argc, argv, "hpPdzZrRuUkKs:o:")) != EOF) {
switch (c) {
case 'p': print = 1; break;
case 'P': print = print_relocs = 1; break;
case 'z': docompress = 1; break;
case 'd': docompress = 2; break;
case 'Z': docompress = -1; break;
case 'r': ramload = 1; break;
case 'R': ramload = -1; break;
case 'k': ktrace = 1; break;
case 'K': ktrace = -1; break;
case 'u': l1stack = 1; break;
case 'U': l1stack = -1; break;
case 'o': ofile = optarg; break;
case 's':
if (sscanf(optarg, "%i", &stacksize) != 1)
usage("invalid stack size");
break;
case 'h':
usage(NULL);
break;
default:
usage("invalid option");
break;
}
noargs = 0;
}
if (optind >= argc)
usage("No input files provided");
if (ofile && argc - optind > 1)
usage("-o can only be used with a single file");
if (!print && noargs) /* no args == print */
print = argc - optind; /* greater than 1 is short format */
for (c = optind; c < argc; c++) {
ifile = argv[c];
if (!ofile)
ofile = ifile;
process_file(ifile, ofile);
ofile = NULL;
}
exit(0);
}
/****************************************************************************/