Skip to content

Instantly share code, notes, and snippets.

@mcprat
Created June 26, 2020 03:29
Show Gist options
  • Save mcprat/6f46b602c14b6c7b874311cfa0a33bcc to your computer and use it in GitHub Desktop.
Save mcprat/6f46b602c14b6c7b874311cfa0a33bcc to your computer and use it in GitHub Desktop.
ENS202EXT OEM upgrade process
#!/bin/sh
. /lib/upgrade/common.sh
firmware="/tmp/firmware.img"
tmpdir="/tmp/_upgrade"
output="/dev/ttyS0"
fw_env_config="/etc/fw_env.config"
sectorsize="$([ -f "${fw_env_config}" ] && grep "/dev/mtd1 " "${fw_env_config}" | sed -e "s/^\([^ \t]*[ \t]*\)\{4\}.*/\1/g")"
if [ -f "/etc/modelname" ]; then
modelname="$(cat /etc/modelname)"
else
modelname="$(cat /proc/sys/kernel/hostname | tr [A-Z] [a-z])"
fi
before_local="/etc/before-upgradelocal.sh"
after_local="/etc/after-upgradelocal.sh"
before="before-upgrade.sh"
after="after-upgrade.sh"
flag_disable_umount="/tmp/flag_disable_umount"
create_mtdaddr() {
addr="0"
cat "/proc/mtd" | grep "mtd[0-9]*: " | grep -v "\"rootfs_data\"" | cut -d " " -f 2,4 | \
while read line; do
printf "0xbf%06x $(echo "${line}" | cut -d " " -f 2)\n" "${addr}"
addr="$((${addr} + 0x$(echo "${line}" | cut -d " " -f 1)))"
done
}
doupgrade() {
mtdaddr="$(create_mtdaddr)"
get_start_addr() {
echo -e "${mtdaddr}" | grep "\"$1\"" | cut -d " " -f 1
}
get_size() {
if [ "$1" = "kernel" ]; then
echo "0x$(printf "%08x" "$(ls -al "${kernel}" | sed -e "s/^\([^ ]* *\)\{4\}\([0-9]\+\).*$/\2/g")")"
elif [ "$1" = "rootfs" ]; then
if [ "$(get_magic_word "${rootfs}")" = "6873" ]; then
len=""
for var in $(hexdump "${rootfs}" -s 40 -n 4 -e '/1 "%02x "'); do
len="${var}${len}"
done
len="$((0x${len}))"
else
len="$(hexdump "${rootfs}" -s 67 -n 4 -e '"%d"')"
fi
printf "0x%08x\n" "$((((${len} - 1) / ${sectorsize} + 1) * ${sectorsize}))"
fi
}
get_checksum() {
if [ "$1" = "kernel" ]; then
md5sum "${kernel}"
elif [ "$1" = "rootfs" ]; then
dd if="${rootfs}" bs="${sectorsize}" count="$(($(get_size "rootfs") / ${sectorsize}))" 2>/dev/null | md5sum -
fi | cut -d " " -f 1
}
[ -f "${before_local}" ] && chmod a+x "${before_local}" && . "${before_local}"
[ -f "${before}" ] && chmod a+x "${before}" && . "${before}"
[ ! -f "${flag_disable_umount}" ] && {
echo "Umount /jffs.." >"${output}"
. /lib/functions/boot.sh && umount -l /jffs && pivot /rom /mnt && umount -l /mnt && ramoverlay && echo "Umount /jffs..Done" >"${output}"
}
[ -n "${kernel}" -a -f "${kernel}" ] &&
[ "${magic_word_kernel}" = "2705" ] && {
echo "Writing kernel..." >"${output}"
fw_setenv vmlinux_start_addr "$(get_start_addr "kernel")"
fw_setenv vmlinux_size "$(get_size "kernel")"
fw_setenv vmlinux_checksum "$(get_checksum "kernel")"
mtd write "${kernel}" "kernel"
}
[ -n "${rootfs}" -a -f "${rootfs}" ] &&
[ "${magic_word_rootfs}" = "7371" -o "${magic_word_rootfs}" = "6873" ] && {
echo "Writing rootfs..." >"${output}"
fw_setenv rootfs_start_addr "$(get_start_addr "rootfs")"
fw_setenv rootfs_size "$(get_size "rootfs")"
fw_setenv rootfs_checksum "$(get_checksum "rootfs")"
mtd -j "/tmp/_sys/sysupgrade.tgz" write "${rootfs}" "rootfs"
}
[ -f "${after_local}" ] && chmod a+x "${after_local}" && . "${after_local}"
[ -f "${after}" ] && chmod a+x "${after}" && . "${after}"
ask_bool 1 "Reboot"
echo "Upgrade completed, rebooting system..." >"${output}"
}
if [ -f "${firmware}" ]; then
if [ -n "${sectorsize}" ]; then
sectorsize="$((${sectorsize}))"
else
echo "sectorsize Not defined." >"${output}"
return 3
fi
[ -e "${tmpdir}" ] && rm -rf "${tmpdir}"
mkdir -p "${tmpdir}" && cd "${tmpdir}" && tar zxf "${firmware}" && {
errcode="1"
kernel="$(ls -1 | grep "^openwrt\-senao\-${modelname}\-uImage\-lzma\.bin$")"
rootfs="$(ls -1 | grep "^openwrt\-senao\-${modelname}\-root\.squashfs$")"
[ -n "${kernel}" -a -f "${kernel}" -a -n "${rootfs}" -a -f "${rootfs}" ] && {
magic_word_kernel="$(get_magic_word "${kernel}")"
magic_word_rootfs="$(get_magic_word "${rootfs}")"
[ "${magic_word_kernel}" = "2705" ] &&
[ "${magic_word_rootfs}" = "7371" -o "${magic_word_rootfs}" = "6873" ] &&
errcode="0"
}
if [ "${errcode}" -eq "0" ] || [ -f "${before}" -o -f "${after}" ]; then
[ "$1" = "test" ] || {
echo doupgrade >"${output}"
rm -rf "${firmware}"
doupgrade
}
else
echo "Firmware invalid format." >"${output}"
return 1
fi
return
} || {
echo "Firmware invalid format." >"${output}"
return 1
}
fi
echo "$firmware Not existed." >"${output}"
return 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment