From ba41c0911dc70b1794e00e3974dd092cd4fe2afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 7 Sep 2020 21:03:37 +0200 Subject: [PATCH] Add necessary sections for stack protection area and implement fill function --- .../include/reflow-controller/stack-check.h | 4 ++ stm-firmware/stack-check.c | 43 ++++++++++++++++++- stm-firmware/stm32f407vet6_flash.ld | 7 +++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/stm-firmware/include/reflow-controller/stack-check.h b/stm-firmware/include/reflow-controller/stack-check.h index 8e0e290..c51a3a7 100644 --- a/stm-firmware/include/reflow-controller/stack-check.h +++ b/stm-firmware/include/reflow-controller/stack-check.h @@ -50,4 +50,8 @@ static inline uint32_t read_stack_pointer() return stack_pointer; } +int stack_check_init_corruption_detect_area(void); + +int stack_check_corruption_detect_area(void); + #endif /* __STACK_CHECK_H__ */ diff --git a/stm-firmware/stack-check.c b/stm-firmware/stack-check.c index 666014d..41e55e7 100644 --- a/stm-firmware/stack-check.c +++ b/stm-firmware/stack-check.c @@ -20,9 +20,10 @@ #include #include +#include extern char __ld_top_of_stack; -extern char __ld_eheap; +extern char __ld_end_stack; int32_t stack_check_get_usage() { @@ -41,7 +42,45 @@ int32_t stack_check_get_free() uint32_t stack_ptr; stack_ptr = read_stack_pointer(); - upper_heap_boundary = (uint32_t)&__ld_eheap; + upper_heap_boundary = (uint32_t)&__ld_end_stack; return stack_ptr - upper_heap_boundary; } + +extern uint32_t __ld_start_stack_corruption_detect_area; +extern uint32_t __ld_end_stack_corruption_detect_area; + +int stack_check_init_corruption_detect_area(void) +{ + volatile uint32_t *ptr = &__ld_start_stack_corruption_detect_area; + volatile uint32_t *end_ptr = &__ld_end_stack_corruption_detect_area; + enum random_number_error rng_stat; + uint32_t rng_number; + int ret = 0; + + end_ptr--; + + random_number_gen_init(false); + + while (ptr < end_ptr) { + rng_stat = random_number_gen_get_number(&rng_number, true); + + if (rng_stat != RNG_ERROR_OK) { + ret = -1; + goto exit_deinit_rng; + } + + *ptr = rng_number; + ptr++; + } + +exit_deinit_rng: + random_number_gen_deinit(); + + return ret; +} + +int stack_check_corruption_detect_area(void) +{ + +} diff --git a/stm-firmware/stm32f407vet6_flash.ld b/stm-firmware/stm32f407vet6_flash.ld index e1f503a..bbb2809 100644 --- a/stm-firmware/stm32f407vet6_flash.ld +++ b/stm-firmware/stm32f407vet6_flash.ld @@ -25,6 +25,7 @@ /* USER PARAMETERS */ __ld_stack_size = 0x3000; __ld_heap_size = 0x2100; +__stack_corruption_area_size = 64; /* END OF USER PARAMETERS */ ENTRY(Reset_Handler) @@ -150,6 +151,12 @@ SECTIONS __ld_sheap = .; . = . + __ld_heap_size; __ld_eheap = .; + . = ALIGN(4) + __ld_start_stack_corruption_detect_area = .; + . = . + __stack_corruption_area_size; + . = ALIGN(4); + __ld_end_stack_corruption_detect_area = .; + __ld_end_stack = . . = . + __ld_stack_size; . = ALIGN(4); } >RAM