您的位置:首页 > 其它

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

2013-11-30 14:12 211 查看
软件环境:win xp,bochs,nasm,硬盘镜像VHD

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

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

MBR引导程序

;MBR.ASM
;
;
;
SECTION ALIGN=16 VSTART=0x7C00
LBA_BASE_ADDRESS EQU 5

MOV AX , [cs:SS_BASE]
MOV SS , AX
XOR SP , SP

MOV AX , [cs:DS_BASE]
MOV DS , AX
XOR BX , BX

MOV CX , 512

write_memory:
mov byte [bx] , 0x02
inc bx
loop write_memory
xor bx , bx

CALL WRITE_DISK

JMP $

;
;
;
;DS:BX MEMORY ADDRESS
;
WRITE_DISK:

mov dx , 0x1f2
mov al , 0x01
call out_port
call pio_delay

mov dx , 0x1f3
mov al , 0x03
call out_port
call pio_delay

mov dx , 0x1f4
mov al , 0x00
call out_port
call pio_delay

mov dx , 0x1f5
mov al , 0x00
call out_port
call pio_delay

mov dx , 0x1f6
mov al , 0xe0
call out_port
call pio_delay

call check_drdy

;wait?until?the?controller?is?not?busy
busy_hd:
call read_status
and al , 0x80
jnz busy_hd

;wait?until?the?controller?accepts?command
accept_hd:
call read_status
and al , 0x40
jz accept_hd

mov dx , 0x1f7
mov al , 0x30
call out_port
call pio_delay

;wait until the controller is not busy
busy_hd1:
call read_status
and al , 0x80
jnz busy_hd1

;wait until the controller accepts command
accept_hd2:
call read_status
and al , 0x88
cmp al , 0x08
jnz accept_hd2

mov cx ,   512
mov al , 0x12
mov dx , 0x1f0
s:
call out_port
loop s

mov dx, 0x1f0     ; port
mov di, bx        ; buf
mov cx, 256
cld
rep outsw

accept_hd3:
call read_status
test al , 0x40
jz accept_hd3

ret

out_port:
out dx , al
ret
;;
check_drdy:
mov dx , 0x1f7
in al, dx
test al, 0x40
jz check_drdy
ret

read_status:
mov dx , 0x1f7
in al , dx
ret
;;
pio_delay:
nop
nop
nop
nop
ret

DS_BASE DW 0x1000
SS_BASE DW 0x2000
TARGET_PROGRAM_ADDRESS DB 0x02
TARGET_PROGRAM_SIZE DB 0x01

times 510 - ($-$$) db 0
dw 0xaa55
bochs配置文件:

#Configuration file for Bochs

#how much memory the emulated mchine will have
megs:64

#filename of ROM image
romimage:file=./BIOS-bochs-latest
vgaromimage: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=63
ata0-master: type=disk, path="LEECHUNG.vhd", mode=flat, cylinders=1003, heads=12, spt=17

#choose the boot disk
boot:disk

#where do we send log message
log:bochsout.txt

#disable the mouse
mouse: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: