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