platform.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 -n 4 -e '1/1 "%02x"'
  6. }
  7. platform_machine() {
  8. grep "machine" /proc/cpuinfo | sed "s/.*:[ \t]*//"
  9. }
  10. platform_expected_image() {
  11. local machine=$(platform_machine)
  12. case "$machine" in
  13. "Netgear WGR614 V8") echo "chk U12H072T00_NETGEAR"; return;;
  14. "Netgear WGR614 V9") echo "chk U12H094T00_NETGEAR"; return;;
  15. "Netgear WGR614 V10") echo "chk U12H139T01_NETGEAR"; return;;
  16. "Netgear WNDR3300") echo "chk U12H093T00_NETGEAR"; return;;
  17. "Netgear WNDR3400 V1") echo "chk U12H155T00_NETGEAR"; return;;
  18. "Netgear WNDR3400 V2") echo "chk U12H187T00_NETGEAR"; return;;
  19. "Netgear WNDR3400 V3") echo "chk U12H208T00_NETGEAR"; return;;
  20. "Netgear WNDR3400 Vcna") echo "chk U12H155T01_NETGEAR"; return;;
  21. "Netgear WNDR3700 V3") echo "chk U12H194T00_NETGEAR"; return;;
  22. "Netgear WNDR4000") echo "chk U12H181T00_NETGEAR"; return;;
  23. "Netgear WNDR4500 V1") echo "chk U12H189T00_NETGEAR"; return;;
  24. "Netgear WNDR4500 V2") echo "chk U12H224T00_NETGEAR"; return;;
  25. "Netgear WNR2000 V2") echo "chk U12H114T00_NETGEAR"; return;;
  26. "Netgear WNR3500L") echo "chk U12H136T99_NETGEAR"; return;;
  27. "Netgear WNR3500U") echo "chk U12H136T00_NETGEAR"; return;;
  28. "Netgear WNR3500 V2") echo "chk U12H127T00_NETGEAR"; return;;
  29. "Netgear WNR3500 V2vc") echo "chk U12H127T70_NETGEAR"; return;;
  30. "Netgear WNR834B V2") echo "chk U12H081T00_NETGEAR"; return;;
  31. "Linksys E900 V1") echo "cybertan E900"; return;;
  32. "Linksys E1000 V1") echo "cybertan E100"; return;;
  33. "Linksys E1000 V2") echo "cybertan E100"; return;;
  34. "Linksys E1000 V2.1") echo "cybertan E100"; return;;
  35. "Linksys E1200 V2") echo "cybertan E122"; return;;
  36. "Linksys E2000 V1") echo "cybertan 32XN"; return;;
  37. "Linksys E3000 V1") echo "cybertan 61XN"; return;;
  38. "Linksys E3200 V1") echo "cybertan 3200"; return;;
  39. "Linksys E4200 V1") echo "cybertan 4200"; return;;
  40. "Linksys WRT150N V1.1") echo "cybertan N150"; return;;
  41. "Linksys WRT150N V1") echo "cybertan N150"; return;;
  42. "Linksys WRT160N V1") echo "cybertan N150"; return;;
  43. "Linksys WRT160N V3") echo "cybertan N150"; return;;
  44. "Linksys WRT300N V1") echo "cybertan EWCB"; return;;
  45. "Linksys WRT300N V1.1") echo "cybertan EWC2"; return;;
  46. "Linksys WRT310N V1") echo "cybertan 310N"; return;;
  47. "Linksys WRT310N V2") echo "cybertan 310N"; return;;
  48. "Linksys WRT610N V1") echo "cybertan 610N"; return;;
  49. "Linksys WRT610N V2") echo "cybertan 610N"; return;;
  50. esac
  51. }
  52. brcm47xx_identify() {
  53. local magic
  54. magic=$(get_magic_long "$1")
  55. case "$magic" in
  56. "48445230")
  57. echo "trx"
  58. return
  59. ;;
  60. "2a23245e")
  61. echo "chk"
  62. return
  63. ;;
  64. esac
  65. magic=$(get_magic_long_at "$1" 14)
  66. [ "$magic" = "55324e44" ] && {
  67. echo "cybertan"
  68. return
  69. }
  70. echo "unknown"
  71. }
  72. platform_check_image() {
  73. [ "$#" -gt 1 ] && return 1
  74. local file_type=$(brcm47xx_identify "$1")
  75. local magic
  76. local error=0
  77. case "$file_type" in
  78. "chk")
  79. local header_len=$((0x$(get_magic_long_at "$1" 4)))
  80. local board_id_len=$(($header_len - 40))
  81. local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
  82. local dev_board_id=$(platform_expected_image)
  83. echo "Found CHK image with device board_id $board_id"
  84. [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
  85. echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
  86. error=1
  87. }
  88. if ! otrx check "$1" -o "$header_len"; then
  89. echo "No valid TRX firmware in the CHK image"
  90. error=1
  91. fi
  92. ;;
  93. "cybertan")
  94. local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
  95. local dev_pattern=$(platform_expected_image)
  96. echo "Found CyberTAN image with device pattern: $pattern"
  97. [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
  98. echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
  99. error=1
  100. }
  101. if ! otrx check "$1" -o 32; then
  102. echo "No valid TRX firmware in the CyberTAN image"
  103. error=1
  104. fi
  105. ;;
  106. "trx")
  107. if ! otrx check "$1"; then
  108. echo "Invalid (corrupted?) TRX firmware"
  109. error=1
  110. fi
  111. ;;
  112. *)
  113. echo "Invalid image type. Please use only .trx files"
  114. error=1
  115. ;;
  116. esac
  117. return $error
  118. }
  119. platform_trx_from_chk_cmd() {
  120. local header_len=$((0x$(get_magic_long_at "$1" 4)))
  121. echo -n dd bs=$header_len skip=1
  122. }
  123. platform_trx_from_cybertan_cmd() {
  124. echo -n dd bs=32 skip=1
  125. }
  126. platform_do_upgrade() {
  127. local file_type=$(brcm47xx_identify "$1")
  128. local trx="$1"
  129. local cmd=""
  130. case "$file_type" in
  131. "chk") cmd=$(platform_trx_from_chk_cmd "$trx");;
  132. "cybertan") cmd=$(platform_trx_from_cybertan_cmd "$trx");;
  133. esac
  134. default_do_upgrade "$trx" "$cmd"
  135. }