Yume 練功地

偷偷練

Raspberry Pi Cross Compile

從 Linux 取出 .config file

導出 rpi config file
1
2
sudo modprobe configs
zcat /proc/config.gz > .config

下載 kernel build 工具

下載相關套件
1
2
git clone --depth=1 https://github.com/raspberrypi/linux
git clone https://github.com/raspberrypi/tools
下載 libs
1
2
3
sudo apt-get install bc
sudo apt-get install git-core
sudo apt-get install libncurses5-dev

設置 Cross Compile Tool

/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin 設定到 PATH

如果 Host 是 64 位元,則用這個 /tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin

.config

這邊我們要使用的 .config,是從 raspberry pi 拿回低。

所以 make 那段可以略過
Pi 1 or Compute Module
1
2
3
cd linux
KERNEL=kernel
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig
For Pi 2/3
1
2
3
cd linux
KERNEL=kernel7
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig

Build Time

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# j4 使用 4 核 build
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j4 zImage modules dtbs

mkdir mnt/fat32
mkdir mnt/ext4
sudo mount /dev/sdb1 mnt/fat32
sudo mount /dev/sdb2 mnt/ext4
sudo make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=mnt/ext4 modules_install
sudo cp mnt/fat32/$KERNEL.img mnt/fat32/$KERNEL-backup.img
sudo scripts/mkknlimg 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

Cross Compile KO

Makefile
1
2
3
4
5
6
7
8
9
10
obj-m += hello-1.o
# 如果不是 Cross Compile,KDIR 會是下面那個
# KDIR ?= /lib/modules/`uname -r`/build
KDIR ?= /home/yume/rasp/linux/mnt/ext4/lib/modules/4.4.10-v7+/build

all:
make -C $(KDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules

clean:
make -C $(KDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- clean

自訂選擇 kernel image

/boot/config.txt
1
kernel=kernel-new.img

參考

KERNEL BUILDING
Raspberry Pi Kernel Compilation
[進階] 編譯 Raspberry Pi 的核心
樹莓派 Raspberry Pi 編譯 Linux 核心(Kernel)步驟教學