现在的位置: 首页 > 综合 > 正文

win7下编写调试自己的OS

2018年02月06日 ⁄ 综合 ⁄ 共 3322字 ⁄ 字号 评论关闭

win7下编写调试自己的OS

一、工具准备

1.到官网下载——bochs for windows(我下载的bochs-2.6.2.exe)
2.到官网下载——nasm(我下载的nasm-2.11.03-installer.exe)
3.到官网下载——WinImage(我在csdn上下载的ha_WinImage-v8.1.rar)
4.到官网下载——Cygwin(忘了哪一版本了)

二、安装
把以上工具都在自己电脑上安装好

三、运行
1.用编辑器编写一个汇编引导文件boot.asm,用nasm -o boot.bin boot.asm 获得boot.bin文件
2.用WinImage产生一个虚拟软盘,如wb.img
3.用Cygwin,输入命令:dd if=boot.bin of=wb.img bs=512 count=1 conv=notrunc 把引导程序boot.bin复制到虚拟软盘wb.img里

(记着:不要在WinImage里直接把boot.bin文件拖进wb.img里!这样的话无法在bochs里识别出引导程序)------------------

---------------发现,这样制作含有引导扇区的软盘是不对的,cygwin功能不足应该在linux-ubuntu环境下制作。先用../bochs-2.6.2/bximage.exe制作出一个.img,然后.img和boot.bin发到Ubuntu里,在终端输入(在有wb.img的目录下):
/sbin/losetup /dev/loop0 wb.img
/sbin/mkfs.vfat /dev/loop0
这里使用回馈设备loop0使设备和fdt.img关联
然后用mkfs.vfat格式化wb.img(vfat代表fat32格式)
再挂载wb.img
mount /dev/loop0 /mnt/temp
这里,我们就可以向/mnt/temp写入文件了,它的效果就相当于向wb.img写入文件,但这个时候的wb.img是不能被boot.bin(自己写的引导程序)引导的
然后通过write程序,把引导程序写入wb.img第一扇区
write.c
#include  /* unistd.h 需要这个文件 */
#include     /* 包含有read和write函数 */
#include
int main()
{
  char boot_buf[512];
   int floppy_desc, file_desc;
  file_desc = open("./boot.bin", O_RDONLY);          ;boot.bin是我自己写的一个引导程序
  read(file_desc, boot_buf, 510);
  close(file_desc);
  boot_buf[510] = 0x55;
  boot_buf[511] = 0xaa;
  floppy_desc = open("/dev/loop0", O_RDWR);      ;注意,这里是设备/dev/loop0,因为我们用loop0与fdt.img相关联
  lseek(floppy_desc, 0, SEEK_CUR);
  write(floppy_desc, boot_buf, 512);
  close(floppy_desc);
}
编译write.c
gcc write.c -o write
运行write
./write
好了,现在wb.img是一个可引导软盘映像文件了,若要加入文件,则把文件复制到/mnt/temp就可以了

4.分析发现bochs的运行是通过.\Bochs-2.6.2\dlxlinux\run.bat的,而run.bat里就两句话:
cd "E:\Bochs-2.6.2\dlxlinux"
..\bochs -q -f bochsrc.bxrc

可以发现,bochs的配置文件都在同文件夹下的bochsrc.bxrc里,打开bochsrc.bxrc发现果然是配置文件,其内容如下示:
###############################################################
# bochsrc.txt file for DLX Linux disk image.
###############################################################

# how much memory the emulated machine will have
megs: 32

# filename of ROM images
romimage: file=../BIOS-bochs-latest
vgaromimage: file=../VGABIOS-lgpl-latest

# what disk images will be used
floppya: 1_44=floppya.img, status=inserted
floppyb: 1_44=floppyb.img, status=inserted

# hard disk
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
ata0-master: type=disk, path="hd10meg.img", cylinders=306, heads=4, spt=17

# choose the boot disk.
boot: c

# default config interface is textconfig.
#config_interface: textconfig
#config_interface: wx

#display_library: x
# other choices: win32 sdl wx carbon amigaos beos macintosh nogui rfb term svga

# where do we send log messages?
log: bochsout.txt

# disable the mouse, since DLX is text only
mouse: enabled=0

# enable key mapping, using US layout as default.
#
# NOTE: In Bochs 1.4, keyboard mapping is only 100% implemented on X windows.
# However, the key mapping tables are used in the paste function, so
# in the DLX Linux example I'm enabling keyboard mapping so that paste
# will work.  Cut&Paste is currently implemented on win32 and X windows only.

#keyboard: keymap=$BXSHARE/keymaps/x11-pc-us.map
#keyboard: keymap=$BXSHARE/keymaps/x11-pc-fr.map
#keyboard: keymap=$BXSHARE/keymaps/x11-pc-de.map
#keyboard: keymap=$BXSHARE/keymaps/x11-pc-es.map

其中“#”开头的行都是注释行,内容看起来虽然不少,但是除去以“#”开头的注释以外,实际起作用的内容并不多,而且凭借字面意思,还是比较容易理解的。

因此,为了运行我们自己的wb.img里的引导程序boot.bin,我们可以在.\Bochs-2.6.2\下新建一个目录为mylinux\,然后把.\Bochs-2.6.2\dlxlinux\下面的文件
全部复制过来,再把我们之前已经做好的wb.img复制到这里。
然后,在修改配置文件bochsrc.bxrc,相应行改成
floppya: 1_44=wb.img, status=inserted
boot: a

然后,在简单滴修改一下run.bat,改为
cd "E:\Bochs-2.6.2\mylinux"
..\bochs -q -f bochsrc.bxrc
即可。

在./mylinux/文件夹下双击run.bat即可看到自己OS的运行了!

从此,我们就可以在win7下快乐地利用上面的工具来编写试验我们自己的OS了,哈哈哈

抱歉!评论已关闭.