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

bootloader refactoring #4572

Open
wants to merge 5 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
22 changes: 20 additions & 2 deletions core/SConscript.bootloader
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,29 @@ SOURCE_BOOTLOADER = [
'embed/projects/bootloader/header.S',
'embed/projects/bootloader/bootui.c',
'embed/projects/bootloader/main.c',
'embed/projects/bootloader/messages.c',
'embed/projects/bootloader/protob/messages.pb.c',
'embed/projects/bootloader/poll.c',
'embed/projects/bootloader/workflow/workflow.c',
'embed/projects/bootloader/workflow/wf_firmware_update.c',
'embed/projects/bootloader/workflow/wf_wipe_device.c',
'embed/projects/bootloader/workflow/wf_get_features.c',
'embed/projects/bootloader/workflow/wf_initialize.c',
'embed/projects/bootloader/workflow/wf_ping.c',
'embed/projects/bootloader/workflow/wf_bootloader.c',
'embed/projects/bootloader/workflow/wf_empty_device.c',
'embed/projects/bootloader/workflow/wf_auto_update.c',
'embed/projects/bootloader/workflow/wf_host_control.c',
'embed/projects/bootloader/wire/codec_v1.c',
'embed/projects/bootloader/wire/wire_iface_usb.c',
'embed/projects/bootloader/protob/protob.c',
'embed/projects/bootloader/protob/pb/messages.pb.c',
'embed/projects/bootloader/version_check.c',
]

if 'optiga' in FEATURES_AVAILABLE:
SOURCE_BOOTLOADER += [
'embed/projects/bootloader/workflow/wf_unlock_bootloader.c',
]


