Author Topic: 2 Stage Bootloader  (Read 24196 times)

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #30 on: July 25, 2014, 06:53:26 PM »
Thanks Frank I found the boot signature well I printed it with the code here it is :
Code: [Select]
mov es, WORD[DiskAddressPacket.Segment]
mov di, WORD[DiskAddressPacket.Offset]

xor bx, bx
.top
mov al, BYTE[ES:di+bx]
cmp al, ' '
je .Done
mov ah, 0xE
int 0x010
inc bx
jmp .top

.Done:

Now onto finding the root directory Apparently its not in the Volume descriptor so I have to go look for it in memory but yet again no idea how I'm Gonna go keep studying
« Last Edit: July 25, 2014, 09:36:42 PM by Anonymous »
Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #31 on: July 26, 2014, 01:30:15 AM »
I think I found the root but when I try and print it out I get these weird symbols is it a problem with my cPrint Function or Is it supposed to be like that?
I attached an image to my post to show you what it looks like.
here is the code:
Code: [Select]
BITS   16

ORG  0x00

Start: jmp main


;Colors for text
%DEFINE TEAL 0x03
%DEFINE RED 0x04
%DEFINE PURPLE 0x05
COL: db 0
ROW:  db 0

;macro for print
%macro Print 2
pusha
xor ax, ax
xor dx, dx
mov dh, BYTE[ROW];puts the row into the dh register
mov dl, BYTE[COL]
xor bx, bx
mov bl, %2
mov si, %1
call cPrint
mov BYTE[COL], dl
 ;saves the rows for the next time we need to print
popa
%endmacro

Print_ln:

pusha   
mov dh, BYTE[ROW]         
    mov ah, 0x02            ;set cursor pos
    mov bh, 0x00            ;page 00
    inc dh            ;row 00
    mov dl, 0x00            ;col. 00   
int 0x10
mov BYTE[ROW], dh
mov BYTE[COL], 0
popa


ret

cPrint:                   ; Routine: output string in SI to screen


 .top:
  ;Paramaters for Input
    mov ah, 09h             ; Must be 9 to print color
    mov cx, 0x01 ;x position
    lodsb                   ; Get character from string
    test al, al
    je .done                ; If char is zero, end of string
    int 0x10                 ; Otherwise, print it

    mov ah, 0x02 ;set cursor position
    mov bh, 0x00 ;page
    inc dl ;column
    int 0x10 ;changes the cursor position so the next char can be written at the new location
    jmp .top

 .done:
    ret

;clears the screen and sets the cursor position to the top left
 clear:
    mov ah, 0x0F            ;get current video mode
    mov al, 0x00            ;reset register
    int 0x10                ;get video mode
    mov ah, 0x00            ;set video mode
    int 0x10                ;reset screen
    mov ah, 0x02            ;set cursor pos
    mov bh, 0x00            ;page 00
    mov dh, 0x00            ;row 00
    mov dl, 0x00            ;col. 00
    int 0x10                ;set pos
ret




Read_Sectors: 
        ;/* Read the sector into memory. */
       
.ForLoop:
mov     ah,042h
xor     al,al
mov     si, DiskAddressPacket
mov     dl, [CDDriveNumber]
int     013h
        jnc    .Success ; /* read error? */

        Print Read_Sector_Error_MSG, RED

cli
hlt

.Success:
Print Progress_MSG , PURPLE
inc WORD[DiskAddressPacket.SectorsToRead]

        loop    .ForLoop
call Print_ln
ret


main:

cli
mov ax, 0x07c0 ;adjust the segment registers
mov ds, ax
mov gs, ax
mov fs, ax


Create_Stack:
xor ax, ax
mov es, ax
mov ss, ax
mov sp ,0x0FFFE
sti

mov     [CDDriveNumber],dl
call clear


Print W_MSG, TEAL;prints the loading message in colour
call Print_ln


;First find the Signature of the CD
LOAD_SIGNATURE:
mov cx, 0x04
call Read_Sectors

Print READ_SUCCESS, TEAL
call Print_ln

;load the Volume descriptor to the Volume variable
mov es, WORD[DiskAddressPacket.Segment]
mov di, WORD[DiskAddressPacket.Offset]

