Loading kernel from a bootsector

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
lol
Posts: 3
Joined: Tue Jan 14, 2014 7:49 pm

Loading kernel from a bootsector

Post 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.
User avatar
Minoto
Member
Member
Posts: 89
Joined: Thu May 12, 2011 7:24 pm

Re: Loading kernel from a bootsector

Post 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?
Those who understand Unix are doomed to copy it, poorly.
lol
Posts: 3
Joined: Tue Jan 14, 2014 7:49 pm

Re: Loading kernel from a bootsector

Post 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?
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: Loading kernel from a bootsector

Post by VolTeK »

lol wrote:can you run my code?
Yes.
lol
Posts: 3
Joined: Tue Jan 14, 2014 7:49 pm

Re: Loading kernel from a bootsector

Post by lol »

so how did it go? mine didn't load the kernel
User avatar
Minoto
Member
Member
Posts: 89
Joined: Thu May 12, 2011 7:24 pm

Re: Loading kernel from a bootsector

Post 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.
Those who understand Unix are doomed to copy it, poorly.
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: Loading kernel from a bootsector

Post by VolTeK »

lol wrote:so how did it go? mine didn't load the kernel
I think so.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Loading kernel from a bootsector

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Post Reply