【发布时间】:2020-02-23 13:23:43
【问题描述】:
我是操作系统的初学者,我正在尝试从头开始构建操作系统(遵循tutorial)。虽然我可以执行作者提供的代码。但是当我添加我的自定义引导加载程序(这显然需要更多内存,并等待键盘中断来加载内核)然后尝试按照教程中提到的那样添加驱动程序时,我的内核不会执行完整的代码(可能会也可能不会)。
实际上我在 kernel.c 中有 5 个打印语句(当我不添加任何驱动程序时,所有语句都可以正常工作),但是一旦我开始添加驱动程序(即使我没有执行任何驱动程序函数) ,代码似乎避免了底部的打印语句。
我尝试更改内核偏移量,但似乎没有任何效果。
kernel.c
void clear_screen() // clear the entire text screen
{
char *vga = (char *)0xb8000;
unsigned int i = 0;
while (i < (80 * 25 * 2))
{
vga[i] = ' ';
i++;
vga[i] = COLOR;
i++;
}
}
unsigned int kprintf(char *message, unsigned int line) // the message and then the line #
{
char *vga = (char *)0xb8000;
unsigned int i = 0;
i = (line * 80 * 2);
while (*message != 0)
{
if (*message == '\n')
{
line++;
i = (line * 80 * 2);
*message++;
}
else
{
vga[i] = *message;
*message++;
i++;
vga[i] = COLOR;
i++;
}
}
return (1);
}
void main() // like main in a normal C program
{
clear_screen();
kprintf("Hi!\nThis is our Kernel\n", 2);
kprintf("Our Team Members:\n", 12);
kprintf("1. Aditya Garg \n",13);
kprintf("2. Ayush Agarwal\n", 14);
kprintf("3. Anup Aglawe \n", 15);
kprintf("4. Anshuman yadav \n", 16);
kprintf("5. Ujjaval shah \n", 17);
};
bootsect.asm
[org 0x7c00]
KERNEL_OFFSET equ 0x1000
mov [BOOT_DRIVE], dl
mov bp, 0x9000
mov sp, bp
call start1
call load_kernel
call switch_to_pm
jmp $
%include "boot/print.asm"
%include "boot/print_hex.asm"
%include "boot/disk.asm"
%include "boot/gdt.asm"
%include "boot/32bit_print.asm"
%include "boot/switch_pm.asm"
[bits 16]
start1:
mov ax, 0x0
mov ds, ax
mov es, ax
mov bx, 0x8000
mov ax, 13;clearScreen
int 0x10
mov ah,2 ;big font
int 0x10
;Position
mov ah,0x02
mov bh,0x00
mov dh,0x06
mov dl,0x09
int 0x10
mov si, start_os_intro
call Color_String
;New Position
mov ah,0x02
mov bh,0x00
mov dh,0x10
mov dl,0x0a
int 0x10
mov si, press_key
call RedColor_String
mov ax,0x00
int 0x16
mov al,2 ;change font
mov ah,0 ;clear screen
int 0x10
ret
start_os_intro db 'Welcome to DarkraiOS!',0
press_key db '>>Press any key<<',0
; text db 'loading ', 0
load_kernel:
mov bx, KERNEL_OFFSET
mov dh, 1
mov dl, [BOOT_DRIVE]
call disk_load
ret
[bits 32]
BEGIN_PM:
call KERNEL_OFFSET
jmp $
BOOT_DRIVE db 0
times 510 - ($-$$) db 0
dw 0xaa55
生成文件
C_SOURCES = $(wildcard kernel/*.c drivers/*.c)
HEADERS = $(wildcard kernel/*.h drivers/*.h)
# Nice syntax for file extension replacement
OBJ = ${C_SOURCES:.c=.o}
# Change this if your cross-compiler is somewhere else
CC = i686-elf-gcc
GDB = i686-elf-gdb
# -g: Use debugging symbols in gcc
CFLAGS = -g
# First rule is run by default
os-image.bin: boot\bootsect.bin kernel.bin
type $^ > os-image.bin
# '--oformat binary' deletes all symbols as a collateral, so we don't need
# to 'strip' them manually on this case
kernel.bin: boot/kernel_entry.o ${OBJ}
i686-elf-ld -o $@ -Ttext 0x1000 $^ --oformat binary
# Used for debugging purposes
kernel.elf: boot\kernel_entry.o ${OBJ}
i686-elf-ld -o $@ -Ttext 0x1000 $^
run: os-image.bin
qemu-system-i386 -fda os-image.bin
# Open the connection to qemu and load our kernel-object file with symbols
debug: os-image.bin kernel.elf
qemu-system-x86_64 -s -fda os-image.bin &
${GDB} -ex "target remote localhost:1234" -ex "symbol-file kernel.elf"
# Generic rules for wildcards
# To make an object, always compile from its .c
%.o: %.c ${HEADERS}
${CC} ${CFLAGS} -ffreestanding -c $< -o $@
%.o: %.asm
nasm $< -f elf -o $@
%.bin: %.asm
nasm $< -f bin -o $@
clean:
rm -rf *.bin *.dis *.o os-image.bin *.elf
rm -rf kernel/*.o boot/*.bin drivers/*.o boot/*.o
驱动程序端口.c
unsigned char result;
__asm__("in %%dx, %%al" : "=a" (result) : "d" (port));
return result;
}
void port_byte_out (unsigned short port, unsigned char data) {
__asm__("out %%al, %%dx" : : "a" (data), "d" (port));
}
unsigned short port_word_in (unsigned short port) {
unsigned short result;
__asm__("in %%dx, %%ax" : "=a" (result) : "d" (port));
return result;
}
void port_word_out (unsigned short port, unsigned short data) {
__asm__("out %%ax, %%dx" : : "a" (data), "d" (port));
}
驱动程序/ports.h
unsigned char port_byte_in (unsigned short port);
void port_byte_out (unsigned short port, unsigned char data);
unsigned short port_word_in (unsigned short port);
void port_word_out (unsigned short port, unsigned short data);
在添加驱动程序文件夹之前
添加驱动程序文件夹后(我没有使用任何驱动程序的功能)
附: - 如果有人要求,我可以分享我的整个代码。
【问题讨论】:
-
你好,你可以用 qemu-img resize os-image.bin 1M 来调整你的 qemu 操作系统映像的大小,这将允许你在引导加载程序中读取多个扇区而不会出现磁盘错误,我认为这是因为 qemu将您的操作系统映像视为整个磁盘。
标签: assembly x86 kernel bootloader osdev