#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/ssl.h>
#include <wolfssl/wolfcrypt/random.h>
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#ifdef USE_WOLF_ARM_STARTUP
extern uint32_t __data_load_start__[];
extern uint32_t __data_start__[];
extern uint32_t __data_end__[];
extern uint32_t __bss_start__[];
extern uint32_t __bss_end__[];
extern uint32_t __stack_process_end__[];
extern uint32_t __heap_start__[];
extern uint32_t __heap_end__[];
void memcpy32(uint32_t* src, uint32_t* dst_beg, uint32_t* dst_end)
{
while (dst_beg < dst_end) {
*dst_beg++ = *src++;
}
}
void meminit32(uint32_t* start, uint32_t* end)
{
while (start < end) {
*start++ = 0;
}
}
#endif
void reset_handler(void)
{
#ifdef USE_WOLF_ARM_STARTUP
memcpy32(__data_load_start__, __data_start__, __data_end__);
meminit32(__bss_start__, __bss_end__);
__heap_start__[0] = 0;
__heap_start__[1] = ((uintptr_t)__heap_end__ - (uintptr_t)__heap_start__);
#endif
extern int main(void);
main();
while(1);
}
#ifdef USE_WOLF_ARM_STARTUP
static void Default_Handler(void)
{
while(1);
}
__attribute__((section(".sys"))) __attribute__ ((used))
void HardFault_HandlerC( uint32_t *hardfault_args )
{
volatile uint32_t stacked_r0;
volatile uint32_t stacked_r1;
volatile uint32_t stacked_r2;
volatile uint32_t stacked_r3;
volatile uint32_t stacked_r12;
volatile uint32_t stacked_lr;
volatile uint32_t stacked_pc;
volatile uint32_t stacked_psr;
volatile uint32_t _CFSR;
volatile uint32_t _HFSR;
volatile uint32_t _DFSR;
volatile uint32_t _AFSR;
volatile uint32_t _BFAR;
volatile uint32_t _MMAR;
stacked_r0 = ((uint32_t)hardfault_args[0]);
stacked_r1 = ((uint32_t)hardfault_args[1]);
stacked_r2 = ((uint32_t)hardfault_args[2]);
stacked_r3 = ((uint32_t)hardfault_args[3]);
stacked_r12 = ((uint32_t)hardfault_args[4]);
stacked_lr = ((uint32_t)hardfault_args[5]);
stacked_pc = ((uint32_t)hardfault_args[6]);
stacked_psr = ((uint32_t)hardfault_args[7]);
_CFSR = (*((volatile uint32_t *)(0xE000ED28)));
_HFSR = (*((volatile uint32_t *)(0xE000ED2C)));
_DFSR = (*((volatile uint32_t *)(0xE000ED30)));
_AFSR = (*((volatile uint32_t *)(0xE000ED3C)));
_MMAR = (*((volatile uint32_t *)(0xE000ED34)));
_BFAR = (*((volatile uint32_t *)(0xE000ED38)));
printf ("\n\nHard fault handler (all numbers in hex):\n");
printf ("R0 = %lx\n", stacked_r0);
printf ("R1 = %lx\n", stacked_r1);
printf ("R2 = %lx\n", stacked_r2);
printf ("R3 = %lx\n", stacked_r3);
printf ("R12 = %lx\n", stacked_r12);
printf ("LR [R14] = %lx subroutine call return address\n", stacked_lr);
printf ("PC [R15] = %lx program counter\n", stacked_pc);
printf ("PSR = %lx\n", stacked_psr);
printf ("CFSR = %lx\n", _CFSR);
printf ("HFSR = %lx\n", _HFSR);
printf ("DFSR = %lx\n", _DFSR);
printf ("AFSR = %lx\n", _AFSR);
printf ("MMAR = %lx\n", _MMAR);
printf ("BFAR = %lx\n", _BFAR);
__asm("BKPT #0\n");
}
__attribute__((section(".sys"))) __attribute__( ( naked ) )
void HardFault_Handler(void)
{
__asm volatile
(
" movs r0,#4 \n"
" mov r1, lr \n"
" tst r0, r1 \n"
" beq _MSP \n"
" mrs r0, psp \n"
" b _GetPC \n"
"_MSP: \n"
" mrs r0, msp \n"
"_GetPC: \n"
" ldr r1,[r0,#20] \n"
" ldr r2, =HardFault_HandlerC \n"
" bx r2 \n"
" bx lr \n"
);
}
typedef void (*vector_entry)(void);
const vector_entry vectors[] __attribute__ ((section(".vectors"),used)) =
{
(vector_entry)__stack_process_end__, reset_handler, Default_Handler, HardFault_Handler, Default_Handler, HardFault_Handler, HardFault_Handler, Default_Handler, Default_Handler, Default_Handler, Default_Handler, Default_Handler, Default_Handler, Default_Handler, Default_Handler, Default_Handler,
};
#endif