Fixed memory layout to avoid running into segfaults

This commit is contained in:
Sebastian 2025-12-28 13:07:42 +01:00
parent 72bf0d8793
commit 523338dfc9
2 changed files with 7 additions and 27 deletions

View file

@ -1,5 +1,5 @@
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
use core::ptr::addr_of_mut;
use stm32f4xx_hal::pac; use stm32f4xx_hal::pac;
fn jump_to_bootloader() { fn jump_to_bootloader() {
@ -23,25 +23,19 @@ fn jump_to_bootloader() {
const BOOTLOADER_REQUESTED: u32 = 0xdecafbad; const BOOTLOADER_REQUESTED: u32 = 0xdecafbad;
fn magic_mut_ptr() -> *mut u32 { #[link_section = ".uninit.DFU_FLAG"]
extern "C" { static mut DFU_FLAG: MaybeUninit<u32> = MaybeUninit::uninit();
#[link_name = "_dfu_magic"]
static mut magic: u32;
}
unsafe { addr_of_mut!(magic) }
}
fn get_bootloader_flag() -> u32 { fn get_bootloader_flag() -> u32 {
unsafe { magic_mut_ptr().read_volatile() } unsafe { DFU_FLAG.assume_init() }
} }
fn set_bootloader_flag() { fn set_bootloader_flag() {
unsafe { magic_mut_ptr().write_volatile(BOOTLOADER_REQUESTED) }; unsafe { DFU_FLAG.write(BOOTLOADER_REQUESTED) };
} }
fn clear_bootloader_flag() { fn clear_bootloader_flag() {
unsafe { magic_mut_ptr().write_volatile(BOOTLOADER_REQUESTED) }; unsafe { DFU_FLAG.write(0) };
} }
pub fn init() { pub fn init() {

View file

@ -1,23 +1,9 @@
MEMORY MEMORY
{ {
FLASH : ORIGIN = 0x08000000, LENGTH = 256K FLASH : ORIGIN = 0x08000000, LENGTH = 256K
UNINIT : ORIGIN = 0x20000000, LENGTH = 0x10 RAM : ORIGIN = 0x20000000, LENGTH = 64K
RAM : ORIGIN = 0x20000010, LENGTH = 64K - LENGTH(UNINIT)
} }
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. */ /* This is where the call stack will be allocated. */
/* The stack is of the full descending type. */ /* The stack is of the full descending type. */
/* NOTE Do NOT modify `_stack_start` unless you know what you are doing */ /* NOTE Do NOT modify `_stack_start` unless you know what you are doing */