common.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #!/bin/sh
  2. RAM_ROOT=/tmp/root
  3. export BACKUP_FILE=sysupgrade.tgz # file extracted by preinit
  4. [ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
  5. libs() { ldd $* 2>/dev/null | sed -r 's/(.* => )?(.*) .*/\2/'; }
  6. install_file() { # <file> [ <file> ... ]
  7. local target dest dir
  8. for file in "$@"; do
  9. if [ -L "$file" ]; then
  10. target="$(readlink -f "$file")"
  11. dest="$RAM_ROOT/$file"
  12. [ ! -f "$dest" ] && {
  13. dir="$(dirname "$dest")"
  14. mkdir -p "$dir"
  15. ln -s "$target" "$dest"
  16. }
  17. file="$target"
  18. fi
  19. dest="$RAM_ROOT/$file"
  20. [ -f "$file" -a ! -f "$dest" ] && {
  21. dir="$(dirname "$dest")"
  22. mkdir -p "$dir"
  23. cp "$file" "$dest"
  24. }
  25. done
  26. }
  27. install_bin() {
  28. local src files
  29. src=$1
  30. files=$1
  31. [ -x "$src" ] && files="$src $(libs $src)"
  32. install_file $files
  33. }
  34. run_hooks() {
  35. local arg="$1"; shift
  36. for func in "$@"; do
  37. eval "$func $arg"
  38. done
  39. }
  40. ask_bool() {
  41. local default="$1"; shift;
  42. local answer="$default"
  43. [ "$INTERACTIVE" -eq 1 ] && {
  44. case "$default" in
  45. 0) echo -n "$* (y/N): ";;
  46. *) echo -n "$* (Y/n): ";;
  47. esac
  48. read answer
  49. case "$answer" in
  50. y*) answer=1;;
  51. n*) answer=0;;
  52. *) answer="$default";;
  53. esac
  54. }
  55. [ "$answer" -gt 0 ]
  56. }
  57. v() {
  58. [ "$VERBOSE" -ge 1 ] && echo "$@"
  59. }
  60. json_string() {
  61. local v="$1"
  62. v="${v//\\/\\\\}"
  63. v="${v//\"/\\\"}"
  64. echo "\"$v\""
  65. }
  66. rootfs_type() {
  67. /bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
  68. }
  69. get_image() { # <source> [ <command> ]
  70. local from="$1"
  71. local cmd="$2"
  72. if [ -z "$cmd" ]; then
  73. local magic="$(dd if="$from" bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
  74. case "$magic" in
  75. 1f8b) cmd="zcat";;
  76. 425a) cmd="bzcat";;
  77. *) cmd="cat";;
  78. esac
  79. fi
  80. cat "$from" 2>/dev/null | $cmd
  81. }
  82. get_magic_word() {
  83. (get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null
  84. }
  85. get_magic_long() {
  86. (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
  87. }
  88. get_magic_gpt() {
  89. (get_image "$@" | dd bs=8 count=1 skip=64) 2>/dev/null
  90. }
  91. get_magic_vfat() {
  92. (get_image "$@" | dd bs=1 count=3 skip=54) 2>/dev/null
  93. }
  94. part_magic_efi() {
  95. local magic=$(get_magic_gpt "$@")
  96. [ "$magic" = "EFI PART" ]
  97. }
  98. part_magic_fat() {
  99. local magic=$(get_magic_vfat "$@")
  100. [ "$magic" = "FAT" ]
  101. }
  102. export_bootdevice() {
  103. local cmdline bootdisk rootpart uuid blockdev uevent line class
  104. local MAJOR MINOR DEVNAME DEVTYPE
  105. if read cmdline < /proc/cmdline; then
  106. case "$cmdline" in
  107. *block2mtd=*)
  108. bootdisk="${cmdline##*block2mtd=}"
  109. bootdisk="${bootdisk%%,*}"
  110. ;;
  111. *root=*)
  112. rootpart="${cmdline##*root=}"
  113. rootpart="${rootpart%% *}"
  114. ;;
  115. esac
  116. case "$bootdisk" in
  117. /dev/*)
  118. uevent="/sys/class/block/${bootdisk##*/}/uevent"
  119. ;;
  120. esac
  121. case "$rootpart" in
  122. 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]-[a-f0-9][a-f0-9])
  123. uuid="${rootpart#PARTUUID=}"
  124. uuid="${uuid%-[a-f0-9][a-f0-9]}"
  125. for blockdev in $(find /dev -type b); do
  126. set -- $(dd if=$blockdev bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
  127. if [ "$4$3$2$1" = "$uuid" ]; then
  128. uevent="/sys/class/block/${blockdev##*/}/uevent"
  129. break
  130. fi
  131. done
  132. ;;
  133. PARTUUID=????????-????-????-????-??????????02)
  134. uuid="${rootpart#PARTUUID=}"
  135. uuid="${uuid%02}00"
  136. for disk in $(find /dev -type b); do
  137. set -- $(dd if=$disk bs=1 skip=568 count=16 2>/dev/null | hexdump -v -e '8/1 "%02x "" "2/1 "%02x""-"6/1 "%02x"')
  138. if [ "$4$3$2$1-$6$5-$8$7-$9" = "$uuid" ]; then
  139. uevent="/sys/class/block/${disk##*/}/uevent"
  140. break
  141. fi
  142. done
  143. ;;
  144. /dev/*)
  145. uevent="/sys/class/block/${rootpart##*/}/../uevent"
  146. ;;
  147. 0x[a-f0-9][a-f0-9][a-f0-9] | 0x[a-f0-9][a-f0-9][a-f0-9][a-f0-9] | \
  148. [a-f0-9][a-f0-9][a-f0-9] | [a-f0-9][a-f0-9][a-f0-9][a-f0-9])
  149. rootpart=0x${rootpart#0x}
  150. for class in /sys/class/block/*; do
  151. while read line; do
  152. export -n "$line"
  153. done < "$class/uevent"
  154. if [ $((rootpart/256)) = $MAJOR -a $((rootpart%256)) = $MINOR ]; then
  155. uevent="$class/../uevent"
  156. fi
  157. done
  158. ;;
  159. esac
  160. if [ -e "$uevent" ]; then
  161. while read line; do
  162. export -n "$line"
  163. done < "$uevent"
  164. export BOOTDEV_MAJOR=$MAJOR
  165. export BOOTDEV_MINOR=$MINOR
  166. return 0
  167. fi
  168. fi
  169. return 1
  170. }
  171. export_partdevice() {
  172. local var="$1" offset="$2"
  173. local uevent line MAJOR MINOR DEVNAME DEVTYPE
  174. for uevent in /sys/class/block/*/uevent; do
  175. while read line; do
  176. export -n "$line"
  177. done < "$uevent"
  178. if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
  179. export "$var=$DEVNAME"
  180. return 0
  181. fi
  182. done
  183. return 1
  184. }
  185. hex_le32_to_cpu() {
  186. [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" = "3031" ] && {
  187. echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
  188. return
  189. }
  190. echo "$@"
  191. }
  192. get_partitions() { # <device> <filename>
  193. local disk="$1"
  194. local filename="$2"
  195. if [ -b "$disk" -o -f "$disk" ]; then
  196. v "Reading partition table from $filename..."
  197. local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null)
  198. if [ "$magic" != $'\x55\xAA' ]; then
  199. v "Invalid partition table on $disk"
  200. exit
  201. fi
  202. rm -f "/tmp/partmap.$filename"
  203. local part
  204. part_magic_efi "$disk" && {
  205. #export_partdevice will fail when partition number is greater than 15, as
  206. #the partition major device number is not equal to the disk major device number
  207. for part in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
  208. set -- $(hexdump -v -n 48 -s "$((0x380 + $part * 0x80))" -e '4/4 "%08x"" "4/4 "%08x"" "4/4 "0x%08X "' "$disk")
  209. local type="$1"
  210. local lba="$(( $(hex_le32_to_cpu $4) * 0x100000000 + $(hex_le32_to_cpu $3) ))"
  211. local end="$(( $(hex_le32_to_cpu $6) * 0x100000000 + $(hex_le32_to_cpu $5) ))"
  212. local num="$(( $end - $lba ))"
  213. [ "$type" = "00000000000000000000000000000000" ] && continue
  214. printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
  215. done
  216. } || {
  217. for part in 1 2 3 4; do
  218. set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
  219. local type="$(( $(hex_le32_to_cpu $1) % 256))"
  220. local lba="$(( $(hex_le32_to_cpu $2) ))"
  221. local num="$(( $(hex_le32_to_cpu $3) ))"
  222. [ $type -gt 0 ] || continue
  223. printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
  224. done
  225. }
  226. fi
  227. }
  228. indicate_upgrade() {
  229. . /etc/diag.sh
  230. set_state upgrade
  231. }
  232. # Flash firmware to MTD partition
  233. #
  234. # $(1): path to image
  235. # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
  236. default_do_upgrade() {
  237. sync
  238. if [ -n "$UPGRADE_BACKUP" ]; then
  239. get_image "$1" "$2" | mtd $MTD_ARGS $MTD_CONFIG_ARGS -j "$UPGRADE_BACKUP" write - "${PART_NAME:-image}"
  240. else
  241. get_image "$1" "$2" | mtd $MTD_ARGS write - "${PART_NAME:-image}"
  242. fi
  243. [ $? -ne 0 ] && exit 1
  244. }