kernel: bump 5.15 to 5.15.76
[openwrt/openwrt.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_detach_ubi() {
150 local ubipart="$1"
151
152 local mtdnum="$( find_mtd_index "$ubipart" )"
153 if [ ! "$mtdnum" ]; then
154 echo "cannot find ubi mtd partition $ubipart"
155 return 1
156 fi
157
158 local ubidev="$( nand_find_ubi "$ubipart" )"
159 if [ "$ubidev" ]; then
160 for ubivol in $(find /dev -name "${ubidev}_*" -maxdepth 1 | sort); do
161 ubivol="${ubivol:5}"
162 nand_remove_ubiblock "$ubivol" || :
163 umount "/dev/$ubivol" && echo "unmounted /dev/$ubivol" || :
164 done
165 if ! ubidetach -m "$mtdnum"; then
166 echo "cannot detach ubi mtd partition $ubipart"
167 return 1
168 fi
169 fi
170 }
171
172 nand_upgrade_prepare_ubi() {
173 local rootfs_length="$1"
174 local rootfs_type="$2"
175 local rootfs_data_max="$(fw_printenv -n rootfs_data_max 2> /dev/null)"
176 [ -n "$rootfs_data_max" ] && rootfs_data_max=$((rootfs_data_max))
177
178 local kernel_length="$3"
179 local has_env="${4:-0}"
180
181 [ -n "$rootfs_length" -o -n "$kernel_length" ] || return 1
182
183 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
184 if [ ! "$mtdnum" ]; then
185 echo "cannot find ubi mtd partition $CI_UBIPART"
186 return 1
187 fi
188
189 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
190 if [ ! "$ubidev" ]; then
191 ubiattach -m "$mtdnum"
192 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
193
194 if [ ! "$ubidev" ]; then
195 ubiformat /dev/mtd$mtdnum -y
196 ubiattach -m "$mtdnum"
197 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
198
199 if [ ! "$ubidev" ]; then
200 echo "cannot attach ubi mtd partition $CI_UBIPART"
201 return 1
202 fi
203
204 if [ "$has_env" -gt 0 ]; then
205 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
206 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
207 fi
208 fi
209 fi
210
211 local kern_ubivol="$( nand_find_volume $ubidev "$CI_KERNPART" )"
212 local root_ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
213 local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
214 [ "$root_ubivol" = "$kern_ubivol" ] && root_ubivol=
215
216 # remove ubiblocks
217 [ "$kern_ubivol" ] && { nand_remove_ubiblock $kern_ubivol || return 1; }
218 [ "$root_ubivol" ] && { nand_remove_ubiblock $root_ubivol || return 1; }
219 [ "$data_ubivol" ] && { nand_remove_ubiblock $data_ubivol || return 1; }
220
221 # kill volumes
222 [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N "$CI_KERNPART" || :
223 [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N "$CI_ROOTPART" || :
224 [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || :
225
226 # create kernel vol
227 if [ -n "$kernel_length" ]; then
228 if ! ubimkvol /dev/$ubidev -N "$CI_KERNPART" -s $kernel_length; then
229 echo "cannot create kernel volume"
230 return 1;
231 fi
232 fi
233
234 # create rootfs vol
235 if [ -n "$rootfs_length" ]; then
236 local rootfs_size_param
237 if [ "$rootfs_type" = "ubifs" ]; then
238 rootfs_size_param="-m"
239 else
240 rootfs_size_param="-s $rootfs_length"
241 fi
242 if ! ubimkvol /dev/$ubidev -N "$CI_ROOTPART" $rootfs_size_param; then
243 echo "cannot create rootfs volume"
244 return 1;
245 fi
246 fi
247
248 # create rootfs_data vol for non-ubifs rootfs
249 if [ "$rootfs_type" != "ubifs" ]; then
250 local rootfs_data_size_param="-m"
251 if [ -n "$rootfs_data_max" ]; then
252 rootfs_data_size_param="-s $rootfs_data_max"
253 fi
254 if ! ubimkvol /dev/$ubidev -N rootfs_data $rootfs_data_size_param; then
255 if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
256 echo "cannot initialize rootfs_data volume"
257 return 1
258 fi
259 fi
260 fi
261
262 return 0
263 }
264
265 # Write the UBI image to MTD ubi partition
266 nand_upgrade_ubinized() {
267 local ubi_file="$1"
268 local gz="$2"
269
270 nand_detach_ubi "$CI_UBIPART" || return 1
271
272 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
273 ${gz}cat "$ubi_file" | ubiformat "/dev/mtd$mtdnum" -y -f - && ubiattach -m "$mtdnum"
274 }
275
276 # Write the UBIFS image to UBI rootfs volume
277 nand_upgrade_ubifs() {
278 local ubifs_file="$1"
279 local gz="$2"
280
281 local ubifs_length=$( (${gz}cat "$ubifs_file" | wc -c) 2> /dev/null)
282
283 nand_upgrade_prepare_ubi "$ubifs_length" "ubifs" "" "" || return 1
284
285 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
286 local root_ubivol="$(nand_find_volume $ubidev "$CI_ROOTPART")"
287 ${gz}cat "$ubifs_file" | ubiupdatevol /dev/$root_ubivol -s "$ubifs_length" -
288 }
289
290 # Write the FIT image to UBI kernel volume
291 nand_upgrade_fit() {
292 local fit_file="$1"
293 local gz="$2"
294
295 local fit_length=$( (${gz}cat "$fit_file" | wc -c) 2> /dev/null)
296
297 nand_upgrade_prepare_ubi "" "" "$fit_length" "1" || return 1
298
299 local fit_ubidev="$(nand_find_ubi "$CI_UBIPART")"
300 local fit_ubivol="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
301 ${gz}cat "$fit_file" | ubiupdatevol /dev/$fit_ubivol -s "$fit_length" -
302 }
303
304 # Write images in the TAR file to MTD partitions and/or UBI volumes as required
305 nand_upgrade_tar() {
306 local tar_file="$1"
307 local gz="$2"
308
309 # WARNING: This fails if tar contains more than one 'sysupgrade-*' directory.
310 local board_dir="$(tar t${gz}f "$tar_file" | grep -m 1 '^sysupgrade-.*/$')"
311 board_dir="${board_dir%/}"
312
313 local kernel_mtd kernel_length
314 if [ "$CI_KERNPART" != "none" ]; then
315 kernel_mtd="$(find_mtd_index "$CI_KERNPART")"
316 kernel_length=$( (tar xO${gz}f "$tar_file" "$board_dir/kernel" | wc -c) 2> /dev/null)
317 [ "$kernel_length" = 0 ] && kernel_length=
318 fi
319 local rootfs_length=$( (tar xO${gz}f "$tar_file" "$board_dir/root" | wc -c) 2> /dev/null)
320 [ "$rootfs_length" = 0 ] && rootfs_length=
321 local rootfs_type
322 [ "$rootfs_length" ] && rootfs_type="$(identify_tar "$tar_file" "$board_dir/root" "$gz")"
323
324 local ubi_kernel_length
325 if [ "$kernel_length" ]; then
326 if [ "$kernel_mtd" ]; then
327 # On some devices, the raw kernel and ubi partitions overlap.
328 # These devices brick if the kernel partition is erased.
329 # Hence only invalidate kernel for now.
330 dd if=/dev/zero bs=4096 count=1 2> /dev/null | \
331 mtd write - "$CI_KERNPART"
332 else
333 ubi_kernel_length="$kernel_length"
334 fi
335 fi
336 local has_env=0
337 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$ubi_kernel_length" "$has_env" || return 1
338
339 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
340 if [ "$rootfs_length" ]; then
341 local root_ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
342 tar xO${gz}f "$tar_file" "$board_dir/root" | \
343 ubiupdatevol /dev/$root_ubivol -s "$rootfs_length" -
344 fi
345 if [ "$kernel_length" ]; then
346 if [ "$kernel_mtd" ]; then
347 tar xO${gz}f "$tar_file" "$board_dir/kernel" | \
348 mtd write - "$CI_KERNPART"
349 else
350 local kern_ubivol="$( nand_find_volume $ubidev "$CI_KERNPART" )"
351 tar xO${gz}f "$tar_file" "$board_dir/kernel" | \
352 ubiupdatevol /dev/$kern_ubivol -s "$kernel_length" -
353 fi
354 fi
355
356 return 0
357 }
358
359 nand_verify_if_gzip_file() {
360 local file="$1"
361 local gz="$2"
362
363 if [ "$gz" = z ]; then
364 echo "verifying compressed sysupgrade file integrity"
365 if ! gzip -t "$file"; then
366 echo "corrupted compressed sysupgrade file"
367 return 1
368 fi
369 fi
370 }
371
372 nand_verify_tar_file() {
373 local file="$1"
374 local gz="$2"
375
376 echo "verifying sysupgrade tar file integrity"
377 if ! tar xO${gz}f "$file" > /dev/null; then
378 echo "corrupted sysupgrade tar file"
379 return 1
380 fi
381 }
382
383 nand_do_flash_file() {
384 local file="$1"
385
386 local gz="$(identify_if_gzip "$file")"
387 local file_type="$(identify "$file" "" "$gz")"
388
389 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART=rootfs
390
391 case "$file_type" in
392 "fit")
393 nand_verify_if_gzip_file "$file" "$gz" || return 1
394 nand_upgrade_fit "$file" "$gz"
395 ;;
396 "ubi")
397 nand_verify_if_gzip_file "$file" "$gz" || return 1
398 nand_upgrade_ubinized "$file" "$gz"
399 ;;
400 "ubifs")
401 nand_verify_if_gzip_file "$file" "$gz" || return 1
402 nand_upgrade_ubifs "$file" "$gz"
403 ;;
404 *)
405 nand_verify_tar_file "$file" "$gz" || return 1
406 nand_upgrade_tar "$file" "$gz"
407 ;;
408 esac
409 }
410
411 nand_do_restore_config() {
412 local conf_tar="/tmp/sysupgrade.tgz"
413 [ ! -f "$conf_tar" ] || nand_restore_config "$conf_tar"
414 }
415
416 # Recognize type of passed file and start the upgrade process
417 nand_do_upgrade() {
418 local file="$1"
419
420 sync
421 if nand_do_flash_file "$file" && nand_do_restore_config && sync; then
422 echo "sysupgrade successful"
423 umount -a
424 reboot -f
425 fi
426
427 sync
428 echo "sysupgrade failed"
429 # Should we reboot or bring up some failsafe mode instead?
430 umount -a
431 reboot -f
432 }
433
434 # Check if passed file is a valid one for NAND sysupgrade.
435 # Currently it accepts 4 types of files:
436 # 1) UBI: a ubinized image containing required UBI volumes.
437 # 2) UBIFS: a UBIFS rootfs volume image.
438 # 3) FIT: a FIT image containing kernel and rootfs.
439 # 4) TAR: an archive that includes directory "sysupgrade-${BOARD_NAME}" containing
440 # a non-empty "CONTROL" file and required partition and/or volume images.
441 #
442 # You usually want to call this function in platform_check_image.
443 #
444 # $(1): board name, used in case of passing TAR file
445 # $(2): file to be checked
446 nand_do_platform_check() {
447 local board_name="$1"
448 local file="$2"
449
450 local gz="$(identify_if_gzip "$file")"
451 local file_type="$(identify "$file" "" "$gz")"
452 local control_length=$( (tar xO${gz}f "$file" "sysupgrade-$board_name/CONTROL" | wc -c) 2> /dev/null)
453
454 if [ "$control_length" != 0 ]; then
455 nand_verify_tar_file "$file" "$gz" || return 1
456 else
457 nand_verify_if_gzip_file "$file" "$gz" || return 1
458 if [ "$file_type" != "fit" -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ]; then
459 echo "invalid sysupgrade file"
460 return 1
461 fi
462 fi
463
464 return 0
465 }