From 24b46a5fff2f25a6ce362d6003093baef03290d0 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 28 Dec 2025 13:07:42 +0100 Subject: [PATCH] Fixed memory layout to avoid running into segfaults --- firmware/src/bootloader.rs | 18 ++++++------------ memory.x | 16 +--------------- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/firmware/src/bootloader.rs b/firmware/src/bootloader.rs index 0c87fe3..c186b18 100644 --- a/firmware/src/bootloader.rs +++ b/firmware/src/bootloader.rs @@ -1,5 +1,5 @@ use core::mem::MaybeUninit; -use core::ptr::addr_of_mut; + use stm32f4xx_hal::pac; fn jump_to_bootloader() { @@ -23,25 +23,19 @@ fn jump_to_bootloader() { const BOOTLOADER_REQUESTED: u32 = 0xdecafbad; -fn magic_mut_ptr() -> *mut u32 { - extern "C" { - #[link_name = "_dfu_magic"] - static mut magic: u32; - } - - unsafe { addr_of_mut!(magic) } -} +#[link_section = ".uninit.DFU_FLAG"] +static mut DFU_FLAG: MaybeUninit = MaybeUninit::uninit(); fn get_bootloader_flag() -> u32 { - unsafe { magic_mut_ptr().read_volatile() } + unsafe { DFU_FLAG.assume_init() } } fn set_bootloader_flag() { - unsafe { magic_mut_ptr().write_volatile(BOOTLOADER_REQUESTED) }; + unsafe { DFU_FLAG.write(BOOTLOADER_REQUESTED) }; } fn clear_bootloader_flag() { - unsafe { magic_mut_ptr().write_volatile(BOOTLOADER_REQUESTED) }; + unsafe { DFU_FLAG.write(0) }; } pub fn init() { diff --git a/memory.x b/memory.x index 33fd2a3..78b279a 100644 --- a/memory.x +++ b/memory.x @@ -1,23 +1,9 @@ MEMORY { FLASH : ORIGIN = 0x08000000, LENGTH = 256K - UNINIT : ORIGIN = 0x20000000, LENGTH = 0x10 - RAM : ORIGIN = 0x20000010, LENGTH = 64K - LENGTH(UNINIT) + RAM : ORIGIN = 0x20000000, LENGTH = 64K } -SECTIONS { - .uninit_flags (NOLOAD) : ALIGN(4) - { - . = ALIGN(4); - __suninit_flags = .; - _dfu_magic = .; . += 4; - *(.uninit_flags .uninit_flags.*) - . = ALIGN(4); - __euninit_flags = .; - } > UNINIT -} - - /* This is where the call stack will be allocated. */ /* The stack is of the full descending type. */ /* NOTE Do NOT modify `_stack_start` unless you know what you are doing */