Create My Little OS : MMP_OS 😊Week 01

Masith Pramuditha Jayarathna
6 min readOct 1, 2022

--

-OS pic

Let’s build our own Operating System — Week 01 👍

Building an OS is somewhat difficult and complex. Today we are going to develop a little OS which is called as MMP_OS😁. Before getting to the development we need some tools to setup in windows computer.

Tools

First we need to install Oracle Virtual Box to our machine. You can download the VM software here.

After successfully installing the Virtual box to your computer. We need to create an Ubuntu virtual machine. But we can’t install Ubuntu the particular VM until we download Ubuntu OS iso file and attach it to the VM😒. You can download latest Ubuntu version here. After that, when initially run the virtual machine it will install ubuntu to the virtual machine.

Our operating system will be developed using C programming language. We choose C because creating an operating system necessitates very fine control over the produced code and easy access to memory. Although C is the sole language covered in this book, other languages that have the same characteristics can also be utilized. One type property unique to GCC will be used by the code.

__attribute__((packed))

By using this feature, we can guarantee that the compiler will utilize a struct’s memory layout precisely as we specify it in the code.

Our Host Operating System

Every code sample presupposes that the code is being compiled on an operating system similar to UNIX. Ubuntu has been used to successfully compile each sample of code.

Ubuntu Operating System

Plugins

Bochs is an emulator for the x86 platform which is well suited for OS development due to its debugging features. Other popular choices are QEMU and VirtualBox . We use Bochs. Bochs can be installed to the Ubuntu using below command.

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

Booting Process

In order to create an operating system, we should have clear understanding about the booting process.

An operating system is started by passing control through a sequence of little programs, each one more potent than the one that came before it. The BIOS, Bootloader, and OS are the principal applications in this process. The last and most potent system is the operating system.

Booting Process
More simple representation

Beginning OS development

Now we’ll work on developing the simplest operating system feasible. The only thing this OS will do is writing 0xCAFEBABE to the EAX register.

Writing and Compiling

Assembly language must be used to construct this OS component. Since the C language requires a stack, which is not there, we are unable to utilize it.

Save the following code in a file titled “loader.s”.

The assembly code to write CAFEBABE to EAX register

Once we run our OS we can check whether code writes number 0xCAFEBABE to the EAX register.

After creation the loader.s then use the following command to compile loader.s into a 32-bit ELF object file.

nasm -f elf32 loader.s

After entering this command in the terminal, we can see ‘loader.o’ file has created in the working directory.

Linking the Kernel

To create an executable file, the code must now be linked, which involves more consideration than when linking typical programs. Because GRUB itself, the BIOS, and memory-mapped I/O all use addresses lower than 1 MB, we want GRUB to load the kernel at a memory address greater than or equal to 0x00100000 (1 MB). Consequently, the linker script below is required:

Script for linker

Make the above file as ‘link.ld’ then enter the following command:

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

Then ‘kernel.elf’ file will be created.

Getting GRUB

Since both GRUB 2 and GRUB Legacy may be used to create the OS ISO image, GRUB Legacy is the version of GRUB we will employ. More precisely, the ‘stage2eltorito’ bootloader from GRUB Legacy will be utilized. The binary file may be downloaded at

MMP_OS/stage2_eltorito at setup_booting_os · MMPjayarathne/MMP_OS (github.com)

since the configure script does not function well with Ubuntu. Add ‘stage2eltorito’ by copying it to the folder that already has loader.s and link.ld.

Creating the iso image

The executable has to be stored on a medium that a physical or virtual machine can load. The media used in this book will be ISO image files, however floppy images can also be used if the virtual or real system supports them. Using the tool ‘genisoimage’, we will produce the kernel ISO image. The files that will be on the ISO image must first be generated in a folder. The folder is made and the files are copied to their proper locations using the commands below:

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 has to be made. This file configures certain settings and instructs GRUB where to find the kernel:

Configuration file for GRUB

Add the ‘menu.lst’ file to the folder iso/boot/grub/ . The iso folder’s contents ought to now resemble the image below:

Folder organization
Root OS directory

Now we can create the iso file. So for that we can use the following code :

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

Then ‘os.iso’ file will be generated. This contains the kernel executable, the GRUB bootloader and the configuration file.

Running our OS with Bochs

The os.iso ISO image now be used to run the OS in the Bochs emulator. Bochs needs a configuration file to run, and one is provided below as an example of a straightforward configuration file:

Depending on how you installed Bochs, you may need to adjust the path to romimage and vgaromimage. For more information refer the Bochs official webpage here.

Save the configuration file as ‘bochsrc.txt’ and run Bochs with following command:

bochs -f bochsrc.txt -q

At this point, Bochs should be working and presenting a console with some data on it. Then after process of terminal ends, enter ‘continue’ and press enter.

Bochs emulator
Enter ‘continue’ like this

Then to end Bochs and see the log it generated, use the command below:

cat bochslog.txt

Hooray!!đŸ„ł Congratulations!! Now, somewhere in the output, the information from the CPU registers that Bochs copied should show up. Your OS has successfully booted if you see RAX=00000000CAFEBABE or EAX=CAFEBABE in the output.

You can find all the codes from below GitHub repository:

MMPjayarathne/MMP_OS at setup_booting_os (github.com)

We only produced a basic operating system. We’ll examine how using the C programming language will assist us improve our OS development in the next week. Till then, good luck and be careful!

References :

Helin, E. and Renberg, A. (n.d.). The little book about OS development.

bochs.sourceforge.io. (n.d.). bochs: The Open Source IA-32 Emulation Project (Home Page). [online] Available at: https://bochs.sourceforge.io/ [Accessed 1 Oct. 2022].

Kili, A. (2018). How to Change or Set System Locales in Linux. [online] www.tecmint.com. Available at: https://www.tecmint.com/set-system-locales-in-linux/ [Accessed 1 Oct. 2022].

‌

‌‌

--

--