2026년 3월 5일 목요일

Embedded Linux Programming with STM32MP257F-DK board(Episode II)

거의 6년만에 STM32MP 보드를 다시 하나 입수했다(이번에는 STM32MP257F-DK 보드다). 이번 시간 부터는 앞으로 몇차례에 걸쳐서, STM32MP257F-DK 보드 기반 embedded linux와 관련한 다양한 주제를 다뤄 보고자 한다. 이번이 그 두번째 시간이다. 😎

 
The 2nd episode

목차
3. OpenSTLinux Yocto Project 기반 Booting Image 생성하기
4. Yocto Project devtool 소개하기
5. Linux Kernel - Device Tree & Device Drivers
6. Bootloader - TF-A, u-boot
7. Buildroot 기반 booting image 생성하기
8. Rust Kernel Programming
9. Cortex-M33 CPU#1 - STM32CubeIDE 기반 Application 돌려 보기
10. Cortex-M33 CPU#2 - Zephyr RTOS Application 돌려보기
11. WireGuard Rust 돌려 보기
12. Qt6 Application 돌려 보기 - C++ or PySide6
References

KeywordsSTM32MP257F-DK, booting flow, u-boot, linux kernel, device tree, yocto project, Buildroot, Rust kernel module, STM32CubeIDE, Zephyr RTOS, PySide6

Embedded linux(echosystem)는 점점 더 복잡해져 가고 있다. 이중 삼중의 bootloader(거기에 보안 기능까지), device tree로 무장한 linux kernel, kernel에 합류한 Rust, build system의 대세 Yocto project ! 어느 하나 무시할 수 없는 것들 뿐이다. 그 중에서도 이번 시간에는 욕나오고 토나오는 Yocto Project를 좀 더 깊히 파헤쳐 보기로 하자. 😋

헛둘 헛둘~ 열심히 달리다 보면 욕나오고 토나온다 ㅋㅋ

3. OpenSTLinux Yocto Project 기반 Booting Image 생성하기
이번 장에서는 Cortex-A35 CPU에 bootloader & Linux를 올리기 위해, yocto project를 내려 받아 build하는 절차를 알아 보고자 한다. 이번 장에서 소개하는 내용은 아래 site의 내용을 기초로 하였다.


[그림 3.1] OpenSTLinux 주요 components [출처 - 참고문헌 8]

3.1  Yocto Project 개요
Yocto project와 관련해서는 (내용이 방대하긴 하지만) 아래 문서를 참조하는게 답이다. 💯

이 밖에도 필자가 정리한 내용도 있으니, 함께 참조해 주면 좋을 듯하다. 😋

=========================================================================
1) yocto 기초편 - 정말 기초 중의 기초 💃

2) beaglebone black board 기반 yocto project 소개 (아래 link 1장 참조 - kernel/bootloader build, sdk 생성 방법)
3) freescale riot board 기반 yocto project 소개 (아래 link 5장 참조 - 특별한 내용은 없음.)

4) freescale riot board 기반 yocto project 소개 (아래 link 2장 참조 - kernel module 추가 방법)
5) stm32mp157c-dk board 기반 yocto project 소개 (아래 link 3장 참조 - 특별한 내용은 없음.)
6) beagleplay board 기반 yocto project 소개 (아래 link 3장 참조 - simplest yocto setup 내용 포함)

(*) 정리를 해 놓고 보니, devtool을 이용한 방법이 빠져 있다. 따라서 이번 posting에서는 이 부분을 중점적으로 소개해 보도록 하자.
=========================================================================

<Yocto Project로 booting image 생성 작업 요약>
 -> yocto project가 build system이란 건 모두가 아는 사실이다. 😋

1) 자신의 board에 맞는 BSP layer 추가
2) 일반 application이 포함되는 신규 layer 추가
3) 남들이 구현해 놓은 meta-XXX layer 추가(아래 site 참조)

4) 여기까지 작업한 내용을 기준으로 booting 가능한 image 1차 생성

5) application 신규 추가 or 기존 application & library 수정(patch) 작업
6) (BSP 개발자의 경우) kernel 및 bootloader에 device driver 신규 추가 혹은 기존 코드 수정(patch 작업)
  -> 5, 6 단계에서 devtool 활용(단, 순수 bitbake 명령으로도 가능함)

7) 최종 image build하여, 동작가능하게 만들기
-------------------------------------------------------------------------

3.2 OpenSTLinux-6.6 Yocto project build 하기
먼저, OpenSTLinux-6.6용 yocto project를 내려 받아 booting용 image를 생성해 보도록 하자.

$ mkdir Distribution-Package; cd Distribution-Package;

repo init -u https://github.com/STMicroelectronics/oe-manifest.git -b refs/tags/openstlinux-6.6-yocto-scarthgap-mpu-v26.02.18
Downloading Repo source from https://gerrit.googlesource.com/git-repo
..
repo has been initialized in /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package

repo sync
Syncing: 57% (4/7) 0:16 | 2 jobs | 0:15 meta-openembedded @ layers/meta-openembedded
...
Syncing:  85% (6/7), done in 58.097s
Syncing: 100% (7/7) 1:47 | 1 job | 0:48 bitbake @ layers/openembedded-core/bitbakerepo sync has finished successfully.

