Achievement 4 - Operating system (2024)

  1. Home
  2. Achievement
  3. Achievement 4

11 Dec

11Dec

Begin building the operating system kernel

Initially we need to install some of the requirements for building the kernel, which may already be

installed.

These requirements are:

GNU / Linux: - ubuntu (That we used to build bots) or Any distribution

Assembler: - GNU Assembler (nasm)

GCC: - GNU Compiler Collection, C compiler. I'm using gcc 4, 5, 6, 7, 8 versions

Xorriso: - A package that creates, loads, manipulates ISO 9660 filesystem images. (Man xorriso)

grub-mkrescue: - Make a GRUB rescue image, this package internally calls the xorriso functionality to build an iso image.

QEMU: - Quick EMUlator to boot our kernel in virtual machine without rebooting the main system.

Now ,we will make sure that some requirements are installed and the rest of the requirements are installed.

First, start the virtual machine and run the installed Linux system (Ubuntu), then open Terminal and type the following commands:

1- sudo apt-get install nasm

2- sudo apt-get install gcc

3- sudo apt-get inastall xorriso

4- sudo apt-get install qemu

5- man grub-mkrescue

Now all the requirements have been installed to start building our operating system.

follow the steps in detail until you build your operating system correctly.

Create a folder on your desktop and name it MyOS.

In the first step, we will open the Notepad ++ editor, then open a new project and write the following codes:

kernel.h

#ifndef KERNEL_H#define KERNEL_Htypedef unsigned char uint8;typedef unsigned short uint16;typedef unsigned int uint32; #define VGA_ADDRESS 0xB8000#define BUFSIZE 2200uint16* vga_buffer; #define NULL 0enum vga_color { BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, GREY, DARK_GREY, BRIGHT_BLUE, BRIGHT_GREEN, BRIGHT_CYAN, BRIGHT_RED, BRIGHT_MAGENTA, YELLOW, WHITE, }; #endif

Save this file in the folder you created on the desktop as kernel.h

Now open a new page in the code editor, then write the following commands:

kernel.c

#include "kernel.h"/*16 bit video buffer elements(register ax) 8 bits(ah) higher : lower 4 bits - forec olor higher 4 bits - back color 8 bits(al) lower : 8 bits : ASCII character to print */uint16 vga_entry(unsigned char ch, uint8 fore_color, uint8 back_color) { uint16 ax = 0; uint8 ah = 0, al = 0; ah = back_color; ah <<= 4; ah |= fore_color; ax = ah; ax <<= 8; al = ch; ax |= al; return ax; }//clear video buffer arrayvoid clear_vga_buffer(uint16 **buffer, uint8 fore_color, uint8 back_color) { uint32 i; for(i = 0; i < BUFSIZE; i++){ (*buffer)[i] = vga_entry(NULL, fore_color, back_color); } }//initialize vga buffervoid init_vga(uint8 fore_color, uint8 back_color) { vga_buffer = (uint16*)VGA_ADDRESS; //point vga_buffer pointer to VGA_ADDRESS clear_vga_buffer(&vga_buffer, fore_color, back_color); //clear buffer} void kernel_entry() { //first init vga with fore & back colors init_vga(WHITE, BLACK); //assign each ASCII character to video buffer //you can change colors here vga_buffer[0] = vga_entry('H', WHITE, BLACK); vga_buffer[1] = vga_entry('e', WHITE, BLACK); vga_buffer[2] = vga_entry('l', WHITE, BLACK); vga_buffer[3] = vga_entry('l', WHITE, BLACK); vga_buffer[4] = vga_entry('o', WHITE, BLACK); vga_buffer[5] = vga_entry(' ', WHITE, BLACK); vga_buffer[6] = vga_entry('W', WHITE, BLACK); vga_buffer[7] = vga_entry('o', WHITE, BLACK); vga_buffer[8] = vga_entry('r', WHITE, BLACK); vga_buffer[9] = vga_entry('l', WHITE, BLACK); vga_buffer[10] = vga_entry('d', WHITE, BLACK); }


Save this file in the folder you created on the desktop as kernel.c



First you need a multiboot bootloader file that instruct the GRUB to load it.
Now close this file and open a new page in the code editor, then write the following commands:

boot.S

# set magic number to 0x1BADB002 to identified by bootloader .set MAGIC, 0x1BADB002 # set flags to 0.set FLAGS, 0# set the checksum.set CHECKSUM, -(MAGIC + FLAGS) # set multiboot enabled.section .multiboot # define type to long for each data defined as above.long MAGIC .long FLAGS .long CHECKSUM # set the stack bottom stackBottom: # define the maximum size of stack to 512 bytes.skip 1024 # set the stack top which grows from higher to lowerstackTop: .section .text .global _start .type _start, @function _start: # assign current stack pointer location to stackTopmov $stackTop, %esp # call the kernel main sourcecall kernel_entry cli # put system in infinite loophltLoop: hlt jmp hltLoop .size _start, . - _start

Save this file in the folder you created on the desktop as boot.S

Now you need a configuration file that instruct the grub to load menu with associated image file

open a new page in the code editor, then write the following command

grub.cfg

menuentry "MyOS" {

multiboot /boot/MyOS.bin

}


Save this file in the folder you created on the desktop as grub.cfg

Now we need a file to link object files and this file is known as the linker.

open a new page in the code editor, then write the following command

linker.ld

/* entry point of our kernel */

ENTRY(_start)

SECTIONS

{

/* we need 1MB of space atleast */

. = 1M;

/* text section */

.text BLOCK(4K) : ALIGN(4K)

{

*(.multiboot)

*(.text)

}

/* read only data section */

.rodata BLOCK(4K) : ALIGN(4K)

{

*(.rodata)

}

/* data section */

.data BLOCK(4K) : ALIGN(4K)

{

*(.data)

}

/* bss section */

.bss BLOCK(4K) : ALIGN(4K)

{

*(COMMON)

*(.bss)

}

}


Save this file in the folder you created on the desktop aslinker.ld

Now let's create a file to create, link and run object files on qemu.

open a new page in the code editor, then write the following command

run.sh

#assemble boot.s file

as --32 boot.s -o boot.o

#compile kernel.c file

gcc -m32 -c kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra

#linking the kernel with kernel.o and boot.o files

ld -m elf_i386 -T linker.ld kernel.o boot.o -o MyOS.bin -nostdlib

#check MyOS.bin file is x86 multiboot file or not

grub-file --is-x86-multiboot MyOS.bin

#building the iso file

mkdir -p isodir/boot/grub

cp MyOS.bin isodir/boot/MyOS.bin

cp grub.cfg isodir/boot/grub/grub.cfg

grub-mkrescue -o MyOS.iso isodir

#run it in qemu

qemu-system-x86_64 -cdrom MyOS.iso

Save this file in the folder you created on the desktop asrun.sh

Now that we have finished building the kernel, let's run it on Qemu.

Open Terminal from the folder created on your desktop (MyOS) and type the following command:

sh run.sh

Achievement 4 - Operating system (1)

For more details see the links below.

https://github.com/pritamzope/OS/tree/master/Kernel/Simple/src/kernel_1

https://www.c-sharpcorner.com/article/create-your-own-kernel/

https://www.codeproject.com/Articles/1225196/Create-Your-Own-Kernel-In-C-2

Achievement 4 - Operating system (2024)

References

Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 5870

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.