flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Boot loader woes

Author
Thread Post new topic Reply to topic
fancy



Joined: 13 Feb 2008
Posts: 3
fancy 16 Feb 2008, 04:59
I've started writing a boot loader for me real mode OS to be, It seems to fail at the point of loading the next sector (stage2). I'll post my code. Perhaps someone can impart some wisdom upon me.

Code:
;----------------------------------------------------------------------------;
; Fancy Loader version 0.1                                                   ;
;                                                                            ;
; Coded by Fancy                                                             ;
;                                                                            ;
; ``Do what thou wilt shall be the whole of the Law.''                       ;
;----------------------------------------------------------------------------;

;----------------------------------------------------------------------------;
; Stage 1                                                                    ;
;                                                                            ;
; Load the the stage 2 bootloader which loads the kernel.                    ;
;----------------------------------------------------------------------------;
use16
org 0x7c00

jmp start

;; FAT stuff goes here

start:
        mov si,stage1_msg
        call PrintString

        mov ax,stage2
        mov es,ax
        mov bx,0

        mov dl,0        ; drive number      = 0
        mov dh,0        ; head number       = 0
        mov ch,0        ; track number      = 0
        mov cl,2        ; sector number     = 2
        mov al,1        ; number of sectors = 1

        mov ah,2        ; BIOS 0x13 function 2

        int 0x13
        jc .read_error
        jmp stage2

.read_error:
        mov si,stage_err_msg
        call PrintString
        jmp $


;----------------------------------------------------------------------------;
; PrintString                                                                ;
;                                                                            ;
; Prints the string in si.                                                   ;
;----------------------------------------------------------------------------;
PrintString:
        mov ah,0x0e
        mov bh,0x00
        mov bl,0x07
@@:
        lodsb
        or al,al
        je .done
        int 0x10
        jmp @b
.done:
        ret

stage1_msg:     db      'Loading stage 2...',13,10,0
stage_err_msg:  db      'Failed to load stage 2.',13,10,0

;; Make sure stage 1 uses 1 whole sector
times 510-($-$$) db 0
dw 0xAA55

;----------------------------------------------------------------------------;
; Stage 2                                                                    ;
;                                                                            ;
; The stage 2 bootloader loads the kernel and such things.                   ;
;----------------------------------------------------------------------------;
stage2:
        mov si,stage2_msg
        call PrintString
        jmp $

stage2_msg:     db      'Loaded stage 2.',13,10,0
    
Post 16 Feb 2008, 04:59
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20401
Location: In your JS exploiting you and your system
revolution 16 Feb 2008, 05:03
A problem looks to be here:
Code:
        mov ax,stage2
        mov es,ax
        mov bx,0    

Perhaps you mean this instead:
Code:
        mov ax,0
        mov es,ax
        mov bx,stage2    
Post 16 Feb 2008, 05:03
View user's profile Send private message Visit poster's website Reply with quote
fancy



Joined: 13 Feb 2008
Posts: 3
fancy 16 Feb 2008, 05:07
Now it just hangs after "Loading stage 2..." Sad
Post 16 Feb 2008, 05:07
View user's profile Send private message Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 794
Location: Adelaide
sinsi 16 Feb 2008, 05:10
Always load your next stage at an absolute address (e.g. 0000:8000) and do a far jump to that explicit address.
Post 16 Feb 2008, 05:10
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20401
Location: In your JS exploiting you and your system
revolution 16 Feb 2008, 05:16
sinsi wrote:
Always load your next stage at an absolute address (e.g. 0000:8000) and do a far jump to that explicit address.
This is not necessary. You can use the near jump.

fnacy: Make sure you have written the second part of your assembled file to the disk at sector 2. Else you may be loading some random data.
Post 16 Feb 2008, 05:16
View user's profile Send private message Visit poster's website Reply with quote
fancy



Joined: 13 Feb 2008
Posts: 3
fancy 16 Feb 2008, 05:20
revolution wrote:
sinsi wrote:
Always load your next stage at an absolute address (e.g. 0000:8000) and do a far jump to that explicit address.
This is not necessary. You can use the near jump.

fnacy: Make sure you have written the second part of your assembled file to the disk at sector 2. Else you may be loading some random data.

I'm compiling it into one binary file. Stage 2 should start where the first sector ends. (it still hangs)

As far as absolute addresses go, why do I need a far jump if I am working in pure real mode?
Post 16 Feb 2008, 05:20
View user's profile Send private message Reply with quote
DJ Mauretto



Joined: 14 Mar 2007
Posts: 464
Location: Rome,Italy
DJ Mauretto 16 Feb 2008, 11:47
Hello Very Happy
I have correct your code,before to write some simply OS
learn asm Wink
Code:
;----------------------------------------------------------------------------; 
; Fancy Loader version 0.1                                                   ; 
;                                                                            ; 
; Coded by Fancy                                                             ; 
;                                                                            ; 
; ``Do what thou wilt shall be the whole of the Law.''                       ; 
;----------------------------------------------------------------------------; 