$ tree
  -> 그 결과는 대략 다음과 같이 요약해 볼 수가 있다.
--------------------------------------------------------------------------
Distribution-Package                      OpenSTLinux distribution
├── layers 
│    ├── meta-openembedded                Collection of layers for the OpenEmbedded-Core universe (OpenEmbedded standard)
│    ├── meta-st
│    │   ├── meta-st-openstlinux          STMicroelectronics layer that contains the frameworks and images settings for the OpenSTLinux distribution
│    │   ├── meta-st-stm32mp              STMicroelectronics layer that contains the description of the BSP for the STM32 MPU devices
│    │   │   ├── recipes-bsp
│    │   │   │   ├── alsa                 Recipes for ALSA control configuration
│    │   │   │   ├── ddr-firmware         Firmware for DDR PHY on STM32MP
│    │   │   │   ├── drivers              Recipes for Vivante GCNANO GPU kernel drivers
│    │   │   │   ├── fip-stm32mp          FIP generation
│    │   │   │   ├── trusted-firmware-a   Recipes for TF-A
│    │   │   │   ├── trusted-firmware-m   For STM32MP2 series - Recipes for TF-M
│    │   │   │   └── u-boot               Recipes for U-Boot
│    │   │   ├── recipes-connectivity
│    │   │   │   └── bluetooth            Systemd service to suspend/resume correctly bluetooth
│    │   │   ├── recipes-core
│    │   │   │   ├── [...]                bbappend recipes to update some core services
│    │   │   ├── recipes-extended
│    │   │   │   ├── external-dt          Recipes for providing device tree files for STM32 MPU devices outside of BSP components
│    │   │   │   ├── linux-examples       Recipes for Linux examples for STM32 MPU devices
│    │   │   │   ├── m4coredump           For STM32MP15x lines  only - Recipes for script to manage coredump of cortexM4
│    │   │   │   ├── stm32mp1-projects    For STM32MP15x lines  only - Recipes for firmware examples for Cortex M4
│    │   │   │   ├── stm32mp2-projects    For STM32MP2 series  - Recipes for firmware examples for Cortex M33/M0
│    │   │   │   └── stm32mp-g0           For STM32MP13x lines  only - Recipes for G0 USB firmware
│    │   │   ├── recipes-graphics
│    │   │   │   ├── gcnano-userland      Recipes for Vivante libraries OpenGL ES, OpenVG and EGL (multi backend)
│    │   │   │   └── [...]
│    │   │   ├── recipes-kernel
│    │   │   │   ├── linux                Recipes for Linux kernel
│    │   │   │   ├── linux-firmware       Recipes for Linux firmwares (example, Bluetooth firmware)
│    │   │   │   └── [...]
│    │   │   ├── recipes-security
│    │   │   │   └── optee                Recipes for OPTEE
│    │   │   ├── recipes-st
│    │   │   │   ├── images               Recipes for the bootfs and userfs partitions binaries
│    │   │   │   └── [...]
│    │   │   └── [...]
│    │   ├── meta-st-stm32mp-addons       STMicroelectronics layer that helps managing the STM32CubeMX integration
│    │   └── scripts
│    │       ├── envsetup.sh              Environment setup script for Distribution Package
│    │       └── [...]
│    └── openembedded-core                Core metadata for current versions of OpenEmbedded (standard)
_______________________________________________________________

DISTRO=openstlinux-weston MACHINE=stm32mp2 source layers/meta-st/scripts/envsetup.sh
 -> OpenEmbedded setup script를 실행한다.
 -> 위 명령을 실행하면, 자동으로 build-openstlinuxweston-stm32mp2 디렉토리로 이동하게 된다.

$ tree
  -> 그 결과는 대략 다음과 같이 요약해 볼 수가 있다.

openstlinux-[...]             OpenSTLinux distribution
├── build-<distro>-<machine>  Build directory
│   ├── conf
│   │   ├── bblayers.conf     Local configuration file
│   │   ├── local.conf        Layers configuration file
│   │   └── [...]
│   └── workspace
├── layers
│   ├── meta-openembedded
│   ├── [...]
__________________________________________

경우에 따라서 위의 내용 중, target board  설정과 관련된 local.conf 파일과 layer 설정 파일인 bblayer.conf 파일을 수정해야 하는 경우도 있으니, 눈여겨 살펴 보기로 한다.

[그림 3.2] build-openstlinuxweston-stm32mp2/conf/local.conf

[그림 3.3] build-openstlinuxweston-stm32mp2/conf/bblayers.conf

자, 그럼 이제부터 bitbake를 이용하여 OpenSTLinux weston image(with basic Wayland support)를 만들어 보자.

[그림 3.4] bitbake의 역할 [출처 - 참고문헌 9]

$ bitbake st-image-weston
  -> 휴~ build하는데 거의 4시간 반이나 걸렸다. 결과 파일의 분량도 엄청난다(오래 걸리는 이유는 stm32mp2 계열의 모든 image를 build하기 때문임). 😂

