Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

285 linhas
11 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief WINC Flash Interface.
  6. *
  7. * Copyright (c) 2017-2018 Microchip Technology Inc. and its subsidiaries.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Subject to your compliance with these terms, you may use Microchip
  14. * software and any derivatives exclusively with Microchip products.
  15. * It is your responsibility to comply with third party license terms applicable
  16. * to your use of third party software (including open source software) that
  17. * may accompany Microchip software.
  18. *
  19. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
  20. * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
  21. * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
  22. * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
  23. * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
  24. * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
  25. * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
  26. * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
  27. * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
  28. * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  29. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  30. *
  31. * \asf_license_stop
  32. *
  33. */
  34. #ifndef __NM_FLASH_H__
  35. #define __NM_FLASH_H__
  36. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  37. INCLUDES
  38. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  39. #include "common/include/nm_common.h"
  40. #include "driver/include/m2m_types.h"
  41. #define FLASH_RETURN_OK 0
  42. #define FLASH_SECTOR_SIZE FLASH_SECTOR_SZ
  43. #define FLASH_SIGNATURE HOST_CONTROL_FLASH_SIG
  44. #define BACKUP_SIGNATURE 0x424b5550
  45. #define FLASH_MODE_FLAGS_CS NBIT0
  46. #define FLASH_MODE_FLAGS_CS_SWITCH NBIT1
  47. #define FLASH_MODE_FLAGS_CS_SWITCH_TARGET NBIT2
  48. #define FLASH_MODE_FLAGS_CS_VALIDATE_IMAGE NBIT3
  49. #define FLASH_MODE_FLAGS_UNCHANGED NBIT4
  50. #define FLASH_MODE_FLAGS_DATA_IN_BACKUP NBIT5
  51. /*! Bit 0 of u8Flags parameter of @ref tpfDataAccessFn.\n
  52. * Compare buffer against existing data, to avoid unnecessary operation. */
  53. #define FLASH_FN_FLAGS_COMPARE_BEFORE NBIT0
  54. /*! Bit 1 of u8Flags parameter of @ref tpfDataAccessFn.\n
  55. * Fill uninitialized portion of buffer with existing data to avoid losing it in subsequent erase.
  56. * Typically not set unless @ref FLASH_FN_FLAGS_ERASE is set. */
  57. #define FLASH_FN_FLAGS_READ_SURROUNDING NBIT1
  58. /*! Bit 2 of u8Flags parameter of @ref tpfDataAccessFn.\n
  59. * Save buffer to a backup persistent location in case of power loss after subsequent erase.
  60. * A (persistent) record of the backup status must also be kept.
  61. * Typically not set unless @ref FLASH_FN_FLAGS_READ_SURROUNDING and @ref FLASH_FN_FLAGS_ERASE are both set. */
  62. #define FLASH_FN_FLAGS_BACKUP NBIT2
  63. /*! Bit 3 of u8Flags parameter of @ref tpfDataAccessFn.\n
  64. * Erase existing data before writing. */
  65. #define FLASH_FN_FLAGS_ERASE NBIT3
  66. /*! Bit 4 of u8Flags parameter of @ref tpfDataAccessFn.\n
  67. * Write buffer. */
  68. #define FLASH_FN_FLAGS_WRITE NBIT4
  69. /*! Bit 5 of u8Flags parameter of @ref tpfDataAccessFn.\n
  70. * Compare buffer against written data, to provide verification.
  71. * Typically not set unless @ref FLASH_FN_FLAGS_WRITE is set. */
  72. #define FLASH_FN_FLAGS_COMPARE_AFTER NBIT5
  73. /*! Bit 6 of u8Flags parameter of @ref tpfDataAccessFn.\n
  74. * Read data to buffer. Typically this would be the only flag set, meaning read the existing data.
  75. * However, if other flags are set, the read should be performed at the end. */
  76. #define FLASH_FN_FLAGS_READ NBIT6
  77. /*! Bit 0 of u8AccessOptions parameter of various function APIs.\n
  78. * Request to erase existing data before writing.\n
  79. * Only applies when module is providing data to the MCU application.\n
  80. * If set, @ref FLASH_FN_FLAGS_ERASE will be set in subsequent call to function of type
  81. * @ref tpfDataAccessFn. */
  82. #define FLASH_ACCESS_OPTION_ERASE_FIRST NBIT0
  83. /*! Bit 1 of u8AccessOptions parameter of various function APIs.\n
  84. * When set with @ref FLASH_ACCESS_OPTION_ERASE_FIRST, this is a request to do
  85. * read-modify-erase-write (eg if MCU application is storing received data in flash).\n
  86. * Only applies when module is providing data to the MCU application.\n
  87. * If set, @ref FLASH_FN_FLAGS_READ_SURROUNDING may be set in subsequent call to function of type
  88. * @ref tpfDataAccessFn. */
  89. #define FLASH_ACCESS_OPTION_KEEP_SURROUNDING NBIT1
  90. /*! Bit 2 of u8AccessOptions parameter of various function APIs.\n
  91. * When set with @ref FLASH_ACCESS_OPTION_ERASE_FIRST and @ref FLASH_ACCESS_OPTION_KEEP_SURROUNDING,
  92. * this is a request to keep a persistent backup of modified contents during read-modify-erase-write.\n
  93. * Only applies when module is providing data to the MCU application.\n
  94. * If set, @ref FLASH_FN_FLAGS_BACKUP may be set in subsequent call to function of type
  95. * @ref tpfDataAccessFn. */
  96. #define FLASH_ACCESS_OPTION_USE_BACKUP NBIT2
  97. /*! Bit 3 of u8AccessOptions parameter of various function APIs.\n
  98. * Request to compare new data against existing data before erasing/writing, to avoid unnecessary operations.\n
  99. * Applies to data transfer in either direction.\n
  100. * If set, @ref FLASH_FN_FLAGS_COMPARE_BEFORE will be set in subsequent call to function of type
  101. * @ref tpfDataAccessFn. */
  102. #define FLASH_ACCESS_OPTION_COMPARE_BEFORE NBIT3
  103. /*! Bit 4 of u8AccessOptions parameter of various function APIs.\n
  104. * Request for byte-wise verification of write.\n
  105. * Applies to data transfer in either direction.\n
  106. * If set, @ref FLASH_FN_FLAGS_COMPARE_AFTER will be set in subsequent call to function of type
  107. * @ref tpfDataAccessFn. */
  108. #define FLASH_ACCESS_OPTION_COMPARE_AFTER NBIT4
  109. /*! When modifying WINC flash contents, the module determines most options internally. Only two
  110. * options are taken from the u8AccessOptions parameter.\n
  111. * When providing data to the MCU application, the module takes all options from the
  112. * u8AccessOptions parameter. */
  113. #define FLASH_ACCESS_WINC_MASK (FLASH_ACCESS_OPTION_COMPARE_BEFORE | FLASH_ACCESS_OPTION_COMPARE_AFTER)
  114. typedef enum {
  115. CS_INITIALIZE,
  116. CS_INVALIDATE_RB,
  117. CS_VALIDATE_RB,
  118. CS_SWITCH,
  119. CS_VALIDATE_SWITCH,
  120. CS_GET_ACTIVE,
  121. CS_GET_INACTIVE,
  122. CS_DEINITIALIZE
  123. }tenuCSOp;
  124. typedef enum {
  125. /*
  126. * Status values arranged so that status can be updated without any erase operation.
  127. */
  128. FLASH_STATUS_EMPTY = 0xFFFFFFFF,
  129. FLASH_STATUS_NOT_ACTIVE = 0xFFFFFF00,
  130. FLASH_STATUS_ACTIVE = 0xFFFF0000,
  131. FLASH_STATUS_DONE = 0xFF000000
  132. }tenuFlashAccessStatus;
  133. typedef enum {
  134. /*
  135. * Status values arranged so that status can be updated without any erase operation.
  136. */
  137. BACKUP_STATUS_EMPTY = 0xFFFFFFFF,
  138. BACKUP_STATUS_NOT_ACTIVE = BACKUP_SIGNATURE | 0xFFFFFF00,
  139. BACKUP_STATUS_ACTIVE = BACKUP_SIGNATURE,
  140. BACKUP_STATUS_DONE = 0x00000000
  141. }tenuBackupStatus;
  142. typedef enum {
  143. /*
  144. * Special location ID values. If top bit is not set, the location ID is interpreted as flash address.
  145. */
  146. MEM_ID_WINC_FLASH = 0x80000000,
  147. MEM_ID_WINC_ACTIVE,
  148. MEM_ID_WINC_INACTIVE,
  149. MEM_ID_NONE = 0xFFFFFFFF
  150. }tenuMemId;
  151. /*!
  152. @enum tenuFlashAccessItemMode
  153. @brief Transfer modes available for accessing items in WINC flash stores such as TLS root
  154. certificate store.
  155. @see m2m_flash_rootcert_access
  156. */
  157. typedef enum {
  158. /*! Add an item to the relevant WINC flash store. */
  159. FLASH_ITEM_ADD,
  160. /*! Remove an item from the relevant WINC flash store. */
  161. FLASH_ITEM_REMOVE,
  162. /*! Read an item from the relevant WINC flash store, using an identifier. */
  163. FLASH_ITEM_READ,
  164. /*! Read an item from the relevant WINC flash store, using an index. */
  165. FLASH_ITEM_READIDX
  166. }tenuFlashAccessItemMode;
  167. typedef struct {
  168. uint32 u32Signature;
  169. tenuFlashAccessStatus enuTransferStatus;
  170. uint16 u16AppId;
  171. uint8 u8AccessFlags; // These correspond bitwise to application access options.
  172. uint8 u8ModeFlags; // These are set internally, with reference to application mode options.
  173. }tstrFlashAccessPersistent;
  174. #define FLASH_SIG_SZ (sizeof(uint32))
  175. #define FLASH_STA_SZ (sizeof(tenuFlashAccessStatus))
  176. #define FLASH_SIG_STA_SZ (FLASH_SIG_SZ+FLASH_STA_SZ)
  177. typedef struct {
  178. tstrFlashAccessPersistent strPersistentInfo;
  179. tpfDataAccessFn pfDestinationFn;
  180. tpfDataAccessFn pfSourceFn;
  181. uint32 u32Size;
  182. }tstrFlashAccess;
  183. typedef struct {
  184. tenuBackupStatus enuTransferStatus;
  185. uint32 u32DestinationAddr;
  186. uint32 u32SourceAddr;
  187. uint32 u32Size;
  188. }tstrBackup;
  189. #define FLASH_BACKUP_STA_SZ sizeof(tenuBackupStatus)
  190. #define FLASH_BACKUP_STORE_SZ (2*sizeof(tstrBackup))
  191. #define FLASH_BACKUP_STORE_OFFSET (HOST_CONTROL_FLASH_OFFSET + HOST_CONTROL_FLASH_SZ - FLASH_BACKUP_STORE_SZ)
  192. /*!
  193. @struct \
  194. tstrDataAccessInitParams
  195. @brief
  196. This structure contains parameters for initializing a local data access (read, erase or write).
  197. @see tpfDataAccessFn.
  198. @see FLASH_FN_FLAGS_COMPARE_BEFORE
  199. @see FLASH_FN_FLAGS_READ_SURROUNDING
  200. @see FLASH_FN_FLAGS_BACKUP
  201. @see FLASH_FN_FLAGS_ERASE
  202. @see FLASH_FN_FLAGS_WRITE
  203. @see FLASH_FN_FLAGS_COMPARE_AFTER
  204. @see FLASH_FN_FLAGS_READ
  205. */
  206. typedef struct {
  207. /*! Total size of data to be accessed in data location. \n
  208. * This field is set by the module in a call to @ref tpfDataAccessFn. */
  209. uint32 u32TotalSize;
  210. /*! Flags indicating type of data access. \n
  211. * This field is set by the module in a call to @ref tpfDataAccessFn. */
  212. uint8 u8Flags;
  213. /*! Block size, to assist with alignment. \n
  214. * This field can be set by the local memory access function @ref tpfDataAccessFn. Defaults to 0 if not set.
  215. * Recommended values are:\n
  216. * - 4096 if accessing flash memory with erase block size <= 4096.\n
  217. * - 0 if accessing normal memory. */
  218. uint32 u32AlignmentSize;
  219. /*! Offset of access start address, relative to u32AlignmentSize. \n
  220. * This field can be set by the local memory access function @ref tpfDataAccessFn. The field is ignored if u32AlignmentSize < 2. */
  221. uint32 u32StartAlignment;
  222. }tstrDataAccessInitParams;
  223. /*!
  224. @struct \
  225. tstrDataAccessParams
  226. @brief
  227. This structure contains data for local data access (read, erase or write).
  228. @see tpfDataAccessFn.
  229. */
  230. typedef struct {
  231. /*! Buffer to be written to or read from. */
  232. uint8 *pu8Buf;
  233. /*! Total size of the buffer. */
  234. uint32 u32BufSize;
  235. /* Offset of data within the buffer. */
  236. uint32 u32DataOffset;
  237. /* Size of data to be written or read. */
  238. uint32 u32DataSize;
  239. }tstrDataAccessParams;
  240. extern uint16 gu16LastAccessId;
  241. extern uint8 gu8Success;
  242. extern uint8 gu8Changed;
  243. extern uint8 gu8Init;
  244. extern uint8 gu8Reset;
  245. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  246. FUNCTION PROTOTYPES
  247. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  248. sint8 winc_flash_write_verify(uint8 *pu8Buf, uint32 u32Offset, uint32 u32Size);
  249. void set_internal_info(tpfDataAccessFn *ppfFn, uint32 u32LocationId);
  250. void set_internal_info_ptr(tpfDataAccessFn *ppfFn, uint8 *pu8Ptr);
  251. uint8 is_internal_info(tpfDataAccessFn pfFn);
  252. sint8 recover_backup(void);
  253. sint8 prepare_backup(uint32 u32Target);
  254. sint8 image_get_target(uint8 *pu8Target);
  255. sint8 rootcert_get_size(tstrRootCertEntryHeader *pstrHdr, uint16 *pu16Size);
  256. sint8 rootcert_access(tenuFlashAccessItemMode enuMode, tstrRootCertEntryHeader *pstrReferenceHdr, uint16 *pu16EntrySize, uint8 *pu8Buff, uint32 *pu32Offset);
  257. sint8 transfer_run(tstrFlashAccess *pstrFlashAccess);
  258. #endif /* __NM_FLASH_H__ */