summaryrefslogtreecommitdiffstats
path: root/update_kernel.sh
blob: a74f91d749e56767efede5dcd9bc2cd265171a47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/sh
#
# update_kernel.sh: (c) 2017 Jonas Gorski <jonas.gorski@gmail.com>
# Licensed under the terms of the GNU GPL License version 2

BUILD=0
BUILD_ARGS=
VERBOSE=w
TEST=0
UPDATE=0

KERNEL=
PATCHVER=

while [ $# -gt 0 ]; do
	case $1 in
		-b|--build)
			BUILD=1
			shift
			BUILD_ARGS=$1
			;;
		-q|--quiet)
			VERBOSE=
			;;
		-t|--test)
			TEST=1
			;;
		-u|--update)
			UPDATE=1
			;;
		-v|--verbose)
			VERBOSE=ws
			;;
		[1-9]*)
			if [ -z "$KERNEL" ]; then
				KERNEL=$1
			elif [ -z "$PATCHVER" ]; then
				PATCHVER=$1
			else
				exit 1
			fi
			;;
		*)
			break
			;;

	esac

	shift
done

if [ -z "$KERNEL" ]; then
	echo "usage: $0 [<options>...] <patchver> [<version>]"
	echo "example: $0 3.18 3.18.30"
	echo "If <version> is not given, it will try to find out the latest from kernel.org"
	echo ""
	echo "valid options:"
	echo "-b|--build <args> also do a test build with <args> as extra arguments (e.g. -j 3)"
	echo "-q|--quiet        less output"
	echo "-t|--test         don't do anything, just print what it would do"
	echo "-u|--update       update include/kernel-version.mk after a successful run"
	echo "-v|--verbose      more output (pass V=ws to all commands)"
	exit 1
fi

if [ -z "$PATCHVER" ]; then
	if [ -n "$(which curl)" ]; then
		DL_CMD="curl -s "
	fi

	if [ -n "$(which wget)" ]; then
		DL_CMD="wget -O - -q "
	fi

	if [ -z "$DL_CMD" ]; then
		echo "Failed to find a suitable download program. Please install either curl or wget." >&2
		exit 1
	fi

	# https://www.kernel.org/feeds/kdist.xml
	# $(curl -s https://www.kernel.org/feeds/kdist.xml | sed -ne 's|^.*"html_url": "\(.*/commit/.*\)",|\1.patch|p')
	# curl -s "https://www.kernel.org/feeds/kdist.xml"
	CURR_VERS=$($DL_CMD "https://www.kernel.org/feeds/kdist.xml" | sed -ne 's|^.*title>\([1-9][^\w]*\): .*|\1|p')

	for ver in $CURR_VERS; do
		case $ver in
			"$KERNEL"*)
				PATCHVER=$ver
				;;
		esac

		if [ -n "$PATCHVER" ]; then
			break
		fi
	done

	if [ -z "$PATCHVER" ]; then
		echo "Failed to find the latest release on kernel.org, please specify the release manually" >&2
		exit 1
	fi
fi

echo "Refreshing Kernel $KERNEL to release $PATCHVER ..."

targets=$(ls -b target/linux)

if [ "$TEST" -eq 1 ]; then
	CMD="echo"
fi

for target in $targets; do
	if [ "$target" = "generic" -o -f "$target" ]; then
		continue
	fi

	grep -q "broken" target/linux/$target/Makefile && { \
		echo "Skipping $target (broken)"
		continue
	}

	if [ -e tmp/${target}_${PATCHVER}_done ]; then
		continue
	fi

	grep -q "${PATCHVER}" target/linux/$target/Makefile || \
	[ -f target/linux/$target/config-${KERNEL} ] || \
	[ -d target/linux/$target/patches-${KERNEL} ] && {
		echo "refreshing $target ..."
		$CMD echo "CONFIG_TARGET_$target=y" > .config || exit 1
		$CMD echo "CONFIG_ALL_KMODS=y" >> .config || exit 1
		$CMD make defconfig KERNEL_PATCHVER=${KERNEL} || exit 1
		if [ ! -f tmp/${target}_${PATCHVER}_refreshed ]; then
			$CMD make target/linux/refresh V=$VERBOSE KERNEL_PATCHVER=${KERNEL} LINUX_VERSION=${PATCHVER} LINUX_KERNEL_HASH=skip || exit 1
			$CMD make target/linux/prepare V=$VERBOSE KERNEL_PATCHVER=${KERNEL} LINUX_VERSION=${PATCHVER} || exit 1
			$CMD touch tmp/${target}_${PATCHVER}_refreshed
		fi
		if [ "$BUILD" = "1" ]; then
			echo "building $target ... "
			$CMD make V=$VERBOSE KERNEL_PATCHVER=${KERNEL} LINUX_VERSION=${PATCHVER} $BUILD_ARGS || exit 1
		fi
		$CMD make target/linux/clean
		$CMD touch tmp/${target}_${PATCHVER}_done
	} || {
		echo "skipping $target (no support for $KERNEL)"
	}
done

if [ "$UPDATE" -eq 1 ]; then
	NEWVER=${PATCHVER#$KERNEL}
	if [ "$TEST" -eq 1 ]; then
		echo ./staging_dir/host/bin/mkhash sha256 dl/linux-$PATCHVER.tar.xz
	fi

	if [ -f dl/linux-$PATCHVER.tar.xz ]; then
		CHECKSUM=$(./staging_dir/host/bin/mkhash sha256 dl/linux-$PATCHVER.tar.xz)
	fi

	if [ -f include/kernel-${KERNEL} ]; then
		# split version files
		KERNEL_VERSION_FILE=include/kernel-${KERNEL}
	else
		# unified version file
		KERNEL_VERSION_FILE=include/kernel-version.mk
	fi

	$CMD ./staging_dir/host/bin/sed -i ${KERNEL_VERSION_FILE} \
		-e "s|LINUX_VERSION-${KERNEL} =.*|LINUX_VERSION-${KERNEL} = ${NEWVER}|" \
		-e "s|LINUX_KERNEL_HASH-${KERNEL}.*|LINUX_KERNEL_HASH-${PATCHVER} = ${CHECKSUM}|"
fi