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