bcm53xx: sysupgrade support for devices with serial flash
[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                         echo "Flashing CHK images in unsupported. Please use only .trx files."
60                         return 1
61                 ;;
62                 "cybertan")
63                         local magic=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
64                         echo "Found CyberTAN image with device magic: $magic"
65                         echo "Flashing CyberTAN images in unsupported. Please use only .trx files."
66                         return 1
67                 ;;
68                 "trx")
69                         return 0
70                 ;;
71                 *)
72                         echo "Invalid image type. Please use only .trx files"
73                         return 1
74                 ;;
75         esac
76 }
77
78 # Extract TRX and use stadard upgrade method
79 platform_do_upgrade_chk() {
80         local header_len=$((0x$(get_magic_long_at "$1" 4)))
81         local trx="/tmp/$1.trx"
82
83         dd if="$1" of="$trx" bs=$header_len skip=1
84         shift
85         platform_do_upgrade_trx "$trx" "$@"
86 }
87
88 # Extract TRX and use stadard upgrade method
89 platform_do_upgrade_cybertan() {
90         local trx="/tmp/$1.trx"
91
92         dd if="$1" of="$trx" bs=32 skip=1
93         shift
94         platform_do_upgrade_trx "$trx" "$@"
95 }
96
97 platform_do_upgrade_trx() {
98         local flash_type=$(platform_flash_type)
99
100         case "$flash_type" in
101                 "serial")
102                         default_do_upgrade "$@"
103                 ;;
104                 "nand")
105                         echo "Firmware upgrade on NAND devices is REALLY unsupported."
106                 ;;
107         esac
108 }
109
110 platform_do_upgrade() {
111         local file_type=$(platform_identify "$1")
112
113         case "$file_type" in
114                 "chk")          platform_do_upgrade_chk "$ARGV";;
115                 "cybertan")     platform_do_upgrade_cybertan "$ARGV";;
116                 *)              platform_do_upgrade_trx "$ARGV";;
117         esac
118 }