fat.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
  4. *
  5. * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
  6. * 2003-03-10 - kharris@nexus-tech.net - ported to u-boot
  7. */
  8. #ifndef _FAT_H_
  9. #define _FAT_H_
  10. #include <asm/byteorder.h>
  11. #include <fs.h>
  12. /* Maximum Long File Name length supported here is 128 UTF-16 code units */
  13. #define VFAT_MAXLEN_BYTES 256 /* Maximum LFN buffer in bytes */
  14. #define VFAT_MAXSEQ 9 /* Up to 9 of 13 2-byte UTF-16 entries */
  15. #define PREFETCH_BLOCKS 2
  16. #define MAX_CLUSTSIZE CONFIG_FS_FAT_MAX_CLUSTSIZE
  17. #define DIRENTSPERBLOCK (mydata->sect_size / sizeof(dir_entry))
  18. #define DIRENTSPERCLUST ((mydata->clust_size * mydata->sect_size) / \
  19. sizeof(dir_entry))
  20. #define FATBUFBLOCKS 6
  21. #define FATBUFSIZE (mydata->sect_size * FATBUFBLOCKS)
  22. #define FAT12BUFSIZE ((FATBUFSIZE*2)/3)
  23. #define FAT16BUFSIZE (FATBUFSIZE/2)
  24. #define FAT32BUFSIZE (FATBUFSIZE/4)
  25. /* Maximum number of entry for long file name according to spec */
  26. #define MAX_LFN_SLOT 20
  27. /* Filesystem identifiers */
  28. #define FAT12_SIGN "FAT12 "
  29. #define FAT16_SIGN "FAT16 "
  30. #define FAT32_SIGN "FAT32 "
  31. #define SIGNLEN 8
  32. /* File attributes */
  33. #define ATTR_RO 1
  34. #define ATTR_HIDDEN 2
  35. #define ATTR_SYS 4
  36. #define ATTR_VOLUME 8
  37. #define ATTR_DIR 16
  38. #define ATTR_ARCH 32
  39. #define ATTR_VFAT (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME)
  40. #define DELETED_FLAG ((char)0xe5) /* Marks deleted files when in name[0] */
  41. #define aRING 0x05 /* Used as special character in name[0] */
  42. /*
  43. * Indicates that the entry is the last long entry in a set of long
  44. * dir entries
  45. */
  46. #define LAST_LONG_ENTRY_MASK 0x40
  47. #define ISDIRDELIM(c) ((c) == '/' || (c) == '\\')
  48. #define FSTYPE_NONE (-1)
  49. #if defined(__linux__) && defined(__KERNEL__)
  50. #define FAT2CPU16 le16_to_cpu
  51. #define FAT2CPU32 le32_to_cpu
  52. #else
  53. #if __LITTLE_ENDIAN
  54. #define FAT2CPU16(x) (x)
  55. #define FAT2CPU32(x) (x)
  56. #else
  57. #define FAT2CPU16(x) ((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8))
  58. #define FAT2CPU32(x) ((((x) & 0x000000ff) << 24) | \
  59. (((x) & 0x0000ff00) << 8) | \
  60. (((x) & 0x00ff0000) >> 8) | \
  61. (((x) & 0xff000000) >> 24))
  62. #endif
  63. #endif
  64. #define START(dent) (FAT2CPU16((dent)->start) \
  65. + (mydata->fatsize != 32 ? 0 : \
  66. (FAT2CPU16((dent)->starthi) << 16)))
  67. #define IS_LAST_CLUST(x, fatsize) ((x) >= ((fatsize) != 32 ? \
  68. ((fatsize) != 16 ? 0xff8 : 0xfff8) : \
  69. 0xffffff8))
  70. #define CHECK_CLUST(x, fatsize) ((x) <= 1 || \
  71. (x) >= ((fatsize) != 32 ? \
  72. ((fatsize) != 16 ? 0xff0 : 0xfff0) : \
  73. 0xffffff0))
  74. typedef struct boot_sector {
  75. __u8 ignored[3]; /* Bootstrap code */
  76. char system_id[8]; /* Name of fs */
  77. __u8 sector_size[2]; /* Bytes/sector */
  78. __u8 cluster_size; /* Sectors/cluster */
  79. __u16 reserved; /* Number of reserved sectors */
  80. __u8 fats; /* Number of FATs */
  81. __u8 dir_entries[2]; /* Number of root directory entries */
  82. __u8 sectors[2]; /* Number of sectors */
  83. __u8 media; /* Media code */
  84. __u16 fat_length; /* Sectors/FAT */
  85. __u16 secs_track; /* Sectors/track */
  86. __u16 heads; /* Number of heads */
  87. __u32 hidden; /* Number of hidden sectors */
  88. __u32 total_sect; /* Number of sectors (if sectors == 0) */
  89. /* FAT32 only */
  90. __u32 fat32_length; /* Sectors/FAT */
  91. __u16 flags; /* Bit 8: fat mirroring, low 4: active fat */
  92. __u8 version[2]; /* Filesystem version */
  93. __u32 root_cluster; /* First cluster in root directory */
  94. __u16 info_sector; /* Filesystem info sector */
  95. __u16 backup_boot; /* Backup boot sector */
  96. __u16 reserved2[6]; /* Unused */
  97. } boot_sector;
  98. typedef struct volume_info
  99. {
  100. __u8 drive_number; /* BIOS drive number */
  101. __u8 reserved; /* Unused */
  102. __u8 ext_boot_sign; /* 0x29 if fields below exist (DOS 3.3+) */
  103. __u8 volume_id[4]; /* Volume ID number */
  104. char volume_label[11]; /* Volume label */
  105. char fs_type[8]; /* Typically FAT12, FAT16, or FAT32 */
  106. /* Boot code comes next, all but 2 bytes to fill up sector */
  107. /* Boot sign comes last, 2 bytes */
  108. } volume_info;
  109. /* see dir_entry::lcase: */
  110. #define CASE_LOWER_BASE 8 /* base (name) is lower case */
  111. #define CASE_LOWER_EXT 16 /* extension is lower case */
  112. typedef struct dir_entry {
  113. char name[8],ext[3]; /* Name and extension */
  114. __u8 attr; /* Attribute bits */
  115. __u8 lcase; /* Case for name and ext (CASE_LOWER_x) */
  116. __u8 ctime_ms; /* Creation time, milliseconds */
  117. __u16 ctime; /* Creation time */
  118. __u16 cdate; /* Creation date */
  119. __u16 adate; /* Last access date */
  120. __u16 starthi; /* High 16 bits of cluster in FAT32 */
  121. __u16 time,date,start;/* Time, date and first cluster */
  122. __u32 size; /* File size in bytes */
  123. } dir_entry;
  124. typedef struct dir_slot {
  125. __u8 id; /* Sequence number for slot */
  126. __u8 name0_4[10]; /* First 5 characters in name */
  127. __u8 attr; /* Attribute byte */
  128. __u8 reserved; /* Unused */
  129. __u8 alias_checksum;/* Checksum for 8.3 alias */
  130. __u8 name5_10[12]; /* 6 more characters in name */
  131. __u16 start; /* Unused */
  132. __u8 name11_12[4]; /* Last 2 characters in name */
  133. } dir_slot;
  134. /*
  135. * Private filesystem parameters
  136. *
  137. * Note: FAT buffer has to be 32 bit aligned
  138. * (see FAT32 accesses)
  139. */
  140. typedef struct {
  141. __u8 *fatbuf; /* Current FAT buffer */
  142. int fatsize; /* Size of FAT in bits */
  143. __u32 fatlength; /* Length of FAT in sectors */
  144. __u16 fat_sect; /* Starting sector of the FAT */
  145. __u8 fat_dirty; /* Set if fatbuf has been modified */
  146. __u32 rootdir_sect; /* Start sector of root directory */
  147. __u16 sect_size; /* Size of sectors in bytes */
  148. __u16 clust_size; /* Size of clusters in sectors */
  149. int data_begin; /* The sector of the first cluster, can be negative */
  150. int fatbufnum; /* Used by get_fatent, init to -1 */
  151. int rootdir_size; /* Size of root dir for non-FAT32 */
  152. __u32 root_cluster; /* First cluster of root dir for FAT32 */
  153. u32 total_sect; /* Number of sectors */
  154. int fats; /* Number of FATs */
  155. } fsdata;
  156. static inline u32 clust_to_sect(fsdata *fsdata, u32 clust)
  157. {
  158. return fsdata->data_begin + clust * fsdata->clust_size;
  159. }
  160. static inline u32 sect_to_clust(fsdata *fsdata, int sect)
  161. {
  162. return (sect - fsdata->data_begin) / fsdata->clust_size;
  163. }
  164. int file_fat_detectfs(void);
  165. int fat_exists(const char *filename);
  166. int fat_size(const char *filename, loff_t *size);
  167. int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
  168. loff_t maxsize, loff_t *actread);
  169. int file_fat_read(const char *filename, void *buffer, int maxsize);
  170. int fat_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info);
  171. int fat_register_device(struct blk_desc *dev_desc, int part_no);
  172. int file_fat_write(const char *filename, void *buf, loff_t offset, loff_t len,
  173. loff_t *actwrite);
  174. int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
  175. loff_t *actread);
  176. int fat_opendir(const char *filename, struct fs_dir_stream **dirsp);
  177. int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
  178. void fat_closedir(struct fs_dir_stream *dirs);
  179. int fat_unlink(const char *filename);
  180. int fat_mkdir(const char *dirname);
  181. void fat_close(void);
  182. #endif /* _FAT_H_ */