Skip to content

Commit

Permalink
Factory flash fix for C2/C6 (#33)
Browse files Browse the repository at this point in the history
* Fix c2 c6 factory flash
* add comments
* bump version
* add MCUs to CLI
  • Loading branch information
Jason2866 authored Nov 21, 2023
1 parent 9b34fe1 commit 67a4cdc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 46 deletions.
5 changes: 5 additions & 0 deletions esp_flasher/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def parse_args(argv):
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument("--esp8266", action="store_true")
group.add_argument("--esp32", action="store_true")
group.add_argument("--esp32s2", action="store_true")
group.add_argument("--esp32s3", action="store_true")
group.add_argument("--esp32c2", action="store_true")
group.add_argument("--esp32c3", action="store_true")
group.add_argument("--esp32c6", action="store_true")
group.add_argument(
"--upload-baud-rate",
type=int,
Expand Down
93 changes: 48 additions & 45 deletions esp_flasher/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def detect_flash_size(stub_chip):


def read_firmware_info(firmware):
firmware.seek(0x10000)
firmware.seek(0x10000) # Check for safeboot image
header = firmware.read(4)
magic, _, flash_mode_raw, flash_size_freq = struct.unpack("BBBB", header)
if magic == esptool.ESPLoader.ESP_IMAGE_MAGIC:
Expand All @@ -169,7 +169,7 @@ def read_firmware_info(firmware):
flag_factory = True
return flash_mode, flash_freq, flag_factory

firmware.seek(0)
firmware.seek(0) # Check for firmware image
header = firmware.read(4)
magic, _, flash_mode_raw, flash_size_freq = struct.unpack("BBBB", header)
if magic == esptool.ESPLoader.ESP_IMAGE_MAGIC:
Expand Down Expand Up @@ -233,7 +233,7 @@ def configure_write_flash_args(
flash_mode, flash_freq, flag_factory = read_firmware_info(firmware)
if flag_factory:
print("Detected factory firmware Image, flashing without changes")
if (isinstance(info, ESP32ChipInfo)) and not flag_factory:
if (isinstance(info, ESP32ChipInfo)): # No esp8266 image, fetching and processing needed files
ofs_partitions = 0x8000
ofs_otadata = 0xe000
ofs_factory_firm = 0x10000
Expand All @@ -243,7 +243,7 @@ def configure_write_flash_args(
model = "esp32c2"
safeboot = "tasmota32c2-safeboot.bin"
ofs_bootloader = 0x0
flash_freq = "60m"
flash_freq = "60m" # For Tasmota we use only fastest
elif "ESP32-C3" in info.model:
model = "esp32c3"
safeboot = "tasmota32c3-safeboot.bin"
Expand All @@ -252,7 +252,7 @@ def configure_write_flash_args(
model = "esp32c6"
safeboot = "tasmota32c6-safeboot.bin"
ofs_bootloader = 0x0
flash_freq = "80m"
flash_freq = "80m" # For Tasmota we use only fastest
elif "ESP32-S3" in info.model:
model = "esp32s3"
safeboot = "tasmota32s3-safeboot.bin"
Expand Down Expand Up @@ -282,47 +282,50 @@ def configure_write_flash_args(
pad_to_size = ""
spi_connection = ""

uwd = os.path.expanduser("~")
esp_flasher_ver = "ESP_Flasher_" + __version__
bootloaderstring = "bootloader_" + flash_mode + "_" + flash_freq + ".elf"
boot_loader_path = join(uwd, esp_flasher_ver, "bootloader", model, "bin")
boot_loader_file = join(boot_loader_path , bootloaderstring)
if not os.path.exists(boot_loader_path):
os.makedirs(boot_loader_path)
output = boot_loader_file.replace(".elf", ".bin")

try:
open(boot_loader_file, "rb") # check for local elf bootloader file
except IOError as err: # download elf bootloader file
boot_elf_path = open_downloadable_binary(
format_bootloader_path(bootloader_path, model, flash_mode, flash_freq)
)
with open(boot_loader_file, "wb") as f:
f.write(boot_elf_path.getbuffer()) # save elf bootloader file local

try:
with open(output, "rb") as fh:
bootloader = BytesIO(fh.read())
except IOError as err:
bootloader="" # Will be there in second call!

input = boot_loader_file # local downloaded elf bootloader file
if not partitions_path:
partitions_path = format_partitions_path(ESP32_DEFAULT_PARTITIONS, model)
if not factory_firm_path:
factory_firm_path = ESP32_SAFEBOOT_SERVER + safeboot

partitions = open_downloadable_binary(partitions_path)
factory_firm = open_downloadable_binary(factory_firm_path)
otadata = open_downloadable_binary(otadata_path)

addr_filename.append((ofs_bootloader, bootloader))
addr_filename.append((ofs_partitions, partitions))
addr_filename.append((ofs_otadata, otadata))
addr_filename.append((ofs_factory_firm, factory_firm))
addr_filename.append((ofs_firmware, firmware))
if not flag_factory: # No factory image
uwd = os.path.expanduser("~")
esp_flasher_ver = "ESP_Flasher_" + __version__
bootloaderstring = "bootloader_" + flash_mode + "_" + flash_freq + ".elf"
boot_loader_path = join(uwd, esp_flasher_ver, "bootloader", model, "bin")
boot_loader_file = join(boot_loader_path , bootloaderstring)
if not os.path.exists(boot_loader_path):
os.makedirs(boot_loader_path)
output = boot_loader_file.replace(".elf", ".bin")

try:
open(boot_loader_file, "rb") # check for local elf bootloader file
except IOError as err: # download elf bootloader file
boot_elf_path = open_downloadable_binary(
format_bootloader_path(bootloader_path, model, flash_mode, flash_freq)
)
with open(boot_loader_file, "wb") as f:
f.write(boot_elf_path.getbuffer()) # save elf bootloader file local

try:
with open(output, "rb") as fh:
bootloader = BytesIO(fh.read())
except IOError as err:
bootloader="" # Will be there in second call!

input = boot_loader_file # local downloaded elf bootloader file
if not partitions_path:
partitions_path = format_partitions_path(ESP32_DEFAULT_PARTITIONS, model)
if not factory_firm_path:
factory_firm_path = ESP32_SAFEBOOT_SERVER + safeboot

partitions = open_downloadable_binary(partitions_path)
factory_firm = open_downloadable_binary(factory_firm_path)
otadata = open_downloadable_binary(otadata_path)
# add all needed files since only firmware is provided
addr_filename.append((ofs_bootloader, bootloader))
addr_filename.append((ofs_partitions, partitions))
addr_filename.append((ofs_otadata, otadata))
addr_filename.append((ofs_factory_firm, factory_firm))
addr_filename.append((ofs_firmware, firmware))
else:
addr_filename.append((0x0, firmware)) # esp32 factory image
else:
addr_filename.append((0x0, firmware))
addr_filename.append((0x0, firmware)) # esp8266 image
return MockEsptoolArgs(chip, flag_factory, flash_size, addr_filename, flash_mode, flash_freq, input, secure_pad, secure_pad_v2,
min_rev, min_rev_full, max_rev_full, elf_sha256_offset, use_segments, flash_mmu_page_size, pad_to_size, spi_connection, output)

Expand Down
2 changes: 1 addition & 1 deletion esp_flasher/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

__version__ = "2.2.1"
__version__ = "2.2.2"

ESP32_DEFAULT_OTA_DATA = (
"https://raw.githubusercontent.com/Jason2866/ESP_Flasher/factory/"
Expand Down

0 comments on commit 67a4cdc

Please sign in to comment.