Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootloader in C/C++?

Tags:

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++.

like image 390
Joshua Vega Avatar asked Jul 24 '11 17:07

Joshua Vega


People also ask

What is bootloader in 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.

Can bootloader be written in C?

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.

What bootloader means?

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.

What is in bootloader?

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.


2 Answers

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!

like image 159
Carl Norum Avatar answered Oct 18 '22 10:10

Carl Norum


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.

like image 20
Mat Avatar answered Oct 18 '22 12:10

Mat