📄 618.html
字号:
border=0></TD>
<TD><IMG height=1 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=103
border=0></TD>
<TD><IMG height=1 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=26
border=0></TD>
<TD><IMG height=1 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=1
border=0></TD></TR>
<TR>
<TD colSpan=3><A title=linux知识宝库
href="index.html" tppabs="http://www.linuxhero.com/docs/index.html"><IMG height=83
src="images/header_r1_c1.gif" tppabs="http://www.linuxhero.com/docs/images/header_r1_c1.gif" width=472 border=0
name=header_r1_c1></A></TD>
<TD colSpan=3>
<form action="http://www.linuxhero.com/docs/search.html" method=get>
<table
style="BORDER-RIGHT: #c4c4c4 1px solid; BORDER-TOP: #c4c4c4 1px solid; BORDER-LEFT: #c4c4c4 1px solid; BORDER-BOTTOM: #c4c4c4 1px solid"
cellspacing=0 cellpadding=3 width="95%" border=0 align="center">
<tbody>
<tr>
<td noWrap background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif">
<div align=center><font class=normalfont>搜索文章:
<input type=hidden value=result name=action2>
<input type=radio checked value=title name=type>标题
<input type=radio value=content name=type>内容
<input type=image src="images/button_go.gif" tppabs="http://www.linuxhero.com/docs/images/button_go.gif" border=0 name=image2>
</font></div>
</td>
</tr>
<tr>
<td noWrap>
<div align="center">
<input maxlength=100 size=30 name=keyword2>
</div>
</td>
</tr></tbody>
</table>
</form>
</TD>
<TD rowSpan=2><IMG src="images/header_r1_c7.gif" tppabs="http://www.linuxhero.com/docs/images/header_r1_c7.gif" width=26 border=0 name=header_r1_c7></TD>
<TD><IMG height=83 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=1 border=0></TD></TR>
<TR>
<TD background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif"><IMG height=22
src="images/header_r2_c1.gif" tppabs="http://www.linuxhero.com/docs/images/header_r2_c1.gif" width=296 border=0
name=header_r2_c1></TD>
<TD background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colSpan=5>
<DIV align=right><FONT class=normalfont>当前位置:
<A href="index.html" tppabs="http://www.linuxhero.com/docs/index.html">本站首页</A>
<font color="#FF6699">>></font>
<A href="type5.html" tppabs="http://www.linuxhero.com/docs/type5.html">安装启动</A> | <A href="copyright.html" tppabs="http://www.linuxhero.com/docs/copyright.html">版权说明</A></font></DIV>
</TD>
<TD><IMG height=22 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=1
border=0></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=10 cellPadding=0 width="100%" bgColor=#ffffff
border=0>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=3 width="100%" border=0>
<TR>
<TD vAlign=top align=middle width="60%">
<TABLE cellSpacing=0 cellPadding=0 width="100%"
background="images/back.gif" tppabs="http://www.linuxhero.com/docs/images/back.gif" border=0>
<TBODY>
<TR>
<TD vAlign=top width="80%">
<DIV align=center>
<FORM action="search.html" tppabs="http://www.linuxhero.com/docs/search.html" method=get>
</FORM>
<TABLE cellSpacing=0 cellPadding=0 width="95%"
border=0><TBODY>
<TR>
<TD background="images/bgi.gif" tppabs="http://www.linuxhero.com/docs/images/bgi.gif"
height=30></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=3 width="95%"
align=center border=0>
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=3 width="100%"
border=0>
<TBODY>
<TR>
<TD vAlign=top>
<p><FONT class=normalfont><B><font color=blue>Linux开机过程的分析(关于bootsect.S)</font></B></FONT><BR><FONT class=smallfont color=#ff9900>2004-04-23 15:18 pm</FONT><BR><FONT class=normalfont>作者:作者<br>来自:Linux知识宝库<br>联系方式:无名<br><br> 本文的目的,在将linux kernel的boot部份做一个介绍,因为笔者觉得很少有这样的
文章来介绍一个操作系统最最开始的一步----把kernel本身载入至内存中,同时进行一些
机器相关(machine dependent)的初始化工作,由于linux刚好使用的是大家最熟悉的386,
486系列PC,所以在说明其程序流程时,也刚好可以对其相关的PC硬体架构做探讨,可以
说是一举两得。不过,我必须假设读者对于汇编语言及PC最基础的架构,如寄存器,分段,
分页,中断服务等有大概的认识。<br>
读者可在linux source code的/boot子目录下找到几个以.S作为副档名的组合语言档,
本文要说明的即是其中的bootsect.S及setup.S两个档案,及尽量简单地说明其所牵涉的
相关硬件部份。<br>
bootsect.S<br>
这个程序是linux kernel的第一个程序,包括了linux自己的bootstrap程序,但是
在说明这个程序前,必须先说明一般IBM PC开机时的动作(此处的开机是指"打开PC的电源"):<br>
一般PC在电源打开时,是由内存中地址FFFF:0000开始执行(这个地址一定在ROMBIOS
中,ROMBIOS一般是在FE000h到FFFFFh中),而此处的内容则是一个jump指令,jump到另
一个位于ROMBIOS中的位置,开始执行一系列的动作,包括了检查RAM,keyboard,显示
器,软硬磁盘等等,这些动作是由系统测试码(system test code)来执行的,随着制作
BIOS厂商的不同而会有些许差异,但都是大同小异,读者可自行观察自家机器开机时,
屏幕上所显示的检查讯息。<br>
紧接着系统测试码之后,控制权会转移给ROM中的启动程序(ROM bootstrap routine),
这个程序会将磁盘上的零道零扇区读入内存中(这就是一般所谓的bootsect,如果你曾
接触过电脑病毒,就大概听过它的大名),至于被读到内存的哪里呢?----绝对位置07C0
:0000(即07C00h处),这是IBM系列PC的特性。而位在linux开机磁盘的bootsect上的正
是linux的bootsect程序,也就是说,bootsect是第一个被读入内存中并执行的程序。
现在,我们可以开始来看看到底bootsect做了什么。<br>
第一步<br>
首先,bootsect将它"自己"从被ROMBIOS载入的绝对地址0x7C00处搬到0x90000处,
然后利用一个jmpi(jumpindirectly)的指令,跳到新位置的jmpi的下一行去执行,关键
的汇编代码如下:<br>
.<br>
(搬移bootsect本身)<br>
.<br>
.<br>
jmpi go,INITSEG<br>
go:<br>
.<br>
.<br>
.<br>
表示将跳到CS为0x9000,IP为offset"go"的位置(CS:IP=0x9000:offsetgo),其中
INITSEG=0x9000定义于程序开头的部分,而go这个label则恰好是下一行指令所在的位置。<br>
第二步<br>
接着,将其它segment registers包括DS,ES,SS都指向0x9000这个位置,与CS看齐。
另外将SP及DX指向一任意位移地址(offset),这个地址等一下会用来存放磁盘参数表
(disk parameter table)。<br>
提到磁盘参数表,就必须提到BIOS中断1Eh。先简单地介绍一下BIOS的中断服务:
80x86将内存最低的256*4bytes保留给256个中断向量(每个interrupt vector大小为4bytes,
所以一共有256*4=1024bytes),而其中的第1Eh个向量指向"磁盘参数表",这个表会告诉
电脑如何去读取磁盘机,而我们所要做的事是搬移磁盘参数表到刚才所设定的任意地址。<br>
接着,改变搬移来的参数表的参数,以符合我们的需要。再将中断向量1Eh指向我们
所修改过的磁盘参数表,然后呼叫BIOSinterrupt的int13h(function0,即AH=0)重置磁
盘控制卡及磁盘驱动器,之后磁盘机就会照我们的意思动作了。如果你曾trace过DOS的
kernel,你会发现,上述的动作在DOS中也有类似的对应流程。<br>
现在让我们来看看关键的程序码:.<br>
.<br>
.<br>
push #0<br>
pop fs<br>
mov bx,#0x78<br>
.<br>
(使GS:SI=FS:BX,指向磁盘参数表,<br>
再将GS:SI所指地址的内容搬移6个<br>
word至ES:DI所指的地址)<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -