AbdooOS - Episode 1: The Start
AbdooOS : An adventure in discovering OS Development
Table of contents
If you live under a rock, an OS is like a big application that runs and controls everything on your computer. For most people, their OS is either Windows or MacOS, and for some other it's Linux.
The idea
Make a basic OS that supports self-coded commands. It will be a terminal-like OS but will have a GUI in the future (if I don't lose motivation). It'd be cool if it was like really small. Imagine an OS smaller than 20Mb! (There are ISOs even smaller than 2Mb!).
So for short: I want an OS that's terminal-like, able to run basic commands. Lightweight and optimized.
How to do it?
Well, it's known that Linux is great for developing stuff, so this will be our environment. Because we need a Unix-like environment and I don't wanna use WSL or a VM, I will use Cygwin. Why use Linux? Because it provides a lot and I mean a LOT of useful commands and tools (like for compilation). But how will it actually boot?
How to Boot
Our computers have a little piece of software already in them called "The BIOS" which stands for Basic Input Output System. And this little guy will go look for an OS to run.
There are two booting ways, but we'll use the "Legacy Boot" instead of its modern version "UEFI" because it's easier and the most "from scratch". The BIOS (Well not the BIOS but the computer) will go take the first 512 bytes it can load, then it will look for a boot signature. What is that you ask? Imagine signing a contract to confirm your agreement or to symbolize the terms you've accepted.. Well, this boot signature whose Hexadecimal value is "0xAA55"*(It's actually "AA55" but we add that '0x' to say that it is a hex value)* tells the BIOS that when it finds that signature it means: "Yo! I just found an OS!".
So now, how will we actually do it? First, we gotta get a compiler for assembly and a VM to test our OS. I'll use nasm
and qemu
.
Why use Assembly? Because it's the most "low-level" programming language that humans (us) can read. And an OS is extremely important for a computer and its systems, so we gotta control each bit we modify, add, remove etc... But later, we'll do the transition to a more "high-level" programming language which is C.
Let's start!
So first, to have a basic start, we'll make a file called boot.asm
, then inside of it we'll write:
times 510-($-$$) db 0
dw 0AA55h
Now, what does this do? First I'll give you a basic explanation of how to code in Assembly. Assembly's basic pattern is <instruction> <parameters>
. In this case, the times
instruction says to repeat this thing n time (this n in our case is that little calculation 510-($-$$)
). For us, it's to fill the boot sector with 0s. Why? Look closely, our boot sector should be 512 bytes, why repeat 510 times then? Because we still have the boot signature we have to write (by the way, putting a h
after a hex value is the same as putting a0x
before it), and this signature is 2 bytes long. And when we do $-$$
it's to count how big our boot sector is. $ means the current location of memory when we're executing the program, $$ means the location of the start of the program. That means we're calculating the program's size, and this to fill what's left with zeros. So for short: we calculate our program's size, then we fill what's left of the boot sector (what's empty) with 0s.
Finally, we write that boot signature with the dw instruction. Now if you try running this command:
nasm boot.asm -f bin -o OS.bin
You'll find yourself with a new file called OS.bin, this is your OS. Now, try running it with Qemu using this command:
qemu-system-i386 OS.bin
If it tells you that it is booting from the disk without saying anything else, then congrats! Because you just coded the very, very beginning of your new OS!
Like NanoByte said: "For now, our OS does nothing, and it does it perfectly".
What's next?
This is it for now, I'll post episode 2 later or idk when. OS Development is a topic I recently got VERY interested in! And I'm still learning and doing quirky stuff. See ya!
References
Birmingham Unversity's (for computer science in UK) os-dev book by Nick Blundell.
Daedalus Community's x86 OS Development playlist.