This page is intended to be a collection place for tips and tricks related to Yocto layers and how Yocto works under Petalinux.

Documentation


This is the first place you should start to better understand many details of the Yocto project.

Yocto project

Working with a user space Yocto layer


This tip has been adapted to PetaLinux using the meta-example layer available here meta-example

Installing the meta-example layer on your host

git clone https://github.com/DynamicDevices/meta-example

About the meta-example layer


This example layer is providing 3 build recipes, bbexample, bbexample-rt and bbexample-lt respectively fetching the source code from a git tree, a remote tar file and a local tar file

petalinux-build -c bbexample
petalinux-build -c bbexample-rt
petalinux-build -c bbexample-lt

Configuring the layer path in the petalinux build system


move to your PetaLinux project build directory, edit the conf/bblayers.conf file and add your layer path to BBLAYERS

BBLAYERS := " \
  ${SDKBASEMETAPATH}/layers/poky/meta \
  ${SDKBASEMETAPATH}/layers/poky/meta-poky \
  /absolute_path_to_meta_layer/meta-example \
  "

Building the meta-layer example package based on a local source archive


The recipe we are going to build is https://github.com/DynamicDevices/meta-example/blob/master/recipes-example/bbexample/bbexample-lt_1.0.bb

The recipe bbexample-lt is using some variables

1. SRC_URI Space-delimited list of URIs to download source code, patches, and other files from git, local absolute path, https, ftp etc.
2. PN is the Package Name. The value of this variable is derived by BitBake from the base name of the recipe file.
3. PV is the Package Version. which is derived by BitBake from the base name of the recipe file

# Use local tarball
SRC_URI = "file://bbexample-${PV}.tar.gz"

The recipe bbexample-lt can invoked using the following command

petalinux-build -c bbexample-lt

here is the command output
    petalinux-build -c bbexample-lt
 
    [INFO] building bbexample-lt
    [INFO] sourcing bitbake
    INFO: bitbake bbexample-lt
    Loading cache: 100% |###############################################################################################################################################################################| ETA:  00:00:00
    Loaded 2941 entries from dependency cache.
    Parsing recipes: 100% |#############################################################################################################################################################################| Time: 00:00:01
    Parsing of 2328 .bb files complete (2294 cached, 34 parsed). 2943 targets, 196 skipped, 0 masked, 0 errors.
    NOTE: Resolving any missing task queue dependencies
    NOTE: Preparing RunQueue
    NOTE: Checking sstate mirror object availability (for 38 objects)
    NOTE: Executing SetScene Tasks
    NOTE: Executing RunQueue Tasks
    NOTE: Tasks Summary: Attempted 872 tasks of which 857 didn't need to be rerun and all succeeded.
    INFO: Copying Images from deploy to images
    [INFO] successfully built bbexample-lt

You have now successfully built the layer but you still need to include the binary produced into the kernel root file system

Including the meta-layer example build output in the Linux root file system


There are two options in order to do so

1. Edit your project conf/local.conf and add
IMAGE_INSTALL_append = " bbexample-lt"
2. Adding a package to an image via a .bbappend

You may wish to add specific packages to specific images, which is generally viewed as better practice.
We are using core-image-minimal bitbake recipe for this tip by creating file core-image-minimal.bbappend
This .bbappend file is extending the original core-image-minimal recipe in order to include the layer build output into
the Linux root file system.
recipes-core/images/core-image-minimal.bbappend
core-image-minimal.bbappend file content
IMAGE_INSTALL += " bbexample-lt"

Running the meta-layer example under QEMU


1. Rebuild your layer
petalinux-build -c bbexample_lt -x cleansstate
2. Rebuild your kernel
petalinux-build -c kernel
3. Start qemu
petalinux-boot --qemu --kernel
4. Log on qemu and start the application
root@plnx_arm:/# /usr/bin/bbexample
Hello Yocto World...
Hello World (from a shared library!)

Patching the Linux Kernel of a PetaLinux Project


1. Copy the patch to project file <plnx-proj-root>/project-spec/meta-user/recipes-kernel/linux/linux-xlnx directory.

2. Modify project file <plnx-proj-root>/project-spec/meta-user/recipes-kernel/linux/linux-xlnx_%.bbappend to use the patch file by adding the patch file name to the SRC_URI_append variable. If the variable does not exist in the file then add a new line with
SRC_URI_append = " file://0001-linux-driver-fix.patch"
 
FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"

3. Make sure the priority for the meta-user layer is 7 in the project file <plnx-proj-root>/project-spec/meta-user/conf/layer.conf .

How to modify inittab or getty in v2016.4 PetaLinux Project


1. Create a sysvinit directory in meta-user layer as
$ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-core/sysvinit/sysvinit-inittab
2. Create a sysvinit-inittab_%.bbappend file and your own inittab file or your inittab patch

$ vim <plnx-proj-root>/project-spec/meta-user/recipes-core/sysvinit/sysvinit-inittab_%.bbappend
SRC_URI_append = " file://inittab"
 
FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"
Or
SRC_URI_append = " file://inittab.patch"
 
FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"

How to patch FSBL in v2017.1 PetaLinux Project


Note: This method can't be used for v2016.4 PetaLinux Projects. PetaLinux tools use externalsrc methodology to fetch the source code from XSDK. Yocto does not allow patching in this externalsrc mechanism. Hence this method can't be used in Yocto.


1. Create a fsbl and files directory in meta-user layer as
$ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-bsp/fsbl
 
$ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-bsp/fsbl/files
2. Copy patch files to <plnx-proj-root>/project-spec/meta-user/recipes-bsp/fsbl/files as
$ cp 0001-FSBL.patch <plnx-proj-root>/project-spec/meta-user/recipes-bsp/fsbl/files
3. Create a fsbl_%.bbappend file and add below content
$ vim <plnx-proj-root>/project-spec/meta-user/recipes-bsp/fsbl/fsbl_%.bbappend
#Patch for FSBL
do_configure_prepend() {
    if [-d "${S}/patches"]; then
        rm -rf ${S}/patches
    fi
    if [-d "${S}/.pc"]; then
        rm -rf ${S}/.pc
    fi
}
 
SRC_URI_append = " \
        file://0001-FSBL.patch \
        "
 
FILESEXTRAPATHS_prepend := "${THISDIR}/files:"
 
#Add debug for FSBL
XSCTH_BUILD_DEBUG = "1"
 
#Enable appropriate FSBL debug flags
YAML_COMPILER_FLAGS_append = " -DXPS_BOARD_ZCU102"
 
EXTERNALXSCTSRC = ""
EXTERNALXSCTSRC_BUILD = ""
4. Remove the <plnx-proj-root>/components/plnx_workspace and clean your project workspace before rebuilding FSBL components
$ petalinux-build -x mrproper
$ rm -rf <plnx-proj-root>/components/plnx_workspace
5. Rebuilding FSBL components
$ petalinux-build -c bootloader

How to patch PMU Firmware in v2017.1 PetaLinux Project


Note: This method can't be used for v2016.4 PetaLinux Projects. PetaLinux tools use externalsrc methodology to fetch the source code from XSDK. Yocto does not allow patching in this externalsrc mechanism. Hence this method can't be used in Yocto.

1. Create a pmu and files directory in meta-user layer as
$ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-bsp/pmu
$ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-bsp/pmu/files
2. Copy patch files to <plnx-proj-root>/project-spec/meta-user/recipes-bsp/pmu/files as
$ cp 0001-PMUFW.patch <plnx-proj-root>/project-spec/meta-user/recipes-bsp/pmu/files
3. Create a pmu-firmware_%.bbappend file and add below content
$ vim <plnx-proj-root>/project-spec/meta-user/recipes-bsp/pmu/pmu-firmware_%.bbappend
#Patch for PMUFW
do_configure_prepend() {
    if [-d "${S}/patches"]; then
        rm -rf ${S}/patches
    fi
    if [-d "${S}/.pc"]; then
        rm -rf ${S}/.pc
    fi
}
 
SRC_URI_append = " \
        file://0001-PMUFW.patch \
        "
 
FILESEXTRAPATHS_prepend := "${THISDIR}/files:"
 
#Enable appropriate PMUFW debug flags
YAML_COMPILER_FLAGS_append = " -DENABLE_EM"
 
EXTERNALXSCTSRC = ""
EXTERNALXSCTSRC_BUILD = ""
4. Remove the <plnx-proj-root>/components/plnx_workspace and clean you project workspace before rebuilding PMUFW components
$ petalinux-build -x mrproper
$ rm -rf <plnx-proj-root>/components/plnx_workspace
5. Rebuilding PMUFW components
$ petalinux-build -c pmufw

How to add pre-built libraries in v2016.4 or later PetaLinux or Yocto recipes


1. Create a new recipe or in existing recipe in meta-user layer as shown
$ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-apps/mylibs/files
$ vim <plnx-proj-root>/project-spec/meta-user/recipes-apps/mylibs/mylibs.bb
2. Copy pre-built libraries files to <plnx-proj-root>/project-spec/meta-user/recipes-apps/mylibs/files as
$ cp libsample.so.7 <plnx-proj-root>/project-spec/meta-user/recipes-apps/mylibs/files
3. Add the below content to recipe
SUMMARY = "pre-built libraries"
LICENSE = "CLOSED"
LIC_FILES_CHKSUM = ""
 
SRC_URI = "file://libsample.so.7"
 
S = "${WORKDIR}
INSANE_SKIP_${PN} = "ldflags"
INHIBIT_PACKAGE_STRIP = "1"
INHIBIT_SYSROOT_STRIP = "1"
 
# This will only copy the libraries to "{TARGET_ROOTFS}/usr/lib" directory
do_install () {
        install -d ${D}${libdir}
        cp -a --no-preserve=ownership ${S}/libsample.so.7 ${D}${libdir}
}
 
# If you need to create a symbolic link using the pre-built libraries you can add something like this.
# This copies libraries to "{TARGET_ROOTFS}/usr/lib" directory and create a symlink as "ln -s libsample.so.7 libsample.so"
do_install () {
        install -d ${D}${libdir}
        oe_soinstall ${S}/libsample.so.7 ${D}${libdir}
}

Configuring FSBL and PMUFW debugs in PetaLinux


Create a fsbl_%.bbappend file and add below content
$ vim <plnx-proj-root>/project-spec/meta-user/recipes-bsp/fsbl/fsbl_%.bbappend
To enable debugs in FSBL:
#Add debug for FSBL
XSCTH_BUILD_DEBUG = "1"
To add compiler flags in FSBL:
#Add compiler flags for FSBL
YAML_COMPILER_FLAGS_append = " -DFSBL_PROT_BYPASS"
To add BSP flags(secure mode) in FSBL:
#Add BSP flags for FSBL
YAML_BSP_CONFIG += "secure_mode"
YAML_BSP_CONFIG[secure_mode] = "set,true"
Create a pmu-firmware_%.bbappend file and add below content
$ vim <plnx-proj-root>/project-spec/meta-user/recipes-bsp/pmu/pmu-firmware_%.bbappend
To enable debugs in PMUFW:
#Add debug for PMUFW
XSCTH_BUILD_DEBUG = "1"
To add compiler flags in PMUFW:
#Add compiler flags for PMUFW
YAML_COMPILER_FLAGS_append = " -DENABLE_ESCALATION"
To add BSP flags(secure mode) in PMUFW:
#Add BSP flags for PMUFW
YAML_BSP_CONFIG += "secure_mode"
YAML_BSP_CONFIG[secure_mode] = "set,true"

Creating libraries in v2017.1 or later PetaLinux Project


1. Create a library using petalinux tools
$ petlainux-create -t apps --template c --name libsample --enable
2. Modify the libsample.bb recipe file <plnx-proj-root>/project-spec/meta-user/recipes-apps/libsample/libsample.bb as
#
# This file is the libsample recipe.
#
 
