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

platform, common, lowlevel modules cleanup #4249

Draft
wants to merge 7 commits into
base: cepetr/unit-props-refactoring
Choose a base branch
from
Draft
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
1 change: 0 additions & 1 deletion core/SConscript.bootloader_emu
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ SOURCE_BOOTLOADER = [

SOURCE_TREZORHAL = [
'embed/trezorhal/unix/bootutils.c',
'embed/trezorhal/unix/common.c',
'embed/trezorhal/unix/flash.c',
'embed/trezorhal/unix/flash_otp.c',
'embed/trezorhal/unix/mpu.c',
Expand Down
1 change: 0 additions & 1 deletion core/SConscript.unix
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ SOURCE_MICROPYTHON = [

SOURCE_UNIX = [
'embed/trezorhal/unix/bootutils.c',
'embed/trezorhal/unix/common.c',
'embed/trezorhal/unix/entropy.c',
'embed/trezorhal/unix/flash_otp.c',
'embed/trezorhal/unix/flash.c',
Expand Down
14 changes: 6 additions & 8 deletions core/embed/boardloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "image.h"
#include "model.h"
#include "mpu.h"
#include "pvd.h"
#include "reset_flags.h"
#include "rng.h"
#include "rsod.h"
#include "secret.h"
Expand All @@ -56,9 +58,9 @@
#endif
#endif

#include "lowlevel.h"
#include "model.h"
#include "monoctr.h"
#include "option_bytes.h"
#include "version.h"

#include "memzero.h"
Expand Down Expand Up @@ -239,9 +241,9 @@ int main(void) {

reset_flags_reset();

// need the systick timer running before many HAL operations.
// want the PVD enabled before flash operations too.
periph_init();
#ifdef USE_PVD
pvd_init();
#endif

if (sectrue != flash_configure_option_bytes()) {
// display is not initialized so don't call ensure
Expand All @@ -257,10 +259,6 @@ int main(void) {

secret_init();

#ifdef STM32F4
clear_otg_hs_memory();
#endif

#ifdef USE_SDRAM
sdram_init();
#endif
Expand Down
31 changes: 26 additions & 5 deletions core/embed/bootloader/emulator.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <unistd.h>

#include <SDL.h>

#include TREZOR_BOARD
#include "bootargs.h"
#include "bootui.h"
Expand All @@ -20,11 +22,6 @@

uint8_t *FIRMWARE_START = 0;

void set_core_clock(int) {}

// used in fw emulator to raise python exception on exit
void __attribute__((noreturn)) main_clean_exit() { exit(3); }

int bootloader_main(void);

// assuming storage is single subarea
Expand Down Expand Up @@ -91,7 +88,31 @@ bool load_firmware(const char *filename, uint8_t *hash) {
return true;
}

static int sdl_event_filter(void *userdata, SDL_Event *event) {
switch (event->type) {
case SDL_QUIT:
exit(3);
return 0;
case SDL_KEYUP:
if (event->key.repeat) {
return 0;
}
switch (event->key.keysym.sym) {
case SDLK_ESCAPE:
exit(3);
return 0;
case SDLK_p:
display_save("emu");
return 0;
}
break;
}
return 1;
}

__attribute__((noreturn)) int main(int argc, char **argv) {
SDL_SetEventFilter(sdl_event_filter, NULL);

display_init(DISPLAY_RESET_CONTENT);
flash_init();
flash_otp_init();
Expand Down
3 changes: 0 additions & 3 deletions core/embed/bootloader/emulator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef __EMULATOR_H__
#define __EMULATOR_H__

#define CLOCK_180_MHZ 0
#define mini_snprintf snprintf

#undef FIRMWARE_START
Expand All @@ -11,8 +10,6 @@

extern uint8_t *FIRMWARE_START;

void emulator_poll_events(void);
void set_core_clock(int);
__attribute__((noreturn)) void jump_to(uint32_t address);

#endif
13 changes: 8 additions & 5 deletions core/embed/bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
#include "flash_otp.h"
#include "flash_utils.h"
#include "image.h"
#include "lowlevel.h"
#include "messages.pb.h"
#include "option_bytes.h"
#include "pvd.h"
#include "random_delays.h"
#include "rsod.h"
#include "secbool.h"
Expand Down Expand Up @@ -79,11 +80,11 @@
#include "version_check.h"

#ifdef TREZOR_EMULATOR
#include "SDL.h"
#include "emulator.h"
#else
#include "compiler_traits.h"
#include "mpu.h"
#include "platform.h"
#endif

#define USB_IFACE_NUM 0
Expand Down Expand Up @@ -149,7 +150,9 @@ static usb_result_t bootloader_usb_loop(const vendor_header *const vhdr,

for (;;) {
#ifdef TREZOR_EMULATOR
emulator_poll_events();
// Ensures that SDL events are processed. This prevents the emulator from
// freezing when the user interacts with the window.
SDL_PumpEvents();
#endif
int r = usb_webusb_read_blocking(USB_IFACE_NUM, buf, USB_PACKET_SIZE,
USB_TIMEOUT);
Expand Down Expand Up @@ -361,8 +364,8 @@ int bootloader_main(void) {

rdi_init();

#if defined TREZOR_MODEL_T
set_core_clock(CLOCK_180_MHZ);
#ifdef USE_PVD
pvd_init();
#endif

#ifdef USE_HASH_PROCESSOR
Expand Down
9 changes: 8 additions & 1 deletion core/embed/extmod/modtrezorio/modtrezorio-poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#include "display.h"
#include "embed/extmod/trezorobj.h"

#ifdef TREZOR_EMULATOR
#include "SDL.h"
#endif

#define USB_DATA_IFACE (253)
#define BUTTON_IFACE (254)
#define TOUCH_IFACE (255)
Expand Down Expand Up @@ -78,7 +82,10 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref,
const mp_uint_t mode = i & 0xFF00;

#if defined TREZOR_EMULATOR
emulator_poll_events();
// Ensures that SDL events are processed even if the ifaces list
// contains only USB interfaces. This prevents the emulator from
// freezing when the user interacts with the window.
SDL_PumpEvents();
#endif

if (false) {
Expand Down
10 changes: 6 additions & 4 deletions core/embed/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include "mpu.h"
#include "optiga_commands.h"
#include "optiga_transport.h"
#include "option_bytes.h"
#include "pvd.h"
#include "random_delays.h"
#include "rsod.h"
#include "sdcard.h"
Expand Down Expand Up @@ -69,14 +71,14 @@ static void optiga_log_hex(const char *prefix, const uint8_t *data,
#endif

void drivers_init() {
#if defined TREZOR_MODEL_T
set_core_clock(CLOCK_180_MHZ);
#endif

#ifdef STM32U5
tamper_init();
#endif

#ifdef USE_PVD
pvd_init();
#endif

rdi_init();

#ifdef RDI
Expand Down
1 change: 1 addition & 0 deletions core/embed/models/D001/boards/stm32f429i-disc1.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define USE_TOUCH 1
#define USE_SDRAM 1
#define USE_RGB_COLORS 1
#define USE_PVD 1

#define I2C_COUNT 1
#define I2C_INSTANCE_0 I2C3
Expand Down
1 change: 1 addition & 0 deletions core/embed/models/T2B1/boards/trezor_r_v10.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define USE_SBU 1
#define USE_I2C 1
#define USE_CONSUMPTION_MASK 1
#define USE_PVD

#define DISPLAY_RESX 128
#define DISPLAY_RESY 64
Expand Down
1 change: 1 addition & 0 deletions core/embed/models/T2B1/boards/trezor_r_v3.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#define USE_BUTTON 1
#define USE_SBU 1
#define USE_PVD 1

#define DISPLAY_RESX 128
#define DISPLAY_RESY 128
Expand Down
1 change: 1 addition & 0 deletions core/embed/models/T2B1/boards/trezor_r_v4.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#define USE_BUTTON 1
#define USE_SBU 1
#define USE_PVD 1

#define DISPLAY_RESX 128
#define DISPLAY_RESY 64
Expand Down
1 change: 1 addition & 0 deletions core/embed/models/T2B1/boards/trezor_r_v6.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#define USE_BUTTON 1
#define USE_SBU 1
#define USE_PVD 1

#define DISPLAY_RESX 128
#define DISPLAY_RESY 64
Expand Down
1 change: 1 addition & 0 deletions core/embed/models/T2T1/boards/trezor_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define USE_RGB_COLORS 1
#define USE_BACKLIGHT 1
#define USE_DISP_I8080_8BIT_DW 1
#define USE_PVD 1

#define DISPLAY_RESX 240
#define DISPLAY_RESY 240
Expand Down
26 changes: 24 additions & 2 deletions core/embed/models/T2T1/compat_settings.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "platform.h"
#include "startup_init.h"

void ensure_compatible_settings(void) { set_core_clock(CLOCK_168_MHZ); }
void ensure_compatible_settings(void) {
// Early version of bootloader on T2T1 expects 168 MHz core clock.
// So we need to set it here before handover to the bootloader.
set_core_clock(CLOCK_168_MHZ);
}
1 change: 1 addition & 0 deletions core/embed/models/T3B1/boards/trezor_t3b1_revB.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define USE_SBU 1
#define USE_HASH_PROCESSOR 1
#define USE_CONSUMPTION_MASK 1
#define USE_PVD 1

#define DISPLAY_LEGACY_HEADER "displays/vg-2864ksweg01.h"

Expand Down
1 change: 1 addition & 0 deletions core/embed/models/T3T1/boards/trezor_t3t1_revE.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define USE_HAPTIC 1
#define USE_BACKLIGHT 1
#define USE_HASH_PROCESSOR 1
#define USE_PVD 1

#define DISPLAY_RESX 240
#define DISPLAY_RESY 240
Expand Down
1 change: 1 addition & 0 deletions core/embed/models/T3T1/boards/trezor_t3t1_v4.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define USE_HAPTIC 1
#define USE_BACKLIGHT 1
#define USE_HASH_PROCESSOR 1
#define USE_PVD 1

#define DISPLAY_RESX 240
#define DISPLAY_RESY 240
Expand Down
14 changes: 14 additions & 0 deletions core/embed/trezorhal/bootutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,18 @@ void __attribute__((noreturn)) reboot_and_upgrade(const uint8_t hash[32]);
// unrecoverable error state.
void __attribute__((noreturn)) secure_shutdown(void);

// Alternative memset with slightly different arguments
//
// This function writes a 32-bit value to a range of memory addresses.
// The range is defined by the start and stop pointers and must
// be aligned to 4 bytes.
void memset_reg(volatile void *start, volatile void *stop, uint32_t val);

// Jumps to the next booting stage (e.g. bootloader to firmware).
// `address` points to the flash at the vector table of the next stage.
//
// Before jumping, the function disables all interrupts and clears the
// memory and registers that could contain sensitive information.
void jump_to(uint32_t address);

#endif // TREZORHAL_BOOTUTILS_H
1 change: 0 additions & 1 deletion core/embed/trezorhal/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "secbool.h"

#include "error_handling.h"
#include "platform.h"
#include "systick.h"

#ifndef MIN_8bits
Expand Down
11 changes: 10 additions & 1 deletion core/embed/trezorhal/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@
#include <stdlib.h>

#include "flash_ll.h"
#include "platform.h"
#include "secbool.h"

#ifdef STM32U5

#define FLASH_QUADWORD_WORDS (4)
#define FLASH_QUADWORD_SIZE (FLASH_QUADWORD_WORDS * sizeof(uint32_t))

#define FLASH_BURST_WORDS (8 * FLASH_QUADWORD_WORDS)
#define FLASH_BURST_SIZE (FLASH_BURST_WORDS * sizeof(uint32_t))

#endif

void flash_init(void);

#endif // TREZORHAL_FLASH_H
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __TREZORHAL_LOWLEVEL_H__
#define __TREZORHAL_LOWLEVEL_H__
#ifndef TREZORHAL_OPTION_BYTES_H
#define TREZORHAL_OPTION_BYTES_H

#include "secbool.h"

Expand All @@ -29,10 +29,9 @@ void flash_lock_option_bytes(void);
void flash_unlock_option_bytes(void);
uint32_t flash_set_option_bytes(void);
secbool flash_configure_option_bytes(void);
void periph_init(void);
secbool reset_flags_check(void);
void reset_flags_reset(void);

void check_oem_keys(void);

#endif // KERNEL_MODE

#endif // __TREZORHAL_LOWLEVEL_H__
#endif // TREZORHAL_OPTION_BYTES_H
Loading
Loading