platform.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. PART_NAME=firmware
  2. # $(1): file to read magic from
  3. # $(2): offset in bytes
  4. get_magic_long_at() {
  5. dd if="$1" skip=$2 bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%02x"'
  6. }
  7. platform_machine() {
  8. cat /proc/device-tree/compatible | tr '\0' '\t' | cut -f 1
  9. }
  10. platform_flash_type() {
  11. # On NAND devices "rootfs" is UBI volume, so won't be find in /proc/mtd
  12. grep -q "\"rootfs\"" /proc/mtd && {
  13. echo "serial"
  14. return
  15. }
  16. echo "nand"
  17. }
  18. platform_expected_image() {
  19. local machine=$(platform_machine)
  20. case "$machine" in
  21. "netgear,r6250v1") echo "chk U12H245T00_NETGEAR"; return;;
  22. "netgear,r6300v2") echo "chk U12H240T00_NETGEAR"; return;;
  23. "netgear,r7000") echo "chk U12H270T00_NETGEAR"; return;;
  24. "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
  25. "netgear,r8500") echo "chk U12H334T00_NETGEAR"; return;;
  26. esac
  27. }
  28. platform_identify() {
  29. local magic
  30. magic=$(get_magic_long "$1")
  31. case "$magic" in
  32. "48445230")
  33. echo "trx"
  34. return
  35. ;;
  36. "2a23245e")
  37. echo "chk"
  38. return
  39. ;;
  40. esac
  41. magic=$(get_magic_long_at "$1" 14)
  42. [ "$magic" = "55324e44" ] && {
  43. echo "cybertan"
  44. return
  45. }
  46. echo "unknown"
  47. }
  48. platform_check_image() {
  49. [ "$#" -gt 1 ] && return 1
  50. local file_type=$(platform_identify "$1")
  51. local magic
  52. local error=0
  53. case "$file_type" in
  54. "chk")
  55. local header_len=$((0x$(get_magic_long_at "$1" 4)))
  56. local board_id_len=$(($header_len - 40))
  57. local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
  58. local dev_board_id=$(platform_expected_image)
  59. echo "Found CHK image with device board_id $board_id"
  60. [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
  61. echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
  62. error=1
  63. }
  64. if ! otrx check "$1" -o "$header_len"; then
  65. echo "No valid TRX firmware in the CHK image"
  66. error=1
  67. fi
  68. ;;
  69. "cybertan")
  70. local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
  71. local dev_pattern=$(platform_expected_image)
  72. echo "Found CyberTAN image with device pattern: $pattern"
  73. [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
  74. echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
  75. error=1
  76. }
  77. if ! otrx check "$1" -o 32; then
  78. echo "No valid TRX firmware in the CyberTAN image"
  79. error=1
  80. fi
  81. ;;
  82. "trx")
  83. if ! otrx check "$1"; then
  84. echo "Invalid (corrupted?) TRX firmware"
  85. error=1
  86. fi
  87. ;;
  88. *)
  89. echo "Invalid image type. Please use only .trx files"
  90. error=1
  91. ;;
  92. esac
  93. return $error
  94. }
  95. platform_pre_upgrade() {
  96. local file_type=$(platform_identify "$1")
  97. local dir="/tmp/sysupgrade-bcm53xx"
  98. local trx="$1"
  99. local offset
  100. [ "$(platform_flash_type)" != "nand" ] && return
  101. # Find trx offset
  102. case "$file_type" in
  103. "chk") offset=$((0x$(get_magic_long_at "$1" 4)));;
  104. "cybertan") offset=32;;
  105. esac
  106. # Extract partitions from trx
  107. rm -fR $dir
  108. mkdir -p $dir
  109. otrx extract "$trx" \
  110. ${offset:+-o $offset} \
  111. -1 $dir/kernel \
  112. -2 $dir/root
  113. [ $? -ne 0 ] && {
  114. echo "Failed to extract TRX partitions."
  115. return
  116. }
  117. # Firmwares without UBI image should be flashed "normally"
  118. local root_type=$(identify $dir/root)
  119. [ "$root_type" != "ubi" ] && {
  120. echo "Provided firmware doesn't use UBI for rootfs."
  121. return
  122. }
  123. # Prepare TRX file with just a kernel that will replace current one
  124. local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
  125. [ -z "$linux_length" ] && {
  126. echo "Unable to find \"linux\" partition size"
  127. exit 1
  128. }
  129. linux_length=$((0x$linux_length))
  130. local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
  131. [ $kernel_length -gt $linux_length ] && {
  132. echo "New kernel doesn't fit \"linux\" partition."
  133. return
  134. }
  135. rm -f /tmp/null.bin
  136. rm -f /tmp/kernel.trx
  137. touch /tmp/null.bin
  138. otrx create /tmp/kernel.trx \
  139. -f $dir/kernel -b $(($linux_length + 28)) \
  140. -f /tmp/null.bin
  141. [ $? -ne 0 ] && {
  142. echo "Failed to create simple TRX with new kernel."
  143. return
  144. }
  145. # Prepare UBI image (drop unwanted extra blocks)
  146. local ubi_length=0
  147. while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
  148. ubi_length=$(($ubi_length + 131072))
  149. done
  150. dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
  151. [ $? -ne 0 ] && {
  152. echo "Failed to prepare new UBI image."
  153. return
  154. }
  155. # Flash
  156. mtd write /tmp/kernel.trx firmware
  157. nand_do_upgrade /tmp/root.ubi
  158. }
  159. platform_trx_from_chk_cmd() {
  160. local header_len=$((0x$(get_magic_long_at "$1" 4)))
  161. echo -n dd bs=$header_len skip=1
  162. }
  163. platform_trx_from_cybertan_cmd() {
  164. echo -n dd bs=32 skip=1
  165. }
  166. platform_do_upgrade() {
  167. local file_type=$(platform_identify "$1")
  168. local trx="$1"
  169. local cmd=
  170. [ "$(platform_flash_type)" == "nand" ] && {
  171. echo "Writing whole image to NAND flash. All erase counters will be lost."
  172. }
  173. case "$file_type" in
  174. "chk") cmd=$(platform_trx_from_chk_cmd "$trx");;
  175. "cybertan") cmd=$(platform_trx_from_cybertan_cmd "$trx");;
  176. esac
  177. default_do_upgrade "$trx" "$cmd"
  178. }