This is a multi step proces:
Step 1: Get the git_hash from the firmware
First you need the commit id of the firmware (firmware-commit-id) from the raspberrypi-bootloader
package:
zgrep '* firmware as of' /usr/share/doc/raspberrypi-bootloader/changelog.Debian.gz | head -1
With the firmware-commit-id in mind, you go to https://github.com/raspberrypi/firmware/commits/master and look up that commit id and click on the link with the title Browse the repository at this point in the history and then you go into the extra
directory. When you're there, you should see a file named git_hash
. Open that file and write down the git commit id (kernel-commit-id) you see there.
From that directory also download Module7.symvers file for the Pi2 (Module.symvers for the Pi1)
Step 2: Prepare the kernel for module compilation
Get the kernel sources and put them into the rpf-linux-kernel
folder:
git clone https://github.com/raspberrypi/linux rpf-linux-kernel
This will download more then 1GB of data and then process it, which will take a while.
Downloading the kernel source alone is not sufficient to compile a kernel module. There are a couple of things we need to do to get the kernel source at the time of the kernel compilation and do some other preparations before we can compile a kernel module.
cd rpf-linux-kernel
git checkout -b rpi-bootloader-3.18.11 <kernel-commit-id>
This will change into the directory with the raspberrypi kernel code and then checkout the code at the earlier retrieved kernel-commit-id into a new branch. I always do things in a separate branch so I can mess up all I want in that branch and start over cleanly by going back and checking out in a new branch. It is not required though.
Now that we have the exact code at the right commit id, it's time to make some preparations:
make mrproper
make bcm2709_defconfig
make modules_prepare
This cleans everything up and then loads the default configuration for the Pi2. For the Pi1 you should use make bcmrpi_defconfig
instead.
It then makes things ready to compile kernel modules.
cp ../Module7.symvers Module.symvers
This copies the previously saved Module7.symvers
into the current directory. It must be named Module.symvers
when placed in the rpf-linux-kernel
directory! When compiling for the Pi1, you should just copy the previously saved Module.symvers
into the rpf-linux-kernel
directory. I assumed here that those *.symvers
files were saved in the parent directory of rpf-linux-kernel
.
Step 3: Set up your system for kernel module compilation
When you're compiling a kernel module, the build system looks in the /lib/modules/<kernel-version>/build
directory for the kernel headers/sources.
For the following example we assume that the user is 'pi' and that the previous git clone command was performed in it's home directory and that we want to compile a module for the current kernel.
The link we need to set up is between the kernel source and the /lib/modules/<current-kernel-version>/build
directory. The <current-kernel-version>
can be obtained by the uname -r
command and so the command to set up the link becomes:
sudo ln -s /home/pi/rpf-linux-kernel/ /lib/modules/$(uname -r)/build
Step 4: Compile your kernel module or test driver
Now that all the preparations are done, you can compile your kernel module or test driver by cd
-ing into that directory and then do make ARCH=arm
or whatever is the right way to compile your module/driver.