linux引导程序剖析(一)

本文详细解析了早期Linux系统的启动流程,包括bootsect.s和setup.s程序如何协作加载内核到内存,设置CPU控制权转移,加载setup.s及内核代码,以及根据磁盘驱动器参数确定根文件系统的设备号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注释版权:Gilbert Wang
!
! SYS_SIZE is the number of clicks (16 bytes) to be loaded.
! 0x3000 is 0x30000 bytes = 196kB, more than enough for current
! versions of linux
!
SYSSIZE = 0x3000
.globl begtext, begdata, begbss, endtext, enddata, endbss
.text
begtext:
.data
begdata:
.bss
begbss:
.text
Setup.s程序文件的4个扇区
SETUPLEN = 4    ! nr of setup-sectors
系统加电后,BIOS会将bootsect.s文件读入到0x07c0处
BOOTSEG  = 0x07c0   ! original address of boot-sector
之后bootsect.s程序取得cpu控制权后,会将自己移动到0x9000处
INITSEG  = 0x9000   ! we move boot here - out of the way
bootsect.s程序将setup.s文件加载到0x9020处
SETUPSEG = 0x9020   ! setup starts here
bootsect.s将内核代码加载到0x1000处
SYSSEG   = 0x1000   ! system loaded at 0x10000 (65536).
内核代码结束位置
ENDSEG   = SYSSEG + SYSSIZE  ! where to stop loading
! ROOT_DEV: 0x000 - same type of floppy as boot.
!  0x301 - first partition on first drive etc
根文件系统设备号,要使linux能跑起来,需要至少一个根文件系统
ROOT_DEV = 0x306
程序入口
entry start
start:
移动到0x9000位置之前的初始化操作
mov ax,#BOOTSEG
mov ds,ax
mov ax,#INITSEG
mov es,ax
mov cx,#256
sub si,si
sub di,di
开始移动
rep
movw
跳转到基地址0x9000,偏移go处继续执行
jmpi go,INITSEG
当前代码段的基地址是0x9000,将cs,es,ds,ss都置为该值。也就是所数据段,代码段和堆栈段重叠
go: mov ax,cs
mov ds,ax
mov es,ax
! put stack at 0x9ff00.
mov ss,ax
堆栈指针尽量偏移内核代码段,steup.s代码段,也就是说大小应该是
sp要指向大于0x200 + 512(一个扇区的大小) * 4 + 堆栈大小
mov sp,#0xFF00  ! arbitrary value >>512
! load the setup-sectors directly after the bootblock.
! Note that 'es' is already set up.
开始加载setup.s程序文件到0x9200处,利用0x13中断
load_setup:
mov dx,#0x0000  ! drive 0, head 0
mov cx,#0x0002  ! sector 2, track 0
mov bx,#0x0200  ! address = 512, in INITSEG
mov ax,#0x0200+SETUPLEN ! service 2, nr of sectors
int 0x13   ! read it
jnc ok_load_setup  ! ok - continue
mov dx,#0x0000
mov ax,#0x0000  ! reset the diskette
int 0x13
j load_setup
加载成功
ok_load_setup:
取得磁盘驱动器参数,为后面根文件系统的相关处理做准备。
取得磁盘扇区数,存放在sectors中。
! Get disk drive parameters, specifically nr of sectors/track
mov dl,#0x00
mov ax,#0x0800  ! AH=8 is get drive parameters
int 0x13
mov ch,#0x00
seg cs
mov sectors,cx
mov ax,#INITSEG
mov es,ax
在屏幕上打印信息,"Loading System……",利用0x10中断
! Print some inane message
mov ah,#0x03  ! read cursor pos
xor bh,bh
int 0x10

mov cx,#24
mov bx,#0x0007  ! page 0, attribute 7 (normal)
mov bp,#msg1
mov ax,#0x1301  ! write string, move cursor
int 0x10
现在将内核代码加载到0x1000处。
! ok, we've written the message, now
! we want to load the system (at 0x10000)
mov ax,#SYSSEG
mov es,ax  ! segment of 0x010000
call read_it
call kill_motor
! After that we check which root-device to use. If the device is
! defined (!= 0), nothing is done and the given device is used.
! Otherwise, either /dev/PS0 (2,28) or /dev/at0 (2,8), depending
! on the number of sectors that the BIOS reports currently.
最后,判断根文件系统类型,如果之前规定好了根文件系统
类型就不用执行一下代码,如果没有规定,程序将根据之前
读出的磁盘驱动程序的扇区信息来决定根文件系统设备号。
如果是1.2m软磁盘,设备号是0x0208,如果是1.44m软磁盘
设备号就是0x021c
seg cs
mov ax,root_dev
cmp ax,#0
jne root_defined
seg cs
mov bx,sectors
mov ax,#0x0208  ! /dev/ps0 - 1.2Mb
cmp bx,#15
je root_defined
mov ax,#0x021c  ! /dev/PS0 - 1.44Mb
cmp bx,#18
je root_defined
undef_root:
jmp undef_root
root_defined:
seg cs
mov root_dev,ax
! after that (everyting loaded), we jump to
! the setup-routine loaded directly after
! the bootblock:
bootsect.s程序执行完成,将控制权转移到setup.s中去。
jmpi 0,SETUPSEG
变量定义
sectors:
.word 0
msg1:
.byte 13,10
.ascii "Loading system ..."
.byte 13,10,13,10
.org 508
root_dev:
.word ROOT_DEV
boot_flag:
.word 0xAA55
.text
endtext:
.data
enddata:
.bss
endbss:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值