Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A boot loader in C++

I have messed around a few times by making a small assembly boot loader on a floppy disk and was wondering if it's possible to make a boot loader in c++ and if so where might I begin? For all I know im not sure it would even use int main().

Thanks for any help.

like image 793
nosedive25 Avatar asked Jul 17 '10 21:07

nosedive25


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 boot loader means?

A boot loader, also called a boot manager, is a small program that places the operating system (OS) of a computer into memory.

Why is a boot loader 512 bytes?

Usually, a sector is 512 bytes in size. This is known as the Master Boot Record (MBR). BIOS simply loads the contents of the MBR into memory location “0x7c00” and jumps to that location to start executing whatever code is in the MBR. Our bootloader should be 512 bytes in size as well.


3 Answers

If you're writing a boot loader, you're essentially starting from nothing: a small chunk of code is loaded into memory, and executed. You can write the majority of your boot loader in C++, but you will need to bootstrap your own C++ runtime environment first.

Assembly is really the only option for the first stage, as you need to set up a sensible environment for running anything higher-level. Doing enough to run C code is fairly straightforward -- you need:

  • code and data loaded in the right place;
  • there may be an additional part of the data area which must be zero-initialised;
  • you need to point the stack pointer at a suitable area of memory for the stack.

Then you can jump into the code at an appropriate point (e.g. main()) and expect that the basic language features will work. (It's possible that any features of the standard library that may have been implemented or linked in might require additional initialisation at this stage.)

Getting a suitable environment going for C++ requires more effort, as it needs more initialisation here, and also has core language features which require runtime support (again, this is before considering library features). These include:

  • running static constructors;
  • memory allocation to support new and delete;
  • support for run-time type information (RTTI);
  • support for exceptions;
  • probably some other things I've forgotten to mention.

None of these are required until the C environment is up and running, so the code that handles these can be written in C rather than assembler (or even in a subset of C++ that does not make use of the above features).

(The same principles apply in embedded systems, and it's not uncommon for such systems to make use of C++, but only in a limited way -- e.g. no exceptions and/or RTTI because the runtime support isn't implemented.)

like image 181
Matthew Slattery Avatar answered Sep 24 '22 00:09

Matthew Slattery


It's been a while since I played with writing bootloaders, so I'm going off memory.

For an x86 bootloader, you need to have a C++ compiler that can emit x86 assembly, or, at the very least, you need to write your own preamble in 16-bit assembly that will put the CPU into 32-bit protected (or 64-bit long) mode, before you can call your C++ functions.

Once you've done that, though, you should be able to make use of most, if not all, of C++'s language features, so long as you stay away from things that require an underlying libc. But statically link everything without the CRT and you're golden.

like image 30
greyfade Avatar answered Sep 24 '22 00:09

greyfade


Bootloaders don't have "int main()"s, unless you write assembly code to call it. If you are writing a stage 1 bootloader, then it is seriously discouraged.

Otherwise, the osdev.org has great documentation on the topic.
While it is probably possible to make a bootloader in C++, remember not to link your code to any dynamic libraries, and remember that just because it is C++, that doesn't mean you can/should use the STL, etc.

like image 22
luiscubal Avatar answered Sep 25 '22 00:09

luiscubal