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