Linux | GRUB / bootloader 详解

注:本文为 “Linux | GRUB / bootloader” 相关文章合辑

英文引文,机翻未校。

图片清晰度限于引文原状。

未整理去重。


What is Grub in Linux? What is it Used for?

Linux 中的 Grub 是什么?它的用途是什么?

Abhishek Prakash
10 Feb 2023

If you ever used a desktop Linux system, you must have seen this screen. This is called the GRUB screen. Yes, it is written in all capital letters.
如果您曾经使用过桌面 Linux 系统,那么您一定看过这个屏幕。这称为 GRUB 屏幕。是的,它全是大写的。

Grub Screen Linux

Remember this screen? This is GRUB

In this chapter of the Linux Jargon Buster series, I’ll tell you what Grub is and what is it used for. I’ll also briefly touch upon the configuration and customization part.
在 Linux Jargon Buster 系列的这一章中,我将告诉您 Grub 是什么以及它的用途。我还将简要介绍配置和自定义部分。

What is GRUB?

什么是 GRUB?

GRUB. is a complete program for loading and managing the boot process. It is the most common bootloader for Linux distributions. A bootloader is the first software that runs when a computer starts. It loads the kernel of the operating system. and then the kernel initializes the rest of the operating system: shell, display manager., desktop environment., etc.
GRUB 是一个用于加载和管理引导过程的完整程序。它是 Linux 发行版中最常见的引导加载程序。引导加载程序是计算机启动时运行的第一个软件。它加载作系统的内核,然后内核初始化作系统的其余部分:shell、显示管理器、桌面环境等。

Boot loader vs boot manager

引导加载程序与引导管理器

I didn’t want to confuse you at this stage but this topic needs to be introduced before going on. There is a blurry line between a bootloader and a boot manager.
我不想在这个阶段让你感到困惑,但在继续之前需要介绍这个话题。引导加载程序和引导管理器之间有一条模糊的界限。

You already know that the bootloader starts first and then loads the kernel into memory and executes it. A boot manager program allows you to choose between operating systems, if there is more than one on your system. A boot manager doesn’t load the OS directly,
您已经知道 bootloader 首先启动,然后将内核加载到内存中并执行它。如果您的系统上有多个作系统,则引导管理器程序允许您在作系统之间进行选择。引导管理器不直接加载作系统,

With Linux kernel version 3.3, the Linux kernel includes a built-in EFI bootloader.. In fact, any operating system that is capable of working the EFI system includes an EFI bootloader.. In EFI capable systems, the firmware reads the EFI system partition (ESP) to locate the EFI files for boot information.
在 Linux 内核版本 3.3 中,Linux 内核包含内置的 EFI 引导加载程序。事实上,任何能够与 EFI 系统一起工作的作系统都包含 EFI 引导加载程序。在支持 EFI 的系统中,固件读取 EFI 系统分区 (ESP) 以查找 EFI 文件以获取引导信息。

Disk Partition Gparted

GRUB is both a bootloader and a boot manager. I’ll come back to GRUB in a moment. First, let’s see other GRUB-like programs.
GRUB 既是引导加载程序,也是引导管理器。我稍后会回到 GRUB。首先,让我们看看其他类似 GRUB 的程序。

📑 Trivia:
 
GRUB is acronym for GRand Unified Bootloader.
 
GRUB 是 GRand Unified Bootloader 的缩写。

What are those lines in Grub screen mean?

Grub screen 中的那些行是什么意思?

You’ll see a few lines on the Grub screen. These lines will be different for different setups, different distributions.
您将在 Grub 屏幕上看到几行。对于不同的设置、不同的发行版,这些行会有所不同。

Usually, the first line is your Linux distribution. If you see something like Advanced option, you can enter into it and find some lines starting with linux-generic-x-y-z etc.
通常,第一行是您的 Linux 发行版。如果你看到类似 高级 选项的内容,你可以进入它并找到一些以 linux-generic-x-y-z 等开头的行。

When your Linux distribution updates the kernel version, it keeps at least one older version. You can choose to boot into the older Linux kernels in case your system encounters any issue with the newly updated Linux kernel.
当您的 Linux 发行版更新内核版本时,它会保留至少一个旧版本。您可以选择启动到较旧的 Linux 内核,以防您的系统遇到新更新的 Linux 内核的任何问题。

You may also see a recovery option in Ubuntu based distribution.
您可能还会在基于 Ubuntu 的分发中看到恢复选项。

Boot into recovery mode

Under the advanced options, there is usually an older kernel version and recovery option

At the last, you may see an entry like System setup or UEFI Settings to access the BIOS settings of your system.
最后,您可能会看到一个条目,例如 系统设置 or UEFI 设置 来访问系统的 BIOS 设置。

What are some other boot managing programs similar to GRUB?

还有哪些其他类似于 GRUB 的引导管理程序?

GRUB is the most popular boot manager for Linux. But it is not the only one. There is the highly customizable rEFInd boot manager. that some Linux users love.
GRUB 是 Linux 上最流行的引导管理器。但这并不是唯一的一个。有一些 Linux 用户喜欢的高度可定制的 rEFInd 引导管理器。

Refind Boot Manager

Customized rEFInd Boot Manager Screen | Image Credit_

There is also systemd-boot. text-based boot manager. You can guess that this is exclusively for systemd-based Linux distributions. Some distributions such as Pop OS, use the systemd-boot.
还有 systemd-boot 基于文本的启动管理器。您可以猜到这仅适用于基于 systemd 的 Linux 发行版。某些发行版(如 Pop OS)使用 systemd-boot。

systemd-Boot in Pop OS

systemd-Boot in Pop OS | Image Credit.

Accessing or editing GRUB

访问或编辑 GRUB

The usual GRUB screen you see is its menu interface. It allows you to choose which operating systems to boot, if there is more than one on your machine. You can also choose to load a different kernel if your Linux distribution has more than one installed.
您看到的通常的 GRUB 屏幕是它的菜单界面。如果您的计算机上有多个作系统,它允许您选择要启动的作系统。如果您的 Linux 发行版安装了多个内核,您还可以选择加载其他内核。

Depending upon the configuration set by the Linux distribution, you may have some other entries on the GRUB menu.
根据 Linux 发行版设置的配置,GRUB 菜单上可能还有一些其他条目。

You can edit a GRUB menu entry by pressing the “e” key. This way, you can change the kernel parameters before loading it. For example, in some cases, disabling the graphics driver from the kernel helps you with Linux system stuck at boot..
您可以通过按 “e” 键来编辑 GRUB 菜单项。这样,您可以在加载内核之前更改内核参数。例如,在某些情况下,从内核禁用图形驱动程序有助于解决 Linux 系统在启动时卡住的问题。

Editing grub to fix frozen boot issue with Ubuntu Linux

You can also enter the command line menu of GRUB using the “c” key at the GRUB menu interface.
您也可以在 GRUB 菜单界面中使用 “c” 键进入 GRUB 的命令行菜单。

GRUB configuration file

GRUB 配置文件

Any changes you make to GRUB from the menu interface is temporary. If you want to make some permanent changes to GRUB, such as changing the default timeout, you can change the configuration file after you boot into your Linux system.
您从菜单界面对 GRUB 所做的任何更改都是临时的。如果要对 GRUB 进行一些永久性更改,例如更改默认超时,可以在引导到 Linux 系统后更改配置文件。

The default GRUB configuration file is located at /etc/default/grub. There is also a /etc/default/grub.d directory. You may edit the /etc/default/grub file directly, however it is advised to make additional changes by adding config files (.cfg files) in this directory.
默认 GRUB 配置文件位于 /etc/default/grub。还有一个 /etc/default/grub.d 目录。您可以直接编辑 /etc/default/grub 文件,但建议通过在此目录中添加配置文件(.cfg 文件)来进行其他更改。

Default Grub Config File

Default GRUB Config File

You must update GRUB for the changes to take into effect.. In fact, whenever you install an additional Linux on your system, it will try to overwrite the existing GRUB config with its own.
您必须更新 GRUB 才能使更改生效。事实上,每当你在系统上安装额外的 Linux 时,它都会尝试用自己的 GRUB 配置覆盖现有的 GRUB 配置。

GRUB customizer for easily customizing GRUB

用于轻松自定义 GRUB 的 GRUB 定制器

If you think editing a file with a text editor in the terminal. is not something you feel comfortable with, you can use a graphical tool called GRUB Customizer..
如果您认为在终端中使用文本编辑器编辑文件不舒服,则可以使用名为 GRUB Customizer 的图形工具。

It allows you to change the boot order, default timeout etc. You can also use it to change the background of GRUB with a custom wallpaper.
它允许您更改启动顺序、默认超时等。您还可以使用它来更改带有自定义壁纸的 GRUB 的背景。

Make Windows default option in Grub in dual boot mode

GRUB Customizer can be installed in Ubuntu 20.04 from Universe repository and via PPA in Ubuntu. 18.04. It is available via AUR in Arch Linux based distributions..
GRUB 定制器可以从 Universe 存储库安装在 Ubuntu 20.04 中,也可以通过 Ubuntu 18.04 中的 PPA 安装。它可以在基于 Arch Linux 的发行版中通过 AUR 获得。

Conclusion

结论

I have touched everything on the surface. EFI, boot loading and GRUB itself is detailed and complicated topic and not in the scope of this article. This article intended to give you a high-level overview of the GRUB boot program.
我已经触及了表面上的一切。EFI、引导加载和 GRUB 本身是一个详细而复杂的主题,不在本文的讨论范围之内。本文旨在为您提供 GRUB 引导程序的高级概述。

