跳转到主要内容

开发者分享 | 如何给 u-boot 的源码生成 patch 并在 Petalinux 中编译

本文转载自:<span id="profileBt"><a href="https://mp.weixin.qq.com/s/T1Y7mQV8UmYcrj5SeP_-1Q">XILINX开发者社区微信公众号</a>…;

<font color="#FF8000">本文来自 XILINX 产品应用工程师 张超</font>

在软件开发过程中我们经常遇到用 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 命令来验证效果。

<strong>前菜: 生成 patch </strong>

首先 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 函数体中加入最后几句打印信息,具体如下:
<pre>… …

#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-&gt;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;

} </pre>

在 u-boot 源码目录下执行 “git diff cmd/bdinfo.c > bdinfo.patch" . 这个操作将在 u-boot源码目录下生成 bdinfo.patch 文件,其内容如下:
<pre>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;

} </pre>

<strong>正餐: 应用 patch</strong>

把 patch 文件拷贝到 Petalinux 工程下 project-spec/meta-user/recipes-bsp/u-boot/files文件夹中
<pre><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

├── … </pre>

编辑 u-boot-xlnx_%.bbappend,添加 SRC_URI += "file://bdinfo.patch",
<pre>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

} </pre>

<strong>餐后甜点:准备就绪,编译&运行 </strong>

一切准备好了,接下来就是常规操作 build petalinux 工程并且打包 boot.bin 文件。最后上板启动 u-boot, 运行 bdinfo 命令:
<pre>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> </pre>

我们添加的打印信息在 bdinfo 中打印了出来,可见 patch 成功了!

<strong>回味: </strong>

这个简单例子演示了 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 相关文档。