Application fails to start after bootloader

I am working with nucleo-f413 board. My project work normally without bootloader. I need to include bootloader to my board. I have also different project on custom board with STM32F429. Custom board has bootloader and works without a problem. I copied bootloader source to nucleo-f413 with appropriate settings. But after upload to nucleo-f413 board do not work. Board successfully start bootloader, but error
occurs when control is passed to main application.

No update found to apply
SD deinit
Starting application at addr: 134479872=MError Info ++
Error Status: 0x8001011D Code: 285 Module: 1
Error Message: 

++ MbedOS Error Info ++
Error Status: 0x80FF0100 Code: 256 Module: 255
Error Message: Fatal Run-time error
Location: 0x48E9
Error Value: 0x0
Current Thread: <unnamed> Id: 0x0 Entry: 0x8000481 StackSize: 0x0 StackMem: 0x80004B1 SP: 0x2004FE54 
For more info, visit: https://mbed.com/s/error?error=0x80FF0100&tgt=NUCLEO_F413ZH
-- MbedOS Error Info --
SetSysClock failed

Bootloader mbed_app.json:

{
    "config": {
        "update_file": {
            "help": "Path to the application update binary on the SD card",
            "value": "\"pam_upload_lora.bin\""
        },
        "hse_value": {
            "value": "8000000",
            "macro_name": "HSE_VALUE"
        }
	},

    "target_overrides": {
        "*": {
            "target.components_add": ["SD"],
            "target.clock_source": "USE_PLL_HSE_XTAL|USE_PLL_HSI",
            "target.restrict_size":"0x20000"
        }
   }
}

main application mbed_app.json:

{
    "target_overrides": {
        "*": {
            "target.components_add": ["SD"],
            "target.clock_source": "USE_PLL_HSE_XTAL|USE_PLL_HSI",
            "rtos.main-thread-stack-size": 16384,
            "rtos.thread-stack-size": 8192,
            "target.bootloader_img": "bootloader_lora.bin",
            "target.header_offset": "0x20000",
            "target.app_offset": "0x40000"
       }
    },
    
    "config": {
        "hse_value": {
            "value": "8000000",
            "macro_name": "HSE_VALUE"
        }
    }
}

header mbed_lib.json:

{
    "name": "application_header",
    "target_overrides": {
        "*": {
            "target.header_format": [
                ["magic", "const", "32be", "0x5a51b3d4"],
                ["headerVersion", "const", "32be", "2"],
                ["firmwareVersion", "timestamp", "64be", null],
                ["firmwareSize", "size", "64be", ["application"]],
                ["firmwareHash", "digest", "SHA256", "application"],
                ["headerCRC", "digest", "CRCITT32be", "header"]
            ]
        }
    }
}

main.cpp bootloader:

#include "mbed.h"

#include "SDBlockDevice.h"
#include "FATFileSystem.h"



#define SD_MOUNT_PATH           "sd"
#define FULL_UPDATE_FILE_PATH   "/" SD_MOUNT_PATH "/" MBED_CONF_APP_UPDATE_FILE

//#define APPLICATION_ADDR      0x08010400


//#define POST_APPLICATION_ADDR ((APPLICATION_ADDR) + (APPLICATION_SIZE))
#define MY_POST_APPLICATION_ADDR  0x08040000

#if !defined(POST_APPLICATION_ADDR)
#error "target.restrict_size must be set for your target in mbed_app.json"
#endif

# define START_L pinLeft.write(0) 
# define STOP_L  pinLeft.write(1) 
# define START_R pinRight.write(0)
# define STOP_R  pinRight.write(1)


//Pin order: MOSI, MISO, SCK, CS
//SDBlockDevice sd(MBED_CONF_APP_SD_CARD_MOSI, MBED_CONF_APP_SD_CARD_MISO,
//                 MBED_CONF_APP_SD_CARD_SCK, MBED_CONF_APP_SD_CARD_CS);

Serial pc( USBTX, USBRX );


SDBlockDevice sd(D11, D12, D13, D4);
FATFileSystem fs("sd", &sd);



DigitalOut pinLeft(PD_0);
DigitalOut pinRight(PD_1);


FlashIAP flash;


void apply_update(FILE *file, uint32_t address);

int main()
{
//    sd.init();
//    fs.mount(&sd);

    STOP_L;
    STOP_R;

    FILE *file = fopen(FULL_UPDATE_FILE_PATH, "rb");
    if (file != NULL) {
        pc.printf("Firmware update found\r\n");

 //       apply_update(file, POST_APPLICATION_ADDR);
        apply_update(file, MY_POST_APPLICATION_ADDR);
//        apply_update(file, POST_APPLICATION_ADDR);

        fclose(file);
        remove(FULL_UPDATE_FILE_PATH);
        pc.printf("Update done\r\n");

    } 
    else 
    {
        pc.printf("No update found to apply\r\n");
    }

    fs.unmount();

    pc.printf("SD deinit\r\n");
    
    sd.deinit();

    pc.printf("Starting application at addr: %d" "\r\n",  MY_POST_APPLICATION_ADDR);

    // mbed_start_application(POST_APPLICATION_ADDR);
    mbed_start_application(MY_POST_APPLICATION_ADDR);
    
 //   mbed_start_application(POST_APPLICATION_ADDR);
}

void apply_update(FILE *file, uint32_t address)
{
    fseek(file, 0, SEEK_END);
    long len = ftell(file);
    pc.printf("Firmware size is %ld bytes\r\n", len);
    fseek(file, 0, SEEK_SET);
  
    flash.init();

    const uint32_t page_size = flash.get_page_size();
    char *page_buffer = new char[page_size];
    uint32_t addr = address;
    uint32_t next_sector = addr + flash.get_sector_size(addr);
    bool sector_erased = false;
    size_t pages_flashed = 0;
    uint32_t percent_done = 0;
    while (true) {

        // Read data for this page
        memset(page_buffer, 0, sizeof(char) * page_size);
        int size_read = fread(page_buffer, 1, page_size, file);
        if (size_read <= 0) {
            break;
        }

        // Erase this page if it hasn't been erased
        if (!sector_erased) {
            if( 0 !=flash.erase(addr, flash.get_sector_size(addr)) )
            {
              pc.printf("Erase is NOT OK!\r\n");
            }
              sector_erased = true;
        }

        // Program page
         if( 0 != flash.program(page_buffer, addr, page_size) )
         {
             pc.printf("Write is NOT OK!\r\n");
         }
    
        addr += page_size;
        if (addr >= next_sector) {
            next_sector = addr + flash.get_sector_size(addr);
            sector_erased = false;
        }

        if (++pages_flashed % 3 == 0) {
            uint32_t percent_done_new = ftell(file) * 100 / len;
            if (percent_done != percent_done_new) {
                percent_done = percent_done_new;
                pc.printf("Flashed %3ld%%\r", percent_done);
            }
        }
    }
    pc.printf("Flashed 100%%\r\n");

    delete[] page_buffer;

    flash.deinit();
}

Thx in advance for your help

Hello,

Did you also use the header with F429Zi?

BR, Jan

Thx for your answer. I am solved this problem. I found different sysconfig functions. F413 have static define address vector table. F429 - calculate. When I correct this in target file - bootloader is work fine.