Perhaps I’ll write a detailed guide on GRUB explaining the low-level details. For now, if you want to learn more about GRUB, you can access the GRUB documentation in your Linux terminal using the info grub command.
也许我会写一个关于 GRUB 的详细指南来解释低级细节。目前,如果您想了解有关 GRUB 的更多信息,可以使用 info grub 命令在 Linux 终端中访问 GRUB 文档。

Grub Manual Linux Terminal

GRUB Manual can be accessed via Terminal


How to Install Grub Customizer on Ubuntu

如何在 Ubuntu 上安装 Grub Customizer

The Grub Customizer is a handy GUI tool that lets you configure various aspects of the Grub bootloader. Learn to install it on Ubuntu using a PPA.

Abhishek Prakash
01 Sep 2023

The Grub Customizer is a handy GUI tool that lets you configure various aspects of the Grub bootloader..
Grub Customizer 是一个方便的 GUI 工具,可让您配置 Grub 引导加载程序的各个方面。

You can use it to change the order of the boot and make Windows boot by default..
您可以使用它来更改启动顺序并使 Windows 默认启动。

Windows as the default OS in dual boot menu of the grub

Grub boot order changed to boot Windows by default

You can also use it to boot automatically into the last booted entry. Don’t like the dark black and white theme? You can even change the background of the grub boot screen.
您也可以使用它来自动引导到上次引导的条目。不喜欢深黑色和白色主题?您甚至可以更改 grub 引导屏幕的背景。

change background image grub customizer

Grub Customizer also allows changing the boot screen background

There are several other customization options available as well. The best thing here is that you can do all this using a graphical application. No need to touch the grub configuration files located at /etc/default/grub and /boot/grub2 in the command line.
还有其他几个自定义选项可用。最好的一点是,您可以使用图形应用程序来完成所有这些作。无需在命令行中触摸位于 /etc/default/grub 和 /boot/grub2 的 grub 配置文件。

You don’t need to be afraid of the terminal but using a GUI tool is always more comfortable.
您无需害怕终端,但使用 GUI 工具总是更舒适。

Let’s see how to install Grub Customizer in Ubuntu Linux. The installation method depends on Ubuntu version you are using.
让我们看看如何在 Ubuntu Linux 中安装 Grub Customizer。安装方法取决于您使用的 Ubuntu 版本。

Check your Ubuntu version. using this command:
使用以下命令检查您的 Ubuntu 版本:

lsb_release -a

Installing Grub Customizer in Ubuntu 20.04

在 Ubuntu 20.04 中安装 Grub Customizer

Ubuntu started providing Grub Customizer in the Universe repository with version 20.04.
Ubuntu 开始在 Universe 存储库中提供 Grub Customizer,版本为 20.04。

If you are using Ubuntu 20.04 LTS, you can install it using the following command:
如果您使用的是 Ubuntu 20.04 LTS,则可以使用以下命令进行安装:

sudo apt install grub-customizer

You may even find it in the Software Center.
您甚至可以在软件中心找到它。

grub customizer ubuntu software center

Grub Customizer in Ubuntu Software Center

But Grub Customizer is not available in Ubuntu 22.04. This was due to a bug noticed. with Grub Customizer in version 22.04.
但 Grub Customizer 在 Ubuntu 22.04 中不可用。这是由于在版本 22.04 中发现的 Grub Customizer 的一个错误。

While the developer has fixed the bug, the application remains absent from the Ubuntu repositories in version 22.04.
虽然开发人员已经修复了该错误,但该应用程序在 22.04 版的 Ubuntu 存储库中仍然不存在。

Don’t worry. You can still install Grub Customizer in Ubuntu 22.04.
不用担心。您仍然可以在 Ubuntu 22.04 中安装 Grub Customizer。

Installing Grub Customizer in Ubuntu 22.04 using PPA

使用 PPA 在 Ubuntu 22.04 中安装 Grub Customizer

There is an official PPA available from the developer of Grub Customizer.. You can use it to install the latest version in any Ubuntu version.
Grub Customizer 的开发人员提供了官方 PPA。您可以使用它在任何 Ubuntu 版本中安装最新版本。

Open the terminal with the Ctrl+Alt+T shortcut in Ubuntu..
在 Ubuntu 中使用 Ctrl+Alt+T 快捷键打开终端。

Now use the following command to add the PPA. to your system. This way, you’ll get updates on the Grub Customizer application along with the rest of the system updates.
现在使用以下命令将 PPA 添加到您的系统。这样,您将获得 Grub Customizer 应用程序的更新以及其他系统更新。

sudo add-apt-repository ppa:danielrichter2007/grub-customizer

You’ll be asked to enter your account’s password. Nothing is seen on the screen when you type the password. That’s normal. Press enter when you are asked for confirmation.
系统将要求您输入帐户的密码。键入密码时,屏幕上看不到任何内容。这很正常。当系统要求您确认时,按 Enter。

add grub customizer repo ubuntu

Adding Grub Customizer PPA in Ubuntu

Ubuntu automatically refreshes the repository cache after adding a new repository. This means that you can install the application now using the apt command:
Ubuntu 在添加新存储库后会自动刷新存储库缓存。这意味着您现在可以使用 apt 命令安装应用程序:

sudo apt install grub-customizer

installing grub customizer ubuntu

Installing Grub Customizer in Ubuntu

And that’s about it. Wait for a few seconds and you’ll have Grub Customizer installed.
就是这样。等待几秒钟,您将安装 Grub Customizer。

Press the super key (Windows key) and start typing Grub Customizer. It will show the application icon in the menu. Click on it and start it.
按超级键(Windows 键)并开始键入 Grub Customizer。它将在菜单中显示应用程序图标。单击它并启动它。

Grub customizer Ubuntu

Since you are indirectly modifying configuration files, you need to have elevated privileges to use this application. You’ll be asked to enter your account password.
由于您间接修改了配置文件,因此您需要具有提升的权限才能使用此应用程序。系统将要求您输入帐户密码。

password required for grub customizer

Enter your account’s password to use Grub Customizer

Once opened, you’ll see an interface similar to this:
打开后,您将看到一个类似于以下内容的界面:

grub customizer ubuntu

Grub Customizer in Ubuntu

You can learn some Grub customization tips in this article.. GUI or not, it is always wise to make a Timeshift backup. so that you can safely revert any unpleasant change.
您可以在 本文 中学习一些 Grub 自定义技巧。无论是否为 GUI,进行 Timeshift 备份总是明智的,这样您就可以安全地恢复任何令人不快的更改。

Before I end this tutorial, let me show you the steps for removing Grub Customizer.
在结束本教程之前,让我向您展示删除 Grub Customizer 的步骤。

Remove Grub Customizer from Ubuntu

从 Ubuntu 中删除 Grub Customizer

Open a terminal and use the following command to remove it:
打开终端并使用以下命令将其删除:

sudo apt remove grub-customizer

If you had added the PPA, you should remove the PPA. as well.
如果您已添加 PPA,则也应删除 PPA。

sudo add-apt-repository -r ppa:danielrichter2007/grub-customizer

And that’s all you need to know about installing and removing Grub Customizer on an Ubuntu Linux system.
这就是您需要了解的有关在 Ubuntu Linux 系统上安装和删除 Grub Customizer 的全部信息。


What Is a Bootloader? How Does a Bootloader Work?

什么是 Bootloader?引导加载程序如何工作?

By Calvin Ebun-Amu
Published Apr 20, 2021

bootloader loading screen feature

A bootloader is very important for operating systems. Yet, it is often overlooked as a key component of computers. This is because it plays its role in the background, helping to activate the correct operating system. Most of the processor boards in our devices have programs pre-loaded on them. These programs are known as bootloaders.
引导加载程序对于作系统非常重要。然而,它作为计算机的关键组件经常被忽视。这是因为它在后台发挥作用,有助于激活正确的作系统。我们设备中的大多数处理器板都预装了程序。这些程序称为 bootloader。

This article explains what a bootloader is and how it works. If you’re interested in learning about what a bootloader is and how it works, this article is for you.
本文介绍了什么是 bootloader 及其工作原理。如果您有兴趣了解什么是引导加载程序及其工作原理,那么本文适合您。

What is a Bootloader

什么是 Bootloader

When turned on, a computer has a clear state. This means that there are no programs in its memory and that its components cannot be accessed.
打开后,计算机具有 Clear 状态。这意味着其内存中没有程序,并且无法访问其组件。

A bootloader helps to load the operating system or runtime environment to add programs to memory and provide access for components. It is needed to run the startup process, initialize the hardware, and pass control to the kernel, which initializes the operating system.
引导加载程序有助于加载作系统或运行时环境,以将程序添加到内存并为组件提供访问权限。需要运行启动过程,初始化硬件,并将控制权传递给内核,内核初始化作系统。

BIOS

The key components of a bootloader include the basic input/output system (BIOS), firmware found in the Read-Only Memory (ROM) of a PC. When the PC is turned on, the BIOS runs before any other program runs.
引导加载程序的关键组件包括基本输入/输出系统 (BIOS),以及 PC 只读存储器 (ROM) 中的固件。当 PC 打开时,BIOS 会在任何其他程序运行之前运行。

The BIOS is composed of the following:
BIOS 由以下部分组成:

  • POST (Power On Self Test)
    POST(开机自检)
  • Boot Sector Loader 引导扇区加载程序
  • BIOS interrupts BIOS 中断
  • Setup Menu 设置菜单

The Setup Menu helps to adjust the parameters of the bootloader. Modern BIOS versions are used to set different parameters. This includes the boot order, which determines the devices the BIOS checks before booting .
Setup Menu 有助于调整 bootloader 的参数。现代 BIOS 版本用于设置不同的参数。这包括引导顺序,它决定了 BIOS 在引导前检查的设备。

The boot sector loader loads the first 512-byte sector from the boot disk into RAM. It is needed for the first available memory block or sector in the medium to be reserved for the record.
引导扇区加载程序将引导磁盘中的第一个 512 字节扇区加载到 RAM 中。需要为记录保留介质中的第一个可用内存块或扇区。

BIOS interrupts are device drivers relied on by bootloaders to access the screen, keyboard, and disks. Not all operating systems use BIOS interrupts.
BIOS 中断是引导加载程序用来访问屏幕、键盘和磁盘的设备驱动程序。并非所有作系统都使用 BIOS 中断。

In addition to the BIOS, there are extension ROMs. The main BIOS may start the extension ROMs.
除了 BIOS 之外,还有扩展 ROM。主 BIOS 可能会启动扩展 ROM。

The bootloader is usually composed of three programs:
引导加载程序通常由三个程序组成:

  • A boot sector program directly loaded by the BIOS at boot time
    BIOS 在引导时直接加载的引导扇区程序
  • A second stage program loaded by the boot sector program to complete the booting process
    由 boot 扇区程序加载的第二阶段程序,用于完成引导过程
  • A bootloader installer to install the bootloader and the second stage program in the boot disk.
    一个 bootloader 安装程序,用于在引导盘中安装 bootloader 和 second stage 程序。

UEFI BIOS

The evolving requirements of computer users have led to the creation of a modern successor to BIOS.
计算机用户不断变化的需求导致了 BIOS 的现代继任者的诞生。

UEFI (Unified Extensible Firmware Interface) is a mini operating system that loads the bootloader in the memory before it executes additional operational routines.
UEFI(统一可扩展固件接口)是一个迷你作系统,它在执行其他作例程之前将引导加载程序加载到内存中。

While it shares some similarities with BIOS, several key differences have led many to consider UEFI as an extension rather than a replacement traditional BIOS.
虽然它与 BIOS 有一些相似之处,但几个关键差异导致许多人将 UEFI 视为扩展,而不是替代传统 BIOS。

One of the main differences between UEFI and BIOS is how UEFI provides an operational interface for computers and uses new mechanisms and functions. Additionally, while UEFI does not define how firmware should be programmed in its entirety, it does influence the interface between the firmware and operating system.
UEFI 和 BIOS 之间的主要区别之一是 UEFI 如何为计算机提供作界面并使用新的机制和功能。此外,虽然 UEFI 没有定义如何对固件进行整体编程,但它确实会影响固件和作系统之间的接口。

The UEFI program is permanently located on a memory chip on the motherboard. This means it is retained even when there is no power. A unique operational layer is used to communicate between the firmware and the operating system. UEFI mode can be initialized. before the operating system is started up.
UEFI 程序永久位于主板上的内存芯片上。这意味着即使没有电源,它也会被保留。唯一的作层用于固件和作系统之间的通信。可以在作系统启动之前初始化 UEFI 模式。

Some of the key advantages of UEFI include:
UEFI 的一些主要优势包括:

  • It does not require an active operative system for network functionality
    它不需要主动作系统即可实现网络功能
  • Better usability through the use of a computer mouse and a graphical user interface
    通过使用计算机鼠标和图形用户界面实现更好的可用性
  • Improved security through the Secure Boot feature
    通过安全启动功能提高安全性
  • A flexible modular structure that helps it adapt to special hardware environments and requirement profiles
    灵活的模块化结构,有助于适应特殊的硬件环境和需求配置文件
  • Boot manager, which manages different bootloaders for various operating systems
    Boot Manager,用于管理各种作系统的不同 Bootloader
  • A command-line tool for carrying out diagnostics and troubleshooting
    用于执行诊断和故障排除的命令行工具

The purpose of Secure Boot is to improve security. Parts of the UEFI firmware, the bootloader, and the operating system kernel must be verified before starting up. The components are verified using cryptographic digital signatures in the signature database of the UEFI firmware. The system boot may be canceled if the security check is not passed.
安全启动的目的是提高安全性。在启动之前,必须验证 UEFI 固件、引导加载程序和作系统内核的某些部分。使用 UEFI 固件的签名数据库中的加密数字签名对组件进行验证。如果未通过安全检查,则可能会取消系统启动。

In professional environments, Secure Boot is typically used with a hardware component. The Trusted Platform Module (TPM) is a key component of Secure Boot., providing security features for computers.
在专业环境中,安全启动通常与硬件组件一起使用。受信任的平台模块 (TPM) 是安全启动的关键组件,为计算机提供安全功能。

When compared against the traditional legacy BIOS system boot method, the following advantages can be gained from UEFI and GPT offers the following advantages:
与传统的传统 BIOS 系统引导方法相比,UEFI 可以获得以下优势,而 GPT 具有以下优势:

  • Allows for multiple operating systems with their own boot managers to be installed at the same time.
    允许同时安装多个作系统,这些作系统都有自己的引导管理器。
  • Boots faster than legacy BIOS systems
    启动速度比传统 BIOS 系统更快
  • Supports pre-boot applications
    支持预启动应用程序

These days, an NVRAM (non-volatile random access memory) is used for boot-related settings. The settings are stored after the computer is turned off. A small amount of power from the motherboard battery is used.
如今,NVRAM (非易失性随机存取存储器) 用于与引导相关的设置。这些设置在计算机关闭后存储。使用来自主板电池的少量电力。

How Does a Bootloader Work?

引导加载程序如何工作?

After a computer is turned on, information about the installed hardware comes up on the screen. The bootloader places its operating system into the memory. The basic input/output system (BIOS) carries out tests before transferring control to the Master Boot Record (MBR), which contains the boot loader.
打开计算机后,屏幕上会显示有关已安装硬件的信息。bootloader 将其 operating system 放入内存中。基本输入/输出系统 (BIOS) 在将控制权转移到包含引导加载程序的主引导记录 (MBR) 之前执行测试。

A lot of bootloaders are configured to give users different booting options. The options include different operating systems, different versions of the same operating system, operating system loading options, and programs that run without an operating system.
许多 bootloader 被配置为为用户提供不同的引导选项。这些选项包括不同的作系统、同一作系统的不同版本、作系统加载选项以及在没有作系统的情况下运行的程序。

In certain cases, a device may have two operating systems. Bootloaders can be used on these devices to start the correct operating system that users prefer automatically. A bootloader can also be used to boot the operating system into safe mode for recovery.
在某些情况下,一个设备可能有两个作系统。可以在这些设备上使用 Bootloader 来自动启动用户喜欢的正确作系统。引导加载程序还可用于将作系统引导到安全模式以进行恢复。

You can use a bootloader to boot into a program without having to start the operating system. This can be useful with devices such as game consoles. After the game disc is inserted into the console and the console is turned on, the user is taken straight to the game instead of the welcome screen.
您可以使用 bootloader 启动到程序中,而无需启动作系统。这对于游戏机等设备非常有用。将游戏光盘插入主机并打开主机后,用户将直接进入游戏,而不是欢迎屏幕。

Exceptions and Interrupts

异常和中断

Bootloaders are very important for the maintenance of security and software architecture. Exceptions and interrupts may be handled differently by bootloaders and applications. The method by which the hardware and software are linked directly affects how the exceptions and interrupts operate and affect the functionality of the bootloader.
Bootloader 对于维护和软件架构非常重要。引导加载程序和应用程序可能以不同的方式处理异常和中断。链接硬件和软件的方法直接影响异常和中断的运行方式,并影响 bootloader 的功能。


What is a bootloader and how does it work?

什么是引导加载程序,它是如何工作的?

IONOS editorial team 05/11/2022

Bootloaders ensure that all relevant operating system data is loaded into the main memory when a device is started. During the boot process, the firmware transmits the required information.
Bootloader 确保在设备启动时将所有相关的作系统数据加载到主内存中。在引导过程中,固件会传输所需的信息。

What is a bootloader?

什么是 Bootloader?

Data of an operating system. must be loaded into the working memory. during device start-up. This is made possible by a so-called bootloader, also known as a boot program or bootstrap loader. For this purpose, immediately after a device starts, a bootloader is generally launched by a bootable medium like a hard drive, a CD/DVD, or a USB stick. The boot medium receives information from the computer’s firmware (e.g. BIOS) about where the bootloader is. The whole process is also described as “booting”.
在设备启动期间,必须将作系统的数据加载到工作内存中。这是通过所谓的 bootloader 实现的,也称为 boot program 或 bootstrap loader。为此,在设备启动后,引导加载程序通常由硬盘驱动器、CD/DVD 或 U 盘等可引导介质立即启动。引导媒体从计算机的固件(例如 BIOS)接收有关 bootloader 位置的信息。整个过程也被描述为 “booting”。

Note

The term “bootloader” is a shortened form of the words “bootstrap loader”. The term stems from the fact that the boot manager is the key component in starting up the computer, so it can be likened to the support of a bootstrap when putting a boot on.
术语 “bootloader” 是单词 “bootstrap loader” 的缩写形式。该术语源于这样一个事实,即启动管理器是启动计算机的关键组件,因此它可以比作启动时对引导程序的支持。

How does a bootloader work?

引导加载程序是如何工作的?

