base-files: quote values when evaluating uevent
[oweals/openwrt.git] / package / base-files / files / lib / upgrade / common.sh
1 #!/bin/sh
2
3 RAM_ROOT=/tmp/root
4
5 [ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
6 libs() { ldd $* 2>/dev/null | sed -r '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         [ "$VERBOSE" -ge 1 ] && echo "$@"
66 }
67
68 json_string() {
69         local v="$1"
70         v="${v//\\/\\\\}"
71         v="${v//\"/\\\"}"
72         echo "\"$v\""
73 }
74
75 rootfs_type() {
76         /bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
77 }
78
79 get_image() { # <source> [ <command> ]
80         local from="$1"
81         local cat="$2"
82
83         if [ -z "$cat" ]; then
84                 local magic="$(dd if="$from" bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
85                 case "$magic" in
86                         1f8b) cat="zcat";;
87                         425a) cat="bzcat";;
88                         *) cat="cat";;
89                 esac
90         fi
91
92         $cat "$from" 2>/dev/null
93 }
94
95 get_magic_word() {
96         (get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null
97 }
98
99 get_magic_long() {
100         (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
101 }
102
103 export_bootdevice() {
104         local cmdline uuid disk uevent
105         local MAJOR MINOR DEVNAME DEVTYPE
106
107         if read cmdline < /proc/cmdline; then
108                 case "$cmdline" in
109                         *block2mtd=*)
110                                 disk="${cmdline##*block2mtd=}"
111                                 disk="${disk%%,*}"
112                         ;;
113                         *root=*)
114                                 disk="${cmdline##*root=}"
115                                 disk="${disk%% *}"
116                         ;;
117                 esac
118
119                 case "$disk" in
120                         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]-02)
121                                 uuid="${disk#PARTUUID=}"
122                                 uuid="${uuid%-02}"
123                                 for disk in $(find /dev -type b); do
124                                         set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
125                                         if [ "$4$3$2$1" = "$uuid" ]; then
126                                                 uevent="/sys/class/block/${disk##*/}/uevent"
127                                                 break
128                                         fi
129                                 done
130                         ;;
131                         /dev/*)
132                                 uevent="/sys/class/block/${disk##*/}/uevent"
133                         ;;
134                 esac
135
136                 if [ -e "$uevent" ]; then
137                         eval "$(sed "s/=\(.*\)/=\'\1\'/" < "$uevent")"
138                         export BOOTDEV_MAJOR=$MAJOR
139                         export BOOTDEV_MINOR=$MINOR
140                         return 0
141                 fi
142         fi
143
144         return 1
145 }
146
147 export_partdevice() {
148         local var="$1" offset="$2"
149         local uevent MAJOR MINOR DEVNAME DEVTYPE
150
151         for uevent in /sys/class/block/*/uevent; do
152                 eval "$(sed "s/=\(.*\)/=\'\1\'/" < "$uevent")"
153                 if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
154                         export "$var=$DEVNAME"
155                         return 0
156                 fi
157         done
158
159         return 1
160 }
161
162 hex_le32_to_cpu() {
163         [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" == "3031" ] && {
164                 echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
165                 return
166         }
167         echo "$@"
168 }
169
170 get_partitions() { # <device> <filename>
171         local disk="$1"
172         local filename="$2"
173
174         if [ -b "$disk" -o -f "$disk" ]; then
175                 v "Reading partition table from $filename..."
176
177                 local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null)
178                 if [ "$magic" != $'\x55\xAA' ]; then
179                         v "Invalid partition table on $disk"
180                         exit
181                 fi
182
183                 rm -f "/tmp/partmap.$filename"
184
185                 local part
186                 for part in 1 2 3 4; do
187                         set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
188
189                         local type="$(( $(hex_le32_to_cpu $1) % 256))"
190                         local lba="$(( $(hex_le32_to_cpu $2) ))"
191                         local num="$(( $(hex_le32_to_cpu $3) ))"
192
193                         [ $type -gt 0 ] || continue
194
195                         printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
196                 done
197         fi
198 }
199
200 jffs2_copy_config() {
201         if grep rootfs_data /proc/mtd >/dev/null; then
202                 # squashfs+jffs2
203                 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
204         else
205                 # jffs2
206                 mtd jffs2write "$CONF_TAR" rootfs
207         fi
208 }
209
210 # Flash firmware to MTD partition
211 #
212 # $(1): path to image
213 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
214 default_do_upgrade() {
215         sync
216         if [ "$SAVE_CONFIG" -eq 1 ]; then
217                 get_image "$1" "$2" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
218         else
219                 get_image "$1" "$2" | mtd write - "${PART_NAME:-image}"
220         fi
221 }
222
223 do_upgrade_stage2() {
224         v "Performing system upgrade..."
225         if [ -n "$do_upgrade" ]; then
226                 eval "$do_upgrade"
227         elif type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
228                 platform_do_upgrade "$IMAGE"
229         else
230                 default_do_upgrade "$IMAGE"
231         fi
232
233         if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then
234                 platform_copy_config
235         fi
236
237         v "Upgrade completed"
238         sleep 1
239
240         v "Rebooting system..."
241         umount -a
242         reboot -f
243         sleep 5
244         echo b 2>/dev/null >/proc/sysrq-trigger
245 }