boot_fdl.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Copyright (C) 2019 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 _BOOT_FDL_H_
  13. #define _BOOT_FDL_H_
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include "boot_fdl_channel.h"
  20. #include "osi_api.h"
  21. struct bootSpiFlash;
  22. /**
  23. * @brief the FDL instance
  24. */
  25. typedef struct fdl_engine fdlEngine_t;
  26. struct fdl_packet
  27. {
  28. uint16_t type; ///< cpu endian
  29. uint16_t size; ///< cpu endian
  30. uint8_t content[1]; ///< placeholder for data
  31. } __attribute__((packed));
  32. /**
  33. * @brief FDL packet struct
  34. */
  35. typedef struct fdl_packet fdlPacket_t;
  36. typedef enum
  37. {
  38. SYS_STAGE_NONE,
  39. SYS_STAGE_CONNECTED,
  40. SYS_STAGE_START,
  41. SYS_STAGE_GATHER,
  42. SYS_STAGE_END,
  43. SYS_STAGE_ERROR
  44. } fdlDnldStage_t;
  45. typedef enum
  46. {
  47. FLASH_STAGE_NONE,
  48. FLASH_STAGE_ERASE,
  49. FLASH_STAGE_WRITE,
  50. FLASH_STAGE_WAIT_DATA,
  51. FLASH_STAGE_FINISH,
  52. } fdlFlashDnldStage_t;
  53. /**
  54. * @brief structure to store download state
  55. */
  56. typedef struct
  57. {
  58. fdlDnldStage_t stage; ///< download stage
  59. fdlFlashDnldStage_t flash_stage; ///< async flash stage
  60. uint32_t total_size; ///< download total size
  61. uint32_t received_size; ///< received size
  62. uint32_t start_address; ///< download start address
  63. uint32_t write_address; ///< written address, end address in async
  64. uint32_t erase_address; ///< erased address, end address in async
  65. uint32_t data_verify; ///< data crc
  66. struct bootSpiFlash *flash; ///< flash instance for flash download
  67. uint32_t skip_erase_flash; ///< skip erase flash
  68. } fdlDataDnld_t;
  69. /**
  70. * @brief FDL packet processor
  71. */
  72. typedef void (*fdlProc_t)(fdlEngine_t *fdl, fdlPacket_t *pkt, void *ctx);
  73. /**
  74. * @brief FDL polling processing
  75. */
  76. typedef void (*fdlPolling_t)(fdlEngine_t *fdl, void *ctx);
  77. /**
  78. * @brief Create a fdl engine
  79. *
  80. * @param channel the fdl channel
  81. * @param max_packet_len maximum packet length
  82. * @return
  83. * - NULL fail
  84. * - otherwise the fdl engine instance
  85. */
  86. fdlEngine_t *fdlEngineCreate(fdlChannel_t *channel, unsigned max_packet_len);
  87. /**
  88. * @brief Destroy the fdl engine
  89. *
  90. * @param fdl the fdl engine
  91. */
  92. void fdlEngineDestroy(fdlEngine_t *fdl);
  93. /**
  94. * @brief Get channel of fdl engine
  95. *
  96. * @param fdl the fdl engine
  97. * @return channel
  98. */
  99. fdlChannel_t *fdlEngineGetChannel(fdlEngine_t *fdl);
  100. /**
  101. * @brief Get maximum packet length of fdl engine
  102. *
  103. * @param fdl the fdl engine
  104. * @return maximum packet lemgth
  105. */
  106. unsigned fdlEngineGetMaxPacketLen(fdlEngine_t *fdl);
  107. /**
  108. * @brief Read raw data from fdl
  109. *
  110. * @param fdl the fdl engine
  111. * @param buf the room to store data
  112. * @param size the buffer size
  113. * @retuen
  114. * - num actual read size in bytes
  115. */
  116. uint32_t fdlEngineReadRaw(fdlEngine_t *fdl, void *buf, unsigned size);
  117. /**
  118. * @brief Write raw data via fdl engine
  119. *
  120. * @param fdl the fdl engine
  121. * @param data the data to be written
  122. * @param length the data length
  123. * @return
  124. * - num actual length written
  125. */
  126. uint32_t fdlEngineWriteRaw(fdlEngine_t *fdl, const void *data, unsigned length);
  127. /**
  128. * @brief FDL Send version string to the other peer
  129. *
  130. * @param fdl the fdl engine
  131. * @return
  132. * - true success
  133. * - false false
  134. */
  135. bool fdlEngineSendVersion(fdlEngine_t *fdl);
  136. /**
  137. * @brief FDL identify
  138. */
  139. bool fdlEngineIdentify(fdlEngine_t *fdl, unsigned timeout);
  140. /**
  141. * @brief FDL send no data response of specific type
  142. *
  143. * @param fdl the fdl engine
  144. * @param type response type
  145. * @return
  146. * - true success
  147. * - false fail
  148. */
  149. bool fdlEngineSendRespNoData(fdlEngine_t *fdl, uint16_t type);
  150. /**
  151. * @brief FDL send response of specific type with data
  152. *
  153. * @param fdl the fdl engine
  154. * @param type response type
  155. * @param data the data to be sent
  156. * @param len the data length
  157. * @return
  158. * - true success
  159. * - false fail
  160. */
  161. bool fdlEngineSendRespData(fdlEngine_t *fdl, uint16_t type, const void *data, unsigned length);
  162. /**
  163. * @brief FDL change device baud rate
  164. *
  165. * @param fdl the fdl engine
  166. * @param baud baud rate to be changed
  167. * @return
  168. * - true on success
  169. * - false on fail, usually the baud rate is not supported
  170. */
  171. bool fdlEngineSetBaud(fdlEngine_t *fdl, unsigned baud);
  172. /**
  173. * @brief FDL engine process
  174. *
  175. * @note never return except failed
  176. *
  177. * @param fdl the fdl engine
  178. * @param proc the fdl packet processor
  179. * @param ctx caller context
  180. * @return
  181. * - false fail
  182. */
  183. bool fdlEngineProcess(fdlEngine_t *fdl, fdlProc_t proc, fdlPolling_t polling, void *ctx);
  184. /**
  185. * @brief start FDL download
  186. *
  187. * \p fdlEngineProcess will be called inside.
  188. *
  189. * @param fdl the fdl engine
  190. * @param devtype device type
  191. * @return
  192. * - true on success
  193. * - false on fail
  194. */
  195. bool fdlDnldStart(fdlEngine_t *fdl, unsigned devtype);
  196. #ifdef __cplusplus
  197. }
  198. #endif
  199. #endif