ath79: do not build TP-Link tiny images by default
[oweals/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 "$@"
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                         *block2mtd=*)
130                                 bootdisk="${cmdline##*block2mtd=}"
131                                 bootdisk="${bootdisk%%,*}"
132                         ;;
133                         *root=*)
134                                 rootpart="${cmdline##*root=}"
135                                 rootpart="${rootpart%% *}"
136                         ;;
137                 esac
138
139                 case "$bootdisk" in
140                         /dev/*)
141                                 uevent="/sys/class/block/${bootdisk##*/}/uevent"
142                         ;;
143                 esac
144
145                 case "$rootpart" in
146                         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])
147                                 uuid="${rootpart#PARTUUID=}"
148                                 uuid="${uuid%-[a-f0-9][a-f0-9]}"
149                                 for blockdev in $(find /dev -type b); do
150                                         set -- $(dd if=$blockdev bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
151                                         if [ "$4$3$2$1" = "$uuid" ]; then
152                                                 uevent="/sys/class/block/${blockdev##*/}/uevent"
153                                                 break
154                                         fi
155                                 done
156                         ;;
157                         PARTUUID=????????-????-????-????-??????????02)
158                                 uuid="${rootpart#PARTUUID=}"
159                                 uuid="${uuid%02}00"
160                                 for disk in $(find /dev -type b); do
161                                         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"')
162                                         if [ "$4$3$2$1-$6$5-$8$7-$9" = "$uuid" ]; then
163                                                 uevent="/sys/class/block/${disk##*/}/uevent"
164                                                 break
165                                         fi
166                                 done
167                         ;;
168                         /dev/*)
169                                 uevent="/sys/class/block/${rootpart##*/}/../uevent"
170                         ;;
171                         0x[a-f0-9][a-f0-9][a-f0-9] | 0x[a-f0-9][a-f0-9][a-f0-9][a-f0-9] | \
172                         [a-f0-9][a-f0-9][a-f0-9] | [a-f0-9][a-f0-9][a-f0-9][a-f0-9])
173                                 rootpart=0x${rootpart#0x}
174                                 for class in /sys/class/block/*; do
175                                         while read line; do
176                                                 export -n "$line"
177                                         done < "$class/uevent"
178                                         if [ $((rootpart/256)) = $MAJOR -a $((rootpart%256)) = $MINOR ]; then
179                                                 uevent="$class/../uevent"
180                                         fi
181                                 done
182                         ;;
183                 esac
184
185                 if [ -e "$uevent" ]; then
186                         while read line; do
187                                 export -n "$line"
188                         done < "$uevent"
189                         export BOOTDEV_MAJOR=$MAJOR
190                         export BOOTDEV_MINOR=$MINOR
191                         return 0
192                 fi
193         fi
194
195         return 1
196 }
197
198 export_partdevice() {
199         local var="$1" offset="$2"
200         local uevent line MAJOR MINOR DEVNAME DEVTYPE
201
202         for uevent in /sys/class/block/*/uevent; do
203                 while read line; do
204                         export -n "$line"
205                 done < "$uevent"
206                 if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
207                         export "$var=$DEVNAME"
208                         return 0
209                 fi
210         done
211
212         return 1
213 }
214
215 hex_le32_to_cpu() {
216         [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" = "3031" ] && {
217                 echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
218                 return
219         }
220         echo "$@"
221 }
222
223 get_partitions() { # <device> <filename>
224         local disk="$1"
225         local filename="$2"
226
227         if [ -b "$disk" -o -f "$disk" ]; then
228                 v "Reading partition table from $filename..."
229
230                 local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null)
231                 if [ "$magic" != $'\x55\xAA' ]; then
232                         v "Invalid partition table on $disk"
233                         exit
234                 fi
235
236                 rm -f "/tmp/partmap.$filename"
237
238                 local part
239                 part_magic_efi "$disk" && {
240                         #export_partdevice will fail when partition number is greater than 15, as
241                         #the partition major device number is not equal to the disk major device number
242                         for part in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
243                                 set -- $(hexdump -v -n 48 -s "$((0x380 + $part * 0x80))" -e '4/4 "%08x"" "4/4 "%08x"" "4/4 "0x%08X "' "$disk")
244
245                                 local type="$1"
246                                 local lba="$(( $(hex_le32_to_cpu $4) * 0x100000000 + $(hex_le32_to_cpu $3) ))"
247                                 local end="$(( $(hex_le32_to_cpu $6) * 0x100000000 + $(hex_le32_to_cpu $5) ))"
248                                 local num="$(( $end - $lba ))"
249
250                                 [ "$type" = "00000000000000000000000000000000" ] && continue
251
252                                 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
253                         done
254                 } || {
255                         for part in 1 2 3 4; do
256                                 set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
257
258                                 local type="$(( $(hex_le32_to_cpu $1) % 256))"
259                                 local lba="$(( $(hex_le32_to_cpu $2) ))"
260                                 local num="$(( $(hex_le32_to_cpu $3) ))"
261
262                                 [ $type -gt 0 ] || continue
263
264                                 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
265                         done
266                 }
267         fi
268 }
269
270 indicate_upgrade() {
271         . /etc/diag.sh
272         set_state upgrade
273 }
274
275 # Flash firmware to MTD partition
276 #
277 # $(1): path to image
278 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
279 default_do_upgrade() {
280         sync
281         if [ -n "$UPGRADE_BACKUP" ]; then
282                 get_image "$1" "$2" | mtd $MTD_ARGS $MTD_CONFIG_ARGS -j "$UPGRADE_BACKUP" write - "${PART_NAME:-image}"
283         else
284                 get_image "$1" "$2" | mtd $MTD_ARGS write - "${PART_NAME:-image}"
285         fi
286         [ $? -ne 0 ] && exit 1
287 }