【问题标题】:Loading a kernel without a filesystem - osdev在没有文件系统的情况下加载内核 - osdev
【发布时间】:2017-10-13 08:31:25
【问题描述】:

我构建了一个引导加载程序来将我的内核加载到内存中。内核代码位于软盘的扇区中。 2 阶段引导加载程序从软盘中读取内存的原始字节并将其放在内存中并执行内核。这在 bochs 模拟器上运行良好,但在 qemu 模拟器上失败。

那里的一些教程建议将内核文件保存在文件系统(如 FAT12)上,然后从中读取文件。 所以,我想问一下,这样的系统真的可以在物理机器上工作,还是会像在 qemu 模拟器上那样失败?另外这样读内核好不好?

我将来可能会在 C 中实现一个文件系统,而不是在使用程序集的引导加载程序中实现它

qemu 的问题可能是由于我的图像文件不是 512 字节的倍数,导致最后一个扇区无法读取

编辑: 第一阶段引导加载程序成功地在 Qemu 和 Bochs 上加载第二阶段。 第 1 阶段引导加载程序是-

[org 0x7c00]

STAGE2 equ 0x800

STAGE2_SECTORS equ 2+1
TRACKS equ 2

mov [BOOT_DRIVE],dl

mov bp,0x9000
mov sp,bp

mov bx, msgReal
call print_string

call load_stage2

call STAGE2

jmp $

%include 'boot/bios.ASM'

[bits 16]
load_stage2:
    mov bx, msgStage2
    call print_string
    mov cl, 2
    mov bx, STAGE2
    mov dh, 1
    mov dl, [BOOT_DRIVE]
load_sector:
    call disk_load
    cmp cl, STAGE2_SECTORS
    je loaded
    cmp cl, 15
    add cl, 1
    add bx, 512
    jmp load_sector
loaded:
    ret

BOOT_DRIVE db 0
msgReal db "Booted in 16-bit mode",0
msgStage2 db "Loading the stage2 boot loader onto memory",0

times 510-($-$$) db 0
dw 0xaa55

第 2 阶段引导加载程序是-

[org 0x800]

KERNEL equ 0x1000
KERNEL_SECTORS equ 24

mov bx, msgStage2
call print_string

call load_kernel

mov bx, msg
call print_string

int 0x12
mov [0x600], ax

call switch_to_pm

%include 'boot/bios.ASM'

%include 'boot/gdt.ASM'
%include 'boot/protected_mode.ASM'
%include 'boot/print32.ASM'

[bits 16]

load_kernel:
    mov bx, msgKernel
    call print_string
    mov ax, 3
    mov cl, 4
    mov ch, 0
    mov bx, KERNEL
    mov dl, [BOOT_DRIVE]
    mov dh, 0
    mov ch, 0
load_sector:
    mov ah, 0x02
    mov al, 1
    int 0x13
    jc error1
    cmp al, 1
    jne error2
    push bx
    mov bl, [Sector]
    cmp bl, KERNEL_SECTORS
    pop bx
    je loaded
    push bx
    mov bl, [Sector]
    inc bl
    mov [Sector], bl
    pop bx
    inc cl
    cmp cl, 18
    jne continue
    add ch, 1
    mov cl, 1
continue:
    add bx, BytesPerSector
    jmp load_sector
loaded:
    ret

error1:
    mov bx, errorMsg1
    call print_string
    jmp $

error2:
    mov bx, errorMsg2
    call print_string
    jmp $

[bits 32]

BEGIN_PM:
    mov ebx, msgProt
    call print_string32
    call KERNEL
    jmp $

BytesPerSector equ 512
NumHeads equ 2
SectorsPerTrack equ 18

Sector db 0

BOOT_DRIVE db 0
msgStage2 db "Stage 2 reached!", 0
msgProt db "Successfully switched to 32-bit mode",0
msgKernel db "Loading the kernel onto memory",0
msg db "Loaded!!", 0
errorMsg1 db "Error1", 0
errorMsg2 db "Error2", 0

times 1024-($-$$) db 0

bios.asm

[bits 16]

print_string:
    pusha
    mov cx,bx
    mov ah,0x0e
    printStringStart:
    mov al,[bx]
    cmp al,0
    je done
    int 0x10
    inc bx
    jmp printStringStart
    done:
    popa
    ret

disk_load:
    pusha
    push dx
    mov ah,0x02
    mov al,dh
    mov dh,0x0
    int 0x13
    jc disk_error
    pop dx
    cmp dh,al
    jne disk_error
    popa
    ret

disk_error:
    mov ah,0x0e
    mov al,'X'
    int 0x10
    mov bx,errMsg
    call print_string
    jmp $

