summaryrefslogtreecommitdiffstats
path: root/drivers/staging/exfat/exfat.h
blob: 0aa14dea4e095bf76e3ee1bafc3cd52361e51215 (plain)
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
/* SPDX-License-Identifier: GPL-2.0 */
/*
 *  Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
 */

#ifndef _EXFAT_H
#define _EXFAT_H

#include <linux/types.h>
#include <linux/buffer_head.h>

#ifdef CONFIG_EXFAT_KERNEL_DEBUG
  /* For Debugging Purpose */
	/* IOCTL code 'f' used by
	 *   - file systems typically #0~0x1F
	 *   - embedded terminal devices #128~
	 *   - exts for debugging purpose #99
	 * number 100 and 101 is available now but has possible conflicts
	 */
#define EXFAT_IOC_GET_DEBUGFLAGS	_IOR('f', 100, long)
#define EXFAT_IOC_SET_DEBUGFLAGS	_IOW('f', 101, long)

#define EXFAT_DEBUGFLAGS_INVALID_UMOUNT	0x01
#define EXFAT_DEBUGFLAGS_ERROR_RW	0x02
#endif /* CONFIG_EXFAT_KERNEL_DEBUG */

#ifdef CONFIG_EXFAT_DEBUG_MSG
#define DEBUG	1
#else
#undef DEBUG
#endif

#define DENTRY_SIZE		32	/* dir entry size */
#define DENTRY_SIZE_BITS	5

/* PBR entries */
#define PBR_SIGNATURE	0xAA55
#define EXT_SIGNATURE	0xAA550000
#define VOL_LABEL	"NO NAME    "	/* size should be 11 */
#define OEM_NAME	"MSWIN4.1"	/* size should be 8 */
#define STR_FAT12	"FAT12   "	/* size should be 8 */
#define STR_FAT16	"FAT16   "	/* size should be 8 */
#define STR_FAT32	"FAT32   "	/* size should be 8 */
#define STR_EXFAT	"EXFAT   "	/* size should be 8 */
#define VOL_CLEAN	0x0000
#define VOL_DIRTY	0x0002

/* max number of clusters */
#define FAT12_THRESHOLD		4087		/* 2^12 - 1 + 2 (clu 0 & 1) */
#define FAT16_THRESHOLD		65527		/* 2^16 - 1 + 2 */
#define FAT32_THRESHOLD		268435457	/* 2^28 - 1 + 2 */
#define EXFAT_THRESHOLD		268435457	/* 2^28 - 1 + 2 */

/* file types */
#define TYPE_UNUSED		0x0000
#define TYPE_DELETED		0x0001
#define TYPE_INVALID		0x0002
#define TYPE_CRITICAL_PRI	0x0100
#define TYPE_BITMAP		0x0101
#define TYPE_UPCASE		0x0102
#define TYPE_VOLUME		0x0103
#define TYPE_DIR		0x0104
#define TYPE_FILE		0x011F
#define TYPE_SYMLINK		0x015F
#define TYPE_CRITICAL_SEC	0x0200
#define TYPE_STREAM		0x0201
#define TYPE_EXTEND		0x0202
#define TYPE_ACL		0x0203
#define TYPE_BENIGN_PRI		0x0400
#define TYPE_GUID		0x0401
#define TYPE_PADDING		0x0402
#define TYPE_ACLTAB		0x0403
#define TYPE_BENIGN_SEC		0x0800
#define TYPE_ALL		0x0FFF

/* time modes */
#define TM_CREATE		0
#define TM_MODIFY		1
#define TM_ACCESS		2

/* checksum types */
#define CS_DIR_ENTRY		0
#define CS_PBR_SECTOR		1
#define CS_DEFAULT		2

#define CLUSTER_16(x)		((u16)(x))
#define CLUSTER_32(x)		((u32)(x))

#define START_SECTOR(x)							\
	((((sector_t)((x) - 2)) << p_fs->sectors_per_clu_bits) +	\
	 p_fs->data_start_sector)

#define IS_LAST_SECTOR_IN_CLUSTER(sec)				\
	((((sec) - p_fs->data_start_sector + 1) &		\
	  ((1 <<  p_fs->sectors_per_clu_bits) - 1)) == 0)

#define GET_CLUSTER_FROM_SECTOR(sec)				\
	((u32)((((sec) - p_fs->data_start_sector) >>		\
		p_fs->sectors_per_clu_bits) + 2))

#define GET16(p_src)						\
	(((u16)(p_src)[0]) | (((u16)(p_src)[1]) << 8))
#define GET32(p_src)						\
	(((u32)(p_src)[0]) | (((u32)(p_src)[1]) << 8) |		\
	(((u32)(p_src)[2]) << 16) | (((u32)(p_src)[3]) << 24))
#define GET64(p_src) \
	(((u64)(p_src)[0]) | (((u64)(p_src)[1]) << 8) |		\
	(((u64)(p_src)[2]) << 16) | (((u64)(p_src)[3]) << 24) |	\
	(((u64)(p_src)[4]) << 32) | (((u64)(p_src)[5]) << 40) |	\
	(((u64)(p_src)[6]) << 48) | (((u64)(p_src)[7]) << 56))

#define SET16(p_dst, src)					\
	do {							\
		(p_dst)[0] = (u8)(src);				\
		(p_dst)[1] = (u8)(((u16)(src)) >> 8);		\
	} while (0)
#define SET32(p_dst, src)					\
	do {							\
		(p_dst)[0] = (u8)(src);				\
		(p_dst)[1] = (u8)(((u32)(src)) >> 8);		\
		(p_dst)[2] = (u8)(((u32)(src)) >> 16);		\
		(p_dst)[3] = (u8)(((u32)(src)) >> 24);		\
	} while (0)
#define SET64(p_dst, src)					\
	do {							\
		(p_dst)[0] = (u8)(src);				\
		(p_dst)[1] = (u8)(((u64)(src)) >> 8);		\
		(p_dst)[2] = (u8)(((u64)(src)) >> 16);		\
		(p_dst)[3] = (u8)(((u64)(src)) >> 24);		\
		(p_dst)[4] = (u8)(((u64)(src)) >> 32);		\
		(p_dst)[5] = (u8)(((u64)(src)) >> 40);		\
		(p_dst)[6] = (u8)(((u64)(src)) >> 48);		\
		(p_dst)[7] = (u8)(((u64)(src)) >> 56);		\
	} while (0)

#ifdef __LITTLE_ENDIAN
#define GET16_A(p_src)		(*((u16 *)(p_src)))
#define GET32_A(p_src)		(*((u32 *)(p_src)))
#define GET64_A(p_src)		(*((u64 *)(p_src)))
#define SET16_A(p_dst, src)	(*((u16 *)(p_dst)) = (u16)(src))
#define SET32_A(p_dst, src)	(*((u32 *)(p_dst)) = (u32)(src))
#define SET64_A(p_dst, src)	(*((u64 *)(p_dst)) = (u64)(src))
#else /* BIG_ENDIAN */
#define GET16_A(p_src)		GET16(p_src)
#define GET32_A(p_src)		GET32(p_src)
#define GET64_A(p_src)		GET64(p_src)
#define SET16_A(p_dst, src)	SET16(p_dst, src)
#define SET32_A(p_dst, src)	SET32(p_dst, src)
#define SET64_A(p_dst, src)	SET64(p_dst, src)
#endif

/* cache size (in number of sectors) */
/* (should be an exponential value of 2) */
#define FAT_CACHE_SIZE		128
#define FAT_CACHE_HASH_SIZE	64
#define BUF_CACHE_SIZE		256
#define BUF_CACHE_HASH_SIZE	64

/* Upcase table macro */
#define HIGH_INDEX_BIT	(8)
#define HIGH_INDEX_MASK	(0xFF00)
#define LOW_INDEX_BIT	(16-HIGH_INDEX_BIT)
#define UTBL_ROW_COUNT	BIT(LOW_INDEX_BIT)
#define UTBL_COL_COUNT	BIT(HIGH_INDEX_BIT)

static inline u16 get_col_index(u16 i)
{
	return i >> LOW_INDEX_BIT;
}

static inline u16 get_row_index(u16 i)
{
	return i & ~HIGH_INDEX_MASK;
}

#define EXFAT_SUPER_MAGIC       (0x2011BAB0L)
#define EXFAT_ROOT_INO          1

/* FAT types */
#define FAT12			0x01	/* FAT12 */
#define FAT16			0x0E	/* Win95 FAT16 (LBA) */
#define FAT32			0x0C	/* Win95 FAT32 (LBA) */
#define EXFAT			0x07	/* exFAT */

