e7ac34f5d0cf3abfa66735db2151e059a642fdb8
[openwrt/staging/aparcar.git] / package / base-files / files / lib / upgrade / nand.sh
1 # Copyright (C) 2014 OpenWrt.org
2 #
3
4 . /lib/functions.sh
5
6 # 'kernel' partition or UBI volume on NAND contains the kernel
7 CI_KERNPART="${CI_KERNPART:-kernel}"
8
9 # 'ubi' partition on NAND contains UBI
10 # There are also CI_KERN_UBIPART and CI_ROOT_UBIPART if kernel
11 # and rootfs are on separated UBIs.
12 CI_UBIPART="${CI_UBIPART:-ubi}"
13
14 # 'rootfs' UBI volume on NAND contains the rootfs
15 CI_ROOTPART="${CI_ROOTPART:-rootfs}"
16
17 ubi_mknod() {
18 local dir="$1"
19 local dev="/dev/$(basename $dir)"
20
21 [ -e "$dev" ] && return 0
22
23 local devid="$(cat $dir/dev)"
24 local major="${devid%%:*}"
25 local minor="${devid##*:}"
26 mknod "$dev" c $major $minor
27 }
28
29 nand_find_volume() {
30 local ubidevdir ubivoldir
31 ubidevdir="/sys/devices/virtual/ubi/$1"
32 [ ! -d "$ubidevdir" ] && return 1
33 for ubivoldir in $ubidevdir/${1}_*; do
34 [ ! -d "$ubivoldir" ] && continue
35 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
36 basename $ubivoldir
37 ubi_mknod "$ubivoldir"
38 return 0
39 fi
40 done
41 }
42
43 nand_find_ubi() {
44 local ubidevdir ubidev mtdnum
45 mtdnum="$( find_mtd_index $1 )"
46 [ ! "$mtdnum" ] && return 1
47 for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
48 [ ! -d "$ubidevdir" ] && continue
49 cmtdnum="$( cat $ubidevdir/mtd_num )"
50 [ ! "$mtdnum" ] && continue
51 if [ "$mtdnum" = "$cmtdnum" ]; then
52 ubidev=$( basename $ubidevdir )
53 ubi_mknod "$ubidevdir"
54 echo $ubidev
55 return 0
56 fi
57 done
58 }
59
60 nand_get_magic_long() {
61 (${3}cat "$1" | dd bs=4 "skip=${2:-0}" count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
62 }
63
64 get_magic_long_tar() {
65 (tar xO${3}f "$1" "$2" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
66 }
67
68 identify() {
69 identify_magic_long $(nand_get_magic_long "$@")
70 }
71
72 identify_tar() {
73 identify_magic_long $(get_magic_long_tar "$@")
74 }
75
76 identify_if_gzip() {
77 if [ "$(identify "$1")" = gzip ]; then echo -n z; fi
78 }
79
80 nand_restore_config() {
81 local ubidev=$( nand_find_ubi "${CI_ROOT_UBIPART:-$CI_UBIPART}" )
82 local ubivol="$( nand_find_volume $ubidev rootfs_data )"
83 if [ ! "$ubivol" ]; then
84 ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
85 if [ ! "$ubivol" ]; then
86 echo "cannot find ubifs data volume"
87 return 1
88 fi
89 fi
90 mkdir /tmp/new_root
91 if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
92 echo "cannot mount ubifs volume $ubivol"
93 rmdir /tmp/new_root
94 return 1
95 fi
96 if mv "$1" "/tmp/new_root/$BACKUP_FILE"; then
97 if umount /tmp/new_root; then
98 echo "configuration saved"
99 rmdir /tmp/new_root
100 return 0
101 fi
102 else
103 umount /tmp/new_root
104 fi
105 echo "could not save configuration to ubifs volume $ubivol"
106 rmdir /tmp/new_root
107 return 1
108 }
109
110 nand_remove_ubiblock() {
111 local ubivol="$1"
112
113 local ubiblk="ubiblock${ubivol:3}"
114 if [ -e "/dev/$ubiblk" ]; then
115 umount "/dev/$ubiblk" && echo "unmounted /dev/$ubiblk" || :
116 if ! ubiblock -r "/dev/$ubivol"; then
117 echo "cannot remove $ubiblk"
118 return 1
119 fi
120 fi
121 }
122
123 nand_attach_ubi() {
124 local ubipart="$1"
125 local has_env="${2:-0}"
126
127 local mtdnum="$( find_mtd_index "$ubipart" )"
128 if [ ! "$mtdnum" ]; then
129 >&2 echo "cannot find ubi mtd partition $ubipart"
130 return 1
131 fi
132
133 local ubidev="$( nand_find_ubi "$ubipart" )"
134 if [ ! "$ubidev" ]; then
135 >&2 ubiattach -m "$mtdnum"
136 ubidev="$( nand_find_ubi "$ubipart" )"
137
138 if [ ! "$ubidev" ]; then
139 >&2 ubiformat /dev/mtd$mtdnum -y
140 >&2 ubiattach -m "$mtdnum"
141 ubidev="$( nand_find_ubi "$ubipart" )"
142
143 if [ ! "$ubidev" ]; then
144 >&2 echo "cannot attach ubi mtd partition $ubipart"
145 return 1
146 fi
147
148 if [ "$has_env" -gt 0 ]; then
149 >&2 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
150 >&2 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
151 fi
152 fi
153 fi
154
155 echo "$ubidev"
156 return 0
157 }
158
159 nand_detach_ubi() {
160 local ubipart="$1"
161
162 local mtdnum="$( find_mtd_index "$ubipart" )"
163 if [ ! "$mtdnum" ]; then
164 echo "cannot find ubi mtd partition $ubipart"
165 return 1
166 fi
167
168 local ubidev="$( nand_find_ubi "$ubipart" )"
169 if [ "$ubidev" ]; then
170 for ubivol in $(find /dev -name "${ubidev}_*" -maxdepth 1 | sort); do
171 ubivol="${ubivol:5}"
172 nand_remove_ubiblock "$ubivol" || :
173 umount "/dev/$ubivol" && echo "unmounted /dev/$ubivol" || :
174 done
175 if ! ubidetach -m "$mtdnum"; then
176 echo "cannot detach ubi mtd partition $ubipart"
177 return 1
178 fi
179 fi
180 }
181
182 nand_upgrade_prepare_ubi() {
183 local rootfs_length="$1"
184 local rootfs_type="$2"
185 local rootfs_data_max="$(fw_printenv -n rootfs_data_max 2> /dev/null)"
186 [ -n "$rootfs_data_max" ] && rootfs_data_max=$((rootfs_data_max))
187
188 local kernel_length="$3"
189 local has_env="${4:-0}"
190 local kern_ubidev
191 local root_ubidev
192
193 [ -n "$rootfs_length" -o -n "$kernel_length" ] || return 1
194
195 if [ -n "$CI_KERN_UBIPART" -a -n "$CI_ROOT_UBIPART" ]; then
196 kern_ubidev="$( nand_attach_ubi "$CI_KERN_UBIPART" "$has_env" )"
197 [ -n "$kern_ubidev" ] || return 1
198 root_ubidev="$( nand_attach_ubi "$CI_ROOT_UBIPART" )"
199 [ -n "$root_ubidev" ] || return 1
200 else
201 kern_ubidev="$( nand_attach_ubi "$CI_UBIPART" "$has_env" )"
202 [ -n "$kern_ubidev" ] || return 1
203 root_ubidev="$kern_ubidev"
204 fi
205
206 local kern_ubivol="$( nand_find_volume $kern_ubidev "$CI_KERNPART" )"
207 local root_ubivol="$( nand_find_volume $root_ubidev "$CI_ROOTPART" )"
208 local data_ubivol="$( nand_find_volume $root_ubidev rootfs_data )"
209 [ "$root_ubivol" = "$kern_ubivol" ] && root_ubivol=
210
211 # remove ubiblocks
212 [ "$kern_ubivol" ] && { nand_remove_ubiblock $kern_ubivol || return 1; }
213 [ "$root_ubivol" ] && { nand_remove_ubiblock $root_ubivol || return 1; }
214 [ "$data_ubivol" ] && { nand_remove_ubiblock $data_ubivol || return 1; }
215
216 # kill volumes
217 [ "$kern_ubivol" ] && ubirmvol /dev/$kern_ubidev -N "$CI_KERNPART" || :
218 [ "$root_ubivol" ] && ubirmvol /dev/$root_ubidev -N "$CI_ROOTPART" || :
219 [ "$data_ubivol" ] && ubirmvol /dev/$root_ubidev -N rootfs_data || :
220
221 # create kernel vol
222 if [ -n "$kernel_length" ]; then
223 if ! ubimkvol /dev/$kern_ubidev -N "$CI_KERNPART" -s $kernel_length; then
224 echo "cannot create kernel volume"
225 return 1;
226 fi
227 fi
228
229 # create rootfs vol
230 if [ -n "$rootfs_length" ]; then
231 local rootfs_size_param
232 if [ "$rootfs_type" = "ubifs" ]; then
233 rootfs_size_param="-m"
234 else
235 rootfs_size_param="-s $rootfs_length"
236 fi
237 if ! ubimkvol /dev/$root_ubidev -N "$CI_ROOTPART" $rootfs_size_param; then
238 echo "cannot create rootfs volume"
239 return 1;
240 fi
241 fi
242
243 # create rootfs_data vol for non-ubifs rootfs
244 if [ "$rootfs_type" != "ubifs" ]; then
245 local rootfs_data_size_param="-m"
246 if [ -n "$rootfs_data_max" ]; then
247 rootfs_data_size_param="-s $rootfs_data_max"
248 fi
249 if ! ubimkvol /dev/$root_ubidev -N rootfs_data $rootfs_data_size_param; then
250 if ! ubimkvol /dev/$root_ubidev -N rootfs_data -m; then
251 echo "cannot initialize rootfs_data volume"
252 return 1
253 fi
254 fi
255 fi
256
257 return 0
258 }
259
260 # Write the UBI image to MTD ubi partition
261 nand_upgrade_ubinized() {
262 local ubi_file="$1"
263 local gz="$2"
264
265 nand_detach_ubi "$CI_UBIPART" || return 1
266
267 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
268 ${gz}cat "$ubi_file" | ubiformat "/dev/mtd$mtdnum" -y -f - && ubiattach -m "$mtdnum"
269 }
270
271 # Write the UBIFS image to UBI rootfs volume
272 nand_upgrade_ubifs() {
273 local ubifs_file="$1"
274 local gz="$2"
275
276 local ubifs_length=$( (${gz}cat "$ubifs_file" | wc -c) 2> /dev/null)
277
278 nand_upgrade_prepare_ubi "$ubifs_length" "ubifs" "" "" || return 1
279
280 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
281 local root_ubivol="$(nand_find_volume $ubidev "$CI_ROOTPART")"
282 ${gz}cat "$ubifs_file" | ubiupdatevol /dev/$root_ubivol -s "$ubifs_length" -
283 }
284
285 # Write the FIT image to UBI kernel volume
286 nand_upgrade_fit() {
287 local fit_file="$1"
288 local gz="$2"
289
290 local fit_length=$( (${gz}cat "$fit_file" | wc -c) 2> /dev/null)
291
292 nand_upgrade_prepare_ubi "" "" "$fit_length" "1" || return 1
293
294 local fit_ubidev="$(nand_find_ubi "$CI_UBIPART")"
295 local fit_ubivol="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
296 ${gz}cat "$fit_file" | ubiupdatevol /dev/$fit_ubivol -s "$fit_length" -
297 }
298
299 # Write images in the TAR file to MTD partitions and/or UBI volumes as required
300 nand_upgrade_tar() {
301 local tar_file="$1"
302 local gz="$2"
303
304 # WARNING: This fails if tar contains more than one 'sysupgrade-*' directory.
305 local board_dir="$(tar t${gz}f "$tar_file" | grep -m 1 '^sysupgrade-.*/$')"
306 board_dir="${board_dir%/}"
307
308 local kernel_mtd kernel_length
309 if [ "$CI_KERNPART" != "none" ]; then
310 kernel_mtd="$(find_mtd_index "$CI_KERNPART")"
311 kernel_length=$( (tar xO${gz}f "$tar_file" "$board_dir/kernel" | wc -c) 2> /dev/null)
312 [ "$kernel_length" = 0 ] && kernel_length=
313 fi
314 local rootfs_length=$( (tar xO${gz}f "$tar_file" "$board_dir/root" | wc -c) 2> /dev/null)
315 [ "$rootfs_length" = 0 ] && rootfs_length=
316 local rootfs_type
317 [ "$rootfs_length" ] && rootfs_type="$(identify_tar "$tar_file" "$board_dir/root" "$gz")"
318
319 local ubi_kernel_length
320 if [ "$kernel_length" ]; then
321 if [ "$kernel_mtd" ]; then
322 # On some devices, the raw kernel and ubi partitions overlap.
323 # These devices brick if the kernel partition is erased.
324 # Hence only invalidate kernel for now.
325 dd if=/dev/zero bs=4096 count=1 2> /dev/null | \
326 mtd write - "$CI_KERNPART"
327 else
328 ubi_kernel_length="$kernel_length"
329 fi
330 fi
331 local has_env=0
332 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$ubi_kernel_length" "$has_env" || return 1
333
334 if [ "$rootfs_length" ]; then
335 local ubidev="$( nand_find_ubi "${CI_ROOT_UBIPART:-$CI_UBIPART}" )"
336 local root_ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
337 tar xO${gz}f "$tar_file" "$board_dir/root" | \
338 ubiupdatevol /dev/$root_ubivol -s "$rootfs_length" -
339 fi
340 if [ "$kernel_length" ]; then
341 if [ "$kernel_mtd" ]; then
342 tar xO${gz}f "$tar_file" "$board_dir/kernel" | \
343 mtd write - "$CI_KERNPART"
344 else
345 local ubidev="$( nand_find_ubi "${CI_KERN_UBIPART:-$CI_UBIPART}" )"
346 local kern_ubivol="$( nand_find_volume $ubidev "$CI_KERNPART" )"
347 tar xO${gz}f "$tar_file" "$board_dir/kernel" | \
348 ubiupdatevol /dev/$kern_ubivol -s "$kernel_length" -
349 fi
350 fi
351
352 return 0
353 }
354
355 nand_verify_if_gzip_file() {
356 local file="$1"
357 local gz="$2"
358
359 if [ "$gz" = z ]; then
360 echo "verifying compressed sysupgrade file integrity"
361 if ! gzip -t "$file"; then
362 echo "corrupted compressed sysupgrade file"
363 return 1
364 fi
365 fi
366 }
367
368 nand_verify_tar_file() {
369 local file="$1"
370 local gz="$2"
371
372 echo "verifying sysupgrade tar file integrity"
373 if ! tar xO${gz}f "$file" > /dev/null; then
374 echo "corrupted sysupgrade tar file"
375 return 1
376 fi
377 }
378
379 nand_do_flash_file() {
380 local file="$1"
381
382 local gz="$(identify_if_gzip "$file")"
383 local file_type="$(identify "$file" "" "$gz")"
384
385 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART=rootfs
386
387 case "$file_type" in
388 "fit")
389 nand_verify_if_gzip_file "$file" "$gz" || return 1
390 nand_upgrade_fit "$file" "$gz"
391 ;;
392 "ubi")
393 nand_verify_if_gzip_file "$file" "$gz" || return 1
394 nand_upgrade_ubinized "$file" "$gz"
395 ;;
396 "ubifs")
397 nand_verify_if_gzip_file "$file" "$gz" || return 1
398 nand_upgrade_ubifs "$file" "$gz"
399 ;;
400 *)
401 nand_verify_tar_file "$file" "$gz" || return 1
402 nand_upgrade_tar "$file" "$gz"
403 ;;
404 esac
405 }
406
407 nand_do_restore_config() {
408 local conf_tar="/tmp/sysupgrade.tgz"
409 [ ! -f "$conf_tar" ] || nand_restore_config "$conf_tar"
410 }
411
412 # Recognize type of passed file and start the upgrade process
413 nand_do_upgrade() {
414 local file="$1"
415
416 sync
417 nand_do_flash_file "$file" && nand_do_upgrade_success
418 nand_do_upgrade_failed
419 }
420
421 nand_do_upgrade_success() {
422 if nand_do_restore_config && sync; then
423 echo "sysupgrade successful"
424 umount -a
425 reboot -f
426 fi
427 nand_do_upgrade_failed
428 }
429
430 nand_do_upgrade_failed() {
431 sync
432 echo "sysupgrade failed"
433 # Should we reboot or bring up some failsafe mode instead?
434 umount -a
435 reboot -f
436 }
437
438 # Check if passed file is a valid one for NAND sysupgrade.
439 # Currently it accepts 4 types of files:
440 # 1) UBI: a ubinized image containing required UBI volumes.
441 # 2) UBIFS: a UBIFS rootfs volume image.
442 # 3) FIT: a FIT image containing kernel and rootfs.
443 # 4) TAR: an archive that includes directory "sysupgrade-${BOARD_NAME}" containing
444 # a non-empty "CONTROL" file and required partition and/or volume images.
445 #
446 # You usually want to call this function in platform_check_image.
447 #
448 # $(1): board name, used in case of passing TAR file
449 # $(2): file to be checked
450 nand_do_platform_check() {
451 local board_name="$1"
452 local file="$2"
453
454 local gz="$(identify_if_gzip "$file")"
455 local file_type="$(identify "$file" "" "$gz")"
456 local control_length=$( (tar xO${gz}f "$file" "sysupgrade-${board_name//,/_}/CONTROL" | wc -c) 2> /dev/null)
457
458 if [ "$control_length" = 0 ]; then
459 control_length=$( (tar xO${gz}f "$file" "sysupgrade-${board_name//_/,}/CONTROL" | wc -c) 2> /dev/null)
460 fi
461
462 if [ "$control_length" != 0 ]; then
463 nand_verify_tar_file "$file" "$gz" || return 1
464 else
465 nand_verify_if_gzip_file "$file" "$gz" || return 1
466 if [ "$file_type" != "fit" -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ]; then
467 echo "invalid sysupgrade file"
468 return 1
469 fi
470 fi
471
472 return 0
473 }