@@ -143,13 +143,6 @@ def stub_and_esp32_function_only(func):
143
143
)
144
144
145
145
146
- def esp32s3_or_newer_function_only (func ):
147
- """Attribute for a function only supported by ESP32S3 and later chips ROM"""
148
- return check_supported_function (
149
- func , lambda o : o .CHIP_NAME not in ["ESP8266" , "ESP32" , "ESP32-S2" ]
150
- )
151
-
152
-
153
146
class StubFlasher :
154
147
STUB_DIR = os .path .join (os .path .dirname (__file__ ), "targets" , "stub_flasher" )
155
148
# directories will be searched in the order of STUB_SUBDIRS
@@ -302,6 +295,9 @@ class ESPLoader(object):
302
295
# Number of attempts to write flash data
303
296
WRITE_FLASH_ATTEMPTS = 2
304
297
298
+ # Chip uses magic number for chip type autodetection
299
+ USES_MAGIC_VALUE = True
300
+
305
301
def __init__ (self , port = DEFAULT_PORT , baud = ESP_ROM_BAUD , trace_enabled = False ):
306
302
"""Base constructor for ESPLoader bootloader interaction
307
303
@@ -752,41 +748,75 @@ def connect(
752
748
)
753
749
754
750
if not detecting :
755
- try :
756
- from .targets import ROM_LIST
751
+ from .targets import ROM_LIST
757
752
758
- # check the date code registers match what we expect to see
753
+ # Perform a dummy read_reg to check if the chip is in secure download mode
754
+ try :
759
755
chip_magic_value = self .read_reg (ESPLoader .CHIP_DETECT_MAGIC_REG_ADDR )
760
- if chip_magic_value not in self .CHIP_DETECT_MAGIC_VALUE :
761
- actually = None
762
- for cls in ROM_LIST :
763
- if chip_magic_value in cls .CHIP_DETECT_MAGIC_VALUE :
764
- actually = cls
765
- break
766
- if warnings and actually is None :
767
- print (
768
- "WARNING: This chip doesn't appear to be a %s "
769
- "(chip magic value 0x%08x). "
770
- "Probably it is unsupported by this version of esptool."
771
- % (self .CHIP_NAME , chip_magic_value )
772
- )
773
- else :
774
- raise FatalError (
775
- "This chip is %s not %s. Wrong --chip argument?"
776
- % (actually .CHIP_NAME , self .CHIP_NAME )
777
- )
778
756
except UnsupportedCommandError :
779
757
self .secure_download_mode = True
780
758
759
+ # Check if chip supports reading chip ID from the get_security_info command
781
760
try :
782
- self .check_chip_id ()
783
- except UnsupportedCommandError :
784
- # Fix for ROM not responding in SDM, reconnect and try again
785
- if self .secure_download_mode :
786
- self ._connect_attempt (mode , reset_sequence [0 ])
787
- self .check_chip_id ()
761
+ chip_id = self .get_chip_id ()
762
+ except (UnsupportedCommandError , struct .error , FatalError ):
763
+ chip_id = None
764
+
765
+ detected = None
766
+ chip_arg_wrong = False
767
+
768
+ # If we can read chip ID (ESP32-S3 and later), verify the ID
769
+ if chip_id and (self .USES_MAGIC_VALUE or chip_id != self .IMAGE_CHIP_ID ):
770
+ chip_arg_wrong = True
771
+ for cls in ROM_LIST :
772
+ if not cls .USES_MAGIC_VALUE and chip_id == cls .IMAGE_CHIP_ID :
773
+ detected = cls
774
+ break
775
+ # If we can't read chip ID (ESP8266, ESP32, ESP32-S2),
776
+ # try to verify the chip by magic value
777
+ elif (
778
+ not chip_id
779
+ and not self .secure_download_mode
780
+ and (not self .USES_MAGIC_VALUE or chip_magic_value != self .MAGIC_VALUE )
781
+ ):
782
+ chip_arg_wrong = True
783
+ for cls in ROM_LIST :
784
+ if cls .USES_MAGIC_VALUE and chip_magic_value == cls .MAGIC_VALUE :
785
+ detected = cls
786
+ break
787
+ # If we can't read chip ID and the chip is in SDM (ESP32 or ESP32-S2),
788
+ # we can't verify
789
+ elif not chip_id and self .secure_download_mode :
790
+ if self .CHIP_NAME not in ["ESP32" , "ESP32-S2" ]:
791
+ chip_arg_wrong = True
792
+ detected = "ESP32 or ESP32-S2"
788
793
else :
789
- raise
794
+ print (
795
+ f"WARNING: Can't verify this chip is { self .CHIP_NAME } "
796
+ "because of active Secure Download Mode. "
797
+ "Please check it manually."
798
+ )
799
+
800
+ if chip_arg_wrong :
801
+ if warnings and detected is None :
802
+ specifier = (
803
+ f"(read chip ID { chip_id } )"
804
+ if chip_id
805
+ else f"(read chip magic value { chip_magic_value :#08x} )"
806
+ )
807
+ print (
808
+ f"WARNING: This chip doesn't appear to be an { self .CHIP_NAME } "
809
+ f"{ specifier } . Probably it is unsupported by this version "
810
+ "of esptool. Will attempt to continue anyway."
811
+ )
812
+ else :
813
+ chip_type = (
814
+ detected if isinstance (detected , str ) else detected .CHIP_NAME
815
+ )
816
+ raise FatalError (
817
+ f"This chip is { chip_type } , not { self .CHIP_NAME } . "
818
+ "Wrong --chip argument?"
819
+ )
790
820
self ._post_connect ()
791
821
792
822
def _post_connect (self ):
@@ -1002,7 +1032,6 @@ def get_security_info(self):
1002
1032
"api_version" : None if esp32s2 else res [10 ],
1003
1033
}
1004
1034
1005
- @esp32s3_or_newer_function_only
1006
1035
def get_chip_id (self ):
1007
1036
if self .cache ["chip_id" ] is None :
1008
1037
res = self .check_command (
@@ -1552,23 +1581,6 @@ def soft_reset(self, stay_in_bootloader):
1552
1581
# in the stub loader
1553
1582
self .command (self .ESP_RUN_USER_CODE , wait_response = False )
1554
1583
1555
- def check_chip_id (self ):
1556
- try :
1557
- chip_id = self .get_chip_id ()
1558
- if chip_id != self .IMAGE_CHIP_ID :
1559
- print (
1560
- "WARNING: Chip ID {} ({}) doesn't match expected Chip ID {}. "
1561
- "esptool may not work correctly." .format (
1562
- chip_id ,
1563
- self .UNSUPPORTED_CHIPS .get (chip_id , "Unknown" ),
1564
- self .IMAGE_CHIP_ID ,
1565
- )
1566
- )
1567
- # Try to flash anyways by disabling stub
1568
- self .stub_is_disabled = True
1569
- except NotImplementedInROMError :
1570
- pass
1571
-
1572
1584
1573
1585
def slip_reader (port , trace_function ):
1574
1586
"""Generator to read SLIP packets from a serial port.
0 commit comments