2014-04-29 12:26:51 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2014-04-29 12:26:51 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
*
|
|
|
|
* This library is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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 Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "trezor.h"
|
|
|
|
#include "bitmaps.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "bl_check.h"
|
|
|
|
#include "buttons.h"
|
|
|
|
#include "common.h"
|
2019-01-25 10:58:23 +00:00
|
|
|
#include "config.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "gettext.h"
|
2014-07-30 18:34:28 +00:00
|
|
|
#include "layout.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
#include "layout2.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "memzero.h"
|
|
|
|
#include "oled.h"
|
2014-07-30 18:34:28 +00:00
|
|
|
#include "rng.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "setup.h"
|
2016-11-17 19:07:41 +00:00
|
|
|
#include "timer.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "usb.h"
|
|
|
|
#include "util.h"
|
2019-02-17 23:53:56 +00:00
|
|
|
#if !EMULATOR
|
2019-02-07 23:46:16 +00:00
|
|
|
#include <libopencm3/stm32/desig.h>
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "otp.h"
|
2019-02-17 23:53:56 +00:00
|
|
|
#endif
|
2014-07-30 18:34:28 +00:00
|
|
|
|
2017-12-13 18:38:51 +00:00
|
|
|
/* Screen timeout */
|
2019-09-30 15:34:38 +00:00
|
|
|
uint32_t system_millis_lock_start = 0;
|
2017-12-13 18:38:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
void check_lock_screen(void) {
|
|
|
|
buttonUpdate();
|
|
|
|
|
|
|
|
// wake from screensaver on any button
|
|
|
|
if (layoutLast == layoutScreensaver && (button.NoUp || button.YesUp)) {
|
|
|
|
layoutHome();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-04 18:55:41 +00:00
|
|
|
// button held for long enough (5 seconds)
|
|
|
|
if (layoutLast == layoutHome && button.NoDown >= 114000 * 5) {
|
2019-03-29 16:10:31 +00:00
|
|
|
layoutDialog(&bmp_icon_question, _("Cancel"), _("Lock Device"), NULL,
|
2019-06-17 18:27:55 +00:00
|
|
|
_("Do you really want to"), _("lock your Trezor?"), NULL, NULL,
|
2019-03-29 16:10:31 +00:00
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
// wait until NoButton is released
|
|
|
|
usbTiny(1);
|
|
|
|
do {
|
|
|
|
usbSleep(5);
|
|
|
|
buttonUpdate();
|
|
|
|
} while (!button.NoUp);
|
|
|
|
|
|
|
|
// wait for confirmation/cancellation of the dialog
|
|
|
|
do {
|
|
|
|
usbSleep(5);
|
|
|
|
buttonUpdate();
|
|
|
|
} while (!button.YesUp && !button.NoUp);
|
|
|
|
usbTiny(0);
|
|
|
|
|
|
|
|
if (button.YesUp) {
|
|
|
|
// lock the screen
|
2020-02-13 09:39:24 +00:00
|
|
|
config_lockDevice();
|
2019-03-29 16:10:31 +00:00
|
|
|
layoutScreensaver();
|
|
|
|
} else {
|
|
|
|
// resume homescreen
|
|
|
|
layoutHome();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if homescreen is shown for too long
|
|
|
|
if (layoutLast == layoutHome) {
|
|
|
|
if ((timer_ms() - system_millis_lock_start) >=
|
|
|
|
config_getAutoLockDelayMs()) {
|
|
|
|
// lock the screen
|
2020-02-13 09:39:24 +00:00
|
|
|
config_lockDevice();
|
2019-03-29 16:10:31 +00:00
|
|
|
layoutScreensaver();
|
|
|
|
}
|
|
|
|
}
|
2016-08-26 16:50:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
static void collect_hw_entropy(bool privileged) {
|
2019-02-18 16:57:09 +00:00
|
|
|
#if EMULATOR
|
2019-03-29 16:10:31 +00:00
|
|
|
(void)privileged;
|
|
|
|
memzero(HW_ENTROPY_DATA, HW_ENTROPY_LEN);
|
2019-02-18 16:57:09 +00:00
|
|
|
#else
|
2019-03-29 16:10:31 +00:00
|
|
|
if (privileged) {
|
|
|
|
desig_get_unique_id((uint32_t *)HW_ENTROPY_DATA);
|
|
|
|
// set entropy in the OTP randomness block
|
|
|
|
if (!flash_otp_is_locked(FLASH_OTP_BLOCK_RANDOMNESS)) {
|
2019-09-30 15:34:38 +00:00
|
|
|
uint8_t entropy[FLASH_OTP_BLOCK_SIZE] = {0};
|
2019-03-29 16:10:31 +00:00
|
|
|
random_buffer(entropy, FLASH_OTP_BLOCK_SIZE);
|
|
|
|
flash_otp_write(FLASH_OTP_BLOCK_RANDOMNESS, 0, entropy,
|
|
|
|
FLASH_OTP_BLOCK_SIZE);
|
|
|
|
flash_otp_lock(FLASH_OTP_BLOCK_RANDOMNESS);
|
|
|
|
}
|
|
|
|
// collect entropy from OTP randomness block
|
|
|
|
flash_otp_read(FLASH_OTP_BLOCK_RANDOMNESS, 0, HW_ENTROPY_DATA + 12,
|
|
|
|
FLASH_OTP_BLOCK_SIZE);
|
|
|
|
} else {
|
|
|
|
// unprivileged mode => use fixed HW_ENTROPY
|
|
|
|
memset(HW_ENTROPY_DATA, 0x3C, HW_ENTROPY_LEN);
|
|
|
|
}
|
2019-02-18 16:57:09 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
int main(void) {
|
2014-07-07 13:03:34 +00:00
|
|
|
#ifndef APPVER
|
2019-03-29 16:10:31 +00:00
|
|
|
setup();
|
|
|
|
__stack_chk_guard = random32(); // this supports compiler provided
|
|
|
|
// unpredictable stack protection checks
|
|
|
|
oledInit();
|
2014-07-07 13:03:34 +00:00
|
|
|
#else
|
2020-08-21 12:58:13 +00:00
|
|
|
check_bootloader(true);
|
2019-03-29 16:10:31 +00:00
|
|
|
setupApp();
|
|
|
|
__stack_chk_guard = random32(); // this supports compiler provided
|
|
|
|
// unpredictable stack protection checks
|
2014-07-07 13:03:34 +00:00
|
|
|
#endif
|
2019-06-09 21:32:19 +00:00
|
|
|
|
|
|
|
drbg_init();
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
if (!is_mode_unprivileged()) {
|
|
|
|
collect_hw_entropy(true);
|
|
|
|
timer_init();
|
2018-02-11 22:10:12 +00:00
|
|
|
#ifdef APPVER
|
2019-03-29 16:10:31 +00:00
|
|
|
// enable MPU (Memory Protection Unit)
|
|
|
|
mpu_config_firmware();
|
2019-02-15 13:22:52 +00:00
|
|
|
#endif
|
2019-03-29 16:10:31 +00:00
|
|
|
} else {
|
|
|
|
collect_hw_entropy(false);
|
|
|
|
}
|
2018-02-11 22:10:12 +00:00
|
|
|
|
2016-12-04 22:24:01 +00:00
|
|
|
#if DEBUG_LINK
|
2019-03-29 16:10:31 +00:00
|
|
|
oledSetDebugLink(1);
|
2019-05-17 15:00:24 +00:00
|
|
|
#if !EMULATOR
|
2019-03-29 16:10:31 +00:00
|
|
|
config_wipe();
|
2019-05-17 15:00:24 +00:00
|
|
|
#endif
|
2014-04-29 12:26:51 +00:00
|
|
|
#endif
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
oledDrawBitmap(40, 0, &bmp_logo64);
|
|
|
|
oledRefresh();
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
config_init();
|
|
|
|
layoutHome();
|
|
|
|
usbInit();
|
|
|
|
for (;;) {
|
|
|
|
usbPoll();
|
|
|
|
check_lock_screen();
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
return 0;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|