当前位置: 华文世界 > 教育

理解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 实际上是嵌入在硬件中的一个小型操作系统,在计算机启动后就存在。