base-files: drop support for NAND upgrade in platform_pre_upgrade()
[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 cmd="$2"
82
83 if [ -z "$cmd" ]; 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) cmd="zcat";;
87 425a) cmd="bzcat";;
88 *) cmd="cat";;
89 esac
90 fi
91
92 cat "$from" 2>/dev/null | $cmd
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 bootdisk rootpart uuid blockdev uevent line
105 local MAJOR MINOR DEVNAME DEVTYPE
106
107 if read cmdline < /proc/cmdline; then
108 case "$cmdline" in
109 *block2mtd=*)
110 bootdisk="${cmdline##*block2mtd=}"
111 bootdisk="${bootdisk%%,*}"
112 ;;
113 *root=*)
114 rootpart="${cmdline##*root=}"
115 rootpart="${rootpart%% *}"
116 ;;
117 esac
118
119 case "$bootdisk" in
120 /dev/*)
121 uevent="/sys/class/block/${bootdisk##*/}/uevent"
122 ;;
123 esac
124
125 case "$rootpart" in
126 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])
127 uuid="${rootpart#PARTUUID=}"
128 uuid="${uuid%-[a-f0-9][a-f0-9]}"
129 for blockdev in $(find /dev -type b); do
130 set -- $(dd if=$blockdev bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
131 if [ "$4$3$2$1" = "$uuid" ]; then
132 uevent="/sys/class/block/${blockdev##*/}/uevent"
133 break
134 fi
135 done
136 ;;
137 /dev/*)
138 uevent="/sys/class/block/${rootpart##*/}/../uevent"
139 ;;
140 esac
141
142 if [ -e "$uevent" ]; then
143 while read line; do
144 export -n "$line"
145 done < "$uevent"
146 export BOOTDEV_MAJOR=$MAJOR
147 export BOOTDEV_MINOR=$MINOR
148 return 0
149 fi
150 fi
151
152 return 1
153 }
154
155 export_partdevice() {
156 local var="$1" offset="$2"
157 local uevent line MAJOR MINOR DEVNAME DEVTYPE
158
159 for uevent in /sys/class/block/*/uevent; do
160 while read line; do
161 export -n "$line"
162 done < "$uevent"
163 if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
164 export "$var=$DEVNAME"
165 return 0
166 fi
167 done
168
169 return 1
170 }
171
172 hex_le32_to_cpu() {
173 [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" = "3031" ] && {
174 echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
175 return
176 }
177 echo "$@"
178 }
179
180 get_partitions() { # <device> <filename>
181 local disk="$1"
182 local filename="$2"
183
184 if [ -b "$disk" -o -f "$disk" ]; then
185 v "Reading partition table from $filename..."
186
187 local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null)
188 if [ "$magic" != $'\x55\xAA' ]; then
189 v "Invalid partition table on $disk"
190 exit
191 fi
192
193 rm -f "/tmp/partmap.$filename"
194
195 local part
196 for part in 1 2 3 4; do
197 set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
198
199 local type="$(( $(hex_le32_to_cpu $1) % 256))"
200 local lba="$(( $(hex_le32_to_cpu $2) ))"
201 local num="$(( $(hex_le32_to_cpu $3) ))"
202
203 [ $type -gt 0 ] || continue
204
205 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
206 done
207 fi
208 }
209
210 indicate_upgrade() {
211 . /etc/diag.sh
212 set_state upgrade
213 }
214
215 # Flash firmware to MTD partition
216 #
217 # $(1): path to image
218 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
219 default_do_upgrade() {
220 sync
221 if [ "$SAVE_CONFIG" -eq 1 ]; then
222 get_image "$1" "$2" | mtd $MTD_ARGS $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
223 else
224 get_image "$1" "$2" | mtd $MTD_ARGS write - "${PART_NAME:-image}"
225 fi
226 [ $? -ne 0 ] && exit 1
227 }
228
229 do_upgrade_stage2() {
230 v "Performing system upgrade..."
231 if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
232 platform_do_upgrade "$IMAGE"
233 else
234 default_do_upgrade "$IMAGE"
235 fi
236
237 if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then
238 platform_copy_config
239 fi
240
241 v "Upgrade completed"
242 sleep 1
243
244 v "Rebooting system..."
245 umount -a
246 reboot -f
247 sleep 5
248 echo b 2>/dev/null >/proc/sysrq-trigger
249 }