73ab5ef0794c942495d18865fa2c2498b84e63e6
[openwrt.git] / target / linux / x86 / base-files / lib / upgrade / platform.sh
1 platform_export_bootpart() {
2         local cmdline uuid disk
3
4         if read cmdline < /proc/cmdline; then
5                 case "$cmdline" in
6                         *block2mtd=*)
7                                 disk="${cmdline##*block2mtd=}"
8                                 disk="${disk%%,*}"
9                         ;;
10                         *root=*)
11                                 disk="${cmdline##*root=}"
12                                 disk="${disk%% *}"
13                         ;;
14                 esac
15
16                 case "$disk" in
17                         PARTUUID=[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]-02)
18                                 uuid="${disk#PARTUUID=}"
19                                 uuid="${uuid%-02}"
20                                 for disk in /dev/[hsv]d[a-z]; do
21                                         set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
22                                         if [ "$4$3$2$1" = "$uuid" ]; then
23                                                 export BOOTPART="${disk}1"
24                                                 return 0
25                                         fi
26                                 done
27                         ;;
28                         /dev/*)
29                                 export BOOTPART="${disk%[0-9]}1"
30                                 return 0
31                         ;;
32                 esac
33         fi
34
35         return 1
36 }
37
38 platform_check_image() {
39         [ "$#" -gt 1 ] && return 1
40
41         case "$(get_magic_word "$1")" in
42                 eb48|eb63) return 0;;
43                 *)
44                         echo "Invalid image type"
45                         return 1
46                 ;;
47         esac
48 }
49
50 platform_copy_config() {
51         if [ -b "$BOOTPART" ]; then
52                 mount -t ext4 -o rw,noatime "$BOOTPART" /mnt
53                 cp -af "$CONF_TAR" /mnt/
54                 umount /mnt
55         fi
56 }
57
58 platform_do_upgrade() {
59         platform_export_bootpart
60
61         if [ -b "${BOOTPART%[0-9]}" ]; then
62                 sync
63                 get_image "$@" | dd of="${BOOTPART%[0-9]}" bs=4096 conv=fsync
64                 sleep 1
65         fi
66 }