One of the simple operating systems for Raspberry Pi: Transplanting Raspberry Pi 4B U-boot and loading bare-metal programs

1. Background

The first step in developing a bare-metal program with the Raspberry Pi is to copy the compiled program into the memory of the Raspberry Pi before allowing it to execute. Regarding the startup process of the Raspberry Pi, this article will not introduce it in detail. I will update the blog or find better bloggers on the Internet for reference when I have time. This article is only a brief introduction, and there are omissions and mistakes. Welcome to add corrections.

1.1 Traditional methods

First of all, you need to prepare a TF card that saves the Linux system that the Raspberry Pi can start normally (it will be divided into two partitions, the boot partition stores the kernel, device tree and some configuration files required by the normal system, usually the fat partition? windows The host can recognize it. The other partition is filesystem? It is the root file system of the system and is in ext4 format. Usually windows cannot recognize it normally, but Linux hosts can. Remember not to listen to the Windows system to format it, otherwise the TF card will be inserted into the Raspberry Pi no root filesystem).
In terms of the startup process, the Raspberry Pi itself has a solidified boot program, which will be executed through VideoCore after the Raspberry Pi is powered on. This program will read the config.txt (very important) file in the boot partition, according to The content of the file is used to initialize the configuration of the system, and finally the loaded program is selected by reading the value of the kernel in the config.txt file and then transferred to execution. (Usually the value here or the default value is the kernel of the Linux system).
How to transfer the bare-metal program we wrote to the development board, the traditional method is to first copy the program file to the TF card, by modifying the config. The value corresponding to the variable modifies it to point to the binary of our bare metal program.
The advantage of the traditional method is that it is simple, you only need to change the config.txt file once, and then you only need to copy the compiled binary program to the TF card, and then power on, the Raspberry Pi will load our program according to the instructions and execute it. But the shortcomings are also obvious. If we develop a bare-metal program, then we cannot avoid modifying and debugging the program. It is very painful to copy the compiled program to the TF card continuously. Not only does the card reader need to copy the data, but also the TF card needs to be plugged and unplugged continuously on the Raspberry Pi. Not to mention the waste of time, it will also cause physical damage to the slot of the Raspberry Pi (after all, the Raspberry Pi is increasingly In short supply, it will become a wealth management product), and the file system in the TF card may be damaged if the number of times is too high at the same time (the author has been damaged by the file system, and it is troublesome to rebuild the TF card that can be used for the Raspberry Pi system ).

1.2 Method via Uboot

Uboot itself is a boot loader. The principle of this method is as follows. After the system is initialized and booted, enter the uboot program. Uboot will have a simple command line through the serial port, so that the binary program can be loaded from the remote end through the network tftp tool, and then execute the command to execute our compilation go. Out of the binary bare-metal program.
The advantage of this is that you only need to pull out the TF card once, put our u-boot binary program into the TF card, and modify the kernel variable in config.txt to point to u-boot, and then plug it into the Raspberry Pi, you can pass The serial port controls uboot to complete a series of operations. Merit in the present age, benefit in the future.

2. Steps

If you just want to load the bare-metal program, you can choose the uboot I compiled . If you need to learn the uboot transplantation steps, you can refer to the following steps.

2.1 Compile U-boot

The following is executed on a Linux host (Ubuntu, Debian, etc.)

2.1.1 Installing the cross compiler

A cross-compiler, also known as a cross-compilation toolchain, as the name implies, is a program that can be compiled on one CPU architecture and executed on another architecture. For example, in this example, we compile a program that can run normally on arm64 on x86. Regarding how to install the cross-compiler, there are many ways on the Internet. For example, the simplest method on the ubuntu system is apt install gcc-aarch64-linux-gnu cpp-aarch64-linux-gnuto complete the installation of the cross-compiler by executing it. However, based on the principle of learning, we choose to use Cross-ng to build a cross-tool chain suitable for Raspberry Pi ARM64. The installation method and the problems encountered can also refer to: Crosstool-ng compile and build cross compiler errors and solutions

1) Get the crosstool

Execute the "git clone git://crosstool-ng.org/crosstool-ng" command

2) Install the necessary components

$ sudo apt update
$ sudo apt install  bison flex gawk automake libncurses5-dev python3-dev autoconf help2man make libtool libtool-bin -y
$ sudo apt install texinfo
$ sudo apt install bzip2

3) Compile and install cross-ng-master

$ ./bootstrap
$ ./configure 
$ make
$ sudo make install

4) Configure and install the toolchain

Modify to the default configuration of Raspberry Pi 4

$ ct-ng aarch64-rpi4-linux-gnu

At the same time, you can execute the "ct-ng menuconfig" command to enter the configuration page, and open "Paths and misc options" in the configuration interface, as shown in the figure: enter the
insert image description here
picture description where "Local tarballs directory" indicates the source package download path required in the construction tool chain , "Working directory" is the path of the intermediate files during the construction process, and "Prefix director" indicates the installation path of the cross compiler after the construction is completed. Can be configured according to their own needs. Finally execute the following command to build the compiler

$ ct-ng build

Environment Configuration After installing the toolchain, the path of the toolchain command is generally added to the system path to facilitate subsequent calls.

$ export PATH=$PATH:/home/dengml/crosstool/x-tools/aarch64-rpi4-linux-gnu/bin

Among them, /home/dengml/crosstool/x-tools/aarch64-rpi4-linux-gnu/bin is the address of the cross compiler compiled and installed by myself. In order to avoid configuring the path every time you start up, you can configure the environment to automatically configure it every time you start up. The configuration method is as follows:

$ vim /etc/profile

Add in it:

export PATH=$PATH:/home/dengml/crosstool/x-tools/aarch64-rpi4-linux-gnu/bin

to validate

source profile

2.1.2 Obtain and compile U-boot source code

git clone https://github.com/u-boot/u-boot (下载源码)
cd u-boot
# 使用刚才下载的交叉工具链
export CROSS_COMPILE=aarch64-linux-gnu-(目的是指向编译器)
make distclean (清除之前编译源码的缓存,首次执行会很快,因为没有缓存)
make rpi_4_defconfig (选择树莓派4的默认配置)
make -j4 (编译)

Compilation common problems and solutions
Q: include/image.h:1394:12: fatal error: openssl/evp.h: No such file or directory
A: sudo apt install libssl-dev
After compiling, a file will be generated in the uboot directory Binaries for u-boot.bin

2.2 Change the Raspberry Pi configuration to enter U-boot

Cp the u-boot.bin file generated in Section 2.1 to the boot partition of the TF card that can start the Raspberry Pi system normally, and modify the content of config.txt to the following:

# Run in 64-bit mode
arm_64bit=1

[cm4]
# Enable host mode on the 2711 built-in XHCI USB controller.
# This line should be removed if the legacy DWC2 controller is required
# (e.g. for USB device mode) or if USB support is not required.
#otg_mode=1

[all]
kernel=u-boot.bin
enable_uart=1
core_freq = 500

2.3 Connect the serial port

This article uses a USB-to-ttl serial port controller. The connection method is that the host side is connected to the host machine through USB, and the other end is connected to the Raspberry Pi with the serial port cable as shown in the figure below (it is best to connect the ground wire, the signal is stable).
raspberry_gpio

Open mobaxterm on the windows host (you need to install the driver of the serial port tool in advance), select the serial port and configure the baud rate to 115200. On ubuntu or other Linux system hosts, it is recommended to use minicom, and you can perform initial configuration on the serial port through commands, such minicom -Sas Modify the baud rate to 115200, and the default serial device, and then enter minicom -D /dev/ttyUSBxthe serial console of the board (/dev/ttyUSBx is the name of the serial controller device, determined according to the actual situation). After U-boot starts, you can see the log on the serial console,
(to do)

2.4 Build tftp server

**What needs to be noted here is that a tftp server needs to be built, not an ftp server. **There are many online tutorials on how to build a tftp server on ubuntu. Here we mainly introduce how to build a tftpd server on a windows host using the Tftpd32 tool. If you encounter a network firewall that causes access to other machines to fail, you can close it first.
Download the program on the tftpd32 official website , download portablethe green free installation version, unzip it and open it to run, as shown in the figure below:
(todo)
This tool is a small file sharing tool that can support multiple functions, such as completing the server as a tftp file transfer Or the client, here we only serve as the tftp server, you can configure the path where the tftp server stores the server-side files on this interface. Uboot loads the program from the windows host to the Raspberry Pi ram through the network.

2.5 Configure uboot environment

Earlier we talked about how to compile and enter the uboot console through the serial port. Now we need to configure uboot to load the program on the host computer to the Raspberry Pi through the serial port.

  1. First of all, you need to connect the network cable to the Raspberry Pi. It should be noted that the network connected to the Raspberry Pi needs to be in the same LAN as the host (directly connected or connected to the same router).
# 设置树莓派的ip地址
setenv ipaddr 192.168.1.101
# 设置路由网关
setenv gatewayip 192.168.1.1
# 设置子网掩码(与宿主机一致)
setenv netmask 255.255.255.0
# 设置宿主机(windows主机)ip,本例中为192.168.1.6
setenv serverip 192.168.1.6
# 保存配置信息
saveenv

The above commands set the serial port of the console, set the path of the root file system mounted on the network, set the ip address, server address, gateway, subnet mask, network device number, etc. After the configuration is complete, you can see Saving Environment to FAT… OKthat the configuration is successful. At this time, you can use ping 192.168.1.6the command to check whether it is connected to the host.
We can tftp <树莓派的RAM地址> <文件名>get the file by , the meaning of the command is to load the specified file in the path set by the host tftp server to the determined address on the Raspberry Pi RAM. The startup command of u-boot can be set by the following command

setenv bootargs "tftp 0x80000 kernel8.img; go 0x80000"
saveenv

Through the above method, the file named kernel8.img can be automatically loaded from the remote end after u-boot is powered on, and placed in the RAM location of 0x80000. Then u-boot jumps to the location where the program is loaded for execution.
Through the above method, when we debug our bare-metal program, we can directly put the compiled binary program (I named it kernel8. It can automatically load the program to the specified address and execute it. As shown below:
(todo)

3. Description

If there are any omissions or mistakes, welcome to correct them. In addition, if you want to know or need to supplement the content or knowledge in this area, although I may not know it, welcome to communicate and learn together.

Subsequent supplements, to be continued.

Guess you like

Origin blog.csdn.net/weixin_43328157/article/details/130241572