Monday, April 30, 2007

强烈推荐IBM developerWorks关于LPI认证的教程

虽然是考证用的,但是说得非常系统,比一般网上流传的教程写得有条理,有结构,不但每篇文章条理清晰地有章节,题目,内容,后面的参考资料也非常详尽。

IBM PC的系统引导过程之我见

在阅读了不少资料后(越来越觉得HK的网络爽了!——其实可能只是内地不好而已……我要求不高阿……只要能看到wikipedia和大多数外文技术网站就好了……),终于对IBM PC的引导过程有了一定的了解,这里写一篇综述以备忘。
-------------------------------------------------------
首先介绍一下所谓的“引导”的历史。Bootstrapping一词来源于一个德国传说-_-!!!
一位叫Baron Münchhausen的大哥声称能抓着头发,自己把自己提起来……后来黑客们引用这个词语(后来简称为booting了)来比喻计算机启动过程——这是一个鸡与蛋的问题!究竟是先有鸡还是先有蛋?对,计算机的启动过程也是这样。大家都知道,计算机运行的是程序,而程序是装载在内存中的。计算机必须以操作系统为用户接口运行,计算机刚刚启动时,内存中没有任何操作系统,要把操作系统装入内存,本身就要有一个可以执行这个动作的程序阿……这就造成了paradox.

因此,人们想到,可以利用boot loader这样一种程序来加载OS。N年前,还是打孔机时代时,程序员通过手动输入程序到内存中来启动计算机……现在则好多了,使用boot loader可以完成这些任务。通过多级的引导,boot loader一级一级引导程序,最终引导操作系统。


事实上,操作系统(这里都是以IBM PC为例,不同架构可能不同,比如Apple)引导的过程是这样的。基于IBM PC架构的机器都会有BIOS,一个嵌入在主板中的芯片,使用CMOS存储某些程序与信息以完成开机后的“加电自检”,Power On Self Tesing,POST,作用是检测各功能部件,如内存,显卡等。然后,读入MBR——Master Boot Record。
这是一个512字节的扇区,位于可引导存储介质的第一个扇区。这个扇区结构如下:



根据上表,可以看到,该512字节扇区的内部结构,其中也指定了一个磁盘只能有4个分区!因为只有64个字节分配给分区表,而每个分区表占了16字节,因此只有4个。

对于每个磁盘/软盘,或其他可引导存储介质。都必须有一个主引导扇区,因为BIOS加电自检后自动装载这个扇区。但是如果有很多磁盘或软盘或光驱呢?因此在BIOS里有一个启动有限选项可以选择。BIOS逐个检测这些启动介质,知道检测到合法的启动介质。观察512字节中的01FE和01FF字节,可以发现,这是所谓的MBR签名。固定的AA55H标识了这个是一个合法的引导扇区!这是一个段的最后两个字节,硬件就是靠它们来区别这个段是否为引导扇区。这就是
所谓的“第一阶段引导”。如果没有操作系统怎么办?那么就会显示:Please Insert a bootable disc and press a key"这是引导扇区(boot sector)显示的,而不是机器的固件。

接下来,在4个主分区中,只有一个被标识为“活动分区”。BIOS通过检测4个分区表进入这个活动分区,读入这个活动分区的第一个扇区。此时控制权交给了该段扇区存储的程序。这个扇区也称为“分区引导记录”(partition boot record)。分区引导记录实际上是另一个“第一阶段引导程序”。但是它能够从这个分区装载一组代码,这些新代码称为“第二阶段引导程序”,然后进行“第二阶段的引导”。对于GRUB,它在MBR写入第一阶段引导程序(最多前446个字节),然后通过这个第一阶段引导程序跳入活动分区的第二阶段引导程序。此时,已经没有512字节限制了!这时再由第二阶段引导程序载入操作系统内核。对于DOS等老式OS,一般是自己载入自己,即loader本身就在操作系统里面。而对于windows,linux等,则由GRUB,LILO,NTLDR等经过几个阶段的引导再装入自己(kernel).

在linux中,可以使用
dd if=/dev/hda of=mbr.bin bs=512 count=1
来生成MBR的镜像。

-------------------------------------------------------
参考资料:
  1. Booting词条from wikipedia
  2. MBR词条from wikipedia
  3. From Power Up to Bash Prompt链接
  4. LPI 证书 101 考试准备: Linux 安装与包管理 from IBM developerWorks

[zz]How to Write an Operating System

[zz]引导过程概述

注:以下资料节选自《LPI 证书 101 考试准备: Linux 安装与包管理》from IBM developerWorks。

引导过程概述

