Skip to content

Commit

Permalink
refactor(core): refactor unit properties detection
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
cepetr committed Oct 2, 2024
1 parent a0c8d5d commit 3269f60
Show file tree
Hide file tree
Showing 22 changed files with 383 additions and 188 deletions.
1 change: 0 additions & 1 deletion core/SConscript.bootloader
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ SOURCE_MOD += [
'embed/lib/mini_printf.c',
'embed/lib/rsod.c',
'embed/lib/terminal.c',
'embed/lib/unit_variant.c',
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',
'vendor/micropython/lib/uzlib/tinflate.c',
Expand Down
2 changes: 1 addition & 1 deletion core/SConscript.bootloader_emu
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ SOURCE_MOD += [
'embed/lib/mini_printf.c',
'embed/lib/rsod.c',
'embed/lib/terminal.c',
'embed/lib/unit_variant.c',
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',
'vendor/micropython/lib/uzlib/tinflate.c',
Expand Down Expand Up @@ -132,6 +131,7 @@ SOURCE_TREZORHAL = [
'embed/trezorhal/unix/system.c',
'embed/trezorhal/unix/systick.c',
'embed/trezorhal/unix/systimer.c',
'embed/trezorhal/unix/unit_properties.c',
'embed/trezorhal/unix/usb.c',
]

Expand Down
1 change: 0 additions & 1 deletion core/SConscript.firmware
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ SOURCE_MOD += [
'embed/lib/rsod.c',
'embed/lib/terminal.c',
'embed/lib/translations.c',
'embed/lib/unit_variant.c',
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',
'vendor/micropython/lib/uzlib/tinflate.c',
Expand Down
1 change: 0 additions & 1 deletion core/SConscript.kernel
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ SOURCE_MOD += [
'embed/lib/rsod.c',
'embed/lib/terminal.c',
'embed/lib/translations.c',
'embed/lib/unit_variant.c',
'embed/extmod/modtrezorcrypto/rand.c',
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',
Expand Down
2 changes: 1 addition & 1 deletion core/SConscript.unix
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ SOURCE_MOD += [
'embed/lib/rsod.c',
'embed/lib/terminal.c',
'embed/lib/translations.c',
'embed/lib/unit_variant.c',
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',
'vendor/micropython/lib/uzlib/tinflate.c',
Expand Down Expand Up @@ -402,6 +401,7 @@ SOURCE_UNIX = [
'embed/trezorhal/unix/systick.c',
'embed/trezorhal/unix/systimer.c',
'embed/trezorhal/unix/time_estimate.c',
'embed/trezorhal/unix/unit_properties.c',
'embed/trezorhal/unix/usb.c',
'embed/unix/main_main.c',
'embed/unix/main.c',
Expand Down
6 changes: 3 additions & 3 deletions core/embed/bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
#include "messages.h"
#include "monoctr.h"
#include "rust_ui.h"
#include "unit_variant.h"
#include "unit_properties.h"
#include "version_check.h"

#ifdef TREZOR_EMULATOR
Expand Down Expand Up @@ -375,7 +375,7 @@ int bootloader_main(void) {
dma2d_init();
#endif

unit_variant_init();
unit_properties_init();

#ifdef USE_TOUCH
secbool touch_initialized = secfalse;
Expand All @@ -384,7 +384,7 @@ int bootloader_main(void) {
// on T3T1, tester needs to run without touch, so making an exception
// until unit variant is written in OTP
const secbool manufacturing_mode =
unit_variant_present() ? secfalse : sectrue;
unit_properties()->locked ? secfalse : sectrue;
allow_touchless_mode = manufacturing_mode;

#endif
Expand Down
14 changes: 9 additions & 5 deletions core/embed/bootloader/messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "image.h"
#include "secbool.h"
#include "secret.h"
#include "unit_variant.h"
#include "unit_properties.h"
#include "usb.h"
#include "version.h"

Expand Down Expand Up @@ -315,10 +315,14 @@ static void send_msg_features(uint8_t iface_num,
} else {
MSG_SEND_ASSIGN_VALUE(firmware_present, false);
}
if (unit_variant_present()) {
MSG_SEND_ASSIGN_VALUE(unit_color, unit_variant_get_color());
MSG_SEND_ASSIGN_VALUE(unit_packaging, unit_variant_get_packaging());
MSG_SEND_ASSIGN_VALUE(unit_btconly, unit_variant_get_btconly());
if (unit_properties()->color_is_valid) {
MSG_SEND_ASSIGN_VALUE(unit_color, unit_properties()->color);
}
if (unit_properties()->packaging_is_valid) {
MSG_SEND_ASSIGN_VALUE(unit_packaging, unit_properties()->packaging);
}
if (unit_properties()->btconly_is_valid) {
MSG_SEND_ASSIGN_VALUE(unit_btconly, unit_properties()->btconly);
}

#if USE_OPTIGA
Expand Down
16 changes: 8 additions & 8 deletions core/embed/extmod/modtrezorutils/modtrezorutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "bootutils.h"
#include "error_handling.h"
#include "fwutils.h"
#include "unit_variant.h"
#include "unit_properties.h"
#include "usb.h"
#include TREZOR_BOARD
#include "model.h"
Expand Down Expand Up @@ -195,10 +195,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_firmware_vendor_obj,
/// Returns the color of the unit.
/// """
STATIC mp_obj_t mod_trezorutils_unit_color(void) {
if (!unit_variant_present()) {
if (!unit_properties()->color_is_valid) {
return mp_const_none;
}
return mp_obj_new_int(unit_variant_get_color());
return mp_obj_new_int(unit_properties()->color);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_unit_color_obj,
mod_trezorutils_unit_color);
Expand All @@ -208,10 +208,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_unit_color_obj,
/// Returns True if the unit is BTConly.
/// """
STATIC mp_obj_t mod_trezorutils_unit_btconly(void) {
if (!unit_variant_present()) {
if (!unit_properties()->btconly_is_valid) {
return mp_const_none;
}
return unit_variant_get_btconly() ? mp_const_true : mp_const_false;
return unit_properties()->btconly ? mp_const_true : mp_const_false;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_unit_btconly_obj,
mod_trezorutils_unit_btconly);
Expand All @@ -221,10 +221,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_unit_btconly_obj,
/// Returns the packaging version of the unit.
/// """
STATIC mp_obj_t mod_trezorutils_unit_packaging(void) {
if (!unit_variant_present()) {
if (!unit_properties()->packaging_is_valid) {
return mp_const_none;
}
return mp_obj_new_int(unit_variant_get_packaging());
return mp_obj_new_int(unit_properties()->packaging);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_unit_packaging_obj,
mod_trezorutils_unit_packaging);
Expand All @@ -234,7 +234,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_unit_packaging_obj,
/// Returns True if SD card hot swapping is enabled
/// """
STATIC mp_obj_t mod_trezorutils_sd_hotswap_enabled(void) {
return unit_variant_is_sd_hotswap_enabled() ? mp_const_true : mp_const_false;
return unit_properties()->sd_hotswap_enabled ? mp_const_true : mp_const_false;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_sd_hotswap_enabled_obj,
mod_trezorutils_sd_hotswap_enabled);
Expand Down
4 changes: 2 additions & 2 deletions core/embed/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#include "systick.h"
#include "tamper.h"
#include "touch.h"
#include "unit_variant.h"
#include "unit_properties.h"

#ifdef USE_OPTIGA
#if !PYOPT
Expand Down Expand Up @@ -103,7 +103,7 @@ void drivers_init() {

parse_boardloader_capabilities();

unit_variant_init();
unit_properties_init();

#ifdef STM32U5
secure_aes_init();
Expand Down
100 changes: 0 additions & 100 deletions core/embed/lib/unit_variant.c

This file was deleted.

20 changes: 0 additions & 20 deletions core/embed/lib/unit_variant.h

This file was deleted.

23 changes: 4 additions & 19 deletions core/embed/trezorhal/stm32f4/syscall_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "systick.h"
#include "touch.h"
#include "translations.h"
#include "unit_variant.h"
#include "unit_properties.h"
#include "usb.h"
#include "usb_hid.h"
#include "usb_vcp.h"
Expand Down Expand Up @@ -383,24 +383,9 @@ __attribute((no_stack_protector)) void syscall_handler(uint32_t *args,
} break;
#endif

case SYSCALL_UNIT_VARIANT_PRESENT: {
args[0] = unit_variant_present();
} break;

case SYSCALL_UNIT_VARIANT_GET_COLOR: {
args[0] = unit_variant_get_color();
} break;

case SYSCALL_UNIT_VARIANT_GET_PACKAGING: {
args[0] = unit_variant_get_packaging();
} break;

case SYSCALL_UNIT_VARIANT_GET_BTCONLY: {
args[0] = unit_variant_get_btconly();
} break;

case SYSCALL_UNIT_VARIANT_IS_SD_HOTSWAP_ENABLED: {
args[0] = unit_variant_is_sd_hotswap_enabled();
case SYSCALL_UNIT_PROPERTIES_GET: {
unit_properties_t *props = (unit_properties_t *)args[0];
unit_properties_get__verified(props);
} break;

case SYSCALL_SECRET_BOOTLOADER_LOCKED: {
Expand Down
6 changes: 1 addition & 5 deletions core/embed/trezorhal/stm32f4/syscall_numbers.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ typedef enum {
SYSCALL_SDCARD_READ_BLOCKS,
SYSCALL_SDCARD_WRITE_BLOCKS,

SYSCALL_UNIT_VARIANT_PRESENT,
SYSCALL_UNIT_VARIANT_GET_COLOR,
SYSCALL_UNIT_VARIANT_GET_PACKAGING,
SYSCALL_UNIT_VARIANT_GET_BTCONLY,
SYSCALL_UNIT_VARIANT_IS_SD_HOTSWAP_ENABLED,
SYSCALL_UNIT_PROPERTIES_GET,

SYSCALL_SECRET_BOOTLOADER_LOCKED,

Expand Down
23 changes: 6 additions & 17 deletions core/embed/trezorhal/stm32f4/syscall_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,32 +334,21 @@ secbool __wur sdcard_write_blocks(const uint32_t *src, uint32_t block_num,
}

// =============================================================================
// unit_variant.h
// unit_properties.h
// =============================================================================

bool unit_variant_present(void) {
return (bool)syscall_invoke0(SYSCALL_UNIT_VARIANT_PRESENT);
}
uint8_t unit_variant_get_color(void) {
return (uint8_t)syscall_invoke0(SYSCALL_UNIT_VARIANT_GET_COLOR);
}

uint8_t unit_variant_get_packaging(void) {
return (uint8_t)syscall_invoke0(SYSCALL_UNIT_VARIANT_GET_PACKAGING);
}
#include "unit_properties.h"

bool unit_variant_get_btconly(void) {
return (bool)syscall_invoke0(SYSCALL_UNIT_VARIANT_GET_BTCONLY);
}

bool unit_variant_is_sd_hotswap_enabled(void) {
return (bool)syscall_invoke0(SYSCALL_UNIT_VARIANT_IS_SD_HOTSWAP_ENABLED);
void unit_properties_get(unit_properties_t *props) {
syscall_invoke1((uint32_t)props, SYSCALL_UNIT_PROPERTIES_GET);
}

// =============================================================================
// secret.h
// =============================================================================

#include "secret.h"

secbool secret_bootloader_locked(void) {
return (secbool)syscall_invoke0(SYSCALL_SECRET_BOOTLOADER_LOCKED);
}
Expand Down
Loading

0 comments on commit 3269f60

Please sign in to comment.