Write variables to flash during upload on STM32

The question is very similiar to How to reserve a page of Flash memory? (STM32/arduino). An example project for that was located at GitHub - maxgerhardt/pio-stm32-reserved-flash-sector: An example for a modified linker script that relocates the code so that a 16kBytey page becomes free.. With this you can prevent the firmware from taking up a certain space (e.g., the last kilobyte or flash sector).

With OpenOCD commands (program with a specific address and .bin file of arbitrary content) you can then write to these addresses. For this you can create a custom target that triggers this write, or implement a fully custom upload method that flashes both firmware and some custom binary blob (Redirecting...).