env.Replace(
CAT='cat',
Expand Down
2 changes: 1 addition & 1 deletion core/SConscript.bootloader_ci
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ SOURCE_BOOTLOADER = [
'embed/projects/bootloader_ci/bootui.c',
'embed/projects/bootloader_ci/main.c',
'embed/projects/bootloader_ci/messages.c',
'embed/projects/bootloader_ci/protob/messages.pb.c',
'embed/projects/bootloader_ci/protob/pb/messages.pb.c',
'embed/projects/bootloader_ci/version_check.c',
]

Expand Down
24 changes: 21 additions & 3 deletions core/SConscript.bootloader_emu
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,23 @@ SOURCE_NANOPB = [
SOURCE_BOOTLOADER = [
'embed/projects/bootloader/bootui.c',
'embed/projects/bootloader/main.c',
'embed/projects/bootloader/messages.c',
'embed/projects/bootloader/emulator.c',
'embed/projects/bootloader/poll.c',
'embed/projects/bootloader/workflow/workflow.c',
'embed/projects/bootloader/workflow/wf_firmware_update.c',
'embed/projects/bootloader/workflow/wf_wipe_device.c',
'embed/projects/bootloader/workflow/wf_get_features.c',
'embed/projects/bootloader/workflow/wf_initialize.c',
'embed/projects/bootloader/workflow/wf_ping.c',
'embed/projects/bootloader/workflow/wf_bootloader.c',
'embed/projects/bootloader/workflow/wf_empty_device.c',
'embed/projects/bootloader/workflow/wf_auto_update.c',
'embed/projects/bootloader/workflow/wf_host_control.c',
'embed/projects/bootloader/wire/codec_v1.c',
'embed/projects/bootloader/wire/wire_iface_usb.c',
'embed/projects/bootloader/protob/protob.c',
'embed/projects/bootloader/protob/pb/messages.pb.c',
'embed/projects/bootloader/version_check.c',
'embed/projects/bootloader/protob/messages.pb.c',
'embed/projects/bootloader/emulator.c',
]

SOURCE_TREZORHAL = [
Expand Down Expand Up @@ -138,6 +151,11 @@ env = Environment(

FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_UNIX, PATH_HAL)

if 'optiga' in FEATURES_AVAILABLE:
SOURCE_BOOTLOADER += [
'embed/projects/bootloader/workflow/wf_unlock_bootloader.c',
]

env.Replace(
CP='cp',
AS='as',
Expand Down
6 changes: 5 additions & 1 deletion core/embed/io/usb/unix/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ secbool usb_init(const usb_dev_info_t *dev_info) {
return sectrue;
}

void usb_deinit(void) {}
void usb_deinit(void) { usb_stop(); }

secbool usb_start(void) {
const char *ip = getenv("TREZOR_UDP_IP");
Expand Down Expand Up @@ -141,6 +141,10 @@ secbool usb_vcp_add(const usb_vcp_info_t *info) {
}

static secbool usb_emulated_poll_read(uint8_t iface_num) {
if (usb_ifaces[iface_num].msg_len > 0) {
return sectrue;
}

struct pollfd fds[] = {
{usb_ifaces[iface_num].sock, POLLIN, 0},
};
Expand Down
1 change: 1 addition & 0 deletions core/embed/projects/bootloader/.changelog.d/4572.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Major code clean-up and refactoring.
39 changes: 9 additions & 30 deletions core/embed/projects/bootloader/bootui.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,16 @@

#include <trezor_rtl.h>

#include <gfx/fonts.h>
#include <io/display.h>
#include <io/display_utils.h>
#include <rtl/mini_printf.h>

#include "bootui.h"
#include "rust_ui.h"
#include "version.h"

#define BACKLIGHT_NORMAL 150

#define COLOR_BL_BG COLOR_WHITE // background
#define COLOR_BL_FG COLOR_BLACK // foreground

#ifdef RGB16
#define COLOR_BL_FAIL RGB16(0xFF, 0x00, 0x00) // red
#define COLOR_BL_DONE RGB16(0x00, 0xAE, 0x0B) // green
#define COLOR_BL_PROCESS RGB16(0x4A, 0x90, 0xE2) // blue
#define COLOR_BL_GRAY RGB16(0x99, 0x99, 0x99) // gray
#else
#define COLOR_BL_FAIL COLOR_BL_FG
#define COLOR_BL_DONE COLOR_BL_FG
#define COLOR_BL_PROCESS COLOR_BL_FG
#define COLOR_BL_GRAY COLOR_BL_FG
#endif

#if !defined TREZOR_MODEL_T2B1 && !defined TREZOR_MODEL_T3B1
#define BOOT_WAIT_HEIGHT 25
#define BOOT_WAIT_Y_TOP (DISPLAY_RESY - BOOT_WAIT_HEIGHT)
#else
#define BOOT_WAIT_HEIGHT 12
#define BOOT_WAIT_Y_TOP (DISPLAY_RESY - BOOT_WAIT_HEIGHT)
#endif

#define TOIF_LENGTH(ptr) ((*(uint32_t *)((ptr) + 8)) + 12)

// common shared functions
Expand Down Expand Up @@ -140,13 +117,15 @@ uint32_t ui_screen_menu(secbool firmware_present) {
return screen_menu(firmware_present);
}

void ui_screen_connect(void) { screen_connect(initial_setup); }

// install UI

uint32_t ui_screen_install_confirm(const vendor_header *const vhdr,
const image_header *const hdr,
secbool should_keep_seed,
secbool is_newvendor, secbool is_newinstall,
int version_cmp) {
ui_result_t ui_screen_install_confirm(const vendor_header *const vhdr,
const image_header *const hdr,
secbool should_keep_seed,
secbool is_newvendor,
secbool is_newinstall, int version_cmp) {
uint8_t fingerprint[32];
char ver_str[64];
get_image_fingerprint(hdr, fingerprint);
Expand All @@ -172,7 +151,7 @@ void ui_screen_install_progress_upload(int pos) {

// wipe UI

uint32_t ui_screen_wipe_confirm(void) { return screen_wipe_confirm(); }
ui_result_t ui_screen_wipe_confirm(void) { return screen_wipe_confirm(); }

void ui_screen_wipe(void) { screen_wipe_progress(0, true); }

Expand Down
31 changes: 14 additions & 17 deletions core/embed/projects/bootloader/bootui.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __BOOTUI_H__
#define __BOOTUI_H__
#pragma once

#include <trezor_types.h>

#include <util/image.h>

typedef enum {
UI_RESULT_CANCEL = 1,
UI_RESULT_CONFIRM = 2,
} ui_result_t;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big issue, but the UI_RESULT_xxx constants are somewhat disconnected from the Rust code and may diverge.


typedef enum {
SCREEN_INTRO = 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This enum describes the states of the workflow in wf_bootloader.c and might belong in this file. Some of the enum entries are unused.

SCREEN_MENU = 1,
Expand Down Expand Up @@ -58,16 +62,18 @@ uint32_t ui_screen_intro(const vendor_header* const vhdr,

uint32_t ui_screen_menu(secbool firmware_present);

uint32_t ui_screen_install_confirm(const vendor_header* const vhdr,
const image_header* const hdr,
secbool shold_keep_seed,
secbool is_newvendor, secbool is_newinstall,
int version_cmp);
void ui_screen_connect(void);

ui_result_t ui_screen_install_confirm(const vendor_header* const vhdr,
const image_header* const hdr,
secbool shold_keep_seed,
secbool is_newvendor,
secbool is_newinstall, int version_cmp);
void ui_screen_install_start();
void ui_screen_install_progress_erase(int pos, int len);
void ui_screen_install_progress_upload(int pos);

uint32_t ui_screen_wipe_confirm(void);
ui_result_t ui_screen_wipe_confirm(void);
void ui_screen_wipe(void);
void ui_screen_wipe_progress(int pos, int len);

Expand All @@ -84,12 +90,3 @@ void ui_screen_boot_stage_1(bool fading);
#ifdef USE_OPTIGA
uint32_t ui_screen_unlock_bootloader_confirm(void);
#endif

// clang-format off
#define INPUT_CANCEL 0x01 // Cancel button
#define INPUT_CONFIRM 0x02 // Confirm button
#define INPUT_LONG_CONFIRM 0x04 // Long Confirm button
#define INPUT_INFO 0x08 // Info icon
// clang-format on

#endif
10 changes: 5 additions & 5 deletions core/embed/projects/bootloader/emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
#include <io/display.h>
#include <sys/bootargs.h>
#include <sys/bootutils.h>
#include <sys/systick.h>
#include <util/flash.h>
#include <util/flash_otp.h>
#include "bootui.h"
#include "rust_ui.h"

#ifdef USE_OPTIGA
#include <sec/secret.h>
Expand Down Expand Up @@ -188,10 +186,12 @@ int main(int argc, char **argv) {
(void)!flash_otp_write(FLASH_OTP_BLOCK_DEVICE_VARIANT, 0, otp_data,
sizeof(otp_data));

bootloader_main();
hal_delay(3000);
jump_to_next_stage(0);
int exit_code = bootloader_main();

char msg[64];
snprintf(msg, sizeof(msg), "Exit code: %d", exit_code);

error_shutdown_ex("BOOTLOADER ERROR", msg, "UNEXPECTED EXIT");
return 0;
}

Expand Down
24 changes: 20 additions & 4 deletions core/embed/projects/bootloader/emulator.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
#ifndef __EMULATOR_H__
#define __EMULATOR_H__
/*
* 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/>.
*/

#pragma once

#include <trezor_types.h>

#undef FIRMWARE_START

extern uint8_t *FIRMWARE_START;

#endif
Loading
Loading