【问题标题】:Bootloader does not work on actual computer引导加载程序在实际计算机上不起作用
【发布时间】:2017-04-11 17:45:36
【问题描述】:

我知道以前有人问过这个问题,但其他答案似乎都没有解决我的问题。也许我错过了什么?

我知道 .iso 有效,因为我在 QEMU 中运行它并且它成功运行。那我做错了什么?

bits 16

xor ax, ax

start:
    cld               ; Set direction flag to forward

    ; Set up registers
    mov ax, 07c0h     ; Segment location which BIOS loads
    add ax, 288       ; (4096 + 512) / 16 bytes
    mov ss, ax        ; Sets stack segment register
    mov sp, 4096      ; Sets stack pointer register (offset of stack)

    mov ax, 07c0h
    mov ds, ax        ; Sets data segment to where we're loaded

    mov si, text      ; Puts string into source index
    call print_string ; Calls print string

    jmp $             ; Infinite loop to prevent shutdown

print_string:
    mov ah, 0eh       ; System call for printing
    xor bh, bh        ; Sets BH register to 0

.repeat:
    lodsb             ; Loads byte into AL
    cmp al, 0         ; Sees if AL is 0
    je .done          ; Jumps to done if AL is zero

    int 10h           ; Otherwise, print
    jmp .repeat       ; Repeat

.done:
    ret

text db 'Test', 0

times 510 - ($ - $$) db 0 ; Pads 510 - (current location - start location) zeros
dw 0xAA55                 ; Standard PC boot signature (takes up 2 bytes)

编辑:我在代码中添加了以下内容:

xor ax, ax
cld
xor bh, bh

为了创建 iso,我运行以下命令:

dd if=/dev/zero of=floppy.img bs=1024 count=1440
dd if=bootloader.bin of=floppy.img seek=0 count=1 conv=notrunc
mkdir iso
cp floppy.img iso/
mkisofs -o file.iso -b floppy.img iso

为了将 iso 刻录到我的 USB,我运行以下命令:

umount /dev/sdX
dd if=/home/mint/Downloads/file.iso of=/dev/sdX bs=4M && sync

【问题讨论】:

  • 您在实际计算机上启动“.iso”的具体操作是什么?
  • 您可能希望使用 CLD 将方向标志设置为向前,因为您依赖于 LODSB 的向前方向。您无法保证 BIOS 在到达您的 coe 之前设置了哪个方向。
  • 对于Int 10h/ah=0eh,您可能希望将 BH 寄存器设置为 0,因为它用作要写入的页码。
  • 开机时是否使用了CD-ROM? (我问是因为您提到您制作了 ISO,但不清楚您是否在真实 PC 上刻录了物理 CD)。我很好奇你启动 QEMU 的命令行是什么样子的。
  • 真实硬件,特别是 EFI CSM,做额外检查:maybe this question will help?

标签: assembly x86 nasm bootloader bare-metal


【解决方案1】:

您的问题是您创建的“iso”是光盘映像。只有当它刻录到光盘(例如 CD-R)时,它才能在真实计算机上启动。当您将它与 QEMU 一起使用时,您显然是在将它用作模拟 CD-ROM。当您将其复制到 USB 驱动器时,它的格式不适合在 USB 驱动器上启动。

幸运的是,从 USB 驱动器启动的正确格式很简单:您的引导加载程序只需位于驱动器的第一个扇区,就像在软盘或硬盘上一样。因此,您可以跳过创建“iso”部分,直接将引导扇区写入 USB 驱动器。例如:

dd if=bootloader.bin of=/dev/sdX 

【讨论】:

  • 也许 OP 对“混合”ISO 映像现在对于 Linux“live cd”映像很常见这一事实感到困惑:您可以 dd 它们到 USB 设备,因为它们看起来像一个有效的分区表以及看起来像可启动的 ISO9660 映像,并且 syslinux 3.72 及更高版本支持从该启动。 (This blog briefly mentions some of that 但主要是关于使用 isohybrid 命令。)
猜你喜欢
  • 1970-01-01
  • 2016-03-26
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 2011-03-18
  • 2012-04-17
  • 2021-09-03
  • 2016-07-27
相关资源
最近更新 更多