小議電腦儲存之五.硬碟引導區與分區表

來源:互聯網
上載者:User

小議電腦儲存之五.硬碟引導區與分區表

草木瓜

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.