Skip to content

Commit efc143b

Browse files
Merge branch 'bugfix/fix_efuse_example_c2' into 'master'
fix(efuse): Fix efuse test examples Closes IDF-11263 See merge request espressif/esp-idf!32575
2 parents 3a6a6ec + cb6e820 commit efc143b

9 files changed

+54
-47
lines changed

examples/system/efuse/conftest.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
1+
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
22
# SPDX-License-Identifier: Apache-2.0
3-
3+
import json
44
import logging
55
import os
6+
import tempfile
7+
from typing import Any
68

9+
import espefuse
710
import pytest
811
from _pytest.fixtures import FixtureRequest
912
from _pytest.monkeypatch import MonkeyPatch
@@ -52,6 +55,12 @@ def bootloader_flash(self) -> None:
5255
# Restore self.app.flash files to original value
5356
self.app.flash_files = prev_flash_files
5457

58+
def erase_field_on_emul_efuse_by_name(self, efuse_names: list) -> None:
59+
pos_of_bits = []
60+
for name in efuse_names:
61+
pos_of_bits.append(self.get_efuse_offset(name))
62+
self.erase_field_on_emul_efuse(pos_of_bits)
63+
5564
def erase_field_on_emul_efuse(self, pos_of_bits: list) -> None:
5665
emul_efuse_bin_path = os.path.join(self.app.binary_path, 'emul_efuse.bin')
5766
self.dump_flash(output=emul_efuse_bin_path, partition='emul_efuse')
@@ -86,6 +95,20 @@ def erase_bit(pos_of_bit: int) -> None:
8695
self.flash()
8796
self.app.flash_files = prev_flash_files
8897

98+
def get_efuse_offset(self, efuse_name: str) -> Any:
99+
with tempfile.NamedTemporaryFile(suffix='.json') as temp_file:
100+
temp_file_path = temp_file.name
101+
espefuse.main(f'--virt -c {self.target} summary --format json --file {temp_file_path}'.split())
102+
with open(temp_file_path, 'r') as file:
103+
efuse_summary = json.load(file)
104+
if efuse_name in efuse_summary:
105+
data = efuse_summary[efuse_name]
106+
offset = int(data['word'] * 32) + data['pos']
107+
print(f'{efuse_name} offset = {offset}')
108+
return offset
109+
else:
110+
raise ValueError(f"eFuse '{efuse_name}' not found in the summary.")
111+
89112

90113
@pytest.fixture(scope='module')
91114
def monkeypatch_module(request: FixtureRequest) -> MonkeyPatch:

examples/system/efuse/main/efuse_main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
#include "esp_efuse_table.h"
1818
#include "esp_efuse_custom_table.h"
1919

20-
#if CONFIG_SECURE_BOOT
20+
#if CONFIG_SECURE_BOOT || CONFIG_IDF_TARGET_ESP32C2
2121
#include "esp_secure_boot.h"
2222
#endif
2323

24-
#if CONFIG_SECURE_FLASH_ENC_ENABLED
24+
#if CONFIG_SECURE_FLASH_ENC_ENABLED || CONFIG_IDF_TARGET_ESP32C2
2525
#include "esp_flash_encrypt.h"
2626
#endif
2727

examples/system/efuse/pytest_system_efuse_example.py

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,12 @@ def test_examples_efuse_with_virt_flash_enc_pre_loaded(dut: Dut) -> None:
174174
dut.expect('example: Done')
175175

176176
if dut.app.target == 'esp32':
177-
print(' - Flash emul_efuse with pre-loaded efuses (FLASH_CRYPT_CNT 1 -> 0)')
178-
# offset of this eFuse is taken from components/efuse/esp32/esp_efuse_table.csv
179-
FLASH_CRYPT_CNT = 20
180-
# Resets eFuse, which enables Flash encryption feature
181-
dut.serial.erase_field_on_emul_efuse([FLASH_CRYPT_CNT])
182-
elif dut.app.target == 'esp32c2':
183-
FLASH_CRYPT_CNT = 39
184-
dut.serial.erase_field_on_emul_efuse([FLASH_CRYPT_CNT])
177+
CRYPT_CNT_EFUSE_NAME = 'FLASH_CRYPT_CNT'
185178
else:
186-
# offset of this eFuse is taken from components/efuse/{target}/esp_efuse_table.csv
187-
print(' - Flash emul_efuse with pre-loaded efuses (SPI_BOOT_CRYPT_CNT 1 -> 0)')
188-
SPI_BOOT_CRYPT_CNT = 82
189-
# Resets eFuse, which enables Flash encryption feature
190-
dut.serial.erase_field_on_emul_efuse([SPI_BOOT_CRYPT_CNT])
179+
CRYPT_CNT_EFUSE_NAME = 'SPI_BOOT_CRYPT_CNT'
180+
print(f' - Flash emul_efuse with pre-loaded efuses ({CRYPT_CNT_EFUSE_NAME} 1 -> 0)')
181+
# Resets eFuse, which enables Flash encryption feature
182+
dut.serial.erase_field_on_emul_efuse_by_name([CRYPT_CNT_EFUSE_NAME])
191183

192184
print(' - Start app (flash partition_table and app)')
193185
dut.serial.write_flash_no_enc()
@@ -347,10 +339,8 @@ def test_examples_efuse_with_virt_secure_boot_v1_pre_loaded(dut: Dut) -> None:
347339
dut.expect('example: Done')
348340

349341
print(' - Flash emul_efuse with pre-loaded efuses (ABS_DONE_0 1 -> 0)')
350-
# offset of this eFuse is taken from components/efuse/esp32/esp_efuse_table.csv
351-
ABS_DONE_0 = 196
352342
# Resets eFuse, which enables Secure boot (V1) feature
353-
dut.serial.erase_field_on_emul_efuse([ABS_DONE_0])
343+
dut.serial.erase_field_on_emul_efuse_by_name(['ABS_DONE_0'])
354344

355345
print(' - Start app (flash partition_table and app)')
356346
dut.serial.flash()
@@ -453,10 +443,8 @@ def test_examples_efuse_with_virt_secure_boot_v2(dut: Dut) -> None:
453443
dut.expect('example: Done')
454444

455445
print(' - Flash emul_efuse with pre-loaded efuses (ABS_DONE_1 1 -> 0)')
456-
# offset of this eFuse is taken from components/efuse/esp32/esp_efuse_table.csv
457-
ABS_DONE_1 = 197
458446
# Resets eFuse, which enables Secure boot (V2) feature
459-
dut.serial.erase_field_on_emul_efuse([ABS_DONE_1])
447+
dut.serial.erase_field_on_emul_efuse_by_name(['ABS_DONE_1'])
460448

461449
print(' - Start app (flash partition_table and app)')
462450
dut.serial.flash()
@@ -518,10 +506,8 @@ def test_examples_efuse_with_virt_secure_boot_v2_pre_loaded(dut: Dut) -> None:
518506
dut.expect('example: Done')
519507

520508
print(' - Flash emul_efuse with pre-loaded efuses (ABS_DONE_1 1 -> 0)')
521-
# offset of this eFuse is taken from components/efuse/esp32/esp_efuse_table.csv
522-
ABS_DONE_1 = 197
523509
# Resets eFuse, which enables Secure boot (V2) feature
524-
dut.serial.erase_field_on_emul_efuse([ABS_DONE_1])
510+
dut.serial.erase_field_on_emul_efuse_by_name(['ABS_DONE_1'])
525511

526512
print(' - Start app (flash partition_table and app)')
527513
dut.serial.flash()
@@ -594,7 +580,7 @@ def test_examples_efuse_with_virt_secure_boot_v2_esp32xx(dut: Dut) -> None:
594580

595581
dut.expect('Verifying image signature...')
596582
dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
597-
if dut.app.target == 'esp32c2':
583+
if dut.app.sdkconfig.get('SECURE_SIGNED_APPS_ECDSA_V2_SCHEME'):
598584
signed_scheme = 'ECDSA'
599585
else:
600586
signed_scheme = 'RSA-PSS'
@@ -670,24 +656,19 @@ def test_example_efuse_with_virt_secure_boot_v2_esp32xx_pre_loaded(dut: Dut) ->
670656

671657
print(' - Flash emul_efuse with pre-loaded efuses (SECURE_BOOT_EN 1 -> 0, SECURE_BOOT_KEY_REVOKE[0..2] -> 0)')
672658
# offsets of eFuses are taken from components/efuse/{target}/esp_efuse_table.csv
673-
if dut.app.target == 'esp32c2':
674-
SECURE_BOOT_EN = 53
675-
dut.serial.erase_field_on_emul_efuse([SECURE_BOOT_EN])
659+
# Resets eFuse, which enables Secure boot feature
660+
# Resets eFuses, which control digest slots
661+
if dut.app.sdkconfig.get('SOC_EFUSE_REVOKE_BOOT_KEY_DIGESTS'):
662+
dut.serial.erase_field_on_emul_efuse_by_name(['SECURE_BOOT_EN', 'SECURE_BOOT_KEY_REVOKE0', 'SECURE_BOOT_KEY_REVOKE1', 'SECURE_BOOT_KEY_REVOKE2'])
676663
else:
677-
SECURE_BOOT_EN = 116
678-
SECURE_BOOT_KEY_REVOKE0 = 85
679-
SECURE_BOOT_KEY_REVOKE1 = 86
680-
SECURE_BOOT_KEY_REVOKE2 = 87
681-
# Resets eFuse, which enables Secure boot feature
682-
# Resets eFuses, which control digest slots
683-
dut.serial.erase_field_on_emul_efuse([SECURE_BOOT_EN, SECURE_BOOT_KEY_REVOKE0, SECURE_BOOT_KEY_REVOKE1, SECURE_BOOT_KEY_REVOKE2])
664+
dut.serial.erase_field_on_emul_efuse_by_name(['SECURE_BOOT_EN'])
684665

685666
print(' - Start app (flash partition_table and app)')
686667
dut.serial.flash()
687668
dut.expect('Loading virtual efuse blocks from flash')
688669

689670
dut.expect('Verifying image signature...')
690-
if dut.app.target == 'esp32c2':
671+
if dut.app.sdkconfig.get('SECURE_SIGNED_APPS_ECDSA_V2_SCHEME'):
691672
signed_scheme = 'ECDSA'
692673
else:
693674
signed_scheme = 'RSA-PSS'
@@ -981,7 +962,10 @@ def test_examples_efuse_with_virt_sb_v2_and_fe_esp32xx(dut: Dut) -> None:
981962

982963
dut.expect('Verifying image signature...')
983964
dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
984-
signed_scheme = 'ECDSA' if dut.app.target == 'esp32c2' else 'RSA-PSS'
965+
if dut.app.sdkconfig.get('SECURE_SIGNED_APPS_ECDSA_V2_SCHEME'):
966+
signed_scheme = 'ECDSA'
967+
else:
968+
signed_scheme = 'RSA-PSS'
985969
dut.expect('secure_boot_v2: Verifying with %s...' % signed_scheme)
986970
dut.expect('secure_boot_v2: Signature verified successfully!')
987971

examples/system/efuse/sdkconfig.ci.virt_sb_v2_and_fe.esp32c5

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
CONFIG_IDF_TARGET="esp32c5"
44

5-
CONFIG_PARTITION_TABLE_OFFSET=0xD000
5+
CONFIG_PARTITION_TABLE_OFFSET=0xE000
66
CONFIG_PARTITION_TABLE_CUSTOM=y
77
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="test/partitions_efuse_emul.csv"
88

99
CONFIG_SECURE_BOOT=y
1010
CONFIG_SECURE_BOOT_V2_ENABLED=y
11-
CONFIG_SECURE_BOOT_SIGNING_KEY="test/secure_boot_signing_key.pem"
11+
CONFIG_SECURE_BOOT_SIGNING_KEY="test/secure_boot_signing_key_ecdsa_nistp256.pem"
1212
CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE=y
1313

1414
CONFIG_SECURE_FLASH_ENC_ENABLED=y

examples/system/efuse/sdkconfig.ci.virt_sb_v2_and_fe.esp32c61

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="test/partitions_efuse_emul.csv"
88

99
CONFIG_SECURE_BOOT=y
1010
CONFIG_SECURE_BOOT_V2_ENABLED=y
11-
CONFIG_SECURE_BOOT_SIGNING_KEY="test/secure_boot_signing_key.pem"
11+
CONFIG_SECURE_BOOT_SIGNING_KEY="test/secure_boot_signing_key_ecdsa_nistp256.pem"
1212
CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE=y
1313

1414
CONFIG_SECURE_FLASH_ENC_ENABLED=y

examples/system/efuse/sdkconfig.ci.virt_sb_v2_and_fe.esp32p4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
CONFIG_IDF_TARGET="esp32p4"
44

5-
CONFIG_PARTITION_TABLE_OFFSET=0xD000
5+
CONFIG_PARTITION_TABLE_OFFSET=0xE000
66
CONFIG_PARTITION_TABLE_CUSTOM=y
77
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="test/partitions_efuse_emul.csv"
88

examples/system/efuse/sdkconfig.ci.virt_secure_boot_v2.esp32c5

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
CONFIG_IDF_TARGET="esp32c5"
44

5-
CONFIG_PARTITION_TABLE_OFFSET=0xC000
5+
CONFIG_PARTITION_TABLE_OFFSET=0xD000
66
CONFIG_PARTITION_TABLE_CUSTOM=y
77
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="test/partitions_efuse_emul.csv"
88

99
CONFIG_SECURE_BOOT=y
1010
CONFIG_SECURE_BOOT_V2_ENABLED=y
11-
CONFIG_SECURE_BOOT_SIGNING_KEY="test/secure_boot_signing_key.pem"
11+
CONFIG_SECURE_BOOT_SIGNING_KEY="test/secure_boot_signing_key_ecdsa_nistp256.pem"
1212
CONFIG_SECURE_INSECURE_ALLOW_DL_MODE=y
1313

1414
# IMPORTANT: ONLY VIRTUAL eFuse MODE!

examples/system/efuse/sdkconfig.ci.virt_secure_boot_v2.esp32c61

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="test/partitions_efuse_emul.csv"
88

99
CONFIG_SECURE_BOOT=y
1010
CONFIG_SECURE_BOOT_V2_ENABLED=y
11-
CONFIG_SECURE_BOOT_SIGNING_KEY="test/secure_boot_signing_key.pem"
11+
CONFIG_SECURE_BOOT_SIGNING_KEY="test/secure_boot_signing_key_ecdsa_nistp256.pem"
1212
CONFIG_SECURE_INSECURE_ALLOW_DL_MODE=y
1313

1414
# IMPORTANT: ONLY VIRTUAL eFuse MODE!

examples/system/efuse/sdkconfig.ci.virt_secure_boot_v2.esp32p4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
CONFIG_IDF_TARGET="esp32p4"
44

5-
CONFIG_PARTITION_TABLE_OFFSET=0xC000
5+
CONFIG_PARTITION_TABLE_OFFSET=0xD000
66
CONFIG_PARTITION_TABLE_CUSTOM=y
77
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="test/partitions_efuse_emul.csv"
88

0 commit comments

Comments
 (0)