base-files: automatically handle paths and symlinks for RAMFS_COPY_BIN
[openwrt/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 . "$uevent"
138
139 export BOOTDEV_MAJOR=$MAJOR
140 export BOOTDEV_MINOR=$MINOR
141 return 0
142 fi
143 fi
144
145 return 1
146 }
147
148 export_partdevice() {
149 local var="$1" offset="$2"
150 local uevent MAJOR MINOR DEVNAME DEVTYPE
151
152 for uevent in /sys/class/block/*/uevent; do
153 . "$uevent"
154 if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
155 export "$var=$DEVNAME"
156 return 0
157 fi
158 done
159
160 return 1
161 }
162
163 get_partitions() { # <device> <filename>
164 local disk="$1"
165 local filename="$2"
166
167 if [ -b "$disk" -o -f "$disk" ]; then
168 v "Reading partition table from $filename..."
169
170 local magic="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x%04X"' "$disk")"
171 if [ "$magic" != 0xAA55 ]; then
172 v "Invalid partition table on $disk"
173 exit
174 fi
175
176 rm -f "/tmp/partmap.$filename"
177
178 local part
179 for part in 1 2 3 4; do
180 set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
181
182 local type="$(($1 % 256))"
183 local lba="$(($2))"
184 local num="$(($3))"
185
186 [ $type -gt 0 ] || continue
187
188 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
189 done
190 fi
191 }
192
193 jffs2_copy_config() {
194 if grep rootfs_data /proc/mtd >/dev/null; then
195 # squashfs+jffs2
196 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
197 else
198 # jffs2
199 mtd jffs2write "$CONF_TAR" rootfs
200 fi
201 }
202
203 # Flash firmware to MTD partition
204 #
205 # $(1): path to image
206 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
207 default_do_upgrade() {
208 sync
209 if [ "$SAVE_CONFIG" -eq 1 ]; then
210 get_image "$1" "$2" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
211 else
212 get_image "$1" "$2" | mtd write - "${PART_NAME:-image}"
213 fi
214 }
215
216 do_upgrade_stage2() {
217 v "Performing system upgrade..."
218 if [ -n "$do_upgrade" ]; then
219 eval "$do_upgrade"
220 elif type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
221 platform_do_upgrade "$IMAGE"
222 else
223 default_do_upgrade "$IMAGE"
224 fi
225
226 if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then
227 platform_copy_config
228 fi
229
230 v "Upgrade completed"
231 sleep 1
232
233 v "Rebooting system..."
234 umount -a
235 reboot -f
236 sleep 5
237 echo b 2>/dev/null >/proc/sysrq-trigger
238 }