| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #! /bin/sh | 
|  | 2 | # Script to apply kernel patches. | 
|  | 3 | #   usage: patch-kernel [ sourcedir [ patchdir [ stopversion ] [ -acxx ] ] ] | 
|  | 4 | #     The source directory defaults to /usr/src/linux, and the patch | 
|  | 5 | #     directory defaults to the current directory. | 
|  | 6 | # e.g. | 
|  | 7 | #   scripts/patch-kernel . .. | 
|  | 8 | #      Update the kernel tree in the current directory using patches in the | 
|  | 9 | #      directory above to the latest Linus kernel | 
|  | 10 | #   scripts/patch-kernel . .. -ac | 
|  | 11 | #      Get the latest Linux kernel and patch it with the latest ac patch | 
|  | 12 | #   scripts/patch-kernel . .. 2.4.9 | 
|  | 13 | #      Gets standard kernel 2.4.9 | 
|  | 14 | #   scripts/patch-kernel . .. 2.4.9 -ac | 
|  | 15 | #      Gets 2.4.9 with latest ac patches | 
|  | 16 | #   scripts/patch-kernel . .. 2.4.9 -ac11 | 
|  | 17 | #      Gets 2.4.9 with ac patch ac11 | 
|  | 18 | #   Note: It uses the patches relative to the Linus kernels, not the | 
|  | 19 | #   ac to ac relative patches | 
|  | 20 | # | 
|  | 21 | # It determines the current kernel version from the top-level Makefile. | 
|  | 22 | # It then looks for patches for the next sublevel in the patch directory. | 
|  | 23 | # This is applied using "patch -p1 -s" from within the kernel directory. | 
|  | 24 | # A check is then made for "*.rej" files to see if the patch was | 
|  | 25 | # successful.  If it is, then all of the "*.orig" files are removed. | 
|  | 26 | # | 
|  | 27 | #       Nick Holloway <Nick.Holloway@alfie.demon.co.uk>, 2nd January 1995. | 
|  | 28 | # | 
|  | 29 | # Added support for handling multiple types of compression. What includes | 
|  | 30 | # gzip, bzip, bzip2, zip, compress, and plaintext. | 
|  | 31 | # | 
|  | 32 | #       Adam Sulmicki <adam@cfar.umd.edu>, 1st January 1997. | 
|  | 33 | # | 
|  | 34 | # Added ability to stop at a given version number | 
|  | 35 | # Put the full version number (i.e. 2.3.31) as the last parameter | 
|  | 36 | #       Dave Gilbert <linux@treblig.org>, 11th December 1999. | 
|  | 37 |  | 
|  | 38 | # Fixed previous patch so that if we are already at the correct version | 
|  | 39 | # not to patch up. | 
|  | 40 | # | 
|  | 41 | # Added -ac option, use -ac or -ac9 (say) to stop at a particular version | 
|  | 42 | #       Dave Gilbert <linux@treblig.org>, 29th September 2001. | 
|  | 43 | # | 
|  | 44 | # Add support for (use of) EXTRAVERSION (to support 2.6.8.x, e.g.); | 
|  | 45 | # update usage message; | 
|  | 46 | # fix some whitespace damage; | 
|  | 47 | # be smarter about stopping when current version is larger than requested; | 
| Adrian Bunk | f4b09eb | 2006-01-03 13:37:51 +0100 | [diff] [blame] | 48 | #	Randy Dunlap <rdunlap@xenotime.net>, 2004-AUG-18. | 
| Randy.Dunlap | 1922163 | 2005-05-05 16:15:43 -0700 | [diff] [blame] | 49 | # | 
|  | 50 | # Add better support for (non-incremental) 2.6.x.y patches; | 
|  | 51 | # If an ending version number if not specified, the script automatically | 
|  | 52 | # increments the SUBLEVEL (x in 2.6.x.y) until no more patch files are found; | 
|  | 53 | # however, EXTRAVERSION (y in 2.6.x.y) is never automatically incremented | 
|  | 54 | # but must be specified fully. | 
|  | 55 | # | 
|  | 56 | # patch-kernel does not normally support reverse patching, but does so when | 
|  | 57 | # applying EXTRAVERSION (x.y) patches, so that moving from 2.6.11.y to 2.6.11.z | 
|  | 58 | # is easy and handled by the script (reverse 2.6.11.y and apply 2.6.11.z). | 
| Adrian Bunk | f4b09eb | 2006-01-03 13:37:51 +0100 | [diff] [blame] | 59 | #	Randy Dunlap <rdunlap@xenotime.net>, 2005-APR-08. | 
| Randy.Dunlap | 1922163 | 2005-05-05 16:15:43 -0700 | [diff] [blame] | 60 |  | 
|  | 61 | PNAME=patch-kernel | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 |  | 
|  | 63 | # Set directories from arguments, or use defaults. | 
|  | 64 | sourcedir=${1-/usr/src/linux} | 
|  | 65 | patchdir=${2-.} | 
|  | 66 | stopvers=${3-default} | 
|  | 67 |  | 
| Andreas Mohr | 22d6a6a | 2007-11-17 21:51:25 +0100 | [diff] [blame] | 68 | if [ "$1" = -h -o "$1" = --help -o ! -r "$sourcedir/Makefile" ]; then | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | cat << USAGE | 
| Randy.Dunlap | 1922163 | 2005-05-05 16:15:43 -0700 | [diff] [blame] | 70 | usage: $PNAME [-h] [ sourcedir [ patchdir [ stopversion ] [ -acxx ] ] ] | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | source directory defaults to /usr/src/linux, | 
|  | 72 | patch directory defaults to the current directory, | 
|  | 73 | stopversion defaults to <all in patchdir>. | 
|  | 74 | USAGE | 
|  | 75 | exit 1 | 
|  | 76 | fi | 
|  | 77 |  | 
|  | 78 | # See if we have any -ac options | 
|  | 79 | for PARM in $* | 
|  | 80 | do | 
|  | 81 | case $PARM in | 
|  | 82 | -ac*) | 
|  | 83 | gotac=$PARM; | 
|  | 84 |  | 
|  | 85 | esac; | 
|  | 86 | done | 
|  | 87 |  | 
|  | 88 | # --------------------------------------------------------------------------- | 
| Randy.Dunlap | 1922163 | 2005-05-05 16:15:43 -0700 | [diff] [blame] | 89 | # arg1 is filename | 
|  | 90 | noFile () { | 
|  | 91 | echo "cannot find patch file: ${patch}" | 
|  | 92 | exit 1 | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | # --------------------------------------------------------------------------- | 
|  | 96 | backwards () { | 
|  | 97 | echo "$PNAME does not support reverse patching" | 
|  | 98 | exit 1 | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | # --------------------------------------------------------------------------- | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | # Find a file, first parameter is basename of file | 
|  | 103 | # it tries many compression mechanisms and sets variables to say how to get it | 
|  | 104 | findFile () { | 
|  | 105 | filebase=$1; | 
|  | 106 |  | 
|  | 107 | if [ -r ${filebase}.gz ]; then | 
|  | 108 | ext=".gz" | 
|  | 109 | name="gzip" | 
|  | 110 | uncomp="gunzip -dc" | 
|  | 111 | elif [ -r ${filebase}.bz  ]; then | 
|  | 112 | ext=".bz" | 
|  | 113 | name="bzip" | 
|  | 114 | uncomp="bunzip -dc" | 
|  | 115 | elif [ -r ${filebase}.bz2 ]; then | 
|  | 116 | ext=".bz2" | 
|  | 117 | name="bzip2" | 
|  | 118 | uncomp="bunzip2 -dc" | 
| Shawn Landden | 354fa22 | 2012-03-12 15:13:37 -0700 | [diff] [blame] | 119 | elif [ -r ${filebase}.xz ]; then | 
|  | 120 | ext=".xz" | 
|  | 121 | name="xz" | 
|  | 122 | uncomp="xz -dc" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | elif [ -r ${filebase}.zip ]; then | 
|  | 124 | ext=".zip" | 
|  | 125 | name="zip" | 
|  | 126 | uncomp="unzip -d" | 
|  | 127 | elif [ -r ${filebase}.Z ]; then | 
|  | 128 | ext=".Z" | 
|  | 129 | name="uncompress" | 
|  | 130 | uncomp="uncompress -c" | 
|  | 131 | elif [ -r ${filebase} ]; then | 
|  | 132 | ext="" | 
|  | 133 | name="plaintext" | 
|  | 134 | uncomp="cat" | 
|  | 135 | else | 
|  | 136 | return 1; | 
|  | 137 | fi | 
|  | 138 |  | 
|  | 139 | return 0; | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | # --------------------------------------------------------------------------- | 
|  | 143 | # Apply a patch and check it goes in cleanly | 
|  | 144 | # First param is patch name (e.g. patch-2.4.9-ac5) - without path or extension | 
|  | 145 |  | 
|  | 146 | applyPatch () { | 
|  | 147 | echo -n "Applying $1 (${name})... " | 
|  | 148 | if $uncomp ${patchdir}/$1${ext} | patch -p1 -s -N -E -d $sourcedir | 
|  | 149 | then | 
|  | 150 | echo "done." | 
|  | 151 | else | 
|  | 152 | echo "failed.  Clean up yourself." | 
|  | 153 | return 1; | 
|  | 154 | fi | 
|  | 155 | if [ "`find $sourcedir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] | 
|  | 156 | then | 
|  | 157 | echo "Aborting.  Reject files found." | 
|  | 158 | return 1; | 
|  | 159 | fi | 
|  | 160 | # Remove backup files | 
|  | 161 | find $sourcedir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \; | 
|  | 162 |  | 
|  | 163 | return 0; | 
|  | 164 | } | 
|  | 165 |  | 
| Randy.Dunlap | 1922163 | 2005-05-05 16:15:43 -0700 | [diff] [blame] | 166 | # --------------------------------------------------------------------------- | 
|  | 167 | # arg1 is patch filename | 
|  | 168 | reversePatch () { | 
|  | 169 | echo -n "Reversing $1 (${name}) ... " | 
|  | 170 | if $uncomp ${patchdir}/"$1"${ext} | patch -p1 -Rs -N -E -d $sourcedir | 
|  | 171 | then | 
|  | 172 | echo "done." | 
|  | 173 | else | 
|  | 174 | echo "failed.  Clean it up." | 
|  | 175 | exit 1 | 
|  | 176 | fi | 
|  | 177 | if [ "`find $sourcedir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ] | 
|  | 178 | then | 
|  | 179 | echo "Aborting.  Reject files found." | 
|  | 180 | return 1 | 
|  | 181 | fi | 
|  | 182 | # Remove backup files | 
|  | 183 | find $sourcedir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \; | 
|  | 184 |  | 
|  | 185 | return 0 | 
|  | 186 | } | 
|  | 187 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | # set current VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION | 
| Andreas Mohr | 22d6a6a | 2007-11-17 21:51:25 +0100 | [diff] [blame] | 189 | # force $TMPFILEs below to be in local directory: a slash character prevents | 
|  | 190 | # the dot command from using the search path. | 
|  | 191 | TMPFILE=`mktemp ./.tmpver.XXXXXX` || { echo "cannot make temp file" ; exit 1; } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | grep -E "^(VERSION|PATCHLEVEL|SUBLEVEL|EXTRAVERSION)" $sourcedir/Makefile > $TMPFILE | 
|  | 193 | tr -d [:blank:] < $TMPFILE > $TMPFILE.1 | 
| Andreas Mohr | 22d6a6a | 2007-11-17 21:51:25 +0100 | [diff] [blame] | 194 | . $TMPFILE.1 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | rm -f $TMPFILE* | 
|  | 196 | if [ -z "$VERSION" -o -z "$PATCHLEVEL" -o -z "$SUBLEVEL" ] | 
|  | 197 | then | 
|  | 198 | echo "unable to determine current kernel version" >&2 | 
|  | 199 | exit 1 | 
|  | 200 | fi | 
|  | 201 |  | 
|  | 202 | NAME=`grep ^NAME $sourcedir/Makefile` | 
|  | 203 | NAME=${NAME##*=} | 
|  | 204 |  | 
|  | 205 | echo "Current kernel version is $VERSION.$PATCHLEVEL.$SUBLEVEL${EXTRAVERSION} ($NAME)" | 
|  | 206 |  | 
|  | 207 | # strip EXTRAVERSION to just a number (drop leading '.' and trailing additions) | 
|  | 208 | EXTRAVER= | 
|  | 209 | if [ x$EXTRAVERSION != "x" ] | 
|  | 210 | then | 
| Andreas Mohr | 22d6a6a | 2007-11-17 21:51:25 +0100 | [diff] [blame] | 211 | EXTRAVER=${EXTRAVERSION#.} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | EXTRAVER=${EXTRAVER%%[[:punct:]]*} | 
| Randy.Dunlap | 1922163 | 2005-05-05 16:15:43 -0700 | [diff] [blame] | 213 | #echo "$PNAME: changing EXTRAVERSION from $EXTRAVERSION to $EXTRAVER" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | fi | 
|  | 215 |  | 
|  | 216 | #echo "stopvers=$stopvers" | 
|  | 217 | if [ $stopvers != "default" ]; then | 
|  | 218 | STOPSUBLEVEL=`echo $stopvers | cut -d. -f3` | 
|  | 219 | STOPEXTRA=`echo $stopvers | cut -d. -f4` | 
| Erkki Lintunen | 0758416 | 2008-08-06 22:11:33 +0200 | [diff] [blame] | 220 | STOPFULLVERSION=${stopvers%%.$STOPEXTRA} | 
| Randy.Dunlap | 1922163 | 2005-05-05 16:15:43 -0700 | [diff] [blame] | 221 | #echo "#___STOPSUBLEVEL=/$STOPSUBLEVEL/, STOPEXTRA=/$STOPEXTRA/" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | else | 
|  | 223 | STOPSUBLEVEL=9999 | 
|  | 224 | STOPEXTRA=9999 | 
|  | 225 | fi | 
|  | 226 |  | 
| Randy.Dunlap | 1922163 | 2005-05-05 16:15:43 -0700 | [diff] [blame] | 227 | # This all assumes a 2.6.x[.y] kernel tree. | 
|  | 228 | # Don't allow backwards/reverse patching. | 
|  | 229 | if [ $STOPSUBLEVEL -lt $SUBLEVEL ]; then | 
|  | 230 | backwards | 
|  | 231 | fi | 
|  | 232 |  | 
|  | 233 | if [ x$EXTRAVER != "x" ]; then | 
|  | 234 | CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$EXTRAVER" | 
|  | 235 | else | 
|  | 236 | CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL" | 
|  | 237 | fi | 
|  | 238 |  | 
|  | 239 | if [ x$EXTRAVER != "x" ]; then | 
|  | 240 | echo "backing up to: $VERSION.$PATCHLEVEL.$SUBLEVEL" | 
|  | 241 | patch="patch-${CURRENTFULLVERSION}" | 
|  | 242 | findFile $patchdir/${patch} || noFile ${patch} | 
|  | 243 | reversePatch ${patch} || exit 1 | 
|  | 244 | fi | 
|  | 245 |  | 
|  | 246 | # now current is 2.6.x, with no EXTRA applied, | 
|  | 247 | # so update to target SUBLEVEL (2.6.SUBLEVEL) | 
|  | 248 | # and then to target EXTRAVER (2.6.SUB.EXTRAVER) if requested. | 
|  | 249 | # If not ending sublevel is specified, it is incremented until | 
|  | 250 | # no further sublevels are found. | 
|  | 251 |  | 
|  | 252 | if [ $STOPSUBLEVEL -gt $SUBLEVEL ]; then | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | while :				# incrementing SUBLEVEL (s in v.p.s) | 
|  | 254 | do | 
| Randy.Dunlap | 1922163 | 2005-05-05 16:15:43 -0700 | [diff] [blame] | 255 | CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL" | 
|  | 256 | EXTRAVER= | 
| Andreas Mohr | 177525d | 2011-04-03 20:58:28 +0200 | [diff] [blame] | 257 | if [ x$STOPFULLVERSION = x$CURRENTFULLVERSION ]; then | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | echo "Stopping at $CURRENTFULLVERSION base as requested." | 
|  | 259 | break | 
|  | 260 | fi | 
|  | 261 |  | 
| Andreas Mohr | 22d6a6a | 2007-11-17 21:51:25 +0100 | [diff] [blame] | 262 | SUBLEVEL=$(($SUBLEVEL + 1)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL" | 
| Randy.Dunlap | 1922163 | 2005-05-05 16:15:43 -0700 | [diff] [blame] | 264 | #echo "#___ trying $FULLVERSION ___" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 |  | 
| Andreas Mohr | 22d6a6a | 2007-11-17 21:51:25 +0100 | [diff] [blame] | 266 | if [ $(($SUBLEVEL)) -gt $(($STOPSUBLEVEL)) ]; then | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | echo "Stopping since sublevel ($SUBLEVEL) is beyond stop-sublevel ($STOPSUBLEVEL)" | 
|  | 268 | exit 1 | 
|  | 269 | fi | 
|  | 270 |  | 
|  | 271 | patch=patch-$FULLVERSION | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | # See if the file exists and find extension | 
| Randy.Dunlap | 1922163 | 2005-05-05 16:15:43 -0700 | [diff] [blame] | 273 | findFile $patchdir/${patch} || noFile ${patch} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 |  | 
|  | 275 | # Apply the patch and check all is OK | 
|  | 276 | applyPatch $patch || break | 
|  | 277 | done | 
| Randy.Dunlap | 1922163 | 2005-05-05 16:15:43 -0700 | [diff] [blame] | 278 | #echo "#___sublevel all done" | 
|  | 279 | fi | 
|  | 280 |  | 
|  | 281 | # There is no incremental searching for extraversion... | 
|  | 282 | if [ "$STOPEXTRA" != "" ]; then | 
|  | 283 | while :				# just to allow break | 
|  | 284 | do | 
|  | 285 | # apply STOPEXTRA directly (not incrementally) (x in v.p.s.x) | 
|  | 286 | FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$STOPEXTRA" | 
|  | 287 | #echo "#... trying $FULLVERSION ..." | 
|  | 288 | patch=patch-$FULLVERSION | 
|  | 289 |  | 
|  | 290 | # See if the file exists and find extension | 
|  | 291 | findFile $patchdir/${patch} || noFile ${patch} | 
|  | 292 |  | 
|  | 293 | # Apply the patch and check all is OK | 
|  | 294 | applyPatch $patch || break | 
|  | 295 | #echo "#___extraver all done" | 
|  | 296 | break | 
|  | 297 | done | 
|  | 298 | fi | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 |  | 
|  | 300 | if [ x$gotac != x ]; then | 
|  | 301 | # Out great user wants the -ac patches | 
|  | 302 | # They could have done -ac (get latest) or -acxx where xx=version they want | 
| Andreas Mohr | 22d6a6a | 2007-11-17 21:51:25 +0100 | [diff] [blame] | 303 | if [ $gotac = "-ac" ]; then | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | # They want the latest version | 
|  | 305 | HIGHESTPATCH=0 | 
|  | 306 | for PATCHNAMES in $patchdir/patch-${CURRENTFULLVERSION}-ac*\.* | 
|  | 307 | do | 
|  | 308 | ACVALUE=`echo $PATCHNAMES | sed -e 's/^.*patch-[0-9.]*-ac\([0-9]*\).*/\1/'` | 
|  | 309 | # Check it is actually a recognised patch type | 
|  | 310 | findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${ACVALUE} || break | 
|  | 311 |  | 
|  | 312 | if [ $ACVALUE -gt $HIGHESTPATCH ]; then | 
|  | 313 | HIGHESTPATCH=$ACVALUE | 
|  | 314 | fi | 
|  | 315 | done | 
|  | 316 |  | 
|  | 317 | if [ $HIGHESTPATCH -ne 0 ]; then | 
|  | 318 | findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH} || break | 
|  | 319 | applyPatch patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH} | 
|  | 320 | else | 
|  | 321 | echo "No -ac patches found" | 
|  | 322 | fi | 
|  | 323 | else | 
|  | 324 | # They want an exact version | 
|  | 325 | findFile $patchdir/patch-${CURRENTFULLVERSION}${gotac} || { | 
|  | 326 | echo "Sorry, I couldn't find the $gotac patch for $CURRENTFULLVERSION.  Hohum." | 
|  | 327 | exit 1 | 
|  | 328 | } | 
|  | 329 | applyPatch patch-${CURRENTFULLVERSION}${gotac} | 
|  | 330 | fi | 
|  | 331 | fi |