FreeBSD启动扇区代码分析( 四 )


main.4:
movb (%di),%cl # Partition
addw %cx,%di # description
callw putx # Display it上面检查看所检查的分区类型是否为我们知道的分区类型,知道的分区类型有11($TBL1SZ=0xb)个,它们是:“0x1, 0x4, 0x6, 0xb, 0xc, 0xe, 0x83,0x9f, 0xa5, 0xa6, 0xa9,见下面的$tables处 。如果不是以上的类型,则跳转到main.4 。那么,(%di)所指的字串是“??,如果分区类型是“0x1, 0x4, 0x6, 0xb, 0xc, 0xe, 0x83,0x9f, 0xa5, 0xa6, 0xa9之一,则(%di)所指的字串是“Dos、“Linux、“FreeBSD或“BSD等 。见下面的“os_misc、“os_dos、“os_linux、“os_freebsd、“os_bsd等标记 。callw putx调用putx函数,在屏幕上打印:“FxXXX 。其中XXX为DOS、“Linux、“FreeBSD或“BSD等 。main.5:
incw %dx # Next item
addb $0x10,%bl # Next entry
jnc main.3 # Till done遍历磁盘分区的举代号dx加1,重复下一轮循环查找 。bl加上0x10(0x10=16)表示寻址到下一个分区信息(加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 。#

推荐阅读