fupdate.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _FUPDATE_H_
  13. #define _FUPDATE_H_
  14. #include "osi_compiler.h"
  15. #include "fupdate_config.h"
  16. OSI_EXTERN_C_BEGIN
  17. // The following APIs are called in application
  18. /**
  19. * \brief firmware update pack file name
  20. *
  21. * The default value is \p FUPDATE_PACK_FILE_NAME. And it can be defined
  22. * as other value to override the default value. When overrided, it
  23. * should be the same as bootloader.
  24. */
  25. extern const char *gFupdatePackFileName;
  26. /**
  27. * \brief firmware update stage file name
  28. *
  29. * The default value is \p FUPDATE_STAGE_FILE_NAME. And it can be defined
  30. * as other value to override the default value. When overrided, it
  31. * should be the same as bootloader.
  32. */
  33. extern const char *gFupdateStageFileName;
  34. /**
  35. * \brief firmware update temporal file name
  36. *
  37. * The default value is \p FUPDATE_TEMP_FILE_NAME. And it can be defined
  38. * as other value to override the default value. When overrided, it
  39. * should be the same as bootloader.
  40. */
  41. extern const char *gFupdateTempFileName;
  42. /**
  43. * \brief firmware update signature file name
  44. *
  45. * The default value is \p FUPDATE_SIGN_FILE_NAME. And it can be defined
  46. * as other value to override the default value. When overrided, it
  47. * should be the same as bootloader.
  48. */
  49. extern const char *gFupdateSignFileName;
  50. /**
  51. * \brief firmware update status
  52. */
  53. typedef enum fupdateStatus
  54. {
  55. FUPDATE_STATUS_NOT_READY, ///< not ready
  56. FUPDATE_STATUS_READY, ///< ready
  57. FUPDATE_STATUS_FINISHED, ///< update finished
  58. } fupdateStatus_t;
  59. /**
  60. * \brief firmware update content type
  61. */
  62. typedef enum fupdateContentType
  63. {
  64. FUPDATE_CONTENT_INVALID = -1, ///< no content updating
  65. FUPDATE_CONTENT_OTHERS, ///< in updating, but not one of the followings
  66. FUPDATE_CONTENT_FLASH, ///< flash updating
  67. } fupdateContentType_t;
  68. /**
  69. * \brief flash updating content
  70. */
  71. typedef struct
  72. {
  73. uint32_t device_name; ///< flash device name
  74. unsigned offset; ///< flash offset
  75. } fupdateFlashContent_t;
  76. /**
  77. * \brief progress callback function type
  78. *
  79. * During firmware update in bootloader, this callback will be called.
  80. * For pre-callback, it will be called before the block will be processed.
  81. * For post-callback, it will be called after the block is processed.
  82. *
  83. * \param block_count total block count
  84. * \param block current block
  85. */
  86. typedef void (*fupdateProgressCallback_t)(int block_count, int block);
  87. /**
  88. * \brief get current update content type
  89. *
  90. * It can be called in pre-callback progress function.
  91. *
  92. * \return
  93. * - current updating content type
  94. */
  95. fupdateContentType_t fupdateGetUpdatingType(void);
  96. /**
  97. * \brief get current flash updating content
  98. *
  99. * It can be called in pre-callback progress function.
  100. *
  101. * \return
  102. * - true on success
  103. * - false on error, invalid parameter or not in flash updating
  104. */
  105. bool fupdateGetFlashUpdatingContent(fupdateFlashContent_t *content);
  106. /**
  107. * \brief get firmware update status
  108. *
  109. * The status is just get from update status file. And it won't perform
  110. * thorough update pack file content check.
  111. *
  112. * \return update status
  113. */
  114. fupdateStatus_t fupdateGetStatus(void);
  115. /**
  116. * \brief get firmware update version information
  117. *
  118. * The old version and new version are embedded inside update pack.
  119. *
  120. * The version strings are specified at update pack creation. It is
  121. * suggestion just use them as trace or report information.
  122. *
  123. * \p old_version and \p new_version are allocated inside. Caller should
  124. * free them after used. When there are no version string inside update
  125. * pack, the returned pointer will be NULL.
  126. *
  127. * \param old_version return pointer for old version
  128. * \param new_version return pointer for new version
  129. * \return
  130. * - true on success
  131. * - false on error
  132. * - out of memory, or invalid parameter
  133. * - status is \p FUPDATE_STATUS_NONE
  134. */
  135. bool fupdateGetVersion(char **old_version, char **new_version);
  136. /**
  137. * \brief invalidate firmware update
  138. *
  139. * Invalidate firmware update status. \p gFupdateStageFileName will be
  140. * removed, \p gFupdateTempFileName will be removed, and
  141. * \p gFupdatePackFileName can be removed optionally.
  142. *
  143. * After previous update is finished, it is recommended to call this,
  144. * and remove update pack file, to release spaces in file system.
  145. *
  146. * It will be ensured that \p fupdateGetStatus will return
  147. * \p FUPDATE_STATUS_NONE after this is called.
  148. *
  149. * \param removePack true to remove update pack file
  150. */
  151. void fupdateInvalidate(bool removePack);
  152. /**
  153. * \brief check whether firmware update file is valid
  154. *
  155. * It will perform thorough update pack file content. So, it will take
  156. * times.
  157. *
  158. * When \p curr_version is not NULL, it will try to check current version
  159. * with old version string embedded in update pack. When old version
  160. * string in update pack is not NULL also, and \p curr_version doesn't
  161. * match old version in update pack, the update pack will be regarded as
  162. * invalid.
  163. *
  164. * It is optional to embed version string inside update pack.
  165. *
  166. * \param curr_version current version string, NULL for not to check
  167. * \return
  168. * - true if the update pack file content is valid
  169. * - false if not
  170. */
  171. bool fupdateIsPackValid(const char *curr_version);
  172. /**
  173. * \brief set firmware update to ready status
  174. *
  175. * Inside this function, the update pack file will be thoroughly checked.
  176. *
  177. * When the update pack file is valid:
  178. * - \p gFupdateStageFileName will be updated to indicate ready status;
  179. * - \p gFupdateTempFileName will be removed;
  180. *
  181. * When the update pack file is invalid:
  182. * - \p gFupdateStageFileName will be removed;
  183. * - \p gFupdateTempFileName will be removed;
  184. *
  185. * It must be called after the update pack file is written and closed. If
  186. * the pack file is modified after \p fupdateSetReady is called,
  187. * bootloader will still try to update. Though bootloader can detected
  188. * update pack invalid and not start real update, it will waste boot
  189. * time.
  190. *
  191. * No matter whether \p fupdateIsPackValid is called, the update pack file
  192. * will be verified inside. So, it will take even longer.
  193. *
  194. * When \p curr_version is not NULL, it will try to check current version
  195. * with old version string embedded in update pack. When old version
  196. * string in update pack is not NULL also, and \p curr_version doesn't
  197. * match old version in update pack, the update pack will be regarded as
  198. * invalid.
  199. *
  200. * It is optional to embed version string inside update pack.
  201. *
  202. * \param curr_version current version string, NULL for not to check
  203. * \return
  204. * - true if update pack file is valid
  205. * - false if not
  206. */
  207. bool fupdateSetReady(const char *curr_version);
  208. /**
  209. * \brief get the required temp file system space of firmware update pack file if it
  210. * can be successfully updated
  211. *
  212. * It will perform thorough update pack file content. So, it will take some time.
  213. *
  214. * \return
  215. * - >0 if the update pack file content is valid and the space size is calcated
  216. * - -1 if check failed
  217. */
  218. int fupdateGetRequiredSpace(void);
  219. /**
  220. * \brief firmware update result
  221. */
  222. typedef enum fupdateResult
  223. {
  224. FUPDATE_RESULT_NOT_READY, ///< not ready
  225. FUPDATE_RESULT_CANNT_START, ///< failed to start
  226. FUPDATE_RESULT_FAILED, ///< started and failed
  227. FUPDATE_RESULT_FINISHED, ///< finished
  228. } fupdateResult_t;
  229. /**
  230. * \brief perform firmware update
  231. *
  232. * It must be called in bootloader. Before \p fupdateRun is called, file
  233. * system should be initializaed.
  234. * - file system for \p CONFIG_FS_FOTA_DATA_DIR
  235. * - file system to be patched
  236. *
  237. * File system layout and mount points must be the same as application.
  238. *
  239. * \p FUPDATE_RESULT_NOT_READY means that \p gFupdateStageFileName doesn't
  240. * exists, or not indicates \p FUPDATE_STATUS_READY. Bootloader shall boot
  241. * application normally.
  242. *
  243. * \p FUPDATE_RESULT_CANNT_START means that \p gFupdateStageFileName
  244. * indicates \p FUPDATE_STATUS_READY, however the update pack is not
  245. * valid or not enough memory can be allocated for upgraded. It shouldn't
  246. * happen, but the current firmware isn't changed. Bootloader can boot
  247. * application normally. And it is suggested to notify application to
  248. * invalidate update status.
  249. *
  250. * \p FUPDATE_RESULT_FAILED means that current firmware is changed, and
  251. * encounter unrecoverable error. Typical case is that power supply is
  252. * unstable. Due to current firmware is changed, bootloader shall **NEVER**
  253. * boot application normally. It is suggested that bootloader shall reboot
  254. * and try again.
  255. *
  256. * \p FUPDATE_RESULT_FINISHED means that current firmware is successfully
  257. * updated. Bootloader can boot application normally, or even better reboot.
  258. * At reboot, \p fupdateRun will return \p FUPDATE_RESULT_NOT_READY.
  259. *
  260. * When \p progress callback is not NULL, it will be called after each block
  261. * is finished.
  262. *
  263. * \param progress progress callback after block is processed
  264. * \return firmware update result
  265. */
  266. fupdateResult_t fupdateRun(fupdateProgressCallback_t progress);
  267. /**
  268. * \brief perform firmware update
  269. *
  270. * It is similar to \p fupdateRun, just \p pre_callback can be specified.
  271. *
  272. * \param pre_callback progress callback before block will be processed
  273. * \param post_callback progress callback after block is processed
  274. * \return firmware update result
  275. */
  276. fupdateResult_t fupdateRunV2(fupdateProgressCallback_t pre_callback, fupdateProgressCallback_t post_callback);
  277. OSI_EXTERN_C_END
  278. #endif