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