說明
默認情況下,PetaLinux在編譯完成后會刪除源代碼,以節省硬盤空間。
在project-spec/meta-user/conf/petalinuxbsp.conf
里,添加如下內容,可以保留Linux和UBoot源代碼。
RM_WORK_EXCLUDE += "linux-xlnx"
RM_WORK_EXCLUDE += "u-boot-xlnx"
可以使用下面的方法創建petalinux工程:
##
# Copyright By Schips, All Rights Reserved
# https://gitee.com/schips/
#
# File Name: copy_petalinux_project_with_hw.sh
# Created : 2020-07-25 11:28:24
#
##
#!/bin/sh
help () {
echo "Error : need \$projectname \$hw_bsp"
echo " eg :"
echo " $0 ax_project ~/hw-vivado-bsp-for-petalinux/some_packakge"
exit 1
}
if [ -z "$1" ]; then
help
fi
if [ -z "$2" ]; then
help
fi
## 刪除源目錄
rm $1 -rf
## 創建項目
petalinux-create --type project --template zynq --name $1
## 導入硬件信息
cd $1
petalinux-config --get-hw-description $2
## 使PetaLinux不刪除源碼
echo 'RM_WORK_EXCLUDE += "linux-xlnx"'>> project-spec/meta-user/conf/petalinuxbsp.conf
echo 'RM_WORK_EXCLUDE += "u-boot-xlnx"'>> project-spec/meta-user/conf/petalinuxbsp.conf
echo ''>> project-spec/meta-user/conf/petalinuxbsp.conf
此后,只要PetaLinux通過編譯就可以獲取源碼。
system-top.dts、system-conf.dtsi 等這些設備樹文件是petalinux-build在之后生成的
Linux、uboot的源碼沒有變化,關鍵在於配置以及設備樹的變化
這里以:2018.03的ZYNQ-7000平台為例。
提取以后,還有一篇文章簡單地介紹了如何打包BOOT.bin以及image.ub
https://www.cnblogs.com/schips/p/using-petalinux-package-boot_bin_and_image_ub.html
文件系統
文件系統位於image/linux
,可以使用下面命令解壓。
mkdir /tmp/rootfs_petalinx -p
sudo tar -zxf image/linux/rootfs.tar.gz -C /tmp/rootfs_petalinx
獲取Linux有關內容
Linux源碼
PetaLinux工程在編譯后,在build/tmp/work-shared/
目錄下,根據對應芯片的$DIR
下的kernel-source
,含有所有Linux源代碼。
使用以下命令進行到達所在目錄:
#find . -type d -name "$1" 2> /dev/null | grep $1
cd build/tmp/work-shared/
cd `find . -type d -name "kernel-source" 2> /dev/null`
可以復制這個目錄里的Linux源代碼,用來使用open source流程編譯。
以ZYNQ-7000為例,目錄為:build/tmp/work-shared/plnx-zynq7/kernel-source
build/tmp/work/plnx_zynq7-xilinx-linux-gnueabi/
這個目錄下的源碼有點不太一樣,不要使用這個目錄下面的。
Linux內核配置
PetaLinux工程在編譯后,在build/tmp/work-shared/
目錄下,根據對應芯片的$DIR
下的kernel-build-artifacts
,保存了Linux的配置文件.config
。
通過以下命令查找它的所在目錄:
#find . -type d -name "$1" 2> /dev/null | grep $1
cd build/tmp/work-shared/
find . -type d -name "kernel-build-artifacts" 2> /dev/null
以ZYNQ-7000為例,目錄為:
build/tmp/work-shared/plnx-zynq7/kernel-build-artifacts
為了方便使用,可以把文件.config復制到Linux源代碼的子目錄arch/arm/configs/下,重命名為xilinx_peta_defconfig
。這樣使用make xilinx_peta_defconfig
,可以創建PetaLinux使用的Linux配置。
DIR=plnx-zynq7 # ZYNQ-7000
cp \
build/tmp/work-shared/$DIR/kernel-build-artifacts/.config \
build/tmp/work-shared/$DIR/kernel-source/arch/arm/configs/xilinx_peta_defconfig
獲取設備樹 .dts 文件
說明:
- zynq-7000.dtsi :文件中的內容是 zynq-7000 系列處理器相同的硬件外設配置信息(PS 端的)
- pl.dtsi :內容是我們在vivado 當中添加的 pl 端外設對應的配置信息
- pcw.dtsi :表示我們在 vivado 當中已經使能的 PS 外設。
獲取設備樹源碼一共有下列的方式:
工具 | 輸入 | 輸出 | 備注 |
---|---|---|---|
SDK | hdf | dts | 需要對SDK工程進行配置,但是使用方便 |
PetaLinux-build | hdf | dtb、dts | 構建后才生成結果,過程不透明 |
PetaLinux-hsi | hdf | dts | 需要使用交互式命令構建,生成的dts與build出來的有點不一樣 |
dtc | dts或dtb | dtb或dts | 傳統方法;由dtb反編譯再構建的dtb不一定可靠 |
kernel-make | dts | dtb | 放在內核樹中進行編譯得到的設備樹,需要修改Makefile |
原理:除了dtc常規編譯設備樹以外,其他的方式都是由tcl腳本生成的。
dtc處理設備樹
使用petalinux根據bsp建立工程以后,會在build/tmp/deploy/images
目錄中根據對應芯片的$DIR
下的產生system.dtb
,用dtc反匯編可獲取dts文件。
#!/bin/bash
DTC=${A_DCT_FULL_PATH} # 一般是在 Linux內核源碼中的 scripts/dtc/dtc
find . -type f -name "system.dtb" 2> /dev/null
# 以 ZYNQ 7000 為例
##./build/tmp/deploy/images/plnx-zynq7/plnx-zynq7-system.dtb
##./images/linux/system.dtb
# 反編譯 dtb獲取 dts
$DTC -I dtb -O dts -o system-top.dts system-top.dtb
# 將dts 編譯為 dtb
$DTC -I dts -O dtb -o system-top.dtb system-top.dts
完整編譯工程以后,在
image/linux
中也會有一個system.dtb
hsi生成設備樹
下載PetaLinux依賴的 設備樹倉庫路徑:
wget https://github.com/Xilinx/device-tree-xlnx/archive/xilinx-v2018.3.tar.gz
tar -xf xilinx-v2018.3.tar.gz -C /opt/env
生成:
##
# Copyright By Schips, All Rights Reserved
# https://gitee.com/schips/
#
# File Name: hdf2dts.sh
# Created : 2020-08-06 20:14:03
#
##
#!/bin/sh
HDF=$1
DEVICE_TREE_DIR=/opt/env/device-tree-xlnx-xilinx-v2018.3
OUTDIR=$2
help () {
echo "ERROR : Need arguments"
echo " eg:"
echo " $0 xx.hdf outputDirForDTS"
exit
}
if [ -z "$1" ]; then
help
fi
if [ -z "$2" ]; then
help
fi
#export PATH=$PATH:${DEVICE_TREE_DIR}/
command -v hsi >/dev/null 2>&1 || { echo >&2 "Aborted : Require \"hsi\" but not found."; exit 1; }
echo "Creating DTS from [$$HDF]"
hsi <<EOF
open_hw_design $HDF
set_repo_path $DEVICE_TREE_DIR
create_sw_design deviec-tree -os device_tree -proc ps7_cortexa9_0
generate_target -dir $OUTDIR
EOF
sed 's/\#include/\/include\//g' $OUTDIR/system-top.dts -i
echo "/include/ \"system-user.dtsi\"" >> $OUTDIR/system-top.dts
find ${OUTDIR} -type f -name "*.dts*" -exec sed -E -i -e "s/@0x([0-9a-fA-F\.]+)\s?\{/@\L\1 \{/g" -e "s/@0+([0-9a-fA-F\.]+)\s?\{/@\L\1 \{/g" {} +
echo "Created DTS from [$HDF]"
echo "You may need [system-conf.dtsi], [system-user.dtsi] from Petalinux Project"
exit 0
# 進入交互命令
hsi
## 打開硬件描述文件
open_hw_design
## 設置倉庫路徑
set_repo_path
## 創建軟件設計(CortexA9用的設備樹)
create_sw_design deviec-tree -os device_tree -proc ps7_cortexa9_0
## 生成目標到目錄
generate_target -dir
從PetaLinux工程中提取設備樹
路徑:
cp project-spec/meta-user/recipes-bsp/device-tree/files/system-user.dtsi ${DTS_OUT}/common
cp components/plnx_workspace/device-tree/device-tree/system-conf.dtsi ${DTS_OUT}/common
cp components/plnx_workspace/device-tree/device-tree/system-top.dts ${DTS_OUT}/common
cp components/plnx_workspace/device-tree/device-tree/pl.dtsi ${DTS_OUT}/common
cp components/plnx_workspace/device-tree/device-tree/pcw.dtsi ${DTS_OUT}/common
# 注意,kernel和uboot用的設備樹中,還有一些地方不太一樣:
cp -v ${petaProject}/`find . -type f -name "zynq-7000.dtsi" 2> /dev/null | grep "kernel-source"` ${DTS_OUT}/kernel
cp -v ${petaProject}/`find . -type f -name "zynq-7000.dtsi" 2> /dev/null | grep "u-boot"` ${DTS_OUT}/uboot
注意,kernel和uboot中的zynq-7000.dtsi
不太一樣,一般用kernel的就可以了。
SDK構建設備樹
略。SDK需要安裝一個類似dts的插件,此后也是根據hdf來創建一個設備樹工程。
但是我認為這種方法還是比較臃腫。
獲取 .its文件
image.ub哪里來?image.ub是U-Boot fitImage
搜索資料,得知這個文件把image, rootfs,dtb打包到一個文件里面
生成image.ub,需要一個后綴名為its的配置文件,來指定使用的設備樹文件、根文件系統文件、內核文件。
image source file(.its)和device tree source file(.dts)類似,負責描述要生成的image
file的信息。mkimage和dtc工具,可以將.its文件以及對應的image data
file,打包成一個image file。再將這個文件下載到么memory中,使用bootm命令就可以執行。
PetaLinux工程在編譯后,在build/tmp/work/
目錄下中有fit-image-petalinux-user-image.its,可以用於生成image.ub。
目錄比較深,我們不對它的路徑規則進行分析,直接使用腳本進行查找:
cd build/tmp/work/
find . -name "fit-image-petalinux-user-image.its" 2> /dev/null
在PetaLinux 2018.2/2018.3里,
images/linux/
下有文件rootfs.its,用diff
工具比較,兩者的內容是一樣的。在PetaLinux 2019.1里,
images/linux/
已經沒有這個文件。
為了方便,可以修改為使用images/linux下的文件。PetaLinux工程的目錄"images/linux/"里,含有創建image.ub的devicetree、rootfs文件等。
取得UBoot有關內容
UBoot源碼
如果使用了外部UBoot源代碼編譯,則沒有這個源代碼。可以復制前面提到的UBoot源代碼,用來使用open source流程編譯。
PetaLinux工程在編譯后,在build/tmp/work/
目錄下,根據對應芯片的$DIR
下的u-boot-xlnx
中的git
目錄含有所有UBoot源代碼。
通過以下命令查找它的所在目錄:
#find . -type d -name "$1" 2> /dev/null | grep $1
cd build/tmp/work/
find . -type d -name "git" 2> /dev/null | grep "u-boot-xlnx"| grep "git"
以ZYNQ-7000為例,目錄為:build/tmp/work/plnx_zynq7-xilinx-linux-gnueabi/u-boot-xlnx/
對於PetaLinux 2019.1的ZCU106 BSP工程,UBoot源代碼在目錄 ./build/tmp/work/zcu106_zynqmp-xilinx-linux/u-boot-xlnx/v2019.01-xilinx-v2019.1+gitAUTOINC+d895ac5e94-r0/git/。
UBoot配置
PetaLinux工程在編譯后,在build/tmp/work/
目錄下,根據對應芯片的$DIR
下的u-boot-xlnx
的build
目錄下保存了UBoot的配置文件.config
。
例如:
build/tmp/work/plnx_zynq7-xilinx-linux-gnueabi/u-boot-xlnx/v2018.01-xilinx-v2018.3+gitAUTOINC+d8fc4b3b70-r0/build/.config
使用以下命令定位所在目錄:
#find . -type d -name "$1" 2> /dev/null | grep $1
cd build/tmp/work/
find . -type d -name "build" 2> /dev/null | grep "u-boot-xlnx"| grep "build"
為了方便使用,可以把文件.config
復制到UBoot源代碼的configs
目錄下,重命名為xilinx_peta_defconfig
。
這樣一來,使用make xilinx_peta_defconfig
,就可以創建PetaLinux使用的UBoot配置。
注意事項:
從PetaLinux工程里得到的UBoot源代碼中自動生成的文件include/configs/platform-auto.h
里的宏定義里的連接符后有空格,可能導致編譯時編譯器會產生大量警告。
使用以下命令去除:
sed 's/ $//' include/configs/platform-auto.h -i
uboot自動生成的配置
uboot頭文件:project-spec/meta-user/recipes-bsp/u-boot/files/platform-top.h
,實際上會變成platform-auto.h
修改bif文件
需要創建BOOT.BIN
,需要bootgen.bif
(boot image format)。
PetaLinux工程生成boot.bin時,會在build
目錄下生成文件bootgen.bif
。
BIF 文件的所有語法都在 Zynq UltraScale+ MPSoC 軟件開發指南 (UG1137) 中,見附錄 A。
默認的bif文件的內容如下,沒有包括aes安全信息等:
the_ROM_image:
{
[bootloader] /tmp/tmp.0MmcVIZQVR/zynq_fsbl.elf
/tmp/tmp.0MmcVIZQVR/system.bit
/tmp/tmp.0MmcVIZQVR/u-boot.elf
}
bootgen.bif里用的是臨時目錄,最好改成PetaLinux工程的目錄images/linux/
,我們這里可以使用一個腳本,生成一個更規范的bif文件,在 頂級目錄下 執行以下腳本:
#!/bin/sh
TOP=`pwd`
DESTDIR="${TOP}/images/linux"
BIF=new.bif
echo "the_ROM_image:" > $BIF
echo "{" >> $BIF
echo " [bootloader] ${DESTDIR}/zynq_fsbl.elf" >> $BIF
echo " ${DESTDIR}/system.bit" >> $BIF
echo " ${DESTDIR}/u-boot.elf" >> $BIF
echo "}" >> $BIF
一鍵拷貝
原文:https://www.cnblogs.com/schips/p/get-source-code-and-config-from-petalinux-project-built.html
一鍵拷貝內核、uboot等源碼(實際上不需要,因為構建時用的源碼沒有被修改)
關鍵在於設備樹、以及配置
##
# Copyright By Schips, All Rights Reserved
# https://gitee.com/schips/
#
# File Name: cp_src_from_petalinux.sh
# Created : 2020-07-25 08:08:47
#
##
#!/bin/sh
help () {
echo "ERROR : Need arguments"
echo " eg:"
echo " $0 petaProject outputDir"
exit
}
if [ -z "$1" ]; then
help
fi
if [ -z "$2" ]; then
help
fi
petaProject=`cd $1 && pwd`
mkdir $2 -p
outputDir=`cd $2 && pwd`
cd $petaProject || exit 1
CONFIG=xilinx_zynq7000_peta_defconfig
KERNEL_DIR=${petaProject}/`find . -type d -name "kernel-source" 2> /dev/null`
UBOOT_DIR=${petaProject}/`find . -type d -name "git" 2> /dev/null | grep "u-boot-xlnx"| grep "git"`
KERNEL_OUT=${outputDir}/linux
UBOOT_OUT=${outputDir}/uboot
DTB=${petaProject}/images/linux/system.dtb
DTS_OUT=${outputDir}/dts
mkdir $DTS_OUT
#DTC=$KERNEL_DIR/scripts/dtc/dtc
## 拷貝配置
cp_config () {
KERNEL_CFG=${petaProject}/`find . -type d -name "kernel-build-artifacts" 2> /dev/null`/.config
UBOOT_CFG=${petaProject}/`find . -type d -name "build" 2> /dev/null | grep "u-boot-xlnx"| grep "build"`/.config
cp ${KERNEL_CFG} ${KERNEL_OUT}/arch/arm/configs/${CONFIG} -v
cp ${UBOOT_CFG} ${UBOOT_OUT}/configs/${CONFIG} -v
cp ./project-spec/meta-plnx-generated/recipes-bsp/u-boot/configs/platform-auto.h ${UBOOT_OUT}/include/configs/platform-auto.h -v
## 處理掉空格
sed 's/ $//' ${UBOOT_OUT}/include/configs/platform-auto.h -i
}
cp_dt() {
echo "Warring : 經過對比分析,提取的設備樹與PetaLinux構建的設備樹很多地方不一致,不建議使用提取的設備樹。"
cp $DTB ${DTS_OUT} -v
mkdir -p ${DTS_OUT}/uboot
mkdir -p ${DTS_OUT}/kernel
cp ${petaProject}/project-spec/meta-user/recipes-bsp/device-tree/files/system-user.dtsi ${DTS_OUT}/uboot -v
cp ${petaProject}/project-spec/meta-user/recipes-bsp/device-tree/files/system-user.dtsi ${DTS_OUT}/kernel -v
cp ${petaProject}/components/plnx_workspace/device-tree/device-tree/system-conf.dtsi ${DTS_OUT}/uboot -v
cp ${petaProject}/components/plnx_workspace/device-tree/device-tree/system-conf.dtsi ${DTS_OUT}/kernel -v
cp ${petaProject}/components/plnx_workspace/device-tree/device-tree/system-top.dts ${DTS_OUT}/uboot -v
cp ${petaProject}/components/plnx_workspace/device-tree/device-tree/system-top.dts ${DTS_OUT}/kernel -v
cp ${petaProject}/components/plnx_workspace/device-tree/device-tree/pl.dtsi ${DTS_OUT}/uboot -v
cp ${petaProject}/components/plnx_workspace/device-tree/device-tree/pl.dtsi ${DTS_OUT}/kernel -v
cp ${petaProject}/components/plnx_workspace/device-tree/device-tree/pcw.dtsi ${DTS_OUT}/uboot -v
cp ${petaProject}/components/plnx_workspace/device-tree/device-tree/pcw.dtsi ${DTS_OUT}/kernel -v
cp -v ${petaProject}/`find . -type f -name "zynq-7000.dtsi" 2> /dev/null | grep "kernel-source"` ${DTS_OUT}/kernel
cp -v ${petaProject}/`find . -type f -name "zynq-7000.dtsi" 2> /dev/null | grep "u-boot"` ${DTS_OUT}/uboot
## 更正設備樹語法格式
sed -i 's/\#include/\/include\//g' ${DTS_OUT}/kernel/system-top.dts
sed -i 's/\#include/\/include\//g' ${DTS_OUT}/uboot/system-top.dts
find ${DTS_OUT} -type f -name "*.dts*" -exec sed -E -i -e "s/@0x([0-9a-fA-F\.]+)\s?\{/@\L\1 \{/g" -e "s/@0+([0-9a-fA-F\.]+)\s?\{/@\L\1 \{/g" {} +
}
cp_rootfs () {
cp ${petaProject}/image/linux/rootfs.tar.gz ${outputDir} -v
}
cp_src () {
## 拷貝源碼 到 指定的 目錄
cp ${KERNEL_DIR} ${KERNEL_OUT} -r
cp ${UBOOT_DIR} ${UBOOT_OUT} -r
cp ${petaProject}/images/linux/rootfs.tar.gz ${outputDir} -v
}
cp_src
cp_dt
cp_config
cp_rootfs
清理、恢復 PetaLinux 工程
使用腳本清理
#
# Copyright By Schips, All Rights Reserved
# https://gitee.com/schips/
#
# File Name: clean_petalinux_project.sh
# Created : 2020-07-29 11:28:24
#
#
#!/bin/sh
BASE=`pwd`
SOURCEDIR=`cd $1 && pwd`
LOG=${SOURCEDIR}/cleanlog
help () {
echo "ERROR : Need arguments"
echo " eg:"
echo " $0 petaProject"
exit
}
if [ -z "$1" ]; then
help
fi
rm ${LOG}
backup_kernel_config () {
cd ${BASE}
cd ${SOURCEDIR}
KERNEL_CFG=${SOURCEDIR}/`find . -type d -name "kernel-build-artifacts" 2> /dev/null`/.config
mkdir -p ${SOURCEDIR}/our_config
cp ${KERNEL_CFG} -v ${SOURCEDIR}/our_config/kernel_config >> $LOG
}
backup_uboot_config () {
cd ${BASE}
cd ${SOURCEDIR}
UBOOT_CFG=${SOURCEDIR}/`find . -type d -name "build" 2> /dev/null | grep "u-boot-xlnx"| grep "build"`/.config
mkdir -p ${SOURCEDIR}/our_config
cp ${UBOOT_CFG} -v ${SOURCEDIR}/our_config/uboot_config >> $LOG
}
backup_uboot_config
backup_kernel_config
cd ${BASE}
rm -rf ${SOURCEDIR}/components ${SOURCEDIR}/build ${SOURCEDIR}/images
echo "See log : [$LOG]"
之后,內核、uboot的配置會被保存到our_config
中。
PetaLinux本身的配置不會改變:使用petalinux-config
會看到之前的配置。
恢復被刪除的工程的配置
Linux的配置,因為完整的構造目錄被刪除了,因此,需要LOAD
回來,要么使用這樣的命令
petalinux-config -c kernel
Uboot也是一樣的,如果配置過了uboot,那么需要額外的LOAD
操作。
petalinux-config -c u-boot
此后,直接petalinux-build
即可。
提取以后,還有一篇文章簡單地介紹了如何打包BOOT.bin以及image.ub
https://www.cnblogs.com/schips/p/xilinx-petalinux-package-boot_bin_and_image_ub.html