;----------------------------------------------------------------------------; 
; Stage 1                                                                    ; 
;                                                                            ; 
; Load the the stage 2 bootloader which loads the kernel.                    ; 
;----------------------------------------------------------------------------; 
use16 
org 0x7c00 

  jmp 0000h:start 

;; FAT stuff goes here 

start: 
      xor ax,ax
   mov ds,ax
        mov si,stage1_msg 
        call PrintString 

        xor ax,ax 
        mov es,ax 
        mov bx,7e00h 

        mov dl,0        ; drive number      = 0 
        mov dh,0        ; head number       = 0 
        mov ch,0        ; track number      = 0 
        mov cl,2        ; sector number     = 2 
        mov al,1        ; number of sectors = 1 

        mov ah,2        ; BIOS 0x13 function 2 

        int 0x13 
        jc .read_error 
        xor ax,ax
        mov ds,ax
        jmp 0000h:7e00h 

.read_error: 
        mov si,stage_err_msg 
        call PrintString 
        jmp $ 


;----------------------------------------------------------------------------; 
; PrintString                                                                ; 
;                                                                            ; 
; Prints the string in si.                                                   ; 
;----------------------------------------------------------------------------; 
PrintString: 
        mov ah,0x0e 
        mov bh,0x00 
        mov bl,0x07 
@@: 
        lodsb 
        or al,al 
        je .done 
        int 0x10 
        jmp @b 
.done: 
        ret 

stage1_msg:     db      'Loading stage 2...',13,10,0 
stage_err_msg:  db      'Failed to load stage 2.',13,10,0 

;; Make sure stage 1 uses 1 whole sector 
times 510-($-$$) db 0 
dw 0xAA55 

;----------------------------------------------------------------------------; 
; Stage 2                                                                    ; 
;                                                                            ; 
; The stage 2 bootloader loads the kernel and such things.                   ; 
;----------------------------------------------------------------------------; 

stage2: 
        mov si,stage2_msg 
        call PrintString 
        jmp $ 


stage2_msg:     db      'Loaded stage 2.',13,10,0     


note: Remember to set up also the stack,
it's better to have offset= 0 ,i have only correct your programs to run
but this is not right way to boot Shocked
Post 16 Feb 2008, 11:47
View user's profile Send private message Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 295
Alphonso 20 Feb 2008, 07:21
Fancy,
revolution has almost the right answer for you there but look at where your loading your sector, right on top of your stage2 routine, so this message 'Loaded stage 2.' will never be printed as it's overwritten. Either load further ahead after stage2_msg or move the stage2: routine to your boot sector.
Post 20 Feb 2008, 07:21
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20401
Location: In your JS exploiting you and your system
revolution 20 Feb 2008, 07:46
Alphonso wrote:
Fancy,
revolution has almost the right answer for you there but look at where your loading your sector, right on top of your stage2 routine, so this message 'Loaded stage 2.' will never be printed as it's overwritten. Either load further ahead after stage2_msg or move the stage2: routine to your boot sector.
The stage 2 code is what fancy wants to load, so loading it "right on top" is what needs to be done. The stage 2 code doesn't exist in memory until it is loaded from disc so there is really nothing to overwrite.
Post 20 Feb 2008, 07:46
View user's profile Send private message Visit poster's website Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 295
Alphonso 20 Feb 2008, 08:18
revolution wrote:
The stage 2 code is what fancy wants to load, so loading it "right on top" is what needs to be done. The stage 2 code doesn't exist in memory until it is loaded from disc so there is really nothing to overwrite.

Yep, your right. Embarassed My apologies, sometimes I see what I want to see instead of what's actually there Sad
Post 20 Feb 2008, 08:18
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4352
Location: Now
edfed 20 Feb 2008, 12:57
Code:
00000000      IVT
00000400      BDA
00000600      BOOT COPY, for some coders
00001000      BOOT n°2 ,for me, GDT, IDT, lib n°1
00007c00      BOOT n°1 ,the address for all PC
00010000      BOOT n°3
00020000
00030000
00040000
00050000     Drive buffer
....
00090000     screen 13 double buffer
000A0000     screen 13 memory
000B8000     screen 3 memory
....               
                   BIOS CODE & DATAs
....
00100000     1 MB mark, where to reload the tables, IDT, GDT, etc... 
00107c00     Where to reload the boot loader
00110000     1 MB + 64KB, where to reload the ORG 1000h code 
....
01000000     16MB, the DMA limit, where to put the vesa buffer
...


    


not really true, but can be a good start for your project!
Post 20 Feb 2008, 12:57
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.