When you press the start button on a computer, the very first thing you see on the screen is information about the hardware installed. The software responsible for this notification is the device firmware mentioned above, which is usually implemented by manufacturers in flash memory on the computer’s motherboard. With most desktop PCs and notebooks this will be the BIOS. (Basic Input/Output System) or the more modern UEFI. (Unified Extensible Firmware Interface). Both applications collect the most diverse hardware data and create a complete list of all of the device’s available drives.
当您按下计算机上的开始按钮时,您在屏幕上看到的第一件事是有关已安装硬件的信息。负责此通知的软件是上述设备固件,通常由制造商在计算机主板上的闪存中实现。对于大多数台式电脑和笔记本电脑,这将是 BIOS(基本输入/输出系统)或更现代的 UEFI(统一可扩展固件接口)。这两个应用程序都收集最多样化的硬件数据,并创建设备所有可用驱动器的完整列表。

When this process is complete, the firmware goes through the data carriers found in sequence, checking for a bootloader by means of a special signature – the so-called boot signature (or “boot record”). The search always starts on the removable media (CD/DVD, USB stick, external hard drive, etc.), followed by the hard-coded drives. With the latter, the bootloader and its signature is generally in the Master Boot Record. (MBR), which also contains the data carrier’s partition tables. When a bootloader is found, it is loaded and the system start is initiated. If the search is unsuccessful, the firmware will return an error message.
此过程完成后,固件将按顺序通过找到的数据载体,通过特殊签名 – 所谓的 boot signature (或 “boot record”) 来检查引导加载程序。搜索总是从可移动媒体(CD/DVD、USB 记忆棒、外部硬盘驱动器等)开始,然后是硬编码驱动器。对于后者,bootloader 及其签名通常位于 Master Boot Record (MBR) 中,其中还包含数据运营商的分区表。找到 bootloader 后,将加载该 bootloader 并启动系统启动。如果搜索不成功,固件将返回错误消息。

Tip

As soon as you have installed an operating system properly it will launch via a bootloader, which is typically located on the main partition. If the system is damaged, however, and ought to be installed again, you are dependent on the bootable removable media mentioned previously. This also applies if no operating system is installed. You can find out how to transform a standard USB stick into a fully functional boot medium. in our guide.
一旦您正确安装了作系统,它就会通过引导加载程序启动,引导加载程序通常位于主分区上。但是,如果系统已损坏,并且应该重新安装,则您依赖于前面提到的可启动可移动媒体。如果未安装作系统,这也适用。您可以在我们的指南中了解如何将标准 USB 记忆棒转换为功能齐全的引导介质。

Where exactly are bootloaders stored?

引导加载程序究竟存储在哪里?

Bootloaders can be stored in two different places:
Bootloader 可以存储在两个不同的地方:

  1. The bootloader is stored in the first block of the bootable medium.
    bootloader 存储在可引导媒体的第一个块中。
  2. The bootloader is stored on a specific partition of the bootable medium.
    引导加载程序存储在可引导媒体的特定分区上。

The first variant is closely connected to the principle of master boot records, which don’t just contain the link to the bootloader required by the firmware, but the boot software itself. The first available memory block or sector in the medium is always reserved for the record. Because of this important function, it is also known as the boot block or boot sector.
第一种变体与 master boot records 的原理密切相关,它不仅包含固件所需的 bootloader 的链接,还包含引导软件本身。介质中的第一个可用内存块或扇区始终保留用于记录。由于这个重要的功能,它也被称为 boot block 或 boot sector。

In the second case, the operating system uses a selected partition as the storage location for the bootloader, though the underlying file system and the partition tables used can vary greatly. The decisive factor here is always the firmware, which stipulates a specific file format for the boot manager in this bootloader storage model. For devices with UEFI, for example, the format is PE/COFF (Portable Executable / Common Object File Format).
在第二种情况下,作系统使用选定的分区作为 bootloader 的存储位置,尽管底层文件系统和使用的分区表可能会有很大差异。这里的决定性因素始终是固件,它为这个 bootloader 存储模型中的 boot manager 规定了特定的文件格式。例如,对于具有 UEFI 的设备,格式为 PE/COFF(可移植可执行文件/通用对象文件格式)。

Note

Bootloaders can also be spread across several layers that build upon each other. These multi-stage bootloaders consist of up to three different levels that are launched in sequence. They are typically used if the boot program is too big for the boot sector.
Bootloader 也可以分布在相互构建的多个层中。这些多阶段引导加载程序由最多三个按顺序启动的不同级别组成。如果引导程序对于引导扇区来说太大,则通常使用它们。

A summary of a bootloader’s functions

引导加载程序的功能摘要

Bootloaders serve as a mediator between hardware and the operating system. As soon as a bootloader has been initialized by the respective firmware, it has system responsibility to get the boot process going. The first task is to load the main memory, which is essential for the processor to work.
引导加载程序充当硬件和作系统之间的中介。一旦 bootloader 被相应的固件初始化,它就有系统责任来启动过程。第一个任务是加载主内存,这对于处理器工作至关重要。

In the second step, the bootloader loads the kernel of the operating system, that is, the primary component of the system software that controls all storage and processor permissions and contains all important drivers. The bootstrap loader also processes different routine tasks and commands, e.g. integrating data storage. Some bootloaders also perform tasks beyond starting up software, including:
在第二步中,引导加载程序加载作系统的内核,即系统软件的主要组件,它控制所有存储和处理器权限,并包含所有重要的驱动程序。bootstrap 加载程序还可以处理不同的日常任务和命令,例如集成数据存储。一些引导加载程序还执行启动软件以外的任务,包括:

  • Identifying and starting other available bootloaders
    识别并启动其他可用的引导加载程序
  • Launching application programs (frequently used in the 1980s to launch computer games directly from a disk)
    启动应用程序(在 1980 年代经常用于直接从磁盘启动计算机游戏)
  • Correcting or expanding missing functions and entries in the firmware
    更正或扩展固件中缺失的功能和条目
  • Loading alternative firmware
    加载备用固件

After successfully completing all tasks, the bootloader returns the system responsibility to the kernel.
成功完成所有任务后,引导加载程序将系统责任返回给内核。

What well-known bootloaders are there?

有哪些著名的引导加载程序?

The following table lists the most important bootloaders:
下表列出了最重要的引导加载程序:

Name of the bootloaderDescription
Bootmgrboot program for Microsoft systems since Windows Vista and Windows Server 2008
NT loader (NTLDR)boot program for Microsoft systems until Windows XP and Windows Server 2003
bareboxbootloader for embedded systems in printers, cameras, cars, airplanes, and more
boot.efiEFI bootloader that has been used in Mac devices since 2006
BootXformer bootloader for Mac operating systems
Grand Unified Bootloader (GRUB)free boot program for Unix-like operating systems such as Linux
ARM Core Bootloaderbootloader for microcontrollers (used in iPhones among others)
OpenBIOSfree, portable boot manager under a GNU-GPL license

Bootloader 详解

「已注销」于 2021-12-25 17:10:49 发布

一、Bootloader 作用(目的)

Bootloader=Boot + loader

Boot 的目的:

最终目的:跳到 C 语言中;为了 C 语言运行程序会进行一系列的初始化,系统一上电后如何通过一系列的设置让软件程序员进入 C 语言 / 更高级语言环境的开发,这个过程就是 boot 的主要目的。

Loader 的目的:

主要目的是开始执行应用逻辑,比如点灯:需要灯的接口开发;串口输入输出:需要串口编程;加载 linux 的内核:flash 的编程、网卡的编程、内核启动前的初始化部分。根据不同的应用会有不同的变化。

一个开发板要想执行 loader,要先看 boot 做了什么事。

二、完成 Boot 最终目的的前提条件

围绕 boot 的最终目的,对执行最终目的的前提条件进行说明:

前提条件:

(1)让 SP 指向可读可写的设备空间中

(2)满足递减栈的规则 —SP 想办法放在内存的高段地址

(3)配置 SDRAM 的控制器。首选空间为 SDRAM,SDRAM 不是系统一上电就好,所以在做(1)、(2)之前要配置 SDRAM 的控制器,使 SDRAM 可以正常工作。

(4)配置系统工作时钟,通过代码对相应寄存器进行相关配置即可实现。

(5)关闭看门狗、中断、MMU、CACHE,通过汇编语言对相应的寄存器进行置位即可实现。

开发时前提条件的执行顺序:(5)、(4)、(3)、(1)、(2)

三、对前提条件的详细说明

3.1、对前提条件(1)的说明:

先看代码:

int main()
{
	abc();//main函数中调用abc函数
}

int abc()
{
	int  a = 10;//假设用r0寄存器保存了立即数10,接下来跳到fun函数中
	fun();//abc函数中调用fun函数
	if(a xx)//函数fun返回后再判断a的情况,发现a不再是10
}

int fun()
{
	int  b = 20;/*假设同样用寄存器r0保存20,不用内存保存是因为寄存器的访问速度比内存访问速度快,
				函数abc中的变量a在整个代码执行区中不需要再往回传给main函数了,a是一个临时的值,
				寄存器r0只有一个,也只能保存一个,所以a只是出现一次,然后就不用了。当abc函数调用fun函数后,
				寄存器r0中的值10会被20替换*/
}

在上述代码运行过程中,一旦函数 abc 又调用了函数 fun,寄存器 r0 中的值会被替换,从 10 变成了 20,函数 fun 在返回后就会出现问题:这时用 if 语句判断 a 的情况,会发现 a 已经不再等于 10 了。

如果按照上述逻辑去设计汇编的话问题会有很多。于此问题,C 语言给我们提供了栈的概念 —stack

栈的逻辑:先进后出、后进先出,压栈出栈是通过代码对指向栈区指针的值进行更新、赋值,进而对指针指向的内存空间进行读和写。由此有了栈指针概念 — 该指针必须指向一个具备可以进行读写的硬件空间。在 Arm 体系架构中,栈指针用寄存器 SP 来描述。何为可读可写 —— 与应用程序思路不同,硬件上的可读可写如代码:

int  a = 10;// 这句话执行完后让以 a 为首地址的那段空间 / 设备把 10 赋进去
*(&a)==10;// 进行解引用后将值取出来和 10 是相等的相当于将 10 写进去了

如果 a 的地址有问题,比如它指向的是一个不存在的空间。何为不存在的空间 ——0x2000 0000 是内存机地址,但硬件公司只买了一个 16M 的内存,现在软件工程师想把 a 放在地址 0x2100 0000,这个地址发出去后显然没有落到 16M 的范围内,那么对 a 的赋值就无法进行了。这时 C 语言无法帮我们进行压栈入栈的操作。

小结:栈指针一定要指向一个合法的设备 —— 可读可写的地址空间。

3.2、对前提条件(2)的说明:

系统刚上电时 SP 指向的是 0 地址,即 ROM,显然写不进去。所以在跳到 C 语言之前一定要让 SP 指向一个可读可写的设备区间中,SP 首选指向地址是 SDRAM 中。如果用汇编写 Bootloader:

代码为:

MOV  SP, #0x2000  0000// 跳之前将 sp 赋值
BL  MAIN// 跳到 MAIN 中

这样会出现问题,我们在学 Arm 基础时知道栈指针在 C 语言中有一个规范,称之为 ATPCS,它规定 C 语言默认将 SP 作为栈指针,且 SP 指向的是一个向下压栈的过程,所以把栈指针指向内存的低地址不合适,SP 指针一定要往上指,所以要满足递减栈的规则。

代码应为:

MOV  SP, #0x2100  0000// 跳之前将 sp 赋值
BL  MAIN// 跳到 MAIN 中

问题没完。既然指向地址 0x2100 0000,而这个地址是内存,内存是一上电就能干活?不行,内存涉及到一个动态的充电过程。内存的访问:发出地址 —— 直接把数据发给它 —— 将数据写过去。

假设内存大小是 16M,地址总线数计算:

在这里插入图片描述

存储总量 = 存储单元个数 × 存储字长,

然后:

存储单元个数 = 2 地址总线位数 存储单元个数 = 2^{地址总线位数} 存储单元个数=2地址总线位数

另:

1 M B = 1024 K B = 1024 B ∗ 1024 = 1048576 B = = 2 20 B 1MB=1024KB=1024B*1024=1048576B== 2^{20} B 1MB=1024KB=1024B1024=1048576B==220B

存储字长是什么:

存储字长:存储单元中的二进制代码(存储字)位数,存储字长可以是 8 位、16 位、32 位等。

早期计算机的存储字长一般和机器的指令字长与数据字长相等,故访问一次主存便可取一条指令或一个数据。随着计算机的应用范围的不断扩大,解题精度的不断提高,往往要求指令字长是可变的,数据字长也要求可变。为了适应指令和数据字长的可变性,其长度不由存储字长来确定,而由字节的个数来表示。1 个字节(Byte) 被定义为由 8 位(Bit)二进制代码组成。

存储字是指存放在一个存储单元中的二进制代码组合。一个存储字可代表一个二进制数,也可代表一串字符,如存储字为 0011011001111101,既可表示为由十六进制字符组成的 367DH(ASCII 码),又可代表 16 位的二进制数,此值对应十进制数为 13 949,还可代表两个 ASCII 码:“6” 和 “}”。一个存储字还可代表一条指令。 字长就是在同一时间中处理二进制数的位数叫字长。通常称处理字长为 8 位数据的 CPU 叫 8 位 CPU,32 位 CPU 就是在同一时间内处理字长为 32 位的二进制数据。

存储字长是在内存中存储字的长度,可以是 16bit 或 8bit

故:

16 M = 2 地址总线位数 × 存储字长 16M=2^{地址总线位数}× 存储字长 16M=2地址总线位数×存储字长

即:

( 2 4 ∗ 2 20 ) B = 2 地址总线位数 × 存储字长 (2^4*2^{20}) B= 2^{地址总线位数}× 存储字长 (24220)B=2地址总线位数×存储字长

存储字长为 1,所以地址总线数为 24。

所以这个内存有 24 根地址总线,用这 24 根地址总线就可以对 16 兆进行逐一编址。现在 CPU 只要发出对应的地址,就会进行数据的读写。

如果一个芯片的内存比较大,为了减小芯片的体积,需要减少地址总线的数目。如何用较少的地址线来将较大的内存全访问到?利用 C 语言中一维数组、二维数组的概念。

如代码:

int  a [16];// 在这里定义了 16 个字节(2^4),用 4 根线就能找到它(用 4bit 可以表示)

即用这 4 根线就能表示这 16 种状态。

现在想办法减少这 4 根线的数目,对于同样的空间我还可以怎样访问?如代码:

int  b [4][4];// 将 16 个字节分为 4 行 4 列

用这种方式访问时需要知道行数和列数,范围都是 0~3,4 个字节大小,现在用 2bit 就能表示(2 根线访问)。先发出行坐标,再发出列坐标。

通过改成 2 维数组使得外部接口上的位数变少了。数组维数越大,在表示相同大小的内存时,需要的位数越小。

芯片内存的内部结构可以理解为一个多维数组,它由多个面构成,要想访问其中的一块空间,要先知道它在哪一个面上,然后知道它在这个面上的行、列坐标(地址)。内部结构大致如下图所示:

在这里插入图片描述

引出问题:现在发出地址 0x2100 0000,要将这个地址转换为在这个芯片中的哪一个面上、哪一行上、那一列上。这个转换过程可以由软件程序员来自行分解,也可以由硬件做。软件做成本高、意义也不大,故而让硬件来帮我们确定。要让硬件做会引出另外一个概念:在芯片的 datasheet 中的 DRAM CONTROLLER 中规定了这一过程:见对前提条件(3)的说明中的内容。

细节补充说明:SP 指针要往上指,但是不是随便指。在 ARM 体系中 ARM 工作分了 7 个工作空间。第一个:SVC 模式,在系统复位后默认会用的一个模式,工作在 Linux 的 kernel 空间;第二个:IRQ 模式,中断一旦发生后的运行模式;第三个:FIQ 模式;第四个:USR 模式,它对应的实际上就是 Linux 下的 app,就是应用程序空间。

… 等七个工作空间。

其中 USR 模式和 SVC 模式工作的空间都有自己的 sp 指针,这样用户空间操作的代码不会影响内核。这样会出现一个问题,内存假如只有一块,两个 sp 指针在遵循指向高地址的原则时可能会重复指向一个地址,所以在对 sp 赋值时要考虑用哪一个模式时要将 sp 分开,如何分开:对应每一个工作模式划分相应的可供使用的内存空间,如下图所示,但这时可能会出现另一个问题,如果 USR 的内存空间没有设计好,一旦发生死循环递归,栈会慢慢的往下延伸,会发生栈的溢出,溢出后会将 SVC 的空间内容修改了,那么在执行 SVC 时结果就错了,程序就跑得乱七八糟。

在这里插入图片描述

所以在执行前提条件(1)(2)的时候要考虑几个问题,首先要考虑用哪些空间 / 模式,就要初始化哪些模式下的 sp,如何初始化:有一套专门用于模式切换的指令。其次在对 sp 赋值时有原则:每个模式的值不能重复、不能覆盖其他模式。

3.3、对前提条件(3)的说明:

在这里插入图片描述

CPU 直接发出地址后现在要访问 SDRAM,让 CPU 把想做的事情告诉给控制器,控制器根据我的配置(行地址数、列地址数、多少块、周期性充电问题、计算多久让它充一次电)

关于计算多久让它充一次电:CPU 是一个脉冲波形,这里要引入时序的概念。

在这里插入图片描述

时序是芯片的心脏,每一个上升沿触发 CPU 干一件事,多久充一次电?

控制器凭什么知道经过多久可以充?所以要先配置控制器,在配置中又有一个问题,CPU 和控制器都有自己的工作频率,它们共用同一个频率吗?即它们共用同一个时钟吗?因为外围控制器的成本 / 芯片工艺过程决定了控制器处理数据的能力较弱,导致不能同步接收到发送给它的数据,所以总时钟总线上的频率不会直接给控制器用,而是通过一个分频器,将频率降低后再给它用。所以还要先配置系统工作时钟。只有配置好了工作时钟才能计算出多久充一次电。

3.4、对前提条件(4)的说明:

在嵌入式开发过程中这一步是最耗时的,因为后期 Linux 内核最终还是运行在 RAM,包括很多程序都是在 RAM 上运行,而 RAM 中,如果我们给它的频率不符合这款芯片本身的物理特性,那么很容易出现程序跑飞,在 PC 中就可能出现程序在运行时突然蓝屏,蓝屏的原因有很多,但是大部分都是由于内存的问题,有些时候买的内存的芯片的物理结构 / 质量不好,响应时间会很慢。举例:现在 CPU 要发出序列 1101,现在芯片要想办法以一定的速度将这 4bit 发到数据总线上,或者说给到这片 SDRAM 上,假设 CPU 工作在 1GHZ(这频率很快了),基本上 1GHZ 分之一再乘以 4,这个时间就可以把 4 个比特发完了。SDRAM 价格便宜,根本就不能同步响应 1GHZ 的 CPU 给的信息,那么内存工作就有问题,出现蓝屏。

