Back to home page

OSCL-LXR

 
 

    


0001 #! /bin/sh
0002 # SPDX-License-Identifier: GPL-2.0
0003 # Script to apply kernel patches.
0004 #   usage: patch-kernel [ sourcedir [ patchdir [ stopversion ] [ -acxx ] ] ]
0005 #     The source directory defaults to /usr/src/linux, and the patch
0006 #     directory defaults to the current directory.
0007 # e.g.
0008 #   scripts/patch-kernel . ..
0009 #      Update the kernel tree in the current directory using patches in the
0010 #      directory above to the latest Linus kernel
0011 #   scripts/patch-kernel . .. -ac
0012 #      Get the latest Linux kernel and patch it with the latest ac patch
0013 #   scripts/patch-kernel . .. 2.4.9
0014 #      Gets standard kernel 2.4.9
0015 #   scripts/patch-kernel . .. 2.4.9 -ac
0016 #      Gets 2.4.9 with latest ac patches
0017 #   scripts/patch-kernel . .. 2.4.9 -ac11
0018 #      Gets 2.4.9 with ac patch ac11
0019 #   Note: It uses the patches relative to the Linus kernels, not the
0020 #   ac to ac relative patches
0021 #
0022 # It determines the current kernel version from the top-level Makefile.
0023 # It then looks for patches for the next sublevel in the patch directory.
0024 # This is applied using "patch -p1 -s" from within the kernel directory.
0025 # A check is then made for "*.rej" files to see if the patch was
0026 # successful.  If it is, then all of the "*.orig" files are removed.
0027 #
0028 #       Nick Holloway <Nick.Holloway@alfie.demon.co.uk>, 2nd January 1995.
0029 #
0030 # Added support for handling multiple types of compression. What includes
0031 # gzip, bzip, bzip2, zip, compress, and plaintext.
0032 #
0033 #       Adam Sulmicki <adam@cfar.umd.edu>, 1st January 1997.
0034 #
0035 # Added ability to stop at a given version number
0036 # Put the full version number (i.e. 2.3.31) as the last parameter
0037 #       Dave Gilbert <linux@treblig.org>, 11th December 1999.
0038 
0039 # Fixed previous patch so that if we are already at the correct version
0040 # not to patch up.
0041 #
0042 # Added -ac option, use -ac or -ac9 (say) to stop at a particular version
0043 #       Dave Gilbert <linux@treblig.org>, 29th September 2001.
0044 #
0045 # Add support for (use of) EXTRAVERSION (to support 2.6.8.x, e.g.);
0046 # update usage message;
0047 # fix some whitespace damage;
0048 # be smarter about stopping when current version is larger than requested;
0049 #       Randy Dunlap <rdunlap@xenotime.net>, 2004-AUG-18.
0050 #
0051 # Add better support for (non-incremental) 2.6.x.y patches;
0052 # If an ending version number if not specified, the script automatically
0053 # increments the SUBLEVEL (x in 2.6.x.y) until no more patch files are found;
0054 # however, EXTRAVERSION (y in 2.6.x.y) is never automatically incremented
0055 # but must be specified fully.
0056 #
0057 # patch-kernel does not normally support reverse patching, but does so when
0058 # applying EXTRAVERSION (x.y) patches, so that moving from 2.6.11.y to 2.6.11.z
0059 # is easy and handled by the script (reverse 2.6.11.y and apply 2.6.11.z).
0060 #       Randy Dunlap <rdunlap@xenotime.net>, 2005-APR-08.
0061 
0062 PNAME=patch-kernel
0063 
0064 # Set directories from arguments, or use defaults.
0065 sourcedir=${1-/usr/src/linux}
0066 patchdir=${2-.}
0067 stopvers=${3-default}
0068 
0069 if [ "$1" = -h -o "$1" = --help -o ! -r "$sourcedir/Makefile" ]; then
0070 cat << USAGE
0071 usage: $PNAME [-h] [ sourcedir [ patchdir [ stopversion ] [ -acxx ] ] ]
0072   source directory defaults to /usr/src/linux,
0073   patch directory defaults to the current directory,
0074   stopversion defaults to <all in patchdir>.
0075 USAGE
0076 exit 1
0077 fi
0078 
0079 # See if we have any -ac options
0080 for PARM in $*
0081 do
0082   case $PARM in
0083           -ac*)
0084                   gotac=$PARM;
0085 
0086         esac;
0087 done
0088 
0089 # ---------------------------------------------------------------------------
0090 # arg1 is filename
0091 noFile () {
0092         echo "cannot find patch file: ${patch}"
0093         exit 1
0094 }
0095 
0096 # ---------------------------------------------------------------------------
0097 backwards () {
0098         echo "$PNAME does not support reverse patching"
0099         exit 1
0100 }
0101 
0102 # ---------------------------------------------------------------------------
0103 # Find a file, first parameter is basename of file
0104 # it tries many compression mechanisms and sets variables to say how to get it
0105 findFile () {
0106   filebase=$1;
0107 
0108   if [ -r ${filebase}.gz ]; then
0109                 ext=".gz"
0110                 name="gzip"
0111                 uncomp="gunzip -dc"
0112   elif [ -r ${filebase}.bz  ]; then
0113                 ext=".bz"
0114                 name="bzip"
0115                 uncomp="bunzip -dc"
0116   elif [ -r ${filebase}.bz2 ]; then
0117                 ext=".bz2"
0118                 name="bzip2"
0119                 uncomp="bunzip2 -dc"
0120   elif [ -r ${filebase}.xz ]; then
0121                 ext=".xz"
0122                 name="xz"
0123                 uncomp="xz -dc"
0124   elif [ -r ${filebase}.zip ]; then
0125                 ext=".zip"
0126                 name="zip"
0127                 uncomp="unzip -d"
0128   elif [ -r ${filebase}.Z ]; then
0129                 ext=".Z"
0130                 name="uncompress"
0131                 uncomp="uncompress -c"
0132   elif [ -r ${filebase} ]; then
0133                 ext=""
0134                 name="plaintext"
0135                 uncomp="cat"
0136   else
0137         return 1;
0138   fi
0139 
0140   return 0;
0141 }
0142 
0143 # ---------------------------------------------------------------------------
0144 # Apply a patch and check it goes in cleanly
0145 # First param is patch name (e.g. patch-2.4.9-ac5) - without path or extension
0146 
0147 applyPatch () {
0148   echo -n "Applying $1 (${name})... "
0149   if $uncomp ${patchdir}/$1${ext} | patch -p1 -s -N -E -d $sourcedir
0150   then
0151     echo "done."
0152   else
0153     echo "failed.  Clean up yourself."
0154     return 1;
0155   fi
0156   if [ "`find $sourcedir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ]
0157   then
0158     echo "Aborting.  Reject files found."
0159     return 1;
0160   fi
0161   # Remove backup files
0162   find $sourcedir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;
0163 
0164   return 0;
0165 }
0166 
0167 # ---------------------------------------------------------------------------
0168 # arg1 is patch filename
0169 reversePatch () {
0170         echo -n "Reversing $1 (${name}) ... "
0171         if $uncomp ${patchdir}/"$1"${ext} | patch -p1 -Rs -N -E -d $sourcedir
0172         then
0173                 echo "done."
0174         else
0175                 echo "failed.  Clean it up."
0176                 exit 1
0177         fi
0178         if [ "`find $sourcedir/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ]
0179         then
0180                 echo "Aborting.  Reject files found."
0181                 return 1
0182         fi
0183         # Remove backup files
0184         find $sourcedir/ '(' -name '*.orig' -o -name '.*.orig' ')' -exec rm -f {} \;
0185 
0186         return 0
0187 }
0188 
0189 # set current VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION
0190 # force $TMPFILEs below to be in local directory: a slash character prevents
0191 # the dot command from using the search path.
0192 TMPFILE=`mktemp ./.tmpver.XXXXXX` || { echo "cannot make temp file" ; exit 1; }
0193 grep -E "^(VERSION|PATCHLEVEL|SUBLEVEL|EXTRAVERSION)" $sourcedir/Makefile > $TMPFILE
0194 tr -d [:blank:] < $TMPFILE > $TMPFILE.1
0195 . $TMPFILE.1
0196 rm -f $TMPFILE*
0197 if [ -z "$VERSION" -o -z "$PATCHLEVEL" -o -z "$SUBLEVEL" ]
0198 then
0199     echo "unable to determine current kernel version" >&2
0200     exit 1
0201 fi
0202 
0203 NAME=`grep ^NAME $sourcedir/Makefile`
0204 NAME=${NAME##*=}
0205 
0206 echo "Current kernel version is $VERSION.$PATCHLEVEL.$SUBLEVEL${EXTRAVERSION} ($NAME)"
0207 
0208 # strip EXTRAVERSION to just a number (drop leading '.' and trailing additions)
0209 EXTRAVER=
0210 if [ x$EXTRAVERSION != "x" ]
0211 then
0212         EXTRAVER=${EXTRAVERSION#.}
0213         EXTRAVER=${EXTRAVER%%[[:punct:]]*}
0214         #echo "$PNAME: changing EXTRAVERSION from $EXTRAVERSION to $EXTRAVER"
0215 fi
0216 
0217 #echo "stopvers=$stopvers"
0218 if [ $stopvers != "default" ]; then
0219         STOPSUBLEVEL=`echo $stopvers | cut -d. -f3`
0220         STOPEXTRA=`echo $stopvers | cut -d. -f4`
0221         STOPFULLVERSION=${stopvers%%.$STOPEXTRA}
0222         #echo "#___STOPSUBLEVEL=/$STOPSUBLEVEL/, STOPEXTRA=/$STOPEXTRA/"
0223 else
0224         STOPSUBLEVEL=9999
0225         STOPEXTRA=9999
0226 fi
0227 
0228 # This all assumes a 2.6.x[.y] kernel tree.
0229 # Don't allow backwards/reverse patching.
0230 if [ $STOPSUBLEVEL -lt $SUBLEVEL ]; then
0231         backwards
0232 fi
0233 
0234 if [ x$EXTRAVER != "x" ]; then
0235         CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$EXTRAVER"
0236 else
0237         CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
0238 fi
0239 
0240 if [ x$EXTRAVER != "x" ]; then
0241         echo "backing up to: $VERSION.$PATCHLEVEL.$SUBLEVEL"
0242         patch="patch-${CURRENTFULLVERSION}"
0243         findFile $patchdir/${patch} || noFile ${patch}
0244         reversePatch ${patch} || exit 1
0245 fi
0246 
0247 # now current is 2.6.x, with no EXTRA applied,
0248 # so update to target SUBLEVEL (2.6.SUBLEVEL)
0249 # and then to target EXTRAVER (2.6.SUB.EXTRAVER) if requested.
0250 # If not ending sublevel is specified, it is incremented until
0251 # no further sublevels are found.
0252 
0253 if [ $STOPSUBLEVEL -gt $SUBLEVEL ]; then
0254 while :                         # incrementing SUBLEVEL (s in v.p.s)
0255 do
0256     CURRENTFULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
0257     EXTRAVER=
0258     if [ x$STOPFULLVERSION = x$CURRENTFULLVERSION ]; then
0259         echo "Stopping at $CURRENTFULLVERSION base as requested."
0260         break
0261     fi
0262 
0263     SUBLEVEL=$(($SUBLEVEL + 1))
0264     FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL"
0265     #echo "#___ trying $FULLVERSION ___"
0266 
0267     if [ $(($SUBLEVEL)) -gt $(($STOPSUBLEVEL)) ]; then
0268         echo "Stopping since sublevel ($SUBLEVEL) is beyond stop-sublevel ($STOPSUBLEVEL)"
0269         exit 1
0270     fi
0271 
0272     patch=patch-$FULLVERSION
0273     # See if the file exists and find extension
0274     findFile $patchdir/${patch} || noFile ${patch}
0275 
0276     # Apply the patch and check all is OK
0277     applyPatch $patch || break
0278 done
0279 #echo "#___sublevel all done"
0280 fi
0281 
0282 # There is no incremental searching for extraversion...
0283 if [ "$STOPEXTRA" != "" ]; then
0284 while :                         # just to allow break
0285 do
0286 # apply STOPEXTRA directly (not incrementally) (x in v.p.s.x)
0287         FULLVERSION="$VERSION.$PATCHLEVEL.$SUBLEVEL.$STOPEXTRA"
0288         #echo "#... trying $FULLVERSION ..."
0289         patch=patch-$FULLVERSION
0290 
0291         # See if the file exists and find extension
0292         findFile $patchdir/${patch} || noFile ${patch}
0293 
0294         # Apply the patch and check all is OK
0295         applyPatch $patch || break
0296         #echo "#___extraver all done"
0297         break
0298 done
0299 fi
0300 
0301 if [ x$gotac != x ]; then
0302   # Out great user wants the -ac patches
0303         # They could have done -ac (get latest) or -acxx where xx=version they want
0304         if [ $gotac = "-ac" ]; then
0305           # They want the latest version
0306                 HIGHESTPATCH=0
0307                 for PATCHNAMES in $patchdir/patch-${CURRENTFULLVERSION}-ac*\.*
0308                 do
0309                         ACVALUE=`echo $PATCHNAMES | sed -e 's/^.*patch-[0-9.]*-ac\([0-9]*\).*/\1/'`
0310                         # Check it is actually a recognised patch type
0311                         findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${ACVALUE} || break
0312 
0313                   if [ $ACVALUE -gt $HIGHESTPATCH ]; then
0314                           HIGHESTPATCH=$ACVALUE
0315                   fi
0316                 done
0317 
0318                 if [ $HIGHESTPATCH -ne 0 ]; then
0319                         findFile $patchdir/patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH} || break
0320                         applyPatch patch-${CURRENTFULLVERSION}-ac${HIGHESTPATCH}
0321                 else
0322                   echo "No -ac patches found"
0323                 fi
0324         else
0325           # They want an exact version
0326                 findFile $patchdir/patch-${CURRENTFULLVERSION}${gotac} || {
0327                   echo "Sorry, I couldn't find the $gotac patch for $CURRENTFULLVERSION.  Hohum."
0328                         exit 1
0329                 }
0330                 applyPatch patch-${CURRENTFULLVERSION}${gotac}
0331         fi
0332 fi