base-files: recognize bootdevice on devices using fitblk
[openwrt/openwrt.git] / package / base-files / files / lib / upgrade / common.sh
index b59a40456551c80948110046f60de72131d7ccf1..7e83379082e50f12104280e64eb5036232899fe6 100644 (file)
@@ -65,16 +65,9 @@ _v() {
        [ -n "$VERBOSE" ] && [ "$VERBOSE" -ge 1 ] && echo "$*" >&2
 }
 
-_vn() {
-       [ -n "$VERBOSE" ] && [ "$VERBOSE" -ge 1 ] && echo -n "$*" >&2
-}
-
 v() {
        _v "$(date) upgrade: $@"
-}
-
-vn() {
-       _vn "$(date) upgrade: $@"
+       logger -p info -t upgrade "$@"
 }
 
 json_string() {
@@ -95,7 +88,7 @@ get_image() { # <source> [ <command> ]
        if [ -z "$cmd" ]; then
                local magic="$(dd if="$from" bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
                case "$magic" in
-                       1f8b) cmd="zcat";;
+                       1f8b) cmd="busybox zcat";;
                        *) cmd="cat";;
                esac
        fi
@@ -134,6 +127,33 @@ get_magic_fat32() {
        (get_image "$@" | dd bs=1 count=5 skip=82) 2>/dev/null
 }
 
+identify_magic_long() {
+       local magic=$1
+       case "$magic" in
+               "55424923")
+                       echo "ubi"
+                       ;;
+               "31181006")
+                       echo "ubifs"
+                       ;;
+               "68737173")
+                       echo "squashfs"
+                       ;;
+               "d00dfeed")
+                       echo "fit"
+                       ;;
+               "4349"*)
+                       echo "combined"
+                       ;;
+               "1f8b"*)
+                       echo "gzip"
+                       ;;
+               *)
+                       echo "unknown $magic"
+                       ;;
+       esac
+}
+
 part_magic_efi() {
        local magic=$(get_magic_gpt "$@")
        [ "$magic" = "EFI PART" ]
@@ -145,6 +165,23 @@ part_magic_fat() {
        [ "$magic" = "FAT" ] || [ "$magic_fat32" = "FAT32" ]
 }
 
+fitblk_get_bootdev() {
+       [ -e /sys/firmware/devicetree/base/chosen/rootdisk ] || return
+
+       local rootdisk="$(cat /sys/firmware/devicetree/base/chosen/rootdisk)"
+       local handle bootdev
+       for handle in /sys/class/block/*/of_node/phandle /sys/class/block/*/device/of_node/phandle; do
+               [ ! -e "$handle" ] && continue
+               if [ "$rootdisk" = "$(cat $handle)" ]; then
+                       bootdev="${handle%/of_node/phandle}"
+                       bootdev="${bootdev%/device}"
+                       bootdev="${bootdev#/sys/class/block/}"
+                       echo "$bootdev"
+                       break
+               fi
+       done
+}
+
 export_bootdevice() {
        local cmdline uuid blockdev uevent line class
        local MAJOR MINOR DEVNAME DEVTYPE
@@ -162,9 +199,11 @@ export_bootdevice() {
                                fi
                        done
                ;;
+               PARTUUID=????????-????-????-????-??????????0?/PARTNROFF=1 | \
                PARTUUID=????????-????-????-????-??????????02)
                        uuid="${rootpart#PARTUUID=}"
-                       uuid="${uuid%02}00"
+                       uuid="${uuid%/PARTNROFF=1}"
+                       uuid="${uuid%0?}00"
                        for disk in $(find /dev -type b); do
                                set -- $(dd if=$disk bs=1 skip=568 count=16 2>/dev/null | hexdump -v -e '8/1 "%02x "" "2/1 "%02x""-"6/1 "%02x"')
                                if [ "$4$3$2$1-$6$5-$8$7-$9" = "$uuid" ]; then
@@ -174,7 +213,11 @@ export_bootdevice() {
                        done
                ;;
                /dev/*)
-                       uevent="/sys/class/block/${rootpart##*/}/../uevent"
+                       if [ "$rootpart" = "/dev/fit0" ]; then
+                               uevent="/sys/class/block/$(fitblk_get_bootdev)/uevent"
+                       else
+                               uevent="/sys/class/block/${rootpart##*/}/../uevent"
+                       fi
                ;;
                0x[a-f0-9][a-f0-9][a-f0-9] | 0x[a-f0-9][a-f0-9][a-f0-9][a-f0-9] | \
                [a-f0-9][a-f0-9][a-f0-9] | [a-f0-9][a-f0-9][a-f0-9][a-f0-9])
@@ -210,7 +253,7 @@ export_partdevice() {
                while read line; do
                        export -n "$line"
                done < "$uevent"
-               if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
+               if [ "$BOOTDEV_MAJOR" = "$MAJOR" -a $(($BOOTDEV_MINOR + $offset)) = "$MINOR" -a -b "/dev/$DEVNAME" ]; then
                        export "$var=$DEVNAME"
                        return 0
                fi
@@ -227,15 +270,6 @@ hex_le32_to_cpu() {
        echo "$@"
 }
 
-get_partition_by_name() {
-       for partname in /sys/class/block/$1/*/name; do
-               [ "$(cat ${partname})" = "$2" ] && {
-                       basename ${partname%%/name}
-                       break
-               }
-       done
-}
-
 get_partitions() { # <device> <filename>
        local disk="$1"
        local filename="$2"
@@ -261,7 +295,7 @@ get_partitions() { # <device> <filename>
                                local type="$1"
                                local lba="$(( $(hex_le32_to_cpu $4) * 0x100000000 + $(hex_le32_to_cpu $3) ))"
                                local end="$(( $(hex_le32_to_cpu $6) * 0x100000000 + $(hex_le32_to_cpu $5) ))"
-                               local num="$(( $end - $lba ))"
+                               local num="$(( $end - $lba + 1 ))"
 
                                [ "$type" = "00000000000000000000000000000000" ] && continue