Bootloader没有加载第二个扇区

时间:2016-09-16 14:46:27

标签: assembly bootloader vga sector

我在汇编中写了一个bootloader。 以下是它的工作原理:

  1. 首先,BIOS正常加载引导加载程序。它被指示去200h。

  2. 在200h,有一些代码位于200h和21Eh之间。它只需切换到VGA模式,使用VGA功能在坐标1,1上绘制品红色像素。它永远地循环代码。

  3. 然而,当我加载它时,它只是闪烁的光标,这是普通的VGA .bin文件不会做的,并会显示一个像素。我检查了一个像素,我没有看到任何东西。我看到的意思是VGA代码没有运行,引导加载器只是加载而没有别的。

    引导程序代码:

     cli
     startload:
     push 0x00000200
     ret
    
     times 510-($-$$) db 0
     db 0x55
     db 0xAA
    

    你可以看到它只是去下一个扇区(从200h开始) 以及200h-21Eh的代码:

    BITS 16
    org 200h
    data:
    xcoord DB '1'
    ycoord DB '1'
    color DB 'D'
    .code:
    vga:
    mov ax, 13h
    int 10h
    mov ax, ycoord
    mov bx, xcoord
    mov cx, 320
    mul cx
    add ax, bx
    mov di, ax
    mov dl, color
    mov [es:di],dl
    jmp vga
    

    (是的,我知道这不是230h字节,这是编译输出的大小,即230h。)

    有什么问题? 注意:这不是讨论如何进入第二部门。它在问为什么不去那里。我还没有找到任何解决方案。

2 个答案:

答案 0 :(得分:3)

回答您的一个问题。这段代码:

startload:
push 0x00000200
ret

几乎相当于几乎绝对跳转到CS:0x200。我们不知道 CS 中的值是多少,但许多BIOS将以CS = 0和IP = 0x7c00开始(但情况并非如此)。除非你把它设置为自己,否则你不能真正依赖 CS 作为一个特定的价值。在大多数情况下, CS 可能为零,这意味着您可能会跳转到物理内存地址0x00200(0x0000:0x0200)。这恰好位于从物理地址0x00000到0x003FF的实模式中断表的中间。跳转到该位置可能会导致某种未定义的行为。

您可以将引导加载程序加载到BOCHS中,BOCHS具有合理的调试器,可以理解16位实模式。您将能够逐步执行代码并确切地确定 CS 是什么以及它跳转到的位置。

您可能要完成的任务可以使用以下代码完成。 这是我之前Stackoverflow answer对另一个问题的简单修改。要了解此代码的作用,请参阅我之前的回答。

简而言之,BIOS从物理内存地址0x7C00开始从磁盘读取单个磁盘扇区(512字节)。如果你想要加载其他扇区,你必须编写将它们加载到内存中的代码,然后在加载后跳转到该代码。

在此示例中,第一阶段是引导加载程序,它从磁盘的扇区2(引导扇区之后的扇区)加载单个扇区第二阶段。在这个例子中,我将通过segment:offset对0x7e0:0x0000在第一个物理地址0x07e00之后加载第二个扇区。 (0x07e0<< 4)+ 0x0000 = 0x07e00。

bootload.asm

bits 16
ORG 0x7c00      ; Bootloader starts at physical address 0x07c00

    ; At start bootloader sets DL to boot drive

    ; Since we specified an ORG(offset) of 0x7c00 we should make sure that
    ; Data Segment (DS) is set accordingly. The DS:Offset that would work
    ; in this case is DS=0 . That would map to segment:offset 0x0000:0x7c00
    ; which is physical memory address (0x0000<<4)+0x7c00 . We can't rely on
    ; DS being set to what we expect upon jumping to our code so we set it
    ; explicitly
    xor ax, ax
    mov ds, ax        ; DS=0

    cli               ; Turn off interrupts for SS:SP update
                      ; to avoid a problem with buggy 8088 CPUs
    mov ss, ax        ; SS = 0x0000
    mov sp, 0x7c00    ; SP = 0x7c00
                      ; We'll set the stack starting just below
                      ; where the bootloader is at 0x0:0x7c00. The
                      ; stack can be placed anywhere in usable and
                      ; unused RAM.
    sti               ; Turn interrupts back on

reset:                ; Resets floppy drive

    xor ax,ax         ; AH = 0 = Reset floppy disk
    int 0x13
    jc reset          ; If carry flag was set, try again

    mov ax,0x07e0     ; When we read the sector, we are going to read to
                      ;    address 0x07e0:0x0000 (phys address 0x07e00)
                      ;    right after the bootloader in memory
    mov es,ax         ; Set ES with 0x07e0
    xor bx,bx         ; Offset to read sector to
floppy:
    mov ah,0x2        ; 2 = Read floppy
    mov al,0x1        ; Reading one sector
    mov ch,0x0        ; Track(Cylinder) 1
    mov cl,0x2        ; Sector 2
    mov dh,0x0        ; Head 1
    int 0x13
    jc floppy         ; If carry flag was set, try again

    jmp 0x07e0:0x0000 ; Jump to 0x7e0:0x0000 setting CS to 0x07e0
                      ;    IP to 0 which is code in second stage
                      ;    (0x07e0<<4)+0x0000 = 0x07e00 physical address

times 510 - ($ - $$) db 0   ; Fill the rest of sector with 0
dw 0xAA55                   ; This is the boot signature

第二阶段将在内存中的引导加载程序之后由INT 13h/AH=2h加载,从物理地址0x07e00开始。

stage2.asm

BITS 16

; ORG needs to be set to the offset of the far jump used to
; reach us. Jump was 0x7e0:0x0000 so ORG = Offset = 0x0000.
ORG 0x0000

main:
    ; Set DS = CS
    mov ax, cs
    mov ds, ax

    ; Set to graphics mode 0x13 (320x200x256)
    mov ax, 13h
    int 10h

    ; Set ES to the VGA video segment at 0xA000
    mov ax, 0xa000
    mov es, ax

vga:
    ; Draw pixel in middle of screen
    mov ax, [ycoord]
    mov bx, [xcoord]
    mov cx, 320
    mul cx
    add ax, bx
    mov di, ax
    mov dl, [color]
    mov [es:di],dl

    ; Put processor in an endless loop
    cli
.endloop:
    hlt
    jmp .endloop

; Put Data after the code
xcoord DW 160
ycoord DW 100
color  DB 0x0D    ; One of the magenta shades in VGA palette

上面的代码是您的VGA代码的略微更改版本,因为您的错误。您的VGA代码没有将 ES 段正确设置为0xA000,这是VGA图形内存的开始;它没有正确地取消引用变量(你使用它们的地址而不是值);坐标的大小是 BYTE 而不是 WORD ;使用ASCII字符值定义变量值。我还在代码后移动了数据。

我修改了代码以在320x200显示屏的中间绘制一个像素,然后无限循环以结束程序。

在Linux(或使用Chrysocome DD的Windows上)使用 NASM 进行汇编,您可以使用以下命令生成720K引导盘:

dd if=/dev/zero of=disk.img bs=1024 count=720
nasm -f bin bootload.asm -o bootload.bin
dd if=bootload.bin of=disk.img conv=notrunc

nasm -f bin stage2.asm -o stage2.bin    
dd if=stage2.bin of=disk.img bs=512 seek=1 conv=notrunc

这构建bootload.bin并将其放入磁盘映像的第一个扇区,然后构建stage2.bin并将其放在磁盘映像的扇区2中。磁盘映像称为disk.img

您应该可以使用 QEMU 进行测试,例如:

qemu-system-i386 -fda disk.img

有关 DD 用法的更多信息,请参阅我的其他Stackoverflow answers。巧合的是,答案是你去年提出的一个问题。

答案 1 :(得分:2)

您似乎缺少一些非常重要的代码来实际将第二个扇区读入内存。像这样:

Program program = programs.Where(X => X.ID == id).First();

if (project == null) // Where does your project come from? Did you mean program?
{
    return HttpNotFound();
}

这假设您已将mov ah,0x02 ; read sectors into memory mov al,0x10 ; number of sectors to read (16) mov dl,[bootDrive] ; drive number mov ch,0 ; cylinder number mov dh,0 ; head number mov cl,2 ; starting sector number mov bx,Main ; address to load to int 0x13 ; call the interrupt routine 的值保存到标签为dl的字节中。显然,您需要更改加载代码的地址。

使用bootDrive只是配置汇编程序来处理非可重定位地址引用的生成。