Skip to content

Instantly share code, notes, and snippets.

@sihorton
Created September 2, 2018 12:32
Show Gist options
  • Save sihorton/56b64cde87eb50e5bbb582eadd43ac8d to your computer and use it in GitHub Desktop.
Save sihorton/56b64cde87eb50e5bbb582eadd43ac8d to your computer and use it in GitHub Desktop.
upload from odroid
#!/bin/bash
if [ "$1" = "" ]; then
echo "$0: please specify $0 (kernelv) (optional kernel-config)"
echo "e.g. $0 4.18.5 or $0 4.18.5 u3_docker_config"
else
CONF="compile-kernel.config"
#source config
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
. $DIR/$CONF
if [ "$1" != "" ]; then KERNELV="$1"; fi
if [ "$2" != "" ]; then KERNEL_CONFIG="$2"; fi
red=$'\e[1;31m'
grn=$'\e[1;32m'
yellow=$'\e[33m'
blu=$'\e[1;34m'
mag=$'\e[1;35m'
cyn=$'\e[1;36m'
white=$'\e[0m'
#function to output command with time and then run it
STEP=0
docmd() {
let "STEP=STEP+1"
echo "$blu`date +"%T"` ${STEP}) $grn$1$white";
`$1` || doexit
}
dotext() {
let "STEP=STEP+1"
echo "$blu`date +"%T"` ${STEP}) $grn$1$white";
}
doexit() {
echo "${red}Error at step ${STEP}$white"
exit 1
}
echo ""
echo "${yellow}`basename $0`${white} will use the following configuration from ${yellow}$CONF$white:"
echo " board_driver $grn${BOARD_DRIVER}$white"
echo " board_kernel $grn${KERNELV}$white"
echo " kernel_config $grn${KERNEL_CONFIG}$white"
echo " target_dir $grn${TARGETD}$white"
echo " temp_dir $grn${SRCD}/kernel-${KERNELV}$white"
echo ""
echo "script will:"
echo " ${blu}1)$white git clone kernel $grn${KERNELV}$white source code into $grn${SRCD}/kernel-${KERNELV}$white if it does not exist"
echo " ${blu}2)$white make olddefconfig using $grn${KERNEL_CONFIG}$white"
echo " ${blu}3)$white prepare kernel modules"
echo " ${blu}4)$white make kernel"
echo " ${blu}5)$white make kernel modules"
echo " ${blu}6)$white copy $grn${BOARD_DRIVER}_${KERNELV}.dtb$white,$grn uInitrd-${KERNELV}$white,$grn config-${KERNELV} ${white}to $grn${TARGETD}$white"
echo " ${blu}7)$white add setenv line for new kernel boot and update $grn${TARGETD}boot.ini$white to boot into new kernel"
echo " ${blu}8)$white make $grn${TARGETD}boot.src$white"
echo "This takes around ${yellow}1 hr$white on the odroid"
MESS=`echo "Do you want to continue [Y/n] " `;
read -r -p "${MESS}" response;if [ -z "$response" ]; then response="y";fi
case "$response" in
[yY][eE][sS]|[yY])
#check if the source code does not exist
if [ ! -d "${SRCD}/kernel-${KERNELV}" ]; then
#git clone from kernel repository just the branch we are interested in
docmd "git clone --depth 1 --single-branch --branch v${KERNELV} git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git ${SRCD}/kernel-${KERNELV}"
else
dotext "${SRCD}/kernel-${KERNELV} exists"
fi
#move to kernel directory
cp "${KERNEL_CONFIG}" "${SRCD}/kernel-${KERNELV}/.config"
cd ${SRCD}/kernel-${KERNELV}
#make the config from provided file
dotext "make olddefconfig"
make olddefconfig
#prepare modules
dotext "prepare modules_prepare"
make prepare modules_prepare
#big compilation step, takes 1 hr on odroid
dotext "make -j4 bzImage modules dtbs"
make -j4 bzImage modules dtbs
#generate the modules
dotext "make modules_install"
make modules_install
RELEASE=`cat include/config/kernel.release`
echo "compiled $grn${RELEASE}$white, generating boot files"
#copy output files into boot partition
cp arch/arm/boot/dts/${BOARD_DRIVER}.dtb ${TARGETD}/${BOARD_DRIVER}_${KERNELV}.dtb
echo "copied $grn${BOARD_DRIVER}_${KERNELV}.dtb$white to ${TARGETD}"
cp arch/arm/boot/zImage ${TARGETD}/zImage_${KERNELV}
echo "copied ${grn}zImage_${KERNELV}$white to ${TARGETD}"
#cp .config /boot/config-${RELEASE}
cp .config ${TARGETD}/config-${KERNELV}
dotext "update-initramfs -c -k ${RELEASE}"
update-initramfs -c -k ${RELEASE}
dotext "mkimage -A arm -O linux -T ramdisk -C none -a 0 -e 0 -n uInitrd -d /boot/initrd.img-${RELEASE} /boot/uInitrd-${KERNELV}"
mkimage -A arm -O linux -T ramdisk -C none -a 0 -e 0 -n uInitrd -d /boot/initrd.img-${RELEASE} /boot/uInitrd-${KERNELV}
cp /boot/uInitrd-${KERNELV} ${TARGETD}/
echo "copied ${grn}uInitrd-${KERNELV}$white to ${TARGETD}"
echo "adding setenv line for $grn${KERNELV}$white and setting new kernel to boot in ${TARGETD}boot.ini"
echo "."
echo "."
#add new board_kernel into boot.ini
sed -i -e '/setenv board_kernel/c\\'"setenv board_kernel \"${KERNELV}\"\nif test \"\${board_kernel}\" = \"${KERNELV}\"; then setenv bootcmd \"fatload mmc 0:1 0x40008000 zImage_${KERNELV}; fatload mmc 0:1 0x42000000 uInitrd-${KERNELV}; fatload mmc 0:1 0x44000000 ${BOARD_DRIVER}_${KERNELV}.dtb; bootz 0x40008000 0x42000000 0x44000000\"; fi" ${TARGETD}/boot.ini
#remove duplicate lines in file (if built same kernel more than once)
gawk -i inplace '!seen[$0]++' /media/boot/boot.ini
#show to the user
cat ${TARGETD}/boot.ini
echo "."
echo "."
dotext "mkimage -C none -A arm -T script -d ${TARGETD}/boot.ini ${TARGETD}boot.scr"
mkimage -C none -A arm -T script -d ${TARGETD}/boot.ini ${TARGETD}boot.scr
echo "made ${grn}${TARGETD}boot.src$white"
sync
echo "to delete kernel directory '${yellow}rm -r ${SRCD}/kernel-${KERNELV}$white'"
echo "reboot now"
;;*)
echo "${yellow}script cancelled$white";;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment