efuse(esp32): Deprecate esp_efuse_burn_new_values() & esp_efuse_write_random_key()

These functions were used only for esp32 in secure_boot and flash encryption.
Use idf efuse APIs instead of efuse regs.
This commit is contained in:
Konstantin Kondrashov
2021-06-17 07:21:36 +08:00
committed by Angus Gratton
parent eca878b37f
commit f339b3fc96
86 changed files with 2785 additions and 2825 deletions

View File

@@ -30,8 +30,13 @@ try:
except ImportError:
gitlab_api = None
try:
from typing import Any, Dict, List, Optional, Tuple, Type # noqa: F401
except ImportError:
pass
def parse_encrypted_flag(args, offs, binary):
def parse_encrypted_flag(args, offs, binary): # type: (Dict, str, str) -> Any
# Find partition entries (e.g. the entries with an offset and a file)
for _, entry in args.items():
# If the current entry is a partition, we have to check whether it is
@@ -50,7 +55,7 @@ def parse_encrypted_flag(args, offs, binary):
return None
def parse_flash_settings(path, default_encryption=False):
def parse_flash_settings(path, default_encryption=False): # type: (str, bool) -> Tuple[List[Tuple[str, str]], List[Tuple[str, str]], Dict, Any]
file_name = os.path.basename(path)
# For compatibility reasons, this list contains all the files to be
@@ -104,6 +109,7 @@ def parse_flash_settings(path, default_encryption=False):
class Artifacts(object):
def __init__(self, dest_root_path, artifact_index_file, app_path, config_name, target):
# type: (str, str, str, str, str) -> None
assert gitlab_api
# at least one of app_path or config_name is not None. otherwise we can't match artifact
assert app_path or config_name
@@ -115,7 +121,7 @@ class Artifacts(object):
self.artifact_info = self._find_artifact(artifact_index, app_path, config_name, target)
@staticmethod
def _find_artifact(artifact_index, app_path, config_name, target):
def _find_artifact(artifact_index, app_path, config_name, target): # type: ignore
for artifact_info in artifact_index:
match_result = True
if app_path:
@@ -133,13 +139,13 @@ class Artifacts(object):
ret = None
return ret
def _get_app_base_path(self):
def _get_app_base_path(self): # type: () -> Any
if self.artifact_info:
return os.path.join(self.artifact_info['work_dir'], self.artifact_info['build_dir'])
else:
return None
def _get_flash_arg_file(self, base_path, job_id):
def _get_flash_arg_file(self, base_path, job_id): # type: (str, str) -> str
if self.artifact_info['build_system'] == 'cmake':
flash_arg_file = os.path.join(base_path, 'flasher_args.json')
else:
@@ -148,20 +154,24 @@ class Artifacts(object):
self.gitlab_inst.download_artifact(job_id, [flash_arg_file], self.dest_root_path)
return flash_arg_file
def _download_binary_files(self, base_path, job_id, flash_arg_file):
def _download_binary_files(self, base_path, job_id, flash_arg_file): # type: (str, str, str) -> None
# Let's ignore the second value returned (encrypt_files) as these
# files also appear in the first list
flash_files, _, _, app_name = parse_flash_settings(os.path.join(self.dest_root_path, flash_arg_file))
artifact_files = [os.path.join(base_path, p[1]) for p in flash_files]
artifact_files.append(os.path.join(base_path, app_name + '.elf'))
bootloader_path = os.path.join(base_path, 'bootloader', 'bootloader.bin')
if bootloader_path not in artifact_files:
artifact_files.append(bootloader_path)
self.gitlab_inst.download_artifact(job_id, artifact_files, self.dest_root_path)
def _download_sdkconfig_file(self, base_path, job_id):
def _download_sdkconfig_file(self, base_path, job_id): # type: (str, str) -> None
self.gitlab_inst.download_artifact(job_id, [os.path.join(os.path.dirname(base_path), 'sdkconfig')],
self.dest_root_path)
def download_artifacts(self):
def download_artifacts(self): # type: () -> Any
if not self.artifact_info:
return None
base_path = self._get_app_base_path()
@@ -176,7 +186,7 @@ class Artifacts(object):
self._download_sdkconfig_file(base_path, job_id)
return base_path
def download_artifact_files(self, file_names):
def download_artifact_files(self, file_names): # type: (List[str]) -> Any
if self.artifact_info:
base_path = os.path.join(self.artifact_info['work_dir'], self.artifact_info['build_dir'])
job_id = self.artifact_info['ci_job_id']
@@ -196,14 +206,14 @@ class Artifacts(object):
class UnitTestArtifacts(Artifacts):
BUILDS_DIR_RE = re.compile(r'^builds/')
def _get_app_base_path(self):
def _get_app_base_path(self): # type: () -> Any
if self.artifact_info:
output_dir = self.BUILDS_DIR_RE.sub('output/', self.artifact_info['build_dir'])
return os.path.join(self.artifact_info['app_dir'], output_dir)
else:
return None
def _download_sdkconfig_file(self, base_path, job_id):
def _download_sdkconfig_file(self, base_path, job_id): # type: (str, str) -> None
self.gitlab_inst.download_artifact(job_id, [os.path.join(base_path, 'sdkconfig')], self.dest_root_path)
@@ -216,17 +226,17 @@ class IDFApp(App.BaseApp):
IDF_DOWNLOAD_CONFIG_FILE = 'download.config'
IDF_FLASH_ARGS_FILE = 'flasher_args.json'
def __init__(self, app_path, config_name=None, target=None, case_group=IDFCaseGroup, artifact_cls=Artifacts):
def __init__(self, app_path, config_name=None, target=None, case_group=IDFCaseGroup, artifact_cls=Artifacts): # type: ignore
super(IDFApp, self).__init__(app_path)
self.app_path = app_path
self.config_name = config_name
self.target = target
self.idf_path = self.get_sdk_path()
self.app_path = app_path # type: (str)
self.config_name = config_name # type: (str)
self.target = target # type: (str)
self.idf_path = self.get_sdk_path() # type: (str)
self.case_group = case_group
self.artifact_cls = artifact_cls
self.binary_path = self.get_binary_path()
self.elf_file = self._get_elf_file_path()
self._elf_file_sha256 = None
self._elf_file_sha256 = None # type: (Optional[str])
assert os.path.exists(self.binary_path)
if self.IDF_DOWNLOAD_CONFIG_FILE not in os.listdir(self.binary_path):
if self.IDF_FLASH_ARGS_FILE not in os.listdir(self.binary_path):
@@ -243,7 +253,7 @@ class IDFApp(App.BaseApp):
self.flash_files, self.encrypt_files, self.flash_settings = self._parse_flash_download_config()
self.partition_table = self._parse_partition_table()
def __str__(self):
def __str__(self): # type: () -> str
parts = ['app<{}>'.format(self.app_path)]
if self.config_name:
parts.append('config<{}>'.format(self.config_name))
@@ -258,7 +268,7 @@ class IDFApp(App.BaseApp):
assert os.path.exists(idf_path)
return idf_path
def _get_sdkconfig_paths(self):
def _get_sdkconfig_paths(self): # type: () -> List[str]
"""
returns list of possible paths where sdkconfig could be found
@@ -266,7 +276,7 @@ class IDFApp(App.BaseApp):
"""
return [os.path.join(self.binary_path, 'sdkconfig'), os.path.join(self.binary_path, '..', 'sdkconfig')]
def get_sdkconfig(self):
def get_sdkconfig(self): # type: () -> Dict
"""
reads sdkconfig and returns a dictionary with all configured variables
@@ -287,10 +297,10 @@ class IDFApp(App.BaseApp):
return d
@abstractmethod
def _try_get_binary_from_local_fs(self):
def _try_get_binary_from_local_fs(self): # type: () -> Optional[str]
pass
def get_binary_path(self):
def get_binary_path(self): # type: () -> str
path = self._try_get_binary_from_local_fs()
if path:
return path
@@ -309,7 +319,7 @@ class IDFApp(App.BaseApp):
else:
raise OSError('Failed to get binary for {}'.format(self))
def _get_elf_file_path(self):
def _get_elf_file_path(self): # type: () -> str
ret = ''
file_names = os.listdir(self.binary_path)
for fn in file_names:
@@ -317,12 +327,12 @@ class IDFApp(App.BaseApp):
ret = os.path.join(self.binary_path, fn)
return ret
def _int_offs_abs_paths(self, files_list):
def _int_offs_abs_paths(self, files_list): # type: (List[tuple[str, str]]) -> List[Tuple[int, str]]
return [(int(offs, 0),
os.path.join(self.binary_path, file_path.strip()))
for (offs, file_path) in files_list]
def _parse_flash_download_config(self):
def _parse_flash_download_config(self): # type: () -> Tuple[List[tuple[int, str]], List[tuple[int, str]], Dict]
"""
Parse flash download config from build metadata files
@@ -355,7 +365,7 @@ class IDFApp(App.BaseApp):
return self._int_offs_abs_paths(flash_files), self._int_offs_abs_paths(encrypt_files), flash_settings
def _parse_partition_table(self):
def _parse_partition_table(self): # type: ignore
"""
Parse partition table contents based on app binaries
@@ -422,7 +432,7 @@ class IDFApp(App.BaseApp):
return partition_table
def get_elf_sha256(self):
def get_elf_sha256(self): # type: () -> Optional[str]
if self._elf_file_sha256:
return self._elf_file_sha256
@@ -435,19 +445,20 @@ class IDFApp(App.BaseApp):
class Example(IDFApp):
def __init__(self, app_path, config_name='default', target='esp32', case_group=ExampleGroup, artifacts_cls=Artifacts):
# type: (str, str, str, Type[ExampleGroup], Type[Artifacts]) -> None
if not config_name:
config_name = 'default'
if not target:
target = 'esp32'
super(Example, self).__init__(app_path, config_name, target, case_group, artifacts_cls)
def _get_sdkconfig_paths(self):
def _get_sdkconfig_paths(self): # type: () -> List[str]
"""
overrides the parent method to provide exact path of sdkconfig for example tests
"""
return [os.path.join(self.binary_path, '..', 'sdkconfig')]
def _try_get_binary_from_local_fs(self):
def _try_get_binary_from_local_fs(self): # type: () -> Optional[str]
# build folder of example path
path = os.path.join(self.idf_path, self.app_path, 'build')
if os.path.exists(path):
@@ -466,17 +477,19 @@ class Example(IDFApp):
return path
else:
return None
return None
class UT(IDFApp):
def __init__(self, app_path, config_name='default', target='esp32', case_group=UnitTestGroup, artifacts_cls=UnitTestArtifacts):
# type: (str, str, str, Type[UnitTestGroup], Type[UnitTestArtifacts]) -> None
if not config_name:
config_name = 'default'
if not target:
target = 'esp32'
super(UT, self).__init__(app_path, config_name, target, case_group, artifacts_cls)
def _try_get_binary_from_local_fs(self):
def _try_get_binary_from_local_fs(self): # type: () -> Optional[str]
path = os.path.join(self.idf_path, self.app_path, 'build')
if os.path.exists(path):
return path
@@ -497,16 +510,19 @@ class UT(IDFApp):
class TestApp(Example):
def __init__(self, app_path, config_name='default', target='esp32', case_group=TestAppsGroup, artifacts_cls=Artifacts):
# type: (str, str, str, Type[TestAppsGroup], Type[Artifacts]) -> None
super(TestApp, self).__init__(app_path, config_name, target, case_group, artifacts_cls)
class ComponentUTApp(TestApp):
def __init__(self, app_path, config_name='default', target='esp32', case_group=ComponentUTGroup, artifacts_cls=Artifacts):
# type: (str, str, str, Type[ComponentUTGroup], Type[Artifacts]) -> None
super(ComponentUTApp, self).__init__(app_path, config_name, target, case_group, artifacts_cls)
class LoadableElfTestApp(TestApp):
def __init__(self, app_path, app_files, config_name='default', target='esp32', case_group=TestAppsGroup, artifacts_cls=Artifacts):
# type: (str, List[str], str, str, Type[TestAppsGroup], Type[Artifacts]) -> None
# add arg `app_files` for loadable elf test_app.
# Such examples only build elf files, so it doesn't generate flasher_args.json.
# So we can't get app files from config file. Test case should pass it to application.
@@ -524,12 +540,12 @@ class LoadableElfTestApp(TestApp):
class SSC(IDFApp):
def get_binary_path(self):
def get_binary_path(self): # type: () -> str
# TODO: to implement SSC get binary path
return self.app_path
class AT(IDFApp):
def get_binary_path(self):
def get_binary_path(self): # type: () -> str
# TODO: to implement AT get binary path
return self.app_path

View File

@@ -29,7 +29,7 @@ import pexpect
try:
import Queue as _queue
except ImportError:
import queue as _queue
import queue as _queue # type: ignore
from serial.tools import list_ports
from tiny_test_fw import DUT, Utility
@@ -207,12 +207,11 @@ class IDFDUT(DUT.SerialDUT):
if inst is not None:
inst._port.close()
@_uses_esptool
def _try_flash(self, esp, erase_nvs, baud_rate):
def _try_flash(self, erase_nvs):
"""
Called by start_app() to try flashing at a particular baud rate.
Called by start_app()
Structured this way so @_uses_esptool will reconnect each time
:return: None
"""
flash_files = []
encrypt_files = []
@@ -259,49 +258,51 @@ class IDFDUT(DUT.SerialDUT):
else:
encrypt_files.append((address, nvs_file))
# fake flasher args object, this is a hack until
# esptool Python API is improved
class FlashArgs(object):
def __init__(self, attributes):
for key, value in attributes.items():
self.__setattr__(key, value)
# write_flash expects the parameter encrypt_files to be None and not
# an empty list, so perform the check here
flash_args = FlashArgs({
'flash_size': self.app.flash_settings['flash_size'],
'flash_mode': self.app.flash_settings['flash_mode'],
'flash_freq': self.app.flash_settings['flash_freq'],
'addr_filename': flash_files,
'encrypt_files': encrypt_files or None,
'no_stub': False,
'compress': True,
'verify': False,
'encrypt': encrypt,
'ignore_flash_encryption_efuse_setting': False,
'erase_all': False,
})
esp.change_baud(baud_rate)
esptool.detect_flash_size(esp, flash_args)
esptool.write_flash(esp, flash_args)
self._write_flash(flash_files, encrypt_files, False, encrypt)
finally:
for (_, f) in flash_files:
f.close()
for (_, f) in encrypt_files:
f.close()
def start_app(self, erase_nvs=ERASE_NVS):
@_uses_esptool
def _write_flash(self, esp, flash_files=None, encrypt_files=None, ignore_flash_encryption_efuse_setting=True, encrypt=False):
"""
download and start app.
Try flashing at a particular baud rate.
:param: erase_nvs: whether erase NVS partition during flash
Structured this way so @_uses_esptool will reconnect each time
:return: None
"""
last_error = None
for baud_rate in [921600, 115200]:
try:
self._try_flash(erase_nvs, baud_rate)
# fake flasher args object, this is a hack until
# esptool Python API is improved
class FlashArgs(object):
def __init__(self, attributes):
for key, value in attributes.items():
self.__setattr__(key, value)
# write_flash expects the parameter encrypt_files to be None and not
# an empty list, so perform the check here
flash_args = FlashArgs({
'flash_size': self.app.flash_settings['flash_size'],
'flash_mode': self.app.flash_settings['flash_mode'],
'flash_freq': self.app.flash_settings['flash_freq'],
'addr_filename': flash_files or None,
'encrypt_files': encrypt_files or None,
'no_stub': False,
'compress': True,
'verify': False,
'encrypt': encrypt,
'ignore_flash_encryption_efuse_setting': ignore_flash_encryption_efuse_setting,
'erase_all': False,
'after': 'no_reset',
})
esp.change_baud(baud_rate)
esptool.detect_flash_size(esp, flash_args)
esptool.write_flash(esp, flash_args)
break
except RuntimeError as e:
last_error = e
@@ -334,6 +335,58 @@ class IDFDUT(DUT.SerialDUT):
sys.stdout = old_stdout
return output
def start_app(self, erase_nvs=ERASE_NVS):
"""
download and start app.
:param: erase_nvs: whether erase NVS partition during flash
:return: None
"""
self._try_flash(erase_nvs)
def start_app_no_enc(self):
"""
download and start app.
:param: erase_nvs: whether erase NVS partition during flash
:return: None
"""
flash_files = self.app.flash_files + self.app.encrypt_files
self.write_flash(flash_files)
def write_flash(self, flash_files=None, encrypt_files=None, ignore_flash_encryption_efuse_setting=True, encrypt=False):
"""
Flash files
:return: None
"""
flash_offs_files = []
encrypt_offs_files = []
try:
if flash_files:
flash_offs_files = [(offs, open(path, 'rb')) for (offs, path) in flash_files]
if encrypt_files:
encrypt_offs_files = [(offs, open(path, 'rb')) for (offs, path) in encrypt_files]
self._write_flash(flash_offs_files, encrypt_offs_files, ignore_flash_encryption_efuse_setting, encrypt)
finally:
for (_, f) in flash_offs_files:
f.close()
for (_, f) in encrypt_offs_files:
f.close()
def bootloader_flash(self):
"""
download bootloader.
:return: None
"""
bootloader_path = os.path.join(self.app.binary_path, 'bootloader', 'bootloader.bin')
offs = int(self.app.get_sdkconfig()['CONFIG_BOOTLOADER_OFFSET_IN_FLASH'], 0)
flash_files = [(offs, bootloader_path)]
self.write_flash(flash_files)
@_uses_esptool
def reset(self, esp):
"""
@@ -351,12 +404,9 @@ class IDFDUT(DUT.SerialDUT):
:param partition: partition name to erase
:return: None
"""
raise NotImplementedError() # TODO: implement this
# address = self.app.partition_table[partition]["offset"]
address = self.app.partition_table[partition]['offset']
size = self.app.partition_table[partition]['size']
# TODO can use esp.erase_region() instead of this, I think
with open('.erase_partition.tmp', 'wb') as f:
f.write(chr(0xFF) * size)
esp.erase_region(address, size)
@_uses_esptool
def erase_flash(self, esp):
@@ -515,9 +565,6 @@ class ESP32DUT(IDFDUT):
def _get_rom(cls):
return esptool.ESP32ROM
def erase_partition(self, esp, partition):
raise NotImplementedError()
class ESP32S2DUT(IDFDUT):
TARGET = 'esp32s2'
@@ -527,9 +574,6 @@ class ESP32S2DUT(IDFDUT):
def _get_rom(cls):
return esptool.ESP32S2ROM
def erase_partition(self, esp, partition):
raise NotImplementedError()
class ESP32C3DUT(IDFDUT):
TARGET = 'esp32c3'
@@ -539,9 +583,6 @@ class ESP32C3DUT(IDFDUT):
def _get_rom(cls):
return esptool.ESP32C3ROM
def erase_partition(self, esp, partition):
raise NotImplementedError()
class ESP8266DUT(IDFDUT):
TARGET = 'esp8266'
@@ -551,9 +592,6 @@ class ESP8266DUT(IDFDUT):
def _get_rom(cls):
return esptool.ESP8266ROM
def erase_partition(self, esp, partition):
raise NotImplementedError()
def get_target_by_rom_class(cls):
for c in [ESP32DUT, ESP32S2DUT, ESP32C3DUT, ESP8266DUT, IDFQEMUDUT]:
@@ -652,5 +690,5 @@ class IDFQEMUDUT(IDFDUT):
class ESP32QEMUDUT(IDFQEMUDUT):
TARGET = 'esp32'
TOOLCHAIN_PREFIX = 'xtensa-esp32-elf-'
TARGET = 'esp32' # type: ignore
TOOLCHAIN_PREFIX = 'xtensa-esp32-elf-' # type: ignore