fat.h 6.8 KB

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