platform.sh 4.7 KB

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