现在的位置: 首页 >> 操作系统 >> FreeBSD >> FreeBSD 5.2.1 boot0(启动扇区代码分析)
添加时间:2005-8-21 来源:网教中国 作者:
FreeBSD 5.2.1 boot0(启动扇区代码分析)

loop test is at main.5.
#
main.3:
movb %ch,-0x4(%bx) # Zero active flag (ch == 0)
btw %dx,_FLAGS(%bp) # Entry enabled?
jnc main.5 # No
上面首先使得第一个分区的活动标志为0,标志它不为活动标志,因为ch的值为0。至
于第二句“btw %dx,_FLAGS(%bp)”中的_FLAGS(%bp)是上面我们说到的“手动指定我们
实际安装FreeBSD的分区代码”。其中的bit 0x20我们在上面已经提到过。_FLAGS(%bp)
中的其他 位表示是否我们需要检查相应的磁盘分区。缺省情况下,我们需要检查所有
的磁盘分区。检查磁盘分区看是否有可以启动的磁盘分区,例如,可能磁盘上的某个
分区为WindowsXP或者是Linux等。如果我们没有改变在磁盘上该处的值,则相应的bit
位的值 为0,表示所有的分区都要检查(因为此时_FLAGS(%bp)中的值为0),否则,
只针对FLAGS(%bp)中相应的bit位未被设置为1的分区进行检查。

大家知道,FreeBSD Manager启动时可能出现以下的提示:

F1 FreeBSD
F2 ??
F3 BSD
F4 ??
Default F1

其中,上面的提示中出现了令人讨厌的“??”,为了避免出现“??”的提示,我们可以设置相应的
第一分区和第四分区不检查,就需要正确设置_FLAGS(%bp)中的bit位。设置好后,屏幕可能
出现以下的提示:

F1 FreeBSD
F2 BSD
Default F1

#
# If any of the entries in the table are
# the same as the 'type' in the slice table entry,
# then this is an empty or non bootable partition. Skip it.
#
movb (%bx),%al # Load type
movw $tables,%di # Lookup tables
movb $TBL0SZ,%cl # Number of entries
repne # Exclude
scasb # partition?
je main.5 # Yes
我们从上面已经知道起始(%bx)指向的是MBR中分区信息1(16字节)的位置(见图(三)),
以上代码在“忽略的分区类型$tables”中查找看是否本分区是不可启动的或者不合法的分区。
不可启动的或者不合法的分区类型有3($TBL0SZ=3)个,它们是“0x0, 0x5, 0xf”,见下面的
$tables处。如果是不可启动的或者不合法的分区类型则跳转到main.5,进行下一轮循环。

#
# Now scan the table of known types
#
movb $TBL1SZ,%cl # Number of entries
repne # Known
scasb # type?
jne main.4 # No
#
# If it matches get the matching element in the
# next array. if it doesn't, we are already
# pointing at its first element which points to a "?".
#
addw $TBL1SZ,%di # Adjust
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函数,在屏幕上打印:“Fx XXX”。其中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)中取出,


上一页 [1] [2] [3] [4] [5] [6]  下一页


上一篇:网桥原理及源代码详解 下一篇:FreeBSD中的设备命名规则
大部分文章摘自网上,如有侵犯您的权益请与我们联系,我们会第一时间进行处理,谢谢! [ 打印文章 ] [ 关闭窗口 ]
推荐文章
·FreeBSD handbook中文版 9 配制F
·FreeBSD 升级系统
·Ports & Package
·FreeBSD kernel 编译大法(二)ker
·FreeBSD 上使用Kerberos 5认证
·FreeBSD 5.x 中 gbfs 的修正,及
·FreeBSD 安装Linuxigd
·FreeBSD 使用cvsd创建安全的cvs
·FreeBSD trafcount命令介绍
·FreeBSD入门安装及汉化
相关文章
 
最新文章
·FreeBSD连载(94):基于NAT的负载
·FreeBSD连载(93):反向代理负载
·FreeBSD连载(92):基于DNS的负载
·FreeBSD连载(91):提升静态网页
·FreeBSD连载(90):单服务器性能
·FreeBSD连载(89):CGI和SSI的安
·FreeBSD连载(88):安全连接方式S
·FreeBSD连载(87):基于用户的访
·FreeBSD连载(86):对IP地址和域
·FreeBSD连载(85):配置Apache服(
Google