bcm53xx: support flashing CHK and CyberTAN images
[openwrt/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_flash_type() {
10 # On NAND devices "rootfs" is UBI volume, so won't be find in /proc/mtd
11 grep -q "\"rootfs\"" /proc/mtd && {
12 echo "serial"
13 return
14 }
15
16 echo "nand"
17 }
18
19 platform_identify() {
20 local magic
21
22 magic=$(get_magic_long "$1")
23 case "$magic" in
24 "48445230")
25 echo "trx"
26 return
27 ;;
28 "2a23245e")
29 echo "chk"
30 return
31 ;;
32 esac
33
34 magic=$(get_magic_long_at "$1" 14)
35 [ "$magic" = "55324e44" ] && {
36 echo "cybertan"
37 return
38 }
39
40 echo "unknown"
41 }
42
43 platform_check_image() {
44 [ "$#" -gt 1 ] && return 1
45
46 [ "$(platform_flash_type)" = "nand" ] && {
47 echo "Firmware upgrade on NAND devices is not implemented."
48 return 1
49 }
50
51 local file_type=$(platform_identify "$1")
52
53 case "$file_type" in
54 "chk")
55 local header_len=$((0x$(get_magic_long_at "$1" 4)))
56 local board_id_len=$(($header_len - 40))
57 local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
58 echo "Found CHK image with device board_id $board_id"
59
60 magic=$(get_magic_long_at "$1" "$header_len")
61 [ "$magic" != "48445230" ] && {
62 echo "No valid TRX firmware in the CHK image"
63 return 1
64 }
65
66 return 0
67 ;;
68 "cybertan")
69 local magic=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
70 echo "Found CyberTAN image with device magic: $magic"
71
72 magic=$(get_magic_long_at "$1" 32)
73 [ "$magic" != "48445230" ] && {
74 echo "No valid TRX firmware in the CyberTAN image"
75 return 1
76 }
77
78 return 0
79 ;;
80 "trx")
81 return 0
82 ;;
83 *)
84 echo "Invalid image type. Please use only .trx files"
85 return 1
86 ;;
87 esac
88 }
89
90 # Extract TRX and use stadard upgrade method
91 platform_do_upgrade_chk() {
92 local header_len=$((0x$(get_magic_long_at "$1" 4)))
93 local trx="/tmp/$1.trx"
94
95 dd if="$1" of="$trx" bs=$header_len skip=1
96 shift
97 platform_do_upgrade_trx "$trx" "$@"
98 }
99
100 # Extract TRX and use stadard upgrade method
101 platform_do_upgrade_cybertan() {
102 local trx="/tmp/$1.trx"
103
104 dd if="$1" of="$trx" bs=32 skip=1
105 shift
106 platform_do_upgrade_trx "$trx" "$@"
107 }
108
109 platform_do_upgrade_trx() {
110 local flash_type=$(platform_flash_type)
111
112 case "$flash_type" in
113 "serial")
114 default_do_upgrade "$@"
115 ;;
116 "nand")
117 echo "Firmware upgrade on NAND devices is REALLY unsupported."
118 ;;
119 esac
120 }
121
122 platform_do_upgrade() {
123 local file_type=$(platform_identify "$1")
124
125 case "$file_type" in
126 "chk") platform_do_upgrade_chk "$ARGV";;
127 "cybertan") platform_do_upgrade_cybertan "$ARGV";;
128 *) platform_do_upgrade_trx "$ARGV";;
129 esac
130 }