github-merge-pr: fix loading .config if symbolic link is used
[maintainer-tools.git] / update_kernel.sh
1 #!/bin/sh
2 #
3 # update_kernel.sh: (c) 2017 Jonas Gorski <jonas.gorski@gmail.com>
4 # Licensed under the terms of the GNU GPL License version 2
5
6 BUILD=0
7 BUILD_ARGS=
8 VERBOSE=w
9 TEST=0
10 UPDATE=0
11
12 KERNEL=
13 PATCHVER=
14
15 while [ $# -gt 0 ]; do
16 case $1 in
17 -b|--build)
18 BUILD=1
19 shift
20 BUILD_ARGS=$1
21 ;;
22 -q|--quiet)
23 VERBOSE=
24 ;;
25 -t|--test)
26 TEST=1
27 ;;
28 -u|--update)
29 UPDATE=1
30 ;;
31 -v|--verbose)
32 VERBOSE=ws
33 ;;
34 [1-9]*)
35 if [ -z "$KERNEL" ]; then
36 KERNEL=$1
37 elif [ -z "$PATCHVER" ]; then
38 PATCHVER=$1
39 else
40 exit 1
41 fi
42 ;;
43 *)
44 break
45 ;;
46
47 esac
48
49 shift
50 done
51
52 if [ -z "$KERNEL" ]; then
53 echo "usage: $0 [<options>...] <patchver> [<version>]"
54 echo "example: $0 3.18 3.18.30"
55 echo "If <version> is not given, it will try to find out the latest from kernel.org"
56 echo ""
57 echo "valid options:"
58 echo "-b|--build <args> also do a test build with <args> as extra arguments (e.g. -j 3)"
59 echo "-q|--quiet less output"
60 echo "-t|--test don't do anything, just print what it would do"
61 echo "-u|--update update include/kernel-version.mk after a successful run"
62 echo "-v|--verbose more output (pass V=ws to all commands)"
63 exit 1
64 fi
65
66 if [ -z "$PATCHVER" ]; then
67 if [ -n "$(which curl)" ]; then
68 DL_CMD="curl -s "
69 fi
70
71 if [ -n "$(which wget)" ]; then
72 DL_CMD="wget -O - -q "
73 fi
74
75 if [ -z "$DL_CMD" ]; then
76 echo "Failed to find a suitable download program. Please install either curl or wget." >&2
77 exit 1
78 fi
79
80 # https://www.kernel.org/feeds/kdist.xml
81 # $(curl -s https://www.kernel.org/feeds/kdist.xml | sed -ne 's|^.*"html_url": "\(.*/commit/.*\)",|\1.patch|p')
82 # curl -s "https://www.kernel.org/feeds/kdist.xml"
83 CURR_VERS=$($DL_CMD "https://www.kernel.org/feeds/kdist.xml" | sed -ne 's|^.*title>\([1-9][^\w]*\): .*|\1|p')
84
85 for ver in $CURR_VERS; do
86 case $ver in
87 "$KERNEL"*)
88 PATCHVER=$ver
89 ;;
90 esac
91
92 if [ -n "$PATCHVER" ]; then
93 break
94 fi
95 done
96
97 if [ -z "$PATCHVER" ]; then
98 echo "Failed to find the latest release on kernel.org, please specify the release manually" >&2
99 exit 1
100 fi
101 fi
102
103 echo "Refreshing Kernel $KERNEL to release $PATCHVER ..."
104
105 targets=$(ls -b target/linux)
106
107 if [ "$TEST" -eq 1 ]; then
108 CMD="echo"
109 fi
110
111 for target in $targets; do
112 if [ "$target" = "generic" -o -f "$target" ]; then
113 continue
114 fi
115
116 grep -q "broken" target/linux/$target/Makefile && { \
117 echo "Skipping $target (broken)"
118 continue
119 }
120
121 if [ -e tmp/${target}_${PATCHVER}_done ]; then
122 continue
123 fi
124
125 grep -q "${PATCHVER}" target/linux/$target/Makefile || \
126 [ -f target/linux/$target/config-${KERNEL} ] || \
127 [ -d target/linux/$target/patches-${KERNEL} ] && {
128 echo "refreshing $target ..."
129 $CMD echo "CONFIG_TARGET_$target=y" > .config || exit 1
130 $CMD echo "CONFIG_ALL_KMODS=y" >> .config || exit 1
131 $CMD make defconfig KERNEL_PATCHVER=${KERNEL} || exit 1
132 if [ ! -f tmp/${target}_${PATCHVER}_refreshed ]; then
133 $CMD make target/linux/refresh V=$VERBOSE KERNEL_PATCHVER=${KERNEL} LINUX_VERSION=${PATCHVER} LINUX_KERNEL_HASH=skip || exit 1
134 $CMD make target/linux/prepare V=$VERBOSE KERNEL_PATCHVER=${KERNEL} LINUX_VERSION=${PATCHVER} || exit 1
135 $CMD touch tmp/${target}_${PATCHVER}_refreshed
136 fi
137 if [ "$BUILD" = "1" ]; then
138 echo "building $target ... "
139 $CMD make V=$VERBOSE KERNEL_PATCHVER=${KERNEL} LINUX_VERSION=${PATCHVER} $BUILD_ARGS || exit 1
140 fi
141 $CMD make target/linux/clean
142 $CMD touch tmp/${target}_${PATCHVER}_done
143 } || {
144 echo "skipping $target (no support for $KERNEL)"
145 }
146 done
147
148 if [ "$UPDATE" -eq 1 ]; then
149 NEWVER=${PATCHVER#$KERNEL}
150 if [ "$TEST" -eq 1 ]; then
151 echo ./staging_dir/host/bin/mkhash sha256 dl/linux-$PATCHVER.tar.xz
152 fi
153
154 if [ -f dl/linux-$PATCHVER.tar.xz ]; then
155 CHECKSUM=$(./staging_dir/host/bin/mkhash sha256 dl/linux-$PATCHVER.tar.xz)
156 fi
157
158 if [ -f include/kernel-${KERNEL} ]; then
159 # split version files
160 KERNEL_VERSION_FILE=include/kernel-${KERNEL}
161 else
162 # unified version file
163 KERNEL_VERSION_FILE=include/kernel-version.mk
164 fi
165
166 $CMD ./staging_dir/host/bin/sed -i ${KERNEL_VERSION_FILE} \
167 -e "s|LINUX_VERSION-${KERNEL} =.*|LINUX_VERSION-${KERNEL} = ${NEWVER}|" \
168 -e "s|LINUX_KERNEL_HASH-${KERNEL}.*|LINUX_KERNEL_HASH-${PATCHVER} = ${CHECKSUM}|"
169 fi