Create Your Own Operating System

Malshani Dahanayaka
7 min readJul 15, 2021

PART 01 — Set up the development environment and booting an operating system.

The operating system is what connects the user and the hardware. But its process is somewhat complicated. In this article, I will give you an idea about how the operating system works. Also, this article will help you set up your development environment and booting a very small (and primitive) operating system.

“Learn a lot about the operating system, maybe you can build your own operating system worth millions of dollars”

I hope to give you a good and clear idea about the operating system before implementing it. So, I will go through following topics:

  • What is an Operating system?
  • Booting process of OS.
  • What is the BIOS and its usage?
  • What is the Bootloader and what it does?
  • Main memory and processor.
  • How to implement simple OS.

What is an Operating system?

An Operating System is a software that acts as an interface between computer hardware components and the user. Every computer system must have at least one operating system to run other programs. That’s why, applications like Browsers, MS Office, Notepad, Games, etc., need some environment to run and perform their tasks.

The OS helps us to communicate with the computer without knowing how to speak computer’s language.

The operating system hides the internal complexity of the hardware from the user and gives an abstract interface. There two types of interfaces( Command Line Interface (CLI) and Graphical User Interface(GUI) ).

OS Goals :

  • Execute user programs
  • Make the computer system convenient
  • Use hardware efficiently

Booting process of OS

Booting is the process of restarting a computer or its operating system software. It starts with switching the computer and ends when the operating system loads into the main memory and the computer is ready to receive commands from the user. Types of computer booting are Cold Booting And Warm Booting.

  • Cold Boot: when the user switches on a computer after it has been power off completely.
  • Warm Boot: when the user restarts the computer.

What is the BIOS and its usage?

Short for Basic Input/Output System, the BIOS is a ROM chip found on motherboards that allows you to access and set up your computer system at the most basic level. The BIOS contains instructions on how to load basic computer hardware. It also includes a test called POST (Power-on Self-Test) which helps to verify that the computer meets the requirements to start properly.

What is the Bootloader and what it does?

A bootloader, also known as a boot program or bootstrap loader, is special OS software that loads into the working memory of a computer after start-up. For this purpose, immediately after a device starts, a bootloader is generally launched by a bootable medium like a hard drive, a CD/DVD, or a USB Pendrive. The boot medium receives information from the computer’s firmware (e.g. BIOS) about where the bootloader is. The whole process is also described as “booting”.

Main memory and processor

Under this topic, I am not going to discuss CPU or RAM in depth. but I try to explain how to work CPU registers and RAM.

CPU registers

In computer architecture, the CPU document plays a major role as a small data storage place or memory and is an integral part of the processor. It is primarily the very fast memory of the computer used to run programs and other major operations fairly efficiently.

So basically a register will perform the following operations:

  • Fetch: To fetch the instructions of the user also the instructions that are present in the main memory in a sorted way.
  • Decode: The second operation is to decode the instructions that need to perform. Thus CPU will be knowing what are the instructions.
  • Execute: Once the instructions are decoded then execute operation is performed by the CPU. Once done the result is presented on the user screen.

Main memory

RAM holds information that the processor is likely to need in pretty short order. In the instance of booting, this would be Operating System (OS) files from the hard drive. The hard drive is slow compared to RAM(especially older mechanical/magnetic hard drives, less true of solid-state drives, but still true). If the processor had to rely on hard-drive speeds for accessing everything, the system would grind to a halt. So, the processor loads things from the hard drive into RAM, where it can get to it quickly and use it. It takes a while to boot up, but once those files are in RAM, it can zip along at whatever speed it’s capable of going.

How to implement simple Booting OS step by step

1) Setup computer environment to implement sample OS

Once Ubuntu is installed, either physical or virtual, the following packages should be installed using your terminal:

sudo apt-get install build-essential nasm genisoimage bochs bochs-sdl

2) Compiling the Operating System

In here, this part of the OS has to be written in assembly code. Create loader.s file in selected file location and compiled into a 32 bits ELF [18] object file with the following command:

Use following command to compile the file loader.s into a 32 bits ELF [18] object file :

nasm -f elf32 loader.s

3) Linking the Kernel

The code must now be linked to produce an executable file, which requires some extra thought compared to when linking most programs. You want GRUB to load the kernel at a memory address larger than or equal to 0x00100000 (1 megabyte (MB)), because addresses lower than 1 MB is used by GRUB itself, BIOS, and memory-mapped I/O.

Save the above linker script into a file called link.ld. The executable can now be linked with the following command:

ld -T link.ld -melf_i386 loader.o -o kernel.elf

Then you can see that, the final executable will be created is called kernel.elf.

4) Obtaining GRUB and create a configuration file for GRUB must be created.

The GRUB version we will use is GRUB Legacy since the OS ISO image can then be generated on systems using both GRUB Legacy and GRUB 2. More specifically, the GRUB Legacy stage2_eltorito bootloader will be used.

download link: https://github.com/littleosbook/littleosbook/blob/master/files/stage2_eltorito

After downloading, copy the file stage2_eltorito to the folder that already contains loader.s and link.ld.

5) Building an ISO Image

The executable must be placed on a media that can be loaded by a virtual or physical machine. Here I will use ISO [22] image files as the media, but you can also use floppy images, depending on what the virtual or physical machine supports.

Now 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. Then use following commands to create the folder and copy the files to their correct places:

 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

Then menu.lst file:

make menu.lst file using below codes.

Place the file menu.lst in the folder iso/boot/grub/. File architecture must be like below:

The ISO image then can be generated with the following command:

6) Running Bochs

Now using the os,iso ISO image, we can run the OS in the Bochs emulator. Bochs needs a configuration file to start and an example of a simple configuration file is given below:

If you saved the configuration in a file named bochsrc.txt then you can run Bochs with the following command:

bochs -f bochsrc.txt -q

After the running above command you can see Bochs starting and displaying a console with some information from GRUB on it.

After quitting Bochs, display the log produced by Boch:

cat bochslog.txt

Now you should see the contents of the registers of the CPU simulated by Bochs somewhere in the output. If you find RAX=00000000CAFEBABE or EAX=CAFEBABE (depending on if you are running Bochs with or without 64-bit support) in the output then your OS has successfully booted!

Hope you get good and clear idea on how to set up the development environment and booting an operating system.

Thank You for reading…

— Malshani dahanayaka —

--

--

Malshani Dahanayaka

Software Engineering Undergraduate of University of Kelaniya