ff_ioman.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * FreeRTOS+FAT V2.3.3
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * https://www.FreeRTOS.org
  23. * https://github.com/FreeRTOS
  24. *
  25. */
  26. /**
  27. * @file ff_ioman.h
  28. * @ingroup IOMAN
  29. **/
  30. #ifndef _FF_IOMAN_H_
  31. #define _FF_IOMAN_H_
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. #include <stdlib.h> /* Use of malloc() */
  36. #ifndef PLUS_FAT_H
  37. #error this header will be included from "ff_headers.h"
  38. #endif
  39. #define FF_T_FAT12 0x0A
  40. #define FF_T_FAT16 0x0B
  41. #define FF_T_FAT32 0x0C
  42. #define FF_MODE_READ 0x01 /* Buffer / FILE Mode for Read Access. */
  43. #define FF_MODE_WRITE 0x02 /* Buffer / FILE Mode for Write Access. */
  44. #define FF_MODE_APPEND 0x04 /* FILE Mode Append Access. */
  45. #define FF_MODE_CREATE 0x08 /* FILE Mode Create file if not existing. */
  46. #define FF_MODE_TRUNCATE 0x10 /* FILE Mode Truncate an Existing file. */
  47. #define FF_MODE_VIRGIN 0x40 /* Buffer mode: do not fetch content from disk. Used for write-only buffers. */
  48. #define FF_MODE_DIR 0x80 /* Special Mode to open a Dir. (Internal use ONLY!) */
  49. #define FF_MODE_RD_WR ( FF_MODE_READ | FF_MODE_WRITE ) /* Just for bit filtering. */
  50. /* The buffer write-only mode saves a fetch from disk.
  51. * The write-only mode is used when a buffer is needed just
  52. * for clearing sectors. */
  53. #define FF_MODE_WR_ONLY ( FF_MODE_VIRGIN | FF_MODE_WRITE ) /* Buffer for Write-only Access (Internal use ONLY!) */
  54. #define FF_BUF_MAX_HANDLES 0xFFFF /* Maximum number handles sharing a buffer. (16 bit integer, we don't want to overflow it!) */
  55. #define FF_MAX_ENTRIES_PER_DIRECTORY 0xFFFF
  56. #define FF_SIZEOF_DIRECTORY_ENTRY 32
  57. #ifndef pdTRUE_SIGNED
  58. /* Temporary solution: eventually the defines below will appear
  59. * in 'Source\include\projdefs.h' */
  60. #define pdTRUE_SIGNED pdTRUE
  61. #define pdFALSE_SIGNED pdFALSE
  62. #define pdTRUE_UNSIGNED ( ( UBaseType_t ) 1u )
  63. #define pdFALSE_UNSIGNED ( ( UBaseType_t ) 0u )
  64. #endif
  65. /**
  66. * I/O Driver Definitions
  67. * Provide access to any Block Device via the following interfaces.
  68. * Returns the number of blocks actually read or written.
  69. **/
  70. /**
  71. * A special information structure for the FreeRTOS+FAT mass storage device
  72. * driver model.
  73. **/
  74. typedef struct
  75. {
  76. uint16_t BlkSize;
  77. uint32_t TotalBlocks;
  78. } FF_DeviceInfo_t;
  79. #if ( ffconfigHASH_CACHE != 0 )
  80. #define FF_HASH_TABLE_ENTRY_COUNT ( ( ffconfigHASH_TABLE_SIZE + 3 ) / 4 )
  81. struct xHASH_TABLE
  82. {
  83. uint32_t ulDirCluster; /* The Starting Cluster of the dir that the hash represents. */
  84. uint32_t ulNumHandles; /* Number of active Handles using this hash table. */
  85. uint32_t ulMisses; /* Number of times this Hash Table was missed, (i.e. how redundant it is). */
  86. uint32_t ulBitTable[ FF_HASH_TABLE_ENTRY_COUNT ];
  87. };
  88. typedef struct xHASH_TABLE FF_HashTable_t;
  89. void FF_ClearHash( FF_HashTable_t * pxHash,
  90. uint32_t ulHash );
  91. void FF_SetHash( FF_HashTable_t * pxHash,
  92. uint32_t ulHash );
  93. BaseType_t FF_isHashSet( FF_HashTable_t * pxHash,
  94. uint32_t ulHash );
  95. #endif /* ffconfigHASH_CACHE */
  96. /* A forward declaration for the I/O manager, to be used in 'struct xFFDisk'. */
  97. struct _FF_IOMAN;
  98. struct xFFDisk;
  99. typedef void ( * FF_FlushApplicationHook )( struct xFFDisk * pxDisk );
  100. /*
  101. * Some low-level drivers also need to flush data to a device.
  102. * Use an Application hook that will be called every time when
  103. * FF_FlushCache() is called. The semaphore will still be taken
  104. * to avoid unwanted reentrancy.
  105. * For example:
  106. *
  107. * void FTL_FlushData( struct xFFDisk *pxDisk )
  108. * {
  109. * // You may or may not inspect 'pxDisk'
  110. * FTL_FlushTableCache();
  111. * }
  112. *
  113. * Make sure you bind the function to the disc object, right after creation:
  114. *
  115. * pxDisk->fnFlushApplicationHook = FTL_FlushData;
  116. */
  117. /* Structure that contains fields common to all media drivers, and can be
  118. * extended to contain additional fields to tailor it for use with a specific media
  119. * type. */
  120. struct xFFDisk
  121. {
  122. struct
  123. {
  124. /* Flags that can optionally be used by the media driver to ensure the
  125. * disk has been initialised, registered and mounted before it is accessed. */
  126. uint32_t bIsInitialised : 1;
  127. uint32_t bIsMounted : 1;
  128. uint32_t spare0 : 5;
  129. /* The partition number on the media described by this structure. */
  130. uint32_t bPartitionNumber : 8;
  131. uint32_t spare1 : 16;
  132. }
  133. xStatus;
  134. /* Provided to allow this structure to be extended to include additional
  135. * attributes that are specific to a media type. */
  136. void * pvTag;
  137. /* Points to input and output manager used by the disk described by this
  138. * structure. */
  139. struct _FF_IOMAN * pxIOManager;
  140. /* The number of sectors on the disk. */
  141. uint32_t ulNumberOfSectors;
  142. /* See comments here above. */
  143. FF_FlushApplicationHook fnFlushApplicationHook;
  144. /* Field that can optionally be set to a signature that is unique to the
  145. * media. Read and write functions can check the ulSignature field to validate
  146. * the media type before they attempt to access the pvTag field, or perform any
  147. * read and write operations. */
  148. uint32_t ulSignature;
  149. };
  150. typedef struct xFFDisk FF_Disk_t;
  151. typedef int32_t ( * FF_WriteBlocks_t ) ( uint8_t * pucBuffer,
  152. uint32_t ulSectorAddress,
  153. uint32_t ulCount,
  154. FF_Disk_t * pxDisk );
  155. typedef int32_t ( * FF_ReadBlocks_t ) ( uint8_t * pucBuffer,
  156. uint32_t ulSectorAddress,
  157. uint32_t ulCount,
  158. FF_Disk_t * pxDisk );
  159. /**
  160. * @public
  161. * @brief Describes the block device driver interface to FreeRTOS+FAT.
  162. **/
  163. typedef struct
  164. {
  165. FF_WriteBlocks_t fnpWriteBlocks; /* Function Pointer, to write a block(s) from a block device. */
  166. FF_ReadBlocks_t fnpReadBlocks; /* Function Pointer, to read a block(s) from a block device. */
  167. FF_Disk_t * pxDisk; /* Earlier called 'pParam': pointer to some parameters e.g. for a Low-Level Driver Handle. */
  168. } FF_BlockDevice_t;
  169. /**
  170. * @private
  171. * @brief FreeRTOS+FAT handles memory with buffers, described as below.
  172. * @note This may change throughout development.
  173. **/
  174. typedef struct
  175. {
  176. uint32_t ulSector; /* The LBA of the Cached sector. */
  177. uint32_t ulLRU; /* For the Least Recently Used algorithm. */
  178. uint8_t * pucBuffer; /* Pointer to the cache block. */
  179. uint32_t ucMode : 8, /* Read or Write mode. */
  180. bModified : 1, /* If the sector was modified since read. */
  181. bValid : 1; /* Initially FALSE. */
  182. uint16_t usNumHandles; /* Number of objects using this buffer. */
  183. uint16_t usPersistance; /* For the persistance algorithm. */
  184. } FF_Buffer_t;
  185. typedef struct
  186. {
  187. #if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
  188. FF_T_WCHAR pcPath[ ffconfigMAX_FILENAME ];
  189. #else
  190. char pcPath[ ffconfigMAX_FILENAME ];
  191. #endif
  192. uint32_t ulDirCluster;
  193. } FF_PathCache_t;
  194. /**
  195. * @private
  196. * @brief FreeRTOS+FAT identifies a partition with the following data.
  197. * @note This may shrink as development and optimisation goes on.
  198. **/
  199. typedef struct
  200. {
  201. uint32_t ulBeginLBA; /* LBA start address of the partition. */
  202. uint32_t ulFATBeginLBA; /* LBA of the FAT tables. */
  203. uint32_t ulSectorsPerFAT; /* Number of sectors per Fat. */
  204. uint32_t ulTotalSectors;
  205. uint32_t ulDataSectors;
  206. #if ( ffconfigWRITE_FREE_COUNT != 0 )
  207. uint32_t ulFSInfoLBA; /* LBA of the FSINFO sector. */
  208. #endif
  209. uint32_t ulRootDirSectors;
  210. uint32_t ulFirstDataSector;
  211. uint32_t ulClusterBeginLBA; /* LBA of first cluster. */
  212. uint32_t ulNumClusters; /* Number of clusters. */
  213. uint32_t ulRootDirCluster; /* Cluster number of the root directory entry. */
  214. uint32_t ulLastFreeCluster;
  215. uint32_t ulFreeClusterCount; /* Records free space on mount. */
  216. uint32_t ulSectorsPerCluster; /* Number of sectors per Cluster. */
  217. char pcVolumeLabel[ 12 ]; /* Volume Label of the partition. */
  218. uint16_t usBlkSize; /* Size of a Sector Block in bytes. */
  219. uint16_t usReservedSectors;
  220. uint8_t ucType; /* Partition Type Identifier. */
  221. uint8_t ucBlkFactor; /* Scale Factor for block sizes above 512! */
  222. uint8_t ucNumFATS; /* Number of FAT tables. */
  223. uint8_t ucPartitionMounted; /* pdTRUE if the partition is mounted, otherwise pdFALSE. */
  224. #if ( ffconfigPATH_CACHE != 0 )
  225. FF_PathCache_t pxPathCache[ ffconfigPATH_CACHE_DEPTH ];
  226. uint32_t ulPCIndex;
  227. #endif
  228. } FF_Partition_t;
  229. /**
  230. * @public
  231. * @brief FF_IOManager_t Object description.
  232. *
  233. * FreeRTOS+FAT functions around an object like this.
  234. **/
  235. #define FF_FAT_LOCK 0x01 /* Lock bit mask for FAT table locking. */
  236. #define FF_DIR_LOCK 0x02 /* Lock bit mask for DIR modification locking. */
  237. #define FF_BUF_LOCK 0x04 /* Lock bit mask for buffers. */
  238. /**
  239. * @public
  240. * @brief FF_IOManager_t Object. A developer should not touch these values.
  241. *
  242. **/
  243. typedef struct _FF_IOMAN
  244. {
  245. FF_BlockDevice_t xBlkDevice; /* Pointer to a Block device description. */
  246. FF_Partition_t xPartition; /* A partition description. */
  247. FF_Buffer_t * pxBuffers; /* Pointer to an array of buffer descriptors. */
  248. void * pvSemaphore; /* Pointer to a Semaphore object. (For buffer description modifications only!). */
  249. void * FirstFile; /* Pointer to the first File object. */
  250. void * xEventGroup; /* An event group, used for locking FAT, DIR and Buffers. Replaces ucLocks. */
  251. uint8_t * pucCacheMem; /* Pointer to a block of memory for the cache. */
  252. uint16_t usSectorSize; /* The sector size that IOMAN is configured to. */
  253. uint16_t usCacheSize; /* Size of the cache in number of Sectors. */
  254. uint8_t ucPreventFlush; /* Flushing to disk only allowed when 0. */
  255. uint8_t ucFlags; /* Bit-Mask: identifying allocated pointers and other flags */
  256. #if ( ffconfigHASH_CACHE != 0 )
  257. FF_HashTable_t xHashCache[ ffconfigHASH_CACHE_DEPTH ];
  258. #endif
  259. void * pvFATLockHandle;
  260. } FF_IOManager_t;
  261. /* Bit values for 'FF_IOManager_t::ucFlags': */
  262. /* Memory Allocation testing and other flags. */
  263. #define FF_IOMAN_ALLOC_BUFDESCR 0x01 /* Flags the pxBuffers pointer is allocated. */
  264. #define FF_IOMAN_ALLOC_BUFFERS 0x02 /* Flags the pucCacheMem pointer is allocated. */
  265. #define FF_IOMAN_BLOCK_DEVICE_IS_REENTRANT 0x10 /* When true, ffRead/ffWrite are not protected by a semaphore. */
  266. #if ( ffconfigREMOVABLE_MEDIA != 0 )
  267. #define FF_IOMAN_DEVICE_IS_EXTRACTED 0x20
  268. #endif /* ffconfigREMOVABLE_MEDIA */
  269. typedef struct xFF_CREATION_PARAMETERS
  270. {
  271. uint8_t * pucCacheMemory; /* User provided memory, or use NULL to malloc the cache memory. */
  272. uint32_t ulMemorySize; /* Size of the cache memory, must be a multiple of 'ulSectorSize'. */
  273. BaseType_t ulSectorSize; /* Sector size, unit for reading/writing to the disk, normally 512 bytes. */
  274. FF_WriteBlocks_t fnWriteBlocks; /* A function to write sectors to the device. */
  275. FF_ReadBlocks_t fnReadBlocks; /* A function to read sectors from the device. */
  276. FF_Disk_t * pxDisk; /* Some properties of the disk driver. */
  277. void * pvSemaphore; /* Pointer to a Semaphore object. */
  278. BaseType_t xBlockDeviceIsReentrant; /* Make non-zero if ffRead/ffWrite are re-entrant. */
  279. } FF_CreationParameters_t;
  280. /*---------- PROTOTYPES (in order of appearance). */
  281. /* PUBLIC (Interfaces): */
  282. FF_IOManager_t * FF_CreateIOManger( FF_CreationParameters_t * pxParameters,
  283. FF_Error_t * pError );
  284. FF_Error_t FF_DeleteIOManager( FF_IOManager_t * pxIOManager );
  285. FF_Error_t FF_Mount( FF_Disk_t * pxDisk,
  286. BaseType_t xPartitionNumber );
  287. FF_Error_t FF_Unmount( FF_Disk_t * pxDisk );
  288. FF_Error_t FF_FlushCache( FF_IOManager_t * pxIOManager );
  289. static portINLINE BaseType_t FF_Mounted( FF_IOManager_t * pxIOManager )
  290. {
  291. return pxIOManager && pxIOManager->xPartition.ucPartitionMounted;
  292. }
  293. int32_t FF_GetPartitionBlockSize( FF_IOManager_t * pxIOManager );
  294. #if ( ffconfig64_NUM_SUPPORT != 0 )
  295. uint64_t FF_GetVolumeSize( FF_IOManager_t * pxIOManager );
  296. #else
  297. uint32_t FF_GetVolumeSize( FF_IOManager_t * pxIOManager );
  298. #endif
  299. /* PUBLIC (To FreeRTOS+FAT Only): */
  300. int32_t FF_BlockRead( FF_IOManager_t * pxIOManager,
  301. uint32_t ulSectorLBA,
  302. uint32_t ulNumSectors,
  303. void * pBuffer,
  304. BaseType_t aSemLocked );
  305. int32_t FF_BlockWrite( FF_IOManager_t * pxIOManager,
  306. uint32_t ulSectorLBA,
  307. uint32_t ulNumSectors,
  308. void * pBuffer,
  309. BaseType_t aSemLocked );
  310. FF_Error_t FF_IncreaseFreeClusters( FF_IOManager_t * pxIOManager,
  311. uint32_t Count );
  312. FF_Error_t FF_DecreaseFreeClusters( FF_IOManager_t * pxIOManager,
  313. uint32_t Count );
  314. FF_Buffer_t * FF_GetBuffer( FF_IOManager_t * pxIOManager,
  315. uint32_t ulSector,
  316. uint8_t Mode );
  317. FF_Error_t FF_ReleaseBuffer( FF_IOManager_t * pxIOManager,
  318. FF_Buffer_t * pBuffer );
  319. /* 'Internal' to FreeRTOS+FAT. */
  320. typedef struct _SPart
  321. {
  322. uint32_t ulStartLBA; /* FF_FAT_PTBL_LBA */
  323. uint32_t ulSectorCount; /* FF_FAT_PTBL_SECT_COUNT */
  324. uint32_t
  325. ucActive : 8, /* FF_FAT_PTBL_ACTIVE */
  326. ucPartitionID : 8, /* FF_FAT_PTBL_ID */
  327. bIsExtended : 1;
  328. } FF_Part_t;
  329. typedef struct _SPartFound
  330. {
  331. int iCount;
  332. FF_Part_t pxPartitions[ ffconfigMAX_PARTITIONS ];
  333. } FF_SPartFound_t;
  334. /* This function will parse the 4 entries in a partition table: */
  335. void FF_ReadParts( uint8_t * pucBuffer,
  336. FF_Part_t * pxParts );
  337. /* FF_PartitionCount() has now been replaced by FF_PartitionSearch()
  338. * It will enumerate all valid partitions found
  339. * If sector-0 happens to be a valid MBR, 1 partition will be returned
  340. */
  341. FF_Error_t FF_PartitionSearch( FF_IOManager_t * pxIOManager,
  342. FF_SPartFound_t * pPartsFound );
  343. /* HT : for debugging only. */
  344. BaseType_t xIsFatSector( FF_IOManager_t * pxIOManager,
  345. uint32_t ulSectorNr );
  346. BaseType_t xNeedLogging( FF_IOManager_t * pxIOManager );
  347. BaseType_t xIsRootDirSector( FF_IOManager_t * pxIOManager,
  348. uint32_t ulSectorNr );
  349. const char * pcSectorType( FF_IOManager_t * pxIOManager,
  350. uint32_t ulSectorNr );
  351. /* Needed to make this public/private to be used in FF_Partition/FF_Format. */
  352. void FF_IOMAN_InitBufferDescriptors( FF_IOManager_t * pxIOManager );
  353. #ifdef __cplusplus
  354. } /* extern "C" */
  355. #endif
  356. #endif /* ifndef _FF_IOMAN_H_ */