STM32MP157开发笔记 | 04 - TF-Aubootlinux内核源码获取编译烧写

Posted Mculover666

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32MP157开发笔记 | 04 - TF-Aubootlinux内核源码获取编译烧写相关的知识,希望对你有一定的参考价值。

一、编译TF-A

1. 获取TF-A源码

下载链接:en.SOURCES-tf-a-stm32mp1-openstlinux-5-10-dunfell-mp1-21-11-17_tar.xz

下载后解压:

xz -d en.SOURCES-tf-a-stm32mp1-openstlinux-5-10-dunfell-mp1-21-11-17_tar.xz
mv en.SOURCES-tf-a-stm32mp1-openstlinux-5-10-dunfell-mp1-21-11-17_tar en.SOURCES-tf-a-stm32mp1-openstlinux-5-10-dunfell-mp1-21-11-17.tar
tar -xvf en.SOURCES-tf-a-stm32mp1-openstlinux-5-10-dunfell-mp1-21-11-17.tar

里面分为源码和patch文件:

tf-a-stm32mp-v2.4-stm32mp-r2-r0			TF-A installation directory
    ├── [*].patch			ST patches to apply during the TF-A preparation (see next chapter)
    ├── tf-a-stm32mp-v2.4-stm32mp-r2		TF-A source code directory
    ├── Makefile.sdk			Makefile for the TF-A compilation
    ├── README.HOW_TO.txt		Helper file for TF-A management: reference for TF-A build
    ├── series				List of all ST patches to apply
    └── tf-a-stm32mp-v2.4-stm32mp-r2-r0.tar.gz	Tarball file of the TF-A source code

除了uboot源码之外,在上一级目录下,还有 FIP_artifacts 目录如下:

FIP_artifacts
├── arm-trusted-firmware
│   ├── bl32
│   └── fwconfig

2. 编译TF-A

编译前的准备

(1)OpenSTLinux SDK 必须安装;
(2)编译内核需要安装的包:

llibncurses、libncursesw dev package、libyaml-dev:

sudo apt-get install libncurses5-dev libncursesw5-dev libyaml-dev

(3)确保SDK环境变量生效(注意路径):

source $HOME/STM32MPU_workspace/STM32MP15-Ecosystem-v3.1.0/Developer-Package/SDK/environment-setup-cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi

接下来开始编译。

解压源码:

tar -zxvf tf-a-stm32mp-v2.4-stm32mp-r2-r0.tar.gz

源码打补丁:

cd tf-a-stm32mp-v2.4-stm32mp-r2/
for p in `ls -1 ../*.patch`; do patch -p1 < $p; done

因为在 OpenSTLinux 默认激活了 FIP,所以在编译前必须要指定FIP_artifacts:

export FIP_DEPLOYDIR_ROOT=$PWD/../../FIP_artifacts

编译全部:

make -f $PWD/../Makefile.sdk all

编译出的结果在上级目录的 deploy 文件夹:

3. 烧写测试

使用第一篇文章中烧写成功过的SD卡,只替换 FSBL 进行验证:

sudo dd if=tf-a-stm32mp157c-dk2-sdcard.stm32 of=/dev/sdb1 conv=fdatasync
sudo dd if=tf-a-stm32mp157c-dk2-sdcard.stm32 of=/dev/sdb2 conv=fdatasync

将SD卡重新插入STM32MP157C-DK2开发板,可以正常启动,查看串口日志,TF-A编译时间已修改:

二、编译uboot

1. 获取uboot源码(2020.10版本)

下载链接:en.SOURCES-u-boot-stm32mp1-openstlinux-5-10-dunfell-mp1-21-11-17_tar.xz

下载后解压:

mv en.SOURCES-u-boot-stm32mp1-openstlinux-5-10-dunfell-mp1-21-11-17_tar.xz en.SOURCES-u-boot-stm32mp1-openstlinux-5-10-dunfell-mp1-21-11-17.tar.xz
tar -xvf en.SOURCES-u-boot-stm32mp1-openstlinux-5-10-dunfell-mp1-21-11-17.tar.xz

里面分为源码和patch文件:

u-boot-stm32mp-v2020.10-stm32mp-r2-r0			U-Boot installation directory
    ├── [*].patch				ST patches to apply during the U-Boot preparation (see next chapter)
    ├── u-boot-stm32mp-v2020.10-stm32mp-r2		U-Boot source code directory
    ├── Makefile.sdk				Makefile for the U-Boot compilation
    ├── README.HOW_TO.txt			Helper file for U-Boot management: reference for U-Boot build
    ├── series					List of all ST patches to apply
    └── u-boot-stm32mp-v2020.10-stm32mp-r2-r0.tar.gz	Tarball file of the U-Boot source code

解压源码:

tar -zxvf u-boot-stm32mp-v2020.10-stm32mp-r2-r0.tar.gz

除了uboot源码之外,在上一级目录下,还有 FIP_artifacts 目录如下:

FIP_artifacts
├── arm-trusted-firmware
│   ├── bl32
│   └── fwconfig
├── optee
├── u-boot
│   ├── u-boot-nodtb-stm32mp15.bin
│   ├── u-boot-stm32mp157a-dk1-trusted.dtb	Device tree for U-Boot → STM32MP15 Discovery kits
│   ├── u-boot-stm32mp157a-ev1-trusted.dtb	Device tree for U-Boot → STM32MP15 Evaluation boards
│   ├── u-boot-stm32mp157c-dk2-trusted.dtb	Device tree for U-Boot → STM32MP15 Discovery kits
│   ├── u-boot-stm32mp157c-ed1-trusted.dtb	Device tree for U-Boot → STM32MP15 Evaluation boards
│   ├── u-boot-stm32mp157c-ev1-trusted.dtb	Device tree for U-Boot → STM32MP15 Evaluation boards
│   ├── u-boot-stm32mp157d-dk1-trusted.dtb	Device tree for U-Boot → STM32MP15 Discovery kits
│   ├── u-boot-stm32mp157d-ev1-trusted.dtb	Device tree for U-Boot → STM32MP15 Evaluation boards
│   ├── u-boot-stm32mp157f-dk2-trusted.dtb	Device tree for U-Boot → STM32MP15 Discovery kits
│   ├── u-boot-stm32mp157f-ed1-trusted.dtb	Device tree for U-Boot → STM32MP15 Evaluation boards
│   └── u-boot-stm32mp157f-ev1-trusted.dtb	Device tree for U-Boot → STM32MP15 Evaluation boards

