自己写操作系统1——引导扇区

来源:互联网 发布:网络推广优化三尾狐 编辑:程序博客网 时间:2024/05/01 07:40

软件环境:win xp,bochs,nasm,硬盘镜像VHD

这是一个简单的引导程序,因为引导扇区只有512字节,并且最后两个自己必须为55aa所以一共可以利用的字节只有510,但是作为联系应该够了。

这只是一个简单的例子,让引导程序读取虚拟硬盘镜像的三号扇区中的512字节数据到内存0x7e00处。

MBR引导程序

;MBR.ASM;;;SECTION ALIGN=16 VSTART=0x7C00LBA_BASE_ADDRESS EQU 5MOV AX , [cs:SS_BASE]MOV SS , AXXOR SP , SPMOV AX , [cs:DS_BASE]MOV DS , AXXOR BX , BXMOV CX , 512write_memory:mov byte [bx] , 0x02inc bxloop write_memoryxor bx , bxCALL WRITE_DISKJMP $;;;;DS:BX MEMORY ADDRESS;WRITE_DISK:mov dx , 0x1f2mov al , 0x01call out_portcall pio_delaymov dx , 0x1f3mov al , 0x03call out_portcall pio_delaymov dx , 0x1f4mov al , 0x00call out_portcall pio_delaymov dx , 0x1f5mov al , 0x00call out_portcall pio_delaymov dx , 0x1f6mov al , 0xe0call out_portcall pio_delaycall check_drdy;wait?until?the?controller?is?not?busybusy_hd:call read_statusand al , 0x80jnz busy_hd;wait?until?the?controller?accepts?commandaccept_hd:call read_statusand al , 0x40jz accept_hdmov dx , 0x1f7mov al , 0x30call out_portcall pio_delay;wait until the controller is not busybusy_hd1:call read_statusand al , 0x80jnz busy_hd1;wait until the controller accepts commandaccept_hd2:call read_statusand al , 0x88cmp al , 0x08jnz accept_hd2mov cx ,   512mov al , 0x12mov dx , 0x1f0s:call out_portloop smov dx, 0x1f0     ; portmov di, bx        ; bufmov cx, 256cldrep outsw  accept_hd3:call read_statustest al , 0x40jz accept_hd3retout_port:out dx , alret;;check_drdy:mov dx , 0x1f7in al, dxtest al, 0x40jz check_drdyretread_status:mov dx , 0x1f7in al , dxret;;pio_delay:       nop       nop       nop       nop  retDS_BASE DW 0x1000SS_BASE DW 0x2000TARGET_PROGRAM_ADDRESS DB 0x02TARGET_PROGRAM_SIZE DB 0x01times 510 - ($-$$) db 0dw 0xaa55
bochs配置文件:

#Configuration file for Bochs#how much memory the emulated mchine will havemegs:64#filename of ROM imageromimage:file=./BIOS-bochs-latestvgaromimage:file=./VGABIOS-lgpl-latest#what disk images will be used#floppya:1_44=./f.img,status=inserted#hard disk#ata0-master: type=disk, path="sda.img", mode=flat, cylinders=1015, heads=16, spt=63ata0-master: type=disk, path="LEECHUNG.vhd", mode=flat, cylinders=1003, heads=12, spt=17#choose the boot diskboot:disk#where do we send log messagelog:bochsout.txt#disable the mousemouse:enabled=0#enable key mapping, using US layout as default.keyboard_mapping:enabled=1,map=./keymaps/x11-pc-us.map
使用dd命令将mbr.asm程序写入到主引导删除中

dd if=mbr.bin of=XXXXX.vhd bs=512 count=1