platform.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. brcm47xx_identify() {
  8. local magic
  9. magic=$(get_magic_long "$1")
  10. case "$magic" in
  11. "48445230")
  12. echo "trx"
  13. return
  14. ;;
  15. "2a23245e")
  16. echo "chk"
  17. return
  18. ;;
  19. esac
  20. magic=$(get_magic_long_at "$1" 14)
  21. [ "$magic" = "55324e44" ] && {
  22. echo "cybertan"
  23. return
  24. }
  25. echo "unknown"
  26. }
  27. platform_check_image() {
  28. [ "$#" -gt 1 ] && return 1
  29. local file_type=$(brcm47xx_identify "$1")
  30. local magic
  31. case "$file_type" in
  32. "chk")
  33. local header_len=$((0x$(get_magic_long_at "$1" 4)))
  34. local board_id_len=$(($header_len - 40))
  35. local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
  36. echo "Found CHK image with device board_id $board_id"
  37. magic=$(get_magic_long_at "$1" "$header_len")
  38. [ "$magic" != "48445230" ] && {
  39. echo "No valid TRX firmware in the CHK image"
  40. return 1
  41. }
  42. return 0
  43. ;;
  44. "cybertan")
  45. magic=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
  46. echo "Found CyberTAN image with device magic: $magic"
  47. magic=$(get_magic_long_at "$1" 32)
  48. [ "$magic" != "48445230" ] && {
  49. echo "No valid TRX firmware in the CyberTAN image"
  50. return 1
  51. }
  52. return 0
  53. ;;
  54. "trx")
  55. return 0
  56. ;;
  57. *)
  58. echo "Invalid image type. Please use only .trx files"
  59. return 1
  60. ;;
  61. esac
  62. }
  63. platform_do_upgrade_chk() {
  64. local header_len=$((0x$(get_magic_long_at "$1" 4)))
  65. local trx="/tmp/$1.trx"
  66. dd if="$1" of="$trx" bs=$header_len skip=1
  67. shift
  68. default_do_upgrade "$trx" "$@"
  69. }
  70. platform_do_upgrade_cybertan() {
  71. local trx="/tmp/$1.trx"
  72. dd if="$1" of="$trx" bs=32 skip=1
  73. shift
  74. default_do_upgrade "$trx" "$@"
  75. }
  76. platform_do_upgrade() {
  77. local file_type=$(brcm47xx_identify "$1")
  78. case "$file_type" in
  79. "chk") platform_do_upgrade_chk "$ARGV";;
  80. "cybertan") platform_do_upgrade_cybertan "$ARGV";;
  81. *) default_do_upgrade "$ARGV";;
  82. esac
  83. }