diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/board.c b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/board.c index e754866df..fe5b35a75 100644 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/board.c +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/board.c @@ -369,17 +369,35 @@ struct InitSequenceDesc _board_init[] = #ifdef BSP_USING_OTA static void OtaCmd(void) { - int32_t Size; + int32_t size; + ota_info_t ota_info; FLASH_Init(); UartConfig(); - UpdateOTAStatus(OTA_STATUS_DOWNLOADING); - Size = SerialDownload(DOWN_FLAH_ADDRESS); - UpdateOTAStatus(OTA_STATUS_DOWNLOADED); - if(Size > 0) + + memcpy(&ota_info, (const void *)FLAG_FLAH_ADDRESS,sizeof(ota_info_t)); + ota_info.status = OTA_STATUS_DOWNLOADING; + UpdateOTAFlag(&ota_info); + size = SerialDownload(DOWN_FLAH_ADDRESS); + ota_info.status = OTA_STATUS_DOWNLOADED; + UpdateOTAFlag(&ota_info); + if(size > 0) { - UpdateOTAFlag(Size, 0x11223344, OTA_STATUS_READY, "OTA Test!","No error!"); + ota_info.down.size = size; + ota_info.down.crc32= calculate_crc32(DOWN_FLAH_ADDRESS, size); + ota_info.down.version = 0x11223344; + strncpy(ota_info.down.description, "OTA Test!",sizeof(ota_info.down.description)); + ota_info.status = OTA_STATUS_READY; + strncpy(ota_info.error_message, "Download the firmware to the download partition successfully!",sizeof(ota_info.error_message)); + UpdateOTAFlag(&ota_info); } + else + { + ota_info.status = OTA_STATUS_ERROR; + strncpy(ota_info.error_message, "Failed to download firmware to download partition!",sizeof(ota_info.error_message)); + UpdateOTAFlag(&ota_info); + } + FLASH_DeInit(); __set_FAULTMASK(1); @@ -437,4 +455,10 @@ void InitBoardHardware() } KPrintf("board init done.\n"); KPrintf("start kernel...\n"); + +#ifdef BSP_USING_OTA + FLASH_Init(); + //跳转成功将对应跳转失败标志清零 + FLASH_DeInit(); +#endif } diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/flash.c b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/flash.c index 7091142e8..93938e5ed 100644 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/flash.c +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/flash.c @@ -684,7 +684,7 @@ status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len) * : FlashָȵĿռ * : addr:ʼַ byte_cnt:Ҫֽ,4kֽΪСλ -* ֵ: None +* ֵ: ִгɹ״ֵ̬Ϊ kStatus_Success״ֵ̬Ϊ * ע : 4kֽڵģҲҪ4kֽ *******************************************************************************/ status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt) @@ -708,16 +708,30 @@ status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt) /******************************************************************************* * : flash_write -* : FLASH_WritePageͬдFlashһҳ +* : ָflashʼַдָȵ * : addr:дʼַ buf:ݴ洢 - len:Ҫдֽ(256) + byte_cnt:Ҫдֽ * ֵ: ִгɹ״ֵ̬Ϊ kStatus_Success״ֵ̬Ϊ -* ע : ַָʼд256ֽڵ *******************************************************************************/ status_t flash_write(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt) { - return FLASH_WritePage(start_addr, (void *)buf, byte_cnt); + uint32_t size; + status_t status; + while(byte_cnt > 0) + { + size = byte_cnt > FLASH_PAGE_SIZE ? FLASH_PAGE_SIZE : byte_cnt; + status = FLASH_WritePage(start_addr, (void *)buf, size); + if(status != kStatus_Success) + { + return status; + } + start_addr += size; + buf += size; + byte_cnt -= size; + } + + return kStatus_Success; } diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/imxrt_ota.c b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/imxrt_ota.c index c6b8c368e..db68e98ef 100644 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/imxrt_ota.c +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/imxrt_ota.c @@ -100,7 +100,7 @@ static const uint32_t crc32tab[] = { len:ʾҪCRC32ݳ * ֵ: õCRC32ֵ *******************************************************************************/ -static uint32_t calculate_crc32(uint32_t addr, uint32_t len) +uint32_t calculate_crc32(uint32_t addr, uint32_t len) { uint32_t crc = 0xFFFFFFFF; uint8_t byte = 0xFF; @@ -114,52 +114,26 @@ static uint32_t calculate_crc32(uint32_t addr, uint32_t len) } -/******************************************************************************* -* : UpdateOTAStatus -* : OTA״̬Ϣ -* : status:Ҫĵ״ֵ̬ -* ֵ: -*******************************************************************************/ -void UpdateOTAStatus(ota_status_t status) -{ - ota_info_t ota_info; - - //FlashжȡOTAϢ - memcpy(&ota_info, (const void *)FLAG_FLAH_ADDRESS,sizeof(ota_info_t)); - ota_info.status = status; - flash_erase(FLAG_FLAH_ADDRESS,sizeof(ota_info_t)); - flash_write(FLAG_FLAH_ADDRESS,(void *)&ota_info,sizeof(ota_info_t)); -} - - /******************************************************************************* * : UpdateOTAFlag * : OTA FlagϢ汾غappе -* : app_size:µĹ̼ĴС,λֽ - version:µĹ̼İ汾 - status:OTA״̬Ϣ - description:°汾Ĺ̼ - error_message:¹д洢ĴϢ -* ֵ: +* : ptr:ota_info_tṹָ,OTAϢ +* ֵ: ִгɹ״ֵ̬Ϊ kStatus_Success״ֵ̬Ϊ *******************************************************************************/ -void UpdateOTAFlag(uint32_t app_size, uint32_t version, uint32_t status, uint8_t* description, uint8_t* error_message) +status_t UpdateOTAFlag(ota_info_t *ptr) { - ota_info_t ota_info; // OTAϢṹ + status_t status; - // FlashжȡOTAϢ - memcpy(&ota_info, (const void *)FLAG_FLAH_ADDRESS,sizeof(ota_info_t)); - ota_info.app_size = app_size; - ota_info.crc = calculate_crc32(DOWN_FLAH_ADDRESS, app_size); - ota_info.version = version; - strncpy(ota_info.description, description,sizeof(ota_info.description)); - ota_info.status = status; - strncpy(ota_info.error_message, error_message,sizeof(ota_info.error_message)); + status = flash_erase(FLAG_FLAH_ADDRESS,sizeof(ota_info_t)); + if(status != kStatus_Success) + { + return status; + } + status = flash_write(FLAG_FLAH_ADDRESS,(void *)ptr,sizeof(ota_info_t)); - flash_erase(FLAG_FLAH_ADDRESS,sizeof(ota_info_t)); - flash_write(FLAG_FLAH_ADDRESS,(void *)&ota_info,sizeof(ota_info_t)); + return status; } - /******************************************************************************* * : UpdateApplication * : bootloaderе,FlashFlagеϢǷа汾 @@ -179,61 +153,44 @@ void UpdateApplication(void) if(ota_info.status == OTA_STATUS_READY) { Serial_PutString("\r\n------Start to update the app!------\r\n"); - // У̼CRC - if(calculate_crc32(DOWN_FLAH_ADDRESS, ota_info.app_size) == ota_info.crc) + // Уdownlad̼CRC + if(calculate_crc32(DOWN_FLAH_ADDRESS, ota_info.down.size) == ota_info.down.crc32) { - // CRCУͨ,ʼ,ֽڰ - UpdateOTAStatus(OTA_STATUS_UPDATING); - status = flash_copy(XIUOS_FLAH_ADDRESS,BAKUP_FLAH_ADDRESS,ota_info.app_size); - if(status != kStatus_Success) + ota_info.status = OTA_STATUS_UPDATING; + UpdateOTAFlag(&ota_info); + + // downloadXiUOS System + status = flash_copy(DOWN_FLAH_ADDRESS, XIUOS_FLAH_ADDRESS, ota_info.down.size); + if((status == kStatus_Success) &&(calculate_crc32(XIUOS_FLAH_ADDRESS, ota_info.down.size) == ota_info.down.crc32)) { - Serial_PutString("------Backup app failed!------\r\n"); - goto finish; + Serial_PutString("\r\n------The download partition is copied successfully!------\r\n"); + + ota_info.os.size = ota_info.down.size; + ota_info.os.crc32 = ota_info.down.crc32; + ota_info.os.version = ota_info.down.version; + strncpy(ota_info.os.description, ota_info.down.description, sizeof(ota_info.down.description)); + ota_info.status == OTA_STATUS_IDLE; // downloadXiUOS Systemɹ,OTA״̬ΪIDLE + UpdateOTAFlag(&ota_info); } - status = flash_copy(DOWN_FLAH_ADDRESS,XIUOS_FLAH_ADDRESS,ota_info.app_size); - if(status != kStatus_Success) + else { - Serial_PutString("------Firmware partition copy failed!------\r\n"); + Serial_PutString("\r\n------The download partition copy failed!------\r\n"); + ota_info.status = OTA_STATUS_ERROR; + strncpy(ota_info.error_message, "The download partition copy failed!",sizeof(ota_info.error_message)); + UpdateOTAFlag(&ota_info); goto finish; } - // УƺĹ̼CRC - if(calculate_crc32(XIUOS_FLAH_ADDRESS, ota_info.app_size) == ota_info.crc) - { - ota_info.status = OTA_STATUS_IDLE; // OTA״̬ΪIDLE - memset(ota_info.error_message,0,sizeof(ota_info.error_message)); // մϢ - - // ºOTAϢдFlash - flash_erase(FLAG_FLAH_ADDRESS,sizeof(ota_info_t)); - flash_write(FLAG_FLAH_ADDRESS,(void *)&ota_info,sizeof(ota_info_t)); - Serial_PutString("\r\n------Update completed!------\r\n"); - goto finish; - } - else - { - // ƺĹ̼CRCУʧܣʧ - ota_info.status = OTA_STATUS_ERROR; // OTA״̬ΪERROR - memset(ota_info.error_message,0,sizeof(ota_info.error_message)); - snprintf((char *)ota_info.error_message, sizeof(ota_info.error_message), "APP Firmware CRC check failed!"); // ¼Ϣ - - // ºOTAϢдFlash - flash_erase(FLAG_FLAH_ADDRESS,sizeof(ota_info_t)); - flash_write(FLAG_FLAH_ADDRESS,(void *)&ota_info,sizeof(ota_info_t)); - Serial_PutString("\r\n------APP Firmware CRC check failed!------\r\n"); - goto finish; - } + Serial_PutString("\r\n------Update completed!------\r\n"); + goto finish; } else { - // CRCУʧܣʧ - ota_info.status = OTA_STATUS_ERROR; // OTA״̬ΪERROR - memset(ota_info.error_message,0,sizeof(ota_info.error_message)); - snprintf((char *)ota_info.error_message, sizeof(ota_info.error_message), "Download Firmware CRC check failed"); // ¼Ϣ - - // ºOTAϢдFlash - flash_erase(FLAG_FLAH_ADDRESS,sizeof(ota_info_t)); - flash_write(FLAG_FLAH_ADDRESS,(void *)&ota_info,sizeof(ota_info_t)); + // downloadCRCУʧܣʧ Serial_PutString("\r\n------Download Firmware CRC check failed!------\r\n"); + ota_info.status = OTA_STATUS_ERROR; + strncpy(ota_info.error_message, "Download Firmware CRC check failed!",sizeof(ota_info.error_message)); + UpdateOTAFlag(&ota_info); goto finish; } } diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/include/imxrt_ota.h b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/include/imxrt_ota.h index de01ab168..ddd4f41bf 100644 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/include/imxrt_ota.h +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/include/imxrt_ota.h @@ -26,27 +26,43 @@ #include "flash.h" #include "ymodem.h" + +/* OTA升级过程中的状态描述 */ typedef enum { - OTA_STATUS_IDLE = 0, // 空闲状态,没有进行OTA升级 - OTA_STATUS_READY, // 准备状态,可以进行OTA升级 - OTA_STATUS_DOWNLOADING, // 正在下载固件 - OTA_STATUS_DOWNLOADED, // 固件下载完成,可以进行升级 - OTA_STATUS_UPDATING, // 正在进行OTA升级 - OTA_STATUS_ERROR, // 出现错误,升级失败 + OTA_STATUS_IDLE = 0, // 空闲状态,没有进行OTA升级 + OTA_STATUS_READY, // 准备状态,可以进行OTA升级 + OTA_STATUS_DOWNLOADING, // 正在下载固件 + OTA_STATUS_DOWNLOADED, // 固件下载完成,可以进行升级 + OTA_STATUS_UPDATING, // 正在进行OTA升级 + OTA_STATUS_ERROR, // 出现错误,升级失败 } ota_status_t; + +/* Flash分区中保存固件的属性描述 */ typedef struct { - uint32_t app_size; // 应用程序大小 - uint32_t crc; // 应用程序CRC校验值 - uint32_t version; // 应用程序版本号 - uint32_t status; // 升级状态 - uint8_t description[64]; // 应用程序升级说明,最多64个字符 - uint8_t error_message[64]; // 错误信息,最多64个字符 + uint32_t size; // 应用程序大小,记录分区固件的大小 + uint32_t crc32; // 应用程序CRC32校验值,记录分区固件的crc32值 + uint32_t version; // 应用程序版本号,记录分区固件的版本号 + uint32_t reserve; // 保留字段 + uint8_t description[128]; // 固件的描述信息,最多128个字符 +} firmware_t; + + +/* OTA升级过程中的信息结构体 */ +typedef struct { + firmware_t os; // XiUOS System分区属性信息 + firmware_t bak; // Bakup分区属性信息 + firmware_t down; // Download分区属性信息 + uint32_t status; // 升级状态,取值来自于ota_status_t类型 + uint32_t initversion; // 恢复出厂设置的标志,0xFFFFFFFF代表未烧写过,烧写过以后设置为0x12345678 + uint32_t jumpfailed; // bootloaer跳转失败的标志,bootloader里置0xabababab,跳转成功后置0x00000000 + uint32_t reserve; // 保留字段 + uint8_t error_message[128]; // 错误信息,最多128个字符 } ota_info_t; -void UpdateOTAStatus(ota_status_t status); -void UpdateOTAFlag(uint32_t app_size, uint32_t version, uint32_t status, uint8_t* description, uint8_t* error_message); +uint32_t calculate_crc32(uint32_t addr, uint32_t len); +status_t UpdateOTAFlag(ota_info_t *ptr); void UpdateApplication(void); #endif