Wiki

Clone wiki

roseline / Tutorial 5 - Cross-compiling a kernel, modules and device tree

Overview

There are cases where you may wish to build a new kernel, recompile modules or a device tree for the BeagleBone Black. For example, you may wish to patch the kernel with the RK extension for real-time support, or modify the in-kernel OMAP clock code. This tutorial covers obtaining, patching and building a kernel, module and device tree from scratch.

STEP 1 : Download Linaro toolchain (cross-compiler)

Firstly, the armhf cross-compiler in Linux has known bugs, which make it impossible to compile the Linux kernel. For this reason we will use a toolchain provided by the Linaro project. Start by downloading and unzipping this toolchain, and creating symbolic link to make things simpler:

cd <roseline>/kernel
wget https://releases.linaro.org/14.11/components/toolchain/binaries/arm-linux-gnueabihf/gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf.tar.xz
tar -xJf gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf.tar.xz
ln -s gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf linaro

STEP 2 : Download and patch the Linux Kernel

The first thing that is required is to download, decompress and create a convenient symbolic link for a stable version of Linux. We have chosen v3.18.9 because it is compatible with the RT patch set and includes drivers for our AT86RF233 radio.

cd <roseline>/kernel
wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.9.tar.xz
tar -xJf linux-3.18.9.tar.xz
ln -s linux-3.18.9 linux

Then, download and apply the Real-time Linux (RT) patch. Please double-check that all patch hunks are successfully applied to the Kernel source tree.

cd <roseline>/kernel
wget https://www.kernel.org/pub/linux/kernel/projects/rt/3.18/older/patch-3.18.9-rt4.patch.xz
pushd linux
xzcat ../patch-3.18.9-rt4.patch.xz | patch -p1
popd

Then, apply the ROSELINE patches to the kernel tree. These patches change the OMAP2 clock to enable TCLKIN, set up hardware interrupts for the radio, and modify the standard pin multiplexing for our board to enable SPI, i2c, TCLKIN and four timer input capture pins.

cd <roseline>/kernel
pushd linux
for i in ../patches/*.patch; do patch -p1 < $i; done
popd

Before compiling it is necessary to download firmware for the board, which should be placed in the firmware directory. This is firmware released by TI that enables power management for the board.

cd <roseline>/kernel
wget http://arago-project.org/git/projects/?p=am33x-cm3.git\;a=blob_plain\;f=bin/am335x-pm-firmware.bin\;hb=HEAD -O linux/firmware/am335x-pm-firmware.bin

We are now ready to copy over the kernel configuration and compile the kernel, modules and device tree. Note that the kernel configuration specified below assumes the toolchain is located at ../linaro. If you have a quad core processor and 8GB+ RAM you can compile with the -j8 in place of -j4.

cd <roseline>/kernel
pushd linux
cp ../kernel.config .config
make ARCH=arm oldconfig
make ARCH=arm zImage dtbs modules -j4
popd

If this finishes without error, then you have a new kernel, modules and device tree ready.

STEP 3 : Build the ROSELINE kernel module

At this stage it makes sense to build the out-of-tree kernel modules. The first of these is the ROSELINE module, which works along with the device tree to set up input capture on several timer pins, as well as act as an information proxy between applications and the synchronization algorithm running in user-space. The second kernel module, kmod-usrcc, allows the processor cycle counter to be read from user-space, which effectively provides a 1GHz timer clocked up from the 24MHz hardware timers.

To do this, just change to the modules directory and build. This should work automatically if you have the linux symbolic link pointing to the compiled Linux source tree in the kernel folder.

cd <roseline>/modules
make

STEP 4 : Install kernel image, modules and device tree

Firstly, copy the kernel image and device tree to your boot location (/path/to/boot). If you are booting via memory card, then this will be on the first partition of the MicroSD card, replacing the precompiled files. If you are network-booting the platforms, then this will be to your TFTP root directory.

cd <roseline>
sudo cp kernel/arch/arm/boot/zImage  /path/to/boot
sudo cp kernel/arch/arm/boot/dts/am335x-boneblack.dtb /path/to/boot

Secondly, copy the in and out of tree kernel modules to the root files ystem. Again, if you are booting off of the memory card, this will be the second partition of your card. Otherwise, for network-booting this will be the location of your NFS shared root file system.

cd <roseline>
pushd kernel/linux
sudo make ARCH=arm INSTALL_MOD_PATH=/path/to/rootfs modules_install
sudo make ARCH=arm INSTALL_HDR_PATH=/path/to/rootfs/usr headers_install
popd
pushd modules
sudo cp *.ko /path/to/rootfs/lib/modules/3.18.9-rt4/kernel/drivers
popd

Updated