Page 1 of 2
stage2 reboots my system
Posted: Tue Apr 14, 2009 1:21 am
by extremecoder
i have written two small codes for testing / learning stage1 and stage2 loaders ..
here is what the code is
stage1:
Code: Select all
[bits 16]
[org 0x07c00]
< print sample string >
mov bx, 0x1000
mov es, bx
mov bx, 0x0000
; read 512 bytes from sector 1 - assuming sector 0 will have stage 1
mov ah, 0x02
mov al, 0x01
mov ch, 0x00
mov cl, 0x01
mov dh, 0x00
mov dl, 0x00
int 0x13
JMP 0x10000
TIMES 510 - ($ - $$) DB 0
dw 0XAA55
stage2:
Code: Select all
[bits 16]
[org 0x10000]
< print sample string >
TIMES 512 - ($ - $$) DB 0
i have used dd to copy the entire bootloader (which is 1024 bytes) into floppy .. i have also used cat boot1.bin boot2.bin > bl.bin for merging both the bin files, as i don't have option to combine the DOS way
but when I run the OS, it's only printing the string from stage1 and not printing the string from stage2 or otherwise after printing the string from stage1 it reboots ...
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 1:41 am
by djmauretto
try this:
Code: Select all
stage1:
[bits 16]
[org 0x07c00]
xor ax,ax
mov ds,ax
mov ss,ax
or ax,0x7c00
mov sp,ax
< print sample string >
mov cx, 0x1000
mov es, cx
xor bx,bx
; read 512 bytes from sector 1 - assuming sector 0 will have stage 1
mov ax, 0x0201
mov cx,1
xor dx,dx
int 0x13
mov ax,0x1000
mov ds,ax
jmp 0x1000:0000
TIMES 510 - ($ - $$) DB 0
dw 0xAA55
stage2:
[bits 16]
[org 0]
< print sample string >
TIMES 512 - ($ - $$) DB 0
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 1:46 am
by egos
extremecoder wrote:stage1:
[bits 16]
[org 0x07c00]
< print sample string >
mov bx, 0x1000
mov es, bx
mov bx, 0x0000
; read 512 bytes from sector 1 - assuming sector 0 will have stage 1
mov ah, 0x02
mov al, 0x01
mov ch, 0x00
mov cl, 0x01
mov dh, 0x00
mov dl, 0x00
int 0x13
JMP 0x10000
Boy, you are being in RM with 16-bit segments... Replace
JUMP 0x10000 with
jmp 0x1000:0... and initialize the stack.
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 1:54 am
by egos
djmauretto, respect
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 1:54 am
by extremecoder
now it's continuously printing the stage1 one string
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 1:59 am
by egos
extremecoder wrote:now it's continuously printing the stage1 one string
Show the code, especially < print sample string >
Edited: Check on errors after calling int 13h. And maybe needs to use Reset function (0) before reading...
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 2:08 am
by extremecoder
here goes the code
Stage1:
Code: Select all
[bits 16]
[org 0x07C00]
; ----------------------------------------------------------------
; code section STARTS
; ----------------------------------------------------------------
start:
cli
mov ax, 0x0000
mov ds, ax
mov ss, ax
mov ax, 0x7c00
mov sp, ax
mov si, rstring
call printstring
call newline
mov si, stagestring
call printstring
call newline
mov ax, 0
mov bx, 0
mov cx, 0
mov dx, 0
mov bx, 0x1000
mov es, bx
mov bx, 0x0000
reset:
mov ax, 0
mov dl, 0
int 0x13
jc reset
readsector:
; mov ah, 0x02 ; service
; mov al, 0x03 ; number of sectors to read
; mov ch, 0x00 ; cylinder number
; mov cl, 0x01 ; sector number
; mov dh, 0x00 ; head number
; mov dl, 0x00 ; drive number
mov ax, 0x0201
mov cx, 1
xor dx, dx
int 0x13
jc readsector
mov ax, 0x1000
mov ds, ax
JMP 0x1000:0000
; ---------------------------------------------------------------
; code section ENDS
; ---------------------------------------------------------------
; ----------------------------------------------------------------
; core functions STARTS
; ----------------------------------------------------------------
printstring:
next_printstring:
lodsb
or al, al
jz exit_printstring
call printchar
jmp next_printstring
exit_printstring:
ret
printchar:
mov ah, 0x0e
mov bh, 0x0
mov bl, 0x07
int 0x10
ret
newline:
mov al, 0xa ; 10 - new line
call printchar
mov al, 0xd ; 13 - line feed
call printchar
ret
; --------------------------------------------------------------------
; core functions ENDS
; --------------------------------------------------------------------
; ------------------------------------------------------------------
; data section STARTS
; ------------------------------------------------------------------
rstring db 'rloader version 0.5. ', 0
stagestring db 'stage1 . ', 0
;-------------------------------------------------------------------
; data section ENDS
;-------------------------------------------------------------------
TIMES 510 - ($ - $$) db 0
DW 0xAA55
Stage2:
Code: Select all
[bits 16]
[org 0]
mov si, stage2
call printstring
JMP $
printstring:
next_printstring:
lodsb
or al, al
jz exit_printstring
call printchar
jmp next_printstring
exit_printstring:
ret
printchar:
mov ah, 0x0E
mov bh, 0x00
mov bl, 0x07
int 0x10
ret
stage2 db "stage 2 loader ! .", 13, 10, 0
TIMES 512 - ($ - $$) db 0
;DW 0x5577
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 2:30 am
by egos
Reset function can destroy es:bx pointer. Move setting up of this pointer immediately before calling the Read function. When Read function returns an error, you must call Reset function before reading anew.
Edited: And remove unbreakable cycles from your code.
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 2:50 am
by extremecoder
still no use
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 3:21 am
by egos
You are reading and running boot sector again and again. Read the sector 0, 0, 2
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 3:34 am
by extremecoder
ooops ... you are right, because it's working now .
but tell me one thing ... why are we changing the ds to 0x1000 ...
mov ax, 0x1000
mov ds, ax
jmp 0x1000:0000
why can't we directly jump to 0x1000:0000
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 4:00 am
by egos
extremecoder wrote:ooops ... you are right, because it's working now .
but tell me one thing ... why are we changing the ds to 0x1000 ...
mov ax, 0x1000
mov ds, ax
jmp 0x1000:0000
why can't we directly jump to 0x1000:0000
It's needs for the printstring function placed in stage2, not for transfer control. You can set up ds register in stage2 boot loader. It's even better.
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 4:19 am
by extremecoder
correct .. I have set ds in stage2 ..
in stage1 we are setting ds to 0x0000 and in stage2 we are setting ds to 0x1000 ... why is that we can't set value 0x0000 in stage2 also ?
once stage1 loads the stage2, in theory stage2 can sit in same place in memory ... so ds of stage2 can also be 0x0000 ... but when I do that in stage2, my stage2 is not working. but when I change ds to 0x1000 in stage2, then it's working ...
am I confused ... ??
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 4:45 am
by egos
extremecoder wrote:correct .. I have set ds in stage2 ..
in stage1 we are setting ds to 0x0000 and in stage2 we are setting ds to 0x1000 ... why is that we can't set value 0x0000 in stage2 also ?
Ask yourself
extremecoder wrote:once stage1 loads the stage2, in theory stage2 can sit in same place in memory ... so ds of stage2 can also be 0x0000 ... but when I do that in stage2, my stage2 is not working. but when I change ds to 0x1000 in stage2, then it's working ...
am I confused ... ??
I'm confused too
Probably because now stage2 places at 0x10000
Edited: If you will use my method, you could use zero segment address only. Load stage2 boot loader at 0x8000 linear address, then stage1 and stage2 will placed in same 64k-segment.
Re: stage2 reboots my system
Posted: Tue Apr 14, 2009 4:48 am
by kay10
I hope I explain it right, correct me if it's wrong
DS has to be 0x1000 because the whole code of the stage2 loader starts there.
Your "printstring" uses the segment in DS and the offset in SI, if DS was 0x0000, it would print something totally different,
because the adress of your stage2 message is 0x1000:stage2 (It was loaded there) and not 0x0000:stage2.
EDIT: egos were faster