Sunday, July 08, 2012

Building modules for the latest Raspberry Pi firmware

The latest firmware for the Raspberry Pi is made available through the git repository. The firmware built is based on the kernel sources which are also made available through a separate git repository.

Users may want to install the latest firmware to avail of the latest fixes available for the Raspberry Pi kernel. However changes in the kernel necessitates the rebuild of any customer kernel modules installed. These are kernel modules which are not available on the usual kernel which was distributed. In my case, I need to recompile the drivers for my wireless card which aren't available on the stock kernel. 

The steps given below is to be used when you recompile your drivers from within the Raspberry Pi.

Before we start, we need to make sure that we have the required tools.
# apt-get update
# apt-get install git gcc make
To update the firmware to the latest one from the git repo, follow the steps below

Download the latest firmware repo
#  git clone --depth 1 https://github.com/raspberrypi/firmware.git
The argument --depth 1 limits the firmware downloaded to only the latest available in the repo.
OR
Update your existing firmware repo by first changing into the firmware repo directory and running the command
# git pull 
You now have to download the latest source code from the git repo. The 256 MB of memory available on the Raspberry Pi isn't sufficient to run the git clone command. To get around this, you can download the git repo on a different machine and then copy over the repo to the Raspberry Pi.
To clone the git repo, use the command
# git clone --branch rpi-patches https://github.com/raspberrypi/linux.git rapi-linux
The argument --branch rpi-patches specifies that you would like to download the branch rpi-patches which is the one on which the kernel is built.
Now copy over the directory rapi-linux created to the Raspberry Pi.
OR
Update the existing sources directory by first changing into the sources repo directory and running the command
# git checkout rpi-patches
# git pull
I have the git repos downloaded to the following location on the Raspberry Pi
/root/firmware
/root/rapi-linux
Now to build the kernel headers required to build the module.

1) Determine the git hash which represents the kernel used to build the firmware and check out the sources.
# cat /root/firmware/extra/git_hash 
85b7821857dd0b9cabab59d47f08eabed74679a3
Use this hash to checkout the kernel sources.
# cd /root/rapi-linux
# git checkout 85b7821857dd0b9cabab59d47f08eabed74679a3


2) Prepare the sources so that they can be used to build the kernel module.
Reset the sources to clear any artifacts from an earlier build
# make mrproper
Copy over the kernel configuration and prepare sources.
# zcat /proc/config.gz > .config
# make oldconfig
# make modules_prepare
3) Copy over the files require to build the module from the firmware repo
# cp /root/firmware/extra/Module.symvers /root/rapi-linux
The kernel sources required to build the kernel module is now complete.

To build the module, change into the directory containing the module sources and run the command
# cd /root/rtl8192_8188CU_linux_v3.0.2164.20110715/
# make -C /root/rapi-linux/ M=`pwd`
We pass the location of the kernel sources directory with the -C parameter. 
We pass the location to the module sources using the M= parameter.

To update the kernel on the Raspberry Pi, change into the firmware directory and copy over the required files.
First backup the existing files 
# cd /root/firmware
mkdir -p /root/fw.backups/
# rsync -av /boot /root/fw.backups/
# rsync -av /lib/modules/3.1.9+ /root/fw.backups/
Now copy over the new files
# cp -av boot/* /boot/
# cp -av modules/3.1.9+/* /lib/modules/3.1.9+/
Run depmod to build the module dependencies for modprobe
# depmod -a
Perform any additional steps required for your custom modules. In my case, I need to copy over my newly built wireless module to the new modules folder.

# cp ~/rtl8192_8188CU_linux_v3.0.2164.20110715/8192cu.ko /lib/modules/3.1.9+/kernel/drivers/net/wireless/
# depmod -a 
Now reboot the Raspberry Pi so that the new kernel can be loaded.