ff_error.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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_error.c
  28. * @ingroup ERROR
  29. *
  30. * @defgroup ERR Error Message
  31. * @brief Used to return human readable strings for FreeRTOS+FAT error codes.
  32. *
  33. **/
  34. #include <stdio.h>
  35. #include "ff_headers.h"
  36. #if !defined( ARRAY_SIZE )
  37. #define ARRAY_SIZE( x ) ( int ) ( sizeof( x ) / sizeof( x )[ 0 ] )
  38. #endif
  39. /* This if-block spans the rest of the source file.*/
  40. #if ( ffconfigDEBUG != 0 )
  41. const struct _FFMODULETAB
  42. {
  43. const char * const strModuleName;
  44. const uint8_t ucModuleID;
  45. }
  46. xFreeRTOSFATModuleTable[] =
  47. {
  48. { "Unknown Module", 1 }, /* 1 here is ok, as the GetError functions start at the end of the table. */
  49. { "ff_ioman.c", FF_GETMODULE( FF_MODULE_IOMAN ) },
  50. { "ff_dir.c", FF_GETMODULE( FF_MODULE_DIR ) },
  51. { "ff_file.c", FF_GETMODULE( FF_MODULE_FILE ) },
  52. { "ff_fat.c", FF_GETMODULE( FF_MODULE_FAT ) },
  53. { "ff_crc.c", FF_GETMODULE( FF_MODULE_CRC ) },
  54. { "ff_format.c", FF_GETMODULE( FF_MODULE_FORMAT ) },
  55. { "ff_memory.c", FF_GETMODULE( FF_MODULE_MEMORY ) },
  56. { "ff_string.c", FF_GETMODULE( FF_MODULE_STRING ) },
  57. { "ff_locking.c", FF_GETMODULE( FF_MODULE_LOCKING ) },
  58. { "ff_time.c", FF_GETMODULE( FF_MODULE_TIME ) },
  59. { "Platform Driver", FF_GETMODULE( FF_MODULE_DRIVER ) },
  60. };
  61. #if ( ffconfigHAS_FUNCTION_TAB != 0 )
  62. const struct _FFFUNCTIONTAB
  63. {
  64. const char * const strFunctionName;
  65. const uint16_t ucFunctionID;
  66. }
  67. xFreeRTOSFATFunctionTable[] =
  68. {
  69. { "Unknown Function", 1 },
  70. /*----- FF_IOManager_t - The FreeRTOS+FAT I/O Manager */
  71. { "FF_CreateIOManger", FF_GETMOD_FUNC( FF_CREATEIOMAN ) },
  72. { "FF_DeleteIOManager", FF_GETMOD_FUNC( FF_DESTROYIOMAN ) },
  73. { "FF_Mount", FF_GETMOD_FUNC( FF_MOUNT ) },
  74. { "FF_Unmount", FF_GETMOD_FUNC( FF_UNMOUNT ) },
  75. { "FF_FlushCache", FF_GETMOD_FUNC( FF_FLUSHCACHE ) },
  76. { "FF_GetPartitionBlockSize", FF_GETMOD_FUNC( FF_GETPARTITIONBLOCKSIZE ) },
  77. { "FF_BlockRead", FF_GETMOD_FUNC( FF_BLOCKREAD ) },
  78. { "FF_BlockWrite", FF_GETMOD_FUNC( FF_BLOCKWRITE ) },
  79. { "FF_DetermineFatType", FF_GETMOD_FUNC( FF_DETERMINEFATTYPE ) },
  80. { "FF_GetEfiPartitionEntry", FF_GETMOD_FUNC( FF_GETEFIPARTITIONENTRY ) },
  81. { "FF_UserDriver", FF_GETMOD_FUNC( FF_USERDRIVER ) },
  82. { "FF_DecreaseFreeClusters", FF_GETMOD_FUNC( FF_DECREASEFREECLUSTERS ) },
  83. { "FF_IncreaseFreeClusters", FF_GETMOD_FUNC( FF_INCREASEFREECLUSTERS ) },
  84. { "FF_PartitionSearch", FF_GETMOD_FUNC( FF_PARTITIONSEARCH ) },
  85. { "FF_ParseExtended", FF_GETMOD_FUNC( FF_PARSEEXTENDED ) },
  86. /*----- FF_DIR - The FreeRTOS+FAT directory handling routines */
  87. { "FF_FetchEntryWithContext", FF_GETMOD_FUNC( FF_FETCHENTRYWITHCONTEXT ) },
  88. { "FF_PushEntryWithContext", FF_GETMOD_FUNC( FF_PUSHENTRYWITHCONTEXT ) },
  89. { "FF_GetEntry", FF_GETMOD_FUNC( FF_GETENTRY ) },
  90. { "FF_FindFirst", FF_GETMOD_FUNC( FF_FINDFIRST ) },
  91. { "FF_FindNext", FF_GETMOD_FUNC( FF_FINDNEXT ) },
  92. { "FF_RewindFind", FF_GETMOD_FUNC( FF_REWINDFIND ) },
  93. { "FF_FindFreeDirent", FF_GETMOD_FUNC( FF_FINDFREEDIRENT ) },
  94. { "FF_PutEntry", FF_GETMOD_FUNC( FF_PUTENTRY ) },
  95. { "FF_CreateShortName", FF_GETMOD_FUNC( FF_CREATESHORTNAME ) },
  96. { "FF_CreateLFNs", FF_GETMOD_FUNC( FF_CREATELFNS ) },
  97. { "FF_ExtendDirectory", FF_GETMOD_FUNC( FF_EXTENDDIRECTORY ) },
  98. { "FF_MkDir", FF_GETMOD_FUNC( FF_MKDIR ) },
  99. { "FF_Traverse", FF_GETMOD_FUNC( FF_TRAVERSE ) },
  100. { "FF_FindDir", FF_GETMOD_FUNC( FF_FINDDIR ) },
  101. /*----- FF_FILE - The FreeRTOS+FAT file handling routines */
  102. { "FF_GetModeBits", FF_GETMOD_FUNC( FF_GETMODEBITS ) },
  103. { "FF_Open", FF_GETMOD_FUNC( FF_OPEN ) },
  104. { "FF_isDirEmpty", FF_GETMOD_FUNC( FF_ISDIREMPTY ) },
  105. { "FF_RmDir", FF_GETMOD_FUNC( FF_RMDIR ) },
  106. { "FF_RmFile", FF_GETMOD_FUNC( FF_RMFILE ) },
  107. { "FF_Move", FF_GETMOD_FUNC( FF_MOVE ) },
  108. { "FF_isEOF", FF_GETMOD_FUNC( FF_ISEOF ) },
  109. { "FF_GetSequentialClusters", FF_GETMOD_FUNC( FF_GETSEQUENTIALCLUSTERS ) },
  110. { "FF_ReadClusters", FF_GETMOD_FUNC( FF_READCLUSTERS ) },
  111. { "FF_ExtendFile", FF_GETMOD_FUNC( FF_EXTENDFILE ) },
  112. { "FF_WriteClusters", FF_GETMOD_FUNC( FF_WRITECLUSTERS ) },
  113. { "FF_Read", FF_GETMOD_FUNC( FF_READ ) },
  114. { "FF_GetC", FF_GETMOD_FUNC( FF_GETC ) },
  115. { "FF_GetLine", FF_GETMOD_FUNC( FF_GETLINE ) },
  116. { "FF_Tell", FF_GETMOD_FUNC( FF_TELL ) },
  117. { "FF_Write", FF_GETMOD_FUNC( FF_WRITE ) },
  118. { "FF_PutC", FF_GETMOD_FUNC( FF_PUTC ) },
  119. { "FF_Seek", FF_GETMOD_FUNC( FF_SEEK ) },
  120. { "FF_Invalidate", FF_GETMOD_FUNC( FF_INVALIDATE ) },
  121. { "FF_CheckValid", FF_GETMOD_FUNC( FF_CHECKVALID ) },
  122. { "FF_Close", FF_GETMOD_FUNC( FF_CLOSE ) },
  123. { "FF_SetTime", FF_GETMOD_FUNC( FF_SETTIME ) },
  124. { "FF_BytesLeft", FF_GETMOD_FUNC( FF_BYTESLEFT ) },
  125. { "FF_SetFileTime", FF_GETMOD_FUNC( FF_SETFILETIME ) },
  126. { "FF_InitBuf", FF_GETMOD_FUNC( FF_INITBUF ) },
  127. /*----- FF_FAT - The FreeRTOS+FAT FAT handling routines */
  128. { "FF_getFATEntry", FF_GETMOD_FUNC( FF_GETFATENTRY ) },
  129. { "FF_ClearCluster", FF_GETMOD_FUNC( FF_CLEARCLUSTER ) },
  130. { "FF_putFATEntry", FF_GETMOD_FUNC( FF_PUTFATENTRY ) },
  131. { "FF_FindFreeCluster", FF_GETMOD_FUNC( FF_FINDFREECLUSTER ) },
  132. { "FF_CountFreeClusters", FF_GETMOD_FUNC( FF_COUNTFREECLUSTERS ) },
  133. /*----- FF_UNICODE - The FreeRTOS+FAT hashing routines */
  134. { "FF_Utf8ctoUtf16c", FF_GETMOD_FUNC( FF_UTF8CTOUTF16C ) },
  135. { "FF_Utf16ctoUtf8c", FF_GETMOD_FUNC( FF_UTF16CTOUTF8C ) },
  136. { "FF_Utf32ctoUtf16c", FF_GETMOD_FUNC( FF_UTF32CTOUTF16C ) },
  137. { "FF_Utf16ctoUtf32c", FF_GETMOD_FUNC( FF_UTF16CTOUTF32C ) },
  138. /*----- FF_FORMAT - The FreeRTOS+FAT format routine */
  139. { "FF_FormatPartition", FF_GETMOD_FUNC( FF_FORMATPARTITION ) },
  140. /*----- FF_STDIO - The FreeRTOS+FAT stdio front-end */
  141. { "ff_chmod", FF_GETMOD_FUNC( FF_CHMOD ) },
  142. { "ff_stat", FF_GETMOD_FUNC( FF_STAT_FUNC ) },
  143. };
  144. #endif /* ffconfigHAS_FUNCTION_TAB */
  145. #define TPASTE2( a, b ) a ## b
  146. #if ( ffconfigLONG_ERR_MSG != 0 )
  147. /* To get the full error msg: "Not enough memory (malloc( ) returned NULL )" */
  148. #define ERR_ENTRY( M, E ) { M, TPASTE2( FF_ERR_, E ) }
  149. #else
  150. /* To get a shorter msg: "NOT_ENOUGH_MEMORY" */
  151. #define ERR_ENTRY( M, E ) { # E, TPASTE2( FF_ERR_, E ) }
  152. #endif /* ffconfigLONG_ERR_MSG */
  153. const struct _FFERRTAB
  154. {
  155. const char * const strErrorString;
  156. const uint8_t ucErrorCode; /* Currently there are less then 256 errors, so lets keep this table small. */
  157. }
  158. xFreeRTOSFATErrorTable[] =
  159. {
  160. { "Unknown or Generic Error!", 1 },
  161. ERR_ENTRY( "No Error", NONE ),
  162. ERR_ENTRY( "Null Pointer provided, (probably for IOMAN)", NULL_POINTER ),
  163. ERR_ENTRY( "Not enough memory (malloc() returned NULL)", NOT_ENOUGH_MEMORY ),
  164. ERR_ENTRY( "Device Driver returned a FATAL error!", DEVICE_DRIVER_FAILED ),
  165. ERR_ENTRY( "The blocksize is not 512 multiple", IOMAN_BAD_BLKSIZE ),
  166. ERR_ENTRY( "The memory size, is not a multiple of the blocksize. (Atleast 2 Blocks)", IOMAN_BAD_MEMSIZE ),
  167. ERR_ENTRY( "Device is already registered, use FF_UnregisterBlkDevice() first", IOMAN_DEV_ALREADY_REGD ),
  168. ERR_ENTRY( "No mountable partition was found on the specified device", IOMAN_NO_MOUNTABLE_PARTITION ),
  169. ERR_ENTRY( "The format of the MBR was unrecognised", IOMAN_INVALID_FORMAT ),
  170. ERR_ENTRY( "The provided partition number is out-of-range (0 - 3)", IOMAN_INVALID_PARTITION_NUM ),
  171. ERR_ENTRY( "The selected partition / volume doesn't appear to be FAT formatted", IOMAN_NOT_FAT_FORMATTED ),
  172. ERR_ENTRY( "Cannot register device. (BlkSize not a multiple of 512)", IOMAN_DEV_INVALID_BLKSIZE ),
  173. ERR_ENTRY( "Cannot unregister device, a partition is still mounted", IOMAN_PARTITION_MOUNTED ),
  174. ERR_ENTRY( "Cannot unmount the partition while there are active FILE handles", IOMAN_ACTIVE_HANDLES ),
  175. ERR_ENTRY( "The GPT partition header appears to be corrupt, refusing to mount", IOMAN_GPT_HEADER_CORRUPT ),
  176. ERR_ENTRY( "Disk full", IOMAN_NOT_ENOUGH_FREE_SPACE ),
  177. ERR_ENTRY( "Attempted to Read a sector out of bounds", IOMAN_OUT_OF_BOUNDS_READ ),
  178. ERR_ENTRY( "Attempted to Write a sector out of bounds", IOMAN_OUT_OF_BOUNDS_WRITE ),
  179. ERR_ENTRY( "I/O driver is busy", IOMAN_DRIVER_BUSY ),
  180. ERR_ENTRY( "I/O driver returned fatal error", IOMAN_DRIVER_FATAL_ERROR ),
  181. ERR_ENTRY( "I/O driver returned \"no medium error\"", IOMAN_DRIVER_NOMEDIUM ),
  182. ERR_ENTRY( "Cannot open the file, file already in use", FILE_ALREADY_OPEN ),
  183. ERR_ENTRY( "The specified file could not be found", FILE_NOT_FOUND ),
  184. ERR_ENTRY( "Cannot open a Directory", FILE_OBJECT_IS_A_DIR ),
  185. ERR_ENTRY( "Cannot open for writing: File is marked as Read-Only", FILE_IS_READ_ONLY ),
  186. ERR_ENTRY( "Path not found", FILE_INVALID_PATH ),
  187. ERR_ENTRY( "File operation failed - the file was not opened for writing", FILE_NOT_OPENED_IN_WRITE_MODE ),
  188. ERR_ENTRY( "File operation failed - the file was not opened for reading", FILE_NOT_OPENED_IN_READ_MODE ),
  189. ERR_ENTRY( "File operation failed - could not extend file", FILE_EXTEND_FAILED ),
  190. ERR_ENTRY( "Destination file already exists", FILE_DESTINATION_EXISTS ),
  191. ERR_ENTRY( "Source file was not found", FILE_SOURCE_NOT_FOUND ),
  192. ERR_ENTRY( "Destination path (dir) was not found", FILE_DIR_NOT_FOUND ),
  193. ERR_ENTRY( "Failed to create the directory Entry", FILE_COULD_NOT_CREATE_DIRENT ),
  194. ERR_ENTRY( "A file handle was invalid", FILE_BAD_HANDLE ),
  195. #if ( ffconfigREMOVABLE_MEDIA != 0 )
  196. ERR_ENTRY( "File handle got invalid because media was removed", FILE_MEDIA_REMOVED ),
  197. #endif /* ffconfigREMOVABLE_MEDIA */
  198. ERR_ENTRY( "A file or folder of the same name already exists", DIR_OBJECT_EXISTS ),
  199. ERR_ENTRY( "DIR_DIRECTORY_FULL", DIR_DIRECTORY_FULL ),
  200. ERR_ENTRY( "DIR_END_OF_DIR", DIR_END_OF_DIR ),
  201. ERR_ENTRY( "The directory is not empty", DIR_NOT_EMPTY ),
  202. ERR_ENTRY( "Could not extend File or Folder - No Free Space!", FAT_NO_FREE_CLUSTERS ),
  203. ERR_ENTRY( "Could not find the directory specified by the path", DIR_INVALID_PATH ),
  204. ERR_ENTRY( "The Root Dir is full, and cannot be extended on Fat12 or 16 volumes", DIR_CANT_EXTEND_ROOT_DIR ),
  205. ERR_ENTRY( "Not enough space to extend the directory.", DIR_EXTEND_FAILED ),
  206. ERR_ENTRY( "Name exceeds the number of allowed characters for a filename", DIR_NAME_TOO_LONG ),
  207. #if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
  208. ERR_ENTRY( "An invalid Unicode character was provided!", UNICODE_INVALID_CODE ),
  209. ERR_ENTRY( "Not enough space in the UTF-16 buffer to encode the entire sequence", UNICODE_DEST_TOO_SMALL ),
  210. ERR_ENTRY( "An invalid UTF-16 sequence was encountered", UNICODE_INVALID_SEQUENCE ),
  211. ERR_ENTRY( "Filename exceeds MAX long-filename length when converted to UTF-16", UNICODE_CONVERSION_EXCEEDED ),
  212. #endif /* ffconfigUNICODE_UTF16_SUPPORT */
  213. };
  214. /**
  215. * @public
  216. * @brief Returns a pointer to a string relating to a FreeRTOS+FAT error code.
  217. *
  218. * @param iErrorCode The error code.
  219. *
  220. * @return Pointer to a string describing the error.
  221. *
  222. **/
  223. const char * FF_GetErrMessage( FF_Error_t iErrorCode )
  224. {
  225. uint32_t stCount = ARRAY_SIZE( xFreeRTOSFATErrorTable );
  226. while( stCount-- )
  227. {
  228. if( ( ( UBaseType_t ) xFreeRTOSFATErrorTable[ stCount ].ucErrorCode ) == FF_GETERROR( iErrorCode ) )
  229. {
  230. break;
  231. }
  232. }
  233. return xFreeRTOSFATErrorTable[ stCount ].strErrorString;
  234. }
  235. const char * FF_GetErrModule( FF_Error_t iErrorCode )
  236. {
  237. uint32_t stCount = ARRAY_SIZE( xFreeRTOSFATModuleTable );
  238. while( stCount-- )
  239. {
  240. if( xFreeRTOSFATModuleTable[ stCount ].ucModuleID == ( uint8_t ) FF_GETMODULE( iErrorCode ) )
  241. {
  242. break;
  243. }
  244. }
  245. return xFreeRTOSFATModuleTable[ stCount ].strModuleName;
  246. }
  247. #if ( ffconfigHAS_FUNCTION_TAB != 0 )
  248. const char * FF_GetErrFunction( FF_Error_t iErrorCode )
  249. {
  250. uint32_t stCount = ARRAY_SIZE( xFreeRTOSFATFunctionTable );
  251. uint16_t ModuleFunc = FF_GETMOD_FUNC( iErrorCode );
  252. static char funcCode[ 32 ];
  253. while( stCount-- != 0 )
  254. {
  255. if( xFreeRTOSFATFunctionTable[ stCount ].ucFunctionID == ModuleFunc )
  256. {
  257. return xFreeRTOSFATFunctionTable[ stCount ].strFunctionName;
  258. }
  259. }
  260. snprintf( funcCode, sizeof( funcCode ), "Func %X", ModuleFunc );
  261. return ( const char * ) funcCode;
  262. }
  263. #endif /* ffconfigHAS_FUNCTION_TAB */
  264. const char * FF_GetErrDescription( FF_Error_t iErrorCode,
  265. char * apBuf,
  266. int aMaxlen )
  267. {
  268. if( FF_isERR( iErrorCode ) )
  269. {
  270. #if ( ffconfigHAS_FUNCTION_TAB != 0 )
  271. snprintf( apBuf, ( size_t ) aMaxlen, "%s::%s::%s",
  272. FF_GetErrModule( iErrorCode ),
  273. FF_GetErrFunction( iErrorCode ),
  274. FF_GetErrMessage( iErrorCode ) );
  275. #else
  276. snprintf( apBuf, ( size_t ) aMaxlen, "%s::%s",
  277. FF_GetErrModule( iErrorCode ),
  278. FF_GetErrMessage( iErrorCode ) );
  279. #endif /* ffconfigHAS_FUNCTION_TAB */
  280. }
  281. else
  282. {
  283. snprintf( apBuf, ( size_t ) aMaxlen, "No error" );
  284. }
  285. return apBuf;
  286. }
  287. #endif /* ffconfigDEBUG != 0 */