base-files: don't evaluate block-device uevent
[openwrt/openwrt.git] / package / base-files / files / lib / upgrade / common.sh
index 428ec735d62e8a2fd7cad973dacc20c0a81b5826..5f5c9dc8a318773a8f4007d5911ae897431638d2 100644 (file)
@@ -101,7 +101,7 @@ get_magic_long() {
 }
 
 export_bootdevice() {
-       local cmdline uuid disk uevent
+       local cmdline uuid disk uevent line
        local MAJOR MINOR DEVNAME DEVTYPE
 
        if read cmdline < /proc/cmdline; then
@@ -134,8 +134,9 @@ export_bootdevice() {
                esac
 
                if [ -e "$uevent" ]; then
-                       . "$uevent"
-
+                       while read line; do
+                               export -n "$line"
+                       done < "$uevent"
                        export BOOTDEV_MAJOR=$MAJOR
                        export BOOTDEV_MINOR=$MINOR
                        return 0
@@ -147,10 +148,12 @@ export_bootdevice() {
 
 export_partdevice() {
        local var="$1" offset="$2"
-       local uevent MAJOR MINOR DEVNAME DEVTYPE
+       local uevent line MAJOR MINOR DEVNAME DEVTYPE
 
        for uevent in /sys/class/block/*/uevent; do
-               . "$uevent"
+               while read line; do
+                       export -n "$line"
+               done < "$uevent"
                if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
                        export "$var=$DEVNAME"
                        return 0
@@ -160,6 +163,14 @@ export_partdevice() {
        return 1
 }
 
+hex_le32_to_cpu() {
+       [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" == "3031" ] && {
+               echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
+               return
+       }
+       echo "$@"
+}
+
 get_partitions() { # <device> <filename>
        local disk="$1"
        local filename="$2"
@@ -167,8 +178,8 @@ get_partitions() { # <device> <filename>
        if [ -b "$disk" -o -f "$disk" ]; then
                v "Reading partition table from $filename..."
 
-               local magic="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x%04X"' "$disk")"
-               if [ "$magic" != 0xAA55 ]; then
+               local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null)
+               if [ "$magic" != $'\x55\xAA' ]; then
                        v "Invalid partition table on $disk"
                        exit
                fi
@@ -179,9 +190,9 @@ get_partitions() { # <device> <filename>
                for part in 1 2 3 4; do
                        set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
 
-                       local type="$(($1 % 256))"
-                       local lba="$(($2))"
-                       local num="$(($3))"
+                       local type="$(( $(hex_le32_to_cpu $1) % 256))"
+                       local lba="$(( $(hex_le32_to_cpu $2) ))"
+                       local num="$(( $(hex_le32_to_cpu $3) ))"
 
                        [ $type -gt 0 ] || continue