在我们研究 LILO 和 GRUB 之前,先看看 PC 是如何启动(即 引导)的。BIOS(表示 Basic Input Output Service, 基本输入输出服务)代码被存储在非易失内存中,比如 ROM、EEPROM 或闪存。当打开或者重新启动 PC 时,执行此代码。它常常执行一次开机自检(power-on self test,POST),对计算机进行检查。最后,它从引导驱动器上的主引导记录装载第一个扇区。

正如前面的小节 “分区” 所提到的,MBR 还包含分区表,所以 MBR 中的可执行代码量少于 512 字节,这非常少。注意,每个硬盘(甚至软盘)都在它的 MBR 中包含可执行代码,即使这些代码只够输出“Non-bootable disk in drive A:”这样的一条消息。BIOS 从第一个扇区装载的代码被称为第一阶段引导装载程序 或者阶段 1 引导装载程序

MS DOS、PC DOS 和 Windows 操作系统使用的标准硬盘驱动器 MBR 会检查分区表,寻找引导驱动器上标为活动 的主分区,从这个分区装载第一个扇区,并且将控制传递到装载的代码的开头。这个新的代码段也称为分区引导记录(partition boot record)。分区引导记录实际上是另一个第一阶段引导装载程序,但是它能够从这个分区装载一组代码块。这些新代码称为第二阶段引导装载程序。在 MS-DOS 和 PC-DOS 中,第二阶段引导装载程序直接装载操作系统的其余部分。这就是操作系统通过引导自举使自己启动的过程。

对于只有一个操作系统的系统,这种过程工作得很不错。如果有多个操作系统(比如 Windows 98、Windows XP 和三个不同的 Linux 发行版),那么应该怎样做?您 可以 使用某些程序(比如 DOS FDISK 程序)改变活动分区并且重新引导。但这太麻烦了。另外,一个硬盘只能有四个主分区,而且标准 MBR 只能引导一个主分区。但是我们假想的例子有五个操作系统,每个都需要一个分区。天啦!

解决方案就是使用某些特殊代码,让用户选择要引导的操作系统。这种代码包括:

  1. Loadlin,一个 DOS 可执行程序,可以从正在运行的 DOS 系统调用它来引导 Linux 分区。在建立多重引导系统还很复杂而且有风险的年代里,这个程序曾经流行过。
  2. OS/2 Boot Manager,一个安装在专用的小分区中的程序。这个分区被标为“活动的”并且标准 MBR 引导过程启动 Boot Manager,这个程序显示一个菜单,让用户选择要引导的操作系统。
  3. 智能引导装载程序,这种程序可以驻留在一个操作系统分区上,可以由活动分区的分区引导记录或者主引导记录调用。这些程序包括:
    • BootMagic™,即 Norton PartitionMagic™ 的组成部分
    • LILO,即 LInux LOader
    • GRUB,即 GRand Unified Boot loader

很明显,如果能够将系统控制传递给一个代码量超过 512 字节的程序,就不难允许从逻辑分区进行引导,或者从不在引导驱动器上的分区进行引导。这些解决方案都提供了这些可能性,因为它们能够从任意分区装载引导记录,或者因为它们了解为了启动引导过程要装载哪些文件。

从 现在开始,我们将主要关注 LILO 和 GRUB,因为在大多数 Linux 发行版中都有这两种引导装载程序。在发行版的安装过程中,可能会让您选择要安装哪一种。这两种引导装载程序都可以用于大多数现代硬盘。硬盘技术发展得很 快,所以应该确保您选择的引导装载程序、Linux 发行版(或其他操作系统)以及系统的 BIOS 可以支持新硬盘。否则就可能导致数据丢失。

LILO 和 GRUB 中使用的第二阶段装载程序允许在几种操作系统或版本之中选择要装载哪一个。但是,LILO 和 GRUB 的显著差异在于,在修改系统(比如升级内核或做其他修改)之后,需要使用一个命令重新创建 LILO 引导设置,而 GRUB 能够通过一个可编辑的配置文本文件来完成设置更新。LILO 的历史比较长,GRUB 比较新。原来的 GRUB 现在成了 GRUB Legacy,GRUB 2 正在 Free Software Foundation 的赞助下进行开发。


CHS-Cylinder,Head,Sector

From Wikipedia, the free encyclopedia

Jump to: navigation, search
Cylinder, head, and sector of a hard drive.

Cylinder-head-sector, also known as CHS, was an early method for giving addresses to each physical block of data on a hard drive. In the case of floppy drives, for which the same exact diskette medium can be truly low-level formatted to different capacities, this is still true.

Though CHS values no longer have a direct physical relationship to the data stored on disks, pseudo CHS values (which can be translated by disk electronics or software) are still being used by many utility programs.

Contents

[hide]

[edit] Definitions

[edit] Platters and Tracks

