diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/board.c b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/board.c index fe5b35a75..69f663516 100644 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/board.c +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/board.c @@ -385,10 +385,10 @@ static void OtaCmd(void) { ota_info.down.size = size; ota_info.down.crc32= calculate_crc32(DOWN_FLAH_ADDRESS, size); - ota_info.down.version = 0x11223344; + ota_info.down.version = ota_info.os.version + 1; 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)); + strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message)); UpdateOTAFlag(&ota_info); } else 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 db68e98ef..872b1e66c 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 @@ -149,8 +149,8 @@ void UpdateApplication(void) // 从Flash中读取OTA信息 memcpy(&ota_info, (const void *)FLAG_FLAH_ADDRESS,sizeof(ota_info_t)); - // 如果OTA升级状态为准备状态,可以进行升级 - if(ota_info.status == OTA_STATUS_READY) + // 如果OTA升级状态为准备状态,且APP分区与download分区版本不同,才可以进行升级 + if((ota_info.status == OTA_STATUS_READY) && (ota_info.os.crc32 != ota_info.down.crc32)) { Serial_PutString("\r\n------Start to update the app!------\r\n"); // 校验downlad分区固件CRC @@ -159,7 +159,27 @@ void UpdateApplication(void) ota_info.status = OTA_STATUS_UPDATING; UpdateOTAFlag(&ota_info); - // 拷贝download分区到XiUOS System分区 + // 1.如果CRC校验通过,开始升级,逐字节拷贝Flash,先备份当前XiUOS System分区内容 + status = flash_copy(XIUOS_FLAH_ADDRESS, BAKUP_FLAH_ADDRESS, ota_info.os.size); + if((status == kStatus_Success) &&(calculate_crc32(BAKUP_FLAH_ADDRESS, ota_info.os.size) == ota_info.os.crc32)) + { + Serial_PutString("\r\n------Backup app success!------\r\n"); + ota_info.bak.size = ota_info.os.size; + ota_info.bak.crc32 = ota_info.os.crc32; + ota_info.bak.version = ota_info.os.version; + strncpy(ota_info.bak.description, ota_info.os.description, sizeof(ota_info.os.description)); + UpdateOTAFlag(&ota_info); + } + else + { + Serial_PutString("\r\n------Backup app failed!------\r\n"); + ota_info.status = OTA_STATUS_ERROR; + strncpy(ota_info.error_message, "Backup app failed!",sizeof(ota_info.error_message)); + UpdateOTAFlag(&ota_info); + goto finish; + } + + // 2.拷贝download分区到XiUOS 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)) { @@ -200,6 +220,5 @@ void UpdateApplication(void) goto finish; } finish: - // 跳转到应用程序 - jump_to_application(); + return; } diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/mcuboot.c b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/mcuboot.c index c1113d6a0..7af9c9231 100644 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/mcuboot.c +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/mcuboot.c @@ -21,6 +21,25 @@ #ifdef MCUBOOT_BOOTLOADER extern void ImxrtMsDelay(uint32 ms); + +static void InitialVersion(void) +{ + int32_t size; + ota_info_t ota_info; + + memset(&ota_info, 0, sizeof(ota_info_t)); + size = SerialDownload(XIUOS_FLAH_ADDRESS); + if(size > 0) + { + ota_info.os.size = size; + ota_info.os.crc32 = calculate_crc32(XIUOS_FLAH_ADDRESS, size); + ota_info.os.version = 0x1; + strncpy(ota_info.os.description, "This is the initial firmware for the device!", sizeof(ota_info.os.description)); + UpdateOTAFlag(&ota_info); + } +} + + void jump_to_application(void) { SCB->VTOR = (uint32_t)XIUOS_FLAH_ADDRESS; @@ -34,10 +53,12 @@ void jump_to_application(void) asm volatile("BX R0"); } + void BootLoaderJumpApp(void) { uint8_t ch1, ch2; uint32_t ret; + ota_info_t ota_info; uint32_t timeout = 500; BOARD_ConfigMPU(); @@ -71,11 +92,24 @@ void BootLoaderJumpApp(void) case 0x31: jump_to_application(); break; + case 0x32: FLASH_Init(); - UpdateApplication(); + memcpy(&ota_info, (const void *)FLAG_FLAH_ADDRESS,sizeof(ota_info_t)); + /* 姝ゆ椂APP鍒嗗尯杩樻病鏈夋湁鏁堢殑鍥轰欢,闇瑕佸湪bootloader涓嬮氳繃iap鐑у啓鍑哄巶鍥轰欢 */ + if((ota_info.os.size > APP_FLASH_SIZE) || (calculate_crc32(XIUOS_FLAH_ADDRESS, ota_info.os.size) != ota_info.os.crc32)) + { + Serial_PutString("\r\nNeed to flash initial firmware!\r\n"); + InitialVersion(); + } + else + { + UpdateApplication(); + } FLASH_DeInit(); + jump_to_application(); break; + case 0x33: __set_FAULTMASK(1); NVIC_SystemReset(); 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 ddd4f41bf..099aca3f0 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,15 +26,14 @@ #include "flash.h" #include "ymodem.h" - /* OTA鍗囩骇杩囩▼涓殑鐘舵佹弿杩 */ typedef enum { - OTA_STATUS_IDLE = 0, // 绌洪棽鐘舵侊紝娌℃湁杩涜OTA鍗囩骇 - OTA_STATUS_READY, // 鍑嗗鐘舵侊紝鍙互杩涜OTA鍗囩骇 + OTA_STATUS_IDLE = 0, // 绌洪棽鐘舵,娌℃湁杩涜OTA鍗囩骇 + OTA_STATUS_READY, // 鍑嗗鐘舵,鍙互杩涜OTA鍗囩骇 OTA_STATUS_DOWNLOADING, // 姝e湪涓嬭浇鍥轰欢 - OTA_STATUS_DOWNLOADED, // 鍥轰欢涓嬭浇瀹屾垚锛屽彲浠ヨ繘琛屽崌绾 + OTA_STATUS_DOWNLOADED, // 鍥轰欢涓嬭浇瀹屾垚 OTA_STATUS_UPDATING, // 姝e湪杩涜OTA鍗囩骇 - OTA_STATUS_ERROR, // 鍑虹幇閿欒锛屽崌绾уけ璐 + OTA_STATUS_ERROR, // 鍑虹幇閿欒,鍗囩骇澶辫触 } ota_status_t; @@ -54,9 +53,8 @@ typedef struct { firmware_t bak; // Bakup鍒嗗尯灞炴т俊鎭 firmware_t down; // Download鍒嗗尯灞炴т俊鎭 uint32_t status; // 鍗囩骇鐘舵,鍙栧兼潵鑷簬ota_status_t绫诲瀷 - uint32_t initversion; // 鎭㈠鍑哄巶璁剧疆鐨勬爣蹇,0xFFFFFFFF浠h〃鏈儳鍐欒繃,鐑у啓杩囦互鍚庤缃负0x12345678 - uint32_t jumpfailed; // bootloaer璺宠浆澶辫触鐨勬爣蹇,bootloader閲岀疆0xabababab,璺宠浆鎴愬姛鍚庣疆0x00000000 - uint32_t reserve; // 淇濈暀瀛楁 + uint32_t lastjumpflag; // bootloaer璺宠浆澶辫触鐨勬爣蹇,bootloader閲岀疆0xABABABAB,璺宠浆鎴愬姛鍚庡湪搴旂敤閲岀疆0x00000000 + uint32_t reserve[2]; // 淇濈暀瀛楁 uint8_t error_message[128]; // 閿欒淇℃伅,鏈澶128涓瓧绗 } ota_info_t;