Merge pull request #14311 from Jason2866/work

Use esptool.py to generate one file firmware
This commit is contained in:
Jason2866 2022-01-06 23:39:54 +01:00 committed by GitHub
commit 046097ffbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 76 deletions

View File

@ -1,97 +1,67 @@
# From: https://github.com/letscontrolit/ESPEasy/blob/mega/tools/pio/post_esp32.py # From: https://github.com/letscontrolit/ESPEasy/blob/mega/tools/pio/post_esp32.py
# Thanks TD-er :) # Thanks TD-er :)
# Combines separate bin files with their respective offsets into a single file
# This single file must then be flashed to an ESP32 node with 0 offset.
#
# Original implementation: Bartłomiej Zimoń (@uzi18)
#
# Special thanks to @Jason2866 for helping debug flashing to >4MB flash
# Thanks @jesserockz (esphome) for adapting to use esptool.py with merge_bin
#
# Typical layout of the generated file:
# Offset | File
# - 0x1000 | ~\.platformio\packages\framework-arduinoespressif32\tools\sdk\esp32\bin\bootloader_dout_40m.bin
# - 0x8000 | ~\Tasmota\.pio\build\<env name>\partitions.bin
# - 0xe000 | ~\.platformio\packages\framework-arduinoespressif32\tools\partitions\boot_app0.bin
# - 0x10000 | ~\Tasmota\.pio\build\<env name>/firmware.bin
Import("env") Import("env")
import os
def decode_flash_size_speed(byte_value): import subprocess
speed = byte_value[0] & 0x0f import sys
size = (byte_value[0] & 0xf0) / 16
spi_speed = { from os.path import expanduser, join
0: "40",
1: "26",
2: "20",
0xf: "80"
}
speed_mhz = spi_speed.get(speed)
size_mb = str(1<<int(size))
return "0x{}: {}MHz, {}MB".format(byte_value.hex(), speed_mhz, size_mb) home = expanduser("~")
esptoolpath = join(home, ".platformio", "packages", "tool-esptoolpy")
sys.path.append(esptoolpath)
import esptool
def esp32_create_combined_bin(source, target, env):
def esp32_create_factory_bin(source, target, env): #print("Generating combined binary for serial flashing")
#print("Generating one file initial flash firmware for esp32")
offset = 0x0
flash_page_size = 256
# The offset from begin of the file where the app0 partition starts # The offset from begin of the file where the app0 partition starts
# This is defined in the partition .csv file # This is defined in the partition .csv file
sketch_partition_start = 0x10000 app_offset = 0x10000
# Typical layout of the generated file:
# Offset | File
# - 0x1000 | ~\.platformio\packages\framework-arduinoespressif32\tools\sdk\esp32\bin\bootloader_dout_40m.bin
# - 0x8000 | ~\Tasmota\.pio\build\<env name>\partitions.bin
# - 0xe000 | ~\.platformio\packages\framework-arduinoespressif32\tools\partitions\boot_app0.bin
# - 0x10000 | ~\Tasmota\.pio\build\<env name>/firmware.bin
new_file_name = env.subst("$BUILD_DIR/${PROGNAME}.factory.bin") new_file_name = env.subst("$BUILD_DIR/${PROGNAME}.factory.bin")
sections = env.subst(env.get('FLASH_EXTRA_IMAGES')) sections = env.subst(env.get("FLASH_EXTRA_IMAGES"))
new_file = open(new_file_name,"wb")
new_file.truncate() # Make sure no left over data is present from a previous build
firmware_name = env.subst("$BUILD_DIR/${PROGNAME}.bin") firmware_name = env.subst("$BUILD_DIR/${PROGNAME}.bin")
chip = env.get("BOARD_MCU")
# fill the file with 0xFF binary data to reflect 'erased and not yet written' data.
# Make sure to use exactly the size that normally would be erased
new_file.seek(0)
total_size = sketch_partition_start + os.path.getsize(firmware_name)
total_size = total_size - (total_size % flash_page_size) + flash_page_size
new_file.write(bytes([0xff] * total_size))
print(" {} | {}".format("Offset", "File"))
for section in sections:
sect_adr,sect_file = section.split(" ",1)
print(" - {} | {}".format(sect_adr,sect_file))
source = open(sect_file,"rb")
new_file.seek(int(sect_adr,0)-offset)
new_file.write(source.read())
if "bootloader" in sect_file:
# Need to patch the bootloader to match the flash size
flash_size = env.BoardConfig().get("upload.flash_size") flash_size = env.BoardConfig().get("upload.flash_size")
cmd = [
"--chip",
chip,
"merge_bin",
"-o",
new_file_name,
"--flash_size",
flash_size,
]
spi_speed_size_byte_offset = 3 print(" Offset | File")
source.seek(spi_speed_size_byte_offset) for section in sections:
spi_speed_size_byte = source.read(1) sect_adr, sect_file = section.split(" ", 1)
new_spi_speed_size_byte = spi_speed_size_byte[0] & 0x0f print(f" - {sect_adr} | {sect_file}")
cmd += [sect_adr, sect_file]
spi_size = { print(f" - {hex(app_offset)} | {firmware_name}")
"1MB": 0x00, cmd += [hex(app_offset), firmware_name]
"2MB": 0x10,
"4MB": 0x20,
"8MB": 0x30,
"16MB": 0x40
}
new_spi_speed_size_byte |= spi_size.get(flash_size, spi_speed_size_byte[0] & 0xf0)
if new_spi_speed_size_byte != spi_speed_size_byte: #print('Using esptool.py arguments: %s' % ' '.join(cmd))
new_byte = bytes([new_spi_speed_size_byte])
print(" | Patch flash size value {} -> {}".format(decode_flash_size_speed(spi_speed_size_byte), decode_flash_size_speed(new_byte)))
new_file.seek(int(sect_adr,0)-offset + spi_speed_size_byte_offset)
new_file.write(new_byte)
source.close() esptool.main(cmd)
firmware_start = sketch_partition_start-offset env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", esp32_create_combined_bin)
firmware = open(firmware_name,"rb")
print(" - {} | {}".format(hex(firmware_start),firmware_name))
new_file.seek(firmware_start)
new_file.write(firmware.read())
new_file.close()
firmware.close()
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", esp32_create_factory_bin)