(写操作系统自然需要一个承载程序的运行环境通过硬盘启动个mbr程序)
0x00 前言
自己动手写操作系统,自然需要一个承载程序的运行环境,这一章记录我使用bochs遇到的坑,凌晨十二点半才填完坑。
这次记录的是使用bochs完整实现一个最简单的操作系统(其实根本不算操作系统,只是通过硬盘启动个mbr程序)。
安装比较简单了,我没有使用源码安装,不想再折腾那个了,不然估计我就没戏能开始了。
yum install bochs bochs-x -y
需要装两个东东,后面一个如果不过,会报错,如果报错可以看我后面的错误记录。
bochs需要一个配置文件,来描述硬件信息。文件里面有详细的注释。
配置文件就叫bochs.disk,名字可以随意,后缀是什么也无所谓。
############################################################### # Configuration file for Bochs ############################################################### # how much memory the emulated machine will have megs: 32 # 对应真实机器的BIOS和VGA BIOS romimage: file=/usr/share/bochs/BIOS-bochs-latest vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest # 设置bochs使用的磁盘,软盘使用关键字floppya,硬盘使用disk # 若有多个软盘,可写floppya,floppyb #floppya: 1_44=a.img, status=inserted # choose the boot disk. # 默认是软盘,注释掉,改为disk #boot: floppy boot: disk # where do we send log messages? log: /tmp/bochsout.txt # disable the mouse mouse: enabled=0 # enable key mapping, using US layout as default. keyboard_mapping: enabled=1, map=/usr/share/bochs/keymaps/x11-pc-us.map ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14 #这一句是根据bximage生成的,后面会解释。 ata0-master: type=disk, path="/home/dante/workspace/os/hd60M.img", mode=flat, cylinders=121, heads=16, spt=63
这里使用bochs配备的bximage。
root@dante-pc:/home/dante/workspace/os# bximage -hd -mode='flat' -size=60 -q hd60M.img ======================================================================== bximage Disk Image Creation Tool for Bochs $Id: bximage.c 11315 2012-08-05 18:13:38Z vruppert $ ======================================================================== I will create a 'flat' hard disk image with cyl=121 heads=16 sectors per track=63 total sectors=121968 total size=59.55 megabytes Writing: [] Done. I wrote 62447616 bytes to hd60M.img. The following line should appear in your bochsrc: ata0-master: type=disk, path="hd60M.img", mode=flat, cylinders=121, heads=16, spt=63
看到输出的最后一句,指示我们把这句写入bochs的配置文件中。
此处的mbr.bin是通过汇编编译后生成的文件,下一篇博客专门记录这次实现的汇编代码。
代码实现的内容就是在屏幕上打印几个字符。
××注意:×如无必要,后面的参数就不要改了,如果想知道每个参数改了×改后在启动的时候会出错。不过也可以看一下错误信息多了解点知识。
root@dante-pc:/home/dante/workspace/os# dd if=mbr.bin of=hd60M.img bs=512 count=1 conv=notrunc 记录了0+1 的读入 记录了0+1 的写出 502字节(502 B)已复制,0.000140041 秒,3.6 MB/秒
bochs -f bochs.disk
这样就能看到在界面上打印的字符了。
Event type: PANIC Device: [ ] Message: dlopen failed for module 'x': file not found A PANIC has occurred. Do you want to: cont - continue execution alwayscont - continue execution, and don't ask again. This affects only PANIC events from device [ ] die - stop execution now abort - dump core debug - continue and return to bochs debugger Choose one of the actions above: [die]
这是由于在安装的时候,少安装了个bochs-x包,安装即可。
sudo apt-get install bochs-x
# bochs -f bochs.disk ======================================================================== Bochs x86 Emulator 2.6 Built from SVN snapshot on September 2nd, 2012 Compiled on Aug 10 2015 at 10:27:14 ======================================================================== 00000000000i[ ] LTDL_LIBRARY_PATH not set. using compile time default '/usr/lib/bochs/plugins' 00000000000i[ ] BXSHARE not set. using compile time default '/usr/share/bochs' 00000000000i[ ] lt_dlhandle is 0x3ae4fe0 00000000000i[PLGIN] loaded plugin libbx_unmapped.so 00000000000i[ ] lt_dlhandle is 0x3ae6ba0 00000000000i[PLGIN] loaded plugin libbx_biosdev.so 00000000000i[ ] lt_dlhandle is 0x3ae74e0 00000000000i[PLGIN] loaded plugin libbx_speaker.so 00000000000i[ ] lt_dlhandle is 0x3ae7d50 00000000000i[PLGIN] loaded plugin libbx_extfpuirq.so 00000000000i[ ] lt_dlhandle is 0x3ae8590 00000000000i[PLGIN] loaded plugin libbx_parallel.so 00000000000i[ ] lt_dlhandle is 0x3aea240 00000000000i[PLGIN] loaded plugin libbx_serial.so 00000000000i[ ] lt_dlhandle is 0x3aede20 00000000000i[PLGIN] loaded plugin libbx_gameport.so 00000000000i[ ] lt_dlhandle is 0x3aee8c0 00000000000i[PLGIN] loaded plugin libbx_iodebug.so 00000000000i[ ] reading configuration from bochs.disk 00000000000p[ ] >>PANIC<< reading from bochs.disk failed 00000000000e[CTRL ] notify called, but no bxevent_callback function is registered 00000000000i[CPU0 ] CPU is in real mode (active) 00000000000i[CPU0 ] CS.mode = 16 bit 00000000000i[CPU0 ] SS.mode = 16 bit 00000000000i[CPU0 ] EFER = 0x00000000 00000000000i[CPU0 ] | EAX=00000000 EBX=00000000 ECX=00000000 EDX=00000000 00000000000i[CPU0 ] | ESP=00000000 EBP=00000000 ESI=00000000 EDI=00000000 00000000000i[CPU0 ] | IOPL=0 id vip vif ac vm rf nt of df if tf sf ZF af PF cf 00000000000i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D 00000000000i[CPU0 ] | CS:0000( 0000| 0| 0) 00000000 00000000 0 0 00000000000i[CPU0 ] | DS:0000( 0000| 0| 0) 00000000 00000000 0 0 00000000000i[CPU0 ] | SS:0000( 0000| 0| 0) 00000000 00000000 0 0 00000000000i[CPU0 ] | ES:0000( 0000| 0| 0) 00000000 00000000 0 0 00000000000i[CPU0 ] | FS:0000( 0000| 0| 0) 00000000 00000000 0 0 00000000000i[CPU0 ] | GS:0000( 0000| 0| 0) 00000000 00000000 0 0 00000000000i[CPU0 ] | EIP=00000000 (00000000) 00000000000i[CPU0 ] | CR0=0x00000000 CR2=0x00000000 00000000000i[CPU0 ] | CR3=0x00000000 CR4=0x00000000 bx_dbg_read_linear: physical memory read error (phy=0x0000000000000000, lin=0x0000000000000000) 00000000000i[CTRL ] quit_sim called with exit code 1
将boch.disk文件中的vgaromimage: /usr/share/vgabios/vgabios.bin
改为vgaromimage: file=/usr/share/vgabios/vgabios.bin
即可,出现这种错误的,很多是因为直接使用了orange's的代码。
启动bochs后没有反应,一直是黑屏。
...... 00000000000e[ ] bochs.disk:28: 'keyboard_mapping' will be replacedby new 'keyboard' option. 00000000000i[ ] lt_dlhandle is 0x36ac110 00000000000i[PLGIN] loaded plugin libbx_x.so 00000000000i[ ] installing x module as the Bochs GUI 00000000000i[ ] using log file /tmp/bochsout.txt Next at t=0 (0) [0x00000000fffffff0] f000:fff0 (unk. ctxt): jmp far f000:e05b ; ea5be000f0 <bochs:1>
因为网路不好,也没有翻墙,官网进不去,在一个论坛里面看到,直接输入c接可以了。
启动后出现无启动设备的错误。
Boot failed: not a bootable device. FATAL:No bootable device.
这个问题我也忘了具体是怎么改的了,不过根据回忆,应该是和程序相关,我中间改过一次程序,冲洗编译并使用dd写入后就没问题了。
联系客服