Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Build Linux 4.9-rc8 with Allwinner H3 EMAC
#!/usr/bin/env bash
# build kernel 4.9-rc8 with sun8i-emac for Allwinner H3
# Copyright (C) 2016 Hal Martin
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
KERNEL_VER="linux-4.9-rc8"
KERNEL_SRC="https://cdn.kernel.org/pub/linux/kernel/v4.x/testing/$KERNEL_VER.tar.xz"
KERNEL_SHA1="ce27213b1028074385eff54e0bb981fe93cb7303"
CONF="linux-4.9rc8_config"
CONF_GZ="$CONF".gz
CONF_SRC="https://watchmysys.com/blog/wp-content/uploads/2016/12/$CONF_GZ"
CONF_SHA1="87e7da01f889879e27bb890707f10825220d600c"
PATCH_GZ="patches.tar.gz"
PATCH_SRC="https://watchmysys.com/blog/wp-content/uploads/2016/12/$PATCH_GZ"
PATCH_DIR="patches"
PATCH_SHA1="9d085f7329b6b8fd35c534c5f3a8392124f46e16"
CPUS="$(cat /proc/cpuinfo | grep processor | wc -l)"
function fetch_kernel {
if [ ! -f "$KERNEL_VER".tar.xz ]; then
wget "$KERNEL_SRC"
if [ $? -ne 0 ]; then
echo "Error downloading kernel source"
exit 1
fi
fi
if [ "$(sha1sum "$KERNEL_VER".tar.xz | awk '{print $1}')" != "$KERNEL_SHA1" ]; then
echo Kernel sha1sum does not match, please check the integrity of "$KERNEL_VER".tar.xz
exit 1
else
echo "Kernel source already downloaded"
fi
if [ ! -f "$CONF_GZ" ]; then
wget "$CONF_SRC"
if [ $? -ne 0 ]; then
echo "Error downloading kernel configuration"
exit 1
fi
fi
if [ "$(sha1sum $CONF_GZ | awk '{print $1}')" != "$CONF_SHA1" ]; then
echo "Kernel config sha1sum does not match"
exit 1
else
echo "Kernel config already downloaded"
fi
if [ ! -f "$PATCH_GZ" ]; then
wget "$PATCH_SRC"
if [ $? -ne 0 ]; then
echo "Error downloading kernel patches"
exit 1
fi
fi
if [ "$(sha1sum $PATCH_GZ | awk '{print $1}')" != "$PATCH_SHA1" ]; then
echo "Patch archive sha1sum does not match"
exit 1
else
echo "Patches already downloaded"
fi
}
function prepare_kernel {
if [ -d "$KERNEL_VER" ]; then
echo "Kernel source already extracted"
else
echo "Extracting kernel source to $(pwd)/$KERNEL_VER"
tar -Jxf "$KERNEL_VER".tar.xz
fi
if [ ! -d "$PATCH_DIR" ]; then
tar -zxf "$PATCH_GZ"
else
echo "Patches already extracted"
fi
gunzip -f -k "$CONF_GZ"
}
function patch_kernel {
for i in {01..10}; do
patch --forward -d "$KERNEL_VER" -p1 -i "$(pwd)"/"$PATCH_DIR"/"$i"_10.patch
if [ $? -ne 0 ]; then
echo Failed to apply patch "$i"_10.patch
fi
done
}
function build_kernel {
if [ ! -f "$KERNEL_VER"/.config ]; then
cp "$CONF" "$KERNEL_VER"/.config
pushd "$(pwd)"/"$KERNEL_VER"
make ARCH=arm CROSS_COMPILE=arm-none-eabi- olddefconfig
if [ $? -ne 0 ]; then
echo "Error configuring kernel"
exit 1
fi
else
echo "Kernel already configured"
pushd "$(pwd)"/"$KERNEL_VER"
fi
make ARCH=arm CROSS_COMPILE=arm-none-eabi- -j"$CPUS"
if [ $? -ne 0 ]; then
echo "Error building kernel"
exit 1
fi
make ARCH=arm CROSS_COMPILE=arm-none-eabi- uImage LOADADDR=0x40008000
if [ $? -ne 0 ]; then
echo "Error building kernel uImage"
exit 1
fi
popd
}
function install_kernel {
TMPDIR=$(mktemp -d -p "$(pwd)")
pushd "$(pwd)"/"$KERNEL_VER"
make ARCH=arm CROSS_COMPILE=arm-none-eabi- INSTALL_MOD_PATH="$TMPDIR" modules_install
cp arch/arm/boot/uImage "$TMPDIR"
popd
echo "====================================="
echo "Kernel image and modules installed to: $TMPDIR"
echo "====================================="
}
fetch_kernel
prepare_kernel
patch_kernel
build_kernel
install_kernel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment