启动扇区代码分析 FreeBSD 5.2.1 boot0( 五 )


一个分区信息(加16字节)入口 。循环直到255字节边界 。

#
# Passed a 256 byte boundary..
# table is finished.
# Add one to the drive number and check it is valid,
#
popw %ax # Drive number
subb $0x80-0x1,%al # Does next
cmpb NHRDRV,%al # drive exist? (from BIOS?)
jb main.6 # Yes
“popw %ax”把上面压入堆栈的bx(当前的启动扇区)值弹出到ax中 。例如,如果计算机是从软盘
启动的则dl=0,若是从IDE的C、D盘启动的则dl分别为 0x80和0x81 。然而,FreeBSD的Boot Manerger不能够
安装到软盘上,所以,dl只能为0x80、0x81,0x82...等 。
在计算机的BIOS地址0:0x475处存放的是计算机的硬盘的数目,“subb $0x80-0x1,%al”一句等于“sub
$0x79,%al”,例如,即当前驱动器如果是C盘,则al的值是ox80-0x79=1,然后再与计算机的硬盘的数目比
较,如果当前所在硬盘不是最后一个硬盘,则直接跳转到main.6 。如果当前所在硬盘是最后一个硬盘,则继
续执行 。

# If not then if there is only one drive,
# Don"t display drive as an option.
#
decw %ax # Already drive 0?
jz main.7 # Yes
如果只有一个硬盘,则直接跳转到main.7,这样,本计算机只有一个硬盘,所以不用显示其他
磁盘相关的提示 。

# If it was illegal or we cycled through them,
# then go back to drive 0.
#
xorb %al,%al # Drive 0

下面的内容表示多于一个磁盘的情况 。此时“al”清0,与磁盘列举相关 。
#
# Whatever drive we selected, make it an ascii digit and save it back
# to the "next drive" location in the loaded block in case we
# want to save it for next time.
# This also is part of the printed drive string so add 0x80 to indicate
# end of string.
#
main.6:
addb $"0"|0x80,%al # Save next
movb %al,_NXTDRV(%bp) # drive number
movw $drive,%di # Display
callw putx # item
首先,在_NXTDR(%bp)处置入“0字符高位置1”的字符,以代表第二个驱动器,
然后在屏幕上显示“Fx Drive”,表示更换另外的磁盘启动 。注意,在调用
putx之前,di中保存的是下面字串“Drive ”的首地址 。dl中存放的是当前
遍历的到的可启动的或者合法的分区类型递增序数,al与dl是不同的,al是ASCII码,
dl是“Fx”中的x值 。

#
# Now that we"ve printed the drive (if we needed to), display a prompt.
# Get ready for the input byt noting the time.
#
main.7:
movw $prompt,%si # Display
callw putstr # prompt
movb _OPT(%bp),%dl # Display
decw %si # default
callw putkey # key
xorb %ah,%ah # BIOS: Get
int $0x1a # system time
movw %dx,%di # Ticks when
addw _TICKS(%bp),%di # timeout
上面的代码首先在屏幕上打印出字符串“Default: ”,缺省启动的磁 盘号放在
“_OPT(%bp)”中,这里有个小小的技巧,在执行“decw %si”和“callw putkey”
两句后屏幕会显示“Fx”,x是_OPT(%bp)的ASCII 。

然后取得当前的tickes放到%di中,等待用户按键超时的时间从_TICKS(%bp)中取出,
加到当前的tickes即是最后超时时间到的tickes 。

#
# Busy loop, looking for keystrokes but
# keeping one eye on the time.
#
main.8:
movb $0x1,%ah # BIOS: Check
int $0x16 # for keypress
jnz main.11 # Have one
xorb %ah,%ah # BIOS: Get
int $0x1a # system time
cmpw %di,%dx # Timeout?
jb main.8 # No
等待用户按下“Fx”键,同时检查当前等待是否超时,如果有用户按键则跳转到main.11,
如果超时时间不到则继续等待 。

#
# If timed out or defaulting, come here.
#
main.9:
movb _OPT(%bp),%al # Load default
jmp main.12 # Join common code
超时时间到,此时表示用户使用缺省分区启动,把缺省的启动分区号置入al中,然后跳转
到main.12 。

#

推荐阅读