Booting (also known as booting up) is the initial set of operations that a computer system performs when electrical power is switched on. The process begins when a computer that has been turned off is re-energized, and ends when the computer is ready to perform its normal operations. It typically involves performing power-on self-test, locating and initializing peripheral devices, and then finding, loading and starting an operating system.

The computer term boot is short for bootstrap or bootstrap load and derives from the phrase to pull oneself up by one’s bootstraps. The usage calls attention to the paradox that a computer cannot run without first loading software but some software must run before any software can be loaded.

The order of booting

1. BIOS

Early computers used a variety of ad-hoc methods to get a fragment of software into memory to solve this problem. The invention of integrated circuit Read-only memory (ROM) of various types solved the paradox by allowing computers to be shipped with a start up program that could not be erased, but growth in the size of ROM has allowed ever more elaborate start up procedures to be implemented. Besides, the program in ROM is call BIOS(Basic Input/Output System).

1.1 POST

KEYPOINT

Once the computer’s power is first turned on, it will read BIOS(Basic Input/Output System) from ROM(read-only memory) which will test the computer to see if the fundamental conditions to boot itselt correctly is met.

Elaboration

In order for a computer to successfully boot, its BIOS, operating system and hardware components must all be working properly; failure of any one of these three elements will likely result in a failed boot sequence.

When the computer’s power is first turned on, the CPU initializes itself, which is triggered by a series of clock ticks generated by the system clock. Part of the CPU’s initialization is to look to the system’s ROM BIOS for its first instruction in the startup program. The ROM BIOS stores the first instruction, which is the instruction to run the power-on self test (POST), in a predetermined memory address.

POST begins by checking the BIOS chip and then tests CMOS RAM. If the POST does not detect a battery failure, it then continues to initialize the CPU, checking the inventoried hardware devices (such as the video card), secondary storage devices, such as hard drives and floppy drives, ports and other hardware devices, such as the keyboard and mouse, to ensure they are functioning properly.

Most BIOS chips use a system of beep codes to indicate the POST status to the user and each BIOS chipset uses a different code.

1.2. Boot sequence

Once the POST has determined that all components are functioning properly and the CPU has successfully initialized, the BIOS looks for an OS to load.

The BIOS typically looks to the CMOS chip to tell it where to find the OS, and in most PCs, the OS loads from the C drive on the hard drive even though the BIOS has the capability to load the OS from a floppy disk, CD or ZIP drive.

The order of drives that the CMOS looks to in order to locate the OS is called the boot sequence, which can be changed by altering the CMOS setup

This is what many people including me who installed Multisystem are familiar with.

Notice:

  • Boot sequence governed by BIOS ROM
  • BIOS parameters stored in CMOS
  • BIOS ROM may be password protected

2. MBR

BIOS 按照"启动顺序",把控制权转交给排在第一位的储存设备。

这时,计算机读取该设备的第一个扇区,也就是读取最前面的 512 个字节,即:"主引导记录"(Master boot record,缩写为 MBR)。如果 MBR 的最后两个字节是 0x55 和 0xAA,表明这个设备可以用于启动;如果不是,表明设备不能用于启动,控制权于是被转交给"启动顺序"中的下一个设备。

关于主引导记录的详情,可以参照之前写的 Boot Sector 博文,其中有 MBR 的内容

根据该博文对分区表中第一个字节的认识,我们知道它如果为 0x80,就表示该主分区是激活分区,控制权要转交给这个分区。四个主分区里面只能有一个是激活的。

3. 硬盘启动

这时,计算机的控制权就要从 MBR 转交给硬盘的某个分区了,这里又分成三种情况。

3.1 情况 A:卷引导记录

上一节提到,四个主分区里面,只有一个是激活的。计算机会读取激活分区的第一个扇区,叫做"卷引导记录"(Volume Boot Record,缩写为 VBR),也成为 “分区引导记录”(Partition Bott Record,缩写为 PBR)

"卷引导记录"的主要作用是,告诉计算机,操作系统在这个分区里的位置。然后,计算机就会加载操作系统了。

3.2 情况 B:扩展分区和逻辑分区

注:关于扩展分区的详细内容,也可以包含在之前写的 Boot Sector 博文

随着硬盘越来越大,四个主分区已经不够了,需要更多的分区。但是,分区表只有四项,因此规定有且仅有一个区可以被定义成"扩展分区"(Extended partition)。但是,似乎很少通过这种方式启动操作系统。如果操作系统确实安装在扩展分区,一般采用下一种方式启动。

3.3 情况 C:启动管理器

在这种情况下,计算机读取"主引导记录"前面 446 字节的机器码之后,不再把控制权转交给某一个分区,而是运行事先安装的"启动管理器"(boot loader),由用户选择启动哪一个操作系统。

Linux 环境中,目前最流行的启动管理器是 grub。嗯,相信玩过 Linux 的朋友对 grub 已经不陌生了。如果有时间,自己再去详细了解一下它,并写篇博文笔记

4. 操作系统

控制权转交给操作系统后,操作系统的内核首先被载入内存。

以 Linux 系统为例,先载入/boot 目录下面的 kernel。内核加载成功后,第一个运行的程序是/sbin/init。它根据配置文件(Debian 系统是/etc/initab)产生 init 进程。这是 Linux 启动后的第一个进程,pid 进程编号为 1,其他进程都是它的后代。

然后,init 线程加载系统的各个模块,比如窗口程序和网络程序,直至执行/bin/login 程序,跳出登录界面,等待用户输入用户名和密码。

至此,全部启动过程完成。

Windows 7 操作系统引导过程

1、电脑开机后,开始启动 BIOS,开始 BIOS 自检。

2、通过自检后,bios 找到硬盘上的主引导记录 MBR.

3、MBR 开始读取硬盘分区表 DPT,找到活动分区,找到活动分区中的分区引导记录 PBR,并且把控制权交给 PBR.

4、PBR 搜索活动区中的启动管理器 bootmgr,找到后,PBR 把控制权交给 bootmgr(相当于 xp 里的 ntldr 文件)。

5、Bootmgr 寻找活动分区中的 boot 文件夹中的 BCD 文件(启动配置数据,相当于 xp 里的 boot.ini 文件)。

6、找到 BCD 后,Bootmgr 首先从 BCD 中读 SYS 取启动管理器 bootmgr 菜单的语言版本信息,然后再调用 BOOTMGR 与相应语言的 BOOTMGR.EXE.MUI (在 boot 文件夹对应语言文件夹中)组成相应语言的启动菜单,之后在显示器上显示多操作系统选择画面。

7、如果存在多个操作系统而且系统设置的等待时间不是 0,那么屏幕就显示多个操作系统的选择界面。如果没有多系统,那么直接进入 windows 7 系统,不显示选择界面。

8、选择 windows 7 系统后,bootmgr 就会读取 BCD 里 win7 系统所在的盘里的 windows.exe 文件,并且将控制权交给 winload.exe。

9、Winload.exe 加载 windows7 内核、硬件、服务等,之后加载桌面等信息,从而启动整个 windows 7 系统。

上面几步可以概括一下就是:

BIOS --> MBR --> DPT --> PBR--> Bootmgr --> BCD--> 系统选择界面 ---> 选择 windows7 ------> Winload.exe ---- >内核加载等 --> 启动整个 windows7 系统

参考资料

  1. The Boot Process and Operating Systems
  2. The Booting Process
  3. 计算机是如何启动的?
  4. 主引导扇区(MBR),分区表(DPT)及活动分区(DBR)
  5. 操作系统引导过程