Writing a Simple Operating System — from Scratch

(Jeff_L) #1

CHAPTER 5. WRITING, BUILDING, AND LOADING YOUR


KERNEL 51


lack of BIOS will make it hard for us to use the disk: we would have to write a floppy
or hard disk driver ourselves!
To simplfy the problem of which disk and from which sectors to load the kernel code,
the boot sector and kernel of an operating system can be grafted together into akernel
image, which can be written to the initial sectors of the boot disk, such that the boot
sector code is always at the head of the kernel image. Once we have compiled the boot
sector described in this section, we can create our kernel image with the following file
concatenation command:


cat bootsect.bin kernel.bin > os-image
Figure XXX shows a boot sector that will bootstrap our kernel from a disk containing
our kernel image,os-image.


; A boot sector that boots a C kernel in 32-bit protected mode
[org 0x7c00]
KERNEL_OFFSET equ 0x1000 ; This is the memory offset to which we will load our kernel

mov [BOOT_DRIVE], dl ; BIOS stores our boot drive in DL, so it’s
; best to remember this for later.

mov bp, 0x9000 ; Set -up the stack.
mov sp, bp
mov bx, MSG_REAL_MODE ; Announce that we are starting
call print_string ; booting from 16-bit real mode

call load_kernel ; Load our kernel

call switch_to_pm ; Switch to protected mode , from which
; we will not return

jmp $

; Include our useful , hard -earned routines
%include "print/print_string.asm"
%include "disk/disk_load.asm"
%include "pm/gdt.asm"
%include "pm/print_string_pm.asm"
%include "pm/switch_to_pm.asm"

[bits 16]

; load_kernel
load_kernel:
mov bx, MSG_LOAD_KERNEL ; Print a message to say we are loading the kernel
call print_string

mov bx, KERNEL_OFFSET ; Set -up parameters for our disk_load routine , so
mov dh, 15 ; that we load the first 15 sectors (excluding
mov dl, [BOOT_DRIVE] ; the boot sector) from the boot disk (i.e. our
call disk_load ; kernel code) to address KERNEL_OFFSET

ret

[bits 32]
; This is where we arrive after switching to and initialising protected mode.
Free download pdf