Skip to content

Debug console #5619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: cepetr/usb-config
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ QUIET_MODE ?= 0
TREZOR_DISABLE_ANIMATION ?= $(if $(filter 0,$(PYOPT)),1,0)
STORAGE_INSECURE_TESTING_MODE ?= 0
UI_PERFORMANCE_OVERLAY ?= 0
DBG_CONSOLE ?=

# If set, VCP writes will be blocking, in order to allow reliable debug data transmission over VCP.
# Disabled by default, to prevent debug firmware from getting stuck while writing log messages (if the host is not reading them).
Expand Down Expand Up @@ -145,7 +146,8 @@ SCONS_VARS = \
TREZOR_MEMPERF="$(TREZOR_MEMPERF)" \
TREZOR_MODEL="$(TREZOR_MODEL)" \
UI_PERFORMANCE_OVERLAY="$(UI_PERFORMANCE_OVERLAY)" \
BLOCK_ON_VCP="$(BLOCK_ON_VCP)"
BLOCK_ON_VCP="$(BLOCK_ON_VCP)" \
DBG_CONSOLE="$(DBG_CONSOLE)"

SCONS_OPTS = -Q -j $(JOBS)
ifeq ($(QUIET_MODE),1)
Expand Down
12 changes: 12 additions & 0 deletions core/SConscript.boardloader
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import tools, models
TREZOR_MODEL = ARGUMENTS.get('TREZOR_MODEL', 'T2T1')
CMAKELISTS = int(ARGUMENTS.get('CMAKELISTS', 0))
HW_REVISION = ARGUMENTS.get('HW_REVISION', None)
DBG_CONSOLE = ARGUMENTS.get('DBG_CONSOLE', '')

