我正在尝试引导加载程序开发教程。我能够使用 FAT2 约定读取 stage12 引导加载程序。现在我正在尝试以实时模式加载内核,然后将其复制到0x0100000地址。
我在复制+跳转到0x0100000时遇到三重错误。 基本上我无法弄清楚如何访问或跳转到0x0100000。
我的代码在使用时有效
IMAGE_PMODE_BASE equ 0x1000
IMAGE_RMODE_BASE equ 0x1000
krnl32.bin: boot/kernel_entry.o ${OBJ}
i386-elf-ld -o $@ -Ttext 0x01000 $^ --oformat binary
我错过了什么?我读过描述符:保护模式下的偏移寻址,它说 DESC:偏移量(16 位)。 此外,使用GDT中的粒度设置将偏移量乘以4KB。
我像这样设置了 gdt:
gdt_start:
dd 0 ; null descriptor
dd 0
; gdt code: ; code descriptor
dw 0FFFFh ; limit low
dw 0 ; base low
db 0 ; base middle
db 10011010b ; access
db 11001111b ; granularity
db 0 ; base high
; gdt data: ; data descriptor
dw 0FFFFh ; limit low (Same as code)10:56 AM 7/8/2007
dw 0 ; base low
db 0 ; base middle
db 10010010b ; access
db 11001111b ; granularity
db 0 ; base high
end_of_gdt:
toc:
dw end_of_gdt - gdt_start - 1 ; limit (Size of GDT)
dd gdt_start ; base of GDT
; give the descriptor offsets names
NULL_DESC equ 0
CODE_DESC equ 0x8
DATA_DESC equ 0x10
我正在链接内核如下:
krnl32.bin: boot/kernel_entry.o ${OBJ}
i386-elf-ld -o $@ -Ttext 0x0100000 $^ --oformat binary
第 2 阶段引导加载程序
[bits 16]
[org 0x500]
jmp main
%include "boot/stage2/print16.s"
%include "boot/stage2/print32.s"
%include "boot/stage2/floppy16_driver.s"
%include "boot/stage2/fat12.s"
%include "boot/stage2/gdt.s"
%include "boot/stage2/a20.s"
;*******************************************************
; Data Section
;*******************************************************
msgFailure db 0x0D, 0x0A, "Failed", 0x00
welcomeMessage db 0x0D, 0x0A, "Landed in STAGE TWO...", 0x00
enableA20Msg db 0x0D, 0x0A, "Enabled A20. Installed GDT", 0x00
ImageName db "KRNL32 BIN"
ImageSize db 0
IMAGE_PMODE_BASE equ 0xffff
IMAGE_RMODE_BASE equ 0x1000
main:
;-------------------------------;
; Setup segments and stack ;
;-------------------------------;
cli ; clear interrupts
xor ax, ax ; null segments
mov ds, ax
mov es, ax
mov ax, 0x0000 ; stack begins at 0x9000-0xffff
mov ss, ax
mov sp, 0xFFFF
sti ; enable interrupts
mov si, welcomeMessage
call Print16
call _EnableA20
call InstallGDT
sti
mov si, enableA20Msg
call Print16
call LoadRoot
mov ebx, 0
mov ebp, IMAGE_RMODE_BASE
mov esi, ImageName
call LoadFile ; load our file
mov dword [ImageSize], ecx
cmp ax, 0
je EnterStage3
mov si, msgFailure
call Print16
mov ah, 0
int 0x16 ; await keypress
int 0x19 ; warm boot computer
jmp $;
EnterStage3:
cli
mov eax, cr0
or eax, 1
mov cr0, eax
jmp CODE_DESC:Stage3
[bits 32]
Stage3:
mov ax, DATA_DESC ; set data segments to data selector (0x10)
mov ds, ax
mov ss, ax
mov es, ax
mov esp, 90000h ; stack begins from 90000h
CopyImage:
mov eax, dword [ImageSize]
movzx ebx, word [bpbBytesPerSector]
mul ebx
mov ebx, 4
div ebx
cld
mov esi, IMAGE_RMODE_BASE
mov edi, IMAGE_PMODE_BASE
mov ecx, eax
rep movsd ; copy image to its protected mode address
jmp IMAGE_PMODE_BASE
jmp $;
设置了一个 GDT,该 GDT 具有用于代码的 32 位描述符和用于数据的 32 位描述符。在保护模式下,寄存器CS/DS/ES/SS/FS/GS不再是在实际模式下看到的分段寄存器。在保护模式下,它们包含一个指向 GDT(或 LDT)中条目的选择器。此代码加载数据寄存器:
mov ax, DATA_DESC ; set data segments to data selector (0x10)
mov ds, ax
mov ss, ax
mov es, ax
您不能像这样设置CS寄存器。FARjmp 既可以设置所需的代码段选择器,也可以在一条指令中设置偏移量。这就是此指令的作用:
jmp CODE_DESC:Stage3
您的GDT使用代码和数据描述符进行设置,这些代码和数据描述符是 4GB 平面内存模型,其中基本0x00000000,限制0xffffffff。这意味着一旦进入保护模式并且您使用指向这些描述符的选择器,您就可以直接从0x00000000访问0xffffffff的所有内存。这意味着您需要做的就是jmp 0x100000
跳转到内存地址0x100000。在代码中,您不仅希望使用 0x100000 作为跳转到的位置,还需要将内存副本用作其基础的内存位置。
考虑到这一点,简单的解决方法是更改:
IMAGE_PMODE_BASE equ 0x1000
自:
IMAGE_PMODE_BASE equ 0x100000