fs.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * fs.h - CC31xx/CC32xx Host Driver Implementation
  3. *
  4. * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * Neither the name of Texas Instruments Incorporated nor the names of
  20. * its contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. /*****************************************************************************/
  37. /* Include files */
  38. /*****************************************************************************/
  39. #include "simplelink.h"
  40. #ifndef __FS_H__
  41. #define __FS_H__
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /*!
  46. \addtogroup FileSystem
  47. @{
  48. */
  49. /*****************************************************************************/
  50. /* Macro declarations */
  51. /*****************************************************************************/
  52. /* FS error codes */
  53. #define SL_FS_OK (0)
  54. #define SL_FS_ERR_EMPTY_SFLASH (-67)
  55. #define SL_FS_ERR_FILE_IS_NOT_SECURE_AND_SIGN (-66)
  56. #define SL_FS_ERASING_FLASH (-65)
  57. #define SL_FS_FILE_HAS_NOT_BEEN_CLOSE_CORRECTLY (-64)
  58. #define SL_FS_WRONG_SIGNATURE (-63)
  59. #define SL_FS_WRONG_SIGNATURE_OR_CERTIFIC_NAME_LENGTH (-62)
  60. #define SL_FS_NOT_16_ALIGNED (-61)
  61. #define SL_FS_CERT_CHAIN_ERROR (-60)
  62. #define SL_FS_FILE_NAME_EXIST (-59)
  63. #define SL_FS_SECURITY_BUF_ALREADY_ALLOC (-58)
  64. #define SL_FS_SECURE_FILE_MUST_BE_COMMIT (-57)
  65. #define SL_FS_ERR_INCORRECT_OFFSET_ALIGNMENT (-56)
  66. #define SL_FS_ERR_FAILED_READ_NVMEM_HEADER (-55)
  67. #define SL_FS_WRONG_FILE_NAME (-54)
  68. #define SL_FS_FILE_SYSTEM_IS_LOCKED (-53)
  69. #define SL_FS_SECURITY_ALLERT (-52)
  70. #define SL_FS_FILE_UNVALID_FILE_SIZE (-51)
  71. #define SL_FS_ERR_TOKEN_IS_NOT_VALID (-50)
  72. #define SL_FS_NO_DEVICE_IS_LOADED (-49)
  73. #define SL_FS_DATA_ADDRESS_SHOUD_BE_IN_DATA_RAM (-48)
  74. #define SL_FS_DATA_IS_NOT_ALIGNED (-47)
  75. #define SL_FS_ERR_OVERLAP_DETECTION_THRESHHOLD (-46)
  76. #define SL_FS_FILE_HAS_RESERVED_NV_INDEX (-45)
  77. #define SL_FS_ERR_MAX_FS_FILES_IS_LARGER (-44)
  78. #define SL_FS_ERR_MAX_FS_FILES_IS_SMALLER (-43)
  79. #define SL_FS_FILE_MAX_SIZE_EXCEEDED (-42)
  80. #define SL_FS_INVALID_BUFFER_FOR_READ (-41)
  81. #define SL_FS_INVALID_BUFFER_FOR_WRITE (-40)
  82. #define SL_FS_ERR_FILE_IMAGE_IS_CORRUPTED (-39)
  83. #define SL_FS_ERR_SIZE_OF_FILE_EXT_EXCEEDED (-38)
  84. #define SL_FS_WARNING_FILE_NAME_NOT_KEPT (-37)
  85. #define SL_FS_ERR_DEVICE_IS_NOT_FORMATTED (-36)
  86. #define SL_FS_ERR_FAILED_WRITE_NVMEM_HEADER (-35)
  87. #define SL_FS_ERR_NO_AVAILABLE_NV_INDEX (-34)
  88. #define SL_FS_ERR_FAILED_TO_ALLOCATE_MEM (-33)
  89. #define SL_FS_ERR_FAILED_TO_READ_INTEGRITY_HEADER_2 (-32)
  90. #define SL_FS_ERR_FAILED_TO_READ_INTEGRITY_HEADER_1 (-31)
  91. #define SL_FS_ERR_NO_AVAILABLE_BLOCKS (-30)
  92. #define SL_FS_ERR_FILE_MAX_SIZE_BIGGER_THAN_EXISTING_FILE (-29)
  93. #define SL_FS_ERR_FILE_EXISTS_ON_DIFFERENT_DEVICE_ID (-28)
  94. #define SL_FS_ERR_INVALID_ACCESS_TYPE (-27)
  95. #define SL_FS_ERR_FILE_ALREADY_EXISTS (-26)
  96. #define SL_FS_ERR_PROGRAM (-25)
  97. #define SL_FS_ERR_NO_ENTRIES_AVAILABLE (-24)
  98. #define SL_FS_ERR_FILE_ACCESS_IS_DIFFERENT (-23)
  99. #define SL_FS_ERR_BAD_FILE_MODE (-22)
  100. #define SL_FS_ERR_FAILED_READ_NVFILE (-21)
  101. #define SL_FS_ERR_FAILED_INIT_STORAGE (-20)
  102. #define SL_FS_ERR_CONTINUE_WRITE_MUST_BE_MOD_4 (-19)
  103. #define SL_FS_ERR_FAILED_LOAD_FILE (-18)
  104. #define SL_FS_ERR_INVALID_HANDLE (-17)
  105. #define SL_FS_ERR_FAILED_TO_WRITE (-16)
  106. #define SL_FS_ERR_OFFSET_OUT_OF_RANGE (-15)
  107. #define SL_FS_ERR_ALLOC (-14)
  108. #define SL_FS_ERR_READ_DATA_LENGTH (-13)
  109. #define SL_FS_ERR_INVALID_FILE_ID (-12)
  110. #define SL_FS_ERR_FILE_NOT_EXISTS (-11)
  111. #define SL_FS_ERR_EMPTY_ERROR (-10)
  112. #define SL_FS_ERR_INVALID_ARGS (-9)
  113. #define SL_FS_ERR_FAILED_TO_CREATE_FILE (-8)
  114. #define SL_FS_ERR_FS_ALREADY_LOADED (-7)
  115. #define SL_FS_ERR_UNKNOWN (-6)
  116. #define SL_FS_ERR_FAILED_TO_CREATE_LOCK_OBJ (-5)
  117. #define SL_FS_ERR_DEVICE_NOT_LOADED (-4)
  118. #define SL_FS_ERR_INVALID_MAGIC_NUM (-3)
  119. #define SL_FS_ERR_FAILED_TO_READ (-2)
  120. #define SL_FS_ERR_NOT_SUPPORTED (-1)
  121. /* end of error codes */
  122. #define _FS_MODE_ACCESS_RESERVED_OFFSET (24)
  123. #define _FS_MODE_ACCESS_RESERVED_MASK (0xFF)
  124. #define _FS_MODE_ACCESS_FLAGS_OFFSET (16)
  125. #define _FS_MODE_ACCESS_FLAGS_MASK (0xFF)
  126. #define _FS_MODE_ACCESS_OFFSET (12)
  127. #define _FS_MODE_ACCESS_MASK (0xF)
  128. #define _FS_MODE_OPEN_SIZE_GRAN_OFFSET (8)
  129. #define _FS_MODE_OPEN_SIZE_GRAN_MASK (0xF)
  130. #define _FS_MODE_OPEN_SIZE_OFFSET (0)
  131. #define _FS_MODE_OPEN_SIZE_MASK (0xFF)
  132. #define MAX_MODE_SIZE (0xFF)
  133. #define _FS_MODE(Access, SizeGran, Size,Flags) (_u32)(((_u32)((Access) & _FS_MODE_ACCESS_MASK)<<_FS_MODE_ACCESS_OFFSET) | \
  134. ((_u32)((SizeGran) & _FS_MODE_OPEN_SIZE_GRAN_MASK)<<_FS_MODE_OPEN_SIZE_GRAN_OFFSET) | \
  135. ((_u32)((Size) & _FS_MODE_OPEN_SIZE_MASK)<<_FS_MODE_OPEN_SIZE_OFFSET) | \
  136. ((_u32)((Flags) & _FS_MODE_ACCESS_FLAGS_MASK)<<_FS_MODE_ACCESS_FLAGS_OFFSET))
  137. /* sl_FsOpen options */
  138. /* Open for Read */
  139. #define FS_MODE_OPEN_READ _FS_MODE(_FS_MODE_OPEN_READ,0,0,0)
  140. /* Open for Write (in case file exist) */
  141. #define FS_MODE_OPEN_WRITE _FS_MODE(_FS_MODE_OPEN_WRITE,0,0,0)
  142. /* Open for Creating a new file */
  143. #define FS_MODE_OPEN_CREATE(maxSizeInBytes,accessModeFlags) _sl_GetCreateFsMode(maxSizeInBytes,accessModeFlags)
  144. /*****************************************************************************/
  145. /* Structure/Enum declarations */
  146. /*****************************************************************************/
  147. typedef struct
  148. {
  149. _u16 flags;
  150. _u32 FileLen;
  151. _u32 AllocatedLen;
  152. _u32 Token[4];
  153. }SlFsFileInfo_t;
  154. typedef enum
  155. {
  156. _FS_MODE_OPEN_READ = 0,
  157. _FS_MODE_OPEN_WRITE,
  158. _FS_MODE_OPEN_CREATE,
  159. _FS_MODE_OPEN_WRITE_CREATE_IF_NOT_EXIST
  160. }SlFsFileOpenAccessType_e;
  161. typedef enum
  162. {
  163. _FS_FILE_OPEN_FLAG_COMMIT = 0x1, /* MIRROR - for fail safe */
  164. _FS_FILE_OPEN_FLAG_SECURE = 0x2, /* SECURE */
  165. _FS_FILE_OPEN_FLAG_NO_SIGNATURE_TEST = 0x4, /* Relevant to secure file only */
  166. _FS_FILE_OPEN_FLAG_STATIC = 0x8, /* Relevant to secure file only */
  167. _FS_FILE_OPEN_FLAG_VENDOR = 0x10, /* Relevant to secure file only */
  168. _FS_FILE_PUBLIC_WRITE= 0x20, /* Relevant to secure file only, the file can be opened for write without Token */
  169. _FS_FILE_PUBLIC_READ = 0x40 /* Relevant to secure file only, the file can be opened for read without Token */
  170. }SlFileOpenFlags_e;
  171. typedef enum
  172. {
  173. _FS_MODE_SIZE_GRAN_256B = 0, /* MAX_SIZE = 64K */
  174. _FS_MODE_SIZE_GRAN_1KB, /* MAX_SIZE = 256K */
  175. _FS_MODE_SIZE_GRAN_4KB, /* MAX_SZIE = 1M */
  176. _FS_MODE_SIZE_GRAN_16KB, /* MAX_SIZE = 4M */
  177. _FS_MODE_SIZE_GRAN_64KB, /* MAX_SIZE = 16M */
  178. _FS_MAX_MODE_SIZE_GRAN
  179. }_SlFsFileOpenMaxSizeGran_e;
  180. /*****************************************************************************/
  181. /* Internal Function prototypes */
  182. /*****************************************************************************/
  183. _u32 _sl_GetCreateFsMode(_u32 maxSizeInBytes,_u32 accessFlags);
  184. /*****************************************************************************/
  185. /* Function prototypes */
  186. /*****************************************************************************/
  187. /*!
  188. \brief open file for read or write from/to storage device
  189. \param[in] pFileName File Name buffer pointer
  190. \param[in] AccessModeAndMaxSize Options: As described below
  191. \param[in] pToken Reserved for future use. Use NULL for this field
  192. \param[out] pFileHandle Pointing on the file and used for read and write commands to the file
  193. AccessModeAndMaxSize possible input \n
  194. FS_MODE_OPEN_READ - Read a file \n
  195. FS_MODE_OPEN_WRITE - Open for write for an existing file \n
  196. FS_MODE_OPEN_CREATE(maxSizeInBytes,accessModeFlags) - Open for creating a new file. Max file size is defined in bytes. \n
  197. For optimal FS size, use max size in 4K-512 bytes steps (e.g. 3584,7680,117760) \n
  198. Several access modes bits can be combined together from SlFileOpenFlags_e enum
  199. \return On success, zero is returned. On error, an error code is returned
  200. \sa sl_FsRead sl_FsWrite sl_FsClose
  201. \note belongs to \ref basic_api
  202. \warning
  203. \par Example:
  204. \code
  205. char* DeviceFileName = "MyFile.txt";
  206. unsigned long MaxSize = 63 * 1024; //62.5K is max file size
  207. long DeviceFileHandle = -1;
  208. long RetVal; //negative retval is an error
  209. unsigned long Offset = 0;
  210. unsigned char InputBuffer[100];
  211. // Create a file and write data. The file in this example is secured, without signature and with a fail safe commit
  212. RetVal = sl_FsOpen((unsigned char *)DeviceFileName,
  213. FS_MODE_OPEN_CREATE(MaxSize , _FS_FILE_OPEN_FLAG_NO_SIGNATURE_TEST | _FS_FILE_OPEN_FLAG_COMMIT ),
  214. NULL, &DeviceFileHandle);
  215. Offset = 0;
  216. //Preferred in secure file that the Offset and the length will be aligned to 16 bytes.
  217. RetVal = sl_FsWrite( DeviceFileHandle, Offset, (unsigned char *)"HelloWorld", strlen("HelloWorld"));
  218. RetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);
  219. // open the same file for read, using the Token we got from the creation procedure above
  220. RetVal = sl_FsOpen((unsigned char *)DeviceFileName,
  221. FS_MODE_OPEN_READ,
  222. NULL, &DeviceFileHandle);
  223. Offset = 0;
  224. RetVal = sl_FsRead( DeviceFileHandle, Offset, (unsigned char *)InputBuffer, strlen("HelloWorld"));
  225. RetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);
  226. \endcode
  227. */
  228. #if _SL_INCLUDE_FUNC(sl_FsOpen)
  229. _i32 sl_FsOpen(const _u8 *pFileName,const _u32 AccessModeAndMaxSize,_u32 *pToken,_i32 *pFileHandle);
  230. #endif
  231. /*!
  232. \brief close file in storage device
  233. \param[in] FileHdl Pointer to the file (assigned from sl_FsOpen)
  234. \param[in] pCeritificateFileName Reserved for future use. Use NULL.
  235. \param[in] pSignature Reserved for future use. Use NULL.
  236. \param[in] SignatureLen Reserved for future use. Use 0.
  237. \return On success, zero is returned. On error, an error code is returned
  238. \sa sl_FsRead sl_FsWrite sl_FsOpen
  239. \note Call the fs_Close with signature = 'A' signature len = 1 for activating an abort action
  240. \warning
  241. \par Example:
  242. \code
  243. sl_FsClose(FileHandle,0,0,0);
  244. \endcode
  245. */
  246. #if _SL_INCLUDE_FUNC(sl_FsClose)
  247. _i16 sl_FsClose(const _i32 FileHdl,const _u8* pCeritificateFileName,const _u8* pSignature,const _u32 SignatureLen);
  248. #endif
  249. /*!
  250. \brief Read block of data from a file in storage device
  251. \param[in] FileHdl Pointer to the file (assigned from sl_FsOpen)
  252. \param[in] Offset Offset to specific read block
  253. \param[out] pData Pointer for the received data
  254. \param[in] Len Length of the received data
  255. \return On success, returns the number of read bytes. On error, negative number is returned
  256. \sa sl_FsClose sl_FsWrite sl_FsOpen
  257. \note belongs to \ref basic_api
  258. \warning
  259. \par Example:
  260. \code
  261. Status = sl_FsRead(FileHandle, 0, &readBuff[0], readSize);
  262. \endcode
  263. */
  264. #if _SL_INCLUDE_FUNC(sl_FsRead)
  265. _i32 sl_FsRead(const _i32 FileHdl,_u32 Offset ,_u8* pData,_u32 Len);
  266. #endif
  267. /*!
  268. \brief write block of data to a file in storage device
  269. \param[in] FileHdl Pointer to the file (assigned from sl_FsOpen)
  270. \param[in] Offset Offset to specific block to be written
  271. \param[in] pData Pointer the transmitted data to the storage device
  272. \param[in] Len Length of the transmitted data
  273. \return On success, returns the number of written bytes. On error, an error code is returned
  274. \sa
  275. \note belongs to \ref basic_api
  276. \warning
  277. \par Example:
  278. \code
  279. Status = sl_FsWrite(FileHandle, 0, &buff[0], readSize);
  280. \endcode
  281. */
  282. #if _SL_INCLUDE_FUNC(sl_FsWrite)
  283. _i32 sl_FsWrite(const _i32 FileHdl,_u32 Offset,_u8* pData,_u32 Len);
  284. #endif
  285. /*!
  286. \brief get info on a file
  287. \param[in] pFileName File name
  288. \param[in] Token Reserved for future use. Use 0
  289. \param[out] pFsFileInfo Returns the File's Information: flags,file size, allocated size and Tokens
  290. \return On success, zero is returned. On error, an error code is returned
  291. \sa sl_FsOpen
  292. \note belongs to \ref basic_api
  293. \warning
  294. \par Example:
  295. \code
  296. Status = sl_FsGetInfo("FileName.html",0,&FsFileInfo);
  297. \endcode
  298. */
  299. #if _SL_INCLUDE_FUNC(sl_FsGetInfo)
  300. _i16 sl_FsGetInfo(const _u8 *pFileName,const _u32 Token,SlFsFileInfo_t* pFsFileInfo);
  301. #endif
  302. /*!
  303. \brief Delete specific file from a storage or all files from a storage (format)
  304. \param[in] pFileName File Name
  305. \param[in] Token Reserved for future use. Use 0
  306. \return On success, zero is returned. On error, an error code is returned
  307. \sa
  308. \note belongs to \ref basic_api
  309. \warning
  310. \par Example:
  311. \code
  312. Status = sl_FsDel("FileName.html",0);
  313. \endcode
  314. */
  315. #if _SL_INCLUDE_FUNC(sl_FsDel)
  316. _i16 sl_FsDel(const _u8 *pFileName,const _u32 Token);
  317. #endif
  318. /*!
  319. Close the Doxygen group.
  320. @}
  321. */
  322. #ifdef __cplusplus
  323. }
  324. #endif /* __cplusplus */
  325. #endif /* __FS_H__ */