全面认识计算机启动过程
【 载自ChinaUnix】
首先让我们来了解一些基本概念。第一个是大家非常熟悉的BIOS(基本输入输出系统),BIOS是直接与硬件打交道的底层
代码,它为操作系统提供了控制硬件设备的基本功能。BIOS包括有系统BIOS(即常说的主板BIOS)、显卡BIOS和其它设备(例
如IDE控制器、SCSI卡或网卡等)的BIOS,其中系统BIOS是本文要讨论的主角,因为计算机的启动过程正是在它的控制下进行
的。BIOS一般被存放在ROM(只读存储芯片)之中,即使在关机或掉电以后,这些代码也不会消失。
第二个基本概念是内存的地址,我们的机器中一般安装有32MB、64MB或128MB内存,这些内存的每一个字节都被赋予了一个
地址,以便CPU访问内存。32MB的地址范围用十六进制数表示就是0~1FFFFFFH,其中0~FFFFFH的低端1MB内存非常特殊,因为
最初的8086处理器能够访问的内存最大只有1MB,这1MB的低端640KB被称为基本内存,而A0000H~BFFFFH要保留给显示卡的显存
使用,C0000H~FFFFFH则被保留给BIOS使用,其中系统BIOS一般占用了最后的64KB或更多一点的空间,显卡BIOS一般在
C0000H~C7FFFH处,IDE控制器的BIOS在C8000H~CBFFFH处。
第一步: 当我们按下电源开关时,电源就开始向主板和其它设备供电,此时电压还不太稳定,主板上的控制芯片组会向CPU
发出并保持一个RESET(重置)信号,让CPU内部自动恢复到初始状态,但CPU在此刻不会马上执行指令。当芯片组检测到电源已
经开始稳定供电了(当然从不稳定到稳定的过程只是一瞬间的事情),它便撤去RESET信号(如果是手工按下计算机面板上的
Reset按钮来重启机器,那么松开该按钮时芯片组就会撤去RESET信号),CPU马上就从地址FFFF0H处开始执行指令,从前面的介
绍可知,这个地址实际上在系统BIOS的地址范围内,无论是Award BIOS还是AMI BIOS,放在这里的只是一条跳转指令,跳到系统
BIOS中真正的启动代码处。
第二步: 系统BIOS的启动代码首先要做的事情就是进行POST(Power-On Self Test,加电后自检),POST的主要任务是检
测系统中一些关键设备是否存在和能否正常工作,例如内存和显卡等设备。由于POST是最早进行的检测过程,此时显卡还没有初
始化,如果系统BIOS在进行POST的过程中发现了一些致命错误,例如没有找到内存或者内存有问题(此时只会检查640K常规内存),
那么系统BIOS就会直接控制喇叭发声来报告错误,声音的长短和次数代表了错误的类型。在正常情况下,POST过程进行得非常快,
我们几乎无法感觉到它的存在,POST结束之后就会调用其它代码来进行更完整的硬件检测。
第三步: 接下来系统BIOS将查找显卡的BIOS,前面说过,存放显卡BIOS的ROM芯片的起始地址通常设在C0000H处,系统BIOS
在这个地方找到显卡BIOS之后就调用它的初始化代码,由显卡BIOS来初始化显卡,此时多数显卡都会在屏幕上显示出一些初始化
信息,介绍生产厂商、图形芯片类型等内容,不过这个画面几乎是一闪而过。系统BIOS接着会查找其它设备的BIOS程序,找到之
后同样要调用这些BIOS内部的初始化代码来初始化相关的设备。
第四步: 查找完所有其它设备的BIOS之后,系统BIOS将显示出它自己的启动画面,其中包括有系统BIOS的类型、序列号和版
本号等内容。
第五步: 接着系统BIOS将检测和显示CPU的类型和工作频率,然后开始测试所有的RAM,并同时在屏幕上显示内存测试的进度,
我们可以在CMOS设置中自行决定使用简单耗时少或者详细耗时多的测试方式。
第六步: 内存测试通过之后,系统BIOS将开始检测系统中安装的一些标准硬件设备,包括硬盘、CD-ROM、串口、并口、软驱
等设备,另外绝大多数较新版本的系统BIOS在这一过程中还要自动检测和设置内存的定时参数、硬盘参数和访问模式等。
第七步: 标准设备检测完毕后,系统BIOS内部的支持即插即用的代码将开始检测和配置系统中安装的即插即用设备,每找到
一个设备之后,系统BIOS都会在屏幕上显示出设备的名称和型号等信息,同时为该设备分配中断、DMA通道和I/O端口等资源。
第八步: 到这一步为止,所有硬件都已经检测配置完毕了,多数系统BIOS会重新清屏并在屏幕上方显示出一个表格,其中概
略地列出了系统中安装的各种标准硬件设备,以及它们使用的资源和一些相关工作参数。
第九步: 接下来系统BIOS将更新ESCD(Extended System Configuration Data,扩展系统配置数据)。ESCD是系统BIOS用来
与操作系统交换硬件配置信息的一种手段,这些数据被存放在CMOS(一小块特殊的RAM,由主板上的电池来供电)之中。通常ESCD
数据只在系统硬件配置发生改变后才会更新,所以不是每次启动机器时我们都能够看到“Update ESCD… Success”这样的信息,
不过,某些主板的系统BIOS在保存ESCD数据时使用了与Windows 9x不相同的数据格式,于是Windows 9x在它自己的启动过程中会把
ESCD数据修改成自己的格式,但在下一次启动机器时,即使硬件配置没有发生改变,系统BIOS也会把ESCD的数据格式改回来,如此
循环,将会导致在每次启动机器时,系统BIOS都要更新一遍ESCD,这就是为什么有些机器在每次启动时都会显示出相关信息的原因。
第十步: ESCD更新完毕后,系统BIOS的启动代码将进行它的最后一项工作,即根据用户指定的启动顺序从软盘、硬盘或光驱
启动。以从C盘启动为例,系统BIOS将读取并执行硬盘上的主引导记录,主引导记录接着从分区表中找到第一个活动分区,然后读
取并执行这个活动分区的分区引导记录,而分区引导记录将负责读取并执行IO.SYS,这是DOS和Windows 9x最基本的系统文件。
Windows 9x的IO.SYS首先要初始化一些重要的系统数据,然后就显示出我们熟悉的蓝天白云,在这幅画面之下,Windows将继续进
行DOS部分和GUI(图形用户界面)部分的引导和初始化工作。
如果系统之中安装有引导多种操作系统的工具软件,通常主引导记录将被替换成该软件的引导代码,这些代码将允许用户选择
一种操作系统,然后读取并执行该操作系统的基本引导代码(DOS和Windows的基本引导代码就是分区引导记录)。
上面介绍的便是计算机在打开电源开关(或按Reset键)进行冷启动时所要完成的各种初始化工作,如果我们在DOS下按
Ctrl+Alt+Del组合键(或从Windows中选择重新启动计算机)来进行热启动,那么POST过程将被跳过去,直接从第三步开始,另
外第五步的检测CPU和内存测试也不会再进行。我们可以看到,无论是冷启动还是热启动,系统BIOS都一次又一次地重复进行着这
些我们平时并不太注意的事情,然而正是这些单调的硬件检测步骤为我们能够正常使用电脑提供了基础。
Comprehensive Understanding of the Computer Boot Process
First, let's understand some basic concepts. The first one is the very familiar BIOS (Basic Input/Output System). BIOS is the bottom-layer code that directly interacts with hardware, and it provides the basic functions for the operating system to control hardware devices. BIOS includes system BIOS (commonly known as the motherboard BIOS), graphics card BIOS, and BIOS of other devices (such as IDE controllers, SCSI cards, or network cards, etc.). Among them, system BIOS is the protagonist of this discussion because the computer boot process is carried out under its control. BIOS is generally stored in ROM (read-only storage chips), and even when the computer is powered off or loses power, these codes will not disappear.
The second basic concept is the memory address. Our machine generally has 32MB, 64MB, or 128MB of memory installed. Each byte of this memory is assigned an address for the CPU to access the memory. The address range of 32MB in hexadecimal is 0 - 1FFFFFFH. Among them, the low-end 1MB memory from 0 - FFFFFH is very special because the initial 8086 processor can only access a maximum of 1MB of memory. The low-end 640KB of this 1MB is called base memory, while A0000H - BFFFFH is reserved for the video memory of the graphics card, and C0000H - FFFFFH is reserved for BIOS. Among them, system BIOS generally occupies the last 64KB or a little more space, graphics card BIOS is generally at C0000H - C7FFFH, and IDE controller BIOS is at C8000H - CBFFFH.
Step 1: When we press the power switch, the power supply starts to supply power to the motherboard and other devices. At this time, the voltage is not very stable. The control chipset on the motherboard will send and maintain a RESET signal to the CPU, so that the CPU internal automatically restores to the initial state, but the CPU will not execute instructions immediately at this moment. When the chipset detects that the power supply has started to supply power stably (of course, the process from unstable to stable is just a moment), it will remove the RESET signal (if it is restarting the machine by manually pressing the Reset button on the computer panel, then when the button is released, the chipset will remove the RESET signal). The CPU immediately starts to execute instructions from address FFFF0H. From the previous introduction, this address is actually within the address range of system BIOS. Whether it is Award BIOS or AMI BIOS, only a jump instruction is placed here, jumping to the real boot code in system BIOS.
Step 2: The boot code of system BIOS first needs to do POST (Power-On Self Test). The main task of POST is to detect whether some key devices in the system exist and can work normally, such as memory and graphics card, etc. Since POST is the earliest detection process, at this time the graphics card has not been initialized. If system BIOS finds some fatal errors during the POST process, such as not finding memory or having problems with memory (at this time, only 640K conventional memory is checked), then system BIOS will directly control the speaker to make a sound to report the error. The length and number of sounds represent the type of error. Under normal circumstances, the POST process is very fast, and we can hardly feel its existence. After POST ends, other codes will be called to perform more complete hardware detection.
Step 3: Next, system BIOS will look for the BIOS of the graphics card. As mentioned earlier, the starting address of the ROM chip storing graphics card BIOS is usually set at C0000H. After system BIOS finds the graphics card BIOS here, it will call its initialization code, and the graphics card BIOS will initialize the graphics card. At this time, most graphics cards will display some initialization information on the screen, introducing the manufacturer, graphics chip type, etc., but this screen almost flashes by. System BIOS then will look for the BIOS programs of other devices. After finding them, it will also call the initialization codes inside these BIOS to initialize the relevant devices.
Step 4: After finding the BIOS of all other devices, system BIOS will display its own boot screen, which includes the type, serial number, and version number of system BIOS, etc.
Step 5: Then system BIOS will detect and display the type and working frequency of the CPU, and then start testing all RAM and at the same time display the progress of memory testing on the screen. We can decide in the CMOS settings to use a simple and time-consuming test or a detailed and time-consuming test method.
Step 6: After the memory test passes, system BIOS will start to detect some standard hardware devices installed in the system, including hard disks, CD-ROMs, serial ports, parallel ports, floppy drives, etc. In addition, most relatively new versions of system BIOS will also automatically detect and set memory timing parameters, hard disk parameters, and access modes, etc., in this process.
Step 7: After the detection of standard devices is completed, the code in system BIOS that supports plug and play will start to detect and configure the plug and play devices installed in the system. After each device is found, system BIOS will display the name and model of the device on the screen, and at the same time allocate interrupts, DMA channels, and I/O ports, etc., for this device.
Step 8: Up to this step, all hardware has been detected and configured. Most system BIOS will clear the screen again and display a table at the top of the screen, which roughly lists various standard hardware devices installed in the system, as well as the resources they use and some relevant working parameters.
Step 9: Next, system BIOS will update ESCD (Extended System Configuration Data). ESCD is a means for system BIOS to exchange hardware configuration information with the operating system. These data are stored in CMOS (a small piece of special RAM powered by the battery on the motherboard). Usually, ESCD data are only updated when the system hardware configuration changes. So we cannot see the information like "Update ESCD... Success" every time we start the machine. However, the system BIOS of some motherboards uses a different data format from Windows 9x when saving ESCD data. Then Windows 9x will modify the ESCD data into its own format during its own boot process. But when starting the machine next time, even if the hardware configuration has not changed, system BIOS will change the ESCD data format back. In this way, a cycle will occur, which will lead to system BIOS updating ESCD every time the machine is started. This is the reason why some machines display relevant information every time they are started.
Step 10: After ESCD is updated, the boot code of system BIOS will carry out its last task, that is, boot from a floppy disk, hard disk, or CD-ROM according to the boot sequence specified by the user. Taking booting from the C drive as an example, system BIOS will read and execute the master boot record on the hard disk. The master boot record then finds the first active partition from the partition table, and then reads and executes the partition boot record of this active partition. The partition boot record will be responsible for reading and executing IO.SYS, which is the most basic system file of DOS and Windows 9x. IO.SYS of Windows 9x first needs to initialize some important system data, and then display the familiar blue sky and white clouds. Under this screen, Windows will continue to carry out the boot and initialization work of the DOS part and the GUI (Graphical User Interface) part.
If there is a tool software that can boot multiple operating systems installed in the system, usually the master boot record will be replaced with the boot code of this software. These codes will allow the user to select an operating system, and then read and execute the basic boot code of this operating system (the basic boot code of DOS and Windows is the partition boot record).
The above introduction is the various initialization work that the computer needs to complete when turning on the power switch (or pressing the Reset key) for cold boot. If we press the Ctrl + Alt + Del combination key under DOS (or select to restart the computer from Windows) to perform hot boot, then the POST process will be skipped, and it will start directly from step 3. In addition, the detection of the CPU and the memory test in step 5 will not be carried out again. We can see that whether it is cold boot or hot boot, system BIOS repeatedly carries out these things that we usually don't pay much attention to. However, it is these monotonous hardware detection steps that provide the basis for us to use the computer normally.