//
// Created by imi415 on 2019/12/17.
//

#include <stm32h7xx_hal.h>

HAL_StatusTypeDef BL_CheckValidImage(uint32_t base_address) {
    uint32_t qspi_sp = *(uint32_t *)base_address;
    if(qspi_sp < 0x40000000) {
        return HAL_OK;
    }
    else return HAL_ERROR;
}

uint32_t BL_GetEntryPoint(uint32_t base_address) {
    uint32_t target_reset_vector = *(uint32_t *)(base_address + 0x04);
    if(target_reset_vector > 0x90000000 && target_reset_vector < 0xA0000000) {
        return target_reset_vector;
    }
    else return 0x00000000;
}

void BL_JumpToXIPStart(uint32_t entry_point, uint32_t vector_base) {
    __disable_irq();
    SysTick->CTRL = 0;
    SysTick->LOAD = 0;
    SysTick->VAL  = 0;
    void (*entry)(void) = (void *)entry_point;
    //__set_MSP(*(uint32_t *)vector_base);
    entry();
}