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

Table of Contents

Documentation

The first place you should start to better understand many details of the Yocto project is the Yocto project website

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 .

Note: Any patches in the project will not be applied to an external source tree for the Linux kernel or u-boot. The user should apply patches to the external source tree.

How to Modify inittab or getty in a 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 the FSBL in a 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/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
Note that patches should be generated based on the embeddedsw GIT repo which contains FSBL as the patches require the path into the repo to apply correctly.

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
# Note: do_configure_prepend task section is required only for 2017.1 release
# Refer https://github.com/Xilinx/meta-xilinx-tools/blob/rel-v2017.2/classes/xsctbase.bbclass#L29-L35
 
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(optional)
XSCTH_BUILD_DEBUG = "1"
 
#Enable appropriate FSBL debug or compiler flags
YAML_COMPILER_FLAGS_append = " -DXPS_BOARD_ZCU102"
 
# Note: This is not required if you are using Yocto
# CAUTION!: EXTERNALXSCTSRC and EXTERNALXSCTSRC_BUILD is required only for 2018.2 and below petalinux releases
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

#Note: In v2018.1 PetaLinux release onwards "petalinux-build -x mrproper" command will remove <plnx-proj-root>/components/plnx_workspace directory
$ rm -rf <plnx-proj-root>/components/plnx_workspace
5. Rebuilding FSBL components
$ petalinux-build -c bootloader

How to Patch the PMU Firmware in a 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/pmu-firmware and files directory in meta-user layer as
$ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-bsp/pmu/files

#For v2018.1 release onwards
$ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-bsp/pmu-firmware/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

#For v2018.1 release onwards
$ cp 0001-PMUFW.patch <plnx-proj-root>/project-spec/meta-user/recipes-bsp/pmu-firmware/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

#For v2018.1 or later PetaLinux releases only
$ vim <plnx-proj-root>/project-spec/meta-user/recipes-bsp/pmu-firmware/pmu-firmware_%.bbappend


# Patch for PMUFW
# Note: do_configure_prepend task section is required only for 2017.1 release
# Refer https://github.com/Xilinx/meta-xilinx-tools/blob/rel-v2017.2/classes/xsctbase.bbclass#L29-L35
 
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 or compiler flags
YAML_COMPILER_FLAGS_append = " -DENABLE_EM"
 
# Note: This is not required if you are using Yocto
# CAUTION!: EXTERNALXSCTSRC and EXTERNALXSCTSRC_BUILD is required only for 2018.2 and below petalinux releases
EXTERNALXSCTSRC = ""
EXTERNALXSCTSRC_BUILD = ""

4. Remove the <plnx-proj-root>/components/plnx_workspace and clean your project workspace before rebuilding PMUFW components
$ petalinux-build -x mrproper

#Note: In v2018.1 or later PetaLinux release "petalinux-build -x mrproper" command will remove <plnx-proj-root>/components/plnx_workspace directory
$ rm -rf <plnx-proj-root>/components/plnx_workspace
5. Rebuilding PMUFW components
$ petalinux-build -c pmufw


Configuring the FSBL and PMUFW Debugs in a PetaLinux Project


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

#For v2018.1 or later PetaLinux releases only
$ vim <plnx-proj-root>/project-spec/meta-user/recipes-bsp/pmu-firmware/pmu-firmware_%.bbappend
To enable debugs in PMUFW refer this https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/18841724/PMU+Firmware page for DEBUG flags list
#Add debug for PMUFW
XSCTH_BUILD_DEBUG = "1"
 To add compiler flags in PMUFW, see PMU FW Build Flags:


Note that the method of loading the PMUFW can prevent early prints (during code initialization) from being output.  See PMU Firmware > Using FSBL to load PMUFW for additional details.


#Add compiler flags for PMUFW
YAML_COMPILER_FLAGS_append = " -DDEBUG_MODE -DXPFW_DEBUG_DETAILED"
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"

To add BSP flags(debug mode) in PMUFW: For example if you want to enable https://github.com/Xilinx/embeddedsw/blob/release-2018.3/lib/sw_apps/zynqmp_pmufw/misc/xfpga_config.h#L34 debug is Xil BSP libraries

