5 [ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
6 libs() { ldd $* 2>/dev/null | sed -r 's/(.* => )?(.*) .*/\2/'; }
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)"
25 for link in "$@"; do {
26 dest="$RAM_ROOT/$link"
27 dir="$(dirname $dest)"
29 [ -f "$dest" ] || ln -s $src $dest
33 supivot() { # <new_root> <old_root>
34 /bin/mount | grep "on $1 type" 2>&- 1>&- || /bin/mount -o bind $1 $1
35 mkdir -p $1$2 $1/proc $1/sys $1/dev $1/tmp $1/overlay && \
36 /bin/mount -o noatime,move /proc $1/proc && \
37 pivot_root $1 $1$2 || {
42 /bin/mount -o noatime,move $2/sys /sys
43 /bin/mount -o noatime,move $2/dev /dev
44 /bin/mount -o noatime,move $2/tmp /tmp
45 /bin/mount -o noatime,move $2/overlay /overlay 2>&-
49 run_ramfs() { # <command> [...]
50 install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount \
51 /sbin/pivot_root /sbin/reboot /bin/sync /bin/dd /bin/grep \
52 /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" /bin/dd \
53 /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump \
54 /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc \
55 /bin/cut /usr/bin/printf /bin/sync /bin/mkdir /bin/rmdir \
56 /bin/rm /usr/bin/basename /bin/kill /bin/chmod /usr/bin/find \
59 install_bin /bin/uclient-fetch /bin/wget
61 install_bin /sbin/mount_root
62 install_bin /sbin/snapshot
63 install_bin /sbin/snapshot_tool
64 install_bin /usr/sbin/ubiupdatevol
65 install_bin /usr/sbin/ubiattach
66 install_bin /usr/sbin/ubiblock
67 install_bin /usr/sbin/ubiformat
68 install_bin /usr/sbin/ubidetach
69 install_bin /usr/sbin/ubirsvol
70 install_bin /usr/sbin/ubirmvol
71 install_bin /usr/sbin/ubimkvol
72 install_bin /usr/sbin/partx
73 install_bin /usr/sbin/losetup
74 install_bin /usr/sbin/mkfs.ext4
75 for file in $RAMFS_COPY_BIN; do
76 install_bin ${file//:/ }
78 install_file /etc/resolv.conf /lib/*.sh /lib/functions/*.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
80 [ -L "/lib64" ] && ln -s /lib $RAM_ROOT/lib64
82 supivot $RAM_ROOT /mnt || {
83 echo "Failed to switch over to ramfs. Please reboot."
87 /bin/mount -o remount,ro /mnt
90 grep /overlay /proc/mounts > /dev/null && {
91 /bin/mount -o noatime,remount,ro /overlay
92 /bin/umount -l /overlay
95 # spawn a new shell from ramdisk to reduce the probability of cache issues
96 exec /bin/busybox ash -c "$*"
99 kill_remaining() { # [ <signal> ]
100 local sig="${1:-TERM}"
101 echo -n "Sending $sig to remaining processes ... "
104 local my_ppid=$(cut -d' ' -f4 /proc/$my_pid/stat)
105 local my_ppisupgraded=
106 grep -q upgraded /proc/$my_ppid/cmdline >/dev/null && {
107 local my_ppisupgraded=1
111 for stat in /proc/[0-9]*/stat; do
112 [ -f "$stat" ] || continue
114 local pid name state ppid rest
115 read pid name state ppid rest < $stat
116 name="${name#(}"; name="${name%)}"
119 read cmdline < /proc/$pid/cmdline
121 # Skip kernel threads
122 [ -n "$cmdline" ] || continue
124 if [ $$ -eq 1 ] || [ $my_ppid -eq 1 ] && [ -n "$my_ppisupgraded" ]; then
125 # Running as init process, kill everything except me
126 if [ $pid -ne $$ ] && [ $pid -ne $my_ppid ]; then
128 kill -$sig $pid 2>/dev/null
132 # Skip essential services
133 *procd*|*ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*|*nas*|*relayd*) : ;;
137 if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then
139 kill -$sig $pid 2>/dev/null
149 local arg="$1"; shift
156 local default="$1"; shift;
157 local answer="$default"
159 [ "$INTERACTIVE" -eq 1 ] && {
161 0) echo -n "$* (y/N): ";;
162 *) echo -n "$* (Y/n): ";;
168 *) answer="$default";;
175 [ "$VERBOSE" -ge 1 ] && echo "$@"
179 /bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
182 get_image() { # <source> [ <command> ]
188 http://*|ftp://*) cmd="wget -O- -q";;
191 if [ -z "$conc" ]; then
192 local magic="$(eval $cmd \"$from\" 2>/dev/null | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
199 eval "$cmd \"$from\" 2>/dev/null ${conc:+| $conc}"
203 (get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null
207 (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
210 export_bootdevice() {
211 local cmdline uuid disk uevent
212 local MAJOR MINOR DEVNAME DEVTYPE
214 if read cmdline < /proc/cmdline; then
217 disk="${cmdline##*block2mtd=}"
221 disk="${cmdline##*root=}"
227 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)
228 uuid="${disk#PARTUUID=}"
230 for disk in $(find /dev -type b); do
231 set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
232 if [ "$4$3$2$1" = "$uuid" ]; then
233 uevent="/sys/class/block/${disk##*/}/uevent"
239 uevent="/sys/class/block/${disk##*/}/uevent"
243 if [ -e "$uevent" ]; then
246 export BOOTDEV_MAJOR=$MAJOR
247 export BOOTDEV_MINOR=$MINOR
255 export_partdevice() {
256 local var="$1" offset="$2"
257 local uevent MAJOR MINOR DEVNAME DEVTYPE
259 for uevent in /sys/class/block/*/uevent; do
261 if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
262 export "$var=$DEVNAME"
270 get_partitions() { # <device> <filename>
274 if [ -b "$disk" -o -f "$disk" ]; then
275 v "Reading partition table from $filename..."
277 local magic="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x%04X"' "$disk")"
278 if [ "$magic" != 0xAA55 ]; then
279 v "Invalid partition table on $disk"
283 rm -f "/tmp/partmap.$filename"
286 for part in 1 2 3 4; do
287 set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
289 local type="$(($1 % 256))"
293 [ $type -gt 0 ] || continue
295 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
300 jffs2_copy_config() {
301 if grep rootfs_data /proc/mtd >/dev/null; then
303 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
306 mtd jffs2write "$CONF_TAR" rootfs
310 # Flash firmware to MTD partition
312 # $(1): path to image
313 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
314 default_do_upgrade() {
316 if [ "$SAVE_CONFIG" -eq 1 ]; then
317 get_image "$1" "$2" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
319 get_image "$1" "$2" | mtd write - "${PART_NAME:-image}"
324 v "Performing system upgrade..."
325 if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
326 platform_do_upgrade "$ARGV"
328 default_do_upgrade "$ARGV"
331 if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then
335 v "Upgrade completed"
336 [ -n "$DELAY" ] && sleep "$DELAY"
337 ask_bool 1 "Reboot" && {
338 v "Rebooting system..."
342 echo b 2>/dev/null >/proc/sysrq-trigger