VirtualFAT.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. #ifndef _VIRTUALFAT_H_
  27. #define _VIRTUALFAT_H_
  28. /* Includes: */
  29. #include <avr/io.h>
  30. #include <avr/pgmspace.h>
  31. #include <LUFA/Drivers/USB/USB.h>
  32. #include "../BootloaderAPI.h"
  33. /* Macros: */
  34. /** Size of the virtual FLASH.BIN file in bytes. */
  35. #define FLASH_FILE_SIZE_BYTES (FLASHEND - (FLASHEND - BOOT_START_ADDR) - AUX_BOOT_SECTION_SIZE)
  36. /** Size of the virtual EEPROM.BIN file in bytes. */
  37. #define EEPROM_FILE_SIZE_BYTES E2END
  38. /** Number of sectors that comprise a single logical disk cluster. */
  39. #define SECTOR_PER_CLUSTER 4
  40. /** Size of a single logical sector on the disk. */
  41. #define SECTOR_SIZE_BYTES 512
  42. /** Size of a logical cluster on the disk, in bytes */
  43. #define CLUSTER_SIZE_BYTES (SECTOR_PER_CLUSTER * SECTOR_SIZE_BYTES)
  44. /** Number of sectors required to store a given size in bytes.
  45. *
  46. * \param[in] size Size of the data that needs to be stored
  47. *
  48. * \return Number of sectors required to store the given data on the disk.
  49. */
  50. #define FILE_SECTORS(size) ((size / SECTOR_SIZE_BYTES) + ((size % SECTOR_SIZE_BYTES) ? 1 : 0))
  51. /** Number of clusters required to store a given size in bytes.
  52. *
  53. * \param[in] size Size of the data that needs to be stored
  54. *
  55. * \return Number of clusters required to store the given data on the disk.
  56. */
  57. #define FILE_CLUSTERS(size) ((size / CLUSTER_SIZE_BYTES) + ((size % CLUSTER_SIZE_BYTES) ? 1 : 0))
  58. /** Total number of logical sectors/blocks on the disk. */
  59. #define LUN_MEDIA_BLOCKS (FILE_SECTORS(FLASH_FILE_SIZE_BYTES) + FILE_SECTORS(EEPROM_FILE_SIZE_BYTES) + 32)
  60. /** Converts a given time in HH:MM:SS format to a FAT filesystem time.
  61. *
  62. * \note The minimum seconds resolution of FAT is 2, thus odd seconds
  63. * will be truncated to the previous integer multiple of 2 seconds.
  64. *
  65. * \param[in] hh Hours (0-23)
  66. * \param[in] mm Minutes (0-59)
  67. * \param[in] ss Seconds (0-59)
  68. *
  69. * \return Given time encoded as a FAT filesystem timestamp
  70. */
  71. #define FAT_TIME(hh, mm, ss) ((hh << 11) | (mm << 5) | (ss >> 1))
  72. /** Converts a given date in DD/MM/YYYY format to a FAT filesystem date.
  73. *
  74. * \param[in] dd Days in the month (1-31)
  75. * \param[in] mm Months in the year (1-12)
  76. * \param[in] yyyy Year (1980 - 2107)
  77. *
  78. * \return Given date encoded as a FAT filesystem datestamp
  79. */
  80. #define FAT_DATE(dd, mm, yyyy) (((yyyy - 1980) << 9) | (mm << 5) | (dd << 0))
  81. /** Bit-rotates a given 8-bit value once to the right.
  82. *
  83. * \param[in] x Value to rotate right once
  84. *
  85. * \return Bit-rotated input value, rotated once to the right.
  86. */
  87. #define ROT8(x) ((((x) & 0xFE) >> 1) | (((x) & 1) ? 0x80 : 0x00))
  88. /** Computes the LFN entry checksum of a MSDOS 8.3 format file entry,
  89. * to associate a LFN entry with its short file entry.
  90. *
  91. * \param[in] n0 MSDOS Filename character 1
  92. * \param[in] n1 MSDOS Filename character 2
  93. * \param[in] n2 MSDOS Filename character 3
  94. * \param[in] n3 MSDOS Filename character 4
  95. * \param[in] n4 MSDOS Filename character 5
  96. * \param[in] n5 MSDOS Filename character 6
  97. * \param[in] n6 MSDOS Filename character 7
  98. * \param[in] n7 MSDOS Filename character 8
  99. * \param[in] e0 MSDOS Extension character 1
  100. * \param[in] e1 MSDOS Extension character 2
  101. * \param[in] e2 MSDOS Extension character 3
  102. *
  103. * \return LFN checksum of the given MSDOS 8.3 filename.
  104. */
  105. #define FAT_CHECKSUM(n0, n1, n2, n3, n4, n5, n6, n7, e0, e1, e2) \
  106. (uint8_t)(ROT8(ROT8(ROT8(ROT8(ROT8(ROT8(ROT8(ROT8(ROT8(ROT8(n0)+n1)+n2)+n3)+n4)+n5)+n6)+n7)+e0)+e1)+e2)
  107. /** \name FAT Filesystem Flags */
  108. //@{
  109. /** FAT attribute flag to indicate a read-only file. */
  110. #define FAT_FLAG_READONLY (1 << 0)
  111. /** FAT attribute flag to indicate a hidden file. */
  112. #define FAT_FLAG_HIDDEN (1 << 1)
  113. /** FAT attribute flag to indicate a system file. */
  114. #define FAT_FLAG_SYSTEM (1 << 2)
  115. /** FAT attribute flag to indicate a Volume name entry. */
  116. #define FAT_FLAG_VOLUME_NAME (1 << 3)
  117. /** FAT attribute flag to indicate a directory entry. */
  118. #define FAT_FLAG_DIRECTORY (1 << 4)
  119. /** FAT attribute flag to indicate a file ready for archiving. */
  120. #define FAT_FLAG_ARCHIVE (1 << 5)
  121. /** FAT pseudo-attribute flag to indicate a Long File Name entry. */
  122. #define FAT_FLAG_LONG_FILE_NAME 0x0F
  123. /** Ordinal flag marker for FAT Long File Name entries to mark the last entry. */
  124. #define FAT_ORDINAL_LAST_ENTRY (1 << 6)
  125. //@}
  126. /* Enums: */
  127. /** Enum for the Root FAT file entry indexes on the disk. This can be used
  128. * to retrieve the current contents of a known directory entry.
  129. */
  130. enum
  131. {
  132. /** Volume ID directory entry, giving the name of the virtual disk. */
  133. DISK_FILE_ENTRY_VolumeID = 0,
  134. /** Long File Name FAT file entry of the virtual FLASH.BIN image file. */
  135. DISK_FILE_ENTRY_FLASH_LFN = 1,
  136. /** Legacy MSDOS FAT file entry of the virtual FLASH.BIN image file. */
  137. DISK_FILE_ENTRY_FLASH_MSDOS = 2,
  138. /** Long File Name FAT file entry of the virtual EEPROM.BIN image file. */
  139. DISK_FILE_ENTRY_EEPROM_LFN = 3,
  140. /** Legacy MSDOS FAT file entry of the virtual EEPROM.BIN image file. */
  141. DISK_FILE_ENTRY_EEPROM_MSDOS = 4,
  142. };
  143. /** Enum for the physical disk blocks of the virtual disk. */
  144. enum
  145. {
  146. /** Boot sector disk block. */
  147. DISK_BLOCK_BootBlock = 0,
  148. /** First copy of the FAT table block. */
  149. DISK_BLOCK_FATBlock1 = 1,
  150. /** Second copy of the FAT table block. */
  151. DISK_BLOCK_FATBlock2 = 2,
  152. /** Root file and directory entries block. */
  153. DISK_BLOCK_RootFilesBlock = 3,
  154. /** Start block of the disk data section. */
  155. DISK_BLOCK_DataStartBlock = 4,
  156. };
  157. /* Type Definitions: */
  158. /** FAT boot block structure definition, used to identify the core
  159. * parameters of a FAT file system stored on a disk.
  160. *
  161. * \note This definition is truncated to save space; the magic signature
  162. * \c 0xAA55 must be appended to the very end of the block for it
  163. * to be detected by the host as a valid boot block.
  164. */
  165. typedef struct
  166. {
  167. uint8_t Bootstrap[3];
  168. uint8_t Description[8];
  169. uint16_t SectorSize;
  170. uint8_t SectorsPerCluster;
  171. uint16_t ReservedSectors;
  172. uint8_t FATCopies;
  173. uint16_t RootDirectoryEntries;
  174. uint16_t TotalSectors16;
  175. uint8_t MediaDescriptor;
  176. uint16_t SectorsPerFAT;
  177. uint16_t SectorsPerTrack;
  178. uint16_t Heads;
  179. uint32_t HiddenSectors;
  180. uint32_t TotalSectors32;
  181. uint16_t PhysicalDriveNum;
  182. uint8_t ExtendedBootRecordSig;
  183. uint32_t VolumeSerialNumber;
  184. uint8_t VolumeLabel[11];
  185. uint8_t FilesystemIdentifier[8];
  186. /* uint8_t BootstrapProgram[448]; */
  187. /* uint16_t MagicSignature; */
  188. } FATBootBlock_t;
  189. /** FAT directory entry structure, for the various kinds of File and
  190. * directory descriptors on a FAT disk.
  191. */
  192. typedef union
  193. {
  194. /** VFAT Long File Name file entry. */
  195. struct
  196. {
  197. uint8_t Ordinal;
  198. uint16_t Unicode1;
  199. uint16_t Unicode2;
  200. uint16_t Unicode3;
  201. uint16_t Unicode4;
  202. uint16_t Unicode5;
  203. uint8_t Attribute;
  204. uint8_t Reserved1;
  205. uint8_t Checksum;
  206. uint16_t Unicode6;
  207. uint16_t Unicode7;
  208. uint16_t Unicode8;
  209. uint16_t Unicode9;
  210. uint16_t Unicode10;
  211. uint16_t Unicode11;
  212. uint16_t Reserved2;
  213. uint16_t Unicode12;
  214. uint16_t Unicode13;
  215. } VFAT_LongFileName;
  216. /** Legacy FAT MSDOS 8.3 file entry. */
  217. struct
  218. {
  219. uint8_t Filename[8];
  220. uint8_t Extension[3];
  221. uint8_t Attributes;
  222. uint8_t Reserved[10];
  223. uint16_t CreationTime;
  224. uint16_t CreationDate;
  225. uint16_t StartingCluster;
  226. uint32_t FileSizeBytes;
  227. } MSDOS_File;
  228. /** Legacy FAT MSDOS (sub-)directory entry. */
  229. struct
  230. {
  231. uint8_t Name[11];
  232. uint8_t Attributes;
  233. uint8_t Reserved[10];
  234. uint16_t CreationTime;
  235. uint16_t CreationDate;
  236. uint16_t StartingCluster;
  237. uint32_t Reserved2;
  238. } MSDOS_Directory;
  239. } FATDirectoryEntry_t;
  240. /* Function Prototypes: */
  241. #if defined(INCLUDE_FROM_VIRTUAL_FAT_C)
  242. static uint8_t ReadEEPROMByte(const uint8_t* const Address) ATTR_NO_INLINE;
  243. static void WriteEEPROMByte(uint8_t* const Address,
  244. const uint8_t Data) ATTR_NO_INLINE;
  245. static void UpdateFAT12ClusterEntry(uint8_t* const FATTable,
  246. const uint16_t Index,
  247. const uint16_t ChainEntry) AUX_BOOT_SECTION;
  248. static void UpdateFAT12ClusterChain(uint8_t* const FATTable,
  249. const uint16_t StartIndex,
  250. const uint8_t ChainLength) AUX_BOOT_SECTION;
  251. static void ReadWriteFLASHFileBlock(const uint16_t BlockNumber,
  252. uint8_t* BlockBuffer,
  253. const bool Read) AUX_BOOT_SECTION;
  254. static void ReadWriteEEPROMFileBlock(const uint16_t BlockNumber,
  255. uint8_t* BlockBuffer,
  256. const bool Read) AUX_BOOT_SECTION;
  257. #endif
  258. void VirtualFAT_WriteBlock(const uint16_t BlockNumber) AUX_BOOT_SECTION;
  259. void VirtualFAT_ReadBlock(const uint16_t BlockNumber) AUX_BOOT_SECTION;
  260. #endif