What is the problem with following boot loader code ??

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
wah_java

What is the problem with following boot loader code ??

Post by wah_java »

Hello,
When executed with bochs-2.1.1, the bochs rebooted while switching to protected mode.
What is the problem with the following code ?
;
;**********************************
;***********Boot Loader************
;**********************************
;

;
; This loader
;       o will load the 2 Sector (1024 bytes) kernel in the memory
;       o switches to protected mode
;       o jump to the kernel
;

org 0x7c00

start:
; setting the stack for loading the program

xor ax, ax ; first we will set up the stack
cli
    mov ss,ax
mov sp, 0xffff
sti

mov [bootdrv], dl ; dl contains the bootdisk name ie; dl = 0 if floppy a:\

call load ; function to load the kernel from disk into memory

mov si, msgloadsuccess
call putstr

    mov ax, 0h
    int 16h
;    jmp init

cli ; no interrupts allowed !

; first we move the system to it's rightful place

mov ax,0x0000
cld ; 'direction'=0, movs moves forward
do_move:
mov es,ax ; destination segment
add ax,0x1000
cmp ax,0x9000
jz end_move
mov ds,ax ; source segment
sub di,di
sub si,si
mov cx,0x8000
rep
movsw
jmp do_move

; then we load the segment descriptors

end_move:

    lgdt    [gdtr]        ; load gdt with whatever appropriate


; Load the GDTR with the base address and limit of the GDT.

    lgdt [gdtr]

    mov si, msgloadgdt
    call putstr

    mov ax, 0h
    int 16h

;
; Set the PE [protected mode enable] bit in register CR0 to begin the
; switch to protected mode.
;
    cli
    mov eax,cr0
    or al,1
    mov cr0,eax
    jmp 08h:init

; loader variables

bootdrv db 0
msgresetfail db 'Disk Reset Failure!', 13, 10, 0
msgresetsuccess db 'Disk reset success...', 13, 10, 0
msgkernelload db 'Loading kernel...', 13, 10, 0
msgloadsuccess db 'Kernel loaded successfully...', 13, 10, 0
msgprot         db 'We are in protected mode...', 13, 10, 0
msgloadgdt db 'GDT Loaded...', 13, 10, 0

msghang db '.', 0


load:

; we have to reset the disk before it move to the loaded program
; say our real program is stored in sector 2
; let's load it

push ds ; reset disk system
mov ax, 0 ; forces controller to recalibrate drive heads (seek to track 0)
mov dl, [bootdrv]
int 13h
pop ds
jc resetfail

mov si, msgresetsuccess
call putstr

    xor ax,ax    ; loads sector into memory
mov es,ax
mov ah,2
    mov al,2    ; 2 Sector Kernel
mov dx,0
mov cx,2 ; ch = cylinder number and cl = sector number 1-63

    mov bx,7e00h    ; 7e00h = 7c00h + 512 ... loading my program here makes it easy for me
    int 13h

jc load ; if fail then try to load it again

mov si, msgkernelload
call putstr

retn

resetfail:

mov si, msgresetfail
call putstr
retn

putstr:
lodsb
or al,al
jz short putstrd
mov ah,0x0E
mov bx,0x0007
int 0x10
jmp putstr
putstrd:
retn

gdtr:
    dw     end-gdt-1
    dd     gdt

gdt:
dw 0,0,0,0 ; dummy

    dw  0xffFF      ; ffffh limit
    dw  0x0000      ; base address 0
    dw  0x9A00      ; Execute/Read Code Segment
    dw  0x00C0      

    dw  0xffFF      ; ffffh limit
    dw  0x0000      ; Base Address 0
    dw  0x9200      ; R/W Data Segment
    dw  0x00C0    
end:

;hangs if kernel not loaded
oops:
mov si, msghang
call putstr
jmp oops

retf

bits 32

init:
    sti

    jmp 0x07e00 ; Jump to the kernel @ 7e00h = 7c00h + 512

times 510-($-$$) db 90 ; Filling the remaining free space in the sector
dw 0AA55h
Thanks
Post Reply