c8bc3f7f608fc82ee3afc049b64af3a740fd2c37
[openwrt/staging/yousong.git] / target / linux / x86 / base-files / lib / upgrade / platform.sh
1 platform_export_partdevice() {
2 local var="$1" offset="$2"
3 local uevent MAJOR MINOR DEVNAME DEVTYPE
4
5 for uevent in /sys/class/block/*/uevent; do
6 . "$uevent"
7 if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
8 export "$var=$DEVNAME"
9 return 0
10 fi
11 done
12
13 return 1
14 }
15
16 platform_export_bootdevice() {
17 local cmdline uuid disk uevent
18 local MAJOR MINOR DEVNAME DEVTYPE
19
20 if read cmdline < /proc/cmdline; then
21 case "$cmdline" in
22 *block2mtd=*)
23 disk="${cmdline##*block2mtd=}"
24 disk="${disk%%,*}"
25 ;;
26 *root=*)
27 disk="${cmdline##*root=}"
28 disk="${disk%% *}"
29 ;;
30 esac
31
32 case "$disk" in
33 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)
34 uuid="${disk#PARTUUID=}"
35 uuid="${uuid%-02}"
36 for disk in $(find /dev -type b); do
37 set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
38 if [ "$4$3$2$1" = "$uuid" ]; then
39 uevent="/sys/class/block/${disk##*/}/uevent"
40 break
41 fi
42 done
43 ;;
44 /dev/*)
45 uevent="/sys/class/block/${disk##*/}/uevent"
46 ;;
47 esac
48
49 if [ -e "$uevent" ]; then
50 . "$uevent"
51
52 export BOOTDEV_MAJOR=$MAJOR
53 export BOOTDEV_MINOR=$MINOR
54
55 return 0
56 fi
57 fi
58
59 return 1
60 }
61
62 platform_check_image() {
63 [ "$#" -gt 1 ] && return 1
64
65 case "$(get_magic_word "$1")" in
66 eb48|eb63) return 0;;
67 *)
68 echo "Invalid image type"
69 return 1
70 ;;
71 esac
72 }
73
74 platform_copy_config() {
75 local partdev
76
77 if platform_export_partdevice partdev 1; then
78 mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt
79 cp -af "$CONF_TAR" /mnt/
80 umount /mnt
81 fi
82 }
83
84 get_partitions() { # <device> <filename>
85 local disk="$1"
86 local filename="$2"
87
88 if [ -b "$disk" -o -f "$disk" ]; then
89 echo "Reading partition table from $filename..."
90
91 local magic="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x%04X"' "$disk")"
92 if [ "$magic" != 0xAA55 ]; then
93 echo "Invalid partition table on $disk"
94 exit
95 fi
96
97 rm -f "/tmp/partmap.$filename"
98
99 local part
100 for part in 1 2 3 4; do
101 set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
102
103 local type="$(($1 % 256))"
104 local lba="$(($2))"
105 local num="$(($3))"
106
107 [ $type -gt 0 ] || continue
108
109 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
110 done
111 fi
112 }
113
114 platform_do_upgrade() {
115 local diskdev partdev ibs diff
116
117 if platform_export_bootdevice && platform_export_partdevice diskdev 0; then
118 sync
119 if [ "$SAVE_PARTITIONS" = "1" ]; then
120 get_partitions "/dev/$diskdev" bootdisk
121
122 #get block size
123 if [ -f "/sys/block/$diskdev/queue/physical_block_size" ]; then
124 ibs="$(cat "/sys/block/$diskdev/queue/physical_block_size")"
125 else
126 ibs=512
127 fi
128
129 #extract the boot sector from the image
130 get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
131
132 get_partitions /tmp/image.bs image
133
134 #compare tables
135 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
136 if [ -n "$diff" ]; then
137 echo "Partition layout is changed. Full image will be written."
138 ask_bool 0 "Abort" && exit
139
140 get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
141 return 0
142 fi
143
144 #iterate over each partition from the image and write it to the boot disk
145 while read part start size; do
146 if platform_export_partdevice partdev $part; then
147 echo "Writing image to /dev/$partdev..."
148 get_image "$@" | dd of="/dev/$partdev" ibs="$ibs" obs=1M skip="$start" count="$size" conv=fsync
149 else
150 echo "Unable to find partition $part device, skipped."
151 fi
152 done < /tmp/partmap.image
153
154 #copy partition uuid
155 echo "Writing new UUID to /dev/$diskdev..."
156 get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
157 else
158 get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
159 fi
160
161 sleep 1
162 fi
163 }