| FreeBSD 5.2.1 boot0(启动扇区代码分析) |
| 责任编辑:admin 更新日期:2005-8-6 |
# If it fails to read or isn't marked bootable, treat it
# as a bad selection.
# XXX what does %si carry?
#
main.15:
movw $LOAD,%bx # Address for read
movb $0x2,%ah # Read sector
callw intx13 # from disk
jc main.10 # If error
把我们上面选择到的分区读到0x7c00处,就象我们刚刚才加电启动一样,只是活动分区改变
了而已。如果发生读错误则直接跳转到main.10。使用户重新选择启动分区。
cmpw $MAGIC,0x1fe(%bx) # Bootable?
jne main.10 # No
判断引导扇区的最后两个字节是否是“0x55AA”,如果不是,则表示此扇区是不可引导的,或
不合法的引导扇区则直接跳转到main.10。使用户重新选择启动分区。
pushw %si # Save
movw $crlf,%si # Leave some
callw puts # space
popw %si # Restore
打印“回车”和“换行”。
jmp *%bx # Invoke bootstrap
跳转到我们选择的引导扇区去执行。整个Boot Manager代码到此执行完毕。
#
# Display routines
#
putkey:
movb $'F',%al # Display
callw putchr # 'F'
movb $'1',%al # Prepare
addb %dl,%al # digit
jmp putstr.1 # Display the rest
“putkey”函数在屏幕上打印“F1”、“F2”或“F3”等。如果dl为0则打印“F1”,
如果dl为1则打印“F2”,如果dl为3则打印“F3”。和调用“putstr”在屏幕上打印
es:si指向的以最高位置1为结束字符的字符串。
#
# Display the option and note that it is a valid option.
# That last point is a bit tricky..
#
putx: #首先,把_MNUOPT(%bp)的第dx位(bit)置1,表示此菜单选项被显示。然后在
屏幕上打印空格和es:di指向的以最高位置1为结束字符的字符串。
btsw %dx,_MNUOPT(%bp) # Enable menu option
movw $item,%si # Display
callw putkey # key
movw %di,%si # Display the rest
puts: #调用“putstr”在屏幕上打印es:si指向的以最高位置1为结束字符的字符串。
callw putstr # Display string
putn: #“putn”打印“回车/换行”后在屏幕上打印es:si指向的以最高位置1为结束字符的字符串。
movw $crlf,%si # To next line
putstr: #“putstr”在屏幕上打印es:si指向的以最高位置1为结束字符的字符串。
lodsb # Get byte
testb $0x80,%al # End of string?
jnz putstr.2 # Yes
putstr.1:
callw putchr # Display char
jmp putstr # Continue
putstr.2:
andb $~0x80,%al # Clear MSB
putchr:
pushw %bx # Save
movw $0x7,%bx # Page:attribute
movb $0xe,%ah # BIOS: Display
int $0x10 # character
popw %bx # Restore
retw # To caller
“putchr”在屏幕上显示“al”中的字符。
# One-sector disk I/O routine
intx13:
movb 0x1(%si),%dh # Load head
movw 0x2(%si),%cx # Load cylinder:sector
movb $0x1,%al # Sector count
pushw %si # Save
movw %sp,%di # Save
testb $0x80,_FLAGS(%bp) # Use packet interface?
jz intx13.1 # No
pushl $0x0 # Set the
pushl 0x8(%si) # LBA address
pushw %es # Set the transfer
pushw %bx # buffer address
push $0x1 # Block count
push $0x10 # Packet size
movw %sp,%si # Packet pointer
decw %ax # Verify off
orb $0x40,%ah # Use disk packet
intx13.1:
int $0x13 # BIOS: Disk I/O
movw %di,%sp # Restore
popw %si # Restore
retw # To caller
# Menu strings
item:
.ascii " "; .byte ' '|0x80
prompt:
.ascii "\nDefault:"; .byte ' '|0x80
crlf:
.ascii "\r"; .byte '\n'|0x80
# Partition type tables
tables:
#
# These entries identify invalid or NON BOOT types and partitions.
#
.byte 0x0, 0x5, 0xf
#
# These values indicate bootable types we know the names of
#
.byte 0x1, 0x4, 0x6, 0xb, 0xc, 0xe, 0x83
.byte 0x9f, 0xa5, 0xa6, 0xa9
#
# These are offsets that match the known names above and point to the strings
# that will be printed.
#
.byte os_misc-. # Unknown
.byte os_dos-. # DOS
.byte os_dos-. # DOS
.byte os_dos-. # DOS
.byte os_dos-. # Windows
.byte os_dos-. # Windows
.byte os_dos-. # Windows
.byte os_linux-. # Linux
.byte os_bsd-. # BSD/OS
.byte os_freebsd-. # FreeBSD
.byte os_bsd-. # OpenBSD
.byte os_bsd-. # NetBSD
#
# And here are the strings themselves. 0x80 or'd into a byte indicates
# the end of the string. (not so great for Russians but...)
#
os_misc: .ascii "?"; .byte '?'|0x80
os_dos: .ascii "DO"; .byte 'S'|0x80
os_linux: .ascii "Linu"; .byte 'x'|0x80
os_freebsd: .ascii "Free"
os_bsd: .ascii "BS"; .byte 'D'|0x80
.org PRT_OFF-0xe,0x90
.word B0MAGIC # Magic number
#
# These values are sometimes changed before writing back to the drive
# Be especially careful that nxtdrv: must come after drive:, as it
# is part of the same string.
#
drive:
.ascii "Drive "
nxtdrv:
.byte 0x0 # Next drive number
opt:
.byte 0x0 # Option
setdrv:
.byte 0x80 # Drive to force
flags:
.byte FLAGS # Flags
ticks:
.word TICKS # Delay
#
# here is the 64 byte partition table that fdisk would fiddle with.
#
partbl: .fill 0x40,0x1,0x0 # Partition table
.word MAGIC # Magic number MAGIC=0x55AA
以上保留0x40(十进制为64)个字节“伪”分区空间,且全部清零。partbl相对引导
山区代码的位置为0x1BE(见下图)。
MBR(Main Boot Record),按其字面上的理解即为主引导记录区,位于整个硬盘的0磁道0
柱面1扇区。不过,在总共512字节的主引导扇区中,MBR只占用了其中的446个字节(偏移
0--偏移1BDH),另外的64个字节(偏移1BEH--偏移1FDH)交给了DPT(Disk Partition Table
硬盘分区表)(见下表),最后两个字节"0x55aa"(偏移1FEH-偏移1FFH)是分区的结束标志。
这个整体构成了硬盘的主引导扇区。大致的结构如图(二):详见王巍的文章:《第一讲
操作系统简介及Win98安装》
0000┏━━━━━━━━━━━━━━━━━━┓
┃ ┃
┃ ┃
┃ ┃
┃ ┃
┃ ┃
┃ 主引导记录(MBR) 446字节 ┃
┃ ┃
┃ ┃
┃ ┃
┃ ┃
01BD┃ ┃
┣━━━━━━━━━━━━━━━━━━┫
01BE┃ 分区信息1(16字节) ┃<--partbl指向这里。
┣━━━━━━━━━━━━━━━━━━┫
01CE┃ 分区信息2(16字节) ┃
┣━━━━━━━━━━━━━━━━━━┫
01DE┃ 分区信息3(16字节) ┃
┣━━━━━━━━━━━━━━━━━━┫
01EE┃ 分区信息4(16字节) ┃
┣━━━━━━━━┳━━━━━━━━━┫
01FE┃ 0x55 ┃ 0xAA ┃
┗━━━━━━━━┻━━━━━━━━━┛
图(三)
其中,分区信息1-4的内容如下图所示:
偏移
长度
所表达的意义
0
字节
分区状态:如0-->非活动分区
80--> 活动分区
1
字节
该分区起始头(HEAD)
2
字
该分区起始扇区和起始柱面
4
字节
该分区类型:如82--> Linux Native分区
83--> Linux Swap 分区
5
字节
该分区终止头(HEAD)
6
字
该分区终止扇区和终止柱面
8
双字
该分区起始绝对分区
C
双字
该分区扇区数
图(四)
注:任何转载或摘抄请保留作者信息和注明文章出处(中文FreeBSD用户组 http://www.cnfug.org )
|
|