common.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 line
  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. while read line; do
  210. export -n "$line"
  211. done < "$uevent"
  212. export BOOTDEV_MAJOR=$MAJOR
  213. export BOOTDEV_MINOR=$MINOR
  214. return 0
  215. fi
  216. fi
  217. return 1
  218. }
  219. export_partdevice() {
  220. local var="$1" offset="$2"
  221. local uevent line MAJOR MINOR DEVNAME DEVTYPE
  222. for uevent in /sys/class/block/*/uevent; do
  223. while read line; do
  224. export -n "$line"
  225. done < "$uevent"
  226. if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
  227. export "$var=$DEVNAME"
  228. return 0
  229. fi
  230. done
  231. return 1
  232. }
  233. hex_le32_to_cpu() {
  234. [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" == "3031" ] && {
  235. echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
  236. return
  237. }
  238. echo "$@"
  239. }
  240. get_partitions() { # <device> <filename>
  241. local disk="$1"
  242. local filename="$2"
  243. if [ -b "$disk" -o -f "$disk" ]; then
  244. v "Reading partition table from $filename..."
  245. local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null)
  246. if [ "$magic" != $'\x55\xAA' ]; then
  247. v "Invalid partition table on $disk"
  248. exit
  249. fi
  250. rm -f "/tmp/partmap.$filename"
  251. local part
  252. for part in 1 2 3 4; do
  253. set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
  254. local type="$(( $(hex_le32_to_cpu $1) % 256))"
  255. local lba="$(( $(hex_le32_to_cpu $2) ))"
  256. local num="$(( $(hex_le32_to_cpu $3) ))"
  257. [ $type -gt 0 ] || continue
  258. printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
  259. done
  260. fi
  261. }
  262. jffs2_copy_config() {
  263. if grep rootfs_data /proc/mtd >/dev/null; then
  264. # squashfs+jffs2
  265. mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
  266. else
  267. # jffs2
  268. mtd jffs2write "$CONF_TAR" rootfs
  269. fi
  270. }
  271. # Flash firmware to MTD partition
  272. #
  273. # $(1): path to image
  274. # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
  275. default_do_upgrade() {
  276. sync
  277. if [ "$SAVE_CONFIG" -eq 1 ]; then
  278. get_image "$1" "$2" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
  279. else
  280. get_image "$1" "$2" | mtd write - "${PART_NAME:-image}"
  281. fi
  282. }
  283. do_upgrade() {
  284. v "Performing system upgrade..."
  285. if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
  286. platform_do_upgrade "$ARGV"
  287. else
  288. default_do_upgrade "$ARGV"
  289. fi
  290. if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then
  291. platform_copy_config
  292. fi
  293. v "Upgrade completed"
  294. [ -n "$DELAY" ] && sleep "$DELAY"
  295. ask_bool 1 "Reboot" && {
  296. v "Rebooting system..."
  297. umount -a
  298. reboot -f
  299. sleep 5
  300. echo b 2>/dev/null >/proc/sysrq-trigger
  301. }
  302. }