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

363 lines
13 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
2022-12-06 20:18:48 +08:00
# 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
2022-12-06 20:18:48 +08:00
# chech_files_same : Check file consistency
# restore_kernel : Restore current kernel
2022-05-27 20:20:55 +08:00
# 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
2022-12-06 20:18:48 +08:00
source "${release_file}"
2022-05-27 20:20:55 +08:00
PLATFORM="${PLATFORM}"
UBOOT_OVERLOAD="${UBOOT_OVERLOAD}"
MAINLINE_UBOOT="${MAINLINE_UBOOT}"
ANDROID_UBOOT="${ANDROID_UBOOT}"
2022-09-06 20:23:01 +08:00
SOC="${SOC}"
LOCK_KERNEL="${LOCK_KERNEL}"
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
2022-09-06 20:23:01 +08:00
allwinner) MYBOOT_VMLINUZ="zImage" ;;
rockchip) MYBOOT_VMLINUZ="Image" ;;
amlogic) MYBOOT_VMLINUZ="zImage" ;;
2022-09-18 20:21:00 +08:00
qemu-aarch64)
MYBOOT_VMLINUZ="vmlinuz"
MYDEVICE_NAME="KVM Virtual Machine"
;;
2022-09-06 20:23:01 +08:00
*) error_msg "Unknown device: [ ${MYDEVICE_NAME} ], Not supported." ;;
2022-05-27 20:20:55 +08:00
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"
;;
[hsv]d[a-z][1-4])
EMMC_NAME="$(echo ${ROOT_PTNAME} | awk '{print substr($1, 1, length($1)-1)}')"
PARTITION_NAME=""
2022-10-20 20:27:30 +08:00
;;
nvme?n?p[1-4])
EMMC_NAME="$(echo ${ROOT_PTNAME} | awk '{print substr($1, 1, length($1)-2)}')"
PARTITION_NAME="p"
2022-05-27 20:20:55 +08:00
;;
*)
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
2022-12-06 20:18:48 +08:00
mv -f /tmp/upload/* ${P4_PATH} 2>/dev/null
2022-05-27 20:20:55 +08:00
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/}"
2022-09-05 17:12:47 +08:00
2022-05-27 20:20:55 +08:00
KERNEL_VERSION="$(echo ${kernel_name} | grep -oE '^[1-9].[0-9]{1,3}.[0-9]+')"
echo -e "Kernel name: ${kernel_name}"
2022-09-06 20:23:01 +08:00
# check if kernel is locked
2022-09-18 20:21:00 +08:00
if [ -n "${LOCK_KERNEL}" ]; then
if [ "${LOCK_KERNEL}" != "${kernel_name}" ]; then
if ! echo "${kernel_name}" | grep -E '^5.10.\d+-.*?rk3588.*?$' >/dev/null; then
error_msg "The kernel version is locked to [ ${LOCK_KERNEL} ], but your kernel is [ ${kernel_name} ]. "
fi
2022-09-06 20:23:01 +08:00
fi
fi
2022-05-27 20:20:55 +08:00
# 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"
2022-09-18 20:21:00 +08:00
if [[ "${PLATFORM}" == "qemu-aarch64" ]]; then
kernel_list=("boot" "modules")
else
kernel_list=("boot" "dtb-${PLATFORM}" "modules")
fi
2022-05-27 20:20:55 +08:00
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-12-06 20:18:48 +08:00
# Check the consistency of amlogic device files
chech_files_same() {
i="0"
max_try="5"
while [[ "${i}" -le "${max_try}" ]]; do
if [[ "$(sha256sum "${1}" | awk '{print $1}')" == "$(sha256sum "${2}" | awk '{print $1}')" ]]; then
echo "" && break
else
cp -f ${1} ${2}
i="$((i + 1))"
fi
done
[[ "${i}" -gt "${max_try}" ]] && echo "God, it's different after ${max_try} copies: [ ${1} ]"
}
# Restore the kernel when the update fails
restore_kernel() {
(
cd /boot
rm -rf \
config-${kernel_name} \
System.map-${kernel_name} \
initrd.img-${kernel_name} \
uInitrd-${kernel_name} \
vmlinuz-${kernel_name} \
initrd.img \
uInitrd \
zImage \
Image \
vmlinuz \
dtb* 2>/dev/null
tar -xzf /tmp/boot-backup.tar.gz 2>/dev/null
)
error_msg "Kernel update failed and has been reverted."
}
2022-05-27 20:20:55 +08:00
# Update the kernel
update_kernel() {
2022-09-18 20:21:00 +08:00
local cur_kernel_name=$(uname -r)
local boot_fstype=$(df -T /boot | tail -n1 | awk '{print $2}')
2022-05-27 20:20:55 +08:00
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
2022-12-06 20:18:48 +08:00
# Backup the current_kernel
2022-09-18 20:21:00 +08:00
(
cd /boot
tar -czf /tmp/boot-backup.tar.gz \
config-${cur_kernel_name} \
System.map-${cur_kernel_name} \
initrd.img-${cur_kernel_name} \
uInitrd-${cur_kernel_name} \
vmlinuz-${cur_kernel_name} \
initrd.img \
uInitrd \
zImage \
Image \
vmlinuz \
dtb* 2>/dev/null
rm -rf \
config-${cur_kernel_name} \
System.map-${cur_kernel_name} \
initrd.img-${cur_kernel_name} \
uInitrd-${cur_kernel_name} \
vmlinuz-${cur_kernel_name} \
initrd.img \
uInitrd \
zImage \
Image \
vmlinuz \
dtb* 2>/dev/null
)
2022-12-06 20:18:48 +08:00
# Extract the new kernel
tar -xf ${P4_PATH}/boot-${kernel_name}.tar.gz -C /boot
2022-09-18 20:21:00 +08:00
2022-12-06 20:18:48 +08:00
# Check if the file exists
2022-09-18 20:21:00 +08:00
local valid_files
if [[ "${PLATFORM}" == "qemu-aarch64" ]]; then
valid_files="vmlinuz-${kernel_name} initrd.img-${kernel_name} config-${kernel_name} System.map-${kernel_name}"
rm -f /boot/uInitrd*
else
valid_files="vmlinuz-${kernel_name} uInitrd-${kernel_name} config-${kernel_name} System.map-${kernel_name}"
rm -f /boot/initrd.img*
fi
2022-12-06 20:18:48 +08:00
for f in ${valid_files}; do [[ -f "/boot/${f}" ]] || restore_kernel; done
2022-09-18 20:21:00 +08:00
2022-12-06 20:18:48 +08:00
# Check if the files are the same
2022-05-27 20:20:55 +08:00
(
2022-09-18 20:21:00 +08:00
cd /boot
if [[ "${PLATFORM}" == "qemu-aarch64" ]]; then
2022-12-06 20:18:48 +08:00
ln -sf initrd.img-${kernel_name} initrd.img
ln -sf vmlinuz-${kernel_name} ${MYBOOT_VMLINUZ}
2022-09-18 20:21:00 +08:00
elif [[ "$boot_fstype" == "vfat" ]]; then
2022-12-06 20:18:48 +08:00
cp -f uInitrd-${kernel_name} uInitrd
[[ -z "$(chech_files_same uInitrd-${kernel_name} uInitrd)" ]] || restore_kernel
cp -f vmlinuz-${kernel_name} ${MYBOOT_VMLINUZ}
[[ -z "$(chech_files_same vmlinuz-${kernel_name} ${MYBOOT_VMLINUZ})" ]] || restore_kernel
2022-09-18 20:21:00 +08:00
else
2022-12-06 20:18:48 +08:00
ln -sf uInitrd-${kernel_name} uInitrd
ln -sf vmlinuz-${kernel_name} ${MYBOOT_VMLINUZ}
2022-09-18 20:21:00 +08:00
fi
2022-05-27 20:20:55 +08:00
)
2022-12-06 20:18:48 +08:00
2022-05-27 20:20:55 +08:00
echo -e "(1/3) Unpacking [ boot-${kernel_name}.tar.gz ] done."
2022-03-25 12:20:39 +08:00
2022-09-18 20:21:00 +08:00
if [[ "${PLATFORM}" == "qemu-aarch64" ]]; then
echo -e "(2/3) skip unpack dtb files."
else
# 02. for /boot/dtb/${PLATFORM}/*
if [[ "${boot_fstype}" == "vfat" ]]; then
2022-12-06 20:18:48 +08:00
(cd /boot && mkdir -p dtb/${PLATFORM})
2022-09-18 20:21:00 +08:00
else
2022-12-06 20:18:48 +08:00
(cd /boot && mkdir -p dtb-${kernel_name}/${PLATFORM} && ln -sf dtb-${kernel_name} dtb)
2022-09-18 20:21:00 +08:00
fi
2022-12-06 20:18:48 +08:00
tar -xf ${P4_PATH}/dtb-${PLATFORM}-${kernel_name}.tar.gz -C /boot/dtb/${PLATFORM}
[[ "$(ls /boot/dtb/${PLATFORM} -l 2>/dev/null | grep "^-" | wc -l)" -ge "1" ]] || error_msg "/boot/dtb/${PLATFORM} file is missing."
2022-09-18 20:21:00 +08:00
echo -e "(2/3) Unpacking [ dtb-${PLATFORM}-${kernel_name}.tar.gz ] done."
2022-03-25 12:20:39 +08:00
fi
2022-05-27 20:20:55 +08:00
# 03. for /lib/modules/*
2022-12-06 20:18:48 +08:00
rm -rf /lib/modules/*
tar -xf ${P4_PATH}/modules-${kernel_name}.tar.gz -C /lib/modules
2022-05-27 20:20:55 +08:00
(
cd /lib/modules/${kernel_name}
2022-12-06 20:18:48 +08:00
rm -f *.ko
2022-05-27 20:20:55 +08:00
find ./ -type f -name '*.ko' -exec ln -s {} ./ \;
sync && sleep 3
x=$(ls *.ko -l 2>/dev/null | grep "^l" | wc -l)
2022-12-06 20:18:48 +08:00
[[ "${x}" -eq "0" ]] && error_msg "Error *.ko Files not found."
2022-05-27 20:20:55 +08:00
)
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
2022-12-06 20:18:48 +08:00
rm -f ${P4_PATH}/*-${kernel_name}.tar.gz ${P4_PATH}/sha256sums
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
2022-12-06 20:18:48 +08:00
[[ ! -f "/boot/u-boot.ext" ]] && cp -f "/boot/${UBOOT_OVERLOAD}" /boot/u-boot.ext && chmod +x /boot/u-boot.ext
[[ ! -f "/boot/u-boot.emmc" ]] && cp -f "/boot/u-boot.ext" /boot/u-boot.emmc && chmod +x /boot/u-boot.emmc
2022-05-27 20:20:55 +08:00
echo -e "The ${UBOOT_OVERLOAD} file copy is complete."
elif [[ "${K510}" -eq "0" ]]; then
2022-12-06 20:18:48 +08:00
rm -f "/boot/u-boot.ext" "/boot/u-boot.emmc"
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
2022-12-06 20:18:48 +08:00
sed -i "s|^KERNEL_VERSION=.*|KERNEL_VERSION='${kernel_name}'|g" ${release_file} 2>/dev/null
sed -i "s|^K510=.*|K510='${K510}'|g" ${release_file} 2>/dev/null
# Update banner file
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