[package] base-files: Detect and decompressed gzipped images automatically when flash...
[openwrt.git] / package / base-files / files / lib / upgrade / common.sh
1 #!/bin/sh
2
3 RAM_ROOT=/tmp/root
4
5 ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
6 libs() { ldd $* | awk '{print $3}'; }
7
8 install_file() { # <file> [ <file> ... ]
9         for file in "$@"; do
10                 dest="$RAM_ROOT/$file"
11                 [ -f $file -a ! -f $dest ] && {
12                         dir="$(dirname $dest)"
13                         mkdir -p "$dir"
14                         cp $file $dest
15                 }
16         done
17 }
18
19 install_bin() { # <file> [ <symlink> ... ]
20         src=$1
21         files=$1
22         [ -x "$src" ] && files="$src $(libs $src)"
23         install_file $files
24         shift
25         for link in "$@"; do {
26                 dest="$RAM_ROOT/$link"
27                 dir="$(dirname $dest)"
28                 mkdir -p "$dir"
29                 [ -f "$dest" ] || ln -s $src $dest
30         }; done
31 }
32
33 pivot() { # <new_root> <old_root>
34         mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
35         mkdir -p $1$2 $1/proc $1/dev $1/tmp $1/jffs && \
36         mount -o move /proc $1/proc && \
37         pivot_root $1 $1$2 || {
38         umount $1 $1
39                 return 1
40         }
41         mount -o move $2/dev /dev
42         mount -o move $2/tmp /tmp
43         mount -o move $2/jffs /jffs 2>&-
44         return 0
45 }
46
47 run_ramfs() { # <command> [...]
48         install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount /sbin/pivot_root /usr/bin/wget /sbin/reboot /bin/sync /bin/dd /bin/grep /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump /bin/sleep /bin/zcat
49         install_bin /sbin/mtd
50         for file in $RAMFS_COPY_BIN; do
51                 install_bin $file
52         done
53         install_file /etc/resolv.conf /etc/functions.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
54
55         pivot $RAM_ROOT /mnt || {
56                 echo "Failed to switch over to ramfs. Please reboot."
57                 exit 1
58         }
59
60         mount -o remount,ro /mnt
61         umount -l /mnt
62
63         grep /jffs /proc/mounts > /dev/null && {
64                 mount -o remount,ro /jffs
65                 umount -l /jffs
66         }
67
68         # spawn a new shell from ramdisk to reduce the probability of cache issues
69         exec /bin/busybox ash -c "$*"
70 }
71
72 run_hooks() {
73         local arg="$1"; shift
74         for func in "$@"; do
75                 eval "$func $arg"
76         done
77 }
78
79 ask_bool() {
80         local default="$1"; shift;
81         local answer="$default"
82
83         [ "$INTERACTIVE" -eq 1 ] && {
84                 case "$default" in
85                         0) echo -n "$* (y/N): ";;
86                         *) echo -n "$* (Y/n): ";;
87                 esac
88                 read answer
89                 case "$answer" in
90                         y*) answer=1;;
91                         n*) answer=0;;
92                         *) answer="$default";;
93                 esac
94         }
95         [ "$answer" -gt 0 ]
96 }
97
98 v() {
99         [ "$VERBOSE" -ge 1 ] && echo "$@"
100 }
101
102 rootfs_type() {
103         mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
104 }
105
106 get_image() { # <source> [ <command> ]
107         local from="$1"
108         local cmd="$2"
109         local conc
110
111         if [ -z "$cmd" ]; then
112                 case "$from" in
113                         http://*|ftp://*) cmd="wget -O- -q";;
114                         *) cmd="cat";;
115                 esac
116                 local magic="$(eval $cmd $from | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
117                 case "$magic" in
118                         1f8b) conc="| zcat";;
119                 esac
120         fi
121
122         eval "$cmd $from $conc"
123 }
124
125 get_magic_word() {
126         get_image "$1" | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"'
127 }
128
129 refresh_mtd_partitions() {
130         mtd refresh rootfs
131 }
132
133 jffs2_copy_config() {
134         if grep rootfs_data /proc/mtd >/dev/null; then
135                 # squashfs+jffs2
136                 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
137         else
138                 # jffs2
139                 mtd jffs2write "$CONF_TAR" rootfs
140         fi
141 }
142
143 default_do_upgrade() {
144         if [ "$SAVE_CONFIG" -eq 1 -a -z "$USE_REFRESH" ]; then
145                 get_image "$1" | mtd -j "$CONF_TAR" write - "${PART_NAME:-image}"
146         else
147                 get_image "$1" | mtd write - "${PART_NAME:-image}"
148         fi
149         sync
150 }
151
152 do_upgrade() {
153         v "Performing system upgrade..."
154         if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
155                 platform_do_upgrade "$ARGV"
156         else
157                 default_do_upgrade "$ARGV"
158         fi
159         
160         [ "$SAVE_CONFIG" -eq 1 -a -n "$USE_REFRESH" ] && {
161                 v "Refreshing partitions"
162                 if type 'platform_refresh_partitions' >/dev/null 2>/dev/null; then
163                         platform_refresh_partitions
164                 else
165                         refresh_mtd_partitions
166                 fi
167                 if type 'platform_copy_config' >/dev/null 2>/dev/null; then
168                         platform_copy_config
169                 else
170                         jffs2_copy_config
171                 fi
172         }
173         v "Upgrade completed"
174         [ -n "$DELAY" ] && sleep "$DELAY"
175         ask_bool 1 "Reboot" && {
176                 v "Rebooting system..."
177                 echo b 2>/dev/null >/proc/sysrq-trigger
178                 reboot
179         }
180 }