vial: use more entropy for determining whether to reset eeprom

This commit is contained in:
Ilya Zhuravlev 2020-12-19 21:51:11 -05:00
parent 10e15c09a4
commit 21bd0cdda0
3 changed files with 22 additions and 0 deletions

View File

@ -641,6 +641,7 @@ $(shell echo '#define QMK_VERSION "$(GIT_VERSION)"' > $(ROOT_DIR)/quantum/versio
$(shell echo '#define QMK_BUILDDATE "$(BUILD_DATE)"' >> $(ROOT_DIR)/quantum/version.h)
$(shell echo '#define CHIBIOS_VERSION "$(CHIBIOS_VERSION)"' >> $(ROOT_DIR)/quantum/version.h)
$(shell echo '#define CHIBIOS_CONTRIB_VERSION "$(CHIBIOS_CONTRIB_VERSION)"' >> $(ROOT_DIR)/quantum/version.h)
$(shell python3 util/build_id.py >> $(ROOT_DIR)/quantum/version.h)
else
BUILD_DATE := NA
endif

View File

@ -65,10 +65,16 @@ void via_qmk_rgblight_get_value(uint8_t *data);
// Can be called in an overriding via_init_kb() to test if keyboard level code usage of
// EEPROM is invalid and use/save defaults.
bool via_eeprom_is_valid(void) {
#ifdef VIAL_ENABLE
uint8_t magic0 = BUILD_ID & 0xFF;
uint8_t magic1 = (BUILD_ID >> 8) & 0xFF;
uint8_t magic2 = (BUILD_ID >> 16) & 0xFF;
#else
char * p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
uint8_t magic0 = ((p[2] & 0x0F) << 4) | (p[3] & 0x0F);
uint8_t magic1 = ((p[5] & 0x0F) << 4) | (p[6] & 0x0F);
uint8_t magic2 = ((p[8] & 0x0F) << 4) | (p[9] & 0x0F);
#endif
return (eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0) == magic0 && eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1) == magic1 && eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2) == magic2);
}
@ -76,10 +82,16 @@ bool via_eeprom_is_valid(void) {
// Sets VIA/keyboard level usage of EEPROM to valid/invalid
// Keyboard level code (eg. via_init_kb()) should not call this
void via_eeprom_set_valid(bool valid) {
#ifdef VIAL_ENABLE
uint8_t magic0 = BUILD_ID & 0xFF;
uint8_t magic1 = (BUILD_ID >> 8) & 0xFF;
uint8_t magic2 = (BUILD_ID >> 16) & 0xFF;
#else
char * p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
uint8_t magic0 = ((p[2] & 0x0F) << 4) | (p[3] & 0x0F);
uint8_t magic1 = ((p[5] & 0x0F) << 4) | (p[6] & 0x0F);
uint8_t magic2 = ((p[8] & 0x0F) << 4) | (p[9] & 0x0F);
#endif
eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0, valid ? magic0 : 0xFF);
eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1, valid ? magic1 : 0xFF);

9
util/build_id.py Normal file
View File

@ -0,0 +1,9 @@
import random
def main():
print("#define BUILD_ID ((uint32_t)0x{:08X})".format(random.randrange(0, 2 ** 24 - 1)))
if __name__ == "__main__":
main()