최종 build 결과를 확인해 보면 다음과 같다(일부러 모든 파일 목록을 나열해 보기로 한다).

$ cd tmp-glibc/deploy/images/stm32mp2
$ ls -la
합계 2626964
drwxr-xr-x 11 chyi chyi       4096  3월  2 20:28 .
drwxr-xr-x  3 chyi chyi       4096  3월  2 17:09 ..
drwxr-xr-x  7 chyi chyi      12288  3월  2 19:54 arm-trusted-firmware
drwxr-xr-x  3 chyi chyi       4096  3월  2 19:43 arm-trusted-firmware-m
-rw-r--r--  1 chyi chyi       1308  4월  6  2011 build-st-image-weston-openstlinux-weston-stm32mp2
drwxr-xr-x  2 chyi chyi       4096  3월  2 17:26 devicetree
drwxr-xr-x  2 chyi chyi      40960  3월  2 20:17 fip
drwxr-xr-x  8 chyi chyi       4096  3월  2 20:28 flashlayout_st-image-weston
drwxr-xr-x  2 chyi chyi       4096  3월  2 19:36 kernel
drwxr-xr-x  3 chyi chyi      20480  3월  2 20:17 optee
drwxr-xr-x  2 chyi chyi       4096  3월  2 17:09 scripts
-rw-r--r--  1 chyi chyi    8459714  3월  2 19:37 st-image-resize-initrd-openstlinux-weston-stm32mp2.rootfs-20260302074246.cpio.gz
-rw-r--r--  1 chyi chyi       1884  3월  2 19:36 st-image-resize-initrd-openstlinux-weston-stm32mp2.rootfs-20260302074246.manifest
-rw-r--r--  1 chyi chyi     174956  3월  2 19:37 st-image-resize-initrd-openstlinux-weston-stm32mp2.rootfs-20260302074246.spdx.tar.zst
-rw-r--r--  1 chyi chyi     517339  3월  2 19:36 st-image-resize-initrd-openstlinux-weston-stm32mp2.rootfs-20260302074246.testdata.json
lrwxrwxrwx  1 chyi chyi         80  3월  2 19:37 st-image-resize-initrd-openstlinux-weston-stm32mp2.rootfs.cpio.gz -> st-image-resize-initrd-openstlinux-weston-stm32mp2.rootfs-20260302074246.cpio.gz
lrwxrwxrwx  1 chyi chyi         81  3월  2 19:36 st-image-resize-initrd-openstlinux-weston-stm32mp2.rootfs.manifest -> st-image-resize-initrd-openstlinux-weston-stm32mp2.rootfs-20260302074246.manifest
lrwxrwxrwx  1 chyi chyi         85  3월  2 19:37 st-image-resize-initrd-openstlinux-weston-stm32mp2.rootfs.spdx.tar.zst -> st-image-resize-initrd-openstlinux-weston-stm32mp2.rootfs-20260302074246.spdx.tar.zst
lrwxrwxrwx  1 chyi chyi         86  3월  2 19:36 st-image-resize-initrd-openstlinux-weston-stm32mp2.rootfs.testdata.json -> st-image-resize-initrd-openstlinux-weston-stm32mp2.rootfs-20260302074246.testdata.json
-rw-r--r--  1 chyi chyi     488460  3월  2 20:28 st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246-license_content.html
-rw-r--r--  1 chyi chyi 1041633280  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246.ext4
-rw-r--r--  1 chyi chyi      47893  3월  2 20:28 st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246.license
-rw-r--r--  1 chyi chyi     118830  3월  2 20:26 st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246.manifest
-rw-r--r--  1 chyi chyi    3963424  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246.spdx.tar.zst
-rw-r--r--  1 chyi chyi  195811256  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246.tar.xz
-rw-r--r--  1 chyi chyi     531816  3월  2 20:26 st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246.testdata.json
lrwxrwxrwx  1 chyi chyi         86  3월  2 20:28 st-image-weston-openstlinux-weston-stm32mp2.rootfs-license_content.html -> st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246-license_content.html
lrwxrwxrwx  1 chyi chyi         70  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.rootfs.ext4 -> st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246.ext4
lrwxrwxrwx  1 chyi chyi         73  3월  2 20:28 st-image-weston-openstlinux-weston-stm32mp2.rootfs.license -> st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246.license
lrwxrwxrwx  1 chyi chyi         74  3월  2 20:26 st-image-weston-openstlinux-weston-stm32mp2.rootfs.manifest -> st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246.manifest
lrwxrwxrwx  1 chyi chyi         78  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.rootfs.spdx.tar.zst -> st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246.spdx.tar.zst
lrwxrwxrwx  1 chyi chyi         72  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.rootfs.tar.xz -> st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246.tar.xz
lrwxrwxrwx  1 chyi chyi         79  3월  2 20:26 st-image-weston-openstlinux-weston-stm32mp2.rootfs.testdata.json -> st-image-weston-openstlinux-weston-stm32mp2.rootfs-20260302074246.testdata.json
-rw-r--r--  1 chyi chyi   67108864  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-bootfs-20260302074246.ext4
-rw-r--r--  1 chyi chyi   21248000  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-bootfs-20260302074246.tar
-rw-r--r--  1 chyi chyi   19601080  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-bootfs-20260302074246.tar.xz
lrwxrwxrwx  1 chyi chyi         79  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-bootfs.ext4 -> st-image-weston-openstlinux-weston-stm32mp2.splitted-bootfs-20260302074246.ext4
lrwxrwxrwx  1 chyi chyi         81  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-bootfs.tar.xz -> st-image-weston-openstlinux-weston-stm32mp2.splitted-bootfs-20260302074246.tar.xz
-rw-r--r--  1 chyi chyi  790131712  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-rootfs-20260302074246.ext4
-rw-r--r--  1 chyi chyi  648038400  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-rootfs-20260302074246.tar
-rw-r--r--  1 chyi chyi  128064080  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-rootfs-20260302074246.tar.xz
lrwxrwxrwx  1 chyi chyi         79  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-rootfs.ext4 -> st-image-weston-openstlinux-weston-stm32mp2.splitted-rootfs-20260302074246.ext4
lrwxrwxrwx  1 chyi chyi         81  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-rootfs.tar.xz -> st-image-weston-openstlinux-weston-stm32mp2.splitted-rootfs-20260302074246.tar.xz
-rw-r--r--  1 chyi chyi   87140352  3월  2 20:28 st-image-weston-openstlinux-weston-stm32mp2.splitted-userfs-20260302074246.ext4
-rw-r--r--  1 chyi chyi   67491840  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-userfs-20260302074246.tar
-rw-r--r--  1 chyi chyi   43968020  3월  2 20:28 st-image-weston-openstlinux-weston-stm32mp2.splitted-userfs-20260302074246.tar.xz
lrwxrwxrwx  1 chyi chyi         79  3월  2 20:28 st-image-weston-openstlinux-weston-stm32mp2.splitted-userfs.ext4 -> st-image-weston-openstlinux-weston-stm32mp2.splitted-userfs-20260302074246.ext4
lrwxrwxrwx  1 chyi chyi         81  3월  2 20:28 st-image-weston-openstlinux-weston-stm32mp2.splitted-userfs.tar.xz -> st-image-weston-openstlinux-weston-stm32mp2.splitted-userfs-20260302074246.tar.xz
-rw-r--r--  1 chyi chyi   50331648  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-vendorfs-20260302074246.ext4
-rw-r--r--  1 chyi chyi   32235520  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-vendorfs-20260302074246.tar
-rw-r--r--  1 chyi chyi    4154292  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-vendorfs-20260302074246.tar.xz
lrwxrwxrwx  1 chyi chyi         81  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-vendorfs.ext4 -> st-image-weston-openstlinux-weston-stm32mp2.splitted-vendorfs-20260302074246.ext4
lrwxrwxrwx  1 chyi chyi         83  3월  2 20:27 st-image-weston-openstlinux-weston-stm32mp2.splitted-vendorfs.tar.xz -> st-image-weston-openstlinux-weston-stm32mp2.splitted-vendorfs-20260302074246.tar.xz
-rw-r--r--  1 chyi chyi         68  3월  2 19:00 st-initrd-openstlinux-weston-stm32mp2
drwxr-xr-x  3 chyi chyi       4096  3월  2 20:03 u-boot
--------------------------------------------------------------------------

