From 36218fd38e0f513d5aae8ba452d4d8baa835b34f Mon Sep 17 00:00:00 2001 From: openshwprojects <85486843+openshwprojects@users.noreply.github.com> Date: Sat, 16 Aug 2025 16:04:57 +0200 Subject: [PATCH] ota cosmetic (#1762) * step 1 * Update obk_main.mk * Update OpenBeken.mk * Update obk_main.cmake * Update hal_ota_bk7231.c * Update hal_ota_bk7231.c * m * split * guards * Update hal_ota_bl602.c * Update hal_ota_bl602.c * bk * better * header * w * TMP * tttttttt * Update rest_interface.c * hal_ota is xradio header.... * undoi * header * move * xradio * read * fx * t * makefile * move out xradi o read * fx * move out esp * move out bl602 * move out beken, ln * move out w8 * ecr tr * realtek * Update rest_interface.c * win * forgot * tr6260 split * Update hal_ota_tr6260.c * rename ota_progress * use OTA_GetProgress on all paltforms * fx * not needed header * call OTA_IncrementProgress on OTA so we at laest know that something is happening --- openBeken_win32_mvsc2017.vcxproj | 3 +- openBeken_win32_mvsc2017.vcxproj.filters | 6 + platforms/BK723x/OpenBeken.mk | 1 + platforms/ECR6600/OpenBeken.mk | 1 + platforms/ESP-IDF/main/CMakeLists.txt | 1 + platforms/ESP8266/main/CMakeLists.txt | 1 + platforms/LN882H/CMakeLists.txt | 1 + platforms/RTL8710A/OpenBeken.mk | 1 + platforms/RTL8710B/OpenBeken.mk | 1 + platforms/RTL8720D/OpenBeken.mk | 1 + platforms/RTL8720E/CMakeLists.txt | 1 + platforms/RTL8721DA/CMakeLists.txt | 1 + platforms/RTL87X0C/OpenBeken.mk | 1 + platforms/TR6260/openbeken.mk | 1 + platforms/W600/Makefile | 1 + platforms/W800/Makefile | 1 + platforms/XR809/Makefile | 1 + platforms/obk_main.cmake | 1 - platforms/obk_main.mk | 1 - src/driver/drv_bl_shared.c | 14 +- src/driver/drv_doorSensorWithDeepSleep.c | 8 +- src/driver/drv_ntp.c | 10 +- src/driver/drv_ntp_events.c | 2 +- .../ota.c => hal/bk7231/hal_ota_bk7231.c} | 106 +- src/hal/bl602/hal_ota_bl602.c | 318 +++ src/hal/ecr6600/hal_ota_ecr6600.c | 97 + src/hal/espidf/hal_ota_espidf.c | 146 ++ src/{ota/ota.h => hal/hal_ota.h} | 17 +- src/hal/ln882h/hal_ota_ln882h.c | 315 +++ src/hal/realtek/hal_ota_realtek.c | 1048 ++++++++ src/hal/tr6260/hal_ota_tr6260.c | 94 + src/hal/w800/hal_ota_w800.c | 302 +++ src/hal/win32/hal_ota_win32.c | 22 + src/hal/xradio/hal_ota_xradio.c | 146 ++ src/httpserver/http_fns.c | 10 +- src/httpserver/json_interface.c | 2 +- src/httpserver/new_http.c | 2 +- src/httpserver/new_http.h | 2 + src/httpserver/rest_interface.c | 2215 +---------------- src/mqtt/new_mqtt.c | 10 +- src/user_main.c | 40 +- src/win_main.c | 6 - 42 files changed, 2647 insertions(+), 2311 deletions(-) rename src/{ota/ota.c => hal/bk7231/hal_ota_bk7231.c} (80%) create mode 100644 src/hal/bl602/hal_ota_bl602.c create mode 100644 src/hal/ecr6600/hal_ota_ecr6600.c create mode 100644 src/hal/espidf/hal_ota_espidf.c rename src/{ota/ota.h => hal/hal_ota.h} (71%) create mode 100644 src/hal/ln882h/hal_ota_ln882h.c create mode 100644 src/hal/realtek/hal_ota_realtek.c create mode 100644 src/hal/tr6260/hal_ota_tr6260.c create mode 100644 src/hal/w800/hal_ota_w800.c create mode 100644 src/hal/win32/hal_ota_win32.c create mode 100644 src/hal/xradio/hal_ota_xradio.c diff --git a/openBeken_win32_mvsc2017.vcxproj b/openBeken_win32_mvsc2017.vcxproj index 921d86b57..7648b8a5c 100644 --- a/openBeken_win32_mvsc2017.vcxproj +++ b/openBeken_win32_mvsc2017.vcxproj @@ -360,6 +360,7 @@ + @@ -1300,4 +1301,4 @@ - + \ No newline at end of file diff --git a/openBeken_win32_mvsc2017.vcxproj.filters b/openBeken_win32_mvsc2017.vcxproj.filters index 5fcf16424..aefd8e02f 100644 --- a/openBeken_win32_mvsc2017.vcxproj.filters +++ b/openBeken_win32_mvsc2017.vcxproj.filters @@ -402,6 +402,11 @@ + + + + + @@ -610,6 +615,7 @@ + diff --git a/platforms/BK723x/OpenBeken.mk b/platforms/BK723x/OpenBeken.mk index 69e335cde..e6b532cee 100644 --- a/platforms/BK723x/OpenBeken.mk +++ b/platforms/BK723x/OpenBeken.mk @@ -34,6 +34,7 @@ APP_C += $(OBK_DIR)/hal/bk7231/hal_main_bk7231.c APP_C += $(OBK_DIR)/hal/bk7231/hal_pins_bk7231.c APP_C += $(OBK_DIR)/hal/bk7231/hal_wifi_bk7231.c APP_C += $(OBK_DIR)/hal/bk7231/hal_uart_bk7231.c +APP_C += $(OBK_DIR)/hal/bk7231/hal_ota_bk7231.c OBK_SRCS = $(OBK_DIR)/ include $(OBK_DIR)/../platforms/obk_main.mk diff --git a/platforms/ECR6600/OpenBeken.mk b/platforms/ECR6600/OpenBeken.mk index 2391c4a8c..aa82aa628 100644 --- a/platforms/ECR6600/OpenBeken.mk +++ b/platforms/ECR6600/OpenBeken.mk @@ -15,6 +15,7 @@ CSRCS += hal/ecr6600/hal_main_ecr6600.c CSRCS += hal/ecr6600/hal_pins_ecr6600.c CSRCS += hal/ecr6600/hal_wifi_ecr6600.c CSRCS += hal/ecr6600/hal_uart_ecr6600.c +CSRCS += hal/ecr6600/hal_ota_ecr6600.c OBK_SRCS = include $(TOPDIR)/../../platforms/obk_main.mk diff --git a/platforms/ESP-IDF/main/CMakeLists.txt b/platforms/ESP-IDF/main/CMakeLists.txt index 1e2992691..7cefd22e9 100644 --- a/platforms/ESP-IDF/main/CMakeLists.txt +++ b/platforms/ESP-IDF/main/CMakeLists.txt @@ -14,6 +14,7 @@ set(PROJ_ALL_SRC ${OBK_SRCS}hal/espidf/hal_pins_espidf.c ${OBK_SRCS}hal/espidf/hal_wifi_espidf.c ${OBK_SRCS}hal/espidf/hal_uart_espidf.c + ${OBK_SRCS}hal/espidf/hal_ota_espidf.c ${OBKM_SRC} ${BERRY_SRC_C} ../../../libraries/mqtt_patched.c diff --git a/platforms/ESP8266/main/CMakeLists.txt b/platforms/ESP8266/main/CMakeLists.txt index 56669e2c8..fb9d8ad37 100644 --- a/platforms/ESP8266/main/CMakeLists.txt +++ b/platforms/ESP8266/main/CMakeLists.txt @@ -13,6 +13,7 @@ set(PROJ_ALL_SRC ${OBK_SRCS}hal/espidf/hal_pins_espidf.c ${OBK_SRCS}hal/espidf/hal_wifi_espidf.c ${OBK_SRCS}hal/espidf/hal_uart_espidf.c + ${OBK_SRCS}hal/espidf/hal_ota_espidf.c ${OBKM_SRC} ${BERRY_SRC_C} ../../../libraries/mqtt_patched.c diff --git a/platforms/LN882H/CMakeLists.txt b/platforms/LN882H/CMakeLists.txt index 38c27081c..13626d31b 100644 --- a/platforms/LN882H/CMakeLists.txt +++ b/platforms/LN882H/CMakeLists.txt @@ -23,6 +23,7 @@ set(PROJ_ALL_SRC ${OBK_SRCS}hal/ln882h/hal_main_ln882h.c ${OBK_SRCS}hal/ln882h/hal_pins_ln882h.c ${OBK_SRCS}hal/ln882h/hal_wifi_ln882h.c + ${OBK_SRCS}hal/ln882h/hal_ota_ln882h.c main.c usr_app.c bsp/serial_hw.c diff --git a/platforms/RTL8710A/OpenBeken.mk b/platforms/RTL8710A/OpenBeken.mk index 77d04793f..9bb572a5b 100644 --- a/platforms/RTL8710A/OpenBeken.mk +++ b/platforms/RTL8710A/OpenBeken.mk @@ -16,6 +16,7 @@ SRC_C += $(OBK_DIR)/src/hal/realtek/hal_flashVars_realtek.c SRC_C += $(OBK_DIR)/src/hal/realtek/hal_generic_realtek.c SRC_C += $(OBK_DIR)/src/hal/realtek/hal_pins_realtek.c SRC_C += $(OBK_DIR)/src/hal/realtek/hal_wifi_realtek.c +SRC_C += $(OBK_DIR)/src/hal/realtek/hal_ota_realtek.c OBK_SRCS = $(OBK_DIR)/src/ include $(OBK_DIR)/platforms/obk_main.mk diff --git a/platforms/RTL8710B/OpenBeken.mk b/platforms/RTL8710B/OpenBeken.mk index a154c2ded..9186cbf40 100644 --- a/platforms/RTL8710B/OpenBeken.mk +++ b/platforms/RTL8710B/OpenBeken.mk @@ -20,6 +20,7 @@ SRC_C += $(OBK_DIR)/src/hal/realtek/hal_flashVars_realtek.c SRC_C += $(OBK_DIR)/src/hal/realtek/hal_generic_realtek.c SRC_C += $(OBK_DIR)/src/hal/realtek/hal_pins_realtek.c SRC_C += $(OBK_DIR)/src/hal/realtek/hal_wifi_realtek.c +SRC_C += $(OBK_DIR)/src/hal/realtek/hal_ota_realtek.c OBK_SRCS = $(OBK_DIR)/src/ include $(OBK_DIR)/platforms/obk_main.mk diff --git a/platforms/RTL8720D/OpenBeken.mk b/platforms/RTL8720D/OpenBeken.mk index e972b2e72..b4962a487 100644 --- a/platforms/RTL8720D/OpenBeken.mk +++ b/platforms/RTL8720D/OpenBeken.mk @@ -13,6 +13,7 @@ CSRC += hal/realtek/hal_flashVars_realtek.c CSRC += hal/realtek/hal_generic_realtek.c CSRC += hal/realtek/hal_pins_realtek.c CSRC += hal/realtek/hal_wifi_realtek.c +CSRC += hal/realtek/hal_ota_realtek.c OBK_SRCS = include $(EFDIR)/../platforms/obk_main.mk diff --git a/platforms/RTL8720E/CMakeLists.txt b/platforms/RTL8720E/CMakeLists.txt index 7752e71a9..cdfd83610 100644 --- a/platforms/RTL8720E/CMakeLists.txt +++ b/platforms/RTL8720E/CMakeLists.txt @@ -51,6 +51,7 @@ target_sources( ${OBK_SRCS}hal/realtek/hal_flashVars_realtek.c ${OBK_SRCS}hal/realtek/hal_generic_realtek.c ${OBK_SRCS}hal/realtek/hal_pins_realtek.c + ${OBK_SRCS}hal/realtek/hal_ota_realtek.c ${OBK_SRCS}hal/realtek/hal_wifi_realtek_new.c ${OBK_SRCS}hal/realtek/rtl8721da/hal_main_rtl8721da.c ${OBK_SRCS}hal/realtek/rtl8720e/hal_pins_rtl8720e.c diff --git a/platforms/RTL8721DA/CMakeLists.txt b/platforms/RTL8721DA/CMakeLists.txt index f05e4ae27..932f585e1 100644 --- a/platforms/RTL8721DA/CMakeLists.txt +++ b/platforms/RTL8721DA/CMakeLists.txt @@ -51,6 +51,7 @@ target_sources( ${OBK_SRCS}hal/realtek/hal_flashVars_realtek.c ${OBK_SRCS}hal/realtek/hal_generic_realtek.c ${OBK_SRCS}hal/realtek/hal_pins_realtek.c + ${OBK_SRCS}hal/realtek/hal_ota_realtek.c ${OBK_SRCS}hal/realtek/hal_wifi_realtek_new.c ${OBK_SRCS}hal/realtek/rtl8721da/hal_main_rtl8721da.c ${OBK_SRCS}hal/realtek/rtl8721da/hal_pins_rtl8721da.c diff --git a/platforms/RTL87X0C/OpenBeken.mk b/platforms/RTL87X0C/OpenBeken.mk index 6a791847f..695675f69 100644 --- a/platforms/RTL87X0C/OpenBeken.mk +++ b/platforms/RTL87X0C/OpenBeken.mk @@ -15,6 +15,7 @@ SRC_C += $(OBK_DIR)/src/hal/realtek/hal_flashVars_realtek.c SRC_C += $(OBK_DIR)/src/hal/realtek/hal_generic_realtek.c SRC_C += $(OBK_DIR)/src/hal/realtek/hal_pins_realtek.c SRC_C += $(OBK_DIR)/src/hal/realtek/hal_wifi_realtek.c +SRC_C += $(OBK_DIR)/src/hal/realtek/hal_ota_realtek.c OBK_SRCS = $(OBK_DIR)/src/ include $(OBK_DIR)/platforms/obk_main.mk diff --git a/platforms/TR6260/openbeken.mk b/platforms/TR6260/openbeken.mk index 5653daa93..699d11936 100644 --- a/platforms/TR6260/openbeken.mk +++ b/platforms/TR6260/openbeken.mk @@ -11,6 +11,7 @@ CSRCS += hal/tr6260/hal_generic_tr6260.c CSRCS += hal/tr6260/hal_main_tr6260.c CSRCS += hal/tr6260/hal_pins_tr6260.c CSRCS += hal/tr6260/hal_wifi_tr6260.c +CSRCS += hal/tr6260/hal_ota_tr6260.c OBK_SRCS = include $(OBK_PATH)/../platforms/obk_main.mk diff --git a/platforms/W600/Makefile b/platforms/W600/Makefile index 4380640c3..cf79246d8 100644 --- a/platforms/W600/Makefile +++ b/platforms/W600/Makefile @@ -28,6 +28,7 @@ CSRCS += $(_SHARED_APP)/hal/w800/hal_generic_w800.c CSRCS += $(_SHARED_APP)/hal/w800/hal_main_w800.c CSRCS += $(_SHARED_APP)/hal/w800/hal_pins_w800.c CSRCS += $(_SHARED_APP)/hal/w800/hal_wifi_w800.c +CSRCS += $(_SHARED_APP)/hal/w800/hal_ota_w800.c CSRCS += main.c OBK_SRCS = $(_SHARED_APP)/ diff --git a/platforms/W800/Makefile b/platforms/W800/Makefile index 8d754b02b..9ee55e907 100644 --- a/platforms/W800/Makefile +++ b/platforms/W800/Makefile @@ -22,6 +22,7 @@ CSRCS += $(_SHARED_APP)/hal/w800/hal_main_w800.c CSRCS += $(_SHARED_APP)/hal/w800/hal_pins_w800.c CSRCS += $(_SHARED_APP)/hal/w800/hal_wifi_w800.c CSRCS += $(_SHARED_APP)/hal/w800/hal_uart_w800.c +CSRCS += $(_SHARED_APP)/hal/w800/hal_ota_w800.c OBK_SRCS = $(_SHARED_APP)/ include $(_SHARED_APP)/../platforms/obk_main.mk diff --git a/platforms/XR809/Makefile b/platforms/XR809/Makefile index 1753904d7..90b9e313e 100644 --- a/platforms/XR809/Makefile +++ b/platforms/XR809/Makefile @@ -9,6 +9,7 @@ SRCS += $(OBK_SRCS)hal/xradio/hal_pins_xradio SRCS += $(OBK_SRCS)hal/xradio/hal_flashConfig_xradio SRCS += $(OBK_SRCS)hal/xradio/hal_flashVars_xradio SRCS += $(OBK_SRCS)hal/xradio/hal_uart_xradio +SRCS += $(OBK_SRCS)hal/xradio/hal_ota_xradio SRCS += $(OBK_SRCS)hal/xradio/xr809/hal_pins_xr809 SRCS += $(OBK_SRCS)hal/xradio/xr806/hal_pins_xr806 SRCS += $(OBK_SRCS)hal/xradio/xr872/hal_pins_xr872 diff --git a/platforms/obk_main.cmake b/platforms/obk_main.cmake index b5b61c33e..62747aa43 100644 --- a/platforms/obk_main.cmake +++ b/platforms/obk_main.cmake @@ -52,7 +52,6 @@ set(OBKM_SRC ${OBK_SRCS}httpclient/utils_net.c ${OBK_SRCS}httpclient/utils_timer.c ${OBK_SRCS}littlefs/our_lfs.c - ${OBK_SRCS}ota/ota.c ${OBK_SRCS}driver/drv_main.c diff --git a/platforms/obk_main.mk b/platforms/obk_main.mk index 72d9d6ba3..979cee230 100644 --- a/platforms/obk_main.mk +++ b/platforms/obk_main.mk @@ -69,7 +69,6 @@ OBKM_SRC += $(OBK_SRCS)httpclient/utils_timer.c OBKM_SRC += $(OBK_SRCS)littlefs/lfs_util.c OBKM_SRC += $(OBK_SRCS)littlefs/lfs.c OBKM_SRC += $(OBK_SRCS)littlefs/our_lfs.c -OBKM_SRC += $(OBK_SRCS)ota/ota.c OBKM_SRC += $(OBK_SRCS)driver/drv_main.c diff --git a/src/driver/drv_bl_shared.c b/src/driver/drv_bl_shared.c index aabc2d405..3b13ab116 100644 --- a/src/driver/drv_bl_shared.c +++ b/src/driver/drv_bl_shared.c @@ -9,7 +9,7 @@ #include "../hal/hal_flashVars.h" #include "../logging/logging.h" #include "../mqtt/new_mqtt.h" -#include "../ota/ota.h" +#include "../hal/hal_ota.h" #include "drv_local.h" #include "drv_ntp.h" #include "drv_public.h" @@ -330,9 +330,7 @@ commandResult_t BL09XX_ResetEnergyCounterEx(int asensdatasetix, float* pvalue) energyCounterStamp[asensdatasetix] = xTaskGetTickCount(); } ConsumptionResetTime = (time_t)NTP_GetCurrentTime(); -#if PLATFORM_BK7231N || PLATFORM_BK7231T - if (ota_progress()==-1) -#endif + if (OTA_GetProgress()==-1) { BL09XX_SaveEmeteringStatistics(); lastConsumptionSaveStamp = xTaskGetTickCount(); @@ -721,9 +719,7 @@ void BL_ProcessUpdate(float voltage, float current, float power, //MQTT_PublishMain_StringFloat(sensdataset->sensors[OBK_CONSUMPTION_YESTERDAY].names.name_mqtt, BL_ChangeEnergyUnitIfNeeded(sensors[OBK_CONSUMPTION_YESTERDAY].lastReading ), // sensdataset->sensors[OBK_CONSUMPTION_YESTERDAY].rounding_decimals, 0); //stat_updatesSent++; -#if PLATFORM_BK7231N || PLATFORM_BK7231T - if (ota_progress()==-1) -#endif + if (OTA_GetProgress()==-1) { BL09XX_SaveEmeteringStatistics(); lastConsumptionSaveStamp = xTaskGetTickCount(); @@ -917,9 +913,7 @@ void BL_ProcessUpdate(float voltage, float current, float power, if (((sensdataset->sensors[OBK_CONSUMPTION_TOTAL].lastReading - lastSavedEnergyCounterValue[asensdatasetix]) >= changeSavedThresholdEnergy) || ((xTaskGetTickCount() - lastConsumptionSaveStamp) >= (6 * 3600 * 1000 / portTICK_PERIOD_MS))) { -#if PLATFORM_BK7231N || PLATFORM_BK7231T - if (ota_progress() == -1) -#endif + if (OTA_GetProgress() == -1) { lastSavedEnergyCounterValue[asensdatasetix] = (float)sensdataset->sensors[OBK_CONSUMPTION_TOTAL].lastReading; BL09XX_SaveEmeteringStatistics(); diff --git a/src/driver/drv_doorSensorWithDeepSleep.c b/src/driver/drv_doorSensorWithDeepSleep.c index 2afa42b76..9ae04a16e 100644 --- a/src/driver/drv_doorSensorWithDeepSleep.c +++ b/src/driver/drv_doorSensorWithDeepSleep.c @@ -16,7 +16,7 @@ #include "../httpserver/new_http.h" #include "../hal/hal_pins.h" #include "../hal/hal_adc.h" -#include "../ota/ota.h" +#include "../hal/hal_ota.h" static int g_noChangeTimePassed = 0; // time without change. Every event in any of the doorsensor channels resets it. static int g_emergencyTimeWithNoConnection = 0; // time without connection to MQTT. Extends the interval till Deep Sleep until connection is established or EMERGENCY_TIME_TO_SLEEP_WITHOUT_MQTT @@ -110,11 +110,7 @@ void DoorDeepSleep_QueueNewEvents() { void DoorDeepSleep_OnEverySecond() { -#if PLATFORM_BK7231N || PLATFORM_BK7231T - if (ota_progress() >= 0) { -#else - if (false) { -#endif + if (OTA_GetProgress() >= 0) { // update active g_noChangeTimePassed = 0; g_emergencyTimeWithNoConnection = 0; diff --git a/src/driver/drv_ntp.c b/src/driver/drv_ntp.c index 8ce50982c..97c2e29f9 100644 --- a/src/driver/drv_ntp.c +++ b/src/driver/drv_ntp.c @@ -10,7 +10,7 @@ #include "../cmnds/cmd_public.h" #include "../httpserver/new_http.h" #include "../logging/logging.h" -#include "../ota/ota.h" +#include "../hal/hal_ota.h" #include "drv_ntp.h" @@ -718,15 +718,11 @@ void NTP_OnEverySecond() if (b_ntp_simulatedTime) { return; } -#elif PLATFORM_BL602 -#elif PLATFORM_W600 || PLATFORM_W800 -#elif PLATFORM_XR809 -#elif PLATFORM_BK7231N || PLATFORM_BK7231T - if (ota_progress() != -1) +#endif + if (OTA_GetProgress() != -1) { return; } -#endif if(g_ntp_socket == 0) { // if no socket, this is a reconnect delay if(g_ntp_delay > 0) { diff --git a/src/driver/drv_ntp_events.c b/src/driver/drv_ntp_events.c index 7eaa27e2f..f9c047b21 100644 --- a/src/driver/drv_ntp_events.c +++ b/src/driver/drv_ntp_events.c @@ -8,7 +8,7 @@ #include "../cmnds/cmd_public.h" #include "../httpserver/new_http.h" #include "../logging/logging.h" -#include "../ota/ota.h" +#include "../hal/hal_ota.h" #include "drv_ntp.h" diff --git a/src/ota/ota.c b/src/hal/bk7231/hal_ota_bk7231.c similarity index 80% rename from src/ota/ota.c rename to src/hal/bk7231/hal_ota_bk7231.c index d1bc45bf3..87678e43d 100644 --- a/src/ota/ota.c +++ b/src/hal/bk7231/hal_ota_bk7231.c @@ -1,20 +1,15 @@ -#include "ota.h" +#include "../hal_ota.h" -#if defined(PLATFORM_W600) - -//W600 uses OTA functions from its SDK. - -#elif PLATFORM_BEKEN - -#include "../new_common.h" -#include "../new_cfg.h" +#include "../../new_common.h" +#include "../../new_cfg.h" #include "typedef.h" #include "flash_pub.h" //#include "flash.h" -#include "../logging/logging.h" -#include "../httpclient/http_client.h" -#include "../driver/drv_public.h" -#include "../driver/drv_bl_shared.h" +#include "../../logging/logging.h" +#include "../../httpclient/http_client.h" +#include "../../httpserver/new_http.h" +#include "../../driver/drv_public.h" +#include "../../driver/drv_bl_shared.h" static unsigned char *sector = (void *)0; int sectorlen = 0; @@ -31,6 +26,11 @@ extern UINT32 flash_read(char *user_buf, UINT32 count, UINT32 address); extern UINT32 flash_write(char *user_buf, UINT32 count, UINT32 address); extern UINT32 flash_ctrl(UINT32 cmd, void *parm); +int HAL_FlashRead(char*buffer, int readlen, int startaddr) { + int res; + res = flash_read((char*)buffer, readlen, startaddr); + return res; +} int init_ota(unsigned int startaddr){ flash_init(); @@ -112,7 +112,6 @@ static void store_sector(unsigned int addr, unsigned char *data){ OTA_IncrementProgress(SECTOR_SIZE); } -#if ENABLE_SEND_POSTANDGET httprequest_t httprequest; @@ -221,42 +220,53 @@ void otarequest(const char *urlin){ OTA_ResetProgress(); OTA_IncrementProgress(1); } -#else -void otarequest(const char *urlin) { - addLogAdv(LOG_INFO, LOG_FEATURE_OTA, ""); -} -#endif - -#endif - - -/***** SDK independent code from this point. ******/ -int ota_status = -1; -int total_bytes = 0; - -int ota_progress() +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) { - return ota_status; + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + + init_ota(startaddr); + + if (request->contentLength >= 0) + { + towrite = request->contentLength; + } + + if (writelen < 0 || (startaddr + writelen > maxaddr)) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "ABORTED: %d bytes to write", writelen); + return http_rest_error(request, -20, "writelen < 0 or end > 0x200000"); + } + + do + { + //ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d bytes to write", writelen); + add_otadata((unsigned char*)writebuf, writelen); + total += writelen; + startaddr += writelen; + towrite -= writelen; + if (towrite > 0) + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + if (writelen < 0) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); + } + } + } while ((towrite > 0) && (writelen >= 0)); + close_ota(); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; } -extern void OTA_ResetProgress() -{ - ota_status = -1; -} - -extern void OTA_IncrementProgress(int value) -{ - ota_status += value; -} - -int OTA_GetTotalBytes() -{ - return total_bytes; -} - -extern void OTA_SetTotalBytes(int value) -{ - total_bytes = value; -} diff --git a/src/hal/bl602/hal_ota_bl602.c b/src/hal/bl602/hal_ota_bl602.c new file mode 100644 index 000000000..c0c4ef803 --- /dev/null +++ b/src/hal/bl602/hal_ota_bl602.c @@ -0,0 +1,318 @@ +#ifdef PLATFORM_BL602 + +#include +#include +#include +#include +#include "../../obk_config.h" +#include "../../new_common.h" +#include "../../new_cfg.h" +#include "../../httpserver/new_http.h" +#include "../../logging/logging.h" +#include "lwip/sockets.h" + + +typedef struct ota_header { + union { + struct { + uint8_t header[16]; + + uint8_t type[4];//RAW XZ + uint32_t len;//body len + uint8_t pad0[8]; + + uint8_t ver_hardware[16]; + uint8_t ver_software[16]; + + uint8_t sha256[32]; + uint32_t unpacked_len;//full len + } s; + uint8_t _pad[512]; + } u; +} ota_header_t; +#define OTA_HEADER_SIZE (sizeof(ota_header_t)) + +static int _check_ota_header(ota_header_t *ota_header, uint32_t *ota_len, int *use_xz) +{ + char str[33];//assume max segment size + int i; + + memcpy(str, ota_header->u.s.header, sizeof(ota_header->u.s.header)); + str[sizeof(ota_header->u.s.header)] = '\0'; + puts("[OTA] [HEADER] ota header is "); + puts(str); + puts("\r\n"); + + memcpy(str, ota_header->u.s.type, sizeof(ota_header->u.s.type)); + str[sizeof(ota_header->u.s.type)] = '\0'; + puts("[OTA] [HEADER] file type is "); + puts(str); + puts("\r\n"); + if (strstr(str, "XZ")) { + *use_xz = 1; + } + else { + *use_xz = 0; + } + + memcpy(ota_len, &(ota_header->u.s.len), 4); + printf("[OTA] [HEADER] file length (exclude ota header) is %lu\r\n", *ota_len); + + memcpy(str, ota_header->u.s.ver_hardware, sizeof(ota_header->u.s.ver_hardware)); + str[sizeof(ota_header->u.s.ver_hardware)] = '\0'; + puts("[OTA] [HEADER] ver_hardware is "); + puts(str); + puts("\r\n"); + + memcpy(str, ota_header->u.s.ver_software, sizeof(ota_header->u.s.ver_software)); + str[sizeof(ota_header->u.s.ver_software)] = '\0'; + puts("[OTA] [HEADER] ver_software is "); + puts(str); + puts("\r\n"); + + memcpy(str, ota_header->u.s.sha256, sizeof(ota_header->u.s.sha256)); + str[sizeof(ota_header->u.s.sha256)] = '\0'; + puts("[OTA] [HEADER] sha256 is "); + for (i = 0; i < sizeof(ota_header->u.s.sha256); i++) { + printf("%02X", str[i]); + } + puts("\r\n"); + + return 0; +} + +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + + int sockfd, i; + int ret; + struct hostent* hostinfo; + uint8_t* recv_buffer; + struct sockaddr_in dest; + iot_sha256_context ctx; + uint8_t sha256_result[32]; + uint8_t sha256_img[32]; + bl_mtd_handle_t handle; + //init_ota(startaddr); + + +#define OTA_PROGRAM_SIZE (512) + int ota_header_found, use_xz; + ota_header_t* ota_header = 0; + + ret = bl_mtd_open(BL_MTD_PARTITION_NAME_FW_DEFAULT, &handle, BL_MTD_OPEN_FLAG_BACKUP); + if (ret) + { + return http_rest_error(request, -20, "Open Default FW partition failed"); + } + + recv_buffer = pvPortMalloc(OTA_PROGRAM_SIZE); + + unsigned int buffer_offset, flash_offset, ota_addr; + uint32_t bin_size, part_size, running_size; + uint8_t activeID; + HALPartition_Entry_Config ptEntry; + + activeID = hal_boot2_get_active_partition(); + + printf("Starting OTA test. OTA bin addr is %p, incoming len %i\r\n", recv_buffer, writelen); + + printf("[OTA] [TEST] activeID is %u\r\n", activeID); + + if (hal_boot2_get_active_entries(BOOT2_PARTITION_TYPE_FW, &ptEntry)) + { + printf("PtTable_Get_Active_Entries fail\r\n"); + vPortFree(recv_buffer); + bl_mtd_close(handle); + return http_rest_error(request, -20, "PtTable_Get_Active_Entries fail"); + } + ota_addr = ptEntry.Address[!ptEntry.activeIndex]; + bin_size = ptEntry.maxLen[!ptEntry.activeIndex]; + part_size = ptEntry.maxLen[!ptEntry.activeIndex]; + running_size = ptEntry.maxLen[ptEntry.activeIndex]; + (void)part_size; + /*XXX if you use bin_size is product env, you may want to set bin_size to the actual + * OTA BIN size, and also you need to splilt XIP_SFlash_Erase_With_Lock into + * serveral pieces. Partition size vs bin_size check is also needed + */ + printf("Starting OTA test. OTA size is %lu\r\n", bin_size); + + printf("[OTA] [TEST] activeIndex is %u, use OTA address=%08x\r\n", ptEntry.activeIndex, (unsigned int)ota_addr); + + printf("[OTA] [TEST] Erase flash with size %lu...", bin_size); + hal_update_mfg_ptable(); + + //Erase in chunks, because erasing everything at once is slow and causes issues with http connection + uint32_t erase_offset = 0; + uint32_t erase_len = 0; + while (erase_offset < bin_size) + { + erase_len = bin_size - erase_offset; + if (erase_len > 0x10000) + { + erase_len = 0x10000; //Erase in 64kb chunks + } + bl_mtd_erase(handle, erase_offset, erase_len); + printf("[OTA] Erased: %lu / %lu \r\n", erase_offset, erase_len); + erase_offset += erase_len; + rtos_delay_milliseconds(100); + } + printf("[OTA] Done\r\n"); + + if (request->contentLength >= 0) + { + towrite = request->contentLength; + } + + // get header + // recv_buffer + //buffer_offset = 0; + //do { + // int take_len; + + // take_len = OTA_PROGRAM_SIZE - buffer_offset; + + // memcpy(recv_buffer + buffer_offset, writebuf, writelen); + // buffer_offset += writelen; + + + // if (towrite > 0) { + // writebuf = request->received; + // writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + // if (writelen < 0) { + // ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); + // } + // } + //} while(true) + + buffer_offset = 0; + flash_offset = 0; + ota_header = 0; + use_xz = 0; + + utils_sha256_init(&ctx); + utils_sha256_starts(&ctx); + memset(sha256_result, 0, sizeof(sha256_result)); + do + { + char* useBuf = writebuf; + int useLen = writelen; + + if (ota_header == 0) + { + int take_len; + + // how much left for header? + take_len = OTA_PROGRAM_SIZE - buffer_offset; + // clamp to available len + if (take_len > useLen) + take_len = useLen; + printf("Header takes %i. ", take_len); + memcpy(recv_buffer + buffer_offset, writebuf, take_len); + buffer_offset += take_len; + useBuf = writebuf + take_len; + useLen = writelen - take_len; + + if (buffer_offset >= OTA_PROGRAM_SIZE) + { + ota_header = (ota_header_t*)recv_buffer; + if (strncmp((const char*)ota_header, "BL60X_OTA", 9)) + { + return http_rest_error(request, -20, "Invalid header ident"); + } + } + } + + + if (ota_header && useLen) + { + + + if (flash_offset + useLen >= part_size) + { + return http_rest_error(request, -20, "Too large bin"); + } + if (ota_header->u.s.unpacked_len != 0xFFFFFFFF && running_size < ota_header->u.s.unpacked_len) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Unpacked OTA image size (%u) is bigger than running partition size (%u)", ota_header->u.s.unpacked_len, running_size); + return http_rest_error(request, -20, ""); + } + //ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d bytes to write", writelen); + //add_otadata((unsigned char*)writebuf, writelen); + + printf("Flash takes %i. ", useLen); + utils_sha256_update(&ctx, (byte*)useBuf, useLen); + bl_mtd_write(handle, flash_offset, useLen, (byte*)useBuf); + flash_offset += useLen; + } + + total += writelen; + startaddr += writelen; + towrite -= writelen; + + + if (towrite > 0) + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + if (writelen < 0) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); + } + } + } while ((towrite > 0) && (writelen >= 0)); + + if (ota_header == 0) + { + return http_rest_error(request, -20, "No header found"); + } + utils_sha256_finish(&ctx, sha256_result); + puts("\r\nCalculated SHA256 Checksum:"); + for (i = 0; i < sizeof(sha256_result); i++) + { + printf("%02X", sha256_result[i]); + } + puts("\r\nHeader SHA256 Checksum:"); + for (i = 0; i < sizeof(sha256_result); i++) + { + printf("%02X", ota_header->u.s.sha256[i]); + } + if (memcmp(ota_header->u.s.sha256, sha256_result, sizeof(sha256_img))) + { + /*Error found*/ + return http_rest_error(request, -20, "SHA256 NOT Correct"); + } + printf("[OTA] [TCP] prepare OTA partition info\r\n"); + ptEntry.len = total; + printf("[OTA] [TCP] Update PARTITION, partition len is %lu\r\n", ptEntry.len); + hal_boot2_update_ptable(&ptEntry); + printf("[OTA] [TCP] Rebooting\r\n"); + //close_ota(); + vPortFree(recv_buffer); + utils_sha256_free(&ctx); + bl_mtd_close(handle); + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; +} + +int HAL_FlashRead(char*buffer, int readlen, int startaddr) { + int res; + res = bl_flash_read(startaddr, (uint8_t *)buffer, readlen); + return res; +} + + +#endif // PLATFORM_BL602 + + diff --git a/src/hal/ecr6600/hal_ota_ecr6600.c b/src/hal/ecr6600/hal_ota_ecr6600.c new file mode 100644 index 000000000..56e70e1a4 --- /dev/null +++ b/src/hal/ecr6600/hal_ota_ecr6600.c @@ -0,0 +1,97 @@ +#if PLATFORM_ECR6600 + +#include "../../obk_config.h" +#include "../../new_common.h" +#include "../../new_cfg.h" +#include "../../logging/logging.h" +#include "../../httpserver/new_http.h" +#include "../hal_ota.h" + +#include "flash.h" + +extern int ota_init(void); +extern int ota_write(unsigned char* data, unsigned int len); +extern int ota_done(bool reset); + +int HAL_FlashRead(char*buffer, int readlen, int startaddr) { + int res; + res = drv_spiflash_read(startaddr, (uint8_t*)buffer, readlen); + return res; +} + +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + + + int ret = 0; + + if (request->contentLength > 0) + { + towrite = request->contentLength; + } + else + { + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); + goto update_ota_exit; + } + + if (ota_init() != 0) + { + ret = -1; + goto update_ota_exit; + } + + do + { + if (ota_write((unsigned char*)writebuf, writelen) != 0) + { + ret = -1; + goto update_ota_exit; + } + delay_ms(10); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); + total += writelen; + startaddr += writelen; + towrite -= writelen; + if (towrite > 0) + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + if (writelen < 0) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); + ret = -1; + } + } + } while ((towrite > 0) && (writelen >= 0)); + +update_ota_exit: + if (ret != -1) + { + ADDLOG_INFO(LOG_FEATURE_OTA, "OTA is successful"); + ota_done(0); + } + else + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed. Reboot to retry"); + return http_rest_error(request, ret, "error"); + } + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; +} + +#endif + + diff --git a/src/hal/espidf/hal_ota_espidf.c b/src/hal/espidf/hal_ota_espidf.c new file mode 100644 index 000000000..09661ffeb --- /dev/null +++ b/src/hal/espidf/hal_ota_espidf.c @@ -0,0 +1,146 @@ +#if PLATFORM_ESPIDF || PLATFORM_ESP8266 + +#include "../../obk_config.h" +#include "../../new_common.h" +#include "../../new_cfg.h" +#include "../../httpserver/new_http.h" +#include "../../logging/logging.h" + +#include "esp_system.h" +#include "esp_ota_ops.h" +#include "esp_app_format.h" +#include "esp_flash_partitions.h" +#include "esp_partition.h" +#include "nvs.h" +#include "nvs_flash.h" +#include "esp_wifi.h" +#if PLATFORM_ESPIDF +#include "esp_flash.h" +#include "esp_pm.h" +#else +#include "esp_image_format.h" +#include "spi_flash.h" +#define esp_flash_read(a,b,c,d) spi_flash_read(c,b,d) +#define OTA_WITH_SEQUENTIAL_WRITES OTA_SIZE_UNKNOWN +#define esp_ota_abort esp_ota_end +#endif + + +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Ota start!\r\n"); + esp_err_t err; + esp_ota_handle_t update_handle = 0; + const esp_partition_t* update_partition = NULL; + const esp_partition_t* running = esp_ota_get_running_partition(); + update_partition = esp_ota_get_next_update_partition(NULL); + if (request->contentLength >= 0) + { + towrite = request->contentLength; + } + + esp_wifi_set_ps(WIFI_PS_NONE); + bool image_header_was_checked = false; + do + { + if (image_header_was_checked == false) + { + esp_app_desc_t new_app_info; + if (towrite > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) + { + memcpy(&new_app_info, &writebuf[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t)); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "New firmware version: %s", new_app_info.version); + + esp_app_desc_t running_app_info; + if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Running firmware version: %s", running_app_info.version); + } + + image_header_was_checked = true; + + err = esp_ota_begin(update_partition, OTA_WITH_SEQUENTIAL_WRITES, &update_handle); + if (err != ESP_OK) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "esp_ota_begin failed (%s)", esp_err_to_name(err)); + esp_ota_abort(update_handle); + return -1; + } + ADDLOG_DEBUG(LOG_FEATURE_OTA, "esp_ota_begin succeeded"); + } + else + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "received package is not fit len"); + esp_ota_abort(update_handle); + return -1; + } + } + err = esp_ota_write(update_handle, (const void*)writebuf, writelen); + if (err != ESP_OK) + { + esp_ota_abort(update_handle); + return -1; + } + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); + total += writelen; + startaddr += writelen; + towrite -= writelen; + + if (towrite > 0) + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + if (writelen < 0) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); + } + } + } while ((towrite > 0) && (writelen >= 0)); + + ADDLOG_INFO(LOG_FEATURE_OTA, "OTA in progress: 100%%, total Write binary data length: %d", total); + + err = esp_ota_end(update_handle); + if (err != ESP_OK) + { + if (err == ESP_ERR_OTA_VALIDATE_FAILED) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Image validation failed, image is corrupted"); + } + else + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "esp_ota_end failed (%s)!", esp_err_to_name(err)); + } + return -1; + } + err = esp_ota_set_boot_partition(update_partition); + if (err != ESP_OK) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err)); + return -1; + } + + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; +} + +int HAL_FlashRead(char*buffer, int readlen, int startaddr) { + int res; + res = esp_flash_read(NULL, (void*)buffer, startaddr, readlen); + return res; +} + +#endif + diff --git a/src/ota/ota.h b/src/hal/hal_ota.h similarity index 71% rename from src/ota/ota.h rename to src/hal/hal_ota.h index c0ca6f04c..a2f9fb794 100644 --- a/src/ota/ota.h +++ b/src/hal/hal_ota.h @@ -13,19 +13,6 @@ #define START_ADR_OF_BK_PARTITION_OTA 0x132000 #endif -/// @brief Initialise OTA flash starting at startaddr. Only used for Beken SDK. -/// @param startaddr -/// @return -int init_ota(unsigned int startaddr); - -/// @brief Add any length of data to OTA. Only used for Beken SDK. -/// @param data -/// @param len -void add_otadata(unsigned char *data, int len); - -/// @brief Finalise OTA flash (write last sector if incomplete). Only used for Beken SDK. -void close_ota(); - /// @brief Handle OTA request. Only used for Beken SDK. /// @param urlin void otarequest(const char *urlin); @@ -35,7 +22,7 @@ void otarequest(const char *urlin); /// @brief Indicates current OTA progress status. A non -ve value indicates active OTA. /// @return -int ota_progress(); +int OTA_GetProgress(); /// @brief Reset OTA progress status to -1. This can be called from other SDKs. /// @param value @@ -53,5 +40,7 @@ int OTA_GetTotalBytes(); /// @param value void OTA_SetTotalBytes(int value); +int HAL_FlashRead(char*buffer, int readlen, int startaddr); + #endif /* __OTA_H__ */ diff --git a/src/hal/ln882h/hal_ota_ln882h.c b/src/hal/ln882h/hal_ota_ln882h.c new file mode 100644 index 000000000..3d4e391d5 --- /dev/null +++ b/src/hal/ln882h/hal_ota_ln882h.c @@ -0,0 +1,315 @@ +#ifdef PLATFORM_LN882H + +#include "hal/hal_flash.h" +#include "flash_partition_table.h" +#include "../../obk_config.h" +#include "../../new_common.h" +#include "../../new_cfg.h" +#include "../../logging/logging.h" +#include "../../httpserver/new_http.h" +#include "../hal_ota.h" + +#include "ota_port.h" +#include "ota_image.h" +#include "ota_types.h" +#include "hal/hal_flash.h" +#include "netif/ethernetif.h" +#include "flash_partition_table.h" + + +#define KV_OTA_UPG_STATE ("kv_ota_upg_state") +#define HTTP_OTA_DEMO_STACK_SIZE (1024 * 16) + +#define SECTOR_SIZE_4KB (1024 * 4) + +static char g_http_uri_buff[512] = "http://192.168.122.48:9090/ota-images/otaimage-v1.3.bin"; + +// a block to save http data. +static char *temp4K_buf = NULL; +static int temp4k_offset = 0; + +// where to save OTA data in flash. +static int32_t flash_ota_start_addr = OTA_SPACE_OFFSET; +static int32_t flash_ota_offset = 0; +static uint8_t is_persistent_started = LN_FALSE; +static uint8_t is_ready_to_verify = LN_FALSE; +static uint8_t is_precheck_ok = LN_FALSE; +static uint8_t httpc_ota_started = 0; + +/** + * @brief Pre-check the image file to be downloaded. + * + * @attention None + * + * @param[in] app_offset The offset of the APP partition in Flash. + * @param[in] ota_hdr pointer to ota partition info struct. + * + * @return whether the check is successful. + * @retval #LN_TRUE successful. + * @retval #LN_FALSE failed. + */ +static int ota_download_precheck(uint32_t app_offset, image_hdr_t * ota_hdr) +{ + + image_hdr_t *app_hdr = NULL; + if (NULL == (app_hdr = OS_Malloc(sizeof(image_hdr_t)))) { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "[%s:%d] malloc failed.\r\n", __func__, __LINE__); + return LN_FALSE; + } + + if (OTA_ERR_NONE != image_header_fast_read(app_offset, app_hdr)) { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "failed to read app header.\r\n"); + goto ret_err; + } + + if ((ota_hdr->image_type == IMAGE_TYPE_ORIGINAL) || \ + (ota_hdr->image_type == IMAGE_TYPE_ORIGINAL_XZ)) + { + // check version + if (((ota_hdr->ver.ver_major << 8) + ota_hdr->ver.ver_minor) == \ + ((app_hdr->ver.ver_major << 8) + app_hdr->ver.ver_minor)) { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "[%s:%d] same version, do not upgrade!\r\n", + __func__, __LINE__); + } + + // check file size + if (((ota_hdr->img_size_orig + sizeof(image_hdr_t)) > APP_SPACE_SIZE) || \ + ((ota_hdr->img_size_orig_xz + sizeof(image_hdr_t)) > OTA_SPACE_SIZE)) { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "[%s:%d] size check failed.\r\n", __func__, __LINE__); + goto ret_err; + } + } + else { + //image type not support! + goto ret_err; + } + + OS_Free(app_hdr); + return LN_TRUE; + +ret_err: + OS_Free(app_hdr); + return LN_FALSE; +} + +static int ota_persistent_start(void) +{ + if (NULL == temp4K_buf) { + temp4K_buf = OS_Malloc(SECTOR_SIZE_4KB); + if (NULL == temp4K_buf) { + LOG(LOG_LVL_INFO, "failed to alloc 4KB!!!\r\n"); + return LN_FALSE; + } + memset(temp4K_buf, 0, SECTOR_SIZE_4KB); + } + + temp4k_offset = 0; + flash_ota_start_addr = OTA_SPACE_OFFSET; + flash_ota_offset = 0; + is_persistent_started = LN_TRUE; + return LN_TRUE; +} + +/** + * @brief Save block to flash. + * + * @param buf + * @param buf_len + * @return return LN_TRUE on success, LN_FALSE on failure. + */ +static int ota_persistent_write(const char *buf, const int32_t buf_len) +{ + int part_len = SECTOR_SIZE_4KB; // we might have a buffer so large, that we need to write multiple 4K segments ... + int buf_offset = 0; // ... and we need to keep track, what is already written + + if (!is_persistent_started) { + return LN_TRUE; + } + + if (temp4k_offset + buf_len < SECTOR_SIZE_4KB) { + // just copy all buf data to temp4K_buf + memcpy(temp4K_buf + temp4k_offset, buf, buf_len); + temp4k_offset += buf_len; + part_len = 0; + } + while (part_len >= SECTOR_SIZE_4KB) { // so we didn't copy all data to buffer (part_len would be 0 then) + // just copy part of buf to temp4K_buf + part_len = temp4k_offset + buf_len - buf_offset - SECTOR_SIZE_4KB; // beware, this can be > SECTOR_SIZE_4KB !!! + memcpy(temp4K_buf + temp4k_offset, buf + buf_offset, buf_len - buf_offset - part_len); + temp4k_offset += buf_len - buf_offset - part_len; + buf_offset = buf_len - part_len; + + if (temp4k_offset >= SECTOR_SIZE_4KB) { + // write to flash + ADDLOG_DEBUG(LOG_FEATURE_OTA, "write at flash: 0x%08x (temp4k_offset=%i)\r\n", flash_ota_start_addr + flash_ota_offset, temp4k_offset); + + if (flash_ota_offset == 0) { + if (LN_TRUE != ota_download_precheck(APP_SPACE_OFFSET, (image_hdr_t *)temp4K_buf)) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "ota download precheck failed!\r\n"); + is_precheck_ok = LN_FALSE; + return LN_FALSE; + } + is_precheck_ok = LN_TRUE; + } + + hal_flash_erase(flash_ota_start_addr + flash_ota_offset, SECTOR_SIZE_4KB); + hal_flash_program(flash_ota_start_addr + flash_ota_offset, SECTOR_SIZE_4KB, (uint8_t *)temp4K_buf); + + flash_ota_offset += SECTOR_SIZE_4KB; + memset(temp4K_buf, 0, SECTOR_SIZE_4KB); + temp4k_offset = 0; + } + } + if (part_len > 0) { + memcpy(temp4K_buf + temp4k_offset, buf + (buf_len - part_len), part_len); + temp4k_offset += part_len; + } + + return LN_TRUE; +} + +/** + * @brief save last block and clear flags. + * @return return LN_TRUE on success, LN_FALSE on failure. + */ +static int ota_persistent_finish(void) +{ + if (!is_persistent_started) { + return LN_FALSE; + } + + // write to flash + ADDLOG_DEBUG(LOG_FEATURE_OTA, "write at flash: 0x%08x\r\n", flash_ota_start_addr + flash_ota_offset); + hal_flash_erase(flash_ota_start_addr + flash_ota_offset, SECTOR_SIZE_4KB); + hal_flash_program(flash_ota_start_addr + flash_ota_offset, SECTOR_SIZE_4KB, (uint8_t *)temp4K_buf); + + OS_Free(temp4K_buf); + temp4K_buf = NULL; + temp4k_offset = 0; + + flash_ota_offset = 0; + is_persistent_started = LN_FALSE; + return LN_TRUE; +} + +static int update_ota_state(void) +{ + upg_state_t state = UPG_STATE_DOWNLOAD_OK; + ln_nvds_set_ota_upg_state(state); + return LN_TRUE; +} +/** + * @brief check ota image header, body. + * @return return LN_TRUE on success, LN_FALSE on failure. + */ +static int ota_verify_download(void) +{ + image_hdr_t ota_header; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Succeed to verify OTA image content.\r\n"); + if (OTA_ERR_NONE != image_header_fast_read(OTA_SPACE_OFFSET, &ota_header)) { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "failed to read ota header.\r\n"); + return LN_FALSE; + } + + if (OTA_ERR_NONE != image_header_verify(&ota_header)) { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "failed to verify ota header.\r\n"); + return LN_FALSE; + } + + if (OTA_ERR_NONE != image_body_verify(OTA_SPACE_OFFSET, &ota_header)) { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "failed to verify ota body.\r\n"); + return LN_FALSE; + } + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Succeed to verify OTA image content.\r\n"); + return LN_TRUE; +} + +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Ota start!\r\n"); + if (LN_TRUE != ota_persistent_start()) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Ota start error, exit...\r\n"); + return 0; + } + + if (request->contentLength >= 0) + { + towrite = request->contentLength; + } + + do + { + //ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d bytes to write", writelen); + + if (LN_TRUE != ota_persistent_write(writebuf, writelen)) + { + // ADDLOG_DEBUG(LOG_FEATURE_OTA, "ota write err.\r\n"); + return -1; + } + + rtos_delay_milliseconds(10); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); + total += writelen; + startaddr += writelen; + towrite -= writelen; + if (towrite > 0) + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + if (writelen < 0) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); + } + } + } while ((towrite > 0) && (writelen >= 0)); + + ota_persistent_finish(); + is_ready_to_verify = LN_TRUE; + ADDLOG_DEBUG(LOG_FEATURE_OTA, "cb info: recv %d finished, no more data to deal with.\r\n", towrite); + + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "http client job done, exit...\r\n"); + if (LN_TRUE == is_precheck_ok) + { + if ((LN_TRUE == is_ready_to_verify) && (LN_TRUE == ota_verify_download())) + { + update_ota_state(); + //ln_chip_reboot(); + } + else + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Veri bad\r\n"); + } + } + else + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Precheck bad\r\n"); + } + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; +} + +int HAL_FlashRead(char*buffer, int readlen, int startaddr) { + int res; + res = hal_flash_read(startaddr, readlen, (uint8_t *)buffer); + return res; +} + +#endif // PLATFORM_LN882H + diff --git a/src/hal/realtek/hal_ota_realtek.c b/src/hal/realtek/hal_ota_realtek.c new file mode 100644 index 000000000..696291add --- /dev/null +++ b/src/hal/realtek/hal_ota_realtek.c @@ -0,0 +1,1048 @@ +#ifdef PLATFORM_REALTEK + +#include "../../obk_config.h" +#include "../../new_common.h" +#include "../../new_cfg.h" +#include "../../logging/logging.h" +#include "../../httpserver/new_http.h" +#include "../hal_ota.h" + + +#include "flash_api.h" +#include "device_lock.h" +#include "sys_api.h" + +extern flash_t flash; + +#if PLATFORM_RTL87X0C + +#include "ota_8710c.h" + +extern uint32_t sys_update_ota_get_curr_fw_idx(void); +extern uint32_t sys_update_ota_prepare_addr(void); +extern void sys_disable_fast_boot(void); +extern void get_fw_info(uint32_t* targetFWaddr, uint32_t* currentFWaddr, uint32_t* fw1_sn, uint32_t* fw2_sn); +static flash_t flash_ota; + +#elif PLATFORM_RTL8710B + +#include "rtl8710b_ota.h" + +extern uint32_t current_fw_idx; + +#elif PLATFORM_RTL8710A + +extern uint32_t current_fw_idx; + +#undef DEFAULT_FLASH_LEN +#define DEFAULT_FLASH_LEN 0x400000 + +#elif PLATFORM_RTL8720D + +#include "rtl8721d_boot.h" +#include "rtl8721d_ota.h" +#include "diag.h" +#include "wdt_api.h" + +extern uint32_t current_fw_idx; +extern uint8_t flash_size_8720; + +#undef DEFAULT_FLASH_LEN +#define DEFAULT_FLASH_LEN (flash_size_8720 << 20) + +#elif PLATFORM_REALTEK_NEW + +#include "ameba_ota.h" +extern uint32_t current_fw_idx; +extern uint32_t IMG_ADDR[OTA_IMGID_MAX][2]; +extern uint8_t flash_size_8720; +#undef DEFAULT_FLASH_LEN +#define DEFAULT_FLASH_LEN (flash_size_8720 << 20) + +#endif + +int HAL_FlashRead(char*buffer, int readlen, int startaddr) { + int res; + device_mutex_lock(RT_DEV_LOCK_FLASH); + flash_stream_read(&flash, startaddr, readlen, (uint8_t*)buffer); + device_mutex_unlock(RT_DEV_LOCK_FLASH); + return res; +} + +#if PLATFORM_RTL87X0C +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + + + uint32_t NewFWLen = 0, NewFWAddr = 0; + uint32_t address = 0; + uint32_t curr_fw_idx = 0; + uint32_t flash_checksum = 0; + uint32_t targetFWaddr; + uint32_t currentFWaddr; + uint32_t fw1_sn; + uint32_t fw2_sn; + _file_checksum file_checksum; + file_checksum.u = 0; + unsigned char sig_backup[32]; + int ret = 1; + + if (request->contentLength >= 0) + { + towrite = request->contentLength; + } + NewFWAddr = sys_update_ota_prepare_addr(); + if (NewFWAddr == -1) + { + ret = -1; + goto update_ota_exit; + } + get_fw_info(&targetFWaddr, ¤tFWaddr, &fw1_sn, &fw2_sn); + ADDLOG_INFO(LOG_FEATURE_OTA, "Current FW addr: 0x%08X, target FW addr: 0x%08X, fw1 sn: %u, fw2 sn: %u", currentFWaddr, targetFWaddr, fw1_sn, fw2_sn); + curr_fw_idx = sys_update_ota_get_curr_fw_idx(); + ADDLOG_INFO(LOG_FEATURE_OTA, "Current firmware index is %d", curr_fw_idx); + int reserase = update_ota_erase_upg_region(towrite, 0, NewFWAddr); + NewFWLen = towrite; + if (reserase == -1) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Erase failed"); + ret = -1; + goto update_ota_exit; + } + if (NewFWAddr != ~0x0) + { + address = NewFWAddr; + ADDLOG_INFO(LOG_FEATURE_OTA, "Start to read data %i bytes", NewFWLen); + } + do + { + // back up signature and only write it to flash till the end of OTA + if (startaddr < 32) + { + memcpy(sig_backup + startaddr, writebuf, (startaddr + writelen > 32 ? (32 - startaddr) : writelen)); + memset(writebuf, 0xFF, (startaddr + writelen > 32 ? (32 - startaddr) : writelen)); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "sig_backup for% d bytes from index% d", (startaddr + writelen > 32 ? (32 - startaddr) : writelen), startaddr); + } + + device_mutex_lock(RT_DEV_LOCK_FLASH); + if (flash_burst_write(&flash_ota, address + startaddr, writelen, (uint8_t*)writebuf) < 0) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Write stream failed"); + device_mutex_unlock(RT_DEV_LOCK_FLASH); + ret = -1; + goto update_ota_exit; + } + device_mutex_unlock(RT_DEV_LOCK_FLASH); + + rtos_delay_milliseconds(10); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); + total += writelen; + startaddr += writelen; + towrite -= writelen; + + // checksum attached at file end + if (startaddr + writelen > NewFWLen - 4) + { + file_checksum.c[0] = writebuf[writelen - 4]; + file_checksum.c[1] = writebuf[writelen - 3]; + file_checksum.c[2] = writebuf[writelen - 2]; + file_checksum.c[3] = writebuf[writelen - 1]; + } + if (towrite > 0) + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + if (writelen < 0) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); + } + } + } while ((towrite > 0) && (writelen >= 0)); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written, verifying checksum", total); + uint8_t* buf = (uint8_t*)os_malloc(2048); + memset(buf, 0, 2048); + // read flash data back and calculate checksum + for (int i = 0; i < NewFWLen; i += 2048) + { + int k; + int rlen = (startaddr - 4 - i) > 2048 ? 2048 : (startaddr - 4 - i); + device_mutex_lock(RT_DEV_LOCK_FLASH); + flash_stream_read(&flash_ota, NewFWAddr + i, rlen, buf); + device_mutex_unlock(RT_DEV_LOCK_FLASH); + for (k = 0; k < rlen; k++) + { + if (i + k < 32) + { + flash_checksum += sig_backup[i + k]; + } + else + { + flash_checksum += buf[k]; + } + } + } + + ADDLOG_INFO(LOG_FEATURE_OTA, "flash checksum 0x%8x attached checksum 0x%8x", flash_checksum, file_checksum.u); + + if (file_checksum.u != flash_checksum) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "The checksum is wrong!"); + ret = -1; + goto update_ota_exit; + } + ret = update_ota_signature(sig_backup, NewFWAddr); + if (ret == -1) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Update signature fail"); + goto update_ota_exit; + } +update_ota_exit: + if (ret != -1) + { + device_mutex_lock(RT_DEV_LOCK_FLASH); + flash_write_word(&flash, targetFWaddr, 4294967295); + flash_write_word(&flash, currentFWaddr, 0); + device_mutex_unlock(RT_DEV_LOCK_FLASH); + + sys_disable_fast_boot(); + if (buf) free(buf); + } + else + { + if (buf) free(buf); + return http_rest_error(request, ret, "error"); + } + + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; +} + +#elif PLATFORM_RTL8710B +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + + int NewImg2BlkSize = 0; + uint32_t NewFWLen = 0, NewFWAddr = 0; + uint32_t address = 0; + uint32_t flash_checksum = 0; + uint32_t ota2_addr = OTA2_ADDR; + union { uint32_t u; unsigned char c[4]; } file_checksum; + unsigned char sig_backup[32]; + int ret = 1; + char* hbuf = NULL; + bool foundhdr = false; + + if (request->contentLength > 0) + { + towrite = request->contentLength; + } + else + { + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); + goto update_ota_exit; + } + int ota1_len = 0, ota2_len = 0; + char* msg = "Incorrect amount of bytes received: %i, required: %i"; + + writebuf = request->received; + writelen = recv(request->fd, &ota1_len, sizeof(ota1_len), 0); + if (writelen != sizeof(ota1_len)) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, msg, writelen, sizeof(ota1_len)); + ret = -1; + goto update_ota_exit; + } + + writebuf = request->received; + writelen = recv(request->fd, &ota2_len, sizeof(ota2_len), 0); + if (writelen != sizeof(ota2_len)) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, msg, writelen, sizeof(ota2_len)); + ret = -1; + goto update_ota_exit; + } + ADDLOG_INFO(LOG_FEATURE_OTA, "OTA1 len: %u, OTA2 len: %u", ota1_len, ota2_len); + + if (ota1_len <= 0 || ota2_len <= 0) + { + ret = -1; + goto update_ota_exit; + } + towrite -= 8; + + if (current_fw_idx == OTA_INDEX_1) + { + towrite = ota2_len; + // skip ota1 + int toskip = ota1_len; + do + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax < toskip ? request->receivedLenmax : toskip, 0); + ADDLOG_EXTRADEBUG(LOG_FEATURE_OTA, "Skipping %i at %i", writelen, total); + total += writelen; + toskip -= writelen; + } while ((toskip > 0) && (writelen >= 0)); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Skipped %i bytes, towrite: %i", total, towrite); + } + else + { + towrite = ota1_len; + } + + writelen = 0; + total = 0; + NewFWAddr = update_ota_prepare_addr(); + ADDLOG_INFO(LOG_FEATURE_OTA, "OTA address: %#010x, len: %u", NewFWAddr - SPI_FLASH_BASE, towrite); + if (NewFWAddr == -1 || NewFWAddr == 0xFFFFFFFF) + { + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "Wrong OTA address:", NewFWAddr); + goto update_ota_exit; + } + NewFWLen = towrite; + if (NewFWAddr == OTA1_ADDR) + { + if (NewFWLen > (OTA2_ADDR - OTA1_ADDR)) + { + // firmware size too large + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "image size should not cross OTA2"); + goto update_ota_exit; + } + } + else if (NewFWAddr == OTA2_ADDR) + { + if (NewFWLen > (0x195000 + SPI_FLASH_BASE - OTA2_ADDR)) + { + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "image size crosses OTA2 boundary"); + goto update_ota_exit; + } + } + else if (NewFWAddr == 0xFFFFFFFF) + { + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "update address is invalid"); + goto update_ota_exit; + } + address = NewFWAddr - SPI_FLASH_BASE; + NewImg2BlkSize = ((NewFWLen - 1) / 4096) + 1; + device_mutex_lock(RT_DEV_LOCK_FLASH); + for (int i = 0; i < NewImg2BlkSize; i++) + { + flash_erase_sector(&flash, address + i * 4096); + } + device_mutex_unlock(RT_DEV_LOCK_FLASH); + OTF_Mask(1, (address), NewImg2BlkSize, 1); + + do + { + if (startaddr < 32) + { + memcpy(sig_backup + startaddr, writebuf, (startaddr + writelen > 32 ? (32 - startaddr) : writelen)); + memset(writebuf, 0xFF, (startaddr + writelen > 32 ? (32 - startaddr) : writelen)); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "sig_backup for% d bytes from index% d", (startaddr + writelen > 32 ? (32 - startaddr) : writelen), startaddr); + } + device_mutex_lock(RT_DEV_LOCK_FLASH); + if (flash_burst_write(&flash, address + startaddr, writelen, (uint8_t*)writebuf) < 0) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Write stream failed"); + device_mutex_unlock(RT_DEV_LOCK_FLASH); + ret = -1; + goto update_ota_exit; + } + device_mutex_unlock(RT_DEV_LOCK_FLASH); + + rtos_delay_milliseconds(10); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); + total += writelen; + startaddr += writelen; + towrite -= writelen; + + if (startaddr + writelen > NewFWLen - 4) + { + file_checksum.c[0] = writebuf[writelen - 4]; + file_checksum.c[1] = writebuf[writelen - 3]; + file_checksum.c[2] = writebuf[writelen - 2]; + file_checksum.c[3] = writebuf[writelen - 1]; + } + + if (towrite > 0) + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax < towrite ? request->receivedLenmax : towrite, 0); + if (writelen < 0) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); + } + } + } while ((towrite > 0) && (writelen >= 0)); + + uint8_t* buf = (uint8_t*)os_malloc(2048); + memset(buf, 0, 2048); + for (int i = 0; i < NewFWLen; i += 2048) + { + int k; + int rlen = (startaddr - 4 - i) > 2048 ? 2048 : (startaddr - 4 - i); + device_mutex_lock(RT_DEV_LOCK_FLASH); + flash_stream_read(&flash, address + i, rlen, buf); + device_mutex_unlock(RT_DEV_LOCK_FLASH); + for (k = 0; k < rlen; k++) + { + if (i + k < 32) + { + flash_checksum += sig_backup[i + k]; + } + else + { + flash_checksum += buf[k]; + } + } + } + ADDLOG_INFO(LOG_FEATURE_OTA, "Update file size = %d flash checksum 0x%8x attached checksum 0x%8x", NewFWLen, flash_checksum, file_checksum.u); + OTF_Mask(1, (address), NewImg2BlkSize, 0); + if (file_checksum.u == flash_checksum) + { + ADDLOG_INFO(LOG_FEATURE_OTA, "Update OTA success!"); + + ret = 0; + } + else + { + /*if checksum error, clear the signature zone which has been + written in flash in case of boot from the wrong firmware*/ + device_mutex_lock(RT_DEV_LOCK_FLASH); + flash_erase_sector(&flash, address); + device_mutex_unlock(RT_DEV_LOCK_FLASH); + ret = -1; + } + // make it to be similar to rtl8720c - write signature only after success - to prevent boot failure if something goes wrong + if (flash_burst_write(&flash, address + 16, 16, sig_backup + 16) < 0) + { + ret = -1; + } + else + { + if (flash_burst_write(&flash, address, 16, sig_backup) < 0) + { + ret = -1; + } + } + if (current_fw_idx != OTA_INDEX_1) + { + // receive file fully + int toskip = ota2_len; + do + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax < toskip ? request->receivedLenmax : toskip, 0); + total += writelen; + toskip -= writelen; + } while ((toskip > 0) && (writelen >= 0)); + } +update_ota_exit: + if (ret != -1) + { + device_mutex_lock(RT_DEV_LOCK_FLASH); + if (current_fw_idx == OTA_INDEX_1) + { + OTA_Change(OTA_INDEX_2); + //ota_write_ota2_addr(OTA2_ADDR); + } + else + { + OTA_Change(OTA_INDEX_1); + //ota_write_ota2_addr(0xffffffff); + } + device_mutex_unlock(RT_DEV_LOCK_FLASH); + if (buf) free(buf); + } + else + { + if (buf) free(buf); + ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed"); + return http_rest_error(request, ret, "error"); + } + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; +} + +#elif PLATFORM_RTL8710A + +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + + uint32_t NewFWLen = 0, NewFWAddr = 0; + uint32_t address = 0; + uint32_t flash_checksum = 0; + union { uint32_t u; unsigned char c[4]; } file_checksum; + int ret = 0; + + if (request->contentLength > 0) + { + towrite = request->contentLength; + } + else + { + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); + goto update_ota_exit; + } + + NewFWAddr = update_ota_prepare_addr(); + if (NewFWAddr == -1) + { + goto update_ota_exit; + } + + int reserase = update_ota_erase_upg_region(towrite, 0, NewFWAddr); + NewFWLen = towrite; + if (reserase == -1) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Erase failed"); + ret = -1; + goto update_ota_exit; + } + + address = NewFWAddr; + ADDLOG_INFO(LOG_FEATURE_OTA, "Start to read data %i bytes, flash address: 0x%8x", NewFWLen, address); + + do + { + device_mutex_lock(RT_DEV_LOCK_FLASH); + if (flash_stream_write(&flash, address + startaddr, writelen, (uint8_t*)writebuf) < 0) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Write stream failed"); + device_mutex_unlock(RT_DEV_LOCK_FLASH); + ret = -1; + goto update_ota_exit; + } + device_mutex_unlock(RT_DEV_LOCK_FLASH); + + rtos_delay_milliseconds(10); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); + total += writelen; + startaddr += writelen; + towrite -= writelen; + + // checksum attached at file end + if (startaddr + writelen > NewFWLen - 4) + { + file_checksum.c[0] = writebuf[writelen - 4]; + file_checksum.c[1] = writebuf[writelen - 3]; + file_checksum.c[2] = writebuf[writelen - 2]; + file_checksum.c[3] = writebuf[writelen - 1]; + } + if (towrite > 0) + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + if (writelen < 0) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); + } + } + } while ((towrite > 0) && (writelen >= 0)); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written, verifying checksum %u", total, flash_checksum); + uint8_t* buf = (uint8_t*)os_malloc(512); + memset(buf, 0, 512); + // read flash data back and calculate checksum + for (int i = 0; i < NewFWLen; i += 512) + { + int k; + int rlen = (NewFWLen - 4 - i) > 512 ? 512 : (NewFWLen - 4 - i); + device_mutex_lock(RT_DEV_LOCK_FLASH); + flash_stream_read(&flash, NewFWAddr + i, rlen, buf); + device_mutex_unlock(RT_DEV_LOCK_FLASH); + for (k = 0; k < rlen; k++) + { + flash_checksum += buf[k]; + } + } + + ADDLOG_INFO(LOG_FEATURE_OTA, "flash checksum 0x%8x attached checksum 0x%8x", flash_checksum, file_checksum.u); + delay_ms(50); + + ret = update_ota_checksum(&file_checksum, flash_checksum, NewFWAddr); + if (ret == -1) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "The checksum is wrong!"); + goto update_ota_exit; + } + +update_ota_exit: + if (ret != -1) + { + //device_mutex_lock(RT_DEV_LOCK_FLASH); + //device_mutex_unlock(RT_DEV_LOCK_FLASH); + if (buf) free(buf); + } + else + { + if (buf) free(buf); + ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed"); + return http_rest_error(request, ret, "error"); + } + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; +} + +#elif PLATFORM_RTL8720D +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + + int ret = -1; + uint32_t ota_target_index = OTA_INDEX_2; + update_file_hdr* pOtaFileHdr; + update_file_img_hdr* pOtaFileImgHdr; + update_ota_target_hdr OtaTargetHdr; + uint32_t ImageCnt, TempLen; + update_dw_info DownloadInfo[MAX_IMG_NUM]; + + int size = 0; + int read_bytes; + int read_bytes_buf; + uint8_t* buf; + uint32_t OtaFg = 0; + uint32_t IncFg = 0; + int RemainBytes = 0; + uint32_t SigCnt = 0; + uint32_t TempCnt = 0; + uint8_t* signature; + uint32_t sector_cnt = 0; + + if (request->contentLength > 0) + { + towrite = request->contentLength; + } + else + { + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); + goto update_ota_exit; + } + + if (flash_size_8720 == 2) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Only 2MB flash detected - OTA is not supported"); + ret = -1; + goto update_ota_exit; + } + + memset((uint8_t*)&OtaTargetHdr, 0, sizeof(update_ota_target_hdr)); + + DBG_INFO_MSG_OFF(MODULE_FLASH); + + ADDLOG_INFO(LOG_FEATURE_OTA, "Current firmware index is %d", current_fw_idx + 1); + if (current_fw_idx == OTA_INDEX_1) + { + ota_target_index = OTA_INDEX_2; + } + else + { + ota_target_index = OTA_INDEX_1; + } + + // get ota header + writebuf = request->received; + writelen = recv(request->fd, writebuf, 16, 0); + if (writelen != 16) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "failed to recv file header"); + ret = -1; + goto update_ota_exit; + } + + pOtaFileHdr = (update_file_hdr*)writebuf; + pOtaFileImgHdr = (update_file_img_hdr*)(writebuf + 8); + + OtaTargetHdr.FileHdr.FwVer = pOtaFileHdr->FwVer; + OtaTargetHdr.FileHdr.HdrNum = pOtaFileHdr->HdrNum; + + writelen = recv(request->fd, writebuf + 16, (pOtaFileHdr->HdrNum * pOtaFileImgHdr->ImgHdrLen) - 8, 0); + writelen = (pOtaFileHdr->HdrNum * pOtaFileImgHdr->ImgHdrLen) + 8; + + // verify ota header + if (!get_ota_tartget_header((uint8_t*)writebuf, writelen, &OtaTargetHdr, ota_target_index)) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Get OTA header failed"); + goto update_ota_exit; + } + + //ADDLOG_INFO(LOG_FEATURE_OTA, "Erasing..."); + for (int i = 0; i < OtaTargetHdr.ValidImgCnt; i++) + { + ADDLOG_INFO(LOG_FEATURE_OTA, "Target addr:0x%08x, img len: %i", OtaTargetHdr.FileImgHdr[i].FlashAddr, OtaTargetHdr.FileImgHdr[i].ImgLen); + if (OtaTargetHdr.FileImgHdr[i].ImgLen >= 0x1A8000) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Img%i too big, skipping", i); + OtaTargetHdr.FileImgHdr[i].ImgLen = 0; + continue; + } + //watchdog_stop(); + //erase_ota_target_flash(OtaTargetHdr.FileImgHdr[i].FlashAddr, OtaTargetHdr.FileImgHdr[i].ImgLen); + //watchdog_start(); + } + + memset((uint8_t*)DownloadInfo, 0, MAX_IMG_NUM * sizeof(update_dw_info)); + + ImageCnt = OtaTargetHdr.ValidImgCnt; + for (uint32_t i = 0; i < ImageCnt; i++) + { + if (OtaTargetHdr.FileImgHdr[i].ImgLen == 0) + { + DownloadInfo[i].ImageLen = 0; + continue; + } + /* get OTA image and Write New Image to flash, skip the signature, + not write signature first for power down protection*/ + DownloadInfo[i].ImgId = OTA_IMAG; + DownloadInfo[i].FlashAddr = OtaTargetHdr.FileImgHdr[i].FlashAddr - SPI_FLASH_BASE + 8; + DownloadInfo[i].ImageLen = OtaTargetHdr.FileImgHdr[i].ImgLen - 8; /*skip the signature*/ + DownloadInfo[i].ImgOffset = OtaTargetHdr.FileImgHdr[i].Offset; + } + + /*initialize the reveiving counter*/ + TempLen = (OtaTargetHdr.FileHdr.HdrNum * OtaTargetHdr.FileImgHdr[0].ImgHdrLen) + sizeof(update_file_hdr); + + for (uint32_t i = 0; i < ImageCnt; i++) + { + if (DownloadInfo[i].ImageLen == 0) continue; + /*the next image length*/ + RemainBytes = DownloadInfo[i].ImageLen; + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Remain: %i", RemainBytes); + signature = &(OtaTargetHdr.Sign[i][0]); + device_mutex_lock(RT_DEV_LOCK_FLASH); + FLASH_EraseXIP(EraseSector, DownloadInfo[i].FlashAddr - SPI_FLASH_BASE); + device_mutex_unlock(RT_DEV_LOCK_FLASH); + + /*download the new firmware from server*/ + while (RemainBytes > 0) + { + buf = (uint8_t*)request->received; + if (IncFg == 1) + { + IncFg = 0; + read_bytes = read_bytes_buf; + } + else + { + memset(buf, 0, request->receivedLenmax); + read_bytes = recv(request->fd, buf, request->receivedLenmax, 0); + if (read_bytes == 0) + { + break; // Read end + } + if (read_bytes < 0) + { + //OtaImgSize = -1; + //printf("\n\r[%s] Read socket failed", __FUNCTION__); + //ret = -1; + //goto update_ota_exit; + break; + } + read_bytes_buf = read_bytes; + TempLen += read_bytes; + } + + if (TempLen > DownloadInfo[i].ImgOffset) + { + if (!OtaFg) + { + /*reach the desired image, the first packet process*/ + OtaFg = 1; + TempCnt = TempLen - DownloadInfo[i].ImgOffset; + if (TempCnt < 8) + { + SigCnt = TempCnt; + } + else + { + SigCnt = 8; + } + + memcpy(signature, buf + read_bytes - TempCnt, SigCnt); + + if ((SigCnt < 8) || (TempCnt - 8 == 0)) + { + continue; + } + + buf = buf + (read_bytes - TempCnt + 8); + read_bytes = TempCnt - 8; + } + else + { + /*normal packet process*/ + if (SigCnt < 8) + { + if (read_bytes < (int)(8 - SigCnt)) + { + memcpy(signature + SigCnt, buf, read_bytes); + SigCnt += read_bytes; + continue; + } + else + { + memcpy(signature + SigCnt, buf, (8 - SigCnt)); + buf = buf + (8 - SigCnt); + read_bytes -= (8 - SigCnt); + SigCnt = 8; + if (!read_bytes) + { + continue; + } + } + } + } + + RemainBytes -= read_bytes; + if (RemainBytes < 0) + { + read_bytes = read_bytes - (-RemainBytes); + } + + device_mutex_lock(RT_DEV_LOCK_FLASH); + if (DownloadInfo[i].FlashAddr + size >= DownloadInfo[i].FlashAddr + sector_cnt * 4096) + { + sector_cnt++; + FLASH_EraseXIP(EraseSector, DownloadInfo[i].FlashAddr - SPI_FLASH_BASE + sector_cnt * 4096); + } + if (ota_writestream_user(DownloadInfo[i].FlashAddr + size, read_bytes, buf) < 0) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Writing failed"); + device_mutex_unlock(RT_DEV_LOCK_FLASH); + ret = -1; + goto update_ota_exit; + } + device_mutex_unlock(RT_DEV_LOCK_FLASH); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Written %i bytes at 0x%08x", read_bytes, DownloadInfo[i].FlashAddr + size); + rtos_delay_milliseconds(5); + size += read_bytes; + } + } + + if ((uint32_t)size != (OtaTargetHdr.FileImgHdr[i].ImgLen - 8)) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Received size != ota size"); + ret = -1; + goto update_ota_exit; + } + + /*update flag status*/ + size = 0; + OtaFg = 0; + IncFg = 1; + } + + if (verify_ota_checksum(&OtaTargetHdr)) + { + if (!change_ota_signature(&OtaTargetHdr, ota_target_index)) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Change signature failed"); + ret = -1; + goto update_ota_exit; + } + ret = 0; + } + +update_ota_exit: + if (ret != -1) + { + ADDLOG_INFO(LOG_FEATURE_OTA, "OTA is successful"); + total = RemainBytes; + } + else + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed"); + return http_rest_error(request, ret, "error"); + } + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; +} +#elif PLATFORM_REALTEK_NEW +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + + + // Bad implementation. While it somewhat works, it is not recommended to use. HTTP OTA is preferable. + int ret = 0; + + if (request->contentLength > 0) + { + towrite = request->contentLength; + } + else + { + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); + goto update_ota_exit; + } + + flash_get_layout_info(IMG_BOOT, &IMG_ADDR[OTA_IMGID_BOOT][OTA_INDEX_1], NULL); + flash_get_layout_info(IMG_BOOT_OTA2, &IMG_ADDR[OTA_IMGID_BOOT][OTA_INDEX_2], NULL); + flash_get_layout_info(IMG_APP_OTA1, &IMG_ADDR[OTA_IMGID_APP][OTA_INDEX_1], NULL); + flash_get_layout_info(IMG_APP_OTA2, &IMG_ADDR[OTA_IMGID_APP][OTA_INDEX_2], NULL); + + ota_context ctx; + memset(&ctx, 0, sizeof(ota_context)); + ctx.otactrl = malloc(sizeof(update_ota_ctrl_info)); + memset(ctx.otactrl, 0, sizeof(update_ota_ctrl_info)); + + ctx.otaTargetHdr = malloc(sizeof(update_ota_target_hdr)); + memset(ctx.otaTargetHdr, 0, sizeof(update_ota_target_hdr)); + writebuf = request->received; + if (!writelen) writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + // for some reason we receive data along with HTTP header. Skip it. + int skip = 0; + for (; skip < writelen - 5; skip++) + { + if (*(uint32_t*)&writebuf[skip] == 0xFFFFFFFF && writebuf[skip + 4] == 0x01) break; + } + writebuf += skip; + writelen -= skip; + towrite -= skip; + update_file_hdr* pOtaFileHdr = (update_file_hdr*)(writebuf); + ctx.otaTargetHdr->FileHdr.FwVer = pOtaFileHdr->FwVer; + ctx.otaTargetHdr->FileHdr.HdrNum = pOtaFileHdr->HdrNum; + + uint32_t RevHdrLen = pOtaFileHdr->HdrNum * SUB_HEADER_LEN + HEADER_LEN; + if (writelen < RevHdrLen) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "failed to recv file header"); + ret = -1; + goto update_ota_exit; + } + + if (!get_ota_tartget_header(&ctx, writebuf, RevHdrLen)) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Get OTA header failed"); + ret = -1; + goto update_ota_exit; + } + if (!ota_checkimage_layout(ctx.otaTargetHdr)) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "ota_checkimage_layout failed"); + ret = -1; + goto update_ota_exit; + } + ctx.otactrl->IsGetOTAHdr = 1; + writebuf += RevHdrLen; + // magic values to make it somewhat work. + writelen -= RevHdrLen + 5 - 384; + towrite -= RevHdrLen + 5 - 384; + ctx.otactrl->NextImgLen = towrite; + do + { + int size = download_packet_process(&ctx, writebuf, writelen); + download_percentage(&ctx, size, ctx.otactrl->ImageLen); + rtos_delay_milliseconds(10); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); + total += writelen; + towrite -= writelen; + + if (towrite > 0) + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, 2048 < towrite ? 2048 : towrite, 0); + if (writelen < 0) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); + ret = -1; + goto update_ota_exit; + } + } + } while ((towrite > 0) && (writelen >= 0)); + + //erase manifest + flash_erase_sector(&flash, ctx.otactrl->FlashAddr); + + if (!verify_ota_checksum(ctx.otaTargetHdr, ctx.otactrl->targetIdx, ctx.otactrl->index)) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "The checksum is wrong!"); + ret = -1; + goto update_ota_exit; + } + + if (!ota_update_manifest(ctx.otaTargetHdr, ctx.otactrl->targetIdx, ctx.otactrl->index)) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "Change signature failed!"); + ret = -1; + goto update_ota_exit; + } + +update_ota_exit: + ota_update_deinit(&ctx); + if (ret != -1) + { + ADDLOG_INFO(LOG_FEATURE_OTA, "OTA is successful"); + sys_clear_ota_signature(); + //sys_recover_ota_signature(); + } + else + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed."); + return http_rest_error(request, ret, "error"); + } + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; +} +#endif + + +#endif + diff --git a/src/hal/tr6260/hal_ota_tr6260.c b/src/hal/tr6260/hal_ota_tr6260.c new file mode 100644 index 000000000..ba4b7f51c --- /dev/null +++ b/src/hal/tr6260/hal_ota_tr6260.c @@ -0,0 +1,94 @@ +#if PLATFORM_TR6260 + +#include "../../obk_config.h" +#include "../../new_common.h" +#include "../../new_cfg.h" +#include "../../logging/logging.h" +#include "../../httpserver/new_http.h" +#include "../hal_ota.h" + +#include "otaHal.h" +#include "drv_spiflash.h" + +int HAL_FlashRead(char*buffer, int readlen, int startaddr) { + int res; + res = hal_spiflash_read(startaddr, (uint8_t*)buffer, readlen); + return res; +} + +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + + + int ret = 0; + + if (request->contentLength > 0) + { + towrite = request->contentLength; + } + else + { + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); + goto update_ota_exit; + } + + if (otaHal_init() != 0) + { + ret = -1; + goto update_ota_exit; + } + + do + { + if (otaHal_write((unsigned char*)writebuf, writelen) != 0) + { + ret = -1; + goto update_ota_exit; + } + delay_ms(10); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); + total += writelen; + startaddr += writelen; + towrite -= writelen; + if (towrite > 0) + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + if (writelen < 0) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); + ret = -1; + } + } + } while ((towrite > 0) && (writelen >= 0)); + +update_ota_exit: + if (ret != -1) + { + ADDLOG_INFO(LOG_FEATURE_OTA, "OTA is successful"); + otaHal_done(); + } + else + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed. Reboot to retry"); + return http_rest_error(request, ret, "error"); + } + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; +} + +#endif + + diff --git a/src/hal/w800/hal_ota_w800.c b/src/hal/w800/hal_ota_w800.c new file mode 100644 index 000000000..0ac3715d1 --- /dev/null +++ b/src/hal/w800/hal_ota_w800.c @@ -0,0 +1,302 @@ +#if defined(PLATFORM_W800) || defined(PLATFORM_W600) + +#include "../../obk_config.h" +#include "../../new_common.h" +#include "../../new_cfg.h" +#include "../../logging/logging.h" +#include "../../httpserver/new_http.h" +#include "../hal_ota.h" +#include "lwip/sockets.h" + +#include "wm_internal_flash.h" +#include "wm_socket_fwup.h" +#include "wm_fwup.h" + +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + +#ifdef PLATFORM_W600 + int nRetCode = 0; + char error_message[256]; + + if (writelen < 0) + { + ADDLOG_DEBUG(LOG_FEATURE_OTA, "ABORTED: %d bytes to write", writelen); + return http_rest_error(request, -20, "writelen < 0"); + } + + struct pbuf* p; + + //Data is uploaded in 1024 sized chunks, creating a bigger buffer just in case this assumption changes. + //The code below is based on sdk\OpenW600\src\app\ota\wm_http_fwup.c + char* Buffer = (char*)os_malloc(2048 + 3); + memset(Buffer, 0, 2048 + 3); + + if (request->contentLength >= 0) + { + towrite = request->contentLength; + } + + int recvLen = 0; + int totalLen = 0; + //printf("\ntowrite %d writelen=%d\n", towrite, writelen); + + do + { + if (writelen > 0) + { + //bk_printf("Copying %d from writebuf to Buffer towrite=%d\n", writelen, towrite); + memcpy(Buffer + 3, writebuf, writelen); + + if (recvLen == 0) + { + T_BOOTER* booter = (T_BOOTER*)(Buffer + 3); + bk_printf("magic_no=%u, img_type=%u, zip_type=%u\n", booter->magic_no, booter->img_type, booter->zip_type); + + if (TRUE == tls_fwup_img_header_check(booter)) + { + totalLen = booter->upd_img_len + sizeof(T_BOOTER); + OTA_ResetProgress(); + OTA_SetTotalBytes(totalLen); + } + else + { + sprintf(error_message, "Image header check failed"); + nRetCode = -19; + break; + } + + nRetCode = socket_fwup_accept(0, ERR_OK); + if (nRetCode != ERR_OK) + { + sprintf(error_message, "Firmware update startup failed"); + break; + } + } + + p = pbuf_alloc(PBUF_TRANSPORT, writelen + 3, PBUF_REF); + if (!p) + { + sprintf(error_message, "Unable to allocate memory for buffer"); + nRetCode = -18; + break; + } + + if (recvLen == 0) + { + *Buffer = SOCKET_FWUP_START; + } + else if (recvLen == (totalLen - writelen)) + { + *Buffer = SOCKET_FWUP_END; + } + else + { + *Buffer = SOCKET_FWUP_DATA; + } + + *(Buffer + 1) = (writelen >> 8) & 0xFF; + *(Buffer + 2) = writelen & 0xFF; + p->payload = Buffer; + p->len = p->tot_len = writelen + 3; + + nRetCode = socket_fwup_recv(0, p, ERR_OK); + if (nRetCode != ERR_OK) + { + sprintf(error_message, "Firmware data processing failed"); + break; + } + else + { + OTA_IncrementProgress(writelen); + recvLen += writelen; + printf("Downloaded %d / %d\n", recvLen, totalLen); + } + + towrite -= writelen; + } + + if (towrite > 0) + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + if (writelen < 0) + { + sprintf(error_message, "recv returned %d - end of data - remaining %d", writelen, towrite); + nRetCode = -17; + } + } + } while ((nRetCode == 0) && (towrite > 0) && (writelen >= 0)); + + tls_mem_free(Buffer); + + if (nRetCode != 0) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, error_message); + socket_fwup_err(0, nRetCode); + return http_rest_error(request, nRetCode, error_message); + } + + +#elif PLATFORM_W800 + int nRetCode = 0; + char error_message[256]; + + if (writelen < 0) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "ABORTED: %d bytes to write", writelen); + return http_rest_error(request, -20, "writelen < 0"); + } + + struct pbuf* p; + + //The code below is based on W600 code and adopted to the differences in sdk\OpenW800\src\app\ota\wm_http_fwup.c + // fiexd crashing caused by not checking "writelen" before doing memcpy + // e.g. if more than 2 packets arrived before next loop, writelen could be > 2048 !! +#define FWUP_MSG_SIZE 3 +#define MAX_BUFF_SIZE 2048 + char* Buffer = (char*)os_malloc(MAX_BUFF_SIZE + FWUP_MSG_SIZE); + + if (!Buffer) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "ABORTED: failed to allocate buffer"); + return http_rest_error(request, -20, ""); + } + + if (request->contentLength >= 0) + { + towrite = request->contentLength; + } + + int recvLen = 0; + int totalLen = 0; + printf("\ntowrite %d writelen=%d\n", towrite, writelen); + + do + { + while (writelen > 0) + { + int actwrite = writelen < MAX_BUFF_SIZE ? writelen : MAX_BUFF_SIZE; // mustn't write more than Buffers size! Will crash else! + //bk_printf("Copying %d from writebuf to Buffer (writelen=%d) towrite=%d -- free_heap:%d\n", actwrite, writelen, towrite, xPortGetFreeHeapSize()); + memset(Buffer, 0, MAX_BUFF_SIZE + FWUP_MSG_SIZE); + memcpy(Buffer + FWUP_MSG_SIZE, writebuf, actwrite); + if (recvLen == 0) + { + IMAGE_HEADER_PARAM_ST *booter = (IMAGE_HEADER_PARAM_ST*)(Buffer + FWUP_MSG_SIZE); + bk_printf("magic_no=%u, img_type=%u, zip_type=%u, signature=%u\n", + booter->magic_no, booter->img_attr.b.img_type, booter->img_attr.b.zip_type, booter->img_attr.b.signature); + + if (TRUE == tls_fwup_img_header_check(booter)) + { + totalLen = booter->img_len + sizeof(IMAGE_HEADER_PARAM_ST); + if (booter->img_attr.b.signature) + { + totalLen += 128; + } + } + else + { + sprintf(error_message, "Image header check failed"); + nRetCode = -19; + break; + } + + nRetCode = socket_fwup_accept(0, ERR_OK); + if (nRetCode != ERR_OK) + { + sprintf(error_message, "Firmware update startup failed"); + break; + } + } + + p = pbuf_alloc(PBUF_TRANSPORT, actwrite + FWUP_MSG_SIZE, PBUF_REF); + if (!p) + { + sprintf(error_message, "Unable to allocate memory for buffer"); + nRetCode = -18; + break; + } + + if (recvLen == 0) + { + *Buffer = SOCKET_FWUP_START; + } + else if (recvLen == (totalLen - actwrite)) + { + *Buffer = SOCKET_FWUP_END; + } + else + { + *Buffer = SOCKET_FWUP_DATA; + } + + *(Buffer + 1) = (actwrite >> 8) & 0xFF; + *(Buffer + 2) = actwrite & 0xFF; + p->payload = Buffer; + p->len = p->tot_len = actwrite + FWUP_MSG_SIZE; + + nRetCode = socket_fwup_recv(0, p, ERR_OK); + if (nRetCode != ERR_OK) + { + sprintf(error_message, "Firmware data processing failed"); + break; + } + else + { + recvLen += actwrite; + } + + towrite -= actwrite; + writelen -= actwrite; // calculate, how much is left to write + writebuf += actwrite; // in case, we only wrote part of buffer, advance in buffer + } + + if (towrite > 0) + { + writebuf = request->received; + writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + if (writelen < 0) + { + sprintf(error_message, "recv returned %d - end of data - remaining %d", writelen, towrite); + nRetCode = -17; + } + } + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Downloaded %d / %d", recvLen, totalLen); + rtos_delay_milliseconds(10); // give some time for flashing - will else increase used memory fast + } while ((nRetCode == 0) && (towrite > 0) && (writelen >= 0)); + bk_printf("Download completed (%d / %d)\n", recvLen, totalLen); + if (Buffer) os_free(Buffer); + if (p) pbuf_free(p); + + + if (nRetCode != 0) + { + ADDLOG_ERROR(LOG_FEATURE_OTA, error_message); + socket_fwup_err(0, nRetCode); + return http_rest_error(request, nRetCode, error_message); + } + + + +#endif + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; +} + +int HAL_FlashRead(char*buffer, int readlen, int startaddr) { + int res; + res = tls_fls_read(startaddr, (uint8_t*)buffer, readlen); + return res; +} + +#endif diff --git a/src/hal/win32/hal_ota_win32.c b/src/hal/win32/hal_ota_win32.c new file mode 100644 index 000000000..d6658e9f5 --- /dev/null +++ b/src/hal/win32/hal_ota_win32.c @@ -0,0 +1,22 @@ +#ifdef WINDOWS + +#include "../../obk_config.h" +#include "../../new_common.h" +#include "../../new_cfg.h" +#include "../../httpserver/new_http.h" +#include "../../logging/logging.h" + +int HAL_FlashRead(char*buffer, int readlen, int startaddr) { + int res; + res = 0; + return res; +} + +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + + return 0; +} + +#endif + diff --git a/src/hal/xradio/hal_ota_xradio.c b/src/hal/xradio/hal_ota_xradio.c new file mode 100644 index 000000000..dce94cba0 --- /dev/null +++ b/src/hal/xradio/hal_ota_xradio.c @@ -0,0 +1,146 @@ +#if PLATFORM_XRADIO + +#include +#include +#include "../../obk_config.h" +#include "../../new_common.h" +#include "../../new_cfg.h" +#include "../../httpserver/new_http.h" +#include "../../logging/logging.h" + +uint32_t flash_read(uint32_t flash, uint32_t addr, void* buf, uint32_t size); +#define FLASH_INDEX_XR809 0 + +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) +{ + int total = 0; + int towrite = request->bodylen; + char* writebuf = request->bodystart; + int writelen = request->bodylen; + + ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); + + bool recvfp = true; + + ota_status_t ota_update_rest_init(void* url) + { + return OTA_STATUS_OK; + } + ota_status_t ota_update_rest_get(uint8_t* buf, uint32_t buf_size, uint32_t* recv_size, uint8_t* eof_flag) + { + if (recvfp) + { + //free(buf); + //recvfp = false; + //buf = writebuf; + //*recv_size = writelen; + //return OTA_STATUS_OK; + int bsize = (writelen > buf_size ? buf_size : writelen); + memcpy(buf, writebuf + startaddr, bsize); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", bsize, startaddr); + startaddr += bsize; + *recv_size = bsize; + *eof_flag = 0; + total += bsize; + towrite -= bsize; + writelen -= bsize; + recvfp = writelen > 0; + return OTA_STATUS_OK; + } + if (towrite > 0) + { + *recv_size = writelen = recv(request->fd, buf, (request->receivedLenmax > buf_size ? buf_size : request->receivedLenmax), 0); + //*recv_size = writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); + ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); + if (writelen < 0) + { + ADDLOG_INFO(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); + *eof_flag = 1; + *recv_size = 0; + return OTA_STATUS_OK; + //return OTA_STATUS_ERROR; + } + } + total += writelen; + towrite -= writelen; + + if ((towrite > 0) && (writelen >= 0)) + { + *eof_flag = 0; + rtos_delay_milliseconds(10); + return OTA_STATUS_OK; + } + *eof_flag = 1; + return OTA_STATUS_OK; + } + + int ret = 0; + uint32_t* verify_value; + ota_verify_t verify_type; + ota_verify_data_t verify_data; + + if (request->contentLength > 0) + { + towrite = request->contentLength; + } + else + { + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); + goto update_ota_exit; + } + + ota_init(); + + if (ota_update_image(NULL, ota_update_rest_init, ota_update_rest_get) != OTA_STATUS_OK) + { + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "ota_update_image failed"); + goto update_ota_exit; + } + + if (ota_get_verify_data(&verify_data) != OTA_STATUS_OK) + { + ADDLOG_INFO(LOG_FEATURE_OTA, "ota_get_verify_data not ok, OTA_VERIFY_NONE"); + verify_type = OTA_VERIFY_NONE; + verify_value = NULL; + } + else + { + verify_type = verify_data.ov_type; + ADDLOG_INFO(LOG_FEATURE_OTA, "ota_get_verify_data ok"); + verify_value = (uint32_t*)(verify_data.ov_data); + } + + if (ota_verify_image(verify_type, verify_value) != OTA_STATUS_OK) + { + ret = -1; + ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA verify image failed"); + goto update_ota_exit; + } + +update_ota_exit: + if (ret != -1) + { + ADDLOG_INFO(LOG_FEATURE_OTA, "OTA is successful"); + } + else + { + ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed."); + return http_rest_error(request, ret, "error"); + } + ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); + http_setup(request, httpMimeTypeJson); + hprintf255(request, "{\"size\":%d}", total); + poststr(request, NULL); + CFG_IncrementOTACount(); + return 0; +} + +int HAL_FlashRead(char*buffer, int readlen, int startaddr) { + int res; + //uint32_t flash_read(uint32_t flash, uint32_t addr,void *buf, uint32_t size) + res = flash_read(0, startaddr, buffer, readlen); + return res; +} +#endif \ No newline at end of file diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index c34bdefb9..c1ce0f9d9 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -2,7 +2,7 @@ #include "http_fns.h" #include "../new_pins.h" #include "../new_cfg.h" -#include "../ota/ota.h" +#include "../hal/hal_ota.h" // Commands register, execution API and cmd tokenizer #include "../cmnds/cmd_public.h" #include "../driver/drv_tuyaMCU.h" @@ -35,7 +35,7 @@ #elif PLATFORM_XRADIO #include -#include "ota/ota.h" +#include #elif defined(PLATFORM_BK7231N) // tuya-iotos-embeded-sdk-wifi-ble-bk7231n/sdk/include/tuya_hal_storage.h #include "tuya_hal_storage.h" @@ -1045,12 +1045,10 @@ typedef enum { #endif -#if PLATFORM_BK7231N || PLATFORM_BK7231T - if (ota_progress() >= 0) + if (OTA_GetProgress() >= 0) { - hprintf255(request, "
OTA In Progress. Downloaded: %i B Flashed: %06lXh
", OTA_GetTotalBytes(), ota_progress()); + hprintf255(request, "
OTA In Progress. Downloaded: %i B Flashed: %06lXh
", OTA_GetTotalBytes(), OTA_GetProgress()); } -#endif if (bSafeMode) { hprintf255(request, "
You are in safe mode (AP mode) because full reboot failed %i times. ", g_bootFailures); diff --git a/src/httpserver/json_interface.c b/src/httpserver/json_interface.c index 0fa972873..ecec403da 100644 --- a/src/httpserver/json_interface.c +++ b/src/httpserver/json_interface.c @@ -2,7 +2,7 @@ #include "http_fns.h" #include "../new_pins.h" #include "../new_cfg.h" -#include "../ota/ota.h" +#include "../hal/hal_ota.h" // Commands register, execution API and cmd tokenizer #include "../cmnds/cmd_public.h" #include "../driver/drv_tuyaMCU.h" diff --git a/src/httpserver/new_http.c b/src/httpserver/new_http.c index a34cff546..312825862 100644 --- a/src/httpserver/new_http.c +++ b/src/httpserver/new_http.c @@ -7,7 +7,7 @@ #include "http_fns.h" #include "../new_pins.h" #include "../new_cfg.h" -#include "../ota/ota.h" +#include "../hal/hal_ota.h" #include "../hal/hal_wifi.h" #include "../base64/base64.h" #include "http_basic_auth.h" diff --git a/src/httpserver/new_http.h b/src/httpserver/new_http.h index 998e40fa2..6ccb02fa2 100644 --- a/src/httpserver/new_http.h +++ b/src/httpserver/new_http.h @@ -97,5 +97,7 @@ int HTTP_RegisterCallback(const char* url, int method, http_callback_fn callback int my_strnicmp(const char* a, const char* b, int len); +int http_rest_error(http_request_t* request, int code, char* msg); + #endif diff --git a/src/httpserver/rest_interface.c b/src/httpserver/rest_interface.c index 4920489a8..f469ef9eb 100644 --- a/src/httpserver/rest_interface.c +++ b/src/httpserver/rest_interface.c @@ -5,7 +5,7 @@ #include "../httpserver/new_http.h" #include "../new_pins.h" #include "../jsmn/jsmn_h.h" -#include "../ota/ota.h" +#include "../hal/hal_ota.h" #include "../hal/hal_wifi.h" #include "../hal/hal_flashVars.h" #include "../littlefs/our_lfs.h" @@ -13,124 +13,7 @@ #define DEFAULT_FLASH_LEN 0x200000 -#if PLATFORM_XRADIO -#include -#include "ota/ota.h" - -uint32_t flash_read(uint32_t flash, uint32_t addr, void* buf, uint32_t size); -#define FLASH_INDEX_XR809 0 - -#elif PLATFORM_BL602 -#include -#include -#include -#include - -#elif defined(PLATFORM_W800) || defined(PLATFORM_W600) - -#include "wm_internal_flash.h" -#include "wm_socket_fwup.h" -#include "wm_fwup.h" - -#elif PLATFORM_LN882H - -#include "hal/hal_flash.h" -#include "flash_partition_table.h" - -#elif PLATFORM_ESPIDF || PLATFORM_ESP8266 - -#include "esp_system.h" -#include "esp_ota_ops.h" -#include "esp_app_format.h" -#include "esp_flash_partitions.h" -#include "esp_partition.h" -#include "nvs.h" -#include "nvs_flash.h" -#include "esp_wifi.h" -#if PLATFORM_ESPIDF -#include "esp_flash.h" -#include "esp_pm.h" -#else -#include "esp_image_format.h" -#include "spi_flash.h" -#define esp_flash_read(a,b,c,d) spi_flash_read(c,b,d) -#define OTA_WITH_SEQUENTIAL_WRITES OTA_SIZE_UNKNOWN -#define esp_ota_abort esp_ota_end -#endif - -#elif PLATFORM_REALTEK - -#include "flash_api.h" -#include "device_lock.h" -#include "sys_api.h" - -extern flash_t flash; - -#if PLATFORM_RTL87X0C - -#include "ota_8710c.h" - -extern uint32_t sys_update_ota_get_curr_fw_idx(void); -extern uint32_t sys_update_ota_prepare_addr(void); -extern void sys_disable_fast_boot(void); -extern void get_fw_info(uint32_t* targetFWaddr, uint32_t* currentFWaddr, uint32_t* fw1_sn, uint32_t* fw2_sn); -static flash_t flash_ota; - -#elif PLATFORM_RTL8710B - -#include "rtl8710b_ota.h" - -extern uint32_t current_fw_idx; - -#elif PLATFORM_RTL8710A - -extern uint32_t current_fw_idx; - -#undef DEFAULT_FLASH_LEN -#define DEFAULT_FLASH_LEN 0x400000 - -#elif PLATFORM_RTL8720D - -#include "rtl8721d_boot.h" -#include "rtl8721d_ota.h" -#include "diag.h" -#include "wdt_api.h" - -extern uint32_t current_fw_idx; -extern uint8_t flash_size_8720; - -#undef DEFAULT_FLASH_LEN -#define DEFAULT_FLASH_LEN (flash_size_8720 << 20) - -#elif PLATFORM_REALTEK_NEW - -#include "ameba_ota.h" -extern uint32_t current_fw_idx; -extern uint32_t IMG_ADDR[OTA_IMGID_MAX][2]; -extern uint8_t flash_size_8720; -#undef DEFAULT_FLASH_LEN -#define DEFAULT_FLASH_LEN (flash_size_8720 << 20) - -#endif - -#elif PLATFORM_ECR6600 - -#include "flash.h" -extern int ota_init(void); -extern int ota_write(unsigned char* data, unsigned int len); -extern int ota_done(bool reset); - -#elif PLATFORM_TR6260 - -#include "otaHal.h" -#include "drv_spiflash.h" - -#else - -extern unsigned int flash_read(char* user_buf, unsigned int count, unsigned int address); - -#endif #include "../new_cfg.h" // Commands register, execution API and cmd tokenizer @@ -143,7 +26,7 @@ extern unsigned int flash_read(char* user_buf, unsigned int count, unsigned int #define MAX_JSON_VALUE_LENGTH 128 -static int http_rest_error(http_request_t* request, int code, char* msg); +int http_rest_error(http_request_t* request, int code, char* msg); static int http_rest_get(http_request_t* request); static int http_rest_post(http_request_t* request); @@ -168,7 +51,7 @@ static int http_rest_post_lfs_file(http_request_t* request); #endif static int http_rest_post_reboot(http_request_t* request); -static int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr); +int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr); static int http_rest_get_flash(http_request_t* request, int startaddr, int len); static int http_rest_get_flash_advanced(http_request_t* request); static int http_rest_post_flash_advanced(http_request_t* request); @@ -299,28 +182,32 @@ static int http_rest_post(http_request_t* request) { return http_rest_post_reboot(request); } if (!strcmp(request->url, "api/ota")) { + OTA_IncrementProgress(1); + int r = 0; #if PLATFORM_BEKEN - return http_rest_post_flash(request, START_ADR_OF_BK_PARTITION_OTA, LFS_BLOCKS_END); + r = http_rest_post_flash(request, START_ADR_OF_BK_PARTITION_OTA, LFS_BLOCKS_END); #elif PLATFORM_W600 - return http_rest_post_flash(request, -1, -1); + r = http_rest_post_flash(request, -1, -1); #elif PLATFORM_W800 - return http_rest_post_flash(request, -1, -1); + r = http_rest_post_flash(request, -1, -1); #elif PLATFORM_BL602 - return http_rest_post_flash(request, -1, -1); + r = http_rest_post_flash(request, -1, -1); #elif PLATFORM_LN882H - return http_rest_post_flash(request, -1, -1); + r = http_rest_post_flash(request, -1, -1); #elif PLATFORM_ESPIDF || PLATFORM_ESP8266 - return http_rest_post_flash(request, -1, -1); + r = http_rest_post_flash(request, -1, -1); #elif PLATFORM_REALTEK && !PLATFORM_RTL8720E - return http_rest_post_flash(request, 0, -1); + r = http_rest_post_flash(request, 0, -1); #elif PLATFORM_ECR6600 || PLATFORM_TR6260 - return http_rest_post_flash(request, -1, -1); + r = http_rest_post_flash(request, -1, -1); #elif PLATFORM_XRADIO && !PLATFORM_XR809 - return http_rest_post_flash(request, 0, -1); + r = http_rest_post_flash(request, 0, -1); #else // TODO ADDLOG_ERROR(LOG_FEATURE_API, "No OTA"); #endif + OTA_ResetProgress(); + return r; } if (!strncmp(request->url, "api/flash/", 10)) { return http_rest_post_flash_advanced(request); @@ -1357,7 +1244,7 @@ static int http_rest_post_channelTypes(http_request_t* request) { return http_rest_error(request, 200, "OK"); } -static int http_rest_error(http_request_t* request, int code, char* msg) { +int http_rest_error(http_request_t* request, int code, char* msg) { request->responseCode = code; http_setup(request, httpMimeTypeJson); if (code != 200) { @@ -1370,2047 +1257,6 @@ static int http_rest_error(http_request_t* request, int code, char* msg) { return 0; } -#if PLATFORM_BL602 - -typedef struct ota_header { - union { - struct { - uint8_t header[16]; - - uint8_t type[4];//RAW XZ - uint32_t len;//body len - uint8_t pad0[8]; - - uint8_t ver_hardware[16]; - uint8_t ver_software[16]; - - uint8_t sha256[32]; - uint32_t unpacked_len;//full len - } s; - uint8_t _pad[512]; - } u; -} ota_header_t; -#define OTA_HEADER_SIZE (sizeof(ota_header_t)) - -static int _check_ota_header(ota_header_t *ota_header, uint32_t *ota_len, int *use_xz) -{ - char str[33];//assume max segment size - int i; - - memcpy(str, ota_header->u.s.header, sizeof(ota_header->u.s.header)); - str[sizeof(ota_header->u.s.header)] = '\0'; - puts("[OTA] [HEADER] ota header is "); - puts(str); - puts("\r\n"); - - memcpy(str, ota_header->u.s.type, sizeof(ota_header->u.s.type)); - str[sizeof(ota_header->u.s.type)] = '\0'; - puts("[OTA] [HEADER] file type is "); - puts(str); - puts("\r\n"); - if (strstr(str, "XZ")) { - *use_xz = 1; - } - else { - *use_xz = 0; - } - - memcpy(ota_len, &(ota_header->u.s.len), 4); - printf("[OTA] [HEADER] file length (exclude ota header) is %lu\r\n", *ota_len); - - memcpy(str, ota_header->u.s.ver_hardware, sizeof(ota_header->u.s.ver_hardware)); - str[sizeof(ota_header->u.s.ver_hardware)] = '\0'; - puts("[OTA] [HEADER] ver_hardware is "); - puts(str); - puts("\r\n"); - - memcpy(str, ota_header->u.s.ver_software, sizeof(ota_header->u.s.ver_software)); - str[sizeof(ota_header->u.s.ver_software)] = '\0'; - puts("[OTA] [HEADER] ver_software is "); - puts(str); - puts("\r\n"); - - memcpy(str, ota_header->u.s.sha256, sizeof(ota_header->u.s.sha256)); - str[sizeof(ota_header->u.s.sha256)] = '\0'; - puts("[OTA] [HEADER] sha256 is "); - for (i = 0; i < sizeof(ota_header->u.s.sha256); i++) { - printf("%02X", str[i]); - } - puts("\r\n"); - - return 0; -} -#endif - -#if PLATFORM_LN882H -#include "ota_port.h" -#include "ota_image.h" -#include "ota_types.h" -#include "hal/hal_flash.h" -#include "netif/ethernetif.h" -#include "flash_partition_table.h" - - -#define KV_OTA_UPG_STATE ("kv_ota_upg_state") -#define HTTP_OTA_DEMO_STACK_SIZE (1024 * 16) - -#define SECTOR_SIZE_4KB (1024 * 4) - -static char g_http_uri_buff[512] = "http://192.168.122.48:9090/ota-images/otaimage-v1.3.bin"; - -// a block to save http data. -static char *temp4K_buf = NULL; -static int temp4k_offset = 0; - -// where to save OTA data in flash. -static int32_t flash_ota_start_addr = OTA_SPACE_OFFSET; -static int32_t flash_ota_offset = 0; -static uint8_t is_persistent_started = LN_FALSE; -static uint8_t is_ready_to_verify = LN_FALSE; -static uint8_t is_precheck_ok = LN_FALSE; -static uint8_t httpc_ota_started = 0; - -/** - * @brief Pre-check the image file to be downloaded. - * - * @attention None - * - * @param[in] app_offset The offset of the APP partition in Flash. - * @param[in] ota_hdr pointer to ota partition info struct. - * - * @return whether the check is successful. - * @retval #LN_TRUE successful. - * @retval #LN_FALSE failed. - */ -static int ota_download_precheck(uint32_t app_offset, image_hdr_t * ota_hdr) -{ - - image_hdr_t *app_hdr = NULL; - if (NULL == (app_hdr = OS_Malloc(sizeof(image_hdr_t)))) { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "[%s:%d] malloc failed.\r\n", __func__, __LINE__); - return LN_FALSE; - } - - if (OTA_ERR_NONE != image_header_fast_read(app_offset, app_hdr)) { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "failed to read app header.\r\n"); - goto ret_err; - } - - if ((ota_hdr->image_type == IMAGE_TYPE_ORIGINAL) || \ - (ota_hdr->image_type == IMAGE_TYPE_ORIGINAL_XZ)) - { - // check version - if (((ota_hdr->ver.ver_major << 8) + ota_hdr->ver.ver_minor) == \ - ((app_hdr->ver.ver_major << 8) + app_hdr->ver.ver_minor)) { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "[%s:%d] same version, do not upgrade!\r\n", - __func__, __LINE__); - } - - // check file size - if (((ota_hdr->img_size_orig + sizeof(image_hdr_t)) > APP_SPACE_SIZE) || \ - ((ota_hdr->img_size_orig_xz + sizeof(image_hdr_t)) > OTA_SPACE_SIZE)) { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "[%s:%d] size check failed.\r\n", __func__, __LINE__); - goto ret_err; - } - } - else { - //image type not support! - goto ret_err; - } - - OS_Free(app_hdr); - return LN_TRUE; - -ret_err: - OS_Free(app_hdr); - return LN_FALSE; -} - -static int ota_persistent_start(void) -{ - if (NULL == temp4K_buf) { - temp4K_buf = OS_Malloc(SECTOR_SIZE_4KB); - if (NULL == temp4K_buf) { - LOG(LOG_LVL_INFO,"failed to alloc 4KB!!!\r\n"); - return LN_FALSE; - } - memset(temp4K_buf, 0, SECTOR_SIZE_4KB); - } - - temp4k_offset = 0; - flash_ota_start_addr = OTA_SPACE_OFFSET; - flash_ota_offset = 0; - is_persistent_started = LN_TRUE; - return LN_TRUE; -} - -/** - * @brief Save block to flash. - * - * @param buf - * @param buf_len - * @return return LN_TRUE on success, LN_FALSE on failure. - */ -static int ota_persistent_write(const char *buf, const int32_t buf_len) -{ - int part_len = SECTOR_SIZE_4KB; // we might have a buffer so large, that we need to write multiple 4K segments ... - int buf_offset = 0; // ... and we need to keep track, what is already written - - if (!is_persistent_started) { - return LN_TRUE; - } - - if (temp4k_offset + buf_len < SECTOR_SIZE_4KB) { - // just copy all buf data to temp4K_buf - memcpy(temp4K_buf + temp4k_offset, buf, buf_len); - temp4k_offset += buf_len; - part_len = 0; - } - while (part_len >= SECTOR_SIZE_4KB) { // so we didn't copy all data to buffer (part_len would be 0 then) - // just copy part of buf to temp4K_buf - part_len = temp4k_offset + buf_len - buf_offset - SECTOR_SIZE_4KB; // beware, this can be > SECTOR_SIZE_4KB !!! - memcpy(temp4K_buf + temp4k_offset, buf + buf_offset, buf_len - buf_offset - part_len); - temp4k_offset += buf_len - buf_offset - part_len; - buf_offset = buf_len - part_len; - - if (temp4k_offset >= SECTOR_SIZE_4KB ) { - // write to flash - ADDLOG_DEBUG(LOG_FEATURE_OTA, "write at flash: 0x%08x (temp4k_offset=%i)\r\n", flash_ota_start_addr + flash_ota_offset,temp4k_offset); - - if (flash_ota_offset == 0) { - if (LN_TRUE != ota_download_precheck(APP_SPACE_OFFSET, (image_hdr_t *)temp4K_buf)) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "ota download precheck failed!\r\n"); - is_precheck_ok = LN_FALSE; - return LN_FALSE; - } - is_precheck_ok = LN_TRUE; - } - - hal_flash_erase(flash_ota_start_addr + flash_ota_offset, SECTOR_SIZE_4KB); - hal_flash_program(flash_ota_start_addr + flash_ota_offset, SECTOR_SIZE_4KB, (uint8_t *)temp4K_buf); - - flash_ota_offset += SECTOR_SIZE_4KB; - memset(temp4K_buf, 0, SECTOR_SIZE_4KB); - temp4k_offset = 0; - } - } - if (part_len > 0) { - memcpy(temp4K_buf + temp4k_offset, buf + (buf_len - part_len), part_len); - temp4k_offset += part_len; - } - - return LN_TRUE; -} - -/** - * @brief save last block and clear flags. - * @return return LN_TRUE on success, LN_FALSE on failure. - */ -static int ota_persistent_finish(void) -{ - if (!is_persistent_started) { - return LN_FALSE; - } - - // write to flash - ADDLOG_DEBUG(LOG_FEATURE_OTA, "write at flash: 0x%08x\r\n", flash_ota_start_addr + flash_ota_offset); - hal_flash_erase(flash_ota_start_addr + flash_ota_offset, SECTOR_SIZE_4KB); - hal_flash_program(flash_ota_start_addr + flash_ota_offset, SECTOR_SIZE_4KB, (uint8_t *)temp4K_buf); - - OS_Free(temp4K_buf); - temp4K_buf = NULL; - temp4k_offset = 0; - - flash_ota_offset = 0; - is_persistent_started = LN_FALSE; - return LN_TRUE; -} - -static int update_ota_state(void) -{ - upg_state_t state = UPG_STATE_DOWNLOAD_OK; - ln_nvds_set_ota_upg_state(state); - return LN_TRUE; -} -/** - * @brief check ota image header, body. - * @return return LN_TRUE on success, LN_FALSE on failure. - */ -static int ota_verify_download(void) -{ - image_hdr_t ota_header; - - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Succeed to verify OTA image content.\r\n"); - if (OTA_ERR_NONE != image_header_fast_read(OTA_SPACE_OFFSET, &ota_header)) { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "failed to read ota header.\r\n"); - return LN_FALSE; - } - - if (OTA_ERR_NONE != image_header_verify(&ota_header)) { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "failed to verify ota header.\r\n"); - return LN_FALSE; - } - - if (OTA_ERR_NONE != image_body_verify(OTA_SPACE_OFFSET, &ota_header)) { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "failed to verify ota body.\r\n"); - return LN_FALSE; - } - - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Succeed to verify OTA image content.\r\n"); - return LN_TRUE; -} -#endif - -static int http_rest_post_flash(http_request_t* request, int startaddr, int maxaddr) -{ - int total = 0; - int towrite = request->bodylen; - char* writebuf = request->bodystart; - int writelen = request->bodylen; - - ADDLOG_DEBUG(LOG_FEATURE_OTA, "OTA post len %d", request->contentLength); - -#ifdef PLATFORM_W600 - int nRetCode = 0; - char error_message[256]; - - if(writelen < 0) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "ABORTED: %d bytes to write", writelen); - return http_rest_error(request, -20, "writelen < 0"); - } - - struct pbuf* p; - - //Data is uploaded in 1024 sized chunks, creating a bigger buffer just in case this assumption changes. - //The code below is based on sdk\OpenW600\src\app\ota\wm_http_fwup.c - char* Buffer = (char*)os_malloc(2048 + 3); - memset(Buffer, 0, 2048 + 3); - - if(request->contentLength >= 0) - { - towrite = request->contentLength; - } - - int recvLen = 0; - int totalLen = 0; - //printf("\ntowrite %d writelen=%d\n", towrite, writelen); - - do - { - if(writelen > 0) - { - //bk_printf("Copying %d from writebuf to Buffer towrite=%d\n", writelen, towrite); - memcpy(Buffer + 3, writebuf, writelen); - - if(recvLen == 0) - { - T_BOOTER* booter = (T_BOOTER*)(Buffer + 3); - bk_printf("magic_no=%u, img_type=%u, zip_type=%u\n", booter->magic_no, booter->img_type, booter->zip_type); - - if(TRUE == tls_fwup_img_header_check(booter)) - { - totalLen = booter->upd_img_len + sizeof(T_BOOTER); - OTA_ResetProgress(); - OTA_SetTotalBytes(totalLen); - } - else - { - sprintf(error_message, "Image header check failed"); - nRetCode = -19; - break; - } - - nRetCode = socket_fwup_accept(0, ERR_OK); - if(nRetCode != ERR_OK) - { - sprintf(error_message, "Firmware update startup failed"); - break; - } - } - - p = pbuf_alloc(PBUF_TRANSPORT, writelen + 3, PBUF_REF); - if(!p) - { - sprintf(error_message, "Unable to allocate memory for buffer"); - nRetCode = -18; - break; - } - - if(recvLen == 0) - { - *Buffer = SOCKET_FWUP_START; - } - else if(recvLen == (totalLen - writelen)) - { - *Buffer = SOCKET_FWUP_END; - } - else - { - *Buffer = SOCKET_FWUP_DATA; - } - - *(Buffer + 1) = (writelen >> 8) & 0xFF; - *(Buffer + 2) = writelen & 0xFF; - p->payload = Buffer; - p->len = p->tot_len = writelen + 3; - - nRetCode = socket_fwup_recv(0, p, ERR_OK); - if(nRetCode != ERR_OK) - { - sprintf(error_message, "Firmware data processing failed"); - break; - } - else - { - OTA_IncrementProgress(writelen); - recvLen += writelen; - printf("Downloaded %d / %d\n", recvLen, totalLen); - } - - towrite -= writelen; - } - - if(towrite > 0) - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); - if(writelen < 0) - { - sprintf(error_message, "recv returned %d - end of data - remaining %d", writelen, towrite); - nRetCode = -17; - } - } - } while((nRetCode == 0) && (towrite > 0) && (writelen >= 0)); - - tls_mem_free(Buffer); - - if(nRetCode != 0) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, error_message); - socket_fwup_err(0, nRetCode); - return http_rest_error(request, nRetCode, error_message); - } - - -#elif PLATFORM_W800 - int nRetCode = 0; - char error_message[256]; - - if(writelen < 0) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "ABORTED: %d bytes to write", writelen); - return http_rest_error(request, -20, "writelen < 0"); - } - - struct pbuf* p; - - //The code below is based on W600 code and adopted to the differences in sdk\OpenW800\src\app\ota\wm_http_fwup.c - // fiexd crashing caused by not checking "writelen" before doing memcpy - // e.g. if more than 2 packets arrived before next loop, writelen could be > 2048 !! -#define FWUP_MSG_SIZE 3 -#define MAX_BUFF_SIZE 2048 - char* Buffer = (char*)os_malloc(MAX_BUFF_SIZE + FWUP_MSG_SIZE); - - if(!Buffer) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "ABORTED: failed to allocate buffer"); - return http_rest_error(request, -20, ""); - } - - if(request->contentLength >= 0) - { - towrite = request->contentLength; - } - - int recvLen = 0; - int totalLen = 0; - printf("\ntowrite %d writelen=%d\n", towrite, writelen); - - do - { - while(writelen > 0) - { - int actwrite = writelen < MAX_BUFF_SIZE ? writelen : MAX_BUFF_SIZE; // mustn't write more than Buffers size! Will crash else! - //bk_printf("Copying %d from writebuf to Buffer (writelen=%d) towrite=%d -- free_heap:%d\n", actwrite, writelen, towrite, xPortGetFreeHeapSize()); - memset(Buffer, 0, MAX_BUFF_SIZE + FWUP_MSG_SIZE); - memcpy(Buffer + FWUP_MSG_SIZE, writebuf, actwrite); - if(recvLen == 0) - { - IMAGE_HEADER_PARAM_ST *booter = (IMAGE_HEADER_PARAM_ST*)(Buffer + FWUP_MSG_SIZE); - bk_printf("magic_no=%u, img_type=%u, zip_type=%u, signature=%u\n", - booter->magic_no, booter->img_attr.b.img_type, booter->img_attr.b.zip_type, booter->img_attr.b.signature); - - if(TRUE == tls_fwup_img_header_check(booter)) - { - totalLen = booter->img_len + sizeof(IMAGE_HEADER_PARAM_ST); - if (booter->img_attr.b.signature) - { - totalLen += 128; - } - } - else - { - sprintf(error_message, "Image header check failed"); - nRetCode = -19; - break; - } - - nRetCode = socket_fwup_accept(0, ERR_OK); - if(nRetCode != ERR_OK) - { - sprintf(error_message, "Firmware update startup failed"); - break; - } - } - - p = pbuf_alloc(PBUF_TRANSPORT, actwrite + FWUP_MSG_SIZE, PBUF_REF); - if(!p) - { - sprintf(error_message, "Unable to allocate memory for buffer"); - nRetCode = -18; - break; - } - - if(recvLen == 0) - { - *Buffer = SOCKET_FWUP_START; - } - else if(recvLen == (totalLen - actwrite)) - { - *Buffer = SOCKET_FWUP_END; - } - else - { - *Buffer = SOCKET_FWUP_DATA; - } - - *(Buffer + 1) = (actwrite >> 8) & 0xFF; - *(Buffer + 2) = actwrite & 0xFF; - p->payload = Buffer; - p->len = p->tot_len = actwrite + FWUP_MSG_SIZE; - - nRetCode = socket_fwup_recv(0, p, ERR_OK); - if(nRetCode != ERR_OK) - { - sprintf(error_message, "Firmware data processing failed"); - break; - } - else - { - recvLen += actwrite; - } - - towrite -= actwrite; - writelen -= actwrite; // calculate, how much is left to write - writebuf += actwrite; // in case, we only wrote part of buffer, advance in buffer - } - - if(towrite > 0) - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); - if(writelen < 0) - { - sprintf(error_message, "recv returned %d - end of data - remaining %d", writelen, towrite); - nRetCode = -17; - } - } - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Downloaded %d / %d", recvLen, totalLen); - rtos_delay_milliseconds(10); // give some time for flashing - will else increase used memory fast - } while((nRetCode == 0) && (towrite > 0) && (writelen >= 0)); - bk_printf("Download completed (%d / %d)\n", recvLen, totalLen); - if(Buffer) os_free(Buffer); - if(p) pbuf_free(p); - - - if(nRetCode != 0) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, error_message); - socket_fwup_err(0, nRetCode); - return http_rest_error(request, nRetCode, error_message); - } - - -#elif PLATFORM_BL602 - int sockfd, i; - int ret; - struct hostent* hostinfo; - uint8_t* recv_buffer; - struct sockaddr_in dest; - iot_sha256_context ctx; - uint8_t sha256_result[32]; - uint8_t sha256_img[32]; - bl_mtd_handle_t handle; - //init_ota(startaddr); - - -#define OTA_PROGRAM_SIZE (512) - int ota_header_found, use_xz; - ota_header_t* ota_header = 0; - - ret = bl_mtd_open(BL_MTD_PARTITION_NAME_FW_DEFAULT, &handle, BL_MTD_OPEN_FLAG_BACKUP); - if(ret) - { - return http_rest_error(request, -20, "Open Default FW partition failed"); - } - - recv_buffer = pvPortMalloc(OTA_PROGRAM_SIZE); - - unsigned int buffer_offset, flash_offset, ota_addr; - uint32_t bin_size, part_size, running_size; - uint8_t activeID; - HALPartition_Entry_Config ptEntry; - - activeID = hal_boot2_get_active_partition(); - - printf("Starting OTA test. OTA bin addr is %p, incoming len %i\r\n", recv_buffer, writelen); - - printf("[OTA] [TEST] activeID is %u\r\n", activeID); - - if(hal_boot2_get_active_entries(BOOT2_PARTITION_TYPE_FW, &ptEntry)) - { - printf("PtTable_Get_Active_Entries fail\r\n"); - vPortFree(recv_buffer); - bl_mtd_close(handle); - return http_rest_error(request, -20, "PtTable_Get_Active_Entries fail"); - } - ota_addr = ptEntry.Address[!ptEntry.activeIndex]; - bin_size = ptEntry.maxLen[!ptEntry.activeIndex]; - part_size = ptEntry.maxLen[!ptEntry.activeIndex]; - running_size = ptEntry.maxLen[ptEntry.activeIndex]; - (void)part_size; - /*XXX if you use bin_size is product env, you may want to set bin_size to the actual - * OTA BIN size, and also you need to splilt XIP_SFlash_Erase_With_Lock into - * serveral pieces. Partition size vs bin_size check is also needed - */ - printf("Starting OTA test. OTA size is %lu\r\n", bin_size); - - printf("[OTA] [TEST] activeIndex is %u, use OTA address=%08x\r\n", ptEntry.activeIndex, (unsigned int)ota_addr); - - printf("[OTA] [TEST] Erase flash with size %lu...", bin_size); - hal_update_mfg_ptable(); - - //Erase in chunks, because erasing everything at once is slow and causes issues with http connection - uint32_t erase_offset = 0; - uint32_t erase_len = 0; - while(erase_offset < bin_size) - { - erase_len = bin_size - erase_offset; - if(erase_len > 0x10000) - { - erase_len = 0x10000; //Erase in 64kb chunks - } - bl_mtd_erase(handle, erase_offset, erase_len); - printf("[OTA] Erased: %lu / %lu \r\n", erase_offset, erase_len); - erase_offset += erase_len; - rtos_delay_milliseconds(100); - } - printf("[OTA] Done\r\n"); - - if(request->contentLength >= 0) - { - towrite = request->contentLength; - } - - // get header - // recv_buffer - //buffer_offset = 0; - //do { - // int take_len; - - // take_len = OTA_PROGRAM_SIZE - buffer_offset; - - // memcpy(recv_buffer + buffer_offset, writebuf, writelen); - // buffer_offset += writelen; - - - // if (towrite > 0) { - // writebuf = request->received; - // writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); - // if (writelen < 0) { - // ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); - // } - // } - //} while(true) - - buffer_offset = 0; - flash_offset = 0; - ota_header = 0; - use_xz = 0; - - utils_sha256_init(&ctx); - utils_sha256_starts(&ctx); - memset(sha256_result, 0, sizeof(sha256_result)); - do - { - char* useBuf = writebuf; - int useLen = writelen; - - if(ota_header == 0) - { - int take_len; - - // how much left for header? - take_len = OTA_PROGRAM_SIZE - buffer_offset; - // clamp to available len - if(take_len > useLen) - take_len = useLen; - printf("Header takes %i. ", take_len); - memcpy(recv_buffer + buffer_offset, writebuf, take_len); - buffer_offset += take_len; - useBuf = writebuf + take_len; - useLen = writelen - take_len; - - if(buffer_offset >= OTA_PROGRAM_SIZE) - { - ota_header = (ota_header_t*)recv_buffer; - if(strncmp((const char*)ota_header, "BL60X_OTA", 9)) - { - return http_rest_error(request, -20, "Invalid header ident"); - } - } - } - - - if(ota_header && useLen) - { - - - if(flash_offset + useLen >= part_size) - { - return http_rest_error(request, -20, "Too large bin"); - } - if(ota_header->u.s.unpacked_len != 0xFFFFFFFF && running_size < ota_header->u.s.unpacked_len) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Unpacked OTA image size (%u) is bigger than running partition size (%u)", ota_header->u.s.unpacked_len, running_size); - return http_rest_error(request, -20, ""); - } - //ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d bytes to write", writelen); - //add_otadata((unsigned char*)writebuf, writelen); - - printf("Flash takes %i. ", useLen); - utils_sha256_update(&ctx, (byte*)useBuf, useLen); - bl_mtd_write(handle, flash_offset, useLen, (byte*)useBuf); - flash_offset += useLen; - } - - total += writelen; - startaddr += writelen; - towrite -= writelen; - - - if(towrite > 0) - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); - if(writelen < 0) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); - } - } - } while((towrite > 0) && (writelen >= 0)); - - if(ota_header == 0) - { - return http_rest_error(request, -20, "No header found"); - } - utils_sha256_finish(&ctx, sha256_result); - puts("\r\nCalculated SHA256 Checksum:"); - for(i = 0; i < sizeof(sha256_result); i++) - { - printf("%02X", sha256_result[i]); - } - puts("\r\nHeader SHA256 Checksum:"); - for(i = 0; i < sizeof(sha256_result); i++) - { - printf("%02X", ota_header->u.s.sha256[i]); - } - if(memcmp(ota_header->u.s.sha256, sha256_result, sizeof(sha256_img))) - { - /*Error found*/ - return http_rest_error(request, -20, "SHA256 NOT Correct"); - } - printf("[OTA] [TCP] prepare OTA partition info\r\n"); - ptEntry.len = total; - printf("[OTA] [TCP] Update PARTITION, partition len is %lu\r\n", ptEntry.len); - hal_boot2_update_ptable(&ptEntry); - printf("[OTA] [TCP] Rebooting\r\n"); - //close_ota(); - vPortFree(recv_buffer); - utils_sha256_free(&ctx); - bl_mtd_close(handle); - -#elif PLATFORM_LN882H - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Ota start!\r\n"); - if(LN_TRUE != ota_persistent_start()) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Ota start error, exit...\r\n"); - return 0; - } - - if(request->contentLength >= 0) - { - towrite = request->contentLength; - } - - do - { - //ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d bytes to write", writelen); - - if(LN_TRUE != ota_persistent_write(writebuf, writelen)) - { - // ADDLOG_DEBUG(LOG_FEATURE_OTA, "ota write err.\r\n"); - return -1; - } - - rtos_delay_milliseconds(10); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); - total += writelen; - startaddr += writelen; - towrite -= writelen; - if(towrite > 0) - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); - if(writelen < 0) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); - } - } - } while((towrite > 0) && (writelen >= 0)); - - ota_persistent_finish(); - is_ready_to_verify = LN_TRUE; - ADDLOG_DEBUG(LOG_FEATURE_OTA, "cb info: recv %d finished, no more data to deal with.\r\n", towrite); - - - ADDLOG_DEBUG(LOG_FEATURE_OTA, "http client job done, exit...\r\n"); - if(LN_TRUE == is_precheck_ok) - { - if((LN_TRUE == is_ready_to_verify) && (LN_TRUE == ota_verify_download())) - { - update_ota_state(); - //ln_chip_reboot(); - } - else - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Veri bad\r\n"); - } - } - else - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Precheck bad\r\n"); - } - - -#elif PLATFORM_ESPIDF || PLATFORM_ESP8266 - - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Ota start!\r\n"); - esp_err_t err; - esp_ota_handle_t update_handle = 0; - const esp_partition_t* update_partition = NULL; - const esp_partition_t* running = esp_ota_get_running_partition(); - update_partition = esp_ota_get_next_update_partition(NULL); - if(request->contentLength >= 0) - { - towrite = request->contentLength; - } - - esp_wifi_set_ps(WIFI_PS_NONE); - bool image_header_was_checked = false; - do - { - if(image_header_was_checked == false) - { - esp_app_desc_t new_app_info; - if(towrite > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) - { - memcpy(&new_app_info, &writebuf[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t)); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "New firmware version: %s", new_app_info.version); - - esp_app_desc_t running_app_info; - if(esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Running firmware version: %s", running_app_info.version); - } - - image_header_was_checked = true; - - err = esp_ota_begin(update_partition, OTA_WITH_SEQUENTIAL_WRITES, &update_handle); - if(err != ESP_OK) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "esp_ota_begin failed (%s)", esp_err_to_name(err)); - esp_ota_abort(update_handle); - return -1; - } - ADDLOG_DEBUG(LOG_FEATURE_OTA, "esp_ota_begin succeeded"); - } - else - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "received package is not fit len"); - esp_ota_abort(update_handle); - return -1; - } - } - err = esp_ota_write(update_handle, (const void*)writebuf, writelen); - if(err != ESP_OK) - { - esp_ota_abort(update_handle); - return -1; - } - - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); - total += writelen; - startaddr += writelen; - towrite -= writelen; - - if(towrite > 0) - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); - if(writelen < 0) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); - } - } - } while((towrite > 0) && (writelen >= 0)); - - ADDLOG_INFO(LOG_FEATURE_OTA, "OTA in progress: 100%%, total Write binary data length: %d", total); - - err = esp_ota_end(update_handle); - if(err != ESP_OK) - { - if(err == ESP_ERR_OTA_VALIDATE_FAILED) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Image validation failed, image is corrupted"); - } - else - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "esp_ota_end failed (%s)!", esp_err_to_name(err)); - } - return -1; - } - err = esp_ota_set_boot_partition(update_partition); - if(err != ESP_OK) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err)); - return -1; - } - -#elif PLATFORM_RTL87X0C - - uint32_t NewFWLen = 0, NewFWAddr = 0; - uint32_t address = 0; - uint32_t curr_fw_idx = 0; - uint32_t flash_checksum = 0; - uint32_t targetFWaddr; - uint32_t currentFWaddr; - uint32_t fw1_sn; - uint32_t fw2_sn; - _file_checksum file_checksum; - file_checksum.u = 0; - unsigned char sig_backup[32]; - int ret = 1; - - if(request->contentLength >= 0) - { - towrite = request->contentLength; - } - NewFWAddr = sys_update_ota_prepare_addr(); - if(NewFWAddr == -1) - { - ret = -1; - goto update_ota_exit; - } - get_fw_info(&targetFWaddr, ¤tFWaddr, &fw1_sn, &fw2_sn); - ADDLOG_INFO(LOG_FEATURE_OTA, "Current FW addr: 0x%08X, target FW addr: 0x%08X, fw1 sn: %u, fw2 sn: %u", currentFWaddr, targetFWaddr, fw1_sn, fw2_sn); - curr_fw_idx = sys_update_ota_get_curr_fw_idx(); - ADDLOG_INFO(LOG_FEATURE_OTA, "Current firmware index is %d", curr_fw_idx); - int reserase = update_ota_erase_upg_region(towrite, 0, NewFWAddr); - NewFWLen = towrite; - if(reserase == -1) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Erase failed"); - ret = -1; - goto update_ota_exit; - } - if(NewFWAddr != ~0x0) - { - address = NewFWAddr; - ADDLOG_INFO(LOG_FEATURE_OTA, "Start to read data %i bytes", NewFWLen); - } - do - { - // back up signature and only write it to flash till the end of OTA - if(startaddr < 32) - { - memcpy(sig_backup + startaddr, writebuf, (startaddr + writelen > 32 ? (32 - startaddr) : writelen)); - memset(writebuf, 0xFF, (startaddr + writelen > 32 ? (32 - startaddr) : writelen)); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "sig_backup for% d bytes from index% d", (startaddr + writelen > 32 ? (32 - startaddr) : writelen), startaddr); - } - - device_mutex_lock(RT_DEV_LOCK_FLASH); - if(flash_burst_write(&flash_ota, address + startaddr, writelen, (uint8_t*)writebuf) < 0) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Write stream failed"); - device_mutex_unlock(RT_DEV_LOCK_FLASH); - ret = -1; - goto update_ota_exit; - } - device_mutex_unlock(RT_DEV_LOCK_FLASH); - - rtos_delay_milliseconds(10); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); - total += writelen; - startaddr += writelen; - towrite -= writelen; - - // checksum attached at file end - if(startaddr + writelen > NewFWLen - 4) - { - file_checksum.c[0] = writebuf[writelen - 4]; - file_checksum.c[1] = writebuf[writelen - 3]; - file_checksum.c[2] = writebuf[writelen - 2]; - file_checksum.c[3] = writebuf[writelen - 1]; - } - if(towrite > 0) - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); - if(writelen < 0) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); - } - } - } while((towrite > 0) && (writelen >= 0)); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written, verifying checksum", total); - uint8_t* buf = (uint8_t*)os_malloc(2048); - memset(buf, 0, 2048); - // read flash data back and calculate checksum - for(int i = 0; i < NewFWLen; i += 2048) - { - int k; - int rlen = (startaddr - 4 - i) > 2048 ? 2048 : (startaddr - 4 - i); - device_mutex_lock(RT_DEV_LOCK_FLASH); - flash_stream_read(&flash_ota, NewFWAddr + i, rlen, buf); - device_mutex_unlock(RT_DEV_LOCK_FLASH); - for(k = 0; k < rlen; k++) - { - if(i + k < 32) - { - flash_checksum += sig_backup[i + k]; - } - else - { - flash_checksum += buf[k]; - } - } - } - - ADDLOG_INFO(LOG_FEATURE_OTA, "flash checksum 0x%8x attached checksum 0x%8x", flash_checksum, file_checksum.u); - - if(file_checksum.u != flash_checksum) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "The checksum is wrong!"); - ret = -1; - goto update_ota_exit; - } - ret = update_ota_signature(sig_backup, NewFWAddr); - if(ret == -1) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Update signature fail"); - goto update_ota_exit; - } -update_ota_exit: - if(ret != -1) - { - device_mutex_lock(RT_DEV_LOCK_FLASH); - flash_write_word(&flash, targetFWaddr, 4294967295); - flash_write_word(&flash, currentFWaddr, 0); - device_mutex_unlock(RT_DEV_LOCK_FLASH); - - sys_disable_fast_boot(); - if(buf) free(buf); - } - else - { - if(buf) free(buf); - return http_rest_error(request, ret, "error"); - } - -#elif PLATFORM_RTL8710B - - int NewImg2BlkSize = 0; - uint32_t NewFWLen = 0, NewFWAddr = 0; - uint32_t address = 0; - uint32_t flash_checksum = 0; - uint32_t ota2_addr = OTA2_ADDR; - union { uint32_t u; unsigned char c[4]; } file_checksum; - unsigned char sig_backup[32]; - int ret = 1; - char* hbuf = NULL; - bool foundhdr = false; - - if(request->contentLength > 0) - { - towrite = request->contentLength; - } - else - { - ret = -1; - ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); - goto update_ota_exit; - } - int ota1_len = 0, ota2_len = 0; - char* msg = "Incorrect amount of bytes received: %i, required: %i"; - - writebuf = request->received; - writelen = recv(request->fd, &ota1_len, sizeof(ota1_len), 0); - if(writelen != sizeof(ota1_len)) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, msg, writelen, sizeof(ota1_len)); - ret = -1; - goto update_ota_exit; - } - - writebuf = request->received; - writelen = recv(request->fd, &ota2_len, sizeof(ota2_len), 0); - if(writelen != sizeof(ota2_len)) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, msg, writelen, sizeof(ota2_len)); - ret = -1; - goto update_ota_exit; - } - ADDLOG_INFO(LOG_FEATURE_OTA, "OTA1 len: %u, OTA2 len: %u", ota1_len, ota2_len); - - if(ota1_len <= 0 || ota2_len <= 0) - { - ret = -1; - goto update_ota_exit; - } - towrite -= 8; - - if(current_fw_idx == OTA_INDEX_1) - { - towrite = ota2_len; - // skip ota1 - int toskip = ota1_len; - do - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, request->receivedLenmax < toskip ? request->receivedLenmax : toskip, 0); - ADDLOG_EXTRADEBUG(LOG_FEATURE_OTA, "Skipping %i at %i", writelen, total); - total += writelen; - toskip -= writelen; - } while((toskip > 0) && (writelen >= 0)); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Skipped %i bytes, towrite: %i", total, towrite); - } - else - { - towrite = ota1_len; - } - - writelen = 0; - total = 0; - NewFWAddr = update_ota_prepare_addr(); - ADDLOG_INFO(LOG_FEATURE_OTA, "OTA address: %#010x, len: %u", NewFWAddr - SPI_FLASH_BASE, towrite); - if(NewFWAddr == -1 || NewFWAddr == 0xFFFFFFFF) - { - ret = -1; - ADDLOG_ERROR(LOG_FEATURE_OTA, "Wrong OTA address:", NewFWAddr); - goto update_ota_exit; - } - NewFWLen = towrite; - if(NewFWAddr == OTA1_ADDR) - { - if(NewFWLen > (OTA2_ADDR - OTA1_ADDR)) - { - // firmware size too large - ret = -1; - ADDLOG_ERROR(LOG_FEATURE_OTA, "image size should not cross OTA2"); - goto update_ota_exit; - } - } - else if(NewFWAddr == OTA2_ADDR) - { - if(NewFWLen > (0x195000 + SPI_FLASH_BASE - OTA2_ADDR)) - { - ret = -1; - ADDLOG_ERROR(LOG_FEATURE_OTA, "image size crosses OTA2 boundary"); - goto update_ota_exit; - } - } - else if(NewFWAddr == 0xFFFFFFFF) - { - ret = -1; - ADDLOG_ERROR(LOG_FEATURE_OTA, "update address is invalid"); - goto update_ota_exit; - } - address = NewFWAddr - SPI_FLASH_BASE; - NewImg2BlkSize = ((NewFWLen - 1) / 4096) + 1; - device_mutex_lock(RT_DEV_LOCK_FLASH); - for(int i = 0; i < NewImg2BlkSize; i++) - { - flash_erase_sector(&flash, address + i * 4096); - } - device_mutex_unlock(RT_DEV_LOCK_FLASH); - OTF_Mask(1, (address), NewImg2BlkSize, 1); - - do - { - if(startaddr < 32) - { - memcpy(sig_backup + startaddr, writebuf, (startaddr + writelen > 32 ? (32 - startaddr) : writelen)); - memset(writebuf, 0xFF, (startaddr + writelen > 32 ? (32 - startaddr) : writelen)); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "sig_backup for% d bytes from index% d", (startaddr + writelen > 32 ? (32 - startaddr) : writelen), startaddr); - } - device_mutex_lock(RT_DEV_LOCK_FLASH); - if(flash_burst_write(&flash, address + startaddr, writelen, (uint8_t*)writebuf) < 0) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Write stream failed"); - device_mutex_unlock(RT_DEV_LOCK_FLASH); - ret = -1; - goto update_ota_exit; - } - device_mutex_unlock(RT_DEV_LOCK_FLASH); - - rtos_delay_milliseconds(10); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); - total += writelen; - startaddr += writelen; - towrite -= writelen; - - if(startaddr + writelen > NewFWLen - 4) - { - file_checksum.c[0] = writebuf[writelen - 4]; - file_checksum.c[1] = writebuf[writelen - 3]; - file_checksum.c[2] = writebuf[writelen - 2]; - file_checksum.c[3] = writebuf[writelen - 1]; - } - - if(towrite > 0) - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, request->receivedLenmax < towrite ? request->receivedLenmax : towrite, 0); - if(writelen < 0) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); - } - } - } while((towrite > 0) && (writelen >= 0)); - - uint8_t* buf = (uint8_t*)os_malloc(2048); - memset(buf, 0, 2048); - for(int i = 0; i < NewFWLen; i += 2048) - { - int k; - int rlen = (startaddr - 4 - i) > 2048 ? 2048 : (startaddr - 4 - i); - device_mutex_lock(RT_DEV_LOCK_FLASH); - flash_stream_read(&flash, address + i, rlen, buf); - device_mutex_unlock(RT_DEV_LOCK_FLASH); - for(k = 0; k < rlen; k++) - { - if(i + k < 32) - { - flash_checksum += sig_backup[i + k]; - } - else - { - flash_checksum += buf[k]; - } - } - } - ADDLOG_INFO(LOG_FEATURE_OTA, "Update file size = %d flash checksum 0x%8x attached checksum 0x%8x", NewFWLen, flash_checksum, file_checksum.u); - OTF_Mask(1, (address), NewImg2BlkSize, 0); - if(file_checksum.u == flash_checksum) - { - ADDLOG_INFO(LOG_FEATURE_OTA, "Update OTA success!"); - - ret = 0; - } - else - { - /*if checksum error, clear the signature zone which has been - written in flash in case of boot from the wrong firmware*/ - device_mutex_lock(RT_DEV_LOCK_FLASH); - flash_erase_sector(&flash, address); - device_mutex_unlock(RT_DEV_LOCK_FLASH); - ret = -1; - } - // make it to be similar to rtl8720c - write signature only after success - to prevent boot failure if something goes wrong - if(flash_burst_write(&flash, address + 16, 16, sig_backup + 16) < 0) - { - ret = -1; - } - else - { - if(flash_burst_write(&flash, address, 16, sig_backup) < 0) - { - ret = -1; - } - } - if(current_fw_idx != OTA_INDEX_1) - { - // receive file fully - int toskip = ota2_len; - do - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, request->receivedLenmax < toskip ? request->receivedLenmax : toskip, 0); - total += writelen; - toskip -= writelen; - } while((toskip > 0) && (writelen >= 0)); - } -update_ota_exit: - if(ret != -1) - { - device_mutex_lock(RT_DEV_LOCK_FLASH); - if(current_fw_idx == OTA_INDEX_1) - { - OTA_Change(OTA_INDEX_2); - //ota_write_ota2_addr(OTA2_ADDR); - } - else - { - OTA_Change(OTA_INDEX_1); - //ota_write_ota2_addr(0xffffffff); - } - device_mutex_unlock(RT_DEV_LOCK_FLASH); - if(buf) free(buf); - } - else - { - if(buf) free(buf); - ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed"); - return http_rest_error(request, ret, "error"); - } - -#elif PLATFORM_RTL8710A - - uint32_t NewFWLen = 0, NewFWAddr = 0; - uint32_t address = 0; - uint32_t flash_checksum = 0; - union { uint32_t u; unsigned char c[4]; } file_checksum; - int ret = 0; - - if(request->contentLength > 0) - { - towrite = request->contentLength; - } - else - { - ret = -1; - ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); - goto update_ota_exit; - } - - NewFWAddr = update_ota_prepare_addr(); - if(NewFWAddr == -1) - { - goto update_ota_exit; - } - - int reserase = update_ota_erase_upg_region(towrite, 0, NewFWAddr); - NewFWLen = towrite; - if(reserase == -1) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Erase failed"); - ret = -1; - goto update_ota_exit; - } - - address = NewFWAddr; - ADDLOG_INFO(LOG_FEATURE_OTA, "Start to read data %i bytes, flash address: 0x%8x", NewFWLen, address); - - do - { - device_mutex_lock(RT_DEV_LOCK_FLASH); - if(flash_stream_write(&flash, address + startaddr, writelen, (uint8_t*)writebuf) < 0) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Write stream failed"); - device_mutex_unlock(RT_DEV_LOCK_FLASH); - ret = -1; - goto update_ota_exit; - } - device_mutex_unlock(RT_DEV_LOCK_FLASH); - - rtos_delay_milliseconds(10); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); - total += writelen; - startaddr += writelen; - towrite -= writelen; - - // checksum attached at file end - if(startaddr + writelen > NewFWLen - 4) - { - file_checksum.c[0] = writebuf[writelen - 4]; - file_checksum.c[1] = writebuf[writelen - 3]; - file_checksum.c[2] = writebuf[writelen - 2]; - file_checksum.c[3] = writebuf[writelen - 1]; - } - if(towrite > 0) - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); - if(writelen < 0) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); - } - } - } while((towrite > 0) && (writelen >= 0)); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written, verifying checksum %u", total, flash_checksum); - uint8_t* buf = (uint8_t*)os_malloc(512); - memset(buf, 0, 512); - // read flash data back and calculate checksum - for(int i = 0; i < NewFWLen; i += 512) - { - int k; - int rlen = (NewFWLen - 4 - i) > 512 ? 512 : (NewFWLen - 4 - i); - device_mutex_lock(RT_DEV_LOCK_FLASH); - flash_stream_read(&flash, NewFWAddr + i, rlen, buf); - device_mutex_unlock(RT_DEV_LOCK_FLASH); - for(k = 0; k < rlen; k++) - { - flash_checksum += buf[k]; - } - } - - ADDLOG_INFO(LOG_FEATURE_OTA, "flash checksum 0x%8x attached checksum 0x%8x", flash_checksum, file_checksum.u); - delay_ms(50); - - ret = update_ota_checksum(&file_checksum, flash_checksum, NewFWAddr); - if(ret == -1) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "The checksum is wrong!"); - goto update_ota_exit; - } - -update_ota_exit: - if(ret != -1) - { - //device_mutex_lock(RT_DEV_LOCK_FLASH); - //device_mutex_unlock(RT_DEV_LOCK_FLASH); - if(buf) free(buf); - } - else - { - if(buf) free(buf); - ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed"); - return http_rest_error(request, ret, "error"); - } - -#elif PLATFORM_RTL8720D - - int ret = -1; - uint32_t ota_target_index = OTA_INDEX_2; - update_file_hdr* pOtaFileHdr; - update_file_img_hdr* pOtaFileImgHdr; - update_ota_target_hdr OtaTargetHdr; - uint32_t ImageCnt, TempLen; - update_dw_info DownloadInfo[MAX_IMG_NUM]; - - int size = 0; - int read_bytes; - int read_bytes_buf; - uint8_t* buf; - uint32_t OtaFg = 0; - uint32_t IncFg = 0; - int RemainBytes = 0; - uint32_t SigCnt = 0; - uint32_t TempCnt = 0; - uint8_t* signature; - uint32_t sector_cnt = 0; - - if(request->contentLength > 0) - { - towrite = request->contentLength; - } - else - { - ret = -1; - ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); - goto update_ota_exit; - } - - if(flash_size_8720 == 2) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Only 2MB flash detected - OTA is not supported"); - ret = -1; - goto update_ota_exit; - } - - memset((uint8_t*)&OtaTargetHdr, 0, sizeof(update_ota_target_hdr)); - - DBG_INFO_MSG_OFF(MODULE_FLASH); - - ADDLOG_INFO(LOG_FEATURE_OTA, "Current firmware index is %d", current_fw_idx + 1); - if(current_fw_idx == OTA_INDEX_1) - { - ota_target_index = OTA_INDEX_2; - } - else - { - ota_target_index = OTA_INDEX_1; - } - - // get ota header - writebuf = request->received; - writelen = recv(request->fd, writebuf, 16, 0); - if(writelen != 16) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "failed to recv file header"); - ret = -1; - goto update_ota_exit; - } - - pOtaFileHdr = (update_file_hdr*)writebuf; - pOtaFileImgHdr = (update_file_img_hdr*)(writebuf + 8); - - OtaTargetHdr.FileHdr.FwVer = pOtaFileHdr->FwVer; - OtaTargetHdr.FileHdr.HdrNum = pOtaFileHdr->HdrNum; - - writelen = recv(request->fd, writebuf + 16, (pOtaFileHdr->HdrNum * pOtaFileImgHdr->ImgHdrLen) - 8, 0); - writelen = (pOtaFileHdr->HdrNum * pOtaFileImgHdr->ImgHdrLen) + 8; - - // verify ota header - if(!get_ota_tartget_header((uint8_t*)writebuf, writelen, &OtaTargetHdr, ota_target_index)) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Get OTA header failed"); - goto update_ota_exit; - } - - //ADDLOG_INFO(LOG_FEATURE_OTA, "Erasing..."); - for(int i = 0; i < OtaTargetHdr.ValidImgCnt; i++) - { - ADDLOG_INFO(LOG_FEATURE_OTA, "Target addr:0x%08x, img len: %i", OtaTargetHdr.FileImgHdr[i].FlashAddr, OtaTargetHdr.FileImgHdr[i].ImgLen); - if(OtaTargetHdr.FileImgHdr[i].ImgLen >= 0x1A8000) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Img%i too big, skipping", i); - OtaTargetHdr.FileImgHdr[i].ImgLen = 0; - continue; - } - //watchdog_stop(); - //erase_ota_target_flash(OtaTargetHdr.FileImgHdr[i].FlashAddr, OtaTargetHdr.FileImgHdr[i].ImgLen); - //watchdog_start(); - } - - memset((uint8_t*)DownloadInfo, 0, MAX_IMG_NUM * sizeof(update_dw_info)); - - ImageCnt = OtaTargetHdr.ValidImgCnt; - for(uint32_t i = 0; i < ImageCnt; i++) - { - if(OtaTargetHdr.FileImgHdr[i].ImgLen == 0) - { - DownloadInfo[i].ImageLen = 0; - continue; - } - /* get OTA image and Write New Image to flash, skip the signature, - not write signature first for power down protection*/ - DownloadInfo[i].ImgId = OTA_IMAG; - DownloadInfo[i].FlashAddr = OtaTargetHdr.FileImgHdr[i].FlashAddr - SPI_FLASH_BASE + 8; - DownloadInfo[i].ImageLen = OtaTargetHdr.FileImgHdr[i].ImgLen - 8; /*skip the signature*/ - DownloadInfo[i].ImgOffset = OtaTargetHdr.FileImgHdr[i].Offset; - } - - /*initialize the reveiving counter*/ - TempLen = (OtaTargetHdr.FileHdr.HdrNum * OtaTargetHdr.FileImgHdr[0].ImgHdrLen) + sizeof(update_file_hdr); - - for(uint32_t i = 0; i < ImageCnt; i++) - { - if(DownloadInfo[i].ImageLen == 0) continue; - /*the next image length*/ - RemainBytes = DownloadInfo[i].ImageLen; - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Remain: %i", RemainBytes); - signature = &(OtaTargetHdr.Sign[i][0]); - device_mutex_lock(RT_DEV_LOCK_FLASH); - FLASH_EraseXIP(EraseSector, DownloadInfo[i].FlashAddr - SPI_FLASH_BASE); - device_mutex_unlock(RT_DEV_LOCK_FLASH); - - /*download the new firmware from server*/ - while(RemainBytes > 0) - { - buf = (uint8_t*)request->received; - if(IncFg == 1) - { - IncFg = 0; - read_bytes = read_bytes_buf; - } - else - { - memset(buf, 0, request->receivedLenmax); - read_bytes = recv(request->fd, buf, request->receivedLenmax, 0); - if(read_bytes == 0) - { - break; // Read end - } - if(read_bytes < 0) - { - //OtaImgSize = -1; - //printf("\n\r[%s] Read socket failed", __FUNCTION__); - //ret = -1; - //goto update_ota_exit; - break; - } - read_bytes_buf = read_bytes; - TempLen += read_bytes; - } - - if(TempLen > DownloadInfo[i].ImgOffset) - { - if(!OtaFg) - { - /*reach the desired image, the first packet process*/ - OtaFg = 1; - TempCnt = TempLen - DownloadInfo[i].ImgOffset; - if(TempCnt < 8) - { - SigCnt = TempCnt; - } - else - { - SigCnt = 8; - } - - memcpy(signature, buf + read_bytes - TempCnt, SigCnt); - - if((SigCnt < 8) || (TempCnt - 8 == 0)) - { - continue; - } - - buf = buf + (read_bytes - TempCnt + 8); - read_bytes = TempCnt - 8; - } - else - { - /*normal packet process*/ - if(SigCnt < 8) - { - if(read_bytes < (int)(8 - SigCnt)) - { - memcpy(signature + SigCnt, buf, read_bytes); - SigCnt += read_bytes; - continue; - } - else - { - memcpy(signature + SigCnt, buf, (8 - SigCnt)); - buf = buf + (8 - SigCnt); - read_bytes -= (8 - SigCnt); - SigCnt = 8; - if(!read_bytes) - { - continue; - } - } - } - } - - RemainBytes -= read_bytes; - if(RemainBytes < 0) - { - read_bytes = read_bytes - (-RemainBytes); - } - - device_mutex_lock(RT_DEV_LOCK_FLASH); - if(DownloadInfo[i].FlashAddr + size >= DownloadInfo[i].FlashAddr + sector_cnt * 4096) - { - sector_cnt++; - FLASH_EraseXIP(EraseSector, DownloadInfo[i].FlashAddr - SPI_FLASH_BASE + sector_cnt * 4096); - } - if(ota_writestream_user(DownloadInfo[i].FlashAddr + size, read_bytes, buf) < 0) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Writing failed"); - device_mutex_unlock(RT_DEV_LOCK_FLASH); - ret = -1; - goto update_ota_exit; - } - device_mutex_unlock(RT_DEV_LOCK_FLASH); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Written %i bytes at 0x%08x", read_bytes, DownloadInfo[i].FlashAddr + size); - rtos_delay_milliseconds(5); - size += read_bytes; - } - } - - if((uint32_t)size != (OtaTargetHdr.FileImgHdr[i].ImgLen - 8)) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Received size != ota size"); - ret = -1; - goto update_ota_exit; - } - - /*update flag status*/ - size = 0; - OtaFg = 0; - IncFg = 1; - } - - if(verify_ota_checksum(&OtaTargetHdr)) - { - if(!change_ota_signature(&OtaTargetHdr, ota_target_index)) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Change signature failed"); - ret = -1; - goto update_ota_exit; - } - ret = 0; - } - -update_ota_exit: - if(ret != -1) - { - ADDLOG_INFO(LOG_FEATURE_OTA, "OTA is successful"); - total = RemainBytes; - } - else - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed"); - return http_rest_error(request, ret, "error"); - } - -#elif PLATFORM_ECR6600 || PLATFORM_TR6260 - -#if PLATFORM_TR6260 -#define OTA_INIT otaHal_init -#define OTA_WRITE otaHal_write -#define OTA_DONE(x) otaHal_done() -#else -#define OTA_INIT ota_init -#define OTA_WRITE ota_write -#define OTA_DONE(x) ota_done(x) -#endif - int ret = 0; - - if(request->contentLength > 0) - { - towrite = request->contentLength; - } - else - { - ret = -1; - ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); - goto update_ota_exit; - } - - if(OTA_INIT() != 0) - { - ret = -1; - goto update_ota_exit; - } - - do - { - if(OTA_WRITE((unsigned char*)writebuf, writelen) != 0) - { - ret = -1; - goto update_ota_exit; - } - delay_ms(10); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); - total += writelen; - startaddr += writelen; - towrite -= writelen; - if(towrite > 0) - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); - if(writelen < 0) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); - ret = -1; - } - } - } while((towrite > 0) && (writelen >= 0)); - -update_ota_exit: - if(ret != -1) - { - ADDLOG_INFO(LOG_FEATURE_OTA, "OTA is successful"); - OTA_DONE(0); - } - else - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed. Reboot to retry"); - return http_rest_error(request, ret, "error"); - } - -#elif PLATFORM_XRADIO - -bool recvfp = true; - -ota_status_t ota_update_rest_init(void* url) -{ - return OTA_STATUS_OK; -} -ota_status_t ota_update_rest_get(uint8_t* buf, uint32_t buf_size, uint32_t* recv_size, uint8_t* eof_flag) -{ - if(recvfp) - { - //free(buf); - //recvfp = false; - //buf = writebuf; - //*recv_size = writelen; - //return OTA_STATUS_OK; - int bsize = (writelen > buf_size ? buf_size : writelen); - memcpy(buf, writebuf + startaddr, bsize); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", bsize, startaddr); - startaddr += bsize; - *recv_size = bsize; - *eof_flag = 0; - total += bsize; - towrite -= bsize; - writelen -= bsize; - recvfp = writelen > 0; - return OTA_STATUS_OK; - } - if(towrite > 0) - { - *recv_size = writelen = recv(request->fd, buf, (request->receivedLenmax > buf_size ? buf_size : request->receivedLenmax), 0); - //*recv_size = writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); - if(writelen < 0) - { - ADDLOG_INFO(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); - *eof_flag = 1; - *recv_size = 0; - return OTA_STATUS_OK; - //return OTA_STATUS_ERROR; - } - } - total += writelen; - towrite -= writelen; - - if((towrite > 0) && (writelen >= 0)) - { - *eof_flag = 0; - rtos_delay_milliseconds(10); - return OTA_STATUS_OK; - } - *eof_flag = 1; - return OTA_STATUS_OK; -} - - int ret = 0; - uint32_t* verify_value; - ota_verify_t verify_type; - ota_verify_data_t verify_data; - - if(request->contentLength > 0) - { - towrite = request->contentLength; - } - else - { - ret = -1; - ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); - goto update_ota_exit; - } - - ota_init(); - - if(ota_update_image(NULL, ota_update_rest_init, ota_update_rest_get) != OTA_STATUS_OK) - { - ret = -1; - ADDLOG_ERROR(LOG_FEATURE_OTA, "ota_update_image failed"); - goto update_ota_exit; - } - - if(ota_get_verify_data(&verify_data) != OTA_STATUS_OK) - { - ADDLOG_INFO(LOG_FEATURE_OTA, "ota_get_verify_data not ok, OTA_VERIFY_NONE"); - verify_type = OTA_VERIFY_NONE; - verify_value = NULL; - } - else - { - verify_type = verify_data.ov_type; - ADDLOG_INFO(LOG_FEATURE_OTA, "ota_get_verify_data ok"); - verify_value = (uint32_t*)(verify_data.ov_data); - } - - if(ota_verify_image(verify_type, verify_value) != OTA_STATUS_OK) - { - ret = -1; - ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA verify image failed"); - goto update_ota_exit; - } - -update_ota_exit: - if(ret != -1) - { - ADDLOG_INFO(LOG_FEATURE_OTA, "OTA is successful"); - } - else - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed."); - return http_rest_error(request, ret, "error"); - } -#elif PLATFORM_REALTEK_NEW - - // Bad implementation. While it somewhat works, it is not recommended to use. HTTP OTA is preferable. - int ret = 0; - - if(request->contentLength > 0) - { - towrite = request->contentLength; - } - else - { - ret = -1; - ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0"); - goto update_ota_exit; - } - - flash_get_layout_info(IMG_BOOT, &IMG_ADDR[OTA_IMGID_BOOT][OTA_INDEX_1], NULL); - flash_get_layout_info(IMG_BOOT_OTA2, &IMG_ADDR[OTA_IMGID_BOOT][OTA_INDEX_2], NULL); - flash_get_layout_info(IMG_APP_OTA1, &IMG_ADDR[OTA_IMGID_APP][OTA_INDEX_1], NULL); - flash_get_layout_info(IMG_APP_OTA2, &IMG_ADDR[OTA_IMGID_APP][OTA_INDEX_2], NULL); - - ota_context ctx; - memset(&ctx, 0, sizeof(ota_context)); - ctx.otactrl = malloc(sizeof(update_ota_ctrl_info)); - memset(ctx.otactrl, 0, sizeof(update_ota_ctrl_info)); - - ctx.otaTargetHdr = malloc(sizeof(update_ota_target_hdr)); - memset(ctx.otaTargetHdr, 0, sizeof(update_ota_target_hdr)); - writebuf = request->received; - if(!writelen) writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); - // for some reason we receive data along with HTTP header. Skip it. - int skip = 0; - for(; skip < writelen - 5; skip++) - { - if(*(uint32_t*)&writebuf[skip] == 0xFFFFFFFF && writebuf[skip + 4] == 0x01) break; - } - writebuf += skip; - writelen -= skip; - towrite -= skip; - update_file_hdr* pOtaFileHdr = (update_file_hdr*)(writebuf); - ctx.otaTargetHdr->FileHdr.FwVer = pOtaFileHdr->FwVer; - ctx.otaTargetHdr->FileHdr.HdrNum = pOtaFileHdr->HdrNum; - - uint32_t RevHdrLen = pOtaFileHdr->HdrNum * SUB_HEADER_LEN + HEADER_LEN; - if(writelen < RevHdrLen) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "failed to recv file header"); - ret = -1; - goto update_ota_exit; - } - - if(!get_ota_tartget_header(&ctx, writebuf, RevHdrLen)) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Get OTA header failed"); - ret = -1; - goto update_ota_exit; - } - if(!ota_checkimage_layout(ctx.otaTargetHdr)) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "ota_checkimage_layout failed"); - ret = -1; - goto update_ota_exit; - } - ctx.otactrl->IsGetOTAHdr = 1; - writebuf += RevHdrLen; - // magic values to make it somewhat work. - writelen -= RevHdrLen + 5 - 384; - towrite -= RevHdrLen + 5 - 384; - ctx.otactrl->NextImgLen = towrite; - do - { - int size = download_packet_process(&ctx, writebuf, writelen); - download_percentage(&ctx, size, ctx.otactrl->ImageLen); - rtos_delay_milliseconds(10); - ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total); - total += writelen; - towrite -= writelen; - - if(towrite > 0) - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, 2048 < towrite ? 2048 : towrite, 0); - if(writelen < 0) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); - ret = -1; - goto update_ota_exit; - } - } - } while((towrite > 0) && (writelen >= 0)); - - //erase manifest - flash_erase_sector(&flash, ctx.otactrl->FlashAddr); - - if(!verify_ota_checksum(ctx.otaTargetHdr, ctx.otactrl->targetIdx, ctx.otactrl->index)) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "The checksum is wrong!"); - ret = -1; - goto update_ota_exit; - } - - if(!ota_update_manifest(ctx.otaTargetHdr, ctx.otactrl->targetIdx, ctx.otactrl->index)) - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "Change signature failed!"); - ret = -1; - goto update_ota_exit; - } - -update_ota_exit: - ota_update_deinit(&ctx); - if(ret != -1) - { - ADDLOG_INFO(LOG_FEATURE_OTA, "OTA is successful"); - sys_clear_ota_signature(); - //sys_recover_ota_signature(); - } - else - { - ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed."); - return http_rest_error(request, ret, "error"); - } - -#else - - init_ota(startaddr); - - if(request->contentLength >= 0) - { - towrite = request->contentLength; - } - - if(writelen < 0 || (startaddr + writelen > maxaddr)) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "ABORTED: %d bytes to write", writelen); - return http_rest_error(request, -20, "writelen < 0 or end > 0x200000"); - } - - do - { - //ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d bytes to write", writelen); - add_otadata((unsigned char*)writebuf, writelen); - total += writelen; - startaddr += writelen; - towrite -= writelen; - if(towrite > 0) - { - writebuf = request->received; - writelen = recv(request->fd, writebuf, request->receivedLenmax, 0); - if(writelen < 0) - { - ADDLOG_DEBUG(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite); - } - } - } while((towrite > 0) && (writelen >= 0)); - close_ota(); -#endif - ADDLOG_DEBUG(LOG_FEATURE_OTA, "%d total bytes written", total); - http_setup(request, httpMimeTypeJson); - hprintf255(request, "{\"size\":%d}", total); - poststr(request, NULL); - CFG_IncrementOTACount(); - return 0; -} - static int http_rest_post_reboot(http_request_t* request) { http_setup(request, httpMimeTypeJson); hprintf255(request, "{\"reboot\":%d}", 3); @@ -3462,32 +1308,7 @@ static int http_rest_get_flash(http_request_t* request, int startaddr, int len) if (readlen > 1024) { readlen = 1024; } -#if PLATFORM_BEKEN - res = flash_read((char*)buffer, readlen, startaddr); -#elif PLATFORM_XRADIO - //uint32_t flash_read(uint32_t flash, uint32_t addr,void *buf, uint32_t size) - res = flash_read(0, startaddr, buffer, readlen); -#elif PLATFORM_XR872 - res = 0; -#elif PLATFORM_BL602 - res = bl_flash_read(startaddr, (uint8_t *)buffer, readlen); -#elif PLATFORM_W600 || PLATFORM_W800 - res = tls_fls_read(startaddr, (uint8_t*)buffer, readlen); -#elif PLATFORM_LN882H - res = hal_flash_read(startaddr, readlen, (uint8_t *)buffer); -#elif PLATFORM_ESPIDF || PLATFORM_ESP8266 - res = esp_flash_read(NULL, (void*)buffer, startaddr, readlen); -#elif PLATFORM_TR6260 - res = hal_spiflash_read(startaddr, (uint8_t*)buffer, readlen); -#elif PLATFORM_ECR6600 - res = drv_spiflash_read(startaddr, (uint8_t*)buffer, readlen); -#elif PLATFORM_REALTEK - device_mutex_lock(RT_DEV_LOCK_FLASH); - flash_stream_read(&flash, startaddr, readlen, (uint8_t*)buffer); - device_mutex_unlock(RT_DEV_LOCK_FLASH); -#else - res = 0; -#endif + res = HAL_FlashRead(buffer, readlen, startaddr); startaddr += readlen; len -= readlen; postany(request, buffer, readlen); diff --git a/src/mqtt/new_mqtt.c b/src/mqtt/new_mqtt.c index 8465160c0..d7e041e0f 100644 --- a/src/mqtt/new_mqtt.c +++ b/src/mqtt/new_mqtt.c @@ -14,7 +14,7 @@ #include "../driver/drv_public.h" #include "../driver/drv_ntp.h" #include "../driver/drv_tuyaMCU.h" -#include "../ota/ota.h" +#include "../hal/hal_ota.h" #ifndef WINDOWS #include #endif @@ -2183,9 +2183,7 @@ int MQTT_RunEverySecondUpdate() if (mqtt_client == 0 || res == 0) { //addLogAdv(LOG_INFO,LOG_FEATURE_MAIN, "Timer discovers disconnected mqtt %i\n",mqtt_loopsWithDisconnected); -#if PLATFORM_BK7231N || PLATFORM_BK7231T - if (ota_progress() == -1) -#endif + if (OTA_GetProgress() == -1) { mqtt_loopsWithDisconnected++; if (mqtt_loopsWithDisconnected > LOOPS_WITH_DISCONNECTED) @@ -2241,8 +2239,7 @@ int MQTT_RunEverySecondUpdate() g_wantTasmotaTeleSend = 0; } g_timeSinceLastMQTTPublish++; -#if PLATFORM_BK7231N || PLATFORM_BK7231T - if (ota_progress() != -1) + if (OTA_GetProgress() != -1) { addLogAdv(LOG_INFO, LOG_FEATURE_MQTT, "OTA started MQTT will be closed\n"); LOCK_TCPIP_CORE(); @@ -2250,7 +2247,6 @@ int MQTT_RunEverySecondUpdate() UNLOCK_TCPIP_CORE(); return 1; } -#endif if (CFG_HasFlag(OBK_FLAG_DO_TASMOTA_TELE_PUBLISHES)) { static int g_mqtt_tasmotaTeleCounter_sensor = 0; diff --git a/src/user_main.c b/src/user_main.c index 0f87ee346..239104ff2 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -28,7 +28,7 @@ #include "httpserver/http_tcp_server.h" #include "httpserver/rest_interface.h" #include "mqtt/new_mqtt.h" -#include "ota/ota.h" +#include "hal/hal_ota.h" #if ENABLE_LITTLEFS #include "littlefs/our_lfs.h" @@ -105,6 +105,40 @@ int DRV_SSDP_Active = 0; void Main_ForceUnsafeInit(); + + +// TEMPORARY +int ota_status = -1; +int total_bytes = 0; + +int OTA_GetProgress() +{ + return ota_status; +} + +void OTA_ResetProgress() +{ + ota_status = -1; +} + +void OTA_IncrementProgress(int value) +{ + ota_status += value; +} + +int OTA_GetTotalBytes() +{ + return total_bytes; +} + +void OTA_SetTotalBytes(int value) +{ + total_bytes = value; +} + + + + #if PLATFORM_XR806 || PLATFORM_XR872 size_t xPortGetFreeHeapSize() { @@ -625,9 +659,7 @@ void Main_OnEverySecond() #endif #endif -#if PLATFORM_BK7231N || PLATFORM_BK7231T - if (ota_progress() == -1) -#endif + if (OTA_GetProgress() == -1) { CFG_Save_IfThereArePendingChanges(); } diff --git a/src/win_main.c b/src/win_main.c index a22582645..b7eff55ca 100644 --- a/src/win_main.c +++ b/src/win_main.c @@ -610,12 +610,6 @@ void otarequest(const char *urlin) { return; } -int ota_progress() { - return 0; -} -int ota_total_bytes() { - return 0; -}