My Operating system Development Experience and understanding. Part 01

M.R.M Abdullah
9 min readJul 16, 2021

--

As a software engineering student of University of Kelaniya I able to follow a course module called “Software architecture and Operating system “. in order to study this course a trained to develop a minimal operating system by myself. In this article I like to share some of my experiences and Understanding About Operating system development. This is the first part of full article I divide this article into some parts to get your attention.

Let get into the article!

Before getting into OS development, You need to know about small understanding about operating system.

What is an operating system?

An operating system (OS) is the program that, after being initially loaded into the computer by a boot program, manages all the other application programs in a computer. The application programs make use of the operating system by making requests for services through a defined application program interface (API). In addition, users can interact directly with the operating system through a user interface, such as a command-line interface (CLI) or a graphical UI (GUI)

I will give you some points to understand Operating system.

· The Operating System (OS) play a very crucial role in any computer system it is used to interface between the computer hardware and applications it uses.

· The OS essentially manages both the applications executing on the computer and how the resources are utilized in the system.

The operating system is used for two things:

- Resource Management: Manages system resources

- Hardware Abstraction: turns hardware into something that applications can use.

· Without an OS, all programs need to take care of every nitty gritty detail, so OS provide abstraction which means no nittier gritty details for programmers.

· An OS must manage CPU, memory, network, secondary storage (hard disk), etc.

Resource management:

-allows multiple apps to share resources

-protects apps from each other

-improves performance by efficient utilization of resources

When sharing resources with different applications, it should be done in a sandbox environment so the application cannot access or interact with each other.

To get more understanding about OS I like to share with you a Online Course that you can understand more

Course Link :https://bit.ly/introOS

Let Get start into OS development.

Developing an operating system (OS) is no easy task, and The reason at the beginning was very simple. I wanted to be a programmer that can use the most efficient code to achieve a combination of software and hardware. Later

Ready to work

Setting up the environment: It is best to install them first (in order)

· Ubuntu 20.04 virtual machine

· nasm

· GCC

· build-essential,

· Bochs 2.6.11

Let Do every steps and understand what happening background

Ubuntu installation

I will tell you how to install Ubuntu Linux on a computer by using VirtualBox. VirtualBox is a program which allows you to install an operating system without changing your computer’s main operating system.

Why VirtualBOX? When developing an OS it is very convenient to be able to run your code in a virtual machine instead of on a physical computer, since starting your OS in a virtual machine is much faster than getting your OS onto a physical medium and then running it on a physical machine.

Ubuntu installation guide

· Go to https://bit.ly/UbuntuDownloadLink in your computer’s web browser. You can download the Ubuntu disk image (also known as an ISO file)

Reference images got from WikiHow

· Go to https://bit.ly/OracleVB . This is the website from which you’ll download the VirtualBox setup and install it on your computer.

· Open VirtualBox. Double-click the VirtualBox app icon.

· Click New. It is a blue badge in the upper-left corner of the VirtualBox window. Doing so opens a pop-up menu.

· Enter a name for your virtual machine. Type whatever you want to name your virtual machine (e.g., Ubuntu) into the “Name” text field that’s near the top of the pop-up menu

· Select Linux as the “Type” value. Click the “Type” drop-down box, then click Linux in the resulting drop-down menu.

· Select Ubuntu as the “Version” value. Ubuntu should be selected by default after you set the “Type” value to Linux, but if it isn’t, click the “Version” drop-down box and click Ubuntu (64-bit) before proceeding.

· Click next and Select an amount of RAM to use. Click Next

· Create your virtual machine’s virtual hard drive. The virtual hard drive is a section of your computer’s hard drive space which will be used to store your virtual machine’s files and programs: Click Create, Click Next, Click Next, Select an amount of space to use, Click Create.

· Double-click your virtual machine’s name. It’s on the left side of the VirtualBox window. Doing so will open a menu.

· Click the folder-shaped icon. This icon is in the lower-right side of the menu. A new window in which you can select the Ubuntu ISO will open.

· Give details for OS install Wait for Ubuntu to finish installing. This process may take anywhere from a couple of minutes to half an hour depending on the speed of your computer. You won’t need to do anything during the installation process.

When you finish your Ubuntu installed open terminal to install others. Form my experience Ubuntu Installation is very simple but beware first download Ubuntu iso file first. Before setting up the VirtualBox

Installing others

After you opened terminal ,to install build-essential ,nasm ,bochs you need to type below code.

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

When you type this it can be pop up a error massage like

Reading package lists… Done

Building dependency tree

Reading state information… Done

E: Unable to locate package build-essentials

E: Unable to locate package nasm

E: Unable to locate package genisoimage

E: Unable to locate package bochs

If this error came please make sure you have connected to internet And type

sudo apt-get update

to make sure that your package index is up to date. It will take more than 5 min to set up after that again type

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

It will get all the packages .

I’m telling this because I had face this problem also

