/* * (c) danielinux 2019 * * GPLv.2 * * See LICENSE for details */ #include #include "system.h" #include "display.h" #include "systick.h" #include "button.h" #include #include #include "usecfs.h" static const char welcome_message0[]= " There is no "; static const char welcome_message1[]= "knowledge that "; static const char welcome_message2[]= " is not power. "; 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; static char password[MAX_PASSWORD] = {}; static int pin_idx = 0; #define SLEEP_TIME (5000) void ui_msg(const char *txt) { display_scroll(NULL, 0); display_clear(NULL); display_text(6, txt); last_action = jiffies; } void lock(void) { //led_on(RED_LED); ui_msg("Locked."); fsm_state = ST_LOCKED; } int unlock(void) { uint8_t stored_uuid[UUID_LEN]; if (pin_idx < 6) { ui_msg("pin code too short"); 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)) { memset(password, 0, MAX_PASSWORD); 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 display_text(6, "Failed."); memset(password, 0, MAX_PASSWORD); pin_idx = 0; fsm_state = ST_LOCKED; return -1; } } void ui_button_hold(uint16_t button) { last_action = jiffies; } void ui_sdcard_insert(void) { ui_msg("SD Card detected"); last_action = jiffies; } 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 "); display_text(2, "4. Reboot "); display_text(1, "3. SD format "); display_text(0, "2. USB vault ON "); switch (in) { case ' ': menu_scroll += 4; break; case '\r': menu_scroll -= 4; break; } menu_scroll %= 0x40; display_scroll(NULL, menu_scroll); } void ui_button_press(uint16_t button) { int i; char c; if (fsm_state == ST_LOCKED) { ui_msg("Insert pin:"); pin_idx = 0; memset(password, 0, MAX_PASSWORD); fsm_state = ST_PIN_ENTRY; return; } if (fsm_state == ST_PIN_ENTRY) { char line[17] = " "; for (i = 0; i < pin_idx; i++) { line[i] = '*'; } c = bu2c(button); if ((c >= '0') && (c <='9')) { if (pin_idx < 16) { line[pin_idx] = 'X'; } password[pin_idx++] = bu2c(button); } if (c == '\r') { unlock(); } else { display_scroll(NULL, 0); display_clear(NULL); display_text(7, line); display_text(6, "Insert pin:"); } } if (fsm_state == ST_UNLOCKED) { ui_menu(bu2c(button)); } last_action = jiffies; } 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) { display_scroll(NULL, 0); display_clear(NULL); if (fsm_state == ST_PIN_ENTRY) lock(); } }