Linux 为什么我们在嵌入式设备中需要引导加载程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15548004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:29:59  来源:igfitidea点击:

Why do we need a bootloader in an embedded device?

linuxembeddedarmbootloader

提问by chainz

I'm working with ELinux kernel on ARM cortex-A8.

我正在 ARM cortex-A8 上使用 ELinux 内核。

I know how the bootloader works and what job it's doing. But i've got a question - why do we need bootloader, why was the bootloader born?

我知道引导加载程序如何工作以及它在做什么。但我有一个问题 - 为什么我们需要引导加载程序,为什么引导加载程序诞生

Why we can't directly load the kernel into RAM from flash memory without bootloader? If we load it what will happen? In fact, processor will not support it, but why are we following the procedure?

为什么我们不能在没有引导加载程序的情况下直接将内核从闪存加载到 RAM 中?如果我们加载它会发生什么?其实处理器是不会支持的,但是我们为什么要按照程序来呢?

采纳答案by Bartek Banachewicz

A boot loader is a computer program that loads the main operating system or runtime environment for the computer after completion of the self-tests.

引导加载程序是一种计算机程序,它在完成自检后为计算机加载主操作系统或运行时环境。

^ From Wikipedia Article

^来自维基百科文章

So basically bootloader is doing just what you wanted - copying data from flash into operating memory. It's really that simple.

所以基本上引导加载程序正在做你想要的 - 将数据从闪存复制到操作内存中。真的就是这么简单。

If you want to know more about boostrapping the OS, I highly recommend you read the linked article. Boot phase consists, apart from tests, also of checking peripherals and some other things. Skipping them makes sense only on very simple embedded devices, and that's why their bootloaders are even simpler:

如果您想了解有关 boostrapping 操作系统的更多信息,我强烈建议您阅读链接的文章。除了测试之外,引导阶段还包括检查外围设备和其他一些事情。只有在非常简单的嵌入式设备上跳过它们才有意义,这就是为什么它们的引导加载程序更简单的原因:

Some embedded systems do not require a noticeable boot sequence to begin functioning and when turned on may simply run operational programs that are stored in ROM.

一些嵌入式系统不需要明显的启动顺序就可以开始运行,并且在打开时可以简单地运行存储在 ROM 中的操作程序。

The same source

同一个来源

回答by Felipe Lavratti

The primary bootloader is usually built in into the silicon and performs the load of the first USER code that will be run in the system.

主引导加载程序通常内置于芯片中,并执行将在系统中运行的第一个用户代码的加载。

The bootloader exists because there is no standardized protocol for loading the first code, since it is chip dependent. Sometimes the code can be loaded through a serial port, a flash memory, or even a hard drive. It is bootloader function to locate it.

引导加载程序的存在是因为没有用于加载第一个代码的标准化协议,因为它依赖于芯片。有时可以通过串行端口、闪存甚至硬盘驱动器加载代码。它是引导加载程序功能来定位它。

Once the user code is loaded and running, the bootloader is no longer used and the correctness of the system execution is user responsibility.

一旦用户代码被加载并运行,引导加载程序就不再使用,系统执行的正确性是用户的责任。

In the embedded linux chain, the primary bootloader will setup and run the Uboot. Then Uboot will find the linux kernel and load it.

在嵌入式 linux 链中,主引导加载程序将设置并运行 Uboot。然后Uboot会找到linux内核并加载它。

回答by artless noise

In the context of Linux, the boot loader is responsible for some predefined tasks. As this question is armtagged, I think that ARM bootingmight be a useful resource. Specifically, the boot loader was/is responsible for setting up an ATAGlist that describing the amount of RAM, a kernel command line, and other parameters. One of the most important parameters is the machine type. With device trees, an entire description of the board is passed. This makes a stock ARM Linux impossible to boot with out some code to setup the parameters as described.

在 Linux 环境中,引导加载程序负责一些预定义的任务。由于这个问题是arm标记的,我认为ARM 启动可能是一个有用的资源。具体来说,引导加载程序负责设置一个ATAG描述 RAM 量、内核命令行和其他参数的列表。最重要的参数之一是机器类型。使用设备树,可以传递电路板的完整描述。这使得在没有一些代码来设置所述参数的情况下,无法启动现有的 ARM Linux。

The parameters allows one genericLinux to support multiple devices. For instance, an ARM Debian kernel can support hundreds of different board types. Ubootor other boot loader can dynamically determine this information or it can be hard coded for the board.

这些参数允许一个通用Linux 支持多个设备。例如,ARM Debian 内核可以支持数百种不同的主板类型。 Uboot或其他引导加载程序可以动态确定此信息,也可以为板进行硬编码。

You might also like to look at bootloader infopage here at stack overflow.

您可能还想在堆栈溢出时查看引导加载程序信息页面。

A basic system might be able to setup ATAGSand copy NOR flash to SRAM. However, it is usually a little more complex than this. Linux needs RAM setup, so you may have to initialize an SDRAM controller. If you use NAND flash, you have to handle bad blocksand the copymay be a little more complex than memcpy().

基本系统可能能够设置ATAGSNOR 闪存并将其复制到 SRAM。然而,它通常比这更复杂一些。Linux 需要 RAM 设置,因此您可能需要初始化 SDRAM 控制器。如果使用 NAND 闪存,则必须处理坏块,并且复制可能比memcpy().

Linux often has some latent driver bugs where a driver will assume that a clock is initialized. For instance if Ubootalways initializes an Ethernet clock for a particular machine, the Linux Ethernet driver may have neglected to setup this clock. This can be especially true with clock trees.

Linux 通常有一些潜在的驱动程序错误,驱动程序会假设时钟已初始化。例如,如果Uboot总是为特定机器初始化以太网时钟,Linux 以太网驱动程序可能忽略了设置这个时钟。对于时钟树来说尤其如此。

Some systems require boot image formats that are not supported by Linux; for example a special header which can initialize hardware immediately; like configuring the devicesto read initial code from. Additionally, often there is hardware that should be configured immediately; a boot loadercan do this quickly whereas the normal structure of Linux may delay this significantly resulting in I/O conflicts, etc.

某些系统需要 Linux 不支持的引导映像格式;例如可以立即初始化硬件的特殊标头;就像配置devices读取初始代码一样。此外,通常有一些硬件应该立即配置;一个引导装载程序可以很快做到这一点,而Linux的正常结构可能会延缓该显著导致I / O冲突,等等。

From a pragmatic perspective, it is simpler to use a boot loader. However, there is nothing to prevent you from altering Linux's source to boot directly from it; although it maybe like pasting the boot loadercode directly to the start of Linux.

从实用的角度来看,使用引导加载程序更简单。但是,没有什么可以阻止您更改 Linux 的源代码以直接从它启动。虽然它可能就像将引导加载程序代码直接粘贴到 Linux 的开头一样。

See Also: Coreboot, Uboot, and Wikipedia's comparison. Bareboxis a lesser known, but well structured and modern boot loader for the ARM. RedBootis also used in some ARM systems; RedBoot partitions are supported in the kernel tree.

另请参阅:CorebootUboot维基百科的比较Barebox是一个鲜为人知但结构良好且现代的 ARM 引导加载程序。 RedBoot也用于一些 ARM 系统;内核树支持 RedBoot 分区。

回答by Rubal

The kernel requires the hardware on which you are working to be in a particular state. All the hardware you used needs to be checked for its state and initialized for its further operation. This is one of the main reasons to use a boot loader in an embedded (or any other environment), apart from its use to load a kernel image into the RAM.
When you turn on a system, the RAM is also not in a useful state (fully initialized to use) for us to load kernel into it. Therefore, we cannot load a kernel directly (to answer your question)and thus arises the need for a construct to initialize it.

内核要求您工作的硬件处于特定状态。您使用的所有硬件都需要检查其状态并对其进行初始化以供进一步操作。除了用于将内核映像加载到 RAM 中之外,这是在嵌入式(或任何其他环境)中使用引导加载程序的主要原因之一。
当您打开系统时,RAM 也没有处于可用状态(完全初始化以供我们使用)以供我们将内核加载到其中。因此,我们不能直接加载内核(以回答您的问题),因此需要一个构造来初始化它。

回答by kch_PE_MSEE_BSCE

Why we can't directly load the kernel into RAM from flash memory without bootloader? If we load it what will happen? In fact, processor will not support it, but why are we following the procedure?

为什么我们不能在没有引导加载程序的情况下直接将内核从闪存加载到 RAM 中?如果我们加载它会发生什么?其实处理器是不会支持的,但是我们为什么要按照程序来呢?

Bartek, Artless, and Felipe all give parts of the picture.

Bartek、Artless 和 Felipe 都给出了图片的一部分。

Every embedded processor type (E.G. 386EX, Coretex-A53, EM5200) will do somethingautomatically when it is reset or powered on. Sometimes that somethingis different depending on whether the power is cycled or the device is reset. Some embedded processors allow you to change that somethingbased on voltages applied to different pins when the device is powered or reset.

