查找根目录BootLoader的开始

时间:2014-07-25 06:44:51

标签: assembly bootloader

我正在创建一个从CD启动的BootLoader,但是我无法发现如何在磁盘上找到根目录的开头这里是我的启动代码:

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



;CMPSB compares the byte at [DS:SI] or [DS:ESI] with the byte at [ES:DI] 
;or [ES:EDI], 
;and sets the flags accordingly. It then increments or decrements (depending 
;on the direction flag: increments
; if the flag is clear, decrements if it is set) SI and DI (or ESI and EDI).
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

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 es, 0x200

    mov     [CDDriveNumber],dl
    call clear


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

    LOAD_ROOT:
    ;first calculate the start of the Root directory
     mov WORD[DiskAddressPacket.Offset], 0x01000
     mov cx, 0x01
     call Read_Sectors

     call Print_ln
     Print READ_SUCCESS, TEAL
     call Print_ln




    cli
    hlt



Sector_Size: dw 512                     
CDDriveNumber: db 80h
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 "\2CD001"
CD_FILE_VER:               db 0x01

;Disk Address Packet


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

RETURN: 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 in correctly",0
Progress_MSG: db ".",0
FILE_NOT_FOUND: db "Error, file not found",0
FOUND: db "Found", 0
times 2046 - ($ - $$) db 0; padd out the rest of the file to 0
DW 0xAA55

我看到一些代码做了类似这样的事情来找到卷描述符我觉得你需要找到根目录,但是我不能把它放在它上面这里是执行该操作的代码:< / p>

get_next_desc: 
        ;/* Read the sector in memory at 0000:1000. */
        mov     [desc_sector],eax
        mov     bx,01000h
        mov     cx,1
        call    read_sectors

        ;/* Check for the signature "\2CD001" at the beginning of the descriptor. */
        mov     si, cd_signature
        mov     di,01000h
        mov     cx,6
        cmpsb
        je      found_desc

        ;/* Check if we have looked in all the descriptors of volume. */
        cmp     byte [es:0x1000],0FFh
        jne     next_desc

        ;/* We looked in all sessions of the disk, and the OS image wasn't found.
        ;   We display an error message on the screen and then prompts the user to
         ;  press a key to restart the computer. */
        mov     si, msg_file_not_found
        call    print_string
        jmp     reboot

next_desc: 

以下是我用来汇编代码的内容:

nasm ZerothStage.asm -o ZerothStage.bin

mkisofs -b ZerothStage.bin -no-emul-boot -o BootLoader.iso ./

1 个答案:

答案 0 :(得分:0)

没关系我明白了我显然需要将段偏移地址从DAP加载到ES:DI并在那里查找卷描述符 如果有人有兴趣,这就是我以前解决的问题。

代码:

    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:   

我读了第四个扇区而不是第16个扇区,显然它是你必须阅读的图像的最后一个扇区 我是新手,所以如果有人来这里可以解释一下这会更好,因为我仍然想要掌握这些启动概念