patch-kernel 9.9 KB

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