2017-10-25 16:22:00 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2017-10-25 16:22:00 +00:00
|
|
|
*
|
2018-02-26 13:06:10 +00:00
|
|
|
* 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/>.
|
2017-10-25 16:22:00 +00:00
|
|
|
*/
|
|
|
|
|
2024-11-01 11:55:31 +00:00
|
|
|
#include <trezor_model.h>
|
|
|
|
#include <trezor_rtl.h>
|
|
|
|
|
2024-11-15 09:07:01 +00:00
|
|
|
#include <gfx/fonts.h>
|
|
|
|
#include <gfx/gfx_draw.h>
|
|
|
|
#include <io/display.h>
|
|
|
|
#include <io/display_utils.h>
|
|
|
|
#include <io/usb.h>
|
2025-01-30 14:03:46 +00:00
|
|
|
#include <rtl/cli.h>
|
2024-11-15 09:07:01 +00:00
|
|
|
#include <sys/system.h>
|
|
|
|
#include <sys/systick.h>
|
|
|
|
#include <util/flash.h>
|
|
|
|
#include <util/flash_otp.h>
|
|
|
|
#include <util/rsod.h>
|
2017-10-25 16:22:00 +00:00
|
|
|
|
2024-11-15 09:07:01 +00:00
|
|
|
#ifdef USE_BUTTON
|
|
|
|
#include <io/button.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_SBU
|
|
|
|
#include <io/sbu.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_SD_CARD
|
|
|
|
#include <io/sdcard.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_TOUCH
|
|
|
|
#include <io/touch.h>
|
|
|
|
#endif
|
|
|
|
|
2023-07-20 11:20:50 +00:00
|
|
|
#ifdef USE_OPTIGA
|
2024-11-15 09:07:01 +00:00
|
|
|
#include <sec/optiga_commands.h>
|
|
|
|
#include <sec/optiga_transport.h>
|
2025-01-30 14:03:46 +00:00
|
|
|
#include "cmd/prodtest_optiga.h"
|
2023-07-20 11:20:50 +00:00
|
|
|
#endif
|
|
|
|
|
2024-01-10 13:56:41 +00:00
|
|
|
#ifdef USE_HAPTIC
|
2024-11-15 09:07:01 +00:00
|
|
|
#include <io/haptic.h>
|
2024-01-10 13:56:41 +00:00
|
|
|
#endif
|
|
|
|
|
2025-01-06 12:24:59 +00:00
|
|
|
#ifdef USE_RGB_LED
|
|
|
|
#include <io/rgb_led.h>
|
|
|
|
#endif
|
|
|
|
|
2023-12-15 22:50:33 +00:00
|
|
|
#ifdef USE_HASH_PROCESSOR
|
2024-11-15 09:07:01 +00:00
|
|
|
#include <sec/hash_processor.h>
|
2023-12-15 22:50:33 +00:00
|
|
|
#endif
|
|
|
|
|
2024-12-10 08:53:59 +00:00
|
|
|
#ifdef USE_POWERCTL
|
2024-12-10 08:53:59 +00:00
|
|
|
#include <sys/powerctl.h>
|
2024-12-10 08:53:59 +00:00
|
|
|
#endif
|
|
|
|
|
2024-10-31 16:05:44 +00:00
|
|
|
#ifdef USE_STORAGE_HWKEY
|
2024-11-15 09:07:01 +00:00
|
|
|
#include <sec/secure_aes.h>
|
2024-10-31 16:05:44 +00:00
|
|
|
#endif
|
|
|
|
|
2025-01-09 22:40:59 +00:00
|
|
|
#ifdef TREZOR_MODEL_T2T1
|
2023-06-07 13:28:04 +00:00
|
|
|
#define MODEL_IDENTIFIER "TREZOR2-"
|
2023-08-24 09:33:50 +00:00
|
|
|
#else
|
|
|
|
#define MODEL_IDENTIFIER MODEL_INTERNAL_NAME "-"
|
2023-06-07 13:28:04 +00:00
|
|
|
#endif
|
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
// Command line interface context
|
|
|
|
cli_t g_cli = {0};
|
|
|
|
|
2024-11-13 10:04:53 +00:00
|
|
|
static gfx_text_attr_t bold = {
|
|
|
|
.font = FONT_BOLD,
|
|
|
|
.fg_color = COLOR_WHITE,
|
|
|
|
.bg_color = COLOR_BLACK,
|
|
|
|
};
|
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
#define VCP_IFACE 0
|
2023-06-07 13:28:04 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
static size_t console_read(void *context, char *buf, size_t size) {
|
|
|
|
return usb_vcp_read_blocking(VCP_IFACE, (uint8_t *)buf, size, -1);
|
2017-10-25 16:22:00 +00:00
|
|
|
}
|
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
static size_t console_write(void *context, const char *buf, size_t size) {
|
|
|
|
return usb_vcp_write_blocking(VCP_IFACE, (const uint8_t *)buf, size, -1);
|
2017-10-25 16:22:00 +00:00
|
|
|
}
|
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
static void vcp_intr(void) { cli_abort(&g_cli); }
|
2017-10-25 16:22:00 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
static void usb_init_all(void) {
|
|
|
|
enum {
|
|
|
|
VCP_PACKET_LEN = 64,
|
|
|
|
VCP_BUFFER_LEN = 1024,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const usb_dev_info_t dev_info = {
|
|
|
|
.device_class = 0xEF, // Composite Device Class
|
|
|
|
.device_subclass = 0x02, // Common Class
|
|
|
|
.device_protocol = 0x01, // Interface Association Descriptor
|
|
|
|
.vendor_id = 0x1209,
|
|
|
|
.product_id = 0x53C1,
|
|
|
|
.release_num = 0x0400,
|
2024-05-03 12:57:55 +00:00
|
|
|
.manufacturer = MODEL_USB_MANUFACTURER,
|
|
|
|
.product = MODEL_USB_PRODUCT,
|
2019-03-29 15:26:02 +00:00
|
|
|
.serial_number = "000000000000",
|
|
|
|
.interface = "TREZOR Interface",
|
|
|
|
.usb21_enabled = secfalse,
|
|
|
|
.usb21_landing = secfalse,
|
|
|
|
};
|
|
|
|
|
|
|
|
static uint8_t tx_packet[VCP_PACKET_LEN];
|
|
|
|
static uint8_t tx_buffer[VCP_BUFFER_LEN];
|
|
|
|
static uint8_t rx_packet[VCP_PACKET_LEN];
|
|
|
|
static uint8_t rx_buffer[VCP_BUFFER_LEN];
|
|
|
|
|
|
|
|
static const usb_vcp_info_t vcp_info = {
|
|
|
|
.tx_packet = tx_packet,
|
|
|
|
.tx_buffer = tx_buffer,
|
|
|
|
.rx_packet = rx_packet,
|
|
|
|
.rx_buffer = rx_buffer,
|
|
|
|
.tx_buffer_len = VCP_BUFFER_LEN,
|
|
|
|
.rx_buffer_len = VCP_BUFFER_LEN,
|
|
|
|
.rx_intr_fn = vcp_intr,
|
|
|
|
.rx_intr_byte = 3, // Ctrl-C
|
|
|
|
.iface_num = VCP_IFACE,
|
|
|
|
.data_iface_num = 0x01,
|
2024-05-29 08:22:54 +00:00
|
|
|
.ep_cmd = 0x02,
|
|
|
|
.ep_in = 0x01,
|
2019-03-29 15:26:02 +00:00
|
|
|
.ep_out = 0x01,
|
|
|
|
.polling_interval = 10,
|
|
|
|
.max_packet_len = VCP_PACKET_LEN,
|
|
|
|
};
|
|
|
|
|
2024-05-31 11:25:47 +00:00
|
|
|
ensure(usb_init(&dev_info), NULL);
|
2019-03-29 15:26:02 +00:00
|
|
|
ensure(usb_vcp_add(&vcp_info), "usb_vcp_add");
|
2024-05-31 11:25:47 +00:00
|
|
|
ensure(usb_start(), NULL);
|
2017-10-25 16:22:00 +00:00
|
|
|
}
|
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
static inline gfx_rect_t gfx_rect_shrink(gfx_rect_t r, int padding) {
|
|
|
|
gfx_rect_t result = {
|
|
|
|
.x0 = r.x0 + padding,
|
|
|
|
.y0 = r.y0 + padding,
|
|
|
|
.x1 = r.x1 - padding,
|
|
|
|
.y1 = r.y1 - padding,
|
|
|
|
};
|
|
|
|
return result;
|
2018-01-09 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 15:32:28 +00:00
|
|
|
static void draw_welcome_screen(void) {
|
2024-11-13 10:04:53 +00:00
|
|
|
gfx_clear();
|
2025-01-30 14:03:46 +00:00
|
|
|
gfx_rect_t r = gfx_rect_wh(0, 0, DISPLAY_RESX, DISPLAY_RESY);
|
2019-03-29 15:26:02 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
#if defined TREZOR_MODEL_T2B1 || defined TREZOR_MODEL_T3B1
|
|
|
|
gfx_draw_bar(r, COLOR_WHITE);
|
2024-04-03 14:54:26 +00:00
|
|
|
#else
|
2025-01-30 14:03:46 +00:00
|
|
|
gfx_draw_bar(gfx_rect_shrink(r, 3), COLOR_WHITE);
|
|
|
|
gfx_draw_bar(gfx_rect_shrink(r, 4), COLOR_BLACK);
|
2023-06-07 13:28:04 +00:00
|
|
|
#endif
|
2017-10-25 16:22:00 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
char dom[32];
|
|
|
|
// format: {MODEL_IDENTIFIER}YYMMDD
|
|
|
|
if (sectrue == flash_otp_read(FLASH_OTP_BLOCK_BATCH, 0, (uint8_t *)dom, 32) &&
|
|
|
|
dom[31] == 0 && cstr_starts_with(dom, MODEL_IDENTIFIER)) {
|
|
|
|
gfx_offset_t pos;
|
2024-09-07 11:50:10 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
pos = gfx_offset(DISPLAY_RESX / 2, DISPLAY_RESY / 2);
|
|
|
|
gfx_draw_qrcode(pos, 4, dom);
|
2024-07-29 12:12:44 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
pos = gfx_offset(DISPLAY_RESX / 2, DISPLAY_RESY - 30);
|
|
|
|
gfx_draw_text(pos, dom + sizeof(MODEL_IDENTIFIER) - 1, -1, &bold,
|
|
|
|
GFX_ALIGN_CENTER);
|
2024-07-29 12:12:44 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
display_refresh();
|
2024-12-10 08:53:59 +00:00
|
|
|
}
|
2017-10-27 04:01:22 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
static void drivers_init(void) {
|
|
|
|
display_init(DISPLAY_RESET_CONTENT);
|
2024-07-12 09:35:49 +00:00
|
|
|
|
2024-10-31 16:05:44 +00:00
|
|
|
#ifdef USE_STORAGE_HWKEY
|
2024-03-06 14:53:50 +00:00
|
|
|
secure_aes_init();
|
|
|
|
#endif
|
2023-12-15 22:50:33 +00:00
|
|
|
#ifdef USE_HASH_PROCESSOR
|
|
|
|
hash_processor_init();
|
|
|
|
#endif
|
2023-06-07 13:28:04 +00:00
|
|
|
#ifdef USE_SD_CARD
|
2019-03-29 15:26:02 +00:00
|
|
|
sdcard_init();
|
2023-06-07 13:28:04 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_BUTTON
|
|
|
|
button_init();
|
|
|
|
#endif
|
2023-07-20 11:20:50 +00:00
|
|
|
#ifdef USE_TOUCH
|
2019-03-29 15:26:02 +00:00
|
|
|
touch_init();
|
2023-06-07 13:28:04 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_SBU
|
2019-03-29 15:26:02 +00:00
|
|
|
sbu_init();
|
2024-01-10 13:56:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_HAPTIC
|
|
|
|
haptic_init();
|
2023-06-07 13:28:04 +00:00
|
|
|
#endif
|
2025-01-06 12:24:59 +00:00
|
|
|
#ifdef USE_RGB_LED
|
|
|
|
rgb_led_init();
|
|
|
|
#endif
|
2025-01-30 14:03:46 +00:00
|
|
|
}
|
2025-01-06 12:24:59 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
#define BACKLIGHT_NORMAL 150
|
2019-03-29 15:26:02 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
int main(void) {
|
|
|
|
system_init(&rsod_panic_handler);
|
2024-07-29 12:12:44 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
drivers_init();
|
|
|
|
usb_init_all();
|
2023-07-20 11:20:50 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
// Draw welcome screen
|
2023-10-04 15:32:28 +00:00
|
|
|
draw_welcome_screen();
|
2019-03-29 15:26:02 +00:00
|
|
|
display_fade(0, BACKLIGHT_NORMAL, 1000);
|
2017-10-24 16:16:36 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
// Initialize command line interface
|
|
|
|
cli_init(&g_cli, console_read, console_write, NULL);
|
2024-07-29 11:13:35 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
extern cli_command_t _prodtest_cli_cmd_section_start;
|
|
|
|
extern cli_command_t _prodtest_cli_cmd_section_end;
|
2024-10-22 10:47:05 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
cli_set_commands(
|
|
|
|
&g_cli, &_prodtest_cli_cmd_section_start,
|
|
|
|
&_prodtest_cli_cmd_section_end - &_prodtest_cli_cmd_section_start);
|
2024-04-10 09:55:15 +00:00
|
|
|
|
2023-07-20 11:20:50 +00:00
|
|
|
#ifdef USE_OPTIGA
|
2025-01-30 14:03:46 +00:00
|
|
|
optiga_init();
|
|
|
|
optiga_open_application();
|
|
|
|
pair_optiga(&g_cli);
|
2023-06-07 13:28:04 +00:00
|
|
|
#endif
|
2017-10-25 16:22:00 +00:00
|
|
|
|
2025-01-30 14:03:46 +00:00
|
|
|
cli_run_loop(&g_cli);
|
2017-10-24 16:16:36 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
return 0;
|
2017-10-24 16:16:36 +00:00
|
|
|
}
|