xor bx, bx
.top:
mov al, BYTE[ES:DI+BX]
mov BYTE[VOLUME+BX], al

inc bx
cmp al, ' '
je .Done
jmp .top
.Done:

;see if the Volume descriptor contains the Signature
xor BX, BX
add BX, 0x01
xor cx, cx
.toploop:
xor ax, ax
mov al, BYTE[VOLUME+BX]
cmp al, BYTE[CD_Signature+BX-1]
je .FOUND_IT
jmp .Done2
inc CX
.FOUND_IT:
Print Progress_MSG, PURPLE
inc BX

jmp .toploop

.Done2:
cmp CX, 0
jne FAIL
call Print_ln

Print FOUND_CD, TEAL
jmp LOAD_ROOT
FAIL:
Print FILE_NOT_FOUND, RED

;Now Load the Root Directory from the Volume Descriptor
LOAD_ROOT:
mov es, WORD[DiskAddressPacket.Segment]
mov di, WORD[DiskAddressPacket.Offset]

call Print_ln
MOV AL,[ES:DI]                      ; Length of the current directory entry
MOV [CD_dir_curr_size],AL
Print CD_dir_curr_size, TEAL
call Print_ln

MOV EAX,[ES:DI+2]                   ; Starting sector of directory entry
MOV [CD_root_dir_start],EAX
Print CD_root_dir_start, TEAL
call Print_ln
MOV EAX,[ES:DI+10]                  ; Size of directory entry on CD/DVD/BD
MOV [CD_root_dir_size],EAX
Print CD_root_dir_size, TEAL
call Print_ln

MOV AL,[ES:DI+32]                   ; File's name length (see El Torito of ISO:9660 or CDROM.ASM)
MOV [CD_FileNameLength],AL

Print CD_FileNameLength, TEAL


call Print_ln

XOR BX,BX                           ; Initialize BX
XOR CX,CX                           ; Initialize CX
MOV SI,DI









Sector_Size: dw   512
CDDriveNumber: db   0x080
CD_bytes_per_sect:          dw    0
CD_root_dir_size:          dd    0
CD_root_dir_sectors:        dw    0
CD_root_dir_start:          dd    0
CD_file_size:              dd    0
CD_file_sectors:            dw    0
CD_file_start:              dd    0
CD_desc_sector:            dd    0
CD_Signature:     db    "CD001"
CD_FILE_VER:     db    0x01
CD_FileNameLength: db   0x0
CD_dir_curr_size: db 0x0
joliet_signature:        db    025h,02Fh,045h
;Disk Address Packet


DiskAddressPacket:          db 0x010,0  
.SectorsToRead:             dw 1                              ; Number of sectors to read (read size of OS)
.Offset:                    dw 0                              ; Offset :0000
.Segment:                   dw 0x0200                         ; Segment 0200
.End:                       dq 0x010                             ; Sector 16 or 10h on CD-ROM

VOLUME: DB 0

W_MSG: db "Loading Z-Boot", 0
KERNEL: db "KRNL.BIN"
Read_Sector_Error_MSG: db "Error, failed to read sector",0
READ_SUCCESS: db "Sectors read correctly",0
Progress_MSG: db ".",0
FILE_NOT_FOUND: db "Error, file not found",0
FOUND_CD: db "Found the CD Signature", 0
times 2046 - ($ - $$) db 0; padd out the rest of the file to 0
DW 0xAA55; boot signature







« Last Edit: July 26, 2014, 02:37:30 AM by Anonymous »
Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #32 on: July 26, 2014, 05:28:35 AM »
NeverMind I found out I needed to use the right offsets in order to find the Root
Here is what I did to solve it
Code: [Select]
READ_STAGE2:
Print LOADING_STAGE2_MSG, TEAL
call Print_ln
mov es, [DiskAddressPacket.Segment]
mov di, [DiskAddressPacket.Offset]

    xor BX, BX
xor si, si

    .top:

MOV AL,[ES:DI+BX] ;starting address
cmp AL,BYTE[STAGE2]
je .Done
cmp AL,BYTE[STAGE2]
je .FAIL
INC BX
jmp .top

