You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
4.1 KiB
160 lines
4.1 KiB
![]()
10 years ago
|
/*
|
||
![]()
7 years ago
|
* This file is part of the TREZOR project, https://trezor.io/
|
||
![]()
10 years ago
|
*
|
||
|
* 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"
|
||
![]()
5 years ago
|
#include "bl_check.h"
|
||
|
#include "buttons.h"
|
||
|
#include "common.h"
|
||
![]()
6 years ago
|
#include "config.h"
|
||
![]()
5 years ago
|
#include "gettext.h"
|
||
![]()
10 years ago
|
#include "layout.h"
|
||
![]()
10 years ago
|
#include "layout2.h"
|
||
![]()
5 years ago
|
#include "memzero.h"
|
||
|
#include "oled.h"
|
||
![]()
10 years ago
|
#include "rng.h"
|
||
![]()
5 years ago
|
#include "setup.h"
|
||
![]()
8 years ago
|
#include "timer.h"
|
||
![]()
5 years ago
|
#include "usb.h"
|
||
|
#include "util.h"
|
||
![]()
6 years ago
|
#if !EMULATOR
|
||
![]()
6 years ago
|
#include <libopencm3/stm32/desig.h>
|
||
![]()
5 years ago
|
#include "otp.h"
|
||
![]()
6 years ago
|
#endif
|
||
![]()
10 years ago
|
|
||
![]()
7 years ago
|
/* Screen timeout */
|
||
|
uint32_t system_millis_lock_start;
|
||
|
|
||
![]()
5 years ago
|
void check_lock_screen(void) {
|
||
|
buttonUpdate();
|
||
|
|
||
|
// wake from screensaver on any button
|
||
|
if (layoutLast == layoutScreensaver && (button.NoUp || button.YesUp)) {
|
||
|
layoutHome();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// button held for long enough (2 seconds)
|
||
|
if (layoutLast == layoutHome && button.NoDown >= 285000 * 2) {
|
||
|
layoutDialog(&bmp_icon_question, _("Cancel"), _("Lock Device"), NULL,
|
||
|
_("Do you really want to"), _("lock your TREZOR?"), NULL, NULL,
|
||
|
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
|
||
|
session_clear(true);
|
||
|
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
|
||
|
session_clear(true);
|
||
|
layoutScreensaver();
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
}
|
||
|
|
||
![]()
5 years ago
|
static void collect_hw_entropy(bool privileged) {
|
||
![]()
6 years ago
|
#if EMULATOR
|
||
![]()
5 years ago
|
(void)privileged;
|
||
|
memzero(HW_ENTROPY_DATA, HW_ENTROPY_LEN);
|
||
![]()
6 years ago
|
#else
|
||
![]()
5 years ago
|
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)) {
|
||
|
uint8_t entropy[FLASH_OTP_BLOCK_SIZE];
|
||
|
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);
|
||
|
}
|
||
![]()
6 years ago
|
#endif
|
||
|
}
|
||
|
|
||
![]()
5 years ago
|
int main(void) {
|
||
![]()
10 years ago
|
#ifndef APPVER
|
||
![]()
5 years ago
|
setup();
|
||
|
__stack_chk_guard = random32(); // this supports compiler provided
|
||
|
// unpredictable stack protection checks
|
||
|
oledInit();
|
||
![]()
10 years ago
|
#else
|
||
![]()
5 years ago
|
check_bootloader();
|
||
|
setupApp();
|
||
|
__stack_chk_guard = random32(); // this supports compiler provided
|
||
|
// unpredictable stack protection checks
|
||
![]()
10 years ago
|
#endif
|
||
![]()
5 years ago
|
if (!is_mode_unprivileged()) {
|
||
|
collect_hw_entropy(true);
|
||
|
timer_init();
|
||
![]()
7 years ago
|
#ifdef APPVER
|
||
![]()
5 years ago
|
// enable MPU (Memory Protection Unit)
|
||
|
mpu_config_firmware();
|
||
![]()
6 years ago
|
#endif
|
||
![]()
5 years ago
|
} else {
|
||
|
collect_hw_entropy(false);
|
||
|
}
|
||
![]()
7 years ago
|
|
||
![]()
8 years ago
|
#if DEBUG_LINK
|
||
![]()
5 years ago
|
oledSetDebugLink(1);
|
||
|
config_wipe();
|
||
![]()
10 years ago
|
#endif
|
||
|
|
||
![]()
5 years ago
|
oledDrawBitmap(40, 0, &bmp_logo64);
|
||
|
oledRefresh();
|
||
![]()
10 years ago
|
|
||
![]()
5 years ago
|
config_init();
|
||
|
layoutHome();
|
||
|
usbInit();
|
||
|
for (;;) {
|
||
|
usbPoll();
|
||
|
check_lock_screen();
|
||
|
}
|
||
![]()
10 years ago
|
|
||
![]()
5 years ago
|
return 0;
|
||
![]()
10 years ago
|
}
|