SUMMARY = "Simple libsample application"
SECTION = "libs"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
 
FILESEXTRAPATHS_prepend := "${THISDIR}/files:"
 
SRC_URI = " \
          file://sample.c \
          file://sample.h \
          file://Makefile \
         "
 
S = "${WORKDIR}"
 
PACKAGE_ARCH = "${MACHINE_ARCH}"
PROVIDES = "sample"
TARGET_CC_ARCH += "${LDFLAGS}"
 
do_install() {
    install -d ${D}${libdir}
    install -d ${D}${includedir}
    oe_libinstall -so libsample ${D}${libdir}
# This is optional and depends if you have any headers to copied along with libraries
# This example includes sample.h to to copied to <TARGET_ROOTFS>/usr/lib/SAMPLE/sample.h
    install -d -m 0655 ${D}${includedir}/SAMPLE
    install -m 0644 ${S}/*.h ${D}${includedir}/SAMPLE/
}
 
FILES_${PN} = "${libdir}/*.so.* ${includedir}/*"
FILES_${PN}-dev = "${libdir}/*.so"
3. Modify the <plnx-proj-root>/project-spec/meta-user/recipes-apps/libsample/files/Makefile generated from petalinux tools as
APP = sample
 
LIBSOURCES=*.c
OUTS = *.o
NAME := sample
MAJOR = 1.0
MINOR = 1
VERSION = $(MAJOR).$(MINOR)
 
all: lib$(NAME).so
lib$(NAME).so.$(VERSION): $(OUTS)
    $(CC) $(LDFLAGS) $(OUTS) -shared -Wl,-soname,lib$(NAME).so.$(MAJOR) -o lib$(NAME).so.$(VERSION)
 
lib$(NAME).so: lib$(NAME).so.$(VERSION)
    rm -f lib$(NAME).so.$(MAJOR) lib$(NAME).so
    ln -s lib$(NAME).so.$(VERSION) lib$(NAME).so.$(MAJOR)
    ln -s lib$(NAME).so.$(MAJOR) lib$(NAME).so
 
%.o: %.c
    $(CC) $(CFLAGS) -c -fPIC $(LIBSOURCES)
 
clean:
    rm -rf *.o *.so *.so.*
4. Build the library recipe
$ petalinux-build -c libsample

Creating Apps(which uses libraries) in v2017.1 or later PetaLinux Project


1. Create a library using petalinux tools
$ petlainux-create -t apps --template c --name sampleapp --enable
2. Modify the sampleapp.bb recipe file <plnx-proj-root>/project-spec/meta-user/recipes-apps/sampleapp/sampleapp.bb as
#
# This file is the sampleapp recipe.
#
 
SUMMARY = "Simple sampleapp application"
SECTION = "PETALINUX/apps"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
 
FILESEXTRAPATHS_prepend := "${THISDIR}/files:"
 
SRC_URI = " \
          file://sampleapp.c \
         "
 
S = "${WORKDIR}"
 
TARGET_CC_ARCH += "${LDFLAGS}"
 
#This uses libsample.so.1.0.1 (-lsample)
do_compile() {
    ${CC} ${CFLAGS} sample.c -lsample -o sampleapp
}
 
do_install() {
    install -d ${D}${bindir}
    install -d -m 0755 sampleapp ${D}${bindir}
}
 
FILES_${PN} += "sampleapp"
 
3. Modify the <plnx-proj-root>/project-spec/meta-user/recipes-apps/libsample/files/sampleapp.c generated from petalinux tools as
#include <stdio.h>
#include <SAMPLE/sample.h>  // From above libs <TARGET_ROOTFS>/usr/lib/SAMPLE/sample.h
 
int main(int argc, char **argv)
{
    printf("Hello World!\n");
    sample_hello();
    return 0;
}
4. Build the apps recipe
$ petalinux-build -c sampleapp

Related Links

Xilinx Yocto