Operating System Development-Part 1

Source: Internet
Author: User
Document directory
  • Kernel. ASM file
  • Kernel. c file
  • Linking the object files
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 clients 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 'help' 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<<0MULTIBOOT_MEMORY_INFO  equ 1<<1MULTIBOOT_HEADER_MAGIC equ 0x1BADB002MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFOCHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS); The Multiboot headeralign 4dd MULTIBOOT_HEADER_MAGICdd MULTIBOOT_HEADER_FLAGSdd CHECKSUM; ----- Multiboot Header Ends Here -----start:call _kernel_maincli ; stop interruptshlt ; 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 definesclrscr,printxyAndkernel_mainFunctions.

Collapse
#define WHITE_SPACE 0x07#define VIDEO_MEMORY 0xb8000char *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 varous 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.