.Done:
Print Found_Possible_FILE, TEAL
call Print_ln
XOR SI, SI;Clear out for use
;INC BX
;INC SI
xor cx, cx;clear out for use as counter

.top2:
xor ax, ax

MOV AL, BYTE[ES:DI+BX]

cmp AL, BYTE[STAGE2+SI]

je .Success
call Print_ln
jmp .top
.Success:

Print Progress_MSG, PURPLE


INC BX
INC SI
INC CX
cmp CX, WORD[STAGE_2_LEN]
jne .top2
call clear

Print File_Found, TEAL
call Print_ln

;call clear
SUB BX, 10
ADD DI, BX

;jump to where the file is located and run it
JMP [ES:DI]

.FAIL:
call Print_ln
Print FILE_NOT_FOUND, RED
cli
hlt


ret

Now I am trying to jump to the file because I found it and run the code there but its not working But at least I found the file :D
here is the stage2 code:
Code: [Select]
[BITS 16]
[ORG 0x500]

Start: jmp main

%DEFINE TEAL 0x03
%DEFINE RED 0x04
%DEFINE PURPLE 0x05
COL: db 0
ROW:  db 0
%macro Print 2
pusha
xor ax, ax
xor dx, dx
mov dh, BYTE[ROW];puts the row into the dh register
mov dl, BYTE[COL]
xor bx, bx
mov bl, %2
mov si, %1
call cPrint
mov BYTE[COL], dl
 ;saves the rows for the next time we need to print
popa
%endmacro

Print_ln:

pusha   
mov dh, BYTE[ROW]         
    mov ah, 0x02            ;set cursor pos
    mov bh, 0x00            ;page 00
    inc dh            ;row 00
    mov dl, 0x00            ;col. 00   
int 0x10
mov BYTE[ROW], dh
mov BYTE[COL], 0
popa


ret

cPrint:                   ; Routine: output string in SI to screen


 .top:
  ;Paramaters for Input
    mov ah, 09h             ; Must be 9 to print color
    mov cx, 0x01 ;x position
    lodsb                   ; Get character from string
    test al, al
    je .done                ; If char is zero, end of string
    int 0x10                 ; Otherwise, print it

    mov ah, 0x02 ;set cursor position
    mov bh, 0x00 ;page
    inc dl ;column
    int 0x10 ;changes the cursor position so the next char can be written at the new location
    jmp .top

 .done:
    ret

;clears the screen and sets the cursor position to the top left
 clear:
    mov ah, 0x0F            ;get current video mode
    mov al, 0x00            ;reset register
    int 0x10                ;get video mode
    mov ah, 0x00            ;set video mode
    int 0x10                ;reset screen
    mov ah, 0x02            ;set cursor pos
    mov bh, 0x02         ;page 00
    mov dh, 0x00            ;row 00
    mov dl, 0x00            ;col. 00
    int 0x10                ;set pos
MOV BYTE[ROW], DH
MOV BYTE[COL], DL
ret





main:
cli ; clear interrupts
xor ax, ax ; null segments
mov ds, ax
mov es, ax
mov ax, 0x9000 ; stack begins at 0x9000-0xffff
mov ss, ax
mov sp, 0xFFFF
sti
;call clear
               ;set pos
call Print_ln
Print LOAD_SUCCESS, TEAL
call Print_ln





LOAD_SUCCESS: db "Stage 2 of the bootloader has loaded successfully!",0






« Last Edit: July 26, 2014, 08:22:10 PM by Anonymous »
Thanks in advance, Anonymous

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2358
  • Country: us
Re: 2 Stage Bootloader
« Reply #33 on: July 27, 2014, 11:51:15 AM »
I'll take your word for it that you've found the file...

Code: [Select]
;jump to where the file is located and run it
JMP [ES:DI]
A near indirect jump to a word found at [es:di]? I doubt it.

Try...
Code: [Select]
    push es
    push di
    retf

Why is stage2 "org 0x500"? Is that where the file is loaded?

