qmk/qmk_firmware

[Bug] EEPROM writes inside via_init_kb are erased

Open

#21,959 创建于 2023年9月9日

在 GitHub 查看
 (0 评论) (1 反应) (0 负责人)C (43,867 fork)batch import
bughelp wanted

仓库指标

Star
 (20,368 star)
PR 合并指标
 (平均合并 20天 9小时) (30 天内合并 27 个 PR)

描述

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

贡献者指南