PetaLinux 使用技巧与缓存配置
🚀 PetaLinux 使用技巧与缓存配置
适用场景:
- Ubuntu 主机
- Zynq 开发板
- 可选 Windows 辅助(用 RDP、CLion Remote 插件协作)
本指南主要聚焦于 Ubuntu 与开发板端的高效协作与缓存配置和启动。
🧠 开发建议
P1: 配置 PetaLinux 缓存,加速编译、防止因网络造成编译慢或失败
通过使用本地 downloads
与 sstate
缓存,避免 Yocto 网络下载导致失败。
P2: 三机连同一网络(Ubuntu / Windows / 开发板)
将 三者连接到同一个路由器,别在网络上浪费时间。
P3: 使用 RDP + CLion 进行远程交叉编译
- Windows 中使用 remote-file-systems 插件 访问 Ubuntu。
- 可直接在 CLion 中浏览与编辑 PetaLinux 工程目录结构。
P4: 启动文件拆分管理
将启动文件拆开
BOOT.BIN, system.dtb, zImage, system.bit, rootfs
📦 PetaLinux 缓存目录结构
$ ls /opt/mycachedownloads sstate_arm_2020.2
🛠️ 缓存配置脚本 fix_complie.sh
脚本位置示例, z7是petalinux工程目录
$ cd ~/petalinux$ lsfix_complie.sh xsa_7010 xsa_7020 z7
使用方式:
$ ./fix_complie.sh z7已成功注释掉 CONFIG_YOCTO_NETWORK_SSTATE_FEEDS 配置项。配置文件已成功更新。BSP 配置文件中已存在 PREMIRRORS_prepend 内容。BSP 配置文件已成功更新。
脚本内容如下:
fix_complie.sh
#!/bin/bash# 配置文件路径CONFIG_FILE=\"$1/project-spec/configs/config\"BSP_CONF_FILE=\"$1/project-spec/meta-user/conf/petalinuxbsp.conf\"# 新的配置值NEW_PRE_MIRROR_URL=\'CONFIG_PRE_MIRROR_URL=\"file:///opt/mycache/downloads/\"\'NEW_SSTATE_FEEDS_URL=\'CONFIG_YOCTO_LOCAL_SSTATE_FEEDS_URL=\"/opt/mycache/sstate_arm_2020.2/arm/ \"\'# 新的 PREMIRRORS_prepend 内容PREMIRRORS_CONTENT=\'PREMIRRORS_prepend = \" \\git://.*/.* file:///opt/mycache/downloads/ \\n\\gitsm://.*/.* file:///opt/mycache/downloads/ \\n\\ftp://.*/.* file:///opt/mycache/downloads/ \\n\\http://.*/.* file:///opt/mycache/downloads/ \\n\\https://.*/.* file:///opt/mycache/downloads/ \\n\"DL_DIR=\"/opt/mycache/downloads\"SSTATE_DIR=\"/opt/mycache/sstate_arm_2020.2/arm\"\'# 检查配置文件是否存在if [ ! -f \"$CONFIG_FILE\" ]; then echo \"配置文件 $CONFIG_FILE 不存在。\" exit 1fi# 注释掉 CONFIG_YOCTO_NETWORK_SSTATE_FEEDS=y 这一行if grep -q \'^CONFIG_YOCTO_NETWORK_SSTATE_FEEDS=\' \"$CONFIG_FILE\"; then sed -i \'/^CONFIG_YOCTO_NETWORK_SSTATE_FEEDS=/ s/^/#/\' \"$CONFIG_FILE\" echo \"已成功注释掉 CONFIG_YOCTO_NETWORK_SSTATE_FEEDS 配置项。\"fi# 替换或添加 CONFIG_PRE_MIRROR_URLif grep -q \'^CONFIG_PRE_MIRROR_URL=\' \"$CONFIG_FILE\"; then # 如果存在,使用 sed 替换配置值 sed -i \'s|^CONFIG_PRE_MIRROR_URL=.*|\'\"$NEW_PRE_MIRROR_URL\"\'|\' \"$CONFIG_FILE\"else # 如果不存在,添加新的配置项 echo \"$NEW_PRE_MIRROR_URL\" >> \"$CONFIG_FILE\"fi# 替换或添加 CONFIG_YOCTO_LOCAL_SSTATE_FEEDS_URLif grep -q \'^CONFIG_YOCTO_LOCAL_SSTATE_FEEDS_URL=\' \"$CONFIG_FILE\"; then # 如果存在,使用 sed 替换配置值 sed -i \'s|^CONFIG_YOCTO_LOCAL_SSTATE_FEEDS_URL=.*|\'\"$NEW_SSTATE_FEEDS_URL\"\'|\' \"$CONFIG_FILE\"else # 如果不存在,添加新的配置项 echo \"$NEW_SSTATE_FEEDS_URL\" >> \"$CONFIG_FILE\"fi# 确认替换或添加是否成功if grep -q \"$NEW_PRE_MIRROR_URL\" \"$CONFIG_FILE\" && grep -q \"$NEW_SSTATE_FEEDS_URL\" \"$CONFIG_FILE\"; then echo \"配置文件已成功更新。\"else echo \"配置文件更新失败。\" exit 1fi# 检查 BSP 配置文件是否存在if [ ! -f \"$BSP_CONF_FILE\" ]; then echo \"BSP 配置文件 $BSP_CONF_FILE 不存在。\" exit 1fi# 添加 PREMIRRORS_prepend 内容到 BSP 配置文件if ! grep -q \'^PREMIRRORS_prepend\' \"$BSP_CONF_FILE\"; then cat <> \"$BSP_CONF_FILE\"$PREMIRRORS_CONTENTEOF echo \"已将 PREMIRRORS_prepend 内容添加到 BSP 配置文件。\"else echo \"BSP 配置文件中已存在 PREMIRRORS_prepend 内容。\"fi# 确认添加是否成功if grep -q \"^PREMIRRORS_prepend\" \"$BSP_CONF_FILE\"; then echo \"BSP 配置文件已成功更新。\"else echo \"BSP 配置文件更新失败。\" exit 1fi
🔧 U-Boot 启动文件拆解技巧
通过编写 boot.cmd.default 来拆分启动项(使用 mkimage 工具生成 boot.scr):
mkimage -c none -A arm -T script -d boot.cmd.default boot.scr
📄 启动脚本 boot.cmd.default 如下
boot.cmd.default
# mkimage -c none -A arm -T script -d boot.cmd.default boot.scr#################ming_mmc_devnum=1for boot_target in ${boot_targets};doif test \"${boot_target}\" = \"jtag\" ; thenbootm 0x00200000 0x04000000 0x00100000exit;fiif test \"${boot_target}\" = \"mmc0\" || test \"${boot_target}\" = \"mmc1\" ; then echo \" AAAAAAAAAAAAAWPF Booting from SD (zImage)...${boot_target}--${devtype} -${devnum}-${distro_bootpart} \"if test -e ${devtype} ${devnum}:${distro_bootpart} /zImage; thenfatload ${devtype} ${devnum}:${distro_bootpart} 0x00200000 zImage;;fiif test -e ${devtype} ${devnum}:${distro_bootpart} /system.dtb; thenfatload ${devtype} ${devnum}:${distro_bootpart} 0x00100000 system.dtb;fiif test -e ${devtype} ${devnum}:${distro_bootpart} /rootfs.cpio.gz.u-boot; thenfatload ${devtype} ${devnum}:${distro_bootpart} 0x04000000 rootfs.cpio.gz.u-boot;bootm 0x00200000 0x04000000 0x00100000exit;fi if test -e ${devtype} ${devnum}:${distro_bootpart} /system.bit; thenfatload ${devtype} ${devnum}:${distro_bootpart} 0x00800000 system.bit;fpga loadb 0 ${fileaddr} ${filesize}fibootz 0x00200000 - 0x00100000exit;fiif test \"${boot_target}\" = \"xspi0\" || test \"${boot_target}\" = \"qspi\" || test \"${boot_target}\" = \"qspi0\"; then echo \"BBBBBBBBBBBBBBBBBKYLV1 Booting from QSPI (zImage)...${boot_target}--${devtype} -${ming_mmc_devnum}-${distro_bootpart} \" if test -z \"${bootargs}\"; then setenv bootargs \'console=ttyPS0,115200 root=/dev/mmcblk1p2 rw rootwait rootfstype=ext4 earlyprintk\' fiif test -e ${devtype} ${ming_mmc_devnum}:${distro_bootpart} /zImage; thenfatload ${devtype} ${ming_mmc_devnum}:${distro_bootpart} 0x00200000 zImage;;fiif test -e ${devtype} ${ming_mmc_devnum}:${distro_bootpart} /system.dtb; thenfatload ${devtype} ${ming_mmc_devnum}:${distro_bootpart} 0x00100000 system.dtb;fiif test -e ${devtype} ${ming_mmc_devnum}:${distro_bootpart} /rootfs.cpio.gz.u-boot; thenfatload ${devtype} ${ming_mmc_devnum}:${distro_bootpart} 0x04000000 rootfs.cpio.gz.u-boot;bootm 0x00200000 0x04000000 0x00100000exit;fiif test -e ${devtype} ${ming_mmc_devnum}:${distro_bootpart} /system.bit; thenfatload ${devtype} ${ming_mmc_devnum}:${distro_bootpart} 0x00800000 system.bit;fpga loadb 0 ${fileaddr} ${filesize}fibootz 0x00200000 - 0x00100000exit;fiif test \"${boot_target}\" = \"nand\" || test \"${boot_target}\" = \"nand0\"; thennand infoif test \"image.ub\" = \"image.ub\"; thennand read 0x10000000 0x1000000 0x6400000;bootm 0x10000000;exit;fiif test \"image.ub\" = \"uImage\"; thennand read 0x00200000 0x1000000 0x3200000;nand read 0x04000000 0x4600000 0x3200000;bootm 0x00200000 0x04000000 0x00100000exit;fifidone
拷贝sd卡的文件到qspi和emmc
如果不再用sd卡,则将sd卡里的文件拷贝到到qspi和emmc里
将 BOOT.BIN 拷贝到qspi
copy_boot_to_qspi.sh
#!/bin/shecho \"##############################################\"echo \"########## COPY BOOT TO Flash QSPI #########\"echo \"##############################################\"echo \"\"boot=/media/sd-mmcblk0p1/BOOT.BINif [ ! -f ${boot} ]; thenecho \"ERROR: ${boot} file does not exist!\"exit 1fiecho \"Erase partition /dev/mtd0\"flash_erase /dev/mtd0 0 0flashcp ${boot} /dev/mtd0echo \"########## Complete ##########\"echo \"##############################################\"
将emmc分区
emmc的分区和sd卡一样,一个fat一个ext4
emmc_partition.sh
#!/bin/sheMMC_DEVICE=\"/dev/mmcblk1\"umount \"${eMMC_DEVICE}p1\" 2>/dev/nullumount \"${eMMC_DEVICE}p2\" 2>/dev/nullif [ ! -b \"$eMMC_DEVICE\" ]; then echo \"$eMMC_DEVICE not found!\" exit 1fifdisk \"$eMMC_DEVICE\" <<EOFonp1+100Mnp2wEOFblockdev --rereadpt \"${eMMC_DEVICE}\"mkfs.vfat -F 32 \"${eMMC_DEVICE}p1\"mkfs.ext4 \"${eMMC_DEVICE}p2\"fdisk -l \"$eMMC_DEVICE\"
将sd卡所有文件拷贝到emmc
copy_sd_to_emmc.sh
#!/bin/shcp -a /media/sd-mmcblk0p1/* /media/sd-mmcblk1p1cp -a /media/sd-mmcblk0p2/* /media/sd-mmcblk1p2