The XamOS — Let's Build an OS!!!

Nipuni Perera
9 min readJul 15, 2021

The #1 step of Operating system development — Setup & Booting

If you came to find out “ What?, When?, Where?, Why?, an Operating system ?..” This is not the article for you. Here I’ll be to the point to show you how to implement your own operating system.

Ubuntu Guest Virtual Machine Vs Windows Host Machine

And I know you're not interested to read long boring content. I’ll lead you through a step-by-step layout. So Lets’ start the journey………………………

Step 01: Installing Oracle VM VirtualBox

By visiting above link you can download and install the Oracle VM VirtualBox, which is open-source software.

VirtualBox

VirtualBox is designed to run virtual machines on your physical machine without reinstalling your OS that is running on a physical machine. One more VirtualBox advantage is that this product can be installed for free.Installing Ubuntu in a virtual machine allows you to try the version of Linux without affecting Windows. The virtual Linux operating system will perform the same way as it would when installed to the physical hard drive, and any software installed in the virtual machine remains intact after rebooting. Virtual machines can be easily backed up and restored, so it’s no big deal if you mess something up.

Step 02: Download the Ubuntu ISO file & get start

After downloading the Ubuntu ISO file ,open VirtualBox and select New in the top taskbar.Then Give your VM a name, choose Linux as the Type, then choose Ubuntu as the Version and select Next. After that Choose how much RAM you want to assign to the virtual machine and select Next. The recommended minimum is 1024 MB.The amount of memory you set aside is only used while the virtual machine is running.Next choose Create a virtual hard disk now and select Create.If you just want to run Ubuntu as a live image, choose Do not add a virtual hard disk. You must create a virtual hard drive to save the changes you make in Ubuntu.Then choose VDI (VirtualBox Disk Image) and select Next.Choose Dynamically allocated or Fixed size for the storage type and select Next.A fixed size disk performs better because the virtual machine doesn’t have to increase the file size as you install software.Then you have to choose how much space you wish to set aside for Ubuntu and select Create.The amount of space you allocate for your virtual machine determines how much room you have to install applications, so set aside an ample amount.The name of your virtual machine will now appear on the left side of the VirtualBox manager. Select Start in the toolbar to launch your VM.This is the point where you need to choose the Ubuntu ISO file you downloaded earlier. If the VM doesn’t automatically detect it, select the folder next to the Empty field.Choose your Ubuntu disk image and select Open.

Here Your VM will now boot into a live version of Ubuntu. Choose your language and select Install Ubuntu,choose your keyboard layout,and then choose Normal installation or Minimal installation, then select Continue.Then choose Erase disk and install Ubuntu and select Install Now, select Continue to ignore the warning.Because this step will not erase your computer’s physical hard drive; it only applies to the virtual machine. Then select your time zone on the map, and set up your user account.finally you have to restart the VM.

After setting up Ubuntu on VirtualBox

Step 03: Get an idea about making files on ubunru

Ubuntu’s Cat and Touch terminal commands will create new files that contain no data, but are visible to the file manager or the Ls directory-listing command. Although the Touch command is normally used to change the time-stamp of existing files, it will also generate one or more new files with any extension you choose. The Cat, or concatenation, terminal command can copy the characters you type at the keyboard to an new file. If you press “Ctrl-C” and no other keys, however, Cat creates an empty file. The Touch and Cat commands work with Ubuntu derivatives like Kubuntu, Xubuntu and Lubuntu as well.

(1)Touch Command

Step A

Use the Touch command to create the file you need. For example, type the following command at the terminal prompt to create a text file named “sample.txt.”

touch sample.txt

Step B

Press “Enter” to execute the Touch command and generate the empty file.

Step C

Type “ls -t -r” at the command prompt and then press “Enter” to confirm that your new file exists. This command will list all files in the current directory in the order in which they were created. Your new file will be the last item in the last line of the listing.

(2)Cat Command

Step A

