Small USB HID bootloader for BluePill and other STM32F10X devices

Generic boards that are not Maple or Maple mini clones, and don't contain the additional USB reset hardware
User avatar
RogerClark
Posts: 8416
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Small USB HID bootloader for BluePill and other STM32F10X devices

Post by RogerClark » Mon Feb 04, 2019 9:04 pm

BennehBoy wrote:
Mon Feb 04, 2019 8:26 pm
Just what's the point of binary compatibility? Each bootloader has a different size so won't there be linker issues? Educate me.
I'm not sure if anyone replied to this question...

When the high memory HID bootloader is flashed to the MCU, one part is installed in the base of Flash and the main body of the bootloader is at the top of flash.

The part of the bootloader that is in the base of Flash is only the vector table (only things like the Stack Pointer and Reset Vector etc, probably some of the ISR's needed for USB and possibly for systick etc)

When the MCU boots it always reads the base of Flash to get the SP and the start address of the application code.
So the execution then runs the main body of the bootloader which is in the top of flash


The trick is... When a normal application binary is uploaded to the high memory HID bootloader, it overwrites the SP and start vectors, with its own SP and start vector.



But if you upload the same binary via Serial UART or ST-Link the SP etc vectors won't get updated, so the high memory bootloader will be orphaned, as it will never get run again

User avatar
RogerClark
Posts: 8416
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Small USB HID bootloader for BluePill and other STM32F10X devices

Post by RogerClark » Mon Feb 04, 2019 9:07 pm

Squonk42 wrote:
Mon Feb 04, 2019 8:49 pm
RogerClark wrote:
Mon Feb 04, 2019 8:35 pm
And with reference to Vassilis comment about people knowing that they will effectively render the HID bootloader useless if they upload via STLINK or Serial...
If they have STLINK or Serial programming skills, then they probably don't need the HID bootloader, or at least they are able to re-enable it if needed by reflashing it with these tools, no need to explain much.
Isn't this the conceptual problem.

Anyone using the HID bootloader may not have the skillset to understand that just because they can upload the same application code via serial or ST-Link , and it will run once... They can't then switch back to the bootloader, without re-installing it

User avatar
Squonk42
Posts: 551
Joined: Thu Dec 29, 2016 9:25 am
Location: Bordeaux, France
Contact:

Re: Small USB HID bootloader for BluePill and other STM32F10X devices

Post by Squonk42 » Mon Feb 04, 2019 9:17 pm

Thank you Roger, very good and clear explanation!

In the particular HID high-memory implementation, no additional ISR for USB (I used polling) or Systick is required, so only the initial Stack Pointer and Reset Vector are required.

And as the user application will have its stack pointer initialized to a valid RAM location and that we can use it before it is launched and then free it back when the BL leaves, we just have to hijack the Reset Vector :mrgreen:

The original Reset Vector is thus copied into a "reserved" (unused) ISR vector of the (low-memory) user application and the BL (high-memory) entry point is installed instead. Upon exit, the BL just reset the Stack Pointer to the original one and jumps to the (low-memory) user application entry point.

This hijacking is performed on the fly by the BL when flashing the user application, but of course, if you use an external mean (STLINK or Serial), this won't happen and the HID bootloader will not be called.

However, it is just a matter of reinstalling the Reset Vector to point to the BL entry point.

User avatar
Squonk42
Posts: 551
Joined: Thu Dec 29, 2016 9:25 am
Location: Bordeaux, France
Contact:

Re: Small USB HID bootloader for BluePill and other STM32F10X devices

Post by Squonk42 » Mon Feb 04, 2019 9:24 pm

RogerClark wrote:
Mon Feb 04, 2019 9:07 pm
Isn't this the conceptual problem.

Anyone using the HID bootloader may not have the skillset to understand that just because they can upload the same application code via serial or ST-Link , and it will run once... They can't then switch back to the bootloader, without re-installing it
I don't think so: people that will install the HID BL will probably use its convenience (simply attach a standard USB cable, no driver required) all the time once it is installed by STLINK/Serial and not go back and forth between bootloaders.

And if they do, they will be able to re-install it as they already did it and have all the tools to do it again, they won't brick their device.

User avatar
RogerClark
Posts: 8416
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Small USB HID bootloader for BluePill and other STM32F10X devices

Post by RogerClark » Mon Feb 04, 2019 10:02 pm

I like the concept of the high memory bootloader loader. But I'm not sure whether the advantages are more than the disadvantages

Please update as necessary... ;-)