配置 SDRAM 的难度就在于不仅要看懂 SDRAM 的芯片手册,同时还要懂得一定的硬件原理,包括一些调试经验,然后去动态的算出针对某一块开发板的 SDRAM 的具体工作参数。

3.5、对前提条件(5)的说明:

配置系统时钟之前还有一个问题,不是配了就马上工作正常,还要经过一段时间,在这期间还要引入看门狗。看门狗就是一个定时器,在一段时间之内,如果不对定时器进行重新的喂狗,一旦减到 0 就会触发一个中断 / 复位,CPU 就相当于重启了。所以要保证有足够的长的时间来执行前提条件(1)、(2)、(3)、(4),所以在配置时钟之前为了安全起见还要把看门狗给关了。同时中断也要关了,因为我们还没进入到 C 或者说还没进到系统处理,即使中断来了,我们还不知道该怎么处理,因为我们的函数还没注册进去,这时最好把中断也关了,在调用 main 函数时再将中断打开。除此之外还可以关闭 MMU、CACHE,MMU 一般指的是有操作系统情况下才用,裸机程序上一般情况下为了简单一点可以不要管它,对于 CACHE,指令 CACHE 无所谓可开可关,但是数据 CACHE 一般情况下在这时最好关了,虽然说速度可以提高,但是由于涉及到数据完整性的问题,有可能 CACHE 打开后数据的完整性就会有影响,这样给后期的调试带来很大麻烦,所以先关了它。

3.6、Boot 阶段基本流程简单总结:

Boot 最终目的是跳到 C 语言,在跳到 C 语言之前要考虑栈指针的初始化,因为 C 语言编译器要自动的利用 SP 指针来作为我们的栈空间,这样我们就必须要把 SP 指向一个可读可写的设备,具体赋什么值要根据规则来决定,然后再放到 SDRAM 上,紧接着 SDRAM 要想工作,在擦之前还要配置 SDRAM,这个工作量比较大,包括:行地址数、列地址数、多少块、周期性充电问题,在配置 SDRAM 之前还要计算多久充一次电,所以还要配置系统时钟,而系统时钟工作还要一些前导,即关闭看门狗、中断、MMU、CACHE。

所以具体的来说,不管什么系统,要想设计一款 Bootloader 中的 Boot 阶段基本是要完成上述前提工作。最后根据每个系统的条件不同,还涉及到一个概念就是代码搬移。

为什么要代码搬移?一个芯片,系统启动时它会规定只能处理多少量的代码,如果代码量超过了规定的可处理的量,多余的代码就处理不了了,这时就要程序员自己把代码搬到一个更大的空间上去运行,要程序员自己对控制器进行控制,所以在 main 函数执行前也还要对相应存储器的控制器进行初始化,在代码搬移前还要初始化对应控制器,这样做软件工程师的工作量大一些,因为首先要看硬件公司用哪一个存储器启动,比如 SD 卡就要写 SD 卡的驱动,如果是 NAND 启动,就要写 NAND 的驱动,然后考虑怎么把代码快读搬到内存上,跳到内存上执行,一般来说代码搬移到 RAM 上,所以都要在 SDRAM 工作正常的基础上。其次是执行速度问题,如果代码本身之前工作在 flash 上,flash 的执行速度比 RAM 慢得多,所以要把程序从存储器(nor-flash)搬移到快速的内存上运行。这就是我们说的代码搬移。

以上就是完成 Bootloader 中的 Boot 工作所要进行的一些事。

四、loader 是干啥的

当 MAIN 函数跳过来之后(进入到 C 语言后)进行 Loader 的目的,即开始执行应用逻辑,这个要根据具体的工程代码来决定。


浅谈 ECU Bootloader

IT 技术猿猴 于 2022-02-24 19:51:08 发布

单片机正常时运行上电 / 复位,第一条指令是固定的,程序正常顺序运行到 Bootloader,由 Bootloader 跳转到 APP 程序运行。

在这里插入图片描述

什么是 Bootloader

单片机正常运行时总是从固定地方取指令,顺序运行,这将对编写程序的人产生巨大的挑战,程序更新时需要使用烧录器等工具烧录,于是有人将程序设计成,由一个程序跳转到另一个程序,这个程序通常称作 Bootloader,另一个叫做 APP。

Bootloader 程序常常具有通信接口和擦写内部存储空间的功能,可将需要更新的 APP 擦除,写入新的 APP。有时会设计成相互跳转,技术也是可以实现的。有些为了保证程序不丢失,单独预留出备份区和出厂版本,出现某些错误时可以恢复到出厂版本或使用其他 APP 均可。

在这里插入图片描述

ECU 的 Bootloader

ECU 是 MCU 的一种,专门用在汽车上。ECU 经常会用在汽车零部件中,零部件密封性等要求都比较苛刻,并且装车,如果想取下零部件可能需要将车拆解才可以做到,这种行为是不被允许的,成本极高,操作复杂,因此大多主机厂(厂商)要求 ECU 具有升级功能,并且通过多年的积淀制定了行业标准 UDS。

UDS 简介:UDS(Unified Diagnostic Services,统一诊断服务)诊断协议是用于汽车行业诊断通信的需求规范,由 ISO 14229 系列标准定义。UDS 诊断通信用于建立诊断仪与 ECU 之间的通信连接,并负责将 ECU 中的诊断结果输送到诊断仪中。UDS 的作用非常广泛,几乎跟随 ECU 软件开发的全过程。

在这里插入图片描述

UDS 后续会专门写相关知识进行讲解。

ECU 开发过程要用到它来构建 bootloader,上传和下载数据,即软件刷写,控制器 Reset;测试时要用它来读写存储,控制外设;产线上,要用它来校准机械件,控制例程,进行下线执行器测试,刷新软件,配置防盗,读取号码,下线配置等。在行驶过程中,要用它来监测各种故障,并记下故障码;4S 店里,技师需要读取当前故障、历史故障,读取故障发生时刻环境信息状态,清除故障,判断故障发生点,还可以用来升级 ECU 程序。

汽车明确规定通过 UDS 进行更新程序,主机厂要求擦写内部存储的代码不可写入正常代码中。汽车电子中 ECU 一旦设计完成,装车量产就很难再拆卸并返回零部件供应商完成功能升级或补丁修复。一旦出现售后质量问题,如果召回的话,零部件供应商和整车厂将面临严重的经济损失,因此设计基于 CAN 总线的 ECU 在线程序更新 Bootloader 可以很好的解决这一问题,将零部件供应商和整车厂的损失降低到最小。目前大部分汽车整机厂(主机厂)都要求在其设计的 ECU 实现 Bootloader 功能。

Bootloader 框架

在这里插入图片描述

在这里插入图片描述

Bootloader 由主机厂或者自己,可以选择用或者不用,本次主要针对使用 Bootloader 情况进行分析。主机使用协议由自己进行定义,ECU 启动模式选择由芯片厂商进行技术支持。内部编写均需要遵循协议,大多数开发都是由多年开发经验沉淀下来,修改而成的,协议依然在进步,代码可能无法维护而无法支持,主机厂也会被迫选择使用旧版协议。

在这里插入图片描述

ECU Bootloader 原理

主机厂规定不可把擦写内部代码的功能直接写入程序中,因此,只能每次用时才能将代码放入 ECU,ECU 内部可以有 Bootloader,但不可以有擦写内部代码的功能,擦写代码的功能称作 NVM (None Valitale Momory–非易失性存储器) 驱动程序。

在这里插入图片描述

在这里插入图片描述

主机将 NVM 驱动程序下载到 RAM 区域,用 NVM 驱动程序对内部 NVM 进行擦除写入新数据等,在最后跳转即可完成更新。

UDS 服务设计复杂,Bootloader 升级一般分为以下三步:

1、预编程:主要进行一些环境配置

2、编程:刷写过程

3、刷新完成:恢复配置

Bootloader 可以保证在上述三个阶段任一问题出现都能再次进入该过程重新刷新。

1、预编程阶段

在进入刷新之前,UDS 的 85 服务和 28 服务,关 DTC 和非诊断报文。使整个 CAN 网络处于安静的状态。这是对整车网络进行操作的,一般都是以功能寻址的方式来发送。注意先用 85 服务关闭 DTC,再使用 28 服务关报文。通常都会关闭故障码保存,关闭 CAN 通信是为了加快刷写速度,过程如下:

在这里插入图片描述

2、编程阶段

UDS 设计了安全访问功能,安全访问是为了保证 ECU 数据的安全,实现方式是由 ECU 发送一个种子到主机,主机通过 dll 文件算法算出结果与 ECU 算出结果进行比对,结果一致则解锁成功通过安全验证。ECU 解锁可以存在多个等级。

写时候先写 DID 指纹,标记写软件人的身份(按照主机厂要求),擦写下载等操作。

在这里插入图片描述

3、编程结束

刷写完成之后,ECU 进行重启,重新进入扩展会话,打开之前关闭的配置即可。

在这里插入图片描述

总体流程

在这里插入图片描述

Bootloader 开发难点分析

硬件初始化

