轉自: http://blog.csdn.net/wwx0715/article/details/25160361
一、前言
配置完Openwrt後,首次編譯時會在編譯過程中下載各種源碼包,而且解壓這些源碼包並打上patch。
需要對源碼進行修改時,可直接修改源碼並重新編譯,但clean後再次編譯時會再次解壓源碼包,以至所做
的修改全部丟失。本文參考Openwrt官網提供的patch管理方法,實踐操作並記錄linux kernel及package源
碼修改並生成patch的方法。
二、環境及準備工作
linux發行版:ubuntu12.0LTS
OpenWrt版本:trunk r37950(linux kernel 3.10.10)
硬件:ap121兼容模塊(ar9331)
2.2 準備工作
patch的管理工具使用quilt,在ubuntu下使用apt-get intallquilt安裝quilt工具。
爲了讓quilt創建適合OpenWrt格式的patch,需要在本地home目錄下創建quilt的配置文件.quiltrc,該配
置文件包含diff和patch的選項。使用如下命令可創建quilt的配置文件:
cat> ~/.quiltrc <<EOF
QUILT_DIFF_ARGS="--no-timestamps--no-index -pab --color=auto"
QUILT_REFRESH_ARGS="--no-timestamps--no-index -pab"
QUILT_PATCH_OPTS="--unified"
QUILT_DIFF_OPTS="-p"
EDITOR="vim"
EOF
EDITOR指定編輯時所用的編輯器,該處使用vim。
三、package的patch方法
3.1 修改內容
package的patch生成方法以修改tftp-hpa爲例進行介紹,修改內容爲在tftp的main函數中加入一條打印
信息。(進行該操作之前,需要在make menuconfig時選擇tftp-hpa包)
3.2 pactch生成步驟
1. 準備tftp-hpa源碼
make package/feeds/packages/tftp-hpa/{clean,prepare} V=s QUILT=1
此命令會解壓tftp-hpa的源碼包並準備patch文件(如果有),通過打印信息可獲取解壓到的目錄路徑。
2.進入tftp-hpa源碼目錄
cd build_dir/ /target-mips_r2_uClibc-0.9.33.2/tftp-hpa-0.48
3.安裝所有已有patch
quilt push -a
4.創建新patch
quilt new 001-main_test.patch
patch文件名以數字開頭,“-”後爲patch的描述信息
開頭的數字必須比已有patch的數字都大,使用命令quilt series查看已有patch的列表
5.修改源碼文件
quilt edit tftp/main.c
該命令將使用在.quiltrc中定義的編輯器打開main.c文件,在main函數中增加一條打印信息。
如果還有其他文件需要修改,可繼續用此命令進行修改
6.查看修改內容
quilt diff
7.更新修改到patch文件
quilt refresh
此命令會將更新的修改保存到當前目錄的patches/001-main_test.patch(如果沒有 patches目錄會自動創建)。
8.返回到buildroot目錄
cd ../../../
9.保存patch文件到buildroot
makepackage/feeds/packages/tftp-hpa/update V=s
10. 重新編譯tftp-hpa包以測試修改
make package/feeds/packages/tftp-hpa/{clean,compile} package/index V=s
11.如果有問題,需要編輯patch文件
3.3 編輯已有patch文件
當需要對patch進行修改時,可使用以下步驟:
1.準備tftp-hpa源碼
make package/feeds/packages/tftp-hpa/{clean,prepare} V=s QUILT=1
2. 進入tftp-hpa源碼目錄
cd build_dir/ /target-mips_r2_uClibc-0.9.33.2/tftp-hpa-0.48
3.列出可用的patch
quilt series
4.準備要修改的patch
quilt push 001-main_test.patch
此命令會按patch編號順序打補丁,直到指定的patch(包含)
如果當前應用的patch編號已經超過了指定的patch編號,將會按相反順序移除 patch直到指定的patch
5.編輯源碼文件
quilt edit tftp/main.c
6.檢查patch中包含的所修改的文件
quilt files
7.查看修改內容
quilt diff
8.保存修改到patch
quilt refresh
9.返回buildroot目錄
cd ../../../
10.保存patch文件到buildroot
make package/feeds/packages/tftp-hpa/update V=s
11.重新編譯tftp-hpa包以測試修改
make package/feeds/packages/tftp-hpa/{clean,compile} package/index V=s
四、linux內核patch方法
4.1 修改內容
使用的模塊爲基於ar9331的ap121兼容板,根據該模塊的硬件需要在ap121的基礎上
做兩處修改:
1.硬件復位的GPIO變更(arch/mips/ath79/mach-ap121.c)
2.增加新flash s25fl164k1的支持(driver/mtd/devices/m25p80.c)
內核的patch分兩種,一種是基於platform的,另一種是generic的,修改1屬於platform,修改2屬於generic。
4.2 platform patch生成步驟
platform形式的patch生成步驟如下(源碼修改mach-ap121.c):
1.準備內核源碼樹,使用如下命令
make target/linux/{clean,prepare} V=s QUILT=1
2.進入kernel源碼樹目錄
對attitudeadjustment版本,kernel源碼樹所在目錄爲build_dir/linux-*/linux-3.*
對本文使用的trunk版本,使用如下命令進入kernel源碼樹
cd build_dir/target-mips_r2_uClibc-0.9.33.2/linux-ar71xx_generic/linux-3.10.10
3.安裝所有已有patch
quilt push -a
4.創建新patch
quilt new platform/910-MIPS-ath79-ap121-reset-gpio-change.patch
patch文件名以數字開頭,“-”後爲patch的描述信息
開頭的數字必須比已有patch的數字都大,使用命令quilt series查看已有patch的列表
新建platform的patch時需要在patch名前添加“platform/”前綴
5.修改源碼文件
quilt edit arch/mips/ath79/mach-ap121.c
該命令將使用在.quiltrc中定義的編輯器打開mach-ap121.c文件,修改對復位GPIO的定義。
如果還有其他文件需要修改,可繼續用此命令進行修改
6.查看修改內容
quilt diff
7.更新修改到patch文件
quilt refresh
此命令會將更新的修改保存到當前目錄的patches/platform/910-MIPS-ath79-ap121-reset-gpio-change.patch
8.返回到buildroot目錄
cd ../../../../
9.保存patch文件到buildroot
make target/linux/update V=s
此命令會將910-MIPS-ath79-ap121-reset-gpio-change.patch保存到target/linux/ar71xx/patches-3.10/
4.3 generic patch生成步驟
generic形式的patch生成步驟如下(源碼修改m25p80.c):
1.準備內核源碼樹,使用如下命令
make target/linux/{clean,prepare} V=s QUILT=1
2.進入kernel源碼樹目錄
對attitude adjustment版本,kernel源碼樹所在目錄爲build_dir/linux-*/linux-3.*
對本文使用的trunk版本,使用如下命令進入kernel源碼樹
cd build_dir/target-mips_r2_uClibc-0.9.33.2/linux-ar71xx_generic/linux-3.10.10
3.安裝所有已有patch
quilt push -a
4.創建新patch
quilt new 998-mtd-m25p80-add-support-for-spansion-s25fl164k1-flash.patch
patch文件名以數字開頭,“-”後爲patch的描述信息
開頭的數字必須比已有patch的數字都大,使用命令quilt series查看已有patch的列表
新建generic的patch時需要在patch名前添加“generic/”前綴
5.修改源碼文件
quilt edit driver/mtd/devices/m25p80.c
該命令將使用在.quiltrc中定義的編輯器打開m25p80.c文件,增加對s25fl164k1的聲明。
如果還有其他文件需要修改,可繼續用此命令進行修改
6.查看修改內容
quilt diff
7.更新修改到patch文件
quilt refresh
此命令會將更新的修改保存到當前目錄的patches/generic/998-mtd-m25p80-add-support-for-spansion-s25fl164k1-flash.patch。
8.返回到buildroot目錄
cd ../../../../
9.保存patch文件到buildroot
make target/linux/update V=s
此命令會將998-mtd-m25p80-add-support-for-spansion-s25fl164k1-flash.patch保存到target/linux/generic/patches-3.10/
五、更新patches
當已打補丁的package(或者kernel)更新到新版本時,patch現有補丁時可能不會順利,出現一些不確定性。
可以通過make的refresh target重建整個patch。
對package,使用類似如下的命令:
make package/feeds/packages/tftp-hpa/refresh V=s
對kernel,使用如下命令:
make target/linux/refresh V=s
六、迭代修改patch
進行新的修改時,可能會對patch進行多次修改。爲了加快開發速度,可以在保持源碼樹的情況下進行修改操作。
1.準備源碼樹
2.進入源碼目錄
3. 應用所要打的patch
4.編輯源碼文件,更新patch
5.應用所有patch(quilt push -a)
6.返回buildroot目錄,運行命令make package/feeds/packages/tftp-hpa/{compile,install}或make target/linux/{compile,install}(對kernel)
7.測試固件
8.如果需要進一步修改,返回第二步
9,使用命令make package/feeds/packages/tftp-hpa/update或make target/linux/update(對kernel)將patches拷貝到buildroot
七、參考
主要參考OpenWrt的官網對patch的相關開發說明openwrt.org(Documentation->Developing->patches)。