Best,
Frank


Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #34 on: July 27, 2014, 04:57:41 PM »
Here is the updated code for it It still isn't jumping to it correctly:
Code: [Select]
[BITS   16]

[ORG  0x00]

Start: jmp main


;Colors for text
%DEFINE TEAL 0x03
%DEFINE RED 0x04
%DEFINE PURPLE 0x05
COL: db 0
ROW:  db 0

;macro for print
%macro Print 2
pusha
xor ax, ax
xor dx, dx
mov dh, BYTE[ROW];puts the row into the dh register
mov dl, BYTE[COL]
xor bx, bx
mov bl, %2
mov si, %1
call cPrint
mov BYTE[COL], dl
 ;saves the rows for the next time we need to print
popa
%endmacro

Print_ln:

pusha   
mov dh, BYTE[ROW]         
    mov ah, 0x02            ;set cursor pos
    mov bh, 0x00            ;page 00
    inc dh            ;row 00
    mov dl, 0x00            ;col. 00   
int 0x10
mov BYTE[ROW], dh
mov BYTE[COL], 0
popa


ret

itoa:;number is passed into ax
jmp .beggining
.negate:

neg eax
push eax

mov al, '-'
mov ah, 0xe
int 0x10
pop ax
jmp .top
.beggining:
xor ebx , ebx
mov ecx, 10;mov into cx 10
cmp eax, 0
jl .negate


.top:
;divide by 10 and push remainder onto stack
xor edx, edx;clear out remainder
div ecx ;divide ax by 10
push edx;push the remainder onto the stack for later
inc ebx;count the number of digits
test eax,eax;if ax = 0 then stop
jne .top

.loop:
pop eax;restore the remainder
add eax, '0';convert to ASCII
mov ah, 0xe;print
int 0x10
dec ebx;get ready for the next digit
cmp ebx, 0;if not zero then jump to .loop
jne .loop
ret

cPrint:                   ; Routine: output string in SI to screen


 .top:
  ;Paramaters for Input
    mov ah, 09h             ; Must be 9 to print color
    mov cx, 0x01 ;x position
    lodsb                   ; Get character from string
    test al, al
    je .done                ; If char is zero, end of string
    int 0x10                 ; Otherwise, print it

    mov ah, 0x02 ;set cursor position
    mov bh, 0x00 ;page
    inc dl ;column
    int 0x10 ;changes the cursor position so the next char can be written at the new location
    jmp .top

 .done:
    ret

;clears the screen and sets the cursor position to the top left
 clear:
    mov ah, 0x0F            ;get current video mode
    mov al, 0x00            ;reset register
    int 0x10                ;get video mode
    mov ah, 0x00            ;set video mode
    int 0x10                ;reset screen
    mov ah, 0x02            ;set cursor pos
    mov bh, 0x01            ;page 00
    mov dh, 0x00            ;row 00
    mov dl, 0x00            ;col. 00
    int 0x10    ;set pos
mov BYTE[ROW], DH
mov BYTE[COL],0
ret




Read_Sectors: 
        ;/* Read the sector into memory. */
       
.ForLoop:
mov     ah,042h
xor     al,al
mov     si, DiskAddressPacket
mov     dl, [CDDriveNumber]
int     013h
        jnc    .Success ; /* read error? */

        Print Read_Sector_Error_MSG, RED

cli
hlt

.Success:
Print Progress_MSG , PURPLE
inc WORD[DiskAddressPacket.SectorsToRead]

        loop    .ForLoop
call Print_ln
ret
CHECK_DESC:
Print CHECK_DESC_MSG, TEAL
mov es, WORD[DiskAddressPacket.Segment]
mov di, WORD[DiskAddressPacket.Offset]

xor bx, bx
.top:
mov al, BYTE[ES:DI+BX]
mov BYTE[VOLUME+BX], al

inc bx
cmp al, ' '
je .Done
jmp .top
.Done:

;see if the Volume descriptor contains the Signature
xor BX, BX; clear out bx
add BX, 0x01;move into bx the offset
xor cx, cx;clear out cx
.toploop:
xor ax, ax
mov al, BYTE[VOLUME+BX]
cmp al, BYTE[CD_Signature+BX-1]
je .FOUND_IT; Compare the letters Byte by Byte to see if they are the same
jmp .Done2
inc CX;increments if even one letter is wrong
.FOUND_IT:
Print Progress_MSG, PURPLE
inc BX;Increments the offset

