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