Operating System Development Part 1

Download basic_kernel.zip - 1.26 KB

Introduction

In this first basic tutorial I'll show how to make a very basic kernel and boot a system from a floppy disk with our kernel.

Even building a simple 'real mode hello world' kernel requires considerable amount of work to be done. To work with own OS kernel in windows environment is little troublesome as most of the documents assumes the development machine to be linux.

I use Windows XP and to test my kernel use Microsoft Virtual PC 2007. Testing the kernel in a virtual machine saves a lot of time. In the Floppy menu we can attach or release floppy image. Any file of length 1,474,560 can be used as a floppy image. The Virtual PC has a virtual hard disk with Windows XP installed (Actually DOS can do all we need- but I installed XP as I am used to it).

Some people prefer to write their own boot loader. But I prefer GRUB to load my kernel so that I can start right now wrining code in kernel. GRUB is a very nice boot loader that support a lot of file systems and file formats.

GRUB Setup

To setup GRUB we need 2 floppy image.

  • 1. boot.img
  • 2. helper.img

The boot.img will be our boot disk and the other is used to create the boot disk.

Now we download the GRUB binary package grub-0.97-i386-pc.tar.gz and extract it. We get two files named stage1 and stage2 with other files.

Now we boot the Virtual PC with Windows XP and after that attach the boot.img. Format the floppy and create a folder named 'system' on root of floppy. Copy the stage1 and stage2 file in system folder. Now release the boot.img and attach helper.img.

We need to concat the stage1 and stage2 file now and copy it on helper floppy starting from first sector as raw image. We concat them with following dos command:

>copy /b stage1 + stage2 grub.bin

Now using rawwrite.exe tool copy the grub.bin in the 'helper' floppy. You may download this file from a lot of locations- some has even GUI.

Now shut down Windows XP and boot the Virtual PC with the helper floppy we just created. To do this we must attach the floppy image before booting the Virtual PC. After the grub command prompt is shown we release the helper.img and capture boot.img and on command prompt enter following command.

grub>install (fd0)/system/stage1 (fd0) (fd0)/system/stage2

Now we have setup the boot disk. We now create our simple kernel to test this. The GRUB loader requeres multiboot kernel (http://www.gnu.org/software/grub/manual/multiboot/multiboot.html). Here we build our multiboot kernel.

The kernel

Here we define very simple multiboot kernel that shows 'Hello World' on screen. We split the kernel into two files. One is in assembly and another in C. Infact we want to use C language as much possible- but we need assembly a little. Not much.

Kernel.asm File

This file defines the multiboot header and call the kernel_main function defined in C source file.

[BITS 32]
[global start]
[extern _kernel_main] ; this is in the c file
; ----- Multiboot Header Starts Here -----
MULTIBOOT_PAGE_ALIGN   equ 1<<0
MULTIBOOT_MEMORY_INFO  equ 1<<1
MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO
CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
; The Multiboot header
align 4
dd MULTIBOOT_HEADER_MAGIC
dd MULTIBOOT_HEADER_FLAGS
dd CHECKSUM
; ----- Multiboot Header Ends Here -----
start:
call _kernel_main
cli ; stop interrupts
hlt ; halt the CPU

To build assembly file I use nasm.exe tool which is available for free. I prefer the ELF file format for my kernel.

>nasm -f elf kernel.asm -o ks.o

Kernel.c File

This file defines clrscr, printxy and kernel_main functions.

Collapse
#define WHITE_SPACE 0x07
#define VIDEO_MEMORY 0xb8000
char *videoMemory = (char*) VIDEO_MEMORY;
void clrscr()
{
int i;
for(i=0;i < (80*25*2);i+=2)
{
videoMemory[i]=' ';
videoMemory[i+1]=WHITE_SPACE;
}
}
void printxy(char *message, unsigned int x, unsigned int y)
{
unsigned int i=0;
i=(y*80*2)+x;
while(*message!=0)
{
if(*message=='\n')
{
y++;
i=(y*80*2);
} else {
videoMemory[i++]=*message;
videoMemory[i++]=WHITE_SPACE;
}
*message++;
}
}
kernel_main()
{
clrscr();
printxy("Hello World", 0, 0);
}

To compile C language files I use DJGPP which is GCC port to windows. Its a free tool and you can download after a search with google. We compile with following command line:

>gcc -c kernel.c -o kernel.o

Linking the object files

For linking we also need the binutils tool for elf support. You can download from here:
http://www.kuashaonline.com/tools/osdev/binutils-2.9-elf.zip.

We first define a linker script (link.ld file). It is very important as it helps to place the various section in right place.

Collapse
OUTPUT_FORMAT("elf32-i386")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
.text phys : AT(phys)
{
code = .;
*(.text)
*(.rodata)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
. = ALIGN(4096);
}
.rodata : AT(phys + (rodata - code))
{
rodata = .;
*(.rodata)
. = ALIGN(4096);
}
end = .;
}

OK. We are almost done and use following command to build our kernel (kernel.bin).

>ld-elf -T link.ld --oformat elf32-i386 -o kernel.bin ks.o kernel.o

To see if the file is created correctly we can use following command:

>objdump-elf kernel.bin --all

Booting with kernel

Now we copy the kernel.bin to our boot disks system folder and boot with that disk. When GRUP command prompt comes we type in following command:

grub>kernel /system/kernel.bin

The grub shows information about our multiboot-elf kernel information. Now we do following command:

grub>boot

Walllla- the Hello World message is shown. Please note here that GRUB already has set Protected Mode, GDT and A20 gate things. The reason why the printing works is GRUB has setup GDT in such a way that Virtual Memory and Physical Memory to video memory is exactly same.

Conclusion

This is very basic tutorial and really old topic. I just wanted to provide what we need to start writing our own OS. Next part may cover some advanced feature like protected mode and descriptor tables and next some more advanced. Please give your comments so that I can improve this article and try to provide better informations on next article.

Also please note that this article contents are not mine. I just collected the manuals and tutorials and add them up. Main intension was to setup the basic environment.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Maruf Maniruzzaman


Have completed BSc in Computer Science & Engineering from Shah Jalal University of Science & Technology, Sylhet, Bangladesh (SUST).

Working as a Software Engineer at KAZ Software Ltd., Dhaka, Bangladesh.

Story books (specially Masud Rana series), tourism, songs and programming is most favorite.

Homepage: http://www.kuashaonline.com/

The CodeProject group on facebook
Occupation: Software Developer
Company: KAZ Software Ltd. Bangladesh.
Location: Bangladesh Bangladesh
原文地址:https://www.cnblogs.com/adylee/p/1237087.html