Wednesday, February 20, 2019

Espressobin - rebuilding kernel based on Armbian sources

Some time ago I bought the Espressobin board. It looked interesting but it turns out there is virtually zero support from the producer. There is some info on their wiki but it's quite obsolete.
I think the best way to get the Espressobin board running is installing Armbian. The good folks (or... the one person running the show?) build a decently working U-Boot and kernel and there is an SD card image that you can just dd onto a SD card. You don't even have to expand the partition to cover the whole SD card after copying the image -- after boot it expands it automatically.
This is all cool and dandy... But if you want to build your own U-Boot or kernel, Armbian does not make it easy. There is official documentation on how to build Armbian. This makes sense when you want to build the whole system but not so much if you just want to build the U-Boot and the kernel. Yet the fact that Armbian uses reasonably recent U-Boot and kernel, together with a bunch of patches, is interesting and I wanted to get to the U-Boot and kernel sources w/o having to run the whole Armbian build machinery. So I took a dive into the Armbian build scripts.


The build scripts including Armbian patches live in a github repo. Depending on when you read this, Armbian might have moved on. This dive is based on commit 5659f7fffa which is current master at the time of writing this.
If you followed the official build instructions, you would run compile.sh. This eventually sources lib/main.sh. Following the breadcrumbs:
The kernel build happens via compile_kernel defined in lib/compilation.sh. This does the following (assuming you build for the Espressobin, kernel "branch" next):
So to replicate the same thing w/o the heavy machinery, the following seems sufficient:
  • clone mainline kernel git repo e.g. from git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
  • check out branch linux-4.19.y
  • clone Armbian build repo from https://github.com/armbian/build.git to get hold of kernel patches
  • apply patches from Armbian build repo, from patch/kernel/mvebu64-next
    • this seems like the tricky part: the patches are a varied bunch, a simple git am would not work as some patches are indeed just plain patches
    • I created a helper script that tries to apply the patches, making a commit for each: apply-armbian-patches.sh
  • use config from config/kernel/linux-mvebu64-next.config
  • make the targets Image, modules and dtb
  • copy the right results
    • To get the modules copied into a directory, you can run make INSTALL_MOD_PATH=some-dir modules_install. These need to be copied into /lib/modules on the Espressobin.
    • You need the actual kernel image which lives in arch/arm64/boot/Image. This needs to be copied into /boot. Armbian names these vmlinuz-$VERSION$LOCALVERSION, e.g. vmlinuz-4.19.13-mvebu64
    • You need device tree binary files that live in arch/arm64/boot/dts/marvel/*.dtb. These should be copied to /boot/dtb-$VERSION$LOCALVERSION/marvell, e.g. /boot/dtb-4.19.13-mvebu64/marvell.
    • You could also use a script that does the build + copying: espressobin-kernel-install.sh
  • Create initramfs for the new kernel. On the Espressobin, run as root: update-initramfs -c -k $VERSION$LOCALVERSION (e.g. update-initramfs -c -k 4.19.14-ebin-mine)
    • as a sometimes-unwanted bonus, this seems to also update the uInitrd symlink (see below)
  • There are symlinks in /boot that point to the current version of kernel, initramfs and dtb, so update these:
    • dtb → dtb-$VERSION$LOCALVERSION
    • Image → vmlinuz-$VERSION$LOCALVERSION
    • uInitrd → uInitrd-$VERSION$LOCALVERSION
  • Reboot and hope for the best. :-)

2 comments:

  1. Hi that's a fantastic breakdown of the workflow inside the armbian build framework. Do you mind if I reuse this and add it to the documentation?

    FYI you're also welcome to contribute to the docs :) https://github.com/armbian/documentation

    ReplyDelete
    Replies
    1. Feel free to use whatever you find useful here.

      This was written mostly as a note to myself, for when I need it next time. With my one interaction with Armbian forums, and after reading the docs, I did not think this kind of info would be welcome.

      Delete