Skip to content

Instantly share code, notes, and snippets.

@leshniak
Last active August 5, 2020 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leshniak/f2cd4baaebf8b425561dfc5a1ad4ffd4 to your computer and use it in GitHub Desktop.
Save leshniak/f2cd4baaebf8b425561dfc5a1ad4ffd4 to your computer and use it in GitHub Desktop.
Building Raspberry Pi kernel

Making a custom ARM64 kernel for Raspberry Pi

install dependencies

sudo apt install build-essential libgmp-dev libmpfr-dev libmpc-dev libisl-dev libncurses5-dev bc git-core bison flex

build cross-binutils

https://ftp.gnu.org/gnu/binutils/

./configure --prefix=../toolchain --target=aarch64-linux-gnu --disable-nls
make -j4
make install

build cross-gcc

https://ftp.gnu.org/gnu/gcc/

./configure --prefix=../toolchain --target=aarch64-linux-gnu --with-newlib --without-headers \
  --disable-nls --disable-shared --disable-threads --disable-libssp --disable-decimal-float \
  --disable-libquadmath --disable-libvtv --disable-libgomp --disable-libatomic \
  --enable-languages=c
make all-gcc -j4
make install-gcc

build kernel

https://github.com/raspberrypi/linux/

if cross-compilation needed

export ARCH=arm64
export CROSS_COMPILE=../toolchain/bin/aarch64-linux-gnu-

if not in root filesystem use

INSTALL_MOD_PATH=/mnt/host

generate .config

make bcm2711_defconfig
# or if it's update from previous build
make oldconfig

build kernel and install kernel modules

make Image modules dtbs -j4
sudo make modules_install

copy the kernel to boot directory

mkdir -pv /boot/custom/overlays
sudo cp arch/arm64/boot/Image /boot/custom/kernel8.img
sudo cp arch/arm64/boot/dts/broadcom/*.dtb /boot/custom/
sudo cp arch/arm64/boot/dts/overlays/*.dtb* /boot/custom/overlays/
sudo cp arch/arm64/boot/dts/overlays/README /boot/custom/overlays/

copy kernel headers

export KERNEL_VERSION=$(strings arch/arm64/boot/Image | grep 'Linux version' | cut -d' ' -f3)
export KERNEL_HEADERS_PATH="../linux-headers-$KERNEL_VERSION"
export RSYNC_OPTS="-aAXv"

mkdir -pv $KERNEL_HEADERS_PATH
find . -name 'Makefile*' -o -name 'Kconfig*' -o -name '*.pl' | rsync $RSYNC_OPTS --files-from=- . $KERNEL_HEADERS_PATH
find arch/*/include include scripts -type f -o -type l | rsync $RSYNC_OPTS --files-from=- . $KERNEL_HEADERS_PATH
find arch/arm64 -name module.lds -o -name Kbuild.platforms -o -name Platform | rsync $RSYNC_OPTS --files-from=- . $KERNEL_HEADERS_PATH
find $(find arch/arm64 -name include -o -name scripts -type d) -type f | rsync $RSYNC_OPTS --files-from=- . $KERNEL_HEADERS_PATH
find tools/objtool -type f -executable | rsync $RSYNC_OPTS --files-from=- . $KERNEL_HEADERS_PATH
find arch/arm64/include Module.symvers include scripts -type f | rsync $RSYNC_OPTS --files-from=- . $KERNEL_HEADERS_PATH
find scripts/gcc-plugins -name '*.so' -o -name gcc-common.h | rsync $RSYNC_OPTS --files-from=- . $KERNEL_HEADERS_PATH
cp .config $KERNEL_HEADERS_PATH

remove symlink to the source and add symlink to kernel headers

sudo rm /lib/modules/$KERNEL_VERSION/source
sudo rm /lib/modules/$KERNEL_VERSION/build
sudo ln -fs $KERNEL_HEADERS_PATH /lib/modules/$KERNEL_VERSION/build

build external kernel module from chroot env

make -C /mnt/host/usr/src/linux-headers-$(uname -r) M=$PWD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment