mandag den 10. november 2025

Working with Yocto: Building an OpenSTLinux image

Back to overview

Building and booting a base image 

In order to work with OpenSTLinux (or any Yocto tree) you need to run Linux. OpenSTLinux is tested on Ubuntu 24.04 which is also what I use for this. You also need an SD card reader and an empty micro-SD card.

 To prepare the machine for development you (one time) need to install a number of packages - this command nicely does it all:

  sudo apt-get install -y gawk wget git-core diffstat unzip texinfo \
    build-essential chrpath socat cpio python3 python3-pip python3-pexpect \
    xz-utils debianutils iputils-ping libsdl1.2-dev xterm \
    repo bmap-tools bsdmainutils gcc-multilib git-lfs libgmp-dev \
     libmpc-dev libssl-dev lz4 pylint python3-git
  
You also need to configure your GIT identity, replacing the example mail and names with real ones:
  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"
Then we create a working directory for our stm32mp work, and fetch the sources (or rather description) of openstlinux using the repo tool:

mkdir -p ~/stm32mp/openstlinux && cd ~/stm32mp/openstlinux
repo init -u https://github.com/STMicroelectronics/oe-manifest.git \
  -b refs/tags/openstlinux-6.6-yocto-scarthgap-mpu-v25.08.27
repo sync

Now it is time to figure out which machine to build for and which distro to build.  

To list the machines that you can target with a given Yocto tree, you can do

find layers/ -path "*/conf/machine/*.conf" -printf "%f\n" | sed 's/\.conf$//' 

One of the machines is the stm32mp15-disco which is the discovery board description. That is the one we will be buildling for.

 To list the distros supported in the tree, you can do

find layers -path "*/conf/distro/*.conf" -printf "%f\n" | sed 's/\.conf$//'

The openstlinux distribution in this tree is called openstlinux-weston. 

To get started we need to start a development environment. While being inside ~/stm32mp/openstlinux we do:

DISTRO=openstlinux-weston MACHINE=stm32mp15-disco \
  source layers/meta-st/scripts/envsetup.sh
This drops you into ~/stm32mp/openstlinux/build-openstlinuxweston-stm32mp15-disco, which is the build directory for this machine. Now in principle we can start building, but to speed up the build we should enable multicore builds. You should specify 1,5x the number of threads as you have cores to balance usability of the machine and build time. To enable multicore builds open conf/local.conf in your favorite editor, and write this in the end
# Multicore build
# Run N BitBake tasks in parallel (fetch/compile/package, etc.)
BB_NUMBER_THREADS = "12"

# Pass -jN to make/ninja inside recipes
PARALLEL_MAKE = "-j 12"

Before building (and after each rebuild) we need to disable apparmor (on Ubuntu 24.04 as per 3/11/2025) as it interferes with bitbake - the tool Yocto uses for builds:
echo 0 | sudo tee /proc/sys/kernel/apparmor_restrict_unprivileged_userns
Then we are ready to build. Note that you should find something else to do while doing this, as it can take very long. On my machine this step takes around 2,5 hours:
bitbake st-image-weston

Now it is time to make the SD card. Insert a fresh card in your SD card reader and eject it. Then we go to the directory where the image files have been put:

cd ./tmp-glibc/deploy/images/stm32mp15-disco/
  
In there we find
scripts/create_sdcard_from_flashlayout.sh
that can be used to either create an image to write to a flash card. . To generate the flash image we go:
./scripts/create_sdcard_from_flashlayout.sh \
flashlayout_st-image-weston/opteemin/FlashLayout_sdcard_stm32mp157f-dk2-opteemin.tsv
This creates a .raw file with the full disk image. Now follow the directions on the screen. First unmount all drives:
sudo umount `lsblk --list | grep mmcblk0 | grep part | \
gawk '{ print $7 }' | tr '\n' ' '`
Then write the image
sudo dd if=FlashLayout_sdcard_stm32mp157f-dk2-opteemin.raw \
of=/dev/mmcblk0 bs=8M conv=fdatasync status=progress

Note that the sd card can be called something else on your machine - on mine it is /dev/mmcblk0. Be patient and wait until the prompt returns for the card to actually be written. It may seem like the prompt locks up but it is actually just flushing cache. 

Now insert the programmed sd card in the evaluation board. Before booting the drive, connect to the discovery board using picocom:

picocom -b 115200 /dev/ttyACM0

and then power up the board. The terminal will now show the bootloader and linux kernel booting. During boot the file systems are put in the right size, so it will take a bit of time to boot. After boot you should see the demo screen show up, and if you press enter in the terminal, you will see a root prompt. 

Congratulations, you have now built your first linux image and booted it successfully.

Ingen kommentarer:

Send en kommentar