dmitrystu / sboot_stm32

Secure USB DFU1.1 bootloader for STM32
Apache License 2.0
303 stars 63 forks source link

User code in bootloader #34

Open dzarda opened 3 years ago

dzarda commented 3 years ago

What is the "correct" procedure for injecting user code into the bootloader? For example I want to display some content on the display while the device is in DFU.

One solution might be adding user_init and user_poll to the existing main:

dfu_init();
user_init(); ///
while(1) {
    usbd_poll(&dfu);
    user_poll(); ///
}

Another solution might be to allow disabling the default main (and then calling dfu/usbd from user source:

#ifndef DFU_CUSTOM_MAIN
dfu_init();
while(1) {
    usbd_poll(&dfu);
}
#endif
dmitrystu commented 3 years ago

It's up to you. It's also possible to add two weak empty functions like void user_init(void) before the main loop and void user_task(state or somewhat_to_track_progress) inside the main loop to generalize this feature.

dmitrystu commented 3 years ago

I think it's a good idea to add void user_exit(void) before a system reset.

dzarda commented 3 years ago

Yeah the weak definitions are probably the way to go.

dzarda commented 3 years ago

Do we want user_init before OR after dfu_init?

dmitrystu commented 3 years ago

I think it doesn't matter.

dzarda commented 1 year ago

I'm back at this thing again. I need to be able to perform custom user_verify() on each start (same stage as validate_checksum). Would you consider having DATA/BSS sections initialized before it? user_verify() would need access to that data.

Or more likely move the initialization directly after clock setup.

I can change it locally but I would want to opensource that feature if you're ok with it.