한편, 위의 결과 파일을 tree 형태로 요약/정리해 보면 다음과 같다.

$ tree
build-<distro>-<machine>/tmp-glibc/deploy
└── images
|   └── stm32mp2
|       ├── flashlayout_st-image-weston                                 Flash layout files (description of the partitions) for the supported flash devices
|       │   ├── FlashLayout_sdcard_stm32mp257f-dk-optee.tsv  Flash layout file for microSD card and OP-TEE
|       │   ├── [...]
|       │   ├── [...]
|       │   └── [...]
|       ├── fip   FIP images to flash pointed by flashlayouts
|       │   ├── fip-<board name>-optee.bin
|       │   └── [...]
|       ├── scripts
|       │   └── create_sdcard_from_flashlayout.sh
|       ├── kernel
|       │   ├── Image                                                      Linux kernel binary image file for bootfs partition
|       │   ├── vmlinux                                                     Debug symbol file for Linux kernel
|       │   └── [...]
|       ├── st-image-weston-openstlinux-weston-<machine>.ext4            Binary for rootfs partition
|       └── [...]
└── [...]
__________________________________________________________

3.3 새로 build한 image를 이용하여 microSD 부팅하기
앞서 build한 결과물이 제대로 동작하는지를 확인해야 한다. 이 부분과 관련해서는 2장에서 이미 자세히 소개한 바 있으니, 이를 참조하기 바란다. 😂


3.4 Yocto SDK 생성하기
이 절에서는 OpenSTLinux Yocto를 위한 SDK(cross toolchain, library 등 포함)를 만들어 보도록 하겠다.


[그림 3.5] SDK generation [출처 - 참고문헌 9]

