bcm53xx: sysupgrade: drop useless shift in platform.sh
[openwrt/staging/chunkeey.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 local file_type=$(platform_identify "$1")
61 local magic
62 local error=0
63
64 case "$file_type" in
65 "chk")
66 local header_len=$((0x$(get_magic_long_at "$1" 4)))
67 local board_id_len=$(($header_len - 40))
68 local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
69 local dev_board_id=$(platform_expected_image)
70 echo "Found CHK image with device board_id $board_id"
71
72 [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
73 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
74 error=1
75 }
76
77 if ! otrx check "$1" -o "$header_len"; then
78 echo "No valid TRX firmware in the CHK image"
79 error=1
80 fi
81 ;;
82 "cybertan")
83 local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
84 local dev_pattern=$(platform_expected_image)
85 echo "Found CyberTAN image with device pattern: $pattern"
86
87 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
88 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
89 error=1
90 }
91
92 if ! otrx check "$1" -o 32; then
93 echo "No valid TRX firmware in the CyberTAN image"
94 error=1
95 fi
96 ;;
97 "trx")
98 if ! otrx check "$1"; then
99 echo "Invalid (corrupted?) TRX firmware"
100 error=1
101 fi
102 ;;
103 *)
104 echo "Invalid image type. Please use only .trx files"
105 error=1
106 ;;
107 esac
108
109 return $error
110 }
111
112 platform_pre_upgrade() {
113 local file_type=$(platform_identify "$1")
114 local dir="/tmp/sysupgrade-bcm53xx"
115 local trx="$1"
116 local offset
117
118 [ "$(platform_flash_type)" != "nand" ] && return
119
120 # Find trx offset
121 case "$file_type" in
122 "chk") offset=$((0x$(get_magic_long_at "$1" 4)));;
123 "cybertan") offset=32;;
124 esac
125
126 # Extract partitions from trx
127 rm -fR $dir
128 mkdir -p $dir
129 otrx extract "$trx" \
130 ${offset:+-o $offset} \
131 -1 $dir/kernel \
132 -2 $dir/root
133
134 # Firmwares without UBI image should be flashed "normally"
135 local root_type=$(identify $dir/root)
136 [ "$root_type" != "ubi" ] && {
137 echo "Provided firmware doesn't use UBI for rootfs."
138 return
139 }
140
141 # Prepare TRX file with just a kernel that will replace current one
142 local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
143 [ -z "$linux_length" ] && {
144 echo "Unable to find \"linux\" partition size"
145 exit 1
146 }
147 linux_length=$((0x$linux_length))
148 local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
149 [ $kernel_length -gt $linux_length ] && {
150 echo "New kernel doesn't fit \"linux\" partition."
151 return
152 }
153 rm -f /tmp/null.bin
154 rm -f /tmp/kernel.trx
155 touch /tmp/null.bin
156 otrx create /tmp/kernel.trx \
157 -f $dir/kernel -b $(($linux_length + 28)) \
158 -f /tmp/null.bin
159
160 # Prepare UBI image (drop unwanted extra blocks)
161 local ubi_length=0
162 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
163 ubi_length=$(($ubi_length + 131072))
164 done
165 dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
166
167 # Flash
168 mtd write /tmp/kernel.trx firmware
169 nand_do_upgrade /tmp/root.ubi
170 }
171
172 platform_trx_from_chk_cmd() {
173 local header_len=$((0x$(get_magic_long_at "$1" 4)))
174
175 echo -n dd bs=$header_len skip=1
176 }
177
178 platform_trx_from_cybertan_cmd() {
179 echo -n dd bs=32 skip=1
180 }
181
182 platform_do_upgrade() {
183 local file_type=$(platform_identify "$1")
184 local trx="$1"
185 local cmd=
186
187 [ "$(platform_flash_type)" == "nand" ] && {
188 echo "Writing whole image to NAND flash. All erase counters will be lost."
189 }
190
191 case "$file_type" in
192 "chk") cmd=$(platform_trx_from_chk_cmd "$trx");;
193 "cybertan") cmd=$(platform_trx_from_cybertan_cmd "$trx");;
194 esac
195
196 default_do_upgrade "$trx" "$cmd"
197 }