自己动手写bootloader

自己动手写bootloader

环境准备

  1. 开发操作系统:Ubuntu 18.04
  2. 所需软件
apt install bochs bochs-x nasm build-essential qemu

写 bootloader

  1. 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
  1. 编译
    nasm boot.asm -o boot.bin

  2. 制作软盘
    执行命令bximage,输入fd,选择软盘映像,其余选项默认即可。会在当前目录下创建 a.img 文件

  3. 将编译后的 boot.bin 写入软盘镜像文件 a.img
    dd if=boot.bin of=a.img

  4. 创建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

  1. 使用bochs 模拟器,输入 bochs,如果弹出的bochs窗口是黑屏,在命令行输入c 回车即可
  2. 使用qemu模拟器,输入 qemu-system-i386 -drive file=a.img
  3. 还可以直接使用 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章