wm_fwup.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * @file wm_fwup.h
  3. *
  4. * @brief Firmware upgrade
  5. *
  6. * @author winnermicro
  7. *
  8. * Copyright (c) 2015 Winner Microelectronics Co., Ltd.
  9. */
  10. #ifndef WM_FWUP_H
  11. #define WM_FWUP_H
  12. #include "wm_osal.h"
  13. #include "list.h"
  14. /** firmware update status */
  15. #define TLS_FWUP_STATUS_OK (0)
  16. #define TLS_FWUP_STATUS_EINVALID (1)
  17. #define TLS_FWUP_STATUS_EMEM (2)
  18. #define TLS_FWUP_STATUS_EPERM (3)
  19. #define TLS_FWUP_STATUS_EBUSY (4)
  20. #define TLS_FWUP_STATUS_ESESSIONID (5)
  21. #define TLS_FWUP_STATUS_EIO (6)
  22. #define TLS_FWUP_STATUS_ESIGNATURE (7)
  23. #define TLS_FWUP_STATUS_ECRC (8)
  24. #define TLS_FWUP_STATUS_EUNDEF (9)
  25. /** firmware block size for one time */
  26. #define TLS_FWUP_BLK_SIZE 512
  27. /** firmware update request status */
  28. #define TLS_FWUP_REQ_STATUS_IDLE (0)
  29. #define TLS_FWUP_REQ_STATUS_BUSY (1)
  30. #define TLS_FWUP_REQ_STATUS_SUCCESS (2)
  31. #define TLS_FWUP_REQ_STATUS_FIO (3)
  32. #define TLS_FWUP_REQ_STATUS_FSIGNATURE (4)
  33. #define TLS_FWUP_REQ_STATUS_FMEM (5)
  34. #define TLS_FWUP_REQ_STATUS_FCRC (6)
  35. #define TLS_FWUP_REQ_STATUS_FCOMPLETE (7)
  36. /** firmware update state */
  37. #define TLS_FWUP_STATE_UNDEF (0xffff)
  38. #define TLS_FWUP_STATE_BUSY (1 << 0)
  39. #define TLS_FWUP_STATE_COMPLETE (1 << 1)
  40. #define TLS_FWUP_STATE_ERROR_IO (1 << 2)
  41. #define TLS_FWUP_STATE_ERROR_SIGNATURE (1 << 3)
  42. #define TLS_FWUP_STATE_ERROR_MEM (1 << 4)
  43. #define TLS_FWUP_STATE_ERROR_CRC (1 << 5)
  44. #define TLS_FWUP_STATE_ERROR (TLS_FWUP_STATE_ERROR_IO | TLS_FWUP_STATE_ERROR_SIGNATURE | TLS_FWUP_STATE_ERROR_MEM | TLS_FWUP_STATE_ERROR_CRC)
  45. /** update type 0:firmware, 1: data */
  46. #define TLS_FWUP_DEST_SPECIFIC_FIRMWARE (0)
  47. #define TLS_FWUP_DEST_SPECIFIC_DATA (1)
  48. enum IMAGE_TYPE_ENUM{
  49. IMG_TYPE_SECBOOT = 0x0,
  50. IMG_TYPE_FLASHBIN0 = 0x1,
  51. IMG_TYPE_CPFT = 0xE
  52. };
  53. enum
  54. {
  55. NOT_ZIP_FILE = 0,
  56. ZIP_FILE = 1
  57. };
  58. typedef union {
  59. struct {
  60. uint32_t img_type: 4; /*!< bit: 0.. 3 IMAGE_TYPE_ENUM */
  61. uint32_t code_encrypt:1; /*!< bit: 4 whether the code in flash encrypted */
  62. uint32_t prikey_sel:3; /*!< bit: 5.. 7 private key selection */
  63. uint32_t signature:1; /*!< bit: 8 whether signature flag, only indicates if img contains 128bytes signature in the end*/
  64. uint32_t _reserved1: 7; /*!< bit: 9.. 15 Reserved */
  65. uint32_t zip_type: 1; /*!< bit: 16 zip_type bit */
  66. uint32_t psram_io: 1; /*!< bit: 17 psram_io bit */
  67. uint32_t erase_block_en: 1; /*!< bit: 18 flash erase block enable bit */
  68. uint32_t erase_always: 1; /*!< bit: 19 flash erase always bit */
  69. uint32_t _reserved2: 12; /*!< bit: 20..31 Reserved */
  70. } b; /*!< Structure Access by bit */
  71. uint32_t w; /*!< Type Access by whole register */
  72. } Img_Attr_Type;
  73. typedef struct IMAGE_HEADER_PARAM{
  74. unsigned int magic_no;
  75. Img_Attr_Type img_attr;
  76. unsigned int img_addr;
  77. unsigned int img_len;
  78. unsigned int img_header_addr;
  79. unsigned int upgrade_img_addr;
  80. unsigned int org_checksum;
  81. unsigned int upd_no;
  82. unsigned char ver[16];
  83. unsigned int _reserved0;
  84. unsigned int _reserved1;
  85. struct IMAGE_HEADER_PARAM *next;
  86. unsigned int hd_checksum;
  87. }IMAGE_HEADER_PARAM_ST;
  88. /** Structure for firmware image header */
  89. struct tls_fwup_image_hdr {
  90. u32 magic;
  91. u8 crc8;
  92. u8 dest_specific;
  93. u16 dest_offset; // unit: 4KB, valid when dest_specific is TRUE
  94. u32 file_len;
  95. char time[4];
  96. };
  97. /** Structure for one packet data */
  98. struct tls_fwup_block {
  99. u16 number; //0~Sum-1
  100. u16 sum;
  101. u8 data[TLS_FWUP_BLK_SIZE];
  102. u32 crc32;
  103. u8 pad[8];
  104. };
  105. /** Enumeration for image soure when firmware update */
  106. enum tls_fwup_image_src {
  107. TLS_FWUP_IMAGE_SRC_LUART = 0, /**< LOW SPEED UART */
  108. TLS_FWUP_IMAGE_SRC_HUART, /**< HIGH SPEED UART */
  109. TLS_FWUP_IMAGE_SRC_HSPI, /**< HIGH SPEED SPI */
  110. TLS_FWUP_IMAGE_SRC_WEB /**< WEB SERVER */
  111. };
  112. /** Structure for firmware update request */
  113. struct tls_fwup_request {
  114. struct dl_list list;
  115. u8 *data;
  116. u32 data_len;
  117. int status;
  118. void (*complete)(struct tls_fwup_request *request, void *arg);
  119. void *arg;
  120. };
  121. /** Structure for firmware update */
  122. struct tls_fwup {
  123. struct dl_list wait_list;
  124. tls_os_sem_t *list_lock;
  125. bool busy;
  126. enum tls_fwup_image_src current_image_src;
  127. u16 current_state;
  128. u32 current_session_id;
  129. u32 received_len;
  130. u32 total_len;
  131. u32 updated_len;
  132. u32 program_base;
  133. u32 program_offset;
  134. s32 received_number;
  135. };
  136. /**
  137. * @defgroup System_APIs System APIs
  138. * @brief System APIs
  139. */
  140. /**
  141. * @addtogroup System_APIs
  142. * @{
  143. */
  144. /**
  145. * @defgroup FWUP_APIs FWUP APIs
  146. * @brief firmware upgrade APIs
  147. */
  148. /**
  149. * @addtogroup FWUP_APIs
  150. * @{
  151. */
  152. /**
  153. * @brief This function is used to initialize firmware update task
  154. *
  155. * @param[in] None
  156. *
  157. * @retval TLS_FWUP_STATUS_OK initial success
  158. * @retval TLS_FWUP_STATUS_EBUSY already initialed
  159. * @retval TLS_FWUP_STATUS_EMEM memory error
  160. * @note None
  161. */
  162. int tls_fwup_init(void);
  163. /**
  164. * @brief This function is used to enter firmware update progress.
  165. *
  166. * @param[in] image_src image file's source,
  167. from TLS_FWUP_IMAGE_SRC_LUART,
  168. TLS_FWUP_IMAGE_SRC_WEB,TLS_FWUP_IMAGE_SRC_HUART,
  169. TLS_FWUP_IMAGE_SRC_HSPI
  170. *
  171. * @retval non-zero successfully, return session id
  172. * @retval 0 failed
  173. *
  174. * @note None
  175. */
  176. u32 tls_fwup_enter(enum tls_fwup_image_src image_src);
  177. /**
  178. * @brief This function is used to exit firmware update progress.
  179. *
  180. * @param[in] session_id session identity of firmware update progress
  181. *
  182. * @retval TLS_FWUP_STATUS_OK exit success
  183. * @retval TLS_FWUP_STATUS_EPERM globle param is not initialed
  184. * @retval TLS_FWUP_STATUS_ESESSIONID error session id
  185. * @retval TLS_FWUP_STATUS_EBUSY update state is busy
  186. *
  187. * @note None
  188. */
  189. int tls_fwup_exit(u32 session_id);
  190. /**
  191. * @brief This function is used to start update progress
  192. *
  193. * @param[in] session_id current sessin id
  194. * @param[in] *data the data want to update
  195. * @param[in] data_len data length
  196. *
  197. * @retval TLS_FWUP_STATUS_OK updade success
  198. * @retval TLS_FWUP_STATUS_EPERM globle param is not initialed
  199. * @retval TLS_FWUP_STATUS_ESESSIONID error session id
  200. * @retval TLS_FWUP_STATUS_EINVALID invalid param
  201. * @retval TLS_FWUP_STATUS_EMEM memory error
  202. * @retval TLS_FWUP_STATUS_EIO write flash error
  203. * @retval TLS_FWUP_STATUS_ECRC crc error
  204. * @retval TLS_FWUP_STATUS_ESIGNATURE signature error
  205. * @retval TLS_FWUP_STATUS_EUNDEF other error
  206. *
  207. * @note None
  208. */
  209. int tls_fwup_request_sync(u32 session_id, u8 *data, u32 data_len);
  210. /**
  211. * @brief This function is used to get current update status
  212. *
  213. * @param[in] session_id current sessin id
  214. *
  215. * @retval current state TLS_FWUP_STATUS_OK to TLS_FWUP_STATUS_EUNDEF
  216. *
  217. * @note None
  218. */
  219. u16 tls_fwup_current_state(u32 session_id);
  220. /**
  221. * @brief This function is used to reset the update information
  222. *
  223. * @param[in] session_id current sessin id
  224. *
  225. * @retval TLS_FWUP_STATUS_OK reset success
  226. * @retval TLS_FWUP_STATUS_EPERM globle param is not initialed
  227. * @retval TLS_FWUP_STATUS_ESESSIONID error session id
  228. * @retval TLS_FWUP_STATUS_EBUSY update state is busy
  229. *
  230. * @note None
  231. */
  232. int tls_fwup_reset(u32 session_id);
  233. /**
  234. * @brief This function is used to clear error update state
  235. *
  236. * @param[in] session_id current sessin id
  237. *
  238. * @retval TLS_FWUP_STATUS_OK reset success
  239. *
  240. * @note None
  241. */
  242. int tls_fwup_clear_error(u32 session_id);
  243. /**
  244. * @brief This function is used to set update state to
  245. TLS_FWUP_STATE_ERROR_CRC
  246. *
  247. * @param[in] session_id current sessin id
  248. *
  249. * @retval TLS_FWUP_STATUS_OK set success
  250. * @retval TLS_FWUP_STATUS_EPERM globle param is not initialed
  251. * @retval TLS_FWUP_STATUS_ESESSIONID error session id
  252. *
  253. * @note None
  254. */
  255. int tls_fwup_set_crc_error(u32 session_id);
  256. /**
  257. * @brief This function is used to get progress's status
  258. *
  259. * @param[in] None
  260. *
  261. * @retval TRUE busy
  262. * @retval FALSE idle
  263. *
  264. * @note None
  265. */
  266. int tls_fwup_get_status(void);
  267. /**
  268. * @brief This function is used to set update packet number
  269. *
  270. * @param[in] number
  271. *
  272. * @retval TLS_FWUP_STATUS_OK success
  273. * @retval TLS_FWUP_STATE_UNDEF failed
  274. *
  275. * @note None
  276. */
  277. int tls_fwup_set_update_numer(int number);
  278. /**
  279. * @brief This function is used to get received update packet number
  280. *
  281. * @param[in] None
  282. *
  283. * @retval return current packet number
  284. *
  285. * @note None
  286. */
  287. int tls_fwup_get_current_update_numer(void);
  288. /**
  289. * @brief This function is used to get current session id
  290. *
  291. * @param[in] None
  292. *
  293. * @retval non-zoro session id
  294. * @retval 0 error
  295. *
  296. * @note None
  297. */
  298. int tls_fwup_get_current_session_id(void);
  299. /**
  300. * @brief This function is used to check image header
  301. *
  302. * @param[in] None
  303. *
  304. * @retval TRUE: success
  305. * @retval FALSE: failure
  306. *
  307. * @note None
  308. */
  309. int tls_fwup_img_header_check(IMAGE_HEADER_PARAM_ST *img_param);
  310. /**
  311. * @}
  312. */
  313. /**
  314. * @}
  315. */
  316. #endif /* WM_FWUP_H */