diff --git a/common_features.mk b/common_features.mk index 97c16e7b66..2270571d7e 100644 --- a/common_features.mk +++ b/common_features.mk @@ -367,7 +367,7 @@ endif ifeq ($(strip $(VIAL_ENABLE)), yes) SRC += $(QUANTUM_DIR)/vial.c EXTRAINCDIRS += $(KEYMAP_OUTPUT) - OPT_DEFS += -DVIAL_ENABLE + OPT_DEFS += -DVIAL_ENABLE -DNO_DEBUG $(QUANTUM_DIR)/vial.c: $(KEYMAP_OUTPUT)/vial_generated_keyboard_definition.h @@ -375,6 +375,10 @@ $(KEYMAP_OUTPUT)/vial_generated_keyboard_definition.h: $(KEYMAP_PATH)/vial.json python3 util/vial_generate_definition.py $(KEYMAP_PATH)/vial.json $(KEYMAP_OUTPUT)/vial_generated_keyboard_definition.h endif +ifeq ($(strip $(VIAL_INSECURE)), yes) + OPT_DEFS += -DVIAL_INSECURE +endif + ifeq ($(strip $(VIAL_ENCODERS_ENABLE)), yes) OPT_DEFS += -DVIAL_ENCODERS_ENABLE endif diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index 12fe7a26f6..77a382305b 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c @@ -22,6 +22,10 @@ #include "dynamic_keymap.h" #include "via.h" // for default VIA_EEPROM_ADDR_END +#ifdef VIAL_ENABLE +#include "vial.h" +#endif + #ifndef DYNAMIC_KEYMAP_MACRO_COUNT # define DYNAMIC_KEYMAP_MACRO_COUNT 16 #endif @@ -183,6 +187,12 @@ extern uint16_t g_vial_magic_keycode_override; // This overrides the one in quantum/keymap_common.c uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) { +#ifdef VIAL_ENABLE + /* Disable any keycode processing while unlocking */ + if (vial_unlock_in_progress) + return KC_NO; +#endif + #ifdef VIAL_ENCODERS_ENABLE if (key.row == 254 && key.col == 254) return g_vial_magic_keycode_override; diff --git a/quantum/quantum.c b/quantum/quantum.c index dab6c9172f..dbe4822927 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -46,6 +46,10 @@ extern backlight_config_t backlight_config; # include "haptic.h" #endif +#ifdef VIAL_ENABLE +# include "vial.h" +#endif + #ifdef AUDIO_ENABLE # ifndef GOODBYE_SONG # define GOODBYE_SONG SONG(GOODBYE_SOUND) @@ -121,6 +125,12 @@ __attribute__((weak)) void post_process_record_kb(uint16_t keycode, keyrecord_t __attribute__((weak)) void post_process_record_user(uint16_t keycode, keyrecord_t *record) {} void reset_keyboard(void) { +#ifdef VIAL_ENABLE + /* Until keyboard is unlocked, disable processing of the RESET keycode */ + if (!vial_unlocked) + return; +#endif + clear_keyboard(); #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) process_midi_all_notes_off(); diff --git a/quantum/via.c b/quantum/via.c index ee102d180c..1afac67264 100644 --- a/quantum/via.c +++ b/quantum/via.c @@ -243,6 +243,11 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { break; } case id_switch_matrix_state: { +#ifdef VIAL_ENABLE + /* We don't need this wannabe keylogger */ + goto skip; +#endif + #if ((MATRIX_COLS / 8 + 1) * MATRIX_ROWS <= 28) uint8_t i = 1; for (uint8_t row = 0; row < MATRIX_ROWS; row++) { @@ -362,6 +367,11 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { break; } case id_dynamic_keymap_macro_set_buffer: { +#ifdef VIAL_ENABLE + /* Until keyboard is unlocked, don't allow changing macros */ + if (!vial_unlocked) + goto skip; +#endif uint16_t offset = (command_data[0] << 8) | command_data[1]; uint16_t size = command_data[2]; // size <= 28 if (size <= 28) @@ -395,6 +405,11 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { break; } case id_bootloader_jump: { +#ifdef VIAL_ENABLE + /* Until keyboard is unlocked, don't allow jumping to bootloader */ + if (!vial_unlocked) + goto skip; +#endif // Need to send data back before the jump // Informs host that the command is handled raw_hid_send(data, length); @@ -416,7 +431,7 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { break; } } - +skip: // Return the same buffer, optionally with values changed // (i.e. returning state to the host, or the unhandled state). raw_hid_send(data, length); diff --git a/quantum/vial.c b/quantum/vial.c index 8fcad65c6d..6d6f2a8d74 100644 --- a/quantum/vial.c +++ b/quantum/vial.c @@ -23,14 +23,29 @@ #include "dynamic_keymap.h" #include "quantum.h" +#define VIAL_UNLOCK_COUNTER_MAX 50 + enum { vial_get_keyboard_id = 0x00, vial_get_size = 0x01, vial_get_def = 0x02, vial_get_encoder = 0x03, vial_set_encoder = 0x04, + vial_get_lock = 0x05, + vial_unlock_start = 0x06, + vial_unlock_poll = 0x07, }; +#ifdef VIAL_INSECURE +#pragma message "Building Vial-enabled firmware in insecure mode." +int vial_unlocked = 1; +#else +int vial_unlocked = 0; +#endif +int vial_unlock_in_progress = 0; +static int vial_unlock_counter = 0; +static uint16_t vial_unlock_timer; + void vial_handle_cmd(uint8_t *msg, uint8_t length) { /* All packets must be fixed 32 bytes */ if (length != RAW_EPSIZE) @@ -87,6 +102,37 @@ void vial_handle_cmd(uint8_t *msg, uint8_t length) { break; } #endif + case vial_get_lock: { + msg[0] = !vial_unlocked; + break; + } + case vial_unlock_start: { + vial_unlock_in_progress = 1; + vial_unlock_counter = VIAL_UNLOCK_COUNTER_MAX; + vial_unlock_timer = timer_read(); + break; + } + case vial_unlock_poll: { + if (vial_unlock_in_progress) { + /* TODO: check specific keys instead of 0,0 */ + if (timer_elapsed(vial_unlock_timer) > 100 && MATRIX_IS_ON(0, 0)) { + vial_unlock_timer = timer_read(); + + vial_unlock_counter--; + if (vial_unlock_counter == 0) { + /* ok unlock succeeded */ + vial_unlock_in_progress = 0; + vial_unlocked = 1; + } + } else { + vial_unlock_counter = VIAL_UNLOCK_COUNTER_MAX; + } + } + msg[0] = vial_unlocked; + msg[1] = vial_unlock_in_progress; + msg[2] = vial_unlock_counter; + break; + } } } diff --git a/quantum/vial.h b/quantum/vial.h index e0d89817f9..875bcb6355 100644 --- a/quantum/vial.h +++ b/quantum/vial.h @@ -26,3 +26,6 @@ void vial_handle_cmd(uint8_t *data, uint8_t length); #ifdef VIAL_ENCODERS_ENABLE void vial_encoder_update(uint8_t index, bool clockwise); #endif + +extern int vial_unlocked; +extern int vial_unlock_in_progress;