0% found this document useful (0 votes)
18 views5 pages

How To Boot An Os Completely Written in C++

The document discusses a user's query about creating a bootloader for a simple operating system written in C++. The user seeks guidance on using GRUB and maintaining C++ libraries and filesystem support while executing commands. The responses highlight the complexities of OS development, the need for inline assembly, and the limitations of using C++ in a freestanding environment.

Uploaded by

djoleusa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

How To Boot An Os Completely Written in C++

The document discusses a user's query about creating a bootloader for a simple operating system written in C++. The user seeks guidance on using GRUB and maintaining C++ libraries and filesystem support while executing commands. The responses highlight the complexities of OS development, the need for inline assembly, and the limitations of using C++ in a freestanding environment.

Uploaded by

djoleusa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

How to boot an os completely written in C+

+? [closed]
Asked 6 years, 1 month ago
Modified 6 years, 1 month ago
Viewed 2k times
1
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by
editing this post.

Closed 6 years ago.

I wrote the basis for a simple OS in C++ and have had no luck with researching how to write a
simple bootloader for this OS to possibly use with GRUB or something similar to make writing
the bootloader a bit easier. Take a look at it's GitHub repository to see what code I'm trying to
boot.

The OS requires some very simple things to run and be fully functional. It needs to be able to use
the standard C++ libraries (such as iostream, fstream, string.h, and possibly iomanip). It
also needs to have any sort filesystem (NTFS, FAT, FAT32, Ext4, etc). And, most importantly, it
needs a way to execute other executables (through system() or any safer methods).

Currently it can run executables (called "commands") from a hardcoded directory, run an
executable before fully loading the kernel (called "kautorun"), and can be compiled and run over
top of Win32, and GNU/Linux.

Preferably, I want to be able to make writing the bootloader easy and I'm thinking using GRUB
and some special compiler commands would do it. I'm just not sure how to approach this. What
can I do to get this working? And, am I approaching this from the wrong angle?

EDIT: To narrow it down a bit, I need a bootloader to run these executables, preserve the C++
libraries I used, and keep the directory structure intact. Hopefully that narrows it down enough
and takes this question out of hold for being too broad.

 c++
 operating-system
 boot

Share
Improve this question
Follow
edited Feb 10, 2019 at 17:51
asked Feb 10, 2019 at 16:19

Benjamin Sykes
2311 silver badge66 bronze badges

 3

Standard C++ libraries + filesystem + even just being able to run processes =/= "very
simple thing". You should give the osdev wiki a look, because you think you have access
to much more things than you really have.

– asu

Commented Feb 10, 2019 at 16:24


 1

Did grub progress enough to relive the OS from the need of going from 16 bit real-mode
to 32/64 bit protected mode? You need to write that part in assembly

– Michael Veksler

Commented Feb 10, 2019 at 16:32

 @MichaelVeksler, actually the GRUB does do it for you, you will only need a few lines
of inline-assembly to set up the stack and get the information passed from GRUB.

– r3mus n0x

Commented Feb 10, 2019 at 16:34

 @r3musn0x is that true? If it is, how would you implement it, because that sounds
awesome!

– Benjamin Sykes

Commented Feb 10, 2019 at 16:35

 That looks like a fun project, Ben. Very ambitious. I think that you are right: focus on
GRUB, but also attend to the suggestion of @MichaelVeksler: the x86 16-bit real mode
might need some attention from you because of the layered procedure by which x86
boots. It's complicated, and is more than I know. Good luck.
– thb

Commented Feb 10, 2019 at 16:35

Show 9 more comments

2 Answers
Sorted by:
1

Honestly, your code has nothing to do with writing an OS. It's just a console application capable
of running a few commands and it's not even close to a shell implementation.

To get a better idea of what it takes to write an OS, I recommend you to read at least a few of the
beginner articles at OSDev Wiki. You should also take a look here to see which C++ standard
headers are available in a freestanding environment (that is an environment without OS). You
will notice that there are no file system or I/O headers there, and no system() function because
there is no shell to run it.

Basically, all this means that developing your own OS is to implement all of this functionality on
your own: memory management, multi-tasking, I/O, etc... Also you do need a bootloader to boot
your OS and you can use GRUB to make thing easier for you, however the bootloader won't
provide you with any of those other things (processes, I/O, etc...), the only thing it does is
transfer control to your code and after that you're on your own.

Regarding using C++ for OS development, yes, you can write most of the OS using C++ but
you will still have to at least use inline-assembly for a hardware specific tasks that just can't be
expressed using C++, such as: port I/O for communication with hardware, loading special
processor registers (control registers, model-specific registers), loading global-descriptor table
(which describes memory segments), loading interrupt-descriptor table (which sets handler for
hardware-interrupts) and maybe more...

Share
Improve this answer
Follow
edited Feb 10, 2019 at 16:52
answered Feb 10, 2019 at 16:46

r3mus n0x
6,15411 gold badge1717 silver badges3737 bronze badges
 I don't think so. I for one have never set up process no. 1 and gotten it to run on the bare
processor, nor have I ever worked directly from GRUB without a kernel. Maybe you
have done, but this is a cool project even if it never gets as far as switching between
tasks. The OSDev Wiki might be a good resource, though.

– thb

Commented Feb 10, 2019 at 16:52


 @thb, basically the code that the OP provided is just a beginning of the shell
implementation and while shell is an important concept in a Unix-like OS, it is, however,
just a user-mode program that relies on already existing OS kernel and just provides it's
functionality (creating processes, redirecting I/O, etc...) to the user. BTW it doesn't make
sense to use system() in a shell implementation because it basically means that it relies
on already existing shell.

– r3mus n0x

Commented Feb 10, 2019 at 17:02

Add a comment
0

To write a booting OS takes a lot of work. I would recommend using something like IncludeOS
which will allow you to write a single "application" which will boot.

Writing a boot loader, you will need to know the hardware you are booting on. For PC's there are
two main ones.

 BIOS
 UEFI

Since your talking about GRUB then most likely talking about a BIOS machine.

A BIOS machine boots into 16bit x86 mode with BIOS services that you can use. It boots by
loading the first sector of the master HD into address "0x0000:0x7C00" and then jumps to it.

This first sector is "normally" a MBR with a partition table. One is marked as the "boot" partition
(or you get some sort of "menu" boot). The MBR code then loads the first sector of the booting
partition into address "0x0000:0x7C00" and then jumps to it.

The first sector of a partition is normally called a BPB.

The BPB is normally a mixture of data about the partition type (FAT,NTFS,EXT,etc) and code
to start the OS boot. Since it's only 1 sector that is loaded you are very limited for code space. So
this is written in assembler only, this code uses the BIOS services to find the "real" boot code for
the OS within the filesystem and load it into memory and jumps to it.

UEFI is similar to BIOS but provides a complete booting environment where you write a EUFI
application and the boot menu configuration runs this application to boot your OS. This
application can be written in C++ and you use the UEFI services (which include a lot more
services than BIOS as you get API's for network and graphics for example). This boot code
normally uses the boot configuration to load the initial OS booting code from the partition and
jump to it (and also turn off UEFI services as well as they are not normally used from some point
onwards).

From this point what the OS booting code does highly depends on the OS that is booting. They
normally:

 Switches to protected mode and sets up a protection ring environment


 Loads "drivers" to handle specific hardware (PC legacy HW and PNP HW)

IncludeOS makes it easier by providing a very simplified versions of the above into a simple
library.

Share
Improve this answer
Follow
edited Feb 10, 2019 at 17:17
answered Feb 10, 2019 at 17:01

Shane Powell
14.2k22 gold badges5252 silver badges64

You might also like