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