Skip to content

Instantly share code, notes, and snippets.

@nathan-osman
Last active October 21, 2016 12:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathan-osman/ed7beb20d10602aa2900 to your computer and use it in GitHub Desktop.
Save nathan-osman/ed7beb20d10602aa2900 to your computer and use it in GitHub Desktop.
Cross-compiles the Raspberry Pi kernel from source on Ubuntu
#!/bin/bash
set -e
######################################
# Raspberry Pi Kernel Builder Script #
# Copyright 2015 - Nathan Osman #
# Released into the Public Domain #
######################################
# Ensure that we are running as root
if ! [ $(id -u) = 0 ]; then
echo "Error: this script must be run as root."
exit 1
fi
# Update and install all necessary packages
apt-get update
apt-get install -y build-essential git ncurses-dev lib32z1-dev libc6:i386
# Checkout the RPi kernel, firmware, and tools from GitHub
git clone --depth=1 https://github.com/raspberrypi/linux.git
git clone --depth=1 https://github.com/raspberrypi/tools.git
git clone --depth=1 https://github.com/raspberrypi/firmware.git
# Set important variables
ROOT=`pwd`
KERNEL="$ROOT/linux"
TOOLS="$ROOT/tools"
FIRMWARE="$ROOT/firmware"
MODULES="$ROOT/modules"
CCPREFIX="$TOOLS/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-"
# cd into the kernel source tree and set things up
cd "$KERNEL"
make mrproper
cp arch/arm/configs/bcmrpi_defconfig .config
# TODO: make this into an option
ARCH=arm CROSS_COMPILE="$CCPREFIX" make menuconfig
# Compile the kernel
ARCH=arm CROSS_COMPILE="$CCPREFIX" make -j`nproc`
# Copy the modules to the appropriate directory
ARCH=arm CROSS_COMPILE="$CCPREFIX" INSTALL_MOD_PATH="$MODULES" make modules_install
# Create the kernel.img
cd "$TOOLS/mkimage"
./imagetool-uncompressed.py "$KERNEL/arch/arm/boot/zImage"
# Go back to the original root and copy everything to one place
cd "$ROOT"
mkdir -p build/boot
cd build
cp "$TOOLS/mkimage/kernel.img" boot/
cp "$FIRMWARE/boot/bootcode.bin" boot/
cp "$FIRMWARE/boot/fixup.dat" boot/
cp "$FIRMWARE/boot/start.elf" boot/
cp -r "$MODULES/lib" .
cp -r "$FIRMWARE/hardfp/opt" .
# Create a compressed archive
tar -cJf "$ROOT/build.tar.xz" boot lib opt
# Tell the user that the task is complete
echo
echo "'$ROOT/build.tar.xz' created!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment