small-package/luci-app-amlogic/root/usr/sbin/openwrt-kernel

253 lines
10 KiB
Plaintext
Raw Normal View History

2022-03-25 12:20:39 +08:00
#!/bin/bash
2022-05-27 20:20:55 +08:00
#====================================================================================
#
2022-03-25 12:20:39 +08:00
# Function: Update the kernel for OpenWrt (Amlogic s9xxx, Allwinner, Rockchip)
# Copyright (C) 2020-- https://github.com/unifreq/openwrt_packit
# Copyright (C) 2021-- https://github.com/ophub/luci-app-amlogic
2022-05-27 20:20:55 +08:00
#
# Support the kernel: boot-*.tar.gz, dtb-*.tar.gz, modules-*.tar.gz
# It is recommended to install MAINLINE_UBOOT for kernel versions above 5.10.y
2022-03-25 12:20:39 +08:00
# openwrt-kernel ${AUTO_MAINLINE_UBOOT}
# E.g: openwrt-kernel yes
# E.g: openwrt-kernel no
2022-05-27 20:20:55 +08:00
#
#================================== Functions list ==================================
#
# error_msg : Output error message
# get_textoffset : Get kernel TEXT_OFFSET
# init_var : Initialize all variables
# check_kernel : Check kernel files list
# update_kernel : Update the kernel
# update_uboot : Update the uboot
#
#============================== Set default parameters ==============================
#
2022-03-25 12:20:39 +08:00
# Receive one-key command related parameters
2022-05-27 20:20:55 +08:00
AUTO_MAINLINE_UBOOT="no"
# Set the release check file
release_file="/etc/flippy-openwrt-release"
# Current device model
MYDEVICE_NAME="$(cat /proc/device-tree/model | tr -d '\000')"
#
#====================================================================================
2022-03-25 12:20:39 +08:00
# Encountered a serious error, abort the script execution
error_msg() {
2022-05-27 20:20:55 +08:00
echo -e "[\033[1;91m Error \033[0m] ${1}"
2022-03-25 12:20:39 +08:00
exit 1
}
2022-05-27 20:20:55 +08:00
# Get kernel TEXT_OFFSET, For u-boot.ext and u-boot.emmc
get_textoffset() {
2022-04-20 23:46:41 +08:00
boot_tgz_file="${1}"
vmlinuz_name="${2}"
K510="1"
temp_dir="$(mktemp -d)"
2022-04-15 18:42:57 +08:00
(
2022-04-20 23:46:41 +08:00
cd ${temp_dir}
2022-05-27 20:20:55 +08:00
tar -xf "${boot_tgz_file}" "${vmlinuz_name}"
2022-04-15 18:42:57 +08:00
)
2022-04-20 23:46:41 +08:00
# With TEXT_OFFSET patch is [ 0108 ], without TEXT_OFFSET patch is [ 0000 ]
[[ "$(hexdump -n 15 -x "${temp_dir}/${vmlinuz_name}" 2>/dev/null | head -n 1 | awk '{print $7}')" == "0108" ]] && K510="0"
2022-04-15 18:42:57 +08:00
}
2022-05-27 20:20:55 +08:00
init_var() {
# Receive one-key command related parameters
[[ "${1}" == "yes" ]] && AUTO_MAINLINE_UBOOT="yes"
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
# Check dependencies
[[ -n "$(which tar)" ]] || error_msg "Missing [ tar ] in OpenWrt firmware, unable to update kernel"
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
# Check release file
if [[ -s "${release_file}" ]]; then
source "${release_file}" 2>/dev/null
PLATFORM="${PLATFORM}"
UBOOT_OVERLOAD="${UBOOT_OVERLOAD}"
MAINLINE_UBOOT="${MAINLINE_UBOOT}"
ANDROID_UBOOT="${ANDROID_UBOOT}"
2022-03-25 12:20:39 +08:00
else
2022-05-27 20:20:55 +08:00
error_msg "${release_file} file is missing!"
2022-03-25 12:20:39 +08:00
fi
2022-05-27 20:20:55 +08:00
[[ -n "${PLATFORM}" ]] || error_msg "Missing ${PLATFORM} value in ${release_file} file."
# Set /boot/vmlinuz-* replication names for different SoCs
case "${PLATFORM}" in
allwinner) MYBOOT_VMLINUZ="zImage" ;;
rockchip) MYBOOT_VMLINUZ="Image" ;;
amlogic) MYBOOT_VMLINUZ="zImage" ;;
*) error_msg "Unknown device: [ ${MYDEVICE_NAME} ], Not supported." ;;
esac
# Find the partition where root is located
ROOT_PTNAME="$(df / | tail -n1 | awk '{print $1}' | awk -F '/' '{print $3}')"
[[ -n "${ROOT_PTNAME}" ]] || error_msg "Cannot find the partition corresponding to the root file system!"
# Find the disk where the partition is located, only supports mmcblk?p? sd?? hd?? vd?? and other formats
case "${ROOT_PTNAME}" in
mmcblk?p[1-4])
EMMC_NAME="$(echo ${ROOT_PTNAME} | awk '{print substr($1, 1, length($1)-2)}')"
PARTITION_NAME="p"
LB_PRE="EMMC_"
;;
[hsv]d[a-z][1-4])
EMMC_NAME="$(echo ${ROOT_PTNAME} | awk '{print substr($1, 1, length($1)-1)}')"
PARTITION_NAME=""
LB_PRE=""
;;
*)
error_msg "Unable to recognize the disk type of ${ROOT_PTNAME}!"
;;
esac
P4_PATH="/mnt/${EMMC_NAME}${PARTITION_NAME}4"
# Move kernel related files to the ${P4_PATH} directory
mv -f /tmp/upload/* ${P4_PATH} 2>/dev/null && sync
echo -e "Current device: ${MYDEVICE_NAME} [ ${PLATFORM} ], Use in [ ${EMMC_NAME} ]"
sync && echo ""
}
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
# Check kernel files list
check_kernel() {
cd ${P4_PATH}
# Determine custom kernel filename
kernel_boot="$(ls boot-*.tar.gz | head -n 1)"
kernel_name="${kernel_boot/boot-/}" && kernel_name="${kernel_name/.tar.gz/}"
KERNEL_VERSION="$(echo ${kernel_name} | grep -oE '^[1-9].[0-9]{1,3}.[0-9]+')"
echo -e "Kernel name: ${kernel_name}"
# Check if the file is added with TEXT_OFFSET patch
get_textoffset "${P4_PATH}/${kernel_boot}" "vmlinuz-${kernel_name}"
echo -e "K510 [ ${K510} ]"
if [[ "${PLATFORM}" == "amlogic" && "${K510}" -eq "1" ]]; then
[[ -n "${UBOOT_OVERLOAD}" && -f "/boot/${UBOOT_OVERLOAD}" ]] || error_msg "The UBOOT_OVERLOAD file is missing and cannot be update."
2022-03-25 12:20:39 +08:00
fi
2022-05-27 20:20:55 +08:00
# Check the sha256sums file
sha256sums_file="sha256sums"
sha256sums_check="1"
[[ -s "${sha256sums_file}" && -n "$(cat ${sha256sums_file})" ]] || sha256sums_check="0"
[[ -n "$(which sha256sum)" ]] || sha256sums_check="0"
[[ "${sha256sums_check}" -eq "1" ]] && echo -e "Enable sha256sum checking..."
# Loop check file
i="1"
kernel_list=("boot" "dtb-${PLATFORM}" "modules")
for kernel_file in ${kernel_list[*]}; do
# Set check filename
tmp_file="${kernel_file}-${kernel_name}.tar.gz"
# Check if file exists
[[ -s "${tmp_file}" ]] || error_msg "The [ ${kernel_file} ] file is missing."
# Check if the file sha256sum is correct
if [[ "${sha256sums_check}" -eq "1" ]]; then
tmp_sha256sum="$(sha256sum "${tmp_file}" | awk '{print $1}')"
tmp_checkcode="$(cat ${sha256sums_file} | grep ${tmp_file} | awk '{print $1}')"
[[ "${tmp_sha256sum}" == "${tmp_checkcode}" ]] || error_msg "${tmp_file}: sha256sum verification failed."
echo -e "(${i}/3) [ ${tmp_file} ] file sha256sum check same."
fi
let i++
done
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
sync && echo ""
}
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
# Update the kernel
update_kernel() {
echo -e "Start unpacking the kernel..."
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
# 01. for /boot five files
rm -f /boot/config-* /boot/initrd.img-* /boot/System.map-* /boot/uInitrd-* /boot/vmlinuz-* 2>/dev/null && sync
rm -f /boot/uInitrd /boot/zImage /boot/Image 2>/dev/null && sync
#
tar -xf ${P4_PATH}/boot-${kernel_name}.tar.gz -C /boot && sync
[[ "$(ls /boot/*${kernel_name}* -l 2>/dev/null | grep "^-" | wc -l)" -ge "4" ]] || error_msg "The /boot files is missing."
(
cd /boot && cp -f uInitrd-${kernel_name} uInitrd && cp -f vmlinuz-${kernel_name} ${MYBOOT_VMLINUZ} && sync
[[ "$(sha256sum uInitrd-${kernel_name} | awk '{print $1}')" == "$(sha256sum uInitrd | awk '{print $1}')" ]] || cp -f uInitrd-${kernel_name} uInitrd
[[ "$(sha256sum vmlinuz-${kernel_name} | awk '{print $1}')" == "$(sha256sum ${MYBOOT_VMLINUZ} | awk '{print $1}')" ]] || cp -f vmlinuz-${kernel_name} ${MYBOOT_VMLINUZ}
)
echo -e "(1/3) Unpacking [ boot-${kernel_name}.tar.gz ] done."
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
# 02. for /boot/dtb/${PLATFORM}/*
[[ -d "/boot/dtb/${PLATFORM}" ]] || mkdir -p /boot/dtb/${PLATFORM}
if [[ "${PLATFORM}" == "rockchip" ]]; then
mkdir -p /boot/dtb-${kernel_name}/${PLATFORM}
ln -sf /boot/dtb-${kernel_name} /boot/dtb
2022-03-25 12:20:39 +08:00
fi
2022-05-27 20:20:55 +08:00
tar -xf ${P4_PATH}/dtb-${PLATFORM}-${kernel_name}.tar.gz -C /boot/dtb/${PLATFORM} && sync
[ "$(ls /boot/dtb/${PLATFORM} -l 2>/dev/null | grep "^-" | wc -l)" -ge "1" ] || error_msg "/boot/dtb/${PLATFORM} file is missing."
echo -e "(2/3) Unpacking [ dtb-${PLATFORM}-${kernel_name}.tar.gz ] done."
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
# 03. for /lib/modules/*
rm -rf /lib/modules/* 2>/dev/null && sync
tar -xf ${P4_PATH}/modules-${kernel_name}.tar.gz -C /lib/modules && sync
(
cd /lib/modules/${kernel_name}
rm -f *.ko 2>/dev/null
find ./ -type f -name '*.ko' -exec ln -s {} ./ \;
sync && sleep 3
x=$(ls *.ko -l 2>/dev/null | grep "^l" | wc -l)
if [ "${x}" -eq "0" ]; then
error_msg "Error *.ko Files not found."
fi
)
echo -e "(3/3) Unpacking [ modules-${kernel_name}.tar.gz ] done."
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
# Delete kernel tmpfiles
rm -f ${P4_PATH}/*.tar.gz ${P4_PATH}/sha256sums 2>/dev/null
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
sync && echo ""
}
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
# Update the uboot
update_uboot() {
# Only amlogic SoCs needs to be updated
if [[ "${PLATFORM}" == "amlogic" ]]; then
# Copy u-boot.ext and u-boot.emmc
if [[ "${K510}" -eq "1" && -n "${UBOOT_OVERLOAD}" && -f "/boot/${UBOOT_OVERLOAD}" ]]; then
[[ ! -f "/boot/u-boot.ext" ]] && cp -f "/boot/${UBOOT_OVERLOAD}" /boot/u-boot.ext && sync && chmod +x /boot/u-boot.ext
[[ ! -f "/boot/u-boot.emmc" ]] && cp -f "/boot/u-boot.ext" /boot/u-boot.emmc && sync && chmod +x /boot/u-boot.emmc
echo -e "The ${UBOOT_OVERLOAD} file copy is complete."
elif [[ "${K510}" -eq "0" ]]; then
rm -f "/boot/u-boot.ext" "/boot/u-boot.emmc" 2>/dev/null
2022-03-25 12:20:39 +08:00
fi
2022-05-27 20:20:55 +08:00
# Write Mainline bootloader
if [[ -f "${MAINLINE_UBOOT}" && "${AUTO_MAINLINE_UBOOT}" == "yes" ]]; then
echo -e "Write Mainline bootloader: [ ${MAINLINE_UBOOT} ] to [ /dev/${EMMC_NAME} ]"
dd if=${MAINLINE_UBOOT} of=/dev/${EMMC_NAME} bs=1 count=442 conv=fsync
dd if=${MAINLINE_UBOOT} of=/dev/${EMMC_NAME} bs=512 skip=1 seek=1 conv=fsync
echo -e "The MAINLINE_UBOOT file write is complete."
2022-03-25 12:20:39 +08:00
fi
2022-05-27 20:20:55 +08:00
fi
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
# Update release file
sed -i '/KERNEL_VERSION/d' /etc/flippy-openwrt-release 2>/dev/null
echo "KERNEL_VERSION='${kernel_name}'" >>/etc/flippy-openwrt-release 2>/dev/null
#
sed -i '/K510/d' /etc/flippy-openwrt-release 2>/dev/null
echo "K510='${K510}'" >>/etc/flippy-openwrt-release 2>/dev/null
#
sed -i "s/ Kernel.*/ Kernel: ${kernel_name}/g" /etc/banner 2>/dev/null
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
sync && echo ""
}
2022-03-25 12:20:39 +08:00
2022-05-27 20:20:55 +08:00
echo -e "Start update the openwrt kernel."
# Operation environment check
[[ -x "/usr/sbin/openwrt-kernel" ]] || error_msg "Please grant execution permission: chmod +x /usr/sbin/openwrt-kernel"
#
# Initialize all variables
init_var "${@}"
# Check kernel files list
check_kernel
# Update the kernel
update_kernel
# Update the uboot
update_uboot
#
2022-03-25 12:20:39 +08:00
sync && sleep 3
echo "Successfully updated, automatic restarting..."
reboot
exit 0