BIOS总是无法执行磁盘操作

3
我正在编写一个引导加载程序,旨在加载一个超过引导扇区允许长度的程序。然而,每次我运行该程序(我在Virtualbox和QEMU中测试),磁盘读取都会失败,磁盘重置也是如此。
该引导加载程序被设计为加载其后的一个扇区(这将是一个FAT16卷,因此我已经在磁盘描述中将其设为保留扇区),并立即运行该程序。但是,磁盘读取总是失败(CF被设置为1),而磁盘重置也是如此。这种情况在Virtualbox和QEMU中都会发生。
以下是引导扇区和第二个扇区的完整代码:
BITS 16

jmp strict short main               ; Jump to main bootloader
nop                                 ; Pad out remaining bytes until boot descriptor

; Disk descriptor

OEM_name            db "HOUSE   "   ; Disk label
bytes_sector        dw 0x0200       ; Bytes per sector
sectors_cluster     db 0x01         ; Sectors per cluster
sectors_reserved    dw 0x0002       ; Number of sectors reserved (in this case 1 for MARBLE, the rest for R)
fats                db 0x02         ; Number of file allocation tables
max_root_entries    dw 0x0200       ; Max number of root entries
sectors             dw 0x1040       ; Number of sectors
medium_type         db 0xF0         ; Type of medium (removable or fixed?)
sectors_fat         dw 0x0010       ; Sectors per file allocation table
sectors_track       dw 0x0020       ; Sectors per track
heads               dw 0x0002       ; Number of heads
hidden_sectors      dd 0x00000000   ; Number of sectors before partition
total_sectors       dd 0x00000000   ; Number of sectors in medium (zero because 2B != 0)
drive_number        db 0x00         ; Drive number (for BIOS int 0x13)
drive_signature     db 0x00         ; NOT USED
ext_signature       db 0x29         ; Extended boot signature
volume_serial       dd 0x00000000   ; Volume's serial number
volume_label        db "HOUSEHOUSE "; Volume label
fs_type             db "FAT16   "   ; Filesystem type

main:
    mov sp, 0x1000                  ; 4K of stack

    mov ax, word [sectors_reserved] ; Read all reserved sectors
    sub al, 0x01                    ; Except this one
    mov ah, 0x02                    ; Read disk sectors
    mov bx, r_buffer                ; Read contents into our buffer
    mov ch, 0x00                    ; Cylinder 0
    mov cl, 0x02                    ; Sector 2
    mov dh, 0x00                    ; Head 0
    jmp .read                       ; Read the disk

.reset:
    pusha                           ; Push register states to stack
    call lps
    mov ah, 0x00                    ; Reset disk
    int 0x13                        ; BIOS disk interrupt
    jnc .read                       ; If successsul, read again
    call lps
    mov ah, 0x00                    ; Otherwise, prepare to reboot
    int 0x19                        ; Reboot

.read:
    call lps
    int 0x13                        ; BIOS disk interrupt
    jc .reset                       ; If failed, reset disk
    jmp r_buffer                    ; Otherwise, jump to R

lps:                                ; Debug message
    pusha
    mov ah, 0x0E
    mov al, 0x52
    mov bh, 0x00
    int 0x10
    mov ah, 0x00
    int 0x16
    popa
    ret

    times 510-($-$$) db 0x00        ; Pad remainder of boot sector with zeros
    sig             dw 0xAA55       ; Boot signature

r_buffer:                           ; Space in memory for loading R

r_start:                            ; Beginning of second sector
    mov ax, 0x07C0
    add ax, 0x0220
    mov ss, ax
    mov ax, 0x07C0
    mov ds, ax

    mov si, success                 ; Successful
    call print_str                  ; Print!
    hlt                             ; Halt here

print_str:                          ; Prints string pointed to by REGISTER SI to cursor location (si=str)
    pusha                           ; Push register states to stack
    mov ah, 0x0E                    ; Print in teletype mode
    mov bh, 0x00                    ; Page 0

.repeat:
    lodsb                           ; Load next character from si
    cmp al, 0x00                    ; Is this a null character?
    je .ret                         ; If it is, return to caller
    int 0x10                        ; Otherwise, BIOS interrupt
    jmp .repeat                     ; Do the same thing

.ret:
    popa                            ; Restore register states
    ret                             ; Return to caller

.data:
    success         db "!", 0x00    ; Success!

    times 1024-($-$$) db 0x00       ; Pad remainder of reserved sectors with zeros

正如我所说的,第二个区段中的代码应该被执行,但由于磁盘重置失败,这并没有发生。

2
你应该在main函数的开头设置段寄存器(ds, es, ss)。在.reset之后,你也应该将ax重置为02xxh。在顶部,你应该放置"org 7C00h"。在第二个扇区中,你应该在hlt之后添加一个跳转,跳回到hlt处。请指定目前你得到的确切输出。 - ecm
谢谢!我已经这样做了,但仍然得到相同的结果。当我在QEMU中运行它时,我会得到第一个'R',我按下一个键,另一个'R',我再按下一个键,最后一个'R'出现。这就是发生的一切。 - House
你也有一个无法匹配的pusha,但这似乎不太可能是你问题的原因。 - ecm
你是要将这个引导扇区安装到软盘类型的设备上吗?如果它在分区设备上,那么你应该设置隐藏扇区字段并将其用作寻址第二个扇区的基础。(这将需要正确的CHS转LBA数字计算。) - ecm
实际上,如果您像这里一样使用sectors_reserved,您就不需要org 7C00h,而是可以将ds初始化为7C0h(就像您在第二阶段中所做的那样)。 - ecm
我修复了不匹配的 pusha。有趣的是,仅仅不从堆栈中弹出某些东西就会导致许多问题。我一直在使用 DD 将引导扇区写入空白磁盘映像。 - House
1个回答

2
在访问任何数据之前,您需要设置段寄存器,并确保您的ORG(原点)适合于在段寄存器中加载的值(特别是DS)。当没有ORG指令(或等效指令)时,默认值为0x0000。在实模式下,每个逻辑地址由2个组成部分组成-一个段和一个偏移量。CPU计算出的物理地址基于公式(段* 16)+偏移量。将ORG视为起始偏移量。如果使用0x0000的段,则需要0x7c00的偏移量。(0x0000 * 16)+0x7c00 = 0x7c00。 0x7c00是引导加载程序的起始位置。由于您要将扇区写入0x7e00以上的内存中,因此将堆栈设置为0x0000:0x7c00以从引导加载程序正下方开始向内存开头增长会更简单。由于您正在使用像LODSB这样的字符串指令,因此应使用CLD指令清除方向标志(DF),以便字符串操作在内存中向前推进。不能依赖启动引导加载程序时DF处于清除状态。在读取磁盘Int 13h/AH=2时,会破坏AX。如果出现错误,则需要重新加载AH为2,并将AL重新加载为要读取的扇区数。如果重新设计代码,则可以使用SI临时存储要读取的扇区数。通常,您不必检查磁盘复位是否失败,只需重新执行读取操作即可。尝试操作几次,然后进入故障状态/重新启动。我修改了您的代码以在DI中放置重试计数。每次进行复位时,重试计数会减少1。如果重试计数>= 0,则尝试读取。如果重试计数<= 0,则重新启动。当跳转到内存地址0x7e00处的代码(其中读取保留扇区)时,您可以利用这个机会通过使用FAR JMP确保CS也设置为0x0000。如果在引导扇区中将段设置为0x0000,则可以在第二阶段中重用它们。如果您打算从引导加载程序进入保护模式,则不建议使用除0x0000以外的段值,以便前64KiB中的逻辑地址和物理地址相同。在加载GDT并进行跳转进入保护模式时,这非常有用。
当使用HLT时,最好确保关闭中断(使用CLI),否则HLT将在下一个中断上退出,并且处理器将继续执行之后存储在内存中的内容。在真实硬件上,即使关闭中断,仍可能发生不可屏蔽中断(NMI),因此最好将HLT放在循环中。
考虑到这些变化,以下代码应该可以工作:
BITS 16

org 0x7c00

jmp strict short main               ; Jump to main bootloader
nop                                 ; Pad out remaining bytes until boot descriptor

; Disk descriptor