Type the Cat command that will create a new file and, if you wish, its contents. For example, to create a text file named “sample.txt,” type the following command at the terminal prompt:

cat >sample.txt

The “>” or redirection symbol tells Cat to copy any characters you type at the keyboard to the designated file

Step B

Press the “Enter” key to execute the Cat command and then “Ctrl-C” to stop it. After Cat stops running, the new, empty file is automatically saved, closed and made accessible to other Ubuntu utilities. If you typed anything before pressing “Ctrl-C,” that input will be written as the file’s contents.

Step C

Type “ls -t -r” at the command prompt and then press “Enter” to confirm that your new file exists.

Step 04 :Booting

1)apt-get should be used to install the following packages once Ubuntu has been installed:

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

It is highly handy to be able to execute your code in a virtual machine rather than on a real computer when building an operating system, because starting your OS in a virtual machine is considerably faster than putting your OS onto a physical media and then running it on a physical machine.Bochs is an emulator for the x86 (IA-32) platform with debugging capabilities that makes it ideal for OS development.The process of booting an operating system entails passing control through a series of tiny programs, each one more “powerful” than the one before it, with the operating system being the last “program.”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.Here, I have used the GNU GRand Unified Bootloader (GRUB). Using GRUB, the operating system can be built as an ordinary ELF executable, which will be loaded by GRUB into the correct memory location. The compilation of the kernel requires that the code is laid out in memory in a specific way.I’ve used the GNU GRand Unified Bootloader in this case (GRUB). The operating system may be produced using GRUB as a regular ELF executable, which will be loaded into the right memory address by GRUB. The kernel’s compilation demands that the code be placed down in memory in a certain order.

2) 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

3) The following command will be used to compile the file loader.s into a 32-bit ELF object file:

nasm -f elf32 loader.s

4) As a result, the following linker script (written for GNU LD) is required:

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 */

}

}

Make a link.ld file using the linker script.

5)With the following command, the executable will now be linked:

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

Kernel.elf is the name of the finished executable.

6) The GRUB version we’ll use is GRUB Legacy, so the OS ISO image may be produced on both GRUB Legacy and GRUB 2 systems. The GRUB Legacy stage2 eltorito bootloader will be utilized in this case. You may get the binary file from here.

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

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

7) We’ll use the application genisoimage to build the kernel ISO image. To begin, make a folder that includes the files that will be included in the ISO image. The instructions 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

8) For GRUB, a menu.lst configuration file must be generated. This file instructs GRUB where to find the kernel and sets certain options:

default=0

timeout=0

title os

kernel /boot/kernel.elf

9) Put the file menu.lst in the iso/boot/grub/ folder. The contents of the iso folder should now resemble the image below:

iso

| — boot

| — grub

| | — menu.lst

| | — stage2_eltorito

| — kernel.elf

10) Afterward, 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

11) Using the os.iso ISO image, we can now execute the OS in the Bochs emulator. To get started with Bochs, you’ll need a configuration file. Here’s an example of a basic setup file:

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

Depending on how you installed Bochs, you may need to modify the path to romimage and vgaromimage.

12) You may launch Bochs with the following command if you stored the settings in a file named bochsrc.txt:

bochs -f bochsrc.txt -q

13) Bochs now appears to be running and providing a terminal with GRUB information.

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

cat bochslog.txt

And at last, the contents of the CPU registers emulated by Bochs may be seen anywhere in the output. This marks the end of the OS’s booting procedure.

Ater Following the above-mentioned procedure, you will discover the following folders/files.

So it leads to the end of todays article which was based on the vey first step of Operating system development — Setup & Booting.

Thank you very much for reading!

I’ll hope to get back to you with the chapter two, “Implement with C,” as soon as possible.Till then,

Stay Safe!!!

-Nipuni Perera-

--

--

Nipuni Perera

As a Software Engineering undergrad at the University of Kelaniya SL , I share insights on coding, dev methodologies & emerging tech. Join me on my journey!