small-package/luci-app-amlogic/root/usr/share/amlogic/amlogic_check_firmware.sh

235 lines
9.4 KiB
Bash
Raw Normal View History

2022-03-25 12:20:39 +08:00
#!/bin/bash
#==================================================================
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.
#
# This file is a part of the luci-app-amlogic plugin
# https://github.com/ophub/luci-app-amlogic
#
# Description: Check and update OpenWrt firmware
# Copyright (C) 2021- https://github.com/unifreq/openwrt_packit
# Copyright (C) 2021- https://github.com/ophub/luci-app-amlogic
#==================================================================
# Set a fixed value
check_option="${1}"
download_version="${2}"
TMP_CHECK_DIR="/tmp/amlogic"
AMLOGIC_SOC_FILE="/etc/flippy-openwrt-release"
START_LOG="${TMP_CHECK_DIR}/amlogic_check_firmware.log"
RUNNING_LOG="${TMP_CHECK_DIR}/amlogic_running_script.log"
LOG_FILE="${TMP_CHECK_DIR}/amlogic.log"
2022-07-24 23:41:49 +08:00
support_platform=("allwinner" "rockchip" "amlogic" "qemu-aarch64")
2022-05-29 20:18:44 +08:00
LOGTIME="$(date "+%Y-%m-%d %H:%M:%S")"
2022-03-25 12:20:39 +08:00
[[ -d ${TMP_CHECK_DIR} ]] || mkdir -p ${TMP_CHECK_DIR}
# Clean the running log
clean_running() {
echo -e '' >${RUNNING_LOG} 2>/dev/null && sync
}
# Add log
tolog() {
echo -e "${1}" >${START_LOG}
echo -e "${LOGTIME} ${1}" >>${LOG_FILE}
[[ -n "${2}" && "${2}" -eq "1" ]] && clean_running && exit 1
}
# Check running scripts, prohibit running concurrently
this_running_log="3@OpenWrt update in progress, try again later!"
running_script="$(cat ${RUNNING_LOG} 2>/dev/null | xargs)"
2022-05-29 20:18:44 +08:00
if [[ -n "${running_script}" ]]; then
2022-03-25 12:20:39 +08:00
run_num=$(echo "${running_script}" | awk -F "@" '{print $1}')
run_log=$(echo "${running_script}" | awk -F "@" '{print $2}')
fi
if [[ -n "${run_log}" && "${run_num}" -ne "3" ]]; then
echo -e "${run_log}" >${START_LOG} 2>/dev/null && sync && exit 1
else
echo -e "${this_running_log}" >${RUNNING_LOG} 2>/dev/null && sync
fi
# Find the partition where root is located
2022-05-29 20:18:44 +08:00
ROOT_PTNAME="$(df / | tail -n1 | awk '{print $1}' | awk -F '/' '{print $3}')"
if [[ -z "${ROOT_PTNAME}" ]]; then
2022-03-25 12:20:39 +08:00
tolog "Cannot find the partition corresponding to the root file system!" "1"
fi
# Find the disk where the partition is located, only supports mmcblk?p? sd?? hd?? vd?? and other formats
2022-05-29 20:18:44 +08:00
case "${ROOT_PTNAME}" in
2022-03-25 12:20:39 +08:00
mmcblk?p[1-4])
2022-05-29 20:18:44 +08:00
EMMC_NAME="$(echo ${ROOT_PTNAME} | awk '{print substr($1, 1, length($1)-2)}')"
2022-03-25 12:20:39 +08:00
PARTITION_NAME="p"
;;
[hsv]d[a-z][1-4])
2022-05-29 20:18:44 +08:00
EMMC_NAME="$(echo ${ROOT_PTNAME} | awk '{print substr($1, 1, length($1)-1)}')"
2022-03-25 12:20:39 +08:00
PARTITION_NAME=""
2022-12-22 23:36:52 +08:00
;;
nvme?n?p[1-4])
EMMC_NAME="$(echo ${ROOT_PTNAME} | awk '{print substr($1, 1, length($1)-2)}')"
PARTITION_NAME="p"
2022-03-25 12:20:39 +08:00
;;
*)
tolog "Unable to recognize the disk type of ${ROOT_PTNAME}!" "1"
;;
esac
# Set the default download path
FIRMWARE_DOWNLOAD_PATH="/mnt/${EMMC_NAME}${PARTITION_NAME}4"
[ -d "${FIRMWARE_DOWNLOAD_PATH}/.luci-app-amlogic" ] || mkdir -p "${FIRMWARE_DOWNLOAD_PATH}/.luci-app-amlogic"
2022-05-29 20:18:44 +08:00
# Check release file
if [[ -s "${AMLOGIC_SOC_FILE}" ]]; then
source "${AMLOGIC_SOC_FILE}" 2>/dev/null
PLATFORM="${PLATFORM}"
2022-03-25 12:20:39 +08:00
SOC="${SOC}"
2023-02-10 20:37:09 +08:00
BOARD="${BOARD}"
2022-03-25 12:20:39 +08:00
else
2022-05-29 20:18:44 +08:00
tolog "${AMLOGIC_SOC_FILE} file is missing!" "1"
2022-03-25 12:20:39 +08:00
fi
2023-02-10 20:37:09 +08:00
if [[ -z "${PLATFORM}" || -z "$(echo "${support_platform[@]}" | grep -w "${PLATFORM}")" || -z "${SOC}" || -z "${BOARD}" ]]; then
tolog "Missing [ PLATFORM / SOC / BOARD ] value in ${AMLOGIC_SOC_FILE} file." "1"
2022-07-24 23:41:49 +08:00
fi
2023-03-27 23:36:41 +08:00
tolog "PLATFORM: [ ${PLATFORM} ], BOARD: [ ${BOARD} ], Use in [ ${EMMC_NAME} ]"
2022-03-25 12:20:39 +08:00
sleep 2
# 01. Query local version information
tolog "01. Query version information."
# 01.01 Query the current version
2023-03-27 23:36:41 +08:00
current_kernel_v="$(uname -r 2>/dev/null | grep -oE '^[1-9].[0-9]{1,3}.[0-9]+')"
2022-03-25 12:20:39 +08:00
tolog "01.01 current version: ${current_kernel_v}"
sleep 2
# 01.01 Version comparison
2023-03-27 23:36:41 +08:00
main_line_version="$(echo ${current_kernel_v} | awk -F '.' '{print $1"."$2}')"
2022-03-25 12:20:39 +08:00
# 01.02. Query the selected branch in the settings
2022-05-29 20:18:44 +08:00
server_kernel_branch="$(uci get amlogic.config.amlogic_kernel_branch 2>/dev/null | grep -oE '^[1-9].[0-9]{1,3}')"
if [[ -z "${server_kernel_branch}" ]]; then
2022-03-25 12:20:39 +08:00
server_kernel_branch="${main_line_version}"
uci set amlogic.config.amlogic_kernel_branch="${main_line_version}" 2>/dev/null
uci commit amlogic 2>/dev/null
fi
if [[ "${server_kernel_branch}" != "${main_line_version}" ]]; then
main_line_version="${server_kernel_branch}"
tolog "01.02 Select branch: ${main_line_version}"
sleep 2
fi
# 01.03. Download server version documentation
2022-07-24 23:41:49 +08:00
server_firmware_url="$(uci get amlogic.config.amlogic_firmware_repo 2>/dev/null)"
2022-03-25 12:20:39 +08:00
[[ ! -z "${server_firmware_url}" ]] || tolog "01.03 The custom firmware download repo is invalid." "1"
2022-07-24 23:41:49 +08:00
releases_tag_keywords="$(uci get amlogic.config.amlogic_firmware_tag 2>/dev/null)"
2022-03-25 12:20:39 +08:00
[[ ! -z "${releases_tag_keywords}" ]] || tolog "01.04 The custom firmware tag keywords is invalid." "1"
2022-07-24 23:41:49 +08:00
firmware_suffix="$(uci get amlogic.config.amlogic_firmware_suffix 2>/dev/null)"
2022-03-25 12:20:39 +08:00
[[ ! -z "${firmware_suffix}" ]] || tolog "01.05 The custom firmware suffix is invalid." "1"
2022-05-29 20:18:44 +08:00
if [[ "${server_firmware_url}" == http* ]]; then
server_firmware_url="${server_firmware_url#*com\/}"
2022-03-25 12:20:39 +08:00
fi
# 02. Check Updated
check_updated() {
2023-03-27 23:36:41 +08:00
tolog "02. Start checking for the latest version..."
# Query the latest version
latest_version="$(
curl -s \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${server_firmware_url}/releases |
jq -r --arg RTK "${releases_tag_keywords}" '.[] | select(.tag_name | contains($RTK))' |
jq -s '.[] | {tag_name:.tag_name, data:.published_at, url:.assets[].browser_download_url }' |
jq -s --arg BOARD "_${BOARD}_" --arg MLV "${main_line_version}." '.[] | select((.url | contains($BOARD)) and (.url | contains($MLV)))' |
jq -s 'sort_by(.data)|reverse[]' |
jq -s '.[0]'
)"
latest_updated_at="$(echo ${latest_version} | jq -r '.data')"
latest_url="$(echo ${latest_version} | jq -r '.url')"
2022-05-29 20:18:44 +08:00
latest_updated_at_format="$(echo ${latest_updated_at} | tr 'T' '(' | tr 'Z' ')')"
2022-03-25 12:20:39 +08:00
# Check the firmware update code
op_release_code="${FIRMWARE_DOWNLOAD_PATH}/.luci-app-amlogic/op_release_code"
2022-05-29 20:18:44 +08:00
if [[ -f "${op_release_code}" ]]; then
2022-03-25 12:20:39 +08:00
update_check_code="$(cat ${op_release_code} | xargs)"
if [[ -n "${update_check_code}" && "${update_check_code}" == "${latest_updated_at}" ]]; then
tolog "02.01 Already the latest version, no need to update." "1"
fi
fi
# Prompt to update
2023-03-27 23:36:41 +08:00
if [[ "${latest_url}" == "http"* ]]; then
tolog '<input type="button" class="cbi-button cbi-button-reload" value="Download" onclick="return b_check_firmware(this, '"'download_${latest_updated_at}@${latest_url##*download/}'"')"/> Latest updated: '${latest_updated_at_format}'' "1"
2022-03-25 12:20:39 +08:00
else
2023-03-27 23:36:41 +08:00
tolog "02.02 [${latest_url}] No firmware available, please use another kernel branch." "1"
2022-03-25 12:20:39 +08:00
fi
exit 0
}
# 03. Download Openwrt firmware
download_firmware() {
tolog "03. Download Openwrt firmware ..."
# Get the openwrt firmware download path
2023-03-27 23:36:41 +08:00
if [[ "${download_version}" == "download_"* ]]; then
2022-03-25 12:20:39 +08:00
tolog "03.01 Start downloading..."
else
2023-03-27 23:36:41 +08:00
tolog "03.02 Invalid parameter." "1"
2022-03-25 12:20:39 +08:00
fi
2022-05-29 20:18:44 +08:00
# Delete other residual firmware files
rm -f ${FIRMWARE_DOWNLOAD_PATH}/*${firmware_suffix} 2>/dev/null && sync
rm -f ${FIRMWARE_DOWNLOAD_PATH}/*.img 2>/dev/null && sync
2022-07-28 20:25:10 +08:00
rm -f ${FIRMWARE_DOWNLOAD_PATH}/sha256sums 2>/dev/null && sync
2022-05-29 20:18:44 +08:00
2023-03-27 23:36:41 +08:00
# OpenWrt make data
latest_updated_at="$(echo ${download_version} | awk -F'@' '{print $1}' | sed -e s'|download_||'g)"
2022-05-29 20:18:44 +08:00
# Download firmware
2023-03-27 23:36:41 +08:00
opfile_path="$(echo ${download_version} | awk -F'@' '{print $2}')"
latest_url="https://github.com/ophub/amlogic-s9xxx-openwrt/releases/download/${opfile_path}"
# Download sha256sums file
shafile_path="$(echo ${opfile_path} | awk -F'/' '{print $1}')"
shafile_file="https://github.com/ophub/amlogic-s9xxx-openwrt/releases/download/${shafile_path}/sha256sums"
# Download to local rename
firmware_download_name="openwrt_${BOARD}_k${main_line_version}_github${firmware_suffix}"
wget "${latest_url}" -q -O "${FIRMWARE_DOWNLOAD_PATH}/${firmware_download_name}"
2022-03-25 12:20:39 +08:00
if [[ "$?" -eq "0" && -s "${FIRMWARE_DOWNLOAD_PATH}/${firmware_download_name}" ]]; then
2023-03-27 23:36:41 +08:00
tolog "03.01 OpenWrt downloaded successfully."
2022-03-25 12:20:39 +08:00
else
2023-03-27 23:36:41 +08:00
tolog "03.02 OpenWrt download failed." "1"
2022-03-25 12:20:39 +08:00
fi
2023-03-27 23:36:41 +08:00
firmware_download_oldname="${opfile_path##*/}"
firmware_download_oldname="${firmware_download_oldname//%2B/+}"
if wget "${shafile_file}" -q -O "${FIRMWARE_DOWNLOAD_PATH}/sha256sums" 2>/dev/null; then
tolog "03.03 Sha256sums download complete."
releases_firmware_sha256sums="$(cat sha256sums | grep ${firmware_download_oldname} | awk '{print $1}')"
download_firmware_sha256sums="$(sha256sum ${firmware_download_name} | awk '{print $1}')"
[[ -n "${releases_firmware_sha256sums}" && "${releases_firmware_sha256sums}" != "${download_firmware_sha256sums}" ]] && tolog "03.04 sha256sum verification failed." "1"
2022-05-29 20:18:44 +08:00
fi
2023-03-27 23:36:41 +08:00
sync && sleep 3
2022-03-25 12:20:39 +08:00
2022-05-29 20:18:44 +08:00
tolog "You can update."
2022-03-25 12:20:39 +08:00
#echo '<a href="javascript:;" onclick="return amlogic_update(this, '"'${firmware_download_name}'"')">Update</a>' >$START_LOG
2023-03-27 23:36:41 +08:00
tolog '<input type="button" class="cbi-button cbi-button-reload" value="Update" onclick="return amlogic_update(this, '"'${firmware_download_name}@${latest_updated_at}@${FIRMWARE_DOWNLOAD_PATH}'"')"/>' "1"
2022-03-25 12:20:39 +08:00
exit 0
}
getopts 'cd' opts
2022-05-29 20:18:44 +08:00
case "${opts}" in
2022-03-25 12:20:39 +08:00
c | check)
check_updated
;;
* | download)
download_firmware
;;
esac