busybox: disable bzip2
[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 _vn() {
69 [ -n "$VERBOSE" ] && [ "$VERBOSE" -ge 1 ] && echo -n "$*" >&2
70 }
71
72 v() {
73 _v "$(date) upgrade: $@"
74 }
75
76 vn() {
77 _vn "$(date) upgrade: $@"
78 }
79
80 json_string() {
81 local v="$1"
82 v="${v//\\/\\\\}"
83 v="${v//\"/\\\"}"
84 echo "\"$v\""
85 }
86
87 rootfs_type() {
88 /bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
89 }
90
91 get_image() { # <source> [ <command> ]
92 local from="$1"
93 local cmd="$2"
94
95 if [ -z "$cmd" ]; then
96 local magic="$(dd if="$from" bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
97 case "$magic" in
98 1f8b) cmd="zcat";;
99 *) cmd="cat";;
100 esac
101 fi
102
103 $cmd <"$from"
104 }
105
106 get_image_dd() {
107 local from="$1"; shift
108
109 (
110 exec 3>&2
111 ( exec 3>&2; get_image "$from" 2>&1 1>&3 | grep -v -F ' Broken pipe' ) 2>&1 1>&3 \
112 | ( exec 3>&2; dd "$@" 2>&1 1>&3 | grep -v -E ' records (in|out)') 2>&1 1>&3
113 exec 3>&-
114 )
115 }
116
117 get_magic_word() {
118 (get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null
119 }
120
121 get_magic_long() {
122 (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
123 }
124
125 get_magic_gpt() {
126 (get_image "$@" | dd bs=8 count=1 skip=64) 2>/dev/null
127 }
128
129 get_magic_vfat() {
130 (get_image "$@" | dd bs=3 count=1 skip=18) 2>/dev/null
131 }
132
133 get_magic_fat32() {
134 (get_image "$@" | dd bs=1 count=5 skip=82) 2>/dev/null
135 }
136
137 part_magic_efi() {
138 local magic=$(get_magic_gpt "$@")
139 [ "$magic" = "EFI PART" ]
140 }
141
142 part_magic_fat() {
143 local magic=$(get_magic_vfat "$@")
144 local magic_fat32=$(get_magic_fat32 "$@")
145 [ "$magic" = "FAT" ] || [ "$magic_fat32" = "FAT32" ]
146 }
147
148 export_bootdevice() {
149 local cmdline uuid blockdev uevent line class
150 local MAJOR MINOR DEVNAME DEVTYPE
151 local rootpart="$(cmdline_get_var root)"
152
153 case "$rootpart" in
154 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])
155 uuid="${rootpart#PARTUUID=}"
156 uuid="${uuid%-[a-f0-9][a-f0-9]}"
157 for blockdev in $(find /dev -type b); do
158 set -- $(dd if=$blockdev bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
159 if [ "$4$3$2$1" = "$uuid" ]; then
160 uevent="/sys/class/block/${blockdev##*/}/uevent"
161 break
162 fi
163 done
164 ;;
165 PARTUUID=????????-????-????-????-??????????02)
166 uuid="${rootpart#PARTUUID=}"
167 uuid="${uuid%02}00"
168 for disk in $(find /dev -type b); do
169 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"')
170 if [ "$4$3$2$1-$6$5-$8$7-$9" = "$uuid" ]; then
171 uevent="/sys/class/block/${disk##*/}/uevent"
172 break
173 fi
174 done
175 ;;
176 /dev/*)
177 uevent="/sys/class/block/${rootpart##*/}/../uevent"
178 ;;
179 0x[a-f0-9][a-f0-9][a-f0-9] | 0x[a-f0-9][a-f0-9][a-f0-9][a-f0-9] | \
180 [a-f0-9][a-f0-9][a-f0-9] | [a-f0-9][a-f0-9][a-f0-9][a-f0-9])
181 rootpart=0x${rootpart#0x}
182 for class in /sys/class/block/*; do
183 while read line; do
184 export -n "$line"
185 done < "$class/uevent"
186 if [ $((rootpart/256)) = $MAJOR -a $((rootpart%256)) = $MINOR ]; then
187 uevent="$class/../uevent"
188 fi
189 done
190 ;;
191 esac
192
193 if [ -e "$uevent" ]; then
194 while read line; do
195 export -n "$line"
196 done < "$uevent"
197 export BOOTDEV_MAJOR=$MAJOR
198 export BOOTDEV_MINOR=$MINOR
199 return 0
200 fi
201
202 return 1
203 }
204
205 export_partdevice() {
206 local var="$1" offset="$2"
207 local uevent line MAJOR MINOR DEVNAME DEVTYPE
208
209 for uevent in /sys/class/block/*/uevent; do
210 while read line; do
211 export -n "$line"
212 done < "$uevent"
213 if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
214 export "$var=$DEVNAME"
215 return 0
216 fi
217 done
218
219 return 1
220 }
221
222 hex_le32_to_cpu() {
223 [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" = "3031" ] && {
224 echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
225 return
226 }
227 echo "$@"
228 }
229
230 get_partition_by_name() {
231 for partname in /sys/class/block/$1/*/name; do
232 [ "$(cat ${partname})" = "$2" ] && {
233 basename ${partname%%/name}
234 break
235 }
236 done
237 }
238
239 get_partitions() { # <device> <filename>
240 local disk="$1"
241 local filename="$2"
242
243 if [ -b "$disk" -o -f "$disk" ]; then
244 v "Reading partition table from $filename..."
245
246 local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null)
247 if [ "$magic" != $'\x55\xAA' ]; then
248 v "Invalid partition table on $disk"
249 exit
250 fi
251
252 rm -f "/tmp/partmap.$filename"
253
254 local part
255 part_magic_efi "$disk" && {
256 #export_partdevice will fail when partition number is greater than 15, as
257 #the partition major device number is not equal to the disk major device number
258 for part in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
259 set -- $(hexdump -v -n 48 -s "$((0x380 + $part * 0x80))" -e '4/4 "%08x"" "4/4 "%08x"" "4/4 "0x%08X "' "$disk")
260
261 local type="$1"
262 local lba="$(( $(hex_le32_to_cpu $4) * 0x100000000 + $(hex_le32_to_cpu $3) ))"
263 local end="$(( $(hex_le32_to_cpu $6) * 0x100000000 + $(hex_le32_to_cpu $5) ))"
264 local num="$(( $end - $lba ))"
265
266 [ "$type" = "00000000000000000000000000000000" ] && continue
267
268 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
269 done
270 } || {
271 for part in 1 2 3 4; do
272 set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
273
274 local type="$(( $(hex_le32_to_cpu $1) % 256))"
275 local lba="$(( $(hex_le32_to_cpu $2) ))"
276 local num="$(( $(hex_le32_to_cpu $3) ))"
277
278 [ $type -gt 0 ] || continue
279
280 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
281 done
282 }
283 fi
284 }
285
286 indicate_upgrade() {
287 . /etc/diag.sh
288 set_state upgrade
289 }
290
291 # Flash firmware to MTD partition
292 #
293 # $(1): path to image
294 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
295 default_do_upgrade() {
296 sync
297 echo 3 > /proc/sys/vm/drop_caches
298 if [ -n "$UPGRADE_BACKUP" ]; then
299 get_image "$1" "$2" | mtd $MTD_ARGS $MTD_CONFIG_ARGS -j "$UPGRADE_BACKUP" write - "${PART_NAME:-image}"
300 else
301 get_image "$1" "$2" | mtd $MTD_ARGS write - "${PART_NAME:-image}"
302 fi
303 [ $? -ne 0 ] && exit 1
304 }