$ bitbake -c populate_sdk st-image-weston
 -> 역시나 이 과정도 꽤나 올래 걸린다. 😓

$ ls -l

[그림 3.6] SDK 생성 결과 [출처 - 참고문헌 9]

준비된 SDK를 PC에 설치하도록 하자.

$ cd tmp-glibc/deploy/sdk/
$ ./st-image-weston-openstlinux-weston-stm32mp2.rootfs-x86_64-toolchain-5.0.15-snapshot.sh
ST OpenSTLinux - Weston - (A Yocto Project Based Distro) SDK installer version 5.0.15-openstlinux-6.6-yocto-scarthgap-mpu-v26.02.18
===================================================================================================================================
You are about to install the SDK to "/mnt/hdd/workspace/ST/STM32MP257K_DK/Developer-Package/SDK". Proceed [Y/n]? y
Extracting SDK............................................................................................................................................................................................................................................done
Setting it up...done
SDK has been successfully set up and is ready to be used.
Each time you wish to use the SDK in a new shell session, you need to source the environment setup script e.g.
 $ . /mnt/hdd/workspace/ST/STM32MP257K_DK/Developer-Package/SDK/environment-setup-cortexa35-ostl-linux

$ cd /mnt/hdd/workspace/ST/STM32MP257K_DK/Developer-Package
source SDK/environment-setup-cortexa35-ostl-linux 
  -> SDK를 사용하기 전에 반드시 이 명령을 실행해 준다.

echo $ARCH
arm64

echo $CROSS_COMPILE
aarch64-ostl-linux-

$CC --version
aarch64-ostl-linux-gcc (GCC) 13.4.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

echo $OECORE_SDK_VERSION
5.0.15-openstlinux-6.6-yocto-scarthgap-mpu-v26.02.18

<Yocto prebuilt SDK download 하기>
아래 site에는 prebuilt SDK file이 올라와 있다. 앞서 살펴본 것 처럼, SDK를 build하는데 시간이 오래 걸리는 문제가 있으니, 아래 site에서 MP2-DEV-x86 파일을 내려 받아 사용하는 것도 좋을 듯하다.


$ tar xvzf SDK-x86_64-stm32mp2-openstlinux-6.6-yocto-scarthgap-mpu-v26.02.18.tar.gz
----------------------------------------------------------------------------


4. Yocto Project devtool 소개하기
이번 장에서는 yocto project에서 제공하는 devtool을 이용하여 좀 더 쉽게 개발하는 방법을 소개해 보고자 한다.

4.1 devtool 사용하기
Yocto project에 새로운 application을 추가하거나, 기존에 있던 내용을 수정하는 등의 작업은 생각보다 복잡하고, 사전에 파악하고 있어야 할 내용이 많은게 사실이다. 따라서 어느 순간부터 이를 보다 간편하게 해주는 도구가 등장(1.8 Fido)하게 되었는데, 그것이 바로 devtool이다. 😍

사실 devtool이 없이 bitbake만으로도 충분히 개발할 수가 있다. 다만, 원하는 source code나 recipe file의 위치를 일일히 찾아 들어가야 하는 등의 불편함을 감수해야만 한다. 가령, bitbake로 linux kernel code를 찾기 위해서는 아래와 같은 명령을 동원해야만 한다.

$ bitbake -e linux-stm32mp | grep ^WORKDIR=      
WORKDIR="/mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/build-openstlinuxweston-stm32mp2/tmp-glibc/work/stm32mp2-ostl-linux/linux-stm32mp/6.6.116-stm32mp-r3"

이후 code를 수정하고, 이를 commit하는 시점이 되면 또다른 고민에 휩싸이게 마련이다. 즉, 원본을 훼손하지 않고 patch 파일 형태로 적용하려면 어느 recipe를 건드려야 하는지, 해당 recipe는 어디에 있는지 등등, 여러가지로 갑갑한 현실과 마주하게 된다. 😓

하지만, devtool을 이용하면, workspace를 하나 만든 후, 새로운 recipe를 생성하거나, 기존에 있던 코드를 복사해 와 곧바로 사용할 수가 있다. 뿐만아니라, 작업을 마친 후에는 수정한 내용을 다시 원래 code에 반영하도록 하는 방법까지 제공해 준다.
  • 새로운 recipe 추가
  •  bbappend 파일을 생성하므로써 실제 recipe에 append 가능하게 만듦
  • 기존의 bbappend 파일을 갱신시켜 줌.

[그림 4.1] yocto project devtool 개요 [출처 - 참고문헌 9]
📌 devtool은 bitbake server와 통신하는 client처럼 보인다. devtool은 껍데기(?)에 불과하고, 실제 작업은 역시 bitbake가 한다는 얘기다.

아래 디렉토리 구조가 devtool이 자동으로 생성해 주는 workspace 디렉토리 모습이 되겠다.

[그림 4.2] devtool이 생성하는 workspace 디렉토리 구조 [출처 - 참고문헌 9]

$ devtool -h
  -> 다양한 option을 제공한다.

[그림 4.3] devtool usage

이렇게 편리한 도구인데, devtool을 사용하지 않을 이유가 없다. 💢

