From 250546b4a0acf7e146a9dabbc30d7158979c9c78 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 1 Jul 2018 19:09:22 +0800 Subject: [PATCH] [AliOS][hal][ota] merge OTA patch from Realtek: chenwen 1. fix an issue for Gree project not to upgrade in slow speed flash 2. ease the ota partition through the HAL flash table in board.c --- board/mk3080/board.c | 2 +- platform/mcu/rtl8710bn/hal/ota_port.c | 99 +++++++++++++++++++++++++-- 2 files changed, 95 insertions(+), 6 deletions(-) diff --git a/board/mk3080/board.c b/board/mk3080/board.c index 0f25cb24a5..8a95fc9321 100644 --- a/board/mk3080/board.c +++ b/board/mk3080/board.c @@ -49,7 +49,7 @@ const hal_logic_partition_t hal_partitions[] = .partition_owner = HAL_FLASH_EMBEDDED, .partition_description = "OTA Storage", .partition_start_addr = 0x100000, - .partition_length = 0x8E000, //568k bytes + .partition_length = 0xF2000, //568k bytes .partition_options = PAR_OPT_READ_EN | PAR_OPT_WRITE_EN, }, [HAL_PARTITION_PARAMETER_3] = diff --git a/platform/mcu/rtl8710bn/hal/ota_port.c b/platform/mcu/rtl8710bn/hal/ota_port.c index a32b38e84c..daaca5e769 100644 --- a/platform/mcu/rtl8710bn/hal/ota_port.c +++ b/platform/mcu/rtl8710bn/hal/ota_port.c @@ -6,6 +6,7 @@ #include #include #include +#include static u32 alinknewImg2Addr = 0xFFFFFFFF; @@ -17,7 +18,10 @@ u8 alink_signature[9] = {0}; extern const update_file_img_id OtaImgId[2]; uint32_t alink_ota_target_index = OTA_INDEX_2; update_ota_target_hdr OtaTargetHdr; - +hal_logic_partition_t *partition_info = NULL; +#ifndef OTA2_DEFAULT_ADDR +#define OTA2_DEFAULT_ADDR (0x08100000) +#endif int check_ota_index() { @@ -33,9 +37,81 @@ int check_ota_index() return ota_index; } +bool prepare_ota_address(u32 ota_target_index, u32 * new_addr) +{ + IMAGE_HEADER *OTA1Hdr = NULL; + uint32_t OTA1Len = 0; + IMAGE_HEADER *FlashImgDataHdr = NULL; + uint32_t ota2_addr; + + ota2_addr = HAL_READ32(SPI_FLASH_BASE, OFFSET_DATA); + + printf("ota2_addr = %x\n", ota2_addr); + + /*if the OTA2 address is not programmed in system data zone, the default OTA2 + address is used. This operation is just used in the local OTA update demo. For the + cloud OTA upgrade based on this demo, this operation may not be used.*/ + if(ota2_addr == 0xffffffff) { + ota_write_ota2_addr(OTA2_DEFAULT_ADDR); + ota2_addr = HAL_READ32(SPI_FLASH_BASE, OFFSET_DATA); + } + + if((ota2_addr%4096) != 0) { + printf("\n\r[%s] ota addr in sys data space not 4k aligned 0x%x", __FUNCTION__, ota2_addr); + goto error; + } + + if(ota_target_index == OTA_INDEX_2) { + /* OAT2 address should not in OTA1 image & should 4K alignment */ + OTA1Hdr = (IMAGE_HEADER *)(OTA1_ADDR); + OTA1Len = OTA1Hdr->image_size; + FlashImgDataHdr = (IMAGE_HEADER *)((u32)OTA1Hdr + OTA1Len + IMAGE_HEADER_LEN); + if ((ota2_addr < ((u32)FlashImgDataHdr + FlashImgDataHdr->image_size + IMAGE_HEADER_LEN)) && ((ota2_addr & 0xfff) == 0)) { + printf("\n\r[%s] illegal ota addr 0x%x", __FUNCTION__, ota2_addr); + goto error; + } + *new_addr = ota2_addr; + } else { + *new_addr = OTA1_ADDR; + } + + if(*new_addr == 0xFFFFFFFF) { + printf("\n\r[%s] update address is invalid \n", __FUNCTION__); + goto error; + } + printf("OTA update address: 0x%x\n", *new_addr); + return TRUE; + +error: + return FALSE; +} + bool rtl8710bn_ota_prepare() { + uint32_t part = HAL_PARTITION_OTA_TEMP; alink_ota_target_index = check_ota_index(); + if(prepare_ota_address(alink_ota_target_index, &alinknewImg2Addr) != TRUE){ + printf("\n get OTA address failed\n"); + return FALSE; + } + + if(alink_ota_target_index == OTA_INDEX_1) { + part = HAL_PARTITION_APPLICATION; + }else if(alink_ota_target_index == OTA_INDEX_2) { + part = HAL_PARTITION_OTA_TEMP; + }else { + printf("flash INDEX failed\n"); + return FALSE; + } + + partition_info = hal_flash_get_info(part); + if(partition_info == NULL) { + printf("hal_flash_get_info failed\n"); + return FALSE; + } + + /*-------------------erase flash space for new firmware--------------*/ + erase_ota_target_flash(alinknewImg2Addr, partition_info->partition_length); DBG_INFO_MSG_OFF(_DBG_SPI_FLASH_); HeadBuffer = rtw_malloc(OTA_HEADER_BUF_SIZE); if (HeadBuffer == NULL) { @@ -109,17 +185,30 @@ static int rtl8710bn_ota_write_ota_cb(hal_ota_module_t *m, volatile uint32_t* of goto update_ota_exit; } - /*get new image addr and check new address validity*/ - if(!get_ota_address(alink_ota_target_index, &alinknewImg2Addr, &OtaTargetHdr)) { - printf("\n get OTA address failed\n"); - goto update_ota_exit; + if (alinknewImg2Addr == 0xFFFFFFFF) { + /*get new image addr and check new address validity*/ + if (!get_ota_address(alink_ota_target_index, &alinknewImg2Addr, &OtaTargetHdr)) { + printf("\n get OTA address failed\n"); + goto update_ota_exit; + } + } else { + /*Image size should not bigger than OTA_SIZE_MAX */ + if(partition_info != NULL) { + if(OtaTargetHdr.FileImgHdr.ImgLen > partition_info->partition_length){ + printf("\n\r[%s] illegal new image length 0x%x", __FUNCTION__, OtaTargetHdr.FileImgHdr.ImgLen); + goto update_ota_exit; + } + } } /*get new image length from the firmware header*/ uint32_t NewImg2Len = 0, NewImg2BlkSize = 0; NewImg2Len = OtaTargetHdr.FileImgHdr.ImgLen; NewImg2BlkSize = ((NewImg2Len - 1)/4096) + 1; +#if 0 + //It takes too long to erase flash ? /*-------------------erase flash space for new firmware--------------*/ erase_ota_target_flash(alinknewImg2Addr, NewImg2Len); +#endif /*the upgrade space should be masked, because the encrypt firmware is used for checksum calculation*/