白季飞龙的个人主页

自制操作系统

准备工具

1. 制作操作系统镜像,用以显示HelloWorld

操作系统镜像可以使用软盘镜像。标准的软盘镜像大小为1440MB。手工操作编辑太过麻烦,因此这里选用Python脚本来创建与编辑镜像。本质是创建一个空文件,填充字节码。

from pathlib import Path

header_hex = "EB4E9048 454C4C4F 49504C00 02010100 02E00040 0BF00900 12000200 00000000 " \
             "400B0000 000029FF FFFFFF48 454C4C4F 2D4F5320 20204641 54313220 20200000 " \
             "00000000 00000000 00000000 00000000 B800008E D0BC007C 8ED88EC0 BE747C8A " \
             "0483C601 3C007409 B40EBB0F 00CD10EB EEF4EBFD 0A0A".replace(" ", "")
header_bytes = bytes.fromhex(header_hex)
message_bytes = "================================\r\n\n" \
                "    Welcome to BJOS.\r\n\n" \
                "    BJOS is running...\r\n\n" \
                "================================\r\n".encode()
header_bytes = header_bytes + message_bytes

central_hex = "55AAF0FFFF"
central_bytes = bytes.fromhex(central_hex)

tail_hex = "F0FFFF"
tail_bytes = bytes.fromhex(tail_hex)

all_bytes = bytearray(1440 * 1024)
all_bytes[:len(header_bytes)] = header_bytes
all_bytes[0x01FE : 0x01FE + len(central_bytes)] = central_bytes
all_bytes[0x1400 : 0x1400 + len(tail_bytes)] = tail_bytes

print("Writing image to file...")
Path("bjos.img").write_bytes(all_bytes)
print("Done.")

执行以上Python脚本后,会在当前目录生成操作系统镜像文件bjos.img。在当前目录下执行命令qemu-system-i386 bjos.img,即可在虚拟机中引导此镜像文件。

虚拟机启动此镜像后,会在虚拟机屏幕打印出以下内容:

================================

    Welcome to BJOS.

    BJOS is running...

================================

文章首发: https://baijifeilong.github.io


漫漫路,莫论逍遥;潜心修,只为悟道