im-tomu / foboot

Bootloader for Fomu
Apache License 2.0
99 stars 32 forks source link

Add platform abstraction layer to parameterise for different platforms #9

Open TomKeddie opened 5 years ago

TomKeddie commented 5 years ago

Need to be able to parameterise the software to allow the same s/w binary to run on different platforms.

An incomplete list would be

Some/all of this could come from a litex platform capabilities mechanism.

TomKeddie commented 5 years ago

@xobs moving conversation from https://github.com/im-tomu/foboot/pull/7 to this issue. Quoting the whole response here for continuity.

I was having a chat with @mithro, and the idea came up to use a thunked syscall interface.

The idea being that you'd have a function at a well-known address, or an address defined in a LiteX CSR, or an address defined in a RISC-V MSR, that has a function signature such as:

int syscall(uint32_t family, uint32_t syscall, uint32_t arg1, void *arg2);

...or similar. Maybe the params would be different. But we'd define family and syscall as enum pairs. For example, "reboot" might be in the "system" family. Using 32-bit IFF-style tags, such a call might be:

syscall('syst', 'boot', 0x20040000, NULL);

Because the boot ROM is currently always memory-mapped, we could fix it at address 0x100, so any machine-level program could cast that value to a function pointer and make syscalls.

Then, as part of the BIOS for various platforms, you'd implement syscall:

__attribute__((section(".text.syscall")))
int syscall(uint32_t family, uint32_t call, uint32_t arg1, void *arg2) {
    switch (family) {
        case 'syst':
            return do_syst(call, arg1, arg2);
        default:
            return -EIMPL;
    }
}

static int do_syst(uint32_t call, uint32_t arg1, void *arg2) {
    switch (family) {
        case 'boot':
            return do_reboot(arg1);
        default:
            return -EIMPL;
    }
}

Agreed, we could also define inlines/macros to make the code more readable.

syscall('syst', 'boot', 0x20040000, NULL); could become syscall_syst_boot(0x20040000);

Once we have the i/o for the bootloader defined in this manner we can take a look at defining some platform i/o for micropython. Stuff like rgb would be platform specific but we can define a very basic interface that exposes n buttons, n leds and a set of n io pins. On fomu the buttons could be touch under the hood. We'd also want to look ahead to circuitpython and make sure we can define an optional platform i2c in some manner too.