jmp .toploop

.Done2:
cmp CX, 0;if signatures don't match then stop the system and print an error Message
jne .FAIL
call Print_ln

Print FOUND_CD, TEAL
jmp .Done3
.FAIL:
Print FILE_NOT_FOUND, RED
cli
hlt
.Done3:
call Print_ln
ret
READ_STAGE2:
Print LOADING_STAGE2_MSG, TEAL;prints the loading message
call Print_ln
;the adress is already stored in ES:DI

MOV ES, WORD[DiskAddressPacket.Segment]
MOV DI, WORD[DiskAddressPacket.Offset]
    xor BX, BX;initialize bx
xor si, si ;initialize si

    .top:

MOV AL,BYTE[ES:DI+BX] ;moves in letter of file name into al
cmp AL,BYTE[STAGE2];compares chars
je .Done;if the same then jump to the next label
cmp AL,BYTE[STAGE2];if not the same then the file wasn't found
je .FAIL
INC BX
jmp .top

.Done:
Print Found_Possible_FILE, TEAL;Prints the possible file found message
call Print_ln
XOR SI, SI;Clear out for use
;INC BX
;INC SI
xor cx, cx;clear out for use as counter

.top2:;compares strings to see if they match
xor ax, ax

MOV AL, BYTE[ES:DI+BX]

cmp AL, BYTE[STAGE2+SI]

je .Success
call Print_ln
jmp .top
.Success:

Print Progress_MSG, PURPLE


INC BX
INC SI
INC CX
cmp CX, WORD[STAGE_2_LEN]
jne .top2
call clear

Print File_Found, TEAL
call Print_ln


SUB BX, 10
ADD DI, BX

LEA EAX, [ES:DI];gets the address of the start of the file

call itoa;prints it
call Print_ln

Print Reading_Sectors, TEAL;Print the reading sectors message

;loads the address into the DAP
    MOV WORD[DiskAddressPacket.Segment], ES
MOV WORD[DiskAddressPacket.Offset], DI
;reads the sectors with the new address
;XOR AX, AX
;MOV AX, WORD[DiskAddressPacket.End]

;MOV WORD[DiskAddressPacket.SectorsToRead], 0
mov cx, 0x04
call Read_Sectors

Print READ_SUCCESS, TEAL;Prints that read was a success
call Print_ln

mov     dl, [CDDriveNumber];puts the drive number into dl for the next stage to use
;xor     si,si

push ES
push DI
retf;jumps to the next stage or tries to jump error here

.FAIL:
call Print_ln
Print FILE_NOT_FOUND, RED;print file not found
cli;halt the system
hlt


ret


main:
;first stage of bootloader is loaded at the address 0x07c0:0x0FFFE
;second stage of bootloader is loaded at address 0x9000:0x0FFFF
cli
mov ax, 0x07c0 ;adjust the segment registers
mov ds, ax
mov gs, ax; stack begins at 0x9000-0xffff
mov fs, ax


Create_Stack:
xor ax, ax
mov es, ax
mov ss, ax
mov sp ,0x0FFFE
sti

mov     [CDDriveNumber],dl
;call clear


Print W_MSG, TEAL;prints the loading message in colour
call Print_ln


;First find the Signature of the CD
Print Reading_Sectors, TEAL
LOAD_SIGNATURE:
mov cx, 0x04
call Read_Sectors

Print READ_SUCCESS, TEAL
call Print_ln
;load the Volume descriptor to the Volume variable
call CHECK_DESC
;Now Load the Root Directory from the Volume Descriptor
LOAD_ROOT:
;Print Reading_Sectors, TEAL
mov es, WORD[DiskAddressPacket.Segment]
mov di, WORD[DiskAddressPacket.Offset]

XOR BX, BX
MOV BX, 40 ;move in the offset
VolumeLabelLoop:

