base-files: remove block2mtd checks from sysupgrade
[openwrt/staging/rmilecki.git] / package / base-files / files / lib / upgrade / common.sh
1 #!/bin/sh
2
3 RAM_ROOT=/tmp/root
4
5 export BACKUP_FILE=sysupgrade.tgz # file extracted by preinit
6
7 [ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
8 libs() { ldd $* 2>/dev/null | sed -E 's/(.* => )?(.*) .*/\2/'; }
9
10 install_file() { # <file> [ <file> ... ]
11 local target dest dir
12 for file in "$@"; do
13 if [ -L "$file" ]; then
14 target="$(readlink -f "$file")"
15 dest="$RAM_ROOT/$file"
16 [ ! -f "$dest" ] && {
17 dir="$(dirname "$dest")"
18 mkdir -p "$dir"
19 ln -s "$target" "$dest"
20 }
21 file="$target"
22 fi
23 dest="$RAM_ROOT/$file"
24 [ -f "$file" -a ! -f "$dest" ] && {
25 dir="$(dirname "$dest")"
26 mkdir -p "$dir"
27 cp "$file" "$dest"
28 }
29 done
30 }
31
32 install_bin() {
33 local src files
34 src=$1
35 files=$1
36 [ -x "$src" ] && files="$src $(libs $src)"
37 install_file $files
38 }
39
40 run_hooks() {
41 local arg="$1"; shift
42 for func in "$@"; do
43 eval "$func $arg"
44 done
45 }
46
47 ask_bool() {
48 local default="$1"; shift;
49 local answer="$default"
50
51 [ "$INTERACTIVE" -eq 1 ] && {
52 case "$default" in
53 0) echo -n "$* (y/N): ";;
54 *) echo -n "$* (Y/n): ";;
55 esac
56 read answer
57 case "$answer" in
58 y*) answer=1;;
59 n*) answer=0;;
60 *) answer="$default";;
61 esac
62 }
63 [ "$answer" -gt 0 ]
64 }
65
66 v() {
67 [ -n "$VERBOSE" ] && [ "$VERBOSE" -ge 1 ] && echo "$@"
68 }
69
70 json_string() {
71 local v="$1"
72 v="${v//\\/\\\\}"
73 v="${v//\"/\\\"}"
74 echo "\"$v\""
75 }
76
77 rootfs_type() {
78 /bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
79 }
80
81 get_image() { # <source> [ <command> ]
82 local from="$1"
83 local cmd="$2"
84
85 if [ -z "$cmd" ]; then
86 local magic="$(dd if="$from" bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
87 case "$magic" in
88 1f8b) cmd="zcat";;
89 425a) cmd="bzcat";;
90 *) cmd="cat";;
91 esac
92 fi
93
94 cat "$from" 2>/dev/null | $cmd
95 }
96
97 get_magic_word() {
98 (get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null
99 }
100
101 get_magic_long() {
102 (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
103 }
104
105 get_magic_gpt() {
106 (get_image "$@" | dd bs=8 count=1 skip=64) 2>/dev/null
107 }
108
109 get_magic_vfat() {
110 (get_image "$@" | dd bs=1 count=3 skip=54) 2>/dev/null
111 }
112
113 part_magic_efi() {
114 local magic=$(get_magic_gpt "$@")
115 [ "$magic" = "EFI PART" ]
116 }
117
118 part_magic_fat() {
119 local magic=$(get_magic_vfat "$@")
120 [ "$magic" = "FAT" ]
121 }
122
123 export_bootdevice() {
124 local cmdline bootdisk rootpart uuid blockdev uevent line class
125 local MAJOR MINOR DEVNAME DEVTYPE
126
127 if read cmdline < /proc/cmdline; then
128 case "$cmdline" in
129 *root=*)
130 rootpart="${cmdline##*root=}"
131 rootpart="${rootpart%% *}"
132 ;;
133 esac
134
135 case "$bootdisk" in
136 /dev/*)
137 uevent="/sys/class/block/${bootdisk##*/}/uevent"
138 ;;
139 esac
140
141 case "$rootpart" in
142 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]-[a-f0-9][a-f0-9])
143 uuid="${rootpart#PARTUUID=}"
144 uuid="${uuid%-[a-f0-9][a-f0-9]}"
145 for blockdev in $(find /dev -type b); do
146 set -- $(dd if=$blockdev bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
147 if [ "$4$3$2$1" = "$uuid" ]; then
148 uevent="/sys/class/block/${blockdev##*/}/uevent"
149 break
150 fi
151 done
152 ;;
153 PARTUUID=????????-????-????-????-??????????02)
154 uuid="${rootpart#PARTUUID=}"
155 uuid="${uuid%02}00"
156 for disk in $(find /dev -type b); do
157 set -- $(dd if=$disk bs=1 skip=568 count=16 2>/dev/null | hexdump -v -e '8/1 "%02x "" "2/1 "%02x""-"6/1 "%02x"')
158 if [ "$4$3$2$1-$6$5-$8$7-$9" = "$uuid" ]; then
159 uevent="/sys/class/block/${disk##*/}/uevent"
160 break
161 fi
162 done
163 ;;
164 /dev/*)
165 uevent="/sys/class/block/${rootpart##*/}/../uevent"
166 ;;
167 0x[a-f0-9][a-f0-9][a-f0-9] | 0x[a-f0-9][a-f0-9][a-f0-9][a-f0-9] | \
168 [a-f0-9][a-f0-9][a-f0-9] | [a-f0-9][a-f0-9][a-f0-9][a-f0-9])
169 rootpart=0x${rootpart#0x}
170 for class in /sys/class/block/*; do
171 while read line; do
172 export -n "$line"
173 done < "$class/uevent"
174 if [ $((rootpart/256)) = $MAJOR -a $((rootpart%256)) = $MINOR ]; then
175 uevent="$class/../uevent"
176 fi
177 done
178 ;;
179 esac
180
181 if [ -e "$uevent" ]; then
182 while read line; do
183 export -n "$line"
184 done < "$uevent"
185 export BOOTDEV_MAJOR=$MAJOR
186 export BOOTDEV_MINOR=$MINOR
187 return 0
188 fi
189 fi
190
191 return 1
192 }
193
194 export_partdevice() {
195 local var="$1" offset="$2"
196 local uevent line MAJOR MINOR DEVNAME DEVTYPE
197
198 for uevent in /sys/class/block/*/uevent; do
199 while read line; do
200 export -n "$line"
201 done < "$uevent"
202 if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
203 export "$var=$DEVNAME"
204 return 0
205 fi
206 done
207
208 return 1
209 }
210
211 hex_le32_to_cpu() {
212 [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" = "3031" ] && {
213 echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
214 return
215 }
216 echo "$@"
217 }
218
219 get_partitions() { # <device> <filename>
220 local disk="$1"
221 local filename="$2"
222
223 if [ -b "$disk" -o -f "$disk" ]; then
224 v "Reading partition table from $filename..."
225
226 local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null)
227 if [ "$magic" != $'\x55\xAA' ]; then
228 v "Invalid partition table on $disk"
229 exit
230 fi
231
232 rm -f "/tmp/partmap.$filename"
233
234 local part
235 part_magic_efi "$disk" && {
236 #export_partdevice will fail when partition number is greater than 15, as
237 #the partition major device number is not equal to the disk major device number
238 for part in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
239 set -- $(hexdump -v -n 48 -s "$((0x380 + $part * 0x80))" -e '4/4 "%08x"" "4/4 "%08x"" "4/4 "0x%08X "' "$disk")
240
241 local type="$1"
242 local lba="$(( $(hex_le32_to_cpu $4) * 0x100000000 + $(hex_le32_to_cpu $3) ))"
243 local end="$(( $(hex_le32_to_cpu $6) * 0x100000000 + $(hex_le32_to_cpu $5) ))"
244 local num="$(( $end - $lba ))"
245
246 [ "$type" = "00000000000000000000000000000000" ] && continue
247
248 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
249 done
250 } || {
251 for part in 1 2 3 4; do
252 set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
253
254 local type="$(( $(hex_le32_to_cpu $1) % 256))"
255 local lba="$(( $(hex_le32_to_cpu $2) ))"
256 local num="$(( $(hex_le32_to_cpu $3) ))"
257
258 [ $type -gt 0 ] || continue
259
260 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
261 done
262 }
263 fi
264 }
265
266 indicate_upgrade() {
267 . /etc/diag.sh
268 set_state upgrade
269 }
270
271 # Flash firmware to MTD partition
272 #
273 # $(1): path to image
274 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
275 default_do_upgrade() {
276 sync
277 if [ -n "$UPGRADE_BACKUP" ]; then
278 get_image "$1" "$2" | mtd $MTD_ARGS $MTD_CONFIG_ARGS -j "$UPGRADE_BACKUP" write - "${PART_NAME:-image}"
279 else
280 get_image "$1" "$2" | mtd $MTD_ARGS write - "${PART_NAME:-image}"
281 fi
282 [ $? -ne 0 ] && exit 1
283 }