OS from SCRATCH

Hello guys!
This time I will talk to you about this project: OsdevTraining
This is an OS constructed from scratch for i686 arch. THANKS to wiki.osdev.org for initial code and explanations!
This code is not optimized and it hasn’t a good structure because my goal is to share with you how to boot an OS from scratch, not how to write well structured and optimized code. Feel free to use my code and try to improve it!
I can’t explain everything about operating systems, I would like to inspire new research.

Let’s go.
1-Requirements.
I worked on Debian 9 and used this software:

To check your program version use: program –version
I installed them with apt packet manager.

2- We need these programs to build our cross compiler for i686-elf because we must program on our host machine, but we need different binary. In my case I work from x86-64 arch so elf-64 is not good for i686 arch: i686-elf is the answer.
Let’s setup our environment (folders creation + env setup):

Now we have our workspace into path for this terminal session (You don’t need to specify the path of each binary for this terminal session).

Download and install binutils in our workspace for i686-elf:

The results will be in $HOME/OsDev/opt/cross/bin folder.

Download and install GCC in our workspace for i686-elf:

The results will be in $HOME/OsDev/opt/cross/bin folder.
We have our cross-compiler!

3- Use my repo. PLEASE try to understand my code, I appreciate that you read and test the article, but I will be happy if you try to understand/improve my code or write your own 🙂 wiki.osdev.org will give you A LOT of help!
Clone my repo:

There are 3 important files:
1- boot.s
2- kernel.c
3- linker.ld

1) To boot into any OS we need bootloader (we will setup and use GRUB next).
How bootloader starts the kernel? Thanks to a “Multiboot Standard” we only need to put some values in some global variables: multiboot header, the bootloader will search for them and then it will know how to boot our OS.
We must write bootstrap in assembly code because there is no stack yet and asm can work without (we use asm to setup stack too).

boot.s is our bootstrap, let’s assemble it with our new binaries:

2) Now we have the setup to use higher level language: C (you can use some other).
Normal C programming is in user space, also called Hosted Environment: you have a lot of useful library!!
But we must use Freestanding environment so there isn’t standard libraries:
We are alone with our code, our logic and our creativity. Keep it in mind!
Initial code has only simple, but good, vga text mode and an hello world.
I implemented some nice and small things, for example input from keyboard, but I will explain it next.

Now compile the kernel!

3)We can now link the boot.o and kernel.o files to create our final kernel.
There aren’t script for linking in kernel-development so we need our linker.ld.
Let’s link:

To verify if myos.bin has multiboot type:

..if it’s quite, it’s OK.

Now we create the bootable image with grub.cfg and myos.bin:
(grub.cfg is a simple file with the entry point OS)

Now we have our myos.iso ! For testing:

In my code there is a loop for command execution.
You can use these commands:
1- help
2- echo
3- clear
4- shutdown

I explain my code by comments in code, there are the VERY GOOD comments by wiki.osdev.org that I left in the code (without this site I can’t do this, another THANKS)
As you can see I wrote some basic functions because in freestanding environment there is nothing!

Testing on real hardware:

I hope that this article will be appreciated by you. Perhaps in the future, when I have more time, I could develop as OS for x86-64 arch..but for now that’s all. Bye bye ! 🙂




Leave a comment