errMsg:
    db "Disk Read Error....."
    times 80-20 db " "
    db 0

protected_mode.asm:

[bits 16]
switch_to_pm:
    cli
    lgdt [gdt_descriptor]
    mov eax, cr0
    or eax, 0x1
    mov cr0, eax
    jmp CODE_SEG:init_pm

[bits 32]
init_pm:
    mov ax, DATA_SEG
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax

    mov ebp,0x90000
    mov esp,0x90000

    call BEGIN_PM

print32.asm:

[bits 32]

VIDEO_MEM equ 0xb8000
DEF_COLOR equ 0x0f

print_string32:
    pusha
    mov edx,VIDEO_MEM

print_string32_loop:
    mov al, [ebx]
    mov ah, DEF_COLOR

    cmp al,0
    je print_string32_end

    mov [edx],ax

    inc ebx
    add edx,2
    jmp print_string32_loop

print_string32_end:
    popa
    ret

gdt.asm:

gdt_start:
gdt_null:
    dd 0x0
    dd 0x0

gdt_code:
    dw 0xffff
    dw 0x0
    db 0x0
    db 10011010b
    db 11001111b
    db 0x0

gdt_data:
    dw 0xffff
    dw 0x0
    db 0x0
    db 10010010b
    db 11001111b
    db 0x0
gdt_end:

gdt_descriptor:
    dw gdt_end - gdt_start - 1
    dd gdt_start

CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start

制作文件

DIRECTORIES = boot kernel drivers HALx86 dataman

C_SOURCES = $(wildcard drivers/*.c HALx86/*.c dataman/*.c)
ASM_SOURCES = $(wildcard HALx86/*.asm)

CC = gcc
CFLAGS = -DDEBUG -m32 -ffreestanding -c -nostdlib

KERNEL = kernel/kernel_start.o kernel/kernel.o

ASM = nasm
AOFLAGS = -f elf32 -o
ABINFLAGS = -f bin -o

OBJ = ${C_SOURCES:.c=.o}
ASMOBJ = ${ASM_SOURCES:.asm=.o}

all: os-image.img

os-image.img: boot/boot_sector.bin boot/boot_stage2.bin kernel/kernel.bin
    cat $^ > $@
    echo "OS Image size:"
    wc -c os-image.img

kernel/kernel.bin: $(KERNEL) ${OBJ} ${ASMOBJ}
    ld -melf_i386 -o $@ -Ttext 0x1000 $^ --oformat binary

%.o : %.c
    $(CC) $(CFLAGS) $< -o $@

%.o : %.asm
    $(ASM) $< $(AOFLAGS) $@

%.bin : %.asm 
    nasm $< $(ABINFLAGS) $@

clean:
    rm -fr kernel/*.o
    rm -fr drivers/*.o
    rm -fr HALx86/*.o
    rm -fr dataman/*.o
    rm -fr boot/*.bin
    rm -fr os-image.img *.bin *.o

rebuild:
    make clean
    make

backup:
    make clean
    zip -r backups/BACKUP_DATE-`date +%d-%m-%Y_%H-%M-%S`.zip $(DIRECTORIES) README.txt makefile
    make

【问题讨论】:

  • 是的,从 FAT12 文件系统读取文件可以通过引导加载程序进行。一些操作系统已经完成了 30 多年。如果它对您不起作用,那么它是您的代码中的一个错误。
  • 我从未尝试使用 FAT12 文件系统,而是将二进制程序按顺序直接放在扇区上。它有什么缺点吗?因为它只适用于 bochs 而不是 qemu。在我看来,我的代码在物理机器上不起作用并没有什么大的原因。 @Mich
  • 如果它适用于一个而不是另一个,那是因为您编码错误。您可能在代码中做了一些不正确的假设。如果您发布所有代码,我可能会告诉您为什么它在 BOCHS 而不是 QEMU 中有效并建议修复。如果使用 USB 在真实硬件上测试它,您甚至可能必须在引导加载程序中放置一个 BIOS 参数块。引导加载程序开发中有很多问题。
  • 我已经为第 2 阶段引导加载程序 @MichaelPetch 添加了代码
  • 从所有文件中添加所有代码。通常问题可能始于早期错误的事情。每个文件,以便人们可以复制它。不这样做很可能不会给你答案。

标签: kernel bootloader osdev bochs


【解决方案1】:

没有文件系统,就无法从文件加载内核。

我见过的大多数旧版 BIOS 教程都没有将内核放在一个文件中。相反,他们只是将引导加载程序和内核连接成一个原始二进制文件,并将其重命名为软盘映像。这样,您可以直接从磁盘的扇区加载内核。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 2010-10-05
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    相关资源
    最近更新 更多