在“引导顺序”期间,BIOS将尝试确定一个“引导设备”(例如软盘、硬盘、CD、USB闪存设备或网络)。我们的操作系统最初将从硬盘引导(但是将来可以从CD或USB闪存设备引导它)。如果引导扇区在偏移量511和512处分别包含有效的签名字节' 0x55 '和' 0xAA '(称为主引导记录的魔法字节,也称为MBR),则认为设备是可引导的。此签名(以二进制)表示为0b1010101001010101。交替位模式被认为是对某些故障(驱动或控制器)的保护。如果该模式被打乱或0x00,则认为该设备不可引导。
BIOS physically searches for a boot device by loading the first 512 bytes from the bootsector of each device into physical memory, starting at the address 0x7C00 (1 KiB below the 32 KiB mark). When the valid signature bytes are detected, BIOS transfers control to the 0x7C00 memory address (via a jump instruction) in order to execute the bootsector code.
BIOS从地址“0x7C00”(低于32 KiB标记的1 KiB)开始,通过将每个设备的引导扇区中的前512字节加载到物理内存中来物理搜索引导设备。当检测到有效的签名字节时,BIOS将控制传输到“0x7C00”内存地址(通过跳转指令),以便执行引导扇区代码。
Throughout this process the CPU has been running in 16-bit Real Mode, which is the default state for x86 CPUs in order to maintain backwards compatibility. To execute the 32-bit instructions within our kernel, a bootloader is required to switch the CPU into Protected Mode.
在整个过程中,CPU一直以16位实模式运行,这是x86 CPU为了保持向后兼容性的默认状态。要在内核中执行32位指令,需要一个引导加载程序将CPU切换到保护模式。
What is GRUB? 什么是GRUB?GNU GRUB (short for GNU GRand Unified Bootloader) is a boot loader package from the GNU Project. GRUB is the reference implementation of the Free Software Foundation's Multiboot Specification, which provides a user the choice to boot one of multiple operating systems installed on a computer or select a specific kernel configuration available on a particular operating system's partitions.
GNU GRUB (GNU GRand Unified Bootloader的简称)是GNU项目中的一个引导加载程序包。GRUB是自由软件基金会(Free Software Foundation)的多引导规范的参考实现,该规范为用户提供了从安装在计算机上的多个操作系统中引导一个操作系统或选择特定操作系统分区上可用的特定内核配置的选项。
To make it simple, GRUB is the first thing booted by the machine (a boot-loader) and will simplify the loading of our kernel stored on the hard-disk.
简单来说,GRUB是机器(引导加载程序)启动的第一件事,它将简化存储在硬盘上的内核的加载
Why are we using GRUB? 我们为什么要使用GRUB?GRUB is very simple to use
Make it very simple to load 32bits kernels without needs of 16bits code
Multiboot with Linux, Windows and others
Make it easy to load external modules in memory
GRUB非常容易使用
使加载32位内核非常简单,不需要16位代码
多引导与Linux, Windows和其他
便于在内存中加载外部模块
How to use GRUB? 如何使用GRUB?GRUB uses the Multiboot specification, the executable binary should be 32bits and must contain a special header (multiboot header) in its 8192 first bytes. Our kernel will be a ELF executable file ("Executable and Linkable Format", a common standard file format for executables in most UNIX system).
GRUB使用多引导规范,可执行二进制文件应该是32位的,并且必须包含一个特殊的头(多引导头),头8192个字节。我们的内核将是ELF可执行文件(“可执行和可链接格式”,大多数UNIX系统中可执行文件的通用标准文件格式)。
The first boot sequence of our kernel is written in Assembly: start.asm and we use a linker file to define our executable structure: linker.ld.
我们内核的第一个引导序列是使用汇编编写的:start.asm,我们使用一个链接器文件来定义我们的可执行结构:linker.ld。
This boot process also initializes some of our C++ runtime, it will be described in the next chapter.
这个引导过程还初始化了一些c++运行时,将在下一章中进行描述。
Multiboot header structure: