sudo apt install build-essential libgmp-dev libmpfr-dev libmpc-dev libisl-dev libncurses5-dev bc git-core bison flex
https://ftp.gnu.org/gnu/binutils/
./configure --prefix=../toolchain --target=aarch64-linux-gnu --disable-nls
make -j4
make install
./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
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