brcm47xx: support for flashing CHK and CyberTAN images
[librecmc/librecmc.git] / target / linux / brcm47xx / base-files / lib / upgrade / platform.sh
1 PART_NAME=firmware
2
3 # $(1): file to read magic from
4 # $(2): offset in bytes
5 get_magic_long_at() {
6         dd if="$1" skip=$2 bs=1 count=4 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
7 }
8
9 brcm47xx_identify() {
10         local magic
11
12         magic=$(get_magic_long "$1")
13         case "$magic" in
14                 "48445230")
15                         echo "trx"
16                         return
17                         ;;
18                 "2a23245e")
19                         echo "chk"
20                         return
21                         ;;
22         esac
23
24         magic=$(get_magic_long_at "$1" 14)
25         [ "$magic" = "55324e44" ] && {
26                 echo "cybertan"
27                 return
28         }
29
30         echo "unknown"
31 }
32
33 platform_check_image() {
34         [ "$#" -gt 1 ] && return 1
35
36         local file_type=$(brcm47xx_identify "$1")
37         local magic
38
39         case "$file_type" in
40                 "chk")
41                         local header_len=$((0x$(get_magic_long_at "$1" 4)))
42                         local board_id_len=$(($header_len - 40))
43                         local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
44                         echo "Found CHK image with device board_id $board_id"
45
46                         magic=$(get_magic_long_at "$1" "$header_len")
47                         [ "$magic" != "48445230" ] && {
48                                 echo "No valid TRX firmware in the CHK image"
49                                 return 1
50                         }
51
52                         return 0
53                 ;;
54                 "cybertan")
55                         magic=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
56                         echo "Found CyberTAN image with device magic: $magic"
57
58                         magic=$(get_magic_long_at "$1" 32)
59                         [ "$magic" != "48445230" ] && {
60                                 echo "No valid TRX firmware in the CyberTAN image"
61                                 return 1
62                         }
63
64                         return 0
65                 ;;
66                 "trx")
67                         return 0
68                 ;;
69                 *)
70                         echo "Invalid image type. Please use only .trx files"
71                         return 1
72                 ;;
73         esac
74 }
75
76 platform_do_upgrade_chk() {
77         local header_len=$((0x$(get_magic_long_at "$1" 4)))
78         local trx="/tmp/$1.trx"
79
80         dd if="$1" of="$trx" bs=$header_len skip=1
81         shift
82         default_do_upgrade "$trx" "$@"
83 }
84
85 platform_do_upgrade_cybertan() {
86         local trx="/tmp/$1.trx"
87
88         dd if="$1" of="$trx" bs=32 skip=1
89         shift
90         default_do_upgrade "$trx" "$@"
91 }
92
93 platform_do_upgrade() {
94         local file_type=$(brcm47xx_identify "$1")
95
96         case "$file_type" in
97                 "chk")          platform_do_upgrade_chk "$ARGV";;
98                 "cybertan")     platform_do_upgrade_cybertan "$ARGV";;
99                 *)              default_do_upgrade "$ARGV";;
100         esac
101 }