每种嵌入式处理器类型(EG 386EX、Coretex-A53、EM5200)在复位或开机时都会自动执行某些操作。有时,一些依赖于电源是否循环或设备复位是不同的。某些嵌入式处理器允许您根据设备通电或复位时施加到不同引脚的电压来更改某些内容

Regardless, there is a limited amount of somethingthat a processor can do, because of the physical space on-processor required to define that something, whether it is on-chip FLASH, instruction micro-code, or some other mechanism.

无论如何,处理器可以做的事情是有限的,因为定义这些东西需要处理器上的物理空间,无论是片上闪存、指令微代码还是其他一些机制。

This limit means that the somethingis

这个限制意味着某物

  • fixed purpose, does one thing as quickly as possible.
  • limited in scope and capability, typically loading a small block of code (often a few kilobytes or less) into a fixed memory location and executing from the start of the loaded code.
  • unmodifiable.
  • 固定目的,尽快做一件事。
  • 范围和能力有限,通常将一小块代码(通常是几千字节或更少)加载到固定的内存位置并从加载的代码的开头执行。
  • 不可修改。

So what a processor does in response to reset or power-cycle cannot be changed, and cannot do very much, and we don't want it to automatically copy hundreds of megabytes or gigabytes into memory which may not exist or may not be initialized, and which could take a looooong time.

所以处理器响应复位或电源循环所做的事情不能改变,也不能做很多事情,我们不希望它自动将数百兆字节或千兆字节复制到可能不存在或可能未初始化的内存中,这可能需要很长时间。

So....

所以....

We set up a small program which is smaller than the smallest size permitted across all of the devices we are going to use. That program is stored wherever the somethingneeds it to be.

我们设置了一个小程序,它小于我们将要使用的所有设备所允许的最小尺寸。该程序被存储在哪里的东西需要它的人。

Sometimes the small program is U-Boot. Sometimes even U-Boot is too big for initial load, so the small program then in turn loads U-Boot.

有时小程序就是U-Boot。有时甚至 U-Boot 对于初始加载来说也太大了,因此小程序会依次加载 U-Boot。

The point is that whatever gets loaded by the something, is modifiableas needed for a particular system. If it is U-Boot, great, if not, it knows where to load the main operating system or where to load U-Boot (or some other bootloader).

关键是无论被something加载,都可以根据特定系统的需要进行修改。如果是 U-Boot,很好,如果不是,它知道在哪里加载主操作系统或在哪里加载 U-Boot(或其他引导加载程序)。

U-Boot (speaking of bootloaders in general) then configures a minimal set of devices, memory, chip settings, etc., to enable the main OS to be loaded and started. The main OS init takes care of any additional configuration or initialization.

U-Boot(一般来说是引导加载程序)然后配置一组最少的设备、内存、芯片设置等,以启用主操作系统加载和启动。主操作系统初始化负责任何额外的配置或初始化。

So the sequence is:

所以顺序是:

  • Processor power-on or reset
  • Somethingloads initial boot code (or U-Boot style embedded bootloader)
  • Initial boot code (may not be needed)
  • U-Boot (or other general embedded bootloader)
  • Linux init
  • 处理器开机或复位
  • 有些东西会加载初始引导代码(或 U-Boot 风格的嵌入式引导加载程序)
  • 初始引导代码(可能不需要)
  • U-Boot(或其他通用嵌入式引导加载程序)
  • Linux初始化

回答by Igor Stoppa

Apart from what is stated in all the other answers - which is correct - in some cases the system has to go through different execution modes, take as example TrustZone for secure ARM chips. It is possible to still consider it as sort of HW initialization, but what makes it peculiar is the fact that there are additional limitations (ex: memory available) that make it impractical, if not impossible, to do everything in a single binary, thus multiple stages of bootloader are available.

除了所有其他答案中所述的内容 - 这是正确的 - 在某些情况下,系统必须经历不同的执行模式,以安全 ARM 芯片的 TrustZone 为例。仍然可以将其视为一种硬件初始化,但它的特殊之处在于存在额外的限制(例如:可用内存),这使得在单个二进制文件中完成所有事情是不切实际的,如果不是不可能的话,因此引导加载程序的多个阶段可用。

Furthermore, for security reason, each of them is signed and can perform its job only if it meets the security requirements.

此外,出于安全原因,它们中的每一个都经过签名,并且只有在满足安全要求的情况下才能执行其工作。