5 ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
6 libs() { ldd $* | awk '{print $3}'; }
8 install_file() { # <file> [ <file> ... ]
10 dest="$RAM_ROOT/$file"
11 [ -f $file -a ! -f $dest ] && {
12 dir="$(dirname $dest)"
19 install_bin() { # <file> [ <symlink> ... ]
22 [ -x "$src" ] && files="$src $(libs $src)"
24 [ -e /lib/ld-linux.so.3 ] && {
25 install_file /lib/ld-linux.so.3
28 for link in "$@"; do {
29 dest="$RAM_ROOT/$link"
30 dir="$(dirname $dest)"
32 [ -f "$dest" ] || ln -s $src $dest
36 pivot() { # <new_root> <old_root>
37 mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
38 mkdir -p $1$2 $1/proc $1/dev $1/tmp $1/overlay && \
39 mount -o move /proc $1/proc && \
40 pivot_root $1 $1$2 || {
44 mount -o move $2/dev /dev
45 mount -o move $2/tmp /tmp
46 mount -o move $2/overlay /overlay 2>&-
50 run_ramfs() { # <command> [...]
51 install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount \
52 /sbin/pivot_root /usr/bin/wget /sbin/reboot /bin/sync /bin/dd \
53 /bin/grep /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" \
54 /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump \
55 /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc
58 for file in $RAMFS_COPY_BIN; do
61 install_file /etc/resolv.conf /etc/functions.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
63 pivot $RAM_ROOT /mnt || {
64 echo "Failed to switch over to ramfs. Please reboot."
68 mount -o remount,ro /mnt
71 grep /overlay /proc/mounts > /dev/null && {
72 mount -o remount,ro /overlay
76 # spawn a new shell from ramdisk to reduce the probability of cache issues
77 exec /bin/busybox ash -c "$*"
80 kill_remaining() { # [ <signal> ]
81 local sig="${1:-TERM}"
82 echo -n "Sending $sig to remaining processes ... "
85 for stat in /proc/[0-9]*/stat; do
86 local pid name state ppid rest
87 read pid name state ppid rest < $stat
88 name="${name#(}"; name="${name%)}"
91 read cmdline < /proc/$pid/cmdline
94 [ -n "$cmdline" ] || continue
97 # Skip essential services
98 *ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*ubusd*|*netifd*|*hostapd*|*wpa_supplicant*|*udhcpc*) : ;;
102 if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then
104 kill -$sig $pid 2>/dev/null
113 local arg="$1"; shift
120 local default="$1"; shift;
121 local answer="$default"
123 [ "$INTERACTIVE" -eq 1 ] && {
125 0) echo -n "$* (y/N): ";;
126 *) echo -n "$* (Y/n): ";;
132 *) answer="$default";;
139 [ "$VERBOSE" -ge 1 ] && echo "$@"
143 mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
146 get_image() { # <source> [ <command> ]
152 http://*|ftp://*) cmd="wget -O- -q";;
155 if [ -z "$conc" ]; then
156 local magic="$(eval $cmd $from | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
163 eval "$cmd $from ${conc:+| $conc}"
167 get_image "$@" | dd bs=2 count=1 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"'
171 get_image "$@" | dd bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
174 refresh_mtd_partitions() {
178 jffs2_copy_config() {
179 if grep rootfs_data /proc/mtd >/dev/null; then
181 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
184 mtd jffs2write "$CONF_TAR" rootfs
188 default_do_upgrade() {
190 if [ "$SAVE_CONFIG" -eq 1 -a -z "$USE_REFRESH" ]; then
191 get_image "$1" | mtd -j "$CONF_TAR" write - "${PART_NAME:-image}"
193 get_image "$1" | mtd write - "${PART_NAME:-image}"
198 v "Performing system upgrade..."
199 if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
200 platform_do_upgrade "$ARGV"
202 default_do_upgrade "$ARGV"
205 [ "$SAVE_CONFIG" -eq 1 -a -n "$USE_REFRESH" ] && {
206 v "Refreshing partitions"
207 if type 'platform_refresh_partitions' >/dev/null 2>/dev/null; then
208 platform_refresh_partitions
210 refresh_mtd_partitions
212 if type 'platform_copy_config' >/dev/null 2>/dev/null; then
218 v "Upgrade completed"
219 [ -n "$DELAY" ] && sleep "$DELAY"
220 ask_bool 1 "Reboot" && {
221 v "Rebooting system..."
224 echo b 2>/dev/null >/proc/sysrq-trigger