作者:Chao Zhang,AMD工程师;来源:AMD开发者社区
在软件开发过程中我们经常遇到用patch来传递和更新代码的场景。今天我们以一个端到端的例子来演示在Petalinux使用过程中,如何给u-boot的源码生成patch并在Petalinux中编译。
操作系统:Ubuntu 18.04
工具:Petalinux 2020.2
开发板:ZCU102 Rev 1.0
源码:https://github.com/Xilinx/u-boot-xlnx
目标:出于演示目的,我们将修改u-boot/cmd/bdinfo.c 文件,在bdinfo命令中加入一段打印,最终通过上板执行bdinfo命令来验证效果。
前菜:生成patch
首先clone u-boot 源码到本地,切换到Petalinux 2020.2 对应的u-boot 分支。可从Petalinux 2020.2 release notes中(https://www.xilinx.com/support/answers/75775.html )查找对应的u-boot 源码 branch和tag信息。
接下来使用任何你喜欢的编辑器修改u-boot/cmd/bdinfo.c代码,在do_bdinfo函数体中加入最后几句打印信息,具体如下:
… … #if CONFIG_VAL(SYS_MALLOC_F_LEN) printf("Early malloc usage: %lx / %x\n", gd->malloc_ptr, CONFIG_VAL(SYS_MALLOC_F_LEN)); #endif if (gd->fdt_blob) print_num("fdt_blob", (ulong)gd->fdt_blob); // Add below lines to test patch-apply function printf("\r\n========================================================\r\n"); printf("If you see this line, the patch is applied successfully!\r\n"); printf("========================================================\r\n"); return 0; }
在u-boot 源码目录下执行“git diff cmd/bdinfo.c > bdinfo.patch" . 这个操作将在u-boot源码目录下生成 bdinfo.patch文件,其内容如下:
diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c index abd9151432..5675a7228d 100644 --- a/cmd/bdinfo.c +++ b/cmd/bdinfo.c @@ -352,6 +352,11 @@ static int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, if (gd->fdt_blob) print_num("fdt_blob", (ulong)gd->fdt_blob); + // Add below lines to test patch-apply function + printf("\r\n========================================================\r\n") + printf("If you see this line, the patch is applied successfully!\r\n") + printf("========================================================\r\n") + return 0; }
正餐:应用patch
把patch文件拷贝到Petalinux工程下project-spec/meta-user/recipes-bsp/u-boot/files文件夹中,
<project dir>/project-spec/meta-user/recipes-bsp/u-boot $ tree . ├── files │ ├── 0001-ubifs-distroboot-support.patch │ ├── bdinfo.patch │ ├── bsp.cfg │ └── platform-top.h ├── u-boot-xlnx_%.bbappend ├── …
编辑u-boot-xlnx_%.bbappend,添加SRC_URI += "file://bdinfo.patch",
SRC_URI_append = " file://platform-top.h" SRC_URI += "file://bsp.cfg" SRC_URI += "file://bdinfo.patch" FILESEXTRAPATHS_prepend := "${THISDIR}/files:" do_configure_append () { if [ "${U_BOOT_AUTO_CONFIG}" = "1" ]; then install ${WORKDIR}/platform-auto.h ${S}/include/configs/ install ${WORKDIR}/platform-top.h ${S}/include/configs/ fi } do_configure_append_microblaze () { if [ "${U_BOOT_AUTO_CONFIG}" = "1" ]; then install -d ${B}/source/board/xilinx/microblaze-generic/ install ${WORKDIR}/config.mk ${B}/source/board/xilinx/microblaze-generic/ fi }
餐后甜点:准备就绪,编译&运行
一切准备好了,接下来就是常规操作build petalinux工程并且打包boot.bin文件。最后上板启动u-boot, 运行bdinfo命令:
ZynqMP> bdinfo arch_number = 0x0000000000000000 boot_params = 0x0000000000000000 DRAM bank = 0x0000000000000000 -> start = 0x0000000000000000 -> size = 0x000000007ff00000 DRAM bank = 0x0000000000000001 -> start = 0x0000000800000000 -> size = 0x0000000080000000 baudrate = 115200 bps TLB addr = 0x000000007fee0000 relocaddr = 0x000000007fdbb000 reloc off = 0x000000006fd3b000 irq_sp = 0x000000007dd9daf0 sp start = 0x000000007dd9daf0 ARM frequency = 99 MHz DSP frequency = 0 MHz DDR frequency = 0 MHz Early malloc usage: fa8 / 8000 fdt_blob = 0x000000007dd9db08 ======================================================== If you see this line, the patch is applied successfully! ======================================================== ZynqMP>
我们添加的打印信息在bdinfo中打印了出来,可见patch成功了!
回味:
这个简单例子演示了Petalinux工程patch源码的完整操作。同样可参照该流程对kernel/FSBL等其它源码进行patch. 需要注意的地方:
1. 用来生成patch的源码版本必须与Petalinux编译用的源码版本完全一致,否则patch文件无法匹配。
2.也可使用linux的diff 命令来生成patch. 需要注意 generate patch时目标文件夹深度要与apply时一致,否则patch也无法匹配(参考阅读diff命令的-p选项)。
3. 修改u-boot-xlnx_%.bbappend文件以将patch加入到build过程中,注意如果/project-spec/meta-user/recipes-bsp/u-boot文件夹不存在的话(很有可能),可以按照该目录结构自己来创建。.bbappend文件的作用及语法可参考Yocto相关文档。