qmk/qmk_firmware

[Bug] EEPROM writes inside via_init_kb are erased

Open

#21.959 geöffnet am 9. Sept. 2023

Auf GitHub ansehen
 (0 Kommentare) (1 Reaktion) (0 zugewiesene Personen)C (43.867 Forks)batch import
bughelp wanted

Repository-Metriken

Stars
 (20.368 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 20T 9h) (27 gemergte PRs in 30 T)

Beschreibung

Describe the Bug

Loading a custom configuration for VIA is commonly implemented as follows:

void via_init_kb(void) {
	if (via_eeprom_is_valid()) {
		// Read config from EEPROM
	} else {
		// Write default config to EEPROM
	}
}

This will load the configuration from the EEPROM if VIA's magic values are present, otherwise a default configuration will be stored. This works correctly in some cases, however, if the keyboard is restarted after clearing the EEPROM without any further writes to the configuration, a blank configuration will be loaded. Steps to replicate are as follows:

  1. Use Bootmagic Lite to reset EEPROM.
  2. Reconnect keyboard to exit bootloader. This causes a default custom configuration to be used and saved.
  3. Reconnect keyboard without making any changes that cause the custom configuration to be updated. This causes a blank configuration to be loaded.

I believe this is caused by the following:

  1. keyboard_init is called which calls via_init which then calls via_init_kb.
  2. On returning from via_init_kb, via_init checks to see if the EEPROM is valid and uses eeconfig_init_via to initialise it if not.
  3. keyboard_init eventually calls quantum_init which calls magic to check if the EEPROM starts with the magic number (different to VIA's magic values). If the magic number isn't present eeconfig_init is called.
  4. eeconfig_init calls eeconfig_init_quantum which calls eeprom_driver_erase to erase the EEPROM, clearing any writes that have already occurred.
  5. eeconfig_init_quantum then calls eeconfig_init_via to initialise VIA's EEPROM again, but without calling via_init_kb.

An easy workaround is to save the configuration at a later stage such as within keyboard_post_init_user, although this potentially causes EEPROM writes on every boot. Another workaround would be to write defaults in eeconfig_init_kb and perform reads within keyboard_post_init_user.

Keyboard Used

No response

Link to product page (if applicable)

No response

Operating System

No response

qmk doctor Output

No response

Is AutoHotKey / Karabiner installed

  • AutoHotKey (Windows)
  • Karabiner (macOS)

Other keyboard-related software installed

No response

Additional Context

No response

Contributor Guide