4.2 신규 Layer 추가하기
그럼 이제부터는 (devtool을 본격적으로 사용하기에 앞서) OpenSTLinux의 내용 중 일부를 수정하거나, 새로운 내용을 추가하는 방법을 소개해 보도록 하자. 뭔가를 추가하기 위해서는, 기존 layer를 직접 이용하기 보다는 새로운 layer를 하나 만들고, 이를 활용하는 방법이 더 나을 수가 있다(물론 기존 layer에서 작업한다고 해서 말릴 수 있는 방법이 있는 건 아니다. 😋).

[그림 4.4] yocto project layers [출처 - 참고문헌 9]

DISTRO=openstlinux-weston MACHINE=stm32mp2 source layers/meta-st/scripts/envsetup.sh

먼저, 기존에 설치되어 있는 layer 목록을 확인해 보자.
bitbake-layers show-layers

[그림 4.5] 현재 사용 중인  layer 확인하기

여기에 meta-my-custo-layer라는 이름으로 custom layer를 하나 생성하도록 해 보자.

bitbake-layers create-layer --priority 7 ../layers/meta-st/meta-my-custo-layer
NOTE: Starting bitbake server...
NOTE: Started PRServer with DBfile: /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/build-openstlinuxweston-stm32mp2/cache/
prserv.sqlite3, Address: 127.0.0.1:36125, PID: 13149
Add your new layer with 'bitbake-layers add-layer ../layers/meta-st/meta-my-custo-layer'


[그림 4.6] tree meta-my-custo-layer

그 다음에, conf/layer.conf 등의 내용을 수정한 후, 아래 명령을 사용하여 새로 생성한 layer를 기존 layer 목록에 추가하도록 하자.

bitbake-layers add-layer ../layers/meta-st/meta-my-custo-layer/
NOTE: Starting bitbake server...
NOTE: Started PRServer with DBfile: /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/build-openstlinuxweston-stm32mp2/cache/
prserv.sqlite3, Address: 127.0.0.1:45081, PID: 14094


이 상태에서 다시, show-layers 명령을 실행해 보면, meta-my-custo-layer가 새롭게 추가된 것을 알 수가 있다.

bitbake-layers show-layers    
NOTE: Starting bitbake server...
NOTE: Started PRServer with DBfile: /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/build-openstlinuxweston-stm32mp2/cache/
prserv.sqlite3, Address: 127.0.0.1:39451, PID: 14302
layer                 path                                                                    priority
========================================================================================================
meta-python           /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/layers/meta-openembedded/meta-python  5
openembedded-layer    /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/layers/meta-openembedded/meta-oe  5
gnome-layer           /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/layers/meta-openembedded/meta-gnome  5
multimedia-layer      /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/layers/meta-openembedded/meta-multimedia  5
networking-layer      /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/layers/meta-openembedded/meta-networking  5
webserver             /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/layers/meta-openembedded/meta-webserver  5
stm-st-stm32mp        /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/layers/meta-st/meta-st-stm32mp  6
st-openstlinux        /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/layers/meta-st/meta-st-openstlinux  5
core                  /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/layers/openembedded-core/meta  5
workspacelayer        /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/build-openstlinuxweston-stm32mp2/workspace  99
meta-my-custo-layer 
  /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/layers/meta-st/meta-my-custo-layer  7

자, 그럼 이제부터는 새로 추가된 meta-my-custo-layer를 활용하여 application이나 각종 library 등에 대한 수정 사항이 반영되도록 작업해 보자.

4.3 간단한 application 추가하기
devtool 테스트를 위해, 먼저 helloyocto라는 간단한 app을 하나 만들어 보자.

$ mkdir -p mysources/helloyocto
  -> 디렉토리를 하나 생성하고, 아래 파일을 추가하도록 한다(테스트하려는 source code가 외부에 있는 경우를 가정한 것임).

$ vi mysources/helloyocto/helloyocto.c

[그림 4.7] helloyocto.c

다음으로 devtool add 명령을 사용하여 helloyocto app을 workspace에 추가하도록 한다.

$ devtool add helloyocto mysources/helloyocto
  -> 최초에 workspace directory가 없으므로 이를 생성한다.
  -> 또한 workspace/recipes/helloyocto/helloyocto.bb recipe 파일이 자동으로 만들어진다.

helloyocto.bb recipe 파일은 기본적인 내용(skeleton file)으로만 구성되어 있으므로, 이를 원하는 대로 적절히 수정하도록 한다.

$ devtool edit-recipe helloyocto 

[그림 4.8] helloyocto.bb recipe 생성 -> 수정

다음으로, devtool build 명령을 사용하여 helloyocto을 build하도록 한다.

$ devtool build helloyocto
  -> build-openstlinuxweston-stm32mp2/mysources/helloyocto/ 아래에 build 결과물이 생성된다.

위의 build 결과물이 정상적인지를 확인(간단하므로 여기서는 관련 절차 생략)한 후, 이상이 없다고 판단되면,  workspace에서 작업한 helloyocto를 4.2절에서 추가한 meta-my-custo-layer에 반영하도록 한다.

