| 1 | [[PageOutline]] |
| 2 | |
| 3 | = Patching OpenWrt = |
| 4 | |
| 5 | The OpenWrt Linux distribution like most other distributions consists of a build system that fetches, prepares, patches, compiles, and creates packages for various kernel and userspace components. Often patches are necessary to upstream source to either make it appropriate for embedded systems, OpenWrt in general, or to simply add a feature or fix a bug that you need. |
| 6 | |
| 7 | The patch process is fairly straight-forward but does take some getting used to and can differ between kernel, out-of-tree kernel-modules, and userspace packages slightly. |
| 8 | |
| 9 | == Patching a Package == |
| 10 | For this example we will patch busybox |
| 11 | |
| 12 | Steps: |
| 13 | * shell1 cd to the OpenWrt trunk and prepare the package for development using quilt to track patches: |
| 14 | {{{ |
| 15 | make package/busybox/{clean,prepare} QUILT=1 V=s |
| 16 | }}} |
| 17 | * open a new shell2 into the package build directory (this differs depending on the target you are working with and the package name/version): |
| 18 | {{{ |
| 19 | cd build_dir/target-arm_v6k_uClibc-0.9.33.2_eabi/busybox-1.19.4 |
| 20 | }}} |
| 21 | * understand the current patches: |
| 22 | {{{ |
| 23 | quilt series ;# show all patches in series |
| 24 | quilt top ;# show current patch in series |
| 25 | quilt files ;# show files in the current patch |
| 26 | quilt diff ;# show the current patch |
| 27 | quilt refresh ;# update current patch |
| 28 | }}} |
| 29 | * tell quilt you want to make a new patch |
| 30 | {{{ |
| 31 | quilt new 999-fix_foo.patch |
| 32 | }}} |
| 33 | * add/edit files to patch (using 'quilt edit' is a good way to avoid forgetting to add a file to a patch): |
| 34 | {{{ |
| 35 | quilt edit foo/foo.c |
| 36 | }}} |
| 37 | * in shell1 rebuild and test your package - repeat this edit/rebuild process until you are satisfied with your changes: |
| 38 | {{{ |
| 39 | make package/busybox/{compile,install} V=s ;# build |
| 40 | make package/index ;# rebuild package indexes if you are using opkg to update/test modules |
| 41 | make -j8 V=s ;# rebuild entire image if you are using that to test your changes |
| 42 | }}} |
| 43 | * when satisfied update the patches in the kernel tree |
| 44 | {{{ |
| 45 | make target/busybox/update ;# copy patches from build dir back to package dir |
| 46 | ls package/busybox/patches ;# notice your patch |
| 47 | }}} |
| 48 | |
| 49 | == Patching the Kernel == |
| 50 | OpenWrt kernel patching differs from package patching in the following ways: |
| 51 | * quilt is always enabled in the kernel tree |
| 52 | * there are 2 sets of patches: |
| 53 | * generic - from target/linux/generic/patches* that apply to the kernel in general |
| 54 | * platform - from target/linux/<target>/patches* that apply to the target platform only |
| 55 | |
| 56 | Steps: |
| 57 | * shell1 cd to the OpenWrt trunk and prepare the kernel for development using quilt to track patches: |
| 58 | {{{ |
| 59 | make target/linux/{clean,prepare} V=s |
| 60 | }}} |
| 61 | * open a new shell2 into the kernel build directory (this differs depending on the target/kernel you are working with): |
| 62 | {{{ |
| 63 | cd build_dir/target-arm_v6k_uClibc-0.9.33.2_eabi/linux-cns3xxx/linux-3.8.6/ |
| 64 | }}} |
| 65 | * understand the current patches: |
| 66 | {{{ |
| 67 | quilt series ;# show all patches in series |
| 68 | quilt top ;# show current patch in series |
| 69 | quilt files ;# show files in the current patch |
| 70 | quilt diff ;# show the current patch |
| 71 | quilt refresh ;# update current patch |
| 72 | }}} |
| 73 | * tell quilt you want to make a new patch |
| 74 | {{{ |
| 75 | quilt new platform/999-fix_foo.patch |
| 76 | }}} |
| 77 | * add/edit files to patch (using 'quilt edit' is a good way to avoid forgetting to add a file to a patch): |
| 78 | {{{ |
| 79 | quilt edit drivers/foo/foo.c |
| 80 | }}} |
| 81 | * in shell1 rebuild and test your package - repeat this edit/rebuild process until you are satisfied with your changes: |
| 82 | {{{ |
| 83 | make target/linux/{compile,install} V=s ;# build kernel and modules |
| 84 | make package/index ;# rebuild package indexes if you are using opkg to update/test modules |
| 85 | make -j8 V=s ;# rebuild entire image if you are using that to test your kernel/modules |
| 86 | }}} |
| 87 | * when satisfied update the patches in the kernel tree |
| 88 | {{{ |
| 89 | make target/linux/update |
| 90 | }}} |
| 91 | |
| 92 | == Pushing patches upstream == |
| 93 | It is highly recommended that you push your patches upstream so that you don't have to maintain them in the future. This can be tricky as it is required that you apply/test/format your patch on the latest upstream OpenWrt. You will have to decide if this is appropriate for you. If you do not push your patch upstream you will need to maintain it in your build directory manually. You can look at the scripts/makepatch script that Gateworks uses to maintain our patches against OpenWrt. We strive to continually push our patches upstream. |
| 94 | |
| 95 | == Applying a .patch file to build == |
| 96 | If a .patch file is acquired from somewhere, this is how you would apply it to a build of OpenWrt. |
| 97 | 1. Place the .patch file in the trunk directory |
| 98 | 2. Depending on the structure of the patch, do a dry run: |
| 99 | 1. |
| 100 | {{{ |
| 101 | patch -p0 --dry-run < ath10kpatch.patch |
| 102 | }}} |
| 103 | 3. If the patch looks good, then apply the patch, knowing you will modify the code: |
| 104 | 1. |
| 105 | {{{ |
| 106 | patch -p0 < ath10kpatch.patch |
| 107 | }}} |
| 108 | 4. Rebuild using make -j8 v=99 in the trunk directory now. |