VirtualFAT.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. /** \file
  27. *
  28. * Virtualized FAT12 filesystem implementation, to perform self-programming
  29. * in response to read and write requests to the virtual filesystem by the
  30. * host PC.
  31. */
  32. #define INCLUDE_FROM_VIRTUAL_FAT_C
  33. #include "VirtualFAT.h"
  34. /** FAT filesystem boot sector block, must be the first sector on the physical
  35. * disk so that the host can identify the presence of a FAT filesystem. This
  36. * block is truncated; normally a large bootstrap section is located near the
  37. * end of the block for booting purposes however as this is not meant to be a
  38. * bootable disk it is omitted for space reasons.
  39. *
  40. * \note When returning the boot block to the host, the magic signature 0xAA55
  41. * must be added to the very end of the block to identify it as a boot
  42. * block.
  43. */
  44. static const FATBootBlock_t BootBlock =
  45. {
  46. .Bootstrap = {0xEB, 0x3C, 0x90},
  47. .Description = "mkdosfs",
  48. .SectorSize = SECTOR_SIZE_BYTES,
  49. .SectorsPerCluster = SECTOR_PER_CLUSTER,
  50. .ReservedSectors = 1,
  51. .FATCopies = 2,
  52. .RootDirectoryEntries = (SECTOR_SIZE_BYTES / sizeof(FATDirectoryEntry_t)),
  53. .TotalSectors16 = LUN_MEDIA_BLOCKS,
  54. .MediaDescriptor = 0xF8,
  55. .SectorsPerFAT = 1,
  56. .SectorsPerTrack = (LUN_MEDIA_BLOCKS % 64),
  57. .Heads = (LUN_MEDIA_BLOCKS / 64),
  58. .HiddenSectors = 0,
  59. .TotalSectors32 = 0,
  60. .PhysicalDriveNum = 0,
  61. .ExtendedBootRecordSig = 0x29,
  62. .VolumeSerialNumber = 0x12345678,
  63. .VolumeLabel = "LUFA BOOT ",
  64. .FilesystemIdentifier = "FAT12 ",
  65. };
  66. /** FAT 8.3 style directory entry, for the virtual FLASH contents file. */
  67. static FATDirectoryEntry_t FirmwareFileEntries[] =
  68. {
  69. /* Root volume label entry; disk label is contained in the Filename and
  70. * Extension fields (concatenated) with a special attribute flag - other
  71. * fields are ignored. Should be the same as the label in the boot block.
  72. */
  73. [DISK_FILE_ENTRY_VolumeID] =
  74. {
  75. .MSDOS_Directory =
  76. {
  77. .Name = "LUFA BOOT ",
  78. .Attributes = FAT_FLAG_VOLUME_NAME,
  79. .Reserved = {0},
  80. .CreationTime = 0,
  81. .CreationDate = 0,
  82. .StartingCluster = 0,
  83. .Reserved2 = 0,
  84. }
  85. },
  86. /* VFAT Long File Name entry for the virtual firmware file; required to
  87. * prevent corruption from systems that are unable to detect the device
  88. * as being a legacy MSDOS style FAT12 volume. */
  89. [DISK_FILE_ENTRY_FLASH_LFN] =
  90. {
  91. .VFAT_LongFileName =
  92. {
  93. .Ordinal = 1 | FAT_ORDINAL_LAST_ENTRY,
  94. .Attribute = FAT_FLAG_LONG_FILE_NAME,
  95. .Reserved1 = 0,
  96. .Reserved2 = 0,
  97. .Checksum = FAT_CHECKSUM('F','L','A','S','H',' ',' ',' ','B','I','N'),
  98. .Unicode1 = 'F',
  99. .Unicode2 = 'L',
  100. .Unicode3 = 'A',
  101. .Unicode4 = 'S',
  102. .Unicode5 = 'H',
  103. .Unicode6 = '.',
  104. .Unicode7 = 'B',
  105. .Unicode8 = 'I',
  106. .Unicode9 = 'N',
  107. .Unicode10 = 0,
  108. .Unicode11 = 0,
  109. .Unicode12 = 0,
  110. .Unicode13 = 0,
  111. }
  112. },
  113. /* MSDOS file entry for the virtual Firmware image. */
  114. [DISK_FILE_ENTRY_FLASH_MSDOS] =
  115. {
  116. .MSDOS_File =
  117. {
  118. .Filename = "FLASH ",
  119. .Extension = "BIN",
  120. .Attributes = 0,
  121. .Reserved = {0},
  122. .CreationTime = FAT_TIME(1, 1, 0),
  123. .CreationDate = FAT_DATE(14, 2, 1989),
  124. .StartingCluster = 2,
  125. .FileSizeBytes = FLASH_FILE_SIZE_BYTES,
  126. }
  127. },
  128. [DISK_FILE_ENTRY_EEPROM_LFN] =
  129. {
  130. .VFAT_LongFileName =
  131. {
  132. .Ordinal = 1 | FAT_ORDINAL_LAST_ENTRY,
  133. .Attribute = FAT_FLAG_LONG_FILE_NAME,
  134. .Reserved1 = 0,
  135. .Reserved2 = 0,
  136. .Checksum = FAT_CHECKSUM('E','E','P','R','O','M',' ',' ','B','I','N'),
  137. .Unicode1 = 'E',
  138. .Unicode2 = 'E',
  139. .Unicode3 = 'P',
  140. .Unicode4 = 'R',
  141. .Unicode5 = 'O',
  142. .Unicode6 = 'M',
  143. .Unicode7 = '.',
  144. .Unicode8 = 'B',
  145. .Unicode9 = 'I',
  146. .Unicode10 = 'N',
  147. .Unicode11 = 0,
  148. .Unicode12 = 0,
  149. .Unicode13 = 0,
  150. }
  151. },
  152. [DISK_FILE_ENTRY_EEPROM_MSDOS] =
  153. {
  154. .MSDOS_File =
  155. {
  156. .Filename = "EEPROM ",
  157. .Extension = "BIN",
  158. .Attributes = 0,
  159. .Reserved = {0},
  160. .CreationTime = FAT_TIME(1, 1, 0),
  161. .CreationDate = FAT_DATE(14, 2, 1989),
  162. .StartingCluster = 2 + FILE_CLUSTERS(FLASH_FILE_SIZE_BYTES),
  163. .FileSizeBytes = EEPROM_FILE_SIZE_BYTES,
  164. }
  165. },
  166. };
  167. /** Starting cluster of the virtual FLASH.BIN file on disk, tracked so that the
  168. * offset from the start of the data sector can be determined. On Windows
  169. * systems files are usually replaced using the original file's disk clusters,
  170. * while Linux appears to overwrite with an offset which must be compensated for.
  171. */
  172. static const uint16_t* FLASHFileStartCluster = &FirmwareFileEntries[DISK_FILE_ENTRY_FLASH_MSDOS].MSDOS_File.StartingCluster;
  173. /** Starting cluster of the virtual EEPROM.BIN file on disk, tracked so that the
  174. * offset from the start of the data sector can be determined. On Windows
  175. * systems files are usually replaced using the original file's disk clusters,
  176. * while Linux appears to overwrite with an offset which must be compensated for.
  177. */
  178. static const uint16_t* EEPROMFileStartCluster = &FirmwareFileEntries[DISK_FILE_ENTRY_EEPROM_MSDOS].MSDOS_File.StartingCluster;
  179. /** Reads a byte of EEPROM out from the EEPROM memory space.
  180. *
  181. * \note This function is required as the avr-libc EEPROM functions do not cope
  182. * with linker relaxations, and a jump longer than 4K of FLASH on the
  183. * larger USB AVRs will break the linker. This function is marked as
  184. * never inlinable and placed into the normal text segment so that the
  185. * call to the EEPROM function will be short even if the AUX boot section
  186. * is used.
  187. *
  188. * \param[in] Address Address of the EEPROM location to read from
  189. *
  190. * \return Read byte of EEPROM data.
  191. */
  192. static uint8_t ReadEEPROMByte(const uint8_t* const Address)
  193. {
  194. return eeprom_read_byte(Address);
  195. }
  196. /** Writes a byte of EEPROM out to the EEPROM memory space.
  197. *
  198. * \note This function is required as the avr-libc EEPROM functions do not cope
  199. * with linker relaxations, and a jump longer than 4K of FLASH on the
  200. * larger USB AVRs will break the linker. This function is marked as
  201. * never inlinable and placed into the normal text segment so that the
  202. * call to the EEPROM function will be short even if the AUX boot section
  203. * is used.
  204. *
  205. * \param[in] Address Address of the EEPROM location to write to
  206. * \param[in] Data New data to write to the EEPROM location
  207. */
  208. static void WriteEEPROMByte(uint8_t* const Address,
  209. const uint8_t Data)
  210. {
  211. eeprom_update_byte(Address, Data);
  212. }
  213. /** Updates a FAT12 cluster entry in the FAT file table with the specified next
  214. * chain index. If the cluster is the last in the file chain, the magic value
  215. * \c 0xFFF should be used.
  216. *
  217. * \note FAT data cluster indexes are offset by 2, so that cluster 2 is the
  218. * first file data cluster on the disk. See the FAT specification.
  219. *
  220. * \param[out] FATTable Pointer to the FAT12 allocation table
  221. * \param[in] Index Index of the cluster entry to update
  222. * \param[in] ChainEntry Next cluster index in the file chain
  223. */
  224. static void UpdateFAT12ClusterEntry(uint8_t* const FATTable,
  225. const uint16_t Index,
  226. const uint16_t ChainEntry)
  227. {
  228. /* Calculate the starting offset of the cluster entry in the FAT12 table */
  229. uint8_t FATOffset = (Index + (Index >> 1));
  230. bool UpperNibble = ((Index & 1) != 0);
  231. /* Check if the start of the entry is at an upper nibble of the byte, fill
  232. * out FAT12 entry as required */
  233. if (UpperNibble)
  234. {
  235. FATTable[FATOffset] = (FATTable[FATOffset] & 0x0F) | ((ChainEntry & 0x0F) << 4);
  236. FATTable[FATOffset + 1] = (ChainEntry >> 4);
  237. }
  238. else
  239. {
  240. FATTable[FATOffset] = ChainEntry;
  241. FATTable[FATOffset + 1] = (FATTable[FATOffset] & 0xF0) | (ChainEntry >> 8);
  242. }
  243. }
  244. /** Updates a FAT12 cluster chain in the FAT file table with a linear chain of
  245. * the specified length.
  246. *
  247. * \note FAT data cluster indexes are offset by 2, so that cluster 2 is the
  248. * first file data cluster on the disk. See the FAT specification.
  249. *
  250. * \param[out] FATTable Pointer to the FAT12 allocation table
  251. * \param[in] Index Index of the start of the cluster chain to update
  252. * \param[in] ChainLength Length of the chain to write, in clusters
  253. */
  254. static void UpdateFAT12ClusterChain(uint8_t* const FATTable,
  255. const uint16_t Index,
  256. const uint8_t ChainLength)
  257. {
  258. for (uint8_t i = 0; i < ChainLength; i++)
  259. {
  260. uint16_t CurrentCluster = Index + i;
  261. uint16_t NextCluster = CurrentCluster + 1;
  262. /* Mark last cluster as end of file */
  263. if (i == (ChainLength - 1))
  264. NextCluster = 0xFFF;
  265. UpdateFAT12ClusterEntry(FATTable, CurrentCluster, NextCluster);
  266. }
  267. }
  268. /** Reads or writes a block of data from/to the physical device FLASH using a
  269. * block buffer stored in RAM, if the requested block is within the virtual
  270. * firmware file's sector ranges in the emulated FAT file system.
  271. *
  272. * \param[in] BlockNumber Physical disk block to read from/write to
  273. * \param[in,out] BlockBuffer Pointer to the start of the block buffer in RAM
  274. * \param[in] Read If \c true, the requested block is read, if
  275. * \c false, the requested block is written
  276. */
  277. static void ReadWriteFLASHFileBlock(const uint16_t BlockNumber,
  278. uint8_t* BlockBuffer,
  279. const bool Read)
  280. {
  281. uint16_t FileStartBlock = DISK_BLOCK_DataStartBlock + (*FLASHFileStartCluster - 2) * SECTOR_PER_CLUSTER;
  282. uint16_t FileEndBlock = FileStartBlock + (FILE_SECTORS(FLASH_FILE_SIZE_BYTES) - 1);
  283. /* Range check the write request - abort if requested block is not within the
  284. * virtual firmware file sector range */
  285. if (!((BlockNumber >= FileStartBlock) && (BlockNumber <= FileEndBlock)))
  286. return;
  287. #if (FLASHEND > 0xFFFF)
  288. uint32_t FlashAddress = (uint32_t)(BlockNumber - FileStartBlock) * SECTOR_SIZE_BYTES;
  289. #else
  290. uint16_t FlashAddress = (uint16_t)(BlockNumber - FileStartBlock) * SECTOR_SIZE_BYTES;
  291. #endif
  292. if (Read)
  293. {
  294. /* Read out the mapped block of data from the device's FLASH */
  295. for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i++)
  296. {
  297. #if (FLASHEND > 0xFFFF)
  298. BlockBuffer[i] = pgm_read_byte_far(FlashAddress++);
  299. #else
  300. BlockBuffer[i] = pgm_read_byte(FlashAddress++);
  301. #endif
  302. }
  303. }
  304. else
  305. {
  306. /* Write out the mapped block of data to the device's FLASH */
  307. for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i += 2)
  308. {
  309. if ((FlashAddress % SPM_PAGESIZE) == 0)
  310. {
  311. /* Erase the given FLASH page, ready to be programmed */
  312. BootloaderAPI_ErasePage(FlashAddress);
  313. }
  314. /* Write the next data word to the FLASH page */
  315. BootloaderAPI_FillWord(FlashAddress, (BlockBuffer[i + 1] << 8) | BlockBuffer[i]);
  316. FlashAddress += 2;
  317. if ((FlashAddress % SPM_PAGESIZE) == 0)
  318. {
  319. /* Write the filled FLASH page to memory */
  320. BootloaderAPI_WritePage(FlashAddress - SPM_PAGESIZE);
  321. }
  322. }
  323. }
  324. }
  325. /** Reads or writes a block of data from/to the physical device EEPROM using a
  326. * block buffer stored in RAM, if the requested block is within the virtual
  327. * firmware file's sector ranges in the emulated FAT file system.
  328. *
  329. * \param[in] BlockNumber Physical disk block to read from/write to
  330. * \param[in,out] BlockBuffer Pointer to the start of the block buffer in RAM
  331. * \param[in] Read If \c true, the requested block is read, if
  332. * \c false, the requested block is written
  333. */
  334. static void ReadWriteEEPROMFileBlock(const uint16_t BlockNumber,
  335. uint8_t* BlockBuffer,
  336. const bool Read)
  337. {
  338. uint16_t FileStartBlock = DISK_BLOCK_DataStartBlock + (*EEPROMFileStartCluster - 2) * SECTOR_PER_CLUSTER;
  339. uint16_t FileEndBlock = FileStartBlock + (FILE_SECTORS(EEPROM_FILE_SIZE_BYTES) - 1);
  340. /* Range check the write request - abort if requested block is not within the
  341. * virtual firmware file sector range */
  342. if (!((BlockNumber >= FileStartBlock) && (BlockNumber <= FileEndBlock)))
  343. return;
  344. uint16_t EEPROMAddress = (uint16_t)(BlockNumber - FileStartBlock) * SECTOR_SIZE_BYTES;
  345. if (Read)
  346. {
  347. /* Read out the mapped block of data from the device's EEPROM */
  348. for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i++)
  349. BlockBuffer[i] = ReadEEPROMByte((uint8_t*)EEPROMAddress++);
  350. }
  351. else
  352. {
  353. /* Write out the mapped block of data to the device's EEPROM */
  354. for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i++)
  355. WriteEEPROMByte((uint8_t*)EEPROMAddress++, BlockBuffer[i]);
  356. }
  357. }
  358. /** Writes a block of data to the virtual FAT filesystem, from the USB Mass
  359. * Storage interface.
  360. *
  361. * \param[in] BlockNumber Index of the block to write.
  362. */
  363. void VirtualFAT_WriteBlock(const uint16_t BlockNumber)
  364. {
  365. uint8_t BlockBuffer[SECTOR_SIZE_BYTES];
  366. /* Buffer the entire block to be written from the host */
  367. Endpoint_Read_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
  368. Endpoint_ClearOUT();
  369. switch (BlockNumber)
  370. {
  371. case DISK_BLOCK_BootBlock:
  372. case DISK_BLOCK_FATBlock1:
  373. case DISK_BLOCK_FATBlock2:
  374. /* Ignore writes to the boot and FAT blocks */
  375. break;
  376. case DISK_BLOCK_RootFilesBlock:
  377. /* Copy over the updated directory entries */
  378. memcpy(FirmwareFileEntries, BlockBuffer, sizeof(FirmwareFileEntries));
  379. break;
  380. default:
  381. ReadWriteFLASHFileBlock(BlockNumber, BlockBuffer, false);
  382. ReadWriteEEPROMFileBlock(BlockNumber, BlockBuffer, false);
  383. break;
  384. }
  385. }
  386. /** Reads a block of data from the virtual FAT filesystem, and sends it to the
  387. * host via the USB Mass Storage interface.
  388. *
  389. * \param[in] BlockNumber Index of the block to read.
  390. */
  391. void VirtualFAT_ReadBlock(const uint16_t BlockNumber)
  392. {
  393. uint8_t BlockBuffer[SECTOR_SIZE_BYTES];
  394. memset(BlockBuffer, 0x00, sizeof(BlockBuffer));
  395. switch (BlockNumber)
  396. {
  397. case DISK_BLOCK_BootBlock:
  398. memcpy(BlockBuffer, &BootBlock, sizeof(FATBootBlock_t));
  399. /* Add the magic signature to the end of the block */
  400. BlockBuffer[SECTOR_SIZE_BYTES - 2] = 0x55;
  401. BlockBuffer[SECTOR_SIZE_BYTES - 1] = 0xAA;
  402. break;
  403. case DISK_BLOCK_FATBlock1:
  404. case DISK_BLOCK_FATBlock2:
  405. /* Cluster 0: Media type/Reserved */
  406. UpdateFAT12ClusterEntry(BlockBuffer, 0, 0xF00 | BootBlock.MediaDescriptor);
  407. /* Cluster 1: Reserved */
  408. UpdateFAT12ClusterEntry(BlockBuffer, 1, 0xFFF);
  409. /* Cluster 2 onwards: Cluster chain of FLASH.BIN */
  410. UpdateFAT12ClusterChain(BlockBuffer, *FLASHFileStartCluster, FILE_CLUSTERS(FLASH_FILE_SIZE_BYTES));
  411. /* Cluster 2+n onwards: Cluster chain of EEPROM.BIN */
  412. UpdateFAT12ClusterChain(BlockBuffer, *EEPROMFileStartCluster, FILE_CLUSTERS(EEPROM_FILE_SIZE_BYTES));
  413. break;
  414. case DISK_BLOCK_RootFilesBlock:
  415. memcpy(BlockBuffer, FirmwareFileEntries, sizeof(FirmwareFileEntries));
  416. break;
  417. default:
  418. ReadWriteFLASHFileBlock(BlockNumber, BlockBuffer, true);
  419. ReadWriteEEPROMFileBlock(BlockNumber, BlockBuffer, true);
  420. break;
  421. }
  422. /* Write the entire read block Buffer to the host */
  423. Endpoint_Write_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
  424. Endpoint_ClearIN();
  425. }