MOV CL,[ES:DI+BX]                   ; Grab a letter
CMP CL,' '                          ; Is it a space? (Assumes end of string is space, may run out)
JE .VolumeLabelDone                 ; Yes, we are done

MOV [VOLUME+BX-40],CL
INC BX
JMP VolumeLabelLoop                 ; Need to compare BX to length of Volume Label on CD (32?)

.VolumeLabelDone:

MOV byte [VOLUME+BX-40],0      ; End the string

MOV EAX,[ES:DI+158]                 ; LBA of root directory, where all things start.
MOV [DiskAddressPacket.End],EAX     ; Load packet with new address on CD of the root directory

xor cx, cx
mov cx, 0x01
Print Reading_Sectors, TEAL
call Read_Sectors                          ; Call read sector from drive
           
Print READ_SUCCESS, TEAL;if the program gets here it means it was a success
call Print_ln
LOAD_STAGE2:
call READ_STAGE2






Sector_Size: dw   512
CDDriveNumber: db   0x080
CD_bytes_per_sect:          dw    0
CD_root_dir_size:          dd    0
CD_root_dir_sectors:        dw    0
CD_root_dir_start:          dd    0
CD_file_size:              dd    0
CD_file_sectors:            dw    0
CD_file_start:              dd    0
CD_desc_sector:            dd    0
CD_Signature:     db    "CD001"
CD_FILE_VER:     db    0x01
CD_FileNameLength: db   0x0
CD_dir_curr_size: db 0x0
joliet_signature:        db    025h,02Fh,045h
Reading_Sectors: db "Reading sectors", 0
CHECK_DESC_MSG: db "Checking for CD Signature",0
LOADING_STAGE2_MSG: db "Loading Stage 2 of boot loader",0
STAGE_2_LEN: db 0xA
File_Found: db "File for Stage 2 of the bootloader was successfully loaded!!",0
LOADING_STAGE2_FAILED: db  "Failed to load Stage 2 of the boot loader !!!!!",0
Found_Possible_FILE: db "Found Possible File",0
;Disk Address Packet


DiskAddressPacket:          db 0x010,0  
.SectorsToRead:             dw 1                              ; Number of sectors to read (read size of OS)
.Offset:                    dw 0                              ; Offset :0000
.Segment:                   dw 0x0200                         ; Segment 0200
.End:                       dq 0x010                             ; Sector 16 or 10h on CD-ROM

VOLUME: DB 0

W_MSG: db "Loading Z-Boot", 0
STAGE2: db "STAGE2.BIN"
Read_Sector_Error_MSG: db "Error, failed to read sector",0
READ_SUCCESS: db "Sectors read correctly!",0
Progress_MSG: db ".",0
FILE_NOT_FOUND: db "Error, file not found!",0
FOUND_CD: db "Found the CD Signature!", 0
times 2046 - ($ - $$) db 0; padd out the rest of the file to 0
DW 0xAA55; boot signature
Updated code for Second stage
Code: [Select]
[BITS 16]
[ORG 0x0100]

Start: jmp main

%DEFINE TEAL 0x03
%DEFINE RED 0x04
%DEFINE PURPLE 0x05
COL: db 0
ROW:  db 0
%macro Print 2
pusha
xor ax, ax
xor dx, dx
mov dh, BYTE[ROW];puts the row into the dh register
mov dl, BYTE[COL]
xor bx, bx
mov bl, %2
mov si, %1
call cPrint
mov BYTE[COL], dl
 ;saves the rows for the next time we need to print
popa
%endmacro

Print_ln:

pusha   
mov dh, BYTE[ROW] 
    mov ah, 0x02            ;set cursor pos
    mov bh, 0x00            ;page 00
    inc dh            ;row 00
    mov dl, 0x00            ;col. 00   
int 0x10
mov BYTE[ROW], dh
mov BYTE[COL], 0
popa


ret
itoa:;number is passed into ax
jmp .beggining
.negate:

neg ax
push ax

mov al, '-'
mov ah, 0xe
int 0x10
pop ax
jmp .top
.beggining:
xor bx , bx
mov cx, 10;mov into cx 10
cmp ax, 0
jl .negate


