platform.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #
  2. # Copyright (C) 2011 OpenWrt.org
  3. #
  4. . /lib/functions/system.sh
  5. PART_NAME=firmware
  6. RAMFS_COPY_BIN='nandwrite'
  7. CI_BLKSZ=65536
  8. CI_LDADR=0x80060000
  9. PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD=0
  10. platform_find_partitions() {
  11. local first dev size erasesize name
  12. while read dev size erasesize name; do
  13. name=${name#'"'}; name=${name%'"'}
  14. case "$name" in
  15. vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
  16. if [ -z "$first" ]; then
  17. first="$name"
  18. else
  19. echo "$erasesize:$first:$name"
  20. break
  21. fi
  22. ;;
  23. esac
  24. done < /proc/mtd
  25. }
  26. platform_find_kernelpart() {
  27. local part
  28. for part in "${1%:*}" "${1#*:}"; do
  29. case "$part" in
  30. vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
  31. echo "$part"
  32. break
  33. ;;
  34. esac
  35. done
  36. }
  37. platform_find_rootfspart() {
  38. local part
  39. for part in "${1%:*}" "${1#*:}"; do
  40. [ "$part" != "$2" ] && echo "$part" && break
  41. done
  42. }
  43. platform_do_upgrade_combined() {
  44. local partitions=$(platform_find_partitions)
  45. local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
  46. local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
  47. local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
  48. local kern_blocks=$(($kern_length / $CI_BLKSZ))
  49. local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
  50. if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
  51. [ ${kern_blocks:-0} -gt 0 ] && \
  52. [ ${root_blocks:-0} -gt 0 ] && \
  53. [ ${erase_size:-0} -gt 0 ];
  54. then
  55. local rootfspart=$(platform_find_rootfspart "$partitions" "$kernelpart")
  56. local append=""
  57. [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
  58. if [ "$PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD" -ne 1 ]; then
  59. ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
  60. dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
  61. mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
  62. elif [ -n "$rootfspart" ]; then
  63. dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null | \
  64. mtd write - $kernelpart
  65. dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null | \
  66. mtd -r $append write - $rootfspart
  67. fi
  68. fi
  69. PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD=0
  70. }
  71. tplink_get_image_hwid() {
  72. get_image "$@" | dd bs=4 count=1 skip=16 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  73. }
  74. tplink_get_image_mid() {
  75. get_image "$@" | dd bs=4 count=1 skip=17 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  76. }
  77. tplink_get_image_boot_size() {
  78. get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  79. }
  80. tplink_pharos_check_image() {
  81. local magic_long="$(get_magic_long "$1")"
  82. [ "$magic_long" != "7f454c46" ] && {
  83. echo "Invalid image magic '$magic_long'"
  84. return 1
  85. }
  86. local model_string="$(tplink_pharos_get_model_string)"
  87. local line
  88. # Here $1 is given to dd directly instead of get_image as otherwise the skip
  89. # will take almost a second (as dd can't seek then)
  90. #
  91. # This will fail if the image isn't local, but that's fine: as the
  92. # read loop won't be executed at all, it will return true, so the image
  93. # is accepted (loading the first 1.5M of a remote image for this check seems
  94. # a bit extreme)
  95. dd if="$1" bs=1 skip=1511432 count=1024 2>/dev/null | while read line; do
  96. [ "$line" = "$model_string" ] && break
  97. done || {
  98. echo "Unsupported image (model not in support-list)"
  99. return 1
  100. }
  101. return 0
  102. }
  103. seama_get_type_magic() {
  104. get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  105. }
  106. wrgg_get_image_magic() {
  107. get_image "$@" | dd bs=4 count=1 skip=8 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  108. }
  109. cybertan_get_image_magic() {
  110. get_image "$@" | dd bs=8 count=1 skip=0 2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
  111. }
  112. cybertan_check_image() {
  113. local magic="$(cybertan_get_image_magic "$1")"
  114. local fw_magic="$(cybertan_get_hw_magic)"
  115. [ "$fw_magic" != "$magic" ] && {
  116. echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
  117. return 1
  118. }
  119. return 0
  120. }
  121. platform_do_upgrade_compex() {
  122. local fw_file=$1
  123. local fw_part=$PART_NAME
  124. local fw_mtd=$(find_mtd_part $fw_part)
  125. local fw_length=0x$(dd if="$fw_file" bs=2 skip=1 count=4 2>/dev/null)
  126. local fw_blocks=$(($fw_length / 65536))
  127. if [ -n "$fw_mtd" ] && [ ${fw_blocks:-0} -gt 0 ]; then
  128. local append=""
  129. [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
  130. sync
  131. dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
  132. mtd $append write - "$fw_part"
  133. fi
  134. }
  135. alfa_check_image() {
  136. local magic_long="$(get_magic_long "$1")"
  137. local fw_part_size=$(mtd_get_part_size firmware)
  138. case "$magic_long" in
  139. "27051956")
  140. [ "$fw_part_size" != "16318464" ] && {
  141. echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
  142. return 1
  143. }
  144. ;;
  145. "68737173")
  146. [ "$fw_part_size" != "7929856" ] && {
  147. echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
  148. return 1
  149. }
  150. ;;
  151. esac
  152. return 0
  153. }
  154. platform_check_image() {
  155. local board=$(board_name)
  156. local magic="$(get_magic_word "$1")"
  157. local magic_long="$(get_magic_long "$1")"
  158. [ "$#" -gt 1 ] && return 1
  159. case "$board" in
  160. "ubnt,unifi")
  161. [ "$magic" != "2705" ] && {
  162. echo "Invalid image type."
  163. return 1
  164. }
  165. return 0
  166. ;;
  167. esac
  168. echo "Sysupgrade is not yet supported on $board."
  169. return 1
  170. }
  171. platform_do_upgrade() {
  172. local board=$(board_name)
  173. case "$board" in
  174. *)
  175. default_do_upgrade "$ARGV"
  176. ;;
  177. esac
  178. }
  179. disable_watchdog() {
  180. killall watchdog
  181. ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
  182. echo 'Could not disable watchdog'
  183. return 1
  184. }
  185. }
  186. append sysupgrade_pre_upgrade disable_watchdog