當前位置: 華文世界 > 教育

理解xv6——附錄B1.2 引導載入程式的現狀

2024-01-25教育

The boot loader described in this appendix compiles to around 470 bytes of machine code, depending on the optimizations used when compiling the C code. In order to fit in that small amount of space, the xv6 boot loader makes a major simplifying assumption, that the kernel has been written to the boot disk contiguously starting at sector 1. More commonly, kernels are stored in ordinary file systems, where they may not be contiguous, or are loaded over a network.

These complications require the boot loader to be able to drive a variety of disk and network controllers and understand various file systems and network protocols. In other words, the boot loader itself must be a small operating system.

Since such complicated boot loaders certainly won’t fit in 512 bytes, most PC operating systems use a two-step boot process. First, a simple boot loader like the one in this appendix loads a full-featured boot-loader from a known disk location, often relying on the less space-constrained BIOS for disk access rather than trying to drive the disk itself. Then the full loader, relieved of the 512-byte limit, can implement the complexity needed to locate, load, and execute the desired kernel. Modern PCs avoid many of the above complexities, because they support the Unified Extensible Firmware Interface (UEFI), which allows the PC to read a larger boot loader from the disk (and start it in protected and 32-bit mode).

本附錄中描述的引導載入程式編譯後的機械碼約為 470 字節,具體取決於編譯 C 程式碼時使用的最佳化。為了適應如此小的空間,xv6 引導載入程式做出了一個重要的簡化假設,即內核已連續寫入引導磁盤的第 1 磁區。更常見的情況是,內核儲存在普通檔案系統中,它們可能不連續,或者透過網絡載入。

這些復雜情況要求引導載入程式能夠驅動各種磁盤和網絡控制器,並了解各種檔案系統和網絡協定。換句話說,引導載入程式本身必須是一個小型作業系統。由於這種復雜的引導載入程式肯定無法適應 512 字節,因此大多數 PC 作業系統采用兩步引導過程。首先,像本附錄中的那樣簡單的引導載入程式從已知的磁盤位置載入功能齊全的引導載入程式,通常依賴於空間限制較小的 BIOS 進行磁盤存取,而不是嘗試驅動磁盤本身。

然後,完整的載入程式,不受 512 字節的限制,可以實作找到、載入和執行所需內核所需的復雜性。現代 PC 避免了上述許多復雜性,因為它們支持統一可延伸韌體介面 (UEFI),這允許 PC 從磁盤讀取更大的引導載入程式(並在受保護和 32 位模式下啟動它)。

This appendix is written as if the only thing that happens between power on and the execution of the boot loader is that the BIOS loads the boot sector. In fact the BIOS does a huge amount of initialization in order to make the complex hardware of a modern computer look like a traditional standard PC. The BIOS is really a small operating system embedded in the hardware, which is present after the computer has booted.

本附錄的編寫方式好像在加電和引導載入程式執行之間唯一發生的事情是 BIOS 載入引導磁區。實際上,BIOS 進行了大量的初始化工作,以使現代電腦的復雜硬件看起來像傳統的標準 PC。BIOS 實際上是嵌入在硬件中的一個小型作業系統,在電腦啟動後就存在。