minix (masterboot.s 2.0)启动源码分析
!这段程序的主要任务就是寻找磁盘设备中活动分区的可引导代码并将其加载到0x0000:0x7c00处
!如果软盘不可以引导则测试硬盘是否可以引担?可以则把硬盘mbr扇区的内容加载到0x0000:0x7c00处
!硬盘或软盘的mbr扇区总共有512字节,其中446字节为代码区,64字节为分区表,最后两字节的魔数AA55
!此时系统处于实模式下,段寄存器存储的是段基地址,系统先读取软盘的mbr
! masterboot 2.0 - Master boot block code Author: Kees J. Bot
!
! This code may be placed in the first sector (the boot sector) of a floppy,
! hard disk or hard disk primary partition. There it will perform the
! following actions at boot time:
!
! - If the booted device is a hard disk and one of the partitions is active
! then the active partition is booted.
!
! - Otherwise the next floppy or hard disk device is booted, trying them one
! by one.
!
! To make things a little clearer, the boot path might be:
! /dev/fd0 - Floppy disk containing data, tries fd1 then d0
! [/dev/fd1] - Drive empty
! /dev/c0d0 - Master boot block, selects active partition 2
! /dev/c0d0p2 - Submaster, selects active subpartition 0
! /dev/c0d0p2s0 - Minix bootblock, reads Boot Monitor /boot
! Minix - Started by /boot from a kernel image in /minix
LOADOFF = 0x7C00 ! 本程序被加载的地方
BUFFER = 0x0600 ! 把本程序拷贝到另外一个地方
PART_TABLE = 446 ! 分区表的入口位置
PENTRYSIZE = 16 ! 分区表每一项16字节
MAGIC = 510 ! 魔数所在的位置
! <ibm/partition>.h:
bootind = 0!
sysind = 4!
lowsec = 8!
分区表的信息格式如下:
.text
! Find active (sub)partition, load its first sector, run it.
master:
xor ax, ax
mov ds, ax
mov es, ax
cli
mov ss, ax ! ds = es = ss = Vector segment
mov sp, #LOADOFF
sti
! Copy this code to safety, then jump to it.
mov si, sp ! si = start of this code
push si ! Also where we'll return to eventually
mov di, #BUFFER ! Buffer area
mov cx, #512/2 ! One sector
cld
rep movs
jmpf BUFFER+migrate, 0 ! To safety
!
!
!
!上面的代码块把软盘mbr的512字节移动到0x0000:0x0600处
!然后跳转到migrate的代码处执行
!(为什么dl会表示这个,希望高人解答下。。。呵呵呵)
!接下来首先测试软盘设备是否可引导,即dl的最高位是否为1
!为1则软盘设备可引导否则跳转到nextdisk处
!如果软盘可引导则检测软盘的分区,测试其类型同时是否是活动分区,总共测试4次,如果还没有找到则显示没有活动分区
!然后直接跳到reboot的地方执行,显示一些信息后系统重启^_^
!
!
!
!
migrate:
! Find the active partition
findactive:
testb dl, dl
jns nextdisk ! No bootable partitions on floppies
mov si, #BUFFER+PART_TABLE
find: cmpb sysind(si), #0 ! Partition type, nonzero when in use
jz nextpart
testb bootind(si), #0x80 ! Active partition flag in bit 7
jz nextpart ! It's not active
loadpart:
call load ! Load partition bootstrap
jc error1 ! Not supposed to fail
bootstrap:
ret ! Jump to the master bootstrap
nextpart:
add si, #PENTRYSIZE
cmp si, #BUFFER+PART_TABLE+4*PENTRYSIZE
jb find
!
!
! No active partition, tell 'em
!利用call命令会把下一条要执行的指令的地址压人堆栈的特点吧.ascii字符的初始地址给压人了堆栈
!这样当进入print函数后就可以通过弹栈把字符的首地址取出,最后字符显示完后直接用jmp (si)指令跳转到下一个地方继续执行
!
!
!
call print
.ascii "No active partition\0"
jmp reboot
! There are no active partitions on this drive, try the next drive.
nextdisk:
incb dl ! Increment dl for the next drive
testb dl, dl
js nexthd ! Hard disk if negative
int 0x11 ! Get equipment configuration
shl ax, #1 ! Highest floppy drive # in bits 6-7
shl ax, #1 ! Now in bits 0-1 of ah
andb ah, #0x03 ! Extract bits
cmpb dl, ah ! Must be dl <= ah for drive to exist
ja nextdisk ! Otherwise try disk 0 eventually
call load0 ! Read the next floppy bootstrap
jc nextdisk ! It failed, next disk please
ret ! Jump to the next master bootstrap
nexthd: call load0 ! Read the hard disk bootstrap
error1: jc error ! No disk?
ret
!
!上面的代码首先测试设备是否为硬盘是则直接跳转到加载0号扇区的load0去执行
!否则调用bios的第11号中断,获取磁盘参数,11号中断的说明如下:
!INT 11 - EQUIPMENT DETERMINATION
!Return: AX = equipment flag bits
! 0 diskette installed
! 1 8087 present
! 2,3 always = 11
! 4,5 initial video mode
! 01 = 40x25 color
! 10 = 80x25 color
! 11 = 80X25 IBM monochrome
! 6,7 number of diskette drives (only if bit 0 = 1)
! 00 = 1, 01 = 2, 10 = 3, 11 = 4
! 8 0 = dma present, 1 = no dma on system (PCjr???)
! 9,10,11 number of RS232 cards
! 12 game I/O attached
! 13 serial printer installed (IBM-PCjr)
! internal modem installed (Convertible)
! 14,15 number of printers
!
!这里把6、7位移到了ah的0、1位,ah<=0x03,因为两位最高能表示的软驱数只能是4个,所以如果dl大于ah的话就不是软驱了。。。只能寻找下一个启动设备
!
!
!
! Load sector 0 from the current device. It's either a floppy bootstrap or
! a hard disk master bootstrap.
load0:
mov si, #BUFFER+zero-lowsec ! si = where lowsec(si) is zero
!jmp load
!这里是把si指向了ext_rw处的起始扇区块的地方即mbr所在的0号扇区
!
!
!
!
! Load sector lowsec(si) from the current device. The obvious head, sector,
! and cylinder numbers are ignored in favour of the more trustworthy absolute
! start of partition.
load:
mov di, #3 ! Three retries for floppy spinup
retry: push dx ! Save drive code
push es
push di ! Next call destroys es and di
movb ah, #0x08 ! Code for drive parameters
int 0x13
pop di
pop es
!接下来把读到得逻辑扇区号转换为c\h\s格式,公式为:
!C代表柱面,H代表磁头,S代表扇区,L代表逻辑扇区号
!一个磁道有63个扇区,一个255个磁头,所以一个柱面总共有255*63=16065个扇区
!L=C*16065+H*63+S-1
!反过来可以从逻辑扇区求C\H\S:
!C=L/16065, H=(L%16065)/63, S=(L%16056)%63+1
!下面的代码就是从逻辑扇区转换为C/H/S格式
andb cl, #0x3F ! cl = max sector number (1-origin)
incb dh ! dh = 1 + max head number (0-origin)
movb al, cl ! al = cl = sectors per track
mulb dh ! dh = heads, ax = heads * sectors
mov bx, ax ! bx = sectors per cylinder = heads * sectors(bx柱面每扇区数)
mov ax, lowsec+0(si)
mov dx, lowsec+2(si)! dx:ax = sector within drive
cmp dx, #[1024*255*63-255]>>16 ! Near 8G limit?
!
!这里不太明白为什么还有再减255??
!这里判断硬盘是否为大硬盘,以便对读取第0号扇区进行相应的bios系统调用
!
!
jae bigdisk
!不是大硬盘则把逻辑扇区号转换为c/h/s格式
!以便int 13号中断调用
!
!
div bx ! ax = cylinder, dx = sector within cylinder
xchg ax, dx ! ax = sector within cylinder, dx = cylinder
movb ch, dl ! ch = low 8 bits of cylinder
divb cl ! al = head, ah = sector (0-origin)(cl为最大扇区数)
xorb dl, dl ! About to shift bits 8-9 of cylinder into dl
shr dx, #1
shr dx, #1 ! dl[6..7] = high cylinder
orb dl, ah ! dl[0..5] = sector (0-origin)
movb cl, dl ! cl[0..5] = sector, cl[6..7] = high cyl
incb cl ! cl[0..5] = sector (1-origin)
pop dx ! Restore drive code in dl
movb dh, al ! dh = al = head
mov bx, #LOADOFF ! es:bx = where sector is loaded
mov ax, #0x0201 ! Code for read, just one sector
int 0x13 ! Call the BIOS for a read
jmp rdeval ! Evaluate read result
bigdisk:
mov bx, dx ! bx:ax = dx:ax = sector to read
pop dx ! Restore drive code in dl
push si ! Save si
mov si, #BUFFER+ext_rw ! si = extended read/write parameter packet
mov 8(si), ax ! Starting block number = bx:ax
mov 10(si), bx
movb ah, #0x42 ! Extended read
int 0x13
!
!
!
!bios读硬盘扩展种卸,中断号42为读扇区操作,调用的参数在?data的ext_rw的最后8字节处,
!参考http://wiki.donews.com/index.php ... 4%E8%B5%84%E6%96%99
!
!
!
pop si ! Restore si to point to partition entry
!jmp rdeval
rdeval:
jnc rdok ! Read succeeded
cmpb ah, #0x80 ! Disk timed out? (Floppy drive empty)
je rdbad
dec di
jl rdbad ! Retry count expired
xorb ah, ah
int 0x13 ! Reset
jnc retry ! Try again
rdbad: stc ! Set carry flag
ret
rdok: cmp LOADOFF+MAGIC, #0xAA55
jne nosig ! Error if signature wrong
ret ! Return with carry still clear
nosig: call print
.ascii "Not bootable\0"
jmp reboot
!
!检测cf标志是否为1是则读取失败,读取失败的话ah包含错误码如果为80H,则表示软驱未备妥
!清cf标志,测试次数减一次,(这里di减一后没有使用cmp指令直接就jl,感觉有点奇怪.......^_^)如果没有达到指定次数则调用13号中断0功能号重设磁碟机
!如果达到了指定次数,清cf标志,函数调用返回接下来有可能会(软盘读取失败的情况)寻找下一个可以引导的设备,(硬盘读取失败)则会跳的error处执行。
!
!如果读取成功则判断所读取的扇区的最后两个字节是不是AA55是则读取成功,函数返回
!
!
!
! A read error occurred, complain and hang
error:
mov si, #LOADOFF+errno+1
prnum: movb al, ah ! Error number in ah
andb al, #0x0F ! Low 4 bits
cmpb al, #10 ! A-F?
jb digit ! 0-9!
addb al, #7 ! 'A' - ':'
digit: addb (si), al ! Modify '0' in string
dec si
movb cl, #4 ! Next 4 bits
shrb ah, cl
jnz prnum ! Again if digit > 0
call print
.ascii "Read error "
errno: .ascii "00\0"
!jmp reboot
reboot:
call print
.ascii ". Hit any key to reboot.\0"
xorb ah, ah ! Wait for keypress
int 0x16
call print
.ascii "\r\n\0"
int 0x19
!
!
!
!上面的代码为硬盘读取失败后的执行代码,显示读取失败的错误号,同时重启
!
!
! Print a message.
print: pop si ! si = String following 'call print'
prnext: lodsb ! al = *si++ is char to be printed
testb al, al ! Null marks end
jz prdone
movb ah, #0x0E ! Print character in teletype mode
mov bx, #0x0001 ! Page 0, foreground color
int 0x10
jmp prnext
prdone: jmp (si) ! Continue after the string
!
!int 13号扩展中断需要的参数。。可参考:
!参考http://wiki.donews.com/index.php ... 4%E8%B5%84%E6%96%99
!
!
!
!
.data
! Extended read/write commands require a parameter packet.
ext_rw:
.data1 0x10 ! Length of extended r/w packet
.data1 0 ! Reserved
.data2 1 ! Blocks to transfer (just one)
.data2 LOADOFF ! Buffer address offset
.data2 0 ! Buffer address segment
.data4 0 ! Starting block number low 32 bits (tbfi)
zero: .data4 0 ! Starting block number high 32 bits
有错误的地方还请不吝赐教。。。3q。。
关于磁盘的那部分可以看看这个:http://blog.hjenglish.com/codewo ... 881247.html#1310149
还有这个很详细的分析了启动代码:http://www.os-forum.com/minix/boot/bootblock.php
[ 本帖最后由 losky 于 2009-8-6 10:27 编辑 ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Minix是个好东西啊,至少有些概念挺好的,可惜现实中关注的人比较少,应用不多。
同感同感哈
1、BIOS 加载 mbr 或者 pbr 的时候,DL 中存放的就是驱动器的编号,硬盘驱动器从 0x80 开始,软盘驱动器小于 0x80,光盘驱动器大于 0x80,不过没有规律。
2、[1024*255*63-255]>>16 和 [1024*255*63]>>16 的结果是一样的,都是 0xFB。
minix理论上很好,但是现实中不适用。