OEM_name            db "HOUSE   "   ; Disk label
bytes_sector        dw 0x0200       ; Bytes per sector
sectors_cluster     db 0x01         ; Sectors per cluster
sectors_reserved    dw 0x0002       ; Number of sectors reserved (in this case 
                                    ; 1 for MARBLE, the rest for R)
fats                db 0x02         ; Number of file allocation tables
max_root_entries    dw 0x0200       ; Max number of root entries
sectors             dw 0x1040       ; Number of sectors
medium_type         db 0xF0         ; Type of medium (removable or fixed?)
sectors_fat         dw 0x0010       ; Sectors per file allocation table
sectors_track       dw 0x0020       ; Sectors per track
heads               dw 0x0002       ; Number of heads
hidden_sectors      dd 0x00000000   ; Number of sectors before partition
total_sectors       dd 0x00000000   ; Number of sectors in medium (zero because 2B != 0)
drive_number        db 0x00         ; Drive number (for BIOS int 0x13)
drive_signature     db 0x00         ; NOT USED
ext_signature       db 0x29         ; Extended boot signature
volume_serial       dd 0x00000000   ; Volume's serial number
volume_label        db "HOUSEHOUSE "; Volume label
fs_type             db "FAT16   "   ; Filesystem type

main:
    xor ax, ax                      ; AX = 0
    mov ds, ax                      ; DS = ES = 0
    mov es, ax
    mov ss, ax                      ; SS:SP = 0x0000:0x7c00 (grows down below bootloader)
    mov sp, 0x7c00
    cld                             ; Set forward direction for string instructions

    mov si, word [sectors_reserved] ; Read all reserved sectors
    dec si                          ; Except this one. SI = sectors to read
    mov di, 3                       ; retry count of 3 and then give up

    mov bx, r_buffer                ; Read contents into our buffer
    mov ch, 0x00                    ; Cylinder 0
    mov cl, 0x02                    ; Sector 2
    mov dh, 0x00                    ; Head 0
    jmp .read                       ; Read the disk

.reset:
    call lps
    dec di                          ; Reduce retry count
    jge .read                       ; If retry >= 0 then try again

                                    ; Otherwise retry count exceeded - reboot
    mov ah, 0x00
    int 0x19                        ; Reboot

.read:
    mov ax, si                      ; Transfer sector read count to AX
    mov ah, 0x02                    ; Read disk sectors
    call lps
    int 0x13                        ; BIOS disk interrupt
    jc  .reset                      ; If failed, reset disk
    jmp 0x0000:r_start              ; Otherwise, jump to r_start. Set CS=0

lps:                                ; Debug message
    pusha
    mov ah, 0x0E
    mov al, 0x52
    mov bh, 0x00
    int 0x10
    mov ah, 0x00
    int 0x16
    popa
    ret

    times 510-($-$$) db 0x00        ; Pad remainder of boot sector with zeros
    sig             dw 0xAA55       ; Boot signature

r_buffer:                           ; Space in memory for loading R

r_start:                            ; Beginning of second sector
    mov si, success                 ; Successful
    call print_str                  ; Print!

    cli
.endloop:
    hlt                             ; Halt here
    jmp .endloop

print_str:                          ; Prints string pointed to by REGISTER SI 
                                    ;     to cursor location (si=str)
    pusha                           ; Push register states to stack
    mov ah, 0x0E                    ; Print in teletype mode
    mov bh, 0x00                    ; Page 0

.repeat:
    lodsb                           ; Load next character from si
    cmp al, 0x00                    ; Is this a null character?
    je .ret                         ; If it is, return to caller
    int 0x10                        ; Otherwise, BIOS interrupt
    jmp .repeat                     ; Do the same thing

.ret:
    popa                            ; Restore register states
    ret                             ; Return to caller

.data:
    success         db "!", 0x00    ; Success!

    times 1024-($-$$) db 0x00       ; Pad remainder of reserved sectors with zeros

观察和建议

  • 对于引导加载程序的开发,我强烈建议使用BOCHS进行初始测试。内置调试器具有实模式意识并理解实模式分段,而QEMU/GDB则不具备。学习使用调试工具是一项有价值的技能。


1
第一个版本的代码,我将重试计数设置为0以进行测试。更新根据描述和注释将其设置为3。 - Michael Petch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接