1 /* 2 * Copyright (c) 2022 ASR Microelectronics (Shanghai) 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 #ifndef _OTA_PORT_H_ 17 #define _OTA_PORT_H_ 18 19 #include <stdint.h> 20 21 #define FLASH_START_ADDR 0x10000000 22 #define USER_IMAGE_ADDR 0x10012000 23 #define OTA_START_ADDR 0x10200000 24 #define FLASH_OTA_INFO_ADDR 0x10010000 25 #define IMAGE_MAX_SIZE 0x166000 26 #define FLASH_EMPTY_DATA 0xFFFFFFFF 27 28 #define FLASH_ACCESS_CONTROL0_DEFAULT_VALUE 0x37053977 29 #define FLASH_ACCESS_CONTROL1_DEFAULT_VALUE 0x000014e5 30 31 #define IMAGE_TOKEN "WIFI 5501 A0V2" 32 #define IMAGE_RESERVED_DATA 0xFF 33 34 #define OTA_TAG_VALUE 0x00000000 35 #define FLASH_REMAPPING_EN_VALUE 0x00000000 36 #define IMAGE_COMPRESS_EN_VALUE 0x00000000 37 #define OTA_REGION_DIRTY_FLAG_VALUE 0x00000000 38 #define FLASH_REMAPPING_BANK0_VALUE 0xFFFFFFFF 39 #define FLASH_REMAPPING_BANK1_VALUE 0x00000000 40 41 // size of image header 42 #define IMAGE_HEADER_SIZE 128 43 #define IMAGE_TOKEN_SIZE 32 44 #define IMAGE_APP_VERSION_MAX_SIZE 32 45 #define FLASH_REMAPPING_EN_SIZE 4 46 #define FLASH_REMAPPING_BANK_SIZE 4 47 #define OTA_FLAG_SIZE 4 48 #define IMAGE_COMPRESS_EN_SIZE 4 49 #define IMAGE_LENGTH_SIZE 4 50 #define IMAGE_CRC_SIZE 4 51 #define APP_LENGTH_SIZE 4 52 #define APP_CRC_SIZE 4 53 #define OTA_REGION_DIRTY_FLAG_SIZE 4 54 #define IMAGE_ROLL_BACK_FLAG_SIZE 4 55 #define IMAGE_VERIFY_DONE_SIZE 4 56 #define IMAGE_RESERVED_SIZE (IMAGE_HEADER_SIZE - IMAGE_TOKEN_SIZE - IMAGE_APP_VERSION_MAX_SIZE \ 57 - FLASH_REMAPPING_EN_SIZE - FLASH_REMAPPING_BANK_SIZE - OTA_FLAG_SIZE - IMAGE_COMPRESS_EN_SIZE \ 58 - IMAGE_LENGTH_SIZE - IMAGE_CRC_SIZE - APP_LENGTH_SIZE - APP_CRC_SIZE \ 59 - OTA_REGION_DIRTY_FLAG_SIZE - IMAGE_ROLL_BACK_FLAG_SIZE - IMAGE_VERIFY_DONE_SIZE) 60 61 // size of image 62 #define IMAGE_APP_VERSION_SIZE 24 // e.g. app-1.0.2-20181115.1553 63 64 // offset in image header 65 #define TOKEN_OFFSET 0 66 #define APP_VERSION_OFFSET (TOKEN_OFFSET + IMAGE_TOKEN_SIZE) 67 68 #define IMAGE_HEADER_OFFSET 0x0 // offset in flash region 69 #define OTA_IMAGE_CRC_OFFSET (IMAGE_TOKEN_SIZE + IMAGE_APP_VERSION_MAX_SIZE \ 70 + FLASH_REMAPPING_EN_SIZE + FLASH_REMAPPING_BANK_SIZE + OTA_FLAG_SIZE + IMAGE_COMPRESS_EN_SIZE \ 71 + IMAGE_LENGTH_SIZE) 72 73 // offset in pure image 74 #define IMAGE_APP_VERSION_OFFSET 0x100 75 76 #define OTA_VERIFY_TOKEN 0xACDF160B 77 struct OTA_INFO { 78 char token[IMAGE_TOKEN_SIZE]; 79 char app_version[IMAGE_APP_VERSION_MAX_SIZE]; 80 uint32_t flash_remapping_en; 81 uint32_t flash_remapping_bank; 82 uint32_t ota_flag; 83 uint32_t image_compress_en; 84 uint32_t ota_image_size; 85 uint32_t ota_image_crc; 86 uint32_t app_image_size; // for image compress, image length after de-compress 87 uint32_t app_image_crc; // for image compress, image length after de-compress 88 uint32_t ota_region_dirty_flag; 89 uint32_t image_roll_back_flag; 90 uint32_t verify_done; 91 uint8_t reserved[IMAGE_RESERVED_SIZE]; 92 }; 93 94 typedef enum { 95 LEGA_OTA_FINISH, 96 LEGA_OTA_BREAKPOINT, 97 LEGA_OTA_VERIFY, 98 LEGA_OTA_FINISH_NOVERIFY, 99 } LEGA_OTA_RES_TYPE_E; 100 101 typedef struct { 102 unsigned int dst_adr; 103 unsigned int src_adr; 104 unsigned int len; 105 unsigned short crc; 106 unsigned int upg_flag; 107 unsigned char boot_count; 108 unsigned int rec_size; 109 unsigned int splict_size; 110 int off_bp; /*Break point offset */ 111 LEGA_OTA_RES_TYPE_E res_type; /* result type: OTA_FINISH, OTA_BREAKPOINT */ 112 unsigned short param_crc; /* Parameter crc */ 113 } __attribute__((packed)) lega_ota_boot_param_t; 114 115 /** 116 * init ota partition 117 * 118 * @note when ota start, maybe it need init something 119 * @param something extra info for ota init 120 * 121 * @return 0 : On success, 1 : If an error occurred with any step 122 */ 123 int lega_ota_init(void *something); 124 125 /** 126 * Write data to an area on ota partition 127 * 128 * @param m Refer the ota module which will be used,default module will be used if value is NULL 129 * @param off_set Point to the start address that the data is written to, and 130 * point to the last unwritten address after this function is 131 * returned, so you can call this function serval times without 132 * update this start address. 133 * @param inbuf point to the data buffer that will be written to flash 134 * @param in_buf_len The length of the buffer 135 * 136 * @return 0 : On success, 1 : If an error occurred with any step 137 */ 138 int lega_ota_write(int *off_set, char *in_buf, int in_buf_len); 139 140 /** 141 * Read data from an area on ota Flash to data buffer in RAM 142 * 143 * @param m Refer the ota module which will be used,default module will be used if value is NULL 144 * @param off_set Point to the start address that the data is read, and 145 * point to the last unread address after this function is 146 * returned, so you can call this function serval times without 147 * update this start address. 148 * @param out_buf Point to the data buffer that stores the data read from flash 149 * @param out_buf_len The length of the buffer 150 * 151 * @return 0 : On success, 1 : If an error occurred with any step 152 */ 153 int lega_ota_read(int *off_set, char *out_buf, int out_buf_len); 154 155 /** 156 * Set boot options when ota reboot 157 * 158 * @param m Refer the ota module which will be used,default module will be used if value is NULL 159 * @param something boot parms 160 * 161 * @return kNoErr : On success. kGeneralErr : If an error occurred with any step 162 */ 163 int lega_ota_set_boot(void *something); 164 int lega_ota_rollback(void *something); 165 const char *lega_ota_get_version(unsigned char dev_type); 166 #ifdef AOS_COMP_MSMART 167 void lega_ota_clear_ota_flag(void); 168 #endif 169 #endif // _OTA_PORT_H_ 170