.top:
;divide by 10 and push remainder onto stack
xor dx, dx;clear out remainder
div cx ;divide ax by 10
push dx;push the remainder onto the stack for later
inc bx;count the number of digits
test ax,ax;if ax = 0 then stop
jne .top

.loop:
pop ax;restore the remainder
add ax, '0';convert to ASCII
mov ah, 0xe;print
int 0x10
dec bx;get ready for the next digit
cmp bx, 0;if not zero then jump to .loop
jne .loop
ret

cPrint:                   ; Routine: output string in SI to screen


 .top:
  ;Paramaters for Input
    mov ah, 09h             ; Must be 9 to print color
    mov cx, 0x01 ;x position
    lodsb                   ; Get character from string
    test al, al
    je .done                ; If char is zero, end of string
    int 0x10                 ; Otherwise, print it

    mov ah, 0x02 ;set cursor position
    mov bh, 0x00 ;page
    inc dl ;column
    int 0x10 ;changes the cursor position so the next char can be written at the new location
    jmp .top

 .done:
    ret

;clears the screen and sets the cursor position to the top left
 clear:
    mov ah, 0x0F            ;get current video mode
    mov al, 0x00            ;reset register
    int 0x10                ;get video mode
    mov ah, 0x00            ;set video mode
    int 0x10                ;reset screen
    mov ah, 0x02            ;set cursor pos
    mov bh, 0x01         ;page 00
    mov dh, 0x00            ;row 00
    mov dl, 0x00            ;col. 00
    int 0x10                ;set pos
MOV BYTE[ROW], DH
MOV BYTE[COL], DL
ret





main:
;first stage of bootloader is loaded at the address 0x07c0:0x0FFFE
;second stage of bootloader is loaded at address 0x9000:0x0FFFF
cli
mov ax, 0x9000 ;adjust the segment registers
mov ds, ax
mov gs, ax
mov fs, ax


Create_Stack:
xor ax, ax
mov es, ax
mov ss, ax
mov sp ,0x0FFFF
sti

               
call Print_ln
Print LOAD_SUCCESS, TEAL
call Print_ln





LOAD_SUCCESS: db "Stage 2 of the bootloader has loaded successfully!",0






Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #35 on: July 27, 2014, 05:06:42 PM »
Hmmm maybe I haven't found the file because whenever I jump to it it does print some code but its not supposed to be printed here let me attach the png am I going through the root incorrectly ??? It is jumping somewhere because it is executing code it shouldn't be once it makes that jump it should be out of the file. By the way the number not in color is the address at [ES:DI]
« Last Edit: July 27, 2014, 06:00:52 PM by Anonymous »
Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #36 on: July 27, 2014, 09:21:12 PM »
OK I have changed it to see the real adress where it is reading and it looks like it is way off here is the code for the read stage 2 function:
Code: [Select]
READ_STAGE2:
        Print LOADING_STAGE2_MSG, TEAL;prints the loading message
        call Print_ln
;the adress is already stored in ES:DI
        ;add DI, 158
xor BX, BX;initialize bx
        xor si, si ;initialize si
         
    .top:
                 
                MOV AL,BYTE[ES:DI+BX] ;moves in letter of file name into al
                cmp AL,BYTE[STAGE2];compares chars
                je .Done;if the same then jump to the next label
                cmp AL,BYTE[STAGE2];if not the same then the file wasn't found
                je .FAIL
                INC BX
        jmp .top
         
        .Done:
        Print Found_Possible_FILE, TEAL;Prints the possible file found message
        call Print_ln
        XOR SI, SI;Clear out for use
        ;INC BX
        ;INC SI
        xor cx, cx;clear out for use as counter
         
        .top2:;compares strings to see if they match
                xor ax, ax
                 
                MOV AL, BYTE[ES:DI+BX]
                 
                cmp AL, BYTE[STAGE2+SI]
                 
                je .Success
                call Print_ln
                jmp .top
                .Success:
                         
                        Print Progress_MSG, PURPLE
                                                 
                         
                        INC BX
                        INC SI   
                        INC CX
        cmp CX, WORD[STAGE_2_LEN] 
        jne .top2
        call clear
         
        Print File_Found, TEAL
        call Print_ln
         
         
        SUB BX, 10
        ADD DI, BX
         
     
     

        Print Reading_Sectors, TEAL;Print the reading sectors message
         
        ;loads the address into the DAP
