common.sh 3.8 KB

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