From b23e60f0d40d1e8b598eab5263f3b3f91e4ef3e8 Mon Sep 17 00:00:00 2001 From: Jaroslav Burian Date: Fri, 6 Dec 2024 12:40:40 +0100 Subject: [PATCH 1/6] feat: Add new app description segments This commit adds new app description segments, the segments are: min_efuse_blk_rev_full, max_efuse_blk_rev_full and mmu_page_size. --- esptool/cmds.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/esptool/cmds.py b/esptool/cmds.py index 2d01d65b0..30a12f3b0 100644 --- a/esptool/cmds.py +++ b/esptool/cmds.py @@ -914,7 +914,7 @@ def get_key_from_value(dict, val): pass # ESP8266 image has no append_digest field if app_desc: - APP_DESC_STRUCT_FMT = " Date: Fri, 6 Dec 2024 13:38:54 +0100 Subject: [PATCH 2/6] fix: Not reading app description for some SoCs This commit updates the DROM and IROM addresses for the ESP32-C5, ESP32-C6, and ESP32-C61. This also fixes the issue when reading app description using image_info command when there are multiple DROM sections. Closes https://github.com/espressif/esptool/issues/1039 --- esptool/cmds.py | 3 ++- esptool/targets/esp32c5.py | 10 +++++----- esptool/targets/esp32c6.py | 4 ++-- esptool/targets/esp32c61.py | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/esptool/cmds.py b/esptool/cmds.py index 30a12f3b0..4e23aeede 100644 --- a/esptool/cmds.py +++ b/esptool/cmds.py @@ -875,7 +875,8 @@ def get_key_from_value(dict, val): for idx, seg in enumerate(image.segments): segs = seg.get_memory_type(image) seg_name = ", ".join(segs) - if "DROM" in segs: # The DROM segment starts with the esp_app_desc_t struct + # The DROM segment starts with the esp_app_desc_t struct + if "DROM" in segs and app_desc is None: app_desc = seg.data[:256] elif "DRAM" in segs: # The DRAM segment starts with the esp_bootloader_desc_t struct diff --git a/esptool/targets/esp32c5.py b/esptool/targets/esp32c5.py index 2604fb77c..4dba3e6d8 100644 --- a/esptool/targets/esp32c5.py +++ b/esptool/targets/esp32c5.py @@ -44,9 +44,9 @@ class ESP32C5ROM(ESP32C6ROM): EFUSE_SECURE_BOOT_EN_MASK = 1 << 20 IROM_MAP_START = 0x42000000 - IROM_MAP_END = 0x42800000 - DROM_MAP_START = 0x42800000 - DROM_MAP_END = 0x43000000 + IROM_MAP_END = 0x44000000 + DROM_MAP_START = 0x42000000 + DROM_MAP_END = 0x44000000 PCR_SYSCLK_CONF_REG = 0x60096110 PCR_SYSCLK_XTAL_FREQ_V = 0x7F << 24 @@ -65,12 +65,12 @@ class ESP32C5ROM(ESP32C6ROM): MEMORY_MAP = [ [0x00000000, 0x00010000, "PADDING"], - [0x42800000, 0x43000000, "DROM"], + [0x42000000, 0x44000000, "DROM"], [0x40800000, 0x40860000, "DRAM"], [0x40800000, 0x40860000, "BYTE_ACCESSIBLE"], [0x4003A000, 0x40040000, "DROM_MASK"], [0x40000000, 0x4003A000, "IROM_MASK"], - [0x42000000, 0x42800000, "IROM"], + [0x42000000, 0x44000000, "IROM"], [0x40800000, 0x40860000, "IRAM"], [0x50000000, 0x50004000, "RTC_IRAM"], [0x50000000, 0x50004000, "RTC_DRAM"], diff --git a/esptool/targets/esp32c6.py b/esptool/targets/esp32c6.py index 16b5531d1..a7e620ac3 100644 --- a/esptool/targets/esp32c6.py +++ b/esptool/targets/esp32c6.py @@ -89,12 +89,12 @@ class ESP32C6ROM(ESP32C3ROM): MEMORY_MAP = [ [0x00000000, 0x00010000, "PADDING"], - [0x42800000, 0x43000000, "DROM"], + [0x42000000, 0x43000000, "DROM"], [0x40800000, 0x40880000, "DRAM"], [0x40800000, 0x40880000, "BYTE_ACCESSIBLE"], [0x4004AC00, 0x40050000, "DROM_MASK"], [0x40000000, 0x4004AC00, "IROM_MASK"], - [0x42000000, 0x42800000, "IROM"], + [0x42000000, 0x43000000, "IROM"], [0x40800000, 0x40880000, "IRAM"], [0x50000000, 0x50004000, "RTC_IRAM"], [0x50000000, 0x50004000, "RTC_DRAM"], diff --git a/esptool/targets/esp32c61.py b/esptool/targets/esp32c61.py index 2132bda3f..740500a7a 100644 --- a/esptool/targets/esp32c61.py +++ b/esptool/targets/esp32c61.py @@ -53,12 +53,12 @@ class ESP32C61ROM(ESP32C6ROM): MEMORY_MAP = [ [0x00000000, 0x00010000, "PADDING"], - [0x41800000, 0x42000000, "DROM"], + [0x42000000, 0x44000000, "DROM"], [0x40800000, 0x40860000, "DRAM"], [0x40800000, 0x40860000, "BYTE_ACCESSIBLE"], [0x4004AC00, 0x40050000, "DROM_MASK"], [0x40000000, 0x4004AC00, "IROM_MASK"], - [0x41000000, 0x41800000, "IROM"], + [0x42000000, 0x44000000, "IROM"], [0x40800000, 0x40860000, "IRAM"], [0x50000000, 0x50004000, "RTC_IRAM"], [0x50000000, 0x50004000, "RTC_DRAM"], From 598e07b4afd5ef140a3778d1461dfc631a6020c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radim=20Karni=C5=A1?= Date: Fri, 6 Dec 2024 12:22:01 +0100 Subject: [PATCH 3/6] fix(chip_type_verification): Enable in SDM, do not rely on magic numbers Closes https://github.com/espressif/esptool/pull/1008 --- esptool/cmds.py | 11 +-- esptool/loader.py | 118 ++++++++++++++++++-------------- esptool/targets/esp32.py | 2 +- esptool/targets/esp32c2.py | 3 - esptool/targets/esp32c3.py | 5 +- esptool/targets/esp32c5.py | 3 - esptool/targets/esp32c5beta3.py | 3 - esptool/targets/esp32c6.py | 3 - esptool/targets/esp32c61.py | 3 - esptool/targets/esp32c6beta.py | 2 - esptool/targets/esp32h2.py | 3 - esptool/targets/esp32h2beta1.py | 2 - esptool/targets/esp32h2beta2.py | 2 - esptool/targets/esp32p4.py | 4 +- esptool/targets/esp32s2.py | 2 +- esptool/targets/esp32s3.py | 4 +- esptool/targets/esp32s3beta2.py | 2 - esptool/targets/esp8266.py | 2 +- 18 files changed, 80 insertions(+), 94 deletions(-) diff --git a/esptool/cmds.py b/esptool/cmds.py index 4e23aeede..ab8af32d2 100644 --- a/esptool/cmds.py +++ b/esptool/cmds.py @@ -108,10 +108,10 @@ def check_if_stub(instance): try: print("Detecting chip type...", end="") chip_id = detect_port.get_chip_id() - for cls in [ - n for n in ROM_LIST if n.CHIP_NAME not in ("ESP8266", "ESP32", "ESP32-S2") - ]: + for cls in ROM_LIST: # cmd not supported on ESP8266 and ESP32 + ESP32-S2 doesn't return chip_id + if cls.USES_MAGIC_VALUE: + continue if chip_id == cls.IMAGE_CHIP_ID: inst = cls(detect_port._port, baud, trace_enabled=trace_enabled) try: @@ -144,11 +144,12 @@ def check_if_stub(instance): ) for cls in ROM_LIST: - if chip_magic_value in cls.CHIP_DETECT_MAGIC_VALUE: + if not cls.USES_MAGIC_VALUE: + continue + if chip_magic_value == cls.MAGIC_VALUE: inst = cls(detect_port._port, baud, trace_enabled=trace_enabled) inst = check_if_stub(inst) inst._post_connect() - inst.check_chip_id() break else: err_msg = f"Unexpected chip magic value {chip_magic_value:#010x}." diff --git a/esptool/loader.py b/esptool/loader.py index 883eeb532..0c0b880b0 100644 --- a/esptool/loader.py +++ b/esptool/loader.py @@ -143,13 +143,6 @@ def stub_and_esp32_function_only(func): ) -def esp32s3_or_newer_function_only(func): - """Attribute for a function only supported by ESP32S3 and later chips ROM""" - return check_supported_function( - func, lambda o: o.CHIP_NAME not in ["ESP8266", "ESP32", "ESP32-S2"] - ) - - class StubFlasher: STUB_DIR = os.path.join(os.path.dirname(__file__), "targets", "stub_flasher") # directories will be searched in the order of STUB_SUBDIRS @@ -302,6 +295,9 @@ class ESPLoader(object): # Number of attempts to write flash data WRITE_FLASH_ATTEMPTS = 2 + # Chip uses magic number for chip type autodetection + USES_MAGIC_VALUE = True + def __init__(self, port=DEFAULT_PORT, baud=ESP_ROM_BAUD, trace_enabled=False): """Base constructor for ESPLoader bootloader interaction @@ -752,41 +748,75 @@ def connect( ) if not detecting: - try: - from .targets import ROM_LIST + from .targets import ROM_LIST - # check the date code registers match what we expect to see + # Perform a dummy read_reg to check if the chip is in secure download mode + try: chip_magic_value = self.read_reg(ESPLoader.CHIP_DETECT_MAGIC_REG_ADDR) - if chip_magic_value not in self.CHIP_DETECT_MAGIC_VALUE: - actually = None - for cls in ROM_LIST: - if chip_magic_value in cls.CHIP_DETECT_MAGIC_VALUE: - actually = cls - break - if warnings and actually is None: - print( - "WARNING: This chip doesn't appear to be a %s " - "(chip magic value 0x%08x). " - "Probably it is unsupported by this version of esptool." - % (self.CHIP_NAME, chip_magic_value) - ) - else: - raise FatalError( - "This chip is %s not %s. Wrong --chip argument?" - % (actually.CHIP_NAME, self.CHIP_NAME) - ) except UnsupportedCommandError: self.secure_download_mode = True + # Check if chip supports reading chip ID from the get_security_info command try: - self.check_chip_id() - except UnsupportedCommandError: - # Fix for ROM not responding in SDM, reconnect and try again - if self.secure_download_mode: - self._connect_attempt(mode, reset_sequence[0]) - self.check_chip_id() + chip_id = self.get_chip_id() + except (UnsupportedCommandError, struct.error, FatalError): + chip_id = None + + detected = None + chip_arg_wrong = False + + # If we can read chip ID (ESP32-S3 and later), verify the ID + if chip_id and (self.USES_MAGIC_VALUE or chip_id != self.IMAGE_CHIP_ID): + chip_arg_wrong = True + for cls in ROM_LIST: + if not cls.USES_MAGIC_VALUE and chip_id == cls.IMAGE_CHIP_ID: + detected = cls + break + # If we can't read chip ID (ESP8266, ESP32, ESP32-S2), + # try to verify the chip by magic value + elif ( + not chip_id + and not self.secure_download_mode + and (not self.USES_MAGIC_VALUE or chip_magic_value != self.MAGIC_VALUE) + ): + chip_arg_wrong = True + for cls in ROM_LIST: + if cls.USES_MAGIC_VALUE and chip_magic_value == cls.MAGIC_VALUE: + detected = cls + break + # If we can't read chip ID and the chip is in SDM (ESP32 or ESP32-S2), + # we can't verify + elif not chip_id and self.secure_download_mode: + if self.CHIP_NAME not in ["ESP32", "ESP32-S2"]: + chip_arg_wrong = True + detected = "ESP32 or ESP32-S2" else: - raise + print( + f"WARNING: Can't verify this chip is {self.CHIP_NAME} " + "because of active Secure Download Mode. " + "Please check it manually." + ) + + if chip_arg_wrong: + if warnings and detected is None: + specifier = ( + f"(read chip ID {chip_id})" + if chip_id + else f"(read chip magic value {chip_magic_value:#08x})" + ) + print( + f"WARNING: This chip doesn't appear to be an {self.CHIP_NAME} " + f"{specifier}. Probably it is unsupported by this version " + "of esptool. Will attempt to continue anyway." + ) + else: + chip_type = ( + detected if isinstance(detected, str) else detected.CHIP_NAME + ) + raise FatalError( + f"This chip is {chip_type}, not {self.CHIP_NAME}. " + "Wrong --chip argument?" + ) self._post_connect() def _post_connect(self): @@ -1002,7 +1032,6 @@ def get_security_info(self): "api_version": None if esp32s2 else res[10], } - @esp32s3_or_newer_function_only def get_chip_id(self): if self.cache["chip_id"] is None: res = self.check_command( @@ -1552,23 +1581,6 @@ def soft_reset(self, stay_in_bootloader): # in the stub loader self.command(self.ESP_RUN_USER_CODE, wait_response=False) - def check_chip_id(self): - try: - chip_id = self.get_chip_id() - if chip_id != self.IMAGE_CHIP_ID: - print( - "WARNING: Chip ID {} ({}) doesn't match expected Chip ID {}. " - "esptool may not work correctly.".format( - chip_id, - self.UNSUPPORTED_CHIPS.get(chip_id, "Unknown"), - self.IMAGE_CHIP_ID, - ) - ) - # Try to flash anyways by disabling stub - self.stub_is_disabled = True - except NotImplementedInROMError: - pass - def slip_reader(port, trace_function): """Generator to read SLIP packets from a serial port. diff --git a/esptool/targets/esp32.py b/esptool/targets/esp32.py index aef531a0d..21d28ef9e 100644 --- a/esptool/targets/esp32.py +++ b/esptool/targets/esp32.py @@ -18,7 +18,7 @@ class ESP32ROM(ESPLoader): IMAGE_CHIP_ID = 0 IS_STUB = False - CHIP_DETECT_MAGIC_VALUE = [0x00F01D83] + MAGIC_VALUE = 0x00F01D83 IROM_MAP_START = 0x400D0000 IROM_MAP_END = 0x40400000 diff --git a/esptool/targets/esp32c2.py b/esptool/targets/esp32c2.py index 45e6dd5af..08a7ad1ce 100644 --- a/esptool/targets/esp32c2.py +++ b/esptool/targets/esp32c2.py @@ -21,9 +21,6 @@ class ESP32C2ROM(ESP32C3ROM): DROM_MAP_START = 0x3C000000 DROM_MAP_END = 0x3C400000 - # Magic value for ESP32C2 ECO0 , ECO1 and ECO4 respectively - CHIP_DETECT_MAGIC_VALUE = [0x6F51306F, 0x7C41A06F, 0x0C21E06F] - EFUSE_BASE = 0x60008800 EFUSE_BLOCK2_ADDR = EFUSE_BASE + 0x040 MAC_EFUSE_REG = EFUSE_BASE + 0x040 diff --git a/esptool/targets/esp32c3.py b/esptool/targets/esp32c3.py index 8abdae866..bf24952ad 100644 --- a/esptool/targets/esp32c3.py +++ b/esptool/targets/esp32c3.py @@ -30,10 +30,9 @@ class ESP32C3ROM(ESP32ROM): SPI_ADDR_REG_MSB = False - BOOTLOADER_FLASH_OFFSET = 0x0 + USES_MAGIC_VALUE = False - # Magic values for ESP32-C3 eco 1+2, eco 3, eco 6, and eco 7 respectively - CHIP_DETECT_MAGIC_VALUE = [0x6921506F, 0x1B31506F, 0x4881606F, 0x4361606F] + BOOTLOADER_FLASH_OFFSET = 0x0 UART_DATE_REG_ADDR = 0x60000000 + 0x7C diff --git a/esptool/targets/esp32c5.py b/esptool/targets/esp32c5.py index 4dba3e6d8..79f44d086 100644 --- a/esptool/targets/esp32c5.py +++ b/esptool/targets/esp32c5.py @@ -54,9 +54,6 @@ class ESP32C5ROM(ESP32C6ROM): UARTDEV_BUF_NO = 0x4085F51C # Variable in ROM .bss which indicates the port in use - # Magic values for ESP32C5 ECO0 and ECO1, respectively - CHIP_DETECT_MAGIC_VALUE = [0x1101406F, 0x63E1406F] - FLASH_FREQUENCY = { "80m": 0xF, "40m": 0x0, diff --git a/esptool/targets/esp32c5beta3.py b/esptool/targets/esp32c5beta3.py index 66b09ce66..86f6aef73 100644 --- a/esptool/targets/esp32c5beta3.py +++ b/esptool/targets/esp32c5beta3.py @@ -19,9 +19,6 @@ class ESP32C5BETA3ROM(ESP32C6ROM): DROM_MAP_START = 0x41000000 DROM_MAP_END = 0x41800000 - # Magic value for ESP32C5(beta3) - CHIP_DETECT_MAGIC_VALUE = [0xE10D8082] - FLASH_FREQUENCY = { "80m": 0xF, "40m": 0x0, diff --git a/esptool/targets/esp32c6.py b/esptool/targets/esp32c6.py index a7e620ac3..1de4e812f 100644 --- a/esptool/targets/esp32c6.py +++ b/esptool/targets/esp32c6.py @@ -21,9 +21,6 @@ class ESP32C6ROM(ESP32C3ROM): BOOTLOADER_FLASH_OFFSET = 0x0 - # Magic value for ESP32C6 - CHIP_DETECT_MAGIC_VALUE = [0x2CE0806F] - SPI_REG_BASE = 0x60003000 SPI_USR_OFFS = 0x18 SPI_USR1_OFFS = 0x1C diff --git a/esptool/targets/esp32c61.py b/esptool/targets/esp32c61.py index 740500a7a..066b5b861 100644 --- a/esptool/targets/esp32c61.py +++ b/esptool/targets/esp32c61.py @@ -12,9 +12,6 @@ class ESP32C61ROM(ESP32C6ROM): CHIP_NAME = "ESP32-C61" IMAGE_CHIP_ID = 20 - # Magic value for ESP32C61 - CHIP_DETECT_MAGIC_VALUE = [0x33F0206F, 0x2421606F] - UART_DATE_REG_ADDR = 0x60000000 + 0x7C EFUSE_BASE = 0x600B4800 diff --git a/esptool/targets/esp32c6beta.py b/esptool/targets/esp32c6beta.py index b6e100bb4..d2a740cd0 100644 --- a/esptool/targets/esp32c6beta.py +++ b/esptool/targets/esp32c6beta.py @@ -10,8 +10,6 @@ class ESP32C6BETAROM(ESP32C3ROM): CHIP_NAME = "ESP32-C6(beta)" IMAGE_CHIP_ID = 7 - CHIP_DETECT_MAGIC_VALUE = [0x0DA1806F] - UART_DATE_REG_ADDR = 0x00000500 def get_chip_description(self): diff --git a/esptool/targets/esp32h2.py b/esptool/targets/esp32h2.py index 65ea03751..2bd7bfef2 100644 --- a/esptool/targets/esp32h2.py +++ b/esptool/targets/esp32h2.py @@ -14,9 +14,6 @@ class ESP32H2ROM(ESP32C6ROM): CHIP_NAME = "ESP32-H2" IMAGE_CHIP_ID = 16 - # Magic value for ESP32H2 - CHIP_DETECT_MAGIC_VALUE = [0xD7B73E80] - DR_REG_LP_WDT_BASE = 0x600B1C00 RTC_CNTL_WDTCONFIG0_REG = DR_REG_LP_WDT_BASE + 0x0 # LP_WDT_RWDT_CONFIG0_REG RTC_CNTL_WDTCONFIG1_REG = DR_REG_LP_WDT_BASE + 0x0004 # LP_WDT_RWDT_CONFIG1_REG diff --git a/esptool/targets/esp32h2beta1.py b/esptool/targets/esp32h2beta1.py index 999a16e08..984506f2b 100644 --- a/esptool/targets/esp32h2beta1.py +++ b/esptool/targets/esp32h2beta1.py @@ -31,8 +31,6 @@ class ESP32H2BETA1ROM(ESP32C3ROM): BOOTLOADER_FLASH_OFFSET = 0x0 - CHIP_DETECT_MAGIC_VALUE = [0xCA26CC22] - UART_DATE_REG_ADDR = 0x60000000 + 0x7C EFUSE_BASE = 0x6001A000 diff --git a/esptool/targets/esp32h2beta2.py b/esptool/targets/esp32h2beta2.py index 6fa8f587c..6f972d719 100644 --- a/esptool/targets/esp32h2beta2.py +++ b/esptool/targets/esp32h2beta2.py @@ -10,8 +10,6 @@ class ESP32H2BETA2ROM(ESP32H2BETA1ROM): CHIP_NAME = "ESP32-H2(beta2)" IMAGE_CHIP_ID = 14 - CHIP_DETECT_MAGIC_VALUE = [0x6881B06F] - def get_chip_description(self): chip_name = { 1: "ESP32-H2(beta2)", diff --git a/esptool/targets/esp32p4.py b/esptool/targets/esp32p4.py index 1f60b69d6..13dadd0cb 100644 --- a/esptool/targets/esp32p4.py +++ b/esptool/targets/esp32p4.py @@ -22,8 +22,6 @@ class ESP32P4ROM(ESP32ROM): BOOTLOADER_FLASH_OFFSET = 0x2000 # First 2 sectors are reserved for FE purposes - CHIP_DETECT_MAGIC_VALUE = [0x0, 0x0ADDBAD0] - UART_DATE_REG_ADDR = 0x500CA000 + 0x8C EFUSE_BASE = 0x5012D000 @@ -40,6 +38,8 @@ class ESP32P4ROM(ESP32ROM): SPI_ADDR_REG_MSB = False + USES_MAGIC_VALUE = False + EFUSE_RD_REG_BASE = EFUSE_BASE + 0x030 # BLOCK0 read base address EFUSE_PURPOSE_KEY0_REG = EFUSE_BASE + 0x34 diff --git a/esptool/targets/esp32s2.py b/esptool/targets/esp32s2.py index 9a8379114..0f226f343 100644 --- a/esptool/targets/esp32s2.py +++ b/esptool/targets/esp32s2.py @@ -20,7 +20,7 @@ class ESP32S2ROM(ESP32ROM): DROM_MAP_START = 0x3F000000 DROM_MAP_END = 0x3F3F0000 - CHIP_DETECT_MAGIC_VALUE = [0x000007C6] + MAGIC_VALUE = 0x000007C6 SPI_REG_BASE = 0x3F402000 SPI_USR_OFFS = 0x18 diff --git a/esptool/targets/esp32s3.py b/esptool/targets/esp32s3.py index 5d811e5c3..7ae97a012 100644 --- a/esptool/targets/esp32s3.py +++ b/esptool/targets/esp32s3.py @@ -16,8 +16,6 @@ class ESP32S3ROM(ESP32ROM): IMAGE_CHIP_ID = 9 - CHIP_DETECT_MAGIC_VALUE = [0x9] - IROM_MAP_START = 0x42000000 IROM_MAP_END = 0x44000000 DROM_MAP_START = 0x3C000000 @@ -35,6 +33,8 @@ class ESP32S3ROM(ESP32ROM): SPI_ADDR_REG_MSB = False + USES_MAGIC_VALUE = False + BOOTLOADER_FLASH_OFFSET = 0x0 SUPPORTS_ENCRYPTED_FLASH = True diff --git a/esptool/targets/esp32s3beta2.py b/esptool/targets/esp32s3beta2.py index f91bb3cb2..6834210cf 100644 --- a/esptool/targets/esp32s3beta2.py +++ b/esptool/targets/esp32s3beta2.py @@ -10,8 +10,6 @@ class ESP32S3BETA2ROM(ESP32S3ROM): CHIP_NAME = "ESP32-S3(beta2)" IMAGE_CHIP_ID = 4 - CHIP_DETECT_MAGIC_VALUE = [0xEB004136] - EFUSE_BASE = 0x6001A000 # BLOCK0 read base address diff --git a/esptool/targets/esp8266.py b/esptool/targets/esp8266.py index 9f8d7c17d..ee1cf0f30 100644 --- a/esptool/targets/esp8266.py +++ b/esptool/targets/esp8266.py @@ -13,7 +13,7 @@ class ESP8266ROM(ESPLoader): CHIP_NAME = "ESP8266" IS_STUB = False - CHIP_DETECT_MAGIC_VALUE = [0xFFF0C101] + MAGIC_VALUE = 0xFFF0C101 # OTP ROM addresses ESP_OTP_MAC0 = 0x3FF00050 From f3e2248f755caaf80a12d351399f60a5c25244a2 Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:41:18 -0300 Subject: [PATCH 4/6] ci(armv7): Add missing packages to build --- .github/workflows/build_esptool.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build_esptool.yml b/.github/workflows/build_esptool.yml index 6225f8f18..3705d4dd0 100644 --- a/.github/workflows/build_esptool.yml +++ b/.github/workflows/build_esptool.yml @@ -31,7 +31,7 @@ jobs: dockerRunArgs: --volume "${PWD}/${{ env.DISTPATH }}:/${{ env.DISTPATH }}" install: | apt-get update -y - apt-get install -y python3 python3-pip python3-setuptools libffi-dev + apt-get install --ignore-missing -y python3 python3-pip python3-setuptools python-is-python3 pkg-config openssl libffi-dev libssl-dev cargo rustc run: | adduser --disabled-password --gecos "" builder chmod -R a+rwx /home/runner/work/esptool @@ -48,13 +48,13 @@ jobs: pyinstaller --distpath ./${{ env.DISTPATH }} -F --icon=ci/espressif.ico --add-data="${{ env.EFUSE_DIR }}*.yaml:${{ env.EFUSE_DIR }}" espefuse.py pyinstaller --distpath ./${{ env.DISTPATH }} -F --icon=ci/espressif.ico espsecure.py pyinstaller --distpath ./${{ env.DISTPATH }} -F --icon=ci/espressif.ico esp_rfc2217_server.py - + # Test binaries - ./${{ env.DISTPATH }}/esptool -h - ./${{ env.DISTPATH }}/espefuse -h - ./${{ env.DISTPATH }}/espsecure -h - ./${{ env.DISTPATH }}/esp_rfc2217_server -h - + ./${{ env.DISTPATH }}/esptool -h || exit 1 + ./${{ env.DISTPATH }}/espefuse -h || exit 1 + ./${{ env.DISTPATH }}/espsecure -h || exit 1 + ./${{ env.DISTPATH }}/esp_rfc2217_server -h || exit 1 + # Add license and readme mv LICENSE README.md ./${{ env.DISTPATH }} From 7681ec0b7e76847ad4f93b21460ed593f2cf8b26 Mon Sep 17 00:00:00 2001 From: Peter Dragun Date: Wed, 11 Dec 2024 12:42:09 +0100 Subject: [PATCH 5/6] test(espsecure): add parametrization and split signing tests Closes https://github.com/espressif/esptool/issues/1013 --- ...ed_v2.bin => bootloader_signed_v2_rsa.bin} | Bin ...m => ecdsa256_secure_boot_signing_key.pem} | 0 ... => ecdsa256_secure_boot_signing_key2.pem} | 0 ...cdsa256_secure_boot_signing_key_pkcs8.pem} | 0 ...> ecdsa256_secure_boot_signing_pubkey.pem} | 0 ... ecdsa256_secure_boot_signing_pubkey2.pem} | 0 test/test_espsecure.py | 395 ++++++------------ 7 files changed, 134 insertions(+), 261 deletions(-) rename test/secure_images/{bootloader_signed_v2.bin => bootloader_signed_v2_rsa.bin} (100%) rename test/secure_images/{ecdsa_secure_boot_signing_key.pem => ecdsa256_secure_boot_signing_key.pem} (100%) rename test/secure_images/{ecdsa_secure_boot_signing_key2.pem => ecdsa256_secure_boot_signing_key2.pem} (100%) rename test/secure_images/{ecdsa_secure_boot_signing_key_pkcs8.pem => ecdsa256_secure_boot_signing_key_pkcs8.pem} (100%) rename test/secure_images/{ecdsa_secure_boot_signing_pubkey.pem => ecdsa256_secure_boot_signing_pubkey.pem} (100%) rename test/secure_images/{ecdsa_secure_boot_signing_pubkey2.pem => ecdsa256_secure_boot_signing_pubkey2.pem} (100%) diff --git a/test/secure_images/bootloader_signed_v2.bin b/test/secure_images/bootloader_signed_v2_rsa.bin similarity index 100% rename from test/secure_images/bootloader_signed_v2.bin rename to test/secure_images/bootloader_signed_v2_rsa.bin diff --git a/test/secure_images/ecdsa_secure_boot_signing_key.pem b/test/secure_images/ecdsa256_secure_boot_signing_key.pem similarity index 100% rename from test/secure_images/ecdsa_secure_boot_signing_key.pem rename to test/secure_images/ecdsa256_secure_boot_signing_key.pem diff --git a/test/secure_images/ecdsa_secure_boot_signing_key2.pem b/test/secure_images/ecdsa256_secure_boot_signing_key2.pem similarity index 100% rename from test/secure_images/ecdsa_secure_boot_signing_key2.pem rename to test/secure_images/ecdsa256_secure_boot_signing_key2.pem diff --git a/test/secure_images/ecdsa_secure_boot_signing_key_pkcs8.pem b/test/secure_images/ecdsa256_secure_boot_signing_key_pkcs8.pem similarity index 100% rename from test/secure_images/ecdsa_secure_boot_signing_key_pkcs8.pem rename to test/secure_images/ecdsa256_secure_boot_signing_key_pkcs8.pem diff --git a/test/secure_images/ecdsa_secure_boot_signing_pubkey.pem b/test/secure_images/ecdsa256_secure_boot_signing_pubkey.pem similarity index 100% rename from test/secure_images/ecdsa_secure_boot_signing_pubkey.pem rename to test/secure_images/ecdsa256_secure_boot_signing_pubkey.pem diff --git a/test/secure_images/ecdsa_secure_boot_signing_pubkey2.pem b/test/secure_images/ecdsa256_secure_boot_signing_pubkey2.pem similarity index 100% rename from test/secure_images/ecdsa_secure_boot_signing_pubkey2.pem rename to test/secure_images/ecdsa256_secure_boot_signing_pubkey2.pem diff --git a/test/test_espsecure.py b/test/test_espsecure.py index 6fc2cb319..bfe3b3435 100755 --- a/test/test_espsecure.py +++ b/test/test_espsecure.py @@ -173,14 +173,14 @@ def _test_sign_v1_data(self, key_name): os.unlink(output_file.name) def test_sign_v1_data(self): - self._test_sign_v1_data("ecdsa_secure_boot_signing_key.pem") + self._test_sign_v1_data("ecdsa256_secure_boot_signing_key.pem") def test_sign_v1_data_pkcs8(self): - self._test_sign_v1_data("ecdsa_secure_boot_signing_key_pkcs8.pem") + self._test_sign_v1_data("ecdsa256_secure_boot_signing_key_pkcs8.pem") def test_sign_v1_with_pre_calculated_signature(self): # Sign using pre-calculated signature + Verify - signing_pubkey = "ecdsa_secure_boot_signing_pubkey.pem" + signing_pubkey = "ecdsa256_secure_boot_signing_pubkey.pem" pre_calculated_signature = "pre_calculated_bootloader_signature.bin" try: @@ -206,34 +206,29 @@ def test_sign_v1_with_pre_calculated_signature(self): output_file.close() os.unlink(output_file.name) - def test_sign_v2_data(self): - signing_keys = [ - "rsa_secure_boot_signing_key.pem", - "ecdsa192_secure_boot_signing_key.pem", - "ecdsa_secure_boot_signing_key.pem", - "ecdsa384_secure_boot_signing_key.pem", - ] - for key in signing_keys: - try: - output_file = tempfile.NamedTemporaryFile(delete=False) - args = self.SignArgs( - "2", - [self._open(key)], - output_file.name, - False, - False, - None, - None, - None, - self._open("bootloader_unsigned_v2.bin"), - ) - espsecure.sign_data(args) + @pytest.mark.parametrize("scheme", ["rsa", "ecdsa192", "ecdsa256", "ecdsa384"]) + def test_sign_v2_data(self, scheme): + key = f"{scheme}_secure_boot_signing_key.pem" + try: + output_file = tempfile.NamedTemporaryFile(delete=False) + args = self.SignArgs( + "2", + [self._open(key)], + output_file.name, + False, + False, + None, + None, + None, + self._open("bootloader_unsigned_v2.bin"), + ) + espsecure.sign_data(args) - args = self.VerifyArgs("2", False, None, self._open(key), output_file) - espsecure.verify_signature(args) - finally: - output_file.close() - os.unlink(output_file.name) + args = self.VerifyArgs("2", False, None, self._open(key), output_file) + espsecure.verify_signature(args) + finally: + output_file.close() + os.unlink(output_file.name) def test_sign_v2_multiple_keys(self): # 3 keys + Verify with 3rd key @@ -290,7 +285,7 @@ def test_sign_v2_multiple_keys(self): def test_sign_v2_append_signatures(self): # Append signatures + Verify with an appended key - # (bootloader_signed_v2.bin already signed with rsa_secure_boot_signing_key.pem) + # (bootloader_signed_v2_rsa.bin already signed with rsa_secure_boot_signing_key.pem) try: output_file = tempfile.NamedTemporaryFile(delete=False) args = self.SignArgs( @@ -305,7 +300,7 @@ def test_sign_v2_append_signatures(self): None, None, None, - self._open("bootloader_signed_v2.bin"), + self._open("bootloader_signed_v2_rsa.bin"), ) espsecure.sign_data(args) @@ -355,7 +350,7 @@ def test_sign_v2_append_signatures_multiple_steps(self): None, None, None, - self._open("bootloader_signed_v2.bin"), + self._open("bootloader_signed_v2_rsa.bin"), ) espsecure.sign_data(args) @@ -406,43 +401,31 @@ def test_sign_v2_append_signatures_multiple_steps(self): output_file2.close() os.unlink(output_file2.name) - def test_sign_v2_with_pre_calculated_signature(self): + @pytest.mark.parametrize("scheme", ["rsa", "ecdsa192", "ecdsa256", "ecdsa384"]) + def test_sign_v2_with_pre_calculated_signature(self, scheme): # Sign using pre-calculated signature + Verify - signing_keys = [ - "rsa_secure_boot_signing_pubkey.pem", - "ecdsa192_secure_boot_signing_pubkey.pem", - "ecdsa_secure_boot_signing_pubkey.pem", - "ecdsa384_secure_boot_signing_pubkey.pem", - ] - pre_calculated_signatures = [ - "pre_calculated_bootloader_signature_rsa.bin", - "pre_calculated_bootloader_signature_ecdsa192.bin", - "pre_calculated_bootloader_signature_ecdsa256.bin", - "pre_calculated_bootloader_signature_ecdsa384.bin", - ] - for pub_key, signature in zip(signing_keys, pre_calculated_signatures): - try: - output_file = tempfile.NamedTemporaryFile(delete=False) - args = self.SignArgs( - "2", - None, - output_file.name, - False, - False, - None, - [self._open(pub_key)], - [self._open(signature)], - self._open("bootloader_unsigned_v2.bin"), - ) - espsecure.sign_data(args) + pub_key = f"{scheme}_secure_boot_signing_pubkey.pem" + signature = f"pre_calculated_bootloader_signature_{scheme}.bin" + try: + output_file = tempfile.NamedTemporaryFile(delete=False) + args = self.SignArgs( + "2", + None, + output_file.name, + False, + False, + None, + [self._open(pub_key)], + [self._open(signature)], + self._open("bootloader_unsigned_v2.bin"), + ) + espsecure.sign_data(args) - args = self.VerifyArgs( - "2", False, None, self._open(pub_key), output_file - ) - espsecure.verify_signature(args) - finally: - output_file.close() - os.unlink(output_file.name) + args = self.VerifyArgs("2", False, None, self._open(pub_key), output_file) + espsecure.verify_signature(args) + finally: + output_file.close() + os.unlink(output_file.name) def test_sign_v2_with_multiple_pre_calculated_signatures(self): # Sign using multiple pre-calculated signatures + Verify @@ -479,130 +462,77 @@ def test_sign_v2_with_multiple_pre_calculated_signatures(self): output_file.close() os.unlink(output_file.name) - def test_verify_signature_signing_key(self): - # correct key v1 - args = self.VerifyArgs( - "1", - False, - None, - self._open("ecdsa_secure_boot_signing_key.pem"), - self._open("bootloader_signed.bin"), - ) - espsecure.verify_signature(args) - - # correct key v2 - args = self.VerifyArgs( - "2", - False, - None, - self._open("rsa_secure_boot_signing_key.pem"), - self._open("bootloader_signed_v2.bin"), - ) - espsecure.verify_signature(args) - - # correct key v2 (ecdsa384) - args = self.VerifyArgs( - "2", - False, - None, - self._open("ecdsa384_secure_boot_signing_key.pem"), - self._open("bootloader_signed_v2_ecdsa384.bin"), - ) - espsecure.verify_signature(args) - - # correct key v2 (ecdsa256) - args = self.VerifyArgs( - "2", - False, - None, - self._open("ecdsa_secure_boot_signing_key.pem"), - self._open("bootloader_signed_v2_ecdsa256.bin"), - ) - espsecure.verify_signature(args) - - # correct key v2 (ecdsa192) + @pytest.mark.parametrize( + "version, keyfile, datafile", + [ + ("1", "ecdsa256_secure_boot_signing_key.pem", "bootloader_signed.bin"), + ("2", "rsa_secure_boot_signing_key.pem", "bootloader_signed_v2_rsa.bin"), + ( + "2", + "ecdsa384_secure_boot_signing_key.pem", + "bootloader_signed_v2_ecdsa384.bin", + ), + ( + "2", + "ecdsa256_secure_boot_signing_key.pem", + "bootloader_signed_v2_ecdsa256.bin", + ), + ( + "2", + "ecdsa192_secure_boot_signing_key.pem", + "bootloader_signed_v2_ecdsa192.bin", + ), + ], + ids=["v1", "v2_rsa", "v2_ecdsa384", "v2_ecdsa256", "v2_ecdsa192"], + ) + def test_verify_signature_correct_key(self, version, keyfile, datafile): args = self.VerifyArgs( - "2", + version, False, None, - self._open("ecdsa192_secure_boot_signing_key.pem"), - self._open("bootloader_signed_v2_ecdsa192.bin"), + self._open(keyfile), + self._open(datafile), ) espsecure.verify_signature(args) - # wrong key v1 + def test_verify_signature_wrong_key_v1(self): args = self.VerifyArgs( "1", False, None, - self._open("ecdsa_secure_boot_signing_key2.pem"), + self._open("ecdsa256_secure_boot_signing_key2.pem"), self._open("bootloader_signed.bin"), ) with pytest.raises(esptool.FatalError) as cm: espsecure.verify_signature(args) assert "Signature is not valid" in str(cm.value) - # wrong key v2 + @pytest.mark.parametrize("scheme", ["rsa", "ecdsa192", "ecdsa256", "ecdsa384"]) + def test_verify_signature_wrong_key_v2(self, scheme): args = self.VerifyArgs( "2", False, None, - self._open("rsa_secure_boot_signing_key2.pem"), - self._open("bootloader_signed_v2.bin"), + self._open(f"{scheme}_secure_boot_signing_key2.pem"), + self._open(f"bootloader_signed_v2_{scheme}.bin"), ) with pytest.raises(esptool.FatalError) as cm: espsecure.verify_signature(args) assert "Signature could not be verified with the provided key." in str(cm.value) - # right key, wrong scheme (ecdsa256, v2) + def test_verify_signature_wrong_scheme(self): args = self.VerifyArgs( "2", False, None, - self._open("ecdsa_secure_boot_signing_key.pem"), + self._open("ecdsa256_secure_boot_signing_key.pem"), self._open("bootloader_signed.bin"), ) with pytest.raises(esptool.FatalError) as cm: espsecure.verify_signature(args) assert "Invalid datafile" in str(cm.value) - # wrong key v2 (ecdsa384) - args = self.VerifyArgs( - "2", - False, - None, - self._open("ecdsa384_secure_boot_signing_key2.pem"), - self._open("bootloader_signed_v2_ecdsa384.bin"), - ) - with pytest.raises(esptool.FatalError) as cm: - espsecure.verify_signature(args) - assert "Signature could not be verified with the provided key." in str(cm.value) - - # wrong key v2 (ecdsa256) - args = self.VerifyArgs( - "2", - False, - None, - self._open("ecdsa_secure_boot_signing_key2.pem"), - self._open("bootloader_signed_v2_ecdsa256.bin"), - ) - with pytest.raises(esptool.FatalError) as cm: - espsecure.verify_signature(args) - assert "Signature could not be verified with the provided key." in str(cm.value) - - # wrong key v2 (ecdsa192) - args = self.VerifyArgs( - "2", - False, - None, - self._open("ecdsa192_secure_boot_signing_key2.pem"), - self._open("bootloader_signed_v2_ecdsa192.bin"), - ) - with pytest.raises(esptool.FatalError) as cm: - espsecure.verify_signature(args) - assert "Signature could not be verified with the provided key." in str(cm.value) - - # multi-signed wrong key v2 + def test_verify_signature_multi_signed_wrong_key(self): args = self.VerifyArgs( "2", False, @@ -614,118 +544,65 @@ def test_verify_signature_signing_key(self): espsecure.verify_signature(args) assert "Signature could not be verified with the provided key." in str(cm.value) - def test_verify_signature_public_key(self): - # correct key v1 - args = self.VerifyArgs( - "1", - False, - None, - self._open("ecdsa_secure_boot_signing_pubkey.pem"), - self._open("bootloader_signed.bin"), - ) - espsecure.verify_signature(args) - - # correct key v2 - args = self.VerifyArgs( - "2", - False, - None, - self._open("rsa_secure_boot_signing_pubkey.pem"), - self._open("bootloader_signed_v2.bin"), - ) - espsecure.verify_signature(args) - - # correct key v2 (ecdsa384) - args = self.VerifyArgs( - "2", - False, - None, - self._open("ecdsa384_secure_boot_signing_pubkey.pem"), - self._open("bootloader_signed_v2_ecdsa384.bin"), - ) - espsecure.verify_signature(args) - - # correct key v2 (ecdsa256) - args = self.VerifyArgs( - "2", - False, - None, - self._open("ecdsa_secure_boot_signing_pubkey.pem"), - self._open("bootloader_signed_v2_ecdsa256.bin"), - ) - espsecure.verify_signature(args) - - # correct key v2 (ecdsa192) + @pytest.mark.parametrize( + "version, keyfile, datafile", + [ + ("1", "ecdsa256_secure_boot_signing_pubkey.pem", "bootloader_signed.bin"), + ("2", "rsa_secure_boot_signing_pubkey.pem", "bootloader_signed_v2_rsa.bin"), + ( + "2", + "ecdsa384_secure_boot_signing_pubkey.pem", + "bootloader_signed_v2_ecdsa384.bin", + ), + ( + "2", + "ecdsa256_secure_boot_signing_pubkey.pem", + "bootloader_signed_v2_ecdsa256.bin", + ), + ( + "2", + "ecdsa192_secure_boot_signing_pubkey.pem", + "bootloader_signed_v2_ecdsa192.bin", + ), + ], + ids=["v1", "v2_rsa", "v2_ecdsa384", "v2_ecdsa256", "v2_ecdsa192"], + ) + def test_verify_signature_correct_pubkey(self, version, keyfile, datafile): args = self.VerifyArgs( - "2", + version, False, None, - self._open("ecdsa192_secure_boot_signing_pubkey.pem"), - self._open("bootloader_signed_v2_ecdsa192.bin"), + self._open(keyfile), + self._open(datafile), ) espsecure.verify_signature(args) - # wrong key v1 + def test_verify_signature_wrong_pubkey_v1(self): args = self.VerifyArgs( "1", False, None, - self._open("ecdsa_secure_boot_signing_pubkey2.pem"), + self._open("ecdsa256_secure_boot_signing_pubkey2.pem"), self._open("bootloader_signed.bin"), ) with pytest.raises(esptool.FatalError) as cm: espsecure.verify_signature(args) assert "Signature is not valid" in str(cm.value) - # wrong key v2 - args = self.VerifyArgs( - "2", - False, - None, - self._open("rsa_secure_boot_signing_pubkey2.pem"), - self._open("bootloader_signed_v2.bin"), - ) - with pytest.raises(esptool.FatalError) as cm: - espsecure.verify_signature(args) - assert "Signature could not be verified with the provided key." in str(cm.value) - - # wrong key v2 (ecdsa384) - args = self.VerifyArgs( - "2", - False, - None, - self._open("ecdsa384_secure_boot_signing_pubkey2.pem"), - self._open("bootloader_signed_v2_ecdsa384.bin"), - ) - with pytest.raises(esptool.FatalError) as cm: - espsecure.verify_signature(args) - assert "Signature could not be verified with the provided key." in str(cm.value) - - # wrong key v2 (ecdsa256) - args = self.VerifyArgs( - "2", - False, - None, - self._open("ecdsa_secure_boot_signing_pubkey2.pem"), - self._open("bootloader_signed_v2_ecdsa256.bin"), - ) - with pytest.raises(esptool.FatalError) as cm: - espsecure.verify_signature(args) - assert "Signature could not be verified with the provided key." in str(cm.value) - - # wrong key v2 (ecdsa192) + @pytest.mark.parametrize("scheme", ["rsa", "ecdsa192", "ecdsa256", "ecdsa384"]) + def test_verify_signature_wrong_pubkey_v2(self, scheme): args = self.VerifyArgs( "2", False, None, - self._open("ecdsa192_secure_boot_signing_pubkey2.pem"), - self._open("bootloader_signed_v2_ecdsa192.bin"), + self._open(f"{scheme}_secure_boot_signing_pubkey2.pem"), + self._open(f"bootloader_signed_v2_{scheme}.bin"), ) with pytest.raises(esptool.FatalError) as cm: espsecure.verify_signature(args) assert "Signature could not be verified with the provided key." in str(cm.value) - # multi-signed wrong key v2 + def test_verify_signature_multi_signed_wrong_pubkey(self): args = self.VerifyArgs( "2", False, @@ -740,12 +617,12 @@ def test_verify_signature_public_key(self): def test_extract_binary_public_key(self): with tempfile.NamedTemporaryFile() as pub_keyfile, tempfile.NamedTemporaryFile() as pub_keyfile2: # noqa E501 args = self.ExtractKeyArgs( - "1", self._open("ecdsa_secure_boot_signing_key.pem"), pub_keyfile + "1", self._open("ecdsa256_secure_boot_signing_key.pem"), pub_keyfile ) espsecure.extract_public_key(args) args = self.ExtractKeyArgs( - "1", self._open("ecdsa_secure_boot_signing_key2.pem"), pub_keyfile2 + "1", self._open("ecdsa256_secure_boot_signing_key2.pem"), pub_keyfile2 ) espsecure.extract_public_key(args) @@ -766,25 +643,21 @@ def test_extract_binary_public_key(self): espsecure.verify_signature(args) assert "Signature is not valid" in str(cm.value) - def test_generate_and_extract_key_v2(self): + @pytest.mark.parametrize("scheme", ["rsa3072", "ecdsa192", "ecdsa256", "ecdsa384"]) + def test_generate_and_extract_key_v2(self, scheme): with tempfile.TemporaryDirectory() as keydir: # keyfile cannot exist before generation -> tempfile.NamedTemporaryFile() # cannot be used for keyfile keyfile_name = os.path.join(keydir, "key.pem") - # We need to manually delete the keyfile as we are iterating over - # different schemes with the same keyfile so instead of using addCleanup, - # we remove it using os.remove at the end of each pass - for scheme in ["rsa3072", "ecdsa192", "ecdsa256", "ecdsa384"]: - args = self.GenerateKeyArgs("2", scheme, keyfile_name) - espsecure.generate_signing_key(args) + args = self.GenerateKeyArgs("2", scheme, keyfile_name) + espsecure.generate_signing_key(args) - with tempfile.NamedTemporaryFile() as pub_keyfile, open( - keyfile_name, "rb" - ) as keyfile: - args = self.ExtractKeyArgs("2", keyfile, pub_keyfile) - espsecure.extract_public_key(args) - os.remove(keyfile_name) + with tempfile.NamedTemporaryFile() as pub_keyfile, open( + keyfile_name, "rb" + ) as keyfile: + args = self.ExtractKeyArgs("2", keyfile, pub_keyfile) + espsecure.extract_public_key(args) class TestFlashEncryption(EspSecureTestCase): @@ -971,7 +844,7 @@ def test_digest_private_key(self): self.run_espsecure( "digest_private_key " - "--keyfile secure_images/ecdsa_secure_boot_signing_key.pem " + "--keyfile secure_images/ecdsa256_secure_boot_signing_key.pem " f"{outfile_name}" ) @@ -981,7 +854,7 @@ def test_digest_private_key(self): ) def test_digest_private_key_with_invalid_output(self, capsys): - fname = "secure_images/ecdsa_secure_boot_signing_key.pem" + fname = "secure_images/ecdsa256_secure_boot_signing_key.pem" with pytest.raises(subprocess.CalledProcessError): self.run_espsecure(f"digest_private_key --keyfile {fname} {fname}") From a6bceb7207f6b4d0f66945b70251eb2f6c493daf Mon Sep 17 00:00:00 2001 From: Konstantin Kondrashov Date: Tue, 5 Nov 2024 16:45:27 +0200 Subject: [PATCH 6/6] fix(save_segment): Adds segment len check the same as bootloader does --- esptool/bin_image.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/esptool/bin_image.py b/esptool/bin_image.py index 652faf24d..93cf305e0 100644 --- a/esptool/bin_image.py +++ b/esptool/bin_image.py @@ -270,13 +270,24 @@ def maybe_patch_segment_data(self, f, segment_data): ) return segment_data - def save_segment(self, f, segment, checksum=None): + def save_segment(self, f, segment, checksum=None, segment_name=None): """ Save the next segment to the image file, return next checksum value if provided """ segment_data = self.maybe_patch_segment_data(f, segment.data) - f.write(struct.pack("= SIXTEEN_MB: + raise FatalError( + f"Invalid {segment_name} segment length {segment_len:#x}. The 16 MB limit has been exceeded." + ) + f.write(struct.pack("