Position-independent Chibios

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

angainor
Posts: 11
Joined: Mon Dec 20, 2021 6:43 pm
Been thanked: 2 times

Position-independent Chibios

Postby angainor » Wed Dec 29, 2021 6:28 pm

Hi!

I am trying to understand what would be needed to compile and run a relocatable Chibios application. A standard way is to compile my app for base address 0x8000000 on my STM32F4, flash it, and it runs. What I would like to do is have a few such complete Chibios applications in different (arbitrary) places on the flash, and have a simple bootloader execute those applications. One purpose of this is to perform firmware upgrades.

What I have done til now is I compiled my Chibios application for a different base address (say, 0x8010000), and I wrote a trivial bootloader that would jump to the correct place using

Code: Select all

msr msp, <...>
bx <...>


This works of course, but is not ideal, because I have to set the base address during compile time. I would rather compile my Chibios app with -fPIC and be able to flash it at an arbitrary address.

Could anyone comment whether this is feasible at all with Chibios, and what would be required? I guess my bootloader would have to do some smart loading, update the GOT? One thing that makes me wonder is what to do with the vectors? I guess I would have to load the vectors into RAM and update them?

I'd appreciate any comments.

Thanks a lot!

Marcin

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Position-independent Chibios

Postby Giovanni » Wed Dec 29, 2021 6:40 pm

Hi,

This position independent code is purely a compiler matter, I never tried that. There are some things that are absolute, for example ISR vectors in the vectors table, PIC code would not help there.

An alternative approach is to write an elf file loader and relocate the code on loading, there are some solutions searching on the web. I will write a loader in the next months, it is planned to become part of the sandbox subsystem, you may just wait for that depending on your time constraints. It will allow to "load" code at runtime from a file system into a sandbox and execute it.

Giovanni

angainor
Posts: 11
Joined: Mon Dec 20, 2021 6:43 pm
Been thanked: 2 times

Re: Position-independent Chibios

Postby angainor » Wed Dec 29, 2021 7:34 pm

Yes, I was expecting that in addition to the PIC stuff I need to do something with the interrupt handlers. But would it not be possible to place the interrupt vectors in RAM and update them in the bootloader? Assuming the bootloader knows the offset of the application, it could make such a table.

I am in no hurry - this is a research/hobby project, so I can surely wait.

The sandbox solution you mentioned - I guess the loaded and executed applications would use the existing OS instance? Then the question would remain how to upgrade that OS part. I guess I can always organize the firmware into two independent parts (bootloader/application), and update either one, or the other. So update the application running the bootloader, and vice-versa.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Position-independent Chibios

Postby Giovanni » Wed Dec 29, 2021 8:00 pm

The model is one normal ChibiOS application which runs multiple sub-applications in sandboxes. Those sub applications can be loaded dynamically in RAM or be statically loaded in flash at fixed positions.

If you want to update the main application then you still need a bootloader, but it is a single application, no need for PIC.

Giovanni


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 7 guests