29eac77dfb0ae52f241696f3f62dce7d16106b20
[openwrt/staging/dedeckeh.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/*; do
21 [ -b "$disk" ] || continue
22 set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
23 if [ "$4$3$2$1" = "$uuid" ]; then
24 export BOOTPART="${disk}1"
25 return 0
26 fi
27 done
28 ;;
29 /dev/*)
30 export BOOTPART="${disk%[0-9]}1"
31 return 0
32 ;;
33 esac
34 fi
35
36 return 1
37 }
38
39 platform_check_image() {
40 [ "$#" -gt 1 ] && return 1
41
42 case "$(get_magic_word "$1")" in
43 eb48|eb63) return 0;;
44 *)
45 echo "Invalid image type"
46 return 1
47 ;;
48 esac
49 }
50
51 platform_copy_config() {
52 if [ -b "$BOOTPART" ]; then
53 mount -t ext4 -o rw,noatime "$BOOTPART" /mnt
54 cp -af "$CONF_TAR" /mnt/
55 umount /mnt
56 fi
57 }
58
59 get_partitions() { # <device> <filename>
60 local disk="$1"
61 local filename="$2"
62
63 if [ -b "$disk" -o -f "$disk" ]; then
64 echo "Reading partition table from $filename..."
65
66 local magic="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x%04X"' "$disk")"
67 if [ "$magic" != 0xAA55 ]; then
68 echo "Invalid partition table on $disk"
69 exit
70 fi
71
72 rm -f "/tmp/partmap.$filename"
73
74 local part
75 for part in 1 2 3 4; do
76 set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
77
78 local type="$(($1 % 256))"
79 local lba="$(($2))"
80 local num="$(($3))"
81
82 [ $type -gt 0 ] || continue
83
84 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
85 done
86 fi
87 }
88
89 platform_do_upgrade() {
90 platform_export_bootpart
91 disk="${BOOTPART%[0-9]}"
92
93 if [ -b "$disk" ]; then
94 sync
95 if [ "$SAVE_PARTITIONS" = "1" ]; then
96 get_partitions "$disk" bootdisk
97
98
99 #get block size
100 if [ -f "/sys/block/${disk##*/}/queue/physical_block_size" ]; then
101 ibs="$(cat "/sys/block/${disk##*/}/queue/physical_block_size")"
102 else
103 ibs=512
104 fi
105
106 #extract the boot sector from the image
107 get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
108
109 get_partitions /tmp/image.bs image
110
111 #compare tables
112 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
113 if [ -n "$diff" ]; then
114 echo "Partition layout is changed. Full image will be written."
115 ask_bool 0 "Abort" && exit
116
117 get_image "$@" | dd of="$disk" bs=4096 conv=fsync
118 return 0
119 fi
120
121 #iterate over each partition from the image and write it to the boot disk
122 while read part start size; do
123 echo "Writing image to $disk$part..."
124 get_image "$@" | dd of="$disk$part" ibs="$ibs" obs=1M skip="$start" count="$size" conv=fsync
125 done < /tmp/partmap.image
126
127 #copy partition uuid
128 echo "Writing new UUID to $disk$part..."
129 get_image "$@" | dd of="$disk" bs=1 skip=440 count=4 seek=440 conv=fsync
130 else
131 get_image "$@" | dd of="$disk" bs=4096 conv=fsync
132 fi
133
134 sleep 1
135 fi
136 }