zoukankan      html  css  js  c++  java
  • ZYNQ:提取PetaLinux中Linux和UBoot配置、源码

    说明

    默认情况下,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?{/@L1 {/g" -e "s/@0+([0-9a-fA-F.]+)s?{/@L1 {/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的配置文件,来指定使用的设备树文件、根文件系统文件、内核文件。

    参考蜗窝科技 u-boot FIT image介绍

    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-xlnxbuild目录下保存了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?{/@L1 {/g" -e "s/@0+([0-9a-fA-F.]+)s?{/@L1 {/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

  • 相关阅读:
    学期总结
    第一次博客作业
    C语言博客作业08
    C语言博客作业07
    C语言博客作业06
    C语言博客作业05
    c语言l博客作业04
    C语言II博客作业04
    C语言II博客作业03
    C语言II博客作业02
  • 原文地址:https://www.cnblogs.com/schips/p/xilinx-petalinux-get-source-code-and-config-from-petalinux-project-built.html
Copyright © 2011-2022 走看看