#Add BSP flags for PMUFW
YAML_BSP_CONFIG += "debug_mode"
YAML_BSP_CONFIG[debug_mode] = "set,true"


By pass PMUFW debug logs on different UART console:
YAML_SERIAL_CONSOLE_STDIN_forcevariable = "psu_uart_1"
YAML_SERIAL_CONSOLE_STDOUT_forcevariable = "psu_uart_1"
Remove the <plnx-proj-root>/components/plnx_workspace and clean you project workspace before rebuilding FSBL or PMUFW components
$ petalinux-build -x mrproper

#Note: In v2018.1 or later PetaLinux release "petalinux-build -x mrproper" command will remove <plnx-proj-root>/components/plnx_workspace directory
$ rm -rf <plnx-proj-root>/components/plnx_workspace
$ petalinux-build

How to Update psu_init Files in PetaLinux Project

Note: PSU_INIT files are generated based on HDF or XSA parsing, Hence these files can't be patched on top of embedded_sw repo using Yocto SRC_URI variable. Alternatively we can update these files by editing <plnx-proj-root>/project-spec/hw-description/psu_init.c and copy to FSBL build workspace during do_compile_prepend bitbake task.


1. Modify <plnx-proj-root>/project-spec/hw-description/psu_init.c file as per your requirement 

2. Create a FSBL bbappend file under <plnx-proj-root>/project-spec/meta-user/recipes-bsp/fsbl/fsbl_%.bbappend and add below content to update psu_init.c changes to FSBL

# For 2018.2 and below versions
do_compile_prepend(){
   install -m 0644 ${TOPDIR}/../project-spec/hw-description/psu_init.c ${TOPDIR}/../components/plnx_workspace/fsbl/fsbl_hwproj/psu_init.c
}

# For 2018.3 and later versions we removed FSBL and PMUFW external workspace from <plnx-proj-root>/components/plnx_workspace directory
# to align Yocto patching mechanism. Hence we are copying the files to build workspace(${B})
# https://github.com/Xilinx/meta-xilinx-tools/blob/rel-v2018.3/classes/xsctbase.bbclass#L14
do_compile_prepend(){
   install -m 0644 ${TOPDIR}/../project-spec/hw-description/psu_init.c ${B}/fsbl/psu_init.c
}

How to Add Pre-built Libraries in 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/libcpsample/files
2. Copy pre-built libraries files and header file required for libraries to <plnx-proj-root>/project-spec/meta-user/recipes-apps/libcpsample/files as
$ cp libcpsample.so.1.0.1 <plnx-proj-root>/project-spec/meta-user/recipes-apps/libcpsample/files
$ cp cpsample.h <plnx-proj-root>/project-spec/meta-user/recipes-apps/libcpsample/files
3. Add the below content to recipe file
#
# This file is the libcpsample recipe.
#
 
SUMMARY = "Sample pre-built library copy to rootfs"
SECTION = "libs"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
 
SRC_URI = " \
    file://libcpsample.so.1.0.1 \
    file://cpsample.h \
"
 
S = "${WORKDIR}"
 
# Add dependency libraires if any
# for example DEPENDS = "libpcap"
 
# If you need to create a symbolic link using the pre-built libraries you should use oe_soinstall.
# This copies libraries to "{TARGET_ROOTFS}/usr/lib" directory and create a symlink as
# lrwxrwxrwx libcpsample.so.1.0 -> libcpsample.so.1.0.1
# -rwxr-xr-x libcpsample.so.1.0.1
 
do_install() {
    install -d ${D}${libdir}
    oe_soinstall ${S}/libcpsample.so.1.0.1 ${D}${libdir}
    install -d -m 0655 ${D}${includedir}/CPSAMPLE
    install -m 0644 ${S}/*.h ${D}${includedir}/CPSAMPLE/
}
 
# Inhibit warnings about files being stripped
INSANE_SKIP_${PN} = "ldflags"
INSANE_SKIP_${PN} = "already-stripped"
 
# If you don't have .h file to copy to /usr/include add something like below
# FILES_${PN} = "${libdir}/*.so.*"
 
FILES_${PN} = "${libdir}/*.so.* ${includedir}/*"
FILES_${PN}-dev = "${libdir}/*.so"
4. Build the pre-built library recipe
$ petalinux-build -c libcpsample


Creating Libraries in a 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 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}"
 
DEPENDS += " libsample"
 
#This uses libsample.so.1.0.1 (-lsample)
do_compile() {
    ${CC} ${CFLAGS} ${LDFLAGS} -o sampleapp sampleapp.c -lsample
}
 
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

How to Auto Mount SD card in Yocto Recipes


1. Create a base-files directory in meta-user layer as
$ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-core/base-files
2. Create a base-files_%.bbappend file and add below content
$ vim <plnx-proj-root>/project-spec/meta-user/recipes-core/base-files/base-files_%.bbappend
 
#base-files_%.bbappend content
 
dirs755 += "/media/card"
 
do_install_append() {
    sed -i '/mmcblk0p1/s/^#//g' ${D}${sysconfdir}/fstab
}
3. Rebuild rootfs
$ petalinux-build

How to Configure a Second Ethernet Interface(eth1) to Get the IP Address from DHCP in Yocto Recipes

Method 1: Append auto eth1 to existing interfaces file


1. Create a init-ifupdown directory in meta-user layer as
$ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-core/init-ifupdown/
2. Create a init_ifupdown_%.bbappend file and add below content
$ vim <plnx-proj-root>/project-spec/meta-user/recipes-core/init-ifupdown/init-ifupdown_%.bbappend
 
# init-ifupdown_%.bbappend content
 
do_install_append() {
    sed -i '/iface eth0 inet dhcp/ a auto eth1' ${D}${sysconfdir}/network/interfaces
}
3. Rebuild rootfs
$ petalinux-build

Method 2: Use your own interfaces file


1. Create a init-ifupdown directory in meta-user layer as
$ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-core/init-ifupdown/files
2. Create your own network interface file as show below
$ vim <plnx-proj-root>/project-spec/meta-user/recipes-core/init-ifupdown/files/myinterfaces
 
# myinterfaces content
 
# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
 
# The loopback interface
auto lo
iface lo inet loopback
 
# Wireless interfaces
iface wlan0 inet dhcp
    wireless_mode managed
    wireless_essid any
    wpa-driver wext
    wpa-conf /etc/wpa_supplicant.conf
 
iface atml0 inet dhcp
 
# Wired or wireless interfaces
auto eth0
iface eth0 inet dhcp
 
# Add auto config for eth1
auto eth1
iface eth1 inet dhcp
 
# Ethernet/RNDIS gadget (g_ether)
# ... or on host side, usbnet and random hwaddr
iface usb0 inet static
    address 192.168.7.2
    netmask 255.255.255.0
    network 192.168.7.0
    gateway 192.168.7.1
 
# Bluetooth networking
iface bnep0 inet dhcp
3. Create a init_ifupdown_%.bbappend file and add below content
$ vim <plnx-proj-root>/project-spec/meta-user/recipes-core/init-ifupdown/init-ifupdown_%.bbappend
 
# init-ifupdown_%.bbappend content
 
SRC_URI += " \
        file://myinterfaces \
        "
 
FILESEXTRAPATHS_prepend := "${THISDIR}/files:"
 
# Overwrite interface file with myinterface file in rootfs
do_install_append() {
     install -m 0644 ${WORKDIR}/myinterfaces ${D}${sysconfdir}/network/interfaces
}
4. Rebuild rootfs
$ petalinux-build


How to Run RootFS Post Process Command in PetaLinux


Below is one of the example and you can add rootfs post process command in <plnx-proj-root>/project-spec/meta-user/recipes-core/images/petalinuxbsp-user-image.bbappend file. You add a function which can perform modifying file, deleting and adding file and directories.
  1. Create a petalinux-user-image.bbappend file in meta-user layer as show below if doesn't exists(Note: Don't use project-spec/meta-user/recipes-core/images/petalinux-image-full.bbappend(2018.3 or later) or petalinux-image.bbappend(2018.2 or below))

    $ vim <plnx-proj-root>/project-spec/meta-user/recipes-core/images/petalinux-user-image.bbappend


  2. Create a post process function as shown below in petalinux-user-image.bbappend 

    # petalinux-user-image.bbappend content
    
    # Auto DHCP for second network interface
    rootfs_postprocess_function() {
    	if [ -e ${IMAGE_ROOTFS}/etc/network/interfaces ]; then
    		sed -i '/iface eth0 inet dhcp/ a auto eth1' ${IMAGE_ROOTFS}/etc/network/interfaces
    	fi
    }
    
    ROOTFS_POSTPROCESS_COMMAND_append = " \
      rootfs_postprocess_function; \
    "


  3. Clean petalinux-user-image sstate and rebuild petalinux-user-image recipe.

    $ petalinux-build -x cleansstate
    $ petalinux-build


How to Add Users and Set Passwords for Users in PetaLinux

  1. Create a petalinux-user-image.bbappend file in meta-user layer as show below (Note: Don't use project-spec/meta-user/recipes-core/images/petalinux-image-full.bbappend(2018.3 or later) or petalinux-image.bbappend(2018.2 or below))

    $ vim <plnx-proj-root>/project-spec/meta-user/recipes-core/images/petalinux-user-image.bbappend


  2. Inherit extrausers.bbclass from openembedded-core and add users in petalinux-user-image.bbappend 

    # petalinux-user-image.bbappend content
    inherit extrausers
    
    EXTRA_USERS_PARAMS = "\
        usermod -P root root; \
        useradd -P test@123 test1; \
        useradd -P test@987 test2; \
        "


  3. Rebuild rootfs

    $ petalinux-build -x cleanall
    $ petalinux-build


How to Override a Recipe Variable in PetaLinux

  1. First step is to find the recipe directory hierarchy, search the recipe in https://layers.openembedded.org/layerindex/branch/rocko/recipes/
  2. Here we are taking Protobuf package as example as SRC_URI like is broken in Yocto 2.4 release. Same concept applies to all the recipes. Protobuf is categorized in meta-openembedded/meta-oe/recipes-devtools/protobuf
  3. Create a directory recipes-devtools/protobuf directory in <plnx-porj-root>/project-spec/meta-user

    $ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-devtools/protobuf


  4. Create a protobuf_%.bbappend file. Note: The % wildcard is actually allow matching of the name and version up to the point of encountering the %. This approach will allow for matching of the major or major.minor. version of recipes.

    $ vim <plnx-proj-root>/project-spec/meta-user/recipes-devtools/protobuf/protobuf_%.bbappend


  5. Now override a recipe variable for protobuf recipe.

    # protobuf_%.bbappend content
    SRC_URI = "git://github.com/google/protobuf.git"


  6. Build protobuf

    $ petalinux-build -c protobuf -x cleanall
    $ petalinux-build -c protobuf


How to Enable Verbose in sysvinit

  1. First step is to find the recipe directory hierarchy, search the recipe in http://layers.openembedded.org/layerindex/branch/thud/recipes/
  2. sysvinit is categorized in meta/recipes-core/sysvinit
  3. Create a directory recipes-core/sysvinit directory in <plnx-porj-root>/project-spec/meta-user

    $ mkdir -p <plnx-proj-root>/project-spec/meta-user/recipes-core/sysvinit


  4. Create a sysvinit_%.bbappend file. Note: The % wildcard is actually allow matching of the name and version up to the point of encountering the %. This approach will allow for matching of the major or major.minor. version of recipes.

    $ vim <plnx-proj-root>/project-spec/meta-user/recipes-core/sysvinit/sysvinit_%.bbappend


  5. Now enable verbose for sysvinit recipe (refer http://cgit.openembedded.org/openembedded-core/tree/meta/recipes-core/sysvinit/sysvinit/rcS-default?h=thud#n15)

    # sysvinit_%.bbappend content
    do_install_prepend () {
            sed -i 's/VERBOSE=no/VERBOSE=yes/' ${WORKDIR}/rcS-default
    }


  6. Clean and rebuild sysvinit

    $ petalinux-build -c sysvinit -x cleanall
    $ petalinux-build -c sysvinit


How to reduce build time using SSTATE CACHE

The OpenEmbedded(OE) build system produces a lot of intermediate output when processing the many tasks entailed in building the packages comprising the Linux OS stack. SSTATE(Shared State) CACHE provides a cache mechanism which drastically reduces build time, especially when you create a new PetaLinux project and run "petalinux-build --sdk". The OE build system detects changes in the "inputs" to a given task by creating a checksum (or signature) of the task's inputs. If the checksum changes, the system assumes the inputs have changed and the task needs to be rerun. The intermediate output can be reused for future builds and shared between multiple build environments to speed up the build process.

Two key variables are used with the shared state cache feature.  The SSTATE_DIR variable contains the path to the shared state cache. The variable DL_DIR tells BitBake where to place the source downloads. The default setting places the files in the directory downloads beneath the top directory(TOPDIR) of your build environment. The variable TOPDIR contains the full (absolute) path to the build environment. Source downloads can be shared among multiple build environments.

For best practice you need to set DL_DIR and SSTATE_DIR outside the petalinux project so that when you run "petalinux-build -s mrproper" which removes build(TOPDIR)directory forcefully, but retaining DL_DIR and SSTATE_DIR directories. These contents can be reused for incremental build or building another project. Note: You can use DL_DIR content for all the PetaLinux project irrespective of the architecture(AARCH64, ARM, MicroBlaze) but SSTATE_DIR can be used for only one architecture. For example, if you build a AARCH64 PetaLinux project the content of SSTATE_DIR can be reused for another AARCH64 project but not for ARM or MicroBlaze projects.

In order to utilize the sstate cache feature in PetaLinux projects follow these steps.

  1. Download and untar the PetaLinux release sstate-cache files from https://www.xilinx.com/support/download/index.html/content/xilinx/en/downloadNav/embedded-design-tools.html. Here we are taking 2019.2 as an example.
  2. Go to your 2019.2 PetaLinux project and set the following options using petalinux-config

    $ petalinux-config ---> Yocto Settings ---> Add pre-mirror url ---> file://<path_to_downloads>
    $ petalinux-config ---> Yocto Settings ---> Local sstate feeds settings ---> local sstate feeds url ---> <path_to_sstate_aarch64_2019.2/aarch64>


    $ petalinux-config ---> Yocto Settings ---> Add pre-mirror url ---> file:///opt/xilinx/petalinux/2019.2/downloads
    $ petalinux-config ---> Yocto Settings ---> Local sstate feeds settings ---> local sstate feeds url ---> /opt/xilinx/petalinux/2019.2/sstate_aarch64_2019.2/aarch64


    Note: For PREMIRRORS usage you need to add the file, git, http or https protocol followed by the path, but this is not true for SSTATE feeds.


  3. For setting up the download and shared state directories add the following variables in the <plnx-proj-root>/project-spec/meta-user/conf/petalinuxbsp.conf file.

    DL_DIR = "<PATH-TO-DOWNLOADS-DIRECTORY>/<PetaLinux-Version>/downloads"
    SSTATE_DIR = "<PATH-TO-SSTATE-DIRECTORY>/<PetaLinux-Version>/sstate-cache/<ARCH>"


    DL_DIR = "/home/$USER/plnx-workspace/2019.2/downloads"
    SSTATE_DIR = "/home/$USER/plnx-workspace/2019.2/sstate-cache/aarch64"


    Note: DL_DIR and SSTATE_DIR directories should have read and write permission.


  4. Now you need to start the build and if you are building the SDK (petalinux --sdk) then enabling the network is mandatory for a fresh build.

    $ petalinux-build
    $ petalinux-build --sdk


  5. If you are building without network access then set the BB_NO_NETWORK variable.

    $ petalinux-config ---> Yocto Settings ---> [*] Enable BB NO NETWORK


    Note: In case the project was built previously, run petalinux-build -x mrproper with above variables to reduce build time especially when using petalinux-build --sdk.


Yocto References 

https://wiki.yoctoproject.org/wiki/Technical_FAQ
https://wiki.yoctoproject.org/wiki/Building_your_own_recipes_from_first_principles
https://www.yoctoproject.org/docs/current/dev-manual/dev-manual.html#new-recipe-writing-a-new-recipe
https://www.openembedded.org/Layers_FAQ
https://www.yoctoproject.org/docs/current/bitbake-user-manual/bitbake-user-manual.html


Related Links
Xilinx Yocto