@@ -295,9 +295,6 @@ class ESPLoader:
295295 # Bootloader flashing offset
296296 BOOTLOADER_FLASH_OFFSET = 0x0
297297
298- # ROM supports an encrypted flashing mode
299- SUPPORTS_ENCRYPTED_FLASH = False
300-
301298 # Response to SYNC might indicate that flasher stub is running
302299 # instead of the ROM bootloader
303300 sync_stub_detected = False
@@ -988,7 +985,7 @@ def mem_finish(self, entrypoint=0):
988985 raise
989986 pass
990987
991- def flash_begin (self , size , offset , begin_rom_encrypted = False , logging = True ):
988+ def flash_begin (self , size , offset , encrypted_write = False , logging = True ):
992989 """
993990 Start downloading to Flash (performs an erase)
994991
@@ -1008,8 +1005,11 @@ def flash_begin(self, size, offset, begin_rom_encrypted=False, logging=True):
10081005 params = struct .pack (
10091006 "<IIII" , erase_size , num_blocks , self .FLASH_WRITE_SIZE , offset
10101007 )
1011- if self .SUPPORTS_ENCRYPTED_FLASH and not self .IS_STUB :
1012- params += struct .pack ("<I" , 1 if begin_rom_encrypted else 0 )
1008+
1009+ # ESP32 and ESP8266 ROMs do not support extended parameter format
1010+ if self .IS_STUB or self .CHIP_NAME not in ("ESP32" , "ESP8266" ):
1011+ params += struct .pack ("<I" , 1 if encrypted_write else 0 )
1012+
10131013 self .check_command (
10141014 "enter flash download mode" ,
10151015 self .ESP_CMDS ["FLASH_BEGIN" ],
@@ -1020,39 +1020,15 @@ def flash_begin(self, size, offset, begin_rom_encrypted=False, logging=True):
10201020 log .print (f"Took { time .time () - t :.2f} s to erase flash block." )
10211021 return num_blocks
10221022
1023- def flash_block (self , data , seq , timeout = DEFAULT_TIMEOUT ):
1023+ def flash_block (self , data , seq , timeout = DEFAULT_TIMEOUT , encrypted = False ):
10241024 """Write block to flash, retry if fail"""
1025- for attempts_left in range (WRITE_BLOCK_ATTEMPTS - 1 , - 1 , - 1 ):
1026- try :
1027- self .check_command (
1028- f"write to target flash after seq { seq } " ,
1029- self .ESP_CMDS ["FLASH_DATA" ],
1030- struct .pack ("<IIII" , len (data ), seq , 0 , 0 ) + data ,
1031- self .checksum (data ),
1032- timeout = timeout ,
1033- )
1034- break
1035- except FatalError :
1036- if attempts_left :
1037- self .trace (
1038- "Block write failed, "
1039- f"retrying with { attempts_left } attempts left..."
1040- )
1041- else :
1042- raise
1043-
1044- def flash_encrypt_block (self , data , seq , timeout = DEFAULT_TIMEOUT ):
1045- """Encrypt, write block to flash, retry if fail"""
1046- if self .SUPPORTS_ENCRYPTED_FLASH and not self .IS_STUB :
1047- # ROM support performs the encrypted writes via the normal write command,
1048- # triggered by flash_begin(begin_rom_encrypted=True)
1049- return self .flash_block (data , seq , timeout )
10501025
1026+ operation = "encrypted " if encrypted else ""
10511027 for attempts_left in range (WRITE_BLOCK_ATTEMPTS - 1 , - 1 , - 1 ):
10521028 try :
10531029 self .check_command (
1054- f"Write encrypted to target flash after seq { seq } " ,
1055- self .ESP_CMDS ["FLASH_ENCRYPT_DATA " ],
1030+ f"write { operation } to target flash after seq { seq } " ,
1031+ self .ESP_CMDS ["FLASH_DATA " ],
10561032 struct .pack ("<IIII" , len (data ), seq , 0 , 0 ) + data ,
10571033 self .checksum (data ),
10581034 timeout = timeout ,
@@ -1061,8 +1037,8 @@ def flash_encrypt_block(self, data, seq, timeout=DEFAULT_TIMEOUT):
10611037 except FatalError :
10621038 if attempts_left :
10631039 self .trace (
1064- "Encrypted block write failed, "
1065- f"retrying with { attempts_left } attempts left"
1040+ f" { operation } block write failed, "
1041+ f"retrying with { attempts_left } attempts left..." . capitalize ()
10661042 )
10671043 else :
10681044 raise
@@ -1342,7 +1318,7 @@ def run_stub(self, stub: StubFlasher | None = None) -> "ESPLoader":
13421318 return self .STUB_CLASS (self ) if self .STUB_CLASS is not None else self
13431319
13441320 @stub_and_esp32_function_only
1345- def flash_defl_begin (self , size , compsize , offset ):
1321+ def flash_defl_begin (self , size , compsize , offset , encrypted_write = False ):
13461322 """
13471323 Start downloading compressed data to Flash (performs an erase)
13481324
@@ -1368,10 +1344,11 @@ def flash_defl_begin(self, size, compsize, offset):
13681344 params = struct .pack (
13691345 "<IIII" , write_size , num_blocks , self .FLASH_WRITE_SIZE , offset
13701346 )
1371- if self .SUPPORTS_ENCRYPTED_FLASH and not self .IS_STUB :
1372- # extra param is to enter encrypted flash mode via ROM
1373- # (not supported currently)
1374- params += struct .pack ("<I" , 0 )
1347+
1348+ # ESP32 and ESP8266 ROMs do not support extended parameter format
1349+ if self .IS_STUB or self .CHIP_NAME not in ("ESP32" , "ESP8266" ):
1350+ params += struct .pack ("<I" , 1 if encrypted_write else 0 )
1351+
13751352 self .check_command (
13761353 "enter compressed flash mode" ,
13771354 self .ESP_CMDS ["FLASH_DEFL_BEGIN" ],
0 commit comments