Advantages
  • Has the same linker etc configuration as Serial and ST-Link uploads???
Disadvantages
  • Currently would conflict with the EEPROM library
  • May confuse users into thinking that they can upload via ST-Link or Serial and return to bootloader operation without reflashing the bootloader
No change
  • Still requires its own configuration lines in both boards.txt and platform.txt
  • Still needs its own upload tools

User avatar
Squonk42
Posts: 551
Joined: Thu Dec 29, 2016 9:25 am
Location: Bordeaux, France
Contact:

Re: Small USB HID bootloader for BluePill and other STM32F10X devices

Post by Squonk42 » Mon Feb 04, 2019 10:20 pm

Advantages
  • Has the same linker etc configuration as Serial and ST-Link uploads???
Yes, but this can also be achieved by using linker script pre-processing, such that all BL-specific settings are defined in boards.txt and platform.txt, and a common set of linker scripts is used for all bootloaders, or even a single monolithic linker script is used, as for the official STM Core.
  • Binary user application compatibility, provided that it fits into memory

Disadvantages
  • Currently would conflict with the EEPROM library (could be fixed by using PROVIDEs() in the linker scripts to defined the Flash region instead of hard-coded values in source)
  • May confuse users into thinking that they can upload via ST-Link or Serial and return to bootloader operation without reflashing the bootloader (given its advantages, once they flashed the HID BL, users will probably never go back to flashing with ST-Link or Serial)
No change
  • Still requires its own configuration lines in both boards.txt and platform.txt
  • Still needs its own upload tools
  • Still does not require a specific driver

User avatar
RogerClark
Posts: 8416
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Small USB HID bootloader for BluePill and other STM32F10X devices

Post by RogerClark » Tue Feb 05, 2019 2:21 am

Re: Still does not require a specific driver

I was thinking of advantages / disadvantages of moving to high memory, rather than using a HID vs a DFU bootloader, so I don't think the driver is relevant

User avatar
Squonk42
Posts: 551
Joined: Thu Dec 29, 2016 9:25 am
Location: Bordeaux, France
Contact:

Re: Small USB HID bootloader for BluePill and other STM32F10X devices

Post by Squonk42 » Tue Feb 05, 2019 5:43 am

Squonk42 wrote:
Mon Feb 04, 2019 10:20 pm
  • May confuse users into thinking that they can upload via ST-Link or Serial and return to bootloader operation without reflashing the bootloader (given its advantages, once they flashed the HID BL, users will probably never go back to flashing with ST-Link or Serial)
BTW, this is the same problem with low-memory bootloaders: if you flash a firmware using either ST-Link or serial with a corresponding memory layout that is not aware of them, they will be removed too, so the bootloader low-memory vs. high-memory debate is not relevant for this point.

The only valid concerns seem to be the non-linear Flash page size for the F4 and the EEPROM library clash, that would be better solved by introducing linker script memory layout symbols rather than hard-coding it into the library.

User avatar
Vassilis
Posts: 480
Joined: Thu May 21, 2015 6:42 am
Location: Thessaloniki, Greece
Contact:

Re: Small USB HID bootloader for BluePill and other STM32F10X devices

Post by Vassilis » Tue Feb 05, 2019 10:33 am

Squonk42 wrote:
Mon Feb 04, 2019 8:25 pm
Vassilis wrote:
Mon Feb 04, 2019 4:24 pm
In my opinion, binary hardware compatibility is more important.
OK, so if I understand you well, you are for having the maximum binary compatibility for sketches .bin among the different bootloaders?
By saying "binary hardware compatibility" I mean one binary file (produced by a sketch) that can be flashed by using different type of upload hardware just like ST-Link, Serial adapter etc. Did you mean something else ?
Squonk42 wrote:
Mon Feb 04, 2019 8:25 pm
The F4 HID upload strategy will be left as-is at the 16 Kbyte low-memory sector.
Last edited by Vassilis on Tue Feb 05, 2019 1:17 pm, edited 1 time in total.

User avatar
Squonk42
Posts: 551
Joined: Thu Dec 29, 2016 9:25 am
Location: Bordeaux, France
Contact:

Re: Small USB HID bootloader for BluePill and other STM32F10X devices

Post by Squonk42 » Tue Feb 05, 2019 12:51 pm

OK, great, now I think the only drawback is the EEPROM library clash with high-memory.

Post Reply