Skip to content

Commit 46ccadb

Browse files
donghengMr-Pi
dongheng
authored andcommitted
bugfix(fix): Fix combine binary overwrite checking
1 parent 97532e6 commit 46ccadb

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

components/esp8266/Kconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ config ESP8266_OTA_FROM_OLD
223223
bool "(**Expected**)ESP8266 update from old SDK by OTA"
224224
default n
225225
depends on IDF_TARGET_ESP8266
226-
select ESP8266_BOOT_COPY_APP
226+
select ESP8266_BOOT_COPY_APP if ESPTOOLPY_FLASHSIZE_1MB
227227
help
228228
The function is not released.
229229

components/esp8266/Makefile.projbuild

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ ifdef CONFIG_ESP8266_OTA_FROM_OLD
134134
$(OTA_V2_TO_V3_BIN): $(OTA_BIN)
135135
@cp $(RAW_BIN) $(RAW_BIN).tmp.bak
136136
@cp $(OTA1_BIN) $(RAW_BIN)
137-
@python $(IDF_PATH)/tools/pack_fw.py --output $(OTA_V2_TO_V3_BIN) pack3 $(ESPTOOL_ALL_FLASH_ARGS)
137+
@python $(IDF_PATH)/tools/pack_fw.py --output $(OTA_V2_TO_V3_BIN) --app $(PROJECT_NAME).bin pack3 $(ESPTOOL_ALL_FLASH_ARGS)
138138
@cp $(RAW_BIN).tmp.bak $(RAW_BIN)
139139
@echo [GEN] $(OTA_V2_TO_V3_BIN)
140140
endif

