Microprocessors
Programs

Hacking Raspberry Pi 4 with Yocto: Configuring Our Custom Layer

6.0 Creating Our Custom Layer

In this section, we'll create our own custom layer for our build. The great thing about separating layers out like this is that we can just switch in and out our own different custom layers if we want to include different packages in different builds. We can keep the meta-raspberrypi and poky directories completely the same, but by specifying a different target recipe with bitbake, we can build one version or another.

6.1 The meta-rpilinux/conf/layer.conf File

Every layer should have a directory called conf, and within that directory, a file called layer.conf. We're going to create a directory called meta-rpi

$ cd ~/Yocto 
$ mkdir meta-rpilinux 
$ cd meta-rpilinux 
$ mkdir conf 
$ cd conf
$ vi layer.conf

The contents of the layer.conf file should look like this:

# We have a conf and classes directory, add to BBPATH 
BBPATH .= ":${LAYERDIR}" 

# We have recipes-* directories, add to BBFILES 
BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \ 
  ${LAYERDIR}/recipes-*/*/*.bbappend" 

BBFILE_COLLECTIONS += "rpilinux" 
BBFILE_PATTERN_rpilinux = "^${LAYERDIR}/" 
BBFILE_PRIORITY_rpilinux = "6" 

LAYERSERIES_COMPAT_rpilinux = "zeus" 

The meaning of these configuration variables is explained in the Yocto Mega Manual. The BBPATH directive just adds this current layer directory (meta-rpibuild) to BBPATH that bitbake uses when it's building an image. The BBFILES directive directive specifies which files should be added to the list of bitbake recipies for the build.

The last line in the file tells what Yocto versions this layer is compatible with. In theory, this layer is so minimal that it should be compatible with every version. However, since I haven't tested that, I'm only going to include zeus. You simply have to change the name zeus to whatever version you're using, e.g. fido or warrior.

6.2 The Image Recipe rpilinux-image.bb

Now we're going to create our own recipe for building our rpilinux image. Recipes come in the form of .bb files, which are nominally placed in recipes-[recipe name] files. If you look at the BBFILES directive we specified above, you'll see that we're telling bitbake that our recipe files for this layer are located in a directory with the path of ~/Yocto/meta-rpilinux/[something]. We're going to create an image directory to hold our recipe.

$ cd ~/Yocto/meta-rpilinux 
$ mkdir recipes-rpilinux 
$ cd recipes-rpilinux 
$ mkdir images 
$ cd images 
$ vi rpilinux-image.bb

The contents of our rpilinux-image.bb file are going to look like this:

require recipes-core/images/core-image-minimal.bb 

IMAGE_INSTALL += "libstdc++ mtd-utils" 
IMAGE_INSTALL += "openssh openssl openssh-sftp-server" 

These two directives are key ingredients in any bitbake recipe. Here is a description of what each does:

  • require : this directive tells bitbake that you want to parse the core-image-minimal.bb recipe file and insert the file in that location. Essentially what this line is telling bitbake to do is to build the core-minimal-image, which that recipe file tells us is "A small image just capable of allowing a device to boot."

  • IMAGE_INSTALL += : this entry tells bitbake which additional packages should be built and installed in the output image. It is a very powerful way to cross-compile and link code before you even put the SD card into the board for the first time.


← Previous    ...    Next →