Raspberry Pi 4 Model B Compiles 64-bit Kernel

Keywords: Linux sudo Ubuntu github

The raspberry pie system installed by default is ARM32-bit system, but since raspberry Pie 3 supports ARM64-bit system, since the official does not give 64-bit system, we have to compile it ourselves.

Actually, there are many 64-bit compiler cores of raspberry pie 3B on the internet. I tried them all, but they can't boot up, nor are they written incorrectly. They just don't seem to be suitable for me. They made me sad for three or four days. So the next steps don't guarantee that everyone can succeed because everyone's situation is different. The problem is different, so the problem can only be solved by Baidu or Google.

The following is only to turn the kernel into 64-bit, I feel that the root file system should also be compiled to 64-bit, then I will study how to compile 64-bit root file system.

Notes are unavoidable to be omitted when they are recorded. If there is any wrong step, please leave a message and revise it in time.

Compilation environment

Through the virtual environment Linux installed by the vm virtual machine, how to install a search on the Internet, follow the steps to install.

Compiler: Ubuntu 18.04-amd64

Cross Compiler

Generating 64-bit Kernel by Cross Compiler

Definition: Cross compiler is a compiler that generates executable files from one system platform to another.

My interpretation of this sentence is as follows:

  • Ubuntu 18.04-amd64: Customized garment processing plant (a system platform)

  • Cross-compiler: Machine for making clothes (compiler)

  • linux source code (kernel): clothing materials (source code of executable files)

  • Raspberry pie 4B: Obese people (another platform)

With the above, now I want to make a dress for obese people, so I need to find a factory specializing in making clothes for obese people. After giving clothing materials to the factory, the clothes made by the clothes making machine can be worn by obese people.

Everyone understands differently. Just remember the definition of the above major. How do you understand it according to your own way?

Pre-compile preparation

As defined above, we all need to get them before we can make them; Ubuntu must be installed first; and raspberry pies must be ready.

You can use ordinary users, try not to use root users to execute, and when you need to use root users, then switch to root users to execute.

  • Get the cross compiler and configure it

Linaro Toolchain

Download gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu.tar.xz from the link above. It is possible to update it, and the date and version will change.
Or download by command

$ sudo wget https://releases.linaro.org/components/toolchain/binaries/latest-7/aarch64-linux-gnu/gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu.tar.xz
$ mkdir ~/build
$ sudo apt-get install lrzsz
# Upload and unzip downloaded files
$ rz
$ sudo tar -xvf  gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu.tar.xz -C /usr/src/
$ cd /usr/src/ && sudo mv gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu aarch64-linux-gnu
$ sudo vi /etc/profile
# Add the following at the end
export ARCH_HOME=/usr/src/aarch64-linux-gnu
export PATH=$PATH:$ARCH_HOME/bin

Note: If it's for ordinary users, you need to switch to root and configure it again. Ordinary users also need to configure it.

  • Get Raspberry Kernel source code

Raspberry's official GitHub download is extremely slow. I migrated Raspberry Linux to the domestic code cloud so that it can be downloaded quickly. The difference is that the official update, not the official update. Update to my code cloud warehouse. For the latest code, you can first transfer the official fork to your github, then migrate to your own code cloud, and change the link to your own. The following command can be downloaded by choosing a download.

# Official github address
$ git clone --depth=1 --branch rpi-4.19.y https://github.com/raspberrypi/linux
# The address of code cloud
$ git clone --depth=1 --branch rpi-4.19.y https://gitee.com/nzwxl/linux
  • Dependencies required to install a compiler environment
$ sudo apt-get install git bison flex libssl-dev zip

Compilation begins

# If the source file is not called linux, you can change mv to Linux or the name you want
$ cd linux
# To compile. config, ARCH should be configured as arm64. If not, it defaults to x86 of the developer. CROSS_COMPILE specifies the compiler.
# Bcm2711_defconfig in arch/arm64/configs/bcm2711_defconfig, it will find the file itself according to Makefile
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig
# Start tailoring the kernel on the basis of the current. config. If you don't want to tailor it twice by ESC, it's OK. I don't know much about it. I'll remember it when I know it.
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
# Start compiling the kernel. The parameter - J 5 means compiling the kernel simultaneously with a multiprocessor. The maximum number is: the number of cpu cores * 1.5, which can be modified by itself.
make -j5 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-

If there is no error in the middle, the Image binary kernel file will be generated in the directory arch/arm64/boot in about an hour.

Replacement of Kernel Files

There are two ways to replace the kernel files. The official way can be replaced according to [1] in the reference. I will record the second way to replace.

.img download

Download Raspbian Buster Lite in the link above and upload the downloaded image file to the working directory, which is a 32-bit root file system.

$ cd .. && rz
$ pwd
~/build
$ sudo unzip 2019-07-10-raspbian-buster-lite.zip && mv 2019-07-10-raspbian-buster-lite.img raspbian-lite.img
# View the. img file
$ fdisk- l raspbian-lite.img
Disk raspberry-lite.img: 2 GiB, 2197815296 bytes, 4292608 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x17869b7d

Device              Boot  Start     End Sectors  Size Id Type
raspberry-lite.img1        8192  532480  524289  256M  c W95 FAT32 (LBA)
raspberry-lite.img2      540672 4292607 3751936  1.8G 83 Linux

The last two above are key. Change the following numbers according to your own mirror file.

Mount the two partitions above, starting with the root partition

$ mkdir mnt
$ sudo mount -o loop,offset=276824064 raspbian-lite.img mnt

offset depends on sector size (512): 276824064 = 512 * 540672

Then start the partition:

$ sudo mount -o loop,offset=4194304,sizelimit=268435968 raspbian-lite.img mnt/boot

offset : 4194304 = 512 * 8192,sizelimit: 268435968 = 512 * 524289

Run the following command to install the module and replace the corresponding files

$ su root
$ cd linux
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- INSTALL_MOD_PATH=../mnt/ modules_install
$ cp arch/arm64/boot/Image ../mnt/boot/kernel8.img
$ cp arch/arm64/boot/dts/broadcom/*.dtb ../mnt/boot/
$ cp arch/arm64/boot/dts/overlays/*.dtb* ../mnt/boot/overlays/
$ cp arch/arm64/boot/dts/overlays/README ../mnt/boot/overlays/
$ vim ../mnt/boot/config.txt

# Exit save after adding the following

# Mandatory 64-bit read of the kernel https://www.raspberrypi.org/documentation/configuration/config-txt/misc.md
arm_64bit=1
# To start in ARMV8 mode, set this option https://elinux.org/RPiconfig to last
arm_control=0x200
# The name of the kernel to load
kernel=kernel8.img
# u-boot boots kernel s with a delay of several seconds
boot_delay=1

$ touch ../mnt/boot/ssh
$ sudo umount ../mnt/boot && sudo umount ../mnt

Download raspbian-lite.img to Windows and burn it to SD card. The way of burning SD card can be referred to:

After starting Raspberry, check the 64-bit version of the kernel by command

Reference resources

Posted by Syranide on Tue, 27 Aug 2019 02:51:08 -0700