Write Your Own Operating System(part 1)

Hiransanjeewaa
6 min readOct 2, 2022

Developing an small operating system to display Hardware Information in the computer by using c language

Nowadays many people don’t even know they using os in computers and mobile phones. People forget about it because modern os programs have very good graphical friendly interfaces and very few bugs. There are few os systems that using worldwide very commonly. Obviously, Windows is the most famous operating system in the world.

76% of users in the world use windows as their operating system. And there are other few famous operating systems as well. such as Mac os, Linux, Ubuntu, Unix etc. If someone need to change their operating system structure or edit their operating system these systems don’t allow to do it. There are few people who think about building their own os. This blog will be an encouragement for them as well.

First if all I must tell you Building our own os is not a easy thing to do. Just look at how much codes windows have it has 50 million code lines. But if you look at how they started , they started with very simple components. It took years to stand what they are right now. But now there are too much knowledge you can get from internet , Projects from experts as well. So, start small and day by day you can get better.

How OS gets control of the computer

As we all know operating systems load into main memory at start of our computer. But there is a procedure before computer gives control to os. First BIOS(Basic input output system) get control of the computer and it check input output devices availability , reading keyboard inputs, provide interface for booting setup as well. Secondly BIOS handover control to Bootloader. Bootloader is the one who handover the control to Operating System. There is two main components in the bootloader . First one gets control from bios and givs to second component and second one gives control to Operating system.

BIOS and BOOTLOADER

When Developing an Operating system BIOS and Bootloader must be developed before . But writing BIOS and Bootloader need high experience in assembly language and hardware components. Therefore in our case we use a existing bootloader called ” GNU GRAND Unified Bootloader (GRUB)”. By using this bootloader we can develop our os as a ordinary ELF executable file. GURB will loaded our os into correct memory locations in memory. GURB will transfer control to OS but first it will check our os is capable for controlling.

Setting up working environment for os building

Install Ubuntu

Before start developing our os we need to make the environment for our operating system implementation. I m using Ubuntu as my operating system for building my own. Ubuntu allows you to run our os physically and virtually. After installing Ubuntu I need you to run this sudo code in Ubuntu terminal. This will install packages we need for our os creation

Programming language

I’m using C language for my Os creation. You also can use C++ and Python But we need have better control in our code and we also can have direct access to our memory in c language. But definitely we can use other laguages as well.

__attribute__((packed))

Because of this attribute code might be easy to compile with GCC rather than having a compiler. we use bash as our scripting language. We can compile our codes by using ubuntu.

Virtual Machine

Every program need to be tested before using it’s very important. Belive me , in os we need some amount of attempts to make it done. We can use physical computer if we want to but it will take too much time to transferring data and between computers and it will take more effort. Easiest thing is we can use virtual machine for testing. In this tutorial I will use the emulator called Bochs. Bochs has useful debugging features for os developing. Also virtual box and QEMU can use as virtual machines.We cannot come to conclusion that our os work well if it run correctly in virtual box we need to make sure of it by running it physically in a computer by using floppy disk, pendrive or CD.

Compiling Our Operating System

We need to write our os in assembly language because c need a stack.You need to copy below codes and paste in the text-editor in ubuntu.save the file as loader.s. Os write the number “0xCAFEBABE” in eax register.

Now this file can be compile in ubuntu into a 32 bit elf object file. Enter below command in terminal to make it happen.

Lets link our Kernel

Now we need to make our file executable ,we need GRUB to load into kernel with a memory address larger than one mb because below 1 mb are using by GRUB BIOS memory is mapped I/O therefore we need a linker script to load into kernel.

This is the linking script copy and paste in text editor and save it as link.ld the execute below command in terminal

The executable can now be linked with the below command.

You will now see the final executable kernel.elf in the folder.

Obtaining GRUB

We are using GRUB legacy version.OS iso img can be created by using GRUB legacy and GRUB 2. So we using tage2_eltorito bootloader this file can be downloaded from below link

https://github.com/whitequark/story-os/blob/master/grub/stage2_eltorito

Copy and paste this file into your working folder which contains loader.s and link.ld.

Building ISO Image

Executable elf file can’t be loaded into virtual machines directly. We need to make a file that can be run in virtual machine(media). Also without doing it you can use floppy image to load your kernel in virtual machine. I will create the kernel ISO image with the program genisoimage. A folder must first be created that contains the files that will be on the ISO image.Enter below three commands in ubuntu terminal and execute it will create folder and copy files to required locations.

mkdir -p iso/boot/grub              # create the folder structure
cp stage2_eltorito iso/boot/grub/ # copy the bootloader
cp kernel.elf iso/boot/ # copy the kernel

Go to iso/boot/grub/ and create a configuration file menu.lst for GRUB. This file tells GRUB where the kernel is located and configures some options.Now our iso folder must have below sequence.

iso
|-- boot
|-- grub
| |-- menu.lst
| |-- stage2_eltorito
|-- kernel.elf

Testing/Running Our Operating System

We can run our new os in bochs emulator with os.iso image. Bochs need be configured at the beginning configure it according to below configuration. You might need to change path to romimage and vgaromimage due to your computer. If you have the file named bochsrc.txt as configuration file for bochs you can run bochs by using below command.

Now you can run bochs from command down.

bochs -f bochsrc.txt -q

Now should now see Bochs starting and displaying a console with some information from GRUB on it.

After quiting bochs you can see log created by bochs.

Now you can see the information of the registers of the CPU simulated by Bochs somewhere in the output. If you find RAX=00000000CAFEBABE or EAX=CAFEBABE in the output then It means our target is achieved. Our OS is now working perfectly.

--

--

Hiransanjeewaa

Software Engineering Undergraduate — University of Kelaniya