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

<-bp指向这里(0:0x800),以此开始的16字节被清零 。
┃ ┣━━━━━┫以下所称的fake partition entry就是指这里 。
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┣━━━━━┫
┃ ┃ 0 ┃
┃ ┗━━━━━┛
0:0x7c00 ┏━━━━━┓ ┛
┃ ┃
┃ 搬 ┃
┃ 移 ┃
┃ 之 ┃
┃ 前 ┃
┃ 的 ┃
┃ 代 ┃
┃ 码 ┃
┃ ┃
0:0x7dff ┗━━━━━┛

图(二)
#
# Relocate to the new copy of the code.
#
incb -0xe(%di) # Sector number
jmp main-LOAD ORIGIN # To relocated code

把以上清零的16字节的第二个字节置为1,表示我们已经读取了一个分区 。然后跳转到搬
移之后的新代码的main处执行 。

#
# Check what flags were loaded with us, specifically, Use a predefined Drive.
# If what the bios gives us is bad, use the "0" in the block instead, as well.
#
main:
testb $0x20,_FLAGS(%bp) # Set number drive?
jnz main.1 # Yes
testb %dl,%dl # Drive number valid?
js main.2 # Possibly (0x80 set)
main.1:
movb _SETDRV(%bp),%dl # Drive number to use
上面说过,BIOS把磁盘的引导扇区读入到内存之后,其dl的内容表示启动设备,但我们 安装好FreeBSD
之后,我们可以改变此引导扇区的内容,其中的一个改变就是可以使我们可以“手动指定”我们实际安
装FreeBSD的分区,如果我们希望指定FreeBSD所在的boot分区,那么我们在bp-0x45处的位置
(即_FLAGS(%bp)处)的bit 0x20置1,那么上面的“movb _SETDRV(%bp),%dl”一句中movb
_SETDRV(%bp),%dl(即bp-0x46)即指向我们“手动指定”FreeBSD所在分区 代码,例如,IDE的C、D
盘(严格来说是第一个物理磁 盘的第一个和第二个分区)的代码分别为 0x80和0x81 。如果没有“手动指
定”启动分区,那么,我们 缺省使用机器当前启动的分区,上面说过,机器当前启动的分区代码放在dl中 。

因为FreeBSD Boot Manager 不可能安装到软盘(如果从软盘启动则dl为0),所以,使用testb %dl,%dl
来判断驱动器代码是否合法(volid) 。

有关_FLAGS(%bp)中其他bit位表示的意义,在随后的代码分析中慢慢给你道来 。

#
# Whatever we decided to use, now store it into the fake
# partition entry that lives in the data space above us.
#
main.2:
movb %dl,_FAKE(%bp) # Save drive number
callw putn # To new line
pushw %dx # Save drive number

以上第一句把FreeBSD启动分区的代码保存到_FAKE(%bp)(bp-0)处,也就是说,上图(二)的bp
处保存的是FreeBSD启动分区的代码(_FAKE=0) 。

“callw putn”一句在屏幕上打印“回车”和“换行”,“pushw %dx”一句把启动分区
的值压入堆栈 。
#
# Start out with a pointer to the 4th byte of the first table entry
# so that after 4 iterations it"s beyond the end of the sector.
# and beyond a 256 byte boundary and has overflowed 8 bits (see next comment).
# (remember that the table starts 2 bytes earlier than you would expect
# as the bootable flag is after it in the block)
#
movw $(partbl 0x4),%bx # Partition table ( 4)
xorw %dx,%dx # Item number

以上代码首先把%bx指向分区表partbl的的第四个字节,这里存放的是分区类型,如82表示

推荐阅读