You don't need the FLASH calls at all. These are some checks to validate if a valid application code has been stored.
Just load the stack pointer and reset address from the bootloader vector table and jump to the reset address.
void (*JumpToSystemBootloader)(void);
// set vector table start address
// volatile uint32_t addr = 0x1FFF0000; // for STM32F4
volatile uint32_t addr = 0x1FF00000; // for STM32L07xxx
// load reset vector address @+0x4
JumpToSystemBootloader = (void (*)(void)) (*((uint32_t *)(addr + 4)));
// load stack pointer address @+0x0
__set_MSP(*(uint32_t *)addr);
// jump to address loaded previously from @+0x4
JumpToSystemBootloader();
You probably need to disable also all interrupts and restore several controller states before entering the system bootloader.
See also: https://stm32f4-discovery.net/2017/04/tutorial-jump-system-memory-software-stm32/
Edit2:
New bootloader versions of the device always jumps to the application code again as long as in either bank1 or bank2 valid stack pointer resides in the vector table.
This is also the case if jumping to the bootloader from the application code.
Edit3:
Your provided code is some hacky way to force entering the bootloader anyway.
Therefore a magic word is stored in some place of the flash rom.
And during reentering the application code, at an early stage,
this value is checked to reenter the bootloader.
To bypass the bank check a new jump address is supplied (0x1FF00369).
(Determined by reverse engineering?)
It is assumed that the controller and the RAM is previously already initialized correctly.
Because the vendor can change the bootloader code whenever he want
the use of this code snippet should be done with caution.
My suggestion is not to use this code at all.
Jump To Bootloader using magic value in SRAM [not for production]
Reference: https://stackoverflow.com/a/43072025/5388805
// vector table start address (STM32L07xxx)
volatile uint32_t u32_boot_vector_addr = 0x1FF00000;
// bootloader bypass offset address
volatile uint32_t u32_boot_vector_offset = 0x369; // substituted from https://stackoverflow.com/a/43072025/5388805
// bootloader check definitions
volatile uint32_t * pu32_boot_tag = (volatile uint32_t *)0x20001800; // AN2606 states bootloader uses up to 5kByte RAM, add some offset
const uint32_t u32_boot_tag_reenter = 0xCAFEFEED;
const uint32_t u32_boot_tag_clear = 0xFFFFFFFF;
// call this at an early stage during startup (preferably right after entering the reset routine)
void checkBootloader()
{
// if magic tag is set jump back to bootloader
if (*pu32_boot_tag == u32_boot_tag_reenter)
{
// erase magic tag
*pu32_boot_tag = u32_boot_tag_clear;
// load bypass address
void (*JumpToSystemBootloader)(void) = (void (*)(void)) (*((uint32_t *)(u32_boot_vector_addr + u32_boot_vector_offset)));
// load stack pointer address @+0x0
__set_MSP(*(uint32_t *)u32_boot_vector_addr);
// jump to bypass address
JumpToSystemBootloader();
}
}
// call this anywhere from your application code
void jumpToBootloader()
{
// set magic tag
*pu32_boot_tag = u32_boot_tag_reenter;
// load reset vector address @+0x4
void (*JumpToSystemBootloader)(void) = (void (*)(void)) (*((uint32_t *)(u32_boot_vector_addr + 4)));
// load stack pointer address @+0x0
__set_MSP(*(uint32_t *)u32_boot_vector_addr);
// jump to address loaded previously from @+0x4
JumpToSystemBootloader();
}