FEATURES_WANTED = [
"boot_ucb",
Expand All @@ -26,6 +27,17 @@ CPPDEFINES_HAL = []
SOURCE_HAL = []
PATH_HAL = []

if DBG_CONSOLE == "SWO":
CPPDEFINES_MOD += ["USE_DBG_CONSOLE_SWO"]
elif DBG_CONSOLE == "SYSTEM_VIEW":
FEATURES_WANTED += ["system_view"]
CPPDEFINES_MOD += ["USE_DBG_CONSOLE_SYSTEM_VIEW"]
elif DBG_CONSOLE == "" or DBG_CONSOLE == "VCP":
pass
else:
raise RuntimeError("DBG_CONSOLE argument is invalid")


env = Environment(ENV=os.environ,
CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0')),
CPPDEFINES_IMPLICIT=[],
Expand Down
15 changes: 15 additions & 0 deletions core/SConscript.bootloader
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ BOOTLOADER_QA = ARGUMENTS.get('BOOTLOADER_QA', '0') == '1'
PRODUCTION = 0 if BOOTLOADER_QA else ARGUMENTS.get('PRODUCTION', '0') == '1'
HW_REVISION = ARGUMENTS.get('HW_REVISION', None)
UI_PERFORMANCE_OVERLAY = ARGUMENTS.get('UI_PERFORMANCE_OVERLAY', '0') == '1'
DBG_CONSOLE = ARGUMENTS.get('DBG_CONSOLE', '')

FEATURES_WANTED = [
"ble",
Expand Down Expand Up @@ -43,6 +44,20 @@ SOURCE_HAL = []
PATH_HAL = []
RUST_UI_FEATURES = []

if DBG_CONSOLE == "VCP":
FEATURES_WANTED += ["usb_iface_vcp"]
CPPDEFINES_MOD += ["USE_DBG_CONSOLE_VCP"]
elif DBG_CONSOLE == "SWO":
CPPDEFINES_MOD += ["USE_DBG_CONSOLE_SWO"]
elif DBG_CONSOLE == "SYSTEM_VIEW":
FEATURES_WANTED += ["system_view"]
CPPDEFINES_MOD += ["USE_DBG_CONSOLE_SYSTEM_VIEW"]
elif DBG_CONSOLE == "":
pass
else:
raise RuntimeError("DBG_CONSOLE argument is invalid")


env = Environment(
ENV=os.environ,
CFLAGS=f"{ARGUMENTS.get('CFLAGS', '')} -DPRODUCTION={int(PRODUCTION)} -DBOOTLOADER_QA={int(BOOTLOADER_QA)}",
Expand Down
53 changes: 25 additions & 28 deletions core/SConscript.kernel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ PYOPT = ARGUMENTS.get('PYOPT', '1')
DISABLE_OPTIGA = ARGUMENTS.get('DISABLE_OPTIGA', '0') == '1'
HW_REVISION = ARGUMENTS.get('HW_REVISION', None)
THP = ARGUMENTS.get('THP', '0') == '1' # Trezor-Host Protocol
DBG_CONSOLE = ARGUMENTS.get('DBG_CONSOLE', '')

STORAGE_INSECURE_TESTING_MODE = ARGUMENTS.get('STORAGE_INSECURE_TESTING_MODE', '0') == '1'
if STORAGE_INSECURE_TESTING_MODE and PRODUCTION:
Expand All @@ -24,10 +25,18 @@ if STORAGE_INSECURE_TESTING_MODE:
DISABLE_OPTIGA = True
PYOPT = "0"

CCFLAGS_MOD = ''
CPPPATH_MOD = []
CPPDEFINES_MOD = []
SOURCE_MOD = []
SOURCE_MOD_CRYPTO = []
CPPDEFINES_HAL = []
SOURCE_HAL = []
PATH_HAL = []

FEATURE_FLAGS = {
"RDI": True,
"SECP256K1_ZKP": True, # required for trezor.crypto.curve.bip340 (BIP340/Taproot)
"SYSTEM_VIEW": False,
"AES_GCM": True,
}

Expand Down Expand Up @@ -62,8 +71,22 @@ if BITCOIN_ONLY == '0':
if PYOPT == '0':
FEATURES_WANTED += [
"usb_iface_debug",
"usb_iface_vcp",
]
DBG_CONSOLE = DBG_CONSOLE or "VCP"

if DBG_CONSOLE == "VCP":
FEATURES_WANTED += ["usb_iface_vcp"]
CPPDEFINES_MOD += ["USE_DBG_CONSOLE_VCP"]
elif DBG_CONSOLE == "SWO":
CPPDEFINES_MOD += ["USE_DBG_CONSOLE_SWO"]
elif DBG_CONSOLE == "SYSTEM_VIEW":
FEATURES_WANTED += ["system_view"]
CPPDEFINES_MOD += ["USE_DBG_CONSOLE_SYSTEM_VIEW"]
elif DBG_CONSOLE == "":
pass
else:
raise RuntimeError("DBG_CONSOLE argument is invalid")


if not TREZOR_MODEL in ['T3W1', 'D002']:
FEATURES_WANTED += ["secure_mode"]
Expand All @@ -74,15 +97,6 @@ if DISABLE_OPTIGA:
raise RuntimeError("DISABLE_OPTIGA requires non-production build")
FEATURES_WANTED.remove("optiga")

CCFLAGS_MOD = ''
CPPPATH_MOD = []
CPPDEFINES_MOD = []
SOURCE_MOD = []
SOURCE_MOD_CRYPTO = []
CPPDEFINES_HAL = []
SOURCE_HAL = []
PATH_HAL = []

FROZEN = True

# modtrezorcrypto
Expand Down Expand Up @@ -232,23 +246,6 @@ CPPDEFINES_MOD += [
if FEATURE_FLAGS["RDI"]:
CPPDEFINES_MOD += ['RDI']

if FEATURE_FLAGS["SYSTEM_VIEW"]:
SOURCE_MOD += [
'embed/sys/systemview/stm32/config/SEGGER_SYSVIEW_Config_NoOS.c',
'embed/sys/systemview/stm32/segger/SEGGER_SYSVIEW.c',
'embed/sys/systemview/stm32/segger/SEGGER_RTT.c',
'embed/sys/systemview/stm32/segger/SEGGER_RTT_ASM_ARMv7M.S',
'embed/sys/systemview/stm32/segger/Syscalls/SEGGER_RTT_Syscalls_GCC.c',
'embed/sys/systemview/systemview.c',
]
CPPPATH_MOD += [
'embed/sys/systemview/inc',
'embed/sys/systemview/stm32/config',
'embed/sys/systemview/stm32/segger',
]
CPPDEFINES_MOD += ['SYSTEM_VIEW']
CCFLAGS_MOD += '-DSYSTEM_VIEW '

TRANSLATION_DATA = [
"translations/en.json",
"translations/order.json",
Expand Down
11 changes: 11 additions & 0 deletions core/SConscript.prodtest
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ PRODUCTION = ARGUMENTS.get('PRODUCTION', '0') == '1'
BOOTLOADER_DEVEL = ARGUMENTS.get('BOOTLOADER_DEVEL', '0') == '1'
HW_REVISION = ARGUMENTS.get('HW_REVISION', None)
UI_PERFORMANCE_OVERLAY = ARGUMENTS.get('UI_PERFORMANCE_OVERLAY', '0') == '1'
DBG_CONSOLE = ARGUMENTS.get('DBG_CONSOLE', '')

FEATURE_FLAGS = {
"AES_GCM": True,
Expand Down Expand Up @@ -58,6 +59,16 @@ SOURCE_HAL = []
PATH_HAL = []
RUST_UI_FEATURES = []

if DBG_CONSOLE == "SWO":
CPPDEFINES_MOD += ["USE_DBG_CONSOLE_SWO"]
elif DBG_CONSOLE == "SYSTEM_VIEW":
FEATURES_WANTED += ["system_view"]
CPPDEFINES_MOD += ["USE_DBG_CONSOLE_SYSTEM_VIEW"]
elif DBG_CONSOLE == "" or DBG_CONSOLE == "VCP":
pass
else:
raise RuntimeError("DBG_CONSOLE argument is invalid")

env = Environment(
ENV=os.environ,
CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0')),
Expand Down
29 changes: 11 additions & 18 deletions core/SConscript.secmon
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ PYOPT = ARGUMENTS.get('PYOPT', '1')
DISABLE_OPTIGA = ARGUMENTS.get('DISABLE_OPTIGA', '0') == '1'
HW_REVISION = ARGUMENTS.get('HW_REVISION', None)
THP = ARGUMENTS.get('THP', '0') == '1' # Trezor-Host Protocol
DBG_CONSOLE = ARGUMENTS.get('DBG_CONSOLE', '')

STORAGE_INSECURE_TESTING_MODE = ARGUMENTS.get('STORAGE_INSECURE_TESTING_MODE', '0') == '1'
if STORAGE_INSECURE_TESTING_MODE and PRODUCTION:
Expand All @@ -27,7 +28,6 @@ if STORAGE_INSECURE_TESTING_MODE:
FEATURE_FLAGS = {
"RDI": True,
"SECP256K1_ZKP": True, # required for trezor.crypto.curve.bip340 (BIP340/Taproot)
"SYSTEM_VIEW": False,
"AES_GCM": True,
}

Expand Down Expand Up @@ -59,6 +59,16 @@ CPPDEFINES_HAL = []
SOURCE_HAL = []
PATH_HAL = []

if DBG_CONSOLE == "SWO":
CPPDEFINES_MOD += ["USE_DBG_CONSOLE_SWO"]
elif DBG_CONSOLE == "SYSTEM_VIEW":
FEATURES_WANTED += ["system_view"]
CPPDEFINES_MOD += ["USE_DBG_CONSOLE_SYSTEM_VIEW"]
elif DBG_CONSOLE == "" or DBG_CONSOLE == "VCP":
pass
else:
raise RuntimeError("DBG_CONSOLE argument is invalid")

FROZEN = True

env = Environment(
Expand Down Expand Up @@ -218,23 +228,6 @@ CPPDEFINES_MOD += [
if FEATURE_FLAGS["RDI"]:
CPPDEFINES_MOD += ['RDI']

if FEATURE_FLAGS["SYSTEM_VIEW"]:
SOURCE_MOD += [
'embed/sys/systemview/stm32/config/SEGGER_SYSVIEW_Config_NoOS.c',
'embed/sys/systemview/stm32/segger/SEGGER_SYSVIEW.c',
'embed/sys/systemview/stm32/segger/SEGGER_RTT.c',
'embed/sys/systemview/stm32/segger/SEGGER_RTT_ASM_ARMv7M.S',
'embed/sys/systemview/stm32/segger/Syscalls/SEGGER_RTT_Syscalls_GCC.c',
'embed/sys/systemview/systemview.c',
]
CPPPATH_MOD += [
'embed/sys/systemview/inc',
'embed/sys/systemview/stm32/config',
'embed/sys/systemview/stm32/segger',
]
CPPDEFINES_MOD += ['SYSTEM_VIEW']
CCFLAGS_MOD += '-DSYSTEM_VIEW '

TRANSLATION_DATA = [
"translations/en.json",
"translations/order.json",
Expand Down
9 changes: 0 additions & 9 deletions core/embed/projects/firmware/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,6 @@
#define MICROPY_PY_TREZORTRANSLATE (1)
#define MICROPY_PY_TREZORUI_API (1)

#ifdef SYSTEM_VIEW
#define MP_PLAT_PRINT_STRN(str, len) segger_print(str, len)
// uncomment DEST_RTT and comment DEST_SYSTEMVIEW
// if you want to print to RTT instead of SystemView
// OpenOCD supports only the RTT output method
// #define SYSTEMVIEW_DEST_RTT (1)
#define SYSTEMVIEW_DEST_SYSTEMVIEW (1)
#endif

#define MP_STATE_PORT MP_STATE_VM

// by default contains nearest git tag, which may not be present in shallow
Expand Down
11 changes: 3 additions & 8 deletions core/embed/projects/firmware/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,17 @@

#include "py/mphal.h"

#include <io/usb_config.h>
#include <sys/dbg_console.h>
#include <sys/systick.h>

int mp_hal_stdin_rx_chr(void) {
uint8_t c = 0;
int r = syshandle_read(SYSHANDLE_USB_VCP, &c, sizeof(c));
(void)r;
dbg_console_read(&c, sizeof(c));
return c;
}

void mp_hal_stdout_tx_strn(const char *str, size_t len) {
// The write timeout defaults to 0, because otherwise when the VCP receive
// buffer on the host gets full, the timeout will block device operation.
int r = syshandle_write_blocking(SYSHANDLE_USB_VCP, (const uint8_t *)str, len,
BLOCK_ON_VCP ? 1000 : 0);
(void)r;
dbg_console_write(str, len);
}

// Dummy implementation required by ports/stm32/gccollect.c.
Expand Down
8 changes: 0 additions & 8 deletions core/embed/projects/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@
#include <sys/rtc.h>
#endif

#ifdef SYSTEM_VIEW
#include <sys/systemview.h>
#endif

#ifdef USE_TAMPER
#include <sys/tamper.h>
#endif
Expand Down Expand Up @@ -144,10 +140,6 @@ void drivers_init() {
pvd_init();
#endif

#ifdef SYSTEM_VIEW
enable_systemview();
#endif

display_init(DISPLAY_JUMP_BEHAVIOR);

#ifdef SECURE_MODE
Expand Down
4 changes: 2 additions & 2 deletions core/embed/projects/unix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <io/display.h>
#include <io/usb_config.h>
#include <sec/secret.h>
#include <sys/dbg_console.h>
#include <sys/system.h>
#include <sys/systimer.h>
#include <util/flash.h>
Expand Down Expand Up @@ -84,9 +85,8 @@ long heap_size = 1024 * 1024 * (sizeof(mp_uint_t) / 4);

STATIC void stderr_print_strn(void *env, const char *str, size_t len) {
(void)env;
ssize_t dummy = write(STDERR_FILENO, str, len);
dbg_console_write(str, len);
mp_uos_dupterm_tx_strn(str, len);
(void)dummy;
}

const mp_print_t mp_stderr_print = {NULL, stderr_print_strn};
Expand Down
14 changes: 9 additions & 5 deletions core/embed/sec/optiga/optiga_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <sec/optiga_commands.h>
#include <sec/optiga_transport.h>
#include <sec/secret_keys.h>
#include <sys/dbg_console.h>
#include <sys/systick.h>

#include "memzero.h"
Expand All @@ -33,17 +34,20 @@
#include <inttypes.h>
#if 1 // color log
#define OPTIGA_LOG_FORMAT \
"%" PRIu32 " \x1b[35moptiga\x1b[0m \x1b[32mDEBUG\x1b[0m %s: "
"%d.%03d \x1b[35moptiga\x1b[0m \x1b[32mDEBUG\x1b[0m %s: "
#else
#define OPTIGA_LOG_FORMAT "%" PRIu32 " optiga DEBUG %s: "
#define OPTIGA_LOG_FORMAT "%d.%03d optiga DEBUG %s: "
#endif
static void optiga_log_hex(const char *prefix, const uint8_t *data,
size_t data_size) {
printf(OPTIGA_LOG_FORMAT, hal_ticks_ms() * 1000, prefix);
ticks_t now = hal_ticks_ms();
uint32_t sec = now / 1000;
uint32_t msec = now % 1000;
dbg_console_printf(OPTIGA_LOG_FORMAT, sec, msec, prefix);
for (size_t i = 0; i < data_size; i++) {
printf("%02x", data[i]);
dbg_console_printf("%02x", data[i]);
}
printf("\n");
dbg_console_printf("\n");
}
#endif

Expand Down
Loading