qmk/qmk_firmware

[Bug] EEPROM writes inside via_init_kb are erased

Open

#21.959 aperta il 9 set 2023

Vedi su GitHub
 (0 commenti) (1 reazione) (0 assegnatari)C (43.867 fork)batch import
bughelp wanted

Metriche repository

Star
 (20.368 star)
Metriche merge PR
 (Merge medio 20g 9h) (27 PR mergiate in 30 g)

Descrizione

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

Guida contributor