Every disk drive consists of one or more platters. A platter can be thought of as a collection of concentric, flat rings. Data is stored on the surface of a platter inside these rings, or circular strips; which are called tracks. These tracks, which may exist on either side of a platter, can be delineated by specifying certain cylinder and head values.

[edit] Heads

Data is written to and read from the surface of a platter by a device called a head. Naturally, a platter has 2 sides and thus 2 surfaces on which data can be manipulated; usually there are 2 heads per platter--one on each side. (Sometimes the term side is substituted for head, since platters may be separated from their head assemblies; as is definitely the case with the removable media of a floppy drive.)

[edit] Cylinders

A cylinder comprises all the tracks that can be accessed by the read/write heads while their access arms remain stationary. If drives used only one side of a single platter, the term "track" would always be used instead of "cylinder."

[edit] Sectors

A platter can also be thought of as a collection of slices called sectors.

[edit] Blocks

The intersection of a cylinder and a sector is called a block. These blocks are the smallest geometrical breakdown of a disk, and they represent the smallest amount of data with which a disk can deal (each block usually contains 512 bytes of data).

Note: Many PC engineers and technicians now use the term sector to speak of the smallest amount of data a disk drive can access. The UNIX/Linux communities, however, still employ the term block in the sense described above, for 512-byte chunks of data, or various multiples thereof. For example, the Linux fdisk utility normally displays partition table information using 1024-byte Blocks while also using the term sector in describing the disk's size in the phrase, "63 sectors/track" (though technically, the phrase "63 sectors/cylinder" should be used).

[edit] CHS Addressing

Hence, each block of data can be addressed by specifying a cylinder, head, and sector. The following formulas detail the CHS geometry and addressing scheme.

The number of blocks on one side of a platter is:

   blocksPerPlatterSide = (cylindersPerPlatter)*(SectorsPerPlatter)

The number of blocks per platter is:

   blocksPerPlatter = (blocksPerPlatterSide)*(sidesUsedPerPlatter)

which is usually written in terms of the number of heads used:

   blocksPerPlatter = (blocksPerPlatterSide)*(HeadsPerPlatter)

This is usually expanded to:

   blocksPerPlatter =
(cylindersPerPlatter)*(SectorsPerPlatter)*(HeadsPerPlatter)

and rearranged:

   blocksPerPlatter =
(cylindersPerPlatter)*(HeadsPerPlatter)*(SectorsPerPlatter)

then finally rewritten as:

   blocksPerPlatter = (Cylinders)*(Heads)*(Sectors)

[edit] Examples

A "1.44 MB"[1] floppy disk has 80 cylinders (numbered 0 to 79), 2 heads (numbered 0 to 1) and 18 sectors (numbered 1 to 18). Therefore, its capacity in blocks is computed as follows:

   blocksPerPlatter = (80)*(2)*(18) = 2880

The four 16-byte entries within an MBR's Partition Table limit the values of their CHS tuples to: 1024 cylinders, 255 heads and 63 sectors. For computers whose BIOS code was also limited to using only these CHS values, what was the largest size hard disk they could handle? Starting with the formula above, but also multiplying by 512 bytes/block, the hard disk could be no larger than:

   (1024)*(255)*(63)*(512) = 8,422,686,720 bytes (about 8.4 GB)

[edit] History

Older hard drives, such as MFM and RLL drives, divided each cylinder into an equal number of sectors and the CHS values matched the physical makeup of the drive. A drive with a CHS value of 500 x 4 x 32 would have 500 tracks per side on each platter, two platters, and 32 sectors per cylinder, with a total of 32,768,000 bytes (about 32.8 MB, or 31.25 MiB). Most modern drives have a surplus space that doesn't make a cylinder boundary. Each partition should always start and end at a cylinder boundary. Only some of the most modern operating systems may disregard this rule, but doing so can still cause some compatibility issues, especially if the user wants to boot more than one OS on the same drive.

ATA/IDE drives have replaced the MFM and RLL drives, and are much more efficient at storing data. They use Zone Bit Recording (ZBR), where the number of sectors in a cylinder varies with its location on the drive. Cylinders nearer to the edge of the platter contain more sectors than cylinders close to the spindle, because there is more space in a given track near the edge of the platter. The CHS addressing scheme does not work on these drives because of the varying number of sectors per cylinder. An IDE drive can be set in the system BIOS with any configuration of cylinders, heads, and sectors that do not exceed the capacity of the drive since the drive will convert any given CHS value into an actual address for its specific hardware configuration.

[edit] Notes

  1. ^ This popular label for the usual format of a 3.5-inch floppy diskette is a misnomer, since its actual capacity is 1440 KiB; or: 1440 binaryKilo-bytes x 1024/binaryKilo = (exactly) 1,474,560 bytes (which is neither 1,440,000 bytes nor the equivalent of 1.44 MiB).

[edit] See also

[edit] External links