Skip to content
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

T3W1 power control #4379

Open
wants to merge 8 commits into
base: main
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
3 changes: 2 additions & 1 deletion core/SConscript.kernel
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ env.Replace(
'-ffreestanding '
'-fstack-protector-all '
+ env.get('ENV')["CPU_CCFLAGS"] + CCFLAGS_MOD,
LINKFLAGS='-T build/kernel/memory.ld -Wl,--gc-sections -Wl,--print-memory-usage -Wl,-Map=build/kernel/kernel.map -Wl,--warn-common',
LINKFLAGS='-T build/kernel/memory.ld -Wl,--gc-sections -Wl,--print-memory-usage '
' -Wl,-Map=build/kernel/kernel.map -Wl,--warn-common -Wl,--undefined=__errno',
CPPPATH=ALLPATHS,
CPPDEFINES=[
'KERNEL',
Expand Down
2 changes: 1 addition & 1 deletion core/SConscript.prodtest
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ program_elf = env.Command(
target='prodtest.elf',
source=obj_program,
action=
'$LINK -o $TARGET $CCFLAGS $CFLAGS $LINKFLAGS $SOURCES -lc_nano -lgcc',
'$LINK -o $TARGET $CCFLAGS $CFLAGS $LINKFLAGS $SOURCES -lc_nano -lgcc -lm',
)

env.Depends(program_elf, linkerscript_gen)
Expand Down
13 changes: 10 additions & 3 deletions core/embed/io/button/stm32/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
#include <trezor_rtl.h>

#include <io/button.h>
#include <sys/irq.h>

#ifdef USE_POWERCTL
#include <sys/wakeup_flags.h>
#endif

#ifdef KERNEL_MODE

Expand Down Expand Up @@ -89,7 +94,7 @@ bool button_init(void) {
EXTI_Config.Trigger = EXTI_TRIGGER_FALLING;
HAL_EXTI_SetConfigLine(&EXTI_Handle, &EXTI_Config);
NVIC_SetPriority(BTN_EXTI_INTERRUPT_NUM, IRQ_PRI_NORMAL);
__HAL_GPIO_EXTI_CLEAR_FLAG(BTN_INT_PIN);
__HAL_GPIO_EXTI_CLEAR_FLAG(BTN_EXTI_INTERRUPT_PIN);
NVIC_EnableIRQ(BTN_EXTI_INTERRUPT_NUM);
#endif // BTN_EXTI_INTERRUPT_HANDLER

Expand Down Expand Up @@ -180,10 +185,12 @@ void BTN_EXTI_INTERRUPT_HANDLER(void) {
// button_driver_t *drv = &g_button_driver;

// Clear the EXTI line pending bit
__HAL_GPIO_EXTI_CLEAR_FLAG(BTN_INT_PIN);
__HAL_GPIO_EXTI_CLEAR_FLAG(BTN_EXTI_INTERRUPT_PIN);

#ifdef USE_POWERCTL
// Inform the powerctl module about button press
// wakeup_flags_set(WAKEUP_FLAGS_BUTTON);
wakeup_flags_set(WAKEUP_FLAG_BUTTON);
#endif
}
#endif

Expand Down
7 changes: 7 additions & 0 deletions core/embed/models/T3W1/boards/trezor_t3w1_revA.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#define BTN_POWER_PIN GPIO_PIN_5
#define BTN_POWER_PORT GPIOE
#define BTN_POWER_CLK_ENA __HAL_RCC_GPIOE_CLK_ENABLE
#define BTN_EXTI_INTERRUPT_GPIOSEL EXTI_GPIOE
#define BTN_EXTI_INTERRUPT_LINE EXTI_LINE_5
#define BTN_EXTI_INTERRUPT_PIN GPIO_PIN_5
#define BTN_EXTI_INTERRUPT_NUM EXTI5_IRQn
#define BTN_EXTI_INTERRUPT_HANDLER EXTI5_IRQHandler

#define DISPLAY_COLOR_MODE DMA2D_OUTPUT_RGB565

Expand All @@ -22,6 +27,8 @@

#define NPM1300_I2C_INSTANCE 0

#define STWLC38_I2C_INSTANCE 1

#define I2C_COUNT 4

#define I2C_INSTANCE_0 I2C1
Expand Down
60 changes: 32 additions & 28 deletions core/embed/projects/boardloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,33 @@ static const uint8_t * const BOARDLOADER_KEYS[] = {
#endif
};

static void drivers_init(void) {
#ifdef USE_PVD
pvd_init();
#endif
#ifdef USE_TAMPER
tamper_init();
#endif
secret_init();
#ifdef USE_HASH_PROCESSOR
hash_processor_init();
#endif
gfx_bitblt_init();
display_init(DISPLAY_RESET_CONTENT);
#ifdef USE_SD_CARD
sdcard_init();
#endif
}

static void drivers_deinit(void) {
#ifdef FIXED_HW_DEINIT
// TODO
#endif

display_deinit(DISPLAY_JUMP_BEHAVIOR);
ensure_compatible_settings();
}

static uint8_t get_bootloader_min_version(void) {
uint8_t version = 0;
ensure(monoctr_read(MONOCTR_BOOTLOADER_VERSION, &version), "monoctr read");
Expand Down Expand Up @@ -232,42 +259,21 @@ static secbool copy_sdcard(void) {
int main(void) {
system_init(&rsod_panic_handler);

reset_flags_reset();

#ifdef USE_PVD
pvd_init();
#endif

if (sectrue != flash_configure_option_bytes()) {
// display is not initialized so don't call ensure
erase_storage(NULL);
return 2;
}

#ifdef USE_TAMPER
tamper_init();
#endif

#ifdef USE_TRUSTZONE
tz_init_boardloader();
#endif

secret_init();

#ifdef USE_HASH_PROCESSOR
hash_processor_init();
#endif
drivers_init();

gfx_bitblt_init();

display_init(DISPLAY_RESET_CONTENT);

gfx_clear();
display_refresh();

#if defined USE_SD_CARD
sdcard_init();
reset_flags_reset();

#ifdef USE_SD_CARD
// If the bootloader is being updated from SD card, we need to preserve the
// monotonic counter from the old bootloader. This is in case that the old
// bootloader did not have the chance yet to write its monotonic counter to
Expand Down Expand Up @@ -311,11 +317,9 @@ int main(void) {
// This includes the version of bootloader potentially updated from SD card.
write_bootloader_min_version(hdr->monotonic);

display_deinit(DISPLAY_JUMP_BEHAVIOR);

ensure_compatible_settings();
drivers_deinit();

mpu_reconfig(MPU_MODE_DISABLED);
system_deinit();

// g_boot_command is preserved on STM32U5
jump_to(IMAGE_CODE_ALIGN(BOOTLOADER_START + IMAGE_HEADER_SIZE));
Expand Down
106 changes: 54 additions & 52 deletions core/embed/projects/bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,56 @@ void failed_jump_to_firmware(void);
CONFIDENTIAL volatile secbool dont_optimize_out_true = sectrue;
CONFIDENTIAL void (*volatile firmware_jump_fn)(void) = failed_jump_to_firmware;

static void drivers_init(secbool *touch_initialized) {
random_delays_init();
#ifdef USE_PVD
pvd_init();
#endif
#ifdef USE_HASH_PROCESSOR
hash_processor_init();
#endif
gfx_bitblt_init();
display_init(DISPLAY_JUMP_BEHAVIOR);
unit_properties_init();

#ifdef USE_TOUCH
secbool allow_touchless_mode = secfalse;
#if defined TREZOR_MODEL_T3T1 || defined TREZOR_MODEL_T3W1
// on T3T1 and T3W1, tester needs to run without touch, so making an exception
// until unit variant is written in OTP
const secbool manufacturing_mode =
unit_properties()->locked ? secfalse : sectrue;
allow_touchless_mode = manufacturing_mode;

#endif
*touch_initialized = touch_init();
if (allow_touchless_mode != sectrue) {
ensure(*touch_initialized, "Touch screen panel was not loaded properly.");
}
#endif

#ifdef USE_OPTIGA
optiga_hal_init();
#endif
#ifdef USE_BUTTON
button_init();
#endif
#ifdef USE_CONSUMPTION_MASK
consumption_mask_init();
#endif
#ifdef USE_RGB_LED
rgb_led_init();
#endif
}

static void drivers_deinit(void) {
#ifdef FIXED_HW_DEINIT
// TODO
#endif
display_deinit(DISPLAY_JUMP_BEHAVIOR);
ensure_compatible_settings();
}

static void usb_init_all(secbool usb21_landing) {
usb_dev_info_t dev_info = {
.device_class = 0x00,
Expand Down Expand Up @@ -324,11 +374,9 @@ void real_jump_to_firmware(void) {
ui_screen_boot_stage_1(false);
}

display_deinit(DISPLAY_JUMP_BEHAVIOR);

ensure_compatible_settings();
drivers_deinit();

mpu_reconfig(MPU_MODE_DISABLED);
system_deinit();

jump_to(IMAGE_CODE_ALIGN(FIRMWARE_START + vhdr.hdrlen + IMAGE_HEADER_SIZE));
}
Expand All @@ -347,41 +395,11 @@ int main(void) {
int bootloader_main(void) {
#endif
secbool stay_in_bootloader = secfalse;
secbool touch_initialized = secfalse;

system_init(&rsod_panic_handler);

random_delays_init();

#ifdef USE_PVD
pvd_init();
#endif

#ifdef USE_HASH_PROCESSOR
hash_processor_init();
#endif

gfx_bitblt_init();

display_init(DISPLAY_JUMP_BEHAVIOR);

unit_properties_init();

#ifdef USE_TOUCH
secbool touch_initialized = secfalse;
secbool allow_touchless_mode = secfalse;
#if defined TREZOR_MODEL_T3T1 || defined TREZOR_MODEL_T3W1
// on T3T1 and T3W1, tester needs to run without touch, so making an exception
// until unit variant is written in OTP
const secbool manufacturing_mode =
unit_properties()->locked ? secfalse : sectrue;
allow_touchless_mode = manufacturing_mode;

#endif
touch_initialized = touch_init();
if (allow_touchless_mode != sectrue) {
ensure(touch_initialized, "Touch screen panel was not loaded properly.");
}
#endif
drivers_init(&touch_initialized);

ui_screen_boot_stage_1(false);

Expand Down Expand Up @@ -449,22 +467,6 @@ int bootloader_main(void) {
firmware_present_backup = firmware_present;
}

#ifdef USE_OPTIGA
optiga_hal_init();
#endif

#ifdef USE_BUTTON
button_init();
#endif

#ifdef USE_CONSUMPTION_MASK
consumption_mask_init();
#endif

#ifdef USE_RGB_LED
rgb_led_init();
#endif

#if PRODUCTION && !defined STM32U5
// for STM32U5, this check is moved to boardloader
ensure_bootloader_min_version();
Expand Down
35 changes: 20 additions & 15 deletions core/embed/projects/bootloader_ci/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
#include <util/image.h>
#include <util/rsod.h>

#ifdef USE_TOUCH
#include <io/touch.h>
#endif

#include "version.h"

#include "bootui.h"
Expand All @@ -52,6 +48,22 @@

#define USB_IFACE_NUM 0

static void drivers_init(void) {
display_init(DISPLAY_RESET_CONTENT);

random_delays_init();

#ifdef USE_HASH_PROCESSOR
hash_processor_init();
#endif
}

static void drivers_deinit(void) {
#ifdef FIXED_HW_DEINIT
display_deinit(DISPLAY_RESET_CONTENT);
#endif
}

static void usb_init_all(secbool usb21_landing) {
usb_dev_info_t dev_info = {
.device_class = 0x00,
Expand Down Expand Up @@ -180,22 +192,13 @@ static secbool check_vendor_header_lock(const vendor_header *const vhdr) {
int main(void) {
system_init(&rsod_panic_handler);

random_delays_init();
#ifdef USE_TOUCH
touch_init();
#endif

#ifdef USE_HASH_PROCESSOR
hash_processor_init();
#endif
drivers_init();

#if PRODUCTION && !defined STM32U5
// for STM32U5, this check is moved to boardloader
ensure_bootloader_min_version();
#endif

gfx_clear();

const image_header *hdr = NULL;
vendor_header vhdr;
// detect whether the device contains a valid firmware
Expand Down Expand Up @@ -269,7 +272,9 @@ int main(void) {

// do not check any trust flags on header, proceed

mpu_reconfig(MPU_MODE_DISABLED);
drivers_deinit();

system_deinit();

jump_to(IMAGE_CODE_ALIGN(FIRMWARE_START + vhdr.hdrlen + IMAGE_HEADER_SIZE));

Expand Down
8 changes: 8 additions & 0 deletions core/embed/projects/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
#include <sec/optiga_transport.h>
#endif

#ifdef USE_POWERCTL
#include <sys/powerctl.h>
#endif

#ifdef USE_PVD
#include <sys/pvd.h>
#endif
Expand Down Expand Up @@ -104,6 +108,10 @@ static void optiga_log_hex(const char *prefix, const uint8_t *data,
#endif

void drivers_init() {
#ifdef USE_POWERCTL
powerctl_init();
#endif

#ifdef USE_TAMPER
tamper_init();
#endif
Expand Down
Loading
Loading