From da92d858f8c335a5558c9427a22ea69b8d58eedb Mon Sep 17 00:00:00 2001 From: dongheng Date: Tue, 19 Feb 2019 10:42:54 +0800 Subject: [PATCH] bugfix(fix): Fix combine binary overwrite checking --- components/esp8266/Kconfig | 2 +- components/esp8266/Makefile.projbuild | 2 +- components/spi_flash/port/port.c | 3 +- examples/system/ota/main/ota_example_main.c | 27 ++++++++++++++++++ tools/pack_fw.py | 31 +++++++++++++++++---- 5 files changed, 56 insertions(+), 9 deletions(-) diff --git a/components/esp8266/Kconfig b/components/esp8266/Kconfig index 1427e193..eed94eba 100644 --- a/components/esp8266/Kconfig +++ b/components/esp8266/Kconfig @@ -217,7 +217,7 @@ config ESP8266_OTA_FROM_OLD bool "(**Expected**)ESP8266 update from old SDK by OTA" default n depends on TARGET_PLATFORM_ESP8266 - select ESP8266_BOOT_COPY_APP + select ESP8266_BOOT_COPY_APP if ESPTOOLPY_FLASHSIZE_1MB help The function is not released. diff --git a/components/esp8266/Makefile.projbuild b/components/esp8266/Makefile.projbuild index 690a16f5..bfa77ef2 100644 --- a/components/esp8266/Makefile.projbuild +++ b/components/esp8266/Makefile.projbuild @@ -119,7 +119,7 @@ endif ifdef CONFIG_ESP8266_OTA_FROM_OLD $(OTA_V2_TO_V3_BIN): - @python $(IDF_PATH)/tools/pack_fw.py --output $(OTA_V2_TO_V3_BIN) pack3 $(ESPTOOL_ALL_FLASH_ARGS) + @python $(IDF_PATH)/tools/pack_fw.py --output $(OTA_V2_TO_V3_BIN) --app $(PROJECT_NAME).bin pack3 $(ESPTOOL_ALL_FLASH_ARGS) @echo [GEN] $(OTA_V2_TO_V3_BIN) endif diff --git a/components/spi_flash/port/port.c b/components/spi_flash/port/port.c index aa60e3c4..e683e1fd 100644 --- a/components/spi_flash/port/port.c +++ b/components/spi_flash/port/port.c @@ -434,7 +434,8 @@ int esp_patition_table_init_data(void *partition_info) { int ret; const uint32_t boot_base = 0x1000; - const uint32_t boot_size = s_v2_flash_bin_size / 2 - boot_base - 4 * SPI_FLASH_SEC_SIZE; + const bootloader_state_t *bs = (const bootloader_state_t *)partition_info; + const uint32_t boot_size = bs->ota[0].offset + bs->ota[0].size - boot_base; if (!esp_sdk_update_from_v2()) return 0; diff --git a/examples/system/ota/main/ota_example_main.c b/examples/system/ota/main/ota_example_main.c index 27e58646..7e62e609 100644 --- a/examples/system/ota/main/ota_example_main.c +++ b/examples/system/ota/main/ota_example_main.c @@ -32,6 +32,24 @@ #define BUFFSIZE 1500 #define TEXT_BUFFSIZE 1024 +#ifdef CONFIG_ESP8266_OTA_FROM_OLD +/* + * Users should add your real firmware information here. + * And the real infoarmation will be generated by "script". + */ +#ifdef CONFIG_ESPTOOLPY_FLASHSIZE_1MB +/* + * The configuration is related to file "partitions_two_ota_v2tov3.1MB". + */ +#define OTA_EXAMPLE_APP_OFFSET 0x6000UL +#else +/* + * The configuration is related to file "partitions_two_ota_v2tov3.2MB". + */ +#define OTA_EXAMPLE_APP_OFFSET 0xf000UL +#endif +#endif + typedef enum esp_ota_firm_state { ESP_OTA_INIT = 0, ESP_OTA_PREPARE, @@ -76,6 +94,10 @@ static EventGroupHandle_t wifi_event_group; to the AP with an IP? */ const int CONNECTED_BIT = BIT0; +#ifdef CONFIG_ESP8266_OTA_FROM_OLD +static const uint32_t s_ota_app_offset = OTA_EXAMPLE_APP_OFFSET; +#endif + static esp_err_t event_handler(void *ctx, system_event_t *event) { switch (event->event_id) { @@ -185,12 +207,17 @@ bool _esp_ota_firm_parse_http(esp_ota_firm_t *ota_firm, const char *text, size_t memset(length_str, 0, sizeof(length_str)); memcpy(length_str, ptr, ptr2 - ptr); ota_firm->content_len = atoi(length_str); +#ifdef CONFIG_ESP8266_OTA_FROM_OLD + ota_firm->ota_size = ota_firm->content_len - s_ota_app_offset; + ota_firm->ota_offset = s_ota_app_offset; +#else #if defined(CONFIG_ESPTOOLPY_FLASHSIZE_1MB) && !defined(CONFIG_ESP8266_BOOT_COPY_APP) ota_firm->ota_size = ota_firm->content_len / ota_firm->ota_num; ota_firm->ota_offset = ota_firm->ota_size * ota_firm->update_ota_num; #else ota_firm->ota_size = ota_firm->content_len; ota_firm->ota_offset = 0; +#endif #endif ESP_LOGI(TAG, "parse Content-Length:%d, ota_size %d", ota_firm->content_len, ota_firm->ota_size); } diff --git a/tools/pack_fw.py b/tools/pack_fw.py index c94d2246..652cc4d1 100644 --- a/tools/pack_fw.py +++ b/tools/pack_fw.py @@ -19,8 +19,9 @@ import inspect import sys import binascii import struct +import logging -__version__ = "1.0.1" +__version__ = "1.0.2" FLASH_SECTOR_SIZE = 0x1000 @@ -52,6 +53,8 @@ class proc_addr_file(argparse.Action): for i in range(0, len(values) ,2): try: address = int(values[i], 0) + if address == 0: + address = 0x1000 except ValueError: raise argparse.ArgumentError(self, 'Address "%s" must be a number' % values[i]) try: @@ -83,10 +86,10 @@ def pack3(args): print(e) end_addr = None + prev_addr = 0 + prec_file = '' + app_offset = 0 for address, argfile in args.addr_filename: - if address == 0: - address = 4096 - if end_addr is not None and address > end_addr: data = (address - end_addr) * ['ff'] filled = binascii.a2b_hex(''.join(data)) @@ -98,10 +101,20 @@ def pack3(args): fw_data += data argfile.seek(0, 2) - end_addr = address + argfile.tell() + prev_addr = address + prec_file = argfile.name + end_addr = address + argfile.tell() + if app_offset is not 0: + raise Exception('Partition %s can be put behind %s'%(argfile.name, args.app)) + else: + if args.app in argfile.name: + app_offset = address - 0x1000 except IOError as e: raise e + if app_offset is 0: + raise Exception('Failed to find application binary %s in all arguments'%args.app) + crc32 = esp8266_crc32(fw_data) fw_data += struct.pack('