<파일 복사 작업>
-------------------------------------
$ mkdir -p ../layers/meta-st/meta-my-custo-layer/recipes-custom/helloyocto/files

cp workspace/recipes/helloyocto/helloyocto.bb  ../layers/meta-st/meta-my-custo-layer/recipes-custom/helloyocto/
$ cp mysources/helloyocto/files/*.c  ../layers/meta-st/meta-my-custo-layer/recipes-custom/helloyocto/files
-------------------------------------

파일 복사가 끝났으면, 옮긴 파일을 기준으로 bitbake build가 정상적으로 진행되는지 확인해 본다.

$ bitbake helloyocto
  -> OK, 정상적으로 build가 진행된다.

다음으로, helloyocto 실행파일이 포함된 새로운 image 즉, (st-image-weston image + helloyocto binary)을 만들기 위해, my-custom-image.bb라는 recipe를 하나 만든다.

$ mkdir -p layers/meta-st/meta-my-custo-layer/recipes-custom/images;
$ cd layers/meta-st/meta-my-custo-layer/recipes-custom/images
 -> 먼저 images 디렉토리를 하나 만든다.

$ vi my-custom-image.bb 
  -> 파일 앞부분에 아래와 같이 st-image-weston.bb를 포함시키자.
require ../meta-st-openstlinux/recipes-st/images/st-image-weston.bb
IMAGE_INSTALL += "helloyocto"
~

자, 모든 준비가 끝났으니, 이제 새로운 image를 만들어 보자.

$ bitbake my-custom-image
    -> 위의 helloyocto가 포함된 st-image-weston image가 tmp-glibc/deploy/images/stm32mp2 아래에 생성된다.
    -> 새로 생성된 rootfs 파일을 찾아 압축을 해제해 보면, 그 안에 helloyocto binary가 추가되어 있는 것을 알 수가 있다.

<rootfs내에서 helloyocto 내용 확인>
$ tar xvJf my-custom-image-openstlinux-weston-stm32mp2.rootfs-20260304080056.tar.xz 
$ find . -name "helloyocto" -print
./usr/bin/helloyocto
 -> OK, 원하는 작업이 제대로 이루어졌음을 알 수 있다.

4.4 Open source library 수정 작업하기
이번에 테스할 내용은 기존에 사용하던 library code에 문제가 있어, 이를 devtool로 수정한 후 원본 project에 적용(bbappend 형태로 반영)하는 과정에 관한 것이다.

먼저 devtool modify 명령을 사용하여 관련 library를 workspace에 복사하도록 한다.

devtool modify -x libmnl
  -> workspace/appends/libmnl_1.0.5.bbappend 파일이 생성되고, sources/libmnl 파일이 만들어진다(복사된다).

[그림 4.9] libmnl_1.0.5.bbappend 파일 생성(자동 생성됨)

$ cd build-openstlinuxweston-stm32mp2/workspace/sources/libmnl
$ ls -la
합계 596
drwxr-xr-x 9 chyi chyi   4096  3월  4 20:51 .
drwxrwxr-x 4 chyi chyi   4096  3월  4 20:51 ..
drwxr-xr-x 8 chyi chyi   4096  3월  4 20:51 .git
-rw-r--r-- 1 chyi chyi  26530  4월  5  2022 COPYING
-rw-r--r-- 1 chyi chyi   1110  4월  5  2022 Make_global.am
-rw-r--r-- 1 chyi chyi    259  4월  5  2022 Makefile.am
-rw-r--r-- 1 chyi chyi  30175  4월  5  2022 Makefile.in
-rw-r--r-- 1 chyi chyi   1221  4월  5  2022 README
-rw-r--r-- 1 chyi chyi  42104  4월  5  2022 aclocal.m4
drwxr-xr-x 2 chyi chyi   4096  4월  5  2022 build-aux
-rw-r--r-- 1 chyi chyi   1694  4월  5  2022 config.h.in
-rwxr-xr-x 1 chyi chyi 435543  4월  5  2022 configure
-rw-r--r-- 1 chyi chyi   1796  4월  5  2022 configure.ac
drwxr-xr-x 2 chyi chyi   4096  4월  5  2022 doxygen
-rw-r--r-- 1 chyi chyi    755  4월  5  2022 doxygen.cfg.in
drwxr-xr-x 6 chyi chyi   4096  4월  5  2022 examples
drwxr-xr-x 4 chyi chyi   4096  4월  5  2022 include
-rw-r--r-- 1 chyi chyi    309  4월  5  2022 libmnl.pc.in
drwxr-xr-x 2 chyi chyi   4096  4월  5  2022 m4
drwxr-xr-x 2 chyi chyi   4096  4월  5  2022 src


이 상태에서 아래와 같이 libmnl 코드중 한 파일을 수정해보자.

[그림 4.10] libmnl/src/nlmsg.c 수정 (수정 예일 뿐임)

이후, 방금 수정한 내용에 이상이 없는지 build를 해 보자.


$ devtool build libmnl
  -> 이상 없이 build가 진행된다.

devtool deplay 명령을 사용하여, 수정한 내용이 제대로 동작하는지 target board에 올려 확인해 보도록 한다.
$ devtool deploy-target libmnl root@192.168.8.227
  -> 필요시 target board로 해당 library를 올려서 원하는 대로 동작하는지 확인해 본다.

앞서 수정한 내용이 이상없이 동작한다면, 수정 사항을 commit하도록 한다(commit을 해야 이후 patch를 만들 수 있음). 

$ cd workspace/sources/libmnl
$ git add src/nlmsg.c 
$ git commit -m "modified for update-recipe"
[devtool b95ed7e] modified for update-recipe
 1 file changed, 3 insertions(+)

이후, devtool update-recipe 명령을 사용하여 수정 사항에 대한 patch 파일을 meta-my-custo-layer layer 아래에 만든다.

$ devtool update-recipe -a ../layers/meta-st/meta-my-custo-layer libmnl
NOTE: Starting bitbake server...
NOTE: Started PRServer with DBfile: /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/build-openstlinuxweston-stm32mp2/cache/
prserv.sqlite3, Address: 127.0.0.1:44185, PID: 1113490
NOTE: Reconnecting to bitbake server...
NOTE: Retrying server connection (#1)... (21:37:02.778981)
NOTE: Started PRServer with DBfile: /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/build-openstlinuxweston-stm32mp2/cache/
prserv.sqlite3, Address: 127.0.0.1:39155, PID: 1113501
Loading cache: 100% |#################################################################################################| Time: 0:00:00
Loaded 5015 entries from dependency cache.
Parsing recipes: 100% |###############################################################################################| Time: 0:00:00
Parsing of 3050 .bb files complete (3047 cached, 3 parsed). 5017 targets, 615 skipped, 0 masked, 0 errors.
NOTE: Writing append file /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/layers/meta-st/meta-my-custo-layer/recipes-extend
ed/libmnl/libmnl_1.0.5.bbappend
NOTE: Copying 0001-modified-for-update-recipe.patch to /mnt/hdd/workspace/ST/STM32MP257K_DK/Distribution-Package/layers/meta-st/meta-
my-custo-layer/recipes-extended/libmnl/libmnl/0001-modified-for-update-recipe.patch


위의 명령 실행으로, 아래 위치에 bbapend 파일과 patch 파일이 생성되는 것을 알 수 있다.

$ cd layers/meta-st/meta-my-custo-layer/recipes-extended/libmnl
$ ls -la
합계 16
drwxrwxr-x 3 chyi chyi 4096  3월  4 21:40 .
drwxrwxr-x 3 chyi chyi 4096  3월  4 21:37 ..
drwxrwxr-x 2 chyi chyi 4096  3월  4 21:37 libmnl
-rw-rw-r-- 1 chyi chyi  107  3월  4 21:37 libmnl_1.0.5.bbappend

$ cd libmnl
$ ls -la
합계 12
drwxrwxr-x 2 chyi chyi 4096  3월  4 21:37 .
drwxrwxr-x 3 chyi chyi 4096  3월  4 21:40 ..
-rw-rw-r-- 1 chyi chyi  588  3월  4 21:37 0001-modified-for-update-recipe.patch

당초 계획했던 모든 작업이 완료되었다. 수정 사항을 최종 image에 반영하려면 bitbake my-custom-image 명령을 실행한 후, build 결과물을 target board에 다시 설치하면 된다.

마지막으로, workspace에서 libmnl을 제거하기 위해서 아래 명령을 실행해 주도록 하자.

$ devtool reset libmnl

_________________________________________________________
이어지는 장에서는 u-boot bootloader와 linux kernel을 devtool로 작업하는 과정을 소개해 보도록 하겠다.


5. Linux Kernel - Device Tree & Device Drivers
이번 장에서는 STM32MP257F-DK 보드의 Cortex-A35 CPU에 Linux kernel을 올리기 위해 yocto project code를 사용하여 build하는 절차를 소개해 보고자 한다.


To be continued...


References
[1] https://www.st.com/en/evaluation-tools/stm32mp257f-dk.html
[2] UM3385 - Discovery kit with STM32MP257F MPU - User manual, STMicroelectronics.
[3] Data brief - STM32MP257F-DK, STMicroelectronics.
[4] STM32MP251C/F STM32MP253C/F, STM32MP255C/F STM32MP257C/F Datasheet, STMicroelectronics.
[5] STM32MP257-DK schematic, STMicroelectronics.
[6] https://wiki.st.com/stm32mpu/wiki/Getting_started/STM32MP2_boards/STM32MP257x-DK
[7] https://wiki.st.com/stm32mpu/wiki/STM32MP2_boot_chain_overview
[8] https://wiki.st.com/stm32mpu/wiki/STM32MPU_Distribution_Package
  -> STM32MP documents

[9] https://docs.yoctoproject.org/
  -> yocto project manual

[10] https://bootlin.com/doc/training/embedded-linux/embedded-linux-stm32mp2-labs.pdf
  -> bootlin documents

[12] And Google and Gemini~


Slowboot

댓글 없음:

댓글 쓰기