USB 硬盘仿真导致磁盘读取失败(BIOS int 13)?

tra*_*day 3 assembly mbr bios bootloader x86-16

一些背景:

我正在开发一个基本的引导加载程序,它通过 BIOSINT 13h AH=02h中断将辅助引导加载程序读入内存。我已经在模拟器(Virtualbox、Qemu 和 Bochs)中运行了它。

随后,我在我的引导加载程序中添加了一个 BPB(BIOS 参数块),制作了一个可引导的 USB,并在我的真机上使用 USB Floppy Emulation(我在真机 BIOS 的配置屏幕中设置)进行了测试。它就像一个魅力。

在我自己的机器上测试引导加载程序后,我在另一台较新的机器上测试了它。这台新计算机的 BIOS 配置中没有软盘仿真选项,因此无法从 USB 驱动器启动。因此,在这个osdev wikipage 之后,我在 MBR 的末尾添加了一个分区表,以便较新的机器可以从 USB 启动。

问题:

使用添加的分区表代码,引导加载程序无法将辅助引导加载程序加载到内存中,并且 BIOSINT 13h失败。我不知道为什么会发生这种情况,因为我没有更改任何实际的引导加载程序代码。我刚刚添加了 64 位 MBR 分区表,将数据读入内存立即失败。

BPB(BIOS 参数块)和磁盘访问例程

bits    16 
org 0x7C00

jmp start
nop
;------------------------------------------;
;  Standard BIOS Parameter Block, "BPB".   ;
;------------------------------------------;
     bpbOEM         db  'MSDOS5.0'
     bpbSectSize    dw  512
     bpbClustSize   db  1
     bpbReservedSe  dw  1
     bpbFats        db  2
     bpbRootSize    dw  224
     bpbTotalSect   dw  2880
     bpbMedia       db  240
     bpbFatSize     dw  9
     bpbTrackSect   dw  18
     bpbHeads       dw  2
     bpbHiddenSect  dd  0
     bpbLargeSect   dd  0
     ;---------------------------------;
     ;  extended BPB for FAT12/FAT16   ;
     ;---------------------------------;
     bpbDriveNo     db  0
     bpbReserved    db  0
     bpbSignature   db  41            
     bpbID          dd  1
     bpbVolumeLabel db  'BOOT FLOPPY'
     bpbFileSystem  db  'FAT12   '

drive_n: db 0
start: 
    mov [drive_n], dl

    ; setup segments
    xor ax, ax
    mov ds, ax
    mov es, ax

    ; setup stack
    cli
    mov ss, ax
    mov sp, 0x7C00   ; stack will grow downward to lower adresses
    sti

    ; write start string
    mov si, start_str    ; start_str = pointer to "Bootloader Found..."
    call write_str       ; routine that prints string in si register to screen 

    ; read bootstrapper into memory
    mov dl, [drive_n]; drive number
    mov dh, 0x00    ; head (base = 0)
    mov ch, 0x00    ; track /cylinder = 0
    mov cl, 0x02    ; (1= bootloader, 2=start of bootstrapper
    mov bx, 0x7E00  ; location to load bootstrapper 
    mov si, 0x04    ; number of attempts

    ; attempt read 4 times 
  read_floppy:
    ; reset floppy disk
    xor ax, ax
    int 0x13

    ; check if attempts to read remain, if not, hlt system (jmp to fail_read)
    test    si, si
    je  fail_read   ; *** This jump happens only on real machines with 
    dec si          ; USB hard drive emulation ***

    ; attempt read 
    mov ah, 0x02    ; select read
    mov al, 0x0F    ; num sectors
    int     0x13
    jc  read_floppy

    ...             ; continue onward happily! (without any errors)
Run Code Online (Sandbox Code Playgroud)

MBR分区表

; 0x1b4
db "12345678", 0x0, 0x0     ; 10 byte unique id

; 0x1be         ; Partition 1 -- create one big partition that spans the whole disk (2880 sectors, 1.44mb)
db 0x80         ; boot indicator flag = on

; start sector
db 0            ; starting head = 0
db 0b00000001   ; cyilinder = 0, sector = 1 (2 cylinder high bits, and sector. 00 000001 = high bits db 0x00)
db 0            ; 7-0 bits of cylinder (insgesamt 9 bits) 

; filesystem type
db 1            ; filesystem type = fat12

; end sector = 2880th sector (because a floppy disk is 1.44mb)
db 1            ; ending head = 1
db 18           ; cyilinder = 79, sector = 18 (2 cylinder high bits, and sector. 00 000001 = high bits db 0x00)
db 79           ; 7-0 bits of cylinder (insgesamt 9 bits) 

dd 0            ; 32 bit value of number of sectors between MBR and partition
dd 2880         ; 32 bit value of total number of sectors

; 0x1ce         ; Partition 2
times 16 db 0

; 0x1de         ; Partition 3
times 16 db 0

; 0x1ee         ; Parititon 4
times 16 db 0

; 0x1fe         ; Signature
dw  0xAA55
Run Code Online (Sandbox Code Playgroud)

问题

当且仅当在 BIOS 中启用 USB 硬盘驱动器模拟时,是什么导致读取磁盘失败?我试过更改分区表和 BPB,但似乎没有任何效果。我敢打赌,这与计算机处理软盘和硬盘信息的方式不同有关,但很难找到任何相关信息。

任何帮助将不胜感激。我不打算问这么长的问题;它只是积累。

Mic*_*tch 5

TL;DR:在某些情况下,引导驱动器未正确存储在 label drive_n。这会导致磁盘读取例程在某些硬件上失败。


我有一个 Stackoverflow 答案,其中包含一组通用的引导加载程序提示。一个重要的提示是:

当 BIOS 跳转到您的代码时,您不能依赖具有有效值或预期值的 CS、DS、ES、SS、SP 寄存器。当您的引导加载程序启动时,它们应该被适当地设置。您只能保证您的引导加载程序将从物理地址 0x00007c00 加载并运行,并且引导驱动器编号已加载到 DL 寄存器中。

在使用更相关的代码更新您的问题后,阅读之前会发生什么,问题变得明显:

drive_n: db 0
start: 
    mov [drive_n], dl

    ; setup segments
    xor ax, ax
    mov ds, ax
    mov es, ax

    ; setup stack
    cli
    mov ss, ax
    mov sp, 0x7C00   ; stack will grow downward to lower adresses
    sti
Run Code Online (Sandbox Code Playgroud)

问题是mov [drive_n], dl在设置段寄存器之前完成。mov [drive_n], dl相当于mov [ds:drive_n], dlDS 中的段很重要。如果 BIOS 将控制权转移到带有非 0x0000的DS段的引导加载程序,那么mov [drive_n], dl会将驱动器编号写入您不期望的内存位置。

如果DS的值不为零,并且引导驱动器不是 0x00,则很有可能发生故障。如果实际启动驱动器存储到错误的内存位置,drive_n则将使用存储在标签处的初始值。在你的情况下是 0x00。

在大多数情况下,你很幸运它起作用了。解决这个问题很简单。确保设置段寄存器(最显着的是DS后将DL的值写入内存。代码应如下所示:

drive_n: db 0
start: 
    ; setup segments
    xor ax, ax
    mov ds, ax
    mov es, ax

    ; setup stack
    cli
    mov ss, ax
    mov sp, 0x7C00   ; stack will grow downward to lower adresses
    sti

    mov [drive_n], dl
Run Code Online (Sandbox Code Playgroud)