-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunyaffs2.c
1779 lines (1454 loc) · 41.8 KB
/
unyaffs2.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
/*
* yaffs2utils: Utilities to make/extract a YAFFS2/YAFFS1 image.
* Copyright (C) 2010-2011 Luen-Yung Lin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "configs.h"
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <libgen.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/types.h>
#ifdef _HAVE_LUTIMES
#include <sys/time.h>
#else
#include <utime.h>
#endif
#ifdef _HAVE_OSX_SYSLIMITS
#include <sys/syslimits.h>
#endif
#include "yaffs_packedtags1.h"
#include "yaffs_packedtags2.h"
#include "yaffs_trace.h"
#include "list.h"
#include "safe_rw.h"
#include "progress_bar.h"
#include "endian_convert.h"
#include "nand_ecclayout.h"
#include "version.h"
/*----------------------------------------------------------------------------*/
#define UNYAFFS2_OBJTABLE_SIZE 4096
#define UNYAFFS2_HARDLINK_MAX 127
#define UNYAFFS2_FLAGS_NONROOT (1 << 0)
#define UNYAFFS2_FLAGS_SHOWBAR (1 << 1)
#define UNYAFFS2_FLAGS_YAFFS1 (1 << 16)
#define UNYAFFS2_FLAGS_ENDIAN (1 << 17)
#define UNYAFFS2_FLAGS_YAFFSECC (1 << 18)
#define UNYAFFS2_FLAGS_VERBOSE (1 << 19)
#define UNYAFFS2_ISSHOWBAR (unyaffs2_flags & UNYAFFS2_FLAGS_SHOWBAR)
#define UNYAFFS2_ISYAFFS1 (unyaffs2_flags & UNYAFFS2_FLAGS_YAFFS1)
#define UNYAFFS2_ISENDIAN (unyaffs2_flags & UNYAFFS2_FLAGS_ENDIAN)
#define UNYAFFS2_ISYAFFSECC (unyaffs2_flags & UNYAFFS2_FLAGS_YAFFSECC)
#define UNYAFFS2_ISVERBOSE (unyaffs2_flags & UNYAFFS2_FLAGS_VERBOSE)
#define UNYAFFS2_PRINTF(s, args...) \
do { \
if (!UNYAFFS2_ISVERBOSE && UNYAFFS2_ISSHOWBAR) { \
unyaffs2_flags &= ~UNYAFFS2_FLAGS_SHOWBAR; \
fprintf(stderr, "\n"); \
} \
fprintf(stdout, s, ##args); \
fflush(stdout); \
} while (0)
#define UNYAFFS2_ERROR_PRINTF(s, args...) \
do { \
if (!UNYAFFS2_ISVERBOSE && UNYAFFS2_ISSHOWBAR) { \
unyaffs2_flags &= ~UNYAFFS2_FLAGS_SHOWBAR; \
fprintf(stderr, "\n"); \
} \
fprintf(stderr, s, ##args); \
fflush(stderr); \
} while (0)
#define UNYAFFS2_HELP(s, args...) \
UNYAFFS2_PRINTF(s, ##args)
#define UNYAFFS2_WARN(s, args...) \
UNYAFFS2_ERROR_PRINTF(s, ##args)
#define UNYAFFS2_ERROR(s, args...) \
UNYAFFS2_ERROR_PRINTF(s, ##args)
#ifdef _UNYAFFS2_DEBUG
#define UNYAFFS2_DEBUG(s, args...) \
UNYAFFS2_ERROR_PRINTF("%s: " s, __FUNCTION__, ##args)
#else
#define UNYAFFS2_DEBUG(s, args...)
#endif
#define UNYAFFS2_VERBOSE(s, args...) \
do { \
if (UNYAFFS2_ISVERBOSE) \
UNYAFFS2_PRINTF(s, ##args); \
} while (0)
#define UNYAFFS2_PROGRESS_INIT() \
do { \
if (!UNYAFFS2_ISVERBOSE) \
progress_init(); \
} while (0)
#define UNYAFFS2_PROGRESS_BAR(objs, total) \
do { \
if (!UNYAFFS2_ISVERBOSE) { \
unyaffs2_flags |= UNYAFFS2_FLAGS_SHOWBAR; \
progress_bar(objs, total); \
} \
} while(0)
/*----------------------------------------------------------------------------*/
typedef struct unyaffs2_file_var {
loff_t file_size;
off_t file_head;
} unyaffs2_file_var_t;
typedef struct unyaffs2_symlink_var {
char *alias;
} unyaffs2_symlink_var_t;
typedef struct unyaffs2_hardlink_var {
struct unyaffs2_obj *equiv_obj;
} unyaffs2_hardlink_var_t;
typedef struct unyaffs2_dev_var {
unsigned rdev;
} unyaffs2_dev_var_t;
typedef union unyaffs2_file_variant {
struct unyaffs2_file_var file;
struct unyaffs2_symlink_var symlink;
struct unyaffs2_hardlink_var hardlink;
struct unyaffs2_dev_var dev;
} unyaffs2_file_variant_t;
typedef struct unyaffs2_obj {
unsigned char valid:1;
unsigned char extracted:1; /* 1 when extracted. */
off_t hdr_off; /* header offset in the image */
unsigned obj_id;
unsigned parent_id;
struct unyaffs2_obj *parent_obj;
unsigned type; /* file type */
union unyaffs2_file_variant variant;
unsigned mode; /* file mode */
unsigned uid; /* owner uid */
unsigned gid; /* owner gid */
unsigned atime;
unsigned mtime;
unsigned ctime;
char name[NAME_MAX + 1];
struct list_head hashlist; /* hash table */
struct list_head children; /* if it is the directory */
struct list_head siblings; /* neighbors of fs tree */
} unyaffs2_obj_t;
typedef struct unyaffs2_fstree {
unsigned objs;
struct unyaffs2_obj *root;
} unyaffs2_fstree_t;
typedef struct unyaffs2_specfile {
char *path; /* file path */
struct unyaffs2_obj *obj; /* object */
struct list_head list; /* specified files list */
} unyaffs2_specfile_t;
#ifdef _HAVE_MMAP
typedef struct unyaffs2_mmap {
unsigned char *addr;
size_t size;
} unyaffs2_mmap_t;
#endif
/*----------------------------------------------------------------------------*/
static unsigned unyaffs2_chunksize = 0;
static unsigned unyaffs2_sparesize = 0;
static unsigned unyaffs2_flags = 0;
static unsigned unyaffs2_image_objs = 0;
static unsigned unyaffs2_bufsize = 0;
static unsigned char *unyaffs2_databuf = NULL;
static int unyaffs2_image_fd = -1;
static char unyaffs2_curfile[PATH_MAX + PATH_MAX] = {0};
static char unyaffs2_linkfile[PATH_MAX + PATH_MAX] = {0};
static LIST_HEAD(unyaffs2_specfile_list); /* specfied files */
static nand_ecclayout_t *unyaffs2_ecclayout = NULL;
static struct unyaffs2_fstree unyaffs2_objtree = {0};
static struct list_head unyaffs2_objtable[UNYAFFS2_OBJTABLE_SIZE];
#ifdef _HAVE_MMAP
static struct unyaffs2_mmap unyaffs2_mmapinfo = {0};
#endif
static void
(*unyaffs2_extract_ptags) (struct yaffs_ext_tags *, unsigned char *,
nand_ecclayout_t *, int) = NULL;
/*----------------------------------------------------------------------------*/
static struct unyaffs2_obj *
unyaffs2_obj_alloc (void)
{
struct unyaffs2_obj *obj;
obj = calloc(sizeof(struct unyaffs2_obj), sizeof(unsigned char));
if (obj == NULL)
return NULL;
obj->parent_obj = obj;
INIT_LIST_HEAD(&obj->children);
INIT_LIST_HEAD(&obj->siblings);
INIT_LIST_HEAD(&obj->hashlist);
return obj;
}
static void
unyaffs2_obj_free (struct unyaffs2_obj *obj)
{
if (obj->type == YAFFS_OBJECT_TYPE_SYMLINK &&
obj->variant.symlink.alias != NULL)
free(obj->variant.symlink.alias);
list_del(&obj->children);
list_del(&obj->siblings);
list_del(&obj->hashlist);
free(obj);
}
/*----------------------------------------------------------------------------*/
/*
* hash table to look up objects
*/
static inline unsigned
unyaffs2_objtable_hash (unsigned hash)
{
return hash % UNYAFFS2_OBJTABLE_SIZE;
}
static inline void
unyaffs2_objtable_insert (struct unyaffs2_obj *obj)
{
unsigned n = unyaffs2_objtable_hash(obj->obj_id);
list_add_tail(&obj->hashlist, &unyaffs2_objtable[n]);
}
static inline struct unyaffs2_obj *
unyaffs2_objtable_find (unsigned obj_id)
{
unsigned n = unyaffs2_objtable_hash(obj_id);
struct list_head *p;
struct unyaffs2_obj *obj;
list_for_each(p, &unyaffs2_objtable[n]) {
obj = list_entry(p, unyaffs2_obj_t, hashlist);
if (obj->obj_id == obj_id)
return obj;
}
return NULL;
}
static struct unyaffs2_obj *
unyaffs2_objtable_find_alloc (unsigned obj_id)
{
struct unyaffs2_obj *obj;
obj = unyaffs2_objtable_find(obj_id);
if (obj)
return obj;
obj = unyaffs2_obj_alloc();
if (obj) {
obj->obj_id = obj_id;
unyaffs2_objtable_insert(obj);
}
return obj;
}
static inline void
unyaffs2_objtable_init (void)
{
unsigned n;
for (n = 0; n < UNYAFFS2_OBJTABLE_SIZE; n++)
INIT_LIST_HEAD(&unyaffs2_objtable[n]);
}
static inline void
unyaffs2_objtable_exit (void)
{
unsigned i;
struct list_head *p, *n;
struct unyaffs2_obj *obj;
for (i = 0; i < UNYAFFS2_OBJTABLE_SIZE; i++) {
list_for_each_safe(p, n, &unyaffs2_objtable[i]) {
obj = list_entry(p, unyaffs2_obj_t, hashlist);
unyaffs2_obj_free(obj);
}
}
}
/*----------------------------------------------------------------------------*/
/*
* fs tree for objects extraction.
*/
static void
unyaffs2_objtree_cleanup (struct unyaffs2_obj *obj)
{
struct list_head *p, *n;
struct unyaffs2_obj *child;
if (obj == NULL)
return;
list_for_each_safe(p, n, &obj->children) {
child = list_entry(p, unyaffs2_obj_t, siblings);
unyaffs2_objtree_cleanup(child);
}
unyaffs2_obj_free(obj);
}
static struct unyaffs2_fstree *
unyaffs2_objtree_init (struct unyaffs2_fstree *fst)
{
struct unyaffs2_fstree *f = fst;
if (f == NULL) {
f = malloc(sizeof(struct unyaffs2_fstree));
if (f == NULL)
return NULL;
}
/* initialize */
memset(f, 0, sizeof(struct unyaffs2_fstree));
return f;
}
static void
unyaffs2_objtree_exit (struct unyaffs2_fstree *fst)
{
return unyaffs2_objtree_cleanup(fst->root);
}
/*----------------------------------------------------------------------------*/
/*
* specfied files
*/
static struct unyaffs2_specfile *
unyaffs2_specfile_find (const char *path)
{
struct unyaffs2_specfile *spec;
struct list_head *p;
size_t len;
list_for_each(p, &unyaffs2_specfile_list) {
spec = list_entry(p, unyaffs2_specfile_t, list);
if (!strncmp(path, spec->path, strlen(spec->path))) {
len = strlen(spec->path);
if (path[len] == '\0' || path[len] == '/')
return spec;
}
}
return NULL;
}
static int
unyaffs2_specfile_insert (const char *path)
{
char *s, *e;
size_t len;
struct unyaffs2_specfile *spec;
/* skipping leading '/' */
s = (char *)path;
while (s[0] != '\0' && s[0] == '/')
s++;
/* skipping following '/' */
e = (char *)path + strlen(path) - 1;
while (e > s && e[0] == '/')
e--;
if (e <= s) {
UNYAFFS2_DEBUG("unsupport path format: %s\n", path);
return -1;
}
spec = calloc(sizeof(struct unyaffs2_specfile), sizeof(unsigned char));
if (!spec) {
UNYAFFS2_DEBUG("allocate obj failed for '%s': %s\n",
path, strerror(errno));
return -1;
}
len = e - s + 1;
spec->path = calloc(len + 1, sizeof(unsigned char));
if (spec->path == NULL) {
UNYAFFS2_DEBUG("allocate string failed for '%s': %s\n",
path, strerror(errno));
free(spec);
return -1;
}
memcpy(spec->path, s, len);
spec->path[len] = '\0';
list_add_tail(&spec->list, &unyaffs2_specfile_list);
return 0;
}
static void
unyaffs2_specfile_exit (void)
{
struct unyaffs2_specfile *spec;
struct list_head *p, *n;
list_for_each_safe(p, n, &unyaffs2_specfile_list) {
spec = list_entry(p, unyaffs2_specfile_t, list);
if (spec->path)
free(spec->path);
list_del(&spec->list);
free(spec);
}
}
/*----------------------------------------------------------------------------*/
static int
unyaffs2_mkdir (const char *name, const mode_t mode)
{
char *p = NULL;
const char *dname;
struct stat statbuf;
dname = strlen(name) ? name : ".";
p = (char *)dname;
while ((p = strchr(p, '/')) != NULL) {
*p = '\0';
mkdir(dname, 0755);
*p++ = '/';
}
if (mkdir(dname, mode) < 0 &&
(stat(dname, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode)))
return -1;
return 0;
}
static char *
unyaffs2_prefix_basestr(const char *path, const char *prefix)
{
char *p = NULL;
size_t len = 0;
if ((len = strlen(prefix)) != 0 &&
strncmp(path, prefix, len) == 0 &&
(path[len] == '\0' || path[len] == '/')) {
p = strrchr(prefix, '/');
p = p ? (char *)path + (p - prefix) + 1: (char *)path;
}
return p;
}
/*----------------------------------------------------------------------------*/
static size_t
unyaffs2_spare2ptags (unsigned char *tag, unsigned char *spare, size_t bytes,
nand_ecclayout_t *ecclayout)
{
unsigned i;
size_t copied = 0;
struct nand_oobfree *oobfree = ecclayout->oobfree;
for (i = 0; i < 8 && copied < bytes; i++) {
size_t size = bytes - copied;
unsigned char *s = spare + oobfree[i].offset;
if (size > oobfree[i].length)
size = oobfree[i].length;
memcpy(tag, s, size);
copied += size;
tag += size;
}
return copied;
}
static void
unyaffs2_extract_ptags1 (struct yaffs_ext_tags *t, unsigned char *pt,
nand_ecclayout_t *ecclayout, int ecc)
{
struct yaffs_packed_tags1 pt1;
nand_ecclayout_t *l = ecclayout ? ecclayout : unyaffs2_ecclayout;
memset(&pt1, 0xff, sizeof(struct yaffs_packed_tags1));
unyaffs2_spare2ptags((unsigned char *)&pt1, pt,
sizeof(struct yaffs_packed_tags1), l);
if (UNYAFFS2_ISENDIAN)
packedtags1_endian_convert(&pt1, 1);
yaffs_unpack_tags1(t, &pt1);
}
static void
unyaffs2_extract_ptags2 (struct yaffs_ext_tags *t, unsigned char *s,
nand_ecclayout_t *ecclayout, int ecc)
{
int result;
enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
struct yaffs_ecc_other tag_ecc;
struct yaffs_packed_tags2 pt2;
nand_ecclayout_t *l = ecclayout ? ecclayout : unyaffs2_ecclayout;
memset(&pt2, 0xff, sizeof(struct yaffs_packed_tags2));
unyaffs2_spare2ptags((unsigned char *)&pt2, s,
sizeof(struct yaffs_packed_tags2), l);
if (pt2.t.seq_number != 0xffffffff && ecc) {
if (UNYAFFS2_ISENDIAN)
packedtags2_eccother_endian_convert(&pt2);
yaffs_ecc_calc_other((unsigned char *)&pt2.t,
sizeof(struct yaffs_packed_tags2_tags_only),
&tag_ecc);
result = yaffs_ecc_correct_other((unsigned char *)&pt2.t,
sizeof(struct yaffs_packed_tags2_tags_only),
&pt2.ecc, &tag_ecc);
switch (result) {
case 0:
ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
break;
case 1:
ecc_result = YAFFS_ECC_RESULT_FIXED;
break;
case -1:
ecc_result = YAFFS_ECC_RESULT_UNFIXED;
break;
default:
ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
}
}
if (UNYAFFS2_ISENDIAN)
packedtags2_tagspart_endian_convert(&pt2);
yaffs_unpack_tags2_tags_only(t, &pt2.t);
t->ecc_result = ecc_result;
}
static inline int
unyaffs2_isempty (unsigned char *buf, unsigned size)
{
while (size--) {
if (*buf != 0xff)
return 0;
buf++;
}
return 1;
}
static inline loff_t
unyaffs2_extract_oh_size (struct yaffs_obj_hdr *oh)
{
loff_t retval;
if(~(oh->file_size_high))
retval = (((loff_t) oh->file_size_high) << 32) |
(((loff_t) oh->file_size_low) & 0xFFFFFFFF);
else
retval = (loff_t) oh->file_size_low;
return retval;
}
static int
unyaffs2_oh2obj (struct unyaffs2_obj *obj, struct yaffs_obj_hdr *oh)
{
switch (oh->type) {
case YAFFS_OBJECT_TYPE_FILE:
obj->type = YAFFS_OBJECT_TYPE_FILE;
obj->variant.file.file_size = unyaffs2_extract_oh_size(oh);
break;
case YAFFS_OBJECT_TYPE_SYMLINK:
obj->type = YAFFS_OBJECT_TYPE_SYMLINK;
obj->variant.symlink.alias = strdup(oh->alias);
break;
case YAFFS_OBJECT_TYPE_DIRECTORY:
obj->type = YAFFS_OBJECT_TYPE_DIRECTORY;
break;
case YAFFS_OBJECT_TYPE_HARDLINK:
obj->type = YAFFS_OBJECT_TYPE_HARDLINK;
obj->variant.hardlink.equiv_obj =
unyaffs2_objtable_find(oh->equiv_id);
break;
case YAFFS_OBJECT_TYPE_SPECIAL:
switch (oh->yst_mode & S_IFMT) {
case S_IFCHR:
obj->type = YAFFS_OBJECT_TYPE_CHR;
break;
case S_IFBLK:
obj->type = YAFFS_OBJECT_TYPE_BLK;
break;
case S_IFIFO:
obj->type = YAFFS_OBJECT_TYPE_FIFO;
break;
case S_IFSOCK:
obj->type = YAFFS_OBJECT_TYPE_SOCK;
break;
default:
obj->type = YAFFS_OBJECT_TYPE_UNKNOWN;
break;
}
if (obj->type != YAFFS_OBJECT_TYPE_UNKNOWN)
obj->variant.dev.rdev = oh->yst_rdev;
break;
default:
obj->type = YAFFS_OBJECT_TYPE_UNKNOWN;
return -1;
}
obj->parent_id = oh->parent_obj_id;
strncpy(obj->name, oh->name, NAME_MAX);
if (obj->type != YAFFS_OBJECT_TYPE_HARDLINK &&
obj->type != YAFFS_OBJECT_TYPE_UNKNOWN) {
obj->mode = oh->yst_mode;
obj->uid = oh->yst_uid;
obj->gid = oh->yst_gid;
obj->atime = oh->yst_atime;
obj->mtime = oh->yst_mtime;
obj->ctime = oh->yst_ctime;
}
return 0;
}
/*----------------------------------------------------------------------------*/
static void
unyaffs2_format_filepath (char *path, size_t size, struct unyaffs2_obj *obj)
{
size_t pathlen = 0;
if (obj->obj_id == YAFFS_OBJECTID_ROOT)
return;
pathlen = strlen(path);
unyaffs2_format_filepath(path, size, obj->parent_obj);
if (pathlen < strlen(path))
strncat(path, "/", size - strlen(path) - 1);
strncat(path, obj->name, size - strlen(path) - 1);
}
static struct unyaffs2_obj *
unyaffs2_follow_hardlink (struct unyaffs2_obj *obj)
{
unsigned link_counts = 0;
struct unyaffs2_obj *equiv = obj;
while (equiv && equiv->valid &&
equiv->type == YAFFS_OBJECT_TYPE_HARDLINK) {
equiv = equiv->variant.hardlink.equiv_obj;
if (++link_counts > UNYAFFS2_HARDLINK_MAX) {
/* make sure everything is right */
UNYAFFS2_DEBUG("too many links for object %d\n",
obj->obj_id);
break;
}
}
return (equiv && equiv->valid &&
equiv->type != YAFFS_OBJECT_TYPE_HARDLINK) ? equiv : NULL;
}
static void
unyaffs2_obj_chattr (const char *fpath, struct unyaffs2_obj *obj)
{
/* access time */
#ifdef _HAVE_LUTIMES
struct timeval ftime[2];
ftime[0].tv_sec = obj->atime;
ftime[0].tv_usec = 0;
ftime[1].tv_sec = obj->mtime;
ftime[1].tv_usec = 0;
lutimes(fpath, ftime);
#else
struct utimbuf ftime;
ftime.actime = obj->atime;
ftime.modtime = obj->mtime;
utime(fpath, &ftime);
#endif
/* owner */
lchown(fpath, obj->uid, obj->gid);
/* mode - no effect on symbolic link */
if (obj->type != YAFFS_OBJECT_TYPE_SYMLINK)
chmod(fpath, obj->mode);
}
static int
unyaffs2_objtree_chattr (struct unyaffs2_obj *obj)
{
struct list_head *p;
struct unyaffs2_obj *child;
/* format the file path */
if (unyaffs2_curfile[0] != '\0' &&
unyaffs2_curfile[strlen(unyaffs2_curfile) - 1] != '/')
strncat(unyaffs2_curfile, "/",
sizeof(unyaffs2_curfile) -
strlen(unyaffs2_curfile) - 1);
strncat(unyaffs2_curfile, obj->name,
sizeof(unyaffs2_curfile) -
strlen(unyaffs2_curfile) - 1);
/* travel */
if (obj->type == YAFFS_OBJECT_TYPE_DIRECTORY) {
list_for_each(p, &obj->children) {
child = list_entry(p, unyaffs2_obj_t, siblings);
unyaffs2_objtree_chattr(child);
}
}
if (obj->type != YAFFS_OBJECT_TYPE_HARDLINK)
unyaffs2_obj_chattr(unyaffs2_curfile, obj);
/* restore current file path */
if (!strcmp(dirname(unyaffs2_curfile), "."))
unyaffs2_curfile[0] = '\0';
return 0;
}
/*----------------------------------------------------------------------------*/
static inline void
unyaffs2_scan_img_status (unsigned status)
{
char st = '-';
switch (status % 4) {
case 1:
st = '\\';
break;
case 2:
st = '|';
break;
case 3:
st = '/';
break;
default:
st = '-';
break;
}
UNYAFFS2_PRINTF("\b\b\b[%c]", st);
fflush(stdout);
}
static int
unyaffs2_scan_chunk (unsigned char *buffer, off_t offset)
{
struct yaffs_obj_hdr oh;
struct yaffs_ext_tags tag;
struct unyaffs2_obj *obj;
unyaffs2_extract_ptags(&tag, buffer + unyaffs2_chunksize, NULL, 1);
if (tag.ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
UNYAFFS2_DEBUG("invalid page skipped @ offset %lu\n", offset);
return 0;
}
/* empty page? */
if (tag.obj_id <= YAFFS_OBJECTID_DELETED ||
tag.obj_id == YAFFS_OBJECTID_SUMMARY ||
tag.chunk_used == 0) {
UNYAFFS2_DEBUG("unused page skipped @ offset %lu\n", offset);
return 0;
}
if (tag.chunk_id == 0) {
/* a new object */
obj = unyaffs2_objtable_find_alloc(tag.obj_id);
if (obj == NULL) {
UNYAFFS2_ERROR("cannot allocate memory ");
UNYAFFS2_ERROR("for object %u\n", tag.obj_id);
return -1;
}
if (obj->valid) {
UNYAFFS2_DEBUG("skip duplicated object %u\n",
tag.obj_id);
return -1;
}
memcpy(&oh, unyaffs2_databuf, sizeof(struct yaffs_obj_hdr));
if (UNYAFFS2_ISENDIAN)
oh_endian_convert(&oh);
/* extract oh to obj */
unyaffs2_oh2obj(obj, &oh);
obj->obj_id = tag.obj_id;
obj->hdr_off = offset;
obj->valid = 1;
unyaffs2_image_objs++;
}
else if (tag.chunk_id == 1) {
/* the first data chunk of a object */
obj = unyaffs2_objtable_find_alloc(tag.obj_id);
if (obj == NULL) {
UNYAFFS2_ERROR("cannot allocate memory ");
UNYAFFS2_ERROR("for object %u\n", tag.obj_id);
return -1;
}
obj->type = YAFFS_OBJECT_TYPE_FILE;
obj->variant.file.file_head = offset;
}
return 0;
}
static int
unyaffs2_scan_img (void)
{
#ifndef _HAVE_MMAP
ssize_t reads;
#endif
off_t offset = 0, remains = 0;
if (unyaffs2_image_fd < 0) {
UNYAFFS2_DEBUG("bad file descriptor.\n");
return 0;
}
#ifdef _HAVE_MMAP
if (unyaffs2_mmapinfo.addr == NULL) {
UNYAFFS2_DEBUG("NULL mmap address.\n");
return 0;
}
#endif
#ifdef _HAVE_MMAP
remains = unyaffs2_mmapinfo.size;
while (offset < unyaffs2_mmapinfo.size && remains >= unyaffs2_bufsize &&
memcpy(unyaffs2_databuf,
unyaffs2_mmapinfo.addr + offset, unyaffs2_bufsize)) {
if (memcmp(unyaffs2_databuf,
unyaffs2_mmapinfo.addr + offset, unyaffs2_bufsize)) {
#else
remains = lseek(unyaffs2_image_fd, 0, SEEK_END);
offset = lseek(unyaffs2_image_fd, 0, SEEK_SET);
while (remains >= unyaffs2_bufsize &&
(reads = safe_read(unyaffs2_image_fd,
unyaffs2_databuf, unyaffs2_bufsize)) != 0) {
if (reads != unyaffs2_bufsize) {
#endif
/* parse image failed */
UNYAFFS2_ERROR("read image failed @ offset %lu.",
offset);
return -1;
}
if (!unyaffs2_isempty(unyaffs2_databuf, unyaffs2_bufsize))
unyaffs2_scan_chunk(unyaffs2_databuf, offset);
offset += unyaffs2_bufsize;
remains -= unyaffs2_bufsize;
}
return 0;
}
/*----------------------------------------------------------------------------*/
static struct unyaffs2_obj*
unyaffs2_create_fakeroot (const char *path)
{
struct stat s;
struct unyaffs2_obj *obj;
if (stat(path, &s) || !S_ISDIR(s.st_mode))
return NULL;
obj = unyaffs2_obj_alloc();
if (!obj)
return NULL;
/* update root info */
obj->obj_id = YAFFS_OBJECTID_ROOT;
obj->parent_id = YAFFS_OBJECTID_ROOT;
obj->type = YAFFS_OBJECT_TYPE_DIRECTORY;
obj->mode = s.st_mode;
obj->uid = s.st_uid;
obj->gid = s.st_gid;
obj->atime = s.st_atime;
obj->mtime = s.st_mtime;
obj->ctime = s.st_ctime;
obj->valid = 1;
unyaffs2_image_objs++;
return obj;
}
static int
unyaffs2_create_objtree (void)
{
unsigned n, objs = 0;
struct list_head *p;
struct unyaffs2_obj *obj, *parent;
unyaffs2_objtree.root = unyaffs2_objtable_find(YAFFS_OBJECTID_ROOT);
for (n = 0; n < UNYAFFS2_OBJTABLE_SIZE; n++) {
list_for_each(p, &unyaffs2_objtable[n]) {
obj = list_entry(p, unyaffs2_obj_t, hashlist);
parent = unyaffs2_objtable_find(obj->parent_id);
if (parent && obj != parent &&
parent->type == YAFFS_OBJECT_TYPE_DIRECTORY) {
obj->parent_obj = parent;
list_add_tail(&obj->siblings,
&parent->children);
}