Is it possible to create a bootloader in C or C++ without using some type of Assembler (and preferably without using __asm)? I'm writing an Operating System and would like it to be completely written in C and C++.
A bootloader is a piece of code which allows user application code to be updated. The new code can be obtained using alternative download channels, such as a USB stick or a network port.
As far as I know, you cannot write bootloader in C. That is because, C needs you to work in a 32-bit protected mode while in bootloader some portions are in 16-bit mode. There are C compilers that will generate 16-bit code.
A bootloader is a vendor-proprietary image responsible for bringing up the kernel on a device. It guards the device state and is responsible for initializing the Trusted Execution Environment and binding its root of trust.
A boot loader is a critical piece of software running on any system. Whenever a computing system is initially powered on, the first piece of code to be loaded and run is the boot loader. It provides an interface for the user to load an operating system and applications.
That's pretty system dependent. In most cases, the answer is going to be no - you will need to write some custom assembly to set up the C runtime before you start running your C code. There are some exceptions, however. The ARM Cortex-M0, for example, can run C code straight out of reset.
Presumably, though, you're not using an M0, so you're going to need to write some assembly. Again, it's system/chip dependent, but you might be able to get away with something as simple as:
reset_vector: mov sp, SOME_KNOWN_GOOD_STACK_ADDRESS call c_entry_point
which simply initializes the stack pointer and calls your C program's entry point. Of course, this simple a setup depends on your chip having a reset vector/vector table that supports it, RAM (or something like RAM) being initialized before the reset vector gets called, and so on. There tend to be a lot of "gotchas" in early system initialization.
Prepare to get pretty friendly with your compiler, assembler, and linker documentation - generating a flat binary that you can flash down as a first stage bootloader is often a big pain in and of itself.
Good luck!
Assuming this is for x86, you can probably get something running in 16bit mode if you have the right compiler options and manage to get the layout of your bootsector correct with the right linker magic.
But you won't get far with plain C
(or C++): you'll need to mask interrupts real fast, and there is no C
function for that. Assembly is required.
This is probably the same for most of the other architectures: C and C++ simply don't have those features built in (some compiler extensions might help you though).
A great resource for what you are attempting to do: OSDev.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With