2017-10-09 22:44:55 -04:00
|
|
|
# Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http:#www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
""" DUT for IDF applications """
|
2021-04-20 03:54:48 -04:00
|
|
|
import collections
|
2021-01-25 21:49:01 -05:00
|
|
|
import functools
|
2021-04-27 06:56:02 -04:00
|
|
|
import io
|
2017-10-09 22:44:55 -04:00
|
|
|
import os
|
2018-12-04 19:13:33 -05:00
|
|
|
import os.path
|
2017-10-09 22:44:55 -04:00
|
|
|
import re
|
2019-03-18 00:16:24 -04:00
|
|
|
import subprocess
|
2021-01-25 21:49:01 -05:00
|
|
|
import sys
|
|
|
|
import tempfile
|
2019-11-19 09:37:38 -05:00
|
|
|
import time
|
2021-01-25 21:49:01 -05:00
|
|
|
|
2019-12-17 07:18:29 -05:00
|
|
|
import pexpect
|
2021-04-20 03:54:48 -04:00
|
|
|
import serial
|
2018-07-26 03:07:32 -04:00
|
|
|
|
2019-03-16 08:07:52 -04:00
|
|
|
# python2 and python3 queue package name is different
|
|
|
|
try:
|
|
|
|
import Queue as _queue
|
|
|
|
except ImportError:
|
2021-06-07 06:17:13 -04:00
|
|
|
import queue as _queue # type: ignore
|
2019-03-16 08:07:52 -04:00
|
|
|
|
2018-06-25 02:43:40 -04:00
|
|
|
from serial.tools import list_ports
|
2019-11-26 22:21:33 -05:00
|
|
|
from tiny_test_fw import DUT, Utility
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2018-12-04 19:13:33 -05:00
|
|
|
try:
|
|
|
|
import esptool
|
|
|
|
except ImportError: # cheat and use IDF's copy of esptool if available
|
2021-01-25 21:49:01 -05:00
|
|
|
idf_path = os.getenv('IDF_PATH')
|
2018-12-04 19:13:33 -05:00
|
|
|
if not idf_path or not os.path.exists(idf_path):
|
|
|
|
raise
|
2021-01-25 21:49:01 -05:00
|
|
|
sys.path.insert(0, os.path.join(idf_path, 'components', 'esptool_py', 'esptool'))
|
2018-12-04 19:13:33 -05:00
|
|
|
import esptool
|
|
|
|
|
2021-04-20 03:54:48 -04:00
|
|
|
import espefuse
|
|
|
|
import espsecure
|
|
|
|
|
2017-10-09 22:44:55 -04:00
|
|
|
|
|
|
|
class IDFToolError(OSError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-03-16 08:07:52 -04:00
|
|
|
class IDFDUTException(RuntimeError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class IDFRecvThread(DUT.RecvThread):
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
PERFORMANCE_PATTERN = re.compile(r'\[Performance]\[(\w+)]: ([^\r\n]+)\r?\n')
|
2019-03-16 08:07:52 -04:00
|
|
|
EXCEPTION_PATTERNS = [
|
|
|
|
re.compile(r"(Guru Meditation Error: Core\s+\d panic'ed \([\w].*?\))"),
|
2021-01-25 21:49:01 -05:00
|
|
|
re.compile(r'(abort\(\) was called at PC 0x[a-fA-F\d]{8} on core \d)'),
|
|
|
|
re.compile(r'(rst 0x\d+ \(TG\dWDT_SYS_RESET|TGWDT_CPU_RESET\))')
|
2019-03-16 08:07:52 -04:00
|
|
|
]
|
2021-01-25 21:49:01 -05:00
|
|
|
BACKTRACE_PATTERN = re.compile(r'Backtrace:((\s(0x[0-9a-f]{8}):0x[0-9a-f]{8})+)')
|
|
|
|
BACKTRACE_ADDRESS_PATTERN = re.compile(r'(0x[0-9a-f]{8}):0x[0-9a-f]{8}')
|
2019-03-16 08:07:52 -04:00
|
|
|
|
2019-03-18 00:16:24 -04:00
|
|
|
def __init__(self, read, dut):
|
|
|
|
super(IDFRecvThread, self).__init__(read, dut)
|
2019-03-16 08:07:52 -04:00
|
|
|
self.exceptions = _queue.Queue()
|
2019-03-07 07:18:32 -05:00
|
|
|
self.performance_items = _queue.Queue()
|
2019-03-16 08:07:52 -04:00
|
|
|
|
|
|
|
def collect_performance(self, comp_data):
|
|
|
|
matches = self.PERFORMANCE_PATTERN.findall(comp_data)
|
|
|
|
for match in matches:
|
2021-05-20 00:18:36 -04:00
|
|
|
Utility.console_log('[Performance][{}]: {}'.format(match[0], match[1]), color='orange')
|
2019-03-07 07:18:32 -05:00
|
|
|
self.performance_items.put((match[0], match[1]))
|
2019-03-16 08:07:52 -04:00
|
|
|
|
|
|
|
def detect_exception(self, comp_data):
|
|
|
|
for pattern in self.EXCEPTION_PATTERNS:
|
|
|
|
start = 0
|
|
|
|
while True:
|
|
|
|
match = pattern.search(comp_data, pos=start)
|
|
|
|
if match:
|
|
|
|
start = match.end()
|
|
|
|
self.exceptions.put(match.group(0))
|
2021-01-25 21:49:01 -05:00
|
|
|
Utility.console_log('[Exception]: {}'.format(match.group(0)), color='red')
|
2019-03-16 08:07:52 -04:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
def detect_backtrace(self, comp_data):
|
|
|
|
start = 0
|
|
|
|
while True:
|
|
|
|
match = self.BACKTRACE_PATTERN.search(comp_data, pos=start)
|
|
|
|
if match:
|
|
|
|
start = match.end()
|
2021-01-25 21:49:01 -05:00
|
|
|
Utility.console_log('[Backtrace]:{}'.format(match.group(1)), color='red')
|
2019-03-18 00:16:24 -04:00
|
|
|
# translate backtrace
|
|
|
|
addresses = self.BACKTRACE_ADDRESS_PATTERN.findall(match.group(1))
|
2021-01-25 21:49:01 -05:00
|
|
|
translated_backtrace = ''
|
2019-03-18 00:16:24 -04:00
|
|
|
for addr in addresses:
|
|
|
|
ret = self.dut.lookup_pc_address(addr)
|
|
|
|
if ret:
|
2021-01-25 21:49:01 -05:00
|
|
|
translated_backtrace += ret + '\n'
|
2019-03-18 00:16:24 -04:00
|
|
|
if translated_backtrace:
|
2021-01-25 21:49:01 -05:00
|
|
|
Utility.console_log('Translated backtrace\n:' + translated_backtrace, color='yellow')
|
2019-03-18 00:16:24 -04:00
|
|
|
else:
|
2021-01-25 21:49:01 -05:00
|
|
|
Utility.console_log('Failed to translate backtrace', color='yellow')
|
2019-03-16 08:07:52 -04:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
CHECK_FUNCTIONS = [collect_performance, detect_exception, detect_backtrace]
|
|
|
|
|
|
|
|
|
2018-12-04 19:13:33 -05:00
|
|
|
def _uses_esptool(func):
|
|
|
|
""" Suspend listener thread, connect with esptool,
|
|
|
|
call target function with esptool instance,
|
|
|
|
then resume listening for output
|
|
|
|
"""
|
2017-10-09 22:44:55 -04:00
|
|
|
@functools.wraps(func)
|
|
|
|
def handler(self, *args, **kwargs):
|
2018-12-04 19:13:33 -05:00
|
|
|
self.stop_receive()
|
|
|
|
|
|
|
|
settings = self.port_inst.get_settings()
|
|
|
|
|
2019-01-02 01:51:36 -05:00
|
|
|
try:
|
2021-04-20 03:54:48 -04:00
|
|
|
if not self.rom_inst:
|
|
|
|
if not self.secure_boot_en:
|
|
|
|
self.rom_inst = esptool.ESPLoader.detect_chip(self.port_inst)
|
|
|
|
else:
|
|
|
|
self.rom_inst = self.get_rom()(self.port_inst)
|
|
|
|
self.rom_inst.connect('hard_reset')
|
|
|
|
|
|
|
|
if (self.secure_boot_en):
|
|
|
|
esp = self.rom_inst
|
|
|
|
esp.flash_spi_attach(0)
|
|
|
|
else:
|
|
|
|
esp = self.rom_inst.run_stub()
|
2018-12-04 19:13:33 -05:00
|
|
|
|
2019-01-02 01:51:36 -05:00
|
|
|
ret = func(self, esp, *args, **kwargs)
|
|
|
|
# do hard reset after use esptool
|
|
|
|
esp.hard_reset()
|
|
|
|
finally:
|
|
|
|
# always need to restore port settings
|
|
|
|
self.port_inst.apply_settings(settings)
|
2018-12-04 19:13:33 -05:00
|
|
|
|
|
|
|
self.start_receive()
|
2019-01-02 01:51:36 -05:00
|
|
|
|
2017-10-09 22:44:55 -04:00
|
|
|
return ret
|
|
|
|
return handler
|
|
|
|
|
|
|
|
|
|
|
|
class IDFDUT(DUT.SerialDUT):
|
2018-12-04 19:13:33 -05:00
|
|
|
""" IDF DUT, extends serial with esptool methods
|
|
|
|
|
|
|
|
(Becomes aware of IDFApp instance which holds app-specific data)
|
|
|
|
"""
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2018-07-10 12:00:40 -04:00
|
|
|
# /dev/ttyAMA0 port is listed in Raspberry Pi
|
|
|
|
# /dev/tty.Bluetooth-Incoming-Port port is listed in Mac
|
2021-01-25 21:49:01 -05:00
|
|
|
INVALID_PORT_PATTERN = re.compile(r'AMA|Bluetooth')
|
2018-07-26 03:07:32 -04:00
|
|
|
# if need to erase NVS partition in start app
|
|
|
|
ERASE_NVS = True
|
2019-03-16 08:07:52 -04:00
|
|
|
RECV_THREAD_CLS = IDFRecvThread
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2019-03-16 08:07:52 -04:00
|
|
|
def __init__(self, name, port, log_file, app, allow_dut_exception=False, **kwargs):
|
2017-10-09 22:44:55 -04:00
|
|
|
super(IDFDUT, self).__init__(name, port, log_file, app, **kwargs)
|
2019-03-16 08:07:52 -04:00
|
|
|
self.allow_dut_exception = allow_dut_exception
|
|
|
|
self.exceptions = _queue.Queue()
|
2019-03-07 07:18:32 -05:00
|
|
|
self.performance_items = _queue.Queue()
|
2021-04-20 03:54:48 -04:00
|
|
|
self.rom_inst = None
|
|
|
|
self.secure_boot_en = self.app.get_sdkconfig_config_value('CONFIG_SECURE_BOOT') and \
|
|
|
|
not self.app.get_sdkconfig_config_value('CONFIG_EFUSE_VIRTUAL')
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2019-08-21 00:04:26 -04:00
|
|
|
@classmethod
|
2021-04-20 03:54:48 -04:00
|
|
|
def get_rom(cls):
|
2021-01-25 21:49:01 -05:00
|
|
|
raise NotImplementedError('This is an abstraction class, method not defined.')
|
2019-08-21 00:04:26 -04:00
|
|
|
|
2017-10-09 22:44:55 -04:00
|
|
|
@classmethod
|
2018-12-04 19:13:33 -05:00
|
|
|
def get_mac(cls, app, port):
|
2017-10-09 22:44:55 -04:00
|
|
|
"""
|
2018-12-04 19:13:33 -05:00
|
|
|
get MAC address via esptool
|
2017-10-09 22:44:55 -04:00
|
|
|
|
|
|
|
:param app: application instance (to get tool)
|
2018-12-04 19:13:33 -05:00
|
|
|
:param port: serial port as string
|
|
|
|
:return: MAC address or None
|
2017-10-09 22:44:55 -04:00
|
|
|
"""
|
2019-10-15 08:15:06 -04:00
|
|
|
esp = None
|
2017-10-09 22:44:55 -04:00
|
|
|
try:
|
2021-04-20 03:54:48 -04:00
|
|
|
esp = cls.get_rom()(port)
|
2018-12-04 19:13:33 -05:00
|
|
|
esp.connect()
|
|
|
|
return esp.read_mac()
|
2018-12-04 07:46:48 -05:00
|
|
|
except RuntimeError:
|
2018-12-04 19:13:33 -05:00
|
|
|
return None
|
|
|
|
finally:
|
2019-10-15 08:15:06 -04:00
|
|
|
if esp:
|
|
|
|
# do hard reset after use esptool
|
|
|
|
esp.hard_reset()
|
|
|
|
esp._port.close()
|
2017-10-09 22:44:55 -04:00
|
|
|
|
|
|
|
@classmethod
|
2019-10-15 08:15:06 -04:00
|
|
|
def confirm_dut(cls, port, **kwargs):
|
2019-10-16 23:38:49 -04:00
|
|
|
inst = None
|
2019-11-06 08:35:19 -05:00
|
|
|
try:
|
2021-04-20 03:54:48 -04:00
|
|
|
expected_rom_class = cls.get_rom()
|
2019-11-06 08:35:19 -05:00
|
|
|
except NotImplementedError:
|
|
|
|
expected_rom_class = None
|
|
|
|
|
2019-08-21 00:04:26 -04:00
|
|
|
try:
|
|
|
|
# TODO: check whether 8266 works with this logic
|
|
|
|
# Otherwise overwrite it in ESP8266DUT
|
|
|
|
inst = esptool.ESPLoader.detect_chip(port)
|
2019-11-06 08:35:19 -05:00
|
|
|
if expected_rom_class and type(inst) != expected_rom_class:
|
2021-01-25 21:49:01 -05:00
|
|
|
raise RuntimeError('Target not expected')
|
2019-10-15 08:15:06 -04:00
|
|
|
return inst.read_mac() is not None, get_target_by_rom_class(type(inst))
|
2022-08-09 08:08:48 -04:00
|
|
|
except (esptool.FatalError, RuntimeError):
|
2019-10-15 08:15:06 -04:00
|
|
|
return False, None
|
2019-08-21 00:04:26 -04:00
|
|
|
finally:
|
2019-10-16 23:38:49 -04:00
|
|
|
if inst is not None:
|
2019-08-21 00:04:26 -04:00
|
|
|
inst._port.close()
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2021-06-16 19:21:36 -04:00
|
|
|
def _try_flash(self, erase_nvs):
|
2017-10-09 22:44:55 -04:00
|
|
|
"""
|
2021-06-16 19:21:36 -04:00
|
|
|
Called by start_app()
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2021-06-16 19:21:36 -04:00
|
|
|
:return: None
|
2017-10-09 22:44:55 -04:00
|
|
|
"""
|
2019-12-11 01:38:19 -05:00
|
|
|
flash_files = []
|
2020-11-26 01:42:55 -05:00
|
|
|
encrypt_files = []
|
2018-07-26 03:07:32 -04:00
|
|
|
try:
|
2020-11-26 01:42:55 -05:00
|
|
|
# Open the files here to prevents us from having to seek back to 0
|
|
|
|
# each time. Before opening them, we have to organize the lists the
|
|
|
|
# way esptool.write_flash needs:
|
|
|
|
# If encrypt is provided, flash_files contains all the files to
|
|
|
|
# flash.
|
|
|
|
# Else, flash_files contains the files to be flashed as plain text
|
|
|
|
# and encrypt_files contains the ones to flash encrypted.
|
|
|
|
flash_files = self.app.flash_files
|
|
|
|
encrypt_files = self.app.encrypt_files
|
2021-01-25 21:49:01 -05:00
|
|
|
encrypt = self.app.flash_settings.get('encrypt', False)
|
2020-11-26 01:42:55 -05:00
|
|
|
if encrypt:
|
|
|
|
flash_files = encrypt_files
|
|
|
|
encrypt_files = []
|
|
|
|
else:
|
|
|
|
flash_files = [entry
|
|
|
|
for entry in flash_files
|
|
|
|
if entry not in encrypt_files]
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
flash_files = [(offs, open(path, 'rb')) for (offs, path) in flash_files]
|
|
|
|
encrypt_files = [(offs, open(path, 'rb')) for (offs, path) in encrypt_files]
|
2018-12-05 23:44:27 -05:00
|
|
|
|
|
|
|
if erase_nvs:
|
2021-01-25 21:49:01 -05:00
|
|
|
address = self.app.partition_table['nvs']['offset']
|
|
|
|
size = self.app.partition_table['nvs']['size']
|
2018-12-05 23:44:27 -05:00
|
|
|
nvs_file = tempfile.TemporaryFile()
|
|
|
|
nvs_file.write(b'\xff' * size)
|
|
|
|
nvs_file.seek(0)
|
2019-12-19 21:00:12 -05:00
|
|
|
if not isinstance(address, int):
|
|
|
|
address = int(address, 0)
|
2020-11-26 01:42:55 -05:00
|
|
|
# We have to check whether this file needs to be added to
|
|
|
|
# flash_files list or encrypt_files.
|
|
|
|
# Get the CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT macro
|
|
|
|
# value. If it is set to True, then NVS is always encrypted.
|
|
|
|
sdkconfig_dict = self.app.get_sdkconfig()
|
2021-01-25 21:49:01 -05:00
|
|
|
macro_encryption = 'CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT' in sdkconfig_dict
|
2020-11-26 01:42:55 -05:00
|
|
|
# If the macro is not enabled (plain text flash) or all files
|
|
|
|
# must be encrypted, add NVS to flash_files.
|
|
|
|
if not macro_encryption or encrypt:
|
|
|
|
flash_files.append((address, nvs_file))
|
|
|
|
else:
|
|
|
|
encrypt_files.append((address, nvs_file))
|
2018-12-05 23:44:27 -05:00
|
|
|
|
2021-04-20 03:54:48 -04:00
|
|
|
self.write_flash_data(flash_files, encrypt_files, False, encrypt)
|
2018-07-26 03:07:32 -04:00
|
|
|
finally:
|
2019-01-02 01:51:36 -05:00
|
|
|
for (_, f) in flash_files:
|
2018-12-04 19:13:33 -05:00
|
|
|
f.close()
|
2020-11-26 01:42:55 -05:00
|
|
|
for (_, f) in encrypt_files:
|
|
|
|
f.close()
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2021-06-16 19:21:36 -04:00
|
|
|
@_uses_esptool
|
2021-04-20 03:54:48 -04:00
|
|
|
def write_flash_data(self, esp, flash_files=None, encrypt_files=None, ignore_flash_encryption_efuse_setting=True, encrypt=False):
|
2018-12-05 23:44:27 -05:00
|
|
|
"""
|
2021-06-16 19:21:36 -04:00
|
|
|
Try flashing at a particular baud rate.
|
2018-12-05 23:44:27 -05:00
|
|
|
|
2021-06-16 19:21:36 -04:00
|
|
|
Structured this way so @_uses_esptool will reconnect each time
|
2018-12-05 23:44:27 -05:00
|
|
|
:return: None
|
|
|
|
"""
|
2020-12-28 23:41:22 -05:00
|
|
|
last_error = None
|
2018-12-04 07:46:48 -05:00
|
|
|
for baud_rate in [921600, 115200]:
|
2018-12-05 23:44:27 -05:00
|
|
|
try:
|
2021-06-16 19:21:36 -04:00
|
|
|
# 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,
|
2021-04-20 03:54:48 -04:00
|
|
|
'no_stub': self.secure_boot_en,
|
|
|
|
'compress': not self.secure_boot_en,
|
2021-06-16 19:21:36 -04:00
|
|
|
'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)
|
2018-12-05 23:44:27 -05:00
|
|
|
break
|
2020-12-28 23:41:22 -05:00
|
|
|
except RuntimeError as e:
|
|
|
|
last_error = e
|
2018-12-05 23:44:27 -05:00
|
|
|
else:
|
2020-12-28 23:41:22 -05:00
|
|
|
raise last_error
|
2018-12-05 23:44:27 -05:00
|
|
|
|
2021-04-27 06:56:02 -04:00
|
|
|
def image_info(self, path_to_file):
|
|
|
|
"""
|
|
|
|
get hash256 of app
|
|
|
|
|
|
|
|
:param: path: path to file
|
|
|
|
:return: sha256 appended to app
|
|
|
|
"""
|
|
|
|
|
|
|
|
old_stdout = sys.stdout
|
|
|
|
new_stdout = io.StringIO()
|
|
|
|
sys.stdout = new_stdout
|
|
|
|
|
|
|
|
class Args(object):
|
|
|
|
def __init__(self, attributes):
|
|
|
|
for key, value in attributes.items():
|
|
|
|
self.__setattr__(key, value)
|
|
|
|
|
|
|
|
args = Args({
|
|
|
|
'chip': self.TARGET,
|
|
|
|
'filename': path_to_file,
|
|
|
|
})
|
|
|
|
esptool.image_info(args)
|
|
|
|
output = new_stdout.getvalue()
|
|
|
|
sys.stdout = old_stdout
|
|
|
|
return output
|
|
|
|
|
2021-06-16 19:21:36 -04:00
|
|
|
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]
|
|
|
|
|
2021-04-20 03:54:48 -04:00
|
|
|
self.write_flash_data(flash_offs_files, encrypt_offs_files, ignore_flash_encryption_efuse_setting, encrypt)
|
2021-06-16 19:21:36 -04:00
|
|
|
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)
|
|
|
|
|
2018-12-04 19:13:33 -05:00
|
|
|
@_uses_esptool
|
|
|
|
def reset(self, esp):
|
2017-10-09 22:44:55 -04:00
|
|
|
"""
|
2018-12-04 19:13:33 -05:00
|
|
|
hard reset DUT
|
2017-10-09 22:44:55 -04:00
|
|
|
|
|
|
|
:return: None
|
|
|
|
"""
|
2019-01-02 01:51:36 -05:00
|
|
|
# decorator `_use_esptool` will do reset
|
|
|
|
# so we don't need to do anything in this method
|
|
|
|
pass
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2018-12-04 19:13:33 -05:00
|
|
|
@_uses_esptool
|
|
|
|
def erase_partition(self, esp, partition):
|
2018-07-26 03:07:32 -04:00
|
|
|
"""
|
|
|
|
:param partition: partition name to erase
|
|
|
|
:return: None
|
|
|
|
"""
|
2021-06-16 19:21:36 -04:00
|
|
|
address = self.app.partition_table[partition]['offset']
|
2021-01-25 21:49:01 -05:00
|
|
|
size = self.app.partition_table[partition]['size']
|
2021-06-16 19:21:36 -04:00
|
|
|
esp.erase_region(address, size)
|
2018-07-26 03:07:32 -04:00
|
|
|
|
2018-12-04 19:13:33 -05:00
|
|
|
@_uses_esptool
|
2021-01-18 06:36:00 -05:00
|
|
|
def erase_flash(self, esp):
|
2017-10-09 22:44:55 -04:00
|
|
|
"""
|
2021-01-18 06:36:00 -05:00
|
|
|
erase the flash completely
|
|
|
|
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
esp.erase_flash()
|
|
|
|
|
|
|
|
@_uses_esptool
|
|
|
|
def dump_flash(self, esp, output_file, **kwargs):
|
|
|
|
"""
|
|
|
|
dump flash
|
2017-10-09 22:44:55 -04:00
|
|
|
|
|
|
|
:param output_file: output file name, if relative path, will use sdk path as base path.
|
|
|
|
:keyword partition: partition name, dump the partition.
|
|
|
|
``partition`` is preferred than using ``address`` and ``size``.
|
|
|
|
:keyword address: dump from address (need to be used with size)
|
|
|
|
:keyword size: dump size (need to be used with address)
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
if os.path.isabs(output_file) is False:
|
|
|
|
output_file = os.path.relpath(output_file, self.app.get_log_folder())
|
2021-01-25 21:49:01 -05:00
|
|
|
if 'partition' in kwargs:
|
|
|
|
partition = self.app.partition_table[kwargs['partition']]
|
|
|
|
_address = partition['offset']
|
|
|
|
_size = partition['size']
|
|
|
|
elif 'address' in kwargs and 'size' in kwargs:
|
|
|
|
_address = kwargs['address']
|
|
|
|
_size = kwargs['size']
|
2017-10-09 22:44:55 -04:00
|
|
|
else:
|
|
|
|
raise IDFToolError("You must specify 'partition' or ('address' and 'size') to dump flash")
|
2018-12-04 19:13:33 -05:00
|
|
|
|
|
|
|
content = esp.read_flash(_address, _size)
|
2021-01-25 21:49:01 -05:00
|
|
|
with open(output_file, 'wb') as f:
|
2018-12-04 19:13:33 -05:00
|
|
|
f.write(content)
|
2018-06-25 02:43:40 -04:00
|
|
|
|
2021-04-07 00:00:20 -04:00
|
|
|
@staticmethod
|
|
|
|
def _sort_usb_ports(ports):
|
|
|
|
"""
|
|
|
|
Move the usb ports to the very beginning
|
|
|
|
:param ports: list of ports
|
|
|
|
:return: list of ports with usb ports at beginning
|
|
|
|
"""
|
|
|
|
usb_ports = []
|
|
|
|
rest_ports = []
|
|
|
|
for port in ports:
|
|
|
|
if 'usb' in port.lower():
|
|
|
|
usb_ports.append(port)
|
|
|
|
else:
|
|
|
|
rest_ports.append(port)
|
|
|
|
return usb_ports + rest_ports
|
|
|
|
|
2018-06-25 02:43:40 -04:00
|
|
|
@classmethod
|
|
|
|
def list_available_ports(cls):
|
2021-04-07 00:00:20 -04:00
|
|
|
# It will return other kinds of ports as well, such as ttyS* ports.
|
|
|
|
# Give the usb ports higher priority
|
|
|
|
ports = cls._sort_usb_ports([x.device for x in list_ports.comports()])
|
2018-07-02 09:45:27 -04:00
|
|
|
espport = os.getenv('ESPPORT')
|
|
|
|
if not espport:
|
2018-07-10 12:00:40 -04:00
|
|
|
# It's a little hard filter out invalid port with `serial.tools.list_ports.grep()`:
|
|
|
|
# The check condition in `grep` is: `if r.search(port) or r.search(desc) or r.search(hwid)`.
|
|
|
|
# This means we need to make all 3 conditions fail, to filter out the port.
|
|
|
|
# So some part of the filters will not be straight forward to users.
|
|
|
|
# And negative regular expression (`^((?!aa|bb|cc).)*$`) is not easy to understand.
|
|
|
|
# Filter out invalid port by our own will be much simpler.
|
|
|
|
return [x for x in ports if not cls.INVALID_PORT_PATTERN.search(x)]
|
|
|
|
|
2018-09-27 05:46:13 -04:00
|
|
|
# On MacOs with python3.6: type of espport is already utf8
|
2018-12-04 07:46:48 -05:00
|
|
|
if isinstance(espport, type(u'')):
|
2018-09-27 05:46:13 -04:00
|
|
|
port_hint = espport
|
|
|
|
else:
|
|
|
|
port_hint = espport.decode('utf8')
|
2018-06-25 02:43:40 -04:00
|
|
|
|
|
|
|
# If $ESPPORT is a valid port, make it appear first in the list
|
|
|
|
if port_hint in ports:
|
|
|
|
ports.remove(port_hint)
|
|
|
|
return [port_hint] + ports
|
|
|
|
|
|
|
|
# On macOS, user may set ESPPORT to /dev/tty.xxx while
|
|
|
|
# pySerial lists only the corresponding /dev/cu.xxx port
|
|
|
|
if sys.platform == 'darwin' and 'tty.' in port_hint:
|
|
|
|
port_hint = port_hint.replace('tty.', 'cu.')
|
|
|
|
if port_hint in ports:
|
|
|
|
ports.remove(port_hint)
|
|
|
|
return [port_hint] + ports
|
|
|
|
|
|
|
|
return ports
|
2019-03-16 08:07:52 -04:00
|
|
|
|
2019-03-18 00:16:24 -04:00
|
|
|
def lookup_pc_address(self, pc_addr):
|
2021-01-25 21:49:01 -05:00
|
|
|
cmd = ['%saddr2line' % self.TOOLCHAIN_PREFIX,
|
|
|
|
'-pfiaC', '-e', self.app.elf_file, pc_addr]
|
|
|
|
ret = ''
|
2019-03-18 00:16:24 -04:00
|
|
|
try:
|
|
|
|
translation = subprocess.check_output(cmd)
|
|
|
|
ret = translation.decode()
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
return ret
|
|
|
|
|
2019-03-07 07:17:43 -05:00
|
|
|
@staticmethod
|
|
|
|
def _queue_read_all(source_queue):
|
|
|
|
output = []
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
output.append(source_queue.get(timeout=0))
|
|
|
|
except _queue.Empty:
|
|
|
|
break
|
|
|
|
return output
|
|
|
|
|
|
|
|
def _queue_copy(self, source_queue, dest_queue):
|
|
|
|
data = self._queue_read_all(source_queue)
|
|
|
|
for d in data:
|
|
|
|
dest_queue.put(d)
|
|
|
|
|
|
|
|
def _get_from_queue(self, queue_name):
|
|
|
|
self_queue = getattr(self, queue_name)
|
|
|
|
if self.receive_thread:
|
|
|
|
recv_thread_queue = getattr(self.receive_thread, queue_name)
|
|
|
|
self._queue_copy(recv_thread_queue, self_queue)
|
|
|
|
return self._queue_read_all(self_queue)
|
|
|
|
|
2019-03-16 08:07:52 -04:00
|
|
|
def stop_receive(self):
|
|
|
|
if self.receive_thread:
|
2021-01-25 21:49:01 -05:00
|
|
|
for name in ['performance_items', 'exceptions']:
|
2019-03-07 07:17:43 -05:00
|
|
|
source_queue = getattr(self.receive_thread, name)
|
|
|
|
dest_queue = getattr(self, name)
|
|
|
|
self._queue_copy(source_queue, dest_queue)
|
2019-03-16 08:07:52 -04:00
|
|
|
super(IDFDUT, self).stop_receive()
|
|
|
|
|
|
|
|
def get_exceptions(self):
|
|
|
|
""" Get exceptions detected by DUT receive thread. """
|
2021-01-25 21:49:01 -05:00
|
|
|
return self._get_from_queue('exceptions')
|
2019-03-07 07:17:43 -05:00
|
|
|
|
|
|
|
def get_performance_items(self):
|
|
|
|
"""
|
|
|
|
DUT receive thread will automatic collect performance results with pattern ``[Performance][name]: value\n``.
|
|
|
|
This method is used to get all performance results.
|
|
|
|
|
|
|
|
:return: a list of performance items.
|
|
|
|
"""
|
2021-01-25 21:49:01 -05:00
|
|
|
return self._get_from_queue('performance_items')
|
2019-03-16 08:07:52 -04:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
super(IDFDUT, self).close()
|
|
|
|
if not self.allow_dut_exception and self.get_exceptions():
|
2021-05-20 02:32:18 -04:00
|
|
|
raise IDFDUTException('DUT exception detected on {}'.format(self))
|
2019-08-21 00:04:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ESP32DUT(IDFDUT):
|
2021-01-25 21:49:01 -05:00
|
|
|
TARGET = 'esp32'
|
|
|
|
TOOLCHAIN_PREFIX = 'xtensa-esp32-elf-'
|
2019-12-17 07:18:29 -05:00
|
|
|
|
2019-08-21 00:04:26 -04:00
|
|
|
@classmethod
|
2021-04-20 03:54:48 -04:00
|
|
|
def get_rom(cls):
|
2019-08-21 00:04:26 -04:00
|
|
|
return esptool.ESP32ROM
|
|
|
|
|
|
|
|
|
|
|
|
class ESP32S2DUT(IDFDUT):
|
2021-01-25 21:49:01 -05:00
|
|
|
TARGET = 'esp32s2'
|
|
|
|
TOOLCHAIN_PREFIX = 'xtensa-esp32s2-elf-'
|
2019-12-17 07:18:29 -05:00
|
|
|
|
2019-08-21 00:04:26 -04:00
|
|
|
@classmethod
|
2021-04-20 03:54:48 -04:00
|
|
|
def get_rom(cls):
|
2019-08-21 00:04:26 -04:00
|
|
|
return esptool.ESP32S2ROM
|
|
|
|
|
2020-12-28 21:00:45 -05:00
|
|
|
|
2021-06-07 06:17:13 -04:00
|
|
|
class ESP32S3DUT(IDFDUT):
|
|
|
|
TARGET = 'esp32s3'
|
|
|
|
TOOLCHAIN_PREFIX = 'xtensa-esp32s3-elf-'
|
|
|
|
|
|
|
|
@classmethod
|
2021-04-20 03:54:48 -04:00
|
|
|
def get_rom(cls):
|
2021-06-07 06:17:13 -04:00
|
|
|
return esptool.ESP32S3ROM
|
|
|
|
|
|
|
|
def erase_partition(self, esp, partition):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
2020-12-28 21:00:45 -05:00
|
|
|
class ESP32C3DUT(IDFDUT):
|
2021-01-25 21:49:01 -05:00
|
|
|
TARGET = 'esp32c3'
|
|
|
|
TOOLCHAIN_PREFIX = 'riscv32-esp-elf-'
|
2020-12-28 21:00:45 -05:00
|
|
|
|
|
|
|
@classmethod
|
2021-04-20 03:54:48 -04:00
|
|
|
def get_rom(cls):
|
2020-12-28 21:00:45 -05:00
|
|
|
return esptool.ESP32C3ROM
|
|
|
|
|
2019-08-21 00:04:26 -04:00
|
|
|
|
|
|
|
class ESP8266DUT(IDFDUT):
|
2021-01-25 21:49:01 -05:00
|
|
|
TARGET = 'esp8266'
|
|
|
|
TOOLCHAIN_PREFIX = 'xtensa-lx106-elf-'
|
2019-12-17 07:18:29 -05:00
|
|
|
|
2019-08-21 00:04:26 -04:00
|
|
|
@classmethod
|
2021-04-20 03:54:48 -04:00
|
|
|
def get_rom(cls):
|
2019-08-21 00:04:26 -04:00
|
|
|
return esptool.ESP8266ROM
|
2019-10-15 08:15:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_target_by_rom_class(cls):
|
2021-06-07 06:17:13 -04:00
|
|
|
for c in [ESP32DUT, ESP32S2DUT, ESP32S3DUT, ESP32C3DUT, ESP8266DUT, IDFQEMUDUT]:
|
2021-04-20 03:54:48 -04:00
|
|
|
if c.get_rom() == cls:
|
2019-10-15 08:15:06 -04:00
|
|
|
return c.TARGET
|
|
|
|
return None
|
2019-11-19 09:37:38 -05:00
|
|
|
|
|
|
|
|
|
|
|
class IDFQEMUDUT(IDFDUT):
|
2019-12-17 07:18:29 -05:00
|
|
|
TARGET = None
|
|
|
|
TOOLCHAIN_PREFIX = None
|
2019-11-19 09:37:38 -05:00
|
|
|
ERASE_NVS = True
|
|
|
|
DEFAULT_EXPECT_TIMEOUT = 30 # longer timeout, since app startup takes more time in QEMU (due to slow SHA emulation)
|
|
|
|
QEMU_SERIAL_PORT = 3334
|
|
|
|
|
|
|
|
def __init__(self, name, port, log_file, app, allow_dut_exception=False, **kwargs):
|
2021-01-25 21:49:01 -05:00
|
|
|
self.flash_image = tempfile.NamedTemporaryFile('rb+', suffix='.bin', prefix='qemu_flash_img')
|
2019-11-19 09:37:38 -05:00
|
|
|
self.app = app
|
|
|
|
self.flash_size = 4 * 1024 * 1024
|
|
|
|
self._write_flash_img()
|
|
|
|
|
|
|
|
args = [
|
2021-01-25 21:49:01 -05:00
|
|
|
'qemu-system-xtensa',
|
|
|
|
'-nographic',
|
|
|
|
'-machine', self.TARGET,
|
|
|
|
'-drive', 'file={},if=mtd,format=raw'.format(self.flash_image.name),
|
|
|
|
'-nic', 'user,model=open_eth',
|
|
|
|
'-serial', 'tcp::{},server,nowait'.format(self.QEMU_SERIAL_PORT),
|
|
|
|
'-S',
|
|
|
|
'-global driver=timer.esp32.timg,property=wdt_disable,value=true']
|
2019-12-17 07:18:29 -05:00
|
|
|
# TODO(IDF-1242): generate a temporary efuse binary, pass it to QEMU
|
2019-11-19 09:37:38 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
if 'QEMU_BIOS_PATH' in os.environ:
|
|
|
|
args += ['-L', os.environ['QEMU_BIOS_PATH']]
|
2019-11-19 09:37:38 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
self.qemu = pexpect.spawn(' '.join(args), timeout=self.DEFAULT_EXPECT_TIMEOUT)
|
|
|
|
self.qemu.expect_exact(b'(qemu)')
|
2019-11-19 09:37:38 -05:00
|
|
|
super(IDFQEMUDUT, self).__init__(name, port, log_file, app, allow_dut_exception=allow_dut_exception, **kwargs)
|
|
|
|
|
|
|
|
def _write_flash_img(self):
|
|
|
|
self.flash_image.seek(0)
|
|
|
|
self.flash_image.write(b'\x00' * self.flash_size)
|
|
|
|
for offs, path in self.app.flash_files:
|
2021-01-25 21:49:01 -05:00
|
|
|
with open(path, 'rb') as flash_file:
|
2019-11-19 09:37:38 -05:00
|
|
|
contents = flash_file.read()
|
|
|
|
self.flash_image.seek(offs)
|
|
|
|
self.flash_image.write(contents)
|
|
|
|
self.flash_image.flush()
|
|
|
|
|
2019-12-17 07:18:29 -05:00
|
|
|
@classmethod
|
2021-04-20 03:54:48 -04:00
|
|
|
def get_rom(cls):
|
2019-12-17 07:18:29 -05:00
|
|
|
return esptool.ESP32ROM
|
|
|
|
|
2019-11-19 09:37:38 -05:00
|
|
|
@classmethod
|
|
|
|
def get_mac(cls, app, port):
|
2019-12-17 07:18:29 -05:00
|
|
|
# TODO(IDF-1242): get this from QEMU/efuse binary
|
2021-01-25 21:49:01 -05:00
|
|
|
return '11:22:33:44:55:66'
|
2019-11-19 09:37:38 -05:00
|
|
|
|
|
|
|
@classmethod
|
2019-12-17 07:18:29 -05:00
|
|
|
def confirm_dut(cls, port, **kwargs):
|
2019-11-19 09:37:38 -05:00
|
|
|
return True, cls.TARGET
|
|
|
|
|
|
|
|
def start_app(self, erase_nvs=ERASE_NVS):
|
|
|
|
# TODO: implement erase_nvs
|
|
|
|
# since the flash image is generated every time in the constructor, maybe this isn't needed...
|
2021-01-25 21:49:01 -05:00
|
|
|
self.qemu.sendline(b'cont\n')
|
|
|
|
self.qemu.expect_exact(b'(qemu)')
|
2019-11-19 09:37:38 -05:00
|
|
|
|
|
|
|
def reset(self):
|
2021-01-25 21:49:01 -05:00
|
|
|
self.qemu.sendline(b'system_reset\n')
|
|
|
|
self.qemu.expect_exact(b'(qemu)')
|
2019-11-19 09:37:38 -05:00
|
|
|
|
|
|
|
def erase_partition(self, partition):
|
2021-01-25 21:49:01 -05:00
|
|
|
raise NotImplementedError('method erase_partition not implemented')
|
2021-01-18 06:36:00 -05:00
|
|
|
|
|
|
|
def erase_flash(self):
|
2021-01-25 21:49:01 -05:00
|
|
|
raise NotImplementedError('method erase_flash not implemented')
|
2019-11-19 09:37:38 -05:00
|
|
|
|
2021-01-18 06:36:00 -05:00
|
|
|
def dump_flash(self, output_file, **kwargs):
|
2021-01-25 21:49:01 -05:00
|
|
|
raise NotImplementedError('method dump_flash not implemented')
|
2019-11-19 09:37:38 -05:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def list_available_ports(cls):
|
2021-01-25 21:49:01 -05:00
|
|
|
return ['socket://localhost:{}'.format(cls.QEMU_SERIAL_PORT)]
|
2019-11-19 09:37:38 -05:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
super(IDFQEMUDUT, self).close()
|
2021-01-25 21:49:01 -05:00
|
|
|
self.qemu.sendline(b'q\n')
|
|
|
|
self.qemu.expect_exact(b'(qemu)')
|
2019-12-17 07:18:29 -05:00
|
|
|
for _ in range(self.DEFAULT_EXPECT_TIMEOUT):
|
|
|
|
if not self.qemu.isalive():
|
|
|
|
break
|
|
|
|
time.sleep(1)
|
|
|
|
else:
|
|
|
|
self.qemu.terminate(force=True)
|
|
|
|
|
2019-11-19 09:37:38 -05:00
|
|
|
|
|
|
|
class ESP32QEMUDUT(IDFQEMUDUT):
|
2021-06-07 06:17:13 -04:00
|
|
|
TARGET = 'esp32' # type: ignore
|
|
|
|
TOOLCHAIN_PREFIX = 'xtensa-esp32-elf-' # type: ignore
|
2021-04-20 03:54:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
class IDFFPGADUT(IDFDUT):
|
|
|
|
TARGET = None # type: str
|
|
|
|
TOOLCHAIN_PREFIX = None # type: str
|
|
|
|
ERASE_NVS = True
|
|
|
|
FLASH_ENCRYPT_SCHEME = None # type: str
|
|
|
|
FLASH_ENCRYPT_CNT_KEY = None # type: str
|
|
|
|
FLASH_ENCRYPT_CNT_VAL = 0
|
|
|
|
FLASH_ENCRYPT_PURPOSE = None # type: str
|
|
|
|
SECURE_BOOT_EN_KEY = None # type: str
|
|
|
|
SECURE_BOOT_EN_VAL = 0
|
|
|
|
FLASH_SECTOR_SIZE = 4096
|
|
|
|
|
|
|
|
def __init__(self, name, port, log_file, app, allow_dut_exception=False, efuse_reset_port=None, **kwargs):
|
|
|
|
super(IDFFPGADUT, self).__init__(name, port, log_file, app, allow_dut_exception=allow_dut_exception, **kwargs)
|
|
|
|
self.esp = self.get_rom()(port)
|
|
|
|
self.efuses = None
|
|
|
|
self.efuse_operations = None
|
|
|
|
self.efuse_reset_port = efuse_reset_port
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_rom(cls):
|
|
|
|
raise NotImplementedError('This is an abstraction class, method not defined.')
|
|
|
|
|
|
|
|
def erase_partition(self, esp, partition):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def enable_efuses(self):
|
|
|
|
# We use an extra COM port to reset the efuses on FPGA.
|
|
|
|
# Connect DTR pin of the COM port to the efuse reset pin on daughter board
|
|
|
|
# Set EFUSEPORT env variable to the extra COM port
|
|
|
|
if not self.efuse_reset_port:
|
|
|
|
raise RuntimeError('EFUSEPORT not specified')
|
|
|
|
|
|
|
|
# Stop any previous serial port operation
|
|
|
|
self.stop_receive()
|
|
|
|
if self.secure_boot_en:
|
|
|
|
self.esp.connect()
|
|
|
|
self.efuses, self.efuse_operations = espefuse.get_efuses(self.esp, False, False, True)
|
|
|
|
|
|
|
|
def burn_efuse(self, field, val):
|
|
|
|
if not self.efuse_operations:
|
|
|
|
self.enable_efuses()
|
|
|
|
BurnEfuseArgs = collections.namedtuple('burn_efuse_args', ['name_value_pairs', 'only_burn_at_end'])
|
|
|
|
args = BurnEfuseArgs({field: val}, False)
|
|
|
|
self.efuse_operations.burn_efuse(self.esp, self.efuses, args)
|
|
|
|
|
|
|
|
def burn_efuse_key(self, key, purpose, block):
|
|
|
|
if not self.efuse_operations:
|
|
|
|
self.enable_efuses()
|
|
|
|
BurnKeyArgs = collections.namedtuple('burn_key_args',
|
|
|
|
['keyfile', 'keypurpose', 'block',
|
|
|
|
'force_write_always', 'no_write_protect', 'no_read_protect', 'only_burn_at_end'])
|
|
|
|
args = BurnKeyArgs([key],
|
|
|
|
[purpose],
|
|
|
|
[block],
|
|
|
|
False, False, False, False)
|
|
|
|
self.efuse_operations.burn_key(self.esp, self.efuses, args)
|
|
|
|
|
|
|
|
def burn_efuse_key_digest(self, key, purpose, block):
|
|
|
|
if not self.efuse_operations:
|
|
|
|
self.enable_efuses()
|
|
|
|
BurnDigestArgs = collections.namedtuple('burn_key_digest_args',
|
|
|
|
['keyfile', 'keypurpose', 'block',
|
|
|
|
'force_write_always', 'no_write_protect', 'no_read_protect', 'only_burn_at_end'])
|
|
|
|
args = BurnDigestArgs([open(key, 'rb')],
|
|
|
|
[purpose],
|
|
|
|
[block],
|
|
|
|
False, False, True, False)
|
|
|
|
self.efuse_operations.burn_key_digest(self.esp, self.efuses, args)
|
|
|
|
|
|
|
|
def reset_efuses(self):
|
|
|
|
if not self.efuse_reset_port:
|
|
|
|
raise RuntimeError('EFUSEPORT not specified')
|
|
|
|
with serial.Serial(self.efuse_reset_port) as efuseport:
|
|
|
|
print('Resetting efuses')
|
|
|
|
efuseport.dtr = 0
|
|
|
|
self.port_inst.setRTS(1)
|
|
|
|
self.port_inst.setRTS(0)
|
|
|
|
time.sleep(1)
|
|
|
|
efuseport.dtr = 1
|
|
|
|
self.efuse_operations = None
|
|
|
|
self.efuses = None
|
|
|
|
|
|
|
|
def sign_data(self, data_file, key_files, version, append_signature=0):
|
|
|
|
SignDataArgs = collections.namedtuple('sign_data_args',
|
|
|
|
['datafile','keyfile','output', 'version', 'append_signatures'])
|
|
|
|
outfile = tempfile.NamedTemporaryFile()
|
|
|
|
args = SignDataArgs(data_file, key_files, outfile.name, str(version), append_signature)
|
|
|
|
espsecure.sign_data(args)
|
|
|
|
outfile.seek(0)
|
|
|
|
return outfile.read()
|
|
|
|
|
|
|
|
|
|
|
|
class ESP32C3FPGADUT(IDFFPGADUT):
|
|
|
|
TARGET = 'esp32c3'
|
|
|
|
TOOLCHAIN_PREFIX = 'riscv32-esp-elf-'
|
|
|
|
FLASH_ENCRYPT_SCHEME = 'AES-XTS'
|
|
|
|
FLASH_ENCRYPT_CNT_KEY = 'SPI_BOOT_CRYPT_CNT'
|
|
|
|
FLASH_ENCRYPT_CNT_VAL = 1
|
|
|
|
FLASH_ENCRYPT_PURPOSE = 'XTS_AES_128_KEY'
|
|
|
|
SECURE_BOOT_EN_KEY = 'SECURE_BOOT_EN'
|
|
|
|
SECURE_BOOT_EN_VAL = 1
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_rom(cls):
|
|
|
|
return esptool.ESP32C3ROM
|
|
|
|
|
|
|
|
def erase_partition(self, esp, partition):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def flash_encrypt_burn_cnt(self):
|
|
|
|
self.burn_efuse(self.FLASH_ENCRYPT_CNT_KEY, self.FLASH_ENCRYPT_CNT_VAL)
|
|
|
|
|
|
|
|
def flash_encrypt_burn_key(self, key, block=0):
|
|
|
|
self.burn_efuse_key(key, self.FLASH_ENCRYPT_PURPOSE, 'BLOCK_KEY%d' % block)
|
|
|
|
|
|
|
|
def flash_encrypt_get_scheme(self):
|
|
|
|
return self.FLASH_ENCRYPT_SCHEME
|
|
|
|
|
|
|
|
def secure_boot_burn_en_bit(self):
|
|
|
|
self.burn_efuse(self.SECURE_BOOT_EN_KEY, self.SECURE_BOOT_EN_VAL)
|
|
|
|
|
|
|
|
def secure_boot_burn_digest(self, digest, key_index=0, block=0):
|
|
|
|
self.burn_efuse_key_digest(digest, 'SECURE_BOOT_DIGEST%d' % key_index, 'BLOCK_KEY%d' % block)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def confirm_dut(cls, port, **kwargs):
|
|
|
|
return True, cls.TARGET
|
2021-08-25 12:46:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ESP32S3FPGADUT(IDFFPGADUT):
|
|
|
|
TARGET = 'esp32s3'
|
|
|
|
TOOLCHAIN_PREFIX = 'xtensa-esp32s3-elf-'
|
|
|
|
FLASH_ENCRYPT_SCHEME = 'AES-XTS'
|
|
|
|
FLASH_ENCRYPT_CNT_KEY = 'SPI_BOOT_CRYPT_CNT'
|
|
|
|
FLASH_ENCRYPT_CNT_VAL = 1
|
|
|
|
FLASH_ENCRYPT_PURPOSE = 'XTS_AES_128_KEY'
|
|
|
|
SECURE_BOOT_EN_KEY = 'SECURE_BOOT_EN'
|
|
|
|
SECURE_BOOT_EN_VAL = 1
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_rom(cls):
|
|
|
|
return esptool.ESP32S3ROM
|
|
|
|
|
|
|
|
def erase_partition(self, esp, partition):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def flash_encrypt_burn_cnt(self):
|
|
|
|
self.burn_efuse(self.FLASH_ENCRYPT_CNT_KEY, self.FLASH_ENCRYPT_CNT_VAL)
|
|
|
|
|
|
|
|
def flash_encrypt_burn_key(self, key, block=0):
|
|
|
|
self.burn_efuse_key(key, self.FLASH_ENCRYPT_PURPOSE, 'BLOCK_KEY%d' % block)
|
|
|
|
|
|
|
|
def flash_encrypt_get_scheme(self):
|
|
|
|
return self.FLASH_ENCRYPT_SCHEME
|
|
|
|
|
|
|
|
def secure_boot_burn_en_bit(self):
|
|
|
|
self.burn_efuse(self.SECURE_BOOT_EN_KEY, self.SECURE_BOOT_EN_VAL)
|
|
|
|
|
|
|
|
def secure_boot_burn_digest(self, digest, key_index=0, block=0):
|
|
|
|
self.burn_efuse_key_digest(digest, 'SECURE_BOOT_DIGEST%d' % key_index, 'BLOCK_KEY%d' % block)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def confirm_dut(cls, port, **kwargs):
|
|
|
|
return True, cls.TARGET
|