hello,I am writing a bootloader and load a kernel

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
thesexhunter
Posts: 2
Joined: Mon Mar 15, 2010 8:47 am

hello,I am writing a bootloader and load a kernel

Post by thesexhunter »

These days I tried to write an simple bootloader which could display a simple string of "Hello world".
------------------------------------------------------------------
------------------------ stage 1 ----------------------------
------------------------------------------------------------------
I use NASM Language to write a program, in which calling bios 13h interrupt of 10h function,which could display a string when booting.

Then I compile it with the following command:
nasm -f bin bootloader.asm -o bootloader
bootloader.asm is my source code.
Then I wrote a program using C language to write the binary file bootloader into the flash disk.

I reboot my PC and it works.When I change the booting order from hard disk to usb flash,it displayed the string Hello World.

---------------------------------------------------------------------
------------------------ stage 2 --------------------------------
---------------------------------------------------------------------

You know that the Hello World program just stayed in the first section.
Now I would like to write a bootloader named bootloader.asm and a kernel file named kernel.asm by using NASM language.This time I would like to make the kernel in the 2nd sector(bootloader in the 1st sector),and loading the kernel by using the bootloader which still stayed in 1st boot sector.

These two binary files could be copied to usb flash sequenctly by using a C language program I wrote.

but I failed.When booting from usb flash,two strings displayed on the screen.They are:
Disk set success...
Kernel loaded failed...
I have try many ways to find the cause,but I failed.

The reason I guess is that the call bios 13H interrupt of 02 function is wrong.

Now I will send you two asm files(one named bootloader.asm other named kernel.asm) and a C language program which functions as a copy utility to write these two binary files into the usbflash

Code: Select all

---------------------------bootloader.asm---------------------------------------
         BITS 16
	 ORG 0x7C00

         SECTION .text
	 
	 ;cli
         ;mov ax,0x9000
         ;mov ss,ax
         ;mov sp,0xffff
         ;sti

         mov [bootdrv],dl

         mov ax,cs
	 mov ds,ax

         mov si,3
         call load

	 jmp 0x0000:0x7E00

load:   
         ;push ds
         mov ax,0
	 mov dl,[bootdrv]
	 int 13h
	 ;pop ds
	 jc resetfail

	 mov bp,msgresetsuccess
	 mov cx,resetsuccesslen
         mov dx,0x00
   	 call putstr

         mov ax,0
         mov es,ax
	 mov ah,2
	 mov al,1
	 mov dx,0
	 ;mov dl,[bootdrv]
	 mov cx,2
	 mov bx,0x7E00
	 int 0x13
	 jc loadfail
         ;jc load

	 mov bp,msgkernelload
	 mov cx,kernelloadlen
         mov dx,0x0400
	 call putstr
	 ret

resetfail:
         mov bp,msgresetfail
         mov cx,resetfaillen
         mov dx,0x00
         call putstr
	 ret

loadfail:
         dec si
	 cmp si,0
	 jne load
         jmp 0x0000:0x7E00
	 ret
;        mov bp,msgloadfailed
;        mov cx,loadfailedlen
;        mov dx,0x0200;
;        call putstr
;        ret

putstr:
         mov ax,cs
	 mov ds,ax
	 mov es,ax

	 mov al,0x01
	 mov bh,0x00
         mov bl,0x02
         
         mov ah,0x13
	 int 0x10
         ret

	 ;dw 0xAA55

         SECTION .data

         bootdrv db 0

         msgresetfail db 'Disk Reset Failure!',10
         resetfaillen equ $-msgresetfail

         msgresetsuccess db 'Disk reset success...',10
         resetsuccesslen equ $-msgresetsuccess

         msgkernelload db 'Loading Kernel...',10
         kernelloadlen equ $-msgkernelload

         msgloadsuccess db 'Kernel loaded successfully...',10
         loadsuccesslen equ $-msgloadsuccess

         msgloadfailed db 'Kernel loaded failed...',10
         loadfailedlen equ $-msgloadfailed

Code: Select all

--------------------------------kernel.asm-------------------------------------
;org 0x7E00
section .text
mov bp,mymsg
mov cx,mymsglen
mov dx,0x0301
call display

call console  ;Simple Console

db 0x0ea   ;machine reboot
dw 0x0000
dw 0xffff

console:
mov bp,msgconsole
mov cx,msgconsolelen
mov dx,0x0401
call display

waitkey:
mov ah,0x00   ;wait for key
int 0x16

cmp al,27     ;check for ESC key
je finish

cmp al,13     ;check for Enter key
je newline

jmp echochar

newline:
mov bp,prompt
mov cx,promptlen
mov dx,0x0401
call display
jmp waitkey

echochar:
mov ah,0x0e
mov bx,0x0007
int 10h
jmp waitkey

finish:
ret

display:
mov ax,cs
mov ds,ax
mov es,ax

mov al,0x00
mov bh,0x00
mov bl,0x02
mov ah,0x13
int 0x10
ret

section .data
mymsg db "Message from sexhunter's Simple Kernel...",10,0
mymsglen equ $-mymsg

prompt db 10,'sexhunter@sexhunter$',10,0
promptlen equ $-prompt

msgconsole db 10,'Console:Press ESC to reboot',10,0
msgconsolelen equ $-msgconsole

Code: Select all

-------------------------------------boot.c---------------------------------------------
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int main ()
{
    char boot_buf[512];
    int floppy_desc,file_desc;
    file_desc=open ("./bootloader",O_RDONLY);
    read (file_desc,boot_buf,510);
    close (file_desc);

    boot_buf[510]=0x55;
    boot_buf[511]=0xAA;

    floppy_desc=open("/dev/sdb",O_RDWR);
    lseek (floppy_desc,0,SEEK_SET);
    write (floppy_desc,boot_buf,512);

    char boot_buf1[188]={0};
    file_desc=open ("./kernel",O_RDONLY);
    read (file_desc,boot_buf,188);
    close (file_desc);

    lseek (floppy_desc,512,SEEK_SET);
    write (floppy_desc,boot_buf,188);
    close (floppy_desc);

    return 0;
}
[EDIT] Code tags added (and email address removed) by Brendan[/EDIT]
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: hello,I am writing a bootloader and load a kernel

Post by piranha »

May I suggest posting a link to your code (so we don't have to scroll for 3 miles to read the thread), or at least using code flags?

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
thesexhunter
Posts: 2
Joined: Mon Mar 15, 2010 8:47 am

Re: hello,I am writing a bootloader and load a kernel

Post by thesexhunter »

ok,i am so sorry about that,i am new here
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: hello,I am writing a bootloader and load a kernel

Post by Combuster »

thesexhunter wrote:ok,i am so sorry about that,i am new here
Being new doesn't excuse you from reading the rules. It also means that you didn't read the links in there and probably spent too little effort on the solution yourself.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply