f12deebf6484df6f3f69e453ad67688a76d57972
[openwrt/svn-archive/archive.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] /dev/xvd[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 get_partitions() { # <device> <filename>
59 local disk="$1"
60 local filename="$2"
61
62 if [ -b "$disk" -o -f "$disk" ]; then
63 echo "Reading partition table from $filename..."
64
65 local magic="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x%04X"' "$disk")"
66 if [ "$magic" != 0xAA55 ]; then
67 echo "Invalid partition table on $disk"
68 exit
69 fi
70
71 rm -f "/tmp/partmap.$filename"
72
73 local part
74 for part in 1 2 3 4; do
75 set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
76
77 local type="$(($1 % 256))"
78 local lba="$(($2))"
79 local num="$(($3))"
80
81 [ $type -gt 0 ] || continue
82
83 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
84 done
85 fi
86 }
87
88 platform_do_upgrade() {
89 platform_export_bootpart
90 disk="${BOOTPART%[0-9]}"
91
92 if [ -b "$disk" ]; then
93 sync
94 if [ "$SAVE_PARTITIONS" = "1" ]; then
95 get_partitions "$disk" bootdisk
96
97
98 #get block size
99 if [ -f "/sys/block/${disk##*/}/queue/physical_block_size" ]; then
100 ibs="$(cat "/sys/block/${disk##*/}/queue/physical_block_size")"
101 else
102 ibs=512
103 fi
104
105 #extract the boot sector from the image
106 get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
107
108 get_partitions /tmp/image.bs image
109
110 #compare tables
111 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
112 if [ -n "$diff" ]; then
113 echo "Partition layout is changed. Full image will be written."
114 ask_bool 0 "Abort" && exit
115
116 get_image "$@" | dd of="$disk" bs=4096 conv=fsync
117 return 0
118 fi
119
120 #iterate over each partition from the image and write it to the boot disk
121 while read part start size; do
122 echo "Writing image to $disk$part..."
123 get_image "$@" | dd of="$disk$part" ibs="$ibs" obs=1M skip="$start" count="$size" conv=fsync
124 done < /tmp/partmap.image
125
126 #copy partition uuid
127 echo "Writing new UUID to $disk$part..."
128 get_image "$@" | dd of="$disk" bs=1 skip=440 count=4 seek=440 conv=fsync
129 else
130 get_image "$@" | dd of="$disk" bs=4096 conv=fsync
131 fi
132
133 sleep 1
134 fi
135 }