fwupdate: use vars a bit wiser

This commit is contained in:
Calin Crisan 2019-02-02 19:46:12 +02:00
parent bebd4f2ac3
commit f7ed30268e

View File

@ -41,30 +41,33 @@ SYS_BOARD_FILE=/etc/board
OS_CONF_FILE=/etc/init.d/os_conf
MIN_FREE_DISK=500 # MB
VER_FILE=version
BOOT_INFO_FILE=boot_info
ROOT_INFO_FILE=root_info
BOOT_READY_FILE=boot_flash_ready
FW_DIR=/data/.fwupdate
FW_FILE_GZ=firmware.img.gz
FW_FILE_XZ=firmware.img.xz
FW_FILE_EXTR=firmware.img
FW_FILE_GZ=${FW_DIR}/firmware.img.gz
FW_FILE_XZ=${FW_DIR}/firmware.img.xz
FW_FILE_EXTR=${FW_DIR}/firmware.img
VER_FILE=${FW_DIR}/version
BOOT_INFO_FILE=${FW_DIR}/boot_info
ROOT_INFO_FILE=${FW_DIR}/root_info
BOOT_READY_FILE=${FW_DIR}/boot_flash_ready
TMP_BOOT_DIR=/tmp/fwupdate_boot
TMP_ROOT_DIR=/tmp/fwupdate_root
CURL_LOG_FILE=curl.log
CURL_PID_FILE=curl.pid
CURL_LOG_FILE=${FW_DIR}/curl.log
CURL_PID_FILE=${FW_DIR}/curl.pid
GUNZIP_LOG_FILE=gunzip.log
GUNZIP_PID_FILE=gunzip.pid
GUNZIP_LOG_FILE=${FW_DIR}/gunzip.log
GUNZIP_PID_FILE=${FW_DIR}/gunzip.pid
XZCAT_LOG_FILE=xzcat.log
XZCAT_PID_FILE=xzcat.pid
XZCAT_LOG_FILE=${FW_DIR}/xzcat.log
XZCAT_PID_FILE=${FW_DIR}/xzcat.pid
DD_LOG_FILE=dd.log
DD_PID_FILE=dd.pid
DD_LOG_FILE=${FW_DIR}/dd.log
DD_PID_FILE=${FW_DIR}/dd.pid
source ${OS_CONF_FILE}
source ${SYS_VERSION_FILE}
@ -141,17 +144,17 @@ function do_download() {
FILEEXT=${FNAME##*.}
DST_FNAME=""
if [[ "${FILEEXT}" == "xz" ]]; then
DST_FNAME="${FW_DIR}/${FW_FILE_XZ}"
DST_FNAME=${FW_FILE_XZ}
elif [[ "${FILEEXT}" == "gz" ]]; then
DST_FNAME="${FW_DIR}/${FW_FILE_GZ}"
DST_FNAME=${FW_FILE_GZ}
fi
if [[ -n "${DST_FNAME}" ]]; then
cp -f $1 ${DST_FNAME}
echo ${version} > ${FW_DIR}/${VER_FILE}
echo ${version} > ${VER_FILE}
return
fi
fi
board=$(cat ${SYS_BOARD_FILE})
url=$1
version=$1
@ -173,40 +176,40 @@ function do_download() {
exit 1
fi
outfile=${FW_DIR}/${FW_FILE_GZ}
outfile=${FW_FILE_GZ}
format=$(echo ${url} | sed -rn 's/.*\.img\.([a-z]+)$/\1/ p')
if [[ "${format}" == "xz" ]]; then
outfile=${FW_DIR}/${FW_FILE_XZ}
outfile=${FW_FILE_XZ}
fi
echo ${version} > ${FW_DIR}/${VER_FILE}
echo ${version} > ${VER_FILE}
curl_opts="-S -f -L"
if [[ -n "${OS_FIRMWARE_USERNAME}" ]]; then
curl_opts+=" --user ${OS_FIRMWARE_USERNAME}:${OS_FIRMWARE_PASSWORD}"
fi
curl ${curl_opts} -o ${outfile} "${url}" &> ${FW_DIR}/${CURL_LOG_FILE} &
curl ${curl_opts} -o ${outfile} "${url}" &> ${CURL_LOG_FILE} &
pid=$!
echo ${pid} > ${FW_DIR}/${CURL_PID_FILE}
echo ${pid} > ${CURL_PID_FILE}
wait ${pid}
if [[ "$?" != 0 ]]; then
cat ${FW_DIR}/${CURL_LOG_FILE}
cat ${CURL_LOG_FILE}
exit 1
fi
}
function download_status() {
if [[ -f ${FW_DIR}/${CURL_PID_FILE} ]]; then
pid=$(cat ${FW_DIR}/${CURL_PID_FILE})
if [[ -f ${CURL_PID_FILE} ]]; then
pid=$(cat ${CURL_PID_FILE})
if kill -0 ${pid} &>/dev/null; then
echo "running"
return
fi
fi
if [[ -f ${FW_DIR}/${FW_FILE_GZ} -o -f ${FW_DIR}/${FW_FILE_XZ} ]]; then
if [[ -f ${FW_FILE_GZ} || -f ${FW_FILE_XZ} ]]; then
echo "done"
fi
}
@ -217,16 +220,16 @@ function download_status() {
function run_pre_upgrade() {
which losetup &>/dev/null || return 0
boot_info=($(cat ${FW_DIR}/${BOOT_INFO_FILE}))
root_info=($(cat ${FW_DIR}/${ROOT_INFO_FILE}))
boot_info=($(cat ${BOOT_INFO_FILE}))
root_info=($(cat ${ROOT_INFO_FILE}))
boot_loop="/dev/loop3"
root_loop="/dev/loop4"
pre_upgrade="${TMP_ROOT_DIR}/usr/share/pre-upgrade/*"
mkdir -p ${TMP_BOOT_DIR}
mkdir -p ${TMP_ROOT_DIR}
losetup -o $((boot_info[0] * 512)) ${boot_loop} ${FW_DIR}/${FW_FILE_EXTR}
losetup -o $((root_info[0] * 512)) ${root_loop} ${FW_DIR}/${FW_FILE_EXTR}
losetup -o $((boot_info[0] * 512)) ${boot_loop} ${FW_FILE_EXTR}
losetup -o $((root_info[0] * 512)) ${root_loop} ${FW_FILE_EXTR}
mount ${boot_loop} ${TMP_BOOT_DIR}
mount ${root_loop} ${TMP_ROOT_DIR}
@ -252,30 +255,30 @@ function run_pre_upgrade() {
function do_extract() {
echo "extracting..."
rm -f ${FW_DIR}/${FW_FILE_EXTR}
rm -f ${FW_DIR}/${BOOT_READY_FILE}
rm -f ${FW_FILE_EXTR}
rm -f ${BOOT_READY_FILE}
if ! [[ -f ${FW_DIR}/${FW_FILE_GZ} || -f ${FW_DIR}/${FW_FILE_XZ} ]]; then
if ! [[ -f ${FW_FILE_GZ} || -f ${FW_FILE_XZ} ]]; then
echo "firmware file not downloaded" 1>&2
exit 1
fi
format="gz"
if [[ -f ${FW_DIR}/${FW_FILE_XZ} ]]; then
if [[ -f ${FW_FILE_XZ} ]]; then
format="xz"
fi
rm -f ${FW_DIR}/${FW_FILE_EXTR}
rm -f ${FW_DIR}/${GUNZIP_PID_FILE} ${FW_DIR}/${XZCAT_PID_FILE}
rm -f ${FW_FILE_EXTR}
rm -f ${GUNZIP_PID_FILE} ${XZCAT_PID_FILE}
if [[ "${format}" == "xz" ]]; then
DECOMPRESS_LOG_FILE=${FW_DIR}/${XZCAT_LOG_FILE}
DECOMPRESS_PID_FILE=${FW_DIR}/${XZCAT_PID_FILE}
xzcat ${FW_DIR}/${FW_FILE_XZ} > ${FW_DIR}/${FW_FILE_EXTR} 2>${FW_DIR}/${XZCAT_LOG_FILE} &
DECOMPRESS_LOG_FILE=${XZCAT_LOG_FILE}
DECOMPRESS_PID_FILE=${XZCAT_PID_FILE}
xzcat ${FW_FILE_XZ} > ${FW_FILE_EXTR} 2>${XZCAT_LOG_FILE} &
elif [[ "${format}" == "gz" ]]; then
DECOMPRESS_LOG_FILE=${FW_DIR}/${GUNZIP_LOG_FILE}
DECOMPRESS_PID_FILE=${FW_DIR}/${GUNZIP_PID_FILE}
gunzip -k -c ${FW_DIR}/${FW_FILE_GZ} > ${FW_DIR}/${FW_FILE_EXTR} 2>${FW_DIR}/${GUNZIP_LOG_FILE} &
DECOMPRESS_LOG_FILE=${GUNZIP_LOG_FILE}
DECOMPRESS_PID_FILE=${GUNZIP_PID_FILE}
gunzip -k -c ${FW_FILE_GZ} > ${FW_FILE_EXTR} 2>${GUNZIP_LOG_FILE} &
else
echo "firmware compression format ${format} not supported" 1>&2
exit 1
@ -292,40 +295,40 @@ function do_extract() {
# TODO verify hash
boot_info=$(fdisk --bytes -l -o device,start,end,size ${FW_DIR}/${FW_FILE_EXTR} | grep "${FW_FILE_EXTR}1")
boot_info=$(fdisk --bytes -l -o device,start,end,size ${FW_FILE_EXTR} | grep "${FW_FILE_EXTR}1")
boot_info=(${boot_info})
boot_start=${boot_info[1]}
boot_end=${boot_info[2]}
boot_size=$((boot_info[3] / 512))
root_info=$(fdisk --bytes -l -o device,start,end,size ${FW_DIR}/${FW_FILE_EXTR} | grep "${FW_FILE_EXTR}2")
root_info=$(fdisk --bytes -l -o device,start,end,size ${FW_FILE_EXTR} | grep "${FW_FILE_EXTR}2")
root_info=(${root_info})
root_start=${root_info[1]}
root_end=${root_info[2]}
root_size=$((root_info[3] / 512))
echo ${boot_start} ${boot_end} ${boot_size} > ${FW_DIR}/${BOOT_INFO_FILE}
echo ${root_start} ${root_end} ${root_size} > ${FW_DIR}/${ROOT_INFO_FILE}
echo ${boot_start} ${boot_end} ${boot_size} > ${BOOT_INFO_FILE}
echo ${root_start} ${root_end} ${root_size} > ${ROOT_INFO_FILE}
run_pre_upgrade
}
function extract_status() {
if [[ -f ${FW_DIR}/${XZCAT_PID_FILE} ]]; then
pid=$(cat ${FW_DIR}/${XZCAT_PID_FILE})
if [[ -f ${XZCAT_PID_FILE} ]]; then
pid=$(cat ${XZCAT_PID_FILE})
if kill -0 ${pid} &>/dev/null; then
echo "running"
return
fi
elif [[ -f ${FW_DIR}/${GUNZIP_PID_FILE} ]]; then
pid=$(cat ${FW_DIR}/${GUNZIP_PID_FILE})
elif [[ -f ${GUNZIP_PID_FILE} ]]; then
pid=$(cat ${GUNZIP_PID_FILE})
if kill -0 ${pid} &>/dev/null; then
echo "running"
return
fi
fi
if [[ -f ${FW_DIR}/${FW_FILE_EXTR} && -f ${FW_DIR}/${ROOT_INFO_FILE} ]]; then
if [[ -f ${FW_FILE_EXTR} && -f ${ROOT_INFO_FILE} ]]; then
echo "done"
fi
}
@ -340,10 +343,10 @@ function reallocate_boot_part() {
current_root_info=$(fdisk --bytes -l -o device,start,end,size ${DISK_DEV} | grep "${ROOT_DEV}")
current_root_info=(${current_root_info})
boot_info=($(cat ${FW_DIR}/${BOOT_INFO_FILE}))
boot_info=($(cat ${BOOT_INFO_FILE}))
if [[ ${current_boot_info[0]} == ${boot_info[0]} ]] &&
[[ ${current_boot_info[1]} == ${boot_info[1]} ]]; then
if [[ ${current_boot_info[1]} == ${boot_info[0]} ]] &&
[[ ${current_boot_info[2]} == ${boot_info[1]} ]]; then
return # all good
fi
@ -351,7 +354,7 @@ function reallocate_boot_part() {
echo "reallocating boot partition..."
# check overlapping with root partition
if [[ ${boot_info[1]} -ge ${current_root_info[0]} ]]; then
if [[ ${boot_info[1]} -ge ${current_root_info[1]} ]]; then
echo "cannot reallocate boot partition: will overlap with root"
return 1
fi
@ -364,6 +367,9 @@ p
1
${boot_info[0]}
${boot_info[1]}
t
1
c
w
END
sync
@ -372,7 +378,7 @@ END
function do_flash_boot() {
echo "flashing boot..."
rm -f ${FW_DIR}/${BOOT_READY_FILE}
rm -f ${BOOT_READY_FILE}
board=$(cat ${SYS_BOARD_FILE})
@ -387,9 +393,9 @@ function do_flash_boot() {
reallocate_boot_part
dd if=${FW_DIR}/${FW_FILE_EXTR} skip=${boot_info[0]} of=${BOOT_DEV} bs=1048576 count=$((boot_info[2] / 2048)) &>${FW_DIR}/${DD_LOG_FILE} &
dd if=${FW_FILE_EXTR} skip=${boot_info[0]} of=${BOOT_DEV} bs=1048576 count=$((boot_info[2] / 2048)) &>${DD_LOG_FILE} &
pid=$!
echo ${pid} > ${FW_DIR}/${DD_PID_FILE}
echo ${pid} > ${DD_PID_FILE}
wait ${pid}
mount -T /etc/fstab.disk -o rw /boot
@ -400,19 +406,19 @@ function do_flash_boot() {
if [[ -x /usr/libexec/fw-restore-boot-cfg ]]; then
/usr/libexec/fw-restore-boot-cfg ${FW_DIR}/old_boot 2>/dev/null || true
fi
touch ${FW_DIR}/${BOOT_READY_FILE}
touch ${BOOT_READY_FILE}
}
function flash_boot_status() {
if [[ -f ${FW_DIR}/${DD_PID_FILE} ]]; then
pid=$(cat ${FW_DIR}/${DD_PID_FILE})
if [[ -f ${DD_PID_FILE} ]]; then
pid=$(cat ${DD_PID_FILE})
if kill -0 ${pid} &>/dev/null; then
echo "running"
return
fi
fi
if [[ -f ${FW_DIR}/${BOOT_READY_FILE} ]]; then
if [[ -f ${BOOT_READY_FILE} ]]; then
echo "done"
fi
}
@ -431,15 +437,15 @@ function flash_cleanup() {
function reallocate_root_part() {
current_root_info=$(fdisk --bytes -l -o device,start,end,size ${DISK_DEV} | grep "${ROOT_DEV}")
current_root_info=(${root_info})
current_root_info=(${current_root_info})
current_data_info=$(fdisk --bytes -l -o device,start,end,size ${DISK_DEV} | grep "${DATA_DEV}")
current_data_info=(${data_info})
current_data_info=(${current_data_info})
root_info=($(cat ${FW_DIR}/${ROOT_INFO_FILE}))
root_info=($(cat ${ROOT_INFO_FILE}))
if [[ ${current_root_info[0]} == ${root_info[0]} ]] &&
[[ ${current_root_info[1]} == ${root_info[1]} ]]; then
if [[ ${current_root_info[1]} == ${root_info[0]} ]] &&
[[ ${current_root_info[2]} == ${root_info[1]} ]]; then
return # all good
fi
@ -447,7 +453,7 @@ function reallocate_root_part() {
echo "reallocating root partition..."
# check overlapping with data partition
if [[ ${root_info[1]} -ge ${current_data_info[0]} ]]; then
if [[ ${root_info[1]} -ge ${current_data_info[1]} ]]; then
echo "cannot reallocate root partition: will overlap with data"
return 1
fi
@ -460,13 +466,16 @@ p
2
${root_info[0]}
${root_info[1]}
t
2
83
w
END
sync
}
function do_flash_reboot() {
if [[ ! -f ${FW_DIR}/${ROOT_INFO_FILE} ]]; then
if [[ ! -f ${ROOT_INFO_FILE} ]]; then
echo "extracted firmware not present"
return 1
fi
@ -497,7 +506,7 @@ function do_flash_reboot() {
#### status ####
function new_version() {
cat ${FW_DIR}/${VER_FILE}
cat ${VER_FILE}
}
function backup_conf() {