/* file name lengths */
#define MAX_CHARSET_SIZE	3	/* max size of multi-byte character */
#define MAX_PATH_DEPTH		15	/* max depth of path name */
#define MAX_NAME_LENGTH		256	/* max len of filename including NULL */
#define MAX_PATH_LENGTH		260	/* max len of pathname including NULL */
#define DOS_NAME_LENGTH		11	/* DOS filename length excluding NULL */
#define DOS_PATH_LENGTH		80	/* DOS pathname length excluding NULL */

/* file attributes */
#define ATTR_NORMAL		0x0000
#define ATTR_READONLY		0x0001
#define ATTR_HIDDEN		0x0002
#define ATTR_SYSTEM		0x0004
#define ATTR_VOLUME		0x0008
#define ATTR_SUBDIR		0x0010
#define ATTR_ARCHIVE		0x0020
#define ATTR_SYMLINK		0x0040
#define ATTR_EXTEND		0x000F
#define ATTR_RWMASK		0x007E

/* file creation modes */
#define FM_REGULAR              0x00
#define FM_SYMLINK              0x40

/* return values */
#define FFS_SUCCESS             0
#define FFS_MEDIAERR            1
#define FFS_FORMATERR           2
#define FFS_MOUNTED             3
#define FFS_NOTMOUNTED          4
#define FFS_ALIGNMENTERR        5
#define FFS_SEMAPHOREERR        6
#define FFS_INVALIDPATH         7
#define FFS_INVALIDFID          8
#define FFS_NOTFOUND            9
#define FFS_FILEEXIST           10
#define FFS_PERMISSIONERR       11
#define FFS_NOTOPENED           12
#define FFS_MAXOPENED           13
#define FFS_FULL                14
#define FFS_EOF                 15
#define FFS_DIRBUSY             16
#define FFS_MEMORYERR           17
#define FFS_NAMETOOLONG		18
#define FFS_ERROR               19

#define NUM_UPCASE              2918

#define DOS_CUR_DIR_NAME        ".          "
#define DOS_PAR_DIR_NAME        "..         "

#ifdef __LITTLE_ENDIAN
#define UNI_CUR_DIR_NAME        ".\0"
#define UNI_PAR_DIR_NAME        ".\0.\0"
#else
#define UNI_CUR_DIR_NAME        "\0."
#define UNI_PAR_DIR_NAME        "\0.\0."
#endif

struct date_time_t {
	u16      Year;
	u16      Month;
	u16      Day;
	u16      Hour;
	u16      Minute;
	u16      Second;
	u16      MilliSecond;
};

struct part_info_t {
	u32      Offset;    /* start sector number of the partition */
	u32      Size;      /* in sectors */
};

struct dev_info_t {
	u32      SecSize;    /* sector size in bytes */
	u32      DevSize;    /* block device size in sectors */
};

struct vol_info_t {
	u32      FatType;
	u32      ClusterSize;
	u32      NumClusters;
	u32      FreeClusters;
	u32      UsedClusters;
};

/* directory structure */
struct chain_t {
	u32      dir;
	s32       size;
	u8       flags;
};

struct file_id_t {
	struct chain_t     dir;
	s32       entry;
	u32      type;
	u32      attr;
	u32      start_clu;
	u64      size;
	u8       flags;
	s64       rwoffset;
	s32       hint_last_off;
	u32      hint_last_clu;
};

struct dir_entry_t {
	char Name[MAX_NAME_LENGTH * MAX_CHARSET_SIZE];

	/* used only for FAT12/16/32, not used for exFAT */
	char ShortName[DOS_NAME_LENGTH + 2];

	u32 Attr;
	u64 Size;
	u32 NumSubdirs;
	struct date_time_t CreateTimestamp;
	struct date_time_t ModifyTimestamp;
	struct date_time_t AccessTimestamp;
};

struct timestamp_t {
	u16      sec;        /* 0 ~ 59               */
	u16      min;        /* 0 ~ 59               */
	u16      hour;       /* 0 ~ 23               */
	u16      day;        /* 1 ~ 31               */
	u16      mon;        /* 1 ~ 12               */
	u16      year;       /* 0 ~ 127 (since 1980) */
};

/* MS_DOS FAT partition boot record (512 bytes) */
struct pbr_sector_t {
	u8       jmp_boot[3];
	u8       oem_name[8];
	u8       bpb[109];
	u8       boot_code[390];
	u8       si