MOV WORD[DiskAddressPacket.Segment], ES
MOV WORD[DiskAddressPacket.Offset],  DI
        ;reads the sectors with the new address
xor cx, cx
mov cx, 0x04
call Read_Sectors     
         
        Print READ_SUCCESS, TEAL;Prints that read was a success
        call Print_ln
         
        mov     dl, [CDDriveNumber];puts the drive number into dl for the next stage to use
        ;xor     si,si
       

;Prints out the segment and offset ES:DI for debugging
mov ax ,es
       
        call itoa;prints it
       
mov al, BYTE[Colon]
mov ah, 0xe
int 0x010

mov ax ,di;gets the address of the start of the file
       
        call itoa;prints it
        call Print_ln
jmp  0x0200:0x0;jumping here


        .FAIL:
        call Print_ln
        Print FILE_NOT_FOUND, RED;print file not found
        cli;halt the system
        hlt
         
         
ret
Here is what the output looks like :
Thanks in advance, Anonymous

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2358
  • Country: us
Re: 2 Stage Bootloader
« Reply #37 on: July 27, 2014, 10:27:22 PM »
Judging from the number you print, the sector you read (if success) goes at 0x200:0x1CE. Then you jump to 0x200:0. This looks like it could be a problem.

Best,
Frank


Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #38 on: July 27, 2014, 10:43:31 PM »
So I tried jumping to 0x0200:0x1CE and no change for some reason, I am jumping to that location because that is where the result of the read sectors is stored or at least it should be I might check it again
« Last Edit: July 27, 2014, 11:05:08 PM by Anonymous »
Thanks in advance, Anonymous

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2358
  • Country: us
Re: 2 Stage Bootloader
« Reply #39 on: July 27, 2014, 11:04:41 PM »
Seems it would make more sense to load it at 0x200:0... or perhaps at 0x200:0x100 since that seems to be your latest (apparently random) "org" in stage2.

Best,
Frank


Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: 2 Stage Bootloader
« Reply #40 on: July 27, 2014, 11:11:09 PM »
I didn't had a close look at your code but I saw this:

Code: [Select]
    .top:
                 
                MOV AL,BYTE[ES:DI+BX] ;moves in letter of file name into al
                cmp AL,BYTE[STAGE2];compares chars
                je .Done;if the same then jump to the next label
                cmp AL,BYTE[STAGE2];if not the same then the file wasn't found
                je .FAIL
                INC BX
        jmp .top

and it looks senseless to me. What are you doing here? Two identical cmp instructions followed by identical jcc instructions to different locations??



btw:

Code: [Select]
MOV AL,BYTE[ES:DI+BX] ;moves in letter of file name into al
If you hadn't told me that you move something into al .... ;)
Please comment your code! It helps to help you.

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #41 on: July 27, 2014, 11:13:44 PM »
I tried loading to that too and it still is giving me the same output hmmm Should I give it a new origin? I thought you could give it whatever you want according to the memory map of course and I think thats ok right?
Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #42 on: July 27, 2014, 11:16:17 PM »
Gammac I am comparing the the file name to the string of the file name I wan't to find if it is the same then I have found the file subtract the offset by the length of the file name and add it to DI.
Also I got a different output this time there is no random prints after I print there address but there is nothing after it at all which still means I failed to jump to it I tried jmp [ES:DI] still didn't work Man this is a real conundrum isn't it im gonna keep hacking at it to see what I get.
« Last Edit: July 27, 2014, 11:22:22 PM by Anonymous »
Thanks in advance, Anonymous

Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: 2 Stage Bootloader
« Reply #43 on: July 27, 2014, 11:19:52 PM »
but this branch

Code: [Select]
je .FAIL
will never happened
Please comment your code! It helps to help you.

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #44 on: July 27, 2014, 11:24:22 PM »
Almost forgot to post output here it is:
Thanks in advance, Anonymous