assembly - 从 DOS 启动 MBR

标签 assembly bootstrapping bootloader bare-metal truecrypt

对于一个项目,我想直接从 DOS 调用第一个硬盘上的 MBR。我编写了一个小型汇编程序,该程序将 MBR 加载到内存中的 0:7c00h 处,并进行远跳转。我已将实用程序放在 (DOS) 可启动软盘上。我尝试启动的磁盘(HD0,0x80)上有一个 TrueCrypt 启动加载程序。当我在此设置中运行该工具时,它会显示 TrueCrypt 屏幕,但输入密码后会导致系统崩溃。当我在普通的 WinXP 机器上运行我的小实用程序 (w00t.com) 时,它似乎立即崩溃。

显然我忘记了 BIOS 通常所做的一些重要的事情,我猜这是一些微不足道的事情。有更好的裸机 DOS 和 BIOS 经验的人可以帮助我吗?

这是我的代码:

.MODEL tiny
.386
_TEXT SEGMENT USE16

INCLUDE BootDefs.i

ORG 100h

start:
    ; http://vxheavens.com/lib/vbw05.html
    ; Before DOS has booted the BIOS stores the amount of usable lower memory 
    ; in a word located at 0:413h in memory. We going to erase this value because
    ; we have booted dos before loading the bootsector, and dos is fat (and ugly).

    ; fake free memory  
    ;push ds
    ;push   0
    ;pop        ds
    ;mov        ax, TC_BOOT_LOADER_SEGMENT / 1024 * 16 + TC_BOOT_MEMORY_REQUIRED
    ;mov    word ptr ds:[413h], ax  ;ax = memory in K
    ;pop ds
    ;lea si, memory_patched_msg
    ;call print

    ;mov ax, cs
    mov ax, 0
    mov es, ax

    ; read first sector to es:7c00h (== cs:7c00)
    mov  dl, 80h
    mov  cl, 1
    mov  al, 1
    mov  bx, 7c00h ;load sector to es:bx
    call read_sectors

    lea si, mbr_loaded_msg
    call print

    lea si, jmp_to_mbr_msg
    call print

    ;Set BIOS default values in environment
    cli
    mov dl, 80h ;(drive C)
    xor ax, ax
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov sp, 0ffffh
    sti

    push es
    push 7c00h
    retf            ;Jump to MBR code at 0:7c00h


    ; Print string
print:
    xor bx, bx
    mov ah, 0eh
    cld

@@: lodsb
    test al, al
    jz print_end

    int 10h
    jmp @B

print_end:
    ret

    ; Read sectors of the first cylinder
read_sectors:
    mov ch, 0           ; Cylinder
    mov dh, 0           ; Head
                        ; DL = drive number passed from BIOS
    mov ah, 2
    int 13h
    jnc read_ok

    lea si, disk_error_msg
    call print
read_ok:
    ret

memory_patched_msg      db 'Memory patched', 13, 10, 7, 0
mbr_loaded_msg          db 'MBR loaded', 13, 10, 7, 0
jmp_to_mbr_msg          db 'Jumping to MBR code', 13, 10, 7, 0
disk_error_msg          db 'Disk error', 13, 10, 7, 0

_TEXT ENDS
END start

最佳答案

已编辑 - 新答案:

好吧,看来我首先误解了你的问题。我能给出的唯一进一步建议是:

  • 检查您是否没有加载 HIMEM.SYS 和/或 EMM386.EXE(也没有加载任何其他内存管理器)。当引导加载程序执行时,CPU必须处于实模式。

  • 看看 Ralf Brown 的中断列表。如果我没记错的话,其中有一些有关启动过程的技术信息。它可能会给你一个提示。

  • 查看其他加载器实用程序的源代码,例如加载林。 (它所做的事情与您的实用程序并不完全相同,但仍然可以为您提供一些见解。)

<小时/>

上一个答案:

ORG 100h 真的是引导加载程序中正确的做法吗?

我认为这仅与 DOS .com 可执行文件相关,因为 DOS 将使用程序段前缀 (PSP) 初始化前 256 个字节。如果你编写一个引导加载程序,就没有 DOS,也没有 PSP 之类的东西。我想这必须是ORG 0

关于assembly - 从 DOS 启动 MBR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2639234/

相关文章:

java - InvokeDynamic - 如何访问引导方法中的参数?

assembly - 启用分页后的指令似乎未执行

x86 - int 13h 42h 在 Bochs 中不加载任何东西

assembly - 在 x86 asm 中输出变量值

assembly - x86 上有偏移写回吗?

linux - 为什么数据和堆栈段是可执行的?

c++ - 内联 asm 将值写入内存

c# - 引导 WPF

memory-management - 烧录到闪存的​​特定扇区

yii - Yii 如何知道 CWebApplication 存在?