base-files: automatically handle paths and symlinks for RAMFS_COPY_BIN
[openwrt/openwrt.git] / target / linux / bcm53xx / base-files / lib / upgrade / platform.sh
1 RAMFS_COPY_BIN='osafeloader oseama'
2
3 PART_NAME=firmware
4
5 # $(1): file to read magic from
6 # $(2): offset in bytes
7 get_magic_long_at() {
8 dd if="$1" skip=$2 bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%02x"'
9 }
10
11 platform_machine() {
12 cat /proc/device-tree/compatible | tr '\0' '\t' | cut -f 1
13 }
14
15 platform_flash_type() {
16 # On NAND devices "rootfs" is UBI volume, so won't be find in /proc/mtd
17 grep -q "\"rootfs\"" /proc/mtd && {
18 echo "serial"
19 return
20 }
21
22 echo "nand"
23 }
24
25 platform_expected_image() {
26 local machine=$(platform_machine)
27
28 case "$machine" in
29 "dlink,dir-885l") echo "seama wrgac42_dlink.2015_dir885l"; return;;
30 "netgear,r6250v1") echo "chk U12H245T00_NETGEAR"; return;;
31 "netgear,r6300v2") echo "chk U12H240T00_NETGEAR"; return;;
32 "netgear,r7000") echo "chk U12H270T00_NETGEAR"; return;;
33 "netgear,r7900") echo "chk U12H315T30_NETGEAR"; return;;
34 "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
35 "netgear,r8500") echo "chk U12H334T00_NETGEAR"; return;;
36 "tplink,archer-c9-v1") echo "safeloader"; return;;
37 esac
38 }
39
40 platform_identify() {
41 local magic
42
43 magic=$(get_magic_long "$1")
44 case "$magic" in
45 "48445230")
46 echo "trx"
47 return
48 ;;
49 "2a23245e")
50 echo "chk"
51 return
52 ;;
53 "5ea3a417")
54 echo "seama"
55 return
56 ;;
57 esac
58
59 magic=$(get_magic_long_at "$1" 14)
60 [ "$magic" = "55324e44" ] && {
61 echo "cybertan"
62 return
63 }
64
65 if osafeloader info "$1" > /dev/null 2>&1; then
66 echo "safeloader"
67 return
68 fi
69
70 echo "unknown"
71 }
72
73 platform_check_image() {
74 [ "$#" -gt 1 ] && return 1
75
76 local file_type=$(platform_identify "$1")
77 local magic
78 local error=0
79
80 case "$file_type" in
81 "chk")
82 local header_len=$((0x$(get_magic_long_at "$1" 4)))
83 local board_id_len=$(($header_len - 40))
84 local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
85 local dev_board_id=$(platform_expected_image)
86 echo "Found CHK image with device board_id $board_id"
87
88 [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
89 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
90 error=1
91 }
92
93 if ! otrx check "$1" -o "$header_len"; then
94 echo "No valid TRX firmware in the CHK image"
95 error=1
96 fi
97 ;;
98 "cybertan")
99 local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
100 local dev_pattern=$(platform_expected_image)
101 echo "Found CyberTAN image with device pattern: $pattern"
102
103 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
104 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
105 error=1
106 }
107
108 if ! otrx check "$1" -o 32; then
109 echo "No valid TRX firmware in the CyberTAN image"
110 error=1
111 fi
112 ;;
113 "safeloader")
114 ;;
115 "seama")
116 local img_signature=$(oseama info "$1" | grep "Meta entry:.*signature=" | sed "s/.*=//")
117 local dev_signature=$(platform_expected_image)
118 echo "Found Seama image with device signature: $img_signature"
119
120 [ -n "$dev_signature" -a "seama $img_signature" != "$dev_signature" ] && {
121 echo "Firmware signature doesn't match device signature ($dev_signature)"
122 error=1
123 }
124
125 $(oseama info "$1" -e 0 | grep -q "Meta entry:.*type=firmware") || {
126 echo "Seama container doesn't have firmware entity"
127 error=1
128 }
129 ;;
130 "trx")
131 local expected=$(platform_expected_image)
132
133 [ "$expected" == "safeloader" ] && {
134 echo "This device expects SafeLoader format and may not work with TRX"
135 error=1
136 }
137
138 if ! otrx check "$1"; then
139 echo "Invalid (corrupted?) TRX firmware"
140 error=1
141 fi
142 ;;
143 *)
144 echo "Invalid image type. Please use only .trx files"
145 error=1
146 ;;
147 esac
148
149 return $error
150 }
151
152 # $(1): image for upgrade (with possible extra header)
153 # $(2): offset of trx in image
154 platform_pre_upgrade_trx() {
155 local dir="/tmp/sysupgrade-bcm53xx"
156 local trx="$1"
157 local offset="$2"
158
159 # Extract partitions from trx
160 rm -fR $dir
161 mkdir -p $dir
162 otrx extract "$trx" \
163 ${offset:+-o $offset} \
164 -1 $dir/kernel \
165 -2 $dir/root
166 [ $? -ne 0 ] && {
167 echo "Failed to extract TRX partitions."
168 return
169 }
170
171 # Firmwares without UBI image should be flashed "normally"
172 local root_type=$(identify $dir/root)
173 [ "$root_type" != "ubi" ] && {
174 echo "Provided firmware doesn't use UBI for rootfs."
175 return
176 }
177
178 # Prepare TRX file with just a kernel that will replace current one
179 local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
180 [ -z "$linux_length" ] && {
181 echo "Unable to find \"linux\" partition size"
182 exit 1
183 }
184 linux_length=$((0x$linux_length))
185 local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
186 [ $kernel_length -gt $linux_length ] && {
187 echo "New kernel doesn't fit \"linux\" partition."
188 return
189 }
190 rm -f /tmp/null.bin
191 rm -f /tmp/kernel.trx
192 touch /tmp/null.bin
193 otrx create /tmp/kernel.trx \
194 -f $dir/kernel -b $(($linux_length + 28)) \
195 -f /tmp/null.bin
196 [ $? -ne 0 ] && {
197 echo "Failed to create simple TRX with new kernel."
198 return
199 }
200
201 # Prepare UBI image (drop unwanted extra blocks)
202 local ubi_length=0
203 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
204 ubi_length=$(($ubi_length + 131072))
205 done
206 dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
207 [ $? -ne 0 ] && {
208 echo "Failed to prepare new UBI image."
209 return
210 }
211
212 # Flash
213 mtd write /tmp/kernel.trx firmware
214 nand_do_upgrade /tmp/root.ubi
215 }
216
217 platform_pre_upgrade_seama() {
218 local dir="/tmp/sysupgrade-bcm53xx"
219 local seama="$1"
220 local tmp
221
222 # Extract Seama entity from Seama seal
223 rm -fR $dir
224 mkdir -p $dir
225 oseama extract "$seama" \
226 -e 0 \
227 -o $dir/seama.entity
228 [ $? -ne 0 ] && {
229 echo "Failed to extract Seama entity."
230 return
231 }
232 local entity_size=$(wc -c $dir/seama.entity | cut -d ' ' -f 1)
233
234 local ubi_offset=0
235 tmp=0
236 while [ 1 ]; do
237 [ $tmp -ge $entity_size ] && break
238 [ "$(dd if=$dir/seama.entity skip=$tmp bs=1 count=4 2>/dev/null)" = "UBI#" ] && {
239 ubi_offset=$tmp
240 break
241 }
242 tmp=$(($tmp + 131072))
243 done
244 [ $ubi_offset -eq 0 ] && {
245 echo "Failed to find UBI in Seama entity."
246 return
247 }
248
249 local ubi_length=0
250 while [ "$(dd if=$dir/seama.entity skip=$(($ubi_offset + $ubi_length)) bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
251 ubi_length=$(($ubi_length + 131072))
252 done
253
254 dd if=$dir/seama.entity of=$dir/kernel.seama bs=131072 count=$(($ubi_offset / 131072)) 2>/dev/null
255 dd if=$dir/seama.entity of=$dir/root.ubi bs=131072 skip=$(($ubi_offset / 131072)) count=$(($ubi_length / 131072)) 2>/dev/null
256
257 # Flash
258 local kernel_size=$(sed -n 's/mtd[0-9]*: \([0-9a-f]*\).*"\(kernel\|linux\)".*/\1/p' /proc/mtd)
259 mtd write $dir/kernel.seama firmware
260 mtd ${kernel_size:+-c 0x$kernel_size} fixseama firmware
261 nand_do_upgrade $dir/root.ubi
262 }
263
264 platform_pre_upgrade() {
265 local file_type=$(platform_identify "$1")
266
267 [ "$(platform_flash_type)" != "nand" ] && return
268
269 # Find trx offset
270 case "$file_type" in
271 "chk") platform_pre_upgrade_trx "$1" $((0x$(get_magic_long_at "$1" 4)));;
272 "cybertan") platform_pre_upgrade_trx "$1" 32;;
273 "seama") platform_pre_upgrade_seama "$1";;
274 "trx") platform_pre_upgrade_trx "$1";;
275 esac
276 }
277
278 platform_trx_from_chk_cmd() {
279 local header_len=$((0x$(get_magic_long_at "$1" 4)))
280
281 echo -n dd bs=$header_len skip=1
282 }
283
284 platform_trx_from_cybertan_cmd() {
285 echo -n dd bs=32 skip=1
286 }
287
288 platform_img_from_safeloader() {
289 local dir="/tmp/sysupgrade-bcm53xx"
290
291 # Extract partitions from SafeLoader
292 rm -fR $dir
293 mkdir -p $dir
294 osafeloader extract "$1" \
295 -p "os-image" \
296 -o $dir/os-image
297 osafeloader extract "$1" \
298 -p "file-system" \
299 -o $dir/file-system
300
301 mtd write $dir/file-system rootfs
302
303 echo -n $dir/os-image
304 }
305
306 platform_img_from_seama() {
307 local dir="/tmp/sysupgrade-bcm53xx"
308 local offset=$(oseama info "$1" -e 0 | grep "Entity offset:" | sed "s/.*:\s*//")
309 local size=$(oseama info "$1" -e 0 | grep "Entity size:" | sed "s/.*:\s*//")
310
311 # Busybox doesn't support required iflag-s
312 # echo -n dd iflag=skip_bytes,count_bytes skip=$offset count=$size
313
314 rm -fR $dir
315 mkdir -p $dir
316 dd if="$1" of=$dir/image-noheader.bin bs=$offset skip=1
317 dd if=$dir/image-noheader.bin of=$dir/image-entity.bin bs=$size count=1
318
319 echo -n $dir/image-entity.bin
320 }
321
322 platform_do_upgrade() {
323 local file_type=$(platform_identify "$1")
324 local trx="$1"
325 local cmd=
326
327 [ "$(platform_flash_type)" == "nand" ] && {
328 echo "Writing whole image to NAND flash. All erase counters will be lost."
329 }
330
331 case "$file_type" in
332 "chk") cmd=$(platform_trx_from_chk_cmd "$trx");;
333 "cybertan") cmd=$(platform_trx_from_cybertan_cmd "$trx");;
334 "safeloader") trx=$(platform_img_from_safeloader "$trx");;
335 "seama") trx=$(platform_img_from_seama "$trx");;
336 esac
337
338 default_do_upgrade "$trx" "$cmd"
339 }