积分305 / 贡献0

提问7答案被采纳4文章45

[经验分享] 如何一键优雅的更新内核patch 原创

润开鸿_闻飞 显示全部楼层 发表于 2024-5-17 14:11:29

itopen组织 1、提供OpenHarmony优雅实用的小工具 2、手把手适配riscv + qemu + linux的三方库移植 3、未来计划riscv + qemu + ohos的三方库移植 + 小程序开发 4、一切拥抱开源,拥抱国产化

一、进入内核,修改内核代码

cd kernel/linux/linux-5.10
vim drivers/Kconfig
# 在倒数第二行加入:source "drivers/tracepoint/Kconfig"

source "drivers/most/Kconfig"

source "drivers/accesstokenid/Kconfig"

source "drivers/tracepoint/Kconfig"
endmenu

二、打patch

patch -p1 < ../patches/linux-5.10/rk3568_patch/kernel.patch

如果想撤回patch方法

patch -Rp1 < ../patches/linux-5.10/rk3568_patch/kernel.patch

三、解决冲突(如果存在)

$ git status | grep rej
Refresh index: 100% (70816/70816), done.
        drivers/Kconfig.rej

上面表示drivers/Kconfig文件的打patch时候有冲突被拒绝了,需要手动进行修改

  • 打开drivers/Kconfig.rej文件

    --- drivers/Kconfig
    +++ drivers/Kconfig
    @@ -239,4 +239,8 @@ source "drivers/counter/Kconfig"
     source "drivers/most/Kconfig"
    
     source "drivers/accesstokenid/Kconfig"
    +
    +source "drivers/rkflash/Kconfig"
    +
    +source "drivers/rk_nand/Kconfig"
     endmenu

    这个文件的意思是需要在source "drivers/accesstokenid/Kconfig"的下面添加四行内容

  • 打开drivers/Kconfig文件,手动解决patch添加

    # 修改前
    source "drivers/most/Kconfig"
    
    source "drivers/accesstokenid/Kconfig"
    
    source "drivers/tracepoint/Kconfig"
    endmenu
    
    # 修改后
    source "drivers/most/Kconfig"
    
    source "drivers/accesstokenid/Kconfig"
    
    source "drivers/tracepoint/Kconfig"
    
    source "drivers/rkflash/Kconfig"
    
    source "drivers/rk_nand/Kconfig"
    endmenu
  • 删除冲突生成的临时orig和rej文件

    rm -rf drivers/Kconfig.orig drivers/Kconfig.rej
  • 至此冲突解决完

四、生成新的patch

git生成patch和打patch

git add .
git commit . -m"add tracepoint"
# 如果只有一次提交使用format-patch方便
git format-patch HEAD^
# 如果想打commit1和commit2之间的patch
# 其中commit1是起始的commit; commit2是结束的commit
git diff commit1 commit2 > demo.patch

此时会生成一个patch文件0001-add-tracepoint.patch

五、原patch修改

我们比对0001-add-tracepoint.patch 和 ../patches/linux-5.10/rk3568_patch/kernel.patch文件发现差异特别大,那是因为每次生成的patch时候对应的文件顺序是不一样的,所以差异比较大,我们要做的是将我们修改的文件patch内容替换即可。
# 比较文件的方法
diff 0001-add-tracepoint.patch ../patches/linux-5.10/rk3568_patch/kernel.patch

打开0001-add-tracepoint.patch文件搜索drivers/Kconfig,将如下内容替换掉../patches/linux-5.10/rk3568_patch/kernel.patch中的相同位置内容,至此,patch修改完成

# 0001-add-tracepoint.patch中的内容
diff --git a/drivers/Kconfig b/drivers/Kconfig
index 826b2b19d..5720a24fb 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -239,4 +239,10 @@ source "drivers/counter/Kconfig"
 source "drivers/most/Kconfig"

 source "drivers/accesstokenid/Kconfig"
+
+source "drivers/tracepoint/Kconfig"
+
+source "drivers/rkflash/Kconfig"
+
+source "drivers/rk_nand/Kconfig"
 endmenu

# 原../patches/linux-5.10/rk3568_patch/kernel.patch中的内容
diff --git a/drivers/Kconfig b/drivers/Kconfig
index 826b2b19d..d7f2fc98b 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -239,4 +239,8 @@ source "drivers/counter/Kconfig"
 source "drivers/most/Kconfig"

 source "drivers/accesstokenid/Kconfig"
+
+source "drivers/rkflash/Kconfig"
+
+source "drivers/rk_nand/Kconfig"
 endmenu

# 替换后../patches/linux-5.10/rk3568_patch/kernel.patch中的内容
diff --git a/drivers/Kconfig b/drivers/Kconfig
index 826b2b19d..5720a24fb 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -239,4 +239,10 @@ source "drivers/counter/Kconfig"
 source "drivers/most/Kconfig"

 source "drivers/accesstokenid/Kconfig"
+
+source "drivers/tracepoint/Kconfig"
+
+source "drivers/rkflash/Kconfig"
+
+source "drivers/rk_nand/Kconfig"
 endmenu

六、内核版本回退

rm 0001-add-tracepoint.patch
git reset --hard HEAD^

七、至此kernel.patch修改完成

©著作权归作者所有,转载或内容合作请联系作者

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部