bcm53xx: support Netgear R8500 in sysupgrade
[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_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 "dlink,dir-885l") echo "seama wrgac42_dlink.2015_dir885l"; return;;
28 "netgear,r6250v1") echo "chk U12H245T00_NETGEAR"; return;;
29 "netgear,r6300v2") echo "chk U12H240T00_NETGEAR"; return;;
30 "netgear,r7000") echo "chk U12H270T00_NETGEAR"; return;;
31 "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
32 "netgear,r8500") echo "chk U12H334T00_NETGEAR"; return;;
33 esac
34 }
35
36 platform_identify() {
37 local magic
38
39 magic=$(get_magic_long "$1")
40 case "$magic" in
41 "48445230")
42 echo "trx"
43 return
44 ;;
45 "2a23245e")
46 echo "chk"
47 return
48 ;;
49 "5ea3a417")
50 echo "seama"
51 return
52 ;;
53 esac
54
55 magic=$(get_magic_long_at "$1" 14)
56 [ "$magic" = "55324e44" ] && {
57 echo "cybertan"
58 return
59 }
60
61 echo "unknown"
62 }
63
64 platform_check_image() {
65 [ "$#" -gt 1 ] && return 1
66
67 local file_type=$(platform_identify "$1")
68 local magic
69 local error=0
70
71 case "$file_type" in
72 "chk")
73 local header_len=$((0x$(get_magic_long_at "$1" 4)))
74 local board_id_len=$(($header_len - 40))
75 local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
76 local dev_board_id=$(platform_expected_image)
77 echo "Found CHK image with device board_id $board_id"
78
79 [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
80 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
81 error=1
82 }
83
84 if ! otrx check "$1" -o "$header_len"; then
85 echo "No valid TRX firmware in the CHK image"
86 error=1
87 fi
88 ;;
89 "cybertan")
90 local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
91 local dev_pattern=$(platform_expected_image)
92 echo "Found CyberTAN image with device pattern: $pattern"
93
94 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
95 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
96 error=1
97 }
98
99 if ! otrx check "$1" -o 32; then
100 echo "No valid TRX firmware in the CyberTAN image"
101 error=1
102 fi
103 ;;
104 "seama")
105 local img_signature=$(oseama info "$1" | grep "Meta entry:.*signature=" | sed "s/.*=//")
106 local dev_signature=$(platform_expected_image)
107 echo "Found Seama image with device signature: $img_signature"
108
109 [ -n "$dev_signature" -a "seama $img_signature" != "$dev_signature" ] && {
110 echo "Firmware signature doesn't match device signature ($dev_signature)"
111 error=1
112 }
113
114 $(oseama info "$1" -e 0 | grep -q "Meta entry:.*type=firmware") || {
115 echo "Seama container doesn't have firmware entity"
116 error=1
117 }
118 ;;
119 "trx")
120 if ! otrx check "$1"; then
121 echo "Invalid (corrupted?) TRX firmware"
122 error=1
123 fi
124 ;;
125 *)
126 echo "Invalid image type. Please use only .trx files"
127 error=1
128 ;;
129 esac
130
131 return $error
132 }
133
134 platform_pre_upgrade() {
135 export RAMFS_COPY_BIN="${RAMFS_COPY_BIN} /usr/bin/oseama /bin/sed"
136
137 local file_type=$(platform_identify "$1")
138 local dir="/tmp/sysupgrade-bcm53xx"
139 local trx="$1"
140 local offset
141
142 [ "$(platform_flash_type)" != "nand" ] && return
143
144 # Find trx offset
145 case "$file_type" in
146 "chk") offset=$((0x$(get_magic_long_at "$1" 4)));;
147 "cybertan") offset=32;;
148 "seama") return;;
149 esac
150
151 # Extract partitions from trx
152 rm -fR $dir
153 mkdir -p $dir
154 otrx extract "$trx" \
155 ${offset:+-o $offset} \
156 -1 $dir/kernel \
157 -2 $dir/root
158 [ $? -ne 0 ] && {
159 echo "Failed to extract TRX partitions."
160 return
161 }
162
163 # Firmwares without UBI image should be flashed "normally"
164 local root_type=$(identify $dir/root)
165 [ "$root_type" != "ubi" ] && {
166 echo "Provided firmware doesn't use UBI for rootfs."
167 return
168 }
169
170 # Prepare TRX file with just a kernel that will replace current one
171 local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
172 [ -z "$linux_length" ] && {
173 echo "Unable to find \"linux\" partition size"
174 exit 1
175 }
176 linux_length=$((0x$linux_length))
177 local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
178 [ $kernel_length -gt $linux_length ] && {
179 echo "New kernel doesn't fit \"linux\" partition."
180 return
181 }
182 rm -f /tmp/null.bin
183 rm -f /tmp/kernel.trx
184 touch /tmp/null.bin
185 otrx create /tmp/kernel.trx \
186 -f $dir/kernel -b $(($linux_length + 28)) \
187 -f /tmp/null.bin
188 [ $? -ne 0 ] && {
189 echo "Failed to create simple TRX with new kernel."
190 return
191 }
192
193 # Prepare UBI image (drop unwanted extra blocks)
194 local ubi_length=0
195 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
196 ubi_length=$(($ubi_length + 131072))
197 done
198 dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
199 [ $? -ne 0 ] && {
200 echo "Failed to prepare new UBI image."
201 return
202 }
203
204 # Flash
205 mtd write /tmp/kernel.trx firmware
206 nand_do_upgrade /tmp/root.ubi
207 }
208
209 platform_trx_from_chk_cmd() {
210 local header_len=$((0x$(get_magic_long_at "$1" 4)))
211
212 echo -n dd bs=$header_len skip=1
213 }
214
215 platform_trx_from_cybertan_cmd() {
216 echo -n dd bs=32 skip=1
217 }
218
219 platform_img_from_seama() {
220 local dir="/tmp/sysupgrade-bcm53xx"
221 local offset=$(oseama info "$1" -e 0 | grep "Entity offset:" | sed "s/.*:\s*//")
222 local size=$(oseama info "$1" -e 0 | grep "Entity size:" | sed "s/.*:\s*//")
223
224 # Busybox doesn't support required iflag-s
225 # echo -n dd iflag=skip_bytes,count_bytes skip=$offset count=$size
226
227 rm -fR $dir
228 mkdir -p $dir
229 dd if="$1" of=$dir/image-noheader.bin bs=$offset skip=1
230 dd if=$dir/image-noheader.bin of=$dir/image-entity.bin bs=$size count=1
231
232 echo -n $dir/image-entity.bin
233 }
234
235 platform_do_upgrade() {
236 local file_type=$(platform_identify "$1")
237 local trx="$1"
238 local cmd=
239
240 [ "$(platform_flash_type)" == "nand" ] && {
241 echo "Writing whole image to NAND flash. All erase counters will be lost."
242 }
243
244 case "$file_type" in
245 "chk") cmd=$(platform_trx_from_chk_cmd "$trx");;
246 "cybertan") cmd=$(platform_trx_from_cybertan_cmd "$trx");;
247 "seama") trx=$(platform_img_from_seama "$trx");;
248 esac
249
250 default_do_upgrade "$trx" "$cmd"
251 }