2019-10-27 07:30:03 +01:00
|
|
|
/*
|
|
|
|
* (c) danielinux 2019
|
|
|
|
*
|
|
|
|
* GPLv.2
|
|
|
|
*
|
|
|
|
* See LICENSE for details
|
|
|
|
*/
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "system.h"
|
|
|
|
#include "display.h"
|
|
|
|
#include "systick.h"
|
|
|
|
#include "button.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "usecfs.h"
|
2019-11-04 15:38:49 +01:00
|
|
|
#include "usb.h"
|
2019-10-27 07:30:03 +01:00
|
|
|
|
2019-11-02 14:44:45 +01:00
|
|
|
|
|
|
|
/* Uncomment for device initialization */
|
|
|
|
//#define DEVICE_INITIALIZATION
|
|
|
|
|
2019-10-27 07:30:03 +01:00
|
|
|
static const char welcome_message0[]= " There is no ";
|
|
|
|
static const char welcome_message1[]= "knowledge that ";
|
|
|
|
static const char welcome_message2[]= " is not power. ";
|
|
|
|
|
2019-11-02 14:44:45 +01:00
|
|
|
|
2019-10-27 07:30:03 +01:00
|
|
|
static const uint8_t uuid[UUID_LEN] = {
|
|
|
|
0xb4, 0x6b, 0x82, 0x05, 0xb6, 0xcd, 0x28, 0xaa, 0xc7, 0x81, 0x2e, 0x2d,
|
|
|
|
0xfd, 0xdd, 0x5e, 0x70, 0x3f, 0xbf, 0x09, 0x03, 0x1b, 0x5f, 0xbe, 0xc6,
|
|
|
|
0x09, 0x62, 0xa8, 0x23, 0xe9, 0x99, 0x5e, 0xcb, 0xb4, 0xab, 0x28, 0xdc,
|
|
|
|
0x14, 0xcb, 0x35, 0xb4, 0x7d, 0xfe, 0x26, 0x81, 0x33, 0xd0, 0x4b, 0xc2,
|
|
|
|
0x49, 0x53, 0x05, 0xc3, 0xe7, 0xbd, 0x9a, 0x50, 0xb8, 0x01, 0x30, 0x0b,
|
|
|
|
0x62, 0x58, 0xad, 0xba
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static uint32_t last_action = 0;
|
|
|
|
static uint32_t offset = 0;
|
|
|
|
|
|
|
|
#define ST_LOCKED 0
|
|
|
|
#define ST_PIN_ENTRY 1
|
|
|
|
#define ST_UNLOCKED 2
|
|
|
|
#define MAX_PASSWORD 32
|
|
|
|
|
|
|
|
static int fsm_state = ST_LOCKED;
|
2019-11-04 15:38:49 +01:00
|
|
|
static int sleeping = 0;
|
2019-10-27 07:30:03 +01:00
|
|
|
|
|
|
|
static char password[MAX_PASSWORD] = {};
|
|
|
|
static int pin_idx = 0;
|
|
|
|
|
|
|
|
|
|
|
|
#define SLEEP_TIME (5000)
|
|
|
|
|
2019-11-04 15:38:49 +01:00
|
|
|
static void ui_sleep(void)
|
|
|
|
{
|
|
|
|
display_scroll(NULL, 0);
|
|
|
|
display_clear(NULL);
|
|
|
|
sleeping = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ui_wakeup(void)
|
|
|
|
{
|
|
|
|
last_action = jiffies;
|
|
|
|
if (sleeping) {
|
|
|
|
sleeping = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-27 07:30:03 +01:00
|
|
|
void ui_msg(const char *txt)
|
|
|
|
{
|
2019-11-04 15:38:49 +01:00
|
|
|
ui_wakeup();
|
2019-10-27 07:30:03 +01:00
|
|
|
display_scroll(NULL, 0);
|
|
|
|
display_clear(NULL);
|
|
|
|
display_text(6, txt);
|
|
|
|
}
|
|
|
|
|
2019-11-08 11:13:17 +01:00
|
|
|
static int ui_vault_on(void)
|
|
|
|
{
|
|
|
|
ui_msg("Activating Vault");
|
|
|
|
bridge_init();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-27 07:30:03 +01:00
|
|
|
void lock(void)
|
|
|
|
{
|
|
|
|
//led_on(RED_LED);
|
2019-11-02 14:44:45 +01:00
|
|
|
ui_msg("Device is Locked");
|
2019-10-27 07:30:03 +01:00
|
|
|
fsm_state = ST_LOCKED;
|
|
|
|
}
|
|
|
|
|
2019-11-04 15:38:49 +01:00
|
|
|
static void pwdrst(void)
|
2019-11-02 14:44:45 +01:00
|
|
|
{
|
|
|
|
memset(password, 0, MAX_PASSWORD);
|
|
|
|
pin_idx = 0;
|
|
|
|
}
|
|
|
|
|
2019-10-27 07:30:03 +01:00
|
|
|
int unlock(void)
|
|
|
|
{
|
|
|
|
uint8_t stored_uuid[UUID_LEN];
|
|
|
|
if (pin_idx < 6) {
|
2019-11-04 15:38:49 +01:00
|
|
|
display_scroll(NULL, 0);
|
|
|
|
display_clear(NULL);
|
|
|
|
display_text(5, "Failed.");
|
|
|
|
display_text(6, "Insert pin:");
|
|
|
|
pwdrst();
|
2019-10-27 07:30:03 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
display_scroll(NULL, 0);
|
|
|
|
display_clear(NULL);
|
|
|
|
display_text(6, " ");
|
|
|
|
display_text(5, "Unlocking secret");
|
|
|
|
if ((usecfs_init(password, 0, stored_uuid) == 0) &&
|
|
|
|
(memcmp(stored_uuid, uuid, UUID_LEN) == 0)) {
|
2019-11-04 15:38:49 +01:00
|
|
|
pwdrst();
|
2019-10-27 07:30:03 +01:00
|
|
|
fsm_state = ST_UNLOCKED;
|
|
|
|
ui_msg("Device UNLOCKED");
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
#ifdef DEVICE_INITIALIZATION
|
|
|
|
if (usecfs_init(password, 1, uuid) == 0) {
|
|
|
|
display_text(6, "DEVICE INITIALIZED.");
|
|
|
|
fsm_state = ST_UNLOCKED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2019-11-04 15:38:49 +01:00
|
|
|
display_scroll(NULL, 0);
|
|
|
|
display_clear(NULL);
|
|
|
|
display_text(5, "Failed.");
|
|
|
|
display_text(6, "Insert pin:");
|
|
|
|
pwdrst();
|
2019-10-27 07:30:03 +01:00
|
|
|
fsm_state = ST_LOCKED;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ui_button_hold(uint16_t button)
|
|
|
|
{
|
2019-11-04 15:38:49 +01:00
|
|
|
ui_wakeup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_status(void)
|
|
|
|
{
|
|
|
|
display_scroll(NULL, 0);
|
|
|
|
display_clear(NULL);
|
|
|
|
display_text(5, "Status Report ");
|
2019-11-08 11:13:17 +01:00
|
|
|
display_text(6, " - UART2 ON ");
|
|
|
|
display_text(7, " - SPI OK ");
|
|
|
|
display_text(0, " - SDCARD OK ");
|
|
|
|
display_text(1, " - USB VAULT ON ");
|
|
|
|
|
2019-10-27 07:30:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ui_sdcard_insert(void)
|
|
|
|
{
|
2019-11-04 15:38:49 +01:00
|
|
|
ui_wakeup();
|
2019-10-27 07:30:03 +01:00
|
|
|
ui_msg("SD Card detected");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_menu(char in)
|
|
|
|
{
|
|
|
|
static uint8_t menu_scroll = 0;
|
|
|
|
if (menu_scroll == 0) {
|
|
|
|
display_scroll(NULL, 0);
|
|
|
|
display_clear(NULL);
|
|
|
|
}
|
|
|
|
display_text(7, "1. Status ");
|
|
|
|
display_text(6, "0. Lockdown ");
|
|
|
|
display_text(5, "Main Menu ");
|
2019-11-04 15:38:49 +01:00
|
|
|
display_text(2, "4. UART menu ");
|
2019-10-27 07:30:03 +01:00
|
|
|
display_text(1, "3. SD format ");
|
|
|
|
display_text(0, "2. USB vault ON ");
|
|
|
|
switch (in) {
|
|
|
|
case ' ':
|
|
|
|
menu_scroll += 4;
|
2019-11-04 15:38:49 +01:00
|
|
|
menu_scroll %= 0x40;
|
|
|
|
display_scroll(NULL, menu_scroll);
|
2019-10-27 07:30:03 +01:00
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
menu_scroll -= 4;
|
2019-11-04 15:38:49 +01:00
|
|
|
menu_scroll %= 0x40;
|
|
|
|
display_scroll(NULL, menu_scroll);
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
ui_status();
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
ui_vault_on();
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
reboot();
|
|
|
|
panic();
|
2019-10-27 07:30:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_button_press(uint16_t button)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char c;
|
2019-11-04 15:38:49 +01:00
|
|
|
if (ui_wakeup())
|
|
|
|
c = 0;
|
|
|
|
else
|
|
|
|
c = bu2c(button);
|
2019-10-27 07:30:03 +01:00
|
|
|
if (fsm_state == ST_LOCKED) {
|
2019-11-04 15:38:49 +01:00
|
|
|
pwdrst();
|
2019-10-27 07:30:03 +01:00
|
|
|
ui_msg("Insert pin:");
|
|
|
|
fsm_state = ST_PIN_ENTRY;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fsm_state == ST_PIN_ENTRY) {
|
|
|
|
char line[17] = " ";
|
|
|
|
for (i = 0; i < pin_idx; i++) {
|
|
|
|
line[i] = '*';
|
|
|
|
}
|
|
|
|
if ((c >= '0') && (c <='9')) {
|
|
|
|
if (pin_idx < 16) {
|
|
|
|
line[pin_idx] = 'X';
|
|
|
|
}
|
|
|
|
password[pin_idx++] = bu2c(button);
|
|
|
|
}
|
|
|
|
if (c == '\r') {
|
|
|
|
unlock();
|
|
|
|
} else {
|
|
|
|
display_text(7, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fsm_state == ST_UNLOCKED)
|
|
|
|
{
|
|
|
|
ui_menu(bu2c(button));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_init(void)
|
|
|
|
{
|
|
|
|
uint32_t now;
|
|
|
|
int i;
|
|
|
|
display_scroll(NULL, 0x3f);
|
|
|
|
display_text(0, welcome_message0);
|
|
|
|
display_text(1, welcome_message1);
|
|
|
|
display_text(2, welcome_message2);
|
|
|
|
now = jiffies;
|
|
|
|
for (i = 0x3f; i >= 0x20; i--) {
|
|
|
|
display_scroll(NULL, i);
|
|
|
|
while ((jiffies - now) < 50)
|
|
|
|
WFI();
|
|
|
|
now = jiffies;
|
|
|
|
}
|
|
|
|
for (i = display_getcontrast(NULL); i >= 0; i--) {
|
|
|
|
display_setcontrast(NULL, i);
|
|
|
|
while ((jiffies - now) < 10)
|
|
|
|
WFI();
|
|
|
|
now = jiffies;
|
|
|
|
}
|
|
|
|
display_scroll(NULL, 0);
|
|
|
|
display_clear(NULL);
|
|
|
|
display_setcontrast(NULL, 0xcf);
|
|
|
|
lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_keepalive(uint32_t timeslice)
|
|
|
|
{
|
|
|
|
if ((jiffies - last_action) > SLEEP_TIME) {
|
2019-11-04 15:38:49 +01:00
|
|
|
ui_sleep();
|
2019-10-27 07:30:03 +01:00
|
|
|
if (fsm_state == ST_PIN_ENTRY)
|
|
|
|
lock();
|
|
|
|
}
|
|
|
|
}
|