引导程序再认识

引导程序再认识

刘建文(http://arttech.us

引导程序

在32位以上的现代计算机里,bootloader,中文叫【引导程序】,是一支将计算机从【开机重置态】过渡到【逻辑虚拟态】的程序。这个引导过程叫Booting。所谓【开机重置态】指机器开机时最基本的功能状态,它是机器可用的基本保证,由厂商定义和提供实现。【开机重置态】可以理解为物理态,而【逻辑虚拟态】则是逻辑态,逻辑态有一些的标准,比如32位处理器的保护模式。计算机的逻辑态是现代计算机的客观需要,比如桌面GUI应用、多进程、互联网等,但由于成本、硬件局限等原因,目前的计算机还不能设计成开机就达到逻辑态的能力要求。bootloader在一段时间内仍在计算机应用中担当重要角色。

开机重置态

【开机重置态】的定义依赖了处理器的体系。虽然各体系会有差导,但Booting过程有一些共同点,例如引导的起点,引导的最小代码和最小数据,这些数据和代码是开机或重置时立即可用的。至于引导起点位于何处,最小代码和最小数据用途则根据体系设计而不同。比如很大一部分的机器的最小引导代码是用来引导功能更强的引导程序,而不是直接过渡到逻辑态。

以下是【处理器体系结构】对boot suite 的影响的例子

To illustrate the effects of the processor family on the boot suite, consider the following:

A bootloader for a device designed around the StrongARM processor has to know whether it's booting the system or waking it up from sleep, because the processor starts execution from the top of its address space (the bootloader) in both cases. The bootloader has to pass control to the kernel code that restores the system state if it's waking up from sleep or load the kernel from the boot device if the system is starting from reset.

An x86 bootloader might need to switch to protected mode to load a kernel bigger than the 1MB real-mode limit.

Embedded systems not based on x86 platforms cannot avail the services of a legacy BIOS. So, if you want your embedded device to boot, for example, from an external USB device, you have to build USB capabilities into your bootloader.

Even when two platforms are based on similar processor cores, the bootloader architecture may differ based on the SoC. For example, consider two ARM-based devices, the Compaq iPAQ H3900 PDA and the Darwin Jukebox. The former is built around the Intel PXA250 controller chip, which has an XScale CPU based on an ARMv5 core, and the latter is designed using the Cirrus Logic EP7312 controller that uses an ARMv3 core. Whereas XScale supports JTAG (named after the Joint Test Action Group, which developed this hardware-assisted debugging standard) to load a bootloader onto flash, the EP7312 has a bootstrap mode to accomplish the same task.

逻辑虚拟态

逻辑态的定义更多的依赖操作系统。另外,相对物理态,逻辑态需要代码量和状态量会更大,比如整个操作系统内核都放置入RAM内待命。

bootloader功能职责

bootloader 的主要功能职责上面已经概述过。bootloader主要职责的具体内容可以理解为对系统各个部件进行初始化。这个过程一般不是一蹴而就的,一般都分为若干阶段,例如在PC里,有BIOS和NTLDR或GRUB两段。前一阶段的计算职责是对后一阶段计算搭建环境,而不是直接完成整个bootloader的主要职责。

bootloader主要职责以外还会负责一些特殊的任务,比如硬件诊断。这类特殊任务在系统论称为【元运算】,它保证计算机可用性,而与之相对的,bootloader各阶段内的的任务称为【体运算】。

bootloader 的一些基本职责和特定功能职责

At the minimum, a bootloader is responsible for processor- and board-specific initializations, loading a kernel and an optional initial ramdisk into memory and passing control to the kernel. In addition, a bootloader might be in charge of providing BIOS services, performing POST, supporting firmware downloads to the target, and passing memory layout and configuration information to the kernel.

On embedded devices that use encrypted firmware images for security reasons, bootloaders may have the task of decrypting firmware. Some bootloaders support a debug monitor to load and debug stand-alone code on to the target device. You may also decide to build a failure-recovery mechanism into your bootloader to recoup from kernel corruption on the field.

booting vs. bootstrap

我们要严格区分两个过程概念——引导(booting)与自举(bootstrap),这两个概念在很多场合是等同的,但在这里,bootstrap是指处于【开发阶段】的引导过程。在PC里,这个过程的例子是升级BIOS,在嵌入式系统是安装系统bootloader。

下面我们以使用【Cirrus Logic EP7211】控制器核心的嵌入系统为例子理解一下bootstrap。

EP7211被设计为两种工作模式——自举模式(bootstrap mode)和普通操作模式(normal operation mode)。在自举模式下,EP7211会读取它内置的128字节记忆体里的最小引导代码。这部分最小代码通过串口下载【下一阶段的引导代码镜像】——称为bootstraper,到容量为2K的板载RAM内,然后跳转到那里,由bootstraper进一步将bootloader下载入闪存。 因此,EP7211的bootstrap必须设计成三阶段的体系结构,每一阶段都位于不同的地址上:

  • 第一阶段是处理器的固件部分,大小为128-byte;
  • 第二阶段是【能读写闪存的】bootstaper,位于RAM内,大小可达2KB;
  • 第三阶段是功能更强的真正bootloader,位于系统闪存内。

可以说,bootstrap 是一次性的。尔后开机时,位于闪存内的bootloader会在处理器工作在普通操作模式下第一时间控制着机器。

为什么分三段?

其实完全可以只做成两段,由处理器内置功能直接下载bootloader镜像到闪存中的,但那样设计会增加处理器开发成本,也降低处理器本身的灵活性。

很多嵌入式处理器并不支持【自举操作模式】,它们是通过一个称为JTAG接口把bootloader写入闪存的。也有一些嵌入式处理既支持自举操作模式,也支持JTAG,例如基于AMR9核心的Freescale i.MX21 。

当bootloader 被安装入闪存后,它不只可引导系统,还可带有升级自身和其它固件组件(内核和根文件系统)的功能,bootloader可以通过UART、USB或Ethernet与开发主机进行通信来执行这些操作。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值