Implementation of SachOS

Sachithra_Manamperi
6 min readJul 16, 2021

This is to share my Experience in Developing My Very First Operating System. I used Ubuntu As the operating system to develop my Operating System SachOS.

To do this task it’s an easy and safe way to use a Virtual Machine rather than using the Original Host OS.you can use any kind of Hypervisor software to do this.

After the installation of the Virtual Box, you have to download and install a Linux Distribution to implement the OS. For this, I used Ubuntu 20.04 Version. You can download the Ubuntu ISO files From Their Official Website.

If you don’t have installed the Virtual Box then Watch this video on youtube and follow the steps to install the oracle virtual box and Install the Ubuntu OS

Oracle Virtual Box Interface

After The SetUp of Ubuntu Virtual Machine Lets get started to Implement Our First Operating System.

The process of booting an operating system is transferring control through a series of some small kinds of programs, each program is more powerful than the program before it.” For an illustration of the boot procedure, see the diagram below:

An example of the booting process. Here, Each box is a program

BIOS

When you switch on your PC, it will run a small software that follows the Basic Input Output System standard. This application is generally saved on the PC’s motherboard on a read-only memory chip. The BIOS program’s initial purpose was to export library operations such as printing to the screen, reading keyboard input, and so on. Modern operating systems do not rely on BIOS operations; instead, they rely on drivers that communicate directly with the hardware, bypassing the BIOS. BIOS now primarily performs certain early diagnostics before handing over control to the bootloader.

Bootloader

The BIOS program will hand over control of the computer to a bootloader program. The bootloader’s job is to hand control over to us, the OS developers, and our programs. However, owing to hardware limitations and backward compatibility, the bootloader is sometimes split into two parts: the first portion transfers control to the second, which then hands over control of the computer to the operating system.

After installing Ubuntu on a physical or virtual machine, use apt-get to install the following packages:

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

We chose C because creating an operating system needs high controllability over the generated code as well as direct memory access.
One type attribute that is unique to GCC will be used in the code:

__attribute ((packed))

This feature allows us to specify a memory layout for a struct that the compiler will use exactly as we express it in the code.
Because C requires a stack, which isn’t available, this section of the OS must be developed in assembly code.
Save the following code in a loader.s file:

global loader                   ; the entry symbol for ELF    MAGIC_NUMBER equ 0x1BADB002     ; define the magic number constant
FLAGS equ 0x0 ; multiboot flags
CHECKSUM equ -MAGIC_NUMBER ; calculate the checksum
; (magic number + checksum + flags should equal 0)
section .text: ; start of the text (code) section
align 4 ; the code must be 4 byte aligned
dd MAGIC_NUMBER ; write the magic number to the machine code,
dd FLAGS ; the flags,
dd CHECKSUM ; and the checksum
loader: ; the loader label (defined as entry point in linker script)
mov eax, 0xCAFEBABE ; place the number 0xCAFEBABE in the register eax
.loop:
jmp .loop ; loop forever

The following command can be used to compile the file loader.s into a 32-bit object file:

nasm -f elf32 loader.s

Linking the Kernel

To link the kernel, you’ll need the linker script below. Make a link.ld file with the linker script.

ENTRY(loader)                /* the name of the entry label */SECTIONS {
. = 0x00100000; /* the code should be loaded at 1 MB */
.text ALIGN (0x1000) : /* align at 4 KB */
{
*(.text) /* all text sections from all files */
}
.rodata ALIGN (0x1000) : /* align at 4 KB */
{
*(.rodata*) /* all read-only data sections from all files */
}
.data ALIGN (0x1000) : /* align at 4 KB */
{
*(.data) /* all data sections from all files */
}
.bss ALIGN (0x1000) : /* align at 4 KB */
{
*(COMMON) /* all COMMON sections from all files */
*(.bss) /* all bss sections from all files */
}
}

With the following command, the executable can now be linked:

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

Kernel.elf is the name of the finished executable.

Obtaining GRUB

The GRUB version we’ll use is GRUB Legacy because the OS ISO image can be created on both GRUB Legacy and GRUB 2 systems.
The GRUB Legacy stage2_eltorito bootloader will be utilized in this case.

By downloading the source from

ftp:/alpha.gnu.org/gnu/grub/grub-0.97.tar.gz, 

this file can be built from GRUB 0.97.

Otherwise, you can use the WGET command to download files using Linux Terminal

However, because the configure script doesn’t work with Ubuntu, you’ll need to download the binary file from

Place the file stage2_eltorito in the same folder as loader.s and link.ld.

Using the tool genisoimage, we will produce the kernel ISO image. To begin, make a folder that contains the files that will be included in the ISO image. The commands below create the folder and copy the files to their proper 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

For GRUB, a menu.lst configuration file should be prepared. This file instructs GRUB where to find the kernel and sets various options:

default=0
timeout=0
title os
kernel /boot/kernel.elf

Place the menu.lst file in the iso/boot/grub/ folder

The contents of the iso folder should now be like the image

After that, use the following command to create the ISO image:

genisoimage -R                              \
-b boot/grub/stage2_eltorito \
-no-emul-boot \
-boot-load-size 4 \
-A os \
-input-charset utf8 \
-quiet \
-boot-info-table \
-o os.iso \
iso

Running Bochs

Using the os.iso ISO image, we can now execute the OS on the Bochs emulator. To get started with Bochs, you’ll need a configuration file, which you may find an example of below:

megs:            32
display_library: sdl
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest
ata0-master: type=cdrom, path=os.iso, status=inserted
boot: cdrom
log: bochslog.txt
clock: sync=realtime, time0=local
cpu: count=1, ips=1000000

You may launch Bochs with the following command if you stored the above code in a file named bochsrc.txt:

bochs -f bochsrc.txt -q
After Running the command Bochs Emulator Opens

If you get an error in your terminal, just change display library: sdl to display library: sdl2 in the second line of the bochsrc.txt file
If you see some text on the Bochs emulator that says “Booting os,” leave the emulator because that means the OS has been successfully booted.
If there is no text on the emulator’s screen,

type

continue

and click enter in the terminal.

It will start the operating system. Bochs has now started and is displaying a console with some GRUB information on it.

After exiting the Bochs emulator,

Display the log created by Boch after you’ve exited Bochs:

cat bochslog.txt

it will display a lot of content on the terminal

Your OS has successfully booted if you see RAX=00000000CAFEBABE or EAX=CAFEBABE in the output

File Contents after completion of the Booting

Ref : https://littleosbook.github.io/

Full Project Files https://github.com/SachPrecious/SachOS

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Sachithra_Manamperi
Sachithra_Manamperi

Written by Sachithra_Manamperi

🚀 DevOps Engineer | 🎓 Software Engineering Graduate | 🇱🇰 Sri Lankan | 💻 Passionate about Cloud Native tech

No responses yet

Write a response