forked from askac/dumpifs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumpifs.c
1395 lines (1245 loc) · 35.3 KB
/
dumpifs.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
#define _DISABLE_MD5_
#define min(a,b) ((a)<=(b)?(a):(b))
#ifdef __USAGE
%C - dump an image file system
%C [-mvxbz -u file] [-f file] image_file_system_file [files]
-b Extract to basenames of files
-u file Put a copy of the uncompressed image file here
-v Verbose
-x Extract files
-m Display MD5 Checksum
-f file Extract named file
-z Disable the zero check while searching for the startup header.
This option should be avoided as it makes the search for the
startup header less reliable.
Note: this may not be supported in the future.
#endif
#ifdef QNX
#include <lib/compat.h>
#else
#include "sys/compat.h"
#endif
#ifdef _NTO_HDR_DIR_
#define _PLATFORM(x) x
#define PLATFORM(x) <_PLATFORM(x)sys/platform.h>
#include PLATFORM(_NTO_HDR_DIR_)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <utime.h>
#include <sys/stat.h>
#ifdef QNX
#include <sys/elf.h>
#include <sys/image.h>
#include <sys/startup.h>
#else
#include "sys/elf.h"
#include "sys/startup.h"
#include "sys/image.h"
#endif
#include <libgen.h>
#include <zlib.h>
#ifdef QNX
#include <lzo1x.h>
#else
#include <lzo/lzo1x.h>
#endif
#include <ucl/ucl.h>
//#include "xplatform.h"
//#include "md5.h"
#if defined(__MINGW32__)
#define mkdir(path, mode) mkdir(path)
#endif
char *progname;
int verbose;
unsigned flags;
char **check_files;
char *ucompress_file;
int zero_check_enabled = 1;
int files_to_extract;
int files_left_to_extract;
int processing_done;
struct extract_file {
struct extract_file *next;
char *path;
} *extract_files = NULL;
#define FLAG_EXTRACT 0x00000001
#define FLAG_DISPLAY 0x00000002
#define FLAG_BASENAME 0x00000004
#define FLAG_MD5 0x00000008
#if 1
#define ENDIAN_RET32(x) ((((x) >> 24) & 0xff) | \
(((x) >> 8) & 0xff00) | \
(((x) & 0xff00) << 8) | \
(((x) & 0xff) << 24))
#define ENDIAN_RET16(x) ((((x) >> 8) & 0xff) | \
(((x) & 0xff) << 8))
#else
#define ENDIAN_RET32(x) (x)
#define ENDIAN_RET16(x) (x)
#endif
#define CROSSENDIAN(big) (big)
void process(const char *file, FILE *fp);
void display_shdr(FILE *fp, int spos, struct startup_header *hdr);
void display_ihdr(FILE *fp, int ipos, struct image_header *hdr);
void process_file(FILE *fp, int ipos, struct image_file *ent);
void process_dir(FILE *fp, int ipos, struct image_dir *ent);
void process_symlink(FILE *fp, int ipos, struct image_symlink *ent);
void process_device(FILE *fp, int ipos, struct image_device *ent);
void display_file(FILE *fp, int ipos, struct image_file *ent);
#define MD5_LENGTH 16
void compute_md5(FILE *fp, int ipos, struct image_file *ent, unsigned char md5_result[16]);
int zero_ok (struct startup_header *shdr);
static int rifs_checksum(void *ptr, long len);
#if defined(__QNXNTO__) || defined(__SOLARIS__)
// Get basename()
#include <libgen.h>
#endif
void error(int level, char *p, ...) {
va_list ap;
fprintf(stderr, "%s: ", progname);
va_start(ap, p);
vfprintf(stderr, p, ap);
va_end(ap);
fputc('\n', stderr);
if(level == 0) {
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[]) {
int c;
char *image;
FILE *fp = NULL;
char *dest_dir_path = NULL;
int tfd = -1;
char tpath[_POSIX_PATH_MAX];
struct extract_file *ef;
progname = basename(argv[0]);
while((c = getopt(argc, argv, "f:d:mvxbu:z")) != -1) {
switch(c) {
case 'f':
ef = malloc( sizeof(*ef) );
if ( ef == NULL ) {
perror("recording path");
break;
}
ef->path = strdup(optarg);
if ( ef->path == NULL ) {
perror("recording path");
break;
}
ef->next = extract_files;
extract_files = ef;
files_to_extract++;
files_left_to_extract++;
flags |= FLAG_EXTRACT;
break;
case 'd':
dest_dir_path = strdup(optarg);
flags |= FLAG_EXTRACT;
break;
case 'm':
flags |= FLAG_MD5;
break;
case 'x':
flags |= FLAG_EXTRACT;
break;
case 'b':
flags |= FLAG_BASENAME;
break;
case 'u':
ucompress_file = optarg;
break;
case 'v':
verbose++;
break;
case 'z':
zero_check_enabled = 0;
break;
default:
break;
}
}
if(flags & (FLAG_EXTRACT|FLAG_MD5)) {
if(verbose > 1) {
flags |= FLAG_DISPLAY;
}
} else {
flags |= FLAG_DISPLAY;
}
if(optind >= argc) {
error(0, "Missing image file system name\n");
}
image = argv[optind++];
if(optind < argc) {
check_files = &argv[optind];
}
if(strcmp(image, "-") != 0) {
if(!(fp = fopen(image, "rb"))) {
error(0, "Unable to open file %s - %s", image, strerror(errno));
}
} else if(isatty(fileno(stdin))) {
error(0, "Must have an image file");
} else {
fp = stdin;
image = "-- stdin --";
}
if ( dest_dir_path != NULL ) {
/* This temp file is created to address the problems of creating a file in /dev/shmem */
sprintf( tpath, "%s/.dumpifs.%d", dest_dir_path, getpid() );
tfd = open( tpath, O_CREAT|O_RDONLY, 0644 );
if ( tfd == -1 ) {
if ( (access( dest_dir_path, W_OK ) != 0 ) && (mkdir( dest_dir_path, 0755) != 0)) {
error(0, "Cannot create directory %s to extract %s", dest_dir_path, image);
}
}
if (chdir(dest_dir_path) != 0) {
error(0, "Cannot cd to directory %s to extract %s", dest_dir_path, image);
}
}
process(image, fp);
if ( tfd != -1 ) {
close(tfd);
unlink( tpath );
}
return EXIT_SUCCESS;
}
int find(FILE *fp, const unsigned char *p, int len, int count) {
int c;
int i;
int n;
i = n = 0;
while(i < len && (c = fgetc(fp)) != EOF) {
n++;
if(c == p[i]) {
i++;
} else {
if(count != -1 && n >= count) {
break;
}
i = c == p[0] ? 1 : 0;
}
}
if(i < len) {
return -1;
}
return ftell(fp) - i;
}
int check(const char *name) {
char **p;
if(!(p = check_files)) {
return 0;
}
name = basename((char *)name);
while(*p) {
if(strcmp(name, *p) == 0) {
return 0;
}
p++;
}
return -1;
}
char *associate(int ix, char *value)
{
static int num = 0;
static char **text = NULL;
char **ptr;
if (value != NULL) {
if (ix >= num) {
if ((ptr = realloc(text, (ix + 1) * sizeof(char *))) == NULL)
return(value);
memset(&ptr[num], 0, (ix - num) * sizeof(char *));
text = ptr, num = ix + 1;
}
text[ix] = strdup(value);
}
else {
value = (ix < num) ? text[ix] : "";
}
return(value);
}
void display_script(FILE *fp, int pos, int len) {
int off;
char buff[512];
union script_cmd *hdr = (union script_cmd *)buff;
int size;
int ext_sched = SCRIPT_SCHED_EXT_NONE;
for(off = 0; off < len; off += size) {
fseek(fp, pos + off, SEEK_SET);
if(fread(hdr, sizeof *hdr, 1, fp) != 1) {
break;
}
if((size = hdr->hdr.size_lo | (hdr->hdr.size_hi << 8)) == 0) {
break;
}
if(size > sizeof *hdr) {
int n = min(sizeof buff, size - sizeof *hdr);
if(fread(hdr + 1, n, 1, fp) != 1) {
break;
}
}
printf(" ");
switch(hdr->hdr.type) {
case SCRIPT_TYPE_EXTERNAL: {
char *cmd, *args, *envs;
int i;
cmd = hdr->external.args;
envs = args = cmd + strlen(cmd) + 1;
for(i = 0; i < hdr->external.argc; i++) {
envs = envs + strlen(envs) + 1;
}
i = strcmp(basename(cmd), args);
if(i || (hdr->external.flags & (SCRIPT_FLAGS_SESSION | SCRIPT_FLAGS_KDEBUG | SCRIPT_FLAGS_SCHED_SET | SCRIPT_FLAGS_CPU_SET | SCRIPT_FLAGS_EXTSCHED))) {
printf("[ ");
if(i) {
printf("argv0=%s ", args);
}
if(hdr->external.flags & SCRIPT_FLAGS_SESSION) {
printf("+session ");
}
if(hdr->external.flags & SCRIPT_FLAGS_KDEBUG) {
printf("+debug ");
}
if(hdr->external.flags & SCRIPT_FLAGS_SCHED_SET) {
printf("priority=%d", hdr->external.priority);
switch(hdr->external.policy) {
case SCRIPT_POLICY_NOCHANGE:
break;
case SCRIPT_POLICY_FIFO:
printf("f");
break;
case SCRIPT_POLICY_RR:
printf("r");
break;
case SCRIPT_POLICY_OTHER:
printf("o");
break;
default:
printf("?%d?", hdr->external.policy);
break;
}
printf(" ");
}
if(hdr->external.flags & SCRIPT_FLAGS_CPU_SET) {
printf("cpu=%d ", hdr->external.cpu);
}
if(hdr->external.flags & SCRIPT_FLAGS_EXTSCHED) {
printf("sched_aps=%s ", associate(hdr->external.extsched.aps.id, NULL));
}
printf("] ");
}
for(i = 0; i < hdr->external.envc; i++) {
printf("%s ", envs);
envs = envs + strlen(envs) + 1;
}
printf("%s", cmd);
args = args + strlen(args) + 1;
for(i = 1; i < hdr->external.argc; i++) {
printf(" %s", args);
args = args + strlen(args) + 1;
}
if(hdr->external.flags & SCRIPT_FLAGS_BACKGROUND) {
printf(" &");
}
printf("\n");
break;
}
case SCRIPT_TYPE_WAITFOR:
printf("waitfor");
goto wait;
case SCRIPT_TYPE_REOPEN:
printf("reopen");
wait:
if(hdr->waitfor_reopen.checks_lo || hdr->waitfor_reopen.checks_hi) {
int checks = hdr->waitfor_reopen.checks_lo | hdr->waitfor_reopen.checks_hi << 8;
printf(" %d.%d", checks / 10, checks % 10);
}
printf(" %s\n", hdr->waitfor_reopen.fname);
break;
case SCRIPT_TYPE_DISPLAY_MSG:
printf("display_msg '%s'\n", hdr->display_msg.msg);
break;
case SCRIPT_TYPE_PROCMGR_SYMLINK:
printf("procmgr_symlink '%s' '%s'\n", hdr->procmgr_symlink.src_dest,
&hdr->procmgr_symlink.src_dest[strlen(hdr->procmgr_symlink.src_dest)+1]);
break;
case SCRIPT_TYPE_EXTSCHED_APS:
if (ext_sched == SCRIPT_SCHED_EXT_NONE)
associate(SCRIPT_APS_SYSTEM_PARTITION_ID, SCRIPT_APS_SYSTEM_PARTITION_NAME);
else if (ext_sched != SCRIPT_SCHED_EXT_APS)
printf("Invalid combination of SCHED_EXT features\n");
ext_sched = SCRIPT_SCHED_EXT_APS;
printf("sched_aps %s %d %d\n", associate(hdr->extsched_aps.id, hdr->extsched_aps.pname), hdr->extsched_aps.budget, hdr->extsched_aps.critical_hi << 8 | hdr->extsched_aps.critical_lo);
break;
default:
printf("Unknown type %d\n", hdr->hdr.type);
}
}
}
static char* hStart=0;
void process(const char *file, FILE *fp) {
struct startup_header shdr = { STARTUP_HDR_SIGNATURE };
int spos;
struct image_header ihdr = { IMAGE_SIGNATURE };
int ipos;
int dpos;
static char buf[0x10000];
static char out_buf[0x10000];
spos = -1;
hStart = (char*)&shdr.signature;
if((ipos = find(fp, (char*)&ihdr.signature, sizeof ihdr.signature, 0)) == -1) {
rewind(fp);
//find startup signature and verify its validity
while(1){
if((spos = find(fp, (char *)&shdr.signature, sizeof shdr.signature, -1)) == -1) {
shdr.signature = ENDIAN_RET32(shdr.signature);
rewind(fp);
if((spos = find(fp, (char *)&shdr.signature, sizeof shdr.signature, -1)) == -1) {
error(1, "Unable to find startup header in %s", file);
return;
}
}
if(fread((char *)&shdr + sizeof shdr.signature, sizeof shdr - sizeof shdr.signature, 1, fp) != 1) {
error(1, "Unable to read image %s", file);
return;
}
// if we are cross endian, flip the shdr members
{
// flip shdr members
}
//check the zero member of startup, one more check to confirm
//we found the correct signature
if (!zero_ok(&shdr)) {
if (zero_check_enabled)
continue;
if (verbose)
printf("Warning: Non zero data in zero fields ignored\n");
break;
} else {
break;
}
}
printf("Image startup_size: %d (0x%x)\n", shdr.startup_size, shdr.startup_size);
printf("Image stored_size: %d (0x%x)\n", shdr.stored_size, shdr.stored_size);
printf("Compressed size: %d (0x%x)\n", shdr.stored_size - shdr.startup_size - sizeof(struct image_trailer), shdr.stored_size - shdr.startup_size - sizeof(struct image_trailer));
// If the image is compressed we need to uncompress it into a
// tempfile and restart.
if((shdr.flags1 & STARTUP_HDR_FLAGS1_COMPRESS_MASK) != 0) {
FILE *fp2;
int n;
// Create a file to hold uncompressed image.
if(ucompress_file) {
fp2 = fopen(ucompress_file, "w+");
} else {
fp2 = tmpfile();
}
if(fp2 == NULL) {
error(1, "Unable to create a file to uncompress image.");
return;
}
// Copy non-compressed part.
rewind(fp);
if(CROSSENDIAN(shdr.flags1 & STARTUP_HDR_FLAGS1_BIGENDIAN))
for(n = 0 ; n < spos + ENDIAN_RET32(shdr.startup_size); ++n) {
putc(getc(fp), fp2);
}
else
for(n = 0 ; n < spos + shdr.startup_size; ++n) {
putc(getc(fp), fp2);
}
fflush(fp2);
// Uncompress compressed part
switch(shdr.flags1 & STARTUP_HDR_FLAGS1_COMPRESS_MASK) {
case STARTUP_HDR_FLAGS1_COMPRESS_ZLIB:
{
int fd;
gzFile *zin;
// We need an fd for zlib, and we cannot count on the fd
// position being the same as the FILE *'s, so fileno()
// and lseek.
fd = fileno(fp);
lseek(fd, ftell(fp), SEEK_SET);
if((zin = gzdopen(fd, "rb")) == NULL) {
error(1, "Unable to open decompression stream.");
return;
}
while((n = gzread(zin, buf, sizeof(buf))) > 0) {
printf("zlib Decompress\n");
fwrite(buf, n, 1, fp2);
}
}
break;
case STARTUP_HDR_FLAGS1_COMPRESS_LZO:
{
unsigned len, til=0, tol=0;
lzo_uint out_len;
int status;
if(lzo_init() != LZO_E_OK) {
error(1,"decompression init failure");
return;
}
printf("LZO Decompress @0x%0lx\n", ftell(fp));
for(;;) {
unsigned int nowPtr=ftell(fp);
int c1= getc(fp);
int c2= getc(fp);
len = (c1 << 8);
len += c2;
//len = getc(fp) << 8;
//len += getc(fp);
if(len == 0) break;
fread(buf, len, 1, fp);
printf("lzo1x_decompress(buf, %d...)\n");
status = lzo1x_decompress(buf, len, out_buf, &out_len, NULL);
if(status != LZO_E_OK) {
error(1, "decompression failure");
printf("decompression not success with code %d. out_len=%d. c1=%x, c2=%x @0x%x\n", status, out_len, c1, c2, nowPtr);
return;
}
til+=len;tol+=out_len;
printf("LZO Decompress rd=%d, wr=%d @ 0x%x\n", len, out_len, nowPtr);
fwrite(out_buf, out_len, 1, fp2);
}
printf("Decompressed %d bytes-> %d bytes\n", til, tol);
}
break;
case STARTUP_HDR_FLAGS1_COMPRESS_UCL:
{
unsigned len, til=0, tol=0;
ucl_uint out_len;
int status;
printf("UCL Decompress @0x%0lx\n", ftell(fp));
for(;;) {
unsigned int nowPtr = ftell(fp);
len = getc(fp) << 8;
len += getc(fp);
if(len == 0) break;
fread(buf, len, 1, fp);
status = ucl_nrv2b_decompress_8(buf, len, out_buf, &out_len, NULL);
if(status != 0) {
error(1, "decompression failure");
return;
}
til+=len;tol+=out_len;
printf("UCL Decompress rd=%d (0x%x), wr=%d @ 0x%x\n", len, len, out_len, nowPtr);
fwrite(out_buf, out_len, 1, fp2);
}
printf("Decompressed %d bytes-> %d bytes\n", til, tol);
}
break;
default:
error(1, "Unsupported compression type.");
return;
}
fclose(fp);
fp = fp2;
rewind(fp2);
}
if(CROSSENDIAN(shdr.flags1 & STARTUP_HDR_FLAGS1_BIGENDIAN)) {
unsigned long *p;
shdr.version = ENDIAN_RET16(shdr.version);
shdr.machine = ENDIAN_RET16(shdr.machine);
shdr.header_size = ENDIAN_RET16(shdr.header_size);
shdr.preboot_size = ENDIAN_RET16(shdr.preboot_size);
for(p = (unsigned long *)&shdr.startup_vaddr; (unsigned char *)p < (unsigned char *)&shdr + sizeof shdr; p++) {
if(p != (unsigned long *)&shdr.preboot_size) {
*p = ENDIAN_RET32(*p);
}
}
}
fseek(fp, spos + shdr.startup_size, SEEK_SET);
if((ipos = find(fp, ihdr.signature, sizeof ihdr.signature, -1)) == -1) {
error(1, "Unable to find image header in %s", file);
return;
}
}
if(fread((char *)&ihdr + sizeof ihdr.signature, sizeof ihdr - sizeof ihdr.signature, 1, fp) != 1) {
error(1, "Unable to read image %s", file);
return;
}
if(CROSSENDIAN(ihdr.flags & IMAGE_FLAGS_BIGENDIAN)) {
unsigned long *p;
for(p = (unsigned long *)&ihdr.image_size; (unsigned char *)p < (unsigned char *)&ihdr + offsetof(struct image_header, mountpoint); p++) {
*p = ENDIAN_RET32(*p);
}
}
dpos = ipos + ihdr.dir_offset;
if(flags & FLAG_DISPLAY) {
printf(" Offset Size Name\n");
if(spos != -1) {
if(spos) {
if(check("*.boot") == 0) {
printf(" %8x %8x %s\n", 0, spos, "*.boot");
}
}
display_shdr(fp, spos, &shdr);
if(check("startup.*") == 0) {
printf(" %8x %8lx %s\n", spos + sizeof shdr, shdr.startup_size - sizeof shdr, "startup.*");
}
}
display_ihdr(fp, ipos, &ihdr);
if(check("Image-directory") == 0) {
printf(" %8x %8lx %s\n", dpos, ihdr.hdr_dir_size - ihdr.dir_offset, "Image-directory");
}
}
while(!processing_done) {
char buff[1024];
union image_dirent *dir = (union image_dirent *)buff;
fseek(fp, dpos, SEEK_SET);
if(fread(&dir->attr, sizeof dir->attr, 1, fp) != 1) {
error(1, "Early end reading directory");
break;
}
if(CROSSENDIAN(ihdr.flags & IMAGE_FLAGS_BIGENDIAN)) {
unsigned long *p;
dir->attr.size = ENDIAN_RET16(dir->attr.size);
dir->attr.extattr_offset = ENDIAN_RET16(dir->attr.extattr_offset);
for(p = (unsigned long *)&dir->attr.ino; (unsigned char *)p < (unsigned char *)dir + sizeof dir->attr; p++) {
*p = ENDIAN_RET32(*p);
}
}
if(dir->attr.size < sizeof dir->attr) {
if(dir->attr.size != 0) {
error(1, "Invalid dir entry");
}
break;
}
if(dir->attr.size > sizeof dir->attr) {
if(fread((char *)dir + sizeof dir->attr, min(sizeof buff, dir->attr.size) - sizeof dir->attr, 1, fp) != 1) {
error(1, "Error reading directory");
break;
}
}
dpos += dir->attr.size;
switch(dir->attr.mode & S_IFMT) {
case S_IFREG:
if(CROSSENDIAN(ihdr.flags & IMAGE_FLAGS_BIGENDIAN)) {
dir->file.offset = ENDIAN_RET32(dir->file.offset);
dir->file.size = ENDIAN_RET32(dir->file.size);
}
process_file(fp, ipos, &dir->file);
if(dir->attr.ino == ihdr.script_ino && verbose > 1) {
display_script(fp, ipos + dir->file.offset, dir->file.size);
}
break;
case S_IFDIR:
process_dir(fp, ipos, &dir->dir);
break;
case S_IFLNK:
if(CROSSENDIAN(ihdr.flags & IMAGE_FLAGS_BIGENDIAN)) {
dir->symlink.sym_offset = ENDIAN_RET16(dir->symlink.sym_offset);
dir->symlink.sym_size = ENDIAN_RET16(dir->symlink.sym_size);
}
process_symlink(fp, ipos, &dir->symlink);
break;
case S_IFCHR:
case S_IFBLK:
case S_IFIFO:
case S_IFNAM:
if(CROSSENDIAN(ihdr.flags & IMAGE_FLAGS_BIGENDIAN)) {
dir->device.dev = ENDIAN_RET32(dir->device.dev);
dir->device.rdev = ENDIAN_RET32(dir->device.rdev);
}
process_device(fp, ipos, &dir->device);
// dir->device;
break;
default:
error(1, "Unknown type\n");
break;
}
}
if(flags & FLAG_DISPLAY) {
struct image_trailer itlr;
struct startup_trailer stlr;
fseek(fp, ipos + ihdr.image_size-sizeof(itlr), SEEK_SET);
if(fread(&itlr, sizeof(itlr), 1, fp) != 1) {
error(1, "Early end reading image trailer");
return;
}
if(CROSSENDIAN(shdr.flags1 & STARTUP_HDR_FLAGS1_BIGENDIAN))
printf("Checksums: image=%#lx", ENDIAN_RET32(itlr.cksum));
else
printf("Checksums: image=%#lx", itlr.cksum);
if(spos != -1) {
fseek(fp, spos + shdr.startup_size-sizeof(stlr), SEEK_SET);
if(fread(&stlr, sizeof(stlr), 1, fp) != 1) {
printf("\n");
error(1, "Early end reading startup trailer");
return;
}
if(CROSSENDIAN(shdr.flags1 & STARTUP_HDR_FLAGS1_BIGENDIAN))
printf(" startup=%#lx", ENDIAN_RET32(stlr.cksum));
else
printf(" startup=%#lx", stlr.cksum);
}
printf("\n");
//printf("ipos=%#lx spos=%#lx ihdr.image_size=%#ld - sizeof(stlr) = %#ld\n", ipos, spos, ihdr.image_size, ihdr.image_size-sizeof(itlr));
{
int data;
int sum;
int counted=0;
int len = ihdr.image_size;
unsigned max;
fseek(fp, ipos , SEEK_SET);
sum = 0;
while(len > 4)
{
fread(&data, 4, 1, fp);
sum += data;
counted +=4;
len -= 4;
}
sum = -1*sum;
if(0 != (itlr.cksum - sum))
{
printf("\nStored checksum not correct!\n");
printf("Expected checksum: %#lx\n", sum);
printf(" NG: %02x %02x %02x %02x\n", itlr.cksum & 0xff, (itlr.cksum >> 8) & 0xff, (itlr.cksum >> 16) & 0xff, (itlr.cksum >> 24) & 0xff);
printf("GOOD: %02x %02x %02x %02x\n", sum & 0xff, (sum >> 8) & 0xff, (sum >> 16) & 0xff, (sum >> 24) & 0xff);
}
}
printf("Image startup_size: %d (0x%x)\n", shdr.startup_size, shdr.startup_size);
printf("Image stored_size: %d (0x%x)\n", shdr.stored_size, shdr.stored_size);
printf("Compressed size: %d (0x%x)\n", shdr.stored_size - shdr.startup_size - sizeof(itlr), shdr.stored_size - shdr.startup_size - sizeof(itlr));
}
}
int copy(FILE *from, FILE *to, int nbytes) {
char buff[4096];
int n;
while(nbytes > 0) {
n = min(sizeof buff, nbytes);
if(fread(buff, n, 1, from) != 1) {
return -1;
}
if(fwrite(buff, n, 1, to) != 1) {
return -1;
}
nbytes -= n;
}
return 0;
}
void display_shdr(FILE *fp, int spos, struct startup_header *hdr) {
if(check("Startup-header") != 0) {
return;
}
printf(" %8x %8x %s flags1=%#x flags2=%#x paddr_bias=%#lx\n",
spos, hdr->header_size, "Startup-header",
hdr->flags1, hdr->flags2,
hdr->paddr_bias);
if(verbose) {
printf(" preboot_size=%#lx\n", (unsigned long)hdr->preboot_size);
printf(" image_paddr=%#lx stored_size=%#lx\n", (unsigned long)hdr->image_paddr, (unsigned long)hdr->stored_size);
printf(" startup_size=%#lx imagefs_size=%#lx\n", (unsigned long)hdr->startup_size, (unsigned long)hdr->imagefs_size);
printf(" ram_paddr=%#lx ram_size=%#lx\n", (unsigned long)hdr->ram_paddr, (unsigned long)hdr->ram_size);
printf(" startup_vaddr=%#lx\n", (unsigned long)hdr->startup_vaddr);
}
}
void display_ihdr(FILE *fp, int ipos, struct image_header *hdr) {
if(check("Image-header") != 0) {
return;
}
printf(" %8x %8x %s", ipos, sizeof *hdr, "Image-header");
if(hdr->mountpoint[0]) {
char buff[512];
fseek(fp, ipos + offsetof(struct image_header, mountpoint), SEEK_SET);
if(fread(buff, min(sizeof buff, hdr->dir_offset - offsetof(struct image_header, mountpoint)), 1, fp) != 1) {
error(1, "Unable to read image mountpoint");
} else {
printf(" mountpoint=%s", buff);
}
}
printf("\n");
if(verbose) {
int i;
printf(" flags=%#x", hdr->flags);
if(hdr->chain_paddr) {
printf(" chain=%#lx", hdr->chain_paddr);
}
if(hdr->script_ino) {
printf(" script=%ld", hdr->script_ino);
}
for(i = 0; i < sizeof hdr->boot_ino / sizeof hdr->boot_ino[0]; i++) {
if(hdr->boot_ino[i]) {
printf(" boot=%ld", hdr->boot_ino[i]);
for(i++; i < sizeof hdr->boot_ino / sizeof hdr->boot_ino[0]; i++) {
if(hdr->boot_ino[i]) {
printf(",%ld", hdr->boot_ino[i]);
}
}
break;
}
}
for(i = 0; i < sizeof hdr->spare / sizeof hdr->spare[0]; i++) {
if(hdr->spare[i]) {
printf(" spare=%#lx", hdr->spare[0]);
for(i = 1; i < sizeof hdr->spare / sizeof hdr->spare[0]; i++) {
printf(",%ld", hdr->spare[i]);
}
break;
}
}
printf(" mntflg=%#lx\n", hdr->mountflags);
}
}
void display_attr(struct image_attr *attr) {
printf(" gid=%ld uid=%ld mode=%#lo ino=%ld mtime=%lx\n", attr->gid, attr->uid, attr->mode & ~S_IFMT, attr->ino, attr->mtime);
}
void display_dir(FILE *fp, int ipos, struct image_dir *ent) {
if(check(ent->path[0] ? ent->path : "Root-dirent") != 0) {
return;
}
printf(" ---- ---- %s\n", ent->path[0] ? ent->path : "Root-dirent");
if(verbose) {
display_attr(&ent->attr);
}
}
void process_dir(FILE *fp, int ipos, struct image_dir *ent) {
if(flags & FLAG_DISPLAY) {
display_dir(fp, ipos, ent);
}
}
void display_symlink(FILE *fp, int ipos, struct image_symlink *ent) {
if(check(ent->path) != 0) {
return;
}
printf(" ---- %8x %s -> %s\n", ent->sym_size, ent->path, &ent->path[ent->sym_offset]);
if(verbose) {
display_attr(&ent->attr);
}
}
void process_symlink(FILE *fp, int ipos, struct image_symlink *ent) {
if(flags & FLAG_DISPLAY) {
display_symlink(fp, ipos, ent);
}
}
void display_device(FILE *fp, int ipos, struct image_device *ent) {
if(check(ent->path) != 0) {
return;
}
printf(" ---- ---- %s dev=%ld rdev=%ld\n", ent->path, ent->dev, ent->rdev);
if(verbose) {
display_attr(&ent->attr);
}
}
void process_device(FILE *fp, int ipos, struct image_device *ent) {
if(flags & FLAG_DISPLAY) {
display_device(fp, ipos, ent);
}
}
int mkdir_p(const char *path)
{
/* Adapted from http://stackoverflow.com/a/2336245/119527 */
const size_t len = strlen(path);
char _path[PATH_MAX];
char *p;
errno = 0;
/* Copy string so its mutable */
if (len > sizeof(_path)-1) {
errno = ENAMETOOLONG;
return -1;
}
strcpy(_path, path);
/* Iterate the string */
for (p = _path + 1; *p; p++) {
if (*p == '/') {
/* Temporarily truncate */
*p = '\0';
if (mkdir(_path, S_IRWXU) != 0) {
if (errno != EEXIST)
return -1;
}
*p = '/';
}
}
if (mkdir(_path, S_IRWXU) != 0) {
if (errno != EEXIST)
return -1;
}
return 0;
}
void extract_file(FILE *fp, int ipos, struct image_file *ent) {
FILE *dst;
struct utimbuf buff;
struct extract_file *ef;