That’s all we set up our environment for develop OS.

Programming language

Select C as the development language. The operating system will be developed using the C programming language and GCC. We use C language because developing OS needs very accurate control of the generated code and directly accessing memory. You can also use other languages ​​that provide the same function

Booting

The above picture shows Control Flow

Boot load program

The BIOS program will transfer the control of the PC to a program called bootloader. The task of booting the loader is to transfer control to us, operating system developers and our code. However, due to certain limitations of hardware 1 and backward compatibility, the bootloader is usually divided into two parts:The first portion of the bootloader transfer controls to the second part and finally gives control to the PC.Writing bootloader involves writing a lot of underlying code with BIOS interaction.

Therefore, an existing boot loader will be used: GNU Grand Unified Boot Loader (GRUB).With GRUB, you can build an operating system as a normal ELF [18] executable, and then load it to the correct memory location by GRUB. The compilation requirements of the kernel are arranged in memory in a specific manner.

GRUB will transfer control to the operating system by jumping to a location in memory. Before jump, GRUB will look for a number to make sure it is actually jumping to the operating system instead of some random code. This number is part of the multi-guidance specification followed by GRUB. Once GRUB jumps up, the operating system can fully control the computer.

This part of the operating system must be written with assembly code because c requires an unavailable stack.

So Please Do all the steps as it is to get into C

Save the following code in a file called loader.s:

(Use text editor on ubuntu to create this file)

I can’t include code (Medium)here so I will upload all long codes in a text file you can download it below link.

https://mega.nz/file/WxBlwSxa#gjDkUBYlGS7GTm_rhvxrjGGq8gsyQ9IWYdZYuBe4-Dg

The only thing to do this is to write a very specific number 0xcafebab to the EAX register. If the operating system does not place the 0xcafebabe number in the EAX register, the possibility is small.
You can compile the loader.s file into a 32-bit ELF target file using the following command:

nasm -f elf32 loader.s

Link kernel

You must now link the code to generate an executable, which requires some additional considerations compared to most of the links. We want GRUB to load the kernel on the memory address greater than or equal to 0x00100000 (1 megabyte (MB)), because the GRUB itself, the BIOS and memory maps are less than 1 MB of addresses. Therefore, the following link program scripts are required (written for GNU LD):

(Code on Zip file Name:Link)

Save the linker script to a file named link.ld.
You can now link executable files using the following command:

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

Get GRUB

The GRUB version we will use is Grub Legacy because of the generated OS ISO image on the system of GRUB LEGACY and GRUB 2. More specifically, the GRUB Legacy Stage2_eltorito boot program will be used.

Copy the files Stage2_ltorito to the folder already containing loader.s and link.ld. (file in zip link above)

Establish an ISO image

The executable must be placed on the media that can be loaded by a virtual machine or physical machine. In this book, we will use the ISO [22] image file as a medium, but can also use a floppy image, depending on the content supported by the virtual machine or physical machine. We will use the program GenisoImage to create a kernel ISO image. First, a folder must be created, which contain files that will be on the ISO image. The following command creates a folder and copies the file to the correct location:

mkdir -p iso/boot/grub
cp stage2_eltorito iso/boot/grub/
cp kernel.elf iso/boot/

make sure you are in the folder that contain loader.s and link.ld

Next must create a configuration file in the directory ISO / BOOT / GRUB / Create a configuration file Menu.lst

((Code on Zip file Name:menu)

The file hierarchical structure is as follows:

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

Use the following command to generate an ISO mirror:

(command on zip file call ISO Mirror)

Os.iso is the generated ISO image file.

Start Bochs

Now we can run Os.iso using Bochs. Bochs requires the configuration file to start.bochsrc.txt

It should be noted that you need to change romimage and vgaromimage, and install the location of Bochs.

With a configuration file, start using the following command.

bochs -f bochsrc.txt -q

The flag -f tells Bochs using a given profile, the flag -Q tells Bochs to skip the interactive start menu. Now you should see Bochs boot and display console with GRUB information. After exiting BOCHS, display the log of Boch

here sometime you will face a error if you faced that you need to change some content in bochsrc.txt that

display_library: sdl to display_library: sdl2

than again type

bochs -f bochsrc.txt -q

Enter CONT in the command line

and close black window . clear all in terminal and type below commend

cat bochslog.txt | grep ‘EAX=’

Now, you should see the contents of the CPU register simulated by Bochs in some somewhere. If RAX = 0000000000 Cafebabe or EAX = Cafebab is found in the output (depending on whether the Bochs runs to support 64 bits), you will successfully start!

that’s all so now we come up all the steps, errors and solution for that .i think you got everything i have discuss in this article see you in next part

thanks you for reading

Abdulla M.R.M

--

--

M.R.M Abdullah
M.R.M Abdullah

Written by M.R.M Abdullah

Software Engineering undergraduate at University of Kelaniya.

No responses yet