base-files: clean up nand tar sysupgrade code
authorRodrigo Balerdi <lanchon@gmail.com>
Fri, 15 Apr 2022 09:31:24 +0000 (06:31 -0300)
committerDaniel Golle <daniel@makrotopia.org>
Tue, 19 Apr 2022 15:28:25 +0000 (16:28 +0100)
Prepares code for ubirename-based safe sysupgrade implementation.

Fixes several issues:
- the special CI_KERNPART value "none" is ignored if an MTD partition
  named "none" exists
- misleading variable names (such as has_kernel to mean "tar has kernel
  and it should not be written to an MTD partition but a UBI volume")
- inconsistent treatment of zero-length tar member files
- inconsistent meaning of "0" and "" variable values
- redundant operations (unneeded untaring, repeated untaring, unneeded
  partition lookups)
- inconsistent variable quoting

Signed-off-by: Rodrigo Balerdi <lanchon@gmail.com>
package/base-files/files/lib/upgrade/nand.sh

index a93c9acccdb0c8fee6e57b6268ea111adbfd97ce..67c6a620b1f4781feb92bab7f6b61c170f8e8ab0 100644 (file)
@@ -283,49 +283,49 @@ nand_upgrade_fit() {
 
 nand_upgrade_tar() {
        local tar_file="$1"
-       local kernel_mtd="$(find_mtd_index $CI_KERNPART)"
 
-       local board_dir=$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')
-       board_dir=${board_dir%/}
+       local board_dir="$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')"
+       board_dir="${board_dir%/}"
 
-       local kernel_length=$( (tar xf "$tar_file" ${board_dir}/kernel -O | wc -c) 2> /dev/null)
-       local has_rootfs=0
-       local rootfs_length
+       local kernel_mtd kernel_length
+       if [ "$CI_KERNPART" != "none" ]; then
+               kernel_mtd="$(find_mtd_index "$CI_KERNPART")"
+               kernel_length=$( (tar xf "$tar_file" "$board_dir/kernel" -O | wc -c) 2> /dev/null)
+               [ "$kernel_length" = 0 ] && kernel_length=
+       fi
+       local rootfs_length=$( (tar xf "$tar_file" "$board_dir/root" -O | wc -c) 2> /dev/null)
+       [ "$rootfs_length" = 0 ] && rootfs_length=
        local rootfs_type
+       [ "$rootfs_length" ] && rootfs_type="$(identify_tar "$tar_file" "$board_dir/root")"
 
-       tar tf "$tar_file" ${board_dir}/root 1>/dev/null 2>/dev/null && has_rootfs=1
-       [ "$has_rootfs" = "1" ] && {
-               rootfs_length=$( (tar xf "$tar_file" ${board_dir}/root -O | wc -c) 2> /dev/null)
-               rootfs_type="$(identify_tar "$tar_file" ${board_dir}/root)"
-       }
-
-       local has_kernel=1
+       local ubi_kernel_length
+       if [ "$kernel_length" ]; then
+               if [ "$kernel_mtd" ]; then
+                       mtd erase "$CI_KERNPART"
+               else
+                       ubi_kernel_length="$kernel_length"
+               fi
+       fi
        local has_env=0
-
-       [ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
-               mtd erase $CI_KERNPART
-       }
-       [ "$kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=
-       [ "$CI_KERNPART" = "none" ] && has_kernel=
-
-       nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "${has_kernel:+$kernel_length}" "$has_env"
+       nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$ubi_kernel_length" "$has_env"
 
        local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
-       [ "$has_rootfs" = "1" ] && {
-               local root_ubivol="$( nand_find_volume $ubidev $CI_ROOTPART )"
-               tar xf "$tar_file" ${board_dir}/root -O | \
+       if [ "$rootfs_length" ]; then
+               local root_ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
+               tar xf "$tar_file" "$board_dir/root" -O | \
                        ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
-       }
+       fi
+       if [ "$kernel_length" ]; then
+               if [ "$kernel_mtd" ]; then
+                       tar xf "$tar_file" "$board_dir/kernel" -O | \
+                               mtd -n write - "$CI_KERNPART"
+               else
+                       local kern_ubivol="$( nand_find_volume $ubidev "$CI_KERNPART" )"
+                       tar xf "$tar_file" "$board_dir/kernel" -O | \
+                               ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
+               fi
+       fi
 
-       [ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
-               tar xf "$tar_file" ${board_dir}/kernel -O | \
-                       mtd -n write - $CI_KERNPART
-       }
-       [ "$has_kernel" = "1" ] && {
-               local kern_ubivol="$( nand_find_volume $ubidev $CI_KERNPART )"
-               tar xf "$tar_file" ${board_dir}/kernel -O | \
-                       ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
-       }
        nand_do_upgrade_success
 }