2. 编译uboot

2. 1. uboot源码打patch

cd u-boot-stm32mp-v2020.10-stm32mp-r2/
for p in `ls -1 ../*.patch`; do patch -p1 < $p; done

2.2. 编译uboot(带FIP)

指定FIP文件夹路径:

export FIP_DEPLOYDIR_ROOT=$PWD/../../FIP_artifacts

编译全部:

make -f $PWD/../Makefile.sdk all

编译出来之后,需要部署的文件在上一级目录 deploy 中:

同时,编译出的 FIP 文件在上一级目录FIP_artifacts中:

3. 烧写测试

使用第一篇文章中烧写成功过的SD卡,只替换 FIP 进行验证:

sudo dd if=fip-stm32mp157c-dk2-trusted.bin of=/dev/sdb3 conv=fdatasync

将SD卡重新插入STM32MP157C-DK2开发板,可以正常启动,查看串口日志,u-boot 编译时间已修改:

三、编译内核

1. 获取内核源码

下载链接:en.SOURCES-kernel-stm32mp1-openstlinux-5.10-dunfell-mp1-21-11-17.tar.xz

下载后解压:

tar xvf en.SOURCES-kernel-stm32mp1-openstlinux-5.10-dunfell-mp1-21-11-17.tar.xz

里面分为源码和patch文件:

linux-stm32mp-5.10.61-r2	Linux kernel installation directory
├── [*].patch			ST patches to apply during the Linux kernel preparation (see next chapter)
├── fragment-[*].config		ST configuration fragments to apply during the Linux kernel configuration (see next chapter)
├── linux-5.10.61		Linux kernel source code directory
├── linux-5.10.61.tar.xz	Tarball file of the Linux kernel source code
├── README.HOW_TO.txt		Helper file for Linux kernel management: reference for Linux kernel build
└── series			List of all ST patches to apply

解压源码:

xz -d xvf linux-5.10.61.tar.xz
tar xvf linux-5.10.61.tar

2. 编译kernel

2.1. 编译前的准备

(1)OpenSTLinux SDK 必须安装;
(2)编译内核需要安装的包:

llibncurses、libncursesw dev package、libyaml-dev:

sudo apt-get install libncurses5-dev libncursesw5-dev libyaml-dev

mkimage:

sudo apt-get install u-boot-tools

yaml (check dts):

sudo apt-get install libyaml-dev

(3)确保SDK环境变量生效:

source STM32MP15-Ecosystem-v3.1.0/Developer-Package/SDK/environment-setup-cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi

2.2. 内核源代码打patch

cd linux-5.10.61/
for p in `ls -1 ../*.patch`; do patch -p1 < $p; done

2.3. 源码配置

方式:配置编译目录 (different of kernel source code directory)

创建build目录:

mkdir -p ../build

生成默认配置文件.config:

make ARCH=arm O="$PWD/../build" multi_v7_defconfig fragment*.config

循环应用 fragment*.config 文件(搞不懂干啥的,执行了之后编译会出错,所以不要执行):

for f in `ls -1 ../fragment*.config`; do scripts/kconfig/merge_config.sh -m -r -O $PWD/../build $PWD/../build/.config $f; done
yes '' | make ARCH=arm oldconfig

2.4. 编译源码

编译内核:

make ARCH=arm uImage vmlinux dtbs LOADADDR=0xC2000040 O="$PWD/../build"

编译完成:

编译完成之后,在上一级目录的build/arch/arm/boot目录下可以看到编译出的镜像:

在dts目录下可以看到编译出的设备树:

2.5. 编译模块

make ARCH=arm modules O="$PWD/../build"

3. 烧写

sdb4分区是ext4文件系统,所以将之前使用的SD卡插入后,需要将该文件系统挂载:

sudo mkdir -p /mnt/bootfs
sudo mount /dev/sdb4 /mnt/bootfs/

挂载后查看其中内容,存放了Linux内核镜像、设备树等:

进入到内核编译目录,将之前内核编译出的镜像和设备树拷贝替换:

sudo cp -f uImage /mnt/bootfs/
sudo cp -f dts/stm32mp157c-dk2.dtb /mnt/bootfs/

卸载文件系统:

sudo umount /dev/sdb4

重新将SD卡插入到开发板中,查看是否可以正常启动:

以上是关于STM32MP157开发笔记 | 04 - TF-Aubootlinux内核源码获取编译烧写的主要内容,如果未能解决你的问题,请参考以下文章

STM32MP157开发笔记 | 04 - TF-Aubootlinux内核源码获取编译烧写

STM32MP157开发笔记 | 01 - STM32MP157C-DK2公板快速上手

STM32MP157开发笔记 | 01 - STM32MP157C-DK2公板快速上手

STM32MP157开发笔记 | 03 - STM32MP157启动程序ROM Code详解

STM32MP157开发笔记 | 03 - STM32MP157启动程序ROM Code详解

STM32MP157开发笔记 | 01 - 开发环境搭建