Try   HackMD

Raspberry PI + cross compile & build kernel

先準備ubuntu 18 (64bit版)


2024版本

  • 現在可以無人值守安裝了!! 不過用了這個反而會遇到奇怪的問題, 重新安裝 + 略過無人值守突然就好了
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

編譯檔案

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


2022版本

  1. 剪貼簿設定 (可以複製貼上指令)

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  2. Install VirtualBox Guest Additions

    ​​​​sudo apt update
    ​​​​sudo apt upgrade
    ​​​​sudo apt install build-essential dkms linux-headers-$(uname -r)
    
  • 重開機後, 再執行 Insert Guest Additional CD image
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    • 裝完之後, 再重開機
  1. Install Required Dependencies and Toolchain
    sudo apt install git bc bison flex libssl-dev make libc6-dev libncurses5-dev
  • Install the 64-bit Toolchain for a 64-bit Kernel
    sudo apt install crossbuild-essential-arm64
  • Get the Kernel Sources
    git clone --depth=1 https://github.com/raspberrypi/linux
  1. Load 64-bit default configs (For Raspberry Pi 3, 3+, 4, 400 and Zero 2 W, and Raspberry Pi Compute Modules 3, 3+ and 4:)

    ​​​​cd linux
    ​​​​KERNEL=kernel8
    ​​​​make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig
    
  2. Make image for all 64-bit Builds
    make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image modules dtbs

    • 第一次跑大約要1小時 (看電腦的性能)
  3. 把micro sd卡連接到虛擬機裡面, 然後掛載磁區

mkdir mnt
mkdir mnt/fat32
mkdir mnt/ext4
sudo mount /dev/sdb1 mnt/fat32
sudo mount /dev/sdb2 mnt/ext4
  1. back up old kernel
    cp -rf /media/xd/boot/* Desktop/boot_PI/

  2. install the kernel modules onto the SD card (For 64-bit)
    sudo env PATH=$PATH make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- INSTALL_MOD_PATH=mnt/ext4 modules_install

  • copy the kernel and Device Tree blobs onto the SD card
sudo cp mnt/fat32/$KERNEL.img mnt/fat32/$KERNEL-backup.img
sudo cp arch/arm64/boot/Image mnt/fat32/$KERNEL.img
sudo cp arch/arm64/boot/dts/broadcom/*.dtb mnt/fat32/
sudo cp arch/arm64/boot/dts/overlays/*.dtb* mnt/fat32/overlays/
sudo cp arch/arm64/boot/dts/overlays/README mnt/fat32/overlays/
sudo umount mnt/fat32
sudo umount mnt/ext4
  1. edit the sd/boot/config.txt file, add the following setting
    kernel=kernel-madebyyou.img

  2. remove SD card

sudo   umount  /dev/sdX1
sudo   umount  /dev/sdX2
  1. Insert SD card to PI and boot!!

make menuconfig

上面的build kernel是使用default config, 可以用menuconfig來調整所需的功能

  1. Preparing to make menuconfig
    sudo apt install libncurses5-dev

  2. cross-compiling a 64-bit kernel:
    make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig

  • 調整你的選項, 最後再按Exit存檔離開
  1. 重複上述的4-10步驟, 從 Make image for all 64-bit Builds 開始



以下是2021整理的資料

cross compile 環境

  • Install required dependencies and toolchain
    sudo apt install git bc bison flex libssl-dev make libc6-dev libncurses5-dev

  • Install the 32-bit toolchain for a 32-bit kernel (因為raspbian是32 bit)
    sudo apt install crossbuild-essential-armhf


測試cross compile

  1. 準備一個c code
#include <stdio.h>
int main()
{
	printf("hello, world\n");
        return 0;
}
  1. 建立流程
nano hello.c		    // write your code
gcc hello.c -o hello.o	// compile it, the output file is hello.o
./hello.o			    // execute hello.o
  1. Cross compile, then copy it to PI and execute
(@PC) arm-linux-gnueabihf-gcc hello.c -o hello.arm32
(@PC) aarch64-linux-gnu-gcc hello.c -o hello.arm64
// 複製hello.arm32到PI裡面
(@PI) chmod  +x  hello.arm32
(@PI) ./hello.arm32

準備Build kernel

Get sources (To download the minimal source tree for the current branch)

git clone --depth=1 https://github.com/raspberrypi/linux
cd linux

load default config and build (For Pi 3, Pi 3+ or Compute Module 3)

// cd linux
KERNEL=kernel7
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs

load default config and build (For Raspberry Pi 4)

// cd linux
KERNEL=kernel8
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2711_defconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs

Use custom config by menuconfig and build

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-  menuconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-  zImage modules dtbs

ps. build的時間很長, 根據電腦的性能有所影響. 第一次build可以估30-60min.


複製檔案到SD卡

  • copy the kernel and Device Tree blobs to your SD card
  1. check SD card state
df  -h
sudo  fdisk  -l
  • 在虛擬機掛載SD卡時, 通常會自動掛載
  • 可以用df -h檢查 before/after 的變化
ex:
/dev/sdb1       253M   46M  207M  18% /media/xd/boot
/dev/sdb2       7.6G  6.6G  672M  91% /media/xd/rootfs
  1. back up old kernel
    cp -rf /media/xd/boot/* Desktop/boot_PI/

  2. copy new kernel to SD card

cd ~/linux
sudo cp arch/arm/boot/zImage sd/boot/kernel-madebyyou.img
sudo cp arch/arm/boot/dts/*.dtb sd/boot/
sudo cp arch/arm/boot/dts/overlays/*.dtb* sd/boot/overlays/
sudo cp arch/arm/boot/dts/overlays/README sd/boot/overlays/
  1. install module in the filesystem (the second partition), ex: /media/username/rootfs
    sudo env PATH=$PATH make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=sd/rootfs modules_install

  2. edit the sd/boot/config.txt file, add the following setting
    kernel=kernel-madebyyou.img

  3. remove SD card

sudo   umount  /dev/sdX1
sudo   umount  /dev/sdX2
  1. Insert SD card to PI and boot!!

//以下是官網範例

sudo env PATH=$PATH make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=/media/xd/rootfs modules_install

sudo cp mnt/fat32/$KERNEL.img mnt/fat32/$KERNEL-backup.img
sudo cp arch/arm/boot/zImage mnt/fat32/$KERNEL.img
sudo cp arch/arm/boot/dts/*.dtb mnt/fat32/
sudo cp arch/arm/boot/dts/overlays/*.dtb* mnt/fat32/overlays/
sudo cp arch/arm/boot/dts/overlays/README mnt/fat32/overlays/
sudo umount mnt/fat32
sudo umount mnt/ext4