components/spi_flash/port/port.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ int esp_patition_table_init_data(void *partition_info)
434434
{
435435
int ret;
436436
const uint32_t boot_base = 0x1000;
437-
const uint32_t boot_size = s_v2_flash_bin_size / 2 - boot_base - 4 * SPI_FLASH_SEC_SIZE;
437+
const bootloader_state_t *bs = (const bootloader_state_t *)partition_info;
438+
const uint32_t boot_size = bs->ota[0].offset + bs->ota[0].size - boot_base;
438439

439440
if (!esp_sdk_update_from_v2())
440441
return 0;

examples/system/ota/native_ota/main/ota_example_main.c

+27
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@
3232
#define BUFFSIZE 1500
3333
#define TEXT_BUFFSIZE 1024
3434

35+
#ifdef CONFIG_ESP8266_OTA_FROM_OLD
36+
/*
37+
* Users should add your real firmware information here.
38+
* And the real infoarmation will be generated by "script".
39+
*/
40+
#ifdef CONFIG_ESPTOOLPY_FLASHSIZE_1MB
41+
/*
42+
* The configuration is related to file "partitions_two_ota_v2tov3.1MB".
43+
*/
44+
#define OTA_EXAMPLE_APP_OFFSET 0x6000UL
45+
#else
46+
/*
47+
* The configuration is related to file "partitions_two_ota_v2tov3.2MB".
48+
*/
49+
#define OTA_EXAMPLE_APP_OFFSET 0xf000UL
50+
#endif
51+
#endif
52+
3553
typedef enum esp_ota_firm_state {
3654
ESP_OTA_INIT = 0,
3755
ESP_OTA_PREPARE,
@@ -76,6 +94,10 @@ static EventGroupHandle_t wifi_event_group;
7694
to the AP with an IP? */
7795
const int CONNECTED_BIT = BIT0;
7896

97+
#ifdef CONFIG_ESP8266_OTA_FROM_OLD
98+
static const uint32_t s_ota_app_offset = OTA_EXAMPLE_APP_OFFSET;
99+
#endif
100+
79101
static esp_err_t event_handler(void *ctx, system_event_t *event)
80102
{
81103
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
185207
memset(length_str, 0, sizeof(length_str));
186208
memcpy(length_str, ptr, ptr2 - ptr);
187209
ota_firm->content_len = atoi(length_str);
210+
#ifdef CONFIG_ESP8266_OTA_FROM_OLD
211+
ota_firm->ota_size = ota_firm->content_len - s_ota_app_offset;
212+
ota_firm->ota_offset = s_ota_app_offset;
213+
#else
188214
#if defined(CONFIG_ESPTOOLPY_FLASHSIZE_1MB) && !defined(CONFIG_ESP8266_BOOT_COPY_APP)
189215
ota_firm->ota_size = ota_firm->content_len / ota_firm->ota_num;
190216
ota_firm->ota_offset = ota_firm->ota_size * ota_firm->update_ota_num;
191217
#else
192218
ota_firm->ota_size = ota_firm->content_len;
193219
ota_firm->ota_offset = 0;
220+
#endif
194221
#endif
195222
ESP_LOGI(TAG, "parse Content-Length:%d, ota_size %d", ota_firm->content_len, ota_firm->ota_size);
196223
}

tools/pack_fw.py

+25-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
import sys
2020
import binascii
2121
import struct
22+
import logging
2223

23-
__version__ = "1.0.1"
24+
__version__ = "1.0.2"
2425

2526
FLASH_SECTOR_SIZE = 0x1000
2627

@@ -52,6 +53,8 @@ def __call__(self, parser, namespace, values, option_string=None):
5253
for i in range(0, len(values) ,2):
5354
try:
5455
address = int(values[i], 0)
56+
if address == 0:
57+
address = 0x1000
5558
except ValueError:
5659
raise argparse.ArgumentError(self, 'Address "%s" must be a number' % values[i])
5760
try:
@@ -83,10 +86,10 @@ def pack3(args):
8386
print(e)
8487

8588
end_addr = None
89+
prev_addr = 0
90+
prec_file = ''
91+
app_offset = 0
8692
for address, argfile in args.addr_filename:
87-
if address == 0:
88-
address = 4096
89-
9093
if end_addr is not None and address > end_addr:
9194
data = (address - end_addr) * ['ff']
9295
filled = binascii.a2b_hex(''.join(data))
@@ -98,10 +101,20 @@ def pack3(args):
98101
fw_data += data
99102

100103
argfile.seek(0, 2)
101-
end_addr = address + argfile.tell()
104+
prev_addr = address
105+
prec_file = argfile.name
106+
end_addr = address + argfile.tell()
107+
if app_offset is not 0:
108+
raise Exception('Partition %s can be put behind %s'%(argfile.name, args.app))
109+
else:
110+
if args.app in argfile.name:
111+
app_offset = address - 0x1000
102112
except IOError as e:
103113
raise e
104114

115+
if app_offset is 0:
116+
raise Exception('Failed to find application binary %s in all arguments'%args.app)
117+
105118
crc32 = esp8266_crc32(fw_data)
106119
fw_data += struct.pack('<I', crc32)
107120

@@ -110,7 +123,8 @@ def pack3(args):
110123
output_file.close()
111124
except IOError as e:
112125
raise e
113-
126+
127+
print('\r\n\033[1;31;40mOTA example should use following macro:\r\n\r\n #define OTA_EXAMPLE_APP_OFFSET 0x%x\r\n\033[0m'%(app_offset))
114128

115129
def main():
116130
parser = argparse.ArgumentParser(description='pack_fw v%s - ESP8266 ROM Bootloader Utility' % __version__, prog='pack_fw')
@@ -120,6 +134,11 @@ def main():
120134
help='Output file name with full path',
121135
default=None)
122136

137+
parser.add_argument(
138+
'--app', '-a',
139+
help='application binary file name',
140+
default=None)
141+
123142
subparsers = parser.add_subparsers(
124143
dest='operation',
125144
help='Run pack_fw {command} -h for additional help')

0 commit comments

Comments
 (0)