1
0

common.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #!/bin/sh
  2. RAM_ROOT=/tmp/root
  3. [ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
  4. libs() { ldd $* 2>/dev/null | sed -r 's/(.* => )?(.*) .*/\2/'; }
  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. supivot() { # <new_root> <old_root>
  29. /bin/mount | grep "on $1 type" 2>&- 1>&- || /bin/mount -o bind $1 $1
  30. mkdir -p $1$2 $1/proc $1/sys $1/dev $1/tmp $1/overlay && \
  31. /bin/mount -o noatime,move /proc $1/proc && \
  32. pivot_root $1 $1$2 || {
  33. /bin/umount -l $1 $1
  34. return 1
  35. }
  36. /bin/mount -o noatime,move $2/sys /sys
  37. /bin/mount -o noatime,move $2/dev /dev
  38. /bin/mount -o noatime,move $2/tmp /tmp
  39. /bin/mount -o noatime,move $2/overlay /overlay 2>&-
  40. return 0
  41. }
  42. run_ramfs() { # <command> [...]
  43. install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount \
  44. /sbin/pivot_root /sbin/reboot /bin/sync /bin/dd /bin/grep \
  45. /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" /bin/dd \
  46. /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump \
  47. /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc \
  48. /bin/cut /usr/bin/printf /bin/sync /bin/mkdir /bin/rmdir \
  49. /bin/rm /usr/bin/basename /bin/kill /bin/chmod /usr/bin/find \
  50. /bin/mknod
  51. install_bin /bin/uclient-fetch /bin/wget
  52. install_bin /sbin/mtd
  53. install_bin /sbin/mount_root
  54. install_bin /sbin/snapshot
  55. install_bin /sbin/snapshot_tool
  56. install_bin /usr/sbin/ubiupdatevol
  57. install_bin /usr/sbin/ubiattach
  58. install_bin /usr/sbin/ubiblock
  59. install_bin /usr/sbin/ubiformat
  60. install_bin /usr/sbin/ubidetach
  61. install_bin /usr/sbin/ubirsvol
  62. install_bin /usr/sbin/ubirmvol
  63. install_bin /usr/sbin/ubimkvol
  64. install_bin /usr/sbin/partx
  65. install_bin /usr/sbin/losetup
  66. install_bin /usr/sbin/mkfs.ext4
  67. for file in $RAMFS_COPY_BIN; do
  68. install_bin ${file//:/ }
  69. done
  70. install_file /etc/resolv.conf /lib/*.sh /lib/functions/*.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
  71. [ -L "/lib64" ] && ln -s /lib $RAM_ROOT/lib64
  72. supivot $RAM_ROOT /mnt || {
  73. echo "Failed to switch over to ramfs. Please reboot."
  74. exit 1
  75. }
  76. /bin/mount -o remount,ro /mnt
  77. /bin/umount -l /mnt
  78. grep /overlay /proc/mounts > /dev/null && {
  79. /bin/mount -o noatime,remount,ro /overlay
  80. /bin/umount -l /overlay
  81. }
  82. # spawn a new shell from ramdisk to reduce the probability of cache issues
  83. exec /bin/busybox ash -c "$*"
  84. }
  85. kill_remaining() { # [ <signal> ]
  86. local sig="${1:-TERM}"
  87. echo -n "Sending $sig to remaining processes ... "
  88. local my_pid=$$
  89. local my_ppid=$(cut -d' ' -f4 /proc/$my_pid/stat)
  90. local my_ppisupgraded=
  91. grep -q upgraded /proc/$my_ppid/cmdline >/dev/null && {
  92. local my_ppisupgraded=1
  93. }
  94. local stat
  95. for stat in /proc/[0-9]*/stat; do
  96. [ -f "$stat" ] || continue
  97. local pid name state ppid rest
  98. read pid name state ppid rest < $stat
  99. name="${name#(}"; name="${name%)}"
  100. local cmdline
  101. read cmdline < /proc/$pid/cmdline
  102. # Skip kernel threads
  103. [ -n "$cmdline" ] || continue
  104. if [ $$ -eq 1 ] || [ $my_ppid -eq 1 ] && [ -n "$my_ppisupgraded" ]; then
  105. # Running as init process, kill everything except me
  106. if [ $pid -ne $$ ] && [ $pid -ne $my_ppid ]; then
  107. echo -n "$name "
  108. kill -$sig $pid 2>/dev/null
  109. fi
  110. else
  111. case "$name" in
  112. # Skip essential services
  113. *procd*|*ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*|*nas*|*relayd*) : ;;
  114. # Killable process
  115. *)
  116. if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then
  117. echo -n "$name "
  118. kill -$sig $pid 2>/dev/null
  119. fi
  120. ;;
  121. esac
  122. fi
  123. done
  124. echo ""
  125. }
  126. run_hooks() {
  127. local arg="$1"; shift
  128. for func in "$@"; do
  129. eval "$func $arg"
  130. done
  131. }
  132. ask_bool() {
  133. local default="$1"; shift;
  134. local answer="$default"
  135. [ "$INTERACTIVE" -eq 1 ] && {
  136. case "$default" in
  137. 0) echo -n "$* (y/N): ";;
  138. *) echo -n "$* (Y/n): ";;
  139. esac
  140. read answer
  141. case "$answer" in
  142. y*) answer=1;;
  143. n*) answer=0;;
  144. *) answer="$default";;
  145. esac
  146. }
  147. [ "$answer" -gt 0 ]
  148. }
  149. v() {
  150. [ "$VERBOSE" -ge 1 ] && echo "$@"
  151. }
  152. rootfs_type() {
  153. /bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
  154. }
  155. get_image() { # <source> [ <command> ]
  156. local from="$1"
  157. local conc="$2"
  158. local cmd
  159. case "$from" in
  160. http://*|ftp://*) cmd="wget -O- -q";;
  161. *) cmd="cat";;
  162. esac
  163. if [ -z "$conc" ]; then
  164. local magic="$(eval $cmd \"$from\" 2>/dev/null | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
  165. case "$magic" in
  166. 1f8b) conc="zcat";;
  167. 425a) conc="bzcat";;
  168. esac
  169. fi
  170. eval "$cmd \"$from\" 2>/dev/null ${conc:+| $conc}"
  171. }
  172. get_magic_word() {
  173. (get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null
  174. }
  175. get_magic_long() {
  176. (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
  177. }
  178. export_bootdevice() {
  179. local cmdline uuid disk uevent
  180. local MAJOR MINOR DEVNAME DEVTYPE
  181. if read cmdline < /proc/cmdline; then
  182. case "$cmdline" in
  183. *block2mtd=*)
  184. disk="${cmdline##*block2mtd=}"
  185. disk="${disk%%,*}"
  186. ;;
  187. *root=*)
  188. disk="${cmdline##*root=}"
  189. disk="${disk%% *}"
  190. ;;
  191. esac
  192. case "$disk" in
  193. 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)
  194. uuid="${disk#PARTUUID=}"
  195. uuid="${uuid%-02}"
  196. for disk in $(find /dev -type b); do
  197. set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
  198. if [ "$4$3$2$1" = "$uuid" ]; then
  199. uevent="/sys/class/block/${disk##*/}/uevent"
  200. break
  201. fi
  202. done
  203. ;;
  204. /dev/*)
  205. uevent="/sys/class/block/${disk##*/}/uevent"
  206. ;;
  207. esac
  208. if [ -e "$uevent" ]; then
  209. . "$uevent"
  210. export BOOTDEV_MAJOR=$MAJOR
  211. export BOOTDEV_MINOR=$MINOR
  212. return 0
  213. fi
  214. fi
  215. return 1
  216. }
  217. export_partdevice() {
  218. local var="$1" offset="$2"
  219. local uevent MAJOR MINOR DEVNAME DEVTYPE
  220. for uevent in /sys/class/block/*/uevent; do
  221. . "$uevent"
  222. if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
  223. export "$var=$DEVNAME"
  224. return 0
  225. fi
  226. done
  227. return 1
  228. }
  229. get_partitions() { # <device> <filename>
  230. local disk="$1"
  231. local filename="$2"
  232. if [ -b "$disk" -o -f "$disk" ]; then
  233. v "Reading partition table from $filename..."
  234. local magic="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x%04X"' "$disk")"
  235. if [ "$magic" != 0xAA55 ]; then
  236. v "Invalid partition table on $disk"
  237. exit
  238. fi
  239. rm -f "/tmp/partmap.$filename"
  240. local part
  241. for part in 1 2 3 4; do
  242. set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
  243. local type="$(($1 % 256))"
  244. local lba="$(($2))"
  245. local num="$(($3))"
  246. [ $type -gt 0 ] || continue
  247. printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
  248. done
  249. fi
  250. }
  251. jffs2_copy_config() {
  252. if grep rootfs_data /proc/mtd >/dev/null; then
  253. # squashfs+jffs2
  254. mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
  255. else
  256. # jffs2
  257. mtd jffs2write "$CONF_TAR" rootfs
  258. fi
  259. }
  260. # Flash firmware to MTD partition
  261. #
  262. # $(1): path to image
  263. # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
  264. default_do_upgrade() {
  265. sync
  266. if [ "$SAVE_CONFIG" -eq 1 ]; then
  267. get_image "$1" "$2" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
  268. else
  269. get_image "$1" "$2" | mtd write - "${PART_NAME:-image}"
  270. fi
  271. }
  272. do_upgrade() {
  273. v "Performing system upgrade..."
  274. if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
  275. platform_do_upgrade "$ARGV"
  276. else
  277. default_do_upgrade "$ARGV"
  278. fi
  279. if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then
  280. platform_copy_config
  281. fi
  282. v "Upgrade completed"
  283. [ -n "$DELAY" ] && sleep "$DELAY"
  284. ask_bool 1 "Reboot" && {
  285. v "Rebooting system..."
  286. umount -a
  287. reboot -f
  288. sleep 5
  289. echo b 2>/dev/null >/proc/sysrq-trigger
  290. }
  291. }