# Raspberry pi Kernel ###### tags: `Raspberry pi` ## Raspberry pi kernel編譯兩種方式解釋 * Raspberry Pi 用的 Linux 核心有以下幾種方式,兩種方式各有利弊,以下分別是這兩種編譯方式的步驟教學。 1. 一種是在 Raspberry Pi 中直接編譯 2. 一種是在一般的個人電腦中交叉編譯(cross compilation) ## 工具下載 * sudo vim piarm_install.sh ``` #!/bin/bash sudo apt-get update sudo apt-get upgrade sudo apt-get install #sudo apt-get install python3-pip #sudo apt-get install python3 sudo apt install -y libncurses5-dev libssl-dev build-essential openssl bison flex bc sudo apt-get install -y gcc-arm-linux-gnueabihf sudo apt-get install -y make git-core ncurses-dev #sudo apt-get install gcc-5 #sudo apt-get install gcc-7 ``` ### Github install raspberry kernel 1. git clone https://github.com/raspberrypi/linux 2. git clone --depth=1 https://github.com/raspberrypi/linux * git branch -a 3. git clone https://github.com/raspberrypi/tools 4. sudo vim git-install.sh 5. sudo chmod +x git-install.sh ``` #!/bin/bash git clone https://github.com/raspberrypi/linux git clone https://github.com/raspberrypi/tools ``` ## Raspberry Pi 中編譯核心 #### Raspberry Pi 1,Pi Zero,Pi Zero W和Compute Module配置 * cd linux * KERNEL=kernel * make bcmrpi_defconfig #### Raspberry Pi 2,Pi 3,Pi 3+和Compute Module 3配置 * cd linux * KERNEL=kernel7 * make bcm2709_defconfig #### Raspberry Pi 4配置 * cd linux * KERNEL=kernel7l * make bcm2711_defconfig #### 圖片 ![](https://i.imgur.com/3Gf9af3.png) ### 編譯內核 * make -j4 zImage modules dtbs * sudo make modules_install * sudo cp arch/arm/boot/dts/*.dtb /boot/ * sudo cp arch/arm/boot/dts/overlays/*.dtb* /boot/overlays/ * sudo cp arch/arm/boot/dts/overlays/README /boot/overlays/ * sudo cp arch/arm/boot/zImage /boot/$KERNEL.img ### 變更環境變數 * echo PATH=\$PATH:~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin >> ~/.bashrc * source ~/.bashrc #### 適用於 Pi 1,Pi Zero,Pi Zero W * cd linux * KERNEL=kernel * make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig #### 適用於Pi 2,Pi 3,Pi 3+ * cd linux * KERNEL=kernel7 * make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig * ![](https://i.imgur.com/k0iWH4f.png) #### 適用於Pi 4 * cd linux * KERNEL=kernel7l * make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2711_defconfig ### 安裝toolchain(Cross compiler),將 gcc-linaro-arm-linux-gnueabihf-raspbian 加到環境變數裡。 ### For 32 位元Linux * export PATH=/home/pi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin:$PATH ### For 64 位元Linux * export PATH=/home/pi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi-x64/bin:$PATH ### 交叉編譯 * 輸入 arm 後連續按兩次 tab 鍵 * arm- #### hello.c 並編譯 * sudo nano hello.c ``` #include <stdio.h> int main() { printf("hello, world\n"); return 0; } ``` * arm-linux-gnueabihf-gcc hello.c -o hello-arm * file hello-arm ![](https://i.imgur.com/wWC7Zbs.png) * ./hello-arm ![](https://i.imgur.com/6iGtou0.png) ### 試寫一個kernel * mkdir kernel * cd kernel * sudo nano hello.c ``` #include <linux/module.h> #include <linux/init.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { printk("Hello world!\n"); return 0; } static void hello_exit(void) { printk("Goodbye, hello world\n"); } module_init(hello_init); module_exit(hello_exit); ``` * sudo nano Makefile ``` obj-m += hello.o KERNEL=/lib/modules/`uname -r`/build all: make -C $(KERNEL) M=$(shell pwd) modules V=1 ARCH=arm clean: make -C $(KERNEL) M=$(shell pwd) clean ``` * 最後產生一個 .Ko 就代表成功了 ![](https://i.imgur.com/PTwtT9p.png) ### 載入模組 > sudo insmod hello.ko ![](https://i.imgur.com/YarweeO.png) * 刪除載入模組 > sudo rmmod hello.ko ![](https://i.imgur.com/3x3FUXa.png) ## PC 編譯 Raspberry pi image ### 進行編譯之前,要先設定編譯的各種選項,這裏我們使用預設的編譯設定: ``` git clone https://github.com/raspberrypi/linux cd linux make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig ``` ### 載入預設的設定之後,可以再使用 menuconfig 來微調: ``` make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig ``` ![](https://i.imgur.com/ufDqz6Y.png) * 如上得知 kernel 版本 5.4.75 #### 進行編譯核心: * 這裡的 -j8 是讓 make 可以同時執行 4 個 jobs,利用多核心的 CPU 來加速編譯,您可以依照您的 PC 的 CPU 核心數來調整這個值。 ``` make -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- ``` #### 編譯模組 ``` make -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules ``` #### 將編譯好的 Linux kernel 複製出來: ``` cp arch/arm/boot/Image ../kernel-new.img ``` #### 將modules install 至一個暫時的目錄中(../modules): ``` make -j4 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=../modules modules_install ``` # 參考資料 * [Kernel building](https://www.raspberrypi.org/documentation/linux/kernel/building.md) * [在PC交叉編譯](https://blog.gtwang.org/iot/raspberry-pi-compile-linux-kernel/) * [自己動手編譯 Raspberry Pi 的 Kernel](https://coldnew.github.io/f5873b3f/) * [編譯 Raspberry Pi 的 Linux 核心](https://blog.gtwang.org/iot/raspberry-pi-compile-linux-kernel/) * [Raspberry pi kernel wiki](https://wiki.gentoo.org/wiki/Raspberry_Pi#WiFi) * [讓你的 Raspberry Pi 透過 GPIO 閃爍 LED 燈](https://coldnew.github.io/f7349436/)