当硬件上电之后,运行的第一条指令就是 Bootloader 的代码(如果各式各样的 BIOS 忽略不计的话)。Bootloader 需要完成硬件初始化的一系列工作。然后才可以进入正常的逻辑,例如加载 OS Image 等。对于软件工程师而言,硬件的初始化工作是很冗长乏味的。需要详细阅读各类硬件的资料规范。然后就是一系列的对寄存器的操作。虽然 Bootloader 中不需要初始化板子上的所有的硬件,而只需要初始化最基本的可以让 Loader 正常工作的硬件就可以,有一些外设可以放到 OS 启动的时候,甚至驱动加载的时候再进行初始化不迟。即使是这样,要初始化的硬件也不在少数,对于一个典型的 ARM 系统而言,有可能要做的事:初始化内存控制器,初始化 MMU,配置 GPIO 口,配置调试串口,对 RTC 进行读写操作,如果要通过以太网下载,还需要驱动网络接口…… 这一系列的工作,没有一个不是体力活。需要细心的琢磨,很有可能对寄存器某一个 bit 的粗心设置,就会导致整个 Bootloader 无法工作。所以这一部分内容通常马虎不得,需要耐心完成。

代码编写和构建

由于 Bootloader 是最底层的代码,汇编语言肯定是少不了的了。就连一些整天喊 “汇编已死” 的人也不能否认,系统启动的那一段代码,还是需要汇编语言的。而汇编语言通常也是软件工程师们不太希望去碰的 “硬骨头”。这也增加了 Bootloader 编写的困难。

代码写好之后,当然要编译成机器码才可以在板子上运行。目前的编译器大多数只会把代码编译成某些流行的可执行文件格式,例如 Windows 上的 PE 和 * Nix 上的 ELF 等。这些带有格式的可执行文件,也没有办法再目标设备上直接运行。所以,通常 OS 都会提供一些工具,把这些可执行文件去头去尾,转成纯二进制格式,这样才可以在目标设备上运行。例如 ADS 提供的 FromELF 工具与 Windows CE 提供的 ROMImage 工具就是完成这类工作的。通常,我们需要为这类工具做一些配置,例如告诉这些工具代码段放在什么地方,起始地址是多少等等。如果这些参数没有配置正确,很有可能最终生成的 Bootloader 的映像是不可用的。那么烧写到目标设备上,自然也就无法运行。同样,对于这些参数的配置,也需要仔细检查核对,一步步进行。

开发的效率

Bootloader 的另外一个开发困难的原因是它的开发效率。通常当我们做了一些代码修改之后,都需要把修改后的二进制文件使用烧写工具烧写到目标设备的 Flash 中,无论是 NAND 还是 NOR Flash,烧写的过程都不快。所以,即使是改了一行代码,也需要经过编译 -> 烧写 -> 运行这样一个完整的流程。一般而言再快也要 10 分钟左右。这样算算,一个钟头可以修改个 5 次代码,一天可以修改个 50 次代码就相当不错了。机械的重复这一过程,经常会使开发人员感到开发效率低下,从而产生反感和抵触情绪。这也是 Bootloader 开发的一大劣势。一个解决的方法是使用硬件调试工具把 Bootloader 的映像直接灌到 RAM 里面运行,往 RAM 里面灌通常比烧写 Flash 要快。但是这样需要调试工具来初始化 RAM,又有很多的其他逻辑上的和细节上的事情要做。

调试

上面说的几大问题,其实还都是可以克服的问题。其实在我看来,开发 Bootloader 最大的问题还是调试问题。试想:无论汇编多么难,我还是写好了,无论烧写多么烦,我还是烧写下去了,但是当我怀着激动的心情按下 Reset 键的时候,整个硬件设备毫无反应。我怎么知道我的代码写的正确还是不正确呢?如果不正确,我又怎么能定位到我的错误呢?现在的软件开发中,无论是编译型语言还是 script,一般都会提供相应的 Debugger,让开发人员来定位代码错误。“摸黑” 写代码是软件工程师们最害怕的事情。代码出了问题,如果没有行之有效的手段来做问题定位,十有八九会造成项目 “卡壳”。如果定位准确,那么问题也就解决了一大半了。所以归根结底,还是调试的方法论问题。Bootloader 中难以调试,是因为可以使用的手段非常少,也不常规。在 OS 下开发应用程序用到的那些调试手段手段,在 Bootloader 的开发中通常都用不上。需要有 “非常” 手段来调试。下面的专题,就向大家介绍一些 Bootloader 的常用调试方法。

一些调试技巧

(1) 硬件调试器

相信很多从应用开发走过来的开发人员,都会对 Set Breakpoint,Step into, Step over 等这些调试手段相当怀念。但是那些东西都是调试器在搞鬼。到了裸板上,可没有 Debugger 来帮我们了。那怎么办?好在我们有硬件调试器。很多 CPU 体系结构都提供了相应的硬件调试器,例如 ARM CPU 的仿真器。借助硬件调试器,我们可以完成汇编级的 Set Breakpoint,Step into, Step over 这些操作。这对于调试 Bootloader 来说,实在是太重要了。至少我们可以看到我们烧写下去的代码是否正确,然后可以看到是否在运行。这对于调试 Reset 后的第一段代码来说,实在是雨后春笋一般的珍贵。

但是硬件调试器也有很多不足:首先,它们一般都价格不菲,不用国内自己的 D 的话,几 W 是少不了了。其次,大多数硬件仿真器只能实现汇编级的调试,当代码进入了 C 语言之后,硬件调试器就显得力不从心了。第三,一劣质的仿真器,通常还无法对 MMU 打开后的虚拟内存进行仿真。所以限制也是很多的。

(2) LED 灯

如果我们买不起硬件仿真器,或者手头上根本就没有。那么只好通过硬件的一些输出端口来输出一些信息,让我们知道代码到底运行到哪里了。在诸多的端口中,最简单的恐怕就是 LED 灯了。一般而言,用个几句汇编,就可以让 LED 闪烁起来。这样,我们就可以在代码中安插让 LED 闪烁的语句。来看我们的代码到底在哪里跑飞或者挂掉的。可惜 LED 等只能闪烁,想通过 LED 来获得更多的消息,是很困难的。例如我们想知道某个变量的值,用 LED 的闪烁表示出来,恐怕就很伤脑筋。用 LED 来闪烁出摩尔斯代码是一种可行方案,但是除了开开玩笑之外,恐怕真的去实践的人是不存在的

(3) Printf―调试串口

目前为止,在 Bootloader 甚至整个 OS 开发过程中,串口可能是使用最广泛的调试端口了。它的设备简约却不简单。用一根串口线,把 PC 跟目标设备连接起来,这样我们可以通过串口来输出一些字符,在 PC 一端接收。终于,在有 OS 的应用程序开发中最传统的,最原始的调试方法 printf 就可以用了。在 Bootloader 的开发中,如果可以通过串口输出调试信息,那调试的手段就前进了一大步了。至少我们可以通过 printf 来跟踪目前代码在哪个函数里,分支语句走了哪个流程,某个变量的值到底是不是我们想要的…… 世界真美好。串口的问题是它不像 LED 那样想用就用,还是需要初始化的。好多嵌入式 CPU 都把串口控制器集成到了里面,甚至有一些会在 CPU 上电的时候自动初始化串口,这使得串口初始化相对简单。但是如果有一些串口是外挂的控制器,那么初始化一个串口有可能也需要耗费你半天时间。

(4) 内核调试器

随着开发进一步复杂,串口输出调试信息恐怕又不能满足我们的要求了。首先,输出的信息一多,很容易乱套。其次,输出信息业是需要花费时间的。在中断处理函数等一些时间敏感的地方使用串口输出,有时候依然不明智。如果能把远程调试器接上,那对 BootLoader 的调试跟对应用程序的调试就没有什么二致了(源码级设置断点,一步步跟,随时看某些数据结构的值,都成了可能)。Linux 提供了 GDB,Windows CE 提供了 KD。但是这都需要实现相当多的工作,实现一些调试器 Stub。GDB 可能串口就可走。但 CE 的 KD 通常是要通过 KITL,通过以太网的。虽然复杂,长远来看还是非常有必要的,所以,当感觉其它手段力不从心的时候,可以考虑启用更高级别的调试器了。

实例

以 NXP 推荐设计简单讲解原理。NVM 驱动 NXP 官方提供完整的实现函数库,不需要自行实现,实际开发中也是如此,大部分是对工具的使用,而不是从零开始开发功能。

使用脚本文件抽取指定 ECU 的 NVM 驱动代码,全部代码是以 C 语言 const 数组存储。

将 NVM 驱动函数地址存储在指定地址作为 NVM 驱动函数地址表。

因为全部使用 const 关键词修饰,全部存储在常量区域,只读数据段,修改链接文件将只读数据段固定到 RAM 设定地址,则 NVM 驱动编译后存储在 RAM 区域,生成 S19 文件。

从得到的 S19 文件分离出 NVM 驱动和 NVM 驱动函数地址表的 S19 文件,称为 NVM 驱动 S19 文件

将 NVM 驱动 S19 文件与应用 S19 文件合并生成完整的 Bootloader。NVM 驱动 S19 文件需要保持在整个文件的开始,以保证系统能够正常运行找到 NVM 驱动。

NVM 驱动是 NVM 独立驱动是灵活可裁剪的。因此可以根据 Bootloader 的功能选择必要的 NVM 驱动函数,从而减少其占用的 RAM 空间,以适应小 RAM 尺寸的 ECU (比如 1KB RAM 的 ECU 系列), 当然还需要改变其编译地址和 NVM 驱动函数映射地址表。

结语

虽然这是一个比较复杂的问题,在分析问题时,将问题分解,比如,整个 Bootloader 分为通信、存储等,梳理过原理之后,可以预测到代码实现逻辑,再追踪定位,验证预测。为保证安全提出并实现了一种基于总线通信将 NVM 驱动程序由上位机下载到 RAM 中运行而非让其驻留于 ECU 片上 FLASH 的安全 Bootloader 设计,有效避免了应用程序跑飞运行至驻留于片上 FLASH 的 NVM 驱动代码所造成的程序 / 数据丢失失效。但对于汽车上不是一朝一夕能实现的,虽然单一功能简单,为了保证这个功能而设计很多框架用来保证,框架是各种协议的实现,难度极大,需要长时间积淀才能理解其中奥义。


via:

  • What is Grub in Linux? What is it Used for?
    https://itsfoss.com/what-is-grub/

  • How to Install Grub Customizer on Ubuntu
    https://itsfoss.com/install-grub-customizer-ubuntu/

  • What Is a Bootloader? How Does a Bootloader Work?
    https://www.makeuseof.com/what-is-a-bootloader/

  • Bootloader: What is it and how does it work? - IONOS
    https://www.ionos.com/digitalguide/server/configuration/what-is-a-bootloader/

  • UEFI vs Legacy BIOS Boot | GPT vs MBR (DOS) | Explained - YouTube
    https://www.youtube.com/watch?v=4Byg1MReCKI

  • Computer Programming for Beginners | Firmware and BIOS | Ep9 - YouTube
    https://www.youtube.com/watch?v=dvuY0ouG1IU

  • Bootloader 详解,理解 Bootloader 看这篇就够了 - CSDN 博客
    https://blog.csdn.net/iduuigdg/article/details/122144655

  • 浅谈 ECU Bootloader-CSDN 博客
    https://blog.csdn.net/baobingji/article/details/123119354

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/978158.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

java高级(IO流多线程)

file 递归 字符集 编码 乱码gbk&#xff0c;a我m&#xff0c;utf-8 缓冲流 冒泡排序 //冒泡排序 public static void bubbleSort(int[] arr) {int n arr.length;for (int i 0; i < n - 1; i) { // 外层循环控制排序轮数for (int j 0; j < n -i - 1; j) { // 内层循环…

Dubbo RPC 原理

一、Dubbo 简介 Apache Dubbo 是一款高性能、轻量级的开源 RPC 框架&#xff0c;支持服务治理、协议扩展、负载均衡、容错机制等核心功能&#xff0c;广泛应用于微服务架构。其核心目标是解决分布式服务之间的高效通信与服务治理问题。 二、Dubbo 架构设计 1. 核心组件 Prov…

普中单片机-51TFT-LCD显示屏(1.8寸 STM32)

普中官方论坛&#xff1a; http://www.prechin.cn/gongsixinwen/208.html 普中科技-各型号开发板资料链接&#xff1a;https://www.bilibili.com/read/cv23681775/?spm_id_from333.999.0.0 27-TFTLCD显示实验_哔哩哔哩_bilibili 2.程序烧录 2.1设置彩屏驱动 3.实验效果

Starlink卫星动力学系统仿真建模第九讲-滑模(SMC)控制算法原理简介及卫星控制应用

滑模控制&#xff08;Sliding Mode Control&#xff09;算法详解 一、基本原理 滑模控制&#xff08;Sliding Mode Control, SMC&#xff09;是一种变结构控制方法&#xff0c;通过设计一个滑模面&#xff08;Sliding Surface&#xff09;&#xff0c;迫使系统状态在有限时间内…

nss刷题5(misc)

[HUBUCTF 2022 新生赛]最简单的misc 打开后是一张图片&#xff0c;没有其他东西&#xff0c;分离不出来&#xff0c;看看lsb&#xff0c;红绿蓝都是0&#xff0c;看到头是png&#xff0c;重新保存为png&#xff0c;得到一张二维码 扫码得到flag [羊城杯 2021]签到题 是个动图…

【C/C++】删除链表的倒数第 N 个结点(leetcode T19)

考点预览&#xff1a; 双指针法&#xff1a;通过维护两个指针来一次遍历链表&#xff0c;避免了多次遍历链表的低效方法。 边界条件&#xff1a;要特别处理删除头结点的情况。 题目描述&#xff1a; 给你一个链表&#xff0c;删除链表的倒数第 n 个结点&#xff0c;并且返回…

人工智能定义

一、人工智能核心概念体系 1.1 人工智能的本质 人工智能的定义:人工智能(Artificial Intelligence,简称 AI)是指计算机系统能够执行通常需要人类智能才能完成的任务,如学习、推理、解决问题、理解自然语言、识别图像和声音等。它通过模拟人类的智能行为,运用算法和数据…

量子计算的威胁,以及企业可以采取的措施

当谷歌、IBM、Honeywell和微软等科技巨头纷纷投身量子计算领域时&#xff0c;一场技术军备竞赛已然拉开帷幕。 量子计算虽能为全球数字经济带来巨大价值&#xff0c;但也有可能对相互关联的系统、设备和数据造成损害。这一潜在影响在全球网络安全领域引起了强烈关注。也正因如…

0—QT ui界面一览

2025.2.26&#xff0c;感谢gpt4 1.控件盒子 1. Layouts&#xff08;布局&#xff09; 布局控件用于组织界面上的控件&#xff0c;确保它们的位置和排列方式合理。 Vertical Layout&#xff08;垂直布局&#xff09; &#xff1a;将控件按垂直方向排列。 建议&#xff1a;适…

【Uniapp-Vue3】导入uni-id用户体系

在uniapp官网的uniCloud中下载uni-id用户体系 或者直接进入加载&#xff0c;下载地址&#xff1a;uni-id-pages - DCloud 插件市场 进入以后下载插件&#xff0c;打开HbuilderX 选中项目&#xff0c;点击确定 点击跳过 点击合并 右键uniCloud文件夹下的database文件夹&#x…

如何免费使用稳定的deepseek

0、背景&#xff1a; 在AI辅助工作中&#xff0c;除了使用cursor做编程外&#xff0c;使用deepseek R1进行问题分析、数据分析、代码分析效果非常好。现在我经常会去拿行业信息、遇到的问题等去咨询R1&#xff0c;也给了自己不少启示。但是由于官网稳定性很差&#xff0c;很多…

VSCode+PlatformIO报错 找不到头文件

如图示&#xff0c;找不到目标头文件 demo工程运行正常&#xff0c;考虑在src文件夹内开辟自己的代码&#xff0c;添加后没有找到 找了些资料&#xff0c;大概记录如下&#xff1a; 1、c_cpp_properties.json 内记录 头文件配置 .vscode 中&#xff0c;此文件是自动生成的&a…

Python 网络爬虫实战全解析:案例驱动的技术探索

Python 网络爬虫实战全解析&#xff1a;案例驱动的技术探索 本文围绕 Python 网络爬虫展开&#xff0c;深入剖析其技术要点&#xff0c;并通过实际案例演示开发流程。从爬虫原理引入&#xff0c;逐步讲解如何使用 Python 中的requests和BeautifulSoup等库进行网页数据抓取与解…

List(3)

前言 上一节我们讲解了list主要接口的模拟实现&#xff0c;本节也是list的最后一节&#xff0c;我们会对list的模拟实现进行收尾&#xff0c;并且讲解list中的迭代器失效的情况&#xff0c;那么废话不多说&#xff0c;我们正式进入今天的学习 list的迭代器失效 之前在讲解vec…

在zotero里部署papaerschat插件,以接入现有大模型

papaerschat插件里集成了openAI的GPT3.5、gpt-4o、gpt-mini大模型以及Claude3、Gemini、Deepseek等大模型。通过接入这些大模型可以辅助我们阅读论文。以部署方式如下&#xff1a; 1.下载zotero的插件市场&#xff0c;用以管理zotero里的插件。下载地址&#xff1a; https://…

Memory Programming ...Error: File does not exist: Max.hex

Memory Programming ... Error: File does not exist: Max.hex 原因 删了确定就可以了

渗透测试【seacms V9】

搭建seacms环境 我选择在虚拟机中用宝塔搭建环境 将在官网选择的下载下来的文件解压后拖入宝塔面板的文件中 创建网站 添加站点 搭建完成seacmsV9 找到一个报错口 代码分析 <?php set_time_limit(0); error_reporting(0); $verMsg V6.x UTF8; $s_lang utf-8; $dfDbn…

仅需三分钟,使用Vue3.x版本组件式风格实现一个消息提示组件!

一、前言 在日常的前端项目开发中&#xff0c;我们时常需要使用到“消息提示”&#xff08;以下简称“消息”&#xff09;这个组件来帮助我们更好的给予用户提示&#xff0c;例如常见的“登录成功”、“操作成功”、“服务器异常”等等提示。 尽管市面上已经有一些组件库提供了…

敏捷开发实践指南:从理论到落地的全面解析

敏捷工程&#xff1a;现代软件开发的变革与实践 近年来&#xff0c;软件工程领域经历了从传统瀑布模型到敏捷开发的深刻转变。这种转变不仅是技术方法的升级&#xff0c;更是团队协作、需求管理和交付模式的革新。本文将从敏捷开发的核心理念、主流方法、实践案例及未来趋势等…

期权帮|股指期货基差和价差有什么区别?

锦鲤三三每日分享期权知识&#xff0c;帮助期权新手及时有效地掌握即市趋势与新资讯&#xff01; 股指期货基差和价差有什么区别&#xff1f; 一、股指期货基差 股指期货基差是指股指期货价格与其对应的现货指数价格之间的差额。 股指期货基差计算公式&#xff1a;基差 现…