自己動手寫bootloader
環境準備
- 開發操作系統:Ubuntu 18.04
- 所需軟件
apt install bochs bochs-x nasm build-essential qemu
寫 bootloader
- boot.asm 代碼
; filename boot.asm
org 7c00h ; bis start address
; int 10
mov ax, cs
mov es, ax
mov ax, msg
mov bp, ax
mov cx, msgLen
mov ax, 1301h ;AH=13h表示向TTY顯示字符,AL=01h表示顯示方式(字符串是否包含顯示屬性,01h表示不包含)
mov bx, 000fh ;AH=13h表示向TTY顯示字符,AL=01h表示顯示方式(字符串是否包含顯示屬性,01h表示不包含)
mov dl, 0
int 10h
msg: db "Hello zft" ,0ah,0dh
msgLen: equ $ -msg
times 510 - ($ - $$) db 0
dw 0aa55h
-
編譯
nasm boot.asm -o boot.bin
-
製作軟盤
執行命令bximage
,輸入fd,選擇軟盤映像,其餘選項默認即可。會在當前目錄下創建 a.img 文件 -
將編譯後的 boot.bin 寫入軟盤鏡像文件 a.img
dd if=boot.bin of=a.img
-
創建bochs配置文件 bochsrc,內容如下
###############################################################
# Configuration file for Bochs
###############################################################
# how much memory the emulated machine will have
megs: 32
# filename of ROM images
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest
# what disk images will be used
floppya: 1_44=/home/zft/cpp/bootloader/a.img, status=inserted
# choose the boot disk.
boot: floppy
# where do we send log messages?
log: bochsout.txt
# disable the mouse
mouse: enabled=0
# enable key mapping, using US layout as default.
keyboard: keymap=/usr/share/bochs/keymaps/x11-pc-us.map
啓動bootloader
- 使用bochs 模擬器,輸入
bochs
,如果彈出的bochs窗口是黑屏,在命令行輸入c 回車即可 - 使用qemu模擬器,輸入
qemu-system-i386 -drive file=a.img
- 還可以直接使用
qemu-system-i386 boot.bin
命令
參考文章
https://blog.csdn.net/zjdnwpu/article/details/70194368
https://blog.csdn.net/judyge/article/details/52278384
https://www.cnblogs.com/honpey/p/8505550.html
https://blog.csdn.net/judyge/article/details/52281374