Skip to content

Commit

Permalink
added kernal files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ritik-Sharma38 committed Mar 10, 2020
0 parents commit 2bfc5e1
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 0 deletions.
51 changes: 51 additions & 0 deletions boot.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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 lower
stackTop:

.section .text
.global _start
.type _start, @function


_start:

# assign current stack pointer location to stackTop
mov $stackTop, %esp

# call the kernel main source
call kernel_entry

cli


# put system in infinite loop
hltLoop:

hlt
jmp hltLoop

.size _start, . - _start
3 changes: 3 additions & 0 deletions grub.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
menuentry "Ritik" {
multiboot /boot/MyOS.bin
}
24 changes: 24 additions & 0 deletions kernel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include"kernel.h"

static UINT16 VGA_DefaultEntry(unsigned char to_print) {
return (UINT16) to_print | (UINT16)WHITE_COLOR << 8;
}

void KERNEL_MAIN()
{
TERMINAL_BUFFER = (UINT16*) VGA_ADDRESS;

TERMINAL_BUFFER[0] = VGA_DefaultEntry('R');
TERMINAL_BUFFER[1] = VGA_DefaultEntry('i');
TERMINAL_BUFFER[2] = VGA_DefaultEntry('t');
TERMINAL_BUFFER[3] = VGA_DefaultEntry('i');
TERMINAL_BUFFER[4] = VGA_DefaultEntry('k');
TERMINAL_BUFFER[5] = VGA_DefaultEntry(' ');
TERMINAL_BUFFER[6] = VGA_DefaultEntry('K');
TERMINAL_BUFFER[7] = VGA_DefaultEntry('e');
TERMINAL_BUFFER[8] = VGA_DefaultEntry('r');
TERMINAL_BUFFER[9] = VGA_DefaultEntry('n');
TERMINAL_BUFFER[10] = VGA_DefaultEntry('e');
TERMINAL_BUFFER[11] = VGA_DefaultEntry('l');
}

12 changes: 12 additions & 0 deletions kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _KERNEL_H_
#define _KERNEL_H_

#define VGA_ADDRESS 0xB8000

#define WHITE_COLOR 15

typedef unsigned short UINT16;

UINT16* TERMINAL_BUFFER;

#endif
35 changes: 35 additions & 0 deletions linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* 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)
}

}
24 changes: 24 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@


#assemble boot.s file
as boot.s -o boot.o

#compile kernel.c file
gcc -c kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra

#linking the kernel with kernel.o and boot.o files
gcc -T linker.ld -o MyOS.bin -ffreestanding -O2 -nostdlib kernel.o boot.o -lgcc

#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 SabMohMayaHai.iso


0 comments on commit 2bfc5e1

Please sign in to comment.