forked from hegdi/libldacdec
-
Notifications
You must be signed in to change notification settings - Fork 7
/
patch-kernel.sh
executable file
·226 lines (188 loc) · 5.67 KB
/
patch-kernel.sh
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
MORE=1
INSTALL=0
RUN_MENUCONFIG=0
WORKDIR="$HOME/bt-kernel-patch"
if [[ "$@" == *"--install"* ]]; then
INSTALL=1
fi
if [[ "$@" == *"--configure"* ]]; then
RUN_MENUCONFIG=1
fi
defconfig=""
generic_defconfig=0
is_pi=0
if ! which apt &> /dev/null; then
echo " - Error: This script works only with Debian-based distros, at least for now." 1>&2
exit 1
fi
if ! grep -q "^deb-src" /etc/apt/sources.list /etc/apt/sources.list.d/*; then
echo " - Error: Source repositories are disabled or not configured in your /etc/apt/sources.list. You need to enable them for this script to work." 1>&2
exit 1
fi
if [[ $MORE > 1 ]]; then
echo " - Warning: You set \$MORE to $MORE, which is greater than 1, and this is unsafe. I recommend you to set it to 0 or 1." 1>&2
fi
JOBS=$(($(nproc --all) + $MORE))
abort() {
echo " - Aborting." 1>&2
exit 2
}
run() {
echo " - Running $*" 1>&2
$*
}
copy_with_backup() {
source="$1"
dest="$2"
if [[ $3 == 1 ]]; then
maybe_sudo="sudo"
else
maybe_sudo=""
fi
if [[ ! -e "$source" ]]; then
return 1
fi
if [ -e "$dest" ]; then
if [ -e "$dest.old" ]; then
counter=1
while [ -e "$dest.old.$counter" ]; do
counter=$((counter + 1))
done
$maybe_sudo mv "$dest" "$dest.old.$counter"
else
$maybe_sudo mv "$dest" "$dest.old"
fi
fi
$maybe_sudo cp "$source" "$dest"
}
if [ -f /proc/device-tree/model ]; then
read -r -d '' model < /proc/device-tree/model
case $model in
"Raspberry Pi 4"*)
defconfig="bcm2711"
is_pi=1
;;
"Raspberry Pi 3"*)
defconfig="bcmrpi3"
is_pi=1
;;
"Raspberry Pi 2"*)
defconfig="bcm2709"
is_pi=1
;;
"Raspberry Pi Zero"* | "Raspberry Pi 1"*)
defconfig="bcmrpi"
is_pi=1
;;
*)
if [ -f "/proc/cpuinfo" ]; then
tempconfig=$(cat /proc/cpuinfo | grep "Hardware" | awk '{print tolower($3)}')
if [ -n $tempconfig ]; then
defconfig=$tempconfig
else
defconfig=$(uname -m)
generic_defconfig=1
fi
else
defconfig=$(uname -m)
generic_defconfig=1
fi
;;
esac
else
defconfig=$(uname -m)
generic_defconfig=1
fi
if [ -e $WORKDIR ]; then
rm -r $WORKDIR
fi
mkdir $WORKDIR
cd $WORKDIR
echo " - Preparing the system to compile the kernel..."
sudo apt update
sudo apt install build-essential libncurses-dev bison flex libssl-dev xz-utils libelf-dev fakeroot patch || abort
MAKE="make -j$JOBS"
echo " - Downloading the kernel source package..."
if [[ $is_pi == 1 ]]; then
apt source raspberrypi-firmware || abort
cd raspberrypi-firmware-*/linux
else
apt source linux-source || abort
cd linux-*
fi
if [[ $generic_defconfig == 1 ]]; then
echo " - Warning: Couldn't get information for defconfig. Falling back to the generic defconfig." 1>&2
fi
echo " - Defconfig: $defconfig"
PATCH=$(cat <<EOF
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index eebe25610..64db1db3f 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1825,7 +1825,7 @@ static void l2cap_sock_init(struct sock *sk, struct sock *parent)
break;
}
- chan->imtu = L2CAP_DEFAULT_MTU;
+ chan->imtu = 0;
chan->omtu = 0;
if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
chan->mode = L2CAP_MODE_ERTM;
EOF
)
if echo $PATCH | patch -sfp0 --ignore-whitespace --dry-run net/bluetooth/l2cap_sock.c &>/dev/null; then
if echo $PATCH | patch -s --ignore-whitespace net/bluetooth/l2cap_sock.c &>/dev/null; then
echo " - Applied patch to net/bluetooth/l2cap_sock.c."
else
echo " - Could not apply patch to net/bluetooth/l2cap_sock.c. Aborting." 1>&2
exit 1
fi
else
echo " - Patch already applied."
fi
run $MAKE clean
run $MAKE mrproper
run $MAKE ${defconfig}_defconfig || run $MAKE defconfig
if [[ $RUN_MENUCONFIG == 1 ]]; then
run $MAKE menuconfig
fi
run $MAKE || abort
if [[ $is_pi == 1 ]]; then
cd ..
run fakeroot debian/rules -j$JOBS binary || abort
cd -
fi
if [[ -e "/boot/kernel8.img" ]]; then
kn=8
else if [[ -e "/boot/kernel7.img" ]]; then
kn=7
fi
case $(uname -m) in
aarch64) arch="arm64" ;;
armv7l) arch="arm" ;;
i386|x86_64|amd64) arch="x86" ;;
*) arch="<INSERT_ARCH_HERE>" ;;
esac
if [[ $INSTALL == 1 ]]; then
if [[ $is_pi == 1 ]]; then
run sudo dpkg -i ../../*.deb || abort
if [[ arch == "<INSERT_ARCH_HERE>" ]]; then
echo " - Error: Could not detect the architecture ($(uname -m)) - you have to copy the image to /boot yourself." 1>&2
exit 1
fi
echo -n " - Copying the Linux image to /boot..."
copy_with_backup arch/$arch/boot/Image /boot/kernel$kn.img 1 || abort
echo "done.\n - To restore the original kernel, copy /boot/kernel$kn.img.old back to /boot/kernel$kn.img"
else
run sudo $MAKE install || abort
fi
echo " - Done, the patched kernel has been successfully installed!"
echo " - Reboot to run the new kernel."
else
echo -n " - Successfully built the patched kernel. To install it, just "
if [[ $is_pi == 1 ]]; then
echo "copy $(readlink -f arch/$arch/boot/Image) to /boot/kernel$kn.img."
else
echo "run \"sudo $MAKE install\" inside the directory $WORKDIR."
fi
fi