common.sh 7.1 KB

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