Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- log() {
- echo $* >> /dev/console
- echo $* >> /tmp/upgrade.log
- }
- log $0
- ps aux > /dev/console
- current_type=$(cat /etc/version)
- MTD_STATUS_FILE=/tmp/cfg/CWMP/MtdStatus
- if [ "$current_type" = "main" ]; then
- log Remounting rootfs in Read-Only Mode
- mount -o remount,ro /old_root/overlay/
- if [ "$?" != 0 ]; then
- echo Remount read-only failed
- exit 1
- fi
- fi
- migrate_conf_to_openrg()
- {
- local ext_ver="$1"
- if yacs check conf_rg_main; then
- log "Converting MAIN configuration"
- /etc/yacs/openrg/start.sh conf_rg_main
- fi
- if yacs check conf_rg_main; then
- log "Converting RECOVERY configuration"
- cp /etc/yacs/openrg/recovery.img /tmp/rec.img
- EXT_VER="$ext_ver" TYPE=file MODULES="recovery cwmp_transfer" /etc/yacs/openrg/start.sh /tmp/rec.img
- if [ "$?" = "0" ]; then
- rg_rec_args=" erase conf_rg_recovery write /tmp/rec.img conf_rg_recovery"
- if ! umount /tmp/cfg; then
- mount -o remount,ro /tmp/cfg || log "Warning: conf_fs still mounted rw"
- fi
- fi
- fi
- if ! yacs check conf_factory; then
- log "Converting FACTORY configuration"
- cp /etc/yacs/openrg/factory.img /tmp/factory.img
- TYPE=factory_file MODULES=factory /etc/yacs/openrg/start.sh /tmp/factory.img
- rg_rec_args="$rg_rec_args write /tmp/factory.img conf_factory"
- umount /tmp/factory 2> /dev/null
- fi
- }
- parse_rmt_image()
- {
- log "Parsing RMT format..."
- Sha1=`sha1sum $file`
- imageSha1cksum=${Sha1%% *}
- i=36
- while true; do
- byte=$(hexdump $file -s $i -n 1 -ve '/1 "%02x"')
- i=$((i + 1))
- [ -z "$byte" ] && return
- [ "$byte" = "00" ] && break
- done
- token1=$(hexdump $file -s 4 -n 16 -ve '/1 "%02x"')
- token2=$(hexdump $file -s 20 -n 16 -ve '/1 "%02x"')
- dd if=/dev/zero of=$file conv=notrunc bs=1 count=32 seek=4
- hash1=$(head -c $i $file | md5sum | cut -d " " -f1)
- [ "$token1" != "$hash1" ] && return
- hash2=$(md5sum $file | cut -d " " -f1)
- if [ "$token2" != "$hash2" ]; then
- log "Wrong validation token"
- exit 2
- fi
- image_size=$((0x$(hexdump -n 4 $file -ve '/1 "%02x"')))
- total_size=$((image_size + i))
- if [ "$(ls -l $file | awk '{ print $5 }')" != "$total_size" ]; then
- log "Wrong image size $total_size"
- exit 2
- fi
- head -c $i $file | grep -q "prod_version: 4.8.3"
- if [ "$?" != 0 ]; then
- log "Wrong product version"
- exit 2
- fi
- magic=$(hexdump -s $i -n 4 -ve '/1 "%02x"' $file)
- if [ "$magic" != "38000000" ]; then
- log "Bad magic number $magic"
- exit 2
- fi
- chip=$(hexdump -s $(($i + 38)) -n 6 -ve '/1 "%c"' $file)
- if [ "$chip" != "6368" ]; then
- log "Wrong chip $chip"
- exit 2
- fi
- boardId=$(hexdump -s $(($i + 44)) -n 16 -ve '/1 "%c"' $file)
- if [ "$boardId" != "96368VVW" ]; then
- log "Wrong board-id"
- exit 2
- fi
- header=$(hexdump -s 36 -n $((i - 36)) -ve '/1 "%c"' $file)
- image_type=$(hexdump -s $(($i + 24)) -n 14 -ve '/1 "%c"' $file)
- if [ "$current_type" = "main" ]; then
- if [ "$image_type" != "RECOVERY" ]; then
- log "Wrong image type $image_type"
- exit 2
- fi
- ext_ver=$(echo "$header" | grep "ext_ver: ")
- ext_ver=${ext_ver#ext_ver: }
- dist=$(echo "$header" | grep "dist: ")
- dist=${dist#dist: }
- full_ver="$ext_ver"
- ext_ver=$(echo "$ext_ver" | awk -F "." '{ printf "%s.%s.%s.%04d", $1, $2, $3, $4 }')
- log "Dist: $dist"
- log "Version: $ext_ver"
- if [ -n $imageSha1cksum ]; then
- if [ ! "$imageSha1cksum" = "8298347f4e970ede5a26ab9304a7bd40fb78ba40" ]; then
- log "Invalid SHA1 sum"
- exit 2
- else
- log "SHA1 SIGNATURE Correct"
- fi
- fi
- if [ "$dist" != "IAD_SWISSCOM_RECOVERY" ]; then
- exit 2
- fi
- if [ "$full_ver" != "5.0.0.37_04b20R" ]; then
- exit 2
- fi
- migrate_conf_to_openrg "$full_ver" >> /dev/console 2>&1
- echo "write $file:$i image_2 write /dev/zero:0:256 image_1 erase conf_fs $rg_rec_args"
- else
- log "RMT images are not supported when recovery is running"
- exit 2
- fi
- }
- parse_yaps_image()
- {
- log "Parsing YAPS format..."
- magic=$(hexdump -s 0 -n 4 -ve '/1 "%02x"' $file)
- tag_ver=$(hexdump -s 4 -n 2 -ve '/1 "%c"' $file)
- if [ "$magic" != "79494d47" ] || [ "$tag_ver" != "1" ]; then
- return
- fi
- cpuinfo=$(grep "system type" /proc/cpuinfo | awk '{print $4}')
- system_type=$(hexdump -s 6 -n 18 -ve '/1 "%c"' $file)
- if [ "$cpuinfo" != "$system_type" ]; then
- log "Wrong system type $system_type (expected: $cpuinfo)"
- exit 2
- fi
- dist=$(hexdump -s 24 -n 24 -ve '/1 "%c"' $file)
- if [ "$dist" != "$(cat /etc/distro)" ]; then
- log "Wrong distribution $dist"
- exit 2
- fi
- if [ -x /usr/sbin/sig_verify ]; then
- sig_verify $file 2> /dev/null
- ret_code=$?
- if [ "$ret_code" = 0 ]; then
- log "Signature OK"
- else
- log "Invalid signature (error: $ret_code)"
- exit 2
- fi
- else
- token=$(hexdump $file -s 240 -n 16 -ve '/1 "%02x"')
- dd if=/dev/zero of=$file conv=notrunc bs=1 count=16 seek=240
- hash=$(md5sum $file | cut -d " " -f1)
- if [ "$token" != "$hash" ]; then
- log "Error: wrong validation token ($token, $hash)"
- exit 2
- fi
- fi
- image1_type=$(hexdump -s 80 -n 4 -ve '/1 "%c"' $file)
- image2_type=$(hexdump -s 104 -n 4 -ve '/1 "%c"' $file)
- if [ "$image1_type" = "M" ]; then
- m_offset=$(hexdump -s 84 -n 10 -ve '/1 "%c"' $file)
- m_len=$(hexdump -s 94 -n 10 -ve '/1 "%c"' $file)
- m_offset=$(printf "%d" "$m_offset" 2> /dev/null)
- m_len=$(printf "%d" "$m_len" 2> /dev/null)
- fi
- if [ "$image2_type" = "R" ]; then
- r_offset=$(hexdump -s 108 -n 10 -ve '/1 "%c"' $file)
- r_len=$(hexdump -s 118 -n 10 -ve '/1 "%c"' $file)
- r_offset=$(printf "%d" "$r_offset" 2> /dev/null)
- r_len=$(printf "%d" "$r_len" 2> /dev/null)
- fi
- if [ "$current_type" = "main" ]; then
- if [ "$image1_type" != "M" ] || [ "$image2_type" != "R" ]; then
- log "Error: image must contain a MAIN and a RECOVERY"
- exit 2
- fi
- echo " write $file:$r_offset:$r_len image_2 write $file:$m_offset:$m_len image_1"
- else
- if [ "$image1_type" != "M" ]; then
- log "Error: image must contain a MAIN"
- exit 2
- fi
- echo " write $file:$m_offset:$m_len image_1"
- fi
- }
- case "$1" in
- http://*|https://*)
- url_remote=$1
- log "loading from remote: $url_remote"
- yaft -d "$url_remote" -o /tmp/fw.bin
- if [ $? -eq 0 ]; then
- file="/tmp/fw.bin"
- else
- log "Cannot download file from $url_remote"
- exit 8
- fi
- ;;
- *)
- file=$1
- ;;
- esac
- [ -f "$file" ] || file="/tmp/fw.bin"
- killall ec hostapd
- mtd_cmd=$(parse_yaps_image)
- [ -z "$mtd_cmd" ] && mtd_cmd=$(parse_rmt_image)
- if [ -z "$mtd_cmd" ]; then
- logger -t "Swisscom" -p 6 "Image not recognized"
- log "Image not recognized"
- exit 1
- fi
- cmclient SET Device.DeviceInfo.X_ADB_UpgradeInProgress true >/dev/null
- log "Kill HTTP server"
- killall -9 nhttpd
- log "Kill Config Man"
- killall -15 cm
- rm -rf /tmp/cfg/cache
- if [ -n "$mtd_cmd" ]; then
- log "mtd -V -r $mtd_cmd"
- mtd -s $MTD_STATUS_FILE -V -r $mtd_cmd >> /dev/console 2>&1
- fi
- log "Upgrade Failed"
- cmclient SET Device.DeviceInfo.X_ADB_UpgradeInProgress false >/dev/null
- exit 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement