Tuesday, February 16, 2016

Notes from compiling a kernel module from scratch

Found the debian SD image for BeagleBone that I started testing with so long ago. It's probably obsolete by now, but at least I know that it works.

Its name is bone-debian-7.8-lxde-4gb-armhf-2015-03-01-4gb.img

Used Win32DiskImager to write it to a blank 4G SD card from AdaFruit.

Beaglebone booted to LXDE desktop after a worrying 45 second pause (It might have been setting up the ethernet connection)

perform apt-get update

sudo apt-get update

Update Tkinter just because

sudoe apt-get install python-tk

start python, verify tkinter installed to make sure that the last installs worked.

$ which python
$ python
import os
import Tkinter
^d

get my kernel version:

$ uname -r
3.8.13-bone70

attempt to get the source tree headers:

$ sudo apt-get install kernel-headers-$(uname -r)
E: Unable to locate package kernel-headers-3.8.130bone70
E: Couldn't find any package by regex 'kernel-headers-3.8.13-bone70'

$ sudo apt-get install kernel-headers
E: Unable to locate package kernel-headers

Try searching the cache for anything that might be what I'm looking for:

$ apt-cache search kernel | grep headers
$apt-cache search headers | grep bone70
linux-headers-3.8.13-bone70

Note: from above searches, I discovered that the cache had headers up to and probably past bone79.
trying again to get the source tree headers:

$ sudo apt-get install linux-headers-$(uname -r)

Worked great.

Download hello.c and Makefile from google cached version of this site (which seems to be not responding right now for some reason) http://www.cyberciti.biz/faq/add-remove-list-linux-kernel-modules/

Copy Makefile and hello.c to new folder under /home/debian, cd to folder, run 'make'. Compiled .ko file as promised.

Run the following commands. Kernel module seems to be installed, although there is an extra message in the /var/log/messages file saying: "hello: module license 'unspecified' taints kernel"

$ modinfo hello.ko
$ sudo insmod hello.ko
$ lsmod
$ rmmod hello
$ tail -f /var/log/messages

Pulled raw rtc-pcf8563.c file from https://github.com/jeffegg/beaglebone/tree/master/drivers/rtc and tried to put it in its own directory under /home/debian with the Makefile from the hello example, changing the name of the object to compile, got some compile errors. Specifically it doesn't like THIS_MODULE and the module property declarations at the end.

The makefile at https://github.com/jeffegg/beaglebone/tree/master/drivers/rtc has a bunch of options for compiling other things like rtc-lib.o, rtc-core.o, all of which seem like I might need them.

Looking at the linux headers directory that I downloaded earlier, I am surprised to find the exact same makefile under /usr/src/linux-headers-3.8.13-bone70/drivers/rtc!!!! This may indicate that I might be able to find the kernel module in the source tree and just build it after all. The question is how to get the source tree. After a lot of fiddling, trying:

$sudo apt-get install linux-source

This seems to be getting me the 3.2 source instead of the 3.8 for unknown reasons, but that might be sufficient to get a drivers build directory that matches the drivers header directory.