ff_fat.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_fat.h
  28. * @ingroup FAT
  29. **/
  30. #ifndef _FF_FAT_H_
  31. #define _FF_FAT_H_
  32. #ifndef PLUS_FAT_H
  33. #error this header will be included from "ff_headers.h"
  34. #endif
  35. /*---------- ERROR CODES */
  36. /*---------- PROTOTYPES */
  37. /* HT statistics Will be taken away after testing: */
  38. #if ( ffconfigFAT_USES_STAT != 0 )
  39. struct SFatStat
  40. {
  41. unsigned initCount;
  42. unsigned clearCount;
  43. unsigned getCount[ 2 ]; /* Index 0 for READ counts, index 1 for WRITE counts. */
  44. unsigned reuseCount[ 2 ];
  45. unsigned missCount[ 2 ];
  46. };
  47. extern struct SFatStat fatStat;
  48. #endif /* if ( ffconfigFAT_USES_STAT != 0 ) */
  49. #if ( ffconfigWRITE_BOTH_FATS != 0 )
  50. #define ffconfigBUF_STORE_COUNT 2
  51. #else
  52. #define ffconfigBUF_STORE_COUNT 1
  53. #endif
  54. typedef struct _FatBuffers
  55. {
  56. FF_Buffer_t * pxBuffers[ ffconfigBUF_STORE_COUNT ];
  57. uint8_t ucMode; /* FF_MODE_READ or WRITE. */
  58. } FF_FATBuffers_t;
  59. uint32_t FF_getClusterPosition( FF_IOManager_t * pxIOManager,
  60. uint32_t ulEntry,
  61. uint32_t ulEntrySize );
  62. uint32_t FF_getClusterChainNumber( FF_IOManager_t * pxIOManager,
  63. uint32_t ulEntry,
  64. uint32_t ulEntrySize );
  65. uint32_t FF_getMajorBlockNumber( FF_IOManager_t * pxIOManager,
  66. uint32_t ulEntry,
  67. uint32_t ulEntrySize );
  68. uint32_t FF_getMinorBlockNumber( FF_IOManager_t * pxIOManager,
  69. uint32_t ulEntry,
  70. uint32_t ulEntrySize );
  71. uint32_t FF_getMinorBlockEntry( FF_IOManager_t * pxIOManager,
  72. uint32_t ulEntry,
  73. uint32_t ulEntrySize );
  74. /* A partition may define a block size larger than 512 bytes (at offset 0x0B of the PBR).
  75. * This function translates a block address to an address based on 'pxIOManager->usBlkSize',
  76. * which is usually 512 bytes.
  77. */
  78. static portINLINE uint32_t FF_getRealLBA( FF_IOManager_t * pxIOManager,
  79. uint32_t LBA )
  80. {
  81. return LBA * pxIOManager->xPartition.ucBlkFactor;
  82. }
  83. uint32_t FF_Cluster2LBA( FF_IOManager_t * pxIOManager,
  84. uint32_t ulCluster );
  85. uint32_t FF_LBA2Cluster( FF_IOManager_t * pxIOManager,
  86. uint32_t ulAddress );
  87. uint32_t FF_getFATEntry( FF_IOManager_t * pxIOManager,
  88. uint32_t ulCluster,
  89. FF_Error_t * pxError,
  90. FF_FATBuffers_t * pxFATBuffers );
  91. FF_Error_t FF_putFATEntry( FF_IOManager_t * pxIOManager,
  92. uint32_t ulCluster,
  93. uint32_t ulValue,
  94. FF_FATBuffers_t * pxFATBuffers );
  95. BaseType_t FF_isEndOfChain( FF_IOManager_t * pxIOManager,
  96. uint32_t ulFatEntry );
  97. uint32_t FF_FindFreeCluster( FF_IOManager_t * pxIOManager,
  98. FF_Error_t * pxError,
  99. BaseType_t aDoClaim );
  100. uint32_t FF_ExtendClusterChain( FF_IOManager_t * pxIOManager,
  101. uint32_t ulStartCluster,
  102. uint32_t ulCount );
  103. FF_Error_t FF_UnlinkClusterChain( FF_IOManager_t * pxIOManager,
  104. uint32_t ulStartCluster,
  105. BaseType_t xDoTruncate );
  106. uint32_t FF_TraverseFAT( FF_IOManager_t * pxIOManager,
  107. uint32_t ulStart,
  108. uint32_t ulCount,
  109. FF_Error_t * pxError );
  110. uint32_t FF_CreateClusterChain( FF_IOManager_t * pxIOManager,
  111. FF_Error_t * pxError );
  112. uint32_t FF_GetChainLength( FF_IOManager_t * pxIOManager,
  113. uint32_t pa_nStartCluster,
  114. uint32_t * piEndOfChain,
  115. FF_Error_t * pxError );
  116. uint32_t FF_FindEndOfChain( FF_IOManager_t * pxIOManager,
  117. uint32_t Start,
  118. FF_Error_t * pxError );
  119. FF_Error_t FF_ClearCluster( FF_IOManager_t * pxIOManager,
  120. uint32_t ulCluster );
  121. #if ( ffconfig64_NUM_SUPPORT != 0 )
  122. uint64_t FF_GetFreeSize( FF_IOManager_t * pxIOManager,
  123. FF_Error_t * pxError );
  124. #else
  125. uint32_t FF_GetFreeSize( FF_IOManager_t * pxIOManager,
  126. FF_Error_t * pxError );
  127. #endif
  128. /* WARNING: If this prototype changes, it must be updated in ff_ioman.c also! */
  129. uint32_t FF_CountFreeClusters( FF_IOManager_t * pxIOManager,
  130. FF_Error_t * pxError );
  131. FF_Error_t FF_ReleaseFATBuffers( FF_IOManager_t * pxIOManager,
  132. FF_FATBuffers_t * pxFATBuffers );
  133. static portINLINE void FF_InitFATBuffers( FF_FATBuffers_t * pxFATBuffers,
  134. uint8_t ucMode )
  135. {
  136. pxFATBuffers->pxBuffers[ 0 ] = NULL;
  137. #if ffconfigBUF_STORE_COUNT > 1
  138. pxFATBuffers->pxBuffers[ 1 ] = NULL;
  139. #endif
  140. #if ffconfigBUF_STORE_COUNT > 2
  141. #error Please check this code, maybe it is time to use memset
  142. #endif
  143. pxFATBuffers->ucMode = ucMode; /* FF_MODE_READ/WRITE */
  144. #if ffconfigFAT_USES_STAT
  145. {
  146. fatStat.initCount++;
  147. }
  148. #endif
  149. }
  150. #endif /* ifndef _FF_FAT_H_ */