fat.h 6.5 KB

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