Author Topic: 2 Stage Bootloader  (Read 66582 times)

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #45 on: July 27, 2014, 11:26:23 PM »
Wow I didn't even notice that *face palm* thanks gammac now I realize why its doing this I messed up so bad :P I'm gonna go ahead and fix that
Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #46 on: July 27, 2014, 11:58:30 PM »
Ok here is my code it is printing out what is at ES:DI+BX and it prints out the file that I wan't but it moves on :( I might have missed something here is the code
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]
MOV AH, 0xE
INT 0x010
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 0x00:0x0200

.FAIL:
call Print_ln
Print FILE_NOT_FOUND, RED
cli
hlt


ret
Here is the output:
Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #47 on: July 28, 2014, 12:48:32 AM »
Now its just going crazy its like its skipping everything and going straight to printing that the file was found but it wasn't
Here is the new code:
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
xor cx, cx
.top3:
add BX, 12
    .top:

MOV AL,[ES:DI+BX] ;starting address
cmp AL,BYTE[STAGE2]
je .Done
cmp AL,BYTE[STAGE2]
jne .top3
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]
MOV AH, 0xE
INT 0x010

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
call Print_ln
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 0x0200:0x00

.FAIL:
call Print_ln
Print FILE_NOT_FOUND, RED
cli
hlt


ret
Here is the output:
Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #48 on: July 28, 2014, 01:52:45 AM »
Here is what the root looks like if you need it at all:
Code: [Select]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         "             r8ð     "             r8ð    ,             r5ð     
BOOT.CAT;1 .       Π     Îr5ð      STAGE2.BIN;1 0             r5ð     ZEROTHST.BIN;1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              ªUUªˆ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             éƒ  `Š6 ´· þƲ ͈6 Æ  aô ¹ ¬„Àt Í´· þÂÍëêô° Í´ Í´· ¶ ² ÍôB0À¾‡Š–Ís`1À1ÒŠ6 Š 1Û³¾±è®ÿˆ aúô`1À1ÒŠ6 Š 1Û³¾æèÿˆ aÿ‰â±èhÿÃ`1À1ÒŠ6 Š 1Û³¾Ìèjÿˆ aŽ‹>‹1Û&Šˆ‡—C< tëò1ÛƒÃ1É1ÀŠ‡—:‡°të A`1À1ÒŠ6 Š 1Û³¾æè ÿˆ aCëÒƒù u!èöþ`1À1ÒŠ6 Š 1Û³¾ÿèùþˆ aë`1À1ÒŠ6 Š 1Û³¾èèÛþˆ aúôè·þÃ`1À1ÒŠ6 Š 1Û³¾æè¹þˆ aè—þ1Û1ö&Š´Í&Š<;tCëïúôÃú¸ÀŽØŽèŽà1ÀŽÀŽÐ¼þÿûˆ–è•þ`1À1ÒŠ6 Š 1Û³¾˜ègþˆ aèEþ`1À1ÒŠ6 Š 1Û³¾¼èHþˆ a¹ èiþ`1À1ÒŠ6 Š 1Û³¾Îè&þˆ aèþèšþŽ‹>‹1Û»( &Š €ù tˆoCëñƇo &f‹…ž f£1ɹ `1À1ÒŠ6 Š 1Û³¾¼èÔýˆ aèøý`1À1ÒŠ6 Š 1Û³¾Îèµýˆ aè“ýèÚþ`1À1ÒŠ6 Š 1Û³¾Cè“ýˆ aèqý €                          CD001  %/EReading sectors Checking for CD Signature Loading Stage 2 of boot loader
File for Stage 2 of the bootloader was successfully loaded!! Failed to load Stage 2 of the boot loader !!!!! Found Possible File
Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #49 on: July 28, 2014, 04:39:57 AM »
I don't know if this is important but it looks like stage 2 comes before zerothstage in the root I don't know if that matters at all just wanted to point that out
Thanks in advance, Anonymous

Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: 2 Stage Bootloader
« Reply #50 on: July 28, 2014, 09:02:56 AM »
Code: [Select]
    xor BX, BX
xor si, si
xor cx, cx
.top3:
add BX, 12 ; What happens if bx is incremented elsewhere?
    .top:

MOV AL,[ES:DI+BX] ;starting address ; address of a filename in your list of names?
cmp AL,BYTE[STAGE2] ; STAGE2 is the address of given filename?
je .Done
cmp AL,BYTE[STAGE2] ; useless, previous branch do not alter flags
jne .top3 ; test next filename?
INC BX ; ATTENTION: never reached
jmp .top ; never reached

.Done:
; OK, we are here that means the first character matches.
; What are you doing now?
...


please explain your whole filesearching algorithm in pseudocode and try to write well commented code.

Code: [Select]
XOR SI, SI;Clear out for use
...
xor cx, cx;clear out for use as counter

this types of comments are useless, only the word counter is an information

Code: [Select]
INT 0x010
what does this interupt?
Please comment your code! It helps to help you.

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #51 on: July 28, 2014, 04:25:54 PM »
Sorry about the undocumented code I will fix that ASAP as for INT 0x010 that is to write to the video memory to output things on the screen basically a print thats how I know that it passes it file I wan't here is the code:
Code: [Select]
READ_STAGE2:
Print LOADING_STAGE2_MSG, TEAL
call Print_ln
;mov es, [DiskAddressPacket.Segment]
;mov di, [DiskAddressPacket.Offset]

    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, 10;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.Segment], ES
MOV WORD[DiskAddressPacket.Offset], DI
xor cx, cx;clears out cx
mov cx, 0x04;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
JMP [ES:DI] ;jumps to the file

.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
EDIT: I finally fixed the string compare it wasn't working because the STAGE2_LEN was a defined BYTE instead of a DEFINED WORD :D now I still have the problem of going to the next file. I will post Output to show what it looks like the code above was updated to the new code.
« Last Edit: July 28, 2014, 04:34:34 PM by Anonymous »
Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #52 on: July 28, 2014, 04:36:32 PM »
Here is the output:
Thanks in advance, Anonymous

Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: 2 Stage Bootloader
« Reply #53 on: July 28, 2014, 05:47:42 PM »
sorry Anonymous, your code is an 'eye-pain' and I bet it won't work correct. Searching for two identical letters and then comparing the rest will produce false positives e.g. if you have files named 'oldstage2' and 'stage2'.
Please comment your code! It helps to help you.

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #54 on: July 28, 2014, 05:54:20 PM »
But I don't have files named oldstage2 because filenames can't be longer than 10 6 for the name 1 for the . and 3 for the extension because it is an El torrito standard ISO that's why I am not checking for files that might have an identical name.
« Last Edit: July 28, 2014, 06:16:16 PM by Anonymous »
Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #55 on: July 28, 2014, 06:27:15 PM »
I have been using the code for CD bootloader to help me out if this helps anyone see what I am trying to do and yes it is in fasm but it is the only one that I could find that is closest to nasm and booting off of a CD
Thanks in advance, Anonymous

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: 2 Stage Bootloader
« Reply #56 on: July 28, 2014, 06:58:16 PM »
I notice you're back to the near jump. I'm really quite sure that isn't going to do what you want.
Code: [Select]
LOADSEG equ 0x200 ; ?
LOADOFFS equ 0 ; ?

; some ways to do a far jump
    jmp LOADSEG:LOADOFFS
; this needs to be "immediate"
; note that segment comes first in this syntax
; in contrast to the usual little-endian arrangement

; jmp es:bx will not work (more's the pity)
; but you can do...
push es
push bx
retf
; with the same result

; or you could use an indirect jump
; it jumps to the address found at the address you provide

;...
    jumptarget dw LOADOFFS, LOADSEG
;...
    jmp far [jumptarget]

; if you don't know 'em in advance, you can...
    mov [jumptarget], bx
    mov [jumptarget + 2], es
    jmp far [jumptarget]

; but...
    jmp [jumptarget]
; or
    jmp [es:bx]
; is going to take just the offset
; and jmp there - in the same segment

Hope that helps...

Best,
Frank

EDIT: Well how does the Fasm file do it? Fine assembler! Why not just use it?


Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #57 on: July 28, 2014, 07:18:36 PM »
I tried everything you said and it is still not going there the fasm file uses some drive geometry but the only thing is that it won't work because my iso will only read 4 sectors anything higher it will freeze because it is not able to ready anything else probably because the files I have on there only occupy 4 sectors
Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 2 Stage Bootloader
« Reply #58 on: July 28, 2014, 07:19:47 PM »
Here is the output of this far jump:
Code: [Select]
mov [jumptarget], ES
mov [jumptarget + 2], DI
jmp far [jumptarget]
The print below the printed segment offset address is not supposed to happen so it is jumping somewhere just maybe not far enough??? 
« Last Edit: July 28, 2014, 07:21:23 PM by Anonymous »
Thanks in advance, Anonymous

Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: 2 Stage Bootloader
« Reply #59 on: July 28, 2014, 07:28:50 PM »
I have been using the code for CD bootloader to help me out if this helps anyone see what I am trying to do and yes it is in fasm but it is the only one that I could find that is closest to nasm and booting off of a CD

Interesting, thank you for sharing this file. :)
Please comment your code! It helps to help you.