Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with custom OS's bootloader not jumping to kernel

I recently had an idea to start developing my own Operating System. After reading many articles on different sites that I thought would help me with this task, I thought I could start now. (I am using Ubuntu 14.10 x64 by the way)

Since a floppy disk is the simplest storage medium for developing OSes, I acquired a 3.5 inch floppy disk drive.

I am using NASM as an assembly compiler, and qemu as an emulator. Using the dd command, I cloned an existing and empty (in terms of files) floppy disk to a file called floppy.img.bak .

After that, I wrote a simple bootloader in x86 assembly:

bootloader.asm

org 7C00h
jmp 0x0000:start    ;go 

msg db 'Loading Kernel...', 0

start:
    ;update the segment registers
    mov ax, cs
    mov ds, ax
    mov es, ax

    mov si, msg

print:          ;prints a string
    lodsb       ;load next char

    cmp al, 0   ;if null terminator...
    je reset    ;...jump to reset:

    mov ah, 0Eh ;print AL
    mov bx, 7   
    int 10h

    jmp print   ;if not null terminator, continue printing

reset:          ;resets the floppy drive
    mov ax, 0   ;
    mov dl, 0   ;drive=0 (=A)
    int 13h     ;
    jc reset    ;if error resetting, reset again

read:
    mov ax, 1000h ;ES:BX = 1000:000
    mov es, ax  ;es is 1000h now
    mov bx, 0   ;bx is 0 now

    mov ah, 2   ;load disk data into ES:BX
    mov al, 1   ;load 1 sector
    mov ch, 0   ;cylinder=0
    mov cl, 2   ;sector=2
    mov dh, 0   ;head=0
    mov dl, 0   ;drive=0
    int 13h     ;read!

    jc read     ;if error then try again


    jmp 1000h:0000;jump to the program

times 510-($-$$) db 0
dw 0AA55h

So far so good. My simple temporary stub kernel is as follows:

kernel.asm

kstart:
    mov ah, 9
    mov al, 'k'
    mov bx, 7
    mov cx, 5
    int 10h

hang:
    jmp hang

times 510-($-$$)+2 db 0

I also have a shell script to compile, write and boot this setup:

compile-and-run.sh

cd ~/Dev/OS                                                          # cd to here

rm asm-bin/bootloader.bin                                               # remove old compiled bootloader
rm asm-bin/kernel.bin                                                   # remove old compiled kernel

nasm asm-src/bootloader.asm -f bin -o asm-bin/bootloader.bin            # compile bootloader
nasm asm-src/kernel.asm -f bin -o asm-bin/kernel.bin                    # compile kernel

rm images/floppy.img                                                    # remove old floppy image
cp images/floppy.img.bak images/floppy.img                              # copy original floppy image to fresh one

dd if=asm-bin/bootloader.bin of=images/floppy.img bs=512 count=1 seek=0 # write bootloader to first sector
dd if=asm-bin/kernel.bin of=images/floppy.img bs=512 count=1 seek=1     # write kernel to second sector

qemu-system-i386 images/floppy.img                                      # start qemu and boot floppy.img

Now, the expected output in qemu would be (at least what I understand):

Loading Kernel...
kkkkk

But instead, it is:

Loading Kernel...

So, obviously, there is something wrong at the jump, I just don't know what. Maybe you could help me? I would appreciate it.

like image 315
Paul Pignet Avatar asked Oct 25 '14 22:10

Paul Pignet


1 Answers

The bootdrive is stored in dl register but you override it to 0.
By default the bootdrive is 80h and not 0 as used by the bootloader code.
If you comment out the 2 lines

;    mov dl, 0   ;drive=0 (=A)

It will boot as you expect.

like image 135
mpromonet Avatar answered Sep 30 '22 05:09

mpromonet