dir825.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2012 OpenWrt.org
  4. #
  5. . /lib/functions.sh
  6. . /lib/ar71xx.sh
  7. get_magic_at() {
  8. local mtddev=$1
  9. local pos=$2
  10. dd bs=1 count=2 skip=$pos if=$mtddev 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  11. }
  12. dir825b_is_caldata_valid() {
  13. local mtddev=$1
  14. local magic
  15. magic=$(get_magic_at $mtddev 4096)
  16. [ "$magic" != "a55a" ] && return 0
  17. magic=$(get_magic_at $mtddev 20480)
  18. [ "$magic" != "a55a" ] && return 0
  19. return 1
  20. }
  21. dir825b_copy_caldata() {
  22. local cal_src=$1
  23. local cal_dst=$2
  24. local mtd_src
  25. local mtd_dst
  26. local md5_src
  27. local md5_dst
  28. mtd_src=$(find_mtd_part $cal_src)
  29. [ -z "$mtd_src" ] && {
  30. echo "no $cal_src partition found"
  31. return 1
  32. }
  33. mtd_dst=$(find_mtd_part $cal_dst)
  34. [ -z "$mtd_dst" ] && {
  35. echo "no $cal_dst partition found"
  36. return 1
  37. }
  38. dir825b_is_caldata_valid "$mtd_src" && {
  39. echo "no valid calibration data found in $cal_src"
  40. return 1
  41. }
  42. dir825b_is_caldata_valid "$mtd_dst" && {
  43. echo "Copying calibration data from $cal_src to $cal_dst..."
  44. dd if="$mtd_src" 2>/dev/null | mtd -q -q write - "$cal_dst"
  45. }
  46. md5_src=$(md5sum "$mtd_src") && md5_src="${md5_src%% *}"
  47. md5_dst=$(md5sum "$mtd_dst") && md5_dst="${md5_dst%% *}"
  48. [ "$md5_src" != "$md5_dst" ] && {
  49. echo "calibration data mismatch $cal_src:$md5_src $cal_dst:$md5_dst"
  50. return 1
  51. }
  52. return 0
  53. }
  54. dir825b_do_upgrade_combined() {
  55. local fw_part=$1
  56. local fw_file=$2
  57. local fw_mtd=$(find_mtd_part $fw_part)
  58. local fw_length=0x$(dd if="$fw_file" bs=2 skip=1 count=4 2>/dev/null)
  59. local fw_blocks=$(($fw_length / 65536))
  60. if [ -n "$fw_mtd" ] && [ ${fw_blocks:-0} -gt 0 ]; then
  61. local append=""
  62. [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
  63. sync
  64. dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
  65. mtd $append write - "$fw_part"
  66. fi
  67. }
  68. dir825b_check_image() {
  69. local magic="$(get_magic_long "$1")"
  70. local fw_mtd=$(find_mtd_part "firmware_orig")
  71. case "$magic" in
  72. "27051956")
  73. ;;
  74. "43493030")
  75. local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
  76. local md5_chk=$(dd if="$1" bs=64k skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
  77. local fw_len=$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
  78. local fw_part_len=$(mtd_get_part_size "firmware")
  79. if [ -z "$fw_mtd" ]; then
  80. ask_bool 0 "Do you have a backup of the caldata partition?" || {
  81. echo "Warning, please make sure that you have a backup of the caldata partition."
  82. echo "Once you have that, use 'sysupgrade -i' for upgrading to the 'fat' firmware."
  83. return 1
  84. }
  85. fi
  86. if [ -z "$md5_img" -o -z "$md5_chk" ]; then
  87. echo "Unable to get image checksums. Maybe you are using a streamed image?"
  88. return 1
  89. fi
  90. if [ "$md5_img" != "$md5_chk" ]; then
  91. echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
  92. return 1
  93. fi
  94. fw_len=$((0x$fw_len))
  95. fw_part_len=${fw_part_len:-0}
  96. if [ $fw_part_len -lt $fw_len ]; then
  97. echo "The upgrade image is too big (size:$fw_len available:$fw_part_len)"
  98. return 1
  99. fi
  100. ;;
  101. *)
  102. echo "Unsupported image format."
  103. return 1
  104. ;;
  105. esac
  106. return 0
  107. }
  108. platform_do_upgrade_dir825b() {
  109. local magic="$(get_magic_long "$1")"
  110. local fw_mtd=$(find_mtd_part "firmware_orig")
  111. case "$magic" in
  112. "27051956")
  113. if [ -n "$fw_mtd" ]; then
  114. # restore calibration data before downgrading to
  115. # the normal image
  116. dir825b_copy_caldata "caldata" "caldata_orig" || {
  117. echo "unable to restore calibration data"
  118. exit 1
  119. }
  120. PART_NAME="firmware_orig"
  121. else
  122. PART_NAME="firmware"
  123. fi
  124. default_do_upgrade "$ARGV"
  125. ;;
  126. "43493030")
  127. if [ -z "$fw_mtd" ]; then
  128. # backup calibration data before upgrading to the
  129. # fat image
  130. dir825b_copy_caldata "caldata" "caldata_copy" || {
  131. echo "unable to backup calibration data"
  132. exit 1
  133. }
  134. fi
  135. dir825b_do_upgrade_combined "firmware" "$ARGV"
  136. ;;
  137. esac
  138. }