Page 1 of 1

Loading kernel from a bootsector

Posted: Tue Jan 14, 2014 9:41 pm
by lol
Hi guys. I'm creating a bootloader in NASM and I'm very new to this stuff. I had my friends and the internet help me in creating it but I'm really lost and confused. I made a kernel.asm file and a bootsector.asm file. I should be able to load my kernel file to the bootsector file. However, it doesn't load.

Here is my code:

Code: Select all

[BITS 16]    
[ORG 0x7C00]

mov [bootdrv], dl   ;put drive number
        ;disable interrupt, set stack, enable interrupt                         
cli                     
mov ax, 0x9000          
mov ss, ax              
mov sp, 0               
sti         

...
*some code here nothing to do with loading
...

.load_kernel:       
    call read_kernel            ; Load stuff from the bootdrive
    jmp dword KERNEL_SEGMENT:0

read_kernel:
    push ds               
    .reset:
      mov ax, 0               ;reset disk
      mov dl, [bootdrv]       ;drive to reset
      int 13h                
      jc .reset               ;try again if fail
    pop ds

.read:
    push es ; save ES if there's something important in it

    ; load the "kernel" at KERNEL_SEGMENT:0
    mov ax, KERNEL_SEGMENT
    mov es, ax
    xor bx, bx

    ; your kernel will have 512 bytes (1 sector) for now
    mov al, 0x1

    ; get the saved drive number back to DL
    mov dl, [bootdrv]

    mov dh, 0x0 ; head 0
    mov cl, 0x2 ; start reading from 2nd sector

    ; read
    int 0x13

    pop es ; restore ES

    retn ; return and continue after call instruction 

    ; fill up to 510 bytes with zeroes and ...
    times 510-($-$$) db 0 

    ; place the boot signature on the end
    dw 0xAA55
I'm sure my load and reset functions are implemented well. I think there is something wrong in my .read function. I'm really confused.

Any answer would be very helpful.

Re: Loading kernel from a bootsector

Posted: Tue Jan 14, 2014 10:48 pm
by Minoto
lol wrote:

Code: Select all

    ; read
    int 0x13
Look up the required parameters for int 13h. Are you even reading the disk here? And if by chance you are, which cylinder are you reading from?

Re: Loading kernel from a bootsector

Posted: Tue Jan 14, 2014 10:59 pm
by lol
thanks for the reply.
well, i really dont understand cylinder header that i'm hearing from others. I'm having a hard time understanding the locations of what must be passed around. can you run my code?

Re: Loading kernel from a bootsector

Posted: Tue Jan 14, 2014 11:30 pm
by VolTeK
lol wrote:can you run my code?
Yes.

Re: Loading kernel from a bootsector

Posted: Tue Jan 14, 2014 11:52 pm
by lol
so how did it go? mine didn't load the kernel

Re: Loading kernel from a bootsector

Posted: Wed Jan 15, 2014 12:05 am
by Minoto
lol wrote:thanks for the reply.
well, i really dont understand cylinder header that i'm hearing from others. I'm having a hard time understanding the locations of what must be passed around.
As previously suggested, look up the required parameters for int 13h. It expects certain pieces of information to be passed in certain registers; if you don't know what those are, you're not going to be able to proceed any further. Also google CHS addressing and LBA addressing if you need to understand how to address a particular sector on a disk. CHS is obsolete and generally only useful if you want to boot from a floppy disk (also obsolete), but it's worth knowing anyway.
can you run my code?
Given that it's incomplete, that would be pointless.

Re: Loading kernel from a bootsector

Posted: Wed Jan 15, 2014 12:47 am
by VolTeK
lol wrote:so how did it go? mine didn't load the kernel
I think so.

Re: Loading kernel from a bootsector

Posted: Wed Jan 15, 2014 7:31 pm
by neon
Hello,

Try to be more careful when establishing what is needed. Read the Int 13h Function 2 carefully. The provided code most probably is not even issuing the correct BIOS call; try to see why. Additionally, you should be using sector 2, head 0, cylinder 0. Read more up on CHS and converting LBA to CHS here.

All of these issues can be resolved by reading the information more carefully.