现在的位置: 首页 > 综合 > 正文

小议计算机存储之五.硬盘引导区与分区表

2013年04月12日 ⁄ 综合 ⁄ 共 4279字 ⁄ 字号 评论关闭

小议计算机存储之五.硬盘引导区与分区表

草木瓜

20080901

一、前言

 硬盘启动读取过程其实与光盘和软盘相似,都需要有一个引导区(Boot Sector),如我们常听
说的MBR( Master Boot Record )又叫主引导记录。引导区是硬盘上最重要的一个数据结构。当
用分区软件创建分区的时候,会自动创建MBR。本文主要介绍引导区(Boot Sector)的逻辑结构。

 <更多相关内容:http://blog.csdn.net/liwei_cmg/archive/2008/03/06/2154833.aspx>
 
二、Boot Sector

 On a Wintel machine the BIOS selects a boot device, then it copies the first sector from the
device (which may be an MBR, VBR or any executable code), to address location 0x7C00.
 0x7C00在《小议计算机存储之四.软盘引导自已的操作系统》一文中已有说明。
 
 Boot Sector (主引导扇区) 是软盘,硬盘或类似存础设备都存在的一个扇区,主要存放引导系统
的一些必要代码,是引导介质的第一个扇区。
 Boot Sector 有两类,一种是 MBR (Master Boot Record) ,另一种是 VBR (Volume Boot Record)。

 A master boot record (MBR) is the first sector of a data storage device that has been
partitioned. The MBR sector may contain code to locate the active partition and invoke its
Volume Boot Record.
 A volume boot record is the first sector of a data storage device that has not been partitioned,
or the first sector of an individual partition on a data storage device that has been partitioned.
It may contain code to load and invoke an operating system (or other standalone program)
installed on that device or within that partition.

 MBR主要包含的分区信息,读取之后,系统转到相应的主分区(设置为活动)的VBR,引导
对应分区下的操作系统代码。
 Boot Sector 最后两个字节必须为0xAA55,称为引导区标志(boot sector signature)。

三、MBR (Master Boot Record)

 MBR structure
 
 Address   Description                 Size
 0000      Code Area                   440   (max 446)
 01B8      Optional Disk signature     4
 01BC      Usually Nulls; 0x0000       2
 01BE      Table of primary partitions
           (Four 16-byte entries, IBM
           Partition Table scheme)     64
 01FE      55h MBR signature;
 01FF      AAh 0xAA55[1]               2 
 
 MBR, total size: 446 + 64 + 2 = 512

 MBR 是512字节引导扇区("LBA 0"),其中包含程序代码和一个小分区表。MBR不在硬盘的分
区内,位置处在第一个分区之前的引导扇区。
 
 <bootloader>
  MBR 前 446 个字节( 0x0000 到 0x01BD ),主引导加载程序,包含可执行代码和错误消息
  文本。

 <partition table>
  MBR 接下来的 64 个字节( 0x01BE 到 0x01FD),记录了磁盘的基本分区信息。主分区表分为
  四个分区项,每项 16 字节,分别记录了每个主分区的信息(因此最多可以有四个主分区)。

   分区表结构
 
    Offset         Description
    0x00 (1 byte)  Status[7] (0x80 = bootable, 0x00 = non-bootable, other = malformed[8])
    0x01 (3 bytes) Cylinder-head-sector address of the first sector in the partition[9]
    0x04 (1 byte)  Partition type[10]
    0x05 (3 bytes) Cylinder-head-sector address of the last sector in the partition[11]
    0x08 (4 bytes) Logical block address of the first sector in the partition
    0x0C (4 bytes) Length of the partition, in sectors
 
    BYTE State     :   分区状态 0x00未激活,0x80激活
    BYTE StartHead :   分区起始磁头号
    WORD StartSC   :   分区起始扇区和柱面号,低字节的低6位为扇区号。
               高2位为柱面号的第9,10位,高字节为柱面号的低8位
    BYTE Type      :   分区类型, 如 0x00=Empty, 0x0B = FAT32, 0x07 = NTFS, 0x82 = Linux Swap, 0x83 Linux native partition ...
    BYTE EndHead   :   分区结束磁头号
    WORD EndSC     :   分区结束扇区和柱面号, 定义同前
    DWORD Relative :   在线性寻址方式下的分区相对扇区地址
    DWORD Sectors  :   分区大小 (总扇区数)
   
    外部链结:0x04 (1 byte)  Partition type[10]  http://www.win.tue.nl/~aeb/partitions/partition_types-1.html

四、VBR (Volume Boot Record)

 系统加电启动后,BIOS引导后执行MBR中的代码,由于x86系列的CPU启动时运行在Real
Mode下,MBR里面的执行代码就是Real Mode的机器指令。MBR执行时,会查找活动分区(
主分区记录的State状态),进而加载这个活动分区的VBR。

 Real Mode:
 To this day, even the newest x86 CPUs start in real mode at power-on, and can run software
written for any previous chip.
 Almost all modern x86 operating systems (FreeBSD, Linux, OS/2, Solaris, Windows 95 and
later, etc.) switch the CPU into protected mode at startup.

 VBR 一般是包含启动操作系统的执行命令。在未分区设备位于设备第一个扇区,而在已分
区情况下则位于这个分区的第一个扇区。

五、扩展分区与 EBR (Extended Boot Record)

 由于(MBR)主分区表中只能存放四个分区,无法满足实际需求。因此,设计了一种扩展分区
格式。扩展分区基本信息存放在MBR的分区表中,一个扩展分区可以划分为多个子分区。子分
区的信息是以链表形式存放的。子分区就是我们经常看到的D: E: 等的硬盘分区。
 主分区表中有一个基本扩展分区项,所有扩展子分区都隶属于它。也就是说其他所有扩展子
分区的空间都必须包括在这个基本扩展分区中。对于DOS / Windows 来说,扩展分区的类型有
0x05 0x0f,而 LINUX 是 0x85。
 扩展子分区以链表的形式级联存放,后一个扩展子分区的数据项记录在前一个扩展子分区的
分区表中,但两个扩展子分区的空间并不重叠。
 扩展分区可以看作是一个完整的硬盘,必须进一步分区才可以使用。
 
 EBR 包含了扩展子分区的相关信息,扩展分区可以含有多个 EBR,对应多个扩展子分区(可以
理解为逻辑驱动器)。第一个 EBR 也就是第一个子分区,自然位于扩展分区的第一个扇区。
 
 Common Structure of Extended Boot Records:
 
 Offsets ( within EBR sectors )  Contents      Size in bytes
  000 - 1BD                       Generally unused; normally filled with zero-bytes 446
  18A - 192                       Possible IBM Boot Manager menu entry
  1BE - 1CD                       Partition Table's First entry                     16
  1CE - 1DD                       Partition Table's Second entry                    16
  1DE - 1FD                       Unused, but should be filled with zero-bytes      32
  1FE                             Boot Record signature: 0xAA55 (AA55h)             2
  1FF
  EBR, total size: 446 + 16 +16 +32 + 2 = 512

  分区表第一个项目(Partition Table's First entry)记录这个扩展子分区的基本信息。
  分区表第二个项目(Partition Table's Second entry)记录了这个扩展子分区的下一个子分区的信息。
 
  分区表项目(Partition Table's entry)数据结构与上面提到的MBR是一致的。
 

参考资料: wikipedia

抱歉!评论已关闭.