SHARE
TWEET

Swisscom Centro Grande upgrade.sh

a guest Jan 17th, 2015 364 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. log() {
  3. echo $* >> /dev/console
  4. echo $* >> /tmp/upgrade.log
  5. }
  6. log $0
  7. ps aux > /dev/console
  8. current_type=$(cat /etc/version)
  9. MTD_STATUS_FILE=/tmp/cfg/CWMP/MtdStatus
  10. if [ "$current_type" = "main" ]; then
  11. log Remounting rootfs in Read-Only Mode
  12. mount -o remount,ro /old_root/overlay/
  13. if [ "$?" != 0 ]; then
  14. echo Remount read-only failed
  15. exit 1
  16. fi
  17. fi
  18. migrate_conf_to_openrg()
  19. {
  20. local ext_ver="$1"
  21. if yacs check conf_rg_main; then
  22. log "Converting MAIN configuration"
  23. /etc/yacs/openrg/start.sh conf_rg_main
  24. fi
  25. if yacs check conf_rg_main; then
  26. log "Converting RECOVERY configuration"
  27. cp /etc/yacs/openrg/recovery.img /tmp/rec.img
  28. EXT_VER="$ext_ver" TYPE=file MODULES="recovery cwmp_transfer" /etc/yacs/openrg/start.sh /tmp/rec.img
  29. if [ "$?" = "0" ]; then
  30. rg_rec_args=" erase conf_rg_recovery write /tmp/rec.img conf_rg_recovery"
  31. if ! umount /tmp/cfg; then
  32. mount -o remount,ro /tmp/cfg || log "Warning: conf_fs still mounted rw"
  33. fi
  34. fi
  35. fi
  36. if ! yacs check conf_factory; then
  37. log "Converting FACTORY configuration"
  38. cp /etc/yacs/openrg/factory.img /tmp/factory.img
  39. TYPE=factory_file MODULES=factory /etc/yacs/openrg/start.sh /tmp/factory.img
  40. rg_rec_args="$rg_rec_args write /tmp/factory.img conf_factory"
  41. umount /tmp/factory 2> /dev/null
  42. fi
  43. }
  44. parse_rmt_image()
  45. {
  46. log "Parsing RMT format..."
  47. Sha1=`sha1sum $file`
  48. imageSha1cksum=${Sha1%% *}
  49. i=36
  50. while true; do
  51. byte=$(hexdump $file -s $i -n 1 -ve '/1 "%02x"')
  52. i=$((i + 1))
  53. [ -z "$byte" ] && return
  54. [ "$byte" = "00" ] && break
  55. done
  56. token1=$(hexdump $file -s 4 -n 16  -ve '/1 "%02x"')
  57. token2=$(hexdump $file -s 20 -n 16  -ve '/1 "%02x"')
  58. dd if=/dev/zero of=$file conv=notrunc bs=1 count=32 seek=4
  59. hash1=$(head -c $i $file | md5sum | cut -d " " -f1)
  60. [ "$token1" != "$hash1" ] && return
  61. hash2=$(md5sum $file | cut -d " " -f1)
  62. if [ "$token2" != "$hash2" ]; then
  63. log "Wrong validation token"
  64. exit 2
  65. fi
  66. image_size=$((0x$(hexdump -n 4 $file  -ve '/1 "%02x"')))
  67. total_size=$((image_size + i))
  68. if [ "$(ls -l $file | awk '{ print $5 }')" != "$total_size" ]; then
  69. log "Wrong image size $total_size"
  70. exit 2
  71. fi
  72. head -c $i $file | grep -q "prod_version: 4.8.3"
  73. if [ "$?" != 0 ]; then
  74. log "Wrong product version"
  75. exit 2
  76. fi
  77. magic=$(hexdump -s $i -n 4 -ve '/1 "%02x"' $file)
  78. if [ "$magic" != "38000000" ]; then
  79. log "Bad magic number $magic"
  80. exit 2
  81. fi
  82. chip=$(hexdump -s $(($i + 38)) -n 6 -ve '/1 "%c"' $file)
  83. if [ "$chip" != "6368" ]; then
  84. log "Wrong chip $chip"
  85. exit 2
  86. fi
  87. boardId=$(hexdump -s $(($i + 44)) -n 16 -ve '/1 "%c"' $file)
  88. if [ "$boardId" != "96368VVW" ]; then
  89. log "Wrong board-id"
  90. exit 2
  91. fi
  92. header=$(hexdump -s 36 -n $((i - 36)) -ve '/1 "%c"' $file)
  93. image_type=$(hexdump -s $(($i + 24)) -n 14 -ve '/1 "%c"' $file)
  94. if [ "$current_type" = "main" ]; then
  95. if [ "$image_type" != "RECOVERY" ]; then
  96. log "Wrong image type $image_type"
  97. exit 2
  98. fi
  99. ext_ver=$(echo "$header" | grep "ext_ver: ")
  100. ext_ver=${ext_ver#ext_ver: }
  101. dist=$(echo "$header" | grep "dist: ")
  102. dist=${dist#dist: }
  103. full_ver="$ext_ver"
  104. ext_ver=$(echo "$ext_ver" | awk -F "." '{ printf "%s.%s.%s.%04d", $1, $2, $3, $4 }')
  105. log "Dist: $dist"
  106. log "Version: $ext_ver"
  107. if [ -n $imageSha1cksum ]; then
  108. if [ ! "$imageSha1cksum" = "8298347f4e970ede5a26ab9304a7bd40fb78ba40" ]; then
  109. log "Invalid SHA1 sum"
  110. exit 2
  111. else
  112. log "SHA1 SIGNATURE Correct"
  113. fi
  114. fi
  115. if [ "$dist" != "IAD_SWISSCOM_RECOVERY" ]; then
  116. exit 2
  117. fi
  118. if [ "$full_ver" != "5.0.0.37_04b20R" ]; then
  119. exit 2
  120. fi
  121. migrate_conf_to_openrg "$full_ver" >> /dev/console 2>&1
  122. echo "write $file:$i image_2 write /dev/zero:0:256 image_1 erase conf_fs $rg_rec_args"
  123. else
  124. log "RMT images are not supported when recovery is running"
  125. exit 2
  126. fi
  127. }
  128. parse_yaps_image()
  129. {
  130. log "Parsing YAPS format..."
  131. magic=$(hexdump -s 0 -n 4 -ve '/1 "%02x"' $file)
  132. tag_ver=$(hexdump -s 4 -n 2 -ve '/1 "%c"' $file)
  133. if [ "$magic" != "79494d47" ] || [ "$tag_ver" != "1" ]; then
  134. return
  135. fi
  136. cpuinfo=$(grep "system type" /proc/cpuinfo | awk '{print $4}')
  137. system_type=$(hexdump -s 6 -n 18 -ve '/1 "%c"' $file)
  138. if [ "$cpuinfo" != "$system_type" ]; then
  139. log "Wrong system type $system_type (expected: $cpuinfo)"
  140. exit 2
  141. fi
  142. dist=$(hexdump -s 24 -n 24 -ve '/1 "%c"' $file)
  143. if [ "$dist" != "$(cat /etc/distro)" ]; then
  144. log "Wrong distribution $dist"
  145. exit 2
  146. fi
  147. if [ -x /usr/sbin/sig_verify ]; then
  148. sig_verify $file 2> /dev/null
  149. ret_code=$?
  150. if [ "$ret_code" = 0 ]; then
  151. log "Signature OK"
  152. else
  153. log "Invalid signature (error: $ret_code)"
  154. exit 2
  155. fi
  156. else
  157. token=$(hexdump $file -s 240 -n 16  -ve '/1 "%02x"')
  158. dd if=/dev/zero of=$file conv=notrunc bs=1 count=16 seek=240
  159. hash=$(md5sum $file | cut -d " " -f1)
  160. if [ "$token" != "$hash" ]; then
  161. log "Error: wrong validation token ($token, $hash)"
  162. exit 2
  163. fi
  164. fi
  165. image1_type=$(hexdump -s 80 -n 4 -ve '/1 "%c"' $file)
  166. image2_type=$(hexdump -s 104 -n 4 -ve '/1 "%c"' $file)
  167. if [ "$image1_type" = "M" ]; then
  168. m_offset=$(hexdump -s 84 -n 10 -ve '/1 "%c"' $file)
  169. m_len=$(hexdump -s 94 -n 10 -ve '/1 "%c"' $file)
  170. m_offset=$(printf "%d" "$m_offset" 2> /dev/null)
  171. m_len=$(printf "%d" "$m_len" 2> /dev/null)
  172. fi
  173. if [ "$image2_type" = "R" ]; then
  174. r_offset=$(hexdump -s 108 -n 10 -ve '/1 "%c"' $file)
  175. r_len=$(hexdump -s 118 -n 10 -ve '/1 "%c"' $file)
  176. r_offset=$(printf "%d" "$r_offset" 2> /dev/null)
  177. r_len=$(printf "%d" "$r_len" 2> /dev/null)
  178. fi
  179. if [ "$current_type" = "main" ]; then
  180. if [ "$image1_type" != "M" ] || [ "$image2_type" != "R" ]; then
  181. log "Error: image must contain a MAIN and a RECOVERY"
  182. exit 2
  183. fi
  184. echo " write $file:$r_offset:$r_len image_2 write $file:$m_offset:$m_len image_1"      
  185. else
  186. if [ "$image1_type" != "M" ]; then
  187. log "Error: image must contain a MAIN"
  188. exit 2
  189. fi
  190. echo " write $file:$m_offset:$m_len image_1"
  191. fi
  192. }
  193. case "$1" in
  194. http://*|https://*)
  195. url_remote=$1
  196. log "loading from remote: $url_remote"
  197. yaft -d "$url_remote" -o /tmp/fw.bin
  198. if [ $? -eq 0 ]; then
  199. file="/tmp/fw.bin"
  200. else
  201. log "Cannot download file from $url_remote"
  202. exit 8
  203. fi
  204. ;;
  205. *)
  206. file=$1
  207. ;;
  208. esac
  209. [ -f "$file" ] || file="/tmp/fw.bin"
  210. killall ec hostapd
  211. mtd_cmd=$(parse_yaps_image)
  212. [ -z "$mtd_cmd" ] && mtd_cmd=$(parse_rmt_image)
  213. if [ -z "$mtd_cmd" ]; then
  214. logger -t "Swisscom" -p 6 "Image not recognized"
  215. log "Image not recognized"
  216. exit 1
  217. fi
  218. cmclient SET Device.DeviceInfo.X_ADB_UpgradeInProgress true >/dev/null
  219. log "Kill HTTP server"
  220. killall -9 nhttpd
  221. log "Kill Config Man"
  222. killall -15 cm
  223. rm -rf /tmp/cfg/cache
  224. if [ -n "$mtd_cmd" ]; then
  225. log "mtd -V -r $mtd_cmd"
  226. mtd -s $MTD_STATUS_FILE -V -r $mtd_cmd >> /dev/console 2>&1
  227. fi
  228. log "Upgrade Failed"
  229. cmclient SET Device.DeviceInfo.X_ADB_UpgradeInProgress false >/dev/null
  230. exit 6
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top