# Raspberry PI + cross compile & build kernel ## 先準備ubuntu 18 (64bit版) * 使用virtualbox + ubuntu18 (64bit, Desktop) (較安定) * http://ubuntu.cs.nycu.edu.tw/ubuntu-release/18.04.6/ubuntu-18.04.6-desktop-amd64.iso ---- 2022版本 0. 剪貼簿設定 (可以複製貼上指令) ![](https://i.imgur.com/E8ImnJF.png) 1. 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 ![](https://i.imgur.com/0PWndxd.png) ![](https://i.imgur.com/QaZnimE.png) * 裝完之後, 再重開機 2. 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` 3. 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 ``` 4. Make image for all 64-bit Builds `make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image modules dtbs` * 第一次跑大約要1小時 (看電腦的性能) 5. 把micro sd卡連接到虛擬機裡面, 然後掛載磁區 ``` mkdir mnt mkdir mnt/fat32 mkdir mnt/ext4 sudo mount /dev/sdb1 mnt/fat32 sudo mount /dev/sdb2 mnt/ext4 ``` 6. back up old kernel `cp -rf /media/xd/boot/* Desktop/boot_PI/` 7. 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 ``` 8. edit the sd/boot/config.txt file, add the following setting ````kernel=kernel-madebyyou.img```` 9. remove SD card ```` sudo umount /dev/sdX1 sudo umount /dev/sdX2 ```` 10. 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存檔離開 3. 重複上述的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 0. 準備一個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 ```` 2. 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 ```` 2. back up old kernel ````cp -rf /media/xd/boot/* Desktop/boot_PI/```` 3. 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/ ```` 4. 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```` 5. edit the sd/boot/config.txt file, add the following setting ````kernel=kernel-madebyyou.img```` 6. remove SD card ```` sudo umount /dev/sdX1 sudo umount /dev/sdX2 ```` 7. 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 ````