bcm53xx: switch to the otrx for verifying TRX images
[openwrt.git] / target / linux / bcm53xx / 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 -e '1/1 "%02x"'
7 }
8
9 platform_machine() {
10         cat /proc/device-tree/compatible | tr '\0' '\t' | cut -f 1
11 }
12
13 platform_flash_type() {
14         # On NAND devices "rootfs" is UBI volume, so won't be find in /proc/mtd
15         grep -q "\"rootfs\"" /proc/mtd && {
16                 echo "serial"
17                 return
18         }
19
20         echo "nand"
21 }
22
23 platform_expected_image() {
24         local machine=$(platform_machine)
25
26         case "$machine" in
27                 "netgear,r6250v1")      echo "chk U12H245T00_NETGEAR"; return;;
28                 "netgear,r6300v2")      echo "chk U12H240T00_NETGEAR"; return;;
29                 "netgear,r8000")        echo "chk U12H315T00_NETGEAR"; return;;
30         esac
31 }
32
33 platform_identify() {
34         local magic
35
36         magic=$(get_magic_long "$1")
37         case "$magic" in
38                 "48445230")
39                         echo "trx"
40                         return
41                         ;;
42                 "2a23245e")
43                         echo "chk"
44                         return
45                         ;;
46         esac
47
48         magic=$(get_magic_long_at "$1" 14)
49         [ "$magic" = "55324e44" ] && {
50                 echo "cybertan"
51                 return
52         }
53
54         echo "unknown"
55 }
56
57 platform_check_image() {
58         [ "$#" -gt 1 ] && return 1
59
60         [ "$(platform_flash_type)" = "nand" ] && {
61                 echo "Firmware upgrade on NAND devices is not implemented."
62                 return 1
63         }
64
65         local file_type=$(platform_identify "$1")
66         local magic
67         local error=0
68
69         case "$file_type" in
70                 "chk")
71                         local header_len=$((0x$(get_magic_long_at "$1" 4)))
72                         local board_id_len=$(($header_len - 40))
73                         local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
74                         local dev_board_id=$(platform_expected_image)
75                         echo "Found CHK image with device board_id $board_id"
76
77                         [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
78                                 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
79                                 error=1
80                         }
81
82                         if ! otrx -c "$1" -o "$header_len"; then
83                                 echo "No valid TRX firmware in the CHK image"
84                                 error=1
85                         fi
86                 ;;
87                 "cybertan")
88                         local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
89                         local dev_pattern=$(platform_expected_image)
90                         echo "Found CyberTAN image with device pattern: $pattern"
91
92                         [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
93                                 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
94                                 error=1
95                         }
96
97                         if ! otrx -c "$1" -o 32; then
98                                 echo "No valid TRX firmware in the CyberTAN image"
99                                 error=1
100                         fi
101                 ;;
102                 "trx")
103                         if ! otrx -c "$1"; then
104                                 echo "Invalid (corrupted?) TRX firmware"
105                                 error=1
106                         fi
107                 ;;
108                 *)
109                         echo "Invalid image type. Please use only .trx files"
110                         error=1
111                 ;;
112         esac
113
114         return $error
115 }
116
117 # Extract TRX and use stadard upgrade method
118 platform_do_upgrade_chk() {
119         local header_len=$((0x$(get_magic_long_at "$1" 4)))
120         local trx="/tmp/$1.trx"
121
122         dd if="$1" of="$trx" bs=$header_len skip=1
123         shift
124         platform_do_upgrade_trx "$trx" "$@"
125 }
126
127 # Extract TRX and use stadard upgrade method
128 platform_do_upgrade_cybertan() {
129         local trx="/tmp/$1.trx"
130
131         dd if="$1" of="$trx" bs=32 skip=1
132         shift
133         platform_do_upgrade_trx "$trx" "$@"
134 }
135
136 platform_do_upgrade_trx() {
137         local flash_type=$(platform_flash_type)
138
139         case "$flash_type" in
140                 "serial")
141                         default_do_upgrade "$@"
142                 ;;
143                 "nand")
144                         echo "Firmware upgrade on NAND devices is REALLY unsupported."
145                 ;;
146         esac
147 }
148
149 platform_do_upgrade() {
150         local file_type=$(platform_identify "$1")
151
152         case "$file_type" in
153                 "chk")          platform_do_upgrade_chk "$ARGV";;
154                 "cybertan")     platform_do_upgrade_cybertan "$ARGV";;
155                 *)              platform_do_upgrade_trx "$ARGV";;
156         esac
157 }