From 3d37bf9c3a4b02a2f9a1dfb47ed8e3619e2bfe14 Mon Sep 17 00:00:00 2001 From: fslongjin Date: Fri, 21 Jan 2022 22:04:24 +0800 Subject: [PATCH] =?UTF-8?q?:wrench:=20=E6=9B=B4=E6=94=B9=E4=B8=BA=E4=BD=BF?= =?UTF-8?q?=E7=94=A8bochs=E8=99=9A=E6=8B=9F=E6=9C=BA=EF=BC=88qemu=E6=9A=82?= =?UTF-8?q?=E6=97=B6=E6=B2=A1=E6=B3=95=E6=AD=A3=E5=B8=B8=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E7=94=BB=E9=9D=A2=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bochsrc | 5 +-- bootloader/Makefile | 2 +- kernel/main.c | 35 +++++++++++++++++++- run_in_bochs.sh | 79 +++++++++++++++++++++++++++++++++++++++++++++ run_in_qemu.sh | 4 +-- 5 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 run_in_bochs.sh diff --git a/bochsrc b/bochsrc index 25759bb7..4dd26aed 100644 --- a/bochsrc +++ b/bochsrc @@ -1,13 +1,13 @@ # configuration file generated by Bochs plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, parallel=1, serial=1, iodebug=1 config_interface: textconfig -display_library: sdl +# display_library: x11 #memory: host=2048, guest=2048 romimage: file="/usr/local/share/bochs/BIOS-bochs-latest" vgaromimage: file="/usr/local/share/bochs/VGABIOS-lgpl-latest" boot: floppy floppy_bootsig_check: disabled=0 -floppya: type=1_44, 1_44="./bin/boot.img", status=inserted, write_protected=0 +floppya: type=1_44, 1_44="bin/boot.img", status=inserted, write_protected=0 # no floppyb ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14 ata0-master: type=none @@ -51,3 +51,4 @@ com4: enabled=0 megs: 2048 + # gdbstub: enabled=1, port=1234, text_base=0, data_base=0, bss_base=0; \ No newline at end of file diff --git a/bootloader/Makefile b/bootloader/Makefile index 10a66a6a..e58c32f0 100644 --- a/bootloader/Makefile +++ b/bootloader/Makefile @@ -4,7 +4,7 @@ boot.bin: boot.asm nasm boot.asm -o ../bin/bootloader/boot.bin loader.bin: loader.asm - nasm loader.asm -o ../bin/bootloader/lodaer.bin + nasm loader.asm -o ../bin/bootloader/loader.bin clean: diff --git a/kernel/main.c b/kernel/main.c index 7be7a141..1c816296 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -1,8 +1,41 @@ // // Created by longjin on 2022/1/20. // +int *address = (int *)0xffff800000a00000; //帧缓存区的地址 +void show_color_band(int width, int height, char a, char b, char c, char d) +{ + /** 向帧缓冲区写入像素值 + * @param address: 帧缓存区的地址 + * @param val:像素值 + */ + + for (int i = 0; i < width * height; ++i) + { + + *((char *)address + 0) = d; + *((char *)address + 1) = c; + *((char *)address + 2) = b; + *((char *)address + 3) = a; + ++address; + } +} + +//操作系统内核从这里开始执行 void Start_Kernel(void) { - while(1); + + + + + show_color_band(1440, 20, 0x00, 0xff, 0x00, 0x00); + + show_color_band(1440, 20, 0x00, 0x00, 0xff, 0x00); + + show_color_band(1440, 20, 0x00, 0x00, 0x00, 0xff); + + show_color_band(1440, 20, 0x00, 0xff, 0xff, 0xff); + + while (1) + ; } diff --git a/run_in_bochs.sh b/run_in_bochs.sh new file mode 100644 index 00000000..9ccb5e1e --- /dev/null +++ b/run_in_bochs.sh @@ -0,0 +1,79 @@ +# ======检查是否以sudo运行================= +uid=`id -u` +if [ ! $uid == "0" ];then + echo "请以sudo权限运行" + exit +fi + +# 第一个参数如果是--notbuild 那就不构建,直接运行 +if [ ! "$1" == "--nobuild" ]; then + echo "开始构建..." + make all + make clean +fi + +# ==============检查文件是否齐全================ +if [ ! -x "bin/bootloader/boot.bin" ]; then + echo "bin/bootloader/boot.bin 不存在!" + exit +fi +if [ ! -x "bin/bootloader/loader.bin" ]; then + echo "bin/bootloader/loader.bin 不存在!" + exit +fi +if [ ! -x "bin/boot.img" ]; then + echo "bin/boot.img 不存在!" + exit +fi +# ===============文件检查完毕=================== + + +# =========将引导程序写入boot.img============= +dd if=bin/bootloader/boot.bin of=bin/boot.img bs=512 count=1 conv=notrunc + +# =========创建临时文件夹================== +# 判断临时文件夹是否存在,若不存在则创建新的 +if [ ! -d "tmp/" ]; then + mkdir tmp/ + echo "创建了tmp文件夹" +fi + +# ==============挂载boot.img============= + mkdir tmp/boot + mount bin/boot.img tmp/boot -t vfat -o loop + + # 检查是否挂载成功 + if mountpoint -q tmp/boot + then + echo "成功挂载 boot.img 到 tmp/boot" + # ========把loader.bin复制到boot.img========== + cp bin/bootloader/loader.bin tmp/boot + # ========把内核程序复制到boot.img====== + cp bin/kernel/kernel.bin tmp/boot + sync + # 卸载磁盘 + umount tmp/boot + else + echo "挂载 boot.img 失败!" + fi + + + +# 运行结束后删除tmp文件夹 +rm -rf tmp + +# 进行启动前检查 +flag_can_run=0 + +if [ -d "tmp/" ]; then + flag_can_run=0 + echo "tmp文件夹未删除!" +else + flag_can_run=1 +fi + +if [ $flag_can_run -eq 1 ]; then + bochs -f ./bochsrc -q +else + echo "不满足运行条件" +fi \ No newline at end of file diff --git a/run_in_qemu.sh b/run_in_qemu.sh index ab053e63..3d81712e 100644 --- a/run_in_qemu.sh +++ b/run_in_qemu.sh @@ -6,7 +6,7 @@ if [ ! $uid == "0" ];then fi # 第一个参数如果是--notbuild 那就不构建,直接运行 -if [ ! "$1" == "--notbuild" ]; then +if [ ! "$1" == "--nobuild" ]; then echo "开始构建..." make all make clean @@ -73,7 +73,7 @@ else fi if [ $flag_can_run -eq 1 ]; then - qemu-system-x86_64 -s -S -m 2048 -fda bin/boot.img + qemu-system-x86_64 -s -S -m 2048 -fda bin/boot.img else echo "不满足运行条件" fi \ No newline at end of file