sdfat_fs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef _SDFAT_FS_H
  18. #define _SDFAT_FS_H
  19. #include <linux/types.h>
  20. #include <linux/magic.h>
  21. #include <asm/byteorder.h>
  22. /*----------------------------------------------------------------------*/
  23. /* Constant & Macro Definitions */
  24. /*----------------------------------------------------------------------*/
  25. #ifndef MSDOS_SUPER_MAGIC
  26. #define MSDOS_SUPER_MAGIC 0x4d44 /* MD */
  27. #endif
  28. #ifndef EXFAT_SUPER_MAGIC
  29. #define EXFAT_SUPER_MAGIC (0x2011BAB0UL)
  30. #endif /* EXFAT_SUPER_MAGIC */
  31. #define SDFAT_SUPER_MAGIC (0x5EC5DFA4UL)
  32. #define SDFAT_ROOT_INO 1
  33. /* FAT types */
  34. #define FAT12 0x01 // FAT12
  35. #define FAT16 0x0E // Win95 FAT16 (LBA)
  36. #define FAT32 0x0C // Win95 FAT32 (LBA)
  37. #define EXFAT 0x07 // exFAT
  38. /* directory file name */
  39. #define DOS_CUR_DIR_NAME ". "
  40. #define DOS_PAR_DIR_NAME ".. "
  41. #ifdef __LITTLE_ENDIAN
  42. #define UNI_CUR_DIR_NAME ".\0"
  43. #define UNI_PAR_DIR_NAME ".\0.\0"
  44. #else
  45. #define UNI_CUR_DIR_NAME "\0."
  46. #define UNI_PAR_DIR_NAME "\0.\0."
  47. #endif
  48. /* file name lengths */
  49. /* NOTE :
  50. * The maximum length of input or output is limited to 256 including NULL,
  51. * But we allocate 4 extra bytes for utf8 translation reside in last position,
  52. * because utf8 can uses memory upto 6 bytes per one character.
  53. * Therefore, MAX_CHARSET_SIZE supports upto 6 bytes for utf8
  54. */
  55. #define MAX_UNINAME_BUF_SIZE (((MAX_NAME_LENGTH+1)*2)+4)
  56. #define MAX_DOSNAME_BUF_SIZE ((DOS_NAME_LENGTH+2)+6)
  57. #define MAX_VFSNAME_BUF_SIZE ((MAX_NAME_LENGTH+1)*MAX_CHARSET_SIZE)
  58. #define MAX_CHARSET_SIZE 6 // max size of multi-byte character
  59. #define MAX_NAME_LENGTH 255 // max len of file name excluding NULL
  60. #define DOS_NAME_LENGTH 11 // DOS file name length excluding NULL
  61. #define SECTOR_SIZE_BITS 9 /* VFS sector size is 512 bytes */
  62. #define DENTRY_SIZE 32 /* directory entry size */
  63. #define DENTRY_SIZE_BITS 5
  64. #define MAX_FAT_DENTRIES 65536 /* FAT allows 65536 directory entries */
  65. #define MAX_EXFAT_DENTRIES 8388608 /* exFAT allows 8388608(256MB) directory entries */
  66. /* PBR entries */
  67. #define PBR_SIGNATURE 0xAA55
  68. #define EXT_SIGNATURE 0xAA550000
  69. #define VOL_LABEL "NO NAME " /* size should be 11 */
  70. #define OEM_NAME "MSWIN4.1" /* size should be 8 */
  71. #define STR_FAT12 "FAT12 " /* size should be 8 */
  72. #define STR_FAT16 "FAT16 " /* size should be 8 */
  73. #define STR_FAT32 "FAT32 " /* size should be 8 */
  74. #define STR_EXFAT "EXFAT " /* size should be 8 */
  75. #define VOL_CLEAN 0x0000
  76. #define VOL_DIRTY 0x0002
  77. #define FAT_VOL_DIRTY 0x01
  78. /* max number of clusters */
  79. #define FAT12_THRESHOLD 4087 // 2^12 - 1 + 2 (clu 0 & 1)
  80. #define FAT16_THRESHOLD 65527 // 2^16 - 1 + 2
  81. #define FAT32_THRESHOLD 268435457 // 2^28 - 1 + 2
  82. #define EXFAT_THRESHOLD 268435457 // 2^28 - 1 + 2
  83. /* dentry types */
  84. #define MSDOS_DELETED 0xE5 /* deleted mark */
  85. #define MSDOS_UNUSED 0x00 /* end of directory */
  86. #define EXFAT_UNUSED 0x00 /* end of directory */
  87. #define IS_EXFAT_DELETED(x) ((x) < 0x80) /* deleted file (0x01~0x7F) */
  88. #define EXFAT_INVAL 0x80 /* invalid value */
  89. #define EXFAT_BITMAP 0x81 /* allocation bitmap */
  90. #define EXFAT_UPCASE 0x82 /* upcase table */
  91. #define EXFAT_VOLUME 0x83 /* volume label */
  92. #define EXFAT_FILE 0x85 /* file or dir */
  93. #define EXFAT_STREAM 0xC0 /* stream entry */
  94. #define EXFAT_NAME 0xC1 /* file name entry */
  95. #define EXFAT_ACL 0xC2 /* stream entry */
  96. /* specific flag */
  97. #define MSDOS_LAST_LFN 0x40
  98. /* file attributes */
  99. #define ATTR_NORMAL 0x0000
  100. #define ATTR_READONLY 0x0001
  101. #define ATTR_HIDDEN 0x0002
  102. #define ATTR_SYSTEM 0x0004
  103. #define ATTR_VOLUME 0x0008
  104. #define ATTR_SUBDIR 0x0010
  105. #define ATTR_ARCHIVE 0x0020
  106. #define ATTR_SYMLINK 0x0040
  107. #define ATTR_EXTEND (ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | \
  108. ATTR_VOLUME) /* 0x000F */
  109. #define ATTR_EXTEND_MASK (ATTR_EXTEND | ATTR_SUBDIR | ATTR_ARCHIVE)
  110. #define ATTR_RWMASK (ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME | \
  111. ATTR_SUBDIR | ATTR_ARCHIVE | ATTR_SYMLINK)/* 0x007E */
  112. /* file creation modes */
  113. #define FM_REGULAR 0x00
  114. #define FM_SYMLINK 0x40
  115. /* time modes */
  116. #define TM_CREATE 0
  117. #define TM_MODIFY 1
  118. #define TM_ACCESS 2
  119. /* checksum types */
  120. #define CS_DIR_ENTRY 0
  121. #define CS_PBR_SECTOR 1
  122. #define CS_DEFAULT 2
  123. /* time min/max */
  124. /* Jan 1 GMT 00:00:00 1980 */
  125. #define SDFAT_MIN_TIMESTAMP_SECS 315532800LL
  126. /* Dec 31 GMT 23:59:59 2107 */
  127. #define SDFAT_MAX_TIMESTAMP_SECS 4354819199LL
  128. /*
  129. * ioctl command
  130. */
  131. #define SDFAT_IOCTL_GET_VOLUME_ID _IOR('r', 0x12, __u32)
  132. #define SDFAT_IOCTL_DFR_INFO _IOC(_IOC_NONE, 'E', 0x13, sizeof(u32))
  133. #define SDFAT_IOCTL_DFR_TRAV _IOC(_IOC_NONE, 'E', 0x14, sizeof(u32))
  134. #define SDFAT_IOCTL_DFR_REQ _IOC(_IOC_NONE, 'E', 0x15, sizeof(u32))
  135. #define SDFAT_IOCTL_DFR_SPO_FLAG _IOC(_IOC_NONE, 'E', 0x16, sizeof(u32))
  136. #define SDFAT_IOCTL_PANIC _IOC(_IOC_NONE, 'E', 0x17, sizeof(u32))
  137. /*
  138. * ioctl command for debugging
  139. */
  140. /*
  141. * IOCTL code 'f' used by
  142. * - file systems typically #0~0x1F
  143. * - embedded terminal devices #128~
  144. * - exts for debugging purpose #99
  145. * number 100 and 101 is available now but has possible conflicts
  146. *
  147. * NOTE : This is available only If CONFIG_SDFAT_DVBG_IOCTL is enabled.
  148. *
  149. */
  150. #define SDFAT_IOC_GET_DEBUGFLAGS _IOR('f', 100, long)
  151. #define SDFAT_IOC_SET_DEBUGFLAGS _IOW('f', 101, long)
  152. #define SDFAT_DEBUGFLAGS_INVALID_UMOUNT 0x01
  153. #define SDFAT_DEBUGFLAGS_ERROR_RW 0x02
  154. /*----------------------------------------------------------------------*/
  155. /* On-Disk Type Definitions */
  156. /*----------------------------------------------------------------------*/
  157. /* FAT12/16/32 BIOS parameter block (64 bytes) */
  158. typedef struct {
  159. __u8 jmp_boot[3];
  160. __u8 oem_name[8];
  161. __u8 sect_size[2]; /* unaligned */
  162. __u8 sect_per_clus;
  163. __le16 num_reserved; /* . */
  164. __u8 num_fats;
  165. __u8 num_root_entries[2]; /* unaligned */
  166. __u8 num_sectors[2]; /* unaligned */
  167. __u8 media_type;
  168. __le16 num_fat_sectors;
  169. __le16 sectors_in_track;
  170. __le16 num_heads;
  171. __le32 num_hid_sectors; /* . */
  172. __le32 num_huge_sectors;
  173. union {
  174. struct {
  175. __u8 phy_drv_no;
  176. __u8 state; /* used by WinNT for mount state */
  177. __u8 ext_signature;
  178. __u8 vol_serial[4];
  179. __u8 vol_label[11];
  180. __u8 vol_type[8];
  181. __le16 nouse;
  182. } f16;
  183. struct {
  184. __le32 num_fat32_sectors;
  185. __le16 ext_flags;
  186. __u8 fs_version[2];
  187. __le32 root_cluster; /* . */
  188. __le16 fsinfo_sector;
  189. __le16 backup_sector;
  190. __le16 reserved[6]; /* . */
  191. } f32;
  192. };
  193. } bpb_t;
  194. /* FAT32 EXTEND BIOS parameter block (32 bytes) */
  195. typedef struct {
  196. __u8 phy_drv_no;
  197. __u8 state; /* used by WindowsNT for mount state */
  198. __u8 ext_signature;
  199. __u8 vol_serial[4];
  200. __u8 vol_label[11];
  201. __u8 vol_type[8];
  202. __le16 dummy[3];
  203. } bsx32_t;
  204. /* EXFAT BIOS parameter block (64 bytes) */
  205. typedef struct {
  206. __u8 jmp_boot[3];
  207. __u8 oem_name[8];
  208. __u8 res_zero[53];
  209. } bpb64_t;
  210. /* EXFAT EXTEND BIOS parameter block (56 bytes) */
  211. typedef struct {
  212. __le64 vol_offset;
  213. __le64 vol_length;
  214. __le32 fat_offset;
  215. __le32 fat_length;
  216. __le32 clu_offset;
  217. __le32 clu_count;
  218. __le32 root_cluster;
  219. __le32 vol_serial;
  220. __u8 fs_version[2];
  221. __le16 vol_flags;
  222. __u8 sect_size_bits;
  223. __u8 sect_per_clus_bits;
  224. __u8 num_fats;
  225. __u8 phy_drv_no;
  226. __u8 perc_in_use;
  227. __u8 reserved2[7];
  228. } bsx64_t;
  229. /* FAT32 PBR (64 bytes) */
  230. typedef struct {
  231. bpb_t bpb;
  232. } pbr16_t;
  233. /* FAT32 PBR[BPB+BSX] (96 bytes) */
  234. typedef struct {
  235. bpb_t bpb;
  236. bsx32_t bsx;
  237. } pbr32_t;
  238. /* EXFAT PBR[BPB+BSX] (120 bytes) */
  239. typedef struct {
  240. bpb64_t bpb;
  241. bsx64_t bsx;
  242. } pbr64_t;
  243. /* Common PBR[Partition Boot Record] (512 bytes) */
  244. typedef struct {
  245. union {
  246. __u8 raw[64];
  247. bpb_t fat;
  248. bpb64_t f64;
  249. } bpb;
  250. union {
  251. __u8 raw[56];
  252. bsx32_t f32;
  253. bsx64_t f64;
  254. } bsx;
  255. __u8 boot_code[390];
  256. __le16 signature;
  257. } pbr_t;
  258. /* FAT32 filesystem information sector (512 bytes) */
  259. typedef struct {
  260. __le32 signature1; // aligned
  261. __u8 reserved1[480];
  262. __le32 signature2; // aligned
  263. __le32 free_cluster; // aligned
  264. __le32 next_cluster; // aligned
  265. __u8 reserved2[14];
  266. __le16 signature3[2];
  267. } fat32_fsi_t;
  268. /* FAT directory entry (32 bytes) */
  269. typedef struct {
  270. __u8 dummy[32];
  271. } DENTRY_T;
  272. typedef struct {
  273. __u8 name[DOS_NAME_LENGTH]; /* 11 chars */
  274. __u8 attr;
  275. __u8 lcase;
  276. __u8 create_time_ms;
  277. __le16 create_time; // aligned
  278. __le16 create_date; // aligned
  279. __le16 access_date; // aligned
  280. __le16 start_clu_hi; // aligned
  281. __le16 modify_time; // aligned
  282. __le16 modify_date; // aligned
  283. __le16 start_clu_lo; // aligned
  284. __le32 size; // aligned
  285. } DOS_DENTRY_T;
  286. /* FAT extended directory entry (32 bytes) */
  287. typedef struct {
  288. __u8 order;
  289. __u8 unicode_0_4[10];
  290. __u8 attr;
  291. __u8 sysid;
  292. __u8 checksum;
  293. __le16 unicode_5_10[6]; // aligned
  294. __le16 start_clu; // aligned
  295. __le16 unicode_11_12[2]; // aligned
  296. } EXT_DENTRY_T;
  297. /* EXFAT file directory entry (32 bytes) */
  298. typedef struct {
  299. __u8 type;
  300. __u8 num_ext;
  301. __le16 checksum; // aligned
  302. __le16 attr; // aligned
  303. __le16 reserved1;
  304. __le16 create_time; // aligned
  305. __le16 create_date; // aligned
  306. __le16 modify_time; // aligned
  307. __le16 modify_date; // aligned
  308. __le16 access_time; // aligned
  309. __le16 access_date; // aligned
  310. __u8 create_time_ms;
  311. __u8 modify_time_ms;
  312. __u8 create_tz;
  313. __u8 modify_tz;
  314. __u8 access_tz;
  315. __u8 reserved2[7];
  316. } FILE_DENTRY_T;
  317. /* EXFAT stream extension directory entry (32 bytes) */
  318. typedef struct {
  319. __u8 type;
  320. __u8 flags;
  321. __u8 reserved1;
  322. __u8 name_len;
  323. __le16 name_hash; // aligned
  324. __le16 reserved2;
  325. __le64 valid_size; // aligned
  326. __le32 reserved3; // aligned
  327. __le32 start_clu; // aligned
  328. __le64 size; // aligned
  329. } STRM_DENTRY_T;
  330. /* EXFAT file name directory entry (32 bytes) */
  331. typedef struct {
  332. __u8 type;
  333. __u8 flags;
  334. __le16 unicode_0_14[15]; // aligned
  335. } NAME_DENTRY_T;
  336. /* EXFAT allocation bitmap directory entry (32 bytes) */
  337. typedef struct {
  338. __u8 type;
  339. __u8 flags;
  340. __u8 reserved[18];
  341. __le32 start_clu; // aligned
  342. __le64 size; // aligned
  343. } BMAP_DENTRY_T;
  344. /* EXFAT up-case table directory entry (32 bytes) */
  345. typedef struct {
  346. __u8 type;
  347. __u8 reserved1[3];
  348. __le32 checksum; // aligned
  349. __u8 reserved2[12];
  350. __le32 start_clu; // aligned
  351. __le64 size; // aligned
  352. } CASE_DENTRY_T;
  353. /* EXFAT volume label directory entry (32 bytes) */
  354. typedef struct {
  355. __u8 type;
  356. __u8 label_len;
  357. __le16 unicode_0_10[11]; // aligned
  358. __u8 reserved[8];
  359. } VOLM_DENTRY_T;
  360. #endif /* _SDFAT_FS_H */