【发布时间】:2014-07-25 06:44:50
【问题描述】:
我正在创建一个从 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
我看到了一些类似这样的代码来查找卷描述符>
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 ./
【问题讨论】:
-
好的,我想我知道如何找到最后一个扇区并从那里加载所有内容,但问题是 mkisofs 出于某种原因仅将其输出为 4 个扇区有什么帮助???? ?
-
据我所知,从 CD 启动时,BIOS 不会从整个 CD 中读取,但启动映像 (ZerothStage.bin) 被视为“硬盘映像”,因此会导致读取扇区 #2在读取 ZerothStage.bin 文件的 0x400 到 0x5FF 字节时。如果是这样,则无法读取此文件之外的扇区。
-
@MartinRosenau 好吧,它仍然在图像上的某个位置,所以我想我将不得不尝试每个扇区才能找到它,感谢您的尝试
标签: assembly bootloader