flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Jumping to new file CD Bootloader

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
AnonymousUser



Joined: 25 Jul 2014
Posts: 32
AnonymousUser 27 Jul 2014, 04:25
Hey all I have been working on a 2 stage CD Boot-Loader and I have recently run into trouble with jumping to the next file which is stage 2 here is my code:
Code:
[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
        
    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
        mov cx, 0x01
        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
        
  jmp 0x0100:0x000;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


    

Here is stage 2:
Code:
[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
Post 27 Jul 2014, 04:25
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20355
Location: In your JS exploiting you and your system
revolution 27 Jul 2014, 10:01
I expect that if you wrote your code in fasm syntax then you might get more help.
Post 27 Jul 2014, 10:01
View user's profile Send private message Visit poster's website Reply with quote
AnonymousUser



Joined: 25 Jul 2014
Posts: 32
AnonymousUser 27 Jul 2014, 10:34
Ya I know this is the FASM forum but I didn't know where else to go since OS DEV doesen't send me my activation email for some reason sorry for not writing in FASM Sad I feel like I have tried everything to jump to that next stage I know my code isn't written in FASM syntax but maybe someone could try help???? I feel like I am really close but I just need a little push in the right direction
Post 27 Jul 2014, 10:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20355
Location: In your JS exploiting you and your system
revolution 27 Jul 2014, 11:22
Well just out of respect for the people you want help from you could at least spend a small amount of time to convert your posted code to fasm syntax. The easier you make for others to help you the better.
Post 27 Jul 2014, 11:22
View user's profile Send private message Visit poster's website Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 27 Jul 2014, 11:51
I think you may need to look at where you are placing the 2nd loader, the ORG of the second loader, and where you are JMP(ing) to to start the 2nd loader, and also what segment offset you place into the DAP. I don't believe they all match, I think, but I don't know NASM convention so...
Post 27 Jul 2014, 11:51
View user's profile Send private message Reply with quote
AnonymousUser



Joined: 25 Jul 2014
Posts: 32
AnonymousUser 27 Jul 2014, 18:40
Ya I just checked they don't match and here is the nasm example its a little off and when I tried to run it in virtual box it wen't a little crazy printing things
but this is as close as I could get for now here it is
Code:
format Binary
ORG  0x00

Start: jmp main


;Colors for text

COL: db 0
ROW:  db 0


Print:

pusha
        xor ax, ax
        xor dx, dx
        mov dh, BYTE[ROW];puts the row into the dh register
        mov dl, BYTE[COL]
        xor bh, bh
        call cPrint
        mov BYTE[COL], dl
 ;saves the rows for the next time we need to print
popa

ret


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? */

        mov si, Read_Sector_Error_MSG
                mov bl,[RED]
                call Print
                
                cli
                hlt

.Success:
                mov si, Progress_MSG
                mov bl, [PURPLE]
                call Print
                
                inc WORD[DiskAddressPacket.SectorsToRead]
                
        loop    .ForLoop
                call Print_ln
ret
CHECK_DESC:
        
        mov si, CHECK_DESC_MSG
        mov bl, [TEAL]
        call Print
        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, [VOLUME+BX]
        cmp al,[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:
        
        mov si, Progress_MSG
        mov bl, [PURPLE]
        call Print
        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
        
        
        mov si, FOUND_CD
        mov bl, [TEAL]
        call Print
        jmp .Done3
        .FAIL:
        
        mov si, FILE_NOT_FOUND
        mov bl, [RED]
        call Print
        cli
        hlt
        .Done3:
        call Print_ln
ret
READ_STAGE2: 
        
        mov si, LOADING_STAGE2_MSG
                mov bl, [TEAL]
                call Print
                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: 
        ;Prints the possible file found message 
        mov si, Found_Possible_FILE
                mov bl, [TEAL]
                call Print
                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: 
                         
                         
                        mov si, Progress_MSG
                                                mov bl, [PURPLE]
                                                call Print                         
                         
                        INC BX 
                        INC SI   
                        INC CX 
        cmp CX, WORD[STAGE_2_LEN]  
        jne .top2 
        call clear 
         
         
                mov si, File_Found
                mov bl, [TEAL]
                call Print
        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 the reading sectors message 
        mov si, Reading_Sectors
                mov bl, [TEAL]
                call Print
        
                ;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, 0x01
                call Read_Sectors   
         
        ;Prints that read was a success 
                mov si, READ_SUCCESS
                mov bl, [TEAL]
                call Print
        call Print_ln 
         
        mov     dl, [CDDriveNumber];puts the drive number into dl for the next stage to use 
        ;xor     si,si 
         
  jmp 0x0200:0x00;jumps to the next stage or tries to jump error here 
         
        .FAIL: 
        call Print_ln 
       mov si, FILE_NOT_FOUND
                mov bl, [RED]
                call Print
                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


        mov si, W_MSG
        mov bl, [TEAL]
        call Print
        call Print_ln
        

        ;First find the Signature of the CD 
        mov si, Reading_Sectors
        mov bl, [TEAL]
        call Print
        LOAD_SIGNATURE:
        mov cx, 0x04
        call Read_Sectors
        
        mov si, READ_SUCCESS
        mov bl, [TEAL]
        call Print
        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 si, Reading_Sectors
                                mov bl, [TEAL]
                                call Print
                                call Print_ln
                                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 
                                
                                MOV DWORD[DiskAddressPacket.End],EAX     ; Load packet with new address on CD of the root directory 
                                MOV DL,BYTE[CDDriveNumber]              ; Set it up again 
                                MOV AH,42h                          ; Read from drive function 
                                MOV SI,DiskAddressPacket            ; Load SI with address of the Disk Address 
                                        ;    Packet 
                                INT 13h                             ; Call read sector from drive 

                                JC .FAILURE                                   ; Call read sector from drive 
                    
                                ;if the program gets here it means it was a success
                                mov si, READ_SUCCESS
                                mov bl, [TEAL]
                                call Print
                                call Print_ln
                                xor cx, cx
                                jmp LOAD_STAGE2
                                .FAILURE:
                                xor cx, cx
                                inc cx
LOAD_STAGE2:
        cmp cx, 00
        ja .FAILURE
        call READ_STAGE2
        
        
                .FAILURE:
                mov si, FILE_NOT_FOUND
                mov bl, [RED]
                call Print
                cli
                hlt
                
                
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

TEAL:                           db 0x03
RED:                            db 0x04
PURPLE:                         db 0x05



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







    


Last edited by AnonymousUser on 27 Jul 2014, 20:29; edited 4 times in total
Post 27 Jul 2014, 18:40
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 27 Jul 2014, 19:27
NOTE: When I jmp to the new locale, I go further than the origin because I am using an MZ *.EXE file, in your case you can jump to the origin. Instead of 0202:0000h, you can go to 0200:0000h, since you are using a direct binary file.
Post 27 Jul 2014, 19:27
View user's profile Send private message Reply with quote
AnonymousUser



Joined: 25 Jul 2014
Posts: 32
AnonymousUser 27 Jul 2014, 19:38
I think maybe I am reading the sectors in the wrong way maybe the adress in ES:DI is wrong because when I jump to it it executes some code but not code that should be executed after the jump I have a screen shot attached so you can see what I am talking about


Description:
Filesize: 3.07 KB
Viewed: 15336 Time(s)

Zeroth.png



_________________
Thanks in advance
Post 27 Jul 2014, 19:38
View user's profile Send private message Reply with quote
AnonymousUser



Joined: 25 Jul 2014
Posts: 32
AnonymousUser 27 Jul 2014, 21:16
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:
READ_STAGE2: 
        Print LOADING_STAGE2_MSG, TEAL;prints the loading message 
                mov si, LOADING_STAGE2_MSG
        mov bl, [TEAL]
        call Print
        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: 
        
                mov si, Found_Possible_FILE
        mov bl, [TEAL]
        call Print
        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: 
                         
                        mov si, Progress_MSG
                                                mov bl, [TEAL]
                                                call Print
         
                                                 
                         
                        INC BX 
                        INC SI   
                        INC CX 
        cmp CX, WORD[STAGE_2_LEN]  
        jne .top2 
        call clear 
         
         
        mov si, File_Found
                mov bl, [TEAL]
                call Print
          
         
         
        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     
         
        mov si, READ_SUCCESS
                mov bl, [TEAL]
                call Print
        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 
        mov si, FILE_NOT_FOUND
                mov bl, [TEAL]
                call Print
         
        cli;halt the system 
        hlt 
         
         
ret 
    

Here is what the output looks like :


Description:
Filesize: 3.15 KB
Viewed: 15316 Time(s)

Zeroth2.png



_________________
Thanks in advance
Post 27 Jul 2014, 21:16
View user's profile Send private message Reply with quote
AnonymousUser



Joined: 25 Jul 2014
Posts: 32
AnonymousUser 28 Jul 2014, 01:27
Apparently I am not comparing the strings correctly will have to fix that
Post 28 Jul 2014, 01:27
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 29 Jul 2014, 01:17
Is the code loading according to this:

Code:
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    


Then you are jumping (JMP 0200:0000h) to that address, correct?
Post 29 Jul 2014, 01:17
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 29 Jul 2014, 01:27
BTW, you're updating DiskAddressPacket.Segment and DiskAddressPacket.Offset with something weird. Also, you want to put the sector of the file you ant to load in DiskAddressPacket.End, and make certain you put the size of the file in (2048) sectors into DiskAddressPacket.SectorsToRead. You can load up to 32 (x 2048) sectors until you go over the 0FFFFh segment boundary, so adjust accordingly for your file size.
Post 29 Jul 2014, 01:27
View user's profile Send private message Reply with quote
AnonymousUser



Joined: 25 Jul 2014
Posts: 32
AnonymousUser 29 Jul 2014, 18:19
I have fixed the string compare but I am still not getting anywhere if it helps the org of my stage2 is 0x200 here is the code for my function:
Code:
READ_STAGE2:
        Print LOADING_STAGE2_MSG, TEAL
        call Print_ln
                
        mov di, [DiskAddressPacket.Offset]
        mov es, [DiskAddressPacket.Segment]

        
    xor BX, BX;clears out bx
        xor si, si ;clears out si
        xor cx, cx
    .top:
                
                
                MOV AL,BYTE[ES:DI+BX] ;moves a byte of a possible start of a file entry
                cmp AL,BYTE[STAGE2];compares it with file I want
                je .Done;if it is then jump out of loop
                INC BX;get ready for next file entry
        jmp .top
        
        .Done:
        Print Found_Possible_FILE, TEAL;prints it found a possible file
        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 are the same
                ;xor ax, ax;clears out acx
                
                ;prints out a letter to the screen
                MOV AL, BYTE[ES:DI+BX]
                MOV AH, 0xE
                INT 0x010
                ;;;;;;;;;;;;;;;;;;
                
                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;progress message
                                                
                        
                        INC BX;get ready for next character
                        INC SI;get ready for next character     
                        INC CX; increment counter 
        cmp CX, WORD[STAGE_2_LEN] 
        jne .top2
        ;call clear
        call Print_ln
        Print File_Found, TEAL;prints found file if found
        call Print_ln
        
        Print Reading_Sectors, TEAL;prints reading sector message
        ;call clear
        SUB BX, WORD[STAGE_2_LEN];goes back to the start of the file
        ADD DI, BX;adds to the offset
        ;moves in the new adress ES:DI into the DAP
        ;MOV WORD[DiskAddressPacket.Offset], DI
        ;MOV WORD[DiskAddressPacket.Segment], ES
        LEA EAX, [ES:DI]
        MOV DWORD[DiskAddressPacket.End],EAX
        MOV WORD[DiskAddressPacket.SectorsToRead], 4
        xor cx, cx;clears out cx
        mov cx, 0x01;puts in cx 0x04 for how many sectors to read
        call Read_Sectors;calls the read sectors
        Print READ_SUCCESS, TEAL;if it gets here that means it was successful
        ;jump to where the file is located and run it
        ;call Print_ln
        ;Print File_Found, TEAL
        ;call Print_ln
        ;MOV AX, ES
        ;call itoa
        
        
        ;mov AL, ':'
        ;MOV AH, 0xE
        ;INT 0x010
        
        

        ;MOV AX, DI
        ;call itoa
        
        ;call Print_ln
        
        ;call clear
        ;mov [jumptarget], WORD DI
    ;mov [jumptarget + 2], WORD ES
  ;  jmp far [jumptarget]
        jmp 0x0200:0x0000
        
        ;PUSH  0x0200
        ;PUSH  0x0000
        ;retf
        .FAIL:;it failed so print that the file wasn't found and halt the system
        call Print_ln
        Print FILE_NOT_FOUND, RED
        cli
        hlt
        
        
ret
    

Also mkisofs only creates 4 sectors on the disk even if I tell mkisofs to make 16 sectors I am only able to read 4 which probably should make it easier but it's not thanks for trying to help me out at this point I am ready for the answer to fall out of the sky Sad

_________________
Thanks in advance
Post 29 Jul 2014, 18:19
View user's profile Send private message Reply with quote
AnonymousUser



Joined: 25 Jul 2014
Posts: 32
AnonymousUser 29 Jul 2014, 22:57
I also noticed that my ES:Di adress is 0x200:0x91 right before it finishes the program could that be a possible problem?????????
Post 29 Jul 2014, 22:57
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 30 Jul 2014, 02:22
Your ORG should likely be 0, since the scheme is based off the OFFSET, but the SEGMENT. So if you JMP to 0200:0000, your ORG should be 0 for the code you are jumping to.

Yes, you could potentially be over writing yourself.

It might be worth while to map out your code and where you want things to be. Pay special attention to segments:offsets since they can mess you up, since there are many way to get the very same address from several of them. It is nice to be in 32-bit linear memory so you don't have to pay particular attention to that.
Post 30 Jul 2014, 02:22
View user's profile Send private message Reply with quote
AnonymousUser



Joined: 25 Jul 2014
Posts: 32
AnonymousUser 30 Jul 2014, 05:55
Ok so Stage1 of the boot loader is loaded at 0x07c0:0x0FFFF and the Stage2 is loaded at 0x9000:0x0FFFF, They both have the same offset that might be a problem right?? Or should it be that way so I can jump to the code correctly? I changed the org to 0 still no change but will keep at it
Post 30 Jul 2014, 05:55
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 30 Jul 2014, 11:27
Initial boot code load at either 07C0:0000h or 0000:7C00h, depending on how you wish to do things to work, see this: http://en.wikibooks.org/wiki/X86_Assembly/Bootloaders

That being said, CD-ROM booting you can specify this in one of the descriptors, where you want the boot loader to be loaded and ran from, see: http://download.intel.com/support/motherboards/desktop/sb/specscdrom.pdf Look up Initial/Default Entry.

Smiddy
Post 30 Jul 2014, 11:27
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 31 Jul 2014, 11:50
AnonymousUser wrote:
Ok so Stage1 of the boot loader is loaded at 0x07c0:0x0FFFF and the Stage2 is loaded at 0x9000:0x0FFFF, They both have the same offset that might be a problem right?? Or should it be that way so I can jump to the code correctly? I changed the org to 0 still no change but will keep at it


The boot loader is automatically loaded to 0x07c0:0000 (or 0x000:7c00) from BIOS (only first sector). If you load additional sectors you should maybe better us 0x07c0:0200 depending on what location the boot loader is compiled for. It is important to set ORG directive respectively before assembling the code.

If you load additional sectors to 0x07c0:0000 you overwrite your own running code, the same will happen when loading to 0x07c0:ffff as there is no automatical increment of the code register, so due to a wraparound the second byte will be loaded to 0x7c00:0000 and the other bytes incrementing respectively and will reach the point where you overwrite your loader code during execution.

This is in general possible but you have to know exactly what you do and what will happen to which memory locations.
Post 31 Jul 2014, 11:50
View user's profile Send private message Send e-mail Reply with quote
AnonymousUser



Joined: 25 Jul 2014
Posts: 32
AnonymousUser 05 Aug 2014, 22:42
It looks like I do load the root correctly but at some point in the code I start executing garbage code I am assuming its in my READ_STAGE2 function I have the disassembly files for both stages attached if this helps


Description:
Download
Filename: Stage2Disasm.asm
Filesize: 4.4 KB
Downloaded: 527 Time(s)

Description:
Download
Filename: Stage1Disasm.asm
Filesize: 12.37 KB
Downloaded: 516 Time(s)


_________________
Thanks in advance
Post 05 Aug 2014, 22:42
View user's profile Send private message Reply with quote
AnonymousUser



Joined: 25 Jul 2014
Posts: 32
AnonymousUser 05 Aug 2014, 22:44
Here is the new Code almost forgot to post it not much difference but I have it attached just incase


Description:
Download
Filename: Stage2.asm
Filesize: 2.88 KB
Downloaded: 535 Time(s)

Description:
Download
Filename: Stage1.asm
Filesize: 11.09 KB
Downloaded: 511 Time(s)


_________________
Thanks in advance
Post 05 Aug 2014, 22:44
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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.